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 * $FreeBSD$ 35 */ 36 37 #include "opt_ffs.h" 38 #include "opt_quota.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/namei.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/fcntl.h> 51 #include <sys/disklabel.h> 52 #include <sys/malloc.h> 53 54 #include <ufs/ufs/extattr.h> 55 #include <ufs/ufs/quota.h> 56 #include <ufs/ufs/ufsmount.h> 57 #include <ufs/ufs/inode.h> 58 #include <ufs/ufs/ufs_extern.h> 59 60 #include <ufs/ffs/fs.h> 61 #include <ufs/ffs/ffs_extern.h> 62 63 #include <vm/vm.h> 64 #include <vm/vm_page.h> 65 66 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part"); 67 68 static int ffs_sbupdate __P((struct ufsmount *, int)); 69 static int ffs_reload __P((struct mount *,struct ucred *,struct proc *)); 70 static int ffs_oldfscompat __P((struct fs *)); 71 static int ffs_mount __P((struct mount *, char *, caddr_t, 72 struct nameidata *, struct proc *)); 73 static int ffs_init __P((struct vfsconf *)); 74 75 static struct vfsops ufs_vfsops = { 76 ffs_mount, 77 ufs_start, 78 ffs_unmount, 79 ufs_root, 80 ufs_quotactl, 81 ffs_statfs, 82 ffs_sync, 83 ffs_vget, 84 ffs_fhtovp, 85 ufs_check_export, 86 ffs_vptofh, 87 ffs_init, 88 vfs_stduninit, 89 #ifdef FFS_EXTATTR 90 ufs_extattrctl, 91 #else 92 vfs_stdextattrctl, 93 #endif 94 }; 95 96 VFS_SET(ufs_vfsops, ufs, 0); 97 98 /* 99 * ffs_mount 100 * 101 * Called when mounting local physical media 102 * 103 * PARAMETERS: 104 * mountroot 105 * mp mount point structure 106 * path NULL (flag for root mount!!!) 107 * data <unused> 108 * ndp <unused> 109 * p process (user credentials check [statfs]) 110 * 111 * mount 112 * mp mount point structure 113 * path path to mount point 114 * data pointer to argument struct in user space 115 * ndp mount point namei() return (used for 116 * credentials on reload), reused to look 117 * up block device. 118 * p process (user credentials check) 119 * 120 * RETURNS: 0 Success 121 * !0 error number (errno.h) 122 * 123 * LOCK STATE: 124 * 125 * ENTRY 126 * mount point is locked 127 * EXIT 128 * mount point is locked 129 * 130 * NOTES: 131 * A NULL path can be used for a flag since the mount 132 * system call will fail with EFAULT in copyinstr in 133 * namei() if it is a genuine NULL from the user. 134 */ 135 static int 136 ffs_mount(mp, path, data, ndp, p) 137 struct mount *mp; /* mount struct pointer*/ 138 char *path; /* path to mount point*/ 139 caddr_t data; /* arguments to FS specific mount*/ 140 struct nameidata *ndp; /* mount point credentials*/ 141 struct proc *p; /* process requesting mount*/ 142 { 143 size_t size; 144 struct vnode *devvp; 145 struct ufs_args args; 146 struct ufsmount *ump = 0; 147 register struct fs *fs; 148 int error, flags; 149 mode_t accessmode; 150 151 /* 152 * Use NULL path to indicate we are mounting the root file system. 153 */ 154 if (path == NULL) { 155 if ((error = bdevvp(rootdev, &rootvp))) { 156 printf("ffs_mountroot: can't find rootvp\n"); 157 return (error); 158 } 159 160 if ((error = ffs_mountfs(rootvp, mp, p, M_FFSNODE)) != 0) 161 return (error); 162 163 (void)VFS_STATFS(mp, &mp->mnt_stat, p); 164 return (0); 165 } 166 167 /* 168 * Mounting non-root file system or updating a file system 169 */ 170 if ((error = copyin(data, (caddr_t)&args, sizeof(struct ufs_args)))!= 0) 171 return (error); 172 173 /* 174 * If updating, check whether changing from read-only to 175 * read/write; if there is no device name, that's all we do. 176 */ 177 if (mp->mnt_flag & MNT_UPDATE) { 178 ump = VFSTOUFS(mp); 179 fs = ump->um_fs; 180 devvp = ump->um_devvp; 181 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 182 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0) 183 return (error); 184 flags = WRITECLOSE; 185 if (mp->mnt_flag & MNT_FORCE) 186 flags |= FORCECLOSE; 187 if (mp->mnt_flag & MNT_SOFTDEP) { 188 error = softdep_flushfiles(mp, flags, p); 189 } else { 190 error = ffs_flushfiles(mp, flags, p); 191 } 192 if (error) { 193 vn_finished_write(mp); 194 return (error); 195 } 196 fs->fs_ronly = 1; 197 if ((fs->fs_flags & FS_UNCLEAN) == 0) 198 fs->fs_clean = 1; 199 if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) { 200 fs->fs_ronly = 0; 201 fs->fs_clean = 0; 202 vn_finished_write(mp); 203 return (error); 204 } 205 vn_finished_write(mp); 206 } 207 if ((mp->mnt_flag & MNT_RELOAD) && 208 (error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p)) != 0) 209 return (error); 210 if (fs->fs_ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 211 /* 212 * If upgrade to read-write by non-root, then verify 213 * that user has necessary permissions on the device. 214 */ 215 if (p->p_ucred->cr_uid != 0) { 216 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 217 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE, 218 p->p_ucred, p)) != 0) { 219 VOP_UNLOCK(devvp, 0, p); 220 return (error); 221 } 222 VOP_UNLOCK(devvp, 0, p); 223 } 224 fs->fs_flags &= ~FS_UNCLEAN; 225 if (fs->fs_clean == 0) { 226 fs->fs_flags |= FS_UNCLEAN; 227 if (mp->mnt_flag & MNT_FORCE) { 228 printf("WARNING: %s was not %s\n", 229 fs->fs_fsmnt, "properly dismounted"); 230 } else { 231 printf( 232 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 233 fs->fs_fsmnt); 234 return (EPERM); 235 } 236 } 237 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0) 238 return (error); 239 fs->fs_ronly = 0; 240 fs->fs_clean = 0; 241 if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) { 242 vn_finished_write(mp); 243 return (error); 244 } 245 /* check to see if we need to start softdep */ 246 if ((fs->fs_flags & FS_DOSOFTDEP) && 247 (error = softdep_mount(devvp, mp, fs, p->p_ucred))){ 248 vn_finished_write(mp); 249 return (error); 250 } 251 if (fs->fs_snapinum[0] != 0) 252 ffs_snapshot_mount(mp); 253 vn_finished_write(mp); 254 } 255 /* 256 * Soft updates is incompatible with "async", 257 * so if we are doing softupdates stop the user 258 * from setting the async flag in an update. 259 * Softdep_mount() clears it in an initial mount 260 * or ro->rw remount. 261 */ 262 if (mp->mnt_flag & MNT_SOFTDEP) 263 mp->mnt_flag &= ~MNT_ASYNC; 264 /* 265 * If not updating name, process export requests. 266 */ 267 if (args.fspec == 0) 268 return (vfs_export(mp, &ump->um_export, &args.export)); 269 /* 270 * If this is a snapshot request, take the snapshot. 271 */ 272 if (mp->mnt_flag & MNT_SNAPSHOT) 273 return (ffs_snapshot(mp, args.fspec)); 274 } 275 276 /* 277 * Not an update, or updating the name: look up the name 278 * and verify that it refers to a sensible block device. 279 */ 280 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 281 if ((error = namei(ndp)) != 0) 282 return (error); 283 NDFREE(ndp, NDF_ONLY_PNBUF); 284 devvp = ndp->ni_vp; 285 if (!vn_isdisk(devvp, &error)) { 286 vrele(devvp); 287 return (error); 288 } 289 290 /* 291 * If mount by non-root, then verify that user has necessary 292 * permissions on the device. 293 */ 294 if (p->p_ucred->cr_uid != 0) { 295 accessmode = VREAD; 296 if ((mp->mnt_flag & MNT_RDONLY) == 0) 297 accessmode |= VWRITE; 298 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 299 if ((error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p))!= 0){ 300 vput(devvp); 301 return (error); 302 } 303 VOP_UNLOCK(devvp, 0, p); 304 } 305 306 if (mp->mnt_flag & MNT_UPDATE) { 307 /* 308 * Update only 309 * 310 * If it's not the same vnode, or at least the same device 311 * then it's not correct. 312 */ 313 314 if (devvp != ump->um_devvp && 315 devvp->v_rdev != ump->um_devvp->v_rdev) 316 error = EINVAL; /* needs translation */ 317 vrele(devvp); 318 if (error) 319 return (error); 320 } else { 321 /* 322 * New mount 323 * 324 * We need the name for the mount point (also used for 325 * "last mounted on") copied in. If an error occurs, 326 * the mount point is discarded by the upper level code. 327 */ 328 copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 329 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 330 if ((error = ffs_mountfs(devvp, mp, p, M_FFSNODE)) != 0) { 331 vrele(devvp); 332 return (error); 333 } 334 } 335 /* 336 * Save "mounted from" device name info for mount point (NULL pad). 337 */ 338 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); 339 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 340 /* 341 * Initialize filesystem stat information in mount struct. 342 */ 343 (void)VFS_STATFS(mp, &mp->mnt_stat, p); 344 return (0); 345 } 346 347 /* 348 * Reload all incore data for a filesystem (used after running fsck on 349 * the root filesystem and finding things to fix). The filesystem must 350 * be mounted read-only. 351 * 352 * Things to do to update the mount: 353 * 1) invalidate all cached meta-data. 354 * 2) re-read superblock from disk. 355 * 3) re-read summary information from disk. 356 * 4) invalidate all inactive vnodes. 357 * 5) invalidate all cached file data. 358 * 6) re-read inode data for all active vnodes. 359 */ 360 static int 361 ffs_reload(mp, cred, p) 362 register struct mount *mp; 363 struct ucred *cred; 364 struct proc *p; 365 { 366 register struct vnode *vp, *nvp, *devvp; 367 struct inode *ip; 368 struct csum *space; 369 struct buf *bp; 370 struct fs *fs, *newfs; 371 struct partinfo dpart; 372 dev_t dev; 373 int i, blks, size, error; 374 int32_t *lp; 375 376 if ((mp->mnt_flag & MNT_RDONLY) == 0) 377 return (EINVAL); 378 /* 379 * Step 1: invalidate all cached meta-data. 380 */ 381 devvp = VFSTOUFS(mp)->um_devvp; 382 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 383 error = vinvalbuf(devvp, 0, cred, p, 0, 0); 384 VOP_UNLOCK(devvp, 0, p); 385 if (error) 386 panic("ffs_reload: dirty1"); 387 388 dev = devvp->v_rdev; 389 390 /* 391 * Only VMIO the backing device if the backing device is a real 392 * block device. See ffs_mountmfs() for more details. 393 */ 394 if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) { 395 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 396 vfs_object_create(devvp, p, p->p_ucred); 397 simple_lock(&devvp->v_interlock); 398 VOP_UNLOCK(devvp, LK_INTERLOCK, p); 399 } 400 401 /* 402 * Step 2: re-read superblock from disk. 403 */ 404 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 405 size = DEV_BSIZE; 406 else 407 size = dpart.disklab->d_secsize; 408 if ((error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, NOCRED,&bp)) != 0) 409 return (error); 410 newfs = (struct fs *)bp->b_data; 411 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE || 412 newfs->fs_bsize < sizeof(struct fs)) { 413 brelse(bp); 414 return (EIO); /* XXX needs translation */ 415 } 416 fs = VFSTOUFS(mp)->um_fs; 417 /* 418 * Copy pointer fields back into superblock before copying in XXX 419 * new superblock. These should really be in the ufsmount. XXX 420 * Note that important parameters (eg fs_ncg) are unchanged. 421 */ 422 bcopy(&fs->fs_csp[0], &newfs->fs_csp[0], sizeof(fs->fs_csp)); 423 newfs->fs_maxcluster = fs->fs_maxcluster; 424 bcopy(newfs, fs, (u_int)fs->fs_sbsize); 425 if (fs->fs_sbsize < SBSIZE) 426 bp->b_flags |= B_INVAL | B_NOCACHE; 427 brelse(bp); 428 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 429 ffs_oldfscompat(fs); 430 431 /* 432 * Step 3: re-read summary information from disk. 433 */ 434 blks = howmany(fs->fs_cssize, fs->fs_fsize); 435 space = fs->fs_csp[0]; 436 for (i = 0; i < blks; i += fs->fs_frag) { 437 size = fs->fs_bsize; 438 if (i + fs->fs_frag > blks) 439 size = (blks - i) * fs->fs_fsize; 440 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 441 NOCRED, &bp); 442 if (error) 443 return (error); 444 bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size); 445 brelse(bp); 446 } 447 /* 448 * We no longer know anything about clusters per cylinder group. 449 */ 450 if (fs->fs_contigsumsize > 0) { 451 lp = fs->fs_maxcluster; 452 for (i = 0; i < fs->fs_ncg; i++) 453 *lp++ = fs->fs_contigsumsize; 454 } 455 456 loop: 457 simple_lock(&mntvnode_slock); 458 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 459 if (vp->v_mount != mp) { 460 simple_unlock(&mntvnode_slock); 461 goto loop; 462 } 463 nvp = vp->v_mntvnodes.le_next; 464 /* 465 * Step 4: invalidate all inactive vnodes. 466 */ 467 if (vrecycle(vp, &mntvnode_slock, p)) 468 goto loop; 469 /* 470 * Step 5: invalidate all cached file data. 471 */ 472 simple_lock(&vp->v_interlock); 473 simple_unlock(&mntvnode_slock); 474 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) { 475 goto loop; 476 } 477 if (vinvalbuf(vp, 0, cred, p, 0, 0)) 478 panic("ffs_reload: dirty2"); 479 /* 480 * Step 6: re-read inode data for all active vnodes. 481 */ 482 ip = VTOI(vp); 483 error = 484 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 485 (int)fs->fs_bsize, NOCRED, &bp); 486 if (error) { 487 vput(vp); 488 return (error); 489 } 490 ip->i_din = *((struct dinode *)bp->b_data + 491 ino_to_fsbo(fs, ip->i_number)); 492 ip->i_effnlink = ip->i_nlink; 493 brelse(bp); 494 vput(vp); 495 simple_lock(&mntvnode_slock); 496 } 497 simple_unlock(&mntvnode_slock); 498 return (0); 499 } 500 501 /* 502 * Common code for mount and mountroot 503 */ 504 int 505 ffs_mountfs(devvp, mp, p, malloctype) 506 register struct vnode *devvp; 507 struct mount *mp; 508 struct proc *p; 509 struct malloc_type *malloctype; 510 { 511 register struct ufsmount *ump; 512 struct buf *bp; 513 register struct fs *fs; 514 dev_t dev; 515 struct partinfo dpart; 516 caddr_t base, space; 517 int error, i, blks, size, ronly; 518 int32_t *lp; 519 struct ucred *cred; 520 u_int64_t maxfilesize; /* XXX */ 521 size_t strsize; 522 int ncount; 523 524 dev = devvp->v_rdev; 525 cred = p ? p->p_ucred : NOCRED; 526 /* 527 * Disallow multiple mounts of the same device. 528 * Disallow mounting of a device that is currently in use 529 * (except for root, which might share swap device for miniroot). 530 * Flush out any old buffers remaining from a previous use. 531 */ 532 error = vfs_mountedon(devvp); 533 if (error) 534 return (error); 535 ncount = vcount(devvp); 536 537 if (ncount > 1 && devvp != rootvp) 538 return (EBUSY); 539 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 540 error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0); 541 VOP_UNLOCK(devvp, 0, p); 542 if (error) 543 return (error); 544 545 /* 546 * Only VMIO the backing device if the backing device is a real 547 * block device. This excludes the original MFS implementation. 548 * Note that it is optional that the backing device be VMIOed. This 549 * increases the opportunity for metadata caching. 550 */ 551 if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) { 552 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 553 vfs_object_create(devvp, p, cred); 554 simple_lock(&devvp->v_interlock); 555 VOP_UNLOCK(devvp, LK_INTERLOCK, p); 556 } 557 558 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 559 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 560 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 561 VOP_UNLOCK(devvp, 0, p); 562 if (error) 563 return (error); 564 if (devvp->v_rdev->si_iosize_max > mp->mnt_iosize_max) 565 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max; 566 if (mp->mnt_iosize_max > MAXPHYS) 567 mp->mnt_iosize_max = MAXPHYS; 568 569 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0) 570 size = DEV_BSIZE; 571 else 572 size = dpart.disklab->d_secsize; 573 574 bp = NULL; 575 ump = NULL; 576 if ((error = bread(devvp, SBLOCK, SBSIZE, cred, &bp)) != 0) 577 goto out; 578 fs = (struct fs *)bp->b_data; 579 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE || 580 fs->fs_bsize < sizeof(struct fs)) { 581 error = EINVAL; /* XXX needs translation */ 582 goto out; 583 } 584 fs->fs_fmod = 0; 585 fs->fs_flags &= ~FS_UNCLEAN; 586 if (fs->fs_clean == 0) { 587 fs->fs_flags |= FS_UNCLEAN; 588 if (ronly || (mp->mnt_flag & MNT_FORCE)) { 589 printf( 590 "WARNING: %s was not properly dismounted\n", 591 fs->fs_fsmnt); 592 } else { 593 printf( 594 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 595 fs->fs_fsmnt); 596 error = EPERM; 597 goto out; 598 } 599 } 600 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */ 601 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) { 602 error = EROFS; /* needs translation */ 603 goto out; 604 } 605 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 606 bzero((caddr_t)ump, sizeof *ump); 607 ump->um_malloctype = malloctype; 608 ump->um_i_effnlink_valid = 1; 609 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, 610 M_WAITOK); 611 ump->um_blkatoff = ffs_blkatoff; 612 ump->um_truncate = ffs_truncate; 613 ump->um_update = ffs_update; 614 ump->um_valloc = ffs_valloc; 615 ump->um_vfree = ffs_vfree; 616 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize); 617 if (fs->fs_sbsize < SBSIZE) 618 bp->b_flags |= B_INVAL | B_NOCACHE; 619 brelse(bp); 620 bp = NULL; 621 fs = ump->um_fs; 622 fs->fs_ronly = ronly; 623 size = fs->fs_cssize; 624 blks = howmany(size, fs->fs_fsize); 625 if (fs->fs_contigsumsize > 0) 626 size += fs->fs_ncg * sizeof(int32_t); 627 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK); 628 for (i = 0; i < blks; i += fs->fs_frag) { 629 size = fs->fs_bsize; 630 if (i + fs->fs_frag > blks) 631 size = (blks - i) * fs->fs_fsize; 632 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 633 cred, &bp)) != 0) { 634 free(base, M_UFSMNT); 635 goto out; 636 } 637 bcopy(bp->b_data, space, (u_int)size); 638 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space; 639 space += size; 640 brelse(bp); 641 bp = NULL; 642 } 643 if (fs->fs_contigsumsize > 0) { 644 fs->fs_maxcluster = lp = (int32_t *)space; 645 for (i = 0; i < fs->fs_ncg; i++) 646 *lp++ = fs->fs_contigsumsize; 647 } 648 mp->mnt_data = (qaddr_t)ump; 649 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0]; 650 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1]; 651 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 652 vfs_getvfs(&mp->mnt_stat.f_fsid)) 653 vfs_getnewfsid(mp); 654 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 655 mp->mnt_flag |= MNT_LOCAL; 656 ump->um_mountp = mp; 657 ump->um_dev = dev; 658 ump->um_devvp = devvp; 659 ump->um_nindir = fs->fs_nindir; 660 ump->um_bptrtodb = fs->fs_fsbtodb; 661 ump->um_seqinc = fs->fs_frag; 662 for (i = 0; i < MAXQUOTAS; i++) 663 ump->um_quotas[i] = NULLVP; 664 #ifdef FFS_EXTATTR 665 ufs_extattr_uepm_init(&ump->um_extattr); 666 #endif 667 devvp->v_specmountpoint = mp; 668 ffs_oldfscompat(fs); 669 670 /* 671 * Set FS local "last mounted on" information (NULL pad) 672 */ 673 copystr( mp->mnt_stat.f_mntonname, /* mount point*/ 674 fs->fs_fsmnt, /* copy area*/ 675 sizeof(fs->fs_fsmnt) - 1, /* max size*/ 676 &strsize); /* real size*/ 677 bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize); 678 679 if( mp->mnt_flag & MNT_ROOTFS) { 680 /* 681 * Root mount; update timestamp in mount structure. 682 * this will be used by the common root mount code 683 * to update the system clock. 684 */ 685 mp->mnt_time = fs->fs_time; 686 } 687 688 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */ 689 maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */ 690 if (fs->fs_maxfilesize > maxfilesize) /* XXX */ 691 fs->fs_maxfilesize = maxfilesize; /* XXX */ 692 if (ronly == 0) { 693 if ((fs->fs_flags & FS_DOSOFTDEP) && 694 (error = softdep_mount(devvp, mp, fs, cred)) != 0) { 695 free(base, M_UFSMNT); 696 goto out; 697 } 698 if (fs->fs_snapinum[0] != 0) 699 ffs_snapshot_mount(mp); 700 fs->fs_fmod = 1; 701 fs->fs_clean = 0; 702 (void) ffs_sbupdate(ump, MNT_WAIT); 703 } 704 return (0); 705 out: 706 devvp->v_specmountpoint = NULL; 707 if (bp) 708 brelse(bp); 709 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p); 710 if (ump) { 711 free(ump->um_fs, M_UFSMNT); 712 free(ump, M_UFSMNT); 713 mp->mnt_data = (qaddr_t)0; 714 } 715 return (error); 716 } 717 718 /* 719 * Sanity checks for old file systems. 720 * 721 * XXX - goes away some day. 722 */ 723 static int 724 ffs_oldfscompat(fs) 725 struct fs *fs; 726 { 727 728 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */ 729 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */ 730 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 731 fs->fs_nrpos = 8; /* XXX */ 732 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 733 #if 0 734 int i; /* XXX */ 735 u_int64_t sizepb = fs->fs_bsize; /* XXX */ 736 /* XXX */ 737 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */ 738 for (i = 0; i < NIADDR; i++) { /* XXX */ 739 sizepb *= NINDIR(fs); /* XXX */ 740 fs->fs_maxfilesize += sizepb; /* XXX */ 741 } /* XXX */ 742 #endif 743 fs->fs_maxfilesize = (u_quad_t) 1LL << 39; 744 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */ 745 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */ 746 } /* XXX */ 747 return (0); 748 } 749 750 /* 751 * unmount system call 752 */ 753 int 754 ffs_unmount(mp, mntflags, p) 755 struct mount *mp; 756 int mntflags; 757 struct proc *p; 758 { 759 register struct ufsmount *ump; 760 register struct fs *fs; 761 int error, flags; 762 763 flags = 0; 764 if (mntflags & MNT_FORCE) { 765 flags |= FORCECLOSE; 766 } 767 #ifdef FFS_EXTATTR 768 if ((error = ufs_extattr_stop(mp, p))) 769 if (error != EOPNOTSUPP) 770 printf("ffs_unmount: ufs_extattr_stop returned %d\n", 771 error); 772 #endif 773 if (mp->mnt_flag & MNT_SOFTDEP) { 774 if ((error = softdep_flushfiles(mp, flags, p)) != 0) 775 return (error); 776 } else { 777 if ((error = ffs_flushfiles(mp, flags, p)) != 0) 778 return (error); 779 } 780 ump = VFSTOUFS(mp); 781 fs = ump->um_fs; 782 if (fs->fs_ronly == 0) { 783 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1; 784 error = ffs_sbupdate(ump, MNT_WAIT); 785 if (error) { 786 fs->fs_clean = 0; 787 return (error); 788 } 789 } 790 ump->um_devvp->v_specmountpoint = NULL; 791 792 vinvalbuf(ump->um_devvp, V_SAVE, NOCRED, p, 0, 0); 793 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, 794 NOCRED, p); 795 796 vrele(ump->um_devvp); 797 798 free(fs->fs_csp[0], M_UFSMNT); 799 free(fs, M_UFSMNT); 800 free(ump, M_UFSMNT); 801 mp->mnt_data = (qaddr_t)0; 802 mp->mnt_flag &= ~MNT_LOCAL; 803 return (error); 804 } 805 806 /* 807 * Flush out all the files in a filesystem. 808 */ 809 int 810 ffs_flushfiles(mp, flags, p) 811 register struct mount *mp; 812 int flags; 813 struct proc *p; 814 { 815 register struct ufsmount *ump; 816 int error; 817 818 ump = VFSTOUFS(mp); 819 #ifdef QUOTA 820 if (mp->mnt_flag & MNT_QUOTA) { 821 int i; 822 error = vflush(mp, NULLVP, SKIPSYSTEM|flags); 823 if (error) 824 return (error); 825 for (i = 0; i < MAXQUOTAS; i++) { 826 if (ump->um_quotas[i] == NULLVP) 827 continue; 828 quotaoff(p, mp, i); 829 } 830 /* 831 * Here we fall through to vflush again to ensure 832 * that we have gotten rid of all the system vnodes. 833 */ 834 } 835 #endif 836 if (ump->um_devvp->v_flag & VCOPYONWRITE) { 837 if ((error = vflush(mp, NULL, SKIPSYSTEM | flags)) != 0) 838 return (error); 839 ffs_snapshot_unmount(mp); 840 /* 841 * Here we fall through to vflush again to ensure 842 * that we have gotten rid of all the system vnodes. 843 */ 844 } 845 /* 846 * Flush all the files. 847 */ 848 if ((error = vflush(mp, NULL, flags)) != 0) 849 return (error); 850 /* 851 * Flush filesystem metadata. 852 */ 853 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p); 854 error = VOP_FSYNC(ump->um_devvp, p->p_ucred, MNT_WAIT, p); 855 VOP_UNLOCK(ump->um_devvp, 0, p); 856 return (error); 857 } 858 859 /* 860 * Get file system statistics. 861 */ 862 int 863 ffs_statfs(mp, sbp, p) 864 struct mount *mp; 865 register struct statfs *sbp; 866 struct proc *p; 867 { 868 register struct ufsmount *ump; 869 register struct fs *fs; 870 871 ump = VFSTOUFS(mp); 872 fs = ump->um_fs; 873 if (fs->fs_magic != FS_MAGIC) 874 panic("ffs_statfs"); 875 sbp->f_bsize = fs->fs_fsize; 876 sbp->f_iosize = fs->fs_bsize; 877 sbp->f_blocks = fs->fs_dsize; 878 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 879 fs->fs_cstotal.cs_nffree; 880 sbp->f_bavail = freespace(fs, fs->fs_minfree); 881 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 882 sbp->f_ffree = fs->fs_cstotal.cs_nifree; 883 if (sbp != &mp->mnt_stat) { 884 sbp->f_type = mp->mnt_vfc->vfc_typenum; 885 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 886 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 887 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 888 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 889 } 890 return (0); 891 } 892 893 /* 894 * Go through the disk queues to initiate sandbagged IO; 895 * go through the inodes to write those that have been modified; 896 * initiate the writing of the super block if it has been modified. 897 * 898 * Note: we are always called with the filesystem marked `MPBUSY'. 899 */ 900 int 901 ffs_sync(mp, waitfor, cred, p) 902 struct mount *mp; 903 int waitfor; 904 struct ucred *cred; 905 struct proc *p; 906 { 907 struct vnode *nvp, *vp; 908 struct inode *ip; 909 struct ufsmount *ump = VFSTOUFS(mp); 910 struct fs *fs; 911 int error, count, wait, lockreq, allerror = 0; 912 913 fs = ump->um_fs; 914 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */ 915 printf("fs = %s\n", fs->fs_fsmnt); 916 panic("ffs_sync: rofs mod"); 917 } 918 /* 919 * Write back each (modified) inode. 920 */ 921 wait = 0; 922 lockreq = LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK; 923 if (waitfor == MNT_WAIT) { 924 wait = 1; 925 lockreq = LK_EXCLUSIVE | LK_INTERLOCK; 926 } 927 simple_lock(&mntvnode_slock); 928 loop: 929 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 930 /* 931 * If the vnode that we are about to sync is no longer 932 * associated with this mount point, start over. 933 */ 934 if (vp->v_mount != mp) 935 goto loop; 936 simple_lock(&vp->v_interlock); 937 nvp = vp->v_mntvnodes.le_next; 938 ip = VTOI(vp); 939 if (vp->v_type == VNON || ((ip->i_flag & 940 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 941 TAILQ_EMPTY(&vp->v_dirtyblkhd))) { 942 simple_unlock(&vp->v_interlock); 943 continue; 944 } 945 if (vp->v_type != VCHR) { 946 simple_unlock(&mntvnode_slock); 947 if ((error = vget(vp, lockreq, p)) != 0) { 948 simple_lock(&mntvnode_slock); 949 if (error == ENOENT) 950 goto loop; 951 continue; 952 } 953 if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0) 954 allerror = error; 955 vput(vp); 956 simple_lock(&mntvnode_slock); 957 } else { 958 simple_unlock(&mntvnode_slock); 959 simple_unlock(&vp->v_interlock); 960 UFS_UPDATE(vp, wait); 961 simple_lock(&mntvnode_slock); 962 } 963 } 964 simple_unlock(&mntvnode_slock); 965 /* 966 * Force stale file system control information to be flushed. 967 */ 968 if (waitfor == MNT_WAIT) { 969 if ((error = softdep_flushworklist(ump->um_mountp, &count, p))) 970 allerror = error; 971 /* Flushed work items may create new vnodes to clean */ 972 if (count) { 973 simple_lock(&mntvnode_slock); 974 goto loop; 975 } 976 } 977 if (waitfor == MNT_NOWAIT) { 978 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p); 979 if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0) 980 allerror = error; 981 VOP_UNLOCK(ump->um_devvp, 0, p); 982 } 983 #ifdef QUOTA 984 qsync(mp); 985 #endif 986 /* 987 * Write back modified superblock. 988 */ 989 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0) 990 allerror = error; 991 return (allerror); 992 } 993 994 /* 995 * Look up a FFS dinode number to find its incore vnode, otherwise read it 996 * in from disk. If it is in core, wait for the lock bit to clear, then 997 * return the inode locked. Detection and handling of mount points must be 998 * done by the calling routine. 999 */ 1000 static int ffs_inode_hash_lock; 1001 1002 int 1003 ffs_vget(mp, ino, vpp) 1004 struct mount *mp; 1005 ino_t ino; 1006 struct vnode **vpp; 1007 { 1008 struct fs *fs; 1009 struct inode *ip; 1010 struct ufsmount *ump; 1011 struct buf *bp; 1012 struct vnode *vp; 1013 dev_t dev; 1014 int error; 1015 1016 ump = VFSTOUFS(mp); 1017 dev = ump->um_dev; 1018 restart: 1019 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) { 1020 return (0); 1021 } 1022 1023 /* 1024 * Lock out the creation of new entries in the FFS hash table in 1025 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate 1026 * may occur! 1027 */ 1028 if (ffs_inode_hash_lock) { 1029 while (ffs_inode_hash_lock) { 1030 ffs_inode_hash_lock = -1; 1031 tsleep(&ffs_inode_hash_lock, PVM, "ffsvgt", 0); 1032 } 1033 goto restart; 1034 } 1035 ffs_inode_hash_lock = 1; 1036 1037 /* 1038 * If this MALLOC() is performed after the getnewvnode() 1039 * it might block, leaving a vnode with a NULL v_data to be 1040 * found by ffs_sync() if a sync happens to fire right then, 1041 * which will cause a panic because ffs_sync() blindly 1042 * dereferences vp->v_data (as well it should). 1043 */ 1044 MALLOC(ip, struct inode *, sizeof(struct inode), 1045 ump->um_malloctype, M_WAITOK); 1046 1047 /* Allocate a new vnode/inode. */ 1048 error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp); 1049 if (error) { 1050 if (ffs_inode_hash_lock < 0) 1051 wakeup(&ffs_inode_hash_lock); 1052 ffs_inode_hash_lock = 0; 1053 *vpp = NULL; 1054 FREE(ip, ump->um_malloctype); 1055 return (error); 1056 } 1057 bzero((caddr_t)ip, sizeof(struct inode)); 1058 lockinit(&ip->i_lock, PINOD, "inode", 0, LK_CANRECURSE); 1059 vp->v_data = ip; 1060 ip->i_vnode = vp; 1061 ip->i_fs = fs = ump->um_fs; 1062 ip->i_dev = dev; 1063 ip->i_number = ino; 1064 #ifdef QUOTA 1065 { 1066 int i; 1067 for (i = 0; i < MAXQUOTAS; i++) 1068 ip->i_dquot[i] = NODQUOT; 1069 } 1070 #endif 1071 /* 1072 * Put it onto its hash chain and lock it so that other requests for 1073 * this inode will block if they arrive while we are sleeping waiting 1074 * for old data structures to be purged or for the contents of the 1075 * disk portion of this inode to be read. 1076 */ 1077 ufs_ihashins(ip); 1078 1079 if (ffs_inode_hash_lock < 0) 1080 wakeup(&ffs_inode_hash_lock); 1081 ffs_inode_hash_lock = 0; 1082 1083 /* Read in the disk contents for the inode, copy into the inode. */ 1084 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 1085 (int)fs->fs_bsize, NOCRED, &bp); 1086 if (error) { 1087 /* 1088 * The inode does not contain anything useful, so it would 1089 * be misleading to leave it on its hash chain. With mode 1090 * still zero, it will be unlinked and returned to the free 1091 * list by vput(). 1092 */ 1093 brelse(bp); 1094 vput(vp); 1095 *vpp = NULL; 1096 return (error); 1097 } 1098 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino)); 1099 if (DOINGSOFTDEP(vp)) 1100 softdep_load_inodeblock(ip); 1101 else 1102 ip->i_effnlink = ip->i_nlink; 1103 bqrelse(bp); 1104 1105 /* 1106 * Initialize the vnode from the inode, check for aliases. 1107 * Note that the underlying vnode may have changed. 1108 */ 1109 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp); 1110 if (error) { 1111 vput(vp); 1112 *vpp = NULL; 1113 return (error); 1114 } 1115 /* 1116 * Finish inode initialization now that aliasing has been resolved. 1117 */ 1118 ip->i_devvp = ump->um_devvp; 1119 VREF(ip->i_devvp); 1120 /* 1121 * Set up a generation number for this inode if it does not 1122 * already have one. This should only happen on old filesystems. 1123 */ 1124 if (ip->i_gen == 0) { 1125 ip->i_gen = random() / 2 + 1; 1126 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 1127 ip->i_flag |= IN_MODIFIED; 1128 } 1129 /* 1130 * Ensure that uid and gid are correct. This is a temporary 1131 * fix until fsck has been changed to do the update. 1132 */ 1133 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 1134 ip->i_uid = ip->i_din.di_ouid; /* XXX */ 1135 ip->i_gid = ip->i_din.di_ogid; /* XXX */ 1136 } /* XXX */ 1137 1138 *vpp = vp; 1139 return (0); 1140 } 1141 1142 /* 1143 * File handle to vnode 1144 * 1145 * Have to be really careful about stale file handles: 1146 * - check that the inode number is valid 1147 * - call ffs_vget() to get the locked inode 1148 * - check for an unallocated inode (i_mode == 0) 1149 * - check that the given client host has export rights and return 1150 * those rights via. exflagsp and credanonp 1151 */ 1152 int 1153 ffs_fhtovp(mp, fhp, vpp) 1154 register struct mount *mp; 1155 struct fid *fhp; 1156 struct vnode **vpp; 1157 { 1158 register struct ufid *ufhp; 1159 struct fs *fs; 1160 1161 ufhp = (struct ufid *)fhp; 1162 fs = VFSTOUFS(mp)->um_fs; 1163 if (ufhp->ufid_ino < ROOTINO || 1164 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 1165 return (ESTALE); 1166 return (ufs_fhtovp(mp, ufhp, vpp)); 1167 } 1168 1169 /* 1170 * Vnode pointer to File handle 1171 */ 1172 /* ARGSUSED */ 1173 int 1174 ffs_vptofh(vp, fhp) 1175 struct vnode *vp; 1176 struct fid *fhp; 1177 { 1178 register struct inode *ip; 1179 register struct ufid *ufhp; 1180 1181 ip = VTOI(vp); 1182 ufhp = (struct ufid *)fhp; 1183 ufhp->ufid_len = sizeof(struct ufid); 1184 ufhp->ufid_ino = ip->i_number; 1185 ufhp->ufid_gen = ip->i_gen; 1186 return (0); 1187 } 1188 1189 /* 1190 * Initialize the filesystem; just use ufs_init. 1191 */ 1192 static int 1193 ffs_init(vfsp) 1194 struct vfsconf *vfsp; 1195 { 1196 1197 softdep_initialize(); 1198 return (ufs_init(vfsp)); 1199 } 1200 1201 /* 1202 * Write a superblock and associated information back to disk. 1203 */ 1204 static int 1205 ffs_sbupdate(mp, waitfor) 1206 struct ufsmount *mp; 1207 int waitfor; 1208 { 1209 register struct fs *dfs, *fs = mp->um_fs; 1210 register struct buf *bp; 1211 int blks; 1212 caddr_t space; 1213 int i, size, error, allerror = 0; 1214 1215 /* 1216 * First write back the summary information. 1217 */ 1218 blks = howmany(fs->fs_cssize, fs->fs_fsize); 1219 space = (caddr_t)fs->fs_csp[0]; 1220 for (i = 0; i < blks; i += fs->fs_frag) { 1221 size = fs->fs_bsize; 1222 if (i + fs->fs_frag > blks) 1223 size = (blks - i) * fs->fs_fsize; 1224 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), 1225 size, 0, 0); 1226 bcopy(space, bp->b_data, (u_int)size); 1227 space += size; 1228 if (waitfor != MNT_WAIT) 1229 bawrite(bp); 1230 else if ((error = bwrite(bp)) != 0) 1231 allerror = error; 1232 } 1233 /* 1234 * Now write back the superblock itself. If any errors occurred 1235 * up to this point, then fail so that the superblock avoids 1236 * being written out as clean. 1237 */ 1238 if (allerror) 1239 return (allerror); 1240 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0); 1241 fs->fs_fmod = 0; 1242 fs->fs_time = time_second; 1243 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 1244 /* Restore compatibility to old file systems. XXX */ 1245 dfs = (struct fs *)bp->b_data; /* XXX */ 1246 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 1247 dfs->fs_nrpos = -1; /* XXX */ 1248 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 1249 int32_t *lp, tmp; /* XXX */ 1250 /* XXX */ 1251 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */ 1252 tmp = lp[4]; /* XXX */ 1253 for (i = 4; i > 0; i--) /* XXX */ 1254 lp[i] = lp[i-1]; /* XXX */ 1255 lp[0] = tmp; /* XXX */ 1256 } /* XXX */ 1257 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */ 1258 if (waitfor != MNT_WAIT) 1259 bawrite(bp); 1260 else if ((error = bwrite(bp)) != 0) 1261 allerror = error; 1262 return (allerror); 1263 } 1264