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.12 2004/07/18 19:43:48 drhodus Exp $
38 #define BLKSIZE(a, b, c) blksize(a, b, c)
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>
51 #include "opt_directio.h"
53 #define VN_KNOTE(vp, b) \
54 KNOTE((struct klist *)&vp->v_pollinfo.vpi_selinfo.si_note, (b))
57 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
61 * Vnode op for reading.
63 * ffs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
64 * struct ucred *a_cred)
68 ffs_read(struct vop_read_args *ap)
75 ufs_daddr_t lbn, nextlbn;
77 long size, xfersize, blkoffset;
78 int error, orig_resid;
85 seqcount = ap->a_ioflag >> 16;
89 ioflag = ap->a_ioflag;
91 if ((ioflag & IO_DIRECT) != 0) {
94 error = ffs_rawread(vp, uio, &workdone);
95 if (error || workdone)
101 if (uio->uio_rw != UIO_READ)
102 panic("ffs_read: mode");
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);
111 if ((uint64_t)uio->uio_offset > fs->fs_maxfilesize)
114 orig_resid = uio->uio_resid;
118 object = vp->v_object;
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;
128 vm_object_reference(object);
130 #ifdef ENABLE_VFS_IOOPT
132 * If IO optimisation is turned on,
133 * and we are NOT a VM based IO request,
134 * (i.e. not headed for the buffer cache)
135 * but there IS a vm object associated with it.
137 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
140 toread = uio->uio_resid;
141 if (toread > bytesinfile)
142 toread = bytesinfile;
143 if (toread >= PAGE_SIZE) {
145 * Then if it's at least a page in size, try
146 * get the data from the object using vm tricks
148 error = uioread(toread, uio, object, &nread);
149 if ((uio->uio_resid == 0) || (error != 0)) {
151 * If we finished or there was an error
152 * then finish up (the reference previously
153 * obtained on object must be released).
156 uio->uio_resid != orig_resid) &&
157 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
158 ip->i_flag |= IN_ACCESS;
161 vm_object_vndeallocate(object);
169 * Ok so we couldn't do it all in one vm trick...
170 * so cycle around trying smaller bites..
172 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
173 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
175 #ifdef ENABLE_VFS_IOOPT
176 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
178 * Obviously we didn't finish above, but we
179 * didn't get an error either. Try the same trick again.
180 * but this time we are looping.
183 toread = uio->uio_resid;
184 if (toread > bytesinfile)
185 toread = bytesinfile;
188 * Once again, if there isn't enough for a
189 * whole page, don't try optimising.
191 if (toread >= PAGE_SIZE) {
192 error = uioread(toread, uio, object, &nread);
193 if ((uio->uio_resid == 0) || (error != 0)) {
195 * If we finished or there was an
196 * error then finish up (the reference
197 * previously obtained on object must
201 uio->uio_resid != orig_resid) &&
202 (vp->v_mount->mnt_flag &
204 ip->i_flag |= IN_ACCESS;
206 vm_object_vndeallocate(object);
210 * To get here we didnt't finish or err.
211 * If we did get some data,
212 * loop to try another bite.
221 lbn = lblkno(fs, uio->uio_offset);
225 * size of buffer. The buffer representing the
226 * end of the file is rounded up to the size of
227 * the block type ( fragment or full block,
230 size = BLKSIZE(fs, ip, lbn);
231 blkoffset = blkoff(fs, uio->uio_offset);
234 * The amount we want to transfer in this iteration is
235 * one FS block less the amount of the data before
236 * our startpoint (duh!)
238 xfersize = fs->fs_bsize - blkoffset;
241 * But if we actually want less than the block,
242 * or the file doesn't have a whole block more of data,
243 * then use the lesser number.
245 if (uio->uio_resid < xfersize)
246 xfersize = uio->uio_resid;
247 if (bytesinfile < xfersize)
248 xfersize = bytesinfile;
250 if (lblktosize(fs, nextlbn) >= ip->i_size) {
252 * Don't do readahead if this is the end of the file.
254 error = bread(vp, lbn, size, &bp);
255 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
257 * Otherwise if we are allowed to cluster,
258 * grab as much as we can.
260 * XXX This may not be a win if we are not
261 * doing sequential access.
263 error = cluster_read(vp, ip->i_size, lbn,
264 size, uio->uio_resid, seqcount, &bp);
265 } else if (seqcount > 1) {
267 * If we are NOT allowed to cluster, then
268 * if we appear to be acting sequentially,
269 * fire off a request for a readahead
270 * as well as a read. Note that the 4th and 5th
271 * arguments point to arrays of the size specified in
274 int nextsize = BLKSIZE(fs, ip, nextlbn);
275 error = breadn(vp, lbn,
276 size, &nextlbn, &nextsize, 1, &bp);
279 * Failing all of the above, just read what the
280 * user asked for. Interestingly, the same as
281 * the first option above.
283 error = bread(vp, lbn, size, &bp);
292 * If IO_DIRECT then set B_DIRECT for the buffer. This
293 * will cause us to attempt to release the buffer later on
294 * and will cause the buffer cache to attempt to free the
297 if (ioflag & IO_DIRECT)
298 bp->b_flags |= B_DIRECT;
301 * We should only get non-zero b_resid when an I/O error
302 * has occurred, which should cause us to break above.
303 * However, if the short read did not cause an error,
304 * then we want to ensure that we do not uiomove bad
305 * or uninitialized data.
307 * XXX b_resid is only valid when an actual I/O has occured
308 * and may be incorrect if the buffer is B_CACHE or if the
309 * last op on the buffer was a failed write. This KASSERT
310 * is a precursor to removing it from the UFS code.
312 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
314 if (size < xfersize) {
320 #ifdef ENABLE_VFS_IOOPT
321 if (vfs_ioopt && object &&
322 (bp->b_flags & B_VMIO) &&
323 ((blkoffset & PAGE_MASK) == 0) &&
324 ((xfersize & PAGE_MASK) == 0)) {
326 * If VFS IO optimisation is turned on,
327 * and it's an exact page multiple
328 * And a normal VM based op,
329 * then use uiomiveco()
332 uiomoveco((char *)bp->b_data + blkoffset,
333 (int)xfersize, uio, object);
338 * otherwise use the general form
341 uiomove((char *)bp->b_data + blkoffset,
348 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
349 (LIST_FIRST(&bp->b_dep) == NULL)) {
351 * If there are no dependencies, and it's VMIO,
352 * then we don't need the buf, mark it available
353 * for freeing. The VM has the data.
355 bp->b_flags |= B_RELBUF;
359 * Otherwise let whoever
360 * made the request take care of
361 * freeing it. We just queue
362 * it onto another list.
369 * This can only happen in the case of an error
370 * because the loop above resets bp to NULL on each iteration
371 * and on normal completion has not set a new value into it.
372 * so it must have come from a 'break' statement
375 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
376 (LIST_FIRST(&bp->b_dep) == NULL)) {
377 bp->b_flags |= B_RELBUF;
385 vm_object_vndeallocate(object);
386 if ((error == 0 || uio->uio_resid != orig_resid) &&
387 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
388 ip->i_flag |= IN_ACCESS;
393 * Vnode op for writing.
395 * ffs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
396 * struct ucred *a_cred)
399 ffs_write(struct vop_write_args *ap)
409 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
414 seqcount = ap->a_ioflag >> 16;
415 ioflag = ap->a_ioflag;
420 object = vp->v_object;
422 vm_object_reference(object);
425 if (uio->uio_rw != UIO_WRITE)
426 panic("ffs_write: mode");
429 switch (vp->v_type) {
431 if (ioflag & IO_APPEND)
432 uio->uio_offset = ip->i_size;
433 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) {
435 vm_object_vndeallocate(object);
442 panic("ffs_write: dir write");
445 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
446 (int)uio->uio_offset,
452 if (uio->uio_offset < 0 ||
453 (uint64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
455 vm_object_vndeallocate(object);
459 * Maybe this should be above the vnode op call, but so long as
460 * file servers have no limits, I don't think it matters.
463 if (vp->v_type == VREG && td && td->td_proc &&
464 uio->uio_offset + uio->uio_resid >
465 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
466 psignal(td->td_proc, SIGXFSZ);
468 vm_object_vndeallocate(object);
472 resid = uio->uio_resid;
476 * NOTE! These B_ flags are actually balloc-only flags, not buffer
477 * flags. They are similar to the BA_ flags in fbsd.
479 if (seqcount > B_SEQMAX)
480 flags = B_SEQMAX << B_SEQSHIFT;
482 flags = seqcount << B_SEQSHIFT;
483 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
486 if (object && (object->flags & OBJ_OPT)) {
487 vm_freeze_copyopts(object,
488 OFF_TO_IDX(uio->uio_offset),
489 OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK));
492 for (error = 0; uio->uio_resid > 0;) {
493 lbn = lblkno(fs, uio->uio_offset);
494 blkoffset = blkoff(fs, uio->uio_offset);
495 xfersize = fs->fs_bsize - blkoffset;
496 if (uio->uio_resid < xfersize)
497 xfersize = uio->uio_resid;
499 if (uio->uio_offset + xfersize > ip->i_size)
500 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
503 * We must perform a read-before-write if the transfer
504 * size does not cover the entire buffer.
506 if (fs->fs_bsize > xfersize)
510 /* XXX is uio->uio_offset the right thing here? */
511 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
512 ap->a_cred, flags, &bp);
516 * If the buffer is not valid and we did not clear garbage
517 * out above, we have to do so here even though the write
518 * covers the entire buffer in order to avoid a mmap()/write
519 * race where another process may see the garbage prior to
520 * the uiomove() for a write replacing it.
522 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
524 if (ioflag & IO_DIRECT)
525 bp->b_flags |= B_DIRECT;
526 if (ioflag & IO_NOWDRAIN)
527 bp->b_flags |= B_NOWDRAIN;
528 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
529 bp->b_flags |= B_NOCACHE;
531 if (uio->uio_offset + xfersize > ip->i_size) {
532 ip->i_size = uio->uio_offset + xfersize;
536 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
541 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
542 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
543 (LIST_FIRST(&bp->b_dep) == NULL)) {
544 bp->b_flags |= B_RELBUF;
548 * If IO_SYNC each buffer is written synchronously. Otherwise
549 * if we have a severe page deficiency write the buffer
550 * asynchronously. Otherwise try to cluster, and if that
551 * doesn't do it then either do an async write (if O_DIRECT),
552 * or a delayed write (if not).
555 if (ioflag & IO_SYNC) {
557 } else if (vm_page_count_severe() ||
558 buf_dirty_count_severe() ||
559 (ioflag & IO_ASYNC)) {
560 bp->b_flags |= B_CLUSTEROK;
562 } else if (xfersize + blkoffset == fs->fs_bsize) {
563 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
564 bp->b_flags |= B_CLUSTEROK;
565 cluster_write(bp, ip->i_size, seqcount);
569 } else if (ioflag & IO_DIRECT) {
570 bp->b_flags |= B_CLUSTEROK;
573 bp->b_flags |= B_CLUSTEROK;
576 if (error || xfersize == 0)
578 ip->i_flag |= IN_CHANGE | IN_UPDATE;
581 * If we successfully wrote any data, and we are not the superuser
582 * we clear the setuid and setgid bits as a precaution against
585 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
586 ip->i_mode &= ~(ISUID | ISGID);
587 if (resid > uio->uio_resid)
588 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
590 if (ioflag & IO_UNIT) {
591 (void)UFS_TRUNCATE(vp, osize,
592 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
593 uio->uio_offset -= resid - uio->uio_resid;
594 uio->uio_resid = resid;
596 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
597 error = UFS_UPDATE(vp, 1);
600 vm_object_vndeallocate(object);
610 ffs_getpages(struct vop_getpages_args *ap)
612 off_t foff, physoffset;
614 struct vnode *dp, *vp;
616 vm_pindex_t pindex, firstindex;
618 int bbackwards, bforwards;
619 int pbackwards, pforwards;
629 pcount = round_page(ap->a_count) / PAGE_SIZE;
630 mreq = ap->a_m[ap->a_reqpage];
631 firstindex = ap->a_m[0]->pindex;
634 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
635 * then the entire page is valid. Since the page may be mapped,
636 * user programs might reference data beyond the actual end of file
637 * occuring within the page. We have to zero that data.
640 if (mreq->valid != VM_PAGE_BITS_ALL)
641 vm_page_zero_invalid(mreq, TRUE);
642 for (i = 0; i < pcount; i++) {
643 if (i != ap->a_reqpage) {
644 vm_page_free(ap->a_m[i]);
652 bsize = vp->v_mount->mnt_stat.f_iosize;
653 pindex = mreq->pindex;
654 foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
656 if (bsize < PAGE_SIZE)
657 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
662 * foff is the file offset of the required page
663 * reqlblkno is the logical block that contains the page
664 * poff is the index of the page into the logical block
666 reqlblkno = foff / bsize;
667 poff = (foff % bsize) / PAGE_SIZE;
669 if ( VOP_BMAP( vp, reqlblkno, &dp, &reqblkno,
670 &bforwards, &bbackwards) || (reqblkno == -1)) {
671 for(i = 0; i < pcount; i++) {
672 if (i != ap->a_reqpage)
673 vm_page_free(ap->a_m[i]);
675 if (reqblkno == -1) {
676 if ((mreq->flags & PG_ZERO) == 0)
677 vm_page_zero_fill(mreq);
678 vm_page_undirty(mreq);
679 mreq->valid = VM_PAGE_BITS_ALL;
682 return VM_PAGER_ERROR;
686 physoffset = (off_t)reqblkno * DEV_BSIZE + poff * PAGE_SIZE;
687 pagesperblock = bsize / PAGE_SIZE;
689 * find the first page that is contiguous...
690 * note that pbackwards is the number of pages that are contiguous
695 pbackwards = poff + bbackwards * pagesperblock;
696 if (ap->a_reqpage > pbackwards) {
697 firstpage = ap->a_reqpage - pbackwards;
698 for(i=0;i<firstpage;i++)
699 vm_page_free(ap->a_m[i]);
703 * pforwards is the number of pages that are contiguous
704 * after the current page.
706 pforwards = (pagesperblock - (poff + 1)) +
707 bforwards * pagesperblock;
708 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
709 for( i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
710 vm_page_free(ap->a_m[i]);
711 pcount = ap->a_reqpage + pforwards + 1;
715 * number of pages for I/O corrected for the non-contig pages at
716 * the beginning of the array.
722 * calculate the size of the transfer
725 size = pcount * PAGE_SIZE;
727 if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) >
728 obj->un_pager.vnp.vnp_size)
729 size = obj->un_pager.vnp.vnp_size -
730 IDX_TO_OFF(ap->a_m[firstpage]->pindex);
733 rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
734 (ap->a_reqpage - firstpage), physoffset);
742 * XXX By default, wimp out... note that a_offset is ignored (and always
746 ffs_putpages(struct vop_putpages_args *ap)
748 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
749 ap->a_sync, ap->a_rtvals);