1 /* 2 * Copyright (c) 1989, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_mac.h" 36 #include "opt_quota.h" 37 #include "opt_ufs.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/namei.h> 42 #include <sys/proc.h> 43 #include <sys/kernel.h> 44 #include <sys/mac.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/fcntl.h> 51 #include <sys/disk.h> 52 #include <sys/malloc.h> 53 #include <sys/mutex.h> 54 55 #include <ufs/ufs/extattr.h> 56 #include <ufs/ufs/quota.h> 57 #include <ufs/ufs/ufsmount.h> 58 #include <ufs/ufs/inode.h> 59 #include <ufs/ufs/ufs_extern.h> 60 61 #include <ufs/ffs/fs.h> 62 #include <ufs/ffs/ffs_extern.h> 63 64 #include <vm/vm.h> 65 #include <vm/uma.h> 66 #include <vm/vm_page.h> 67 68 uma_zone_t uma_inode, uma_ufs1, uma_ufs2; 69 70 static int ffs_sbupdate(struct ufsmount *, int); 71 int ffs_reload(struct mount *,struct ucred *,struct thread *); 72 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *); 73 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, 74 ufs2_daddr_t); 75 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *); 76 static void ffs_ifree(struct ufsmount *ump, struct inode *ip); 77 static vfs_init_t ffs_init; 78 static vfs_uninit_t ffs_uninit; 79 static vfs_extattrctl_t ffs_extattrctl; 80 81 static struct vfsops ufs_vfsops = { 82 .vfs_extattrctl = ffs_extattrctl, 83 .vfs_fhtovp = ffs_fhtovp, 84 .vfs_init = ffs_init, 85 .vfs_mount = ffs_mount, 86 .vfs_quotactl = ufs_quotactl, 87 .vfs_root = ufs_root, 88 .vfs_start = ufs_start, 89 .vfs_statfs = ffs_statfs, 90 .vfs_sync = ffs_sync, 91 .vfs_uninit = ffs_uninit, 92 .vfs_unmount = ffs_unmount, 93 .vfs_vget = ffs_vget, 94 .vfs_vptofh = ffs_vptofh, 95 }; 96 97 VFS_SET(ufs_vfsops, ufs, 0); 98 99 /* 100 * ffs_mount 101 * 102 * Called when mounting local physical media 103 * 104 * PARAMETERS: 105 * mountroot 106 * mp mount point structure 107 * path NULL (flag for root mount!!!) 108 * data <unused> 109 * ndp <unused> 110 * p process (user credentials check [statfs]) 111 * 112 * mount 113 * mp mount point structure 114 * path path to mount point 115 * data pointer to argument struct in user space 116 * ndp mount point namei() return (used for 117 * credentials on reload), reused to look 118 * up block device. 119 * p process (user credentials check) 120 * 121 * RETURNS: 0 Success 122 * !0 error number (errno.h) 123 * 124 * LOCK STATE: 125 * 126 * ENTRY 127 * mount point is locked 128 * EXIT 129 * mount point is locked 130 * 131 * NOTES: 132 * A NULL path can be used for a flag since the mount 133 * system call will fail with EFAULT in copyinstr in 134 * namei() if it is a genuine NULL from the user. 135 */ 136 int 137 ffs_mount(mp, path, data, ndp, td) 138 struct mount *mp; /* mount struct pointer*/ 139 char *path; /* path to mount point*/ 140 caddr_t data; /* arguments to FS specific mount*/ 141 struct nameidata *ndp; /* mount point credentials*/ 142 struct thread *td; /* process requesting mount*/ 143 { 144 size_t size; 145 struct vnode *devvp; 146 struct ufs_args args; 147 struct ufsmount *ump = 0; 148 struct fs *fs; 149 int error, flags; 150 mode_t accessmode; 151 152 if (uma_inode == NULL) { 153 uma_inode = uma_zcreate("FFS inode", 154 sizeof(struct inode), NULL, NULL, NULL, NULL, 155 UMA_ALIGN_PTR, 0); 156 uma_ufs1 = uma_zcreate("FFS1 dinode", 157 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL, 158 UMA_ALIGN_PTR, 0); 159 uma_ufs2 = uma_zcreate("FFS2 dinode", 160 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL, 161 UMA_ALIGN_PTR, 0); 162 } 163 /* 164 * Use NULL path to indicate we are mounting the root filesystem. 165 */ 166 if (path == NULL) { 167 if ((error = bdevvp(rootdev, &rootvp))) { 168 printf("ffs_mountroot: can't find rootvp\n"); 169 return (error); 170 } 171 172 if ((error = ffs_mountfs(rootvp, mp, td)) != 0) 173 return (error); 174 return (0); 175 } 176 177 /* 178 * Mounting non-root filesystem or updating a filesystem 179 */ 180 if ((error = copyin(data, (caddr_t)&args, sizeof(struct ufs_args)))!= 0) 181 return (error); 182 183 /* 184 * If updating, check whether changing from read-only to 185 * read/write; if there is no device name, that's all we do. 186 */ 187 if (mp->mnt_flag & MNT_UPDATE) { 188 ump = VFSTOUFS(mp); 189 fs = ump->um_fs; 190 devvp = ump->um_devvp; 191 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 192 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0) 193 return (error); 194 /* 195 * Flush any dirty data. 196 */ 197 if ((error = VFS_SYNC(mp, MNT_WAIT, 198 td->td_ucred, td)) != 0) { 199 vn_finished_write(mp); 200 return (error); 201 } 202 /* 203 * Check for and optionally get rid of files open 204 * for writing. 205 */ 206 flags = WRITECLOSE; 207 if (mp->mnt_flag & MNT_FORCE) 208 flags |= FORCECLOSE; 209 if (mp->mnt_flag & MNT_SOFTDEP) { 210 error = softdep_flushfiles(mp, flags, td); 211 } else { 212 error = ffs_flushfiles(mp, flags, td); 213 } 214 if (error) { 215 vn_finished_write(mp); 216 return (error); 217 } 218 if (fs->fs_pendingblocks != 0 || 219 fs->fs_pendinginodes != 0) { 220 printf("%s: %s: blocks %jd files %d\n", 221 fs->fs_fsmnt, "update error", 222 (intmax_t)fs->fs_pendingblocks, 223 fs->fs_pendinginodes); 224 fs->fs_pendingblocks = 0; 225 fs->fs_pendinginodes = 0; 226 } 227 fs->fs_ronly = 1; 228 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) 229 fs->fs_clean = 1; 230 if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) { 231 fs->fs_ronly = 0; 232 fs->fs_clean = 0; 233 vn_finished_write(mp); 234 return (error); 235 } 236 vn_finished_write(mp); 237 } 238 if ((mp->mnt_flag & MNT_RELOAD) && 239 (error = ffs_reload(mp, ndp->ni_cnd.cn_cred, td)) != 0) 240 return (error); 241 if (fs->fs_ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 242 /* 243 * If upgrade to read-write by non-root, then verify 244 * that user has necessary permissions on the device. 245 */ 246 if (suser(td)) { 247 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 248 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE, 249 td->td_ucred, td)) != 0) { 250 VOP_UNLOCK(devvp, 0, td); 251 return (error); 252 } 253 VOP_UNLOCK(devvp, 0, td); 254 } 255 fs->fs_flags &= ~FS_UNCLEAN; 256 if (fs->fs_clean == 0) { 257 fs->fs_flags |= FS_UNCLEAN; 258 if ((mp->mnt_flag & MNT_FORCE) || 259 ((fs->fs_flags & FS_NEEDSFSCK) == 0 && 260 (fs->fs_flags & FS_DOSOFTDEP))) { 261 printf("WARNING: %s was not %s\n", 262 fs->fs_fsmnt, "properly dismounted"); 263 } else { 264 printf( 265 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 266 fs->fs_fsmnt); 267 return (EPERM); 268 } 269 } 270 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0) 271 return (error); 272 fs->fs_ronly = 0; 273 fs->fs_clean = 0; 274 if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) { 275 vn_finished_write(mp); 276 return (error); 277 } 278 /* check to see if we need to start softdep */ 279 if ((fs->fs_flags & FS_DOSOFTDEP) && 280 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){ 281 vn_finished_write(mp); 282 return (error); 283 } 284 if (fs->fs_snapinum[0] != 0) 285 ffs_snapshot_mount(mp); 286 vn_finished_write(mp); 287 } 288 /* 289 * Soft updates is incompatible with "async", 290 * so if we are doing softupdates stop the user 291 * from setting the async flag in an update. 292 * Softdep_mount() clears it in an initial mount 293 * or ro->rw remount. 294 */ 295 if (mp->mnt_flag & MNT_SOFTDEP) 296 mp->mnt_flag &= ~MNT_ASYNC; 297 /* 298 * If not updating name, process export requests. 299 */ 300 if (args.fspec == 0) 301 return (vfs_export(mp, &args.export)); 302 /* 303 * If this is a snapshot request, take the snapshot. 304 */ 305 if (mp->mnt_flag & MNT_SNAPSHOT) 306 return (ffs_snapshot(mp, args.fspec)); 307 } 308 309 /* 310 * Not an update, or updating the name: look up the name 311 * and verify that it refers to a sensible disk device. 312 */ 313 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td); 314 if ((error = namei(ndp)) != 0) 315 return (error); 316 NDFREE(ndp, NDF_ONLY_PNBUF); 317 devvp = ndp->ni_vp; 318 if (!vn_isdisk(devvp, &error)) { 319 vrele(devvp); 320 return (error); 321 } 322 323 /* 324 * If mount by non-root, then verify that user has necessary 325 * permissions on the device. 326 */ 327 if (suser(td)) { 328 accessmode = VREAD; 329 if ((mp->mnt_flag & MNT_RDONLY) == 0) 330 accessmode |= VWRITE; 331 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 332 if ((error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td))!= 0){ 333 vput(devvp); 334 return (error); 335 } 336 VOP_UNLOCK(devvp, 0, td); 337 } 338 339 if (mp->mnt_flag & MNT_UPDATE) { 340 /* 341 * Update only 342 * 343 * If it's not the same vnode, or at least the same device 344 * then it's not correct. 345 */ 346 347 if (devvp != ump->um_devvp && 348 devvp->v_rdev != ump->um_devvp->v_rdev) 349 error = EINVAL; /* needs translation */ 350 vrele(devvp); 351 if (error) 352 return (error); 353 } else { 354 /* 355 * New mount 356 * 357 * We need the name for the mount point (also used for 358 * "last mounted on") copied in. If an error occurs, 359 * the mount point is discarded by the upper level code. 360 * Note that vfs_mount() populates f_mntonname for us. 361 */ 362 if ((error = ffs_mountfs(devvp, mp, td)) != 0) { 363 vrele(devvp); 364 return (error); 365 } 366 } 367 /* 368 * Save "mounted from" device name info for mount point (NULL pad). 369 */ 370 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); 371 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 372 return (0); 373 } 374 375 /* 376 * Reload all incore data for a filesystem (used after running fsck on 377 * the root filesystem and finding things to fix). The filesystem must 378 * be mounted read-only. 379 * 380 * Things to do to update the mount: 381 * 1) invalidate all cached meta-data. 382 * 2) re-read superblock from disk. 383 * 3) re-read summary information from disk. 384 * 4) invalidate all inactive vnodes. 385 * 5) invalidate all cached file data. 386 * 6) re-read inode data for all active vnodes. 387 */ 388 int 389 ffs_reload(mp, cred, td) 390 struct mount *mp; 391 struct ucred *cred; 392 struct thread *td; 393 { 394 struct vnode *vp, *nvp, *devvp; 395 struct inode *ip; 396 void *space; 397 struct buf *bp; 398 struct fs *fs, *newfs; 399 ufs2_daddr_t sblockloc; 400 int i, blks, size, error; 401 int32_t *lp; 402 403 if ((mp->mnt_flag & MNT_RDONLY) == 0) 404 return (EINVAL); 405 /* 406 * Step 1: invalidate all cached meta-data. 407 */ 408 devvp = VFSTOUFS(mp)->um_devvp; 409 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 410 if (vinvalbuf(devvp, 0, cred, td, 0, 0) != 0) 411 panic("ffs_reload: dirty1"); 412 /* 413 * Only VMIO the backing device if the backing device is a real 414 * disk device. See ffs_mountfs() for more details. 415 */ 416 if (vn_isdisk(devvp, NULL)) 417 vfs_object_create(devvp, td, td->td_ucred); 418 VOP_UNLOCK(devvp, 0, td); 419 420 /* 421 * Step 2: re-read superblock from disk. 422 */ 423 fs = VFSTOUFS(mp)->um_fs; 424 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize, 425 NOCRED, &bp)) != 0) 426 return (error); 427 newfs = (struct fs *)bp->b_data; 428 if ((newfs->fs_magic != FS_UFS1_MAGIC && 429 newfs->fs_magic != FS_UFS2_MAGIC) || 430 newfs->fs_bsize > MAXBSIZE || 431 newfs->fs_bsize < sizeof(struct fs)) { 432 brelse(bp); 433 return (EIO); /* XXX needs translation */ 434 } 435 /* 436 * Copy pointer fields back into superblock before copying in XXX 437 * new superblock. These should really be in the ufsmount. XXX 438 * Note that important parameters (eg fs_ncg) are unchanged. 439 */ 440 newfs->fs_csp = fs->fs_csp; 441 newfs->fs_maxcluster = fs->fs_maxcluster; 442 newfs->fs_contigdirs = fs->fs_contigdirs; 443 newfs->fs_active = fs->fs_active; 444 /* The file system is still read-only. */ 445 newfs->fs_ronly = 1; 446 sblockloc = fs->fs_sblockloc; 447 bcopy(newfs, fs, (u_int)fs->fs_sbsize); 448 brelse(bp); 449 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 450 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc); 451 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 452 printf("%s: reload pending error: blocks %jd files %d\n", 453 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 454 fs->fs_pendinginodes); 455 fs->fs_pendingblocks = 0; 456 fs->fs_pendinginodes = 0; 457 } 458 459 /* 460 * Step 3: re-read summary information from disk. 461 */ 462 blks = howmany(fs->fs_cssize, fs->fs_fsize); 463 space = fs->fs_csp; 464 for (i = 0; i < blks; i += fs->fs_frag) { 465 size = fs->fs_bsize; 466 if (i + fs->fs_frag > blks) 467 size = (blks - i) * fs->fs_fsize; 468 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 469 NOCRED, &bp); 470 if (error) 471 return (error); 472 bcopy(bp->b_data, space, (u_int)size); 473 space = (char *)space + size; 474 brelse(bp); 475 } 476 /* 477 * We no longer know anything about clusters per cylinder group. 478 */ 479 if (fs->fs_contigsumsize > 0) { 480 lp = fs->fs_maxcluster; 481 for (i = 0; i < fs->fs_ncg; i++) 482 *lp++ = fs->fs_contigsumsize; 483 } 484 485 loop: 486 MNT_ILOCK(mp); 487 MNT_VNODE_FOREACH(vp, mp, nvp) { 488 VI_LOCK(vp); 489 if (vp->v_iflag & VI_XLOCK) { 490 VI_UNLOCK(vp); 491 continue; 492 } 493 MNT_IUNLOCK(mp); 494 /* 495 * Step 4: invalidate all inactive vnodes. 496 */ 497 if (vp->v_usecount == 0) { 498 vgonel(vp, td); 499 goto loop; 500 } 501 /* 502 * Step 5: invalidate all cached file data. 503 */ 504 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) { 505 goto loop; 506 } 507 if (vinvalbuf(vp, 0, cred, td, 0, 0)) 508 panic("ffs_reload: dirty2"); 509 /* 510 * Step 6: re-read inode data for all active vnodes. 511 */ 512 ip = VTOI(vp); 513 error = 514 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 515 (int)fs->fs_bsize, NOCRED, &bp); 516 if (error) { 517 VOP_UNLOCK(vp, 0, td); 518 vrele(vp); 519 return (error); 520 } 521 ffs_load_inode(bp, ip, fs, ip->i_number); 522 ip->i_effnlink = ip->i_nlink; 523 brelse(bp); 524 VOP_UNLOCK(vp, 0, td); 525 vrele(vp); 526 MNT_ILOCK(mp); 527 } 528 MNT_IUNLOCK(mp); 529 return (0); 530 } 531 532 /* 533 * Possible superblock locations ordered from most to least likely. 534 */ 535 static int sblock_try[] = SBLOCKSEARCH; 536 537 /* 538 * Common code for mount and mountroot 539 */ 540 static int 541 ffs_mountfs(devvp, mp, td) 542 struct vnode *devvp; 543 struct mount *mp; 544 struct thread *td; 545 { 546 struct ufsmount *ump; 547 struct buf *bp; 548 struct fs *fs; 549 struct cdev *dev; 550 void *space; 551 ufs2_daddr_t sblockloc; 552 int error, i, blks, size, ronly; 553 int32_t *lp; 554 struct ucred *cred; 555 size_t strsize; 556 557 dev = devvp->v_rdev; 558 cred = td ? td->td_ucred : NOCRED; 559 /* 560 * Disallow multiple mounts of the same device. 561 * Disallow mounting of a device that is currently in use 562 * (except for root, which might share swap device for miniroot). 563 * Flush out any old buffers remaining from a previous use. 564 */ 565 error = vfs_mountedon(devvp); 566 if (error) 567 return (error); 568 if (vcount(devvp) > 1 && devvp != rootvp) 569 return (EBUSY); 570 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 571 error = vinvalbuf(devvp, V_SAVE, cred, td, 0, 0); 572 if (error) { 573 VOP_UNLOCK(devvp, 0, td); 574 return (error); 575 } 576 577 /* 578 * Only VMIO the backing device if the backing device is a real 579 * disk device. 580 * Note that it is optional that the backing device be VMIOed. This 581 * increases the opportunity for metadata caching. 582 */ 583 if (vn_isdisk(devvp, NULL)) 584 vfs_object_create(devvp, td, cred); 585 586 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 587 /* 588 * XXX: open the device with read and write access even if only 589 * read access is needed now. Write access is needed if the 590 * filesystem is ever mounted read/write, and we don't change the 591 * access mode for remounts. 592 */ 593 #ifdef notyet 594 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE, FSCRED, td, -1); 595 #else 596 error = VOP_OPEN(devvp, FREAD | FWRITE, FSCRED, td, -1); 597 #endif 598 VOP_UNLOCK(devvp, 0, td); 599 if (error) 600 return (error); 601 if (devvp->v_rdev->si_iosize_max != 0) 602 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max; 603 if (mp->mnt_iosize_max > MAXPHYS) 604 mp->mnt_iosize_max = MAXPHYS; 605 606 bp = NULL; 607 ump = NULL; 608 fs = NULL; 609 sblockloc = 0; 610 /* 611 * Try reading the superblock in each of its possible locations. 612 */ 613 for (i = 0; sblock_try[i] != -1; i++) { 614 if ((error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, 615 cred, &bp)) != 0) 616 goto out; 617 fs = (struct fs *)bp->b_data; 618 sblockloc = sblock_try[i]; 619 if ((fs->fs_magic == FS_UFS1_MAGIC || 620 (fs->fs_magic == FS_UFS2_MAGIC && 621 (fs->fs_sblockloc == sblockloc || 622 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) && 623 fs->fs_bsize <= MAXBSIZE && 624 fs->fs_bsize >= sizeof(struct fs)) 625 break; 626 brelse(bp); 627 bp = NULL; 628 } 629 if (sblock_try[i] == -1) { 630 error = EINVAL; /* XXX needs translation */ 631 goto out; 632 } 633 fs->fs_fmod = 0; 634 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */ 635 fs->fs_flags &= ~FS_UNCLEAN; 636 if (fs->fs_clean == 0) { 637 fs->fs_flags |= FS_UNCLEAN; 638 if (ronly || (mp->mnt_flag & MNT_FORCE) || 639 ((fs->fs_flags & FS_NEEDSFSCK) == 0 && 640 (fs->fs_flags & FS_DOSOFTDEP))) { 641 printf( 642 "WARNING: %s was not properly dismounted\n", 643 fs->fs_fsmnt); 644 } else { 645 printf( 646 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 647 fs->fs_fsmnt); 648 error = EPERM; 649 goto out; 650 } 651 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) && 652 (mp->mnt_flag & MNT_FORCE)) { 653 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt, 654 (intmax_t)fs->fs_pendingblocks, 655 fs->fs_pendinginodes); 656 fs->fs_pendingblocks = 0; 657 fs->fs_pendinginodes = 0; 658 } 659 } 660 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 661 printf("%s: mount pending error: blocks %jd files %d\n", 662 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 663 fs->fs_pendinginodes); 664 fs->fs_pendingblocks = 0; 665 fs->fs_pendinginodes = 0; 666 } 667 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO); 668 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, 669 M_WAITOK); 670 if (fs->fs_magic == FS_UFS1_MAGIC) { 671 ump->um_fstype = UFS1; 672 ump->um_balloc = ffs_balloc_ufs1; 673 } else { 674 ump->um_fstype = UFS2; 675 ump->um_balloc = ffs_balloc_ufs2; 676 } 677 ump->um_blkatoff = ffs_blkatoff; 678 ump->um_truncate = ffs_truncate; 679 ump->um_update = ffs_update; 680 ump->um_valloc = ffs_valloc; 681 ump->um_vfree = ffs_vfree; 682 ump->um_ifree = ffs_ifree; 683 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize); 684 if (fs->fs_sbsize < SBLOCKSIZE) 685 bp->b_flags |= B_INVAL | B_NOCACHE; 686 brelse(bp); 687 bp = NULL; 688 fs = ump->um_fs; 689 ffs_oldfscompat_read(fs, ump, sblockloc); 690 fs->fs_ronly = ronly; 691 size = fs->fs_cssize; 692 blks = howmany(size, fs->fs_fsize); 693 if (fs->fs_contigsumsize > 0) 694 size += fs->fs_ncg * sizeof(int32_t); 695 size += fs->fs_ncg * sizeof(u_int8_t); 696 space = malloc((u_long)size, M_UFSMNT, M_WAITOK); 697 fs->fs_csp = space; 698 for (i = 0; i < blks; i += fs->fs_frag) { 699 size = fs->fs_bsize; 700 if (i + fs->fs_frag > blks) 701 size = (blks - i) * fs->fs_fsize; 702 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 703 cred, &bp)) != 0) { 704 free(fs->fs_csp, M_UFSMNT); 705 goto out; 706 } 707 bcopy(bp->b_data, space, (u_int)size); 708 space = (char *)space + size; 709 brelse(bp); 710 bp = NULL; 711 } 712 if (fs->fs_contigsumsize > 0) { 713 fs->fs_maxcluster = lp = space; 714 for (i = 0; i < fs->fs_ncg; i++) 715 *lp++ = fs->fs_contigsumsize; 716 space = lp; 717 } 718 size = fs->fs_ncg * sizeof(u_int8_t); 719 fs->fs_contigdirs = (u_int8_t *)space; 720 bzero(fs->fs_contigdirs, size); 721 fs->fs_active = NULL; 722 mp->mnt_data = (qaddr_t)ump; 723 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0]; 724 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1]; 725 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 726 vfs_getvfs(&mp->mnt_stat.f_fsid)) 727 vfs_getnewfsid(mp); 728 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 729 mp->mnt_flag |= MNT_LOCAL; 730 if ((fs->fs_flags & FS_MULTILABEL) != 0) 731 #ifdef MAC 732 mp->mnt_flag |= MNT_MULTILABEL; 733 #else 734 printf( 735 "WARNING: %s: multilabel flag on fs but no MAC support\n", 736 fs->fs_fsmnt); 737 #endif 738 if ((fs->fs_flags & FS_ACLS) != 0) 739 #ifdef UFS_ACL 740 mp->mnt_flag |= MNT_ACLS; 741 #else 742 printf( 743 "WARNING: %s: ACLs flag on fs but no ACLs support\n", 744 fs->fs_fsmnt); 745 #endif 746 ump->um_mountp = mp; 747 ump->um_dev = dev; 748 ump->um_devvp = devvp; 749 ump->um_nindir = fs->fs_nindir; 750 ump->um_bptrtodb = fs->fs_fsbtodb; 751 ump->um_seqinc = fs->fs_frag; 752 for (i = 0; i < MAXQUOTAS; i++) 753 ump->um_quotas[i] = NULLVP; 754 #ifdef UFS_EXTATTR 755 ufs_extattr_uepm_init(&ump->um_extattr); 756 #endif 757 devvp->v_rdev->si_mountpoint = mp; 758 759 /* 760 * Set FS local "last mounted on" information (NULL pad) 761 */ 762 copystr( mp->mnt_stat.f_mntonname, /* mount point*/ 763 fs->fs_fsmnt, /* copy area*/ 764 sizeof(fs->fs_fsmnt) - 1, /* max size*/ 765 &strsize); /* real size*/ 766 bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize); 767 768 if( mp->mnt_flag & MNT_ROOTFS) { 769 /* 770 * Root mount; update timestamp in mount structure. 771 * this will be used by the common root mount code 772 * to update the system clock. 773 */ 774 mp->mnt_time = fs->fs_time; 775 } 776 777 if (ronly == 0) { 778 if ((fs->fs_flags & FS_DOSOFTDEP) && 779 (error = softdep_mount(devvp, mp, fs, cred)) != 0) { 780 free(fs->fs_csp, M_UFSMNT); 781 goto out; 782 } 783 if (fs->fs_snapinum[0] != 0) 784 ffs_snapshot_mount(mp); 785 fs->fs_fmod = 1; 786 fs->fs_clean = 0; 787 (void) ffs_sbupdate(ump, MNT_WAIT); 788 } 789 /* 790 * Initialize filesystem stat information in mount struct. 791 */ 792 (void)VFS_STATFS(mp, &mp->mnt_stat, td); 793 #ifdef UFS_EXTATTR 794 #ifdef UFS_EXTATTR_AUTOSTART 795 /* 796 * 797 * Auto-starting does the following: 798 * - check for /.attribute in the fs, and extattr_start if so 799 * - for each file in .attribute, enable that file with 800 * an attribute of the same name. 801 * Not clear how to report errors -- probably eat them. 802 * This would all happen while the filesystem was busy/not 803 * available, so would effectively be "atomic". 804 */ 805 (void) ufs_extattr_autostart(mp, td); 806 #endif /* !UFS_EXTATTR_AUTOSTART */ 807 #endif /* !UFS_EXTATTR */ 808 return (0); 809 out: 810 devvp->v_rdev->si_mountpoint = NULL; 811 if (bp) 812 brelse(bp); 813 /* XXX: see comment above VOP_OPEN. */ 814 #ifdef notyet 815 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, cred, td); 816 #else 817 (void)VOP_CLOSE(devvp, FREAD | FWRITE, cred, td); 818 #endif 819 if (ump) { 820 free(ump->um_fs, M_UFSMNT); 821 free(ump, M_UFSMNT); 822 mp->mnt_data = (qaddr_t)0; 823 } 824 return (error); 825 } 826 827 #include <sys/sysctl.h> 828 int bigcgs = 0; 829 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, ""); 830 831 /* 832 * Sanity checks for loading old filesystem superblocks. 833 * See ffs_oldfscompat_write below for unwound actions. 834 * 835 * XXX - Parts get retired eventually. 836 * Unfortunately new bits get added. 837 */ 838 static void 839 ffs_oldfscompat_read(fs, ump, sblockloc) 840 struct fs *fs; 841 struct ufsmount *ump; 842 ufs2_daddr_t sblockloc; 843 { 844 off_t maxfilesize; 845 846 /* 847 * If not yet done, update fs_flags location and value of fs_sblockloc. 848 */ 849 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) { 850 fs->fs_flags = fs->fs_old_flags; 851 fs->fs_old_flags |= FS_FLAGS_UPDATED; 852 fs->fs_sblockloc = sblockloc; 853 } 854 /* 855 * If not yet done, update UFS1 superblock with new wider fields. 856 */ 857 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) { 858 fs->fs_maxbsize = fs->fs_bsize; 859 fs->fs_time = fs->fs_old_time; 860 fs->fs_size = fs->fs_old_size; 861 fs->fs_dsize = fs->fs_old_dsize; 862 fs->fs_csaddr = fs->fs_old_csaddr; 863 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir; 864 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree; 865 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree; 866 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree; 867 } 868 if (fs->fs_magic == FS_UFS1_MAGIC && 869 fs->fs_old_inodefmt < FS_44INODEFMT) { 870 fs->fs_maxfilesize = (u_quad_t) 1LL << 39; 871 fs->fs_qbmask = ~fs->fs_bmask; 872 fs->fs_qfmask = ~fs->fs_fmask; 873 } 874 if (fs->fs_magic == FS_UFS1_MAGIC) { 875 ump->um_savedmaxfilesize = fs->fs_maxfilesize; 876 maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1; 877 if (fs->fs_maxfilesize > maxfilesize) 878 fs->fs_maxfilesize = maxfilesize; 879 } 880 /* Compatibility for old filesystems */ 881 if (fs->fs_avgfilesize <= 0) 882 fs->fs_avgfilesize = AVFILESIZ; 883 if (fs->fs_avgfpdir <= 0) 884 fs->fs_avgfpdir = AFPDIR; 885 if (bigcgs) { 886 fs->fs_save_cgsize = fs->fs_cgsize; 887 fs->fs_cgsize = fs->fs_bsize; 888 } 889 } 890 891 /* 892 * Unwinding superblock updates for old filesystems. 893 * See ffs_oldfscompat_read above for details. 894 * 895 * XXX - Parts get retired eventually. 896 * Unfortunately new bits get added. 897 */ 898 static void 899 ffs_oldfscompat_write(fs, ump) 900 struct fs *fs; 901 struct ufsmount *ump; 902 { 903 904 /* 905 * Copy back UFS2 updated fields that UFS1 inspects. 906 */ 907 if (fs->fs_magic == FS_UFS1_MAGIC) { 908 fs->fs_old_time = fs->fs_time; 909 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir; 910 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree; 911 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree; 912 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree; 913 fs->fs_maxfilesize = ump->um_savedmaxfilesize; 914 } 915 if (bigcgs) { 916 fs->fs_cgsize = fs->fs_save_cgsize; 917 fs->fs_save_cgsize = 0; 918 } 919 } 920 921 /* 922 * unmount system call 923 */ 924 int 925 ffs_unmount(mp, mntflags, td) 926 struct mount *mp; 927 int mntflags; 928 struct thread *td; 929 { 930 struct ufsmount *ump = VFSTOUFS(mp); 931 struct fs *fs; 932 int error, flags; 933 934 flags = 0; 935 if (mntflags & MNT_FORCE) { 936 flags |= FORCECLOSE; 937 } 938 #ifdef UFS_EXTATTR 939 if ((error = ufs_extattr_stop(mp, td))) { 940 if (error != EOPNOTSUPP) 941 printf("ffs_unmount: ufs_extattr_stop returned %d\n", 942 error); 943 } else { 944 ufs_extattr_uepm_destroy(&ump->um_extattr); 945 } 946 #endif 947 if (mp->mnt_flag & MNT_SOFTDEP) { 948 if ((error = softdep_flushfiles(mp, flags, td)) != 0) 949 return (error); 950 } else { 951 if ((error = ffs_flushfiles(mp, flags, td)) != 0) 952 return (error); 953 } 954 fs = ump->um_fs; 955 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 956 printf("%s: unmount pending error: blocks %jd files %d\n", 957 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 958 fs->fs_pendinginodes); 959 fs->fs_pendingblocks = 0; 960 fs->fs_pendinginodes = 0; 961 } 962 if (fs->fs_ronly == 0) { 963 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1; 964 error = ffs_sbupdate(ump, MNT_WAIT); 965 if (error) { 966 fs->fs_clean = 0; 967 return (error); 968 } 969 } 970 ump->um_devvp->v_rdev->si_mountpoint = NULL; 971 972 vinvalbuf(ump->um_devvp, V_SAVE, NOCRED, td, 0, 0); 973 /* XXX: see comment above VOP_OPEN. */ 974 #ifdef notyet 975 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE, 976 NOCRED, td); 977 #else 978 error = VOP_CLOSE(ump->um_devvp, FREAD | FWRITE, NOCRED, td); 979 #endif 980 vrele(ump->um_devvp); 981 free(fs->fs_csp, M_UFSMNT); 982 free(fs, M_UFSMNT); 983 free(ump, M_UFSMNT); 984 mp->mnt_data = (qaddr_t)0; 985 mp->mnt_flag &= ~MNT_LOCAL; 986 return (error); 987 } 988 989 /* 990 * Flush out all the files in a filesystem. 991 */ 992 int 993 ffs_flushfiles(mp, flags, td) 994 struct mount *mp; 995 int flags; 996 struct thread *td; 997 { 998 struct ufsmount *ump; 999 int error; 1000 1001 ump = VFSTOUFS(mp); 1002 #ifdef QUOTA 1003 if (mp->mnt_flag & MNT_QUOTA) { 1004 int i; 1005 error = vflush(mp, 0, SKIPSYSTEM|flags, td); 1006 if (error) 1007 return (error); 1008 for (i = 0; i < MAXQUOTAS; i++) { 1009 if (ump->um_quotas[i] == NULLVP) 1010 continue; 1011 quotaoff(td, mp, i); 1012 } 1013 /* 1014 * Here we fall through to vflush again to ensure 1015 * that we have gotten rid of all the system vnodes. 1016 */ 1017 } 1018 #endif 1019 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles"); 1020 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) { 1021 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0) 1022 return (error); 1023 ffs_snapshot_unmount(mp); 1024 /* 1025 * Here we fall through to vflush again to ensure 1026 * that we have gotten rid of all the system vnodes. 1027 */ 1028 } 1029 /* 1030 * Flush all the files. 1031 */ 1032 if ((error = vflush(mp, 0, flags, td)) != 0) 1033 return (error); 1034 /* 1035 * Flush filesystem metadata. 1036 */ 1037 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td); 1038 error = VOP_FSYNC(ump->um_devvp, td->td_ucred, MNT_WAIT, td); 1039 VOP_UNLOCK(ump->um_devvp, 0, td); 1040 return (error); 1041 } 1042 1043 /* 1044 * Get filesystem statistics. 1045 */ 1046 int 1047 ffs_statfs(mp, sbp, td) 1048 struct mount *mp; 1049 struct statfs *sbp; 1050 struct thread *td; 1051 { 1052 struct ufsmount *ump; 1053 struct fs *fs; 1054 1055 ump = VFSTOUFS(mp); 1056 fs = ump->um_fs; 1057 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC) 1058 panic("ffs_statfs"); 1059 sbp->f_version = STATFS_VERSION; 1060 sbp->f_bsize = fs->fs_fsize; 1061 sbp->f_iosize = fs->fs_bsize; 1062 sbp->f_blocks = fs->fs_dsize; 1063 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 1064 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks); 1065 sbp->f_bavail = freespace(fs, fs->fs_minfree) + 1066 dbtofsb(fs, fs->fs_pendingblocks); 1067 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 1068 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes; 1069 sbp->f_namemax = NAME_MAX; 1070 if (sbp != &mp->mnt_stat) { 1071 sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 1072 sbp->f_type = mp->mnt_vfc->vfc_typenum; 1073 sbp->f_syncwrites = mp->mnt_stat.f_syncwrites; 1074 sbp->f_asyncwrites = mp->mnt_stat.f_asyncwrites; 1075 sbp->f_syncreads = mp->mnt_stat.f_syncreads; 1076 sbp->f_asyncreads = mp->mnt_stat.f_asyncreads; 1077 sbp->f_owner = mp->mnt_stat.f_owner; 1078 sbp->f_fsid = mp->mnt_stat.f_fsid; 1079 bcopy((caddr_t)mp->mnt_stat.f_fstypename, 1080 (caddr_t)&sbp->f_fstypename[0], MFSNAMELEN); 1081 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 1082 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 1083 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 1084 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 1085 } 1086 return (0); 1087 } 1088 1089 /* 1090 * Go through the disk queues to initiate sandbagged IO; 1091 * go through the inodes to write those that have been modified; 1092 * initiate the writing of the super block if it has been modified. 1093 * 1094 * Note: we are always called with the filesystem marked `MPBUSY'. 1095 */ 1096 int 1097 ffs_sync(mp, waitfor, cred, td) 1098 struct mount *mp; 1099 int waitfor; 1100 struct ucred *cred; 1101 struct thread *td; 1102 { 1103 struct vnode *nvp, *vp, *devvp; 1104 struct inode *ip; 1105 struct ufsmount *ump = VFSTOUFS(mp); 1106 struct fs *fs; 1107 int error, count, wait, lockreq, allerror = 0; 1108 1109 fs = ump->um_fs; 1110 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */ 1111 printf("fs = %s\n", fs->fs_fsmnt); 1112 panic("ffs_sync: rofs mod"); 1113 } 1114 /* 1115 * Write back each (modified) inode. 1116 */ 1117 wait = 0; 1118 lockreq = LK_EXCLUSIVE | LK_NOWAIT; 1119 if (waitfor == MNT_WAIT) { 1120 wait = 1; 1121 lockreq = LK_EXCLUSIVE; 1122 } 1123 lockreq |= LK_INTERLOCK; 1124 MNT_ILOCK(mp); 1125 loop: 1126 MNT_VNODE_FOREACH(vp, mp, nvp) { 1127 /* 1128 * Depend on the mntvnode_slock to keep things stable enough 1129 * for a quick test. Since there might be hundreds of 1130 * thousands of vnodes, we cannot afford even a subroutine 1131 * call unless there's a good chance that we have work to do. 1132 */ 1133 VI_LOCK(vp); 1134 if (vp->v_iflag & VI_XLOCK) { 1135 VI_UNLOCK(vp); 1136 continue; 1137 } 1138 ip = VTOI(vp); 1139 if (vp->v_type == VNON || ((ip->i_flag & 1140 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 1141 TAILQ_EMPTY(&vp->v_dirtyblkhd))) { 1142 VI_UNLOCK(vp); 1143 continue; 1144 } 1145 MNT_IUNLOCK(mp); 1146 if ((error = vget(vp, lockreq, td)) != 0) { 1147 MNT_ILOCK(mp); 1148 if (error == ENOENT) 1149 goto loop; 1150 continue; 1151 } 1152 if ((error = VOP_FSYNC(vp, cred, waitfor, td)) != 0) 1153 allerror = error; 1154 VOP_UNLOCK(vp, 0, td); 1155 vrele(vp); 1156 MNT_ILOCK(mp); 1157 } 1158 MNT_IUNLOCK(mp); 1159 /* 1160 * Force stale filesystem control information to be flushed. 1161 */ 1162 if (waitfor == MNT_WAIT) { 1163 if ((error = softdep_flushworklist(ump->um_mountp, &count, td))) 1164 allerror = error; 1165 /* Flushed work items may create new vnodes to clean */ 1166 if (allerror == 0 && count) { 1167 MNT_ILOCK(mp); 1168 goto loop; 1169 } 1170 } 1171 #ifdef QUOTA 1172 qsync(mp); 1173 #endif 1174 devvp = ump->um_devvp; 1175 VI_LOCK(devvp); 1176 if (waitfor != MNT_LAZY && 1177 (devvp->v_numoutput > 0 || TAILQ_FIRST(&devvp->v_dirtyblkhd))) { 1178 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td); 1179 if ((error = VOP_FSYNC(devvp, cred, waitfor, td)) != 0) 1180 allerror = error; 1181 VOP_UNLOCK(devvp, 0, td); 1182 if (allerror == 0 && waitfor == MNT_WAIT) { 1183 MNT_ILOCK(mp); 1184 goto loop; 1185 } 1186 } else 1187 VI_UNLOCK(devvp); 1188 /* 1189 * Write back modified superblock. 1190 */ 1191 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0) 1192 allerror = error; 1193 return (allerror); 1194 } 1195 1196 int 1197 ffs_vget(mp, ino, flags, vpp) 1198 struct mount *mp; 1199 ino_t ino; 1200 int flags; 1201 struct vnode **vpp; 1202 { 1203 struct thread *td = curthread; /* XXX */ 1204 struct fs *fs; 1205 struct inode *ip; 1206 struct ufsmount *ump; 1207 struct buf *bp; 1208 struct vnode *vp; 1209 struct cdev *dev; 1210 int error; 1211 1212 ump = VFSTOUFS(mp); 1213 dev = ump->um_dev; 1214 1215 /* 1216 * We do not lock vnode creation as it is believed to be too 1217 * expensive for such rare case as simultaneous creation of vnode 1218 * for same ino by different processes. We just allow them to race 1219 * and check later to decide who wins. Let the race begin! 1220 */ 1221 if ((error = ufs_ihashget(dev, ino, flags, vpp)) != 0) 1222 return (error); 1223 if (*vpp != NULL) 1224 return (0); 1225 1226 /* 1227 * If this MALLOC() is performed after the getnewvnode() 1228 * it might block, leaving a vnode with a NULL v_data to be 1229 * found by ffs_sync() if a sync happens to fire right then, 1230 * which will cause a panic because ffs_sync() blindly 1231 * dereferences vp->v_data (as well it should). 1232 */ 1233 ip = uma_zalloc(uma_inode, M_WAITOK); 1234 1235 /* Allocate a new vnode/inode. */ 1236 error = getnewvnode("ufs", mp, ffs_vnodeop_p, &vp); 1237 if (error) { 1238 *vpp = NULL; 1239 uma_zfree(uma_inode, ip); 1240 return (error); 1241 } 1242 bzero((caddr_t)ip, sizeof(struct inode)); 1243 /* 1244 * FFS supports recursive locking. 1245 */ 1246 fs = ump->um_fs; 1247 vp->v_vnlock->lk_flags |= LK_CANRECURSE; 1248 vp->v_data = ip; 1249 vp->v_bsize = fs->fs_bsize; 1250 ip->i_vnode = vp; 1251 ip->i_ump = ump; 1252 ip->i_fs = fs; 1253 ip->i_dev = dev; 1254 ip->i_number = ino; 1255 #ifdef QUOTA 1256 { 1257 int i; 1258 for (i = 0; i < MAXQUOTAS; i++) 1259 ip->i_dquot[i] = NODQUOT; 1260 } 1261 #endif 1262 /* 1263 * Exclusively lock the vnode before adding to hash. Note, that we 1264 * must not release nor downgrade the lock (despite flags argument 1265 * says) till it is fully initialized. 1266 */ 1267 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, (struct mtx *)0, td); 1268 1269 /* 1270 * Atomicaly (in terms of ufs_hash operations) check the hash for 1271 * duplicate of vnode being created and add it to the hash. If a 1272 * duplicate vnode was found, it will be vget()ed from hash for us. 1273 */ 1274 if ((error = ufs_ihashins(ip, flags, vpp)) != 0) { 1275 vput(vp); 1276 *vpp = NULL; 1277 return (error); 1278 } 1279 1280 /* We lost the race, then throw away our vnode and return existing */ 1281 if (*vpp != NULL) { 1282 vput(vp); 1283 return (0); 1284 } 1285 1286 /* Read in the disk contents for the inode, copy into the inode. */ 1287 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 1288 (int)fs->fs_bsize, NOCRED, &bp); 1289 if (error) { 1290 /* 1291 * The inode does not contain anything useful, so it would 1292 * be misleading to leave it on its hash chain. With mode 1293 * still zero, it will be unlinked and returned to the free 1294 * list by vput(). 1295 */ 1296 brelse(bp); 1297 vput(vp); 1298 *vpp = NULL; 1299 return (error); 1300 } 1301 if (ip->i_ump->um_fstype == UFS1) 1302 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK); 1303 else 1304 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK); 1305 ffs_load_inode(bp, ip, fs, ino); 1306 if (DOINGSOFTDEP(vp)) 1307 softdep_load_inodeblock(ip); 1308 else 1309 ip->i_effnlink = ip->i_nlink; 1310 bqrelse(bp); 1311 1312 /* 1313 * Initialize the vnode from the inode, check for aliases. 1314 * Note that the underlying vnode may have changed. 1315 */ 1316 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp); 1317 if (error) { 1318 vput(vp); 1319 *vpp = NULL; 1320 return (error); 1321 } 1322 /* 1323 * Finish inode initialization. 1324 */ 1325 VREF(ip->i_devvp); 1326 /* 1327 * Set up a generation number for this inode if it does not 1328 * already have one. This should only happen on old filesystems. 1329 */ 1330 if (ip->i_gen == 0) { 1331 ip->i_gen = arc4random() / 2 + 1; 1332 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 1333 ip->i_flag |= IN_MODIFIED; 1334 DIP(ip, i_gen) = ip->i_gen; 1335 } 1336 } 1337 /* 1338 * Ensure that uid and gid are correct. This is a temporary 1339 * fix until fsck has been changed to do the update. 1340 */ 1341 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */ 1342 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */ 1343 ip->i_uid = ip->i_din1->di_ouid; /* XXX */ 1344 ip->i_gid = ip->i_din1->di_ogid; /* XXX */ 1345 } /* XXX */ 1346 1347 #ifdef MAC 1348 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) { 1349 /* 1350 * If this vnode is already allocated, and we're running 1351 * multi-label, attempt to perform a label association 1352 * from the extended attributes on the inode. 1353 */ 1354 error = mac_associate_vnode_extattr(mp, vp); 1355 if (error) { 1356 /* ufs_inactive will release ip->i_devvp ref. */ 1357 vput(vp); 1358 *vpp = NULL; 1359 return (error); 1360 } 1361 } 1362 #endif 1363 1364 *vpp = vp; 1365 return (0); 1366 } 1367 1368 /* 1369 * File handle to vnode 1370 * 1371 * Have to be really careful about stale file handles: 1372 * - check that the inode number is valid 1373 * - call ffs_vget() to get the locked inode 1374 * - check for an unallocated inode (i_mode == 0) 1375 * - check that the given client host has export rights and return 1376 * those rights via. exflagsp and credanonp 1377 */ 1378 int 1379 ffs_fhtovp(mp, fhp, vpp) 1380 struct mount *mp; 1381 struct fid *fhp; 1382 struct vnode **vpp; 1383 { 1384 struct ufid *ufhp; 1385 struct fs *fs; 1386 1387 ufhp = (struct ufid *)fhp; 1388 fs = VFSTOUFS(mp)->um_fs; 1389 if (ufhp->ufid_ino < ROOTINO || 1390 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 1391 return (ESTALE); 1392 return (ufs_fhtovp(mp, ufhp, vpp)); 1393 } 1394 1395 /* 1396 * Vnode pointer to File handle 1397 */ 1398 /* ARGSUSED */ 1399 int 1400 ffs_vptofh(vp, fhp) 1401 struct vnode *vp; 1402 struct fid *fhp; 1403 { 1404 struct inode *ip; 1405 struct ufid *ufhp; 1406 1407 ip = VTOI(vp); 1408 ufhp = (struct ufid *)fhp; 1409 ufhp->ufid_len = sizeof(struct ufid); 1410 ufhp->ufid_ino = ip->i_number; 1411 ufhp->ufid_gen = ip->i_gen; 1412 return (0); 1413 } 1414 1415 /* 1416 * Initialize the filesystem. 1417 */ 1418 static int 1419 ffs_init(vfsp) 1420 struct vfsconf *vfsp; 1421 { 1422 1423 softdep_initialize(); 1424 return (ufs_init(vfsp)); 1425 } 1426 1427 /* 1428 * Undo the work of ffs_init(). 1429 */ 1430 static int 1431 ffs_uninit(vfsp) 1432 struct vfsconf *vfsp; 1433 { 1434 int ret; 1435 1436 ret = ufs_uninit(vfsp); 1437 softdep_uninitialize(); 1438 return (ret); 1439 } 1440 1441 /* 1442 * Write a superblock and associated information back to disk. 1443 */ 1444 static int 1445 ffs_sbupdate(mp, waitfor) 1446 struct ufsmount *mp; 1447 int waitfor; 1448 { 1449 struct fs *fs = mp->um_fs; 1450 struct buf *bp; 1451 int blks; 1452 void *space; 1453 int i, size, error, allerror = 0; 1454 1455 if (fs->fs_ronly == 1 && 1456 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) != 1457 (MNT_RDONLY | MNT_UPDATE)) 1458 panic("ffs_sbupdate: write read-only filesystem"); 1459 /* 1460 * First write back the summary information. 1461 */ 1462 blks = howmany(fs->fs_cssize, fs->fs_fsize); 1463 space = fs->fs_csp; 1464 for (i = 0; i < blks; i += fs->fs_frag) { 1465 size = fs->fs_bsize; 1466 if (i + fs->fs_frag > blks) 1467 size = (blks - i) * fs->fs_fsize; 1468 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), 1469 size, 0, 0, 0); 1470 bcopy(space, bp->b_data, (u_int)size); 1471 space = (char *)space + size; 1472 if (waitfor != MNT_WAIT) 1473 bawrite(bp); 1474 else if ((error = bwrite(bp)) != 0) 1475 allerror = error; 1476 } 1477 /* 1478 * Now write back the superblock itself. If any errors occurred 1479 * up to this point, then fail so that the superblock avoids 1480 * being written out as clean. 1481 */ 1482 if (allerror) 1483 return (allerror); 1484 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 && 1485 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) { 1486 printf("%s: correcting fs_sblockloc from %jd to %d\n", 1487 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1); 1488 fs->fs_sblockloc = SBLOCK_UFS1; 1489 } 1490 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 && 1491 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) { 1492 printf("%s: correcting fs_sblockloc from %jd to %d\n", 1493 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2); 1494 fs->fs_sblockloc = SBLOCK_UFS2; 1495 } 1496 bp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize, 1497 0, 0, 0); 1498 fs->fs_fmod = 0; 1499 fs->fs_time = time_second; 1500 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 1501 ffs_oldfscompat_write((struct fs *)bp->b_data, mp); 1502 if (waitfor != MNT_WAIT) 1503 bawrite(bp); 1504 else if ((error = bwrite(bp)) != 0) 1505 allerror = error; 1506 return (allerror); 1507 } 1508 1509 static int 1510 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 1511 int attrnamespace, const char *attrname, struct thread *td) 1512 { 1513 1514 #ifdef UFS_EXTATTR 1515 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace, 1516 attrname, td)); 1517 #else 1518 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, 1519 attrname, td)); 1520 #endif 1521 } 1522 1523 static void 1524 ffs_ifree(struct ufsmount *ump, struct inode *ip) 1525 { 1526 1527 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL) 1528 uma_zfree(uma_ufs1, ip->i_din1); 1529 else if (ip->i_din2 != NULL) 1530 uma_zfree(uma_ufs2, ip->i_din2); 1531 uma_zfree(uma_inode, ip); 1532 } 1533