3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.4 2003/06/26 05:55:21 dillon Exp $
38 #define BLKSIZE(a, b, c) blksize(a, b, c)
42 #define READ_S "ffs_read"
43 #define WRITE ffs_write
44 #define WRITE_S "ffs_write"
47 #include <vm/vm_object.h>
48 #include <vm/vm_pager.h>
49 #include <vm/vm_map.h>
50 #include <vm/vnode_pager.h>
51 #include <sys/event.h>
52 #include <sys/vmmeter.h>
53 #include "opt_directio.h"
55 #define VN_KNOTE(vp, b) \
56 KNOTE((struct klist *)&vp->v_pollinfo.vpi_selinfo.si_note, (b))
59 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
63 * Vnode op for reading.
68 struct vop_read_args /* {
75 register struct vnode *vp;
76 register struct inode *ip;
77 register struct uio *uio;
80 ufs_daddr_t lbn, nextlbn;
82 long size, xfersize, blkoffset;
83 int error, orig_resid;
90 seqcount = ap->a_ioflag >> 16;
94 ioflag = ap->a_ioflag;
96 if ((ioflag & IO_DIRECT) != 0) {
99 error = ffs_rawread(vp, uio, &workdone);
100 if (error || workdone)
106 if (uio->uio_rw != UIO_READ)
107 panic("%s: mode", READ_S);
109 if (vp->v_type == VLNK) {
110 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
111 panic("%s: short symlink", READ_S);
112 } else if (vp->v_type != VREG && vp->v_type != VDIR)
113 panic("%s: type %d", READ_S, vp->v_type);
116 if ((u_int64_t)uio->uio_offset > fs->fs_maxfilesize)
119 orig_resid = uio->uio_resid;
123 object = vp->v_object;
125 bytesinfile = ip->i_size - uio->uio_offset;
126 if (bytesinfile <= 0) {
127 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
128 ip->i_flag |= IN_ACCESS;
133 vm_object_reference(object);
135 #ifdef ENABLE_VFS_IOOPT
137 * If IO optimisation is turned on,
138 * and we are NOT a VM based IO request,
139 * (i.e. not headed for the buffer cache)
140 * but there IS a vm object associated with it.
142 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
145 toread = uio->uio_resid;
146 if (toread > bytesinfile)
147 toread = bytesinfile;
148 if (toread >= PAGE_SIZE) {
150 * Then if it's at least a page in size, try
151 * get the data from the object using vm tricks
153 error = uioread(toread, uio, object, &nread);
154 if ((uio->uio_resid == 0) || (error != 0)) {
156 * If we finished or there was an error
157 * then finish up (the reference previously
158 * obtained on object must be released).
161 uio->uio_resid != orig_resid) &&
162 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
163 ip->i_flag |= IN_ACCESS;
166 vm_object_vndeallocate(object);
174 * Ok so we couldn't do it all in one vm trick...
175 * so cycle around trying smaller bites..
177 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
178 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
180 #ifdef ENABLE_VFS_IOOPT
181 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
183 * Obviously we didn't finish above, but we
184 * didn't get an error either. Try the same trick again.
185 * but this time we are looping.
188 toread = uio->uio_resid;
189 if (toread > bytesinfile)
190 toread = bytesinfile;
193 * Once again, if there isn't enough for a
194 * whole page, don't try optimising.
196 if (toread >= PAGE_SIZE) {
197 error = uioread(toread, uio, object, &nread);
198 if ((uio->uio_resid == 0) || (error != 0)) {
200 * If we finished or there was an
201 * error then finish up (the reference
202 * previously obtained on object must
206 uio->uio_resid != orig_resid) &&
207 (vp->v_mount->mnt_flag &
209 ip->i_flag |= IN_ACCESS;
211 vm_object_vndeallocate(object);
215 * To get here we didnt't finish or err.
216 * If we did get some data,
217 * loop to try another bite.
226 lbn = lblkno(fs, uio->uio_offset);
230 * size of buffer. The buffer representing the
231 * end of the file is rounded up to the size of
232 * the block type ( fragment or full block,
235 size = BLKSIZE(fs, ip, lbn);
236 blkoffset = blkoff(fs, uio->uio_offset);
239 * The amount we want to transfer in this iteration is
240 * one FS block less the amount of the data before
241 * our startpoint (duh!)
243 xfersize = fs->fs_bsize - blkoffset;
246 * But if we actually want less than the block,
247 * or the file doesn't have a whole block more of data,
248 * then use the lesser number.
250 if (uio->uio_resid < xfersize)
251 xfersize = uio->uio_resid;
252 if (bytesinfile < xfersize)
253 xfersize = bytesinfile;
255 if (lblktosize(fs, nextlbn) >= ip->i_size) {
257 * Don't do readahead if this is the end of the file.
259 error = bread(vp, lbn, size, &bp);
260 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
262 * Otherwise if we are allowed to cluster,
263 * grab as much as we can.
265 * XXX This may not be a win if we are not
266 * doing sequential access.
268 error = cluster_read(vp, ip->i_size, lbn,
269 size, uio->uio_resid, seqcount, &bp);
270 } else if (seqcount > 1) {
272 * If we are NOT allowed to cluster, then
273 * if we appear to be acting sequentially,
274 * fire off a request for a readahead
275 * as well as a read. Note that the 4th and 5th
276 * arguments point to arrays of the size specified in
279 int nextsize = BLKSIZE(fs, ip, nextlbn);
280 error = breadn(vp, lbn,
281 size, &nextlbn, &nextsize, 1, &bp);
284 * Failing all of the above, just read what the
285 * user asked for. Interestingly, the same as
286 * the first option above.
288 error = bread(vp, lbn, size, &bp);
297 * If IO_DIRECT then set B_DIRECT for the buffer. This
298 * will cause us to attempt to release the buffer later on
299 * and will cause the buffer cache to attempt to free the
302 if (ioflag & IO_DIRECT)
303 bp->b_flags |= B_DIRECT;
306 * We should only get non-zero b_resid when an I/O error
307 * has occurred, which should cause us to break above.
308 * However, if the short read did not cause an error,
309 * then we want to ensure that we do not uiomove bad
310 * or uninitialized data.
312 * XXX b_resid is only valid when an actual I/O has occured
313 * and may be incorrect if the buffer is B_CACHE or if the
314 * last op on the buffer was a failed write. This KASSERT
315 * is a precursor to removing it from the UFS code.
317 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
319 if (size < xfersize) {
325 #ifdef ENABLE_VFS_IOOPT
326 if (vfs_ioopt && object &&
327 (bp->b_flags & B_VMIO) &&
328 ((blkoffset & PAGE_MASK) == 0) &&
329 ((xfersize & PAGE_MASK) == 0)) {
331 * If VFS IO optimisation is turned on,
332 * and it's an exact page multiple
333 * And a normal VM based op,
334 * then use uiomiveco()
337 uiomoveco((char *)bp->b_data + blkoffset,
338 (int)xfersize, uio, object);
343 * otherwise use the general form
346 uiomove((char *)bp->b_data + blkoffset,
353 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
354 (LIST_FIRST(&bp->b_dep) == NULL)) {
356 * If there are no dependencies, and it's VMIO,
357 * then we don't need the buf, mark it available
358 * for freeing. The VM has the data.
360 bp->b_flags |= B_RELBUF;
364 * Otherwise let whoever
365 * made the request take care of
366 * freeing it. We just queue
367 * it onto another list.
374 * This can only happen in the case of an error
375 * because the loop above resets bp to NULL on each iteration
376 * and on normal completion has not set a new value into it.
377 * so it must have come from a 'break' statement
380 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
381 (LIST_FIRST(&bp->b_dep) == NULL)) {
382 bp->b_flags |= B_RELBUF;
390 vm_object_vndeallocate(object);
391 if ((error == 0 || uio->uio_resid != orig_resid) &&
392 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
393 ip->i_flag |= IN_ACCESS;
398 * Vnode op for writing.
402 struct vop_write_args /* {
406 struct ucred *a_cred;
409 register struct vnode *vp;
410 register struct uio *uio;
411 register struct inode *ip;
417 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
422 seqcount = ap->a_ioflag >> 16;
423 ioflag = ap->a_ioflag;
428 object = vp->v_object;
430 vm_object_reference(object);
433 if (uio->uio_rw != UIO_WRITE)
434 panic("%s: mode", WRITE_S);
437 switch (vp->v_type) {
439 if (ioflag & IO_APPEND)
440 uio->uio_offset = ip->i_size;
441 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) {
443 vm_object_vndeallocate(object);
450 panic("%s: dir write", WRITE_S);
453 panic("%s: type %p %d (%d,%d)", WRITE_S, vp, (int)vp->v_type,
454 (int)uio->uio_offset,
460 if (uio->uio_offset < 0 ||
461 (u_int64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
463 vm_object_vndeallocate(object);
467 * Maybe this should be above the vnode op call, but so long as
468 * file servers have no limits, I don't think it matters.
471 if (vp->v_type == VREG && td->td_proc &&
472 uio->uio_offset + uio->uio_resid >
473 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
474 psignal(td->td_proc, SIGXFSZ);
476 vm_object_vndeallocate(object);
480 resid = uio->uio_resid;
484 * NOTE! These B_ flags are actually balloc-only flags, not buffer
485 * flags. They are similar to the BA_ flags in -current.
487 if (seqcount > B_SEQMAX)
488 flags = B_SEQMAX << B_SEQSHIFT;
490 flags = seqcount << B_SEQSHIFT;
491 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
494 if (object && (object->flags & OBJ_OPT)) {
495 vm_freeze_copyopts(object,
496 OFF_TO_IDX(uio->uio_offset),
497 OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK));
500 for (error = 0; uio->uio_resid > 0;) {
501 lbn = lblkno(fs, uio->uio_offset);
502 blkoffset = blkoff(fs, uio->uio_offset);
503 xfersize = fs->fs_bsize - blkoffset;
504 if (uio->uio_resid < xfersize)
505 xfersize = uio->uio_resid;
507 if (uio->uio_offset + xfersize > ip->i_size)
508 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
511 * We must perform a read-before-write if the transfer
512 * size does not cover the entire buffer.
514 if (fs->fs_bsize > xfersize)
518 /* XXX is uio->uio_offset the right thing here? */
519 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
520 ap->a_cred, flags, &bp);
524 * If the buffer is not valid and we did not clear garbage
525 * out above, we have to do so here even though the write
526 * covers the entire buffer in order to avoid a mmap()/write
527 * race where another process may see the garbage prior to
528 * the uiomove() for a write replacing it.
530 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
532 if (ioflag & IO_DIRECT)
533 bp->b_flags |= B_DIRECT;
534 if (ioflag & IO_NOWDRAIN)
535 bp->b_flags |= B_NOWDRAIN;
537 if (uio->uio_offset + xfersize > ip->i_size) {
538 ip->i_size = uio->uio_offset + xfersize;
542 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
547 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
548 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
549 (LIST_FIRST(&bp->b_dep) == NULL)) {
550 bp->b_flags |= B_RELBUF;
554 * If IO_SYNC each buffer is written synchronously. Otherwise
555 * if we have a severe page deficiency write the buffer
556 * asynchronously. Otherwise try to cluster, and if that
557 * doesn't do it then either do an async write (if O_DIRECT),
558 * or a delayed write (if not).
561 if (ioflag & IO_SYNC) {
563 } else if (vm_page_count_severe() ||
564 buf_dirty_count_severe() ||
565 (ioflag & IO_ASYNC)) {
566 bp->b_flags |= B_CLUSTEROK;
568 } else if (xfersize + blkoffset == fs->fs_bsize) {
569 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
570 bp->b_flags |= B_CLUSTEROK;
571 cluster_write(bp, ip->i_size, seqcount);
575 } else if (ioflag & IO_DIRECT) {
576 bp->b_flags |= B_CLUSTEROK;
579 bp->b_flags |= B_CLUSTEROK;
582 if (error || xfersize == 0)
584 ip->i_flag |= IN_CHANGE | IN_UPDATE;
587 * If we successfully wrote any data, and we are not the superuser
588 * we clear the setuid and setgid bits as a precaution against
591 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
592 ip->i_mode &= ~(ISUID | ISGID);
593 if (resid > uio->uio_resid)
594 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
596 if (ioflag & IO_UNIT) {
597 (void)UFS_TRUNCATE(vp, osize,
598 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
599 uio->uio_offset -= resid - uio->uio_resid;
600 uio->uio_resid = resid;
602 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
603 error = UFS_UPDATE(vp, 1);
606 vm_object_vndeallocate(object);
617 struct vop_getpages_args *ap;
619 off_t foff, physoffset;
621 struct vnode *dp, *vp;
623 vm_pindex_t pindex, firstindex;
625 int bbackwards, bforwards;
626 int pbackwards, pforwards;
636 pcount = round_page(ap->a_count) / PAGE_SIZE;
637 mreq = ap->a_m[ap->a_reqpage];
638 firstindex = ap->a_m[0]->pindex;
641 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
642 * then the entire page is valid. Since the page may be mapped,
643 * user programs might reference data beyond the actual end of file
644 * occuring within the page. We have to zero that data.
647 if (mreq->valid != VM_PAGE_BITS_ALL)
648 vm_page_zero_invalid(mreq, TRUE);
649 for (i = 0; i < pcount; i++) {
650 if (i != ap->a_reqpage) {
651 vm_page_free(ap->a_m[i]);
659 bsize = vp->v_mount->mnt_stat.f_iosize;
660 pindex = mreq->pindex;
661 foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
663 if (bsize < PAGE_SIZE)
664 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
669 * foff is the file offset of the required page
670 * reqlblkno is the logical block that contains the page
671 * poff is the index of the page into the logical block
673 reqlblkno = foff / bsize;
674 poff = (foff % bsize) / PAGE_SIZE;
676 if ( VOP_BMAP( vp, reqlblkno, &dp, &reqblkno,
677 &bforwards, &bbackwards) || (reqblkno == -1)) {
678 for(i = 0; i < pcount; i++) {
679 if (i != ap->a_reqpage)
680 vm_page_free(ap->a_m[i]);
682 if (reqblkno == -1) {
683 if ((mreq->flags & PG_ZERO) == 0)
684 vm_page_zero_fill(mreq);
685 vm_page_undirty(mreq);
686 mreq->valid = VM_PAGE_BITS_ALL;
689 return VM_PAGER_ERROR;
693 physoffset = (off_t)reqblkno * DEV_BSIZE + poff * PAGE_SIZE;
694 pagesperblock = bsize / PAGE_SIZE;
696 * find the first page that is contiguous...
697 * note that pbackwards is the number of pages that are contiguous
702 pbackwards = poff + bbackwards * pagesperblock;
703 if (ap->a_reqpage > pbackwards) {
704 firstpage = ap->a_reqpage - pbackwards;
705 for(i=0;i<firstpage;i++)
706 vm_page_free(ap->a_m[i]);
710 * pforwards is the number of pages that are contiguous
711 * after the current page.
713 pforwards = (pagesperblock - (poff + 1)) +
714 bforwards * pagesperblock;
715 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
716 for( i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
717 vm_page_free(ap->a_m[i]);
718 pcount = ap->a_reqpage + pforwards + 1;
722 * number of pages for I/O corrected for the non-contig pages at
723 * the beginning of the array.
729 * calculate the size of the transfer
732 size = pcount * PAGE_SIZE;
734 if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) >
735 obj->un_pager.vnp.vnp_size)
736 size = obj->un_pager.vnp.vnp_size -
737 IDX_TO_OFF(ap->a_m[firstpage]->pindex);
740 rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
741 (ap->a_reqpage - firstpage), physoffset);
749 * XXX By default, wimp out... note that a_offset is ignored (and always
754 struct vop_putpages_args *ap;
756 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
757 ap->a_sync, ap->a_rtvals);