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