1 /*- 2 * modified for EXT2FS support in Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 */ 7 /*- 8 * SPDX-License-Identifier: BSD-3-Clause 9 * 10 * Copyright (c) 1989, 1991, 1993, 1994 11 * The Regents of the University of California. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94 38 * $FreeBSD$ 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/namei.h> 44 #include <sys/priv.h> 45 #include <sys/proc.h> 46 #include <sys/kernel.h> 47 #include <sys/vnode.h> 48 #include <sys/mount.h> 49 #include <sys/bio.h> 50 #include <sys/buf.h> 51 #include <sys/conf.h> 52 #include <sys/endian.h> 53 #include <sys/fcntl.h> 54 #include <sys/malloc.h> 55 #include <sys/sdt.h> 56 #include <sys/stat.h> 57 #include <sys/mutex.h> 58 59 #include <geom/geom.h> 60 #include <geom/geom_vfs.h> 61 62 #include <fs/ext2fs/fs.h> 63 #include <fs/ext2fs/ext2_mount.h> 64 #include <fs/ext2fs/inode.h> 65 66 #include <fs/ext2fs/ext2fs.h> 67 #include <fs/ext2fs/ext2_dinode.h> 68 #include <fs/ext2fs/ext2_extern.h> 69 #include <fs/ext2fs/ext2_extents.h> 70 71 SDT_PROVIDER_DECLARE(ext2fs); 72 /* 73 * ext2fs trace probe: 74 * arg0: verbosity. Higher numbers give more verbose messages 75 * arg1: Textual message 76 */ 77 SDT_PROBE_DEFINE2(ext2fs, , vfsops, trace, "int", "char*"); 78 SDT_PROBE_DEFINE2(ext2fs, , vfsops, ext2_cg_validate_error, "char*", "int"); 79 SDT_PROBE_DEFINE1(ext2fs, , vfsops, ext2_compute_sb_data_error, "char*"); 80 81 82 static int ext2_flushfiles(struct mount *mp, int flags, struct thread *td); 83 static int ext2_mountfs(struct vnode *, struct mount *); 84 static int ext2_reload(struct mount *mp, struct thread *td); 85 static int ext2_sbupdate(struct ext2mount *, int); 86 static int ext2_cgupdate(struct ext2mount *, int); 87 static vfs_unmount_t ext2_unmount; 88 static vfs_root_t ext2_root; 89 static vfs_statfs_t ext2_statfs; 90 static vfs_sync_t ext2_sync; 91 static vfs_vget_t ext2_vget; 92 static vfs_fhtovp_t ext2_fhtovp; 93 static vfs_mount_t ext2_mount; 94 95 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part"); 96 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure"); 97 98 static struct vfsops ext2fs_vfsops = { 99 .vfs_fhtovp = ext2_fhtovp, 100 .vfs_mount = ext2_mount, 101 .vfs_root = ext2_root, /* root inode via vget */ 102 .vfs_statfs = ext2_statfs, 103 .vfs_sync = ext2_sync, 104 .vfs_unmount = ext2_unmount, 105 .vfs_vget = ext2_vget, 106 }; 107 108 VFS_SET(ext2fs_vfsops, ext2fs, 0); 109 110 static int ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, 111 int ronly); 112 static int ext2_compute_sb_data(struct vnode * devvp, 113 struct ext2fs * es, struct m_ext2fs * fs); 114 115 static const char *ext2_opts[] = { "acls", "async", "noatime", "noclusterr", 116 "noclusterw", "noexec", "export", "force", "from", "multilabel", 117 "suiddir", "nosymfollow", "sync", "union", NULL }; 118 119 /* 120 * VFS Operations. 121 * 122 * mount system call 123 */ 124 static int 125 ext2_mount(struct mount *mp) 126 { 127 struct vfsoptlist *opts; 128 struct vnode *devvp; 129 struct thread *td; 130 struct ext2mount *ump = NULL; 131 struct m_ext2fs *fs; 132 struct nameidata nd, *ndp = &nd; 133 accmode_t accmode; 134 char *path, *fspec; 135 int error, flags, len; 136 137 td = curthread; 138 opts = mp->mnt_optnew; 139 140 if (vfs_filteropt(opts, ext2_opts)) 141 return (EINVAL); 142 143 vfs_getopt(opts, "fspath", (void **)&path, NULL); 144 /* Double-check the length of path.. */ 145 if (strlen(path) >= MAXMNTLEN) 146 return (ENAMETOOLONG); 147 148 fspec = NULL; 149 error = vfs_getopt(opts, "from", (void **)&fspec, &len); 150 if (!error && fspec[len - 1] != '\0') 151 return (EINVAL); 152 153 /* 154 * If updating, check whether changing from read-only to 155 * read/write; if there is no device name, that's all we do. 156 */ 157 if (mp->mnt_flag & MNT_UPDATE) { 158 ump = VFSTOEXT2(mp); 159 fs = ump->um_e2fs; 160 error = 0; 161 if (fs->e2fs_ronly == 0 && 162 vfs_flagopt(opts, "ro", NULL, 0)) { 163 error = VFS_SYNC(mp, MNT_WAIT); 164 if (error) 165 return (error); 166 flags = WRITECLOSE; 167 if (mp->mnt_flag & MNT_FORCE) 168 flags |= FORCECLOSE; 169 error = ext2_flushfiles(mp, flags, td); 170 if (error == 0 && fs->e2fs_wasvalid && 171 ext2_cgupdate(ump, MNT_WAIT) == 0) { 172 fs->e2fs->e2fs_state |= E2FS_ISCLEAN; 173 ext2_sbupdate(ump, MNT_WAIT); 174 } 175 fs->e2fs_ronly = 1; 176 vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY); 177 g_topology_lock(); 178 g_access(ump->um_cp, 0, -1, 0); 179 g_topology_unlock(); 180 } 181 if (!error && (mp->mnt_flag & MNT_RELOAD)) 182 error = ext2_reload(mp, td); 183 if (error) 184 return (error); 185 devvp = ump->um_devvp; 186 if (fs->e2fs_ronly && !vfs_flagopt(opts, "ro", NULL, 0)) { 187 if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0)) 188 return (EPERM); 189 190 /* 191 * If upgrade to read-write by non-root, then verify 192 * that user has necessary permissions on the device. 193 */ 194 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 195 error = VOP_ACCESS(devvp, VREAD | VWRITE, 196 td->td_ucred, td); 197 if (error) 198 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 199 if (error) { 200 VOP_UNLOCK(devvp, 0); 201 return (error); 202 } 203 VOP_UNLOCK(devvp, 0); 204 g_topology_lock(); 205 error = g_access(ump->um_cp, 0, 1, 0); 206 g_topology_unlock(); 207 if (error) 208 return (error); 209 210 if ((fs->e2fs->e2fs_state & E2FS_ISCLEAN) == 0 || 211 (fs->e2fs->e2fs_state & E2FS_ERRORS)) { 212 if (mp->mnt_flag & MNT_FORCE) { 213 printf( 214 "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt); 215 } else { 216 printf( 217 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 218 fs->e2fs_fsmnt); 219 return (EPERM); 220 } 221 } 222 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN; 223 (void)ext2_cgupdate(ump, MNT_WAIT); 224 fs->e2fs_ronly = 0; 225 MNT_ILOCK(mp); 226 mp->mnt_flag &= ~MNT_RDONLY; 227 MNT_IUNLOCK(mp); 228 } 229 if (vfs_flagopt(opts, "export", NULL, 0)) { 230 /* Process export requests in vfs_mount.c. */ 231 return (error); 232 } 233 } 234 235 /* 236 * Not an update, or updating the name: look up the name 237 * and verify that it refers to a sensible disk device. 238 */ 239 if (fspec == NULL) 240 return (EINVAL); 241 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td); 242 if ((error = namei(ndp)) != 0) 243 return (error); 244 NDFREE(ndp, NDF_ONLY_PNBUF); 245 devvp = ndp->ni_vp; 246 247 if (!vn_isdisk(devvp, &error)) { 248 vput(devvp); 249 return (error); 250 } 251 252 /* 253 * If mount by non-root, then verify that user has necessary 254 * permissions on the device. 255 * 256 * XXXRW: VOP_ACCESS() enough? 257 */ 258 accmode = VREAD; 259 if ((mp->mnt_flag & MNT_RDONLY) == 0) 260 accmode |= VWRITE; 261 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td); 262 if (error) 263 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 264 if (error) { 265 vput(devvp); 266 return (error); 267 } 268 269 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 270 error = ext2_mountfs(devvp, mp); 271 } else { 272 if (devvp != ump->um_devvp) { 273 vput(devvp); 274 return (EINVAL); /* needs translation */ 275 } else 276 vput(devvp); 277 } 278 if (error) { 279 vrele(devvp); 280 return (error); 281 } 282 ump = VFSTOEXT2(mp); 283 fs = ump->um_e2fs; 284 285 /* 286 * Note that this strncpy() is ok because of a check at the start 287 * of ext2_mount(). 288 */ 289 strncpy(fs->e2fs_fsmnt, path, MAXMNTLEN); 290 fs->e2fs_fsmnt[MAXMNTLEN - 1] = '\0'; 291 vfs_mountedfrom(mp, fspec); 292 return (0); 293 } 294 295 static int 296 ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly) 297 { 298 uint32_t i, mask; 299 300 if (es->e2fs_magic != E2FS_MAGIC) { 301 printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n", 302 devtoname(dev), es->e2fs_magic, E2FS_MAGIC); 303 return (1); 304 } 305 if (es->e2fs_rev > E2FS_REV0) { 306 mask = es->e2fs_features_incompat & ~(EXT2F_INCOMPAT_SUPP); 307 if (mask) { 308 printf("WARNING: mount of %s denied due to " 309 "unsupported optional features:\n", devtoname(dev)); 310 for (i = 0; 311 i < sizeof(incompat)/sizeof(struct ext2_feature); 312 i++) 313 if (mask & incompat[i].mask) 314 printf("%s ", incompat[i].name); 315 printf("\n"); 316 return (1); 317 } 318 mask = es->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP; 319 if (!ronly && mask) { 320 printf("WARNING: R/W mount of %s denied due to " 321 "unsupported optional features:\n", devtoname(dev)); 322 for (i = 0; 323 i < sizeof(ro_compat)/sizeof(struct ext2_feature); 324 i++) 325 if (mask & ro_compat[i].mask) 326 printf("%s ", ro_compat[i].name); 327 printf("\n"); 328 return (1); 329 } 330 } 331 return (0); 332 } 333 334 static e4fs_daddr_t 335 ext2_cg_location(struct m_ext2fs *fs, int number) 336 { 337 int cg, descpb, logical_sb, has_super = 0; 338 339 /* 340 * Adjust logical superblock block number. 341 * Godmar thinks: if the blocksize is greater than 1024, then 342 * the superblock is logically part of block zero. 343 */ 344 logical_sb = fs->e2fs_bsize > SBSIZE ? 0 : 1; 345 346 if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) || 347 number < fs->e2fs->e3fs_first_meta_bg) 348 return (logical_sb + number + 1); 349 350 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) 351 descpb = fs->e2fs_bsize / sizeof(struct ext2_gd); 352 else 353 descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE; 354 355 cg = descpb * number; 356 357 if (ext2_cg_has_sb(fs, cg)) 358 has_super = 1; 359 360 return (has_super + cg * (e4fs_daddr_t)EXT2_BLOCKS_PER_GROUP(fs) + 361 fs->e2fs->e2fs_first_dblock); 362 } 363 364 static int 365 ext2_cg_validate(struct m_ext2fs *fs) 366 { 367 uint64_t b_bitmap; 368 uint64_t i_bitmap; 369 uint64_t i_tables; 370 uint64_t first_block, last_block, last_cg_block; 371 struct ext2_gd *gd; 372 unsigned int i, cg_count; 373 374 first_block = fs->e2fs->e2fs_first_dblock; 375 last_cg_block = ext2_cg_number_gdb(fs, 0); 376 cg_count = fs->e2fs_gcount; 377 378 for (i = 0; i < fs->e2fs_gcount; i++) { 379 gd = &fs->e2fs_gd[i]; 380 381 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) || 382 i == fs->e2fs_gcount - 1) { 383 last_block = fs->e2fs_bcount - 1; 384 } else { 385 last_block = first_block + 386 (EXT2_BLOCKS_PER_GROUP(fs) - 1); 387 } 388 389 if ((cg_count == fs->e2fs_gcount) && 390 !(gd->ext4bgd_flags & EXT2_BG_INODE_ZEROED)) 391 cg_count = i; 392 393 b_bitmap = e2fs_gd_get_b_bitmap(gd); 394 if (b_bitmap == 0) { 395 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 396 "block bitmap is zero", i); 397 return (EINVAL); 398 399 } 400 if (b_bitmap <= last_cg_block) { 401 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 402 "block bitmap overlaps gds", i); 403 return (EINVAL); 404 } 405 if (b_bitmap < first_block || b_bitmap > last_block) { 406 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 407 "block bitmap not in group", i); 408 return (EINVAL); 409 } 410 411 i_bitmap = e2fs_gd_get_i_bitmap(gd); 412 if (i_bitmap == 0) { 413 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 414 "inode bitmap is zero", i); 415 return (EINVAL); 416 } 417 if (i_bitmap <= last_cg_block) { 418 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 419 "inode bitmap overlaps gds", i); 420 return (EINVAL); 421 } 422 if (i_bitmap < first_block || i_bitmap > last_block) { 423 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 424 "inode bitmap not in group blk", i); 425 return (EINVAL); 426 } 427 428 i_tables = e2fs_gd_get_i_tables(gd); 429 if (i_tables == 0) { 430 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 431 "inode table is zero", i); 432 return (EINVAL); 433 } 434 if (i_tables <= last_cg_block) { 435 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 436 "inode talbes overlaps gds", i); 437 return (EINVAL); 438 } 439 if (i_tables < first_block || 440 i_tables + fs->e2fs_itpg - 1 > last_block) { 441 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error, 442 "inode tables not in group blk", i); 443 return (EINVAL); 444 } 445 446 if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG)) 447 first_block += EXT2_BLOCKS_PER_GROUP(fs); 448 } 449 450 return (0); 451 } 452 453 /* 454 * This computes the fields of the m_ext2fs structure from the 455 * data in the ext2fs structure read in. 456 */ 457 static int 458 ext2_compute_sb_data(struct vnode *devvp, struct ext2fs *es, 459 struct m_ext2fs *fs) 460 { 461 struct buf *bp; 462 uint32_t e2fs_descpb, e2fs_gdbcount_alloc; 463 int i, j; 464 int g_count = 0; 465 int error; 466 467 /* Check checksum features */ 468 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) && 469 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) { 470 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 471 "incorrect checksum features combination"); 472 return (EINVAL); 473 } 474 475 /* Precompute checksum seed for all metadata */ 476 ext2_sb_csum_set_seed(fs); 477 478 /* Verify sb csum if possible */ 479 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) { 480 error = ext2_sb_csum_verify(fs); 481 if (error) { 482 return (error); 483 } 484 } 485 486 /* Check for block size = 1K|2K|4K */ 487 if (es->e2fs_log_bsize > 2) { 488 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 489 "bad block size"); 490 return (EINVAL); 491 } 492 493 fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize; 494 fs->e2fs_bsize = 1U << fs->e2fs_bshift; 495 fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1; 496 fs->e2fs_qbmask = fs->e2fs_bsize - 1; 497 498 /* Check for fragment size */ 499 if (es->e2fs_log_fsize > 500 (EXT2_MAX_FRAG_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) { 501 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 502 "invalid log cluster size"); 503 return (EINVAL); 504 } 505 506 fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << es->e2fs_log_fsize; 507 if (fs->e2fs_fsize != fs->e2fs_bsize) { 508 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 509 "fragment size != block size"); 510 return (EINVAL); 511 } 512 513 fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize; 514 515 /* Check reserved gdt blocks for future filesystem expansion */ 516 if (es->e2fs_reserved_ngdb > (fs->e2fs_bsize / 4)) { 517 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 518 "number of reserved GDT blocks too large"); 519 return (EINVAL); 520 } 521 522 if (es->e2fs_rev == E2FS_REV0) { 523 fs->e2fs_isize = E2FS_REV0_INODE_SIZE; 524 } else { 525 fs->e2fs_isize = es->e2fs_inode_size; 526 527 /* 528 * Check first ino. 529 */ 530 if (es->e2fs_first_ino < EXT2_FIRSTINO) { 531 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 532 "invalid first ino"); 533 return (EINVAL); 534 } 535 536 /* 537 * Simple sanity check for superblock inode size value. 538 */ 539 if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE || 540 EXT2_INODE_SIZE(fs) > fs->e2fs_bsize || 541 (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) { 542 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 543 "invalid inode size"); 544 return (EINVAL); 545 } 546 } 547 548 /* Check group descriptors */ 549 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) && 550 es->e3fs_desc_size != E2FS_64BIT_GD_SIZE) { 551 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 552 "unsupported 64bit descriptor size"); 553 return (EINVAL); 554 } 555 556 fs->e2fs_bpg = es->e2fs_bpg; 557 fs->e2fs_fpg = es->e2fs_fpg; 558 if (fs->e2fs_bpg == 0 || fs->e2fs_fpg == 0) { 559 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 560 "zero blocks/fragments per group"); 561 return (EINVAL); 562 } 563 if (fs->e2fs_bpg != fs->e2fs_bsize * 8) { 564 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 565 "non-standard group size unsupported"); 566 return (EINVAL); 567 } 568 569 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs); 570 if (fs->e2fs_ipb == 0 || 571 fs->e2fs_ipb > fs->e2fs_bsize / E2FS_REV0_INODE_SIZE) { 572 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 573 "bad inodes per block size"); 574 return (EINVAL); 575 } 576 577 fs->e2fs_ipg = es->e2fs_ipg; 578 if (fs->e2fs_ipg < fs->e2fs_ipb || fs->e2fs_ipg > fs->e2fs_bsize * 8) { 579 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 580 "invalid inodes per group"); 581 return (EINVAL); 582 } 583 584 fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb; 585 586 fs->e2fs_bcount = es->e2fs_bcount; 587 fs->e2fs_rbcount = es->e2fs_rbcount; 588 fs->e2fs_fbcount = es->e2fs_fbcount; 589 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) { 590 fs->e2fs_bcount |= (uint64_t)(es->e4fs_bcount_hi) << 32; 591 fs->e2fs_rbcount |= (uint64_t)(es->e4fs_rbcount_hi) << 32; 592 fs->e2fs_fbcount |= (uint64_t)(es->e4fs_fbcount_hi) << 32; 593 } 594 if (fs->e2fs_rbcount > fs->e2fs_bcount || 595 fs->e2fs_fbcount > fs->e2fs_bcount) { 596 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 597 "invalid block count"); 598 return (EINVAL); 599 } 600 if (es->e2fs_first_dblock >= fs->e2fs_bcount) { 601 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 602 "first data block out of range"); 603 return (EINVAL); 604 } 605 606 fs->e2fs_gcount = howmany(fs->e2fs_bcount - es->e2fs_first_dblock, 607 EXT2_BLOCKS_PER_GROUP(fs)); 608 if (fs->e2fs_gcount > ((uint64_t)1 << 32) - EXT2_DESCS_PER_BLOCK(fs)) { 609 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 610 "groups count too large"); 611 return (EINVAL); 612 } 613 614 /* Check for extra isize in big inodes. */ 615 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) && 616 EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) { 617 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error, 618 "no space for extra inode timestamps"); 619 return (EINVAL); 620 } 621 622 /* s_resuid / s_resgid ? */ 623 624 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) { 625 e2fs_descpb = fs->e2fs_bsize / E2FS_64BIT_GD_SIZE; 626 e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb); 627 } else { 628 e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE; 629 e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, 630 fs->e2fs_bsize / sizeof(struct ext2_gd)); 631 } 632 fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb); 633 fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize, 634 M_EXT2MNT, M_WAITOK | M_ZERO); 635 fs->e2fs_contigdirs = malloc(fs->e2fs_gcount * 636 sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO); 637 638 for (i = 0; i < fs->e2fs_gdbcount; i++) { 639 error = bread(devvp, 640 fsbtodb(fs, ext2_cg_location(fs, i)), 641 fs->e2fs_bsize, NOCRED, &bp); 642 if (error) { 643 /* 644 * fs->e2fs_gd and fs->e2fs_contigdirs 645 * will be freed later by the caller, 646 * because this function could be called from 647 * MNT_UPDATE path. 648 */ 649 brelse(bp); 650 return (error); 651 } 652 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) { 653 memcpy(&fs->e2fs_gd[ 654 i * fs->e2fs_bsize / sizeof(struct ext2_gd)], 655 bp->b_data, fs->e2fs_bsize); 656 } else { 657 for (j = 0; j < e2fs_descpb && 658 g_count < fs->e2fs_gcount; j++, g_count++) 659 memcpy(&fs->e2fs_gd[g_count], 660 bp->b_data + j * E2FS_REV0_GD_SIZE, 661 E2FS_REV0_GD_SIZE); 662 } 663 brelse(bp); 664 bp = NULL; 665 } 666 667 /* Validate cgs consistency */ 668 error = ext2_cg_validate(fs); 669 if (error) 670 return (error); 671 672 /* Verfy cgs csum */ 673 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) || 674 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) { 675 error = ext2_gd_csum_verify(fs, devvp->v_rdev); 676 if (error) 677 return (error); 678 } 679 /* Initialization for the ext2 Orlov allocator variant. */ 680 fs->e2fs_total_dir = 0; 681 for (i = 0; i < fs->e2fs_gcount; i++) 682 fs->e2fs_total_dir += e2fs_gd_get_ndirs(&fs->e2fs_gd[i]); 683 684 if (es->e2fs_rev == E2FS_REV0 || 685 !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE)) 686 fs->e2fs_maxfilesize = 0x7fffffff; 687 else { 688 fs->e2fs_maxfilesize = 0xffffffffffff; 689 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) 690 fs->e2fs_maxfilesize = 0x7fffffffffffffff; 691 } 692 if (es->e4fs_flags & E2FS_UNSIGNED_HASH) { 693 fs->e2fs_uhash = 3; 694 } else if ((es->e4fs_flags & E2FS_SIGNED_HASH) == 0) { 695 #ifdef __CHAR_UNSIGNED__ 696 es->e4fs_flags |= E2FS_UNSIGNED_HASH; 697 fs->e2fs_uhash = 3; 698 #else 699 es->e4fs_flags |= E2FS_SIGNED_HASH; 700 #endif 701 } 702 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) 703 error = ext2_sb_csum_verify(fs); 704 705 return (error); 706 } 707 708 /* 709 * Reload all incore data for a filesystem (used after running fsck on 710 * the root filesystem and finding things to fix). The filesystem must 711 * be mounted read-only. 712 * 713 * Things to do to update the mount: 714 * 1) invalidate all cached meta-data. 715 * 2) re-read superblock from disk. 716 * 3) invalidate all cluster summary information. 717 * 4) invalidate all inactive vnodes. 718 * 5) invalidate all cached file data. 719 * 6) re-read inode data for all active vnodes. 720 * XXX we are missing some steps, in particular # 3, this has to be reviewed. 721 */ 722 static int 723 ext2_reload(struct mount *mp, struct thread *td) 724 { 725 struct vnode *vp, *mvp, *devvp; 726 struct inode *ip; 727 struct buf *bp; 728 struct ext2fs *es; 729 struct m_ext2fs *fs; 730 struct csum *sump; 731 int error, i; 732 int32_t *lp; 733 734 if ((mp->mnt_flag & MNT_RDONLY) == 0) 735 return (EINVAL); 736 /* 737 * Step 1: invalidate all cached meta-data. 738 */ 739 devvp = VFSTOEXT2(mp)->um_devvp; 740 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 741 if (vinvalbuf(devvp, 0, 0, 0) != 0) 742 panic("ext2_reload: dirty1"); 743 VOP_UNLOCK(devvp, 0); 744 745 /* 746 * Step 2: re-read superblock from disk. 747 * constants have been adjusted for ext2 748 */ 749 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0) 750 return (error); 751 es = (struct ext2fs *)bp->b_data; 752 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) { 753 brelse(bp); 754 return (EIO); /* XXX needs translation */ 755 } 756 fs = VFSTOEXT2(mp)->um_e2fs; 757 bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs)); 758 759 if ((error = ext2_compute_sb_data(devvp, es, fs)) != 0) { 760 brelse(bp); 761 return (error); 762 } 763 #ifdef UNKLAR 764 if (fs->fs_sbsize < SBSIZE) 765 bp->b_flags |= B_INVAL; 766 #endif 767 brelse(bp); 768 769 /* 770 * Step 3: invalidate all cluster summary information. 771 */ 772 if (fs->e2fs_contigsumsize > 0) { 773 lp = fs->e2fs_maxcluster; 774 sump = fs->e2fs_clustersum; 775 for (i = 0; i < fs->e2fs_gcount; i++, sump++) { 776 *lp++ = fs->e2fs_contigsumsize; 777 sump->cs_init = 0; 778 bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1); 779 } 780 } 781 782 loop: 783 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 784 /* 785 * Step 4: invalidate all cached file data. 786 */ 787 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) { 788 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 789 goto loop; 790 } 791 if (vinvalbuf(vp, 0, 0, 0)) 792 panic("ext2_reload: dirty2"); 793 794 /* 795 * Step 5: re-read inode data for all active vnodes. 796 */ 797 ip = VTOI(vp); 798 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 799 (int)fs->e2fs_bsize, NOCRED, &bp); 800 if (error) { 801 VOP_UNLOCK(vp, 0); 802 vrele(vp); 803 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 804 return (error); 805 } 806 807 error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data + 808 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip); 809 810 brelse(bp); 811 VOP_UNLOCK(vp, 0); 812 vrele(vp); 813 814 if (error) { 815 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 816 return (error); 817 } 818 } 819 return (0); 820 } 821 822 /* 823 * Common code for mount and mountroot. 824 */ 825 static int 826 ext2_mountfs(struct vnode *devvp, struct mount *mp) 827 { 828 struct ext2mount *ump; 829 struct buf *bp; 830 struct m_ext2fs *fs; 831 struct ext2fs *es; 832 struct cdev *dev = devvp->v_rdev; 833 struct g_consumer *cp; 834 struct bufobj *bo; 835 struct csum *sump; 836 int error; 837 int ronly; 838 int i; 839 u_long size; 840 int32_t *lp; 841 int32_t e2fs_maxcontig; 842 843 ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0); 844 /* XXX: use VOP_ACESS to check FS perms */ 845 g_topology_lock(); 846 error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1); 847 g_topology_unlock(); 848 VOP_UNLOCK(devvp, 0); 849 if (error) 850 return (error); 851 852 /* XXX: should we check for some sectorsize or 512 instead? */ 853 if (((SBSIZE % cp->provider->sectorsize) != 0) || 854 (SBSIZE < cp->provider->sectorsize)) { 855 g_topology_lock(); 856 g_vfs_close(cp); 857 g_topology_unlock(); 858 return (EINVAL); 859 } 860 861 bo = &devvp->v_bufobj; 862 bo->bo_private = cp; 863 bo->bo_ops = g_vfs_bufops; 864 if (devvp->v_rdev->si_iosize_max != 0) 865 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max; 866 if (mp->mnt_iosize_max > MAXPHYS) 867 mp->mnt_iosize_max = MAXPHYS; 868 869 bp = NULL; 870 ump = NULL; 871 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0) 872 goto out; 873 es = (struct ext2fs *)bp->b_data; 874 if (ext2_check_sb_compat(es, dev, ronly) != 0) { 875 error = EINVAL; /* XXX needs translation */ 876 goto out; 877 } 878 if ((es->e2fs_state & E2FS_ISCLEAN) == 0 || 879 (es->e2fs_state & E2FS_ERRORS)) { 880 if (ronly || (mp->mnt_flag & MNT_FORCE)) { 881 printf( 882 "WARNING: Filesystem was not properly dismounted\n"); 883 } else { 884 printf( 885 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n"); 886 error = EPERM; 887 goto out; 888 } 889 } 890 ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO); 891 892 /* 893 * I don't know whether this is the right strategy. Note that 894 * we dynamically allocate both an m_ext2fs and an ext2fs 895 * while Linux keeps the super block in a locked buffer. 896 */ 897 ump->um_e2fs = malloc(sizeof(struct m_ext2fs), 898 M_EXT2MNT, M_WAITOK | M_ZERO); 899 ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs), 900 M_EXT2MNT, M_WAITOK); 901 mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF); 902 bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs)); 903 if ((error = ext2_compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs))) 904 goto out; 905 906 /* 907 * Calculate the maximum contiguous blocks and size of cluster summary 908 * array. In FFS this is done by newfs; however, the superblock 909 * in ext2fs doesn't have these variables, so we can calculate 910 * them here. 911 */ 912 e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize); 913 ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG); 914 if (ump->um_e2fs->e2fs_contigsumsize > 0) { 915 size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t); 916 ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK); 917 size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum); 918 ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK); 919 lp = ump->um_e2fs->e2fs_maxcluster; 920 sump = ump->um_e2fs->e2fs_clustersum; 921 for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) { 922 *lp++ = ump->um_e2fs->e2fs_contigsumsize; 923 sump->cs_init = 0; 924 sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) * 925 sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO); 926 } 927 } 928 929 brelse(bp); 930 bp = NULL; 931 fs = ump->um_e2fs; 932 fs->e2fs_ronly = ronly; /* ronly is set according to mnt_flags */ 933 934 /* 935 * If the fs is not mounted read-only, make sure the super block is 936 * always written back on a sync(). 937 */ 938 fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0; 939 if (ronly == 0) { 940 fs->e2fs_fmod = 1; /* mark it modified */ 941 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN; /* set fs invalid */ 942 } 943 mp->mnt_data = ump; 944 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 945 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 946 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN; 947 MNT_ILOCK(mp); 948 mp->mnt_flag |= MNT_LOCAL; 949 MNT_IUNLOCK(mp); 950 ump->um_mountp = mp; 951 ump->um_dev = dev; 952 ump->um_devvp = devvp; 953 ump->um_bo = &devvp->v_bufobj; 954 ump->um_cp = cp; 955 956 /* 957 * Setting those two parameters allowed us to use 958 * ufs_bmap w/o changse! 959 */ 960 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs); 961 ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1; 962 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs); 963 if (ronly == 0) 964 ext2_sbupdate(ump, MNT_WAIT); 965 /* 966 * Initialize filesystem stat information in mount struct. 967 */ 968 MNT_ILOCK(mp); 969 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED | 970 MNTK_USES_BCACHE; 971 MNT_IUNLOCK(mp); 972 return (0); 973 out: 974 if (bp) 975 brelse(bp); 976 if (cp != NULL) { 977 g_topology_lock(); 978 g_vfs_close(cp); 979 g_topology_unlock(); 980 } 981 if (ump) { 982 mtx_destroy(EXT2_MTX(ump)); 983 free(ump->um_e2fs->e2fs_gd, M_EXT2MNT); 984 free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT); 985 free(ump->um_e2fs->e2fs, M_EXT2MNT); 986 free(ump->um_e2fs, M_EXT2MNT); 987 free(ump, M_EXT2MNT); 988 mp->mnt_data = NULL; 989 } 990 return (error); 991 } 992 993 /* 994 * Unmount system call. 995 */ 996 static int 997 ext2_unmount(struct mount *mp, int mntflags) 998 { 999 struct ext2mount *ump; 1000 struct m_ext2fs *fs; 1001 struct csum *sump; 1002 int error, flags, i, ronly; 1003 1004 flags = 0; 1005 if (mntflags & MNT_FORCE) { 1006 if (mp->mnt_flag & MNT_ROOTFS) 1007 return (EINVAL); 1008 flags |= FORCECLOSE; 1009 } 1010 if ((error = ext2_flushfiles(mp, flags, curthread)) != 0) 1011 return (error); 1012 ump = VFSTOEXT2(mp); 1013 fs = ump->um_e2fs; 1014 ronly = fs->e2fs_ronly; 1015 if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) { 1016 if (fs->e2fs_wasvalid) 1017 fs->e2fs->e2fs_state |= E2FS_ISCLEAN; 1018 ext2_sbupdate(ump, MNT_WAIT); 1019 } 1020 1021 g_topology_lock(); 1022 g_vfs_close(ump->um_cp); 1023 g_topology_unlock(); 1024 vrele(ump->um_devvp); 1025 sump = fs->e2fs_clustersum; 1026 for (i = 0; i < fs->e2fs_gcount; i++, sump++) 1027 free(sump->cs_sum, M_EXT2MNT); 1028 free(fs->e2fs_clustersum, M_EXT2MNT); 1029 free(fs->e2fs_maxcluster, M_EXT2MNT); 1030 free(fs->e2fs_gd, M_EXT2MNT); 1031 free(fs->e2fs_contigdirs, M_EXT2MNT); 1032 free(fs->e2fs, M_EXT2MNT); 1033 free(fs, M_EXT2MNT); 1034 free(ump, M_EXT2MNT); 1035 mp->mnt_data = NULL; 1036 MNT_ILOCK(mp); 1037 mp->mnt_flag &= ~MNT_LOCAL; 1038 MNT_IUNLOCK(mp); 1039 return (error); 1040 } 1041 1042 /* 1043 * Flush out all the files in a filesystem. 1044 */ 1045 static int 1046 ext2_flushfiles(struct mount *mp, int flags, struct thread *td) 1047 { 1048 int error; 1049 1050 error = vflush(mp, 0, flags, td); 1051 return (error); 1052 } 1053 1054 /* 1055 * Get filesystem statistics. 1056 */ 1057 int 1058 ext2_statfs(struct mount *mp, struct statfs *sbp) 1059 { 1060 struct ext2mount *ump; 1061 struct m_ext2fs *fs; 1062 uint32_t overhead, overhead_per_group, ngdb; 1063 int i, ngroups; 1064 1065 ump = VFSTOEXT2(mp); 1066 fs = ump->um_e2fs; 1067 if (fs->e2fs->e2fs_magic != E2FS_MAGIC) 1068 panic("ext2_statfs"); 1069 1070 /* 1071 * Compute the overhead (FS structures) 1072 */ 1073 overhead_per_group = 1074 1 /* block bitmap */ + 1075 1 /* inode bitmap */ + 1076 fs->e2fs_itpg; 1077 overhead = fs->e2fs->e2fs_first_dblock + 1078 fs->e2fs_gcount * overhead_per_group; 1079 if (fs->e2fs->e2fs_rev > E2FS_REV0 && 1080 fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) { 1081 for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) { 1082 if (ext2_cg_has_sb(fs, i)) 1083 ngroups++; 1084 } 1085 } else { 1086 ngroups = fs->e2fs_gcount; 1087 } 1088 ngdb = fs->e2fs_gdbcount; 1089 if (fs->e2fs->e2fs_rev > E2FS_REV0 && 1090 fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE) 1091 ngdb += fs->e2fs->e2fs_reserved_ngdb; 1092 overhead += ngroups * (1 /* superblock */ + ngdb); 1093 1094 sbp->f_bsize = EXT2_FRAG_SIZE(fs); 1095 sbp->f_iosize = EXT2_BLOCK_SIZE(fs); 1096 sbp->f_blocks = fs->e2fs_bcount - overhead; 1097 sbp->f_bfree = fs->e2fs_fbcount; 1098 sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount; 1099 sbp->f_files = fs->e2fs->e2fs_icount; 1100 sbp->f_ffree = fs->e2fs->e2fs_ficount; 1101 return (0); 1102 } 1103 1104 /* 1105 * Go through the disk queues to initiate sandbagged IO; 1106 * go through the inodes to write those that have been modified; 1107 * initiate the writing of the super block if it has been modified. 1108 * 1109 * Note: we are always called with the filesystem marked `MPBUSY'. 1110 */ 1111 static int 1112 ext2_sync(struct mount *mp, int waitfor) 1113 { 1114 struct vnode *mvp, *vp; 1115 struct thread *td; 1116 struct inode *ip; 1117 struct ext2mount *ump = VFSTOEXT2(mp); 1118 struct m_ext2fs *fs; 1119 int error, allerror = 0; 1120 1121 td = curthread; 1122 fs = ump->um_e2fs; 1123 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */ 1124 panic("ext2_sync: rofs mod fs=%s", fs->e2fs_fsmnt); 1125 } 1126 1127 /* 1128 * Write back each (modified) inode. 1129 */ 1130 loop: 1131 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 1132 if (vp->v_type == VNON) { 1133 VI_UNLOCK(vp); 1134 continue; 1135 } 1136 ip = VTOI(vp); 1137 if ((ip->i_flag & 1138 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 1139 (vp->v_bufobj.bo_dirty.bv_cnt == 0 || 1140 waitfor == MNT_LAZY)) { 1141 VI_UNLOCK(vp); 1142 continue; 1143 } 1144 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 1145 if (error) { 1146 if (error == ENOENT) { 1147 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 1148 goto loop; 1149 } 1150 continue; 1151 } 1152 if ((error = VOP_FSYNC(vp, waitfor, td)) != 0) 1153 allerror = error; 1154 VOP_UNLOCK(vp, 0); 1155 vrele(vp); 1156 } 1157 1158 /* 1159 * Force stale filesystem control information to be flushed. 1160 */ 1161 if (waitfor != MNT_LAZY) { 1162 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 1163 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0) 1164 allerror = error; 1165 VOP_UNLOCK(ump->um_devvp, 0); 1166 } 1167 1168 /* 1169 * Write back modified superblock. 1170 */ 1171 if (fs->e2fs_fmod != 0) { 1172 fs->e2fs_fmod = 0; 1173 fs->e2fs->e2fs_wtime = time_second; 1174 if ((error = ext2_cgupdate(ump, waitfor)) != 0) 1175 allerror = error; 1176 } 1177 return (allerror); 1178 } 1179 1180 /* 1181 * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it 1182 * in from disk. If it is in core, wait for the lock bit to clear, then 1183 * return the inode locked. Detection and handling of mount points must be 1184 * done by the calling routine. 1185 */ 1186 static int 1187 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 1188 { 1189 struct m_ext2fs *fs; 1190 struct inode *ip; 1191 struct ext2mount *ump; 1192 struct buf *bp; 1193 struct vnode *vp; 1194 struct thread *td; 1195 unsigned int i, used_blocks; 1196 int error; 1197 1198 td = curthread; 1199 error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL); 1200 if (error || *vpp != NULL) 1201 return (error); 1202 1203 ump = VFSTOEXT2(mp); 1204 ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO); 1205 1206 /* Allocate a new vnode/inode. */ 1207 if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) { 1208 *vpp = NULL; 1209 free(ip, M_EXT2NODE); 1210 return (error); 1211 } 1212 vp->v_data = ip; 1213 ip->i_vnode = vp; 1214 ip->i_e2fs = fs = ump->um_e2fs; 1215 ip->i_ump = ump; 1216 ip->i_number = ino; 1217 1218 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL); 1219 error = insmntque(vp, mp); 1220 if (error != 0) { 1221 free(ip, M_EXT2NODE); 1222 *vpp = NULL; 1223 return (error); 1224 } 1225 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL); 1226 if (error || *vpp != NULL) 1227 return (error); 1228 1229 /* Read in the disk contents for the inode, copy into the inode. */ 1230 if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 1231 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) { 1232 /* 1233 * The inode does not contain anything useful, so it would 1234 * be misleading to leave it on its hash chain. With mode 1235 * still zero, it will be unlinked and returned to the free 1236 * list by vput(). 1237 */ 1238 brelse(bp); 1239 vput(vp); 1240 *vpp = NULL; 1241 return (error); 1242 } 1243 /* convert ext2 inode to dinode */ 1244 error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data + 1245 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ino)), ip); 1246 if (error) { 1247 brelse(bp); 1248 vput(vp); 1249 *vpp = NULL; 1250 return (error); 1251 } 1252 ip->i_block_group = ino_to_cg(fs, ino); 1253 ip->i_next_alloc_block = 0; 1254 ip->i_next_alloc_goal = 0; 1255 1256 /* 1257 * Now we want to make sure that block pointers for unused 1258 * blocks are zeroed out - ext2_balloc depends on this 1259 * although for regular files and directories only 1260 * 1261 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed 1262 * out because we could corrupt the extent tree. 1263 */ 1264 if (!(ip->i_flag & IN_E4EXTENTS) && 1265 (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) { 1266 used_blocks = howmany(ip->i_size, fs->e2fs_bsize); 1267 for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++) 1268 ip->i_db[i] = 0; 1269 } 1270 #ifdef EXT2FS_PRINT_EXTENTS 1271 ext2_print_inode(ip); 1272 ext4_ext_print_extent_tree_status(ip); 1273 #endif 1274 bqrelse(bp); 1275 1276 /* 1277 * Initialize the vnode from the inode, check for aliases. 1278 * Note that the underlying vnode may have changed. 1279 */ 1280 if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) { 1281 vput(vp); 1282 *vpp = NULL; 1283 return (error); 1284 } 1285 1286 /* 1287 * Finish inode initialization. 1288 */ 1289 1290 *vpp = vp; 1291 return (0); 1292 } 1293 1294 /* 1295 * File handle to vnode 1296 * 1297 * Have to be really careful about stale file handles: 1298 * - check that the inode number is valid 1299 * - call ext2_vget() to get the locked inode 1300 * - check for an unallocated inode (i_mode == 0) 1301 * - check that the given client host has export rights and return 1302 * those rights via. exflagsp and credanonp 1303 */ 1304 static int 1305 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp) 1306 { 1307 struct inode *ip; 1308 struct ufid *ufhp; 1309 struct vnode *nvp; 1310 struct m_ext2fs *fs; 1311 int error; 1312 1313 ufhp = (struct ufid *)fhp; 1314 fs = VFSTOEXT2(mp)->um_e2fs; 1315 if (ufhp->ufid_ino < EXT2_ROOTINO || 1316 ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg) 1317 return (ESTALE); 1318 1319 error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp); 1320 if (error) { 1321 *vpp = NULLVP; 1322 return (error); 1323 } 1324 ip = VTOI(nvp); 1325 if (ip->i_mode == 0 || 1326 ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) { 1327 vput(nvp); 1328 *vpp = NULLVP; 1329 return (ESTALE); 1330 } 1331 *vpp = nvp; 1332 vnode_create_vobject(*vpp, 0, curthread); 1333 return (0); 1334 } 1335 1336 /* 1337 * Write a superblock and associated information back to disk. 1338 */ 1339 static int 1340 ext2_sbupdate(struct ext2mount *mp, int waitfor) 1341 { 1342 struct m_ext2fs *fs = mp->um_e2fs; 1343 struct ext2fs *es = fs->e2fs; 1344 struct buf *bp; 1345 int error = 0; 1346 1347 es->e2fs_bcount = fs->e2fs_bcount & 0xffffffff; 1348 es->e2fs_rbcount = fs->e2fs_rbcount & 0xffffffff; 1349 es->e2fs_fbcount = fs->e2fs_fbcount & 0xffffffff; 1350 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) { 1351 es->e4fs_bcount_hi = fs->e2fs_bcount >> 32; 1352 es->e4fs_rbcount_hi = fs->e2fs_rbcount >> 32; 1353 es->e4fs_fbcount_hi = fs->e2fs_fbcount >> 32; 1354 } 1355 1356 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) 1357 ext2_sb_csum_set(fs); 1358 1359 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0); 1360 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs)); 1361 if (waitfor == MNT_WAIT) 1362 error = bwrite(bp); 1363 else 1364 bawrite(bp); 1365 1366 /* 1367 * The buffers for group descriptors, inode bitmaps and block bitmaps 1368 * are not busy at this point and are (hopefully) written by the 1369 * usual sync mechanism. No need to write them here. 1370 */ 1371 return (error); 1372 } 1373 int 1374 ext2_cgupdate(struct ext2mount *mp, int waitfor) 1375 { 1376 struct m_ext2fs *fs = mp->um_e2fs; 1377 struct buf *bp; 1378 int i, j, g_count = 0, error = 0, allerror = 0; 1379 1380 allerror = ext2_sbupdate(mp, waitfor); 1381 1382 /* Update gd csums */ 1383 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) || 1384 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) 1385 ext2_gd_csum_set(fs); 1386 1387 for (i = 0; i < fs->e2fs_gdbcount; i++) { 1388 bp = getblk(mp->um_devvp, fsbtodb(fs, 1389 ext2_cg_location(fs, i)), 1390 fs->e2fs_bsize, 0, 0, 0); 1391 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) { 1392 memcpy(bp->b_data, &fs->e2fs_gd[ 1393 i * fs->e2fs_bsize / sizeof(struct ext2_gd)], 1394 fs->e2fs_bsize); 1395 } else { 1396 for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE && 1397 g_count < fs->e2fs_gcount; j++, g_count++) 1398 memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE, 1399 &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE); 1400 } 1401 if (waitfor == MNT_WAIT) 1402 error = bwrite(bp); 1403 else 1404 bawrite(bp); 1405 } 1406 1407 if (!allerror && error) 1408 allerror = error; 1409 return (allerror); 1410 } 1411 1412 /* 1413 * Return the root of a filesystem. 1414 */ 1415 static int 1416 ext2_root(struct mount *mp, int flags, struct vnode **vpp) 1417 { 1418 struct vnode *nvp; 1419 int error; 1420 1421 error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp); 1422 if (error) 1423 return (error); 1424 *vpp = nvp; 1425 return (0); 1426 } 1427