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