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