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