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