18bae9962525c2cdc4febbd59e1509d9af43658b
[dragonfly.git] / sys / vfs / ufs / ffs_rawread.c
1 /*-
2  * Copyright (c) 2000-2003 Tor Egge
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  *
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
24  * SUCH DAMAGE.
25  *
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.28 2008/06/19 23:27:39 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/fcntl.h>
33 #include <sys/file.h>
34 #include <sys/stat.h>
35 #include <sys/proc.h>
36 #include <sys/mount.h>
37 #include <sys/namei.h>
38 #include <sys/vnode.h>
39 #include <sys/conf.h>
40 #include <sys/filio.h>
41 #include <sys/ttycom.h>
42 #include <sys/buf.h>
43 #include "quota.h"
44 #include "inode.h"
45 #include "fs.h"
46
47 #include <machine/limits.h>
48 #include <vm/vm.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_object.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53
54 static int ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t offset,
55                                  size_t len, struct buf *bp, int *baseticks);
56 static int ffs_rawread_main(struct vnode *vp,
57                             struct uio *uio);
58
59 static int ffs_rawread_sync(struct vnode *vp);
60
61 int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
62
63 void ffs_rawread_setup(void);
64
65 SYSCTL_DECL(_vfs_ffs);
66
67 static int ffsrawbufcnt = 4;
68 SYSCTL_INT(_vfs_ffs, OID_AUTO, ffsrawbufcnt, CTLFLAG_RD, &ffsrawbufcnt, 0,
69            "Buffers available for raw reads");
70
71 static int allowrawread = 1;
72 SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0,
73            "Flag to enable raw reads");
74
75 static int rawreadahead = 1;
76 SYSCTL_INT(_vfs_ffs, OID_AUTO, rawreadahead, CTLFLAG_RW, &rawreadahead, 0,
77            "Flag to enable readahead for long raw reads");
78
79
80 void
81 ffs_rawread_setup(void)
82 {
83         ffsrawbufcnt = (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8;
84 }
85
86
87 static int
88 ffs_rawread_sync(struct vnode *vp)
89 {
90         int error;
91         int upgraded;
92         lwkt_tokref vlock;
93
94         /*
95          * Check for dirty mmap, pending writes and dirty buffers
96          */
97         lwkt_gettoken(&vlock, &vp->v_token);
98         if (bio_track_active(&vp->v_track_write) ||
99             !RB_EMPTY(&vp->v_rbdirty_tree) ||
100             (vp->v_flag & VOBJDIRTY) != 0) {
101                 if (vn_islocked(vp) != LK_EXCLUSIVE) {
102                         upgraded = 1;
103                         /* Upgrade to exclusive lock, this might block */
104                         vn_lock(vp, LK_UPGRADE);
105                 } else
106                         upgraded = 0;
107                 
108                 /* Attempt to msync mmap() regions to clean dirty mmap */ 
109                 if ((vp->v_flag & VOBJDIRTY) != 0) {
110                         struct vm_object *obj;
111                         if ((obj = vp->v_object) != NULL)
112                                 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
113                 }
114
115                 /* Wait for pending writes to complete */
116                 error = bio_track_wait(&vp->v_track_write, 0, 0);
117                 if (error != 0) {
118                         if (upgraded != 0)
119                                 vn_lock(vp, LK_DOWNGRADE);
120                         goto done;
121                 }
122                 /* Flush dirty buffers */
123                 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
124                         if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0) {
125                                 if (upgraded != 0)
126                                         vn_lock(vp, LK_DOWNGRADE);
127                                 goto done;
128                         }
129                         if (bio_track_active(&vp->v_track_write) ||
130                             !RB_EMPTY(&vp->v_rbdirty_tree))
131                                 panic("ffs_rawread_sync: dirty bufs");
132                 }
133                 if (upgraded != 0)
134                         vn_lock(vp, LK_DOWNGRADE);
135         } else {
136                 error = 0;
137         }
138 done:
139         lwkt_reltoken(&vlock);
140         return error;
141 }
142
143
144 static int
145 ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t loffset,
146                       size_t len, struct buf *bp, int *baseticks)
147 {
148         int error;
149         int iolen;
150         int blockoff;
151         int bsize;
152         struct vnode *dp;
153         int bforwards;
154         
155         bsize = vp->v_mount->mnt_stat.f_iosize;
156
157         /*
158          * Make sure it fits into the pbuf
159          */
160         iolen = (int)(intptr_t)udata & PAGE_MASK;
161         if (len + iolen > bp->b_kvasize) {
162                 len = bp->b_kvasize;
163                 if (iolen != 0)
164                         len -= PAGE_SIZE;
165         }
166
167         /*
168          * Raw disk address is in bio2, but we wait for it to
169          * chain to bio1.
170          */
171         bp->b_flags &= ~B_ERROR;
172         bp->b_loffset = loffset;
173         bp->b_bio2.bio_offset = NOOFFSET;
174         bp->b_bio1.bio_done = biodone_sync;
175         bp->b_bio1.bio_flags |= BIO_SYNC;
176
177         blockoff = (loffset % bsize) / DEV_BSIZE;
178
179         error = VOP_BMAP(vp, bp->b_loffset, &bp->b_bio2.bio_offset,
180                          &bforwards, NULL, BUF_CMD_READ);
181         if (error != 0)
182                 return error;
183         dp = VTOI(vp)->i_devvp;
184         if (bp->b_bio2.bio_offset == NOOFFSET) {
185                 /* 
186                  * Fill holes with NULs to preserve semantics 
187                  */
188                 if (len + blockoff * DEV_BSIZE > bsize)
189                         len = bsize - blockoff * DEV_BSIZE;
190                 
191                 if (vmapbuf(bp, udata, len) < 0)
192                         return EFAULT;
193                 
194                 if (ticks - *baseticks >= hogticks) {
195                         *baseticks = ticks;
196                         uio_yield();
197                 }
198                 bzero(bp->b_data, bp->b_bcount);
199
200                 /* Mark operation completed (similar to bufdone()) */
201
202                 bp->b_resid = 0;
203                 return 0;
204         }
205         
206         if (len + blockoff * DEV_BSIZE > bforwards)
207                 len = bforwards - blockoff * DEV_BSIZE;
208         bp->b_bio2.bio_offset += blockoff * DEV_BSIZE;
209         
210         if (vmapbuf(bp, udata, len) < 0)
211                 return EFAULT;
212         
213         /*
214          * Access the block device layer using the device vnode (dp) and
215          * the translated block number (bio2) instead of the logical block
216          * number (bio1).
217          *
218          * Even though we are bypassing the vnode layer, we still
219          * want the vnode state to indicate that an I/O on its behalf
220          * is in progress.
221          */
222         bp->b_cmd = BUF_CMD_READ;
223         bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
224         vn_strategy(dp, &bp->b_bio2);
225         return 0;
226 }
227
228 static int
229 ffs_rawread_main(struct vnode *vp, struct uio *uio)
230 {
231         int error, nerror;
232         struct buf *bp, *nbp, *tbp;
233         int iolen;
234         int baseticks = ticks;
235         caddr_t udata;
236         int resid;
237         off_t offset;
238         
239         udata = uio->uio_iov->iov_base;
240         resid = uio->uio_resid;
241         offset = uio->uio_offset;
242
243         error = 0;
244         nerror = 0;
245         
246         bp = NULL;
247         nbp = NULL;
248         
249         while (resid > 0) {
250                 
251                 if (bp == NULL) { /* Setup first read */
252                         /* XXX: Leave some bufs for swap */
253                         bp = getpbuf(&ffsrawbufcnt);
254                         error = ffs_rawread_readahead(vp, udata, offset, resid,
255                                                       bp, &baseticks);
256                         if (error != 0)
257                                 break;
258                         
259                         if (resid > bp->b_bufsize) { /* Setup fist readahead */
260                                 /* XXX: Leave bufs for swap */
261                                 if (rawreadahead != 0) 
262                                         nbp = trypbuf(&ffsrawbufcnt);
263                                 else
264                                         nbp = NULL;
265                                 if (nbp != NULL) {
266                                         nerror = ffs_rawread_readahead(
267                                                         vp, 
268                                                         udata + bp->b_bufsize,
269                                                         offset + bp->b_bufsize,
270                                                         resid - bp->b_bufsize,
271                                                         nbp, &baseticks);
272                                         if (nerror) {
273                                                 relpbuf(nbp, &ffsrawbufcnt);
274                                                 nbp = NULL;
275                                         }
276                                 }
277                         }
278                 }
279                 
280                 biowait(&bp->b_bio1, "rawrd");
281                 
282                 vunmapbuf(bp);
283                 
284                 iolen = bp->b_bcount - bp->b_resid;
285                 if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) {
286                         nerror = 0;     /* Ignore possible beyond EOF error */
287                         break; /* EOF */
288                 }
289                 
290                 if ((bp->b_flags & B_ERROR) != 0) {
291                         error = bp->b_error;
292                         break;
293                 }
294                 clearbiocache(&bp->b_bio2);
295                 resid -= iolen;
296                 udata += iolen;
297                 offset += iolen;
298                 if (iolen < bp->b_bufsize) {
299                         /* Incomplete read.  Try to read remaining part */
300                         error = ffs_rawread_readahead(
301                                     vp, udata, offset,
302                                     bp->b_bufsize - iolen, bp, &baseticks);
303                         if (error != 0)
304                                 break;
305                 } else if (nbp != NULL) { /* Complete read with readahead */
306                         
307                         tbp = bp;
308                         bp = nbp;
309                         nbp = tbp;
310                         
311                         clearbiocache(&nbp->b_bio2);
312                         
313                         if (resid <= bp->b_bufsize) { /* No more readaheads */
314                                 relpbuf(nbp, &ffsrawbufcnt);
315                                 nbp = NULL;
316                         } else { /* Setup next readahead */
317                                 nerror = ffs_rawread_readahead(
318                                                 vp, udata + bp->b_bufsize,
319                                                 offset + bp->b_bufsize,
320                                                 resid - bp->b_bufsize,
321                                                 nbp, &baseticks);
322                                 if (nerror != 0) {
323                                         relpbuf(nbp, &ffsrawbufcnt);
324                                         nbp = NULL;
325                                 }
326                         }
327                 } else if (nerror != 0) {/* Deferred Readahead error */
328                         break;          
329                 }  else if (resid > 0) { /* More to read, no readahead */
330                         error = ffs_rawread_readahead(vp, udata, offset,
331                                                       resid, bp,
332                                                       &baseticks);
333                         if (error != 0)
334                                 break;
335                 }
336         }
337         
338         if (bp != NULL)
339                 relpbuf(bp, &ffsrawbufcnt);
340         if (nbp != NULL) {                      /* Run down readahead buffer */
341                 biowait(&nbp->b_bio1, "rawrd");
342                 vunmapbuf(nbp);
343                 relpbuf(nbp, &ffsrawbufcnt);
344         }
345         
346         if (error == 0)
347                 error = nerror;
348         uio->uio_iov->iov_base = udata;
349         uio->uio_resid = resid;
350         uio->uio_offset = offset;
351         return error;
352 }
353
354
355 int
356 ffs_rawread(struct vnode *vp,
357             struct uio *uio,
358             int *workdone)
359 {
360         if (allowrawread != 0 &&
361             uio->uio_iovcnt == 1 && 
362             uio->uio_segflg == UIO_USERSPACE &&
363             uio->uio_resid == uio->uio_iov->iov_len &&
364             (curthread->td_flags & TDF_DEADLKTREAT) == 0) {
365                 int secsize;            /* Media sector size */
366                 off_t filebytes;        /* Bytes left of file */
367                 int blockbytes;         /* Bytes left of file in full blocks */
368                 int partialbytes;       /* Bytes in last partial block */
369                 int skipbytes;          /* Bytes not to read in ffs_rawread */
370                 struct inode *ip;
371                 int error;
372                 
373
374                 /* Only handle sector aligned reads */
375                 ip = VTOI(vp);
376                 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
377                 if ((uio->uio_offset & (secsize - 1)) == 0 &&
378                     (uio->uio_resid & (secsize - 1)) == 0) {
379                         
380                         /* Sync dirty pages and buffers if needed */
381                         error = ffs_rawread_sync(vp);
382                         if (error != 0)
383                                 return error;
384                         
385                         /* Check for end of file */
386                         if (ip->i_size > uio->uio_offset) {
387                                 filebytes = ip->i_size - uio->uio_offset;
388
389                                 /* No special eof handling needed ? */
390                                 if (uio->uio_resid <= filebytes) {
391                                         *workdone = 1;
392                                         return ffs_rawread_main(vp, uio);
393                                 }
394                                 
395                                 partialbytes = ((unsigned int) ip->i_size) %
396                                         ip->i_fs->fs_bsize;
397                                 blockbytes = (int) filebytes - partialbytes;
398                                 if (blockbytes > 0) {
399                                         skipbytes = uio->uio_resid -
400                                                 blockbytes;
401                                         uio->uio_resid = blockbytes;
402                                         error = ffs_rawread_main(vp, uio);
403                                         uio->uio_resid += skipbytes;
404                                         if (error != 0)
405                                                 return error;
406                                         /* Read remaining part using buffer */
407                                 }
408                         }
409                 }
410         }
411         *workdone = 0;
412         return 0;
413 }
414