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.9 2003/07/26 22:04:27 rob 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.
66 struct vop_read_args /* {
78 ufs_daddr_t lbn, nextlbn;
80 long size, xfersize, blkoffset;
81 int error, orig_resid;
88 seqcount = ap->a_ioflag >> 16;
92 ioflag = ap->a_ioflag;
94 if ((ioflag & IO_DIRECT) != 0) {
97 error = ffs_rawread(vp, uio, &workdone);
98 if (error || workdone)
104 if (uio->uio_rw != UIO_READ)
105 panic("ffs_read: mode");
107 if (vp->v_type == VLNK) {
108 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
109 panic("ffs_read: short symlink");
110 } else if (vp->v_type != VREG && vp->v_type != VDIR)
111 panic("ffs_read: type %d", vp->v_type);
114 if ((u_int64_t)uio->uio_offset > fs->fs_maxfilesize)
117 orig_resid = uio->uio_resid;
121 object = vp->v_object;
123 bytesinfile = ip->i_size - uio->uio_offset;
124 if (bytesinfile <= 0) {
125 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
126 ip->i_flag |= IN_ACCESS;
131 vm_object_reference(object);
133 #ifdef ENABLE_VFS_IOOPT
135 * If IO optimisation is turned on,
136 * and we are NOT a VM based IO request,
137 * (i.e. not headed for the buffer cache)
138 * but there IS a vm object associated with it.
140 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
143 toread = uio->uio_resid;
144 if (toread > bytesinfile)
145 toread = bytesinfile;
146 if (toread >= PAGE_SIZE) {
148 * Then if it's at least a page in size, try
149 * get the data from the object using vm tricks
151 error = uioread(toread, uio, object, &nread);
152 if ((uio->uio_resid == 0) || (error != 0)) {
154 * If we finished or there was an error
155 * then finish up (the reference previously
156 * obtained on object must be released).
159 uio->uio_resid != orig_resid) &&
160 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
161 ip->i_flag |= IN_ACCESS;
164 vm_object_vndeallocate(object);
172 * Ok so we couldn't do it all in one vm trick...
173 * so cycle around trying smaller bites..
175 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
176 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
178 #ifdef ENABLE_VFS_IOOPT
179 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
181 * Obviously we didn't finish above, but we
182 * didn't get an error either. Try the same trick again.
183 * but this time we are looping.
186 toread = uio->uio_resid;
187 if (toread > bytesinfile)
188 toread = bytesinfile;
191 * Once again, if there isn't enough for a
192 * whole page, don't try optimising.
194 if (toread >= PAGE_SIZE) {
195 error = uioread(toread, uio, object, &nread);
196 if ((uio->uio_resid == 0) || (error != 0)) {
198 * If we finished or there was an
199 * error then finish up (the reference
200 * previously obtained on object must
204 uio->uio_resid != orig_resid) &&
205 (vp->v_mount->mnt_flag &
207 ip->i_flag |= IN_ACCESS;
209 vm_object_vndeallocate(object);
213 * To get here we didnt't finish or err.
214 * If we did get some data,
215 * loop to try another bite.
224 lbn = lblkno(fs, uio->uio_offset);
228 * size of buffer. The buffer representing the
229 * end of the file is rounded up to the size of
230 * the block type ( fragment or full block,
233 size = BLKSIZE(fs, ip, lbn);
234 blkoffset = blkoff(fs, uio->uio_offset);
237 * The amount we want to transfer in this iteration is
238 * one FS block less the amount of the data before
239 * our startpoint (duh!)
241 xfersize = fs->fs_bsize - blkoffset;
244 * But if we actually want less than the block,
245 * or the file doesn't have a whole block more of data,
246 * then use the lesser number.
248 if (uio->uio_resid < xfersize)
249 xfersize = uio->uio_resid;
250 if (bytesinfile < xfersize)
251 xfersize = bytesinfile;
253 if (lblktosize(fs, nextlbn) >= ip->i_size) {
255 * Don't do readahead if this is the end of the file.
257 error = bread(vp, lbn, size, &bp);
258 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
260 * Otherwise if we are allowed to cluster,
261 * grab as much as we can.
263 * XXX This may not be a win if we are not
264 * doing sequential access.
266 error = cluster_read(vp, ip->i_size, lbn,
267 size, uio->uio_resid, seqcount, &bp);
268 } else if (seqcount > 1) {
270 * If we are NOT allowed to cluster, then
271 * if we appear to be acting sequentially,
272 * fire off a request for a readahead
273 * as well as a read. Note that the 4th and 5th
274 * arguments point to arrays of the size specified in
277 int nextsize = BLKSIZE(fs, ip, nextlbn);
278 error = breadn(vp, lbn,
279 size, &nextlbn, &nextsize, 1, &bp);
282 * Failing all of the above, just read what the
283 * user asked for. Interestingly, the same as
284 * the first option above.
286 error = bread(vp, lbn, size, &bp);
295 * If IO_DIRECT then set B_DIRECT for the buffer. This
296 * will cause us to attempt to release the buffer later on
297 * and will cause the buffer cache to attempt to free the
300 if (ioflag & IO_DIRECT)
301 bp->b_flags |= B_DIRECT;
304 * We should only get non-zero b_resid when an I/O error
305 * has occurred, which should cause us to break above.
306 * However, if the short read did not cause an error,
307 * then we want to ensure that we do not uiomove bad
308 * or uninitialized data.
310 * XXX b_resid is only valid when an actual I/O has occured
311 * and may be incorrect if the buffer is B_CACHE or if the
312 * last op on the buffer was a failed write. This KASSERT
313 * is a precursor to removing it from the UFS code.
315 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
317 if (size < xfersize) {
323 #ifdef ENABLE_VFS_IOOPT
324 if (vfs_ioopt && object &&
325 (bp->b_flags & B_VMIO) &&
326 ((blkoffset & PAGE_MASK) == 0) &&
327 ((xfersize & PAGE_MASK) == 0)) {
329 * If VFS IO optimisation is turned on,
330 * and it's an exact page multiple
331 * And a normal VM based op,
332 * then use uiomiveco()
335 uiomoveco((char *)bp->b_data + blkoffset,
336 (int)xfersize, uio, object);
341 * otherwise use the general form
344 uiomove((char *)bp->b_data + blkoffset,
351 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
352 (LIST_FIRST(&bp->b_dep) == NULL)) {
354 * If there are no dependencies, and it's VMIO,
355 * then we don't need the buf, mark it available
356 * for freeing. The VM has the data.
358 bp->b_flags |= B_RELBUF;
362 * Otherwise let whoever
363 * made the request take care of
364 * freeing it. We just queue
365 * it onto another list.
372 * This can only happen in the case of an error
373 * because the loop above resets bp to NULL on each iteration
374 * and on normal completion has not set a new value into it.
375 * so it must have come from a 'break' statement
378 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
379 (LIST_FIRST(&bp->b_dep) == NULL)) {
380 bp->b_flags |= B_RELBUF;
388 vm_object_vndeallocate(object);
389 if ((error == 0 || uio->uio_resid != orig_resid) &&
390 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
391 ip->i_flag |= IN_ACCESS;
396 * Vnode op for writing.
400 struct vop_write_args /* {
404 struct ucred *a_cred;
415 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
420 seqcount = ap->a_ioflag >> 16;
421 ioflag = ap->a_ioflag;
426 object = vp->v_object;
428 vm_object_reference(object);
431 if (uio->uio_rw != UIO_WRITE)
432 panic("ffs_write: mode");
435 switch (vp->v_type) {
437 if (ioflag & IO_APPEND)
438 uio->uio_offset = ip->i_size;
439 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) {
441 vm_object_vndeallocate(object);
448 panic("ffs_write: dir write");
451 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
452 (int)uio->uio_offset,
458 if (uio->uio_offset < 0 ||
459 (u_int64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
461 vm_object_vndeallocate(object);
465 * Maybe this should be above the vnode op call, but so long as
466 * file servers have no limits, I don't think it matters.
469 if (vp->v_type == VREG && td && td->td_proc &&
470 uio->uio_offset + uio->uio_resid >
471 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
472 psignal(td->td_proc, SIGXFSZ);
474 vm_object_vndeallocate(object);
478 resid = uio->uio_resid;
482 * NOTE! These B_ flags are actually balloc-only flags, not buffer
483 * flags. They are similar to the BA_ flags in -current.
485 if (seqcount > B_SEQMAX)
486 flags = B_SEQMAX << B_SEQSHIFT;
488 flags = seqcount << B_SEQSHIFT;
489 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
492 if (object && (object->flags & OBJ_OPT)) {
493 vm_freeze_copyopts(object,
494 OFF_TO_IDX(uio->uio_offset),
495 OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK));
498 for (error = 0; uio->uio_resid > 0;) {
499 lbn = lblkno(fs, uio->uio_offset);
500 blkoffset = blkoff(fs, uio->uio_offset);
501 xfersize = fs->fs_bsize - blkoffset;
502 if (uio->uio_resid < xfersize)
503 xfersize = uio->uio_resid;
505 if (uio->uio_offset + xfersize > ip->i_size)
506 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
509 * We must perform a read-before-write if the transfer
510 * size does not cover the entire buffer.
512 if (fs->fs_bsize > xfersize)
516 /* XXX is uio->uio_offset the right thing here? */
517 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
518 ap->a_cred, flags, &bp);
522 * If the buffer is not valid and we did not clear garbage
523 * out above, we have to do so here even though the write
524 * covers the entire buffer in order to avoid a mmap()/write
525 * race where another process may see the garbage prior to
526 * the uiomove() for a write replacing it.
528 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
530 if (ioflag & IO_DIRECT)
531 bp->b_flags |= B_DIRECT;
532 if (ioflag & IO_NOWDRAIN)
533 bp->b_flags |= B_NOWDRAIN;
535 if (uio->uio_offset + xfersize > ip->i_size) {
536 ip->i_size = uio->uio_offset + xfersize;
540 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
545 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
546 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
547 (LIST_FIRST(&bp->b_dep) == NULL)) {
548 bp->b_flags |= B_RELBUF;
552 * If IO_SYNC each buffer is written synchronously. Otherwise
553 * if we have a severe page deficiency write the buffer
554 * asynchronously. Otherwise try to cluster, and if that
555 * doesn't do it then either do an async write (if O_DIRECT),
556 * or a delayed write (if not).
559 if (ioflag & IO_SYNC) {
561 } else if (vm_page_count_severe() ||
562 buf_dirty_count_severe() ||
563 (ioflag & IO_ASYNC)) {
564 bp->b_flags |= B_CLUSTEROK;
566 } else if (xfersize + blkoffset == fs->fs_bsize) {
567 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
568 bp->b_flags |= B_CLUSTEROK;
569 cluster_write(bp, ip->i_size, seqcount);
573 } else if (ioflag & IO_DIRECT) {
574 bp->b_flags |= B_CLUSTEROK;
577 bp->b_flags |= B_CLUSTEROK;
580 if (error || xfersize == 0)
582 ip->i_flag |= IN_CHANGE | IN_UPDATE;
585 * If we successfully wrote any data, and we are not the superuser
586 * we clear the setuid and setgid bits as a precaution against
589 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
590 ip->i_mode &= ~(ISUID | ISGID);
591 if (resid > uio->uio_resid)
592 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
594 if (ioflag & IO_UNIT) {
595 (void)UFS_TRUNCATE(vp, osize,
596 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
597 uio->uio_offset -= resid - uio->uio_resid;
598 uio->uio_resid = resid;
600 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
601 error = UFS_UPDATE(vp, 1);
604 vm_object_vndeallocate(object);
615 struct vop_getpages_args *ap;
617 off_t foff, physoffset;
619 struct vnode *dp, *vp;
621 vm_pindex_t pindex, firstindex;
623 int bbackwards, bforwards;
624 int pbackwards, pforwards;
634 pcount = round_page(ap->a_count) / PAGE_SIZE;
635 mreq = ap->a_m[ap->a_reqpage];
636 firstindex = ap->a_m[0]->pindex;
639 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
640 * then the entire page is valid. Since the page may be mapped,
641 * user programs might reference data beyond the actual end of file
642 * occuring within the page. We have to zero that data.
645 if (mreq->valid != VM_PAGE_BITS_ALL)
646 vm_page_zero_invalid(mreq, TRUE);
647 for (i = 0; i < pcount; i++) {
648 if (i != ap->a_reqpage) {
649 vm_page_free(ap->a_m[i]);
657 bsize = vp->v_mount->mnt_stat.f_iosize;
658 pindex = mreq->pindex;
659 foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
661 if (bsize < PAGE_SIZE)
662 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
667 * foff is the file offset of the required page
668 * reqlblkno is the logical block that contains the page
669 * poff is the index of the page into the logical block
671 reqlblkno = foff / bsize;
672 poff = (foff % bsize) / PAGE_SIZE;
674 if ( VOP_BMAP( vp, reqlblkno, &dp, &reqblkno,
675 &bforwards, &bbackwards) || (reqblkno == -1)) {
676 for(i = 0; i < pcount; i++) {
677 if (i != ap->a_reqpage)
678 vm_page_free(ap->a_m[i]);
680 if (reqblkno == -1) {
681 if ((mreq->flags & PG_ZERO) == 0)
682 vm_page_zero_fill(mreq);
683 vm_page_undirty(mreq);
684 mreq->valid = VM_PAGE_BITS_ALL;
687 return VM_PAGER_ERROR;
691 physoffset = (off_t)reqblkno * DEV_BSIZE + poff * PAGE_SIZE;
692 pagesperblock = bsize / PAGE_SIZE;
694 * find the first page that is contiguous...
695 * note that pbackwards is the number of pages that are contiguous
700 pbackwards = poff + bbackwards * pagesperblock;
701 if (ap->a_reqpage > pbackwards) {
702 firstpage = ap->a_reqpage - pbackwards;
703 for(i=0;i<firstpage;i++)
704 vm_page_free(ap->a_m[i]);
708 * pforwards is the number of pages that are contiguous
709 * after the current page.
711 pforwards = (pagesperblock - (poff + 1)) +
712 bforwards * pagesperblock;
713 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
714 for( i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
715 vm_page_free(ap->a_m[i]);
716 pcount = ap->a_reqpage + pforwards + 1;
720 * number of pages for I/O corrected for the non-contig pages at
721 * the beginning of the array.
727 * calculate the size of the transfer
730 size = pcount * PAGE_SIZE;
732 if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) >
733 obj->un_pager.vnp.vnp_size)
734 size = obj->un_pager.vnp.vnp_size -
735 IDX_TO_OFF(ap->a_m[firstpage]->pindex);
738 rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
739 (ap->a_reqpage - firstpage), physoffset);
747 * XXX By default, wimp out... note that a_offset is ignored (and always
752 struct vop_putpages_args *ap;
754 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
755 ap->a_sync, ap->a_rtvals);