1cfe60390STomohiro Kusumi /*-
2cfe60390STomohiro Kusumi * modified for EXT2FS support in Lites 1.1
3cfe60390STomohiro Kusumi *
4cfe60390STomohiro Kusumi * Aug 1995, Godmar Back (gback@cs.utah.edu)
5cfe60390STomohiro Kusumi * University of Utah, Department of Computer Science
6cfe60390STomohiro Kusumi */
7cfe60390STomohiro Kusumi /*-
8cfe60390STomohiro Kusumi * SPDX-License-Identifier: BSD-3-Clause
9cfe60390STomohiro Kusumi *
10cfe60390STomohiro Kusumi * Copyright (c) 1989, 1991, 1993, 1994
11cfe60390STomohiro Kusumi * The Regents of the University of California. All rights reserved.
12cfe60390STomohiro Kusumi *
13cfe60390STomohiro Kusumi * Redistribution and use in source and binary forms, with or without
14cfe60390STomohiro Kusumi * modification, are permitted provided that the following conditions
15cfe60390STomohiro Kusumi * are met:
16cfe60390STomohiro Kusumi * 1. Redistributions of source code must retain the above copyright
17cfe60390STomohiro Kusumi * notice, this list of conditions and the following disclaimer.
18cfe60390STomohiro Kusumi * 2. Redistributions in binary form must reproduce the above copyright
19cfe60390STomohiro Kusumi * notice, this list of conditions and the following disclaimer in the
20cfe60390STomohiro Kusumi * documentation and/or other materials provided with the distribution.
21cfe60390STomohiro Kusumi * 3. Neither the name of the University nor the names of its contributors
22cfe60390STomohiro Kusumi * may be used to endorse or promote products derived from this software
23cfe60390STomohiro Kusumi * without specific prior written permission.
24cfe60390STomohiro Kusumi *
25cfe60390STomohiro Kusumi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26cfe60390STomohiro Kusumi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27cfe60390STomohiro Kusumi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28cfe60390STomohiro Kusumi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29cfe60390STomohiro Kusumi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30cfe60390STomohiro Kusumi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31cfe60390STomohiro Kusumi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32cfe60390STomohiro Kusumi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33cfe60390STomohiro Kusumi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34cfe60390STomohiro Kusumi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35cfe60390STomohiro Kusumi * SUCH DAMAGE.
36cfe60390STomohiro Kusumi *
37cfe60390STomohiro Kusumi * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94
38cfe60390STomohiro Kusumi * $FreeBSD$
39cfe60390STomohiro Kusumi */
40cfe60390STomohiro Kusumi
41cfe60390STomohiro Kusumi #include <sys/param.h>
42cfe60390STomohiro Kusumi #include <sys/systm.h>
43cfe60390STomohiro Kusumi #include <sys/namei.h>
44*2b3f93eaSMatthew Dillon #include <sys/caps.h>
45cfe60390STomohiro Kusumi #include <sys/proc.h>
46cfe60390STomohiro Kusumi #include <sys/kernel.h>
47cfe60390STomohiro Kusumi #include <sys/vnode.h>
48cfe60390STomohiro Kusumi #include <sys/mount.h>
49cfe60390STomohiro Kusumi #include <sys/bio.h>
50cfe60390STomohiro Kusumi #include <sys/buf2.h>
51cfe60390STomohiro Kusumi #include <sys/conf.h>
52cfe60390STomohiro Kusumi #include <sys/endian.h>
53cfe60390STomohiro Kusumi #include <sys/fcntl.h>
54cfe60390STomohiro Kusumi #include <sys/malloc.h>
55cfe60390STomohiro Kusumi #include <sys/stat.h>
56cfe60390STomohiro Kusumi #include <sys/mutex2.h>
57cfe60390STomohiro Kusumi #include <sys/nlookup.h>
58cfe60390STomohiro Kusumi
59cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
60cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_mount.h>
61cfe60390STomohiro Kusumi #include <vfs/ext2fs/inode.h>
62cfe60390STomohiro Kusumi
63cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2fs.h>
64cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dinode.h>
65cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extern.h>
66cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extents.h>
67cfe60390STomohiro Kusumi
68cfe60390STomohiro Kusumi SDT_PROVIDER_DECLARE(ext2fs);
69cfe60390STomohiro Kusumi /*
70cfe60390STomohiro Kusumi * ext2fs trace probe:
71cfe60390STomohiro Kusumi * arg0: verbosity. Higher numbers give more verbose messages
72cfe60390STomohiro Kusumi * arg1: Textual message
73cfe60390STomohiro Kusumi */
74cfe60390STomohiro Kusumi SDT_PROBE_DEFINE2(ext2fs, , vfsops, trace, "int", "char*");
75cfe60390STomohiro Kusumi SDT_PROBE_DEFINE2(ext2fs, , vfsops, ext2_cg_validate_error, "char*", "int");
76cfe60390STomohiro Kusumi SDT_PROBE_DEFINE1(ext2fs, , vfsops, ext2_compute_sb_data_error, "char*");
77cfe60390STomohiro Kusumi
78cfe60390STomohiro Kusumi static int ext2_flushfiles(struct mount *mp, int flags);
79cfe60390STomohiro Kusumi static int ext2_mountfs(struct vnode *, struct mount *);
80cfe60390STomohiro Kusumi static int ext2_reload(struct mount *mp);
81cfe60390STomohiro Kusumi static int ext2_sbupdate(struct ext2mount *, int);
82cfe60390STomohiro Kusumi static int ext2_cgupdate(struct ext2mount *, int);
83cfe60390STomohiro Kusumi static int ext2_init(struct vfsconf *);
84cfe60390STomohiro Kusumi static int ext2_uninit(struct vfsconf *);
85cfe60390STomohiro Kusumi static vfs_unmount_t ext2_unmount;
86cfe60390STomohiro Kusumi static vfs_root_t ext2_root;
87cfe60390STomohiro Kusumi static vfs_statfs_t ext2_statfs;
88cfe60390STomohiro Kusumi static vfs_statvfs_t ext2_statvfs;
89cfe60390STomohiro Kusumi static vfs_sync_t ext2_sync;
90cfe60390STomohiro Kusumi static vfs_vget_t ext2_vget;
91cfe60390STomohiro Kusumi static vfs_fhtovp_t ext2_fhtovp;
92cfe60390STomohiro Kusumi static vfs_vptofh_t ext2_vptofh;
93cfe60390STomohiro Kusumi static vfs_checkexp_t ext2_check_export;
94cfe60390STomohiro Kusumi static vfs_mount_t ext2_mount;
95cfe60390STomohiro Kusumi
96cfe60390STomohiro Kusumi MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
97cfe60390STomohiro Kusumi static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
98cfe60390STomohiro Kusumi
99cfe60390STomohiro Kusumi static struct vfsops ext2fs_vfsops = {
100cfe60390STomohiro Kusumi .vfs_flags = 0,
101cfe60390STomohiro Kusumi .vfs_mount = ext2_mount,
102cfe60390STomohiro Kusumi .vfs_unmount = ext2_unmount,
103cfe60390STomohiro Kusumi .vfs_root = ext2_root, /* root inode via vget */
104cfe60390STomohiro Kusumi .vfs_statfs = ext2_statfs,
105cfe60390STomohiro Kusumi .vfs_statvfs = ext2_statvfs,
106cfe60390STomohiro Kusumi .vfs_sync = ext2_sync,
107cfe60390STomohiro Kusumi .vfs_vget = ext2_vget,
108cfe60390STomohiro Kusumi .vfs_fhtovp = ext2_fhtovp,
109cfe60390STomohiro Kusumi .vfs_vptofh = ext2_vptofh,
110cfe60390STomohiro Kusumi .vfs_checkexp = ext2_check_export,
111cfe60390STomohiro Kusumi .vfs_init = ext2_init,
112cfe60390STomohiro Kusumi .vfs_uninit = ext2_uninit
113cfe60390STomohiro Kusumi };
114cfe60390STomohiro Kusumi
1154a24abd0STomohiro Kusumi VFS_SET(ext2fs_vfsops, ext2fs, VFCF_MPSAFE);
116cfe60390STomohiro Kusumi MODULE_VERSION(ext2fs, 1);
117cfe60390STomohiro Kusumi
118cfe60390STomohiro Kusumi static int ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
119cfe60390STomohiro Kusumi int ronly);
120cfe60390STomohiro Kusumi static int ext2_compute_sb_data(struct vnode * devvp,
121cfe60390STomohiro Kusumi struct ext2fs * es, struct m_ext2fs * fs);
122cfe60390STomohiro Kusumi
123ffd4f0afSTomohiro Kusumi static int ext2fs_inode_hash_lock;
124cfe60390STomohiro Kusumi
125cfe60390STomohiro Kusumi /*
126cfe60390STomohiro Kusumi * VFS Operations.
127cfe60390STomohiro Kusumi *
128cfe60390STomohiro Kusumi * mount system call
129cfe60390STomohiro Kusumi */
130cfe60390STomohiro Kusumi static int
ext2_mount(struct mount * mp,char * path,caddr_t data,struct ucred * cred)131cfe60390STomohiro Kusumi ext2_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
132cfe60390STomohiro Kusumi {
133cfe60390STomohiro Kusumi struct ext2_args args;
134cfe60390STomohiro Kusumi struct vnode *devvp;
135cfe60390STomohiro Kusumi struct ext2mount *ump = NULL;
136cfe60390STomohiro Kusumi struct m_ext2fs *fs;
137cfe60390STomohiro Kusumi struct nlookupdata nd;
138cfe60390STomohiro Kusumi mode_t accmode;
139cfe60390STomohiro Kusumi int error, flags;
140cfe60390STomohiro Kusumi size_t size;
141cfe60390STomohiro Kusumi
142cfe60390STomohiro Kusumi if ((error = copyin(data, (caddr_t)&args, sizeof (struct ext2_args))) != 0)
143cfe60390STomohiro Kusumi return (error);
144cfe60390STomohiro Kusumi
145cfe60390STomohiro Kusumi /*
146cfe60390STomohiro Kusumi * If updating, check whether changing from read-only to
147cfe60390STomohiro Kusumi * read/write; if there is no device name, that's all we do.
148cfe60390STomohiro Kusumi */
149cfe60390STomohiro Kusumi if (mp->mnt_flag & MNT_UPDATE) {
150cfe60390STomohiro Kusumi ump = VFSTOEXT2(mp);
151cfe60390STomohiro Kusumi fs = ump->um_e2fs;
152cfe60390STomohiro Kusumi devvp = ump->um_devvp;
153cfe60390STomohiro Kusumi error = 0;
154cfe60390STomohiro Kusumi if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
155cfe60390STomohiro Kusumi error = VFS_SYNC(mp, MNT_WAIT);
156cfe60390STomohiro Kusumi if (error)
157cfe60390STomohiro Kusumi return (error);
158cfe60390STomohiro Kusumi flags = WRITECLOSE;
159cfe60390STomohiro Kusumi if (mp->mnt_flag & MNT_FORCE)
160cfe60390STomohiro Kusumi flags |= FORCECLOSE;
161cfe60390STomohiro Kusumi if (vfs_busy(mp, LK_NOWAIT))
162cfe60390STomohiro Kusumi return (EBUSY);
163cfe60390STomohiro Kusumi error = ext2_flushfiles(mp, flags);
164cfe60390STomohiro Kusumi vfs_unbusy(mp);
165cfe60390STomohiro Kusumi if (error == 0 && fs->e2fs_wasvalid &&
166cfe60390STomohiro Kusumi ext2_cgupdate(ump, MNT_WAIT) == 0) {
167cfe60390STomohiro Kusumi fs->e2fs->e2fs_state =
168cfe60390STomohiro Kusumi htole16((le16toh(fs->e2fs->e2fs_state) |
169cfe60390STomohiro Kusumi E2FS_ISCLEAN));
170cfe60390STomohiro Kusumi ext2_sbupdate(ump, MNT_WAIT);
171cfe60390STomohiro Kusumi }
172cfe60390STomohiro Kusumi fs->e2fs_ronly = 1;
173cfe60390STomohiro Kusumi vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
174cfe60390STomohiro Kusumi VOP_OPEN(devvp, FREAD, FSCRED, NULL);
175cfe60390STomohiro Kusumi VOP_CLOSE(devvp, FREAD | FWRITE, NULL);
176cfe60390STomohiro Kusumi vn_unlock(devvp);
177cfe60390STomohiro Kusumi }
178cfe60390STomohiro Kusumi if (!error && (mp->mnt_flag & MNT_RELOAD))
179cfe60390STomohiro Kusumi error = ext2_reload(mp);
180cfe60390STomohiro Kusumi if (error)
181cfe60390STomohiro Kusumi return (error);
182cfe60390STomohiro Kusumi devvp = ump->um_devvp;
183cfe60390STomohiro Kusumi if (fs->e2fs_ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
184cfe60390STomohiro Kusumi if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0))
185cfe60390STomohiro Kusumi return (EPERM);
186cfe60390STomohiro Kusumi
187cfe60390STomohiro Kusumi /*
188cfe60390STomohiro Kusumi * If upgrade to read-write by non-root, then verify
189cfe60390STomohiro Kusumi * that user has necessary permissions on the device.
190cfe60390STomohiro Kusumi */
191cfe60390STomohiro Kusumi if (cred->cr_uid != 0) {
192cfe60390STomohiro Kusumi vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
193cfe60390STomohiro Kusumi error = VOP_EACCESS(devvp, VREAD | VWRITE, cred);
194cfe60390STomohiro Kusumi if (error) {
195cfe60390STomohiro Kusumi vn_unlock(devvp);
196cfe60390STomohiro Kusumi return (error);
197cfe60390STomohiro Kusumi }
198cfe60390STomohiro Kusumi vn_unlock(devvp);
199cfe60390STomohiro Kusumi }
200cfe60390STomohiro Kusumi
201cfe60390STomohiro Kusumi if ((le16toh(fs->e2fs->e2fs_state) & E2FS_ISCLEAN) == 0 ||
202cfe60390STomohiro Kusumi (le16toh(fs->e2fs->e2fs_state) & E2FS_ERRORS)) {
203cfe60390STomohiro Kusumi if (mp->mnt_flag & MNT_FORCE) {
204cfe60390STomohiro Kusumi printf(
205cfe60390STomohiro Kusumi "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
206cfe60390STomohiro Kusumi } else {
207cfe60390STomohiro Kusumi printf(
208cfe60390STomohiro Kusumi "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
209cfe60390STomohiro Kusumi fs->e2fs_fsmnt);
210cfe60390STomohiro Kusumi return (EPERM);
211cfe60390STomohiro Kusumi }
212cfe60390STomohiro Kusumi }
213cfe60390STomohiro Kusumi fs->e2fs->e2fs_state =
214cfe60390STomohiro Kusumi htole16(le16toh(fs->e2fs->e2fs_state) & ~E2FS_ISCLEAN);
215cfe60390STomohiro Kusumi (void)ext2_cgupdate(ump, MNT_WAIT);
216cfe60390STomohiro Kusumi fs->e2fs_ronly = 0;
217cfe60390STomohiro Kusumi mp->mnt_flag &= ~MNT_RDONLY;
218cfe60390STomohiro Kusumi
219cfe60390STomohiro Kusumi vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
220cfe60390STomohiro Kusumi VOP_OPEN(devvp, FREAD | FWRITE, FSCRED, NULL);
221cfe60390STomohiro Kusumi VOP_CLOSE(devvp, FREAD, NULL);
222cfe60390STomohiro Kusumi vn_unlock(devvp);
223cfe60390STomohiro Kusumi }
224cfe60390STomohiro Kusumi if (args.fspec == NULL) {
225cfe60390STomohiro Kusumi /*
226cfe60390STomohiro Kusumi * Process export requests.
227cfe60390STomohiro Kusumi */
228cfe60390STomohiro Kusumi return (vfs_export(mp, &ump->um_export, &args.export));
229cfe60390STomohiro Kusumi }
230cfe60390STomohiro Kusumi }
231cfe60390STomohiro Kusumi
232cfe60390STomohiro Kusumi /*
233cfe60390STomohiro Kusumi * Not an update, or updating the name: look up the name
234cfe60390STomohiro Kusumi * and verify that it refers to a sensible disk device.
235cfe60390STomohiro Kusumi */
236cfe60390STomohiro Kusumi devvp = NULL;
237cfe60390STomohiro Kusumi error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
238cfe60390STomohiro Kusumi if (error == 0)
239cfe60390STomohiro Kusumi error = nlookup(&nd);
240cfe60390STomohiro Kusumi if (error == 0)
241cfe60390STomohiro Kusumi error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
242cfe60390STomohiro Kusumi nlookup_done(&nd);
243cfe60390STomohiro Kusumi if (error)
244cfe60390STomohiro Kusumi return (error);
245cfe60390STomohiro Kusumi
246cfe60390STomohiro Kusumi if (!vn_isdisk(devvp, &error)) {
247cfe60390STomohiro Kusumi vrele(devvp);
248cfe60390STomohiro Kusumi return (error);
249cfe60390STomohiro Kusumi }
250cfe60390STomohiro Kusumi
251cfe60390STomohiro Kusumi /*
252cfe60390STomohiro Kusumi * If mount by non-root, then verify that user has necessary
253cfe60390STomohiro Kusumi * permissions on the device.
254cfe60390STomohiro Kusumi *
255cfe60390STomohiro Kusumi * XXXRW: VOP_ACCESS() enough?
256cfe60390STomohiro Kusumi */
257cfe60390STomohiro Kusumi if (cred->cr_uid != 0) {
258cfe60390STomohiro Kusumi accmode = VREAD;
259cfe60390STomohiro Kusumi if ((mp->mnt_flag & MNT_RDONLY) == 0)
260cfe60390STomohiro Kusumi accmode |= VWRITE;
261cfe60390STomohiro Kusumi vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
262cfe60390STomohiro Kusumi if ((error = VOP_EACCESS(devvp, accmode, cred)) != 0) {
263cfe60390STomohiro Kusumi vput(devvp);
264cfe60390STomohiro Kusumi return (error);
265cfe60390STomohiro Kusumi }
266cfe60390STomohiro Kusumi vn_unlock(devvp);
267cfe60390STomohiro Kusumi }
268cfe60390STomohiro Kusumi
269cfe60390STomohiro Kusumi if ((mp->mnt_flag & MNT_UPDATE) == 0) {
270cfe60390STomohiro Kusumi error = ext2_mountfs(devvp, mp);
271cfe60390STomohiro Kusumi } else {
272cfe60390STomohiro Kusumi if (devvp != ump->um_devvp)
273cfe60390STomohiro Kusumi error = EINVAL; /* needs translation */
274cfe60390STomohiro Kusumi else
275cfe60390STomohiro Kusumi vrele(devvp);
276cfe60390STomohiro Kusumi }
277cfe60390STomohiro Kusumi if (error) {
278cfe60390STomohiro Kusumi vrele(devvp);
279cfe60390STomohiro Kusumi return (error);
280cfe60390STomohiro Kusumi }
281cfe60390STomohiro Kusumi ump = VFSTOEXT2(mp);
282cfe60390STomohiro Kusumi fs = ump->um_e2fs;
283cfe60390STomohiro Kusumi
284cfe60390STomohiro Kusumi /*
285cfe60390STomohiro Kusumi * Note that this strncpy() is ok because of a check at the start
286cfe60390STomohiro Kusumi * of ext2_mount().
287cfe60390STomohiro Kusumi */
288cfe60390STomohiro Kusumi copyinstr(path, fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt) - 1, &size);
289cfe60390STomohiro Kusumi bzero(fs->e2fs_fsmnt + size, sizeof(fs->e2fs_fsmnt) - size);
290cfe60390STomohiro Kusumi copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
291cfe60390STomohiro Kusumi bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
292cfe60390STomohiro Kusumi ext2_statfs(mp, &mp->mnt_stat, cred);
293cfe60390STomohiro Kusumi return (0);
294cfe60390STomohiro Kusumi }
295cfe60390STomohiro Kusumi
296cfe60390STomohiro Kusumi static int
ext2_check_sb_compat(struct ext2fs * es,struct cdev * dev,int ronly)297cfe60390STomohiro Kusumi ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
298cfe60390STomohiro Kusumi {
299cfe60390STomohiro Kusumi uint32_t i, mask;
300cfe60390STomohiro Kusumi
301cfe60390STomohiro Kusumi if (le16toh(es->e2fs_magic) != E2FS_MAGIC) {
302cfe60390STomohiro Kusumi printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
303cfe60390STomohiro Kusumi devtoname(dev), le16toh(es->e2fs_magic), E2FS_MAGIC);
304cfe60390STomohiro Kusumi return (1);
305cfe60390STomohiro Kusumi }
306cfe60390STomohiro Kusumi if (le32toh(es->e2fs_rev) > E2FS_REV0) {
307cfe60390STomohiro Kusumi mask = le32toh(es->e2fs_features_incompat) & ~(EXT2F_INCOMPAT_SUPP);
308cfe60390STomohiro Kusumi if (mask) {
309cfe60390STomohiro Kusumi printf("WARNING: mount of %s denied due to "
310cfe60390STomohiro Kusumi "unsupported optional features:\n", devtoname(dev));
311cfe60390STomohiro Kusumi for (i = 0;
312cfe60390STomohiro Kusumi i < sizeof(incompat)/sizeof(struct ext2_feature);
313cfe60390STomohiro Kusumi i++)
314cfe60390STomohiro Kusumi if (mask & incompat[i].mask)
315cfe60390STomohiro Kusumi printf("%s ", incompat[i].name);
316cfe60390STomohiro Kusumi printf("\n");
317cfe60390STomohiro Kusumi return (1);
318cfe60390STomohiro Kusumi }
319cfe60390STomohiro Kusumi mask = le32toh(es->e2fs_features_rocompat) & ~EXT2F_ROCOMPAT_SUPP;
320cfe60390STomohiro Kusumi if (!ronly && mask) {
321cfe60390STomohiro Kusumi printf("WARNING: R/W mount of %s denied due to "
322cfe60390STomohiro Kusumi "unsupported optional features:\n", devtoname(dev));
323cfe60390STomohiro Kusumi for (i = 0;
324cfe60390STomohiro Kusumi i < sizeof(ro_compat)/sizeof(struct ext2_feature);
325cfe60390STomohiro Kusumi i++)
326cfe60390STomohiro Kusumi if (mask & ro_compat[i].mask)
327cfe60390STomohiro Kusumi printf("%s ", ro_compat[i].name);
328cfe60390STomohiro Kusumi printf("\n");
329cfe60390STomohiro Kusumi return (1);
330cfe60390STomohiro Kusumi }
331cfe60390STomohiro Kusumi }
332cfe60390STomohiro Kusumi return (0);
333cfe60390STomohiro Kusumi }
334cfe60390STomohiro Kusumi
335cfe60390STomohiro Kusumi static e4fs_daddr_t
ext2_cg_location(struct m_ext2fs * fs,int number)336cfe60390STomohiro Kusumi ext2_cg_location(struct m_ext2fs *fs, int number)
337cfe60390STomohiro Kusumi {
338cfe60390STomohiro Kusumi int cg, descpb, logical_sb, has_super = 0;
339cfe60390STomohiro Kusumi
340cfe60390STomohiro Kusumi /*
341cfe60390STomohiro Kusumi * Adjust logical superblock block number.
342cfe60390STomohiro Kusumi * Godmar thinks: if the blocksize is greater than 1024, then
343cfe60390STomohiro Kusumi * the superblock is logically part of block zero.
344cfe60390STomohiro Kusumi */
345cfe60390STomohiro Kusumi logical_sb = fs->e2fs_bsize > SBSIZE ? 0 : 1;
346cfe60390STomohiro Kusumi
347cfe60390STomohiro Kusumi if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) ||
348cfe60390STomohiro Kusumi number < le32toh(fs->e2fs->e3fs_first_meta_bg))
349cfe60390STomohiro Kusumi return (logical_sb + number + 1);
350cfe60390STomohiro Kusumi
351cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT))
352cfe60390STomohiro Kusumi descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
353cfe60390STomohiro Kusumi else
354cfe60390STomohiro Kusumi descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
355cfe60390STomohiro Kusumi
356cfe60390STomohiro Kusumi cg = descpb * number;
357cfe60390STomohiro Kusumi
358cfe60390STomohiro Kusumi if (ext2_cg_has_sb(fs, cg))
359cfe60390STomohiro Kusumi has_super = 1;
360cfe60390STomohiro Kusumi
361cfe60390STomohiro Kusumi return (has_super + cg * (e4fs_daddr_t)EXT2_BLOCKS_PER_GROUP(fs) +
362cfe60390STomohiro Kusumi le32toh(fs->e2fs->e2fs_first_dblock));
363cfe60390STomohiro Kusumi }
364cfe60390STomohiro Kusumi
365cfe60390STomohiro Kusumi static int
ext2_cg_validate(struct m_ext2fs * fs)366cfe60390STomohiro Kusumi ext2_cg_validate(struct m_ext2fs *fs)
367cfe60390STomohiro Kusumi {
368cfe60390STomohiro Kusumi uint64_t b_bitmap;
369cfe60390STomohiro Kusumi uint64_t i_bitmap;
370cfe60390STomohiro Kusumi uint64_t i_tables;
371cfe60390STomohiro Kusumi uint64_t first_block, last_block, last_cg_block;
372cfe60390STomohiro Kusumi struct ext2_gd *gd;
373cfe60390STomohiro Kusumi unsigned int i, cg_count;
374cfe60390STomohiro Kusumi
375cfe60390STomohiro Kusumi first_block = le32toh(fs->e2fs->e2fs_first_dblock);
376cfe60390STomohiro Kusumi last_cg_block = ext2_cg_number_gdb(fs, 0);
377cfe60390STomohiro Kusumi cg_count = fs->e2fs_gcount;
378cfe60390STomohiro Kusumi
379cfe60390STomohiro Kusumi for (i = 0; i < fs->e2fs_gcount; i++) {
380cfe60390STomohiro Kusumi gd = &fs->e2fs_gd[i];
381cfe60390STomohiro Kusumi
382cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) ||
383cfe60390STomohiro Kusumi i == fs->e2fs_gcount - 1) {
384cfe60390STomohiro Kusumi last_block = fs->e2fs_bcount - 1;
385cfe60390STomohiro Kusumi } else {
386cfe60390STomohiro Kusumi last_block = first_block +
387cfe60390STomohiro Kusumi (EXT2_BLOCKS_PER_GROUP(fs) - 1);
388cfe60390STomohiro Kusumi }
389cfe60390STomohiro Kusumi
390cfe60390STomohiro Kusumi if ((cg_count == fs->e2fs_gcount) &&
391cfe60390STomohiro Kusumi !(le16toh(gd->ext4bgd_flags) & EXT2_BG_INODE_ZEROED))
392cfe60390STomohiro Kusumi cg_count = i;
393cfe60390STomohiro Kusumi
394cfe60390STomohiro Kusumi b_bitmap = e2fs_gd_get_b_bitmap(gd);
395cfe60390STomohiro Kusumi if (b_bitmap == 0) {
396cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
397cfe60390STomohiro Kusumi "block bitmap is zero", i);
398cfe60390STomohiro Kusumi return (EINVAL);
399cfe60390STomohiro Kusumi }
400cfe60390STomohiro Kusumi if (b_bitmap <= last_cg_block) {
401cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
402cfe60390STomohiro Kusumi "block bitmap overlaps gds", i);
403cfe60390STomohiro Kusumi return (EINVAL);
404cfe60390STomohiro Kusumi }
405cfe60390STomohiro Kusumi if (b_bitmap < first_block || b_bitmap > last_block) {
406cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
407cfe60390STomohiro Kusumi "block bitmap not in group", i);
408cfe60390STomohiro Kusumi return (EINVAL);
409cfe60390STomohiro Kusumi }
410cfe60390STomohiro Kusumi
411cfe60390STomohiro Kusumi i_bitmap = e2fs_gd_get_i_bitmap(gd);
412cfe60390STomohiro Kusumi if (i_bitmap == 0) {
413cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
414cfe60390STomohiro Kusumi "inode bitmap is zero", i);
415cfe60390STomohiro Kusumi return (EINVAL);
416cfe60390STomohiro Kusumi }
417cfe60390STomohiro Kusumi if (i_bitmap <= last_cg_block) {
418cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
419cfe60390STomohiro Kusumi "inode bitmap overlaps gds", i);
420cfe60390STomohiro Kusumi return (EINVAL);
421cfe60390STomohiro Kusumi }
422cfe60390STomohiro Kusumi if (i_bitmap < first_block || i_bitmap > last_block) {
423cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
424cfe60390STomohiro Kusumi "inode bitmap not in group blk", i);
425cfe60390STomohiro Kusumi return (EINVAL);
426cfe60390STomohiro Kusumi }
427cfe60390STomohiro Kusumi
428cfe60390STomohiro Kusumi i_tables = e2fs_gd_get_i_tables(gd);
429cfe60390STomohiro Kusumi if (i_tables == 0) {
430cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
431cfe60390STomohiro Kusumi "inode table is zero", i);
432cfe60390STomohiro Kusumi return (EINVAL);
433cfe60390STomohiro Kusumi }
434cfe60390STomohiro Kusumi if (i_tables <= last_cg_block) {
435cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
436cde80429STomohiro Kusumi "inode tables overlaps gds", i);
437cfe60390STomohiro Kusumi return (EINVAL);
438cfe60390STomohiro Kusumi }
439cfe60390STomohiro Kusumi if (i_tables < first_block ||
440cfe60390STomohiro Kusumi i_tables + fs->e2fs_itpg - 1 > last_block) {
441cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
442cfe60390STomohiro Kusumi "inode tables not in group blk", i);
443cfe60390STomohiro Kusumi return (EINVAL);
444cfe60390STomohiro Kusumi }
445cfe60390STomohiro Kusumi
446cfe60390STomohiro Kusumi if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG))
447cfe60390STomohiro Kusumi first_block += EXT2_BLOCKS_PER_GROUP(fs);
448cfe60390STomohiro Kusumi }
449cfe60390STomohiro Kusumi
450cfe60390STomohiro Kusumi return (0);
451cfe60390STomohiro Kusumi }
452cfe60390STomohiro Kusumi
453cfe60390STomohiro Kusumi /*
454cfe60390STomohiro Kusumi * This computes the fields of the m_ext2fs structure from the
455cfe60390STomohiro Kusumi * data in the ext2fs structure read in.
456cfe60390STomohiro Kusumi */
457cfe60390STomohiro Kusumi static int
ext2_compute_sb_data(struct vnode * devvp,struct ext2fs * es,struct m_ext2fs * fs)458cfe60390STomohiro Kusumi ext2_compute_sb_data(struct vnode *devvp, struct ext2fs *es,
459cfe60390STomohiro Kusumi struct m_ext2fs *fs)
460cfe60390STomohiro Kusumi {
461cfe60390STomohiro Kusumi struct buf *bp;
462cfe60390STomohiro Kusumi uint32_t e2fs_descpb, e2fs_gdbcount_alloc;
463cfe60390STomohiro Kusumi int i, j;
464cfe60390STomohiro Kusumi int g_count = 0;
465cfe60390STomohiro Kusumi int error;
466cfe60390STomohiro Kusumi
467cfe60390STomohiro Kusumi /* Check checksum features */
468cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) &&
469cfe60390STomohiro Kusumi EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
470cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
471cfe60390STomohiro Kusumi "incorrect checksum features combination");
472cfe60390STomohiro Kusumi return (EINVAL);
473cfe60390STomohiro Kusumi }
474cfe60390STomohiro Kusumi
475cfe60390STomohiro Kusumi /* Precompute checksum seed for all metadata */
476cfe60390STomohiro Kusumi ext2_sb_csum_set_seed(fs);
477cfe60390STomohiro Kusumi
478cfe60390STomohiro Kusumi /* Verify sb csum if possible */
479cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
480cfe60390STomohiro Kusumi error = ext2_sb_csum_verify(fs);
481cfe60390STomohiro Kusumi if (error) {
482cfe60390STomohiro Kusumi return (error);
483cfe60390STomohiro Kusumi }
484cfe60390STomohiro Kusumi }
485cfe60390STomohiro Kusumi
486cfe60390STomohiro Kusumi /* Check for block size = 1K|2K|4K */
487cfe60390STomohiro Kusumi if (le32toh(es->e2fs_log_bsize) > 2) {
488cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
489cfe60390STomohiro Kusumi "bad block size");
490cfe60390STomohiro Kusumi return (EINVAL);
491cfe60390STomohiro Kusumi }
492cfe60390STomohiro Kusumi
493cfe60390STomohiro Kusumi fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + le32toh(es->e2fs_log_bsize);
494cfe60390STomohiro Kusumi fs->e2fs_bsize = 1U << fs->e2fs_bshift;
495cfe60390STomohiro Kusumi fs->e2fs_fsbtodb = le32toh(es->e2fs_log_bsize) + 1;
496cfe60390STomohiro Kusumi fs->e2fs_qbmask = fs->e2fs_bsize - 1;
497cfe60390STomohiro Kusumi
498cfe60390STomohiro Kusumi /* Check for fragment size */
499cfe60390STomohiro Kusumi if (le32toh(es->e2fs_log_fsize) >
500cfe60390STomohiro Kusumi (EXT2_MAX_FRAG_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) {
501cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
502cfe60390STomohiro Kusumi "invalid log cluster size");
503cfe60390STomohiro Kusumi return (EINVAL);
504cfe60390STomohiro Kusumi }
505cfe60390STomohiro Kusumi
506cfe60390STomohiro Kusumi fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << le32toh(es->e2fs_log_fsize);
507cfe60390STomohiro Kusumi if (fs->e2fs_fsize != fs->e2fs_bsize) {
508cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
509cfe60390STomohiro Kusumi "fragment size != block size");
510cfe60390STomohiro Kusumi return (EINVAL);
511cfe60390STomohiro Kusumi }
512cfe60390STomohiro Kusumi
513cfe60390STomohiro Kusumi fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
514cfe60390STomohiro Kusumi
515cfe60390STomohiro Kusumi /* Check reserved gdt blocks for future filesystem expansion */
516cfe60390STomohiro Kusumi if (le16toh(es->e2fs_reserved_ngdb) > (fs->e2fs_bsize / 4)) {
517cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
518cfe60390STomohiro Kusumi "number of reserved GDT blocks too large");
519cfe60390STomohiro Kusumi return (EINVAL);
520cfe60390STomohiro Kusumi }
521cfe60390STomohiro Kusumi
522cfe60390STomohiro Kusumi if (le32toh(es->e2fs_rev) == E2FS_REV0) {
523cfe60390STomohiro Kusumi fs->e2fs_isize = E2FS_REV0_INODE_SIZE;
524cfe60390STomohiro Kusumi } else {
525cfe60390STomohiro Kusumi fs->e2fs_isize = le16toh(es->e2fs_inode_size);
526cfe60390STomohiro Kusumi
527cfe60390STomohiro Kusumi /*
528cfe60390STomohiro Kusumi * Check first ino.
529cfe60390STomohiro Kusumi */
530cfe60390STomohiro Kusumi if (le32toh(es->e2fs_first_ino) < EXT2_FIRSTINO) {
531cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
532cfe60390STomohiro Kusumi "invalid first ino");
533cfe60390STomohiro Kusumi return (EINVAL);
534cfe60390STomohiro Kusumi }
535cfe60390STomohiro Kusumi
536cfe60390STomohiro Kusumi /*
537cfe60390STomohiro Kusumi * Simple sanity check for superblock inode size value.
538cfe60390STomohiro Kusumi */
539cfe60390STomohiro Kusumi if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
540cfe60390STomohiro Kusumi EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
541cfe60390STomohiro Kusumi (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
542cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
543cfe60390STomohiro Kusumi "invalid inode size");
544cfe60390STomohiro Kusumi return (EINVAL);
545cfe60390STomohiro Kusumi }
546cfe60390STomohiro Kusumi }
547cfe60390STomohiro Kusumi
548cfe60390STomohiro Kusumi /* Check group descriptors */
549cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
550cfe60390STomohiro Kusumi le16toh(es->e3fs_desc_size) != E2FS_64BIT_GD_SIZE) {
551cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
552cfe60390STomohiro Kusumi "unsupported 64bit descriptor size");
553cfe60390STomohiro Kusumi return (EINVAL);
554cfe60390STomohiro Kusumi }
555cfe60390STomohiro Kusumi
556cfe60390STomohiro Kusumi fs->e2fs_bpg = le32toh(es->e2fs_bpg);
557cfe60390STomohiro Kusumi fs->e2fs_fpg = le32toh(es->e2fs_fpg);
558cfe60390STomohiro Kusumi if (fs->e2fs_bpg == 0 || fs->e2fs_fpg == 0) {
559cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
560cfe60390STomohiro Kusumi "zero blocks/fragments per group");
561cfe60390STomohiro Kusumi return (EINVAL);
562cfe60390STomohiro Kusumi } else if (fs->e2fs_bpg != fs->e2fs_fpg) {
563cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
564cfe60390STomohiro Kusumi "blocks per group not equal fragments per group");
565cfe60390STomohiro Kusumi return (EINVAL);
566cfe60390STomohiro Kusumi }
567cfe60390STomohiro Kusumi
568cfe60390STomohiro Kusumi if (fs->e2fs_bpg != fs->e2fs_bsize * 8) {
569cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
570cfe60390STomohiro Kusumi "non-standard group size unsupported");
571cfe60390STomohiro Kusumi return (EINVAL);
572cfe60390STomohiro Kusumi }
573cfe60390STomohiro Kusumi
574cfe60390STomohiro Kusumi fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
575cfe60390STomohiro Kusumi if (fs->e2fs_ipb == 0 ||
576cfe60390STomohiro Kusumi fs->e2fs_ipb > fs->e2fs_bsize / E2FS_REV0_INODE_SIZE) {
577cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
578cfe60390STomohiro Kusumi "bad inodes per block size");
579cfe60390STomohiro Kusumi return (EINVAL);
580cfe60390STomohiro Kusumi }
581cfe60390STomohiro Kusumi
582cfe60390STomohiro Kusumi fs->e2fs_ipg = le32toh(es->e2fs_ipg);
583cfe60390STomohiro Kusumi if (fs->e2fs_ipg < fs->e2fs_ipb || fs->e2fs_ipg > fs->e2fs_bsize * 8) {
584cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
585cfe60390STomohiro Kusumi "invalid inodes per group");
586cfe60390STomohiro Kusumi return (EINVAL);
587cfe60390STomohiro Kusumi }
588cfe60390STomohiro Kusumi
589cfe60390STomohiro Kusumi fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb;
590cfe60390STomohiro Kusumi
591cfe60390STomohiro Kusumi fs->e2fs_bcount = le32toh(es->e2fs_bcount);
592cfe60390STomohiro Kusumi fs->e2fs_rbcount = le32toh(es->e2fs_rbcount);
593cfe60390STomohiro Kusumi fs->e2fs_fbcount = le32toh(es->e2fs_fbcount);
594cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
595cfe60390STomohiro Kusumi fs->e2fs_bcount |= (uint64_t)(le32toh(es->e4fs_bcount_hi)) << 32;
596cfe60390STomohiro Kusumi fs->e2fs_rbcount |= (uint64_t)(le32toh(es->e4fs_rbcount_hi)) << 32;
597cfe60390STomohiro Kusumi fs->e2fs_fbcount |= (uint64_t)(le32toh(es->e4fs_fbcount_hi)) << 32;
598cfe60390STomohiro Kusumi }
599cfe60390STomohiro Kusumi if (fs->e2fs_rbcount > fs->e2fs_bcount ||
600cfe60390STomohiro Kusumi fs->e2fs_fbcount > fs->e2fs_bcount) {
601cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
602cfe60390STomohiro Kusumi "invalid block count");
603cfe60390STomohiro Kusumi return (EINVAL);
604cfe60390STomohiro Kusumi }
605cfe60390STomohiro Kusumi
606cfe60390STomohiro Kusumi fs->e2fs_ficount = le32toh(es->e2fs_ficount);
607cfe60390STomohiro Kusumi if (fs->e2fs_ficount > le32toh(es->e2fs_icount)) {
608cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
609cfe60390STomohiro Kusumi "invalid number of free inodes");
610cfe60390STomohiro Kusumi return (EINVAL);
611cfe60390STomohiro Kusumi }
612cfe60390STomohiro Kusumi
613d7cd4e5dSTomohiro Kusumi if (le32toh(es->e2fs_first_dblock) != (fs->e2fs_bsize > 1024 ? 0 : 1) ||
614d7cd4e5dSTomohiro Kusumi le32toh(es->e2fs_first_dblock) >= fs->e2fs_bcount) {
615cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
616cfe60390STomohiro Kusumi "first data block out of range");
617cfe60390STomohiro Kusumi return (EINVAL);
618cfe60390STomohiro Kusumi }
619cfe60390STomohiro Kusumi
620cfe60390STomohiro Kusumi fs->e2fs_gcount = howmany(fs->e2fs_bcount -
621cfe60390STomohiro Kusumi le32toh(es->e2fs_first_dblock), EXT2_BLOCKS_PER_GROUP(fs));
622cfe60390STomohiro Kusumi if (fs->e2fs_gcount > ((uint64_t)1 << 32) - EXT2_DESCS_PER_BLOCK(fs)) {
623cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
624cfe60390STomohiro Kusumi "groups count too large");
625cfe60390STomohiro Kusumi return (EINVAL);
626cfe60390STomohiro Kusumi }
627cfe60390STomohiro Kusumi
628cfe60390STomohiro Kusumi /* Check for extra isize in big inodes. */
629cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
630cfe60390STomohiro Kusumi EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
631cfe60390STomohiro Kusumi SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
632cfe60390STomohiro Kusumi "no space for extra inode timestamps");
633cfe60390STomohiro Kusumi return (EINVAL);
634cfe60390STomohiro Kusumi }
635cfe60390STomohiro Kusumi
636cfe60390STomohiro Kusumi /* s_resuid / s_resgid ? */
637cfe60390STomohiro Kusumi
638cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
639cfe60390STomohiro Kusumi e2fs_descpb = fs->e2fs_bsize / E2FS_64BIT_GD_SIZE;
640cfe60390STomohiro Kusumi e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb);
641cfe60390STomohiro Kusumi } else {
642cfe60390STomohiro Kusumi e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
643cfe60390STomohiro Kusumi e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount,
644cfe60390STomohiro Kusumi fs->e2fs_bsize / sizeof(struct ext2_gd));
645cfe60390STomohiro Kusumi }
646cfe60390STomohiro Kusumi fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb);
647cfe60390STomohiro Kusumi fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize,
648cfe60390STomohiro Kusumi M_EXT2MNT, M_WAITOK | M_ZERO);
649cfe60390STomohiro Kusumi fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
650cfe60390STomohiro Kusumi sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO);
651cfe60390STomohiro Kusumi
652cfe60390STomohiro Kusumi for (i = 0; i < fs->e2fs_gdbcount; i++) {
653e5b38eb5STomohiro Kusumi error = bread(devvp, fsbtodoff(fs, ext2_cg_location(fs, i)),
654cfe60390STomohiro Kusumi fs->e2fs_bsize, &bp);
655cfe60390STomohiro Kusumi if (error) {
656cfe60390STomohiro Kusumi /*
657cfe60390STomohiro Kusumi * fs->e2fs_gd and fs->e2fs_contigdirs
658cfe60390STomohiro Kusumi * will be freed later by the caller,
659cfe60390STomohiro Kusumi * because this function could be called from
660cfe60390STomohiro Kusumi * MNT_UPDATE path.
661cfe60390STomohiro Kusumi */
662e5b38eb5STomohiro Kusumi brelse(bp);
663cfe60390STomohiro Kusumi return (error);
664cfe60390STomohiro Kusumi }
665cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
666cfe60390STomohiro Kusumi memcpy(&fs->e2fs_gd[
667cfe60390STomohiro Kusumi i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
668cfe60390STomohiro Kusumi bp->b_data, fs->e2fs_bsize);
669cfe60390STomohiro Kusumi } else {
670cfe60390STomohiro Kusumi for (j = 0; j < e2fs_descpb &&
671cfe60390STomohiro Kusumi g_count < fs->e2fs_gcount; j++, g_count++)
672cfe60390STomohiro Kusumi memcpy(&fs->e2fs_gd[g_count],
673cfe60390STomohiro Kusumi bp->b_data + j * E2FS_REV0_GD_SIZE,
674cfe60390STomohiro Kusumi E2FS_REV0_GD_SIZE);
675cfe60390STomohiro Kusumi }
676e5b38eb5STomohiro Kusumi brelse(bp);
677cfe60390STomohiro Kusumi bp = NULL;
678cfe60390STomohiro Kusumi }
679cfe60390STomohiro Kusumi
680cfe60390STomohiro Kusumi /* Validate cgs consistency */
681cfe60390STomohiro Kusumi error = ext2_cg_validate(fs);
682cfe60390STomohiro Kusumi if (error)
683cfe60390STomohiro Kusumi return (error);
684cfe60390STomohiro Kusumi
685cfe60390STomohiro Kusumi /* Verfy cgs csum */
686cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
687cfe60390STomohiro Kusumi EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
688cfe60390STomohiro Kusumi error = ext2_gd_csum_verify(fs, devvp->v_rdev);
689cfe60390STomohiro Kusumi if (error)
690cfe60390STomohiro Kusumi return (error);
691cfe60390STomohiro Kusumi }
692cfe60390STomohiro Kusumi /* Initialization for the ext2 Orlov allocator variant. */
693cfe60390STomohiro Kusumi fs->e2fs_total_dir = 0;
694cfe60390STomohiro Kusumi for (i = 0; i < fs->e2fs_gcount; i++)
695cfe60390STomohiro Kusumi fs->e2fs_total_dir += e2fs_gd_get_ndirs(&fs->e2fs_gd[i]);
696cfe60390STomohiro Kusumi
697cfe60390STomohiro Kusumi if (le32toh(es->e2fs_rev) == E2FS_REV0 ||
698cfe60390STomohiro Kusumi !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
699cfe60390STomohiro Kusumi fs->e2fs_maxfilesize = 0x7fffffff;
700cfe60390STomohiro Kusumi else {
701cfe60390STomohiro Kusumi fs->e2fs_maxfilesize = 0xffffffffffff;
702cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
703cfe60390STomohiro Kusumi fs->e2fs_maxfilesize = 0x7fffffffffffffff;
704cfe60390STomohiro Kusumi }
705cfe60390STomohiro Kusumi if (le32toh(es->e4fs_flags) & E2FS_UNSIGNED_HASH) {
706cfe60390STomohiro Kusumi fs->e2fs_uhash = 3;
707cfe60390STomohiro Kusumi } else if ((le32toh(es->e4fs_flags) & E2FS_SIGNED_HASH) == 0) {
708cfe60390STomohiro Kusumi #ifdef __CHAR_UNSIGNED__
709cfe60390STomohiro Kusumi es->e4fs_flags = htole32(le32toh(es->e4fs_flags) | E2FS_UNSIGNED_HASH);
710cfe60390STomohiro Kusumi fs->e2fs_uhash = 3;
711cfe60390STomohiro Kusumi #else
712cfe60390STomohiro Kusumi es->e4fs_flags = htole32(le32toh(es->e4fs_flags) | E2FS_SIGNED_HASH);
713cfe60390STomohiro Kusumi #endif
714cfe60390STomohiro Kusumi }
715cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
716cfe60390STomohiro Kusumi error = ext2_sb_csum_verify(fs);
717cfe60390STomohiro Kusumi
718cfe60390STomohiro Kusumi return (error);
719cfe60390STomohiro Kusumi }
720cfe60390STomohiro Kusumi
721cfe60390STomohiro Kusumi struct scaninfo {
722cfe60390STomohiro Kusumi int rescan;
723cfe60390STomohiro Kusumi int allerror;
724cfe60390STomohiro Kusumi int waitfor;
725cfe60390STomohiro Kusumi struct vnode *devvp;
726cfe60390STomohiro Kusumi struct m_ext2fs *fs;
727cfe60390STomohiro Kusumi };
728cfe60390STomohiro Kusumi
729cfe60390STomohiro Kusumi static int
ext2_reload_scan(struct mount * mp,struct vnode * vp,void * data)730cfe60390STomohiro Kusumi ext2_reload_scan(struct mount *mp, struct vnode *vp, void *data)
731cfe60390STomohiro Kusumi {
732cfe60390STomohiro Kusumi struct scaninfo *info = data;
733cfe60390STomohiro Kusumi struct inode *ip;
734cfe60390STomohiro Kusumi struct buf *bp;
735cfe60390STomohiro Kusumi int error;
736cfe60390STomohiro Kusumi
737cfe60390STomohiro Kusumi /*
738cfe60390STomohiro Kusumi * Try to recycle
739cfe60390STomohiro Kusumi */
740cfe60390STomohiro Kusumi if (vrecycle(vp))
741cfe60390STomohiro Kusumi return (0);
742cfe60390STomohiro Kusumi
743cfe60390STomohiro Kusumi /*
744cfe60390STomohiro Kusumi * Step 1: invalidate all cached file data.
745cfe60390STomohiro Kusumi */
746cfe60390STomohiro Kusumi if (vinvalbuf(vp, 0, 0, 0))
747cfe60390STomohiro Kusumi panic("ext2_reload: dirty2");
748cfe60390STomohiro Kusumi /*
749cfe60390STomohiro Kusumi * Step 2: re-read inode data for all active vnodes.
750cfe60390STomohiro Kusumi */
751cfe60390STomohiro Kusumi ip = VTOI(vp);
752e5b38eb5STomohiro Kusumi error = bread(info->devvp,
753cfe60390STomohiro Kusumi fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
754cfe60390STomohiro Kusumi (int)info->fs->e2fs_bsize, &bp);
755f9507b19STomohiro Kusumi if (error) {
756e5b38eb5STomohiro Kusumi brelse(bp);
757cfe60390STomohiro Kusumi return (error);
758f9507b19STomohiro Kusumi }
759cfe60390STomohiro Kusumi
760cfe60390STomohiro Kusumi error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
761cfe60390STomohiro Kusumi EXT2_INODE_SIZE(info->fs) * ino_to_fsbo(info->fs, ip->i_number)),
762cfe60390STomohiro Kusumi ip);
763cfe60390STomohiro Kusumi
764e5b38eb5STomohiro Kusumi brelse(bp);
765cfe60390STomohiro Kusumi return (error);
766cfe60390STomohiro Kusumi }
767cfe60390STomohiro Kusumi
768cfe60390STomohiro Kusumi /*
769cfe60390STomohiro Kusumi * Reload all incore data for a filesystem (used after running fsck on
770cfe60390STomohiro Kusumi * the root filesystem and finding things to fix). The filesystem must
771cfe60390STomohiro Kusumi * be mounted read-only.
772cfe60390STomohiro Kusumi *
773cfe60390STomohiro Kusumi * Things to do to update the mount:
774cfe60390STomohiro Kusumi * 1) invalidate all cached meta-data.
775cfe60390STomohiro Kusumi * 2) re-read superblock from disk.
776cfe60390STomohiro Kusumi * 3) invalidate all cluster summary information.
777cfe60390STomohiro Kusumi * 4) invalidate all inactive vnodes.
778cfe60390STomohiro Kusumi * 5) invalidate all cached file data.
779cfe60390STomohiro Kusumi * 6) re-read inode data for all active vnodes.
780cfe60390STomohiro Kusumi * XXX we are missing some steps, in particular # 3, this has to be reviewed.
781cfe60390STomohiro Kusumi */
782cfe60390STomohiro Kusumi static int
ext2_reload(struct mount * mp)783cfe60390STomohiro Kusumi ext2_reload(struct mount *mp)
784cfe60390STomohiro Kusumi {
785cfe60390STomohiro Kusumi struct vnode *devvp;
786cfe60390STomohiro Kusumi struct buf *bp;
787cfe60390STomohiro Kusumi struct ext2fs *es;
788cfe60390STomohiro Kusumi struct m_ext2fs *fs;
789cfe60390STomohiro Kusumi struct csum *sump;
790cfe60390STomohiro Kusumi struct scaninfo scaninfo;
791cfe60390STomohiro Kusumi int error, i;
792cfe60390STomohiro Kusumi int32_t *lp;
793cfe60390STomohiro Kusumi
794cfe60390STomohiro Kusumi if ((mp->mnt_flag & MNT_RDONLY) == 0)
795cfe60390STomohiro Kusumi return (EINVAL);
796cfe60390STomohiro Kusumi /*
797cfe60390STomohiro Kusumi * Step 1: invalidate all cached meta-data.
798cfe60390STomohiro Kusumi */
799cfe60390STomohiro Kusumi devvp = VFSTOEXT2(mp)->um_devvp;
800cfe60390STomohiro Kusumi vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
801cfe60390STomohiro Kusumi if (vinvalbuf(devvp, 0, 0, 0) != 0)
802cfe60390STomohiro Kusumi panic("ext2_reload: dirty1");
803cfe60390STomohiro Kusumi vn_unlock(devvp);
804cfe60390STomohiro Kusumi
805cfe60390STomohiro Kusumi /*
806cfe60390STomohiro Kusumi * Step 2: re-read superblock from disk.
807cfe60390STomohiro Kusumi * constants have been adjusted for ext2
808cfe60390STomohiro Kusumi */
809e5b38eb5STomohiro Kusumi if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
810e5b38eb5STomohiro Kusumi brelse(bp);
811cfe60390STomohiro Kusumi return (error);
812f9507b19STomohiro Kusumi }
813cfe60390STomohiro Kusumi es = (struct ext2fs *)bp->b_data;
814cfe60390STomohiro Kusumi if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
815e5b38eb5STomohiro Kusumi brelse(bp);
816cfe60390STomohiro Kusumi return (EIO); /* XXX needs translation */
817cfe60390STomohiro Kusumi }
818cfe60390STomohiro Kusumi fs = VFSTOEXT2(mp)->um_e2fs;
819cfe60390STomohiro Kusumi bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
820cfe60390STomohiro Kusumi
821cfe60390STomohiro Kusumi if ((error = ext2_compute_sb_data(devvp, es, fs)) != 0) {
822e5b38eb5STomohiro Kusumi brelse(bp);
823cfe60390STomohiro Kusumi return (error);
824cfe60390STomohiro Kusumi }
825cfe60390STomohiro Kusumi #ifdef UNKLAR
826cfe60390STomohiro Kusumi if (fs->fs_sbsize < SBSIZE)
827cfe60390STomohiro Kusumi bp->b_flags |= B_INVAL;
828cfe60390STomohiro Kusumi #endif
829e5b38eb5STomohiro Kusumi brelse(bp);
830cfe60390STomohiro Kusumi
831cfe60390STomohiro Kusumi /*
832cfe60390STomohiro Kusumi * Step 3: invalidate all cluster summary information.
833cfe60390STomohiro Kusumi */
834cfe60390STomohiro Kusumi if (fs->e2fs_contigsumsize > 0) {
835cfe60390STomohiro Kusumi lp = fs->e2fs_maxcluster;
836cfe60390STomohiro Kusumi sump = fs->e2fs_clustersum;
837cfe60390STomohiro Kusumi for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
838cfe60390STomohiro Kusumi *lp++ = fs->e2fs_contigsumsize;
839cfe60390STomohiro Kusumi sump->cs_init = 0;
840cfe60390STomohiro Kusumi bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
841cfe60390STomohiro Kusumi }
842cfe60390STomohiro Kusumi }
843cfe60390STomohiro Kusumi
844cfe60390STomohiro Kusumi scaninfo.rescan = 1;
845cfe60390STomohiro Kusumi scaninfo.devvp = devvp;
846cfe60390STomohiro Kusumi scaninfo.fs = fs;
847cfe60390STomohiro Kusumi while (error == 0 && scaninfo.rescan) {
848cfe60390STomohiro Kusumi scaninfo.rescan = 0;
849cfe60390STomohiro Kusumi error = vmntvnodescan(mp, VMSC_GETVX, NULL, ext2_reload_scan,
850cfe60390STomohiro Kusumi &scaninfo);
851cfe60390STomohiro Kusumi }
852cfe60390STomohiro Kusumi return (error);
853cfe60390STomohiro Kusumi }
854cfe60390STomohiro Kusumi
855cfe60390STomohiro Kusumi /*
856cfe60390STomohiro Kusumi * Common code for mount and mountroot.
857cfe60390STomohiro Kusumi */
858cfe60390STomohiro Kusumi static int
ext2_mountfs(struct vnode * devvp,struct mount * mp)859cfe60390STomohiro Kusumi ext2_mountfs(struct vnode *devvp, struct mount *mp)
860cfe60390STomohiro Kusumi {
861cfe60390STomohiro Kusumi struct ext2mount *ump;
862cfe60390STomohiro Kusumi struct buf *bp;
863cfe60390STomohiro Kusumi struct m_ext2fs *fs;
864cfe60390STomohiro Kusumi struct ext2fs *es;
865cfe60390STomohiro Kusumi struct cdev *dev = devvp->v_rdev;
866cfe60390STomohiro Kusumi struct csum *sump;
867cfe60390STomohiro Kusumi int error;
868cfe60390STomohiro Kusumi int ronly;
869cfe60390STomohiro Kusumi int i;
870cfe60390STomohiro Kusumi u_long size;
871cfe60390STomohiro Kusumi int32_t *lp;
872cfe60390STomohiro Kusumi int32_t e2fs_maxcontig;
873cfe60390STomohiro Kusumi
874cfe60390STomohiro Kusumi /*
875cfe60390STomohiro Kusumi * Disallow multiple mounts of the same device.
876cfe60390STomohiro Kusumi * Disallow mounting of a device that is currently in use
877cfe60390STomohiro Kusumi * (except for root, which might share swap device for miniroot).
878cfe60390STomohiro Kusumi * Flush out any old buffers remaining from a previous use.
879cfe60390STomohiro Kusumi */
880cfe60390STomohiro Kusumi if ((error = vfs_mountedon(devvp)) != 0)
881cfe60390STomohiro Kusumi return (error);
882cfe60390STomohiro Kusumi if (vcount(devvp) > 0)
883cfe60390STomohiro Kusumi return (EBUSY);
884cfe60390STomohiro Kusumi if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)) != 0)
885cfe60390STomohiro Kusumi return (error);
886cfe60390STomohiro Kusumi #ifdef READONLY
887cfe60390STomohiro Kusumi /* Turn on this to force it to be read-only. */
888cfe60390STomohiro Kusumi mp->mnt_flag |= MNT_RDONLY;
889cfe60390STomohiro Kusumi #endif
890cfe60390STomohiro Kusumi ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
891cfe60390STomohiro Kusumi vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
892cfe60390STomohiro Kusumi error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE, FSCRED, NULL);
893cfe60390STomohiro Kusumi vn_unlock(devvp);
894cfe60390STomohiro Kusumi if (error)
895cfe60390STomohiro Kusumi return (error);
896cfe60390STomohiro Kusumi
897cfe60390STomohiro Kusumi if (devvp->v_rdev->si_iosize_max != 0)
898cfe60390STomohiro Kusumi mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
899cfe60390STomohiro Kusumi if (mp->mnt_iosize_max > MAXPHYS)
900cfe60390STomohiro Kusumi mp->mnt_iosize_max = MAXPHYS;
901cfe60390STomohiro Kusumi
902cfe60390STomohiro Kusumi bp = NULL;
903cfe60390STomohiro Kusumi ump = NULL;
904e5b38eb5STomohiro Kusumi if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
905cfe60390STomohiro Kusumi goto out;
906cfe60390STomohiro Kusumi es = (struct ext2fs *)bp->b_data;
907cfe60390STomohiro Kusumi if (ext2_check_sb_compat(es, dev, ronly) != 0) {
908cfe60390STomohiro Kusumi error = EINVAL; /* XXX needs translation */
909cfe60390STomohiro Kusumi goto out;
910cfe60390STomohiro Kusumi }
911cfe60390STomohiro Kusumi if ((le16toh(es->e2fs_state) & E2FS_ISCLEAN) == 0 ||
912cfe60390STomohiro Kusumi (le16toh(es->e2fs_state) & E2FS_ERRORS)) {
913cfe60390STomohiro Kusumi if (ronly || (mp->mnt_flag & MNT_FORCE)) {
914cfe60390STomohiro Kusumi printf(
915cfe60390STomohiro Kusumi "WARNING: Filesystem was not properly dismounted\n");
916cfe60390STomohiro Kusumi } else {
917cfe60390STomohiro Kusumi printf(
918cfe60390STomohiro Kusumi "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
919cfe60390STomohiro Kusumi error = EPERM;
920cfe60390STomohiro Kusumi goto out;
921cfe60390STomohiro Kusumi }
922cfe60390STomohiro Kusumi }
923cfe60390STomohiro Kusumi ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
924cfe60390STomohiro Kusumi
925cfe60390STomohiro Kusumi /*
926cfe60390STomohiro Kusumi * I don't know whether this is the right strategy. Note that
927cfe60390STomohiro Kusumi * we dynamically allocate both an m_ext2fs and an ext2fs
928cfe60390STomohiro Kusumi * while Linux keeps the super block in a locked buffer.
929cfe60390STomohiro Kusumi */
930cfe60390STomohiro Kusumi ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
931cfe60390STomohiro Kusumi M_EXT2MNT, M_WAITOK | M_ZERO);
932cfe60390STomohiro Kusumi ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
933cfe60390STomohiro Kusumi M_EXT2MNT, M_WAITOK);
934cfe60390STomohiro Kusumi mtx_init(EXT2_MTX(ump), "EXT2FS Lock");
935cfe60390STomohiro Kusumi bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
936cfe60390STomohiro Kusumi if ((error = ext2_compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
937cfe60390STomohiro Kusumi goto out;
938cfe60390STomohiro Kusumi
939cfe60390STomohiro Kusumi /*
940cfe60390STomohiro Kusumi * Calculate the maximum contiguous blocks and size of cluster summary
941cfe60390STomohiro Kusumi * array. In FFS this is done by newfs; however, the superblock
942cfe60390STomohiro Kusumi * in ext2fs doesn't have these variables, so we can calculate
943cfe60390STomohiro Kusumi * them here.
944cfe60390STomohiro Kusumi */
945cfe60390STomohiro Kusumi e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
946cfe60390STomohiro Kusumi ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
947cfe60390STomohiro Kusumi if (ump->um_e2fs->e2fs_contigsumsize > 0) {
948cfe60390STomohiro Kusumi size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
949cfe60390STomohiro Kusumi ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
950cfe60390STomohiro Kusumi size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
951cfe60390STomohiro Kusumi ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
952cfe60390STomohiro Kusumi lp = ump->um_e2fs->e2fs_maxcluster;
953cfe60390STomohiro Kusumi sump = ump->um_e2fs->e2fs_clustersum;
954cfe60390STomohiro Kusumi for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
955cfe60390STomohiro Kusumi *lp++ = ump->um_e2fs->e2fs_contigsumsize;
956cfe60390STomohiro Kusumi sump->cs_init = 0;
957cfe60390STomohiro Kusumi sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
958cfe60390STomohiro Kusumi sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
959cfe60390STomohiro Kusumi }
960cfe60390STomohiro Kusumi }
961cfe60390STomohiro Kusumi
962e5b38eb5STomohiro Kusumi brelse(bp);
963cfe60390STomohiro Kusumi bp = NULL;
964cfe60390STomohiro Kusumi fs = ump->um_e2fs;
965cfe60390STomohiro Kusumi fs->e2fs_ronly = ronly; /* ronly is set according to mnt_flags */
966cfe60390STomohiro Kusumi
967cfe60390STomohiro Kusumi /*
968cfe60390STomohiro Kusumi * If the fs is not mounted read-only, make sure the super block is
969cfe60390STomohiro Kusumi * always written back on a sync().
970cfe60390STomohiro Kusumi */
971cfe60390STomohiro Kusumi fs->e2fs_wasvalid = le16toh(fs->e2fs->e2fs_state) & E2FS_ISCLEAN ? 1 : 0;
972cfe60390STomohiro Kusumi if (ronly == 0) {
973cfe60390STomohiro Kusumi fs->e2fs_fmod = 1; /* mark it modified and set fs invalid */
974cfe60390STomohiro Kusumi fs->e2fs->e2fs_state =
975cfe60390STomohiro Kusumi htole16(le16toh(fs->e2fs->e2fs_state) & ~E2FS_ISCLEAN);
976cfe60390STomohiro Kusumi }
977cfe60390STomohiro Kusumi mp->mnt_data = (qaddr_t)ump;
978cfe60390STomohiro Kusumi mp->mnt_stat.f_fsid.val[0] = devid_from_dev(dev);
979cfe60390STomohiro Kusumi mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
980cfe60390STomohiro Kusumi mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
981cfe60390STomohiro Kusumi mp->mnt_flag |= MNT_LOCAL;
9824a24abd0STomohiro Kusumi mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
983cfe60390STomohiro Kusumi ump->um_mountp = mp;
984cfe60390STomohiro Kusumi ump->um_dev = dev;
985cfe60390STomohiro Kusumi ump->um_devvp = devvp;
986cfe60390STomohiro Kusumi
987cfe60390STomohiro Kusumi /*
988cfe60390STomohiro Kusumi * Setting those two parameters allowed us to use
989cfe60390STomohiro Kusumi * ufs_bmap w/o changse!
990cfe60390STomohiro Kusumi */
991cfe60390STomohiro Kusumi ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
992cfe60390STomohiro Kusumi ump->um_bptrtodb = le32toh(fs->e2fs->e2fs_log_bsize) + 1;
993cfe60390STomohiro Kusumi ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
994cfe60390STomohiro Kusumi dev->si_mountpoint = mp;
995cfe60390STomohiro Kusumi
996cfe60390STomohiro Kusumi vfs_add_vnodeops(mp, &ext2_vnodeops, &mp->mnt_vn_norm_ops);
997cfe60390STomohiro Kusumi vfs_add_vnodeops(mp, &ext2_specops, &mp->mnt_vn_spec_ops);
998cfe60390STomohiro Kusumi vfs_add_vnodeops(mp, &ext2_fifoops, &mp->mnt_vn_fifo_ops);
999cfe60390STomohiro Kusumi
1000cfe60390STomohiro Kusumi if (ronly == 0)
1001cfe60390STomohiro Kusumi ext2_sbupdate(ump, MNT_WAIT);
1002cfe60390STomohiro Kusumi return (0);
1003cfe60390STomohiro Kusumi out:
1004cfe60390STomohiro Kusumi if (bp)
1005e5b38eb5STomohiro Kusumi brelse(bp);
1006cfe60390STomohiro Kusumi vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1007cfe60390STomohiro Kusumi VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NULL);
1008cfe60390STomohiro Kusumi vn_unlock(devvp);
1009cfe60390STomohiro Kusumi if (ump) {
1010cfe60390STomohiro Kusumi mtx_uninit(EXT2_MTX(ump));
1011cfe60390STomohiro Kusumi free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
1012cfe60390STomohiro Kusumi free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
1013cfe60390STomohiro Kusumi free(ump->um_e2fs->e2fs, M_EXT2MNT);
1014cfe60390STomohiro Kusumi free(ump->um_e2fs, M_EXT2MNT);
1015cfe60390STomohiro Kusumi free(ump, M_EXT2MNT);
1016cfe60390STomohiro Kusumi mp->mnt_data = NULL;
1017cfe60390STomohiro Kusumi }
1018cfe60390STomohiro Kusumi return (error);
1019cfe60390STomohiro Kusumi }
1020cfe60390STomohiro Kusumi
1021cfe60390STomohiro Kusumi /*
1022cfe60390STomohiro Kusumi * Unmount system call.
1023cfe60390STomohiro Kusumi */
1024cfe60390STomohiro Kusumi static int
ext2_unmount(struct mount * mp,int mntflags)1025cfe60390STomohiro Kusumi ext2_unmount(struct mount *mp, int mntflags)
1026cfe60390STomohiro Kusumi {
1027cfe60390STomohiro Kusumi struct ext2mount *ump;
1028cfe60390STomohiro Kusumi struct m_ext2fs *fs;
1029cfe60390STomohiro Kusumi struct csum *sump;
1030cfe60390STomohiro Kusumi int error, flags, i, ronly;
1031cfe60390STomohiro Kusumi
1032cfe60390STomohiro Kusumi flags = 0;
1033cfe60390STomohiro Kusumi if (mntflags & MNT_FORCE) {
1034cfe60390STomohiro Kusumi if (mp->mnt_flag & MNT_ROOTFS)
1035cfe60390STomohiro Kusumi return (EINVAL);
1036cfe60390STomohiro Kusumi flags |= FORCECLOSE;
1037cfe60390STomohiro Kusumi }
1038cfe60390STomohiro Kusumi if ((error = ext2_flushfiles(mp, flags)) != 0)
1039cfe60390STomohiro Kusumi return (error);
1040cfe60390STomohiro Kusumi ump = VFSTOEXT2(mp);
1041cfe60390STomohiro Kusumi fs = ump->um_e2fs;
1042cfe60390STomohiro Kusumi ronly = fs->e2fs_ronly;
1043cfe60390STomohiro Kusumi if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
1044cfe60390STomohiro Kusumi if (fs->e2fs_wasvalid)
1045cfe60390STomohiro Kusumi fs->e2fs->e2fs_state =
1046cfe60390STomohiro Kusumi htole16(le16toh(fs->e2fs->e2fs_state) | E2FS_ISCLEAN);
1047cfe60390STomohiro Kusumi ext2_sbupdate(ump, MNT_WAIT);
1048cfe60390STomohiro Kusumi }
1049cfe60390STomohiro Kusumi
1050cfe60390STomohiro Kusumi ump->um_devvp->v_rdev->si_mountpoint = NULL;
1051cfe60390STomohiro Kusumi
1052cfe60390STomohiro Kusumi vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1053cfe60390STomohiro Kusumi error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD | FWRITE, NULL);
1054cfe60390STomohiro Kusumi vn_unlock(ump->um_devvp);
1055cfe60390STomohiro Kusumi
1056cfe60390STomohiro Kusumi vrele(ump->um_devvp);
1057cfe60390STomohiro Kusumi sump = fs->e2fs_clustersum;
1058cfe60390STomohiro Kusumi for (i = 0; i < fs->e2fs_gcount; i++, sump++)
1059cfe60390STomohiro Kusumi free(sump->cs_sum, M_EXT2MNT);
1060cfe60390STomohiro Kusumi free(fs->e2fs_clustersum, M_EXT2MNT);
1061cfe60390STomohiro Kusumi free(fs->e2fs_maxcluster, M_EXT2MNT);
1062cfe60390STomohiro Kusumi free(fs->e2fs_gd, M_EXT2MNT);
1063cfe60390STomohiro Kusumi free(fs->e2fs_contigdirs, M_EXT2MNT);
1064cfe60390STomohiro Kusumi free(fs->e2fs, M_EXT2MNT);
1065cfe60390STomohiro Kusumi free(fs, M_EXT2MNT);
1066cfe60390STomohiro Kusumi free(ump, M_EXT2MNT);
1067cfe60390STomohiro Kusumi mp->mnt_data = NULL;
1068cfe60390STomohiro Kusumi mp->mnt_flag &= ~MNT_LOCAL;
1069cfe60390STomohiro Kusumi return (error);
1070cfe60390STomohiro Kusumi }
1071cfe60390STomohiro Kusumi
1072cfe60390STomohiro Kusumi /*
1073cfe60390STomohiro Kusumi * Flush out all the files in a filesystem.
1074cfe60390STomohiro Kusumi */
1075cfe60390STomohiro Kusumi static int
ext2_flushfiles(struct mount * mp,int flags)1076cfe60390STomohiro Kusumi ext2_flushfiles(struct mount *mp, int flags)
1077cfe60390STomohiro Kusumi {
1078cfe60390STomohiro Kusumi int error;
1079cfe60390STomohiro Kusumi
1080cfe60390STomohiro Kusumi error = vflush(mp, 0, flags);
1081cfe60390STomohiro Kusumi return (error);
1082cfe60390STomohiro Kusumi }
1083cfe60390STomohiro Kusumi
1084cfe60390STomohiro Kusumi /*
1085cfe60390STomohiro Kusumi * Get filesystem statistics.
1086cfe60390STomohiro Kusumi */
1087cfe60390STomohiro Kusumi static int
ext2_statfs(struct mount * mp,struct statfs * sbp,struct ucred * cred)1088cfe60390STomohiro Kusumi ext2_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1089cfe60390STomohiro Kusumi {
1090cfe60390STomohiro Kusumi struct ext2mount *ump;
1091cfe60390STomohiro Kusumi struct m_ext2fs *fs;
1092cfe60390STomohiro Kusumi uint32_t overhead, overhead_per_group, ngdb;
1093cfe60390STomohiro Kusumi int i, ngroups;
1094cfe60390STomohiro Kusumi
1095cfe60390STomohiro Kusumi ump = VFSTOEXT2(mp);
1096cfe60390STomohiro Kusumi fs = ump->um_e2fs;
1097cfe60390STomohiro Kusumi if (le16toh(fs->e2fs->e2fs_magic) != E2FS_MAGIC)
1098cfe60390STomohiro Kusumi panic("ext2_statfs");
1099cfe60390STomohiro Kusumi
1100cfe60390STomohiro Kusumi /*
1101cfe60390STomohiro Kusumi * Compute the overhead (FS structures)
1102cfe60390STomohiro Kusumi */
1103cfe60390STomohiro Kusumi overhead_per_group =
1104cfe60390STomohiro Kusumi 1 /* block bitmap */ +
1105cfe60390STomohiro Kusumi 1 /* inode bitmap */ +
1106cfe60390STomohiro Kusumi fs->e2fs_itpg;
1107cfe60390STomohiro Kusumi overhead = le32toh(fs->e2fs->e2fs_first_dblock) +
1108cfe60390STomohiro Kusumi fs->e2fs_gcount * overhead_per_group;
1109cfe60390STomohiro Kusumi if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1110cfe60390STomohiro Kusumi le32toh(fs->e2fs->e2fs_features_rocompat) & EXT2F_ROCOMPAT_SPARSESUPER) {
1111cfe60390STomohiro Kusumi for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
1112cfe60390STomohiro Kusumi if (ext2_cg_has_sb(fs, i))
1113cfe60390STomohiro Kusumi ngroups++;
1114cfe60390STomohiro Kusumi }
1115cfe60390STomohiro Kusumi } else {
1116cfe60390STomohiro Kusumi ngroups = fs->e2fs_gcount;
1117cfe60390STomohiro Kusumi }
1118cfe60390STomohiro Kusumi ngdb = fs->e2fs_gdbcount;
1119cfe60390STomohiro Kusumi if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1120cfe60390STomohiro Kusumi le32toh(fs->e2fs->e2fs_features_compat) & EXT2F_COMPAT_RESIZE)
1121cfe60390STomohiro Kusumi ngdb += le16toh(fs->e2fs->e2fs_reserved_ngdb);
1122cfe60390STomohiro Kusumi overhead += ngroups * (1 /* superblock */ + ngdb);
1123cfe60390STomohiro Kusumi
1124cfe60390STomohiro Kusumi sbp->f_type = mp->mnt_vfc->vfc_typenum;
1125cfe60390STomohiro Kusumi sbp->f_bsize = EXT2_FRAG_SIZE(fs);
1126cfe60390STomohiro Kusumi sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
1127cfe60390STomohiro Kusumi sbp->f_blocks = fs->e2fs_bcount - overhead;
1128cfe60390STomohiro Kusumi sbp->f_bfree = fs->e2fs_fbcount;
1129cfe60390STomohiro Kusumi sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
1130cfe60390STomohiro Kusumi sbp->f_files = le32toh(fs->e2fs->e2fs_icount);
1131cfe60390STomohiro Kusumi sbp->f_ffree = fs->e2fs_ficount;
1132cfe60390STomohiro Kusumi if (sbp != &mp->mnt_stat) {
1133cfe60390STomohiro Kusumi bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
1134cfe60390STomohiro Kusumi (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
1135cfe60390STomohiro Kusumi }
1136cfe60390STomohiro Kusumi return (0);
1137cfe60390STomohiro Kusumi }
1138cfe60390STomohiro Kusumi
1139cfe60390STomohiro Kusumi static int
ext2_statvfs(struct mount * mp,struct statvfs * sbp,struct ucred * cred)1140cfe60390STomohiro Kusumi ext2_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1141cfe60390STomohiro Kusumi {
1142cfe60390STomohiro Kusumi struct ext2mount *ump;
1143cfe60390STomohiro Kusumi struct m_ext2fs *fs;
1144cfe60390STomohiro Kusumi uint32_t overhead, overhead_per_group, ngdb;
1145cfe60390STomohiro Kusumi int i, ngroups;
1146cfe60390STomohiro Kusumi
1147cfe60390STomohiro Kusumi ump = VFSTOEXT2(mp);
1148cfe60390STomohiro Kusumi fs = ump->um_e2fs;
1149cfe60390STomohiro Kusumi if (le16toh(fs->e2fs->e2fs_magic) != E2FS_MAGIC)
1150cfe60390STomohiro Kusumi panic("ext2_statfs");
1151cfe60390STomohiro Kusumi
1152cfe60390STomohiro Kusumi /*
1153cfe60390STomohiro Kusumi * Compute the overhead (FS structures)
1154cfe60390STomohiro Kusumi */
1155cfe60390STomohiro Kusumi overhead_per_group =
1156cfe60390STomohiro Kusumi 1 /* block bitmap */ +
1157cfe60390STomohiro Kusumi 1 /* inode bitmap */ +
1158cfe60390STomohiro Kusumi fs->e2fs_itpg;
1159cfe60390STomohiro Kusumi overhead = le32toh(fs->e2fs->e2fs_first_dblock) +
1160cfe60390STomohiro Kusumi fs->e2fs_gcount * overhead_per_group;
1161cfe60390STomohiro Kusumi if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1162cfe60390STomohiro Kusumi le32toh(fs->e2fs->e2fs_features_rocompat) & EXT2F_ROCOMPAT_SPARSESUPER) {
1163cfe60390STomohiro Kusumi for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
1164cfe60390STomohiro Kusumi if (ext2_cg_has_sb(fs, i))
1165cfe60390STomohiro Kusumi ngroups++;
1166cfe60390STomohiro Kusumi }
1167cfe60390STomohiro Kusumi } else {
1168cfe60390STomohiro Kusumi ngroups = fs->e2fs_gcount;
1169cfe60390STomohiro Kusumi }
1170cfe60390STomohiro Kusumi ngdb = fs->e2fs_gdbcount;
1171cfe60390STomohiro Kusumi if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1172cfe60390STomohiro Kusumi le32toh(fs->e2fs->e2fs_features_compat) & EXT2F_COMPAT_RESIZE)
1173cfe60390STomohiro Kusumi ngdb += le16toh(fs->e2fs->e2fs_reserved_ngdb);
1174cfe60390STomohiro Kusumi overhead += ngroups * (1 /* superblock */ + ngdb);
1175cfe60390STomohiro Kusumi
1176cfe60390STomohiro Kusumi sbp->f_type = mp->mnt_vfc->vfc_typenum;
1177cfe60390STomohiro Kusumi sbp->f_bsize = EXT2_FRAG_SIZE(fs);
1178cfe60390STomohiro Kusumi sbp->f_frsize = EXT2_BLOCK_SIZE(fs);
1179cfe60390STomohiro Kusumi sbp->f_blocks = fs->e2fs_bcount - overhead;
1180cfe60390STomohiro Kusumi sbp->f_bfree = fs->e2fs_fbcount;
1181cfe60390STomohiro Kusumi sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
1182cfe60390STomohiro Kusumi sbp->f_files = le32toh(fs->e2fs->e2fs_icount);
1183cfe60390STomohiro Kusumi sbp->f_ffree = fs->e2fs_ficount;
1184cfe60390STomohiro Kusumi return (0);
1185cfe60390STomohiro Kusumi }
1186cfe60390STomohiro Kusumi
1187cfe60390STomohiro Kusumi static int
ext2_sync_scan(struct mount * mp,struct vnode * vp,void * data)1188cfe60390STomohiro Kusumi ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
1189cfe60390STomohiro Kusumi {
1190cfe60390STomohiro Kusumi struct scaninfo *info = data;
1191cfe60390STomohiro Kusumi struct inode *ip;
1192cfe60390STomohiro Kusumi int error;
1193cfe60390STomohiro Kusumi
1194cfe60390STomohiro Kusumi ip = VTOI(vp);
1195cfe60390STomohiro Kusumi if (vp->v_type == VNON ||
1196cfe60390STomohiro Kusumi ((ip->i_flag &
1197cfe60390STomohiro Kusumi (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1198cfe60390STomohiro Kusumi (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
1199cfe60390STomohiro Kusumi return (0);
1200cfe60390STomohiro Kusumi }
1201cfe60390STomohiro Kusumi if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
1202cfe60390STomohiro Kusumi info->allerror = error;
1203cfe60390STomohiro Kusumi return (0);
1204cfe60390STomohiro Kusumi }
1205cfe60390STomohiro Kusumi
1206cfe60390STomohiro Kusumi /*
1207cfe60390STomohiro Kusumi * Go through the disk queues to initiate sandbagged IO;
1208cfe60390STomohiro Kusumi * go through the inodes to write those that have been modified;
1209cfe60390STomohiro Kusumi * initiate the writing of the super block if it has been modified.
1210cfe60390STomohiro Kusumi *
1211cfe60390STomohiro Kusumi * Note: we are always called with the filesystem marked `MPBUSY'.
1212cfe60390STomohiro Kusumi */
1213cfe60390STomohiro Kusumi static int
ext2_sync(struct mount * mp,int waitfor)1214cfe60390STomohiro Kusumi ext2_sync(struct mount *mp, int waitfor)
1215cfe60390STomohiro Kusumi {
1216cfe60390STomohiro Kusumi struct ext2mount *ump = VFSTOEXT2(mp);
1217cfe60390STomohiro Kusumi struct m_ext2fs *fs;
1218cfe60390STomohiro Kusumi struct scaninfo scaninfo;
1219cfe60390STomohiro Kusumi int error;
1220cfe60390STomohiro Kusumi
1221cfe60390STomohiro Kusumi fs = ump->um_e2fs;
1222cfe60390STomohiro Kusumi if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
1223cfe60390STomohiro Kusumi panic("ext2_sync: rofs mod fs=%s", fs->e2fs_fsmnt);
1224cfe60390STomohiro Kusumi }
1225cfe60390STomohiro Kusumi
1226cfe60390STomohiro Kusumi /*
1227cfe60390STomohiro Kusumi * Write back each (modified) inode.
1228cfe60390STomohiro Kusumi */
1229cfe60390STomohiro Kusumi scaninfo.allerror = 0;
1230cfe60390STomohiro Kusumi scaninfo.rescan = 1;
1231cfe60390STomohiro Kusumi scaninfo.waitfor = waitfor;
1232cfe60390STomohiro Kusumi while (scaninfo.rescan) {
1233cfe60390STomohiro Kusumi scaninfo.rescan = 0;
1234cfe60390STomohiro Kusumi vmntvnodescan(mp, VMSC_GETVP | VMSC_NOWAIT,
1235cfe60390STomohiro Kusumi NULL, ext2_sync_scan, &scaninfo);
1236cfe60390STomohiro Kusumi }
1237cfe60390STomohiro Kusumi
1238cfe60390STomohiro Kusumi /*
1239cfe60390STomohiro Kusumi * Force stale filesystem control information to be flushed.
1240cfe60390STomohiro Kusumi */
1241cfe60390STomohiro Kusumi if ((waitfor & MNT_LAZY) == 0) {
1242cfe60390STomohiro Kusumi vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1243cfe60390STomohiro Kusumi if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
1244cfe60390STomohiro Kusumi scaninfo.allerror = error;
1245cfe60390STomohiro Kusumi vn_unlock(ump->um_devvp);
1246cfe60390STomohiro Kusumi }
1247cfe60390STomohiro Kusumi
1248cfe60390STomohiro Kusumi /*
1249cfe60390STomohiro Kusumi * Write back modified superblock.
1250cfe60390STomohiro Kusumi */
1251cfe60390STomohiro Kusumi if (fs->e2fs_fmod != 0) {
1252cfe60390STomohiro Kusumi fs->e2fs_fmod = 0;
1253cfe60390STomohiro Kusumi fs->e2fs->e2fs_wtime = htole32(time_second);
1254cfe60390STomohiro Kusumi if ((error = ext2_cgupdate(ump, waitfor)) != 0)
1255cfe60390STomohiro Kusumi scaninfo.allerror = error;
1256cfe60390STomohiro Kusumi }
1257cfe60390STomohiro Kusumi return (scaninfo.allerror);
1258cfe60390STomohiro Kusumi }
1259cfe60390STomohiro Kusumi
1260ffd4f0afSTomohiro Kusumi int
ext2_alloc_vnode(struct mount * mp,ino_t ino,struct vnode ** vpp)1261ffd4f0afSTomohiro Kusumi ext2_alloc_vnode(struct mount *mp, ino_t ino, struct vnode **vpp)
1262cfe60390STomohiro Kusumi {
1263cfe60390STomohiro Kusumi struct ext2mount *ump;
1264cfe60390STomohiro Kusumi struct vnode *vp;
1265ffd4f0afSTomohiro Kusumi struct inode *ip;
1266cfe60390STomohiro Kusumi int error;
1267cfe60390STomohiro Kusumi
1268cfe60390STomohiro Kusumi ump = VFSTOEXT2(mp);
1269cfe60390STomohiro Kusumi /*
1270cfe60390STomohiro Kusumi * Lock out the creation of new entries in the FFS hash table in
1271cfe60390STomohiro Kusumi * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1272cfe60390STomohiro Kusumi * may occur!
1273cfe60390STomohiro Kusumi */
1274cfe60390STomohiro Kusumi if (ext2fs_inode_hash_lock) {
1275cfe60390STomohiro Kusumi while (ext2fs_inode_hash_lock) {
1276cfe60390STomohiro Kusumi ext2fs_inode_hash_lock = -1;
1277cfe60390STomohiro Kusumi tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
1278cfe60390STomohiro Kusumi }
1279ffd4f0afSTomohiro Kusumi return (-1);
1280cfe60390STomohiro Kusumi }
1281cfe60390STomohiro Kusumi ext2fs_inode_hash_lock = 1;
1282cfe60390STomohiro Kusumi
1283cfe60390STomohiro Kusumi ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
1284cfe60390STomohiro Kusumi
1285cfe60390STomohiro Kusumi /* Allocate a new vnode/inode. */
1286cfe60390STomohiro Kusumi if ((error = getnewvnode(VT_EXT2FS, mp, &vp, VLKTIMEOUT,
1287cfe60390STomohiro Kusumi LK_CANRECURSE)) != 0) {
1288cfe60390STomohiro Kusumi if (ext2fs_inode_hash_lock < 0)
1289cfe60390STomohiro Kusumi wakeup(&ext2fs_inode_hash_lock);
1290cfe60390STomohiro Kusumi ext2fs_inode_hash_lock = 0;
1291cfe60390STomohiro Kusumi *vpp = NULL;
1292cfe60390STomohiro Kusumi free(ip, M_EXT2NODE);
1293cfe60390STomohiro Kusumi return (error);
1294cfe60390STomohiro Kusumi }
12955df373bcSTomohiro Kusumi //lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1296cfe60390STomohiro Kusumi vp->v_data = ip;
1297cfe60390STomohiro Kusumi ip->i_vnode = vp;
1298ffd4f0afSTomohiro Kusumi ip->i_e2fs = ump->um_e2fs;
12995df373bcSTomohiro Kusumi ip->i_dev = ump->um_dev;
1300cfe60390STomohiro Kusumi ip->i_ump = ump;
1301cfe60390STomohiro Kusumi ip->i_number = ino;
1302ffd4f0afSTomohiro Kusumi ip->i_block_group = ino_to_cg(ip->i_e2fs, ino);
13035df373bcSTomohiro Kusumi ip->i_next_alloc_block = 0;
13045df373bcSTomohiro Kusumi ip->i_next_alloc_goal = 0;
1305cfe60390STomohiro Kusumi
1306cfe60390STomohiro Kusumi /*
1307cfe60390STomohiro Kusumi * Put it onto its hash chain. Since our vnode is locked, other
1308cfe60390STomohiro Kusumi * requests for this inode will block if they arrive while we are
1309cfe60390STomohiro Kusumi * sleeping waiting for old data structures to be purged or for the
1310cfe60390STomohiro Kusumi * contents of the disk portion of this inode to be read.
1311cfe60390STomohiro Kusumi */
1312cfe60390STomohiro Kusumi if (ext2_ihashins(ip)) {
1313078fc6fdSTomohiro Kusumi printf("ext2_alloc_vnode: ihashins collision, retrying inode %ld\n",
1314cfe60390STomohiro Kusumi (long)ip->i_number);
1315cfe60390STomohiro Kusumi *vpp = NULL;
1316cfe60390STomohiro Kusumi vp->v_type = VBAD;
1317cfe60390STomohiro Kusumi vx_put(vp);
1318cfe60390STomohiro Kusumi free(ip, M_EXT2NODE);
1319ffd4f0afSTomohiro Kusumi return (-1);
1320cfe60390STomohiro Kusumi }
1321cfe60390STomohiro Kusumi
1322cfe60390STomohiro Kusumi if (ext2fs_inode_hash_lock < 0)
1323cfe60390STomohiro Kusumi wakeup(&ext2fs_inode_hash_lock);
1324cfe60390STomohiro Kusumi ext2fs_inode_hash_lock = 0;
1325ffd4f0afSTomohiro Kusumi *vpp = vp;
1326ffd4f0afSTomohiro Kusumi
1327ffd4f0afSTomohiro Kusumi return (0);
1328ffd4f0afSTomohiro Kusumi }
1329ffd4f0afSTomohiro Kusumi
1330ffd4f0afSTomohiro Kusumi /*
1331ffd4f0afSTomohiro Kusumi * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
1332ffd4f0afSTomohiro Kusumi * in from disk. If it is in core, wait for the lock bit to clear, then
1333ffd4f0afSTomohiro Kusumi * return the inode locked. Detection and handling of mount points must be
1334ffd4f0afSTomohiro Kusumi * done by the calling routine.
1335ffd4f0afSTomohiro Kusumi */
1336ffd4f0afSTomohiro Kusumi static int
ext2_vget(struct mount * mp,struct vnode * dvp,ino_t ino,struct vnode ** vpp)1337ffd4f0afSTomohiro Kusumi ext2_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
1338ffd4f0afSTomohiro Kusumi {
1339ffd4f0afSTomohiro Kusumi struct m_ext2fs *fs;
1340ffd4f0afSTomohiro Kusumi struct inode *ip;
1341ffd4f0afSTomohiro Kusumi struct ext2mount *ump;
1342ffd4f0afSTomohiro Kusumi struct buf *bp;
1343ffd4f0afSTomohiro Kusumi struct vnode *vp;
1344ffd4f0afSTomohiro Kusumi unsigned int i, used_blocks;
1345ffd4f0afSTomohiro Kusumi int error;
1346ffd4f0afSTomohiro Kusumi
1347ffd4f0afSTomohiro Kusumi ump = VFSTOEXT2(mp);
1348ffd4f0afSTomohiro Kusumi restart:
1349ffd4f0afSTomohiro Kusumi if ((*vpp = ext2_ihashget(ump->um_dev, ino)) != NULL)
1350ffd4f0afSTomohiro Kusumi return (0);
1351ffd4f0afSTomohiro Kusumi if (ext2_alloc_vnode(mp, ino, &vp) == -1)
1352ffd4f0afSTomohiro Kusumi goto restart;
1353ffd4f0afSTomohiro Kusumi ip = VTOI(vp);
1354ffd4f0afSTomohiro Kusumi fs = ip->i_e2fs;
1355cfe60390STomohiro Kusumi
1356cfe60390STomohiro Kusumi /* Read in the disk contents for the inode, copy into the inode. */
1357e5b38eb5STomohiro Kusumi if ((error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1358cfe60390STomohiro Kusumi (int)fs->e2fs_bsize, &bp)) != 0) {
1359cfe60390STomohiro Kusumi /*
1360cfe60390STomohiro Kusumi * The inode does not contain anything useful, so it would
1361cfe60390STomohiro Kusumi * be misleading to leave it on its hash chain. With mode
1362cfe60390STomohiro Kusumi * still zero, it will be unlinked and returned to the free
1363cfe60390STomohiro Kusumi * list by vput().
1364cfe60390STomohiro Kusumi */
1365cfe60390STomohiro Kusumi vp->v_type = VBAD;
1366e5b38eb5STomohiro Kusumi brelse(bp);
1367cfe60390STomohiro Kusumi vx_put(vp);
1368cfe60390STomohiro Kusumi *vpp = NULL;
1369cfe60390STomohiro Kusumi return (error);
1370cfe60390STomohiro Kusumi }
1371cfe60390STomohiro Kusumi /* convert ext2 inode to dinode */
1372cfe60390STomohiro Kusumi error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
1373cfe60390STomohiro Kusumi EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ino)), ip);
1374cfe60390STomohiro Kusumi if (error) {
1375e5b38eb5STomohiro Kusumi brelse(bp);
1376cfe60390STomohiro Kusumi vx_put(vp);
1377cfe60390STomohiro Kusumi *vpp = NULL;
1378cfe60390STomohiro Kusumi return (error);
1379cfe60390STomohiro Kusumi }
1380cfe60390STomohiro Kusumi
1381cfe60390STomohiro Kusumi /*
1382cfe60390STomohiro Kusumi * Now we want to make sure that block pointers for unused
1383cfe60390STomohiro Kusumi * blocks are zeroed out - ext2_balloc depends on this
1384cfe60390STomohiro Kusumi * although for regular files and directories only
1385cfe60390STomohiro Kusumi *
1386cfe60390STomohiro Kusumi * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
1387cfe60390STomohiro Kusumi * out because we could corrupt the extent tree.
1388cfe60390STomohiro Kusumi */
1389cfe60390STomohiro Kusumi if (!(ip->i_flag & IN_E4EXTENTS) &&
1390cfe60390STomohiro Kusumi (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
1391cfe60390STomohiro Kusumi used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
1392cfe60390STomohiro Kusumi for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1393cfe60390STomohiro Kusumi ip->i_db[i] = 0;
1394cfe60390STomohiro Kusumi }
1395cfe60390STomohiro Kusumi #ifdef EXT2FS_PRINT_EXTENTS
1396cfe60390STomohiro Kusumi ext2_print_inode(ip);
1397cfe60390STomohiro Kusumi ext4_ext_print_extent_tree_status(ip);
1398cfe60390STomohiro Kusumi #endif
1399e5b38eb5STomohiro Kusumi bqrelse(bp);
1400cfe60390STomohiro Kusumi
1401cfe60390STomohiro Kusumi /*
1402cfe60390STomohiro Kusumi * Initialize the vnode from the inode, check for aliases.
1403cfe60390STomohiro Kusumi * Note that the underlying vnode may have changed.
1404cfe60390STomohiro Kusumi */
1405cfe60390STomohiro Kusumi if ((error = ext2_vinit(mp, &vp)) != 0) {
1406cfe60390STomohiro Kusumi vx_put(vp);
1407cfe60390STomohiro Kusumi *vpp = NULL;
1408cfe60390STomohiro Kusumi return (error);
1409cfe60390STomohiro Kusumi }
1410cfe60390STomohiro Kusumi
1411cfe60390STomohiro Kusumi /*
1412cfe60390STomohiro Kusumi * Finish inode initialization now that aliasing has been resolved.
1413cfe60390STomohiro Kusumi */
1414cfe60390STomohiro Kusumi vref(ip->i_devvp);
1415cfe60390STomohiro Kusumi /*
1416cfe60390STomohiro Kusumi * Set up a generation number for this inode if it does not
1417cfe60390STomohiro Kusumi * already have one. This should only happen on old filesystems.
1418cfe60390STomohiro Kusumi */
1419cfe60390STomohiro Kusumi if (ip->i_gen == 0) {
1420cfe60390STomohiro Kusumi ip->i_gen = krandom() / 2 + 1;
1421cfe60390STomohiro Kusumi if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1422cfe60390STomohiro Kusumi ip->i_flag |= IN_MODIFIED;
1423cfe60390STomohiro Kusumi }
1424cfe60390STomohiro Kusumi /*
1425cfe60390STomohiro Kusumi * Return the locked and refd vnode.
1426cfe60390STomohiro Kusumi */
1427cfe60390STomohiro Kusumi vx_downgrade(vp); /* downgrade VX lock to VN lock */
1428cfe60390STomohiro Kusumi *vpp = vp;
1429cfe60390STomohiro Kusumi
1430cfe60390STomohiro Kusumi return (0);
1431cfe60390STomohiro Kusumi }
1432cfe60390STomohiro Kusumi
1433cfe60390STomohiro Kusumi /*
1434cfe60390STomohiro Kusumi * File handle to vnode
1435cfe60390STomohiro Kusumi *
1436cfe60390STomohiro Kusumi * Have to be really careful about stale file handles:
1437cfe60390STomohiro Kusumi * - check that the inode number is valid
1438cfe60390STomohiro Kusumi * - call ext2_vget() to get the locked inode
1439cfe60390STomohiro Kusumi * - check for an unallocated inode (i_mode == 0)
1440cfe60390STomohiro Kusumi * - check that the given client host has export rights and return
1441cfe60390STomohiro Kusumi * those rights via. exflagsp and credanonp
1442cfe60390STomohiro Kusumi */
1443cfe60390STomohiro Kusumi static int
ext2_fhtovp(struct mount * mp,struct vnode * rootvp,struct fid * fhp,struct vnode ** vpp)1444cfe60390STomohiro Kusumi ext2_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp,
1445cfe60390STomohiro Kusumi struct vnode **vpp)
1446cfe60390STomohiro Kusumi {
1447cfe60390STomohiro Kusumi struct inode *ip;
1448cfe60390STomohiro Kusumi struct ufid *ufhp;
1449cfe60390STomohiro Kusumi struct vnode *nvp;
1450cfe60390STomohiro Kusumi struct m_ext2fs *fs;
1451cfe60390STomohiro Kusumi int error;
1452cfe60390STomohiro Kusumi
1453cfe60390STomohiro Kusumi ufhp = (struct ufid *)fhp;
1454cfe60390STomohiro Kusumi fs = VFSTOEXT2(mp)->um_e2fs;
1455cfe60390STomohiro Kusumi if (ufhp->ufid_ino < EXT2_ROOTINO ||
1456cfe60390STomohiro Kusumi ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs_ipg)
1457cfe60390STomohiro Kusumi return (ESTALE);
1458cfe60390STomohiro Kusumi
1459f241f77eSTomohiro Kusumi error = VFS_VGET(mp, NULL, ufhp->ufid_ino, &nvp);
1460cfe60390STomohiro Kusumi if (error) {
1461cfe60390STomohiro Kusumi *vpp = NULLVP;
1462cfe60390STomohiro Kusumi return (error);
1463cfe60390STomohiro Kusumi }
1464cfe60390STomohiro Kusumi ip = VTOI(nvp);
1465cfe60390STomohiro Kusumi if (ip->i_mode == 0 ||
1466cfe60390STomohiro Kusumi ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1467cfe60390STomohiro Kusumi vput(nvp);
1468cfe60390STomohiro Kusumi *vpp = NULLVP;
1469cfe60390STomohiro Kusumi return (ESTALE);
1470cfe60390STomohiro Kusumi }
1471cfe60390STomohiro Kusumi *vpp = nvp;
1472cfe60390STomohiro Kusumi return (0);
1473cfe60390STomohiro Kusumi }
1474cfe60390STomohiro Kusumi
1475cfe60390STomohiro Kusumi /*
1476cfe60390STomohiro Kusumi * Vnode pointer to File handle
1477cfe60390STomohiro Kusumi */
1478cfe60390STomohiro Kusumi /* ARGSUSED */
1479cfe60390STomohiro Kusumi static int
ext2_vptofh(struct vnode * vp,struct fid * fhp)1480cfe60390STomohiro Kusumi ext2_vptofh(struct vnode *vp, struct fid *fhp)
1481cfe60390STomohiro Kusumi {
1482cfe60390STomohiro Kusumi struct inode *ip;
1483cfe60390STomohiro Kusumi struct ufid *ufhp;
1484cfe60390STomohiro Kusumi
1485cfe60390STomohiro Kusumi ip = VTOI(vp);
1486cfe60390STomohiro Kusumi ufhp = (struct ufid *)fhp;
1487cfe60390STomohiro Kusumi ufhp->ufid_len = sizeof(struct ufid);
1488cfe60390STomohiro Kusumi ufhp->ufid_ino = ip->i_number;
1489cfe60390STomohiro Kusumi ufhp->ufid_gen = ip->i_gen;
1490cfe60390STomohiro Kusumi return (0);
1491cfe60390STomohiro Kusumi }
1492cfe60390STomohiro Kusumi
1493cfe60390STomohiro Kusumi /*
1494cfe60390STomohiro Kusumi * This is the generic part of fhtovp called after the underlying
1495cfe60390STomohiro Kusumi * filesystem has validated the file handle.
1496cfe60390STomohiro Kusumi *
1497cfe60390STomohiro Kusumi * Verify that a host should have access to a filesystem.
1498cfe60390STomohiro Kusumi */
1499cfe60390STomohiro Kusumi static int
ext2_check_export(struct mount * mp,struct sockaddr * nam,int * exflagsp,struct ucred ** credanonp)1500cfe60390STomohiro Kusumi ext2_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
1501cfe60390STomohiro Kusumi struct ucred **credanonp)
1502cfe60390STomohiro Kusumi {
1503cfe60390STomohiro Kusumi struct netcred *np;
1504cfe60390STomohiro Kusumi struct ext2mount *ump;
1505cfe60390STomohiro Kusumi
1506cfe60390STomohiro Kusumi ump = VFSTOEXT2(mp);
1507cfe60390STomohiro Kusumi /*
1508cfe60390STomohiro Kusumi * Get the export permission structure for this <mp, client> tuple.
1509cfe60390STomohiro Kusumi */
1510cfe60390STomohiro Kusumi np = vfs_export_lookup(mp, &ump->um_export, nam);
1511cfe60390STomohiro Kusumi if (np == NULL)
1512cfe60390STomohiro Kusumi return (EACCES);
1513cfe60390STomohiro Kusumi
1514cfe60390STomohiro Kusumi *exflagsp = np->netc_exflags;
1515cfe60390STomohiro Kusumi *credanonp = &np->netc_anon;
1516cfe60390STomohiro Kusumi return (0);
1517cfe60390STomohiro Kusumi }
1518cfe60390STomohiro Kusumi
1519cfe60390STomohiro Kusumi /*
1520cfe60390STomohiro Kusumi * Write a superblock and associated information back to disk.
1521cfe60390STomohiro Kusumi */
1522cfe60390STomohiro Kusumi static int
ext2_sbupdate(struct ext2mount * mp,int waitfor)1523cfe60390STomohiro Kusumi ext2_sbupdate(struct ext2mount *mp, int waitfor)
1524cfe60390STomohiro Kusumi {
1525cfe60390STomohiro Kusumi struct m_ext2fs *fs = mp->um_e2fs;
1526cfe60390STomohiro Kusumi struct ext2fs *es = fs->e2fs;
1527cfe60390STomohiro Kusumi struct buf *bp;
1528cfe60390STomohiro Kusumi int error = 0;
1529cfe60390STomohiro Kusumi
1530cfe60390STomohiro Kusumi es->e2fs_bcount = htole32(fs->e2fs_bcount & 0xffffffff);
1531cfe60390STomohiro Kusumi es->e2fs_rbcount = htole32(fs->e2fs_rbcount & 0xffffffff);
1532cfe60390STomohiro Kusumi es->e2fs_fbcount = htole32(fs->e2fs_fbcount & 0xffffffff);
1533cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1534cfe60390STomohiro Kusumi es->e4fs_bcount_hi = htole32(fs->e2fs_bcount >> 32);
1535cfe60390STomohiro Kusumi es->e4fs_rbcount_hi = htole32(fs->e2fs_rbcount >> 32);
1536cfe60390STomohiro Kusumi es->e4fs_fbcount_hi = htole32(fs->e2fs_fbcount >> 32);
1537cfe60390STomohiro Kusumi }
1538cfe60390STomohiro Kusumi
1539cfe60390STomohiro Kusumi es->e2fs_ficount = htole32(fs->e2fs_ficount);
1540cfe60390STomohiro Kusumi
1541cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1542cfe60390STomohiro Kusumi ext2_sb_csum_set(fs);
1543cfe60390STomohiro Kusumi
1544cfe60390STomohiro Kusumi bp = getblk(mp->um_devvp, SBOFF, SBSIZE, 0, 0);
1545cfe60390STomohiro Kusumi bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1546cfe60390STomohiro Kusumi if (waitfor == MNT_WAIT)
1547cfe60390STomohiro Kusumi error = bwrite(bp);
1548cfe60390STomohiro Kusumi else
1549cfe60390STomohiro Kusumi bawrite(bp);
1550cfe60390STomohiro Kusumi
1551cfe60390STomohiro Kusumi /*
1552cfe60390STomohiro Kusumi * The buffers for group descriptors, inode bitmaps and block bitmaps
1553cfe60390STomohiro Kusumi * are not busy at this point and are (hopefully) written by the
1554cfe60390STomohiro Kusumi * usual sync mechanism. No need to write them here.
1555cfe60390STomohiro Kusumi */
1556cfe60390STomohiro Kusumi return (error);
1557cfe60390STomohiro Kusumi }
1558cfe60390STomohiro Kusumi
1559cfe60390STomohiro Kusumi static int
ext2_cgupdate(struct ext2mount * mp,int waitfor)1560cfe60390STomohiro Kusumi ext2_cgupdate(struct ext2mount *mp, int waitfor)
1561cfe60390STomohiro Kusumi {
1562cfe60390STomohiro Kusumi struct m_ext2fs *fs = mp->um_e2fs;
1563cfe60390STomohiro Kusumi struct buf *bp;
1564cfe60390STomohiro Kusumi int i, j, g_count = 0, error = 0, allerror = 0;
1565cfe60390STomohiro Kusumi
1566cfe60390STomohiro Kusumi allerror = ext2_sbupdate(mp, waitfor);
1567cfe60390STomohiro Kusumi
1568cfe60390STomohiro Kusumi /* Update gd csums */
1569cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1570cfe60390STomohiro Kusumi EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1571cfe60390STomohiro Kusumi ext2_gd_csum_set(fs);
1572cfe60390STomohiro Kusumi
1573cfe60390STomohiro Kusumi for (i = 0; i < fs->e2fs_gdbcount; i++) {
1574cfe60390STomohiro Kusumi bp = getblk(mp->um_devvp, fsbtodoff(fs,
1575cfe60390STomohiro Kusumi ext2_cg_location(fs, i)),
1576cfe60390STomohiro Kusumi fs->e2fs_bsize, 0, 0);
1577cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1578cfe60390STomohiro Kusumi memcpy(bp->b_data, &fs->e2fs_gd[
1579cfe60390STomohiro Kusumi i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1580cfe60390STomohiro Kusumi fs->e2fs_bsize);
1581cfe60390STomohiro Kusumi } else {
1582cfe60390STomohiro Kusumi for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE &&
1583cfe60390STomohiro Kusumi g_count < fs->e2fs_gcount; j++, g_count++)
1584cfe60390STomohiro Kusumi memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE,
1585cfe60390STomohiro Kusumi &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE);
1586cfe60390STomohiro Kusumi }
1587cfe60390STomohiro Kusumi if (waitfor == MNT_WAIT)
1588cfe60390STomohiro Kusumi error = bwrite(bp);
1589cfe60390STomohiro Kusumi else
1590cfe60390STomohiro Kusumi bawrite(bp);
1591cfe60390STomohiro Kusumi }
1592cfe60390STomohiro Kusumi
1593cfe60390STomohiro Kusumi if (!allerror && error)
1594cfe60390STomohiro Kusumi allerror = error;
1595cfe60390STomohiro Kusumi return (allerror);
1596cfe60390STomohiro Kusumi }
1597cfe60390STomohiro Kusumi
1598cfe60390STomohiro Kusumi /*
1599cfe60390STomohiro Kusumi * Return the root of a filesystem.
1600cfe60390STomohiro Kusumi */
1601cfe60390STomohiro Kusumi static int
ext2_root(struct mount * mp,struct vnode ** vpp)1602cfe60390STomohiro Kusumi ext2_root(struct mount *mp, struct vnode **vpp)
1603cfe60390STomohiro Kusumi {
1604cfe60390STomohiro Kusumi struct vnode *nvp;
1605cfe60390STomohiro Kusumi int error;
1606cfe60390STomohiro Kusumi
1607cfe60390STomohiro Kusumi error = VFS_VGET(mp, NULL, (ino_t)EXT2_ROOTINO, &nvp);
1608cfe60390STomohiro Kusumi if (error)
1609cfe60390STomohiro Kusumi return (error);
1610cfe60390STomohiro Kusumi *vpp = nvp;
1611cfe60390STomohiro Kusumi return (0);
1612cfe60390STomohiro Kusumi }
1613cfe60390STomohiro Kusumi
1614cfe60390STomohiro Kusumi /*
1615cfe60390STomohiro Kusumi * Initialize ext2 filesystems, done only once.
1616cfe60390STomohiro Kusumi */
1617cfe60390STomohiro Kusumi static int
ext2_init(struct vfsconf * vfsp)1618cfe60390STomohiro Kusumi ext2_init(struct vfsconf *vfsp)
1619cfe60390STomohiro Kusumi {
1620cfe60390STomohiro Kusumi static int done;
1621cfe60390STomohiro Kusumi
1622cfe60390STomohiro Kusumi if (done)
1623cfe60390STomohiro Kusumi return (0);
1624cfe60390STomohiro Kusumi done = 1;
1625cfe60390STomohiro Kusumi ext2_ihashinit();
1626cfe60390STomohiro Kusumi
1627cfe60390STomohiro Kusumi return (0);
1628cfe60390STomohiro Kusumi }
1629cfe60390STomohiro Kusumi
1630cfe60390STomohiro Kusumi static int
ext2_uninit(struct vfsconf * vfsp)1631cfe60390STomohiro Kusumi ext2_uninit(struct vfsconf *vfsp)
1632cfe60390STomohiro Kusumi {
1633cfe60390STomohiro Kusumi
1634cfe60390STomohiro Kusumi ext2_ihashuninit();
1635cfe60390STomohiro Kusumi
1636cfe60390STomohiro Kusumi return (0);
1637cfe60390STomohiro Kusumi }
1638