1 /*- 2 * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-3-Clause) 3 * 4 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * This software was developed for the FreeBSD Project by Marshall 8 * Kirk McKusick and Network Associates Laboratories, the Security 9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 11 * research program 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Copyright (c) 1982, 1986, 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * from: @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95 62 * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ... 63 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95 64 */ 65 66 #include <sys/cdefs.h> 67 __FBSDID("$FreeBSD$"); 68 69 #include "opt_directio.h" 70 #include "opt_ffs.h" 71 #include "opt_ufs.h" 72 73 #include <sys/param.h> 74 #include <sys/bio.h> 75 #include <sys/systm.h> 76 #include <sys/buf.h> 77 #include <sys/conf.h> 78 #include <sys/extattr.h> 79 #include <sys/kernel.h> 80 #include <sys/limits.h> 81 #include <sys/malloc.h> 82 #include <sys/mount.h> 83 #include <sys/priv.h> 84 #include <sys/rwlock.h> 85 #include <sys/stat.h> 86 #include <sys/sysctl.h> 87 #include <sys/vmmeter.h> 88 #include <sys/vnode.h> 89 90 #include <vm/vm.h> 91 #include <vm/vm_param.h> 92 #include <vm/vm_extern.h> 93 #include <vm/vm_object.h> 94 #include <vm/vm_page.h> 95 #include <vm/vm_pager.h> 96 #include <vm/vnode_pager.h> 97 98 #include <ufs/ufs/extattr.h> 99 #include <ufs/ufs/quota.h> 100 #include <ufs/ufs/inode.h> 101 #include <ufs/ufs/ufs_extern.h> 102 #include <ufs/ufs/ufsmount.h> 103 #include <ufs/ufs/dir.h> 104 #ifdef UFS_DIRHASH 105 #include <ufs/ufs/dirhash.h> 106 #endif 107 108 #include <ufs/ffs/fs.h> 109 #include <ufs/ffs/ffs_extern.h> 110 111 #define ALIGNED_TO(ptr, s) \ 112 (((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0) 113 114 #ifdef DIRECTIO 115 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone); 116 #endif 117 static vop_fdatasync_t ffs_fdatasync; 118 static vop_fsync_t ffs_fsync; 119 static vop_getpages_t ffs_getpages; 120 static vop_getpages_async_t ffs_getpages_async; 121 static vop_lock1_t ffs_lock; 122 #ifdef INVARIANTS 123 static vop_unlock_t ffs_unlock_debug; 124 #endif 125 static vop_read_t ffs_read; 126 static vop_write_t ffs_write; 127 static int ffs_extread(struct vnode *vp, struct uio *uio, int ioflag); 128 static int ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, 129 struct ucred *cred); 130 static vop_strategy_t ffsext_strategy; 131 static vop_closeextattr_t ffs_closeextattr; 132 static vop_deleteextattr_t ffs_deleteextattr; 133 static vop_getextattr_t ffs_getextattr; 134 static vop_listextattr_t ffs_listextattr; 135 static vop_openextattr_t ffs_openextattr; 136 static vop_setextattr_t ffs_setextattr; 137 static vop_vptofh_t ffs_vptofh; 138 static vop_vput_pair_t ffs_vput_pair; 139 140 /* Global vfs data structures for ufs. */ 141 struct vop_vector ffs_vnodeops1 = { 142 .vop_default = &ufs_vnodeops, 143 .vop_fsync = ffs_fsync, 144 .vop_fdatasync = ffs_fdatasync, 145 .vop_getpages = ffs_getpages, 146 .vop_getpages_async = ffs_getpages_async, 147 .vop_lock1 = ffs_lock, 148 #ifdef INVARIANTS 149 .vop_unlock = ffs_unlock_debug, 150 #endif 151 .vop_read = ffs_read, 152 .vop_reallocblks = ffs_reallocblks, 153 .vop_write = ffs_write, 154 .vop_vptofh = ffs_vptofh, 155 .vop_vput_pair = ffs_vput_pair, 156 }; 157 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1); 158 159 struct vop_vector ffs_fifoops1 = { 160 .vop_default = &ufs_fifoops, 161 .vop_fsync = ffs_fsync, 162 .vop_fdatasync = ffs_fdatasync, 163 .vop_lock1 = ffs_lock, 164 #ifdef INVARIANTS 165 .vop_unlock = ffs_unlock_debug, 166 #endif 167 .vop_vptofh = ffs_vptofh, 168 }; 169 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1); 170 171 /* Global vfs data structures for ufs. */ 172 struct vop_vector ffs_vnodeops2 = { 173 .vop_default = &ufs_vnodeops, 174 .vop_fsync = ffs_fsync, 175 .vop_fdatasync = ffs_fdatasync, 176 .vop_getpages = ffs_getpages, 177 .vop_getpages_async = ffs_getpages_async, 178 .vop_lock1 = ffs_lock, 179 #ifdef INVARIANTS 180 .vop_unlock = ffs_unlock_debug, 181 #endif 182 .vop_read = ffs_read, 183 .vop_reallocblks = ffs_reallocblks, 184 .vop_write = ffs_write, 185 .vop_closeextattr = ffs_closeextattr, 186 .vop_deleteextattr = ffs_deleteextattr, 187 .vop_getextattr = ffs_getextattr, 188 .vop_listextattr = ffs_listextattr, 189 .vop_openextattr = ffs_openextattr, 190 .vop_setextattr = ffs_setextattr, 191 .vop_vptofh = ffs_vptofh, 192 .vop_vput_pair = ffs_vput_pair, 193 }; 194 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2); 195 196 struct vop_vector ffs_fifoops2 = { 197 .vop_default = &ufs_fifoops, 198 .vop_fsync = ffs_fsync, 199 .vop_fdatasync = ffs_fdatasync, 200 .vop_lock1 = ffs_lock, 201 #ifdef INVARIANTS 202 .vop_unlock = ffs_unlock_debug, 203 #endif 204 .vop_reallocblks = ffs_reallocblks, 205 .vop_strategy = ffsext_strategy, 206 .vop_closeextattr = ffs_closeextattr, 207 .vop_deleteextattr = ffs_deleteextattr, 208 .vop_getextattr = ffs_getextattr, 209 .vop_listextattr = ffs_listextattr, 210 .vop_openextattr = ffs_openextattr, 211 .vop_setextattr = ffs_setextattr, 212 .vop_vptofh = ffs_vptofh, 213 }; 214 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2); 215 216 /* 217 * Synch an open file. 218 */ 219 /* ARGSUSED */ 220 static int 221 ffs_fsync(struct vop_fsync_args *ap) 222 { 223 struct vnode *vp; 224 struct bufobj *bo; 225 int error; 226 227 vp = ap->a_vp; 228 bo = &vp->v_bufobj; 229 retry: 230 error = ffs_syncvnode(vp, ap->a_waitfor, 0); 231 if (error) 232 return (error); 233 if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) { 234 error = softdep_fsync(vp); 235 if (error) 236 return (error); 237 238 /* 239 * The softdep_fsync() function may drop vp lock, 240 * allowing for dirty buffers to reappear on the 241 * bo_dirty list. Recheck and resync as needed. 242 */ 243 BO_LOCK(bo); 244 if ((vp->v_type == VREG || vp->v_type == VDIR) && 245 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) { 246 BO_UNLOCK(bo); 247 goto retry; 248 } 249 BO_UNLOCK(bo); 250 } 251 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0)) 252 return (ENXIO); 253 return (0); 254 } 255 256 int 257 ffs_syncvnode(struct vnode *vp, int waitfor, int flags) 258 { 259 struct inode *ip; 260 struct bufobj *bo; 261 struct ufsmount *ump; 262 struct buf *bp, *nbp; 263 ufs_lbn_t lbn; 264 int error, passes; 265 bool still_dirty, unlocked, wait; 266 267 ip = VTOI(vp); 268 bo = &vp->v_bufobj; 269 ump = VFSTOUFS(vp->v_mount); 270 271 /* 272 * When doing MNT_WAIT we must first flush all dependencies 273 * on the inode. 274 */ 275 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT && 276 (error = softdep_sync_metadata(vp)) != 0) { 277 if (ffs_fsfail_cleanup(ump, error)) 278 error = 0; 279 return (error); 280 } 281 282 /* 283 * Flush all dirty buffers associated with a vnode. 284 */ 285 error = 0; 286 passes = 0; 287 wait = false; /* Always do an async pass first. */ 288 unlocked = false; 289 lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1)); 290 BO_LOCK(bo); 291 loop: 292 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 293 bp->b_vflags &= ~BV_SCANNED; 294 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 295 /* 296 * Reasons to skip this buffer: it has already been considered 297 * on this pass, the buffer has dependencies that will cause 298 * it to be redirtied and it has not already been deferred, 299 * or it is already being written. 300 */ 301 if ((bp->b_vflags & BV_SCANNED) != 0) 302 continue; 303 bp->b_vflags |= BV_SCANNED; 304 /* 305 * Flush indirects in order, if requested. 306 * 307 * Note that if only datasync is requested, we can 308 * skip indirect blocks when softupdates are not 309 * active. Otherwise we must flush them with data, 310 * since dependencies prevent data block writes. 311 */ 312 if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR && 313 (lbn_level(bp->b_lblkno) >= passes || 314 ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp)))) 315 continue; 316 if (bp->b_lblkno > lbn) 317 panic("ffs_syncvnode: syncing truncated data."); 318 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) { 319 BO_UNLOCK(bo); 320 } else if (wait) { 321 if (BUF_LOCK(bp, 322 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 323 BO_LOCKPTR(bo)) != 0) { 324 bp->b_vflags &= ~BV_SCANNED; 325 goto next; 326 } 327 } else 328 continue; 329 if ((bp->b_flags & B_DELWRI) == 0) 330 panic("ffs_fsync: not dirty"); 331 /* 332 * Check for dependencies and potentially complete them. 333 */ 334 if (!LIST_EMPTY(&bp->b_dep) && 335 (error = softdep_sync_buf(vp, bp, 336 wait ? MNT_WAIT : MNT_NOWAIT)) != 0) { 337 /* 338 * Lock order conflict, buffer was already unlocked, 339 * and vnode possibly unlocked. 340 */ 341 if (error == ERELOOKUP) { 342 if (vp->v_data == NULL) 343 return (EBADF); 344 unlocked = true; 345 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT && 346 (error = softdep_sync_metadata(vp)) != 0) { 347 if (ffs_fsfail_cleanup(ump, error)) 348 error = 0; 349 return (unlocked && error == 0 ? 350 ERELOOKUP : error); 351 } 352 /* Re-evaluate inode size */ 353 lbn = lblkno(ITOFS(ip), (ip->i_size + 354 ITOFS(ip)->fs_bsize - 1)); 355 goto next; 356 } 357 /* I/O error. */ 358 if (error != EBUSY) { 359 BUF_UNLOCK(bp); 360 return (error); 361 } 362 /* If we deferred once, don't defer again. */ 363 if ((bp->b_flags & B_DEFERRED) == 0) { 364 bp->b_flags |= B_DEFERRED; 365 BUF_UNLOCK(bp); 366 goto next; 367 } 368 } 369 if (wait) { 370 bremfree(bp); 371 error = bwrite(bp); 372 if (ffs_fsfail_cleanup(ump, error)) 373 error = 0; 374 if (error != 0) 375 return (error); 376 } else if ((bp->b_flags & B_CLUSTEROK)) { 377 (void) vfs_bio_awrite(bp); 378 } else { 379 bremfree(bp); 380 (void) bawrite(bp); 381 } 382 next: 383 /* 384 * Since we may have slept during the I/O, we need 385 * to start from a known point. 386 */ 387 BO_LOCK(bo); 388 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd); 389 } 390 if (waitfor != MNT_WAIT) { 391 BO_UNLOCK(bo); 392 if ((flags & NO_INO_UPDT) != 0) 393 return (unlocked ? ERELOOKUP : 0); 394 error = ffs_update(vp, 0); 395 if (error == 0 && unlocked) 396 error = ERELOOKUP; 397 return (error); 398 } 399 /* Drain IO to see if we're done. */ 400 bufobj_wwait(bo, 0, 0); 401 /* 402 * Block devices associated with filesystems may have new I/O 403 * requests posted for them even if the vnode is locked, so no 404 * amount of trying will get them clean. We make several passes 405 * as a best effort. 406 * 407 * Regular files may need multiple passes to flush all dependency 408 * work as it is possible that we must write once per indirect 409 * level, once for the leaf, and once for the inode and each of 410 * these will be done with one sync and one async pass. 411 */ 412 if (bo->bo_dirty.bv_cnt > 0) { 413 if ((flags & DATA_ONLY) == 0) { 414 still_dirty = true; 415 } else { 416 /* 417 * For data-only sync, dirty indirect buffers 418 * are ignored. 419 */ 420 still_dirty = false; 421 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 422 if (bp->b_lblkno > -UFS_NDADDR) { 423 still_dirty = true; 424 break; 425 } 426 } 427 } 428 429 if (still_dirty) { 430 /* Write the inode after sync passes to flush deps. */ 431 if (wait && DOINGSOFTDEP(vp) && 432 (flags & NO_INO_UPDT) == 0) { 433 BO_UNLOCK(bo); 434 ffs_update(vp, 1); 435 BO_LOCK(bo); 436 } 437 /* switch between sync/async. */ 438 wait = !wait; 439 if (wait || ++passes < UFS_NIADDR + 2) 440 goto loop; 441 } 442 } 443 BO_UNLOCK(bo); 444 error = 0; 445 if ((flags & DATA_ONLY) == 0) { 446 if ((flags & NO_INO_UPDT) == 0) 447 error = ffs_update(vp, 1); 448 if (DOINGSUJ(vp)) 449 softdep_journal_fsync(VTOI(vp)); 450 } else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) { 451 error = ffs_update(vp, 1); 452 } 453 if (error == 0 && unlocked) 454 error = ERELOOKUP; 455 if (error == 0) 456 ip->i_flag &= ~IN_NEEDSYNC; 457 return (error); 458 } 459 460 static int 461 ffs_fdatasync(struct vop_fdatasync_args *ap) 462 { 463 464 return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY)); 465 } 466 467 static int 468 ffs_lock(ap) 469 struct vop_lock1_args /* { 470 struct vnode *a_vp; 471 int a_flags; 472 char *file; 473 int line; 474 } */ *ap; 475 { 476 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC) 477 struct vnode *vp = ap->a_vp; 478 #endif /* !NO_FFS_SNAPSHOT || DIAGNOSTIC */ 479 #ifdef DIAGNOSTIC 480 struct inode *ip; 481 #endif /* DIAGNOSTIC */ 482 int result; 483 #ifndef NO_FFS_SNAPSHOT 484 int flags; 485 struct lock *lkp; 486 487 /* 488 * Adaptive spinning mixed with SU leads to trouble. use a giant hammer 489 * and only use it when LK_NODDLKTREAT is set. Currently this means it 490 * is only used during path lookup. 491 */ 492 if ((ap->a_flags & LK_NODDLKTREAT) != 0) 493 ap->a_flags |= LK_ADAPTIVE; 494 switch (ap->a_flags & LK_TYPE_MASK) { 495 case LK_SHARED: 496 case LK_UPGRADE: 497 case LK_EXCLUSIVE: 498 flags = ap->a_flags; 499 for (;;) { 500 #ifdef DEBUG_VFS_LOCKS 501 VNPASS(vp->v_holdcnt != 0, vp); 502 #endif /* DEBUG_VFS_LOCKS */ 503 lkp = vp->v_vnlock; 504 result = lockmgr_lock_flags(lkp, flags, 505 &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line); 506 if (lkp == vp->v_vnlock || result != 0) 507 break; 508 /* 509 * Apparent success, except that the vnode 510 * mutated between snapshot file vnode and 511 * regular file vnode while this process 512 * slept. The lock currently held is not the 513 * right lock. Release it, and try to get the 514 * new lock. 515 */ 516 lockmgr_unlock(lkp); 517 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) == 518 (LK_INTERLOCK | LK_NOWAIT)) 519 return (EBUSY); 520 if ((flags & LK_TYPE_MASK) == LK_UPGRADE) 521 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE; 522 flags &= ~LK_INTERLOCK; 523 } 524 #ifdef DIAGNOSTIC 525 switch (ap->a_flags & LK_TYPE_MASK) { 526 case LK_UPGRADE: 527 case LK_EXCLUSIVE: 528 if (result == 0 && vp->v_vnlock->lk_recurse == 0) { 529 ip = VTOI(vp); 530 if (ip != NULL) 531 ip->i_lock_gen++; 532 } 533 } 534 #endif /* DIAGNOSTIC */ 535 break; 536 default: 537 #ifdef DIAGNOSTIC 538 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) { 539 ip = VTOI(vp); 540 if (ip != NULL) 541 ufs_unlock_tracker(ip); 542 } 543 #endif /* DIAGNOSTIC */ 544 result = VOP_LOCK1_APV(&ufs_vnodeops, ap); 545 break; 546 } 547 #else /* NO_FFS_SNAPSHOT */ 548 /* 549 * See above for an explanation. 550 */ 551 if ((ap->a_flags & LK_NODDLKTREAT) != 0) 552 ap->a_flags |= LK_ADAPTIVE; 553 #ifdef DIAGNOSTIC 554 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) { 555 ip = VTOI(vp); 556 if (ip != NULL) 557 ufs_unlock_tracker(ip); 558 } 559 #endif /* DIAGNOSTIC */ 560 result = VOP_LOCK1_APV(&ufs_vnodeops, ap); 561 #endif /* NO_FFS_SNAPSHOT */ 562 #ifdef DIAGNOSTIC 563 switch (ap->a_flags & LK_TYPE_MASK) { 564 case LK_UPGRADE: 565 case LK_EXCLUSIVE: 566 if (result == 0 && vp->v_vnlock->lk_recurse == 0) { 567 ip = VTOI(vp); 568 if (ip != NULL) 569 ip->i_lock_gen++; 570 } 571 } 572 #endif /* DIAGNOSTIC */ 573 return (result); 574 } 575 576 #ifdef INVARIANTS 577 static int 578 ffs_unlock_debug(struct vop_unlock_args *ap) 579 { 580 struct vnode *vp; 581 struct inode *ip; 582 583 vp = ap->a_vp; 584 ip = VTOI(vp); 585 if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) { 586 if ((vp->v_mflag & VMP_LAZYLIST) == 0) { 587 VI_LOCK(vp); 588 VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp, 589 ("%s: modified vnode (%x) not on lazy list", 590 __func__, ip->i_flag)); 591 VI_UNLOCK(vp); 592 } 593 } 594 KASSERT(vp->v_type != VDIR || vp->v_vnlock->lk_recurse != 0 || 595 (ip->i_flag & IN_ENDOFF) == 0, 596 ("ufs dir vp %p ip %p flags %#x", vp, ip, ip->i_flag)); 597 #ifdef DIAGNOSTIC 598 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL && 599 vp->v_vnlock->lk_recurse == 0) 600 ufs_unlock_tracker(ip); 601 #endif 602 return (VOP_UNLOCK_APV(&ufs_vnodeops, ap)); 603 } 604 #endif 605 606 static int 607 ffs_read_hole(struct uio *uio, long xfersize, long *size) 608 { 609 ssize_t saved_resid, tlen; 610 int error; 611 612 while (xfersize > 0) { 613 tlen = min(xfersize, ZERO_REGION_SIZE); 614 saved_resid = uio->uio_resid; 615 error = vn_io_fault_uiomove(__DECONST(void *, zero_region), 616 tlen, uio); 617 if (error != 0) 618 return (error); 619 tlen = saved_resid - uio->uio_resid; 620 xfersize -= tlen; 621 *size -= tlen; 622 } 623 return (0); 624 } 625 626 /* 627 * Vnode op for reading. 628 */ 629 static int 630 ffs_read(ap) 631 struct vop_read_args /* { 632 struct vnode *a_vp; 633 struct uio *a_uio; 634 int a_ioflag; 635 struct ucred *a_cred; 636 } */ *ap; 637 { 638 struct vnode *vp; 639 struct inode *ip; 640 struct uio *uio; 641 struct fs *fs; 642 struct buf *bp; 643 ufs_lbn_t lbn, nextlbn; 644 off_t bytesinfile; 645 long size, xfersize, blkoffset; 646 ssize_t orig_resid; 647 int bflag, error, ioflag, seqcount; 648 649 vp = ap->a_vp; 650 uio = ap->a_uio; 651 ioflag = ap->a_ioflag; 652 if (ap->a_ioflag & IO_EXT) 653 #ifdef notyet 654 return (ffs_extread(vp, uio, ioflag)); 655 #else 656 panic("ffs_read+IO_EXT"); 657 #endif 658 #ifdef DIRECTIO 659 if ((ioflag & IO_DIRECT) != 0) { 660 int workdone; 661 662 error = ffs_rawread(vp, uio, &workdone); 663 if (error != 0 || workdone != 0) 664 return error; 665 } 666 #endif 667 668 seqcount = ap->a_ioflag >> IO_SEQSHIFT; 669 ip = VTOI(vp); 670 671 #ifdef INVARIANTS 672 if (uio->uio_rw != UIO_READ) 673 panic("ffs_read: mode"); 674 675 if (vp->v_type == VLNK) { 676 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen) 677 panic("ffs_read: short symlink"); 678 } else if (vp->v_type != VREG && vp->v_type != VDIR) 679 panic("ffs_read: type %d", vp->v_type); 680 #endif 681 orig_resid = uio->uio_resid; 682 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0")); 683 if (orig_resid == 0) 684 return (0); 685 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0")); 686 fs = ITOFS(ip); 687 if (uio->uio_offset < ip->i_size && 688 uio->uio_offset >= fs->fs_maxfilesize) 689 return (EOVERFLOW); 690 691 bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE); 692 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { 693 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0) 694 break; 695 lbn = lblkno(fs, uio->uio_offset); 696 nextlbn = lbn + 1; 697 698 /* 699 * size of buffer. The buffer representing the 700 * end of the file is rounded up to the size of 701 * the block type ( fragment or full block, 702 * depending ). 703 */ 704 size = blksize(fs, ip, lbn); 705 blkoffset = blkoff(fs, uio->uio_offset); 706 707 /* 708 * The amount we want to transfer in this iteration is 709 * one FS block less the amount of the data before 710 * our startpoint (duh!) 711 */ 712 xfersize = fs->fs_bsize - blkoffset; 713 714 /* 715 * But if we actually want less than the block, 716 * or the file doesn't have a whole block more of data, 717 * then use the lesser number. 718 */ 719 if (uio->uio_resid < xfersize) 720 xfersize = uio->uio_resid; 721 if (bytesinfile < xfersize) 722 xfersize = bytesinfile; 723 724 if (lblktosize(fs, nextlbn) >= ip->i_size) { 725 /* 726 * Don't do readahead if this is the end of the file. 727 */ 728 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp); 729 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 730 /* 731 * Otherwise if we are allowed to cluster, 732 * grab as much as we can. 733 * 734 * XXX This may not be a win if we are not 735 * doing sequential access. 736 */ 737 error = cluster_read(vp, ip->i_size, lbn, 738 size, NOCRED, blkoffset + uio->uio_resid, 739 seqcount, bflag, &bp); 740 } else if (seqcount > 1) { 741 /* 742 * If we are NOT allowed to cluster, then 743 * if we appear to be acting sequentially, 744 * fire off a request for a readahead 745 * as well as a read. Note that the 4th and 5th 746 * arguments point to arrays of the size specified in 747 * the 6th argument. 748 */ 749 u_int nextsize = blksize(fs, ip, nextlbn); 750 error = breadn_flags(vp, lbn, lbn, size, &nextlbn, 751 &nextsize, 1, NOCRED, bflag, NULL, &bp); 752 } else { 753 /* 754 * Failing all of the above, just read what the 755 * user asked for. Interestingly, the same as 756 * the first option above. 757 */ 758 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp); 759 } 760 if (error == EJUSTRETURN) { 761 error = ffs_read_hole(uio, xfersize, &size); 762 if (error == 0) 763 continue; 764 } 765 if (error != 0) { 766 brelse(bp); 767 bp = NULL; 768 break; 769 } 770 771 /* 772 * We should only get non-zero b_resid when an I/O error 773 * has occurred, which should cause us to break above. 774 * However, if the short read did not cause an error, 775 * then we want to ensure that we do not uiomove bad 776 * or uninitialized data. 777 */ 778 size -= bp->b_resid; 779 if (size < xfersize) { 780 if (size == 0) 781 break; 782 xfersize = size; 783 } 784 785 if (buf_mapped(bp)) { 786 error = vn_io_fault_uiomove((char *)bp->b_data + 787 blkoffset, (int)xfersize, uio); 788 } else { 789 error = vn_io_fault_pgmove(bp->b_pages, blkoffset, 790 (int)xfersize, uio); 791 } 792 if (error) 793 break; 794 795 vfs_bio_brelse(bp, ioflag); 796 } 797 798 /* 799 * This can only happen in the case of an error 800 * because the loop above resets bp to NULL on each iteration 801 * and on normal completion has not set a new value into it. 802 * so it must have come from a 'break' statement 803 */ 804 if (bp != NULL) 805 vfs_bio_brelse(bp, ioflag); 806 807 if ((error == 0 || uio->uio_resid != orig_resid) && 808 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 809 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS); 810 return (error); 811 } 812 813 /* 814 * Vnode op for writing. 815 */ 816 static int 817 ffs_write(ap) 818 struct vop_write_args /* { 819 struct vnode *a_vp; 820 struct uio *a_uio; 821 int a_ioflag; 822 struct ucred *a_cred; 823 } */ *ap; 824 { 825 struct vnode *vp; 826 struct uio *uio; 827 struct inode *ip; 828 struct fs *fs; 829 struct buf *bp; 830 ufs_lbn_t lbn; 831 off_t osize; 832 ssize_t resid; 833 int seqcount; 834 int blkoffset, error, flags, ioflag, size, xfersize; 835 836 vp = ap->a_vp; 837 uio = ap->a_uio; 838 ioflag = ap->a_ioflag; 839 if (ap->a_ioflag & IO_EXT) 840 #ifdef notyet 841 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred)); 842 #else 843 panic("ffs_write+IO_EXT"); 844 #endif 845 846 seqcount = ap->a_ioflag >> IO_SEQSHIFT; 847 ip = VTOI(vp); 848 849 #ifdef INVARIANTS 850 if (uio->uio_rw != UIO_WRITE) 851 panic("ffs_write: mode"); 852 #endif 853 854 switch (vp->v_type) { 855 case VREG: 856 if (ioflag & IO_APPEND) 857 uio->uio_offset = ip->i_size; 858 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) 859 return (EPERM); 860 /* FALLTHROUGH */ 861 case VLNK: 862 break; 863 case VDIR: 864 panic("ffs_write: dir write"); 865 break; 866 default: 867 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type, 868 (int)uio->uio_offset, 869 (int)uio->uio_resid 870 ); 871 } 872 873 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0")); 874 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0")); 875 fs = ITOFS(ip); 876 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) 877 return (EFBIG); 878 /* 879 * Maybe this should be above the vnode op call, but so long as 880 * file servers have no limits, I don't think it matters. 881 */ 882 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 883 return (EFBIG); 884 885 resid = uio->uio_resid; 886 osize = ip->i_size; 887 if (seqcount > BA_SEQMAX) 888 flags = BA_SEQMAX << BA_SEQSHIFT; 889 else 890 flags = seqcount << BA_SEQSHIFT; 891 if (ioflag & IO_SYNC) 892 flags |= IO_SYNC; 893 flags |= BA_UNMAPPED; 894 895 for (error = 0; uio->uio_resid > 0;) { 896 lbn = lblkno(fs, uio->uio_offset); 897 blkoffset = blkoff(fs, uio->uio_offset); 898 xfersize = fs->fs_bsize - blkoffset; 899 if (uio->uio_resid < xfersize) 900 xfersize = uio->uio_resid; 901 if (uio->uio_offset + xfersize > ip->i_size) 902 vnode_pager_setsize(vp, uio->uio_offset + xfersize); 903 904 /* 905 * We must perform a read-before-write if the transfer size 906 * does not cover the entire buffer. 907 */ 908 if (fs->fs_bsize > xfersize) 909 flags |= BA_CLRBUF; 910 else 911 flags &= ~BA_CLRBUF; 912 /* XXX is uio->uio_offset the right thing here? */ 913 error = UFS_BALLOC(vp, uio->uio_offset, xfersize, 914 ap->a_cred, flags, &bp); 915 if (error != 0) { 916 vnode_pager_setsize(vp, ip->i_size); 917 break; 918 } 919 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL)) 920 bp->b_flags |= B_NOCACHE; 921 922 if (uio->uio_offset + xfersize > ip->i_size) { 923 ip->i_size = uio->uio_offset + xfersize; 924 DIP_SET(ip, i_size, ip->i_size); 925 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 926 } 927 928 size = blksize(fs, ip, lbn) - bp->b_resid; 929 if (size < xfersize) 930 xfersize = size; 931 932 if (buf_mapped(bp)) { 933 error = vn_io_fault_uiomove((char *)bp->b_data + 934 blkoffset, (int)xfersize, uio); 935 } else { 936 error = vn_io_fault_pgmove(bp->b_pages, blkoffset, 937 (int)xfersize, uio); 938 } 939 /* 940 * If the buffer is not already filled and we encounter an 941 * error while trying to fill it, we have to clear out any 942 * garbage data from the pages instantiated for the buffer. 943 * If we do not, a failed uiomove() during a write can leave 944 * the prior contents of the pages exposed to a userland mmap. 945 * 946 * Note that we need only clear buffers with a transfer size 947 * equal to the block size because buffers with a shorter 948 * transfer size were cleared above by the call to UFS_BALLOC() 949 * with the BA_CLRBUF flag set. 950 * 951 * If the source region for uiomove identically mmaps the 952 * buffer, uiomove() performed the NOP copy, and the buffer 953 * content remains valid because the page fault handler 954 * validated the pages. 955 */ 956 if (error != 0 && (bp->b_flags & B_CACHE) == 0 && 957 fs->fs_bsize == xfersize) 958 vfs_bio_clrbuf(bp); 959 960 vfs_bio_set_flags(bp, ioflag); 961 962 /* 963 * If IO_SYNC each buffer is written synchronously. Otherwise 964 * if we have a severe page deficiency write the buffer 965 * asynchronously. Otherwise try to cluster, and if that 966 * doesn't do it then either do an async write (if O_DIRECT), 967 * or a delayed write (if not). 968 */ 969 if (ioflag & IO_SYNC) { 970 (void)bwrite(bp); 971 } else if (vm_page_count_severe() || 972 buf_dirty_count_severe() || 973 (ioflag & IO_ASYNC)) { 974 bp->b_flags |= B_CLUSTEROK; 975 bawrite(bp); 976 } else if (xfersize + blkoffset == fs->fs_bsize) { 977 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) { 978 bp->b_flags |= B_CLUSTEROK; 979 cluster_write(vp, &ip->i_clusterw, bp, 980 ip->i_size, seqcount, GB_UNMAPPED); 981 } else { 982 bawrite(bp); 983 } 984 } else if (ioflag & IO_DIRECT) { 985 bp->b_flags |= B_CLUSTEROK; 986 bawrite(bp); 987 } else { 988 bp->b_flags |= B_CLUSTEROK; 989 bdwrite(bp); 990 } 991 if (error || xfersize == 0) 992 break; 993 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE); 994 } 995 /* 996 * If we successfully wrote any data, and we are not the superuser 997 * we clear the setuid and setgid bits as a precaution against 998 * tampering. 999 */ 1000 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && 1001 ap->a_cred) { 1002 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) { 1003 vn_seqc_write_begin(vp); 1004 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID)); 1005 DIP_SET(ip, i_mode, ip->i_mode); 1006 vn_seqc_write_end(vp); 1007 } 1008 } 1009 if (error) { 1010 if (ioflag & IO_UNIT) { 1011 (void)ffs_truncate(vp, osize, 1012 IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred); 1013 uio->uio_offset -= resid - uio->uio_resid; 1014 uio->uio_resid = resid; 1015 } 1016 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) { 1017 if (!(ioflag & IO_DATASYNC) || 1018 (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA))) 1019 error = ffs_update(vp, 1); 1020 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error)) 1021 error = ENXIO; 1022 } 1023 return (error); 1024 } 1025 1026 /* 1027 * Extended attribute area reading. 1028 */ 1029 static int 1030 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag) 1031 { 1032 struct inode *ip; 1033 struct ufs2_dinode *dp; 1034 struct fs *fs; 1035 struct buf *bp; 1036 ufs_lbn_t lbn, nextlbn; 1037 off_t bytesinfile; 1038 long size, xfersize, blkoffset; 1039 ssize_t orig_resid; 1040 int error; 1041 1042 ip = VTOI(vp); 1043 fs = ITOFS(ip); 1044 dp = ip->i_din2; 1045 1046 #ifdef INVARIANTS 1047 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC) 1048 panic("ffs_extread: mode"); 1049 1050 #endif 1051 orig_resid = uio->uio_resid; 1052 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0")); 1053 if (orig_resid == 0) 1054 return (0); 1055 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0")); 1056 1057 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { 1058 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0) 1059 break; 1060 lbn = lblkno(fs, uio->uio_offset); 1061 nextlbn = lbn + 1; 1062 1063 /* 1064 * size of buffer. The buffer representing the 1065 * end of the file is rounded up to the size of 1066 * the block type ( fragment or full block, 1067 * depending ). 1068 */ 1069 size = sblksize(fs, dp->di_extsize, lbn); 1070 blkoffset = blkoff(fs, uio->uio_offset); 1071 1072 /* 1073 * The amount we want to transfer in this iteration is 1074 * one FS block less the amount of the data before 1075 * our startpoint (duh!) 1076 */ 1077 xfersize = fs->fs_bsize - blkoffset; 1078 1079 /* 1080 * But if we actually want less than the block, 1081 * or the file doesn't have a whole block more of data, 1082 * then use the lesser number. 1083 */ 1084 if (uio->uio_resid < xfersize) 1085 xfersize = uio->uio_resid; 1086 if (bytesinfile < xfersize) 1087 xfersize = bytesinfile; 1088 1089 if (lblktosize(fs, nextlbn) >= dp->di_extsize) { 1090 /* 1091 * Don't do readahead if this is the end of the info. 1092 */ 1093 error = bread(vp, -1 - lbn, size, NOCRED, &bp); 1094 } else { 1095 /* 1096 * If we have a second block, then 1097 * fire off a request for a readahead 1098 * as well as a read. Note that the 4th and 5th 1099 * arguments point to arrays of the size specified in 1100 * the 6th argument. 1101 */ 1102 u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn); 1103 1104 nextlbn = -1 - nextlbn; 1105 error = breadn(vp, -1 - lbn, 1106 size, &nextlbn, &nextsize, 1, NOCRED, &bp); 1107 } 1108 if (error) { 1109 brelse(bp); 1110 bp = NULL; 1111 break; 1112 } 1113 1114 /* 1115 * We should only get non-zero b_resid when an I/O error 1116 * has occurred, which should cause us to break above. 1117 * However, if the short read did not cause an error, 1118 * then we want to ensure that we do not uiomove bad 1119 * or uninitialized data. 1120 */ 1121 size -= bp->b_resid; 1122 if (size < xfersize) { 1123 if (size == 0) 1124 break; 1125 xfersize = size; 1126 } 1127 1128 error = uiomove((char *)bp->b_data + blkoffset, 1129 (int)xfersize, uio); 1130 if (error) 1131 break; 1132 vfs_bio_brelse(bp, ioflag); 1133 } 1134 1135 /* 1136 * This can only happen in the case of an error 1137 * because the loop above resets bp to NULL on each iteration 1138 * and on normal completion has not set a new value into it. 1139 * so it must have come from a 'break' statement 1140 */ 1141 if (bp != NULL) 1142 vfs_bio_brelse(bp, ioflag); 1143 return (error); 1144 } 1145 1146 /* 1147 * Extended attribute area writing. 1148 */ 1149 static int 1150 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred) 1151 { 1152 struct inode *ip; 1153 struct ufs2_dinode *dp; 1154 struct fs *fs; 1155 struct buf *bp; 1156 ufs_lbn_t lbn; 1157 off_t osize; 1158 ssize_t resid; 1159 int blkoffset, error, flags, size, xfersize; 1160 1161 ip = VTOI(vp); 1162 fs = ITOFS(ip); 1163 dp = ip->i_din2; 1164 1165 #ifdef INVARIANTS 1166 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC) 1167 panic("ffs_extwrite: mode"); 1168 #endif 1169 1170 if (ioflag & IO_APPEND) 1171 uio->uio_offset = dp->di_extsize; 1172 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0")); 1173 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0")); 1174 if ((uoff_t)uio->uio_offset + uio->uio_resid > 1175 UFS_NXADDR * fs->fs_bsize) 1176 return (EFBIG); 1177 1178 resid = uio->uio_resid; 1179 osize = dp->di_extsize; 1180 flags = IO_EXT; 1181 if (ioflag & IO_SYNC) 1182 flags |= IO_SYNC; 1183 1184 for (error = 0; uio->uio_resid > 0;) { 1185 lbn = lblkno(fs, uio->uio_offset); 1186 blkoffset = blkoff(fs, uio->uio_offset); 1187 xfersize = fs->fs_bsize - blkoffset; 1188 if (uio->uio_resid < xfersize) 1189 xfersize = uio->uio_resid; 1190 1191 /* 1192 * We must perform a read-before-write if the transfer size 1193 * does not cover the entire buffer. 1194 */ 1195 if (fs->fs_bsize > xfersize) 1196 flags |= BA_CLRBUF; 1197 else 1198 flags &= ~BA_CLRBUF; 1199 error = UFS_BALLOC(vp, uio->uio_offset, xfersize, 1200 ucred, flags, &bp); 1201 if (error != 0) 1202 break; 1203 /* 1204 * If the buffer is not valid we have to clear out any 1205 * garbage data from the pages instantiated for the buffer. 1206 * If we do not, a failed uiomove() during a write can leave 1207 * the prior contents of the pages exposed to a userland 1208 * mmap(). XXX deal with uiomove() errors a better way. 1209 */ 1210 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize) 1211 vfs_bio_clrbuf(bp); 1212 1213 if (uio->uio_offset + xfersize > dp->di_extsize) { 1214 dp->di_extsize = uio->uio_offset + xfersize; 1215 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 1216 } 1217 1218 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid; 1219 if (size < xfersize) 1220 xfersize = size; 1221 1222 error = 1223 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio); 1224 1225 vfs_bio_set_flags(bp, ioflag); 1226 1227 /* 1228 * If IO_SYNC each buffer is written synchronously. Otherwise 1229 * if we have a severe page deficiency write the buffer 1230 * asynchronously. Otherwise try to cluster, and if that 1231 * doesn't do it then either do an async write (if O_DIRECT), 1232 * or a delayed write (if not). 1233 */ 1234 if (ioflag & IO_SYNC) { 1235 (void)bwrite(bp); 1236 } else if (vm_page_count_severe() || 1237 buf_dirty_count_severe() || 1238 xfersize + blkoffset == fs->fs_bsize || 1239 (ioflag & (IO_ASYNC | IO_DIRECT))) 1240 bawrite(bp); 1241 else 1242 bdwrite(bp); 1243 if (error || xfersize == 0) 1244 break; 1245 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 1246 } 1247 /* 1248 * If we successfully wrote any data, and we are not the superuser 1249 * we clear the setuid and setgid bits as a precaution against 1250 * tampering. 1251 */ 1252 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) { 1253 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID)) { 1254 vn_seqc_write_begin(vp); 1255 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID)); 1256 dp->di_mode = ip->i_mode; 1257 vn_seqc_write_end(vp); 1258 } 1259 } 1260 if (error) { 1261 if (ioflag & IO_UNIT) { 1262 (void)ffs_truncate(vp, osize, 1263 IO_EXT | (ioflag&IO_SYNC), ucred); 1264 uio->uio_offset -= resid - uio->uio_resid; 1265 uio->uio_resid = resid; 1266 } 1267 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) 1268 error = ffs_update(vp, 1); 1269 return (error); 1270 } 1271 1272 /* 1273 * Vnode operating to retrieve a named extended attribute. 1274 * 1275 * Locate a particular EA (nspace:name) in the area (ptr:length), and return 1276 * the length of the EA, and possibly the pointer to the entry and to the data. 1277 */ 1278 static int 1279 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name, 1280 struct extattr **eapp, u_char **eac) 1281 { 1282 struct extattr *eap, *eaend; 1283 size_t nlen; 1284 1285 nlen = strlen(name); 1286 KASSERT(ALIGNED_TO(ptr, struct extattr), ("unaligned")); 1287 eap = (struct extattr *)ptr; 1288 eaend = (struct extattr *)(ptr + length); 1289 for (; eap < eaend; eap = EXTATTR_NEXT(eap)) { 1290 KASSERT(EXTATTR_NEXT(eap) <= eaend, 1291 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend)); 1292 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen 1293 || memcmp(eap->ea_name, name, nlen) != 0) 1294 continue; 1295 if (eapp != NULL) 1296 *eapp = eap; 1297 if (eac != NULL) 1298 *eac = EXTATTR_CONTENT(eap); 1299 return (EXTATTR_CONTENT_SIZE(eap)); 1300 } 1301 return (-1); 1302 } 1303 1304 static int 1305 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td) 1306 { 1307 const struct extattr *eap, *eaend, *eapnext; 1308 struct inode *ip; 1309 struct ufs2_dinode *dp; 1310 struct fs *fs; 1311 struct uio luio; 1312 struct iovec liovec; 1313 u_int easize; 1314 int error; 1315 u_char *eae; 1316 1317 ip = VTOI(vp); 1318 fs = ITOFS(ip); 1319 dp = ip->i_din2; 1320 easize = dp->di_extsize; 1321 if ((uoff_t)easize > UFS_NXADDR * fs->fs_bsize) 1322 return (EFBIG); 1323 1324 eae = malloc(easize, M_TEMP, M_WAITOK); 1325 1326 liovec.iov_base = eae; 1327 liovec.iov_len = easize; 1328 luio.uio_iov = &liovec; 1329 luio.uio_iovcnt = 1; 1330 luio.uio_offset = 0; 1331 luio.uio_resid = easize; 1332 luio.uio_segflg = UIO_SYSSPACE; 1333 luio.uio_rw = UIO_READ; 1334 luio.uio_td = td; 1335 1336 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC); 1337 if (error) { 1338 free(eae, M_TEMP); 1339 return (error); 1340 } 1341 /* Validate disk xattrfile contents. */ 1342 for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend; 1343 eap = eapnext) { 1344 eapnext = EXTATTR_NEXT(eap); 1345 /* Bogusly short entry or bogusly long entry. */ 1346 if (eap->ea_length < sizeof(*eap) || eapnext > eaend) { 1347 free(eae, M_TEMP); 1348 return (EINTEGRITY); 1349 } 1350 } 1351 *p = eae; 1352 return (0); 1353 } 1354 1355 static void 1356 ffs_lock_ea(struct vnode *vp) 1357 { 1358 struct inode *ip; 1359 1360 ip = VTOI(vp); 1361 VI_LOCK(vp); 1362 while (ip->i_flag & IN_EA_LOCKED) { 1363 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKWAIT); 1364 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea", 1365 0); 1366 } 1367 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKED); 1368 VI_UNLOCK(vp); 1369 } 1370 1371 static void 1372 ffs_unlock_ea(struct vnode *vp) 1373 { 1374 struct inode *ip; 1375 1376 ip = VTOI(vp); 1377 VI_LOCK(vp); 1378 if (ip->i_flag & IN_EA_LOCKWAIT) 1379 wakeup(&ip->i_ea_refs); 1380 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT); 1381 VI_UNLOCK(vp); 1382 } 1383 1384 static int 1385 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td) 1386 { 1387 struct inode *ip; 1388 struct ufs2_dinode *dp; 1389 int error; 1390 1391 ip = VTOI(vp); 1392 1393 ffs_lock_ea(vp); 1394 if (ip->i_ea_area != NULL) { 1395 ip->i_ea_refs++; 1396 ffs_unlock_ea(vp); 1397 return (0); 1398 } 1399 dp = ip->i_din2; 1400 error = ffs_rdextattr(&ip->i_ea_area, vp, td); 1401 if (error) { 1402 ffs_unlock_ea(vp); 1403 return (error); 1404 } 1405 ip->i_ea_len = dp->di_extsize; 1406 ip->i_ea_error = 0; 1407 ip->i_ea_refs++; 1408 ffs_unlock_ea(vp); 1409 return (0); 1410 } 1411 1412 /* 1413 * Vnode extattr transaction commit/abort 1414 */ 1415 static int 1416 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td) 1417 { 1418 struct inode *ip; 1419 struct uio luio; 1420 struct iovec liovec; 1421 int error; 1422 struct ufs2_dinode *dp; 1423 1424 ip = VTOI(vp); 1425 1426 ffs_lock_ea(vp); 1427 if (ip->i_ea_area == NULL) { 1428 ffs_unlock_ea(vp); 1429 return (EINVAL); 1430 } 1431 dp = ip->i_din2; 1432 error = ip->i_ea_error; 1433 if (commit && error == 0) { 1434 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit"); 1435 if (cred == NOCRED) 1436 cred = vp->v_mount->mnt_cred; 1437 liovec.iov_base = ip->i_ea_area; 1438 liovec.iov_len = ip->i_ea_len; 1439 luio.uio_iov = &liovec; 1440 luio.uio_iovcnt = 1; 1441 luio.uio_offset = 0; 1442 luio.uio_resid = ip->i_ea_len; 1443 luio.uio_segflg = UIO_SYSSPACE; 1444 luio.uio_rw = UIO_WRITE; 1445 luio.uio_td = td; 1446 /* XXX: I'm not happy about truncating to zero size */ 1447 if (ip->i_ea_len < dp->di_extsize) 1448 error = ffs_truncate(vp, 0, IO_EXT, cred); 1449 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred); 1450 } 1451 if (--ip->i_ea_refs == 0) { 1452 free(ip->i_ea_area, M_TEMP); 1453 ip->i_ea_area = NULL; 1454 ip->i_ea_len = 0; 1455 ip->i_ea_error = 0; 1456 } 1457 ffs_unlock_ea(vp); 1458 return (error); 1459 } 1460 1461 /* 1462 * Vnode extattr strategy routine for fifos. 1463 * 1464 * We need to check for a read or write of the external attributes. 1465 * Otherwise we just fall through and do the usual thing. 1466 */ 1467 static int 1468 ffsext_strategy(struct vop_strategy_args *ap) 1469 /* 1470 struct vop_strategy_args { 1471 struct vnodeop_desc *a_desc; 1472 struct vnode *a_vp; 1473 struct buf *a_bp; 1474 }; 1475 */ 1476 { 1477 struct vnode *vp; 1478 daddr_t lbn; 1479 1480 vp = ap->a_vp; 1481 lbn = ap->a_bp->b_lblkno; 1482 if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR) 1483 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap)); 1484 if (vp->v_type == VFIFO) 1485 return (VOP_STRATEGY_APV(&ufs_fifoops, ap)); 1486 panic("spec nodes went here"); 1487 } 1488 1489 /* 1490 * Vnode extattr transaction commit/abort 1491 */ 1492 static int 1493 ffs_openextattr(struct vop_openextattr_args *ap) 1494 /* 1495 struct vop_openextattr_args { 1496 struct vnodeop_desc *a_desc; 1497 struct vnode *a_vp; 1498 IN struct ucred *a_cred; 1499 IN struct thread *a_td; 1500 }; 1501 */ 1502 { 1503 1504 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) 1505 return (EOPNOTSUPP); 1506 1507 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td)); 1508 } 1509 1510 /* 1511 * Vnode extattr transaction commit/abort 1512 */ 1513 static int 1514 ffs_closeextattr(struct vop_closeextattr_args *ap) 1515 /* 1516 struct vop_closeextattr_args { 1517 struct vnodeop_desc *a_desc; 1518 struct vnode *a_vp; 1519 int a_commit; 1520 IN struct ucred *a_cred; 1521 IN struct thread *a_td; 1522 }; 1523 */ 1524 { 1525 1526 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) 1527 return (EOPNOTSUPP); 1528 1529 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) 1530 return (EROFS); 1531 1532 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td)); 1533 } 1534 1535 /* 1536 * Vnode operation to remove a named attribute. 1537 */ 1538 static int 1539 ffs_deleteextattr(struct vop_deleteextattr_args *ap) 1540 /* 1541 vop_deleteextattr { 1542 IN struct vnode *a_vp; 1543 IN int a_attrnamespace; 1544 IN const char *a_name; 1545 IN struct ucred *a_cred; 1546 IN struct thread *a_td; 1547 }; 1548 */ 1549 { 1550 struct inode *ip; 1551 struct extattr *eap; 1552 uint32_t ul; 1553 int olen, error, i, easize; 1554 u_char *eae; 1555 void *tmp; 1556 1557 ip = VTOI(ap->a_vp); 1558 1559 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) 1560 return (EOPNOTSUPP); 1561 1562 if (strlen(ap->a_name) == 0) 1563 return (EINVAL); 1564 1565 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) 1566 return (EROFS); 1567 1568 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 1569 ap->a_cred, ap->a_td, VWRITE); 1570 if (error) { 1571 /* 1572 * ffs_lock_ea is not needed there, because the vnode 1573 * must be exclusively locked. 1574 */ 1575 if (ip->i_ea_area != NULL && ip->i_ea_error == 0) 1576 ip->i_ea_error = error; 1577 return (error); 1578 } 1579 1580 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td); 1581 if (error) 1582 return (error); 1583 1584 /* CEM: delete could be done in-place instead */ 1585 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK); 1586 bcopy(ip->i_ea_area, eae, ip->i_ea_len); 1587 easize = ip->i_ea_len; 1588 1589 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name, 1590 &eap, NULL); 1591 if (olen == -1) { 1592 /* delete but nonexistent */ 1593 free(eae, M_TEMP); 1594 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); 1595 return (ENOATTR); 1596 } 1597 ul = eap->ea_length; 1598 i = (u_char *)EXTATTR_NEXT(eap) - eae; 1599 bcopy(EXTATTR_NEXT(eap), eap, easize - i); 1600 easize -= ul; 1601 1602 tmp = ip->i_ea_area; 1603 ip->i_ea_area = eae; 1604 ip->i_ea_len = easize; 1605 free(tmp, M_TEMP); 1606 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td); 1607 return (error); 1608 } 1609 1610 /* 1611 * Vnode operation to retrieve a named extended attribute. 1612 */ 1613 static int 1614 ffs_getextattr(struct vop_getextattr_args *ap) 1615 /* 1616 vop_getextattr { 1617 IN struct vnode *a_vp; 1618 IN int a_attrnamespace; 1619 IN const char *a_name; 1620 INOUT struct uio *a_uio; 1621 OUT size_t *a_size; 1622 IN struct ucred *a_cred; 1623 IN struct thread *a_td; 1624 }; 1625 */ 1626 { 1627 struct inode *ip; 1628 u_char *eae, *p; 1629 unsigned easize; 1630 int error, ealen; 1631 1632 ip = VTOI(ap->a_vp); 1633 1634 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) 1635 return (EOPNOTSUPP); 1636 1637 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 1638 ap->a_cred, ap->a_td, VREAD); 1639 if (error) 1640 return (error); 1641 1642 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td); 1643 if (error) 1644 return (error); 1645 1646 eae = ip->i_ea_area; 1647 easize = ip->i_ea_len; 1648 1649 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name, 1650 NULL, &p); 1651 if (ealen >= 0) { 1652 error = 0; 1653 if (ap->a_size != NULL) 1654 *ap->a_size = ealen; 1655 else if (ap->a_uio != NULL) 1656 error = uiomove(p, ealen, ap->a_uio); 1657 } else 1658 error = ENOATTR; 1659 1660 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); 1661 return (error); 1662 } 1663 1664 /* 1665 * Vnode operation to retrieve extended attributes on a vnode. 1666 */ 1667 static int 1668 ffs_listextattr(struct vop_listextattr_args *ap) 1669 /* 1670 vop_listextattr { 1671 IN struct vnode *a_vp; 1672 IN int a_attrnamespace; 1673 INOUT struct uio *a_uio; 1674 OUT size_t *a_size; 1675 IN struct ucred *a_cred; 1676 IN struct thread *a_td; 1677 }; 1678 */ 1679 { 1680 struct inode *ip; 1681 struct extattr *eap, *eaend; 1682 int error, ealen; 1683 1684 ip = VTOI(ap->a_vp); 1685 1686 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) 1687 return (EOPNOTSUPP); 1688 1689 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 1690 ap->a_cred, ap->a_td, VREAD); 1691 if (error) 1692 return (error); 1693 1694 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td); 1695 if (error) 1696 return (error); 1697 1698 error = 0; 1699 if (ap->a_size != NULL) 1700 *ap->a_size = 0; 1701 1702 KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned")); 1703 eap = (struct extattr *)ip->i_ea_area; 1704 eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len); 1705 for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) { 1706 KASSERT(EXTATTR_NEXT(eap) <= eaend, 1707 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend)); 1708 if (eap->ea_namespace != ap->a_attrnamespace) 1709 continue; 1710 1711 ealen = eap->ea_namelength; 1712 if (ap->a_size != NULL) 1713 *ap->a_size += ealen + 1; 1714 else if (ap->a_uio != NULL) 1715 error = uiomove(&eap->ea_namelength, ealen + 1, 1716 ap->a_uio); 1717 } 1718 1719 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); 1720 return (error); 1721 } 1722 1723 /* 1724 * Vnode operation to set a named attribute. 1725 */ 1726 static int 1727 ffs_setextattr(struct vop_setextattr_args *ap) 1728 /* 1729 vop_setextattr { 1730 IN struct vnode *a_vp; 1731 IN int a_attrnamespace; 1732 IN const char *a_name; 1733 INOUT struct uio *a_uio; 1734 IN struct ucred *a_cred; 1735 IN struct thread *a_td; 1736 }; 1737 */ 1738 { 1739 struct inode *ip; 1740 struct fs *fs; 1741 struct extattr *eap; 1742 uint32_t ealength, ul; 1743 ssize_t ealen; 1744 int olen, eapad1, eapad2, error, i, easize; 1745 u_char *eae; 1746 void *tmp; 1747 1748 ip = VTOI(ap->a_vp); 1749 fs = ITOFS(ip); 1750 1751 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) 1752 return (EOPNOTSUPP); 1753 1754 if (strlen(ap->a_name) == 0) 1755 return (EINVAL); 1756 1757 /* XXX Now unsupported API to delete EAs using NULL uio. */ 1758 if (ap->a_uio == NULL) 1759 return (EOPNOTSUPP); 1760 1761 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) 1762 return (EROFS); 1763 1764 ealen = ap->a_uio->uio_resid; 1765 if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR)) 1766 return (EINVAL); 1767 1768 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 1769 ap->a_cred, ap->a_td, VWRITE); 1770 if (error) { 1771 /* 1772 * ffs_lock_ea is not needed there, because the vnode 1773 * must be exclusively locked. 1774 */ 1775 if (ip->i_ea_area != NULL && ip->i_ea_error == 0) 1776 ip->i_ea_error = error; 1777 return (error); 1778 } 1779 1780 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td); 1781 if (error) 1782 return (error); 1783 1784 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name); 1785 eapad1 = roundup2(ealength, 8) - ealength; 1786 eapad2 = roundup2(ealen, 8) - ealen; 1787 ealength += eapad1 + ealen + eapad2; 1788 1789 /* 1790 * CEM: rewrites of the same size or smaller could be done in-place 1791 * instead. (We don't acquire any fine-grained locks in here either, 1792 * so we could also do bigger writes in-place.) 1793 */ 1794 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK); 1795 bcopy(ip->i_ea_area, eae, ip->i_ea_len); 1796 easize = ip->i_ea_len; 1797 1798 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name, 1799 &eap, NULL); 1800 if (olen == -1) { 1801 /* new, append at end */ 1802 KASSERT(ALIGNED_TO(eae + easize, struct extattr), 1803 ("unaligned")); 1804 eap = (struct extattr *)(eae + easize); 1805 easize += ealength; 1806 } else { 1807 ul = eap->ea_length; 1808 i = (u_char *)EXTATTR_NEXT(eap) - eae; 1809 if (ul != ealength) { 1810 bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength, 1811 easize - i); 1812 easize += (ealength - ul); 1813 } 1814 } 1815 if (easize > lblktosize(fs, UFS_NXADDR)) { 1816 free(eae, M_TEMP); 1817 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); 1818 if (ip->i_ea_area != NULL && ip->i_ea_error == 0) 1819 ip->i_ea_error = ENOSPC; 1820 return (ENOSPC); 1821 } 1822 eap->ea_length = ealength; 1823 eap->ea_namespace = ap->a_attrnamespace; 1824 eap->ea_contentpadlen = eapad2; 1825 eap->ea_namelength = strlen(ap->a_name); 1826 memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name)); 1827 bzero(&eap->ea_name[strlen(ap->a_name)], eapad1); 1828 error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio); 1829 if (error) { 1830 free(eae, M_TEMP); 1831 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); 1832 if (ip->i_ea_area != NULL && ip->i_ea_error == 0) 1833 ip->i_ea_error = error; 1834 return (error); 1835 } 1836 bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2); 1837 1838 tmp = ip->i_ea_area; 1839 ip->i_ea_area = eae; 1840 ip->i_ea_len = easize; 1841 free(tmp, M_TEMP); 1842 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td); 1843 return (error); 1844 } 1845 1846 /* 1847 * Vnode pointer to File handle 1848 */ 1849 static int 1850 ffs_vptofh(struct vop_vptofh_args *ap) 1851 /* 1852 vop_vptofh { 1853 IN struct vnode *a_vp; 1854 IN struct fid *a_fhp; 1855 }; 1856 */ 1857 { 1858 struct inode *ip; 1859 struct ufid *ufhp; 1860 1861 ip = VTOI(ap->a_vp); 1862 ufhp = (struct ufid *)ap->a_fhp; 1863 ufhp->ufid_len = sizeof(struct ufid); 1864 ufhp->ufid_ino = ip->i_number; 1865 ufhp->ufid_gen = ip->i_gen; 1866 return (0); 1867 } 1868 1869 SYSCTL_DECL(_vfs_ffs); 1870 static int use_buf_pager = 1; 1871 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0, 1872 "Always use buffer pager instead of bmap"); 1873 1874 static daddr_t 1875 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off) 1876 { 1877 1878 return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off)); 1879 } 1880 1881 static int 1882 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn) 1883 { 1884 1885 return (blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn)); 1886 } 1887 1888 static int 1889 ffs_getpages(struct vop_getpages_args *ap) 1890 { 1891 struct vnode *vp; 1892 struct ufsmount *um; 1893 1894 vp = ap->a_vp; 1895 um = VFSTOUFS(vp->v_mount); 1896 1897 if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) 1898 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count, 1899 ap->a_rbehind, ap->a_rahead, NULL, NULL)); 1900 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind, 1901 ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz)); 1902 } 1903 1904 static int 1905 ffs_getpages_async(struct vop_getpages_async_args *ap) 1906 { 1907 struct vnode *vp; 1908 struct ufsmount *um; 1909 bool do_iodone; 1910 int error; 1911 1912 vp = ap->a_vp; 1913 um = VFSTOUFS(vp->v_mount); 1914 do_iodone = true; 1915 1916 if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) { 1917 error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count, 1918 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg); 1919 if (error == 0) 1920 do_iodone = false; 1921 } else { 1922 error = vfs_bio_getpages(vp, ap->a_m, ap->a_count, 1923 ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno, 1924 ffs_gbp_getblksz); 1925 } 1926 if (do_iodone && ap->a_iodone != NULL) 1927 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error); 1928 1929 return (error); 1930 } 1931 1932 static int 1933 ffs_vput_pair(struct vop_vput_pair_args *ap) 1934 { 1935 struct mount *mp; 1936 struct vnode *dvp, *vp, *vp1, **vpp; 1937 struct inode *dp, *ip; 1938 ino_t ip_ino; 1939 u_int64_t ip_gen; 1940 off_t old_size; 1941 int error, vp_locked; 1942 1943 dvp = ap->a_dvp; 1944 dp = VTOI(dvp); 1945 vpp = ap->a_vpp; 1946 vp = vpp != NULL ? *vpp : NULL; 1947 1948 if ((dp->i_flag & (IN_NEEDSYNC | IN_ENDOFF)) == 0) { 1949 vput(dvp); 1950 if (vp != NULL && ap->a_unlock_vp) 1951 vput(vp); 1952 return (0); 1953 } 1954 1955 mp = dvp->v_mount; 1956 if (vp != NULL) { 1957 if (ap->a_unlock_vp) { 1958 vput(vp); 1959 } else { 1960 MPASS(vp->v_type != VNON); 1961 vp_locked = VOP_ISLOCKED(vp); 1962 ip = VTOI(vp); 1963 ip_ino = ip->i_number; 1964 ip_gen = ip->i_gen; 1965 VOP_UNLOCK(vp); 1966 } 1967 } 1968 1969 /* 1970 * If compaction or fsync was requested do it in ffs_vput_pair() 1971 * now that other locks are no longer held. 1972 */ 1973 if ((dp->i_flag & IN_ENDOFF) != 0) { 1974 VNASSERT(I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size, dvp, 1975 ("IN_ENDOFF set but I_ENDOFF() is not")); 1976 dp->i_flag &= ~IN_ENDOFF; 1977 old_size = dp->i_size; 1978 error = UFS_TRUNCATE(dvp, (off_t)I_ENDOFF(dp), IO_NORMAL | 1979 (DOINGASYNC(dvp) ? 0 : IO_SYNC), curthread->td_ucred); 1980 if (error != 0 && error != ERELOOKUP) { 1981 if (!ffs_fsfail_cleanup(VFSTOUFS(mp), error)) { 1982 vn_printf(dvp, 1983 "IN_ENDOFF: failed to truncate, " 1984 "error %d\n", error); 1985 } 1986 #ifdef UFS_DIRHASH 1987 ufsdirhash_free(dp); 1988 #endif 1989 } 1990 SET_I_ENDOFF(dp, 0); 1991 } 1992 if ((dp->i_flag & IN_NEEDSYNC) != 0) { 1993 do { 1994 error = ffs_syncvnode(dvp, MNT_WAIT, 0); 1995 } while (error == ERELOOKUP); 1996 } 1997 1998 vput(dvp); 1999 2000 if (vp == NULL || ap->a_unlock_vp) 2001 return (0); 2002 MPASS(mp != NULL); 2003 2004 /* 2005 * It is possible that vp is reclaimed at this point. Only 2006 * routines that call us with a_unlock_vp == false can find 2007 * that their vp has been reclaimed. There are three areas 2008 * that are affected: 2009 * 1) vn_open_cred() - later VOPs could fail, but 2010 * dead_open() returns 0 to simulate successful open. 2011 * 2) ffs_snapshot() - creation of snapshot fails with EBADF. 2012 * 3) NFS server (several places) - code is prepared to detect 2013 * and respond to dead vnodes by returning ESTALE. 2014 */ 2015 VOP_LOCK(vp, vp_locked | LK_RETRY); 2016 if (!VN_IS_DOOMED(vp)) 2017 return (0); 2018 2019 /* 2020 * Try harder to recover from reclaimed vp if reclaim was not 2021 * because underlying inode was cleared. We saved inode 2022 * number and inode generation, so we can try to reinstantiate 2023 * exactly same version of inode. If this fails, return 2024 * original doomed vnode and let caller to handle 2025 * consequences. 2026 * 2027 * Note that callers must keep write started around 2028 * VOP_VPUT_PAIR() calls, so it is safe to use mp without 2029 * busying it. 2030 */ 2031 VOP_UNLOCK(vp); 2032 error = ffs_inotovp(mp, ip_ino, ip_gen, LK_EXCLUSIVE, &vp1, 2033 FFSV_REPLACE_DOOMED); 2034 if (error != 0) { 2035 VOP_LOCK(vp, vp_locked | LK_RETRY); 2036 } else { 2037 vrele(vp); 2038 *vpp = vp1; 2039 } 2040 return (error); 2041 } 2042