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.15 2006/03/27 01:54:17 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 <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;
79 int size, xfersize, blkoffset;
80 int error, orig_resid;
87 seqcount = ap->a_ioflag >> 16;
91 ioflag = ap->a_ioflag;
93 if ((ioflag & IO_DIRECT) != 0) {
96 error = ffs_rawread(vp, uio, &workdone);
97 if (error || workdone)
103 if (uio->uio_rw != UIO_READ)
104 panic("ffs_read: mode");
106 if (vp->v_type == VLNK) {
107 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
108 panic("ffs_read: short symlink");
109 } else if (vp->v_type != VREG && vp->v_type != VDIR)
110 panic("ffs_read: type %d", vp->v_type);
113 if ((uint64_t)uio->uio_offset > fs->fs_maxfilesize)
116 orig_resid = uio->uio_resid;
120 object = vp->v_object;
122 bytesinfile = ip->i_size - uio->uio_offset;
123 if (bytesinfile <= 0) {
124 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
125 ip->i_flag |= IN_ACCESS;
130 vm_object_reference(object);
133 * Ok so we couldn't do it all in one vm trick...
134 * so cycle around trying smaller bites..
136 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
137 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
140 lbn = lblkno(fs, uio->uio_offset);
141 loffset = lblktodoff(fs, lbn);
143 nextloffset = lblktodoff(fs, nextlbn);
146 * size of buffer. The buffer representing the
147 * end of the file is rounded up to the size of
148 * the block type ( fragment or full block,
151 size = BLKSIZE(fs, ip, lbn);
152 blkoffset = blkoff(fs, uio->uio_offset);
155 * The amount we want to transfer in this iteration is
156 * one FS block less the amount of the data before
157 * our startpoint (duh!)
159 xfersize = fs->fs_bsize - blkoffset;
162 * But if we actually want less than the block,
163 * or the file doesn't have a whole block more of data,
164 * then use the lesser number.
166 if (uio->uio_resid < xfersize)
167 xfersize = uio->uio_resid;
168 if (bytesinfile < xfersize)
169 xfersize = bytesinfile;
171 if (nextloffset >= ip->i_size) {
173 * Don't do readahead if this is the end of the file.
175 error = bread(vp, loffset, size, &bp);
176 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
178 * Otherwise if we are allowed to cluster,
179 * grab as much as we can.
181 * XXX This may not be a win if we are not
182 * doing sequential access.
184 error = cluster_read(vp, (off_t)ip->i_size,
186 uio->uio_resid, seqcount, &bp);
187 } else if (seqcount > 1) {
189 * If we are NOT allowed to cluster, then
190 * if we appear to be acting sequentially,
191 * fire off a request for a readahead
192 * as well as a read. Note that the 4th and 5th
193 * arguments point to arrays of the size specified in
196 int nextsize = BLKSIZE(fs, ip, nextlbn);
197 error = breadn(vp, loffset,
198 size, &nextloffset, &nextsize, 1, &bp);
201 * Failing all of the above, just read what the
202 * user asked for. Interestingly, the same as
203 * the first option above.
205 error = bread(vp, loffset, size, &bp);
214 * If IO_DIRECT then set B_DIRECT for the buffer. This
215 * will cause us to attempt to release the buffer later on
216 * and will cause the buffer cache to attempt to free the
219 if (ioflag & IO_DIRECT)
220 bp->b_flags |= B_DIRECT;
223 * We should only get non-zero b_resid when an I/O error
224 * has occurred, which should cause us to break above.
225 * However, if the short read did not cause an error,
226 * then we want to ensure that we do not uiomove bad
227 * or uninitialized data.
229 * XXX b_resid is only valid when an actual I/O has occured
230 * and may be incorrect if the buffer is B_CACHE or if the
231 * last op on the buffer was a failed write. This KASSERT
232 * is a precursor to removing it from the UFS code.
234 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
236 if (size < xfersize) {
243 * otherwise use the general form
245 error = uiomove((char *)bp->b_data + blkoffset,
251 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
252 (LIST_FIRST(&bp->b_dep) == NULL)) {
254 * If there are no dependencies, and it's VMIO,
255 * then we don't need the buf, mark it available
256 * for freeing. The VM has the data.
258 bp->b_flags |= B_RELBUF;
262 * Otherwise let whoever
263 * made the request take care of
264 * freeing it. We just queue
265 * it onto another list.
272 * This can only happen in the case of an error
273 * because the loop above resets bp to NULL on each iteration
274 * and on normal completion has not set a new value into it.
275 * so it must have come from a 'break' statement
278 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
279 (LIST_FIRST(&bp->b_dep) == NULL)) {
280 bp->b_flags |= B_RELBUF;
288 vm_object_vndeallocate(object);
289 if ((error == 0 || uio->uio_resid != orig_resid) &&
290 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
291 ip->i_flag |= IN_ACCESS;
296 * Vnode op for writing.
298 * ffs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
299 * struct ucred *a_cred)
302 ffs_write(struct vop_write_args *ap)
312 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
317 seqcount = ap->a_ioflag >> 16;
318 ioflag = ap->a_ioflag;
323 object = vp->v_object;
325 vm_object_reference(object);
328 if (uio->uio_rw != UIO_WRITE)
329 panic("ffs_write: mode");
332 switch (vp->v_type) {
334 if (ioflag & IO_APPEND)
335 uio->uio_offset = ip->i_size;
336 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) {
338 vm_object_vndeallocate(object);
345 panic("ffs_write: dir write");
348 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
349 (int)uio->uio_offset,
355 if (uio->uio_offset < 0 ||
356 (uint64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
358 vm_object_vndeallocate(object);
362 * Maybe this should be above the vnode op call, but so long as
363 * file servers have no limits, I don't think it matters.
366 if (vp->v_type == VREG && td && td->td_proc &&
367 uio->uio_offset + uio->uio_resid >
368 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
369 psignal(td->td_proc, SIGXFSZ);
371 vm_object_vndeallocate(object);
375 resid = uio->uio_resid;
379 * NOTE! These B_ flags are actually balloc-only flags, not buffer
380 * flags. They are similar to the BA_ flags in fbsd.
382 if (seqcount > B_SEQMAX)
383 flags = B_SEQMAX << B_SEQSHIFT;
385 flags = seqcount << B_SEQSHIFT;
386 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
389 if (object && (object->flags & OBJ_OPT)) {
390 vm_freeze_copyopts(object,
391 OFF_TO_IDX(uio->uio_offset),
392 OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK));
395 for (error = 0; uio->uio_resid > 0;) {
396 lbn = lblkno(fs, uio->uio_offset);
397 blkoffset = blkoff(fs, uio->uio_offset);
398 xfersize = fs->fs_bsize - blkoffset;
399 if (uio->uio_resid < xfersize)
400 xfersize = uio->uio_resid;
402 if (uio->uio_offset + xfersize > ip->i_size)
403 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
406 * We must perform a read-before-write if the transfer
407 * size does not cover the entire buffer.
409 if (fs->fs_bsize > xfersize)
413 /* XXX is uio->uio_offset the right thing here? */
414 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
415 ap->a_cred, flags, &bp);
419 * If the buffer is not valid and we did not clear garbage
420 * out above, we have to do so here even though the write
421 * covers the entire buffer in order to avoid a mmap()/write
422 * race where another process may see the garbage prior to
423 * the uiomove() for a write replacing it.
425 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
427 if (ioflag & IO_DIRECT)
428 bp->b_flags |= B_DIRECT;
429 if (ioflag & IO_NOWDRAIN)
430 bp->b_flags |= B_NOWDRAIN;
431 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
432 bp->b_flags |= B_NOCACHE;
434 if (uio->uio_offset + xfersize > ip->i_size) {
435 ip->i_size = uio->uio_offset + xfersize;
439 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
444 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
445 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
446 (LIST_FIRST(&bp->b_dep) == NULL)) {
447 bp->b_flags |= B_RELBUF;
451 * If IO_SYNC each buffer is written synchronously. Otherwise
452 * if we have a severe page deficiency write the buffer
453 * asynchronously. Otherwise try to cluster, and if that
454 * doesn't do it then either do an async write (if O_DIRECT),
455 * or a delayed write (if not).
458 if (ioflag & IO_SYNC) {
460 } else if (vm_page_count_severe() ||
461 buf_dirty_count_severe() ||
462 (ioflag & IO_ASYNC)) {
463 bp->b_flags |= B_CLUSTEROK;
465 } else if (xfersize + blkoffset == fs->fs_bsize) {
466 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
467 bp->b_flags |= B_CLUSTEROK;
468 cluster_write(bp, (off_t)ip->i_size, seqcount);
472 } else if (ioflag & IO_DIRECT) {
473 bp->b_flags |= B_CLUSTEROK;
476 bp->b_flags |= B_CLUSTEROK;
479 if (error || xfersize == 0)
481 ip->i_flag |= IN_CHANGE | IN_UPDATE;
484 * If we successfully wrote any data, and we are not the superuser
485 * we clear the setuid and setgid bits as a precaution against
488 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
489 ip->i_mode &= ~(ISUID | ISGID);
490 if (resid > uio->uio_resid)
491 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
493 if (ioflag & IO_UNIT) {
494 (void)UFS_TRUNCATE(vp, osize,
495 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
496 uio->uio_offset -= resid - uio->uio_resid;
497 uio->uio_resid = resid;
499 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
500 error = UFS_UPDATE(vp, 1);
503 vm_object_vndeallocate(object);
513 ffs_getpages(struct vop_getpages_args *ap)
515 off_t foff, physoffset;
517 struct vnode *dp, *vp;
519 vm_pindex_t pindex, firstindex;
521 int bbackwards, bforwards;
522 int pbackwards, pforwards;
532 pcount = round_page(ap->a_count) / PAGE_SIZE;
533 mreq = ap->a_m[ap->a_reqpage];
534 firstindex = ap->a_m[0]->pindex;
537 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
538 * then the entire page is valid. Since the page may be mapped,
539 * user programs might reference data beyond the actual end of file
540 * occuring within the page. We have to zero that data.
543 if (mreq->valid != VM_PAGE_BITS_ALL)
544 vm_page_zero_invalid(mreq, TRUE);
545 for (i = 0; i < pcount; i++) {
546 if (i != ap->a_reqpage) {
547 vm_page_free(ap->a_m[i]);
555 bsize = vp->v_mount->mnt_stat.f_iosize;
556 pindex = mreq->pindex;
557 foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
559 if (bsize < PAGE_SIZE)
560 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
565 * foff is the file offset of the required page
566 * reqlblkno is the logical block that contains the page
567 * poff is the bytes offset of the page in the logical block
569 poff = (int)(foff % bsize);
570 reqoffset = foff - poff;
572 if (VOP_BMAP(vp, reqoffset, &dp, &doffset,
573 &bforwards, &bbackwards) || (doffset == NOOFFSET)
575 for (i = 0; i < pcount; i++) {
576 if (i != ap->a_reqpage)
577 vm_page_free(ap->a_m[i]);
579 if (doffset == NOOFFSET) {
580 if ((mreq->flags & PG_ZERO) == 0)
581 vm_page_zero_fill(mreq);
582 vm_page_undirty(mreq);
583 mreq->valid = VM_PAGE_BITS_ALL;
586 return VM_PAGER_ERROR;
590 physoffset = doffset + poff;
591 pagesperblock = bsize / PAGE_SIZE;
594 * find the first page that is contiguous.
596 * bforwards and bbackwards are the number of contiguous bytes
597 * available before and after the block offset. poff is the page
598 * offset, in bytes, relative to the block offset.
600 * pforwards and pbackwards are the number of contiguous pages
601 * relative to the requested page, non-inclusive of the requested
602 * page (so a pbackwards and pforwards of 0 indicates just the
608 * Calculate pbackwards and clean up any requested
609 * pages that are too far back.
611 pbackwards = (poff + bbackwards) >> PAGE_SHIFT;
612 if (ap->a_reqpage > pbackwards) {
613 firstpage = ap->a_reqpage - pbackwards;
614 for (i = 0; i < firstpage; i++)
615 vm_page_free(ap->a_m[i]);
619 * Calculate pforwards
621 pforwards = (bforwards - poff - PAGE_SIZE) >> PAGE_SHIFT;
624 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
625 for(i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
626 vm_page_free(ap->a_m[i]);
627 pcount = ap->a_reqpage + pforwards + 1;
631 * Adjust pcount to be relative to firstpage. All pages prior
632 * to firstpage in the array have been cleaned up.
638 * calculate the size of the transfer
640 size = pcount * PAGE_SIZE;
642 if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) > vp->v_filesize) {
643 size = vp->v_filesize - IDX_TO_OFF(ap->a_m[firstpage]->pindex);
647 rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
648 (ap->a_reqpage - firstpage), physoffset);
656 * XXX By default, wimp out... note that a_offset is ignored (and always
660 ffs_putpages(struct vop_putpages_args *ap)
662 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
663 ap->a_sync, ap->a_rtvals);