BUF/BIO cleanup 3/99:
[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.12 2005/08/03 16:36:33 hmp 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 buf *bp);
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_numoutput > 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_numoutput) {
121                         vp->v_flag |= VBWAIT;
122                         error = tsleep((caddr_t)&vp->v_numoutput, 0,
123                                        "rawrdfls", 0);
124                         if (error != 0) {
125                                 crit_exit();
126                                 if (upgraded != 0)
127                                         VOP_LOCK(vp, LK_DOWNGRADE, td);
128                                 return (error);
129                         }
130                 }
131                 /* Flush dirty buffers */
132                 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
133                         crit_exit();
134                         if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0) {
135                                 if (upgraded != 0)
136                                         VOP_LOCK(vp, LK_DOWNGRADE, td);
137                                 return (error);
138                         }
139                         crit_enter();
140                         if (vp->v_numoutput > 0 ||
141                             !RB_EMPTY(&vp->v_rbdirty_tree))
142                                 panic("ffs_rawread_sync: dirty bufs");
143                 }
144                 crit_exit();
145                 if (upgraded != 0)
146                         VOP_LOCK(vp, LK_DOWNGRADE, td);
147         } else {
148                 crit_exit();
149         }
150         return 0;
151 }
152
153
154 static int
155 ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t offset,
156                       size_t len, struct thread *td, struct buf *bp,
157                       caddr_t sa, int *baseticks)
158 {
159         int error;
160         uint iolen;
161         off_t blockno;
162         int blockoff;
163         int bsize;
164         struct vnode *dp;
165         int bforwards;
166         
167         bsize = vp->v_mount->mnt_stat.f_iosize;
168         
169         iolen = ((vm_offset_t) udata) & PAGE_MASK;
170         bp->b_bcount = len;
171         if (bp->b_bcount + iolen > bp->b_kvasize) {
172                 bp->b_bcount = bp->b_kvasize;
173                 if (iolen != 0)
174                         bp->b_bcount -= PAGE_SIZE;
175         }
176         bp->b_flags = B_PHYS | B_READ;
177         bp->b_iodone = ffs_rawreadwakeup;
178         bp->b_data = udata;
179         bp->b_saveaddr = sa;
180         bp->b_offset = offset;
181         blockno = bp->b_offset / bsize;
182         blockoff = (bp->b_offset % bsize) / DEV_BSIZE;
183         if ((daddr_t) blockno != blockno) {
184                 return EINVAL; /* blockno overflow */
185         }
186         
187         bp->b_lblkno = bp->b_blkno = blockno;
188         
189         error = VOP_BMAP(vp, bp->b_lblkno, &dp, &bp->b_blkno, &bforwards,
190                          NULL);
191         if (error != 0) {
192                 return error;
193         }
194         if (bp->b_blkno == -1) {
195
196                 /* Fill holes with NULs to preserve semantics */
197                 
198                 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize)
199                         bp->b_bcount = bsize - blockoff * DEV_BSIZE;
200                 bp->b_bufsize = bp->b_bcount;
201                 
202                 if (vmapbuf(bp) < 0)
203                         return EFAULT;
204                 
205                 if (ticks - *baseticks >= hogticks) {
206                         *baseticks = ticks;
207                         uio_yield();
208                 }
209                 bzero(bp->b_data, bp->b_bufsize);
210
211                 /* Mark operation completed (similar to bufdone()) */
212
213                 bp->b_resid = 0;
214                 bp->b_flags |= B_DONE;
215                 return 0;
216         }
217         
218         if (bp->b_bcount + blockoff * DEV_BSIZE > bsize * (1 + bforwards))
219                 bp->b_bcount = bsize * (1 + bforwards) - blockoff * DEV_BSIZE;
220         bp->b_bufsize = bp->b_bcount;
221         bp->b_blkno += blockoff;
222         bp->b_dev = dp->v_rdev;
223         
224         if (vmapbuf(bp) < 0)
225                 return EFAULT;
226         
227         (void) VOP_STRATEGY(dp, bp);
228         return 0;
229 }
230
231
232 static int
233 ffs_rawread_main(struct vnode *vp, struct uio *uio)
234 {
235         int error, nerror;
236         struct buf *bp, *nbp, *tbp;
237         caddr_t sa, nsa, tsa;
238         uint iolen;
239         int baseticks = ticks;
240         caddr_t udata;
241         long resid;
242         off_t offset;
243         struct thread *td;
244         
245         td = uio->uio_td ? uio->uio_td : curthread;
246         udata = uio->uio_iov->iov_base;
247         resid = uio->uio_resid;
248         offset = uio->uio_offset;
249
250         /*
251          * keep the process from being swapped
252          */
253         if (uio->uio_td && uio->uio_td->td_proc)
254                 PHOLD(uio->uio_td->td_proc);
255         
256         error = 0;
257         nerror = 0;
258         
259         bp = NULL;
260         nbp = NULL;
261         sa = NULL;
262         nsa = NULL;
263         
264         while (resid > 0) {
265                 
266                 if (bp == NULL) { /* Setup first read */
267                         /* XXX: Leave some bufs for swap */
268                         bp = getpbuf(&ffsrawbufcnt);
269                         sa = bp->b_data;
270                         bp->b_vp = vp; 
271                         error = ffs_rawread_readahead(vp, udata, offset, resid,
272                                     td, bp, sa, &baseticks);
273                         if (error != 0)
274                                 break;
275                         
276                         if (resid > bp->b_bufsize) { /* Setup fist readahead */
277                                 /* XXX: Leave bufs for swap */
278                                 if (rawreadahead != 0) 
279                                         nbp = trypbuf(&ffsrawbufcnt);
280                                 else
281                                         nbp = NULL;
282                                 if (nbp != NULL) {
283                                         nsa = nbp->b_data;
284                                         nbp->b_vp = vp;
285                                         
286                                         nerror = ffs_rawread_readahead(
287                                                         vp, 
288                                                         udata + bp->b_bufsize,
289                                                         offset + bp->b_bufsize,
290                                                         resid - bp->b_bufsize,
291                                                         td, nbp, nsa,
292                                                         &baseticks);
293                                         if (nerror) {
294                                                 relpbuf(nbp, &ffsrawbufcnt);
295                                                 nbp = NULL;
296                                         }
297                                 }
298                         }
299                 }
300                 
301                 crit_enter();
302                 while ((bp->b_flags & B_DONE) == 0) {
303                         tsleep((caddr_t)bp, 0, "rawrd", 0);
304                 }
305                 crit_exit();
306                 
307                 vunmapbuf(bp);
308                 
309                 iolen = bp->b_bcount - bp->b_resid;
310                 if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) {
311                         nerror = 0;     /* Ignore possible beyond EOF error */
312                         break; /* EOF */
313                 }
314                 
315                 if ((bp->b_flags & B_ERROR) != 0) {
316                         error = bp->b_error;
317                         break;
318                 }
319                 resid -= iolen;
320                 udata += iolen;
321                 offset += iolen;
322                 if (iolen < bp->b_bufsize) {
323                         /* Incomplete read.  Try to read remaining part */
324                         error = ffs_rawread_readahead(
325                                     vp, udata, offset,
326                                     bp->b_bufsize - iolen, td, bp,
327                                     sa, &baseticks);
328                         if (error != 0)
329                                 break;
330                 } else if (nbp != NULL) { /* Complete read with readahead */
331                         
332                         tbp = bp;
333                         bp = nbp;
334                         nbp = tbp;
335                         
336                         tsa = sa;
337                         sa = nsa;
338                         nsa = tsa;
339                         
340                         if (resid <= bp->b_bufsize) { /* No more readaheads */
341                                 relpbuf(nbp, &ffsrawbufcnt);
342                                 nbp = NULL;
343                         } else { /* Setup next readahead */
344                                 nerror = ffs_rawread_readahead(
345                                                 vp, udata + bp->b_bufsize,
346                                                 offset + bp->b_bufsize,
347                                                 resid - bp->b_bufsize,
348                                                 td, nbp, nsa, &baseticks);
349                                 if (nerror != 0) {
350                                         relpbuf(nbp, &ffsrawbufcnt);
351                                         nbp = NULL;
352                                 }
353                         }
354                 } else if (nerror != 0) {/* Deferred Readahead error */
355                         break;          
356                 }  else if (resid > 0) { /* More to read, no readahead */
357                         error = ffs_rawread_readahead(vp, udata, offset,
358                                                       resid, td, bp, sa,
359                                                       &baseticks);
360                         if (error != 0)
361                                 break;
362                 }
363         }
364         
365         if (bp != NULL)
366                 relpbuf(bp, &ffsrawbufcnt);
367         if (nbp != NULL) {                      /* Run down readahead buffer */
368                 crit_enter();
369                 while ((nbp->b_flags & B_DONE) == 0) {
370                         tsleep((caddr_t)nbp, 0, "rawrd", 0);
371                 }
372                 crit_exit();
373                 vunmapbuf(nbp);
374                 relpbuf(nbp, &ffsrawbufcnt);
375         }
376         
377         if (error == 0)
378                 error = nerror;
379         if (uio->uio_td && uio->uio_td->td_proc)
380                 PRELE(uio->uio_td->td_proc);
381         uio->uio_iov->iov_base = udata;
382         uio->uio_resid = resid;
383         uio->uio_offset = offset;
384         return error;
385 }
386
387
388 int
389 ffs_rawread(struct vnode *vp,
390             struct uio *uio,
391             int *workdone)
392 {
393         if (allowrawread != 0 &&
394             uio->uio_iovcnt == 1 && 
395             uio->uio_segflg == UIO_USERSPACE &&
396             uio->uio_resid == uio->uio_iov->iov_len &&
397             (((uio->uio_td != NULL) ? uio->uio_td : curthread)->td_flags &
398              TDF_DEADLKTREAT) == 0) {
399                 int secsize;            /* Media sector size */
400                 off_t filebytes;        /* Bytes left of file */
401                 int blockbytes;         /* Bytes left of file in full blocks */
402                 int partialbytes;       /* Bytes in last partial block */
403                 int skipbytes;          /* Bytes not to read in ffs_rawread */
404                 struct inode *ip;
405                 int error;
406                 
407
408                 /* Only handle sector aligned reads */
409                 ip = VTOI(vp);
410                 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
411                 if ((uio->uio_offset & (secsize - 1)) == 0 &&
412                     (uio->uio_resid & (secsize - 1)) == 0) {
413                         
414                         /* Sync dirty pages and buffers if needed */
415                         error = ffs_rawread_sync(vp,
416                                                  (uio->uio_td != NULL) ?
417                                                  uio->uio_td : curthread);
418                         if (error != 0)
419                                 return error;
420                         
421                         /* Check for end of file */
422                         if (ip->i_size > uio->uio_offset) {
423                                 filebytes = ip->i_size - uio->uio_offset;
424
425                                 /* No special eof handling needed ? */
426                                 if (uio->uio_resid <= filebytes) {
427                                         *workdone = 1;
428                                         return ffs_rawread_main(vp, uio);
429                                 }
430                                 
431                                 partialbytes = ((unsigned int) ip->i_size) %
432                                         ip->i_fs->fs_bsize;
433                                 blockbytes = (int) filebytes - partialbytes;
434                                 if (blockbytes > 0) {
435                                         skipbytes = uio->uio_resid -
436                                                 blockbytes;
437                                         uio->uio_resid = blockbytes;
438                                         error = ffs_rawread_main(vp, uio);
439                                         uio->uio_resid += skipbytes;
440                                         if (error != 0)
441                                                 return error;
442                                         /* Read remaining part using buffer */
443                                 }
444                         }
445                 }
446         }
447         *workdone = 0;
448         return 0;
449 }
450
451
452 static void
453 ffs_rawreadwakeup(struct buf *bp)
454 {
455         wakeup((caddr_t) bp);
456 }