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