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