1 /*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95 34 * $FreeBSD: src/sys/ufs/ufs/ufs_readwrite.c,v 1.65.2.14 2003/04/04 22:21:29 tegge Exp $ 35 * $DragonFly: src/sys/vfs/ufs/ufs_readwrite.c,v 1.13 2004/10/25 19:14:34 dillon Exp $ 36 */ 37 38 #define BLKSIZE(a, b, c) blksize(a, b, c) 39 #define FS struct fs 40 #define I_FS i_fs 41 42 #include <vm/vm.h> 43 #include <vm/vm_object.h> 44 #include <vm/vm_pager.h> 45 #include <vm/vm_map.h> 46 #include <vm/vnode_pager.h> 47 #include <sys/event.h> 48 #include <sys/vmmeter.h> 49 #include <vm/vm_page2.h> 50 51 #include "opt_directio.h" 52 53 #define VN_KNOTE(vp, b) \ 54 KNOTE((struct klist *)&vp->v_pollinfo.vpi_selinfo.si_note, (b)) 55 56 #ifdef DIRECTIO 57 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone); 58 #endif 59 60 /* 61 * Vnode op for reading. 62 * 63 * ffs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 64 * struct ucred *a_cred) 65 */ 66 /* ARGSUSED */ 67 int 68 ffs_read(struct vop_read_args *ap) 69 { 70 struct vnode *vp; 71 struct inode *ip; 72 struct uio *uio; 73 FS *fs; 74 struct buf *bp; 75 ufs_daddr_t lbn, nextlbn; 76 off_t bytesinfile; 77 long size, xfersize, blkoffset; 78 int error, orig_resid; 79 u_short mode; 80 int seqcount; 81 int ioflag; 82 vm_object_t object; 83 84 vp = ap->a_vp; 85 seqcount = ap->a_ioflag >> 16; 86 ip = VTOI(vp); 87 mode = ip->i_mode; 88 uio = ap->a_uio; 89 ioflag = ap->a_ioflag; 90 #ifdef DIRECTIO 91 if ((ioflag & IO_DIRECT) != 0) { 92 int workdone; 93 94 error = ffs_rawread(vp, uio, &workdone); 95 if (error || workdone) 96 return error; 97 } 98 #endif 99 100 #ifdef DIAGNOSTIC 101 if (uio->uio_rw != UIO_READ) 102 panic("ffs_read: mode"); 103 104 if (vp->v_type == VLNK) { 105 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen) 106 panic("ffs_read: short symlink"); 107 } else if (vp->v_type != VREG && vp->v_type != VDIR) 108 panic("ffs_read: type %d", vp->v_type); 109 #endif 110 fs = ip->I_FS; 111 if ((uint64_t)uio->uio_offset > fs->fs_maxfilesize) 112 return (EFBIG); 113 114 orig_resid = uio->uio_resid; 115 if (orig_resid <= 0) 116 return (0); 117 118 object = vp->v_object; 119 120 bytesinfile = ip->i_size - uio->uio_offset; 121 if (bytesinfile <= 0) { 122 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 123 ip->i_flag |= IN_ACCESS; 124 return 0; 125 } 126 127 if (object) 128 vm_object_reference(object); 129 130 /* 131 * Ok so we couldn't do it all in one vm trick... 132 * so cycle around trying smaller bites.. 133 */ 134 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { 135 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0) 136 break; 137 138 lbn = lblkno(fs, uio->uio_offset); 139 nextlbn = lbn + 1; 140 141 /* 142 * size of buffer. The buffer representing the 143 * end of the file is rounded up to the size of 144 * the block type ( fragment or full block, 145 * depending ). 146 */ 147 size = BLKSIZE(fs, ip, lbn); 148 blkoffset = blkoff(fs, uio->uio_offset); 149 150 /* 151 * The amount we want to transfer in this iteration is 152 * one FS block less the amount of the data before 153 * our startpoint (duh!) 154 */ 155 xfersize = fs->fs_bsize - blkoffset; 156 157 /* 158 * But if we actually want less than the block, 159 * or the file doesn't have a whole block more of data, 160 * then use the lesser number. 161 */ 162 if (uio->uio_resid < xfersize) 163 xfersize = uio->uio_resid; 164 if (bytesinfile < xfersize) 165 xfersize = bytesinfile; 166 167 if (lblktosize(fs, nextlbn) >= ip->i_size) { 168 /* 169 * Don't do readahead if this is the end of the file. 170 */ 171 error = bread(vp, lbn, size, &bp); 172 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 173 /* 174 * Otherwise if we are allowed to cluster, 175 * grab as much as we can. 176 * 177 * XXX This may not be a win if we are not 178 * doing sequential access. 179 */ 180 error = cluster_read(vp, ip->i_size, lbn, 181 size, uio->uio_resid, seqcount, &bp); 182 } else if (seqcount > 1) { 183 /* 184 * If we are NOT allowed to cluster, then 185 * if we appear to be acting sequentially, 186 * fire off a request for a readahead 187 * as well as a read. Note that the 4th and 5th 188 * arguments point to arrays of the size specified in 189 * the 6th argument. 190 */ 191 int nextsize = BLKSIZE(fs, ip, nextlbn); 192 error = breadn(vp, lbn, 193 size, &nextlbn, &nextsize, 1, &bp); 194 } else { 195 /* 196 * Failing all of the above, just read what the 197 * user asked for. Interestingly, the same as 198 * the first option above. 199 */ 200 error = bread(vp, lbn, size, &bp); 201 } 202 if (error) { 203 brelse(bp); 204 bp = NULL; 205 break; 206 } 207 208 /* 209 * If IO_DIRECT then set B_DIRECT for the buffer. This 210 * will cause us to attempt to release the buffer later on 211 * and will cause the buffer cache to attempt to free the 212 * underlying pages. 213 */ 214 if (ioflag & IO_DIRECT) 215 bp->b_flags |= B_DIRECT; 216 217 /* 218 * We should only get non-zero b_resid when an I/O error 219 * has occurred, which should cause us to break above. 220 * However, if the short read did not cause an error, 221 * then we want to ensure that we do not uiomove bad 222 * or uninitialized data. 223 * 224 * XXX b_resid is only valid when an actual I/O has occured 225 * and may be incorrect if the buffer is B_CACHE or if the 226 * last op on the buffer was a failed write. This KASSERT 227 * is a precursor to removing it from the UFS code. 228 */ 229 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0")); 230 size -= bp->b_resid; 231 if (size < xfersize) { 232 if (size == 0) 233 break; 234 xfersize = size; 235 } 236 237 /* 238 * otherwise use the general form 239 */ 240 error = uiomove((char *)bp->b_data + blkoffset, 241 (int)xfersize, uio); 242 243 if (error) 244 break; 245 246 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 247 (LIST_FIRST(&bp->b_dep) == NULL)) { 248 /* 249 * If there are no dependencies, and it's VMIO, 250 * then we don't need the buf, mark it available 251 * for freeing. The VM has the data. 252 */ 253 bp->b_flags |= B_RELBUF; 254 brelse(bp); 255 } else { 256 /* 257 * Otherwise let whoever 258 * made the request take care of 259 * freeing it. We just queue 260 * it onto another list. 261 */ 262 bqrelse(bp); 263 } 264 } 265 266 /* 267 * This can only happen in the case of an error 268 * because the loop above resets bp to NULL on each iteration 269 * and on normal completion has not set a new value into it. 270 * so it must have come from a 'break' statement 271 */ 272 if (bp != NULL) { 273 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 274 (LIST_FIRST(&bp->b_dep) == NULL)) { 275 bp->b_flags |= B_RELBUF; 276 brelse(bp); 277 } else { 278 bqrelse(bp); 279 } 280 } 281 282 if (object) 283 vm_object_vndeallocate(object); 284 if ((error == 0 || uio->uio_resid != orig_resid) && 285 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 286 ip->i_flag |= IN_ACCESS; 287 return (error); 288 } 289 290 /* 291 * Vnode op for writing. 292 * 293 * ffs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 294 * struct ucred *a_cred) 295 */ 296 int 297 ffs_write(struct vop_write_args *ap) 298 { 299 struct vnode *vp; 300 struct uio *uio; 301 struct inode *ip; 302 FS *fs; 303 struct buf *bp; 304 ufs_daddr_t lbn; 305 off_t osize; 306 int seqcount; 307 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize; 308 vm_object_t object; 309 struct thread *td; 310 311 extended = 0; 312 seqcount = ap->a_ioflag >> 16; 313 ioflag = ap->a_ioflag; 314 uio = ap->a_uio; 315 vp = ap->a_vp; 316 ip = VTOI(vp); 317 318 object = vp->v_object; 319 if (object) 320 vm_object_reference(object); 321 322 #ifdef DIAGNOSTIC 323 if (uio->uio_rw != UIO_WRITE) 324 panic("ffs_write: mode"); 325 #endif 326 327 switch (vp->v_type) { 328 case VREG: 329 if (ioflag & IO_APPEND) 330 uio->uio_offset = ip->i_size; 331 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) { 332 if (object) 333 vm_object_vndeallocate(object); 334 return (EPERM); 335 } 336 /* FALLTHROUGH */ 337 case VLNK: 338 break; 339 case VDIR: 340 panic("ffs_write: dir write"); 341 break; 342 default: 343 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type, 344 (int)uio->uio_offset, 345 (int)uio->uio_resid 346 ); 347 } 348 349 fs = ip->I_FS; 350 if (uio->uio_offset < 0 || 351 (uint64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) { 352 if (object) 353 vm_object_vndeallocate(object); 354 return (EFBIG); 355 } 356 /* 357 * Maybe this should be above the vnode op call, but so long as 358 * file servers have no limits, I don't think it matters. 359 */ 360 td = uio->uio_td; 361 if (vp->v_type == VREG && td && td->td_proc && 362 uio->uio_offset + uio->uio_resid > 363 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 364 psignal(td->td_proc, SIGXFSZ); 365 if (object) 366 vm_object_vndeallocate(object); 367 return (EFBIG); 368 } 369 370 resid = uio->uio_resid; 371 osize = ip->i_size; 372 373 /* 374 * NOTE! These B_ flags are actually balloc-only flags, not buffer 375 * flags. They are similar to the BA_ flags in fbsd. 376 */ 377 if (seqcount > B_SEQMAX) 378 flags = B_SEQMAX << B_SEQSHIFT; 379 else 380 flags = seqcount << B_SEQSHIFT; 381 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp)) 382 flags |= B_SYNC; 383 384 if (object && (object->flags & OBJ_OPT)) { 385 vm_freeze_copyopts(object, 386 OFF_TO_IDX(uio->uio_offset), 387 OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK)); 388 } 389 390 for (error = 0; uio->uio_resid > 0;) { 391 lbn = lblkno(fs, uio->uio_offset); 392 blkoffset = blkoff(fs, uio->uio_offset); 393 xfersize = fs->fs_bsize - blkoffset; 394 if (uio->uio_resid < xfersize) 395 xfersize = uio->uio_resid; 396 397 if (uio->uio_offset + xfersize > ip->i_size) 398 vnode_pager_setsize(vp, uio->uio_offset + xfersize); 399 400 /* 401 * We must perform a read-before-write if the transfer 402 * size does not cover the entire buffer. 403 */ 404 if (fs->fs_bsize > xfersize) 405 flags |= B_CLRBUF; 406 else 407 flags &= ~B_CLRBUF; 408 /* XXX is uio->uio_offset the right thing here? */ 409 error = VOP_BALLOC(vp, uio->uio_offset, xfersize, 410 ap->a_cred, flags, &bp); 411 if (error != 0) 412 break; 413 /* 414 * If the buffer is not valid and we did not clear garbage 415 * out above, we have to do so here even though the write 416 * covers the entire buffer in order to avoid a mmap()/write 417 * race where another process may see the garbage prior to 418 * the uiomove() for a write replacing it. 419 */ 420 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize) 421 vfs_bio_clrbuf(bp); 422 if (ioflag & IO_DIRECT) 423 bp->b_flags |= B_DIRECT; 424 if (ioflag & IO_NOWDRAIN) 425 bp->b_flags |= B_NOWDRAIN; 426 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL)) 427 bp->b_flags |= B_NOCACHE; 428 429 if (uio->uio_offset + xfersize > ip->i_size) { 430 ip->i_size = uio->uio_offset + xfersize; 431 extended = 1; 432 } 433 434 size = BLKSIZE(fs, ip, lbn) - bp->b_resid; 435 if (size < xfersize) 436 xfersize = size; 437 438 error = 439 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio); 440 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 441 (LIST_FIRST(&bp->b_dep) == NULL)) { 442 bp->b_flags |= B_RELBUF; 443 } 444 445 /* 446 * If IO_SYNC each buffer is written synchronously. Otherwise 447 * if we have a severe page deficiency write the buffer 448 * asynchronously. Otherwise try to cluster, and if that 449 * doesn't do it then either do an async write (if O_DIRECT), 450 * or a delayed write (if not). 451 */ 452 453 if (ioflag & IO_SYNC) { 454 (void)bwrite(bp); 455 } else if (vm_page_count_severe() || 456 buf_dirty_count_severe() || 457 (ioflag & IO_ASYNC)) { 458 bp->b_flags |= B_CLUSTEROK; 459 bawrite(bp); 460 } else if (xfersize + blkoffset == fs->fs_bsize) { 461 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) { 462 bp->b_flags |= B_CLUSTEROK; 463 cluster_write(bp, ip->i_size, seqcount); 464 } else { 465 bawrite(bp); 466 } 467 } else if (ioflag & IO_DIRECT) { 468 bp->b_flags |= B_CLUSTEROK; 469 bawrite(bp); 470 } else { 471 bp->b_flags |= B_CLUSTEROK; 472 bdwrite(bp); 473 } 474 if (error || xfersize == 0) 475 break; 476 ip->i_flag |= IN_CHANGE | IN_UPDATE; 477 } 478 /* 479 * If we successfully wrote any data, and we are not the superuser 480 * we clear the setuid and setgid bits as a precaution against 481 * tampering. 482 */ 483 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0) 484 ip->i_mode &= ~(ISUID | ISGID); 485 if (resid > uio->uio_resid) 486 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); 487 if (error) { 488 if (ioflag & IO_UNIT) { 489 (void)UFS_TRUNCATE(vp, osize, 490 ioflag & IO_SYNC, ap->a_cred, uio->uio_td); 491 uio->uio_offset -= resid - uio->uio_resid; 492 uio->uio_resid = resid; 493 } 494 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) 495 error = UFS_UPDATE(vp, 1); 496 497 if (object) 498 vm_object_vndeallocate(object); 499 500 return (error); 501 } 502 503 504 /* 505 * get page routine 506 */ 507 int 508 ffs_getpages(struct vop_getpages_args *ap) 509 { 510 off_t foff, physoffset; 511 int i, size, bsize; 512 struct vnode *dp, *vp; 513 vm_object_t obj; 514 vm_pindex_t pindex, firstindex; 515 vm_page_t mreq; 516 int bbackwards, bforwards; 517 int pbackwards, pforwards; 518 int firstpage; 519 int reqlblkno; 520 daddr_t reqblkno; 521 int poff; 522 int pcount; 523 int rtval; 524 int pagesperblock; 525 526 527 pcount = round_page(ap->a_count) / PAGE_SIZE; 528 mreq = ap->a_m[ap->a_reqpage]; 529 firstindex = ap->a_m[0]->pindex; 530 531 /* 532 * if ANY DEV_BSIZE blocks are valid on a large filesystem block, 533 * then the entire page is valid. Since the page may be mapped, 534 * user programs might reference data beyond the actual end of file 535 * occuring within the page. We have to zero that data. 536 */ 537 if (mreq->valid) { 538 if (mreq->valid != VM_PAGE_BITS_ALL) 539 vm_page_zero_invalid(mreq, TRUE); 540 for (i = 0; i < pcount; i++) { 541 if (i != ap->a_reqpage) { 542 vm_page_free(ap->a_m[i]); 543 } 544 } 545 return VM_PAGER_OK; 546 } 547 548 vp = ap->a_vp; 549 obj = vp->v_object; 550 bsize = vp->v_mount->mnt_stat.f_iosize; 551 pindex = mreq->pindex; 552 foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */; 553 554 if (bsize < PAGE_SIZE) 555 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, 556 ap->a_count, 557 ap->a_reqpage); 558 559 /* 560 * foff is the file offset of the required page 561 * reqlblkno is the logical block that contains the page 562 * poff is the index of the page into the logical block 563 */ 564 reqlblkno = foff / bsize; 565 poff = (foff % bsize) / PAGE_SIZE; 566 567 if ( VOP_BMAP( vp, reqlblkno, &dp, &reqblkno, 568 &bforwards, &bbackwards) || (reqblkno == -1)) { 569 for(i = 0; i < pcount; i++) { 570 if (i != ap->a_reqpage) 571 vm_page_free(ap->a_m[i]); 572 } 573 if (reqblkno == -1) { 574 if ((mreq->flags & PG_ZERO) == 0) 575 vm_page_zero_fill(mreq); 576 vm_page_undirty(mreq); 577 mreq->valid = VM_PAGE_BITS_ALL; 578 return VM_PAGER_OK; 579 } else { 580 return VM_PAGER_ERROR; 581 } 582 } 583 584 physoffset = (off_t)reqblkno * DEV_BSIZE + poff * PAGE_SIZE; 585 pagesperblock = bsize / PAGE_SIZE; 586 /* 587 * find the first page that is contiguous... 588 * note that pbackwards is the number of pages that are contiguous 589 * backwards. 590 */ 591 firstpage = 0; 592 if (ap->a_count) { 593 pbackwards = poff + bbackwards * pagesperblock; 594 if (ap->a_reqpage > pbackwards) { 595 firstpage = ap->a_reqpage - pbackwards; 596 for(i=0;i<firstpage;i++) 597 vm_page_free(ap->a_m[i]); 598 } 599 600 /* 601 * pforwards is the number of pages that are contiguous 602 * after the current page. 603 */ 604 pforwards = (pagesperblock - (poff + 1)) + 605 bforwards * pagesperblock; 606 if (pforwards < (pcount - (ap->a_reqpage + 1))) { 607 for( i = ap->a_reqpage + pforwards + 1; i < pcount; i++) 608 vm_page_free(ap->a_m[i]); 609 pcount = ap->a_reqpage + pforwards + 1; 610 } 611 612 /* 613 * number of pages for I/O corrected for the non-contig pages at 614 * the beginning of the array. 615 */ 616 pcount -= firstpage; 617 } 618 619 /* 620 * calculate the size of the transfer 621 */ 622 623 size = pcount * PAGE_SIZE; 624 625 if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) > 626 obj->un_pager.vnp.vnp_size) 627 size = obj->un_pager.vnp.vnp_size - 628 IDX_TO_OFF(ap->a_m[firstpage]->pindex); 629 630 physoffset -= foff; 631 rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size, 632 (ap->a_reqpage - firstpage), physoffset); 633 634 return (rtval); 635 } 636 637 /* 638 * put page routine 639 * 640 * XXX By default, wimp out... note that a_offset is ignored (and always 641 * XXX has been). 642 */ 643 int 644 ffs_putpages(struct vop_putpages_args *ap) 645 { 646 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count, 647 ap->a_sync, ap->a_rtvals); 648 } 649