ext2fs_vfsops.c revision 1.228 1 /* $NetBSD: ext2fs_vfsops.c,v 1.228 2024/12/30 09:01:35 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.228 2024/12/30 09:01:35 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/file.h>
81 #include <sys/disklabel.h>
82 #include <sys/ioctl.h>
83 #include <sys/errno.h>
84 #include <sys/pool.h>
85 #include <sys/lock.h>
86 #include <sys/conf.h>
87 #include <sys/kauth.h>
88 #include <sys/module.h>
89
90 #include <miscfs/genfs/genfs.h>
91 #include <miscfs/specfs/specdev.h>
92
93 #include <ufs/ufs/quota.h>
94 #include <ufs/ufs/ufsmount.h>
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/dir.h>
97 #include <ufs/ufs/ufs_extern.h>
98
99 #include <ufs/ext2fs/ext2fs.h>
100 #include <ufs/ext2fs/ext2fs_dir.h>
101 #include <ufs/ext2fs/ext2fs_extern.h>
102
103 MODULE(MODULE_CLASS_VFS, ext2fs, "ufs");
104
105 int ext2fs_sbupdate(struct ufsmount *, int);
106 static int ext2fs_sbfill(struct m_ext2fs *, int);
107
108 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
109 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
110 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
111
112 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
113 &ext2fs_vnodeop_opv_desc,
114 &ext2fs_specop_opv_desc,
115 &ext2fs_fifoop_opv_desc,
116 NULL,
117 };
118
119 struct vfsops ext2fs_vfsops = {
120 .vfs_name = MOUNT_EXT2FS,
121 .vfs_min_mount_data = sizeof (struct ufs_args),
122 .vfs_mount = ext2fs_mount,
123 .vfs_start = ufs_start,
124 .vfs_unmount = ext2fs_unmount,
125 .vfs_root = ufs_root,
126 .vfs_quotactl = ufs_quotactl,
127 .vfs_statvfs = ext2fs_statvfs,
128 .vfs_sync = ext2fs_sync,
129 .vfs_vget = ufs_vget,
130 .vfs_loadvnode = ext2fs_loadvnode,
131 .vfs_newvnode = ext2fs_newvnode,
132 .vfs_fhtovp = ext2fs_fhtovp,
133 .vfs_vptofh = ext2fs_vptofh,
134 .vfs_init = ext2fs_init,
135 .vfs_reinit = ext2fs_reinit,
136 .vfs_done = ext2fs_done,
137 .vfs_mountroot = ext2fs_mountroot,
138 .vfs_snapshot = (void *)eopnotsupp,
139 .vfs_extattrctl = vfs_stdextattrctl,
140 .vfs_suspendctl = genfs_suspendctl,
141 .vfs_renamelock_enter = genfs_renamelock_enter,
142 .vfs_renamelock_exit = genfs_renamelock_exit,
143 .vfs_fsync = (void *)eopnotsupp,
144 .vfs_opv_descs = ext2fs_vnodeopv_descs
145 };
146
147 static const struct genfs_ops ext2fs_genfsops = {
148 .gop_size = genfs_size,
149 .gop_alloc = ext2fs_gop_alloc,
150 .gop_write = genfs_gop_write,
151 .gop_markupdate = ufs_gop_markupdate,
152 .gop_putrange = genfs_gop_putrange,
153 };
154
155 static const struct ufs_ops ext2fs_ufsops = {
156 .uo_itimes = ext2fs_itimes,
157 .uo_update = ext2fs_update,
158 .uo_bufrd = ext2fs_bufrd,
159 .uo_bufwr = ext2fs_bufwr,
160 };
161
162 static void
163 e2fs_cgload(const char *ondisk, struct ext2_gd *inmemory, int cg_size,
164 int shift_cg_entry_size)
165 {
166
167 if (shift_cg_entry_size == 6) {
168 memcpy(inmemory, ondisk, cg_size);
169 return;
170 }
171
172 const char *iptr = ondisk;
173 struct ext2_gd *optr = inmemory;
174 int sh = 1 << shift_cg_entry_size;
175 int lim = cg_size >> shift_cg_entry_size;
176 if (shift_cg_entry_size > 6) {
177 for (int i = 0; i < lim; i++, optr++, iptr += sh) {
178 memcpy(optr, iptr, sizeof(*optr));
179 }
180 } else {
181 for (int i = 0; i < lim; i++, optr++, iptr += sh) {
182 memcpy(optr, iptr, E2FS_REV0_GD_SIZE);
183 memset((char *)optr + E2FS_REV0_GD_SIZE, 0,
184 sizeof(*optr) - E2FS_REV0_GD_SIZE);
185 }
186 }
187 }
188
189 static void
190 e2fs_cgsave(const struct ext2_gd *inmemory, char *ondisk, int cg_size,
191 int shift_cg_entry_size)
192 {
193
194 if (shift_cg_entry_size == 6) {
195 memcpy(ondisk, inmemory, cg_size);
196 return;
197 }
198
199 const struct ext2_gd *iptr = inmemory;
200 char *optr = ondisk;
201 int sh = 1 << shift_cg_entry_size;
202 int lim = cg_size >> shift_cg_entry_size;
203 if (shift_cg_entry_size > 6) {
204 for (int i = 0; i < lim; i++, iptr++, optr += sh) {
205 memcpy(optr, iptr, sizeof(*iptr));
206 memset(optr + sizeof(*iptr), 0, sh - sizeof(*iptr));
207 }
208 } else {
209 for (int i = 0; i < lim; i++, iptr++, optr += sh) {
210 memcpy(optr, iptr, E2FS_REV0_GD_SIZE);
211 }
212 }
213 }
214
215 /* Fill in the inode uid/gid from ext2 halves. */
216 void
217 ext2fs_set_inode_guid(struct inode *ip)
218 {
219
220 ip->i_gid = ip->i_e2fs_gid;
221 ip->i_uid = ip->i_e2fs_uid;
222 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
223 ip->i_gid |= ip->i_e2fs_gid_high << 16;
224 ip->i_uid |= ip->i_e2fs_uid_high << 16;
225 }
226 }
227
228 SYSCTL_SETUP(ext2fs_sysctl_setup, "ext2fs sysctl")
229 {
230
231 sysctl_createv(clog, 0, NULL, NULL,
232 CTLFLAG_PERMANENT,
233 CTLTYPE_NODE, "ext2fs",
234 SYSCTL_DESCR("Linux EXT2FS file system"),
235 NULL, 0, NULL, 0,
236 CTL_VFS, 17, CTL_EOL);
237 /*
238 * XXX the "17" above could be dynamic, thereby eliminating
239 * one more instance of the "number to vfs" mapping problem,
240 * but "17" is the order as taken from sys/mount.h
241 */
242 }
243
244 static int
245 ext2fs_modcmd(modcmd_t cmd, void *arg)
246 {
247 int error;
248
249 switch (cmd) {
250 case MODULE_CMD_INIT:
251 error = vfs_attach(&ext2fs_vfsops);
252 if (error != 0)
253 break;
254 break;
255 case MODULE_CMD_FINI:
256 error = vfs_detach(&ext2fs_vfsops);
257 if (error != 0)
258 break;
259 break;
260 default:
261 error = ENOTTY;
262 break;
263 }
264
265 return error;
266 }
267
268 /*
269 * XXX Same structure as FFS inodes? Should we share a common pool?
270 */
271 struct pool ext2fs_inode_pool;
272
273 extern u_long ext2gennumber;
274
275 void
276 ext2fs_init(void)
277 {
278
279 pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
280 "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
281 ufs_init();
282 }
283
284 void
285 ext2fs_reinit(void)
286 {
287 ufs_reinit();
288 }
289
290 void
291 ext2fs_done(void)
292 {
293
294 ufs_done();
295 pool_destroy(&ext2fs_inode_pool);
296 }
297
298 static void
299 ext2fs_sb_setmountinfo(struct m_ext2fs *fs, struct mount *mp)
300 {
301 (void)strlcpy(fs->e2fs_fsmnt, mp->mnt_stat.f_mntonname,
302 sizeof(fs->e2fs_fsmnt));
303 if (fs->e2fs_ronly == 0 && fs->e2fs.e2fs_rev > E2FS_REV0) {
304 (void)strlcpy(fs->e2fs.e2fs_fsmnt, mp->mnt_stat.f_mntonname,
305 sizeof(fs->e2fs.e2fs_fsmnt));
306
307 fs->e2fs.e2fs_mtime = time_second;
308 fs->e2fs.e2fs_mnt_count++;
309
310 fs->e2fs_fmod = 1;
311 }
312 }
313
314 /*
315 * Called by main() when ext2fs is going to be mounted as root.
316 *
317 * Name is updated by mount(8) after booting.
318 */
319
320 int
321 ext2fs_mountroot(void)
322 {
323 extern struct vnode *rootvp;
324 struct m_ext2fs *fs;
325 struct mount *mp;
326 struct ufsmount *ump;
327 int error;
328
329 if (device_class(root_device) != DV_DISK)
330 return ENODEV;
331
332 if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
333 vrele(rootvp);
334 return error;
335 }
336
337 if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
338 vfs_unbusy(mp);
339 vfs_rele(mp);
340 return error;
341 }
342 mountlist_append(mp);
343 ump = VFSTOUFS(mp);
344 fs = ump->um_e2fs;
345 ext2fs_sb_setmountinfo(fs, mp);
346 (void)ext2fs_statvfs(mp, &mp->mnt_stat);
347 vfs_unbusy(mp);
348 setrootfstime((time_t)fs->e2fs.e2fs_wtime);
349 return 0;
350 }
351
352 /*
353 * VFS Operations.
354 *
355 * mount system call
356 */
357 int
358 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
359 {
360 struct lwp *l = curlwp;
361 struct vnode *devvp;
362 struct ufs_args *args = data;
363 struct ufsmount *ump = NULL;
364 struct m_ext2fs *fs;
365 int error = 0, flags, update;
366 mode_t accessmode;
367
368 if (args == NULL)
369 return EINVAL;
370 if (*data_len < sizeof *args)
371 return EINVAL;
372
373 if (mp->mnt_flag & MNT_GETARGS) {
374 ump = VFSTOUFS(mp);
375 if (ump == NULL)
376 return EIO;
377 memset(args, 0, sizeof *args);
378 args->fspec = NULL;
379 *data_len = sizeof *args;
380 return 0;
381 }
382
383 update = mp->mnt_flag & MNT_UPDATE;
384
385 /* Check arguments */
386 if (args->fspec != NULL) {
387 /*
388 * Look up the name and verify that it's sane.
389 */
390 error = namei_simple_user(args->fspec,
391 NSM_FOLLOW_NOEMULROOT, &devvp);
392 if (error != 0)
393 return error;
394
395 if (!update) {
396 /*
397 * Be sure this is a valid block device
398 */
399 if (devvp->v_type != VBLK)
400 error = ENOTBLK;
401 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
402 error = ENXIO;
403 } else {
404 /*
405 * Be sure we're still naming the same device
406 * used for our initial mount
407 */
408 ump = VFSTOUFS(mp);
409 if (devvp != ump->um_devvp) {
410 if (devvp->v_rdev != ump->um_devvp->v_rdev)
411 error = EINVAL;
412 else {
413 vrele(devvp);
414 devvp = ump->um_devvp;
415 vref(devvp);
416 }
417 }
418 }
419 } else {
420 if (!update) {
421 /* New mounts must have a filename for the device */
422 return EINVAL;
423 } else {
424 ump = VFSTOUFS(mp);
425 devvp = ump->um_devvp;
426 vref(devvp);
427 }
428 }
429
430 /*
431 * If mount by non-root, then verify that user has necessary
432 * permissions on the device.
433 *
434 * Permission to update a mount is checked higher, so here we presume
435 * updating the mount is okay (for example, as far as securelevel goes)
436 * which leaves us with the normal check.
437 */
438 if (error == 0) {
439 accessmode = VREAD;
440 if (update ?
441 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
442 (mp->mnt_flag & MNT_RDONLY) == 0)
443 accessmode |= VWRITE;
444 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
445 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
446 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
447 KAUTH_ARG(accessmode));
448 VOP_UNLOCK(devvp);
449 }
450
451 if (error) {
452 vrele(devvp);
453 return error;
454 }
455
456 if (!update) {
457 int xflags;
458
459 if (mp->mnt_flag & MNT_RDONLY)
460 xflags = FREAD;
461 else
462 xflags = FREAD|FWRITE;
463 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
464 error = VOP_OPEN(devvp, xflags, FSCRED);
465 VOP_UNLOCK(devvp);
466 if (error)
467 goto fail;
468 error = ext2fs_mountfs(devvp, mp);
469 if (error) {
470 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
471 (void)VOP_CLOSE(devvp, xflags, NOCRED);
472 VOP_UNLOCK(devvp);
473 goto fail;
474 }
475
476 ump = VFSTOUFS(mp);
477 fs = ump->um_e2fs;
478 } else {
479 /*
480 * Update the mount.
481 */
482
483 /*
484 * The initial mount got a reference on this
485 * device, so drop the one obtained via
486 * namei(), above.
487 */
488 vrele(devvp);
489
490 ump = VFSTOUFS(mp);
491 fs = ump->um_e2fs;
492 if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
493 /*
494 * Changing from r/w to r/o
495 */
496 flags = WRITECLOSE;
497 if (mp->mnt_flag & MNT_FORCE)
498 flags |= FORCECLOSE;
499 error = ext2fs_flushfiles(mp, flags);
500 if (error == 0 &&
501 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
502 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
503 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
504 (void) ext2fs_sbupdate(ump, MNT_WAIT);
505 }
506 if (error)
507 return error;
508 fs->e2fs_ronly = 1;
509 }
510
511 if (mp->mnt_flag & MNT_RELOAD) {
512 error = ext2fs_reload(mp, l->l_cred, l);
513 if (error)
514 return error;
515 }
516
517 if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
518 /*
519 * Changing from read-only to read/write
520 */
521 fs->e2fs_ronly = 0;
522 if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
523 fs->e2fs.e2fs_state = 0;
524 else
525 fs->e2fs.e2fs_state = E2FS_ERRORS;
526 fs->e2fs_fmod = 1;
527 }
528 if (args->fspec == NULL)
529 return 0;
530 }
531
532 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
533 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
534 if (error == 0)
535 ext2fs_sb_setmountinfo(fs, mp);
536
537 if (fs->e2fs_fmod != 0) { /* XXX */
538 fs->e2fs_fmod = 0;
539 if (fs->e2fs.e2fs_state == 0)
540 fs->e2fs.e2fs_wtime = time_second;
541 else
542 printf("%s: file system not clean; please fsck(8)\n",
543 mp->mnt_stat.f_mntfromname);
544 (void) ext2fs_cgupdate(ump, MNT_WAIT);
545 }
546 return error;
547
548 fail:
549 vrele(devvp);
550 return error;
551 }
552
553 /*
554 * Sanity check the disk vnode content, and copy it over to inode structure.
555 */
556 static int
557 ext2fs_loadvnode_content(struct m_ext2fs *fs, ino_t ino, struct buf *bp, struct inode *ip)
558 {
559 struct ext2fs_dinode *din;
560 int error = 0;
561
562 din = (struct ext2fs_dinode *)((char *)bp->b_data +
563 (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs)));
564
565 /* sanity checks - inode data NOT byteswapped at this point */
566 if (EXT2_DINODE_FITS(din, e2di_extra_isize, EXT2_DINODE_SIZE(fs))
567 && (EXT2_DINODE_SIZE(fs) - EXT2_REV0_DINODE_SIZE)
568 < fs2h16(din->e2di_extra_isize))
569 {
570 printf("ext2fs: inode %"PRIu64" bad extra_isize %u",
571 ino, din->e2di_extra_isize);
572 error = EINVAL;
573 goto bad;
574 }
575
576 /* everything alright, proceed with copy */
577 if (ip->i_din.e2fs_din == NULL)
578 ip->i_din.e2fs_din = kmem_alloc(EXT2_DINODE_SIZE(fs), KM_SLEEP);
579
580 e2fs_iload(din, ip->i_din.e2fs_din, EXT2_DINODE_SIZE(fs));
581
582 ext2fs_set_inode_guid(ip);
583
584 bad:
585 return error;
586 }
587
588 /*
589 * Reload all incore data for a filesystem (used after running fsck on
590 * the root filesystem and finding things to fix). The filesystem must
591 * be mounted read-only.
592 *
593 * Things to do to update the mount:
594 * 1) invalidate all cached meta-data.
595 * 2) re-read superblock from disk.
596 * 3) re-read summary information from disk.
597 * 4) invalidate all inactive vnodes.
598 * 5) invalidate all cached file data.
599 * 6) re-read inode data for all active vnodes.
600 */
601 int
602 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
603 {
604 struct vnode *vp, *devvp;
605 struct inode *ip;
606 struct buf *bp;
607 struct m_ext2fs *fs;
608 struct ext2fs *newfs;
609 int i, error;
610 struct ufsmount *ump;
611 struct vnode_iterator *marker;
612
613 if ((mp->mnt_flag & MNT_RDONLY) == 0)
614 return EINVAL;
615
616 ump = VFSTOUFS(mp);
617 /*
618 * Step 1: invalidate all cached meta-data.
619 */
620 devvp = ump->um_devvp;
621 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
622 error = vinvalbuf(devvp, 0, cred, l, 0, 0);
623 VOP_UNLOCK(devvp);
624 if (error)
625 panic("ext2fs_reload: dirty1");
626
627 fs = ump->um_e2fs;
628 /*
629 * Step 2: re-read superblock from disk. Copy in new superblock, and
630 * compute in-memory values.
631 */
632 error = bread(devvp, SBLOCK, SBSIZE, 0, &bp);
633 if (error)
634 return error;
635 newfs = (struct ext2fs *)bp->b_data;
636 e2fs_sbload(newfs, &fs->e2fs);
637
638 brelse(bp, 0);
639
640 error = ext2fs_sbfill(fs, (mp->mnt_flag & MNT_RDONLY) != 0);
641 if (error)
642 return error;
643
644 /*
645 * Step 3: re-read summary information from disk.
646 */
647 for (i = 0; i < fs->e2fs_ngdb; i++) {
648 error = bread(devvp ,
649 EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
650 1 /* superblock */ + i),
651 fs->e2fs_bsize, 0, &bp);
652 if (error) {
653 return error;
654 }
655 e2fs_cgload(bp->b_data,
656 &fs->e2fs_gd[i *
657 (fs->e2fs_bsize >> fs->e2fs_group_desc_shift)],
658 fs->e2fs_bsize, fs->e2fs_group_desc_shift);
659 brelse(bp, 0);
660 }
661
662 vfs_vnode_iterator_init(mp, &marker);
663 while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
664 /*
665 * Step 4: invalidate all inactive vnodes.
666 */
667 if (vrecycle(vp))
668 continue;
669 /*
670 * Step 5: invalidate all cached file data.
671 */
672 if (vn_lock(vp, LK_EXCLUSIVE)) {
673 vrele(vp);
674 continue;
675 }
676 if (vinvalbuf(vp, 0, cred, l, 0, 0))
677 panic("ext2fs_reload: dirty2");
678 /*
679 * Step 6: re-read inode data for all active vnodes.
680 */
681 ip = VTOI(vp);
682 error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
683 (int)fs->e2fs_bsize, 0, &bp);
684 if (error) {
685 vput(vp);
686 break;
687 }
688 error = ext2fs_loadvnode_content(fs, ip->i_number, bp, ip);
689 brelse(bp, 0);
690 if (error) {
691 vput(vp);
692 break;
693 }
694
695 vput(vp);
696 }
697 vfs_vnode_iterator_destroy(marker);
698 return error;
699 }
700
701 /*
702 * Common code for mount and mountroot
703 */
704 int
705 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
706 {
707 struct lwp *l = curlwp;
708 struct ufsmount *ump;
709 struct buf *bp;
710 struct ext2fs *fs;
711 struct m_ext2fs *m_fs;
712 dev_t dev;
713 int error, i, ronly;
714 kauth_cred_t cred;
715
716 dev = devvp->v_rdev;
717 cred = l->l_cred;
718
719 /* Flush out any old buffers remaining from a previous use. */
720 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
721 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
722 VOP_UNLOCK(devvp);
723 if (error)
724 return error;
725
726 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
727
728 bp = NULL;
729 ump = NULL;
730
731 /* Read the superblock from disk, and swap it directly. */
732 error = bread(devvp, SBLOCK, SBSIZE, 0, &bp);
733 if (error)
734 goto out;
735 fs = (struct ext2fs *)bp->b_data;
736 m_fs = kmem_zalloc(sizeof(*m_fs), KM_SLEEP);
737 e2fs_sbload(fs, &m_fs->e2fs);
738
739 brelse(bp, 0);
740 bp = NULL;
741
742 /* Once swapped, validate and fill in the superblock. */
743 error = ext2fs_sbfill(m_fs, ronly);
744 if (error) {
745 kmem_free(m_fs, sizeof(*m_fs));
746 goto out;
747 }
748 m_fs->e2fs_ronly = ronly;
749
750 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
751 ump->um_fstype = UFS1;
752 ump->um_ops = &ext2fs_ufsops;
753 ump->um_e2fs = m_fs;
754
755 if (ronly == 0) {
756 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
757 m_fs->e2fs.e2fs_state = 0;
758 else
759 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
760 m_fs->e2fs_fmod = 1;
761 }
762
763 int32_t sh = m_fs->e2fs_bsize >> m_fs->e2fs_group_desc_shift;
764 /* XXX: should be added in ext2fs_sbfill()? */
765 m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * sh
766 * sizeof(struct ext2_gd), KM_SLEEP);
767 for (i = 0; i < m_fs->e2fs_ngdb; i++) {
768 error = bread(devvp,
769 EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
770 1 /* superblock */ + i),
771 m_fs->e2fs_bsize, 0, &bp);
772 if (error)
773 goto out1;
774 e2fs_cgload(bp->b_data, &m_fs->e2fs_gd[i *
775 (m_fs->e2fs_bsize >> m_fs->e2fs_group_desc_shift)],
776 m_fs->e2fs_bsize, m_fs->e2fs_group_desc_shift);
777 brelse(bp, 0);
778 bp = NULL;
779 }
780
781 error = ext2fs_cg_verify_and_initialize(devvp, m_fs, ronly);
782 if (error)
783 goto out1;
784
785 mp->mnt_data = ump;
786 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
787 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
788 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
789 mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
790 mp->mnt_flag |= MNT_LOCAL;
791 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
792 mp->mnt_fs_bshift = m_fs->e2fs_bshift;
793 mp->mnt_iflag |= IMNT_DTYPE | IMNT_SHRLOOKUP;
794 ump->um_flags = 0;
795 ump->um_mountp = mp;
796 ump->um_dev = dev;
797 ump->um_devvp = devvp;
798 ump->um_nindir = EXT2_NINDIR(m_fs);
799 ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
800 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
801 ump->um_seqinc = 1; /* no frags */
802 ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
803 ump->um_dirblksiz = m_fs->e2fs_bsize;
804 ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
805 spec_node_setmountedfs(devvp, mp);
806 return 0;
807
808 out1:
809 kmem_free(m_fs->e2fs_gd, m_fs->e2fs_ngdb * sh * sizeof(struct ext2_gd));
810 out:
811 if (bp != NULL)
812 brelse(bp, 0);
813 if (ump) {
814 kmem_free(ump->um_e2fs, sizeof(*m_fs));
815 kmem_free(ump, sizeof(*ump));
816 mp->mnt_data = NULL;
817 }
818 return error;
819 }
820
821 /*
822 * unmount system call
823 */
824 int
825 ext2fs_unmount(struct mount *mp, int mntflags)
826 {
827 struct ufsmount *ump;
828 struct m_ext2fs *fs;
829 int error, flags;
830
831 flags = 0;
832 if (mntflags & MNT_FORCE)
833 flags |= FORCECLOSE;
834 if ((error = ext2fs_flushfiles(mp, flags)) != 0)
835 return error;
836 ump = VFSTOUFS(mp);
837 fs = ump->um_e2fs;
838 if (fs->e2fs_ronly == 0 &&
839 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
840 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
841 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
842 (void) ext2fs_sbupdate(ump, MNT_WAIT);
843 }
844 if (ump->um_devvp->v_type != VBAD)
845 spec_node_setmountedfs(ump->um_devvp, NULL);
846 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
847 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
848 NOCRED);
849 vput(ump->um_devvp);
850 int32_t sh = fs->e2fs_bsize >> fs->e2fs_group_desc_shift;
851 kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * sh * sizeof(struct ext2_gd));
852 kmem_free(fs, sizeof(*fs));
853 kmem_free(ump, sizeof(*ump));
854 mp->mnt_data = NULL;
855 mp->mnt_flag &= ~MNT_LOCAL;
856 return error;
857 }
858
859 /*
860 * Flush out all the files in a filesystem.
861 */
862 int
863 ext2fs_flushfiles(struct mount *mp, int flags)
864 {
865 extern int doforce;
866 int error;
867
868 if (!doforce)
869 flags &= ~FORCECLOSE;
870 error = vflush(mp, NULLVP, flags);
871 return error;
872 }
873
874 /*
875 * Get file system statistics.
876 */
877 int
878 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
879 {
880 struct ufsmount *ump;
881 struct m_ext2fs *fs;
882 uint32_t overhead, overhead_per_group, ngdb;
883 int i, ngroups;
884
885 ump = VFSTOUFS(mp);
886 fs = ump->um_e2fs;
887 if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
888 panic("ext2fs_statvfs");
889
890 /*
891 * Compute the overhead (FS structures)
892 */
893 overhead_per_group =
894 1 /* block bitmap */ +
895 1 /* inode bitmap */ +
896 fs->e2fs_itpg;
897 overhead = fs->e2fs.e2fs_first_dblock +
898 fs->e2fs_ncg * overhead_per_group;
899 if (EXT2F_HAS_COMPAT_FEATURE(fs, EXT2F_COMPAT_SPARSESUPER2)) {
900 /*
901 * Superblock and group descriptions is in group zero,
902 * then optionally 0, 1 or 2 extra copies.
903 */
904 ngroups = 1
905 + (fs->e2fs.e4fs_backup_bgs[0] ? 1 : 0)
906 + (fs->e2fs.e4fs_backup_bgs[1] ? 1 : 0);
907 } else if (EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_SPARSESUPER)) {
908 for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
909 if (cg_has_sb(i))
910 ngroups++;
911 }
912 } else {
913 ngroups = fs->e2fs_ncg;
914 }
915 ngdb = fs->e2fs_ngdb;
916 if (EXT2F_HAS_COMPAT_FEATURE(fs, EXT2F_COMPAT_RESIZE))
917 ngdb += fs->e2fs.e2fs_reserved_ngdb;
918 overhead += ngroups * (1 /* superblock */ + ngdb);
919
920 sbp->f_bsize = fs->e2fs_bsize;
921 sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
922 sbp->f_iosize = fs->e2fs_bsize;
923 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
924 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
925 sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
926 if (sbp->f_bfree > sbp->f_bresvd)
927 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
928 else
929 sbp->f_bavail = 0;
930 sbp->f_files = fs->e2fs.e2fs_icount;
931 sbp->f_ffree = fs->e2fs.e2fs_ficount;
932 sbp->f_favail = fs->e2fs.e2fs_ficount;
933 sbp->f_fresvd = 0;
934 copy_statvfs_info(sbp, mp);
935 return 0;
936 }
937
938 static bool
939 ext2fs_sync_selector(void *cl, struct vnode *vp)
940 {
941 struct inode *ip;
942
943 KASSERT(mutex_owned(vp->v_interlock));
944
945 ip = VTOI(vp);
946 /*
947 * Skip the vnode/inode if inaccessible.
948 */
949 if (ip == NULL || vp->v_type == VNON)
950 return false;
951
952 if (((ip->i_flag &
953 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
954 LIST_EMPTY(&vp->v_dirtyblkhd) &&
955 (vp->v_iflag & VI_ONWORKLST) == 0))
956 return false;
957 return true;
958 }
959
960 /*
961 * Go through the disk queues to initiate sandbagged IO;
962 * go through the inodes to write those that have been modified;
963 * initiate the writing of the super block if it has been modified.
964 */
965 int
966 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
967 {
968 struct vnode *vp;
969 struct ufsmount *ump = VFSTOUFS(mp);
970 struct m_ext2fs *fs;
971 struct vnode_iterator *marker;
972 int error, allerror = 0;
973
974 fs = ump->um_e2fs;
975 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
976 printf("fs = %s\n", fs->e2fs_fsmnt);
977 panic("update: rofs mod");
978 }
979
980 /*
981 * Write back each (modified) inode.
982 */
983 vfs_vnode_iterator_init(mp, &marker);
984 while ((vp = vfs_vnode_iterator_next(marker, ext2fs_sync_selector,
985 NULL)))
986 {
987 error = vn_lock(vp, LK_EXCLUSIVE);
988 if (error) {
989 vrele(vp);
990 continue;
991 }
992 if (vp->v_type == VREG && waitfor == MNT_LAZY)
993 error = ext2fs_update(vp, NULL, NULL, 0);
994 else
995 error = VOP_FSYNC(vp, cred,
996 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
997 if (error)
998 allerror = error;
999 vput(vp);
1000 }
1001 vfs_vnode_iterator_destroy(marker);
1002 /*
1003 * Force stale file system control information to be flushed.
1004 */
1005 if (waitfor != MNT_LAZY) {
1006 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1007 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1008 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1009 allerror = error;
1010 VOP_UNLOCK(ump->um_devvp);
1011 }
1012 /*
1013 * Write back modified superblock.
1014 */
1015 if (fs->e2fs_fmod != 0) {
1016 fs->e2fs_fmod = 0;
1017 fs->e2fs.e2fs_wtime = time_second;
1018 if ((error = ext2fs_cgupdate(ump, waitfor)))
1019 allerror = error;
1020 }
1021 return allerror;
1022 }
1023
1024 /*
1025 * Load inode from disk and initialize vnode.
1026 */
1027 static int
1028 ext2fs_init_vnode(struct ufsmount *ump, struct vnode *vp, ino_t ino)
1029 {
1030 struct m_ext2fs *fs;
1031 struct inode *ip;
1032 struct buf *bp;
1033 int error;
1034
1035 fs = ump->um_e2fs;
1036
1037 /* Read in the disk contents for the inode, copy into the inode. */
1038 error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
1039 (int)fs->e2fs_bsize, 0, &bp);
1040 if (error)
1041 return error;
1042
1043 /* Allocate and initialize inode. */
1044 ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
1045 memset(ip, 0, sizeof(struct inode));
1046 ip->i_vnode = vp;
1047 ip->i_ump = ump;
1048 ip->i_e2fs = fs;
1049 ip->i_dev = ump->um_dev;
1050 ip->i_number = ino;
1051 ip->i_e2fs_last_lblk = 0;
1052 ip->i_e2fs_last_blk = 0;
1053
1054 error = ext2fs_loadvnode_content(fs, ino, bp, ip);
1055 brelse(bp, 0);
1056 if (error) {
1057 pool_put(&ext2fs_inode_pool, ip);
1058 return error;
1059 }
1060
1061 /* If the inode was deleted, reset all fields */
1062 if (ip->i_e2fs_dtime != 0) {
1063 ip->i_e2fs_mode = 0;
1064 (void)ext2fs_setsize(ip, 0);
1065 (void)ext2fs_setnblock(ip, 0);
1066 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
1067 }
1068
1069 /* Initialise vnode with this inode. */
1070 vp->v_tag = VT_EXT2FS;
1071 vp->v_op = ext2fs_vnodeop_p;
1072 vp->v_data = ip;
1073
1074 /* Initialize genfs node. */
1075 genfs_node_init(vp, &ext2fs_genfsops);
1076
1077 return 0;
1078 }
1079
1080 /*
1081 * Read an inode from disk and initialize this vnode / inode pair.
1082 * Caller assures no other thread will try to load this inode.
1083 */
1084 int
1085 ext2fs_loadvnode(struct mount *mp, struct vnode *vp,
1086 const void *key, size_t key_len, const void **new_key)
1087 {
1088 ino_t ino;
1089 struct inode *ip;
1090 struct ufsmount *ump;
1091 int error;
1092
1093 KASSERT(key_len == sizeof(ino));
1094 memcpy(&ino, key, key_len);
1095 ump = VFSTOUFS(mp);
1096
1097 error = ext2fs_init_vnode(ump, vp, ino);
1098 if (error)
1099 return error;
1100
1101 ip = VTOI(vp);
1102
1103 /* Initialize the vnode from the inode. */
1104 ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1105
1106 /* Finish inode initialization. */
1107 ip->i_devvp = ump->um_devvp;
1108 vref(ip->i_devvp);
1109
1110 /*
1111 * Set up a generation number for this inode if it does not
1112 * already have one. This should only happen on old filesystems.
1113 */
1114
1115 if (ip->i_e2fs_gen == 0) {
1116 if (++ext2gennumber < (u_long)time_second)
1117 ext2gennumber = time_second;
1118 ip->i_e2fs_gen = ext2gennumber;
1119 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1120 ip->i_flag |= IN_MODIFIED;
1121 }
1122 uvm_vnp_setsize(vp, ext2fs_size(ip));
1123 *new_key = &ip->i_number;
1124 return 0;
1125 }
1126
1127 /*
1128 * Create a new inode on disk and initialize this vnode / inode pair.
1129 */
1130 int
1131 ext2fs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
1132 struct vattr *vap, kauth_cred_t cred, void *extra,
1133 size_t *key_len, const void **new_key)
1134 {
1135 ino_t ino;
1136 struct inode *ip, *pdir;
1137 struct m_ext2fs *fs;
1138 struct ufsmount *ump;
1139 int error, mode;
1140
1141 KASSERT(dvp->v_mount == mp);
1142 KASSERT(vap->va_type != VNON);
1143
1144 *key_len = sizeof(ino);
1145
1146 pdir = VTOI(dvp);
1147 fs = pdir->i_e2fs;
1148 ump = VFSTOUFS(mp);
1149 mode = MAKEIMODE(vap->va_type, vap->va_mode);
1150
1151 /* Allocate fresh inode. */
1152 error = ext2fs_valloc(dvp, mode, cred, &ino);
1153 if (error)
1154 return error;
1155
1156 /* Attach inode to vnode. */
1157 error = ext2fs_init_vnode(ump, vp, ino);
1158 if (error) {
1159 ext2fs_vfree(dvp, ino, mode);
1160 return error;
1161 }
1162
1163 ip = VTOI(vp);
1164
1165 KASSERT(!E2FS_HAS_GD_CSUM(fs) ||
1166 (fs->e2fs_gd[ino_to_cg(fs, ino)].ext2bgd_flags &
1167 h2fs16(E2FS_BG_INODE_ZEROED)) != 0);
1168
1169 /* check for already used inode; makes sense only for ZEROED itable */
1170 if (__predict_false(ip->i_e2fs_mode && ip->i_e2fs_nlink != 0)) {
1171 printf("mode = 0%o, nlinks %d, inum = %llu, fs = %s\n",
1172 ip->i_e2fs_mode, ip->i_e2fs_nlink,
1173 (unsigned long long)ip->i_number, fs->e2fs_fsmnt);
1174 panic("ext2fs_valloc: dup alloc");
1175 }
1176
1177 memset(ip->i_din.e2fs_din, 0, EXT2_DINODE_SIZE(fs));
1178
1179 /*
1180 * Set up a new generation number for this inode.
1181 */
1182 if (++ext2gennumber < time_second)
1183 ext2gennumber = time_second;
1184 ip->i_e2fs_gen = ext2gennumber;
1185
1186 ip->i_uid = kauth_cred_geteuid(cred);
1187 ip->i_e2fs_uid = ip->i_uid & 0xffff;
1188 ip->i_e2fs_gid = pdir->i_e2fs_gid;
1189 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
1190 ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
1191 ip->i_e2fs_gid_high = pdir->i_e2fs_gid_high;
1192 } else {
1193 ip->i_e2fs_uid_high = 0;
1194 ip->i_e2fs_gid_high = 0;
1195 }
1196 ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
1197 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1198 ip->i_e2fs_mode = mode;
1199 vp->v_type = IFTOVT(mode);
1200 ip->i_e2fs_nlink = 1;
1201
1202 /* Authorize setting SGID if needed. */
1203 if (ip->i_e2fs_mode & ISGID) {
1204 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY,
1205 vp, NULL, genfs_can_chmod(vp, cred, ip->i_uid, ip->i_gid,
1206 mode));
1207 if (error)
1208 ip->i_e2fs_mode &= ~ISGID;
1209 }
1210
1211 /* Initialize extra_isize according to what is set in superblock */
1212 if (EXT2F_HAS_ROCOMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_EXTRA_ISIZE)
1213 && EXT2_DINODE_SIZE(ip->i_e2fs) > EXT2_REV0_DINODE_SIZE) {
1214 ip->i_din.e2fs_din->e2di_extra_isize =
1215 ip->i_e2fs->e2fs.e4fs_want_extra_isize;
1216 }
1217
1218 /* Set create time if possible */
1219 if (EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime,
1220 EXT2_DINODE_SIZE(ip->i_e2fs))) {
1221 struct timespec now;
1222 vfs_timestamp(&now);
1223 EXT2_DINODE_TIME_SET(&now, ip->i_din.e2fs_din, e2di_crtime,
1224 EXT2_DINODE_SIZE(ip->i_e2fs));
1225 }
1226
1227 /* Initialize the vnode from the inode. */
1228 ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1229
1230 /* Finish inode initialization. */
1231 ip->i_devvp = ump->um_devvp;
1232 vref(ip->i_devvp);
1233
1234 uvm_vnp_setsize(vp, ext2fs_size(ip));
1235 *new_key = &ip->i_number;
1236 return 0;
1237 }
1238
1239 /*
1240 * File handle to vnode
1241 *
1242 * Have to be really careful about stale file handles:
1243 * - check that the inode number is valid
1244 * - call ext2fs_vget() to get the locked inode
1245 * - check for an unallocated inode (i_mode == 0)
1246 */
1247 int
1248 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
1249 {
1250 struct inode *ip;
1251 struct vnode *nvp;
1252 int error;
1253 struct ufid ufh;
1254 struct m_ext2fs *fs;
1255
1256 if (fhp->fid_len != sizeof(struct ufid))
1257 return EINVAL;
1258
1259 memcpy(&ufh, fhp, sizeof(struct ufid));
1260 fs = VFSTOUFS(mp)->um_e2fs;
1261 if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
1262 ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1263 return ESTALE;
1264
1265 if ((error = VFS_VGET(mp, ufh.ufid_ino, lktype, &nvp)) != 0) {
1266 *vpp = NULLVP;
1267 return error;
1268 }
1269 ip = VTOI(nvp);
1270 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1271 ip->i_e2fs_gen != ufh.ufid_gen) {
1272 vput(nvp);
1273 *vpp = NULLVP;
1274 return ESTALE;
1275 }
1276 *vpp = nvp;
1277 return 0;
1278 }
1279
1280 /*
1281 * Vnode pointer to File handle
1282 */
1283 /* ARGSUSED */
1284 int
1285 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1286 {
1287 struct inode *ip;
1288 struct ufid ufh;
1289
1290 if (*fh_size < sizeof(struct ufid)) {
1291 *fh_size = sizeof(struct ufid);
1292 return E2BIG;
1293 }
1294 *fh_size = sizeof(struct ufid);
1295
1296 ip = VTOI(vp);
1297 memset(&ufh, 0, sizeof(ufh));
1298 ufh.ufid_len = sizeof(struct ufid);
1299 ufh.ufid_ino = ip->i_number;
1300 ufh.ufid_gen = ip->i_e2fs_gen;
1301 memcpy(fhp, &ufh, sizeof(ufh));
1302 return 0;
1303 }
1304
1305 /*
1306 * Write a superblock and associated information back to disk.
1307 */
1308 int
1309 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1310 {
1311 struct m_ext2fs *fs = mp->um_e2fs;
1312 struct buf *bp;
1313 int error = 0;
1314
1315 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1316 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1317 if (waitfor == MNT_WAIT)
1318 error = bwrite(bp);
1319 else
1320 bawrite(bp);
1321 return error;
1322 }
1323
1324 int
1325 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1326 {
1327 struct m_ext2fs *fs = mp->um_e2fs;
1328 struct buf *bp;
1329 int i, error = 0, allerror = 0;
1330
1331 allerror = ext2fs_sbupdate(mp, waitfor);
1332 for (i = 0; i < fs->e2fs_ngdb; i++) {
1333 bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
1334 fs->e2fs.e2fs_first_dblock +
1335 1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
1336 e2fs_cgsave(&fs->e2fs_gd[i *
1337 (fs->e2fs_bsize >> fs->e2fs_group_desc_shift)],
1338 bp->b_data, fs->e2fs_bsize, fs->e2fs_group_desc_shift);
1339 if (waitfor == MNT_WAIT)
1340 error = bwrite(bp);
1341 else
1342 bawrite(bp);
1343 }
1344
1345 if (!allerror && error)
1346 allerror = error;
1347 return allerror;
1348 }
1349
1350 /*
1351 * Fill in the m_fs structure, and validate the fields of the superblock.
1352 * NOTE: here, the superblock is already swapped.
1353 */
1354 static int
1355 ext2fs_sbfill(struct m_ext2fs *m_fs, int ronly)
1356 {
1357 uint32_t u32;
1358 struct ext2fs *fs = &m_fs->e2fs;
1359
1360 /*
1361 * General sanity checks
1362 */
1363 if (fs->e2fs_magic != E2FS_MAGIC)
1364 return EINVAL;
1365 if (fs->e2fs_rev > E2FS_REV1) {
1366 printf("ext2fs: unsupported revision number: %#x\n",
1367 fs->e2fs_rev);
1368 return EINVAL;
1369 }
1370 if (fs->e2fs_log_bsize > 2) {
1371 /* block size = 1024|2048|4096 */
1372 printf("ext2fs: bad block size: %d\n", fs->e2fs_log_bsize);
1373 return EINVAL;
1374 }
1375 if (fs->e2fs_bpg == 0) {
1376 printf("ext2fs: zero blocks per group\n");
1377 return EINVAL;
1378 }
1379 if (fs->e2fs_ipg == 0) {
1380 printf("ext2fs: zero inodes per group\n");
1381 return EINVAL;
1382 }
1383
1384 if (fs->e2fs_first_dblock >= fs->e2fs_bcount) {
1385 printf("ext2fs: invalid first data block\n");
1386 return EINVAL;
1387 }
1388 if (fs->e2fs_rbcount > fs->e2fs_bcount ||
1389 fs->e2fs_fbcount > fs->e2fs_bcount) {
1390 printf("ext2fs: invalid block count\n");
1391 return EINVAL;
1392 }
1393
1394 /*
1395 * Compute the fields of the superblock
1396 */
1397 u32 = fs->e2fs_bcount - fs->e2fs_first_dblock; /* > 0 */
1398 m_fs->e2fs_ncg = howmany(u32, fs->e2fs_bpg);
1399 if (m_fs->e2fs_ncg == 0) {
1400 printf("ext2fs: invalid number of cylinder groups\n");
1401 return EINVAL;
1402 }
1403
1404 m_fs->e2fs_fsbtodb = fs->e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
1405 m_fs->e2fs_bsize = MINBSIZE << fs->e2fs_log_bsize;
1406 m_fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs_log_bsize;
1407 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
1408 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
1409
1410 if (!(fs->e2fs_features_incompat & EXT2F_INCOMPAT_64BIT) ||
1411 (fs->e2fs_rev == E2FS_REV0))
1412 m_fs->e2fs_group_desc_shift = 5;
1413 else {
1414 for (m_fs->e2fs_group_desc_shift = 0;
1415 (1 << m_fs->e2fs_group_desc_shift)
1416 < fs->e3fs_desc_size;
1417 m_fs->e2fs_group_desc_shift++);
1418 }
1419
1420 if ((u32 = (m_fs->e2fs_bsize >> m_fs->e2fs_group_desc_shift)) == 0) {
1421 /* Unlikely to happen */
1422 printf("ext2fs: invalid block size\n");
1423 return EINVAL;
1424 }
1425 m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg, u32);
1426 if (m_fs->e2fs_ngdb == 0) {
1427 printf("ext2fs: invalid number of group descriptor blocks\n");
1428 return EINVAL;
1429 }
1430
1431 if (m_fs->e2fs_bsize < EXT2_DINODE_SIZE(m_fs)) {
1432 printf("ext2fs: invalid inode size\n");
1433 return EINVAL;
1434 }
1435 m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
1436
1437 m_fs->e2fs_itpg = fs->e2fs_ipg / m_fs->e2fs_ipb;
1438
1439 /*
1440 * Revision-specific checks
1441 */
1442 if (fs->e2fs_rev > E2FS_REV0) {
1443 char buf[256];
1444 if (fs->e2fs_first_ino != EXT2_FIRSTINO) {
1445 printf("ext2fs: unsupported first inode position\n");
1446 return EINVAL;
1447 }
1448 u32 = fs->e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP;
1449 if (u32) {
1450 snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
1451 printf("ext2fs: unsupported incompat features: %s\n",
1452 buf);
1453 #ifndef EXT2_IGNORE_INCOMPAT_FEATURES
1454 return EINVAL;
1455 #endif
1456 }
1457 u32 = fs->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
1458 if (!ronly && u32) {
1459 snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
1460 printf("ext2fs: unsupported ro-incompat features: %s\n",
1461 buf);
1462 #ifndef EXT2_IGNORE_ROCOMPAT_FEATURES
1463 return EROFS;
1464 #endif
1465 }
1466 if (fs->e2fs_inode_size == 0 || !powerof2(fs->e2fs_inode_size) || fs->e2fs_inode_size > m_fs->e2fs_bsize) {
1467 printf("ext2fs: bad inode size\n");
1468 return EINVAL;
1469 }
1470 }
1471
1472 return 0;
1473 }
1474