Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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 static void ffs_rawreadwakeup(struct bio *bio);
66
67
68 SYSCTL_DECL(_vfs_ffs);
69
70 static int ffsrawbufcnt = 4;
71 SYSCTL_INT(_vfs_ffs, OID_AUTO, ffsrawbufcnt, CTLFLAG_RD, &ffsrawbufcnt, 0,
72            "Buffers available for raw reads");
73
74 static int allowrawread = 1;
75 SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0,
76            "Flag to enable raw reads");
77
78 static int rawreadahead = 1;
79 SYSCTL_INT(_vfs_ffs, OID_AUTO, rawreadahead, CTLFLAG_RW, &rawreadahead, 0,
80            "Flag to enable readahead for long raw reads");
81
82
83 void
84 ffs_rawread_setup(void)
85 {
86         ffsrawbufcnt = (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8;
87 }
88
89
90 static int
91 ffs_rawread_sync(struct vnode *vp)
92 {
93         int error;
94         int upgraded;
95         lwkt_tokref vlock;
96
97         /*
98          * Check for dirty mmap, pending writes and dirty buffers
99          */
100         lwkt_gettoken(&vlock, &vp->v_token);
101         if (bio_track_active(&vp->v_track_write) ||
102             !RB_EMPTY(&vp->v_rbdirty_tree) ||
103             (vp->v_flag & VOBJDIRTY) != 0) {
104                 if (vn_islocked(vp) != LK_EXCLUSIVE) {
105                         upgraded = 1;
106                         /* Upgrade to exclusive lock, this might block */
107                         vn_lock(vp, LK_UPGRADE);
108                 } else
109                         upgraded = 0;
110                 
111                 /* Attempt to msync mmap() regions to clean dirty mmap */ 
112                 if ((vp->v_flag & VOBJDIRTY) != 0) {
113                         struct vm_object *obj;
114                         if ((obj = vp->v_object) != NULL)
115                                 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
116                 }
117
118                 /* Wait for pending writes to complete */
119                 error = bio_track_wait(&vp->v_track_write, 0, 0);
120                 if (error != 0) {
121                         if (upgraded != 0)
122                                 vn_lock(vp, LK_DOWNGRADE);
123                         goto done;
124                 }
125                 /* Flush dirty buffers */
126                 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
127                         if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0) {
128                                 if (upgraded != 0)
129                                         vn_lock(vp, LK_DOWNGRADE);
130                                 goto done;
131                         }
132                         if (bio_track_active(&vp->v_track_write) ||
133                             !RB_EMPTY(&vp->v_rbdirty_tree))
134                                 panic("ffs_rawread_sync: dirty bufs");
135                 }
136                 if (upgraded != 0)
137                         vn_lock(vp, LK_DOWNGRADE);
138         } else {
139                 error = 0;
140         }
141 done:
142         lwkt_reltoken(&vlock);
143         return error;
144 }
145
146
147 static int
148 ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t loffset,
149                       size_t len, struct buf *bp, int *baseticks)
150 {
151         int error;
152         int iolen;
153         int blockoff;
154         int bsize;
155         struct vnode *dp;
156         int bforwards;
157         
158         bsize = vp->v_mount->mnt_stat.f_iosize;
159
160         /*
161          * Make sure it fits into the pbuf
162          */
163         iolen = (int)(intptr_t)udata & PAGE_MASK;
164         if (len + iolen > bp->b_kvasize) {
165                 len = bp->b_kvasize;
166                 if (iolen != 0)
167                         len -= PAGE_SIZE;
168         }
169         bp->b_flags &= ~B_ERROR;
170         bp->b_loffset = loffset;
171         bp->b_bio2.bio_offset = NOOFFSET;
172         bp->b_bio2.bio_done = ffs_rawreadwakeup;
173
174         blockoff = (loffset % bsize) / DEV_BSIZE;
175
176         error = VOP_BMAP(vp, bp->b_loffset, &bp->b_bio2.bio_offset,
177                          &bforwards, NULL, BUF_CMD_READ);
178         if (error != 0)
179                 return error;
180         dp = VTOI(vp)->i_devvp;
181         if (bp->b_bio2.bio_offset == NOOFFSET) {
182                 /* 
183                  * Fill holes with NULs to preserve semantics 
184                  */
185                 if (len + blockoff * DEV_BSIZE > bsize)
186                         len = bsize - blockoff * DEV_BSIZE;
187                 
188                 if (vmapbuf(bp, udata, len) < 0)
189                         return EFAULT;
190                 
191                 if (ticks - *baseticks >= hogticks) {
192                         *baseticks = ticks;
193                         uio_yield();
194                 }
195                 bzero(bp->b_data, bp->b_bcount);
196
197                 /* Mark operation completed (similar to bufdone()) */
198
199                 bp->b_resid = 0;
200                 return 0;
201         }
202         
203         if (len + blockoff * DEV_BSIZE > bforwards)
204                 len = bforwards - blockoff * DEV_BSIZE;
205         bp->b_bio2.bio_offset += blockoff * DEV_BSIZE;
206         
207         if (vmapbuf(bp, udata, len) < 0)
208                 return EFAULT;
209         
210         /*
211          * Access the block device layer using the device vnode (dp) and
212          * the translated block number (bio2) instead of the logical block
213          * number (bio1).
214          *
215          * Even though we are bypassing the vnode layer, we still
216          * want the vnode state to indicate that an I/O on its behalf
217          * is in progress.
218          */
219         bp->b_cmd = BUF_CMD_READ;
220         bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
221         vn_strategy(dp, &bp->b_bio2);
222         return 0;
223 }
224
225 static int
226 ffs_rawread_main(struct vnode *vp, struct uio *uio)
227 {
228         int error, nerror;
229         struct buf *bp, *nbp, *tbp;
230         int iolen;
231         int baseticks = ticks;
232         caddr_t udata;
233         int resid;
234         off_t offset;
235         
236         udata = uio->uio_iov->iov_base;
237         resid = uio->uio_resid;
238         offset = uio->uio_offset;
239
240         error = 0;
241         nerror = 0;
242         
243         bp = NULL;
244         nbp = NULL;
245         
246         while (resid > 0) {
247                 
248                 if (bp == NULL) { /* Setup first read */
249                         /* XXX: Leave some bufs for swap */
250                         bp = getpbuf(&ffsrawbufcnt);
251                         error = ffs_rawread_readahead(vp, udata, offset, resid,
252                                                       bp, &baseticks);
253                         if (error != 0)
254                                 break;
255                         
256                         if (resid > bp->b_bufsize) { /* Setup fist readahead */
257                                 /* XXX: Leave bufs for swap */
258                                 if (rawreadahead != 0) 
259                                         nbp = trypbuf(&ffsrawbufcnt);
260                                 else
261                                         nbp = NULL;
262                                 if (nbp != NULL) {
263                                         nerror = ffs_rawread_readahead(
264                                                         vp, 
265                                                         udata + bp->b_bufsize,
266                                                         offset + bp->b_bufsize,
267                                                         resid - bp->b_bufsize,
268                                                         nbp, &baseticks);
269                                         if (nerror) {
270                                                 relpbuf(nbp, &ffsrawbufcnt);
271                                                 nbp = NULL;
272                                         }
273                                 }
274                         }
275                 }
276                 
277                 crit_enter();
278                 while (bp->b_cmd != BUF_CMD_DONE)
279                         tsleep((caddr_t)&bp->b_bio2, 0, "rawrd", 0);
280                 crit_exit();
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                 crit_enter();
342                 while (nbp->b_cmd != BUF_CMD_DONE)
343                         tsleep(&nbp->b_bio2, 0, "rawrd", 0);
344                 crit_exit();
345                 vunmapbuf(nbp);
346                 relpbuf(nbp, &ffsrawbufcnt);
347         }
348         
349         if (error == 0)
350                 error = nerror;
351         uio->uio_iov->iov_base = udata;
352         uio->uio_resid = resid;
353         uio->uio_offset = offset;
354         return error;
355 }
356
357
358 int
359 ffs_rawread(struct vnode *vp,
360             struct uio *uio,
361             int *workdone)
362 {
363         if (allowrawread != 0 &&
364             uio->uio_iovcnt == 1 && 
365             uio->uio_segflg == UIO_USERSPACE &&
366             uio->uio_resid == uio->uio_iov->iov_len &&
367             (curthread->td_flags & TDF_DEADLKTREAT) == 0) {
368                 int secsize;            /* Media sector size */
369                 off_t filebytes;        /* Bytes left of file */
370                 int blockbytes;         /* Bytes left of file in full blocks */
371                 int partialbytes;       /* Bytes in last partial block */
372                 int skipbytes;          /* Bytes not to read in ffs_rawread */
373                 struct inode *ip;
374                 int error;
375                 
376
377                 /* Only handle sector aligned reads */
378                 ip = VTOI(vp);
379                 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
380                 if ((uio->uio_offset & (secsize - 1)) == 0 &&
381                     (uio->uio_resid & (secsize - 1)) == 0) {
382                         
383                         /* Sync dirty pages and buffers if needed */
384                         error = ffs_rawread_sync(vp);
385                         if (error != 0)
386                                 return error;
387                         
388                         /* Check for end of file */
389                         if (ip->i_size > uio->uio_offset) {
390                                 filebytes = ip->i_size - uio->uio_offset;
391
392                                 /* No special eof handling needed ? */
393                                 if (uio->uio_resid <= filebytes) {
394                                         *workdone = 1;
395                                         return ffs_rawread_main(vp, uio);
396                                 }
397                                 
398                                 partialbytes = ((unsigned int) ip->i_size) %
399                                         ip->i_fs->fs_bsize;
400                                 blockbytes = (int) filebytes - partialbytes;
401                                 if (blockbytes > 0) {
402                                         skipbytes = uio->uio_resid -
403                                                 blockbytes;
404                                         uio->uio_resid = blockbytes;
405                                         error = ffs_rawread_main(vp, uio);
406                                         uio->uio_resid += skipbytes;
407                                         if (error != 0)
408                                                 return error;
409                                         /* Read remaining part using buffer */
410                                 }
411                         }
412                 }
413         }
414         *workdone = 0;
415         return 0;
416 }
417
418
419 static void
420 ffs_rawreadwakeup(struct bio *bio)
421 {
422         bio->bio_buf->b_cmd = BUF_CMD_DONE;
423         wakeup(bio);
424 }