2 * Copyright (c) 2000-2003 Tor Egge
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/fcntl.h>
36 #include <sys/limits.h>
37 #include <sys/mount.h>
38 #include <sys/namei.h>
39 #include <sys/vnode.h>
41 #include <sys/filio.h>
42 #include <sys/ttycom.h>
45 #include <ufs/ufs/extattr.h>
46 #include <ufs/ufs/quota.h>
47 #include <ufs/ufs/inode.h>
48 #include <ufs/ufs/ufsmount.h>
49 #include <ufs/ufs/ufs_extern.h>
50 #include <ufs/ffs/fs.h>
51 #include <ufs/ffs/ffs_extern.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_object.h>
56 #include <sys/kernel.h>
57 #include <sys/sysctl.h>
59 static int ffs_rawread_readahead(struct vnode *vp,
66 static int ffs_rawread_main(struct vnode *vp,
69 static int ffs_rawread_sync(struct vnode *vp, struct thread *td);
71 int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
73 void ffs_rawread_setup(void);
75 SYSCTL_DECL(_vfs_ffs);
77 static int ffsrawbufcnt = 4;
78 SYSCTL_INT(_vfs_ffs, OID_AUTO, ffsrawbufcnt, CTLFLAG_RD, &ffsrawbufcnt, 0,
79 "Buffers available for raw reads");
81 static int allowrawread = 1;
82 SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0,
83 "Flag to enable raw reads");
85 static int rawreadahead = 1;
86 SYSCTL_INT(_vfs_ffs, OID_AUTO, rawreadahead, CTLFLAG_RW, &rawreadahead, 0,
87 "Flag to enable readahead for long raw reads");
91 ffs_rawread_setup(void)
93 ffsrawbufcnt = (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8;
98 ffs_rawread_sync(struct vnode *vp, struct thread *td)
106 /* Check for dirty mmap, pending writes and dirty buffers */
110 if (bo->bo_numoutput > 0 ||
111 bo->bo_dirty.bv_cnt > 0 ||
112 (vp->v_iflag & VI_OBJDIRTY) != 0) {
116 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
117 if (VOP_ISLOCKED(vp, td) != LK_EXCLUSIVE)
121 VOP_UNLOCK(vp, 0, td);
122 (void) vn_start_write(vp, &mp, V_WAIT);
123 VOP_LOCK(vp, LK_EXCLUSIVE, td);
124 } else if (VOP_ISLOCKED(vp, td) != LK_EXCLUSIVE) {
126 /* Upgrade to exclusive lock, this might block */
127 VOP_LOCK(vp, LK_UPGRADE, td);
133 /* Check if vnode was reclaimed while unlocked. */
134 if ((vp->v_iflag & VI_DOOMED) != 0) {
137 VOP_LOCK(vp, LK_DOWNGRADE, td);
138 vn_finished_write(mp);
141 /* Attempt to msync mmap() regions to clean dirty mmap */
142 if ((vp->v_iflag & VI_OBJDIRTY) != 0) {
144 if (vp->v_object != NULL) {
145 VM_OBJECT_LOCK(vp->v_object);
146 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
147 VM_OBJECT_UNLOCK(vp->v_object);
152 /* Wait for pending writes to complete */
154 error = bufobj_wwait(&vp->v_bufobj, 0, 0);
156 /* XXX: can't happen with a zero timeout ??? */
160 VOP_LOCK(vp, LK_DOWNGRADE, td);
161 vn_finished_write(mp);
164 /* Flush dirty buffers */
165 if (bo->bo_dirty.bv_cnt > 0) {
168 if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0) {
170 VOP_LOCK(vp, LK_DOWNGRADE, td);
171 vn_finished_write(mp);
176 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
177 panic("ffs_rawread_sync: dirty bufs");
182 VOP_LOCK(vp, LK_DOWNGRADE, td);
183 vn_finished_write(mp);
193 ffs_rawread_readahead(struct vnode *vp,
211 bsize = vp->v_mount->mnt_stat.f_iosize;
216 iolen = ((vm_offset_t) udata) & PAGE_MASK;
218 if (bp->b_bcount + iolen > bp->b_kvasize) {
219 bp->b_bcount = bp->b_kvasize;
221 bp->b_bcount -= PAGE_SIZE;
223 bp->b_flags = 0; /* XXX necessary ? */
224 bp->b_iocmd = BIO_READ;
225 bp->b_iodone = bdone;
228 blockno = offset / bsize;
229 blockoff = (offset % bsize) / DEV_BSIZE;
230 if ((daddr_t) blockno != blockno) {
231 return EINVAL; /* blockno overflow */
234 bp->b_lblkno = bp->b_blkno = blockno;
236 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, NULL, &bforwards, NULL);
241 /* Fill holes with NULs to preserve semantics */
243 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize)
244 bp->b_bcount = bsize - blockoff * DEV_BSIZE;
245 bp->b_bufsize = bp->b_bcount;
250 if (ticks - PCPU_GET(switchticks) >= hogticks)
252 bzero(bp->b_data, bp->b_bufsize);
254 /* Mark operation completed (similar to bufdone()) */
257 bp->b_flags |= B_DONE;
260 bp->b_blkno = blkno + blockoff;
261 bp->b_offset = bp->b_iooffset = (blkno + blockoff) * DEV_BSIZE;
263 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize * (1 + bforwards))
264 bp->b_bcount = bsize * (1 + bforwards) - blockoff * DEV_BSIZE;
265 bp->b_bufsize = bp->b_bcount;
270 BO_STRATEGY(&dp->v_bufobj, bp);
276 ffs_rawread_main(struct vnode *vp,
280 struct buf *bp, *nbp, *tbp;
281 caddr_t sa, nsa, tsa;
289 td = uio->uio_td ? uio->uio_td : curthread;
290 udata = uio->uio_iov->iov_base;
291 resid = uio->uio_resid;
292 offset = uio->uio_offset;
295 * keep the process from being swapped
309 if (bp == NULL) { /* Setup first read */
310 /* XXX: Leave some bufs for swap */
311 bp = getpbuf(&ffsrawbufcnt);
314 error = ffs_rawread_readahead(vp, udata, offset,
319 if (resid > bp->b_bufsize) { /* Setup fist readahead */
320 /* XXX: Leave bufs for swap */
321 if (rawreadahead != 0)
322 nbp = trypbuf(&ffsrawbufcnt);
329 nerror = ffs_rawread_readahead(vp,
341 relpbuf(nbp, &ffsrawbufcnt);
349 bwait(bp, PRIBIO, "rawrd");
354 iolen = bp->b_bcount - bp->b_resid;
355 if (iolen == 0 && (bp->b_ioflags & BIO_ERROR) == 0) {
356 nerror = 0; /* Ignore possible beyond EOF error */
360 if ((bp->b_ioflags & BIO_ERROR) != 0) {
367 if (iolen < bp->b_bufsize) {
368 /* Incomplete read. Try to read remaining part */
369 error = ffs_rawread_readahead(vp,
372 bp->b_bufsize - iolen,
378 } else if (nbp != NULL) { /* Complete read with readahead */
388 if (resid <= bp->b_bufsize) { /* No more readaheads */
390 relpbuf(nbp, &ffsrawbufcnt);
392 } else { /* Setup next readahead */
393 nerror = ffs_rawread_readahead(vp,
405 relpbuf(nbp, &ffsrawbufcnt);
409 } else if (nerror != 0) {/* Deferred Readahead error */
411 } else if (resid > 0) { /* More to read, no readahead */
412 error = ffs_rawread_readahead(vp, udata, offset,
421 relpbuf(bp, &ffsrawbufcnt);
423 if (nbp != NULL) { /* Run down readahead buffer */
425 bwait(nbp, PRIBIO, "rawrd");
429 relpbuf(nbp, &ffsrawbufcnt);
435 uio->uio_iov->iov_base = udata;
436 uio->uio_resid = resid;
437 uio->uio_offset = offset;
443 ffs_rawread(struct vnode *vp,
447 if (allowrawread != 0 &&
448 uio->uio_iovcnt == 1 &&
449 uio->uio_segflg == UIO_USERSPACE &&
450 uio->uio_resid == uio->uio_iov->iov_len &&
451 (((uio->uio_td != NULL) ? uio->uio_td : curthread)->td_pflags &
452 TDP_DEADLKTREAT) == 0) {
453 int secsize; /* Media sector size */
454 off_t filebytes; /* Bytes left of file */
455 int blockbytes; /* Bytes left of file in full blocks */
456 int partialbytes; /* Bytes in last partial block */
457 int skipbytes; /* Bytes not to read in ffs_rawread */
462 /* Only handle sector aligned reads */
464 secsize = ip->i_devvp->v_bufobj.bo_bsize;
465 if ((uio->uio_offset & (secsize - 1)) == 0 &&
466 (uio->uio_resid & (secsize - 1)) == 0) {
468 /* Sync dirty pages and buffers if needed */
469 error = ffs_rawread_sync(vp,
470 (uio->uio_td != NULL) ?
471 uio->uio_td : curthread);
475 /* Check for end of file */
476 if (ip->i_size > uio->uio_offset) {
477 filebytes = ip->i_size - uio->uio_offset;
479 /* No special eof handling needed ? */
480 if (uio->uio_resid <= filebytes) {
482 return ffs_rawread_main(vp, uio);
485 partialbytes = ((unsigned int) ip->i_size) %
487 blockbytes = (int) filebytes - partialbytes;
488 if (blockbytes > 0) {
489 skipbytes = uio->uio_resid -
491 uio->uio_resid = blockbytes;
492 error = ffs_rawread_main(vp, uio);
493 uio->uio_resid += skipbytes;
496 /* Read remaining part using buffer */