13550dc1bcb6255b3961c2b54540cd54330e1af3
[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);
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
93         /*
94          * Check for dirty mmap, pending writes and dirty buffers
95          */
96         lwkt_gettoken(&vp->v_token);
97         if (bio_track_active(&vp->v_track_write) ||
98             !RB_EMPTY(&vp->v_rbdirty_tree) ||
99             (vp->v_flag & VOBJDIRTY) != 0) {
100                 if (vn_islocked(vp) != LK_EXCLUSIVE) {
101                         upgraded = 1;
102                         /* Upgrade to exclusive lock, this might block */
103                         vn_lock(vp, LK_UPGRADE);
104                 } else
105                         upgraded = 0;
106                 
107                 /* Attempt to msync mmap() regions to clean dirty mmap */ 
108                 if ((vp->v_flag & VOBJDIRTY) != 0) {
109                         struct vm_object *obj;
110                         if ((obj = vp->v_object) != NULL)
111                                 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
112                 }
113
114                 /* Wait for pending writes to complete */
115                 error = bio_track_wait(&vp->v_track_write, 0, 0);
116                 if (error != 0) {
117                         if (upgraded != 0)
118                                 vn_lock(vp, LK_DOWNGRADE);
119                         goto done;
120                 }
121                 /* Flush dirty buffers */
122                 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
123                         if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0) {
124                                 if (upgraded != 0)
125                                         vn_lock(vp, LK_DOWNGRADE);
126                                 goto done;
127                         }
128                         if (bio_track_active(&vp->v_track_write) ||
129                             !RB_EMPTY(&vp->v_rbdirty_tree))
130                                 panic("ffs_rawread_sync: dirty bufs");
131                 }
132                 if (upgraded != 0)
133                         vn_lock(vp, LK_DOWNGRADE);
134         } else {
135                 error = 0;
136         }
137 done:
138         lwkt_reltoken(&vp->v_token);
139         return error;
140 }
141
142
143 static int
144 ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t loffset,
145                       size_t len, struct buf *bp)
146 {
147         int error;
148         int iolen;
149         int blockoff;
150         int bsize;
151         struct vnode *dp;
152         int bforwards;
153         
154         bsize = vp->v_mount->mnt_stat.f_iosize;
155
156         /*
157          * Make sure it fits into the pbuf
158          */
159         iolen = (int)(intptr_t)udata & PAGE_MASK;
160         if (len + iolen > bp->b_kvasize) {
161                 len = bp->b_kvasize;
162                 if (iolen != 0)
163                         len -= PAGE_SIZE;
164         }
165
166         /*
167          * Raw disk address is in bio2, but we wait for it to
168          * chain to bio1.
169          */
170         bp->b_flags &= ~B_ERROR;
171         bp->b_loffset = loffset;
172         bp->b_bio2.bio_offset = NOOFFSET;
173         bp->b_bio1.bio_done = biodone_sync;
174         bp->b_bio1.bio_flags |= BIO_SYNC;
175
176         blockoff = (loffset % bsize) / DEV_BSIZE;
177
178         error = VOP_BMAP(vp, bp->b_loffset, &bp->b_bio2.bio_offset,
179                          &bforwards, NULL, BUF_CMD_READ);
180         if (error != 0)
181                 return error;
182         dp = VTOI(vp)->i_devvp;
183         if (bp->b_bio2.bio_offset == NOOFFSET) {
184                 /* 
185                  * Fill holes with NULs to preserve semantics 
186                  */
187                 if (len + blockoff * DEV_BSIZE > bsize)
188                         len = bsize - blockoff * DEV_BSIZE;
189                 
190                 if (vmapbuf(bp, udata, len) < 0)
191                         return EFAULT;
192                 
193                 lwkt_user_yield();
194                 bzero(bp->b_data, bp->b_bcount);
195
196                 /* Mark operation completed (similar to bufdone()) */
197
198                 bp->b_resid = 0;
199                 return 0;
200         }
201         
202         if (len + blockoff * DEV_BSIZE > bforwards)
203                 len = bforwards - blockoff * DEV_BSIZE;
204         bp->b_bio2.bio_offset += blockoff * DEV_BSIZE;
205         
206         if (vmapbuf(bp, udata, len) < 0)
207                 return EFAULT;
208         
209         /*
210          * Access the block device layer using the device vnode (dp) and
211          * the translated block number (bio2) instead of the logical block
212          * number (bio1).
213          *
214          * Even though we are bypassing the vnode layer, we still
215          * want the vnode state to indicate that an I/O on its behalf
216          * is in progress.
217          */
218         bp->b_cmd = BUF_CMD_READ;
219         bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
220         vn_strategy(dp, &bp->b_bio2);
221         return 0;
222 }
223
224 static int
225 ffs_rawread_main(struct vnode *vp, struct uio *uio)
226 {
227         int error, nerror;
228         struct buf *bp, *nbp, *tbp;
229         int iolen;
230         caddr_t udata;
231         int resid;
232         off_t offset;
233         
234         udata = uio->uio_iov->iov_base;
235         resid = uio->uio_resid;
236         offset = uio->uio_offset;
237
238         error = 0;
239         nerror = 0;
240         
241         bp = NULL;
242         nbp = NULL;
243         
244         while (resid > 0) {
245                 
246                 if (bp == NULL) { /* Setup first read */
247                         /* XXX: Leave some bufs for swap */
248                         bp = getpbuf_kva(&ffsrawbufcnt);
249                         error = ffs_rawread_readahead(vp, udata, offset,
250                                                       resid, bp);
251                         if (error != 0)
252                                 break;
253                         
254                         if (resid > bp->b_bufsize) { /* Setup fist readahead */
255                                 /* XXX: Leave bufs for swap */
256                                 if (rawreadahead != 0) 
257                                         nbp = trypbuf_kva(&ffsrawbufcnt);
258                                 else
259                                         nbp = NULL;
260                                 if (nbp != NULL) {
261                                         nerror = ffs_rawread_readahead(
262                                                         vp, 
263                                                         udata + bp->b_bufsize,
264                                                         offset + bp->b_bufsize,
265                                                         resid - bp->b_bufsize,
266                                                         nbp);
267                                         if (nerror) {
268                                                 relpbuf(nbp, &ffsrawbufcnt);
269                                                 nbp = NULL;
270                                         }
271                                 }
272                         }
273                 }
274                 
275                 biowait(&bp->b_bio1, "rawrd");
276                 
277                 vunmapbuf(bp);
278                 
279                 iolen = bp->b_bcount - bp->b_resid;
280                 if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) {
281                         nerror = 0;     /* Ignore possible beyond EOF error */
282                         break; /* EOF */
283                 }
284                 
285                 if ((bp->b_flags & B_ERROR) != 0) {
286                         error = bp->b_error;
287                         break;
288                 }
289                 clearbiocache(&bp->b_bio2);
290                 resid -= iolen;
291                 udata += iolen;
292                 offset += iolen;
293                 if (iolen < bp->b_bufsize) {
294                         /* Incomplete read.  Try to read remaining part */
295                         error = ffs_rawread_readahead(
296                                     vp, udata, offset,
297                                     bp->b_bufsize - iolen, bp);
298                         if (error != 0)
299                                 break;
300                 } else if (nbp != NULL) { /* Complete read with readahead */
301                         
302                         tbp = bp;
303                         bp = nbp;
304                         nbp = tbp;
305                         
306                         clearbiocache(&nbp->b_bio2);
307                         
308                         if (resid <= bp->b_bufsize) { /* No more readaheads */
309                                 relpbuf(nbp, &ffsrawbufcnt);
310                                 nbp = NULL;
311                         } else { /* Setup next readahead */
312                                 nerror = ffs_rawread_readahead(
313                                                 vp, udata + bp->b_bufsize,
314                                                 offset + bp->b_bufsize,
315                                                 resid - bp->b_bufsize,
316                                                 nbp);
317                                 if (nerror != 0) {
318                                         relpbuf(nbp, &ffsrawbufcnt);
319                                         nbp = NULL;
320                                 }
321                         }
322                 } else if (nerror != 0) {/* Deferred Readahead error */
323                         break;          
324                 }  else if (resid > 0) { /* More to read, no readahead */
325                         error = ffs_rawread_readahead(vp, udata, offset,
326                                                       resid, bp);
327                         if (error != 0)
328                                 break;
329                 }
330         }
331         
332         if (bp != NULL)
333                 relpbuf(bp, &ffsrawbufcnt);
334         if (nbp != NULL) {                      /* Run down readahead buffer */
335                 biowait(&nbp->b_bio1, "rawrd");
336                 vunmapbuf(nbp);
337                 relpbuf(nbp, &ffsrawbufcnt);
338         }
339         
340         if (error == 0)
341                 error = nerror;
342         uio->uio_iov->iov_base = udata;
343         uio->uio_resid = resid;
344         uio->uio_offset = offset;
345         return error;
346 }
347
348
349 int
350 ffs_rawread(struct vnode *vp,
351             struct uio *uio,
352             int *workdone)
353 {
354         if (allowrawread != 0 &&
355             uio->uio_iovcnt == 1 && 
356             uio->uio_segflg == UIO_USERSPACE &&
357             uio->uio_resid == uio->uio_iov->iov_len &&
358             (curthread->td_flags & TDF_DEADLKTREAT) == 0) {
359                 int secsize;            /* Media sector size */
360                 off_t filebytes;        /* Bytes left of file */
361                 int blockbytes;         /* Bytes left of file in full blocks */
362                 int partialbytes;       /* Bytes in last partial block */
363                 int skipbytes;          /* Bytes not to read in ffs_rawread */
364                 struct inode *ip;
365                 int error;
366                 
367
368                 /* Only handle sector aligned reads */
369                 ip = VTOI(vp);
370                 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
371                 if ((uio->uio_offset & (secsize - 1)) == 0 &&
372                     (uio->uio_resid & (secsize - 1)) == 0) {
373                         
374                         /* Sync dirty pages and buffers if needed */
375                         error = ffs_rawread_sync(vp);
376                         if (error != 0)
377                                 return error;
378                         
379                         /* Check for end of file */
380                         if (ip->i_size > uio->uio_offset) {
381                                 filebytes = ip->i_size - uio->uio_offset;
382
383                                 /* No special eof handling needed ? */
384                                 if (uio->uio_resid <= filebytes) {
385                                         *workdone = 1;
386                                         return ffs_rawread_main(vp, uio);
387                                 }
388                                 
389                                 partialbytes = ((unsigned int) ip->i_size) %
390                                         ip->i_fs->fs_bsize;
391                                 blockbytes = (int) filebytes - partialbytes;
392                                 if (blockbytes > 0) {
393                                         skipbytes = uio->uio_resid -
394                                                 blockbytes;
395                                         uio->uio_resid = blockbytes;
396                                         error = ffs_rawread_main(vp, uio);
397                                         uio->uio_resid += skipbytes;
398                                         if (error != 0)
399                                                 return error;
400                                         /* Read remaining part using buffer */
401                                 }
402                         }
403                 }
404         }
405         *workdone = 0;
406         return 0;
407 }
408