1 /* $NetBSD: lfs_vfsops.c,v 1.76 2002/05/17 21:42:38 perseant Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Konrad E. Schroder <perseant@hhhh.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 /*- 39 * Copyright (c) 1989, 1991, 1993, 1994 40 * The Regents of the University of California. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 * 70 * @(#)lfs_vfsops.c 8.20 (Berkeley) 6/10/95 71 */ 72 73 #include <sys/cdefs.h> 74 __KERNEL_RCSID(0, "$NetBSD: lfs_vfsops.c,v 1.76 2002/05/17 21:42:38 perseant Exp $"); 75 76 #if defined(_KERNEL_OPT) 77 #include "opt_quota.h" 78 #endif 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/namei.h> 83 #include <sys/proc.h> 84 #include <sys/kernel.h> 85 #include <sys/vnode.h> 86 #include <sys/mount.h> 87 #include <sys/buf.h> 88 #include <sys/device.h> 89 #include <sys/mbuf.h> 90 #include <sys/file.h> 91 #include <sys/disklabel.h> 92 #include <sys/ioctl.h> 93 #include <sys/errno.h> 94 #include <sys/malloc.h> 95 #include <sys/pool.h> 96 #include <sys/socket.h> 97 #include <uvm/uvm_extern.h> 98 #include <sys/sysctl.h> 99 100 #include <miscfs/specfs/specdev.h> 101 102 #include <ufs/ufs/quota.h> 103 #include <ufs/ufs/inode.h> 104 #include <ufs/ufs/ufsmount.h> 105 #include <ufs/ufs/ufs_extern.h> 106 107 #include <ufs/lfs/lfs.h> 108 #include <ufs/lfs/lfs_extern.h> 109 110 int lfs_mountfs(struct vnode *, struct mount *, struct proc *); 111 112 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc; 113 extern const struct vnodeopv_desc lfs_specop_opv_desc; 114 extern const struct vnodeopv_desc lfs_fifoop_opv_desc; 115 116 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = { 117 &lfs_vnodeop_opv_desc, 118 &lfs_specop_opv_desc, 119 &lfs_fifoop_opv_desc, 120 NULL, 121 }; 122 123 struct vfsops lfs_vfsops = { 124 MOUNT_LFS, 125 lfs_mount, 126 ufs_start, 127 lfs_unmount, 128 ufs_root, 129 ufs_quotactl, 130 lfs_statfs, 131 lfs_sync, 132 lfs_vget, 133 lfs_fhtovp, 134 lfs_vptofh, 135 lfs_init, 136 lfs_reinit, 137 lfs_done, 138 lfs_sysctl, 139 lfs_mountroot, 140 ufs_check_export, 141 lfs_vnodeopv_descs, 142 }; 143 144 struct genfs_ops lfs_genfsops = { 145 NULL, 146 NULL, 147 genfs_compat_gop_write, 148 }; 149 150 struct pool lfs_inode_pool; 151 152 extern int locked_queue_count; 153 extern long locked_queue_bytes; 154 155 /* 156 * Initialize the filesystem, most work done by ufs_init. 157 */ 158 void 159 lfs_init() 160 { 161 ufs_init(); 162 163 /* 164 * XXX Same structure as FFS inodes? Should we share a common pool? 165 */ 166 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0, 167 "lfsinopl", &pool_allocator_nointr); 168 #ifdef DEBUG 169 memset(lfs_log, 0, sizeof(lfs_log)); 170 #endif 171 } 172 173 void 174 lfs_reinit() 175 { 176 ufs_reinit(); 177 } 178 179 void 180 lfs_done() 181 { 182 ufs_done(); 183 pool_destroy(&lfs_inode_pool); 184 } 185 186 /* 187 * Called by main() when ufs is going to be mounted as root. 188 */ 189 int 190 lfs_mountroot() 191 { 192 extern struct vnode *rootvp; 193 struct mount *mp; 194 struct proc *p = curproc; /* XXX */ 195 int error; 196 197 if (root_device->dv_class != DV_DISK) 198 return (ENODEV); 199 200 if (rootdev == NODEV) 201 return (ENODEV); 202 /* 203 * Get vnodes for swapdev and rootdev. 204 */ 205 if ((error = bdevvp(rootdev, &rootvp))) { 206 printf("lfs_mountroot: can't setup bdevvp's"); 207 return (error); 208 } 209 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) { 210 vrele(rootvp); 211 return (error); 212 } 213 if ((error = lfs_mountfs(rootvp, mp, p))) { 214 mp->mnt_op->vfs_refcount--; 215 vfs_unbusy(mp); 216 free(mp, M_MOUNT); 217 vrele(rootvp); 218 return (error); 219 } 220 simple_lock(&mountlist_slock); 221 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 222 simple_unlock(&mountlist_slock); 223 (void)lfs_statfs(mp, &mp->mnt_stat, p); 224 vfs_unbusy(mp); 225 inittodr(VFSTOUFS(mp)->um_lfs->lfs_tstamp); 226 return (0); 227 } 228 229 /* 230 * VFS Operations. 231 * 232 * mount system call 233 */ 234 int 235 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct proc *p) 236 { 237 struct vnode *devvp; 238 struct ufs_args args; 239 struct ufsmount *ump = NULL; 240 struct lfs *fs = NULL; /* LFS */ 241 size_t size; 242 int error; 243 mode_t accessmode; 244 245 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)); 246 if (error) 247 return (error); 248 249 #if 0 250 /* Until LFS can do NFS right. XXX */ 251 if (args.export.ex_flags & MNT_EXPORTED) 252 return (EINVAL); 253 #endif 254 255 /* 256 * If updating, check whether changing from read-only to 257 * read/write; if there is no device name, that's all we do. 258 */ 259 if (mp->mnt_flag & MNT_UPDATE) { 260 ump = VFSTOUFS(mp); 261 fs = ump->um_lfs; 262 if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) { 263 /* 264 * If upgrade to read-write by non-root, then verify 265 * that user has necessary permissions on the device. 266 */ 267 if (p->p_ucred->cr_uid != 0) { 268 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 269 error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE, 270 p->p_ucred, p); 271 VOP_UNLOCK(ump->um_devvp, 0); 272 if (error) 273 return (error); 274 } 275 fs->lfs_ronly = 0; 276 } 277 if (args.fspec == 0) { 278 /* 279 * Process export requests. 280 */ 281 return (vfs_export(mp, &ump->um_export, &args.export)); 282 } 283 } 284 /* 285 * Not an update, or updating the name: look up the name 286 * and verify that it refers to a sensible block device. 287 */ 288 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 289 if ((error = namei(ndp)) != 0) 290 return (error); 291 devvp = ndp->ni_vp; 292 if (devvp->v_type != VBLK) { 293 vrele(devvp); 294 return (ENOTBLK); 295 } 296 if (major(devvp->v_rdev) >= nblkdev) { 297 vrele(devvp); 298 return (ENXIO); 299 } 300 /* 301 * If mount by non-root, then verify that user has necessary 302 * permissions on the device. 303 */ 304 if (p->p_ucred->cr_uid != 0) { 305 accessmode = VREAD; 306 if ((mp->mnt_flag & MNT_RDONLY) == 0) 307 accessmode |= VWRITE; 308 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 309 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p); 310 if (error) { 311 vput(devvp); 312 return (error); 313 } 314 VOP_UNLOCK(devvp, 0); 315 } 316 if ((mp->mnt_flag & MNT_UPDATE) == 0) 317 error = lfs_mountfs(devvp, mp, p); /* LFS */ 318 else { 319 if (devvp != ump->um_devvp) 320 error = EINVAL; /* needs translation */ 321 else 322 vrele(devvp); 323 } 324 if (error) { 325 vrele(devvp); 326 return (error); 327 } 328 ump = VFSTOUFS(mp); 329 fs = ump->um_lfs; /* LFS */ 330 (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size); 331 bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size); 332 bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN); 333 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 334 &size); 335 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 336 return (0); 337 } 338 339 /* 340 * Roll-forward code. 341 */ 342 343 /* 344 * Load the appropriate indirect block, and change the appropriate pointer. 345 * Mark the block dirty. Do segment and avail accounting. 346 */ 347 static int 348 update_meta(struct lfs *fs, ino_t ino, int version, ufs_daddr_t lbn, 349 daddr_t ndaddr, size_t size, struct proc *p) 350 { 351 int error; 352 struct vnode *vp; 353 struct inode *ip; 354 daddr_t odaddr, ooff; 355 struct indir a[NIADDR], *ap; 356 struct buf *bp; 357 SEGUSE *sup; 358 int num; 359 360 if ((error = lfs_rf_valloc(fs, ino, version, p, &vp)) != 0) { 361 #ifdef DEBUG_LFS_RFW 362 printf("update_meta: ino %d: lfs_rf_valloc returned %d\n", ino, 363 error); 364 #endif 365 return error; 366 } 367 368 if ((error = VOP_BALLOC(vp, (lbn << fs->lfs_bshift), size, 369 NOCRED, 0, &bp)) != 0) { 370 vput(vp); 371 return (error); 372 } 373 /* No need to write, the block is already on disk */ 374 if (bp->b_flags & B_DELWRI) { 375 LFS_UNLOCK_BUF(bp); 376 fs->lfs_avail += btofsb(fs, bp->b_bcount); 377 } 378 bp->b_flags |= B_INVAL; 379 brelse(bp); 380 381 /* 382 * Extend the file, if it is not large enough already. 383 * XXX this is not exactly right, we don't know how much of the 384 * XXX last block is actually used. We hope that an inode will 385 * XXX appear later to give the correct size. 386 */ 387 ip = VTOI(vp); 388 if (ip->i_ffs_size <= (lbn << fs->lfs_bshift)) { 389 if (lbn < NDADDR) 390 ip->i_ffs_size = (lbn << fs->lfs_bshift) + 391 (size - fs->lfs_fsize) + 1; 392 else 393 ip->i_ffs_size = (lbn << fs->lfs_bshift) + 1; 394 } 395 396 error = ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL); 397 if (error) { 398 #ifdef DEBUG_LFS_RFW 399 printf("update_meta: ufs_bmaparray returned %d\n", error); 400 #endif 401 vput(vp); 402 return error; 403 } 404 switch (num) { 405 case 0: 406 ooff = ip->i_ffs_db[lbn]; 407 if (ooff == UNWRITTEN) 408 ip->i_ffs_blocks += btofsb(fs, size); 409 ip->i_ffs_db[lbn] = ndaddr; 410 break; 411 case 1: 412 ooff = ip->i_ffs_ib[a[0].in_off]; 413 if (ooff == UNWRITTEN) 414 ip->i_ffs_blocks += btofsb(fs, size); 415 ip->i_ffs_ib[a[0].in_off] = ndaddr; 416 break; 417 default: 418 ap = &a[num - 1]; 419 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 420 panic("update_meta: bread bno %d", ap->in_lbn); 421 422 ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off]; 423 if (ooff == UNWRITTEN) 424 ip->i_ffs_blocks += btofsb(fs, size); 425 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = ndaddr; 426 (void) VOP_BWRITE(bp); 427 } 428 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE); 429 430 /* Update segment usage information. */ 431 if (odaddr > 0) { 432 LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, odaddr)), bp); 433 #ifdef DIAGNOSTIC 434 if (sup->su_nbytes < size) { 435 panic("update_meta: negative bytes " 436 "(segment %d short by %ld)\n", 437 dtosn(fs, dbtofsb(fs, odaddr)), (long)size - sup->su_nbytes); 438 sup->su_nbytes = size; 439 } 440 #endif 441 sup->su_nbytes -= size; 442 LFS_BWRITE_LOG(bp); 443 } 444 LFS_SEGENTRY(sup, fs, dtosn(fs, ndaddr), bp); 445 sup->su_nbytes += size; 446 LFS_BWRITE_LOG(bp); 447 448 /* Fix this so it can be released */ 449 /* ip->i_lfs_effnblks = ip->i_ffs_blocks; */ 450 451 #ifdef DEBUG_LFS_RFW 452 /* Now look again to make sure it worked */ 453 ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL ); 454 if (dbtofsb(fs, odaddr) != ndaddr) 455 printf("update_meta: failed setting ino %d lbn %d to %x\n", 456 ino, lbn, ndaddr); 457 #endif 458 vput(vp); 459 return 0; 460 } 461 462 static int 463 update_inoblk(struct lfs *fs, daddr_t offset, struct ucred *cred, 464 struct proc *p) 465 { 466 struct vnode *devvp, *vp; 467 struct inode *ip; 468 struct dinode *dip; 469 struct buf *dbp, *ibp; 470 int error; 471 daddr_t daddr; 472 IFILE *ifp; 473 SEGUSE *sup; 474 475 devvp = VTOI(fs->lfs_ivnode)->i_devvp; 476 477 /* 478 * Get the inode, update times and perms. 479 * DO NOT update disk blocks, we do that separately. 480 */ 481 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_ibsize, cred, &dbp); 482 if (error) { 483 #ifdef DEBUG_LFS_RFW 484 printf("update_inoblk: bread returned %d\n", error); 485 #endif 486 return error; 487 } 488 dip = ((struct dinode *)(dbp->b_data)) + INOPB(fs); 489 while (--dip >= (struct dinode *)dbp->b_data) { 490 if (dip->di_inumber > LFS_IFILE_INUM) { 491 /* printf("ino %d version %d\n", dip->di_inumber, 492 dip->di_gen); */ 493 error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen, 494 p, &vp); 495 if (error) { 496 #ifdef DEBUG_LFS_RFW 497 printf("update_inoblk: lfs_rf_valloc returned %d\n", error); 498 #endif 499 continue; 500 } 501 ip = VTOI(vp); 502 if (dip->di_size != ip->i_ffs_size) 503 VOP_TRUNCATE(vp, dip->di_size, 0, NOCRED, p); 504 /* Get mode, link count, size, and times */ 505 memcpy(&ip->i_din.ffs_din, dip, 506 offsetof(struct dinode, di_db[0])); 507 508 /* Then the rest, except di_blocks */ 509 ip->i_ffs_flags = dip->di_flags; 510 ip->i_ffs_gen = dip->di_gen; 511 ip->i_ffs_uid = dip->di_uid; 512 ip->i_ffs_gid = dip->di_gid; 513 514 ip->i_ffs_effnlink = dip->di_nlink; 515 516 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE); 517 518 /* Re-initialize to get type right */ 519 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, 520 &vp); 521 vput(vp); 522 523 /* Record change in location */ 524 LFS_IENTRY(ifp, fs, dip->di_inumber, ibp); 525 daddr = ifp->if_daddr; 526 ifp->if_daddr = dbtofsb(fs, dbp->b_blkno); 527 error = LFS_BWRITE_LOG(ibp); /* Ifile */ 528 /* And do segment accounting */ 529 if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) { 530 if (daddr > 0) { 531 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr), 532 ibp); 533 sup->su_nbytes -= DINODE_SIZE; 534 LFS_BWRITE_LOG(ibp); 535 } 536 LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)), 537 ibp); 538 sup->su_nbytes += DINODE_SIZE; 539 LFS_BWRITE_LOG(ibp); 540 } 541 } 542 } 543 dbp->b_flags |= B_AGE; 544 brelse(dbp); 545 546 return 0; 547 } 548 549 #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */ 550 #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */ 551 552 static daddr_t 553 check_segsum(struct lfs *fs, daddr_t offset, 554 struct ucred *cred, int flags, int *pseg_flags, struct proc *p) 555 { 556 struct vnode *devvp; 557 struct buf *bp, *dbp; 558 int error, nblocks, ninos, i, j; 559 SEGSUM *ssp; 560 u_long *dp, *datap; /* XXX u_int32_t */ 561 daddr_t *iaddr, oldoffset; 562 FINFO *fip; 563 SEGUSE *sup; 564 size_t size; 565 u_int64_t serial; 566 567 devvp = VTOI(fs->lfs_ivnode)->i_devvp; 568 /* 569 * If the segment has a superblock and we're at the top 570 * of the segment, skip the superblock. 571 */ 572 if (sntod(fs, dtosn(fs, offset)) == offset) { 573 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp); 574 if (sup->su_flags & SEGUSE_SUPERBLOCK) 575 offset += btofsb(fs, LFS_SBPAD); 576 brelse(bp); 577 } 578 579 /* Read in the segment summary */ 580 error = bread(devvp, offset, fs->lfs_sumsize, cred, &bp); 581 if (error) 582 return -1; 583 584 /* Check summary checksum */ 585 ssp = (SEGSUM *)bp->b_data; 586 if (flags & CHECK_CKSUM) { 587 if (ssp->ss_sumsum != cksum(&ssp->ss_datasum, 588 fs->lfs_sumsize - 589 sizeof(ssp->ss_sumsum))) { 590 #ifdef DEBUG_LFS_RFW 591 printf("Sumsum error at 0x%x\n", offset); 592 #endif 593 offset = -1; 594 goto err1; 595 } 596 if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) { 597 #ifdef DEBUG_LFS_RFW 598 printf("Empty pseg at 0x%x\n", offset); 599 #endif 600 offset = -1; 601 goto err1; 602 } 603 if (ssp->ss_create < fs->lfs_tstamp) { 604 #ifdef DEBUG_LFS_RFW 605 printf("Old data at 0x%x\n", offset); 606 #endif 607 offset = -1; 608 goto err1; 609 } 610 } 611 if (fs->lfs_version > 1) { 612 serial = ssp->ss_serial; 613 if (serial != fs->lfs_serial + 1) { 614 #ifdef DEBUG_LFS_RFW 615 printf("Unexpected serial number at 0x%x\n", offset); 616 #endif 617 offset = -1; 618 goto err1; 619 } 620 if (ssp->ss_ident != fs->lfs_ident) { 621 #ifdef DEBUG_LFS_RFW 622 printf("Incorrect fsid (0x%x vs 0x%x) at 0x%x\n", 623 ssp->ss_ident, fs->lfs_ident, offset); 624 #endif 625 offset = -1; 626 goto err1; 627 } 628 } 629 if (pseg_flags) 630 *pseg_flags = ssp->ss_flags; 631 oldoffset = offset; 632 offset += btofsb(fs, fs->lfs_sumsize); 633 634 ninos = howmany(ssp->ss_ninos, INOPB(fs)); 635 iaddr = (daddr_t *)(bp->b_data + fs->lfs_sumsize - sizeof(daddr_t)); 636 if (flags & CHECK_CKSUM) { 637 /* Count blocks */ 638 nblocks = 0; 639 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs)); 640 for (i = 0; i < ssp->ss_nfinfo; ++i) { 641 nblocks += fip->fi_nblocks; 642 if (fip->fi_nblocks <= 0) 643 break; 644 fip = (FINFO *)(((char *)fip) + sizeof(FINFO) + 645 (fip->fi_nblocks - 1) * 646 sizeof(ufs_daddr_t)); 647 } 648 nblocks += ninos; 649 /* Create the sum array */ 650 datap = dp = (u_long *)malloc(nblocks * sizeof(u_long), 651 M_SEGMENT, M_WAITOK); 652 } 653 654 /* Handle individual blocks */ 655 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs)); 656 for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) { 657 /* Inode block? */ 658 if (ninos && *iaddr == offset) { 659 if (flags & CHECK_CKSUM) { 660 /* Read in the head and add to the buffer */ 661 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize, 662 cred, &dbp); 663 if (error) { 664 offset = -1; 665 goto err2; 666 } 667 (*dp++) = ((u_long *)(dbp->b_data))[0]; 668 dbp->b_flags |= B_AGE; 669 brelse(dbp); 670 } 671 if (flags & CHECK_UPDATE) { 672 if ((error = update_inoblk(fs, offset, cred, p)) 673 != 0) { 674 offset = -1; 675 goto err2; 676 } 677 } 678 offset += btofsb(fs, fs->lfs_ibsize); 679 --iaddr; 680 --ninos; 681 --i; /* compensate */ 682 continue; 683 } 684 /* printf("check: blocks from ino %d version %d\n", 685 fip->fi_ino, fip->fi_version); */ 686 size = fs->lfs_bsize; 687 for (j = 0; j < fip->fi_nblocks; ++j) { 688 if (j == fip->fi_nblocks - 1) 689 size = fip->fi_lastlength; 690 if (flags & CHECK_CKSUM) { 691 error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp); 692 if (error) { 693 offset = -1; 694 goto err2; 695 } 696 (*dp++) = ((u_long *)(dbp->b_data))[0]; 697 dbp->b_flags |= B_AGE; 698 brelse(dbp); 699 } 700 /* Account for and update any direct blocks */ 701 if ((flags & CHECK_UPDATE) && 702 fip->fi_ino > LFS_IFILE_INUM && 703 fip->fi_blocks[j] >= 0) { 704 update_meta(fs, fip->fi_ino, fip->fi_version, 705 fip->fi_blocks[j], offset, size, p); 706 } 707 offset += btofsb(fs, size); 708 } 709 fip = (FINFO *)(((char *)fip) + sizeof(FINFO) 710 + (fip->fi_nblocks - 1) * sizeof(ufs_daddr_t)); 711 } 712 /* Checksum the array, compare */ 713 if ((flags & CHECK_CKSUM) && 714 ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long))) 715 { 716 #ifdef DEBUG_LFS_RFW 717 printf("Datasum error at 0x%x (wanted %x got %x)\n", offset, 718 ssp->ss_datasum, cksum(datap, nblocks * 719 sizeof(u_long))); 720 #endif 721 offset = -1; 722 goto err2; 723 } 724 725 /* If we're at the end of the segment, move to the next */ 726 if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) != 727 dtosn(fs, offset)) { 728 if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) { 729 offset = -1; 730 goto err2; 731 } 732 offset = ssp->ss_next; 733 #ifdef DEBUG_LFS_RFW 734 printf("LFS roll forward: moving on to offset 0x%x " 735 " -> segment %d\n", offset, dtosn(fs,offset)); 736 #endif 737 } 738 739 if (flags & CHECK_UPDATE) { 740 fs->lfs_avail -= (offset - oldoffset); 741 /* Don't clog the buffer queue */ 742 if (locked_queue_count > LFS_MAX_BUFS || 743 locked_queue_bytes > LFS_MAX_BYTES) { 744 ++fs->lfs_writer; 745 lfs_flush(fs, SEGM_CKP); 746 if (--fs->lfs_writer == 0) 747 wakeup(&fs->lfs_dirops); 748 } 749 } 750 751 err2: 752 if (flags & CHECK_CKSUM) 753 free(datap, M_SEGMENT); 754 err1: 755 bp->b_flags |= B_AGE; 756 brelse(bp); 757 758 /* XXX should we update the serial number even for bad psegs? */ 759 if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1) 760 fs->lfs_serial = serial; 761 return offset; 762 } 763 764 /* 765 * Common code for mount and mountroot 766 * LFS specific 767 */ 768 int 769 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p) 770 { 771 extern struct vnode *rootvp; 772 struct dlfs *tdfs, *dfs, *adfs; 773 struct lfs *fs; 774 struct ufsmount *ump; 775 struct vnode *vp; 776 struct buf *bp, *abp; 777 struct partinfo dpart; 778 dev_t dev; 779 int error, i, ronly, secsize, fsbsize; 780 struct ucred *cred; 781 CLEANERINFO *cip; 782 SEGUSE *sup; 783 int flags, dirty, do_rollforward; 784 daddr_t offset, oldoffset, lastgoodpseg, sb_addr; 785 int sn, curseg; 786 787 cred = p ? p->p_ucred : NOCRED; 788 /* 789 * Disallow multiple mounts of the same device. 790 * Disallow mounting of a device that is currently in use 791 * (except for root, which might share swap device for miniroot). 792 * Flush out any old buffers remaining from a previous use. 793 */ 794 if ((error = vfs_mountedon(devvp)) != 0) 795 return (error); 796 if (vcount(devvp) > 1 && devvp != rootvp) 797 return (EBUSY); 798 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0) 799 return (error); 800 801 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 802 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 803 if (error) 804 return (error); 805 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0) 806 secsize = DEV_BSIZE; 807 else 808 secsize = dpart.disklab->d_secsize; 809 810 /* Don't free random space on error. */ 811 bp = NULL; 812 abp = NULL; 813 ump = NULL; 814 815 sb_addr = LFS_LABELPAD / secsize; 816 while (1) { 817 /* Read in the superblock. */ 818 error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp); 819 if (error) 820 goto out; 821 dfs = (struct dlfs *)bp->b_data; 822 823 /* Check the basics. */ 824 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize >= MAXBSIZE || 825 dfs->dlfs_version > LFS_VERSION || 826 dfs->dlfs_bsize < sizeof(struct dlfs)) { 827 #ifdef DEBUG_LFS 828 printf("lfs_mountfs: primary superblock sanity failed\n"); 829 #endif 830 error = EINVAL; /* XXX needs translation */ 831 goto out; 832 } 833 if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT) 834 printf("lfs_mountfs: warning: unknown inode format %d\n", 835 dfs->dlfs_inodefmt); 836 837 if (dfs->dlfs_version == 1) 838 fsbsize = secsize; 839 else { 840 fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb + 841 dfs->dlfs_fsbtodb); 842 /* 843 * Could be, if the frag size is large enough, that we 844 * don't have the "real" primary superblock. If that's 845 * the case, get the real one, and try again. 846 */ 847 if (sb_addr != dfs->dlfs_sboffs[0] << 848 dfs->dlfs_fsbtodb) { 849 /* #ifdef DEBUG_LFS */ 850 printf("lfs_mountfs: sb daddr 0x%x is not right, trying 0x%x\n", 851 sb_addr, dfs->dlfs_sboffs[0] << 852 dfs->dlfs_fsbtodb); 853 /* #endif */ 854 sb_addr = dfs->dlfs_sboffs[0] << 855 dfs->dlfs_fsbtodb; 856 brelse(bp); 857 continue; 858 } 859 } 860 break; 861 } 862 863 /* 864 * Check the second superblock to see which is newer; then mount 865 * using the older of the two. This is necessary to ensure that 866 * the filesystem is valid if it was not unmounted cleanly. 867 */ 868 869 if (dfs->dlfs_sboffs[1] && 870 dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize) 871 { 872 error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize), 873 LFS_SBPAD, cred, &abp); 874 if (error) 875 goto out; 876 adfs = (struct dlfs *)abp->b_data; 877 878 if (dfs->dlfs_version == 1) { 879 /* 1s resolution comparison */ 880 if (adfs->dlfs_tstamp < dfs->dlfs_tstamp) 881 tdfs = adfs; 882 else 883 tdfs = dfs; 884 } else { 885 /* monotonic infinite-resolution comparison */ 886 if (adfs->dlfs_serial < dfs->dlfs_serial) 887 tdfs = adfs; 888 else 889 tdfs = dfs; 890 } 891 892 /* Check the basics. */ 893 if (tdfs->dlfs_magic != LFS_MAGIC || 894 tdfs->dlfs_bsize > MAXBSIZE || 895 tdfs->dlfs_version > LFS_VERSION || 896 tdfs->dlfs_bsize < sizeof(struct dlfs)) { 897 #ifdef DEBUG_LFS 898 printf("lfs_mountfs: alt superblock sanity failed\n"); 899 #endif 900 error = EINVAL; /* XXX needs translation */ 901 goto out; 902 } 903 } else { 904 #ifdef DEBUG_LFS 905 printf("lfs_mountfs: invalid alt superblock daddr=0x%x\n", 906 dfs->dlfs_sboffs[1]); 907 #endif 908 error = EINVAL; 909 goto out; 910 } 911 912 /* Allocate the mount structure, copy the superblock into it. */ 913 fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK); 914 memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs)); 915 916 /* Compatibility */ 917 if (fs->lfs_version < 2) { 918 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE; 919 fs->lfs_ibsize = fs->lfs_bsize; 920 fs->lfs_start = fs->lfs_sboffs[0]; 921 fs->lfs_tstamp = fs->lfs_otstamp; 922 fs->lfs_fsbtodb = 0; 923 } 924 925 /* Before rolling forward, lock so vget will sleep for other procs */ 926 fs->lfs_flags = LFS_NOTYET; 927 fs->lfs_rfpid = p->p_pid; 928 929 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 930 memset((caddr_t)ump, 0, sizeof *ump); 931 ump->um_lfs = fs; 932 if (sizeof(struct lfs) < LFS_SBPAD) { /* XXX why? */ 933 bp->b_flags |= B_INVAL; 934 abp->b_flags |= B_INVAL; 935 } 936 brelse(bp); 937 bp = NULL; 938 brelse(abp); 939 abp = NULL; 940 941 /* Set up the I/O information */ 942 fs->lfs_devbsize = secsize; 943 fs->lfs_iocount = 0; 944 fs->lfs_diropwait = 0; 945 fs->lfs_activesb = 0; 946 fs->lfs_uinodes = 0; 947 fs->lfs_ravail = 0; 948 fs->lfs_sbactive = 0; 949 950 /* Set up the ifile and lock aflags */ 951 fs->lfs_doifile = 0; 952 fs->lfs_writer = 0; 953 fs->lfs_dirops = 0; 954 fs->lfs_nadirop = 0; 955 fs->lfs_seglock = 0; 956 lockinit(&fs->lfs_freelock, PINOD, "lfs_freelock", 0, 0); 957 958 /* Set the file system readonly/modify bits. */ 959 fs->lfs_ronly = ronly; 960 if (ronly == 0) 961 fs->lfs_fmod = 1; 962 963 /* Initialize the mount structure. */ 964 dev = devvp->v_rdev; 965 mp->mnt_data = (qaddr_t)ump; 966 mp->mnt_stat.f_fsid.val[0] = (long)dev; 967 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS); 968 mp->mnt_stat.f_iosize = fs->lfs_bsize; 969 mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen; 970 mp->mnt_flag |= MNT_LOCAL; 971 ump->um_flags = 0; 972 ump->um_mountp = mp; 973 ump->um_dev = dev; 974 ump->um_devvp = devvp; 975 ump->um_bptrtodb = fs->lfs_fsbtodb; 976 ump->um_seqinc = fragstofsb(fs, fs->lfs_frag); 977 ump->um_nindir = fs->lfs_nindir; 978 ump->um_lognindir = ffs(fs->lfs_nindir) - 1; 979 for (i = 0; i < MAXQUOTAS; i++) 980 ump->um_quotas[i] = NULLVP; 981 devvp->v_specmountpoint = mp; 982 983 /* 984 * We use the ifile vnode for almost every operation. Instead of 985 * retrieving it from the hash table each time we retrieve it here, 986 * artificially increment the reference count and keep a pointer 987 * to it in the incore copy of the superblock. 988 */ 989 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) { 990 #ifdef DEBUG 991 printf("lfs_mountfs: ifile vget failed, error=%d\n", error); 992 #endif 993 goto out; 994 } 995 fs->lfs_ivnode = vp; 996 VREF(vp); 997 998 /* 999 * Roll forward. 1000 * 1001 * We don't automatically roll forward for v1 filesystems, because 1002 * of the danger that the clock was turned back between the last 1003 * checkpoint and crash. This would roll forward garbage. 1004 * 1005 * v2 filesystems don't have this problem because they use a 1006 * monotonically increasing serial number instead of a timestamp. 1007 */ 1008 #ifdef LFS_DO_ROLLFORWARD 1009 do_rollforward = !fs->lfs_ronly; 1010 #else 1011 do_rollforward = (fs->lfs_version > 1 && !fs->lfs_ronly && 1012 !(fs->lfs_pflags & LFS_PF_CLEAN)); 1013 #endif 1014 if (do_rollforward) { 1015 /* 1016 * Phase I: Find the address of the last good partial 1017 * segment that was written after the checkpoint. Mark 1018 * the segments in question dirty, so they won't be 1019 * reallocated. 1020 */ 1021 lastgoodpseg = oldoffset = offset = fs->lfs_offset; 1022 flags = 0x0; 1023 #ifdef DEBUG_LFS_RFW 1024 printf("LFS roll forward phase 1: starting at offset 0x%x\n", 1025 offset); 1026 #endif 1027 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp); 1028 if (!(sup->su_flags & SEGUSE_DIRTY)) 1029 --fs->lfs_nclean; 1030 sup->su_flags |= SEGUSE_DIRTY; 1031 (void) LFS_BWRITE_LOG(bp); 1032 while ((offset = check_segsum(fs, offset, cred, CHECK_CKSUM, 1033 &flags, p)) > 0) 1034 { 1035 if (sntod(fs, oldoffset) != sntod(fs, offset)) { 1036 LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset), 1037 bp); 1038 if (!(sup->su_flags & SEGUSE_DIRTY)) 1039 --fs->lfs_nclean; 1040 sup->su_flags |= SEGUSE_DIRTY; 1041 (void) LFS_BWRITE_LOG(bp); 1042 } 1043 1044 #ifdef DEBUG_LFS_RFW 1045 printf("LFS roll forward phase 1: offset=0x%x\n", 1046 offset); 1047 if (flags & SS_DIROP) { 1048 printf("lfs_mountfs: dirops at 0x%x\n", 1049 oldoffset); 1050 if (!(flags & SS_CONT)) 1051 printf("lfs_mountfs: dirops end " 1052 "at 0x%x\n", oldoffset); 1053 } 1054 #endif 1055 if (!(flags & SS_CONT)) 1056 lastgoodpseg = offset; 1057 oldoffset = offset; 1058 } 1059 #ifdef DEBUG_LFS_RFW 1060 if (flags & SS_CONT) { 1061 printf("LFS roll forward: warning: incomplete " 1062 "dirops discarded\n"); 1063 } 1064 printf("LFS roll forward phase 1: completed: " 1065 "lastgoodpseg=0x%x\n", lastgoodpseg); 1066 #endif 1067 oldoffset = fs->lfs_offset; 1068 if (fs->lfs_offset != lastgoodpseg) { 1069 /* Don't overwrite what we're trying to preserve */ 1070 offset = fs->lfs_offset; 1071 fs->lfs_offset = lastgoodpseg; 1072 fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset)); 1073 for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) { 1074 sn = (sn + 1) % fs->lfs_nseg; 1075 if (sn == curseg) 1076 panic("lfs_mountfs: no clean segments"); 1077 LFS_SEGENTRY(sup, fs, sn, bp); 1078 dirty = (sup->su_flags & SEGUSE_DIRTY); 1079 brelse(bp); 1080 if (!dirty) 1081 break; 1082 } 1083 fs->lfs_nextseg = sntod(fs, sn); 1084 1085 /* 1086 * Phase II: Roll forward from the first superblock. 1087 */ 1088 while (offset != lastgoodpseg) { 1089 #ifdef DEBUG_LFS_RFW 1090 printf("LFS roll forward phase 2: 0x%x\n", 1091 offset); 1092 #endif 1093 offset = check_segsum(fs, offset, cred, 1094 CHECK_UPDATE, NULL, p); 1095 } 1096 1097 /* 1098 * Finish: flush our changes to disk. 1099 */ 1100 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC); 1101 printf("lfs_mountfs: roll forward recovered %d blocks\n", 1102 lastgoodpseg - oldoffset); 1103 } 1104 #ifdef DEBUG_LFS_RFW 1105 printf("LFS roll forward complete\n"); 1106 #endif 1107 } 1108 /* If writing, sb is not clean; record in case of immediate crash */ 1109 if (!fs->lfs_ronly) { 1110 fs->lfs_pflags &= ~LFS_PF_CLEAN; 1111 lfs_writesuper(fs, fs->lfs_sboffs[0]); 1112 } 1113 1114 /* Allow vget now that roll-forward is complete */ 1115 fs->lfs_flags &= ~(LFS_NOTYET); 1116 wakeup(&fs->lfs_flags); 1117 1118 /* 1119 * Initialize the ifile cleaner info with information from 1120 * the superblock. 1121 */ 1122 LFS_CLEANERINFO(cip, fs, bp); 1123 cip->clean = fs->lfs_nclean; 1124 cip->dirty = fs->lfs_nseg - fs->lfs_nclean; 1125 cip->avail = fs->lfs_avail; 1126 cip->bfree = fs->lfs_bfree; 1127 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 1128 1129 /* 1130 * Mark the current segment as ACTIVE, since we're going to 1131 * be writing to it. 1132 */ 1133 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp); 1134 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE; 1135 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 1136 1137 /* Now that roll-forward is done, unlock the Ifile */ 1138 vput(vp); 1139 1140 /* Comment on ifile size if it is too large */ 1141 if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS) { 1142 fs->lfs_flags |= LFS_WARNED; 1143 printf("lfs_mountfs: please consider increasing NBUF to at least %lld\n", 1144 (long long)(fs->lfs_ivnode->v_size / fs->lfs_bsize) * (nbuf / LFS_MAX_BUFS)); 1145 } 1146 if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES) { 1147 fs->lfs_flags |= LFS_WARNED; 1148 printf("lfs_mountfs: please consider increasing BUFPAGES to at least %lld\n", 1149 (long long)fs->lfs_ivnode->v_size * bufpages / LFS_MAX_BYTES); 1150 } 1151 1152 return (0); 1153 out: 1154 if (bp) 1155 brelse(bp); 1156 if (abp) 1157 brelse(abp); 1158 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1159 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p); 1160 VOP_UNLOCK(devvp, 0); 1161 if (ump) { 1162 free(ump->um_lfs, M_UFSMNT); 1163 free(ump, M_UFSMNT); 1164 mp->mnt_data = (qaddr_t)0; 1165 } 1166 return (error); 1167 } 1168 1169 /* 1170 * unmount system call 1171 */ 1172 int 1173 lfs_unmount(struct mount *mp, int mntflags, struct proc *p) 1174 { 1175 struct ufsmount *ump; 1176 struct lfs *fs; 1177 int error, flags, ronly, s; 1178 1179 flags = 0; 1180 if (mntflags & MNT_FORCE) 1181 flags |= FORCECLOSE; 1182 1183 ump = VFSTOUFS(mp); 1184 fs = ump->um_lfs; 1185 #ifdef QUOTA 1186 if (mp->mnt_flag & MNT_QUOTA) { 1187 int i; 1188 error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags); 1189 if (error) 1190 return (error); 1191 for (i = 0; i < MAXQUOTAS; i++) { 1192 if (ump->um_quotas[i] == NULLVP) 1193 continue; 1194 quotaoff(p, mp, i); 1195 } 1196 /* 1197 * Here we fall through to vflush again to ensure 1198 * that we have gotten rid of all the system vnodes. 1199 */ 1200 } 1201 #endif 1202 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0) 1203 return (error); 1204 if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0) 1205 return (error); 1206 if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd)) 1207 panic("lfs_unmount: still dirty blocks on ifile vnode\n"); 1208 1209 /* Explicitly write the superblock, to update serial and pflags */ 1210 fs->lfs_pflags |= LFS_PF_CLEAN; 1211 lfs_writesuper(fs, fs->lfs_sboffs[0]); 1212 lfs_writesuper(fs, fs->lfs_sboffs[1]); 1213 1214 /* Comment on ifile size if it has become too large */ 1215 if (!(fs->lfs_flags & LFS_WARNED)) { 1216 if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS) 1217 printf("lfs_unmount: please consider increasing" 1218 " NBUF to at least %lld\n", 1219 (long long)(fs->lfs_ivnode->v_size / 1220 fs->lfs_bsize) * 1221 (long long)(nbuf / LFS_MAX_BUFS)); 1222 if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES) 1223 printf("lfs_unmount: please consider increasing" 1224 " BUFPAGES to at least %lld\n", 1225 (long long)fs->lfs_ivnode->v_size * 1226 bufpages / LFS_MAX_BYTES); 1227 } 1228 1229 /* Finish with the Ifile, now that we're done with it */ 1230 vrele(fs->lfs_ivnode); 1231 vgone(fs->lfs_ivnode); 1232 1233 /* Wait for superblock writes to complete */ 1234 s = splbio(); 1235 while (fs->lfs_iocount) 1236 tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0); 1237 splx(s); 1238 1239 ronly = !fs->lfs_ronly; 1240 if (ump->um_devvp->v_type != VBAD) 1241 ump->um_devvp->v_specmountpoint = NULL; 1242 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 1243 error = VOP_CLOSE(ump->um_devvp, 1244 ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 1245 vput(ump->um_devvp); 1246 1247 /* XXX KS - wake up the cleaner so it can die */ 1248 wakeup(&fs->lfs_nextseg); 1249 wakeup(&lfs_allclean_wakeup); 1250 1251 free(fs, M_UFSMNT); 1252 free(ump, M_UFSMNT); 1253 mp->mnt_data = (qaddr_t)0; 1254 mp->mnt_flag &= ~MNT_LOCAL; 1255 return (error); 1256 } 1257 1258 /* 1259 * Get file system statistics. 1260 */ 1261 int 1262 lfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 1263 { 1264 struct lfs *fs; 1265 struct ufsmount *ump; 1266 1267 ump = VFSTOUFS(mp); 1268 fs = ump->um_lfs; 1269 if (fs->lfs_magic != LFS_MAGIC) 1270 panic("lfs_statfs: magic"); 1271 1272 sbp->f_type = 0; 1273 sbp->f_bsize = fs->lfs_fsize; 1274 sbp->f_iosize = fs->lfs_bsize; 1275 sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs)); 1276 sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs)); 1277 sbp->f_bavail = fsbtofrags(fs, (long)LFS_EST_BFREE(fs) - 1278 (long)LFS_EST_RSVD(fs)); 1279 1280 sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs); 1281 sbp->f_ffree = sbp->f_files - fs->lfs_nfiles; 1282 if (sbp != &mp->mnt_stat) { 1283 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 1284 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 1285 } 1286 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN); 1287 return (0); 1288 } 1289 1290 /* 1291 * Go through the disk queues to initiate sandbagged IO; 1292 * go through the inodes to write those that have been modified; 1293 * initiate the writing of the super block if it has been modified. 1294 * 1295 * Note: we are always called with the filesystem marked `MPBUSY'. 1296 */ 1297 int 1298 lfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p) 1299 { 1300 int error; 1301 struct lfs *fs; 1302 1303 fs = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs; 1304 if (fs->lfs_ronly) 1305 return 0; 1306 while (fs->lfs_dirops) 1307 error = tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_dirops", 0); 1308 fs->lfs_writer++; 1309 1310 /* All syncs must be checkpoints until roll-forward is implemented. */ 1311 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0)); 1312 if (--fs->lfs_writer == 0) 1313 wakeup(&fs->lfs_dirops); 1314 #ifdef QUOTA 1315 qsync(mp); 1316 #endif 1317 return (error); 1318 } 1319 1320 extern struct lock ufs_hashlock; 1321 1322 /* 1323 * Look up an LFS dinode number to find its incore vnode. If not already 1324 * in core, read it in from the specified device. Return the inode locked. 1325 * Detection and handling of mount points must be done by the calling routine. 1326 */ 1327 int 1328 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 1329 { 1330 struct lfs *fs; 1331 struct dinode *dip; 1332 struct inode *ip; 1333 struct buf *bp; 1334 struct ifile *ifp; 1335 struct vnode *vp; 1336 struct ufsmount *ump; 1337 ufs_daddr_t daddr; 1338 dev_t dev; 1339 int error, retries; 1340 struct timespec ts; 1341 1342 ump = VFSTOUFS(mp); 1343 dev = ump->um_dev; 1344 fs = ump->um_lfs; 1345 1346 /* 1347 * If the filesystem is not completely mounted yet, suspend 1348 * any access requests (wait for roll-forward to complete). 1349 */ 1350 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid) 1351 tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0); 1352 1353 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) 1354 return (0); 1355 1356 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) { 1357 *vpp = NULL; 1358 return (error); 1359 } 1360 1361 do { 1362 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) { 1363 ungetnewvnode(vp); 1364 return (0); 1365 } 1366 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0)); 1367 1368 /* Translate the inode number to a disk address. */ 1369 if (ino == LFS_IFILE_INUM) 1370 daddr = fs->lfs_idaddr; 1371 else { 1372 /* XXX bounds-check this too */ 1373 LFS_IENTRY(ifp, fs, ino, bp); 1374 daddr = ifp->if_daddr; 1375 if (fs->lfs_version > 1) { 1376 ts.tv_sec = ifp->if_atime_sec; 1377 ts.tv_nsec = ifp->if_atime_nsec; 1378 } 1379 1380 brelse(bp); 1381 if (daddr == LFS_UNUSED_DADDR) { 1382 *vpp = NULLVP; 1383 ungetnewvnode(vp); 1384 lockmgr(&ufs_hashlock, LK_RELEASE, 0); 1385 return (ENOENT); 1386 } 1387 } 1388 1389 /* Allocate/init new vnode/inode. */ 1390 lfs_vcreate(mp, ino, vp); 1391 1392 /* 1393 * Put it onto its hash chain and lock it so that other requests for 1394 * this inode will block if they arrive while we are sleeping waiting 1395 * for old data structures to be purged or for the contents of the 1396 * disk portion of this inode to be read. 1397 */ 1398 ip = VTOI(vp); 1399 ufs_ihashins(ip); 1400 lockmgr(&ufs_hashlock, LK_RELEASE, 0); 1401 1402 /* 1403 * XXX 1404 * This may not need to be here, logically it should go down with 1405 * the i_devvp initialization. 1406 * Ask Kirk. 1407 */ 1408 ip->i_lfs = ump->um_lfs; 1409 1410 /* Read in the disk contents for the inode, copy into the inode. */ 1411 retries = 0; 1412 again: 1413 error = bread(ump->um_devvp, fsbtodb(fs, daddr), 1414 (fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize), 1415 NOCRED, &bp); 1416 if (error) { 1417 /* 1418 * The inode does not contain anything useful, so it would 1419 * be misleading to leave it on its hash chain. With mode 1420 * still zero, it will be unlinked and returned to the free 1421 * list by vput(). 1422 */ 1423 vput(vp); 1424 brelse(bp); 1425 *vpp = NULL; 1426 return (error); 1427 } 1428 1429 dip = lfs_ifind(fs, ino, bp); 1430 if (dip == NULL) { 1431 /* Assume write has not completed yet; try again */ 1432 bp->b_flags |= B_INVAL; 1433 brelse(bp); 1434 ++retries; 1435 if (retries > LFS_IFIND_RETRIES) { 1436 #ifdef DEBUG 1437 /* If the seglock is held look at the bpp to see 1438 what is there anyway */ 1439 if (fs->lfs_seglock > 0) { 1440 struct buf **bpp; 1441 struct dinode *dp; 1442 int i; 1443 1444 for (bpp = fs->lfs_sp->bpp; 1445 bpp != fs->lfs_sp->cbpp; ++bpp) { 1446 if ((*bpp)->b_vp == fs->lfs_ivnode && 1447 bpp != fs->lfs_sp->bpp) { 1448 /* Inode block */ 1449 printf("block 0x%x: ", (*bpp)->b_blkno); 1450 dp = (struct dinode *)(*bpp)->b_data; 1451 for (i = 0; i < INOPB(fs); i++) 1452 if (dp[i].di_u.inumber) 1453 printf("%d ", dp[i].di_u.inumber); 1454 printf("\n"); 1455 } 1456 } 1457 } 1458 #endif 1459 panic("lfs_vget: dinode not found"); 1460 } 1461 printf("lfs_vget: dinode %d not found, retrying...\n", ino); 1462 (void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs ifind", 1); 1463 goto again; 1464 } 1465 ip->i_din.ffs_din = *dip; 1466 1467 ip->i_ffs_effnlink = ip->i_ffs_nlink; 1468 ip->i_lfs_effnblks = ip->i_ffs_blocks; 1469 if (fs->lfs_version > 1) { 1470 ip->i_ffs_atime = ts.tv_sec; 1471 ip->i_ffs_atimensec = ts.tv_nsec; 1472 } 1473 brelse(bp); 1474 1475 /* 1476 * Initialize the vnode from the inode, check for aliases. In all 1477 * cases re-init ip, the underlying vnode/inode may have changed. 1478 */ 1479 ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp); 1480 #ifdef DIAGNOSTIC 1481 if (vp->v_type == VNON) { 1482 panic("lfs_vget: ino %d is type VNON! (ifmt %o)\n", 1483 ip->i_number, (ip->i_ffs_mode & IFMT) >> 12); 1484 } 1485 #endif 1486 /* 1487 * Finish inode initialization now that aliasing has been resolved. 1488 */ 1489 1490 genfs_node_init(vp, &lfs_genfsops); 1491 ip->i_devvp = ump->um_devvp; 1492 VREF(ip->i_devvp); 1493 *vpp = vp; 1494 1495 uvm_vnp_setsize(vp, ip->i_ffs_size); 1496 1497 return (0); 1498 } 1499 1500 /* 1501 * File handle to vnode 1502 * 1503 * Have to be really careful about stale file handles: 1504 * - check that the inode number is valid 1505 * - call lfs_vget() to get the locked inode 1506 * - check for an unallocated inode (i_mode == 0) 1507 * 1508 * XXX 1509 * use ifile to see if inode is allocated instead of reading off disk 1510 * what is the relationship between my generational number and the NFS 1511 * generational number. 1512 */ 1513 int 1514 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 1515 { 1516 struct ufid *ufhp; 1517 1518 ufhp = (struct ufid *)fhp; 1519 if (ufhp->ufid_ino < ROOTINO) 1520 return (ESTALE); 1521 return (ufs_fhtovp(mp, ufhp, vpp)); 1522 } 1523 1524 /* 1525 * Vnode pointer to File handle 1526 */ 1527 /* ARGSUSED */ 1528 int 1529 lfs_vptofh(struct vnode *vp, struct fid *fhp) 1530 { 1531 struct inode *ip; 1532 struct ufid *ufhp; 1533 1534 ip = VTOI(vp); 1535 ufhp = (struct ufid *)fhp; 1536 ufhp->ufid_len = sizeof(struct ufid); 1537 ufhp->ufid_ino = ip->i_number; 1538 ufhp->ufid_gen = ip->i_ffs_gen; 1539 return (0); 1540 } 1541 1542 int 1543 lfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen, struct proc *p) 1544 { 1545 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead; 1546 extern struct lfs_stats lfs_stats; 1547 int error; 1548 1549 /* all sysctl names at this level are terminal */ 1550 if (namelen != 1) 1551 return (ENOTDIR); 1552 1553 switch (name[0]) { 1554 case LFS_WRITEINDIR: 1555 return (sysctl_int(oldp, oldlenp, newp, newlen, 1556 &lfs_writeindir)); 1557 case LFS_CLEAN_VNHEAD: 1558 return (sysctl_int(oldp, oldlenp, newp, newlen, 1559 &lfs_clean_vnhead)); 1560 case LFS_DOSTATS: 1561 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, 1562 &lfs_dostats))) 1563 return error; 1564 if (lfs_dostats == 0) 1565 memset(&lfs_stats,0,sizeof(lfs_stats)); 1566 return 0; 1567 case LFS_STATS: 1568 return (sysctl_rdstruct(oldp, oldlenp, newp, 1569 &lfs_stats, sizeof(lfs_stats))); 1570 default: 1571 return (EOPNOTSUPP); 1572 } 1573 /* NOTREACHED */ 1574 } 1575