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