1 /* 2 * Copyright (c) 1989, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ffs_vfsops.c 8.17 (Berkeley) 01/02/95 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/namei.h> 13 #include <sys/proc.h> 14 #include <sys/kernel.h> 15 #include <sys/vnode.h> 16 #include <sys/socket.h> 17 #include <sys/mount.h> 18 #include <sys/buf.h> 19 #include <sys/mbuf.h> 20 #include <sys/file.h> 21 #include <sys/disklabel.h> 22 #include <sys/ioctl.h> 23 #include <sys/errno.h> 24 #include <sys/malloc.h> 25 26 #include <miscfs/specfs/specdev.h> 27 28 #include <ufs/ufs/quota.h> 29 #include <ufs/ufs/ufsmount.h> 30 #include <ufs/ufs/inode.h> 31 #include <ufs/ufs/ufs_extern.h> 32 33 #include <ufs/ffs/fs.h> 34 #include <ufs/ffs/ffs_extern.h> 35 36 int ffs_sbupdate __P((struct ufsmount *, int)); 37 38 struct vfsops ufs_vfsops = { 39 ffs_mount, 40 ufs_start, 41 ffs_unmount, 42 ufs_root, 43 ufs_quotactl, 44 ffs_statfs, 45 ffs_sync, 46 ffs_vget, 47 ffs_fhtovp, 48 ffs_vptofh, 49 ffs_init, 50 }; 51 52 extern u_long nextgennumber; 53 54 /* 55 * Called by main() when ufs is going to be mounted as root. 56 * 57 * Name is updated by mount(8) after booting. 58 */ 59 #define ROOTNAME "root_device" 60 61 ffs_mountroot() 62 { 63 extern struct vnode *rootvp; 64 register struct fs *fs; 65 register struct mount *mp; 66 struct proc *p = curproc; /* XXX */ 67 struct ufsmount *ump; 68 u_int size; 69 int error; 70 71 /* 72 * Get vnodes for swapdev and rootdev. 73 */ 74 if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp)) 75 panic("ffs_mountroot: can't setup bdevvp's"); 76 77 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 78 bzero((char *)mp, (u_long)sizeof(struct mount)); 79 mp->mnt_op = &ufs_vfsops; 80 mp->mnt_flag = MNT_RDONLY; 81 if (error = ffs_mountfs(rootvp, mp, p)) { 82 free(mp, M_MOUNT); 83 return (error); 84 } 85 if (error = vfs_lock(mp)) { 86 (void)ffs_unmount(mp, 0, p); 87 free(mp, M_MOUNT); 88 return (error); 89 } 90 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 91 mp->mnt_flag |= MNT_ROOTFS; 92 mp->mnt_vnodecovered = NULLVP; 93 ump = VFSTOUFS(mp); 94 fs = ump->um_fs; 95 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt)); 96 fs->fs_fsmnt[0] = '/'; 97 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 98 MNAMELEN); 99 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 100 &size); 101 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 102 (void)ffs_statfs(mp, &mp->mnt_stat, p); 103 vfs_unlock(mp); 104 inittodr(fs->fs_time); 105 return (0); 106 } 107 108 /* 109 * VFS Operations. 110 * 111 * mount system call 112 */ 113 int 114 ffs_mount(mp, path, data, ndp, p) 115 register struct mount *mp; 116 char *path; 117 caddr_t data; 118 struct nameidata *ndp; 119 struct proc *p; 120 { 121 struct vnode *devvp; 122 struct ufs_args args; 123 struct ufsmount *ump; 124 register struct fs *fs; 125 u_int size; 126 int error, flags; 127 mode_t accessmode; 128 129 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) 130 return (error); 131 /* 132 * If updating, check whether changing from read-only to 133 * read/write; if there is no device name, that's all we do. 134 */ 135 if (mp->mnt_flag & MNT_UPDATE) { 136 ump = VFSTOUFS(mp); 137 fs = ump->um_fs; 138 error = 0; 139 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 140 flags = WRITECLOSE; 141 if (mp->mnt_flag & MNT_FORCE) 142 flags |= FORCECLOSE; 143 if (vfs_busy(mp)) 144 return (EBUSY); 145 error = ffs_flushfiles(mp, flags, p); 146 vfs_unbusy(mp); 147 } 148 if (!error && (mp->mnt_flag & MNT_RELOAD)) 149 error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p); 150 if (error) 151 return (error); 152 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) { 153 /* 154 * If upgrade to read-write by non-root, then verify 155 * that user has necessary permissions on the device. 156 */ 157 if (p->p_ucred->cr_uid != 0) { 158 devvp = ump->um_devvp; 159 VOP_LOCK(devvp); 160 if (error = VOP_ACCESS(devvp, VREAD | VWRITE, 161 p->p_ucred, p)) { 162 VOP_UNLOCK(devvp); 163 return (error); 164 } 165 VOP_UNLOCK(devvp); 166 } 167 fs->fs_ronly = 0; 168 } 169 if (args.fspec == 0) { 170 /* 171 * Process export requests. 172 */ 173 return (vfs_export(mp, &ump->um_export, &args.export)); 174 } 175 } 176 /* 177 * Not an update, or updating the name: look up the name 178 * and verify that it refers to a sensible block device. 179 */ 180 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 181 if (error = namei(ndp)) 182 return (error); 183 devvp = ndp->ni_vp; 184 185 if (devvp->v_type != VBLK) { 186 vrele(devvp); 187 return (ENOTBLK); 188 } 189 if (major(devvp->v_rdev) >= nblkdev) { 190 vrele(devvp); 191 return (ENXIO); 192 } 193 /* 194 * If mount by non-root, then verify that user has necessary 195 * permissions on the device. 196 */ 197 if (p->p_ucred->cr_uid != 0) { 198 accessmode = VREAD; 199 if ((mp->mnt_flag & MNT_RDONLY) == 0) 200 accessmode |= VWRITE; 201 VOP_LOCK(devvp); 202 if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) { 203 vput(devvp); 204 return (error); 205 } 206 VOP_UNLOCK(devvp); 207 } 208 if ((mp->mnt_flag & MNT_UPDATE) == 0) 209 error = ffs_mountfs(devvp, mp, p); 210 else { 211 if (devvp != ump->um_devvp) 212 error = EINVAL; /* needs translation */ 213 else 214 vrele(devvp); 215 } 216 if (error) { 217 vrele(devvp); 218 return (error); 219 } 220 ump = VFSTOUFS(mp); 221 fs = ump->um_fs; 222 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 223 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 224 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 225 MNAMELEN); 226 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 227 &size); 228 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 229 (void)ffs_statfs(mp, &mp->mnt_stat, p); 230 return (0); 231 } 232 233 /* 234 * Reload all incore data for a filesystem (used after running fsck on 235 * the root filesystem and finding things to fix). The filesystem must 236 * be mounted read-only. 237 * 238 * Things to do to update the mount: 239 * 1) invalidate all cached meta-data. 240 * 2) re-read superblock from disk. 241 * 3) re-read summary information from disk. 242 * 4) invalidate all inactive vnodes. 243 * 5) invalidate all cached file data. 244 * 6) re-read inode data for all active vnodes. 245 */ 246 ffs_reload(mountp, cred, p) 247 register struct mount *mountp; 248 struct ucred *cred; 249 struct proc *p; 250 { 251 register struct vnode *vp, *nvp, *devvp; 252 struct inode *ip; 253 struct csum *space; 254 struct buf *bp; 255 struct fs *fs; 256 struct partinfo dpart; 257 int i, blks, size, error; 258 259 if ((mountp->mnt_flag & MNT_RDONLY) == 0) 260 return (EINVAL); 261 /* 262 * Step 1: invalidate all cached meta-data. 263 */ 264 devvp = VFSTOUFS(mountp)->um_devvp; 265 if (vinvalbuf(devvp, 0, cred, p, 0, 0)) 266 panic("ffs_reload: dirty1"); 267 /* 268 * Step 2: re-read superblock from disk. 269 */ 270 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 271 size = DEV_BSIZE; 272 else 273 size = dpart.disklab->d_secsize; 274 if (error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp)) 275 return (error); 276 fs = (struct fs *)bp->b_data; 277 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE || 278 fs->fs_bsize < sizeof(struct fs)) { 279 brelse(bp); 280 return (EIO); /* XXX needs translation */ 281 } 282 fs = VFSTOUFS(mountp)->um_fs; 283 bcopy(&fs->fs_csp[0], &((struct fs *)bp->b_data)->fs_csp[0], 284 sizeof(fs->fs_csp)); 285 bcopy(bp->b_data, fs, (u_int)fs->fs_sbsize); 286 if (fs->fs_sbsize < SBSIZE) 287 bp->b_flags |= B_INVAL; 288 brelse(bp); 289 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 290 ffs_oldfscompat(fs); 291 /* 292 * Step 3: re-read summary information from disk. 293 */ 294 blks = howmany(fs->fs_cssize, fs->fs_fsize); 295 space = fs->fs_csp[0]; 296 for (i = 0; i < blks; i += fs->fs_frag) { 297 size = fs->fs_bsize; 298 if (i + fs->fs_frag > blks) 299 size = (blks - i) * fs->fs_fsize; 300 if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 301 NOCRED, &bp)) 302 return (error); 303 bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size); 304 brelse(bp); 305 } 306 loop: 307 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 308 nvp = vp->v_mntvnodes.le_next; 309 /* 310 * Step 4: invalidate all inactive vnodes. 311 */ 312 if (vp->v_usecount == 0) { 313 vgone(vp); 314 continue; 315 } 316 /* 317 * Step 5: invalidate all cached file data. 318 */ 319 if (vget(vp, 1)) 320 goto loop; 321 if (vinvalbuf(vp, 0, cred, p, 0, 0)) 322 panic("ffs_reload: dirty2"); 323 /* 324 * Step 6: re-read inode data for all active vnodes. 325 */ 326 ip = VTOI(vp); 327 if (error = 328 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 329 (int)fs->fs_bsize, NOCRED, &bp)) { 330 vput(vp); 331 return (error); 332 } 333 ip->i_din = *((struct dinode *)bp->b_data + 334 ino_to_fsbo(fs, ip->i_number)); 335 brelse(bp); 336 vput(vp); 337 if (vp->v_mount != mountp) 338 goto loop; 339 } 340 return (0); 341 } 342 343 /* 344 * Common code for mount and mountroot 345 */ 346 int 347 ffs_mountfs(devvp, mp, p) 348 register struct vnode *devvp; 349 struct mount *mp; 350 struct proc *p; 351 { 352 register struct ufsmount *ump; 353 struct buf *bp; 354 register struct fs *fs; 355 dev_t dev; 356 struct partinfo dpart; 357 caddr_t base, space; 358 int error, i, blks, size, ronly; 359 int32_t *lp; 360 struct ucred *cred; 361 extern struct vnode *rootvp; 362 u_int64_t maxfilesize; /* XXX */ 363 364 dev = devvp->v_rdev; 365 cred = p ? p->p_ucred : NOCRED; 366 /* 367 * Disallow multiple mounts of the same device. 368 * Disallow mounting of a device that is currently in use 369 * (except for root, which might share swap device for miniroot). 370 * Flush out any old buffers remaining from a previous use. 371 */ 372 if (error = vfs_mountedon(devvp)) 373 return (error); 374 if (vcount(devvp) > 1 && devvp != rootvp) 375 return (EBUSY); 376 if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) 377 return (error); 378 379 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 380 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p)) 381 return (error); 382 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0) 383 size = DEV_BSIZE; 384 else 385 size = dpart.disklab->d_secsize; 386 387 bp = NULL; 388 ump = NULL; 389 if (error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, cred, &bp)) 390 goto out; 391 fs = (struct fs *)bp->b_data; 392 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE || 393 fs->fs_bsize < sizeof(struct fs)) { 394 error = EINVAL; /* XXX needs translation */ 395 goto out; 396 } 397 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */ 398 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) { 399 error = EROFS; /* needs translation */ 400 goto out; 401 } 402 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 403 bzero((caddr_t)ump, sizeof *ump); 404 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, 405 M_WAITOK); 406 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize); 407 if (fs->fs_sbsize < SBSIZE) 408 bp->b_flags |= B_INVAL; 409 brelse(bp); 410 bp = NULL; 411 fs = ump->um_fs; 412 fs->fs_ronly = ronly; 413 if (ronly == 0) 414 fs->fs_fmod = 1; 415 size = fs->fs_cssize; 416 blks = howmany(size, fs->fs_fsize); 417 if (fs->fs_contigsumsize > 0) 418 size += fs->fs_ncg * sizeof(int32_t); 419 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK); 420 for (i = 0; i < blks; i += fs->fs_frag) { 421 size = fs->fs_bsize; 422 if (i + fs->fs_frag > blks) 423 size = (blks - i) * fs->fs_fsize; 424 if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 425 cred, &bp)) { 426 free(base, M_UFSMNT); 427 goto out; 428 } 429 bcopy(bp->b_data, space, (u_int)size); 430 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space; 431 space += size; 432 brelse(bp); 433 bp = NULL; 434 } 435 if (fs->fs_contigsumsize > 0) { 436 fs->fs_maxcluster = lp = (int32_t *)space; 437 for (i = 0; i < fs->fs_ncg; i++) 438 *lp++ = fs->fs_contigsumsize; 439 } 440 mp->mnt_data = (qaddr_t)ump; 441 mp->mnt_stat.f_fsid.val[0] = (long)dev; 442 mp->mnt_stat.f_fsid.val[1] = MOUNT_UFS; 443 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 444 mp->mnt_flag |= MNT_LOCAL; 445 ump->um_mountp = mp; 446 ump->um_dev = dev; 447 ump->um_devvp = devvp; 448 ump->um_nindir = fs->fs_nindir; 449 ump->um_bptrtodb = fs->fs_fsbtodb; 450 ump->um_seqinc = fs->fs_frag; 451 for (i = 0; i < MAXQUOTAS; i++) 452 ump->um_quotas[i] = NULLVP; 453 devvp->v_specflags |= SI_MOUNTEDON; 454 ffs_oldfscompat(fs); 455 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */ 456 maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */ 457 if (fs->fs_maxfilesize > maxfilesize) /* XXX */ 458 fs->fs_maxfilesize = maxfilesize; /* XXX */ 459 return (0); 460 out: 461 if (bp) 462 brelse(bp); 463 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p); 464 if (ump) { 465 free(ump->um_fs, M_UFSMNT); 466 free(ump, M_UFSMNT); 467 mp->mnt_data = (qaddr_t)0; 468 } 469 return (error); 470 } 471 472 /* 473 * Sanity checks for old file systems. 474 * 475 * XXX - goes away some day. 476 */ 477 ffs_oldfscompat(fs) 478 struct fs *fs; 479 { 480 int i; 481 482 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */ 483 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */ 484 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 485 fs->fs_nrpos = 8; /* XXX */ 486 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 487 u_int64_t sizepb = fs->fs_bsize; /* XXX */ 488 /* XXX */ 489 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */ 490 for (i = 0; i < NIADDR; i++) { /* XXX */ 491 sizepb *= NINDIR(fs); /* XXX */ 492 fs->fs_maxfilesize += sizepb; /* XXX */ 493 } /* XXX */ 494 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */ 495 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */ 496 } /* XXX */ 497 return (0); 498 } 499 500 /* 501 * unmount system call 502 */ 503 int 504 ffs_unmount(mp, mntflags, p) 505 struct mount *mp; 506 int mntflags; 507 struct proc *p; 508 { 509 register struct ufsmount *ump; 510 register struct fs *fs; 511 int error, flags; 512 513 flags = 0; 514 if (mntflags & MNT_FORCE) { 515 if (mp->mnt_flag & MNT_ROOTFS) 516 return (EINVAL); 517 flags |= FORCECLOSE; 518 } 519 if (error = ffs_flushfiles(mp, flags, p)) 520 return (error); 521 ump = VFSTOUFS(mp); 522 fs = ump->um_fs; 523 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON; 524 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, 525 NOCRED, p); 526 vrele(ump->um_devvp); 527 free(fs->fs_csp[0], M_UFSMNT); 528 free(fs, M_UFSMNT); 529 free(ump, M_UFSMNT); 530 mp->mnt_data = (qaddr_t)0; 531 mp->mnt_flag &= ~MNT_LOCAL; 532 return (error); 533 } 534 535 /* 536 * Flush out all the files in a filesystem. 537 */ 538 ffs_flushfiles(mp, flags, p) 539 register struct mount *mp; 540 int flags; 541 struct proc *p; 542 { 543 extern int doforce; 544 register struct ufsmount *ump; 545 int i, error; 546 547 if (!doforce) 548 flags &= ~FORCECLOSE; 549 ump = VFSTOUFS(mp); 550 #ifdef QUOTA 551 if (mp->mnt_flag & MNT_QUOTA) { 552 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) 553 return (error); 554 for (i = 0; i < MAXQUOTAS; i++) { 555 if (ump->um_quotas[i] == NULLVP) 556 continue; 557 quotaoff(p, mp, i); 558 } 559 /* 560 * Here we fall through to vflush again to ensure 561 * that we have gotten rid of all the system vnodes. 562 */ 563 } 564 #endif 565 error = vflush(mp, NULLVP, flags); 566 return (error); 567 } 568 569 /* 570 * Get file system statistics. 571 */ 572 int 573 ffs_statfs(mp, sbp, p) 574 struct mount *mp; 575 register struct statfs *sbp; 576 struct proc *p; 577 { 578 register struct ufsmount *ump; 579 register struct fs *fs; 580 581 ump = VFSTOUFS(mp); 582 fs = ump->um_fs; 583 if (fs->fs_magic != FS_MAGIC) 584 panic("ffs_statfs"); 585 sbp->f_type = MOUNT_UFS; 586 sbp->f_bsize = fs->fs_fsize; 587 sbp->f_iosize = fs->fs_bsize; 588 sbp->f_blocks = fs->fs_dsize; 589 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 590 fs->fs_cstotal.cs_nffree; 591 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) - 592 (fs->fs_dsize - sbp->f_bfree); 593 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 594 sbp->f_ffree = fs->fs_cstotal.cs_nifree; 595 if (sbp != &mp->mnt_stat) { 596 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 597 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 598 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 599 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 600 } 601 return (0); 602 } 603 604 /* 605 * Go through the disk queues to initiate sandbagged IO; 606 * go through the inodes to write those that have been modified; 607 * initiate the writing of the super block if it has been modified. 608 * 609 * Note: we are always called with the filesystem marked `MPBUSY'. 610 */ 611 int 612 ffs_sync(mp, waitfor, cred, p) 613 struct mount *mp; 614 int waitfor; 615 struct ucred *cred; 616 struct proc *p; 617 { 618 register struct vnode *vp; 619 register struct inode *ip; 620 register struct ufsmount *ump = VFSTOUFS(mp); 621 register struct fs *fs; 622 int error, allerror = 0; 623 624 fs = ump->um_fs; 625 /* 626 * Write back modified superblock. 627 * Consistency check that the superblock 628 * is still in the buffer cache. 629 */ 630 if (fs->fs_fmod != 0) { 631 if (fs->fs_ronly != 0) { /* XXX */ 632 printf("fs = %s\n", fs->fs_fsmnt); 633 panic("update: rofs mod"); 634 } 635 fs->fs_fmod = 0; 636 fs->fs_time = time.tv_sec; 637 allerror = ffs_sbupdate(ump, waitfor); 638 } 639 /* 640 * Write back each (modified) inode. 641 */ 642 loop: 643 for (vp = mp->mnt_vnodelist.lh_first; 644 vp != NULL; 645 vp = vp->v_mntvnodes.le_next) { 646 /* 647 * If the vnode that we are about to sync is no longer 648 * associated with this mount point, start over. 649 */ 650 if (vp->v_mount != mp) 651 goto loop; 652 if (VOP_ISLOCKED(vp)) 653 continue; 654 ip = VTOI(vp); 655 if ((ip->i_flag & 656 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 657 vp->v_dirtyblkhd.lh_first == NULL) 658 continue; 659 if (vget(vp, 1)) 660 goto loop; 661 if (error = VOP_FSYNC(vp, cred, waitfor, p)) 662 allerror = error; 663 vput(vp); 664 } 665 /* 666 * Force stale file system control information to be flushed. 667 */ 668 if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) 669 allerror = error; 670 #ifdef QUOTA 671 qsync(mp); 672 #endif 673 return (allerror); 674 } 675 676 /* 677 * Look up a FFS dinode number to find its incore vnode, otherwise read it 678 * in from disk. If it is in core, wait for the lock bit to clear, then 679 * return the inode locked. Detection and handling of mount points must be 680 * done by the calling routine. 681 */ 682 int 683 ffs_vget(mp, ino, vpp) 684 struct mount *mp; 685 ino_t ino; 686 struct vnode **vpp; 687 { 688 register struct fs *fs; 689 register struct inode *ip; 690 struct ufsmount *ump; 691 struct buf *bp; 692 struct vnode *vp; 693 dev_t dev; 694 int i, type, error; 695 696 ump = VFSTOUFS(mp); 697 dev = ump->um_dev; 698 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 699 return (0); 700 701 /* Allocate a new vnode/inode. */ 702 if (error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) { 703 *vpp = NULL; 704 return (error); 705 } 706 type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */ 707 MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK); 708 bzero((caddr_t)ip, sizeof(struct inode)); 709 vp->v_data = ip; 710 ip->i_vnode = vp; 711 ip->i_fs = fs = ump->um_fs; 712 ip->i_dev = dev; 713 ip->i_number = ino; 714 #ifdef QUOTA 715 for (i = 0; i < MAXQUOTAS; i++) 716 ip->i_dquot[i] = NODQUOT; 717 #endif 718 /* 719 * Put it onto its hash chain and lock it so that other requests for 720 * this inode will block if they arrive while we are sleeping waiting 721 * for old data structures to be purged or for the contents of the 722 * disk portion of this inode to be read. 723 */ 724 ufs_ihashins(ip); 725 726 /* Read in the disk contents for the inode, copy into the inode. */ 727 if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 728 (int)fs->fs_bsize, NOCRED, &bp)) { 729 /* 730 * The inode does not contain anything useful, so it would 731 * be misleading to leave it on its hash chain. With mode 732 * still zero, it will be unlinked and returned to the free 733 * list by vput(). 734 */ 735 vput(vp); 736 brelse(bp); 737 *vpp = NULL; 738 return (error); 739 } 740 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino)); 741 brelse(bp); 742 743 /* 744 * Initialize the vnode from the inode, check for aliases. 745 * Note that the underlying vnode may have changed. 746 */ 747 if (error = ufs_vinit(mp, ffs_specop_p, FFS_FIFOOPS, &vp)) { 748 vput(vp); 749 *vpp = NULL; 750 return (error); 751 } 752 /* 753 * Finish inode initialization now that aliasing has been resolved. 754 */ 755 ip->i_devvp = ump->um_devvp; 756 VREF(ip->i_devvp); 757 /* 758 * Set up a generation number for this inode if it does not 759 * already have one. This should only happen on old filesystems. 760 */ 761 if (ip->i_gen == 0) { 762 if (++nextgennumber < (u_long)time.tv_sec) 763 nextgennumber = time.tv_sec; 764 ip->i_gen = nextgennumber; 765 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 766 ip->i_flag |= IN_MODIFIED; 767 } 768 /* 769 * Ensure that uid and gid are correct. This is a temporary 770 * fix until fsck has been changed to do the update. 771 */ 772 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 773 ip->i_uid = ip->i_din.di_ouid; /* XXX */ 774 ip->i_gid = ip->i_din.di_ogid; /* XXX */ 775 } /* XXX */ 776 777 *vpp = vp; 778 return (0); 779 } 780 781 /* 782 * File handle to vnode 783 * 784 * Have to be really careful about stale file handles: 785 * - check that the inode number is valid 786 * - call ffs_vget() to get the locked inode 787 * - check for an unallocated inode (i_mode == 0) 788 * - check that the given client host has export rights and return 789 * those rights via. exflagsp and credanonp 790 */ 791 int 792 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 793 register struct mount *mp; 794 struct fid *fhp; 795 struct mbuf *nam; 796 struct vnode **vpp; 797 int *exflagsp; 798 struct ucred **credanonp; 799 { 800 register struct ufid *ufhp; 801 struct fs *fs; 802 803 ufhp = (struct ufid *)fhp; 804 fs = VFSTOUFS(mp)->um_fs; 805 if (ufhp->ufid_ino < ROOTINO || 806 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 807 return (ESTALE); 808 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp)); 809 } 810 811 /* 812 * Vnode pointer to File handle 813 */ 814 /* ARGSUSED */ 815 ffs_vptofh(vp, fhp) 816 struct vnode *vp; 817 struct fid *fhp; 818 { 819 register struct inode *ip; 820 register struct ufid *ufhp; 821 822 ip = VTOI(vp); 823 ufhp = (struct ufid *)fhp; 824 ufhp->ufid_len = sizeof(struct ufid); 825 ufhp->ufid_ino = ip->i_number; 826 ufhp->ufid_gen = ip->i_gen; 827 return (0); 828 } 829 830 /* 831 * Write a superblock and associated information back to disk. 832 */ 833 int 834 ffs_sbupdate(mp, waitfor) 835 struct ufsmount *mp; 836 int waitfor; 837 { 838 register struct fs *dfs, *fs = mp->um_fs; 839 register struct buf *bp; 840 int blks; 841 caddr_t space; 842 int i, size, error = 0; 843 844 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0); 845 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 846 /* Restore compatibility to old file systems. XXX */ 847 dfs = (struct fs *)bp->b_data; /* XXX */ 848 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 849 dfs->fs_nrpos = -1; /* XXX */ 850 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 851 int32_t *lp, tmp; /* XXX */ 852 /* XXX */ 853 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */ 854 tmp = lp[4]; /* XXX */ 855 for (i = 4; i > 0; i--) /* XXX */ 856 lp[i] = lp[i-1]; /* XXX */ 857 lp[0] = tmp; /* XXX */ 858 } /* XXX */ 859 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */ 860 if (waitfor == MNT_WAIT) 861 error = bwrite(bp); 862 else 863 bawrite(bp); 864 blks = howmany(fs->fs_cssize, fs->fs_fsize); 865 space = (caddr_t)fs->fs_csp[0]; 866 for (i = 0; i < blks; i += fs->fs_frag) { 867 size = fs->fs_bsize; 868 if (i + fs->fs_frag > blks) 869 size = (blks - i) * fs->fs_fsize; 870 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), 871 size, 0, 0); 872 bcopy(space, bp->b_data, (u_int)size); 873 space += size; 874 if (waitfor == MNT_WAIT) 875 error = bwrite(bp); 876 else 877 bawrite(bp); 878 } 879 return (error); 880 } 881