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