Initial import from FreeBSD RELENG_4:
[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  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/fcntl.h>
32 #include <sys/file.h>
33 #include <sys/stat.h>
34 #include <sys/proc.h>
35 #include <sys/mount.h>
36 #include <sys/namei.h>
37 #include <sys/vnode.h>
38 #include <sys/conf.h>
39 #include <sys/filio.h>
40 #include <sys/ttycom.h>
41 #include <sys/buf.h>
42 #include <ufs/ufs/quota.h>
43 #include <ufs/ufs/inode.h>
44 #include <ufs/ffs/fs.h>
45
46 #include <machine/limits.h>
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_object.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52
53 static int ffs_rawread_readahead(struct vnode *vp,
54                                  caddr_t udata,
55                                  off_t offset,
56                                  size_t len,
57                                  struct proc *p,
58                                  struct buf *bp,
59                                  caddr_t sa);
60 static int ffs_rawread_main(struct vnode *vp,
61                             struct uio *uio);
62
63 static int ffs_rawread_sync(struct vnode *vp, struct proc *p);
64
65 int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
66
67 void ffs_rawread_setup(void);
68
69 static void ffs_rawreadwakeup(struct buf *bp);
70
71
72 SYSCTL_DECL(_vfs_ffs);
73
74 static int ffsrawbufcnt = 4;
75 SYSCTL_INT(_vfs_ffs, OID_AUTO, ffsrawbufcnt, CTLFLAG_RD, &ffsrawbufcnt, 0,
76            "Buffers available for raw reads");
77
78 static int allowrawread = 1;
79 SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0,
80            "Flag to enable raw reads");
81
82 static int rawreadahead = 1;
83 SYSCTL_INT(_vfs_ffs, OID_AUTO, rawreadahead, CTLFLAG_RW, &rawreadahead, 0,
84            "Flag to enable readahead for long raw reads");
85
86
87 void
88 ffs_rawread_setup(void)
89 {
90         ffsrawbufcnt = (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8;
91 }
92
93
94 static int
95 ffs_rawread_sync(struct vnode *vp, struct proc *p)
96 {
97         int spl;
98         int error;
99         int upgraded;
100
101         /* Check for dirty mmap, pending writes and dirty buffers */
102         spl = splbio();
103         if (vp->v_numoutput > 0 ||
104             !TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
105             (vp->v_flag & VOBJDIRTY) != 0) {
106                 splx(spl);
107
108                 if (VOP_ISLOCKED(vp, p) != LK_EXCLUSIVE) {
109                         upgraded = 1;
110                         /* Upgrade to exclusive lock, this might block */
111                         VOP_LOCK(vp, LK_UPGRADE | LK_NOPAUSE, p);
112                 } else
113                         upgraded = 0;
114                 
115                 /* Attempt to msync mmap() regions to clean dirty mmap */ 
116                 if ((vp->v_flag & VOBJDIRTY) != 0) {
117                         struct vm_object *obj;
118                         if (VOP_GETVOBJECT(vp, &obj) == 0)
119                                 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
120                 }
121
122                 /* Wait for pending writes to complete */
123                 spl = splbio();
124                 while (vp->v_numoutput) {
125                         vp->v_flag |= VBWAIT;
126                         error = tsleep((caddr_t)&vp->v_numoutput,
127                                        PRIBIO + 1,
128                                        "rawrdfls", 0);
129                         if (error != 0) {
130                                 splx(spl);
131                                 if (upgraded != 0)
132                                         VOP_LOCK(vp, LK_DOWNGRADE, p);
133                                 return (error);
134                         }
135                 }
136                 /* Flush dirty buffers */
137                 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
138                         splx(spl);
139                         if ((error = VOP_FSYNC(vp, NOCRED, MNT_WAIT, p)) != 0) {
140                                 if (upgraded != 0)
141                                         VOP_LOCK(vp, LK_DOWNGRADE, p);
142                                 return (error);
143                         }
144                         spl = splbio();
145                         if (vp->v_numoutput > 0 ||
146                             !TAILQ_EMPTY(&vp->v_dirtyblkhd))
147                                 panic("ffs_rawread_sync: dirty bufs");
148                 }
149                 splx(spl);
150                 if (upgraded != 0)
151                         VOP_LOCK(vp, LK_DOWNGRADE, p);
152         } else {
153                 splx(spl);
154         }
155         return 0;
156 }
157
158
159 static int
160 ffs_rawread_readahead(struct vnode *vp,
161                       caddr_t udata,
162                       off_t offset,
163                       size_t len,
164                       struct proc *p,
165                       struct buf *bp,
166                       caddr_t sa)
167 {
168         int error;
169         u_int iolen;
170         off_t blockno;
171         int blockoff;
172         int bsize;
173         struct vnode *dp;
174         int bforwards;
175         
176         bsize = vp->v_mount->mnt_stat.f_iosize;
177         
178         iolen = ((vm_offset_t) udata) & PAGE_MASK;
179         bp->b_bcount = len;
180         if (bp->b_bcount + iolen > bp->b_kvasize) {
181                 bp->b_bcount = bp->b_kvasize;
182                 if (iolen != 0)
183                         bp->b_bcount -= PAGE_SIZE;
184         }
185         bp->b_flags = B_PHYS | B_CALL | B_READ;
186         bp->b_iodone = ffs_rawreadwakeup;
187         bp->b_data = udata;
188         bp->b_saveaddr = sa;
189         bp->b_offset = offset;
190         blockno = bp->b_offset / bsize;
191         blockoff = (bp->b_offset % bsize) / DEV_BSIZE;
192         if ((daddr_t) blockno != blockno) {
193                 return EINVAL; /* blockno overflow */
194         }
195         
196         bp->b_lblkno = bp->b_blkno = blockno;
197         
198         error = VOP_BMAP(vp, bp->b_lblkno, &dp, &bp->b_blkno, &bforwards,
199                          NULL);
200         if (error != 0) {
201                 return error;
202         }
203         if (bp->b_blkno == -1) {
204
205                 /* Fill holes with NULs to preserve semantics */
206                 
207                 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize)
208                         bp->b_bcount = bsize - blockoff * DEV_BSIZE;
209                 bp->b_bufsize = bp->b_bcount;
210                 
211                 if (vmapbuf(bp) < 0)
212                         return EFAULT;
213                 
214                 if (ticks - switchticks >= hogticks)
215                         uio_yield();
216                 bzero(bp->b_data, bp->b_bufsize);
217
218                 /* Mark operation completed (similar to bufdone()) */
219
220                 bp->b_resid = 0;
221                 bp->b_flags |= B_DONE;
222                 return 0;
223         }
224         
225         if (bp->b_bcount + blockoff * DEV_BSIZE > bsize * (1 + bforwards))
226                 bp->b_bcount = bsize * (1 + bforwards) - blockoff * DEV_BSIZE;
227         bp->b_bufsize = bp->b_bcount;
228         bp->b_blkno += blockoff;
229         bp->b_dev = dp->v_rdev;
230         
231         if (vmapbuf(bp) < 0)
232                 return EFAULT;
233         
234         (void) VOP_STRATEGY(dp, bp);
235         return 0;
236 }
237
238
239 static int
240 ffs_rawread_main(struct vnode *vp,
241                  struct uio *uio)
242 {
243         int error, nerror;
244         struct buf *bp, *nbp, *tbp;
245         caddr_t sa, nsa, tsa;
246         u_int iolen;
247         int spl;
248         caddr_t udata;
249         long resid;
250         off_t offset;
251         struct proc *p;
252         
253         p = uio->uio_procp ? uio->uio_procp : curproc;
254         udata = uio->uio_iov->iov_base;
255         resid = uio->uio_resid;
256         offset = uio->uio_offset;
257
258         /*
259          * keep the process from being swapped
260          */
261         PHOLD(p);
262         
263         error = 0;
264         nerror = 0;
265         
266         bp = NULL;
267         nbp = NULL;
268         sa = NULL;
269         nsa = NULL;
270         
271         while (resid > 0) {
272                 
273                 if (bp == NULL) { /* Setup first read */
274                         /* XXX: Leave some bufs for swap */
275                         bp = getpbuf(&ffsrawbufcnt);
276                         sa = bp->b_data;
277                         bp->b_vp = vp; 
278                         error = ffs_rawread_readahead(vp, udata, offset,
279                                                      resid, p, bp, sa);
280                         if (error != 0)
281                                 break;
282                         
283                         if (resid > bp->b_bufsize) { /* Setup fist readahead */
284                                 /* XXX: Leave bufs for swap */
285                                 if (rawreadahead != 0) 
286                                         nbp = trypbuf(&ffsrawbufcnt);
287                                 else
288                                         nbp = NULL;
289                                 if (nbp != NULL) {
290                                         nsa = nbp->b_data;
291                                         nbp->b_vp = vp;
292                                         
293                                         nerror = ffs_rawread_readahead(vp, 
294                                                                        udata +
295                                                                        bp->b_bufsize,
296                                                                        offset +
297                                                                        bp->b_bufsize,
298                                                                        resid -
299                                                                        bp->b_bufsize,
300                                                                        p,
301                                                                        nbp,
302                                                                        nsa);
303                                         if (nerror) {
304                                                 relpbuf(nbp, &ffsrawbufcnt);
305                                                 nbp = NULL;
306                                         }
307                                 }
308                         }
309                 }
310                 
311                 spl = splbio();
312                 while ((bp->b_flags & B_DONE) == 0) {
313                         tsleep((caddr_t)bp, PRIBIO, "rawrd", 0);
314                 }
315                 splx(spl);
316                 
317                 vunmapbuf(bp);
318                 
319                 iolen = bp->b_bcount - bp->b_resid;
320                 if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) {
321                         nerror = 0;     /* Ignore possible beyond EOF error */
322                         break; /* EOF */
323                 }
324                 
325                 if ((bp->b_flags & B_ERROR) != 0) {
326                         error = bp->b_error;
327                         break;
328                 }
329                 resid -= iolen;
330                 udata += iolen;
331                 offset += iolen;
332                 if (iolen < bp->b_bufsize) {
333                         /* Incomplete read.  Try to read remaining part */
334                         error = ffs_rawread_readahead(vp,
335                                                       udata,
336                                                       offset,
337                                                       bp->b_bufsize - iolen,
338                                                       p,
339                                                       bp,
340                                                       sa);
341                         if (error != 0)
342                                 break;
343                 } else if (nbp != NULL) { /* Complete read with readahead */
344                         
345                         tbp = bp;
346                         bp = nbp;
347                         nbp = tbp;
348                         
349                         tsa = sa;
350                         sa = nsa;
351                         nsa = tsa;
352                         
353                         if (resid <= bp->b_bufsize) { /* No more readaheads */
354                                 relpbuf(nbp, &ffsrawbufcnt);
355                                 nbp = NULL;
356                         } else { /* Setup next readahead */
357                                 nerror = ffs_rawread_readahead(vp,
358                                                                udata +
359                                                                bp->b_bufsize,
360                                                                offset +
361                                                                bp->b_bufsize,
362                                                                resid -
363                                                                bp->b_bufsize,
364                                                                p,
365                                                                nbp,
366                                                                nsa);
367                                 if (nerror != 0) {
368                                         relpbuf(nbp, &ffsrawbufcnt);
369                                         nbp = NULL;
370                                 }
371                         }
372                 } else if (nerror != 0) {/* Deferred Readahead error */
373                         break;          
374                 }  else if (resid > 0) { /* More to read, no readahead */
375                         error = ffs_rawread_readahead(vp, udata, offset,
376                                                       resid, p, bp, sa);
377                         if (error != 0)
378                                 break;
379                 }
380         }
381         
382         if (bp != NULL)
383                 relpbuf(bp, &ffsrawbufcnt);
384         if (nbp != NULL) {                      /* Run down readahead buffer */
385                 spl = splbio();
386                 while ((nbp->b_flags & B_DONE) == 0) {
387                         tsleep((caddr_t)nbp, PRIBIO, "rawrd", 0);
388                 }
389                 splx(spl);
390                 vunmapbuf(nbp);
391                 relpbuf(nbp, &ffsrawbufcnt);
392         }
393         
394         if (error == 0)
395                 error = nerror;
396         PRELE(p);
397         uio->uio_iov->iov_base = udata;
398         uio->uio_resid = resid;
399         uio->uio_offset = offset;
400         return error;
401 }
402
403
404 int
405 ffs_rawread(struct vnode *vp,
406             struct uio *uio,
407             int *workdone)
408 {
409         if (allowrawread != 0 &&
410             uio->uio_iovcnt == 1 && 
411             uio->uio_segflg == UIO_USERSPACE &&
412             uio->uio_resid == uio->uio_iov->iov_len &&
413             (((uio->uio_procp != NULL) ? uio->uio_procp : curproc)->p_flag &
414              P_DEADLKTREAT) == 0) {
415                 int secsize;            /* Media sector size */
416                 off_t filebytes;        /* Bytes left of file */
417                 int blockbytes;         /* Bytes left of file in full blocks */
418                 int partialbytes;       /* Bytes in last partial block */
419                 int skipbytes;          /* Bytes not to read in ffs_rawread */
420                 struct inode *ip;
421                 int error;
422                 
423
424                 /* Only handle sector aligned reads */
425                 ip = VTOI(vp);
426                 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
427                 if ((uio->uio_offset & (secsize - 1)) == 0 &&
428                     (uio->uio_resid & (secsize - 1)) == 0) {
429                         
430                         /* Sync dirty pages and buffers if needed */
431                         error = ffs_rawread_sync(vp,
432                                                  (uio->uio_procp != NULL) ?
433                                                  uio->uio_procp : curproc);
434                         if (error != 0)
435                                 return error;
436                         
437                         /* Check for end of file */
438                         if (ip->i_size > uio->uio_offset) {
439                                 filebytes = ip->i_size - uio->uio_offset;
440
441                                 /* No special eof handling needed ? */
442                                 if (uio->uio_resid <= filebytes) {
443                                         *workdone = 1;
444                                         return ffs_rawread_main(vp, uio);
445                                 }
446                                 
447                                 partialbytes = ((unsigned int) ip->i_size) %
448                                         ip->i_fs->fs_bsize;
449                                 blockbytes = (int) filebytes - partialbytes;
450                                 if (blockbytes > 0) {
451                                         skipbytes = uio->uio_resid -
452                                                 blockbytes;
453                                         uio->uio_resid = blockbytes;
454                                         error = ffs_rawread_main(vp, uio);
455                                         uio->uio_resid += skipbytes;
456                                         if (error != 0)
457                                                 return error;
458                                         /* Read remaining part using buffer */
459                                 }
460                         }
461                 }
462         }
463         *workdone = 0;
464         return 0;
465 }
466
467
468 static void
469 ffs_rawreadwakeup(struct buf *bp)
470 {
471         wakeup((caddr_t) bp);
472 }