2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000-2003 Tor Egge
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/systm.h>
33 #include <sys/fcntl.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/mount.h>
37 #include <sys/namei.h>
39 #include <sys/rwlock.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
44 #include <ufs/ufs/extattr.h>
45 #include <ufs/ufs/quota.h>
46 #include <ufs/ufs/inode.h>
47 #include <ufs/ufs/ufsmount.h>
48 #include <ufs/ufs/ufs_extern.h>
49 #include <ufs/ffs/fs.h>
50 #include <ufs/ffs/ffs_extern.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_object.h>
55 #include <vm/vnode_pager.h>
57 static int ffs_rawread_readahead(struct vnode *vp,
63 static int ffs_rawread_main(struct vnode *vp,
66 static int ffs_rawread_sync(struct vnode *vp);
68 int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
70 SYSCTL_DECL(_vfs_ffs);
72 static uma_zone_t ffsraw_pbuf_zone;
74 static int allowrawread = 1;
75 SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0,
76 "Flag to enable raw reads");
78 static int rawreadahead = 1;
79 SYSCTL_INT(_vfs_ffs, OID_AUTO, rawreadahead, CTLFLAG_RW, &rawreadahead, 0,
80 "Flag to enable readahead for long raw reads");
83 ffs_rawread_setup(void *arg __unused)
86 ffsraw_pbuf_zone = pbuf_zsecond_create("ffsrawpbuf",
87 (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8);
89 SYSINIT(ffs_raw, SI_SUB_VM_CONF, SI_ORDER_ANY, ffs_rawread_setup, NULL);
92 ffs_rawread_sync(struct vnode *vp)
100 /* Check for dirty mmap, pending writes and dirty buffers */
104 if (bo->bo_numoutput > 0 ||
105 bo->bo_dirty.bv_cnt > 0 ||
106 ((obj = vp->v_object) != NULL &&
107 vm_object_mightbedirty(obj))) {
111 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
112 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
117 (void) vn_start_write(vp, &mp, V_WAIT);
118 VOP_LOCK(vp, LK_EXCLUSIVE);
119 } else if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
121 /* Upgrade to exclusive lock, this might block */
122 VOP_LOCK(vp, LK_UPGRADE);
128 /* Check if vnode was reclaimed while unlocked. */
129 if (VN_IS_DOOMED(vp)) {
132 VOP_LOCK(vp, LK_DOWNGRADE);
133 vn_finished_write(mp);
138 /* Attempt to msync mmap() regions to clean dirty mmap */
139 vnode_pager_clean_sync(vp);
141 /* Wait for pending writes to complete */
143 error = bufobj_wwait(&vp->v_bufobj, 0, 0);
145 /* XXX: can't happen with a zero timeout ??? */
148 VOP_LOCK(vp, LK_DOWNGRADE);
149 vn_finished_write(mp);
152 /* Flush dirty buffers */
153 if (bo->bo_dirty.bv_cnt > 0) {
155 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0) {
157 VOP_LOCK(vp, LK_DOWNGRADE);
158 vn_finished_write(mp);
162 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
163 panic("ffs_rawread_sync: dirty bufs");
167 VOP_LOCK(vp, LK_DOWNGRADE);
168 vn_finished_write(mp);
177 ffs_rawread_readahead(struct vnode *vp,
194 bsize = vp->v_mount->mnt_stat.f_iosize;
199 iolen = ((vm_offset_t) udata) & PAGE_MASK;
201 if (bp->b_bcount + iolen > bp->b_kvasize) {
202 bp->b_bcount = bp->b_kvasize;
204 bp->b_bcount -= PAGE_SIZE;
206 bp->b_flags = 0; /* XXX necessary ? */
207 bp->b_iocmd = BIO_READ;
208 bp->b_iodone = bdone;
209 blockno = offset / bsize;
210 blockoff = (offset % bsize) / DEV_BSIZE;
211 if ((daddr_t) blockno != blockno) {
212 return EINVAL; /* blockno overflow */
215 bp->b_lblkno = bp->b_blkno = blockno;
217 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, NULL, &bforwards, NULL);
221 /* Fill holes with NULs to preserve semantics */
223 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize)
224 bp->b_bcount = bsize - blockoff * DEV_BSIZE;
226 if (vmapbuf(bp, udata, bp->b_bcount, 1) < 0)
230 bzero(bp->b_data, bp->b_bufsize);
232 /* Mark operation completed (similar to bufdone()) */
235 bp->b_flags |= B_DONE;
238 bp->b_blkno = blkno + blockoff;
239 bp->b_offset = bp->b_iooffset = (blkno + blockoff) * DEV_BSIZE;
241 if (bp->b_bcount + blockoff * DEV_BSIZE > bsize * (1 + bforwards))
242 bp->b_bcount = bsize * (1 + bforwards) - blockoff * DEV_BSIZE;
244 if (vmapbuf(bp, udata, bp->b_bcount, 1) < 0)
247 BO_STRATEGY(&dp->v_bufobj, bp);
252 ffs_rawread_main(struct vnode *vp,
256 struct buf *bp, *nbp, *tbp;
263 td = uio->uio_td ? uio->uio_td : curthread;
264 udata = uio->uio_iov->iov_base;
265 resid = uio->uio_resid;
266 offset = uio->uio_offset;
269 * keep the process from being swapped
281 if (bp == NULL) { /* Setup first read */
282 bp = uma_zalloc(ffsraw_pbuf_zone, M_WAITOK);
284 error = ffs_rawread_readahead(vp, udata, offset,
289 if (resid > bp->b_bufsize) { /* Setup fist readahead */
290 if (rawreadahead != 0)
291 nbp = uma_zalloc(ffsraw_pbuf_zone,
298 nerror = ffs_rawread_readahead(vp,
309 uma_zfree(ffsraw_pbuf_zone,
317 bwait(bp, PRIBIO, "rawrd");
320 iolen = bp->b_bcount - bp->b_resid;
321 if (iolen == 0 && (bp->b_ioflags & BIO_ERROR) == 0) {
322 nerror = 0; /* Ignore possible beyond EOF error */
326 if ((bp->b_ioflags & BIO_ERROR) != 0) {
333 if (iolen < bp->b_bufsize) {
334 /* Incomplete read. Try to read remaining part */
335 error = ffs_rawread_readahead(vp,
338 bp->b_bufsize - iolen,
343 } else if (nbp != NULL) { /* Complete read with readahead */
349 if (resid <= bp->b_bufsize) { /* No more readaheads */
351 uma_zfree(ffsraw_pbuf_zone, nbp);
353 } else { /* Setup next readahead */
354 nerror = ffs_rawread_readahead(vp,
365 uma_zfree(ffsraw_pbuf_zone, nbp);
369 } else if (nerror != 0) {/* Deferred Readahead error */
371 } else if (resid > 0) { /* More to read, no readahead */
372 error = ffs_rawread_readahead(vp, udata, offset,
381 uma_zfree(ffsraw_pbuf_zone, bp);
383 if (nbp != NULL) { /* Run down readahead buffer */
384 bwait(nbp, PRIBIO, "rawrd");
387 uma_zfree(ffsraw_pbuf_zone, nbp);
393 uio->uio_iov->iov_base = udata;
394 uio->uio_resid = resid;
395 uio->uio_offset = offset;
400 ffs_rawread(struct vnode *vp,
404 if (allowrawread != 0 &&
405 uio->uio_iovcnt == 1 &&
406 uio->uio_segflg == UIO_USERSPACE &&
407 uio->uio_resid == uio->uio_iov->iov_len &&
408 (((uio->uio_td != NULL) ? uio->uio_td : curthread)->td_pflags &
409 TDP_DEADLKTREAT) == 0) {
410 int secsize; /* Media sector size */
411 off_t filebytes; /* Bytes left of file */
412 int blockbytes; /* Bytes left of file in full blocks */
413 int partialbytes; /* Bytes in last partial block */
414 int skipbytes; /* Bytes not to read in ffs_rawread */
419 /* Only handle sector aligned reads */
421 secsize = ITODEVVP(ip)->v_bufobj.bo_bsize;
422 if ((uio->uio_offset & (secsize - 1)) == 0 &&
423 (uio->uio_resid & (secsize - 1)) == 0) {
425 /* Sync dirty pages and buffers if needed */
426 error = ffs_rawread_sync(vp);
430 /* Check for end of file */
431 if (ip->i_size > uio->uio_offset) {
432 filebytes = ip->i_size - uio->uio_offset;
434 /* No special eof handling needed ? */
435 if (uio->uio_resid <= filebytes) {
437 return ffs_rawread_main(vp, uio);
440 partialbytes = ((unsigned int) ip->i_size) %
442 blockbytes = (int) filebytes - partialbytes;
443 if (blockbytes > 0) {
444 skipbytes = uio->uio_resid -
446 uio->uio_resid = blockbytes;
447 error = ffs_rawread_main(vp, uio);
448 uio->uio_resid += skipbytes;
451 /* Read remaining part using buffer */