1*0a6a1f1dSLionel Sambuc /* $NetBSD: ext2fs_subr.c,v 1.31 2015/03/28 19:24:04 maxv Exp $ */
2d65f6f70SBen Gras
3d65f6f70SBen Gras /*
4d65f6f70SBen Gras * Copyright (c) 1982, 1986, 1989, 1993
5d65f6f70SBen Gras * The Regents of the University of California. All rights reserved.
6d65f6f70SBen Gras *
7d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
8d65f6f70SBen Gras * modification, are permitted provided that the following conditions
9d65f6f70SBen Gras * are met:
10d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
11d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
12d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
14d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
15d65f6f70SBen Gras * 3. Neither the name of the University nor the names of its contributors
16d65f6f70SBen Gras * may be used to endorse or promote products derived from this software
17d65f6f70SBen Gras * without specific prior written permission.
18d65f6f70SBen Gras *
19d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20d65f6f70SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d65f6f70SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d65f6f70SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23d65f6f70SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d65f6f70SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d65f6f70SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d65f6f70SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d65f6f70SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d65f6f70SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d65f6f70SBen Gras * SUCH DAMAGE.
30d65f6f70SBen Gras *
31d65f6f70SBen Gras * @(#)ffs_subr.c 8.2 (Berkeley) 9/21/93
32d65f6f70SBen Gras * Modified for ext2fs by Manuel Bouyer.
33d65f6f70SBen Gras */
34d65f6f70SBen Gras
35d65f6f70SBen Gras /*
36d65f6f70SBen Gras * Copyright (c) 1997 Manuel Bouyer.
37d65f6f70SBen Gras *
38d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
39d65f6f70SBen Gras * modification, are permitted provided that the following conditions
40d65f6f70SBen Gras * are met:
41d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
42d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
43d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
44d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
45d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
46d65f6f70SBen Gras *
47d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48d65f6f70SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49d65f6f70SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50d65f6f70SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51d65f6f70SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52d65f6f70SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53d65f6f70SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54d65f6f70SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55d65f6f70SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56d65f6f70SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57d65f6f70SBen Gras *
58d65f6f70SBen Gras * @(#)ffs_subr.c 8.2 (Berkeley) 9/21/93
59d65f6f70SBen Gras * Modified for ext2fs by Manuel Bouyer.
60d65f6f70SBen Gras */
61d65f6f70SBen Gras
62d65f6f70SBen Gras #include <sys/cdefs.h>
63*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ext2fs_subr.c,v 1.31 2015/03/28 19:24:04 maxv Exp $");
64d65f6f70SBen Gras
65d65f6f70SBen Gras #include <sys/param.h>
66d65f6f70SBen Gras #include <sys/systm.h>
67d65f6f70SBen Gras #include <sys/vnode.h>
68d65f6f70SBen Gras #include <sys/buf.h>
69d65f6f70SBen Gras #include <sys/inttypes.h>
70d65f6f70SBen Gras #include <sys/kauth.h>
71d65f6f70SBen Gras
72d65f6f70SBen Gras #include <ufs/ufs/inode.h>
73d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs.h>
74d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs_extern.h>
75d65f6f70SBen Gras
76d65f6f70SBen Gras /*
77d65f6f70SBen Gras * Return buffer with the contents of block "offset" from the beginning of
78d65f6f70SBen Gras * directory "ip". If "res" is non-zero, fill it in with a pointer to the
79d65f6f70SBen Gras * remaining space in the directory.
80d65f6f70SBen Gras */
81d65f6f70SBen Gras int
ext2fs_blkatoff(struct vnode * vp,off_t offset,char ** res,struct buf ** bpp)82d65f6f70SBen Gras ext2fs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
83d65f6f70SBen Gras {
84d65f6f70SBen Gras struct inode *ip;
85d65f6f70SBen Gras struct m_ext2fs *fs;
86d65f6f70SBen Gras struct buf *bp;
87d65f6f70SBen Gras daddr_t lbn;
88d65f6f70SBen Gras int error;
89d65f6f70SBen Gras
90d65f6f70SBen Gras ip = VTOI(vp);
91d65f6f70SBen Gras fs = ip->i_e2fs;
9284d9c625SLionel Sambuc lbn = ext2_lblkno(fs, offset);
93d65f6f70SBen Gras
94d65f6f70SBen Gras *bpp = NULL;
95*0a6a1f1dSLionel Sambuc if ((error = bread(vp, lbn, fs->e2fs_bsize, 0, &bp)) != 0) {
96d65f6f70SBen Gras return (error);
97d65f6f70SBen Gras }
98d65f6f70SBen Gras if (res)
9984d9c625SLionel Sambuc *res = (char *)bp->b_data + ext2_blkoff(fs, offset);
100d65f6f70SBen Gras *bpp = bp;
101d65f6f70SBen Gras return (0);
102d65f6f70SBen Gras }
103d65f6f70SBen Gras
104d65f6f70SBen Gras void
ext2fs_itimes(struct inode * ip,const struct timespec * acc,const struct timespec * mod,const struct timespec * cre)105d65f6f70SBen Gras ext2fs_itimes(struct inode *ip, const struct timespec *acc,
106d65f6f70SBen Gras const struct timespec *mod, const struct timespec *cre)
107d65f6f70SBen Gras {
108d65f6f70SBen Gras struct timespec now;
109d65f6f70SBen Gras
110d65f6f70SBen Gras if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
111d65f6f70SBen Gras return;
112d65f6f70SBen Gras }
113d65f6f70SBen Gras
114d65f6f70SBen Gras vfs_timestamp(&now);
115d65f6f70SBen Gras if (ip->i_flag & IN_ACCESS) {
116d65f6f70SBen Gras if (acc == NULL)
117d65f6f70SBen Gras acc = &now;
118d65f6f70SBen Gras ip->i_e2fs_atime = acc->tv_sec;
119d65f6f70SBen Gras }
120d65f6f70SBen Gras if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
121d65f6f70SBen Gras if (mod == NULL)
122d65f6f70SBen Gras mod = &now;
123d65f6f70SBen Gras ip->i_e2fs_mtime = mod->tv_sec;
124d65f6f70SBen Gras ip->i_modrev++;
125d65f6f70SBen Gras }
126d65f6f70SBen Gras if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
127d65f6f70SBen Gras if (cre == NULL)
128d65f6f70SBen Gras cre = &now;
129d65f6f70SBen Gras ip->i_e2fs_ctime = cre->tv_sec;
130d65f6f70SBen Gras }
131d65f6f70SBen Gras if (ip->i_flag & (IN_ACCESS | IN_MODIFY))
132d65f6f70SBen Gras ip->i_flag |= IN_ACCESSED;
133d65f6f70SBen Gras if (ip->i_flag & (IN_UPDATE | IN_CHANGE))
134d65f6f70SBen Gras ip->i_flag |= IN_MODIFIED;
135d65f6f70SBen Gras ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
136d65f6f70SBen Gras }
137