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
37 #define BLKSIZE(a, b, c) blksize(a, b, c)
41 #define READ_S "ffs_read"
42 #define WRITE ffs_write
43 #define WRITE_S "ffs_write"
46 #include <vm/vm_object.h>
47 #include <vm/vm_pager.h>
48 #include <vm/vm_map.h>
49 #include <vm/vnode_pager.h>
53 * Vnode op for reading.
58 struct vop_read_args /* {
65 register struct vnode *vp;
66 register struct inode *ip;
67 register struct uio *uio;
70 ufs_daddr_t lbn, nextlbn;
72 long size, xfersize, blkoffset;
73 int error, orig_resid;
80 seqcount = ap->a_ioflag >> 16;
84 ioflag = ap->a_ioflag;
87 if (uio->uio_rw != UIO_READ)
88 panic("%s: mode", READ_S);
90 if (vp->v_type == VLNK) {
91 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
92 panic("%s: short symlink", READ_S);
93 } else if (vp->v_type != VREG && vp->v_type != VDIR)
94 panic("%s: type %d", READ_S, vp->v_type);
97 if ((u_int64_t)uio->uio_offset > fs->fs_maxfilesize)
100 orig_resid = uio->uio_resid;
104 object = vp->v_object;
106 bytesinfile = ip->i_size - uio->uio_offset;
107 if (bytesinfile <= 0) {
108 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
109 ip->i_flag |= IN_ACCESS;
114 vm_object_reference(object);
116 #ifdef ENABLE_VFS_IOOPT
118 * If IO optimisation is turned on,
119 * and we are NOT a VM based IO request,
120 * (i.e. not headed for the buffer cache)
121 * but there IS a vm object associated with it.
123 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
126 toread = uio->uio_resid;
127 if (toread > bytesinfile)
128 toread = bytesinfile;
129 if (toread >= PAGE_SIZE) {
131 * Then if it's at least a page in size, try
132 * get the data from the object using vm tricks
134 error = uioread(toread, uio, object, &nread);
135 if ((uio->uio_resid == 0) || (error != 0)) {
137 * If we finished or there was an error
138 * then finish up (the reference previously
139 * obtained on object must be released).
142 uio->uio_resid != orig_resid) &&
143 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
144 ip->i_flag |= IN_ACCESS;
147 vm_object_vndeallocate(object);
155 * Ok so we couldn't do it all in one vm trick...
156 * so cycle around trying smaller bites..
158 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
159 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
161 #ifdef ENABLE_VFS_IOOPT
162 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
164 * Obviously we didn't finish above, but we
165 * didn't get an error either. Try the same trick again.
166 * but this time we are looping.
169 toread = uio->uio_resid;
170 if (toread > bytesinfile)
171 toread = bytesinfile;
174 * Once again, if there isn't enough for a
175 * whole page, don't try optimising.
177 if (toread >= PAGE_SIZE) {
178 error = uioread(toread, uio, object, &nread);
179 if ((uio->uio_resid == 0) || (error != 0)) {
181 * If we finished or there was an
182 * error then finish up (the reference
183 * previously obtained on object must
187 uio->uio_resid != orig_resid) &&
188 (vp->v_mount->mnt_flag &
190 ip->i_flag |= IN_ACCESS;
192 vm_object_vndeallocate(object);
196 * To get here we didnt't finish or err.
197 * If we did get some data,
198 * loop to try another bite.
207 lbn = lblkno(fs, uio->uio_offset);
211 * size of buffer. The buffer representing the
212 * end of the file is rounded up to the size of
213 * the block type ( fragment or full block,
216 size = BLKSIZE(fs, ip, lbn);
217 blkoffset = blkoff(fs, uio->uio_offset);
220 * The amount we want to transfer in this iteration is
221 * one FS block less the amount of the data before
222 * our startpoint (duh!)
224 xfersize = fs->fs_bsize - blkoffset;
227 * But if we actually want less than the block,
228 * or the file doesn't have a whole block more of data,
229 * then use the lesser number.
231 if (uio->uio_resid < xfersize)
232 xfersize = uio->uio_resid;
233 if (bytesinfile < xfersize)
234 xfersize = bytesinfile;
236 if (lblktosize(fs, nextlbn) >= ip->i_size)
238 * Don't do readahead if this is the end of the file.
240 error = bread(vp, lbn, size, NOCRED, &bp);
241 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0)
243 * Otherwise if we are allowed to cluster,
244 * grab as much as we can.
246 * XXX This may not be a win if we are not
247 * doing sequential access.
249 error = cluster_read(vp, ip->i_size, lbn,
250 size, NOCRED, uio->uio_resid, seqcount, &bp);
251 else if (seqcount > 1) {
253 * If we are NOT allowed to cluster, then
254 * if we appear to be acting sequentially,
255 * fire off a request for a readahead
256 * as well as a read. Note that the 4th and 5th
257 * arguments point to arrays of the size specified in
260 int nextsize = BLKSIZE(fs, ip, nextlbn);
261 error = breadn(vp, lbn,
262 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
265 * Failing all of the above, just read what the
266 * user asked for. Interestingly, the same as
267 * the first option above.
269 error = bread(vp, lbn, size, NOCRED, &bp);
277 * We should only get non-zero b_resid when an I/O error
278 * has occurred, which should cause us to break above.
279 * However, if the short read did not cause an error,
280 * then we want to ensure that we do not uiomove bad
281 * or uninitialized data.
284 if (size < xfersize) {
290 #ifdef ENABLE_VFS_IOOPT
291 if (vfs_ioopt && object &&
292 (bp->b_flags & B_VMIO) &&
293 ((blkoffset & PAGE_MASK) == 0) &&
294 ((xfersize & PAGE_MASK) == 0)) {
296 * If VFS IO optimisation is turned on,
297 * and it's an exact page multiple
298 * And a normal VM based op,
299 * then use uiomiveco()
302 uiomoveco((char *)bp->b_data + blkoffset,
303 (int)xfersize, uio, object);
308 * otherwise use the general form
311 uiomove((char *)bp->b_data + blkoffset,
318 if ((ioflag & IO_VMIO) &&
319 (LIST_FIRST(&bp->b_dep) == NULL)) {
321 * If there are no dependencies, and
322 * it's VMIO, then we don't need the buf,
323 * mark it available for freeing. The VM has the data.
325 bp->b_flags |= B_RELBUF;
329 * Otherwise let whoever
330 * made the request take care of
331 * freeing it. We just queue
332 * it onto another list.
339 * This can only happen in the case of an error
340 * because the loop above resets bp to NULL on each iteration
341 * and on normal completion has not set a new value into it.
342 * so it must have come from a 'break' statement
345 if ((ioflag & IO_VMIO) &&
346 (LIST_FIRST(&bp->b_dep) == NULL)) {
347 bp->b_flags |= B_RELBUF;
355 vm_object_vndeallocate(object);
356 if ((error == 0 || uio->uio_resid != orig_resid) &&
357 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
358 ip->i_flag |= IN_ACCESS;
363 * Vnode op for writing.
367 struct vop_write_args /* {
371 struct ucred *a_cred;
374 register struct vnode *vp;
375 register struct uio *uio;
376 register struct inode *ip;
382 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
386 ioflag = ap->a_ioflag;
391 object = vp->v_object;
393 vm_object_reference(object);
396 if (uio->uio_rw != UIO_WRITE)
397 panic("%s: mode", WRITE_S);
400 switch (vp->v_type) {
402 if (ioflag & IO_APPEND)
403 uio->uio_offset = ip->i_size;
404 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) {
406 vm_object_vndeallocate(object);
413 panic("%s: dir write", WRITE_S);
416 panic("%s: type %p %d (%d,%d)", WRITE_S, vp, (int)vp->v_type,
417 (int)uio->uio_offset,
423 if (uio->uio_offset < 0 ||
424 (u_int64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
426 vm_object_vndeallocate(object);
430 * Maybe this should be above the vnode op call, but so long as
431 * file servers have no limits, I don't think it matters.
434 if (vp->v_type == VREG && p &&
435 uio->uio_offset + uio->uio_resid >
436 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
439 vm_object_vndeallocate(object);
443 resid = uio->uio_resid;
446 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
449 if (object && (object->flags & OBJ_OPT)) {
450 vm_freeze_copyopts(object,
451 OFF_TO_IDX(uio->uio_offset),
452 OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK));
455 for (error = 0; uio->uio_resid > 0;) {
456 lbn = lblkno(fs, uio->uio_offset);
457 blkoffset = blkoff(fs, uio->uio_offset);
458 xfersize = fs->fs_bsize - blkoffset;
459 if (uio->uio_resid < xfersize)
460 xfersize = uio->uio_resid;
462 if (uio->uio_offset + xfersize > ip->i_size)
463 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
465 if (fs->fs_bsize > xfersize)
469 /* XXX is uio->uio_offset the right thing here? */
470 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
471 ap->a_cred, flags, &bp);
475 if (uio->uio_offset + xfersize > ip->i_size) {
476 ip->i_size = uio->uio_offset + xfersize;
480 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
485 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
486 if ((ioflag & IO_VMIO) &&
487 (LIST_FIRST(&bp->b_dep) == NULL))
488 bp->b_flags |= B_RELBUF;
490 if (ioflag & IO_SYNC) {
492 } else if (xfersize + blkoffset == fs->fs_bsize) {
493 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
494 bp->b_flags |= B_CLUSTEROK;
495 cluster_write(bp, ip->i_size);
500 bp->b_flags |= B_CLUSTEROK;
503 if (error || xfersize == 0)
505 ip->i_flag |= IN_CHANGE | IN_UPDATE;
508 * If we successfully wrote any data, and we are not the superuser
509 * we clear the setuid and setgid bits as a precaution against
512 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
513 ip->i_mode &= ~(ISUID | ISGID);
515 if (ioflag & IO_UNIT) {
516 (void)UFS_TRUNCATE(vp, osize,
517 ioflag & IO_SYNC, ap->a_cred, uio->uio_procp);
518 uio->uio_offset -= resid - uio->uio_resid;
519 uio->uio_resid = resid;
521 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
522 error = UFS_UPDATE(vp, 1);
524 VN_POLLEVENT(vp, POLLWRITE | (extended ? POLLEXTEND : 0));
527 vm_object_vndeallocate(object);
538 struct vop_getpages_args *ap;
540 off_t foff, physoffset;
542 struct vnode *dp, *vp;
544 vm_pindex_t pindex, firstindex;
546 int bbackwards, bforwards;
547 int pbackwards, pforwards;
557 pcount = round_page(ap->a_count) / PAGE_SIZE;
558 mreq = ap->a_m[ap->a_reqpage];
559 firstindex = ap->a_m[0]->pindex;
562 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
563 * then the entire page is valid. Since the page may be mapped,
564 * user programs might reference data beyond the actual end of file
565 * occuring within the page. We have to zero that data.
568 if (mreq->valid != VM_PAGE_BITS_ALL)
569 vm_page_zero_invalid(mreq, TRUE);
570 for (i = 0; i < pcount; i++) {
571 if (i != ap->a_reqpage) {
572 vm_page_free(ap->a_m[i]);
580 bsize = vp->v_mount->mnt_stat.f_iosize;
581 pindex = mreq->pindex;
582 foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
584 if (bsize < PAGE_SIZE)
585 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
590 * foff is the file offset of the required page
591 * reqlblkno is the logical block that contains the page
592 * poff is the index of the page into the logical block
594 reqlblkno = foff / bsize;
595 poff = (foff % bsize) / PAGE_SIZE;
597 if ( VOP_BMAP( vp, reqlblkno, &dp, &reqblkno,
598 &bforwards, &bbackwards) || (reqblkno == -1)) {
599 for(i = 0; i < pcount; i++) {
600 if (i != ap->a_reqpage)
601 vm_page_free(ap->a_m[i]);
603 if (reqblkno == -1) {
604 if ((mreq->flags & PG_ZERO) == 0)
605 vm_page_zero_fill(mreq);
606 vm_page_undirty(mreq);
607 mreq->valid = VM_PAGE_BITS_ALL;
610 return VM_PAGER_ERROR;
614 physoffset = (off_t)reqblkno * DEV_BSIZE + poff * PAGE_SIZE;
615 pagesperblock = bsize / PAGE_SIZE;
617 * find the first page that is contiguous...
618 * note that pbackwards is the number of pages that are contiguous
623 pbackwards = poff + bbackwards * pagesperblock;
624 if (ap->a_reqpage > pbackwards) {
625 firstpage = ap->a_reqpage - pbackwards;
626 for(i=0;i<firstpage;i++)
627 vm_page_free(ap->a_m[i]);
631 * pforwards is the number of pages that are contiguous
632 * after the current page.
634 pforwards = (pagesperblock - (poff + 1)) +
635 bforwards * pagesperblock;
636 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
637 for( i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
638 vm_page_free(ap->a_m[i]);
639 pcount = ap->a_reqpage + pforwards + 1;
643 * number of pages for I/O corrected for the non-contig pages at
644 * the beginning of the array.
650 * calculate the size of the transfer
653 size = pcount * PAGE_SIZE;
655 if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) >
656 obj->un_pager.vnp.vnp_size)
657 size = obj->un_pager.vnp.vnp_size -
658 IDX_TO_OFF(ap->a_m[firstpage]->pindex);
661 rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
662 (ap->a_reqpage - firstpage), physoffset);
670 * XXX By default, wimp out... note that a_offset is ignored (and always
675 struct vop_putpages_args *ap;
677 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
678 ap->a_sync, ap->a_rtvals);