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