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.6 2003/06/26 19:24:50 dillon 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 "opt_directio.h"
51 #define VN_KNOTE(vp, b) \
52 KNOTE((struct klist *)&vp->v_pollinfo.vpi_selinfo.si_note, (b))
55 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
59 * Vnode op for reading.
64 struct vop_read_args /* {
71 register struct vnode *vp;
72 register struct inode *ip;
73 register struct uio *uio;
76 ufs_daddr_t lbn, nextlbn;
78 long size, xfersize, blkoffset;
79 int error, orig_resid;
86 seqcount = ap->a_ioflag >> 16;
90 ioflag = ap->a_ioflag;
92 if ((ioflag & IO_DIRECT) != 0) {
95 error = ffs_rawread(vp, uio, &workdone);
96 if (error || workdone)
102 if (uio->uio_rw != UIO_READ)
103 panic("ffs_read: mode");
105 if (vp->v_type == VLNK) {
106 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
107 panic("ffs_read: short symlink");
108 } else if (vp->v_type != VREG && vp->v_type != VDIR)
109 panic("ffs_read: type %d", vp->v_type);
112 if ((u_int64_t)uio->uio_offset > fs->fs_maxfilesize)
115 orig_resid = uio->uio_resid;
119 object = vp->v_object;
121 bytesinfile = ip->i_size - uio->uio_offset;
122 if (bytesinfile <= 0) {
123 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
124 ip->i_flag |= IN_ACCESS;
129 vm_object_reference(object);
131 #ifdef ENABLE_VFS_IOOPT
133 * If IO optimisation is turned on,
134 * and we are NOT a VM based IO request,
135 * (i.e. not headed for the buffer cache)
136 * but there IS a vm object associated with it.
138 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
141 toread = uio->uio_resid;
142 if (toread > bytesinfile)
143 toread = bytesinfile;
144 if (toread >= PAGE_SIZE) {
146 * Then if it's at least a page in size, try
147 * get the data from the object using vm tricks
149 error = uioread(toread, uio, object, &nread);
150 if ((uio->uio_resid == 0) || (error != 0)) {
152 * If we finished or there was an error
153 * then finish up (the reference previously
154 * obtained on object must be released).
157 uio->uio_resid != orig_resid) &&
158 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
159 ip->i_flag |= IN_ACCESS;
162 vm_object_vndeallocate(object);
170 * Ok so we couldn't do it all in one vm trick...
171 * so cycle around trying smaller bites..
173 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
174 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
176 #ifdef ENABLE_VFS_IOOPT
177 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
179 * Obviously we didn't finish above, but we
180 * didn't get an error either. Try the same trick again.
181 * but this time we are looping.
184 toread = uio->uio_resid;
185 if (toread > bytesinfile)
186 toread = bytesinfile;
189 * Once again, if there isn't enough for a
190 * whole page, don't try optimising.
192 if (toread >= PAGE_SIZE) {
193 error = uioread(toread, uio, object, &nread);
194 if ((uio->uio_resid == 0) || (error != 0)) {
196 * If we finished or there was an
197 * error then finish up (the reference
198 * previously obtained on object must
202 uio->uio_resid != orig_resid) &&
203 (vp->v_mount->mnt_flag &
205 ip->i_flag |= IN_ACCESS;
207 vm_object_vndeallocate(object);
211 * To get here we didnt't finish or err.
212 * If we did get some data,
213 * loop to try another bite.
222 lbn = lblkno(fs, uio->uio_offset);
226 * size of buffer. The buffer representing the
227 * end of the file is rounded up to the size of
228 * the block type ( fragment or full block,
231 size = BLKSIZE(fs, ip, lbn);
232 blkoffset = blkoff(fs, uio->uio_offset);
235 * The amount we want to transfer in this iteration is
236 * one FS block less the amount of the data before
237 * our startpoint (duh!)
239 xfersize = fs->fs_bsize - blkoffset;
242 * But if we actually want less than the block,
243 * or the file doesn't have a whole block more of data,
244 * then use the lesser number.
246 if (uio->uio_resid < xfersize)
247 xfersize = uio->uio_resid;
248 if (bytesinfile < xfersize)
249 xfersize = bytesinfile;
251 if (lblktosize(fs, nextlbn) >= ip->i_size) {
253 * Don't do readahead if this is the end of the file.
255 error = bread(vp, lbn, size, &bp);
256 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
258 * Otherwise if we are allowed to cluster,
259 * grab as much as we can.
261 * XXX This may not be a win if we are not
262 * doing sequential access.
264 error = cluster_read(vp, ip->i_size, lbn,
265 size, uio->uio_resid, seqcount, &bp);
266 } else if (seqcount > 1) {
268 * If we are NOT allowed to cluster, then
269 * if we appear to be acting sequentially,
270 * fire off a request for a readahead
271 * as well as a read. Note that the 4th and 5th
272 * arguments point to arrays of the size specified in
275 int nextsize = BLKSIZE(fs, ip, nextlbn);
276 error = breadn(vp, lbn,
277 size, &nextlbn, &nextsize, 1, &bp);
280 * Failing all of the above, just read what the
281 * user asked for. Interestingly, the same as
282 * the first option above.
284 error = bread(vp, lbn, size, &bp);
293 * If IO_DIRECT then set B_DIRECT for the buffer. This
294 * will cause us to attempt to release the buffer later on
295 * and will cause the buffer cache to attempt to free the
298 if (ioflag & IO_DIRECT)
299 bp->b_flags |= B_DIRECT;
302 * We should only get non-zero b_resid when an I/O error
303 * has occurred, which should cause us to break above.
304 * However, if the short read did not cause an error,
305 * then we want to ensure that we do not uiomove bad
306 * or uninitialized data.
308 * XXX b_resid is only valid when an actual I/O has occured
309 * and may be incorrect if the buffer is B_CACHE or if the
310 * last op on the buffer was a failed write. This KASSERT
311 * is a precursor to removing it from the UFS code.
313 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
315 if (size < xfersize) {
321 #ifdef ENABLE_VFS_IOOPT
322 if (vfs_ioopt && object &&
323 (bp->b_flags & B_VMIO) &&
324 ((blkoffset & PAGE_MASK) == 0) &&
325 ((xfersize & PAGE_MASK) == 0)) {
327 * If VFS IO optimisation is turned on,
328 * and it's an exact page multiple
329 * And a normal VM based op,
330 * then use uiomiveco()
333 uiomoveco((char *)bp->b_data + blkoffset,
334 (int)xfersize, uio, object);
339 * otherwise use the general form
342 uiomove((char *)bp->b_data + blkoffset,
349 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
350 (LIST_FIRST(&bp->b_dep) == NULL)) {
352 * If there are no dependencies, and it's VMIO,
353 * then we don't need the buf, mark it available
354 * for freeing. The VM has the data.
356 bp->b_flags |= B_RELBUF;
360 * Otherwise let whoever
361 * made the request take care of
362 * freeing it. We just queue
363 * it onto another list.
370 * This can only happen in the case of an error
371 * because the loop above resets bp to NULL on each iteration
372 * and on normal completion has not set a new value into it.
373 * so it must have come from a 'break' statement
376 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
377 (LIST_FIRST(&bp->b_dep) == NULL)) {
378 bp->b_flags |= B_RELBUF;
386 vm_object_vndeallocate(object);
387 if ((error == 0 || uio->uio_resid != orig_resid) &&
388 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
389 ip->i_flag |= IN_ACCESS;
394 * Vnode op for writing.
398 struct vop_write_args /* {
402 struct ucred *a_cred;
405 register struct vnode *vp;
406 register struct uio *uio;
407 register struct inode *ip;
413 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
418 seqcount = ap->a_ioflag >> 16;
419 ioflag = ap->a_ioflag;
424 object = vp->v_object;
426 vm_object_reference(object);
429 if (uio->uio_rw != UIO_WRITE)
430 panic("ffs_write: mode");
433 switch (vp->v_type) {
435 if (ioflag & IO_APPEND)
436 uio->uio_offset = ip->i_size;
437 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) {
439 vm_object_vndeallocate(object);
446 panic("ffs_write: dir write");
449 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
450 (int)uio->uio_offset,
456 if (uio->uio_offset < 0 ||
457 (u_int64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
459 vm_object_vndeallocate(object);
463 * Maybe this should be above the vnode op call, but so long as
464 * file servers have no limits, I don't think it matters.
467 if (vp->v_type == VREG && td->td_proc &&
468 uio->uio_offset + uio->uio_resid >
469 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
470 psignal(td->td_proc, SIGXFSZ);
472 vm_object_vndeallocate(object);
476 resid = uio->uio_resid;
480 * NOTE! These B_ flags are actually balloc-only flags, not buffer
481 * flags. They are similar to the BA_ flags in -current.
483 if (seqcount > B_SEQMAX)
484 flags = B_SEQMAX << B_SEQSHIFT;
486 flags = seqcount << B_SEQSHIFT;
487 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
490 if (object && (object->flags & OBJ_OPT)) {
491 vm_freeze_copyopts(object,
492 OFF_TO_IDX(uio->uio_offset),
493 OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK));
496 for (error = 0; uio->uio_resid > 0;) {
497 lbn = lblkno(fs, uio->uio_offset);
498 blkoffset = blkoff(fs, uio->uio_offset);
499 xfersize = fs->fs_bsize - blkoffset;
500 if (uio->uio_resid < xfersize)
501 xfersize = uio->uio_resid;
503 if (uio->uio_offset + xfersize > ip->i_size)
504 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
507 * We must perform a read-before-write if the transfer
508 * size does not cover the entire buffer.
510 if (fs->fs_bsize > xfersize)
514 /* XXX is uio->uio_offset the right thing here? */
515 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
516 ap->a_cred, flags, &bp);
520 * If the buffer is not valid and we did not clear garbage
521 * out above, we have to do so here even though the write
522 * covers the entire buffer in order to avoid a mmap()/write
523 * race where another process may see the garbage prior to
524 * the uiomove() for a write replacing it.
526 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
528 if (ioflag & IO_DIRECT)
529 bp->b_flags |= B_DIRECT;
530 if (ioflag & IO_NOWDRAIN)
531 bp->b_flags |= B_NOWDRAIN;
533 if (uio->uio_offset + xfersize > ip->i_size) {
534 ip->i_size = uio->uio_offset + xfersize;
538 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
543 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
544 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
545 (LIST_FIRST(&bp->b_dep) == NULL)) {
546 bp->b_flags |= B_RELBUF;
550 * If IO_SYNC each buffer is written synchronously. Otherwise
551 * if we have a severe page deficiency write the buffer
552 * asynchronously. Otherwise try to cluster, and if that
553 * doesn't do it then either do an async write (if O_DIRECT),
554 * or a delayed write (if not).
557 if (ioflag & IO_SYNC) {
559 } else if (vm_page_count_severe() ||
560 buf_dirty_count_severe() ||
561 (ioflag & IO_ASYNC)) {
562 bp->b_flags |= B_CLUSTEROK;
564 } else if (xfersize + blkoffset == fs->fs_bsize) {
565 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
566 bp->b_flags |= B_CLUSTEROK;
567 cluster_write(bp, ip->i_size, seqcount);
571 } else if (ioflag & IO_DIRECT) {
572 bp->b_flags |= B_CLUSTEROK;
575 bp->b_flags |= B_CLUSTEROK;
578 if (error || xfersize == 0)
580 ip->i_flag |= IN_CHANGE | IN_UPDATE;
583 * If we successfully wrote any data, and we are not the superuser
584 * we clear the setuid and setgid bits as a precaution against
587 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
588 ip->i_mode &= ~(ISUID | ISGID);
589 if (resid > uio->uio_resid)
590 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
592 if (ioflag & IO_UNIT) {
593 (void)UFS_TRUNCATE(vp, osize,
594 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
595 uio->uio_offset -= resid - uio->uio_resid;
596 uio->uio_resid = resid;
598 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
599 error = UFS_UPDATE(vp, 1);
602 vm_object_vndeallocate(object);
613 struct vop_getpages_args *ap;
615 off_t foff, physoffset;
617 struct vnode *dp, *vp;
619 vm_pindex_t pindex, firstindex;
621 int bbackwards, bforwards;
622 int pbackwards, pforwards;
632 pcount = round_page(ap->a_count) / PAGE_SIZE;
633 mreq = ap->a_m[ap->a_reqpage];
634 firstindex = ap->a_m[0]->pindex;
637 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
638 * then the entire page is valid. Since the page may be mapped,
639 * user programs might reference data beyond the actual end of file
640 * occuring within the page. We have to zero that data.
643 if (mreq->valid != VM_PAGE_BITS_ALL)
644 vm_page_zero_invalid(mreq, TRUE);
645 for (i = 0; i < pcount; i++) {
646 if (i != ap->a_reqpage) {
647 vm_page_free(ap->a_m[i]);
655 bsize = vp->v_mount->mnt_stat.f_iosize;
656 pindex = mreq->pindex;
657 foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
659 if (bsize < PAGE_SIZE)
660 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
665 * foff is the file offset of the required page
666 * reqlblkno is the logical block that contains the page
667 * poff is the index of the page into the logical block
669 reqlblkno = foff / bsize;
670 poff = (foff % bsize) / PAGE_SIZE;
672 if ( VOP_BMAP( vp, reqlblkno, &dp, &reqblkno,
673 &bforwards, &bbackwards) || (reqblkno == -1)) {
674 for(i = 0; i < pcount; i++) {
675 if (i != ap->a_reqpage)
676 vm_page_free(ap->a_m[i]);
678 if (reqblkno == -1) {
679 if ((mreq->flags & PG_ZERO) == 0)
680 vm_page_zero_fill(mreq);
681 vm_page_undirty(mreq);
682 mreq->valid = VM_PAGE_BITS_ALL;
685 return VM_PAGER_ERROR;
689 physoffset = (off_t)reqblkno * DEV_BSIZE + poff * PAGE_SIZE;
690 pagesperblock = bsize / PAGE_SIZE;
692 * find the first page that is contiguous...
693 * note that pbackwards is the number of pages that are contiguous
698 pbackwards = poff + bbackwards * pagesperblock;
699 if (ap->a_reqpage > pbackwards) {
700 firstpage = ap->a_reqpage - pbackwards;
701 for(i=0;i<firstpage;i++)
702 vm_page_free(ap->a_m[i]);
706 * pforwards is the number of pages that are contiguous
707 * after the current page.
709 pforwards = (pagesperblock - (poff + 1)) +
710 bforwards * pagesperblock;
711 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
712 for( i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
713 vm_page_free(ap->a_m[i]);
714 pcount = ap->a_reqpage + pforwards + 1;
718 * number of pages for I/O corrected for the non-contig pages at
719 * the beginning of the array.
725 * calculate the size of the transfer
728 size = pcount * PAGE_SIZE;
730 if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) >
731 obj->un_pager.vnp.vnp_size)
732 size = obj->un_pager.vnp.vnp_size -
733 IDX_TO_OFF(ap->a_m[firstpage]->pindex);
736 rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
737 (ap->a_reqpage - firstpage), physoffset);
745 * XXX By default, wimp out... note that a_offset is ignored (and always
750 struct vop_putpages_args *ap;
752 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
753 ap->a_sync, ap->a_rtvals);