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