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