Merge from vendor branch SENDMAIL:
[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.14 2006/02/17 19:18:08 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         blockno = loffset / bsize;
180         blockoff = (loffset % bsize) / DEV_BSIZE;
181         if ((daddr_t)blockno != blockno) {
182                 return EINVAL; /* blockno overflow */
183         }
184         bp->b_lblkno = blockno;
185         bp->b_bio2.bio_offset = NOOFFSET;
186         bp->b_bio2.bio_blkno = (daddr_t)-1;
187         bp->b_bio2.bio_done = ffs_rawreadwakeup;
188
189         error = VOP_BMAP(vp, bp->b_lblkno, &dp, &bp->b_bio2.bio_blkno,
190                          &bforwards, NULL);
191         if (error != 0) {
192                 return error;
193         }
194         if (bp->b_bio2.bio_blkno == (daddr_t)-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_bio2.bio_blkno += blockoff;
222         
223         if (vmapbuf(bp) < 0)
224                 return EFAULT;
225         
226         /*
227          * Access the block device layer using the device vnode (dp) and
228          * the translated block number (bio2) instead of the logical block
229          * number (bio1).
230          *
231          * Even though we are bypassing the vnode layer, we still
232          * want the vnode state to indicate that an I/O on its behalf
233          * is in progress.
234          */
235         bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
236         vn_strategy(dp, &bp->b_bio2);
237         return 0;
238 }
239
240
241 static int
242 ffs_rawread_main(struct vnode *vp, struct uio *uio)
243 {
244         int error, nerror;
245         struct buf *bp, *nbp, *tbp;
246         caddr_t sa, nsa, tsa;
247         uint iolen;
248         int baseticks = ticks;
249         caddr_t udata;
250         long resid;
251         off_t offset;
252         struct thread *td;
253         
254         td = uio->uio_td ? uio->uio_td : curthread;
255         udata = uio->uio_iov->iov_base;
256         resid = uio->uio_resid;
257         offset = uio->uio_offset;
258
259         error = 0;
260         nerror = 0;
261         
262         bp = NULL;
263         nbp = NULL;
264         sa = NULL;
265         nsa = NULL;
266         
267         while (resid > 0) {
268                 
269                 if (bp == NULL) { /* Setup first read */
270                         /* XXX: Leave some bufs for swap */
271                         bp = getpbuf(&ffsrawbufcnt);
272                         sa = bp->b_data;
273                         bp->b_vp = vp; 
274                         error = ffs_rawread_readahead(vp, udata, offset, resid,
275                                     td, bp, sa, &baseticks);
276                         if (error != 0)
277                                 break;
278                         
279                         if (resid > bp->b_bufsize) { /* Setup fist readahead */
280                                 /* XXX: Leave bufs for swap */
281                                 if (rawreadahead != 0) 
282                                         nbp = trypbuf(&ffsrawbufcnt);
283                                 else
284                                         nbp = NULL;
285                                 if (nbp != NULL) {
286                                         nsa = nbp->b_data;
287                                         nbp->b_vp = vp;
288                                         
289                                         nerror = ffs_rawread_readahead(
290                                                         vp, 
291                                                         udata + bp->b_bufsize,
292                                                         offset + bp->b_bufsize,
293                                                         resid - bp->b_bufsize,
294                                                         td, nbp, nsa,
295                                                         &baseticks);
296                                         if (nerror) {
297                                                 relpbuf(nbp, &ffsrawbufcnt);
298                                                 nbp = NULL;
299                                         }
300                                 }
301                         }
302                 }
303                 
304                 crit_enter();
305                 while ((bp->b_flags & B_DONE) == 0) {
306                         tsleep((caddr_t)&bp->b_bio2, 0, "rawrd", 0);
307                 }
308                 crit_exit();
309                 
310                 vunmapbuf(bp);
311                 
312                 iolen = bp->b_bcount - bp->b_resid;
313                 if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) {
314                         nerror = 0;     /* Ignore possible beyond EOF error */
315                         break; /* EOF */
316                 }
317                 
318                 if ((bp->b_flags & B_ERROR) != 0) {
319                         error = bp->b_error;
320                         break;
321                 }
322                 clearbiocache(&bp->b_bio2);
323                 resid -= iolen;
324                 udata += iolen;
325                 offset += iolen;
326                 if (iolen < bp->b_bufsize) {
327                         /* Incomplete read.  Try to read remaining part */
328                         error = ffs_rawread_readahead(
329                                     vp, udata, offset,
330                                     bp->b_bufsize - iolen, td, bp,
331                                     sa, &baseticks);
332                         if (error != 0)
333                                 break;
334                 } else if (nbp != NULL) { /* Complete read with readahead */
335                         
336                         tbp = bp;
337                         bp = nbp;
338                         nbp = tbp;
339                         
340                         tsa = sa;
341                         sa = nsa;
342                         nsa = tsa;
343
344                         clearbiocache(&nbp->b_bio2);
345                         
346                         if (resid <= bp->b_bufsize) { /* No more readaheads */
347                                 relpbuf(nbp, &ffsrawbufcnt);
348                                 nbp = NULL;
349                         } else { /* Setup next readahead */
350                                 nerror = ffs_rawread_readahead(
351                                                 vp, udata + bp->b_bufsize,
352                                                 offset + bp->b_bufsize,
353                                                 resid - bp->b_bufsize,
354                                                 td, nbp, nsa, &baseticks);
355                                 if (nerror != 0) {
356                                         relpbuf(nbp, &ffsrawbufcnt);
357                                         nbp = NULL;
358                                 }
359                         }
360                 } else if (nerror != 0) {/* Deferred Readahead error */
361                         break;          
362                 }  else if (resid > 0) { /* More to read, no readahead */
363                         error = ffs_rawread_readahead(vp, udata, offset,
364                                                       resid, td, bp, sa,
365                                                       &baseticks);
366                         if (error != 0)
367                                 break;
368                 }
369         }
370         
371         if (bp != NULL)
372                 relpbuf(bp, &ffsrawbufcnt);
373         if (nbp != NULL) {                      /* Run down readahead buffer */
374                 crit_enter();
375                 while ((nbp->b_flags & B_DONE) == 0) {
376                         tsleep(&nbp->b_bio2, 0, "rawrd", 0);
377                 }
378                 crit_exit();
379                 vunmapbuf(nbp);
380                 relpbuf(nbp, &ffsrawbufcnt);
381         }
382         
383         if (error == 0)
384                 error = nerror;
385         uio->uio_iov->iov_base = udata;
386         uio->uio_resid = resid;
387         uio->uio_offset = offset;
388         return error;
389 }
390
391
392 int
393 ffs_rawread(struct vnode *vp,
394             struct uio *uio,
395             int *workdone)
396 {
397         if (allowrawread != 0 &&
398             uio->uio_iovcnt == 1 && 
399             uio->uio_segflg == UIO_USERSPACE &&
400             uio->uio_resid == uio->uio_iov->iov_len &&
401             (((uio->uio_td != NULL) ? uio->uio_td : curthread)->td_flags &
402              TDF_DEADLKTREAT) == 0) {
403                 int secsize;            /* Media sector size */
404                 off_t filebytes;        /* Bytes left of file */
405                 int blockbytes;         /* Bytes left of file in full blocks */
406                 int partialbytes;       /* Bytes in last partial block */
407                 int skipbytes;          /* Bytes not to read in ffs_rawread */
408                 struct inode *ip;
409                 int error;
410                 
411
412                 /* Only handle sector aligned reads */
413                 ip = VTOI(vp);
414                 secsize = ip->i_devvp->v_rdev->si_bsize_phys;
415                 if ((uio->uio_offset & (secsize - 1)) == 0 &&
416                     (uio->uio_resid & (secsize - 1)) == 0) {
417                         
418                         /* Sync dirty pages and buffers if needed */
419                         error = ffs_rawread_sync(vp,
420                                                  (uio->uio_td != NULL) ?
421                                                  uio->uio_td : curthread);
422                         if (error != 0)
423                                 return error;
424                         
425                         /* Check for end of file */
426                         if (ip->i_size > uio->uio_offset) {
427                                 filebytes = ip->i_size - uio->uio_offset;
428
429                                 /* No special eof handling needed ? */
430                                 if (uio->uio_resid <= filebytes) {
431                                         *workdone = 1;
432                                         return ffs_rawread_main(vp, uio);
433                                 }
434                                 
435                                 partialbytes = ((unsigned int) ip->i_size) %
436                                         ip->i_fs->fs_bsize;
437                                 blockbytes = (int) filebytes - partialbytes;
438                                 if (blockbytes > 0) {
439                                         skipbytes = uio->uio_resid -
440                                                 blockbytes;
441                                         uio->uio_resid = blockbytes;
442                                         error = ffs_rawread_main(vp, uio);
443                                         uio->uio_resid += skipbytes;
444                                         if (error != 0)
445                                                 return error;
446                                         /* Read remaining part using buffer */
447                                 }
448                         }
449                 }
450         }
451         *workdone = 0;
452         return 0;
453 }
454
455
456 static void
457 ffs_rawreadwakeup(struct bio *bio)
458 {
459         wakeup(bio);
460 }