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