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