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
26 * $FreeBSD: src/sys/ufs/ffs/ffs_rawread.c,v 1.3.2.2 2003/05/29 06:15:35 alc Exp $
27 * $DragonFly: src/sys/vfs/ufs/ffs_rawread.c,v 1.17 2006/03/25 21:31:21 swildner Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/fcntl.h>
36 #include <sys/mount.h>
37 #include <sys/namei.h>
38 #include <sys/vnode.h>
40 #include <sys/filio.h>
41 #include <sys/ttycom.h>
47 #include <machine/limits.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_object.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
54 static int ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t offset,
55 size_t len, struct thread *td, struct buf *bp,
56 caddr_t sa, int *baseticks);
57 static int ffs_rawread_main(struct vnode *vp,
60 static int ffs_rawread_sync(struct vnode *vp, struct thread *td);
62 int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
64 void ffs_rawread_setup(void);
66 static void ffs_rawreadwakeup(struct bio *bio);
69 SYSCTL_DECL(_vfs_ffs);
71 static int ffsrawbufcnt = 4;
72 SYSCTL_INT(_vfs_ffs, OID_AUTO, ffsrawbufcnt, CTLFLAG_RD, &ffsrawbufcnt, 0,
73 "Buffers available for raw reads");
75 static int allowrawread = 1;
76 SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0,
77 "Flag to enable raw reads");
79 static int rawreadahead = 1;
80 SYSCTL_INT(_vfs_ffs, OID_AUTO, rawreadahead, CTLFLAG_RW, &rawreadahead, 0,
81 "Flag to enable readahead for long raw reads");
85 ffs_rawread_setup(void)
87 ffsrawbufcnt = (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8;
92 ffs_rawread_sync(struct vnode *vp, struct thread *td)
97 /* Check for dirty mmap, pending writes and dirty buffers */
99 if (vp->v_track_write.bk_active > 0 ||
100 !RB_EMPTY(&vp->v_rbdirty_tree) ||
101 (vp->v_flag & VOBJDIRTY) != 0) {
104 if (VOP_ISLOCKED(vp, td) != LK_EXCLUSIVE) {
106 /* Upgrade to exclusive lock, this might block */
107 VOP_LOCK(vp, LK_UPGRADE | LK_NOPAUSE, td);
111 /* Attempt to msync mmap() regions to clean dirty mmap */
112 if ((vp->v_flag & VOBJDIRTY) != 0) {
113 struct vm_object *obj;
114 if (VOP_GETVOBJECT(vp, &obj) == 0)
115 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
118 /* Wait for pending writes to complete */
120 while (vp->v_track_write.bk_active) {
121 vp->v_track_write.bk_waitflag = 1;
122 error = tsleep(&vp->v_track_write, 0, "rawrdfls", 0);
126 VOP_LOCK(vp, LK_DOWNGRADE, td);
130 /* Flush dirty buffers */
131 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
133 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0) {
135 VOP_LOCK(vp, LK_DOWNGRADE, td);
139 if (vp->v_track_write.bk_active > 0 ||
140 !RB_EMPTY(&vp->v_rbdirty_tree))
141 panic("ffs_rawread_sync: dirty bufs");
145 VOP_LOCK(vp, LK_DOWNGRADE, td);
154 ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t loffset,
155 size_t len, struct thread *td, struct buf *bp,
156 caddr_t sa, int *baseticks)
165 bsize = vp->v_mount->mnt_stat.f_iosize;
167 iolen = ((vm_offset_t) udata) & PAGE_MASK;
169 if (bp->b_bcount + iolen > bp->b_kvasize) {
170 bp->b_bcount = bp->b_kvasize;
172 bp->b_bcount -= PAGE_SIZE;
174 bp->b_flags = B_PHYS | B_READ;
177 bp->b_loffset = loffset;
178 bp->b_bio2.bio_offset = NOOFFSET;
179 bp->b_bio2.bio_done = ffs_rawreadwakeup;
181 blockoff = (loffset % bsize) / DEV_BSIZE;
183 error = VOP_BMAP(vp, bp->b_loffset, &dp, &bp->b_bio2.bio_offset,
188 if (bp->b_bio2.bio_offset == NOOFFSET) {
190 * Fill holes with NULs to preserve semantics
192 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize)
193 bp->b_bcount = bsize - blockoff * DEV_BSIZE;
194 bp->b_bufsize = bp->b_bcount;
199 if (ticks - *baseticks >= hogticks) {
203 bzero(bp->b_data, bp->b_bufsize);
205 /* Mark operation completed (similar to bufdone()) */
208 bp->b_flags |= B_DONE;
212 if (bp->b_bcount + blockoff * DEV_BSIZE > bforwards)
213 bp->b_bcount = bforwards - blockoff * DEV_BSIZE;
214 bp->b_bufsize = bp->b_bcount;
215 bp->b_bio2.bio_offset += blockoff * DEV_BSIZE;
221 * Access the block device layer using the device vnode (dp) and
222 * the translated block number (bio2) instead of the logical block
225 * Even though we are bypassing the vnode layer, we still
226 * want the vnode state to indicate that an I/O on its behalf
229 bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
230 vn_strategy(dp, &bp->b_bio2);
236 ffs_rawread_main(struct vnode *vp, struct uio *uio)
239 struct buf *bp, *nbp, *tbp;
240 caddr_t sa, nsa, tsa;
242 int baseticks = ticks;
248 td = uio->uio_td ? uio->uio_td : curthread;
249 udata = uio->uio_iov->iov_base;
250 resid = uio->uio_resid;
251 offset = uio->uio_offset;
263 if (bp == NULL) { /* Setup first read */
264 /* XXX: Leave some bufs for swap */
265 bp = getpbuf(&ffsrawbufcnt);
268 error = ffs_rawread_readahead(vp, udata, offset, resid,
269 td, bp, sa, &baseticks);
273 if (resid > bp->b_bufsize) { /* Setup fist readahead */
274 /* XXX: Leave bufs for swap */
275 if (rawreadahead != 0)
276 nbp = trypbuf(&ffsrawbufcnt);
283 nerror = ffs_rawread_readahead(
285 udata + bp->b_bufsize,
286 offset + bp->b_bufsize,
287 resid - bp->b_bufsize,
291 relpbuf(nbp, &ffsrawbufcnt);
299 while ((bp->b_flags & B_DONE) == 0) {
300 tsleep((caddr_t)&bp->b_bio2, 0, "rawrd", 0);
306 iolen = bp->b_bcount - bp->b_resid;
307 if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) {
308 nerror = 0; /* Ignore possible beyond EOF error */
312 if ((bp->b_flags & B_ERROR) != 0) {
316 clearbiocache(&bp->b_bio2);
320 if (iolen < bp->b_bufsize) {
321 /* Incomplete read. Try to read remaining part */
322 error = ffs_rawread_readahead(
324 bp->b_bufsize - iolen, td, bp,
328 } else if (nbp != NULL) { /* Complete read with readahead */
338 clearbiocache(&nbp->b_bio2);
340 if (resid <= bp->b_bufsize) { /* No more readaheads */
341 relpbuf(nbp, &ffsrawbufcnt);
343 } else { /* Setup next readahead */
344 nerror = ffs_rawread_readahead(
345 vp, udata + bp->b_bufsize,
346 offset + bp->b_bufsize,
347 resid - bp->b_bufsize,
348 td, nbp, nsa, &baseticks);
350 relpbuf(nbp, &ffsrawbufcnt);
354 } else if (nerror != 0) {/* Deferred Readahead error */
356 } else if (resid > 0) { /* More to read, no readahead */
357 error = ffs_rawread_readahead(vp, udata, offset,
366 relpbuf(bp, &ffsrawbufcnt);
367 if (nbp != NULL) { /* Run down readahead buffer */
369 while ((nbp->b_flags & B_DONE) == 0) {
370 tsleep(&nbp->b_bio2, 0, "rawrd", 0);
374 relpbuf(nbp, &ffsrawbufcnt);
379 uio->uio_iov->iov_base = udata;
380 uio->uio_resid = resid;
381 uio->uio_offset = offset;
387 ffs_rawread(struct vnode *vp,
391 if (allowrawread != 0 &&
392 uio->uio_iovcnt == 1 &&
393 uio->uio_segflg == UIO_USERSPACE &&
394 uio->uio_resid == uio->uio_iov->iov_len &&
395 (((uio->uio_td != NULL) ? uio->uio_td : curthread)->td_flags &
396 TDF_DEADLKTREAT) == 0) {
397 int secsize; /* Media sector size */
398 off_t filebytes; /* Bytes left of file */
399 int blockbytes; /* Bytes left of file in full blocks */
400 int partialbytes; /* Bytes in last partial block */
401 int skipbytes; /* Bytes not to read in ffs_rawread */
406 /* Only handle sector aligned reads */
408 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
409 if ((uio->uio_offset & (secsize - 1)) == 0 &&
410 (uio->uio_resid & (secsize - 1)) == 0) {
412 /* Sync dirty pages and buffers if needed */
413 error = ffs_rawread_sync(vp,
414 (uio->uio_td != NULL) ?
415 uio->uio_td : curthread);
419 /* Check for end of file */
420 if (ip->i_size > uio->uio_offset) {
421 filebytes = ip->i_size - uio->uio_offset;
423 /* No special eof handling needed ? */
424 if (uio->uio_resid <= filebytes) {
426 return ffs_rawread_main(vp, uio);
429 partialbytes = ((unsigned int) ip->i_size) %
431 blockbytes = (int) filebytes - partialbytes;
432 if (blockbytes > 0) {
433 skipbytes = uio->uio_resid -
435 uio->uio_resid = blockbytes;
436 error = ffs_rawread_main(vp, uio);
437 uio->uio_resid += skipbytes;
440 /* Read remaining part using buffer */
451 ffs_rawreadwakeup(struct bio *bio)