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