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