Major BUF/BIO work commit. Make I/O BIO-centric and specify the disk or
[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.15 2006/03/24 18:35:34 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 thread *td, struct buf *bp,
56                                  caddr_t sa, int *baseticks);
57 static int ffs_rawread_main(struct vnode *vp,
58                             struct uio *uio);
59
60 static int ffs_rawread_sync(struct vnode *vp, struct thread *td);
61
62 int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
63
64 void ffs_rawread_setup(void);
65
66 static void ffs_rawreadwakeup(struct bio *bio);
67
68
69 SYSCTL_DECL(_vfs_ffs);
70
71 static int ffsrawbufcnt = 4;
72 SYSCTL_INT(_vfs_ffs, OID_AUTO, ffsrawbufcnt, CTLFLAG_RD, &ffsrawbufcnt, 0,
73            "Buffers available for raw reads");
74
75 static int allowrawread = 1;
76 SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0,
77            "Flag to enable raw reads");
78
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");
82
83
84 void
85 ffs_rawread_setup(void)
86 {
87         ffsrawbufcnt = (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8;
88 }
89
90
91 static int
92 ffs_rawread_sync(struct vnode *vp, struct thread *td)
93 {
94         int error;
95         int upgraded;
96
97         /* Check for dirty mmap, pending writes and dirty buffers */
98         crit_enter();
99         if (vp->v_track_write.bk_active > 0 ||
100             !RB_EMPTY(&vp->v_rbdirty_tree) ||
101             (vp->v_flag & VOBJDIRTY) != 0) {
102                 crit_exit();
103
104                 if (VOP_ISLOCKED(vp, td) != LK_EXCLUSIVE) {
105                         upgraded = 1;
106                         /* Upgrade to exclusive lock, this might block */
107                         VOP_LOCK(vp, LK_UPGRADE | LK_NOPAUSE, td);
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 (VOP_GETVOBJECT(vp, &obj) == 0)
115                                 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
116                 }
117
118                 /* Wait for pending writes to complete */
119                 crit_enter();
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);
123                         if (error != 0) {
124                                 crit_exit();
125                                 if (upgraded != 0)
126                                         VOP_LOCK(vp, LK_DOWNGRADE, td);
127                                 return (error);
128                         }
129                 }
130                 /* Flush dirty buffers */
131                 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
132                         crit_exit();
133                         if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0) {
134                                 if (upgraded != 0)
135                                         VOP_LOCK(vp, LK_DOWNGRADE, td);
136                                 return (error);
137                         }
138                         crit_enter();
139                         if (vp->v_track_write.bk_active > 0 ||
140                             !RB_EMPTY(&vp->v_rbdirty_tree))
141                                 panic("ffs_rawread_sync: dirty bufs");
142                 }
143                 crit_exit();
144                 if (upgraded != 0)
145                         VOP_LOCK(vp, LK_DOWNGRADE, td);
146         } else {
147                 crit_exit();
148         }
149         return 0;
150 }
151
152
153 static int
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)
157 {
158         int error;
159         uint iolen;
160         off_t blockno;
161         int blockoff;
162         int bsize;
163         struct vnode *dp;
164         int bforwards;
165         
166         bsize = vp->v_mount->mnt_stat.f_iosize;
167         
168         iolen = ((vm_offset_t) udata) & PAGE_MASK;
169         bp->b_bcount = len;
170         if (bp->b_bcount + iolen > bp->b_kvasize) {
171                 bp->b_bcount = bp->b_kvasize;
172                 if (iolen != 0)
173                         bp->b_bcount -= PAGE_SIZE;
174         }
175         bp->b_flags = B_PHYS | B_READ;
176         bp->b_data = udata;
177         bp->b_saveaddr = sa;
178         bp->b_loffset = loffset;
179         bp->b_bio2.bio_offset = NOOFFSET;
180         bp->b_bio2.bio_done = ffs_rawreadwakeup;
181
182         error = VOP_BMAP(vp, bp->b_loffset, &dp, &bp->b_bio2.bio_offset,
183                          &bforwards, NULL);
184         if (error != 0) {
185                 return error;
186         }
187         if (bp->b_bio2.bio_offset == NOOFFSET) {
188                 /* 
189                  * Fill holes with NULs to preserve semantics 
190                  */
191                 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize)
192                         bp->b_bcount = bsize - blockoff * DEV_BSIZE;
193                 bp->b_bufsize = bp->b_bcount;
194                 
195                 if (vmapbuf(bp) < 0)
196                         return EFAULT;
197                 
198                 if (ticks - *baseticks >= hogticks) {
199                         *baseticks = ticks;
200                         uio_yield();
201                 }
202                 bzero(bp->b_data, bp->b_bufsize);
203
204                 /* Mark operation completed (similar to bufdone()) */
205
206                 bp->b_resid = 0;
207                 bp->b_flags |= B_DONE;
208                 return 0;
209         }
210         
211         if (bp->b_bcount + blockoff * DEV_BSIZE > bforwards)
212                 bp->b_bcount = bforwards - blockoff * DEV_BSIZE;
213         bp->b_bufsize = bp->b_bcount;
214         bp->b_bio2.bio_offset += blockoff * DEV_BSIZE;
215         
216         if (vmapbuf(bp) < 0)
217                 return EFAULT;
218         
219         /*
220          * Access the block device layer using the device vnode (dp) and
221          * the translated block number (bio2) instead of the logical block
222          * number (bio1).
223          *
224          * Even though we are bypassing the vnode layer, we still
225          * want the vnode state to indicate that an I/O on its behalf
226          * is in progress.
227          */
228         bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
229         vn_strategy(dp, &bp->b_bio2);
230         return 0;
231 }
232
233
234 static int
235 ffs_rawread_main(struct vnode *vp, struct uio *uio)
236 {
237         int error, nerror;
238         struct buf *bp, *nbp, *tbp;
239         caddr_t sa, nsa, tsa;
240         uint iolen;
241         int baseticks = ticks;
242         caddr_t udata;
243         int resid;
244         off_t offset;
245         struct thread *td;
246         
247         td = uio->uio_td ? uio->uio_td : curthread;
248         udata = uio->uio_iov->iov_base;
249         resid = uio->uio_resid;
250         offset = uio->uio_offset;
251
252         error = 0;
253         nerror = 0;
254         
255         bp = NULL;
256         nbp = NULL;
257         sa = NULL;
258         nsa = NULL;
259         
260         while (resid > 0) {
261                 
262                 if (bp == NULL) { /* Setup first read */
263                         /* XXX: Leave some bufs for swap */
264                         bp = getpbuf(&ffsrawbufcnt);
265                         sa = bp->b_data;
266                         bp->b_vp = vp; 
267                         error = ffs_rawread_readahead(vp, udata, offset, resid,
268                                     td, bp, sa, &baseticks);
269                         if (error != 0)
270                                 break;
271                         
272                         if (resid > bp->b_bufsize) { /* Setup fist readahead */
273                                 /* XXX: Leave bufs for swap */
274                                 if (rawreadahead != 0) 
275                                         nbp = trypbuf(&ffsrawbufcnt);
276                                 else
277                                         nbp = NULL;
278                                 if (nbp != NULL) {
279                                         nsa = nbp->b_data;
280                                         nbp->b_vp = vp;
281                                         
282                                         nerror = ffs_rawread_readahead(
283                                                         vp, 
284                                                         udata + bp->b_bufsize,
285                                                         offset + bp->b_bufsize,
286                                                         resid - bp->b_bufsize,
287                                                         td, nbp, nsa,
288                                                         &baseticks);
289                                         if (nerror) {
290                                                 relpbuf(nbp, &ffsrawbufcnt);
291                                                 nbp = NULL;
292                                         }
293                                 }
294                         }
295                 }
296                 
297                 crit_enter();
298                 while ((bp->b_flags & B_DONE) == 0) {
299                         tsleep((caddr_t)&bp->b_bio2, 0, "rawrd", 0);
300                 }
301                 crit_exit();
302                 
303                 vunmapbuf(bp);
304                 
305                 iolen = bp->b_bcount - bp->b_resid;
306                 if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) {
307                         nerror = 0;     /* Ignore possible beyond EOF error */
308                         break; /* EOF */
309                 }
310                 
311                 if ((bp->b_flags & B_ERROR) != 0) {
312                         error = bp->b_error;
313                         break;
314                 }
315                 clearbiocache(&bp->b_bio2);
316                 resid -= iolen;
317                 udata += iolen;
318                 offset += iolen;
319                 if (iolen < bp->b_bufsize) {
320                         /* Incomplete read.  Try to read remaining part */
321                         error = ffs_rawread_readahead(
322                                     vp, udata, offset,
323                                     bp->b_bufsize - iolen, td, bp,
324                                     sa, &baseticks);
325                         if (error != 0)
326                                 break;
327                 } else if (nbp != NULL) { /* Complete read with readahead */
328                         
329                         tbp = bp;
330                         bp = nbp;
331                         nbp = tbp;
332                         
333                         tsa = sa;
334                         sa = nsa;
335                         nsa = tsa;
336
337                         clearbiocache(&nbp->b_bio2);
338                         
339                         if (resid <= bp->b_bufsize) { /* No more readaheads */
340                                 relpbuf(nbp, &ffsrawbufcnt);
341                                 nbp = NULL;
342                         } else { /* Setup next readahead */
343                                 nerror = ffs_rawread_readahead(
344                                                 vp, udata + bp->b_bufsize,
345                                                 offset + bp->b_bufsize,
346                                                 resid - bp->b_bufsize,
347                                                 td, nbp, nsa, &baseticks);
348                                 if (nerror != 0) {
349                                         relpbuf(nbp, &ffsrawbufcnt);
350                                         nbp = NULL;
351                                 }
352                         }
353                 } else if (nerror != 0) {/* Deferred Readahead error */
354                         break;          
355                 }  else if (resid > 0) { /* More to read, no readahead */
356                         error = ffs_rawread_readahead(vp, udata, offset,
357                                                       resid, td, bp, sa,
358                                                       &baseticks);
359                         if (error != 0)
360                                 break;
361                 }
362         }
363         
364         if (bp != NULL)
365                 relpbuf(bp, &ffsrawbufcnt);
366         if (nbp != NULL) {                      /* Run down readahead buffer */
367                 crit_enter();
368                 while ((nbp->b_flags & B_DONE) == 0) {
369                         tsleep(&nbp->b_bio2, 0, "rawrd", 0);
370                 }
371                 crit_exit();
372                 vunmapbuf(nbp);
373                 relpbuf(nbp, &ffsrawbufcnt);
374         }
375         
376         if (error == 0)
377                 error = nerror;
378         uio->uio_iov->iov_base = udata;
379         uio->uio_resid = resid;
380         uio->uio_offset = offset;
381         return error;
382 }
383
384
385 int
386 ffs_rawread(struct vnode *vp,
387             struct uio *uio,
388             int *workdone)
389 {
390         if (allowrawread != 0 &&
391             uio->uio_iovcnt == 1 && 
392             uio->uio_segflg == UIO_USERSPACE &&
393             uio->uio_resid == uio->uio_iov->iov_len &&
394             (((uio->uio_td != NULL) ? uio->uio_td : curthread)->td_flags &
395              TDF_DEADLKTREAT) == 0) {
396                 int secsize;            /* Media sector size */
397                 off_t filebytes;        /* Bytes left of file */
398                 int blockbytes;         /* Bytes left of file in full blocks */
399                 int partialbytes;       /* Bytes in last partial block */
400                 int skipbytes;          /* Bytes not to read in ffs_rawread */
401                 struct inode *ip;
402                 int error;
403                 
404
405                 /* Only handle sector aligned reads */
406                 ip = VTOI(vp);
407                 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
408                 if ((uio->uio_offset & (secsize - 1)) == 0 &&
409                     (uio->uio_resid & (secsize - 1)) == 0) {
410                         
411                         /* Sync dirty pages and buffers if needed */
412                         error = ffs_rawread_sync(vp,
413                                                  (uio->uio_td != NULL) ?
414                                                  uio->uio_td : curthread);
415                         if (error != 0)
416                                 return error;
417                         
418                         /* Check for end of file */
419                         if (ip->i_size > uio->uio_offset) {
420                                 filebytes = ip->i_size - uio->uio_offset;
421
422                                 /* No special eof handling needed ? */
423                                 if (uio->uio_resid <= filebytes) {
424                                         *workdone = 1;
425                                         return ffs_rawread_main(vp, uio);
426                                 }
427                                 
428                                 partialbytes = ((unsigned int) ip->i_size) %
429                                         ip->i_fs->fs_bsize;
430                                 blockbytes = (int) filebytes - partialbytes;
431                                 if (blockbytes > 0) {
432                                         skipbytes = uio->uio_resid -
433                                                 blockbytes;
434                                         uio->uio_resid = blockbytes;
435                                         error = ffs_rawread_main(vp, uio);
436                                         uio->uio_resid += skipbytes;
437                                         if (error != 0)
438                                                 return error;
439                                         /* Read remaining part using buffer */
440                                 }
441                         }
442                 }
443         }
444         *workdone = 0;
445         return 0;
446 }
447
448
449 static void
450 ffs_rawreadwakeup(struct bio *bio)
451 {
452         wakeup(bio);
453 }