ext2fs_vfsops.c revision 1.151 1 /* $NetBSD: ext2fs_vfsops.c,v 1.151 2009/10/19 18:41:17 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
32 * Modified for ext2fs by Manuel Bouyer.
33 */
34
35 /*
36 * Copyright (c) 1997 Manuel Bouyer.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 *
58 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
59 * Modified for ext2fs by Manuel Bouyer.
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.151 2009/10/19 18:41:17 bouyer Exp $");
64
65 #if defined(_KERNEL_OPT)
66 #include "opt_compat_netbsd.h"
67 #endif
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sysctl.h>
72 #include <sys/namei.h>
73 #include <sys/proc.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode.h>
76 #include <sys/socket.h>
77 #include <sys/mount.h>
78 #include <sys/buf.h>
79 #include <sys/device.h>
80 #include <sys/mbuf.h>
81 #include <sys/file.h>
82 #include <sys/disklabel.h>
83 #include <sys/ioctl.h>
84 #include <sys/errno.h>
85 #include <sys/malloc.h>
86 #include <sys/pool.h>
87 #include <sys/lock.h>
88 #include <sys/conf.h>
89 #include <sys/kauth.h>
90 #include <sys/module.h>
91
92 #include <miscfs/genfs/genfs.h>
93 #include <miscfs/specfs/specdev.h>
94
95 #include <ufs/ufs/quota.h>
96 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ufs/inode.h>
98 #include <ufs/ufs/dir.h>
99 #include <ufs/ufs/ufs_extern.h>
100
101 #include <ufs/ext2fs/ext2fs.h>
102 #include <ufs/ext2fs/ext2fs_dir.h>
103 #include <ufs/ext2fs/ext2fs_extern.h>
104
105 MODULE(MODULE_CLASS_VFS, ext2fs, "ffs");
106
107 int ext2fs_sbupdate(struct ufsmount *, int);
108 static int ext2fs_checksb(struct ext2fs *, int);
109 static void ext2fs_set_inode_guid(struct inode *);
110
111 static struct sysctllog *ext2fs_sysctl_log;
112
113 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
114 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
115 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
116
117 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
118 &ext2fs_vnodeop_opv_desc,
119 &ext2fs_specop_opv_desc,
120 &ext2fs_fifoop_opv_desc,
121 NULL,
122 };
123
124 struct vfsops ext2fs_vfsops = {
125 MOUNT_EXT2FS,
126 sizeof (struct ufs_args),
127 ext2fs_mount,
128 ufs_start,
129 ext2fs_unmount,
130 ufs_root,
131 ufs_quotactl,
132 ext2fs_statvfs,
133 ext2fs_sync,
134 ext2fs_vget,
135 ext2fs_fhtovp,
136 ext2fs_vptofh,
137 ext2fs_init,
138 ext2fs_reinit,
139 ext2fs_done,
140 ext2fs_mountroot,
141 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
142 vfs_stdextattrctl,
143 (void *)eopnotsupp, /* vfs_suspendctl */
144 genfs_renamelock_enter,
145 genfs_renamelock_exit,
146 (void *)eopnotsupp,
147 ext2fs_vnodeopv_descs,
148 0,
149 { NULL, NULL },
150 };
151
152 static const struct genfs_ops ext2fs_genfsops = {
153 .gop_size = genfs_size,
154 .gop_alloc = ext2fs_gop_alloc,
155 .gop_write = genfs_gop_write,
156 .gop_markupdate = ufs_gop_markupdate,
157 };
158
159 static const struct ufs_ops ext2fs_ufsops = {
160 .uo_itimes = ext2fs_itimes,
161 .uo_update = ext2fs_update,
162 .uo_vfree = ext2fs_vfree,
163 .uo_unmark_vnode = (void (*)(vnode_t *))nullop,
164 };
165
166 /* Fill in the inode uid/gid from ext2 halves. */
167 static void
168 ext2fs_set_inode_guid(struct inode *ip)
169 {
170
171 ip->i_gid = ip->i_e2fs_gid;
172 ip->i_uid = ip->i_e2fs_uid;
173 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
174 ip->i_gid |= ip->i_e2fs_gid_high << 16;
175 ip->i_uid |= ip->i_e2fs_uid_high << 16;
176 }
177 }
178
179 static int
180 ext2fs_modcmd(modcmd_t cmd, void *arg)
181 {
182 int error;
183
184 switch (cmd) {
185 case MODULE_CMD_INIT:
186 error = vfs_attach(&ext2fs_vfsops);
187 if (error != 0)
188 break;
189 sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
190 CTLFLAG_PERMANENT,
191 CTLTYPE_NODE, "vfs", NULL,
192 NULL, 0, NULL, 0,
193 CTL_VFS, CTL_EOL);
194 sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
195 CTLFLAG_PERMANENT,
196 CTLTYPE_NODE, "ext2fs",
197 SYSCTL_DESCR("Linux EXT2FS file system"),
198 NULL, 0, NULL, 0,
199 CTL_VFS, 17, CTL_EOL);
200 /*
201 * XXX the "17" above could be dynamic, thereby eliminating
202 * one more instance of the "number to vfs" mapping problem,
203 * but "17" is the order as taken from sys/mount.h
204 */
205 break;
206 case MODULE_CMD_FINI:
207 error = vfs_detach(&ext2fs_vfsops);
208 if (error != 0)
209 break;
210 sysctl_teardown(&ext2fs_sysctl_log);
211 break;
212 default:
213 error = ENOTTY;
214 break;
215 }
216
217 return (error);
218 }
219
220 /*
221 * XXX Same structure as FFS inodes? Should we share a common pool?
222 */
223 struct pool ext2fs_inode_pool;
224 struct pool ext2fs_dinode_pool;
225
226 extern u_long ext2gennumber;
227
228 void
229 ext2fs_init(void)
230 {
231
232 pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
233 "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
234 pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
235 "ext2dinopl", &pool_allocator_nointr, IPL_NONE);
236 ufs_init();
237 }
238
239 void
240 ext2fs_reinit(void)
241 {
242 ufs_reinit();
243 }
244
245 void
246 ext2fs_done(void)
247 {
248
249 ufs_done();
250 pool_destroy(&ext2fs_inode_pool);
251 pool_destroy(&ext2fs_dinode_pool);
252 }
253
254 /*
255 * Called by main() when ext2fs is going to be mounted as root.
256 *
257 * Name is updated by mount(8) after booting.
258 */
259 #define ROOTNAME "root_device"
260
261 int
262 ext2fs_mountroot(void)
263 {
264 extern struct vnode *rootvp;
265 struct m_ext2fs *fs;
266 struct mount *mp;
267 struct ufsmount *ump;
268 int error;
269
270 if (device_class(root_device) != DV_DISK)
271 return (ENODEV);
272
273 if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
274 vrele(rootvp);
275 return (error);
276 }
277
278 if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
279 vfs_unbusy(mp, false, NULL);
280 vfs_destroy(mp);
281 return (error);
282 }
283 mutex_enter(&mountlist_lock);
284 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
285 mutex_exit(&mountlist_lock);
286 ump = VFSTOUFS(mp);
287 fs = ump->um_e2fs;
288 memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
289 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
290 sizeof(fs->e2fs_fsmnt) - 1, 0);
291 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
292 memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
293 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
294 sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
295 }
296 (void)ext2fs_statvfs(mp, &mp->mnt_stat);
297 vfs_unbusy(mp, false, NULL);
298 setrootfstime((time_t)fs->e2fs.e2fs_wtime);
299 return (0);
300 }
301
302 /*
303 * VFS Operations.
304 *
305 * mount system call
306 */
307 int
308 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
309 {
310 struct lwp *l = curlwp;
311 struct vnode *devvp;
312 struct ufs_args *args = data;
313 struct ufsmount *ump = NULL;
314 struct m_ext2fs *fs;
315 size_t size;
316 int error = 0, flags, update;
317 mode_t accessmode;
318
319 if (*data_len < sizeof *args)
320 return EINVAL;
321
322 if (mp->mnt_flag & MNT_GETARGS) {
323 ump = VFSTOUFS(mp);
324 if (ump == NULL)
325 return EIO;
326 memset(args, 0, sizeof *args);
327 args->fspec = NULL;
328 *data_len = sizeof *args;
329 return 0;
330 }
331
332 update = mp->mnt_flag & MNT_UPDATE;
333
334 /* Check arguments */
335 if (args->fspec != NULL) {
336 /*
337 * Look up the name and verify that it's sane.
338 */
339 error = namei_simple_user(args->fspec,
340 NSM_FOLLOW_NOEMULROOT, &devvp);
341 if (error != 0)
342 return (error);
343
344 if (!update) {
345 /*
346 * Be sure this is a valid block device
347 */
348 if (devvp->v_type != VBLK)
349 error = ENOTBLK;
350 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
351 error = ENXIO;
352 } else {
353 /*
354 * Be sure we're still naming the same device
355 * used for our initial mount
356 */
357 ump = VFSTOUFS(mp);
358 if (devvp != ump->um_devvp) {
359 if (devvp->v_rdev != ump->um_devvp->v_rdev)
360 error = EINVAL;
361 else {
362 vrele(devvp);
363 devvp = ump->um_devvp;
364 vref(devvp);
365 }
366 }
367 }
368 } else {
369 if (!update) {
370 /* New mounts must have a filename for the device */
371 return (EINVAL);
372 } else {
373 ump = VFSTOUFS(mp);
374 devvp = ump->um_devvp;
375 vref(devvp);
376 }
377 }
378
379 /*
380 * If mount by non-root, then verify that user has necessary
381 * permissions on the device.
382 *
383 * Permission to update a mount is checked higher, so here we presume
384 * updating the mount is okay (for example, as far as securelevel goes)
385 * which leaves us with the normal check.
386 */
387 if (error == 0) {
388 accessmode = VREAD;
389 if (update ?
390 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
391 (mp->mnt_flag & MNT_RDONLY) == 0)
392 accessmode |= VWRITE;
393 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
394 error = genfs_can_mount(devvp, accessmode, l->l_cred);
395 VOP_UNLOCK(devvp, 0);
396 }
397
398 if (error) {
399 vrele(devvp);
400 return (error);
401 }
402
403 if (!update) {
404 int xflags;
405
406 if (mp->mnt_flag & MNT_RDONLY)
407 xflags = FREAD;
408 else
409 xflags = FREAD|FWRITE;
410 error = VOP_OPEN(devvp, xflags, FSCRED);
411 if (error)
412 goto fail;
413 error = ext2fs_mountfs(devvp, mp);
414 if (error) {
415 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
416 (void)VOP_CLOSE(devvp, xflags, NOCRED);
417 VOP_UNLOCK(devvp, 0);
418 goto fail;
419 }
420
421 ump = VFSTOUFS(mp);
422 fs = ump->um_e2fs;
423 } else {
424 /*
425 * Update the mount.
426 */
427
428 /*
429 * The initial mount got a reference on this
430 * device, so drop the one obtained via
431 * namei(), above.
432 */
433 vrele(devvp);
434
435 ump = VFSTOUFS(mp);
436 fs = ump->um_e2fs;
437 if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
438 /*
439 * Changing from r/w to r/o
440 */
441 flags = WRITECLOSE;
442 if (mp->mnt_flag & MNT_FORCE)
443 flags |= FORCECLOSE;
444 error = ext2fs_flushfiles(mp, flags);
445 if (error == 0 &&
446 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
447 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
448 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
449 (void) ext2fs_sbupdate(ump, MNT_WAIT);
450 }
451 if (error)
452 return (error);
453 fs->e2fs_ronly = 1;
454 }
455
456 if (mp->mnt_flag & MNT_RELOAD) {
457 error = ext2fs_reload(mp, l->l_cred, l);
458 if (error)
459 return (error);
460 }
461
462 if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
463 /*
464 * Changing from read-only to read/write
465 */
466 fs->e2fs_ronly = 0;
467 if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
468 fs->e2fs.e2fs_state = 0;
469 else
470 fs->e2fs.e2fs_state = E2FS_ERRORS;
471 fs->e2fs_fmod = 1;
472 }
473 if (args->fspec == NULL)
474 return 0;
475 }
476
477 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
478 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
479 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
480 sizeof(fs->e2fs_fsmnt) - 1, &size);
481 memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
482 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
483 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
484 sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
485 memset(fs->e2fs.e2fs_fsmnt, 0,
486 sizeof(fs->e2fs.e2fs_fsmnt) - size);
487 }
488 if (fs->e2fs_fmod != 0) { /* XXX */
489 fs->e2fs_fmod = 0;
490 if (fs->e2fs.e2fs_state == 0)
491 fs->e2fs.e2fs_wtime = time_second;
492 else
493 printf("%s: file system not clean; please fsck(8)\n",
494 mp->mnt_stat.f_mntfromname);
495 (void) ext2fs_cgupdate(ump, MNT_WAIT);
496 }
497 return (error);
498
499 fail:
500 vrele(devvp);
501 return (error);
502 }
503
504 /*
505 * Reload all incore data for a filesystem (used after running fsck on
506 * the root filesystem and finding things to fix). The filesystem must
507 * be mounted read-only.
508 *
509 * Things to do to update the mount:
510 * 1) invalidate all cached meta-data.
511 * 2) re-read superblock from disk.
512 * 3) re-read summary information from disk.
513 * 4) invalidate all inactive vnodes.
514 * 5) invalidate all cached file data.
515 * 6) re-read inode data for all active vnodes.
516 */
517 int
518 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
519 {
520 struct vnode *vp, *mvp, *devvp;
521 struct inode *ip;
522 struct buf *bp;
523 struct m_ext2fs *fs;
524 struct ext2fs *newfs;
525 struct partinfo dpart;
526 int i, size, error;
527 void *cp;
528 struct ufsmount *ump;
529
530 if ((mp->mnt_flag & MNT_RDONLY) == 0)
531 return (EINVAL);
532
533 ump = VFSTOUFS(mp);
534 /*
535 * Step 1: invalidate all cached meta-data.
536 */
537 devvp = ump->um_devvp;
538 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
539 error = vinvalbuf(devvp, 0, cred, l, 0, 0);
540 VOP_UNLOCK(devvp, 0);
541 if (error)
542 panic("ext2fs_reload: dirty1");
543 /*
544 * Step 2: re-read superblock from disk.
545 */
546 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED) != 0)
547 size = DEV_BSIZE;
548 else
549 size = dpart.disklab->d_secsize;
550 error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, 0, &bp);
551 if (error) {
552 brelse(bp, 0);
553 return (error);
554 }
555 newfs = (struct ext2fs *)bp->b_data;
556 error = ext2fs_checksb(newfs, (mp->mnt_flag & MNT_RDONLY) != 0);
557 if (error) {
558 brelse(bp, 0);
559 return (error);
560 }
561
562 fs = ump->um_e2fs;
563 /*
564 * copy in new superblock, and compute in-memory values
565 */
566 e2fs_sbload(newfs, &fs->e2fs);
567 fs->e2fs_ncg =
568 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
569 fs->e2fs.e2fs_bpg);
570 /* XXX assume hw bsize = 512 */
571 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
572 fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
573 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
574 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
575 fs->e2fs_bmask = ~fs->e2fs_qbmask;
576 fs->e2fs_ngdb =
577 howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
578 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
579 fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
580 brelse(bp, 0);
581
582 /*
583 * Step 3: re-read summary information from disk.
584 */
585
586 for (i = 0; i < fs->e2fs_ngdb; i++) {
587 error = bread(devvp ,
588 fsbtodb(fs, fs->e2fs.e2fs_first_dblock +
589 1 /* superblock */ + i),
590 fs->e2fs_bsize, NOCRED, 0, &bp);
591 if (error) {
592 brelse(bp, 0);
593 return (error);
594 }
595 e2fs_cgload((struct ext2_gd *)bp->b_data,
596 &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
597 fs->e2fs_bsize);
598 brelse(bp, 0);
599 }
600
601 /* Allocate a marker vnode. */
602 if ((mvp = vnalloc(mp)) == NULL)
603 return ENOMEM;
604 /*
605 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
606 * and vclean() can be called indirectly
607 */
608 mutex_enter(&mntvnode_lock);
609 loop:
610 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
611 vmark(mvp, vp);
612 if (vp->v_mount != mp || vismarker(vp))
613 continue;
614 /*
615 * Step 4: invalidate all inactive vnodes.
616 */
617 if (vrecycle(vp, &mntvnode_lock, l)) {
618 mutex_enter(&mntvnode_lock);
619 (void)vunmark(mvp);
620 goto loop;
621 }
622 /*
623 * Step 5: invalidate all cached file data.
624 */
625 mutex_enter(&vp->v_interlock);
626 mutex_exit(&mntvnode_lock);
627 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
628 mutex_enter(&mntvnode_lock);
629 (void)vunmark(mvp);
630 goto loop;
631 }
632 if (vinvalbuf(vp, 0, cred, l, 0, 0))
633 panic("ext2fs_reload: dirty2");
634 /*
635 * Step 6: re-read inode data for all active vnodes.
636 */
637 ip = VTOI(vp);
638 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
639 (int)fs->e2fs_bsize, NOCRED, 0, &bp);
640 if (error) {
641 vput(vp);
642 mutex_enter(&mntvnode_lock);
643 (void)vunmark(mvp);
644 break;
645 }
646 cp = (char *)bp->b_data +
647 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
648 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
649 ext2fs_set_inode_guid(ip);
650 brelse(bp, 0);
651 vput(vp);
652 mutex_enter(&mntvnode_lock);
653 }
654 mutex_exit(&mntvnode_lock);
655 vnfree(mvp);
656 return (error);
657 }
658
659 /*
660 * Common code for mount and mountroot
661 */
662 int
663 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
664 {
665 struct lwp *l = curlwp;
666 struct ufsmount *ump;
667 struct buf *bp;
668 struct ext2fs *fs;
669 struct m_ext2fs *m_fs;
670 dev_t dev;
671 struct partinfo dpart;
672 int error, i, size, ronly;
673 kauth_cred_t cred;
674 struct proc *p;
675
676 dev = devvp->v_rdev;
677 p = l ? l->l_proc : NULL;
678 cred = l ? l->l_cred : NOCRED;
679
680 /* Flush out any old buffers remaining from a previous use. */
681 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
682 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
683 VOP_UNLOCK(devvp, 0);
684 if (error)
685 return (error);
686
687 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
688 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) != 0)
689 size = DEV_BSIZE;
690 else
691 size = dpart.disklab->d_secsize;
692
693 bp = NULL;
694 ump = NULL;
695
696 #ifdef DEBUG_EXT2
697 printf("ext2 sb size: %d\n", sizeof(struct ext2fs));
698 #endif
699 error = bread(devvp, (SBOFF / size), SBSIZE, cred, 0, &bp);
700 if (error)
701 goto out;
702 fs = (struct ext2fs *)bp->b_data;
703 error = ext2fs_checksb(fs, ronly);
704 if (error)
705 goto out;
706 ump = malloc(sizeof(*ump), M_UFSMNT, M_WAITOK);
707 memset(ump, 0, sizeof(*ump));
708 ump->um_fstype = UFS1;
709 ump->um_ops = &ext2fs_ufsops;
710 ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
711 memset(ump->um_e2fs, 0, sizeof(struct m_ext2fs));
712 e2fs_sbload((struct ext2fs *)bp->b_data, &ump->um_e2fs->e2fs);
713 brelse(bp, 0);
714 bp = NULL;
715 m_fs = ump->um_e2fs;
716 m_fs->e2fs_ronly = ronly;
717
718 #ifdef DEBUG_EXT2
719 printf("ext2 ino size %d\n", EXT2_DINODE_SIZE(m_fs));
720 #endif
721 if (ronly == 0) {
722 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
723 m_fs->e2fs.e2fs_state = 0;
724 else
725 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
726 m_fs->e2fs_fmod = 1;
727 }
728
729 /* compute dynamic sb infos */
730 m_fs->e2fs_ncg =
731 howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
732 m_fs->e2fs.e2fs_bpg);
733 /* XXX assume hw bsize = 512 */
734 m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
735 m_fs->e2fs_bsize = MINBSIZE << m_fs->e2fs.e2fs_log_bsize;
736 m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
737 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
738 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
739 m_fs->e2fs_ngdb =
740 howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
741 m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
742 m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg / m_fs->e2fs_ipb;
743
744 m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
745 M_UFSMNT, M_WAITOK);
746 for (i = 0; i < m_fs->e2fs_ngdb; i++) {
747 error = bread(devvp ,
748 fsbtodb(m_fs, m_fs->e2fs.e2fs_first_dblock +
749 1 /* superblock */ + i),
750 m_fs->e2fs_bsize, NOCRED, 0, &bp);
751 if (error) {
752 free(m_fs->e2fs_gd, M_UFSMNT);
753 goto out;
754 }
755 e2fs_cgload((struct ext2_gd *)bp->b_data,
756 &m_fs->e2fs_gd[
757 i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
758 m_fs->e2fs_bsize);
759 brelse(bp, 0);
760 bp = NULL;
761 }
762
763 mp->mnt_data = ump;
764 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
765 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
766 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
767 mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
768 mp->mnt_flag |= MNT_LOCAL;
769 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
770 mp->mnt_fs_bshift = m_fs->e2fs_bshift;
771 mp->mnt_iflag |= IMNT_DTYPE;
772 ump->um_flags = 0;
773 ump->um_mountp = mp;
774 ump->um_dev = dev;
775 ump->um_devvp = devvp;
776 ump->um_nindir = NINDIR(m_fs);
777 ump->um_lognindir = ffs(NINDIR(m_fs)) - 1;
778 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
779 ump->um_seqinc = 1; /* no frags */
780 ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
781 ump->um_dirblksiz = m_fs->e2fs_bsize;
782 ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
783 devvp->v_specmountpoint = mp;
784 return (0);
785
786 out:
787 KASSERT(bp != NULL);
788 brelse(bp, 0);
789 if (ump) {
790 free(ump->um_e2fs, M_UFSMNT);
791 free(ump, M_UFSMNT);
792 mp->mnt_data = NULL;
793 }
794 return (error);
795 }
796
797 /*
798 * unmount system call
799 */
800 int
801 ext2fs_unmount(struct mount *mp, int mntflags)
802 {
803 struct ufsmount *ump;
804 struct m_ext2fs *fs;
805 int error, flags;
806
807 flags = 0;
808 if (mntflags & MNT_FORCE)
809 flags |= FORCECLOSE;
810 if ((error = ext2fs_flushfiles(mp, flags)) != 0)
811 return (error);
812 ump = VFSTOUFS(mp);
813 fs = ump->um_e2fs;
814 if (fs->e2fs_ronly == 0 &&
815 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
816 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
817 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
818 (void) ext2fs_sbupdate(ump, MNT_WAIT);
819 }
820 if (ump->um_devvp->v_type != VBAD)
821 ump->um_devvp->v_specmountpoint = NULL;
822 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
823 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
824 NOCRED);
825 vput(ump->um_devvp);
826 free(fs->e2fs_gd, M_UFSMNT);
827 free(fs, M_UFSMNT);
828 free(ump, M_UFSMNT);
829 mp->mnt_data = NULL;
830 mp->mnt_flag &= ~MNT_LOCAL;
831 return (error);
832 }
833
834 /*
835 * Flush out all the files in a filesystem.
836 */
837 int
838 ext2fs_flushfiles(struct mount *mp, int flags)
839 {
840 extern int doforce;
841 int error;
842
843 if (!doforce)
844 flags &= ~FORCECLOSE;
845 error = vflush(mp, NULLVP, flags);
846 return (error);
847 }
848
849 /*
850 * Get file system statistics.
851 */
852 int
853 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
854 {
855 struct ufsmount *ump;
856 struct m_ext2fs *fs;
857 uint32_t overhead, overhead_per_group, ngdb;
858 int i, ngroups;
859
860 ump = VFSTOUFS(mp);
861 fs = ump->um_e2fs;
862 if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
863 panic("ext2fs_statvfs");
864
865 /*
866 * Compute the overhead (FS structures)
867 */
868 overhead_per_group =
869 1 /* block bitmap */ +
870 1 /* inode bitmap */ +
871 fs->e2fs_itpg;
872 overhead = fs->e2fs.e2fs_first_dblock +
873 fs->e2fs_ncg * overhead_per_group;
874 if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
875 fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
876 for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
877 if (cg_has_sb(i))
878 ngroups++;
879 }
880 } else {
881 ngroups = fs->e2fs_ncg;
882 }
883 ngdb = fs->e2fs_ngdb;
884 if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
885 fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
886 ngdb += fs->e2fs.e2fs_reserved_ngdb;
887 overhead += ngroups * (1 /* superblock */ + ngdb);
888
889 sbp->f_bsize = fs->e2fs_bsize;
890 sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
891 sbp->f_iosize = fs->e2fs_bsize;
892 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
893 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
894 sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
895 if (sbp->f_bfree > sbp->f_bresvd)
896 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
897 else
898 sbp->f_bavail = 0;
899 sbp->f_files = fs->e2fs.e2fs_icount;
900 sbp->f_ffree = fs->e2fs.e2fs_ficount;
901 sbp->f_favail = fs->e2fs.e2fs_ficount;
902 sbp->f_fresvd = 0;
903 copy_statvfs_info(sbp, mp);
904 return (0);
905 }
906
907 /*
908 * Go through the disk queues to initiate sandbagged IO;
909 * go through the inodes to write those that have been modified;
910 * initiate the writing of the super block if it has been modified.
911 *
912 * Note: we are always called with the filesystem marked `MPBUSY'.
913 */
914 int
915 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
916 {
917 struct vnode *vp, *mvp;
918 struct inode *ip;
919 struct ufsmount *ump = VFSTOUFS(mp);
920 struct m_ext2fs *fs;
921 int error, allerror = 0;
922
923 fs = ump->um_e2fs;
924 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
925 printf("fs = %s\n", fs->e2fs_fsmnt);
926 panic("update: rofs mod");
927 }
928
929 /* Allocate a marker vnode. */
930 if ((mvp = vnalloc(mp)) == NULL)
931 return (ENOMEM);
932
933 /*
934 * Write back each (modified) inode.
935 */
936 mutex_enter(&mntvnode_lock);
937 loop:
938 /*
939 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
940 * and vclean() can be called indirectly
941 */
942 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
943 vmark(mvp, vp);
944 if (vp->v_mount != mp || vismarker(vp))
945 continue;
946 mutex_enter(&vp->v_interlock);
947 ip = VTOI(vp);
948 if (ip == NULL || (vp->v_iflag & (VI_XLOCK|VI_CLEAN)) != 0 ||
949 vp->v_type == VNON ||
950 ((ip->i_flag &
951 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
952 LIST_EMPTY(&vp->v_dirtyblkhd) &&
953 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
954 {
955 mutex_exit(&vp->v_interlock);
956 continue;
957 }
958 mutex_exit(&mntvnode_lock);
959 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
960 if (error) {
961 mutex_enter(&mntvnode_lock);
962 if (error == ENOENT) {
963 mutex_enter(&mntvnode_lock);
964 (void)vunmark(mvp);
965 goto loop;
966 }
967 continue;
968 }
969 if (vp->v_type == VREG && waitfor == MNT_LAZY)
970 error = ext2fs_update(vp, NULL, NULL, 0);
971 else
972 error = VOP_FSYNC(vp, cred,
973 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
974 if (error)
975 allerror = error;
976 vput(vp);
977 mutex_enter(&mntvnode_lock);
978 }
979 mutex_exit(&mntvnode_lock);
980 vnfree(mvp);
981 /*
982 * Force stale file system control information to be flushed.
983 */
984 if (waitfor != MNT_LAZY) {
985 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
986 if ((error = VOP_FSYNC(ump->um_devvp, cred,
987 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
988 allerror = error;
989 VOP_UNLOCK(ump->um_devvp, 0);
990 }
991 /*
992 * Write back modified superblock.
993 */
994 if (fs->e2fs_fmod != 0) {
995 fs->e2fs_fmod = 0;
996 fs->e2fs.e2fs_wtime = time_second;
997 if ((error = ext2fs_cgupdate(ump, waitfor)))
998 allerror = error;
999 }
1000 return (allerror);
1001 }
1002
1003 /*
1004 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
1005 * in from disk. If it is in core, wait for the lock bit to clear, then
1006 * return the inode locked. Detection and handling of mount points must be
1007 * done by the calling routine.
1008 */
1009 int
1010 ext2fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1011 {
1012 struct m_ext2fs *fs;
1013 struct inode *ip;
1014 struct ufsmount *ump;
1015 struct buf *bp;
1016 struct vnode *vp;
1017 dev_t dev;
1018 int error;
1019 void *cp;
1020
1021 ump = VFSTOUFS(mp);
1022 dev = ump->um_dev;
1023 retry:
1024 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1025 return (0);
1026
1027 /* Allocate a new vnode/inode. */
1028 if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
1029 *vpp = NULL;
1030 return (error);
1031 }
1032 ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
1033
1034 mutex_enter(&ufs_hashlock);
1035 if ((*vpp = ufs_ihashget(dev, ino, 0)) != NULL) {
1036 mutex_exit(&ufs_hashlock);
1037 ungetnewvnode(vp);
1038 pool_put(&ext2fs_inode_pool, ip);
1039 goto retry;
1040 }
1041
1042 vp->v_vflag |= VV_LOCKSWORK;
1043
1044 memset(ip, 0, sizeof(struct inode));
1045 vp->v_data = ip;
1046 ip->i_vnode = vp;
1047 ip->i_ump = ump;
1048 ip->i_e2fs = fs = ump->um_e2fs;
1049 ip->i_dev = dev;
1050 ip->i_number = ino;
1051 ip->i_e2fs_last_lblk = 0;
1052 ip->i_e2fs_last_blk = 0;
1053 genfs_node_init(vp, &ext2fs_genfsops);
1054
1055 /*
1056 * Put it onto its hash chain and lock it so that other requests for
1057 * this inode will block if they arrive while we are sleeping waiting
1058 * for old data structures to be purged or for the contents of the
1059 * disk portion of this inode to be read.
1060 */
1061
1062 ufs_ihashins(ip);
1063 mutex_exit(&ufs_hashlock);
1064
1065 /* Read in the disk contents for the inode, copy into the inode. */
1066 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1067 (int)fs->e2fs_bsize, NOCRED, 0, &bp);
1068 if (error) {
1069
1070 /*
1071 * The inode does not contain anything useful, so it would
1072 * be misleading to leave it on its hash chain. With mode
1073 * still zero, it will be unlinked and returned to the free
1074 * list by vput().
1075 */
1076
1077 vput(vp);
1078 brelse(bp, 0);
1079 *vpp = NULL;
1080 return (error);
1081 }
1082 cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs));
1083 ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
1084 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
1085 ext2fs_set_inode_guid(ip);
1086 brelse(bp, 0);
1087
1088 /* If the inode was deleted, reset all fields */
1089 if (ip->i_e2fs_dtime != 0) {
1090 ip->i_e2fs_mode = ip->i_e2fs_nblock = 0;
1091 (void)ext2fs_setsize(ip, 0);
1092 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
1093 }
1094
1095 /*
1096 * Initialize the vnode from the inode, check for aliases.
1097 */
1098
1099 error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1100 if (error) {
1101 vput(vp);
1102 *vpp = NULL;
1103 return (error);
1104 }
1105 /*
1106 * Finish inode initialization now that aliasing has been resolved.
1107 */
1108
1109 ip->i_devvp = ump->um_devvp;
1110 VREF(ip->i_devvp);
1111
1112 /*
1113 * Set up a generation number for this inode if it does not
1114 * already have one. This should only happen on old filesystems.
1115 */
1116
1117 if (ip->i_e2fs_gen == 0) {
1118 if (++ext2gennumber < (u_long)time_second)
1119 ext2gennumber = time_second;
1120 ip->i_e2fs_gen = ext2gennumber;
1121 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1122 ip->i_flag |= IN_MODIFIED;
1123 }
1124 uvm_vnp_setsize(vp, ext2fs_size(ip));
1125 *vpp = vp;
1126 return (0);
1127 }
1128
1129 /*
1130 * File handle to vnode
1131 *
1132 * Have to be really careful about stale file handles:
1133 * - check that the inode number is valid
1134 * - call ext2fs_vget() to get the locked inode
1135 * - check for an unallocated inode (i_mode == 0)
1136 */
1137 int
1138 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1139 {
1140 struct inode *ip;
1141 struct vnode *nvp;
1142 int error;
1143 struct ufid ufh;
1144 struct m_ext2fs *fs;
1145
1146 if (fhp->fid_len != sizeof(struct ufid))
1147 return EINVAL;
1148
1149 memcpy(&ufh, fhp, sizeof(struct ufid));
1150 fs = VFSTOUFS(mp)->um_e2fs;
1151 if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
1152 ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1153 return (ESTALE);
1154
1155 if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
1156 *vpp = NULLVP;
1157 return (error);
1158 }
1159 ip = VTOI(nvp);
1160 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1161 ip->i_e2fs_gen != ufh.ufid_gen) {
1162 vput(nvp);
1163 *vpp = NULLVP;
1164 return (ESTALE);
1165 }
1166 *vpp = nvp;
1167 return (0);
1168 }
1169
1170 /*
1171 * Vnode pointer to File handle
1172 */
1173 /* ARGSUSED */
1174 int
1175 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1176 {
1177 struct inode *ip;
1178 struct ufid ufh;
1179
1180 if (*fh_size < sizeof(struct ufid)) {
1181 *fh_size = sizeof(struct ufid);
1182 return E2BIG;
1183 }
1184 *fh_size = sizeof(struct ufid);
1185
1186 ip = VTOI(vp);
1187 memset(&ufh, 0, sizeof(ufh));
1188 ufh.ufid_len = sizeof(struct ufid);
1189 ufh.ufid_ino = ip->i_number;
1190 ufh.ufid_gen = ip->i_e2fs_gen;
1191 memcpy(fhp, &ufh, sizeof(ufh));
1192 return (0);
1193 }
1194
1195 /*
1196 * Write a superblock and associated information back to disk.
1197 */
1198 int
1199 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1200 {
1201 struct m_ext2fs *fs = mp->um_e2fs;
1202 struct buf *bp;
1203 int error = 0;
1204
1205 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1206 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1207 if (waitfor == MNT_WAIT)
1208 error = bwrite(bp);
1209 else
1210 bawrite(bp);
1211 return (error);
1212 }
1213
1214 int
1215 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1216 {
1217 struct m_ext2fs *fs = mp->um_e2fs;
1218 struct buf *bp;
1219 int i, error = 0, allerror = 0;
1220
1221 allerror = ext2fs_sbupdate(mp, waitfor);
1222 for (i = 0; i < fs->e2fs_ngdb; i++) {
1223 bp = getblk(mp->um_devvp, fsbtodb(fs,
1224 fs->e2fs.e2fs_first_dblock +
1225 1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
1226 e2fs_cgsave(&fs->e2fs_gd[
1227 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1228 (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1229 if (waitfor == MNT_WAIT)
1230 error = bwrite(bp);
1231 else
1232 bawrite(bp);
1233 }
1234
1235 if (!allerror && error)
1236 allerror = error;
1237 return (allerror);
1238 }
1239
1240 static int
1241 ext2fs_checksb(struct ext2fs *fs, int ronly)
1242 {
1243
1244 if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
1245 return (EINVAL); /* XXX needs translation */
1246 }
1247 if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
1248 #ifdef DIAGNOSTIC
1249 printf("Ext2 fs: unsupported revision number: %x\n",
1250 fs2h32(fs->e2fs_rev));
1251 #endif
1252 return (EINVAL); /* XXX needs translation */
1253 }
1254 if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
1255 #ifdef DIAGNOSTIC
1256 printf("Ext2 fs: bad block size: %d "
1257 "(expected <= 2 for ext2 fs)\n",
1258 fs2h32(fs->e2fs_log_bsize));
1259 #endif
1260 return (EINVAL); /* XXX needs translation */
1261 }
1262 if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
1263 if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO) {
1264 printf("Ext2 fs: unsupported first inode position\n");
1265 return (EINVAL); /* XXX needs translation */
1266 }
1267 if (fs2h32(fs->e2fs_features_incompat) &
1268 ~EXT2F_INCOMPAT_SUPP) {
1269 printf("Ext2 fs: unsupported optional feature\n");
1270 return (EINVAL); /* XXX needs translation */
1271 }
1272 if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
1273 ~EXT2F_ROCOMPAT_SUPP) {
1274 return (EROFS); /* XXX needs translation */
1275 }
1276 }
1277 return (0);
1278 }
1279