| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
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 $ | |
| e92ca23a | 27 | * $DragonFly: src/sys/vfs/ufs/ffs_rawread.c,v 1.28 2008/06/19 23:27:39 dillon Exp $ |
| 984263bc MD |
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> | |
| 1f2de5d4 MD |
43 | #include "quota.h" |
| 44 | #include "inode.h" | |
| 45 | #include "fs.h" | |
| 984263bc MD |
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 | ||
| 2d53bf02 | 54 | static int ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t offset, |
| a11aaa81 | 55 | size_t len, struct buf *bp, int *baseticks); |
| 984263bc MD |
56 | static int ffs_rawread_main(struct vnode *vp, |
| 57 | struct uio *uio); | |
| 58 | ||
| a11aaa81 | 59 | static int ffs_rawread_sync(struct vnode *vp); |
| 984263bc MD |
60 | |
| 61 | int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone); | |
| 62 | ||
| 63 | void ffs_rawread_setup(void); | |
| 64 | ||
| 984263bc MD |
65 | SYSCTL_DECL(_vfs_ffs); |
| 66 | ||
| 67 | static int ffsrawbufcnt = 4; | |
| 68 | SYSCTL_INT(_vfs_ffs, OID_AUTO, ffsrawbufcnt, CTLFLAG_RD, &ffsrawbufcnt, 0, | |
| 69 | "Buffers available for raw reads"); | |
| 70 | ||
| 71 | static int allowrawread = 1; | |
| 72 | SYSCTL_INT(_vfs_ffs, OID_AUTO, allowrawread, CTLFLAG_RW, &allowrawread, 0, | |
| 73 | "Flag to enable raw reads"); | |
| 74 | ||
| 75 | static int rawreadahead = 1; | |
| 76 | SYSCTL_INT(_vfs_ffs, OID_AUTO, rawreadahead, CTLFLAG_RW, &rawreadahead, 0, | |
| 77 | "Flag to enable readahead for long raw reads"); | |
| 78 | ||
| 79 | ||
| 80 | void | |
| 81 | ffs_rawread_setup(void) | |
| 82 | { | |
| 83 | ffsrawbufcnt = (nswbuf > 100 ) ? (nswbuf - (nswbuf >> 4)) : nswbuf - 8; | |
| 84 | } | |
| 85 | ||
| 86 | ||
| 87 | static int | |
| a11aaa81 | 88 | ffs_rawread_sync(struct vnode *vp) |
| 984263bc | 89 | { |
| 984263bc MD |
90 | int error; |
| 91 | int upgraded; | |
| 92 | ||
| 0202303b MD |
93 | /* |
| 94 | * Check for dirty mmap, pending writes and dirty buffers | |
| 95 | */ | |
| 3b998fa9 | 96 | lwkt_gettoken(&vp->v_token); |
| a9a20f98 | 97 | if (bio_track_active(&vp->v_track_write) || |
| 6bae6177 | 98 | !RB_EMPTY(&vp->v_rbdirty_tree) || |
| 984263bc | 99 | (vp->v_flag & VOBJDIRTY) != 0) { |
| a11aaa81 | 100 | if (vn_islocked(vp) != LK_EXCLUSIVE) { |
| 984263bc MD |
101 | upgraded = 1; |
| 102 | /* Upgrade to exclusive lock, this might block */ | |
| a11aaa81 | 103 | vn_lock(vp, LK_UPGRADE); |
| 984263bc MD |
104 | } else |
| 105 | upgraded = 0; | |
| 106 | ||
| 107 | /* Attempt to msync mmap() regions to clean dirty mmap */ | |
| 108 | if ((vp->v_flag & VOBJDIRTY) != 0) { | |
| 109 | struct vm_object *obj; | |
| 7540ab49 | 110 | if ((obj = vp->v_object) != NULL) |
| 984263bc MD |
111 | vm_object_page_clean(obj, 0, 0, OBJPC_SYNC); |
| 112 | } | |
| 113 | ||
| 114 | /* Wait for pending writes to complete */ | |
| a9a20f98 MD |
115 | error = bio_track_wait(&vp->v_track_write, 0, 0); |
| 116 | if (error != 0) { | |
| a9a20f98 MD |
117 | if (upgraded != 0) |
| 118 | vn_lock(vp, LK_DOWNGRADE); | |
| 0202303b | 119 | goto done; |
| 984263bc MD |
120 | } |
| 121 | /* Flush dirty buffers */ | |
| 6bae6177 | 122 | if (!RB_EMPTY(&vp->v_rbdirty_tree)) { |
| 52174f71 | 123 | if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0) { |
| 984263bc | 124 | if (upgraded != 0) |
| a11aaa81 | 125 | vn_lock(vp, LK_DOWNGRADE); |
| 0202303b | 126 | goto done; |
| 984263bc | 127 | } |
| a9a20f98 | 128 | if (bio_track_active(&vp->v_track_write) || |
| 6bae6177 | 129 | !RB_EMPTY(&vp->v_rbdirty_tree)) |
| 984263bc MD |
130 | panic("ffs_rawread_sync: dirty bufs"); |
| 131 | } | |
| 984263bc | 132 | if (upgraded != 0) |
| a11aaa81 | 133 | vn_lock(vp, LK_DOWNGRADE); |
| 984263bc | 134 | } else { |
| 0202303b | 135 | error = 0; |
| 984263bc | 136 | } |
| 0202303b | 137 | done: |
| 3b998fa9 | 138 | lwkt_reltoken(&vp->v_token); |
| 0202303b | 139 | return error; |
| 984263bc MD |
140 | } |
| 141 | ||
| 142 | ||
| 143 | static int | |
| 81b5c339 | 144 | ffs_rawread_readahead(struct vnode *vp, caddr_t udata, off_t loffset, |
| a11aaa81 | 145 | size_t len, struct buf *bp, int *baseticks) |
| 984263bc MD |
146 | { |
| 147 | int error; | |
| 3591bbc6 | 148 | int iolen; |
| 984263bc MD |
149 | int blockoff; |
| 150 | int bsize; | |
| 151 | struct vnode *dp; | |
| 152 | int bforwards; | |
| 153 | ||
| 154 | bsize = vp->v_mount->mnt_stat.f_iosize; | |
| 3591bbc6 MD |
155 | |
| 156 | /* | |
| 157 | * Make sure it fits into the pbuf | |
| 158 | */ | |
| 159 | iolen = (int)(intptr_t)udata & PAGE_MASK; | |
| 160 | if (len + iolen > bp->b_kvasize) { | |
| 161 | len = bp->b_kvasize; | |
| 984263bc | 162 | if (iolen != 0) |
| 3591bbc6 | 163 | len -= PAGE_SIZE; |
| 984263bc | 164 | } |
| ae8e83e6 MD |
165 | |
| 166 | /* | |
| 167 | * Raw disk address is in bio2, but we wait for it to | |
| 168 | * chain to bio1. | |
| 169 | */ | |
| 10f3fee5 | 170 | bp->b_flags &= ~B_ERROR; |
| 81b5c339 | 171 | bp->b_loffset = loffset; |
| 81b5c339 | 172 | bp->b_bio2.bio_offset = NOOFFSET; |
| ae8e83e6 MD |
173 | bp->b_bio1.bio_done = biodone_sync; |
| 174 | bp->b_bio1.bio_flags |= BIO_SYNC; | |
| 81b5c339 | 175 | |
| aa73238b MD |
176 | blockoff = (loffset % bsize) / DEV_BSIZE; |
| 177 | ||
| 08daea96 | 178 | error = VOP_BMAP(vp, bp->b_loffset, &bp->b_bio2.bio_offset, |
| e92ca23a | 179 | &bforwards, NULL, BUF_CMD_READ); |
| 08daea96 | 180 | if (error != 0) |
| 984263bc | 181 | return error; |
| 08daea96 | 182 | dp = VTOI(vp)->i_devvp; |
| 54078292 | 183 | if (bp->b_bio2.bio_offset == NOOFFSET) { |
| 81b5c339 MD |
184 | /* |
| 185 | * Fill holes with NULs to preserve semantics | |
| 186 | */ | |
| 3591bbc6 MD |
187 | if (len + blockoff * DEV_BSIZE > bsize) |
| 188 | len = bsize - blockoff * DEV_BSIZE; | |
| 984263bc | 189 | |
| 3591bbc6 | 190 | if (vmapbuf(bp, udata, len) < 0) |
| 984263bc MD |
191 | return EFAULT; |
| 192 | ||
| 2d53bf02 MD |
193 | if (ticks - *baseticks >= hogticks) { |
| 194 | *baseticks = ticks; | |
| 984263bc | 195 | uio_yield(); |
| 2d53bf02 | 196 | } |
| 3591bbc6 | 197 | bzero(bp->b_data, bp->b_bcount); |
| 984263bc MD |
198 | |
| 199 | /* Mark operation completed (similar to bufdone()) */ | |
| 200 | ||
| 201 | bp->b_resid = 0; | |
| 984263bc MD |
202 | return 0; |
| 203 | } | |
| 204 | ||
| 3591bbc6 MD |
205 | if (len + blockoff * DEV_BSIZE > bforwards) |
| 206 | len = bforwards - blockoff * DEV_BSIZE; | |
| 54078292 | 207 | bp->b_bio2.bio_offset += blockoff * DEV_BSIZE; |
| 984263bc | 208 | |
| 3591bbc6 | 209 | if (vmapbuf(bp, udata, len) < 0) |
| 984263bc MD |
210 | return EFAULT; |
| 211 | ||
| 81b5c339 MD |
212 | /* |
| 213 | * Access the block device layer using the device vnode (dp) and | |
| 214 | * the translated block number (bio2) instead of the logical block | |
| 215 | * number (bio1). | |
| 216 | * | |
| 217 | * Even though we are bypassing the vnode layer, we still | |
| 218 | * want the vnode state to indicate that an I/O on its behalf | |
| 219 | * is in progress. | |
| 220 | */ | |
| 10f3fee5 | 221 | bp->b_cmd = BUF_CMD_READ; |
| 81b5c339 MD |
222 | bio_start_transaction(&bp->b_bio1, &vp->v_track_read); |
| 223 | vn_strategy(dp, &bp->b_bio2); | |
| 984263bc MD |
224 | return 0; |
| 225 | } | |
| 226 | ||
| 984263bc | 227 | static int |
| 2d53bf02 | 228 | ffs_rawread_main(struct vnode *vp, struct uio *uio) |
| 984263bc MD |
229 | { |
| 230 | int error, nerror; | |
| 231 | struct buf *bp, *nbp, *tbp; | |
| 3591bbc6 | 232 | int iolen; |
| 2d53bf02 | 233 | int baseticks = ticks; |
| 984263bc | 234 | caddr_t udata; |
| 54078292 | 235 | int resid; |
| 984263bc | 236 | off_t offset; |
| 984263bc | 237 | |
| 984263bc MD |
238 | udata = uio->uio_iov->iov_base; |
| 239 | resid = uio->uio_resid; | |
| 240 | offset = uio->uio_offset; | |
| 241 | ||
| 984263bc MD |
242 | error = 0; |
| 243 | nerror = 0; | |
| 244 | ||
| 245 | bp = NULL; | |
| 246 | nbp = NULL; | |
| 984263bc MD |
247 | |
| 248 | while (resid > 0) { | |
| 249 | ||
| 250 | if (bp == NULL) { /* Setup first read */ | |
| 251 | /* XXX: Leave some bufs for swap */ | |
| 252 | bp = getpbuf(&ffsrawbufcnt); | |
| 2d53bf02 | 253 | error = ffs_rawread_readahead(vp, udata, offset, resid, |
| a11aaa81 | 254 | bp, &baseticks); |
| 984263bc MD |
255 | if (error != 0) |
| 256 | break; | |
| 257 | ||
| 258 | if (resid > bp->b_bufsize) { /* Setup fist readahead */ | |
| 259 | /* XXX: Leave bufs for swap */ | |
| 260 | if (rawreadahead != 0) | |
| 261 | nbp = trypbuf(&ffsrawbufcnt); | |
| 262 | else | |
| 263 | nbp = NULL; | |
| 264 | if (nbp != NULL) { | |
| 2d53bf02 MD |
265 | nerror = ffs_rawread_readahead( |
| 266 | vp, | |
| 267 | udata + bp->b_bufsize, | |
| 268 | offset + bp->b_bufsize, | |
| 269 | resid - bp->b_bufsize, | |
| a11aaa81 | 270 | nbp, &baseticks); |
| 984263bc MD |
271 | if (nerror) { |
| 272 | relpbuf(nbp, &ffsrawbufcnt); | |
| 273 | nbp = NULL; | |
| 274 | } | |
| 275 | } | |
| 276 | } | |
| 277 | } | |
| 278 | ||
| ae8e83e6 | 279 | biowait(&bp->b_bio1, "rawrd"); |
| 984263bc MD |
280 | |
| 281 | vunmapbuf(bp); | |
| 282 | ||
| 283 | iolen = bp->b_bcount - bp->b_resid; | |
| 284 | if (iolen == 0 && (bp->b_flags & B_ERROR) == 0) { | |
| 285 | nerror = 0; /* Ignore possible beyond EOF error */ | |
| 286 | break; /* EOF */ | |
| 287 | } | |
| 288 | ||
| 289 | if ((bp->b_flags & B_ERROR) != 0) { | |
| 290 | error = bp->b_error; | |
| 291 | break; | |
| 292 | } | |
| 81b5c339 | 293 | clearbiocache(&bp->b_bio2); |
| 984263bc MD |
294 | resid -= iolen; |
| 295 | udata += iolen; | |
| 296 | offset += iolen; | |
| 297 | if (iolen < bp->b_bufsize) { | |
| 298 | /* Incomplete read. Try to read remaining part */ | |
| 2d53bf02 MD |
299 | error = ffs_rawread_readahead( |
| 300 | vp, udata, offset, | |
| a11aaa81 | 301 | bp->b_bufsize - iolen, bp, &baseticks); |
| 984263bc MD |
302 | if (error != 0) |
| 303 | break; | |
| 304 | } else if (nbp != NULL) { /* Complete read with readahead */ | |
| 305 | ||
| 306 | tbp = bp; | |
| 307 | bp = nbp; | |
| 308 | nbp = tbp; | |
| 309 | ||
| 81b5c339 | 310 | clearbiocache(&nbp->b_bio2); |
| 984263bc MD |
311 | |
| 312 | if (resid <= bp->b_bufsize) { /* No more readaheads */ | |
| 313 | relpbuf(nbp, &ffsrawbufcnt); | |
| 314 | nbp = NULL; | |
| 315 | } else { /* Setup next readahead */ | |
| 2d53bf02 MD |
316 | nerror = ffs_rawread_readahead( |
| 317 | vp, udata + bp->b_bufsize, | |
| 318 | offset + bp->b_bufsize, | |
| 319 | resid - bp->b_bufsize, | |
| a11aaa81 | 320 | nbp, &baseticks); |
| 984263bc MD |
321 | if (nerror != 0) { |
| 322 | relpbuf(nbp, &ffsrawbufcnt); | |
| 323 | nbp = NULL; | |
| 324 | } | |
| 325 | } | |
| 326 | } else if (nerror != 0) {/* Deferred Readahead error */ | |
| 327 | break; | |
| 328 | } else if (resid > 0) { /* More to read, no readahead */ | |
| 329 | error = ffs_rawread_readahead(vp, udata, offset, | |
| a11aaa81 | 330 | resid, bp, |
| 2d53bf02 | 331 | &baseticks); |
| 984263bc MD |
332 | if (error != 0) |
| 333 | break; | |
| 334 | } | |
| 335 | } | |
| 336 | ||
| 337 | if (bp != NULL) | |
| 338 | relpbuf(bp, &ffsrawbufcnt); | |
| 339 | if (nbp != NULL) { /* Run down readahead buffer */ | |
| ae8e83e6 | 340 | biowait(&nbp->b_bio1, "rawrd"); |
| 984263bc MD |
341 | vunmapbuf(nbp); |
| 342 | relpbuf(nbp, &ffsrawbufcnt); | |
| 343 | } | |
| 344 | ||
| 345 | if (error == 0) | |
| 346 | error = nerror; | |
| 984263bc MD |
347 | uio->uio_iov->iov_base = udata; |
| 348 | uio->uio_resid = resid; | |
| 349 | uio->uio_offset = offset; | |
| 350 | return error; | |
| 351 | } | |
| 352 | ||
| 353 | ||
| 354 | int | |
| 355 | ffs_rawread(struct vnode *vp, | |
| 356 | struct uio *uio, | |
| 357 | int *workdone) | |
| 358 | { | |
| 359 | if (allowrawread != 0 && | |
| 360 | uio->uio_iovcnt == 1 && | |
| 361 | uio->uio_segflg == UIO_USERSPACE && | |
| 362 | uio->uio_resid == uio->uio_iov->iov_len && | |
| a11aaa81 | 363 | (curthread->td_flags & TDF_DEADLKTREAT) == 0) { |
| 984263bc MD |
364 | int secsize; /* Media sector size */ |
| 365 | off_t filebytes; /* Bytes left of file */ | |
| 366 | int blockbytes; /* Bytes left of file in full blocks */ | |
| 367 | int partialbytes; /* Bytes in last partial block */ | |
| 368 | int skipbytes; /* Bytes not to read in ffs_rawread */ | |
| 369 | struct inode *ip; | |
| 370 | int error; | |
| 371 | ||
| 372 | ||
| 373 | /* Only handle sector aligned reads */ | |
| 374 | ip = VTOI(vp); | |
| 375 | secsize = ip->i_devvp->v_rdev->si_bsize_phys; | |
| 376 | if ((uio->uio_offset & (secsize - 1)) == 0 && | |
| 377 | (uio->uio_resid & (secsize - 1)) == 0) { | |
| 378 | ||
| 379 | /* Sync dirty pages and buffers if needed */ | |
| a11aaa81 | 380 | error = ffs_rawread_sync(vp); |
| 984263bc MD |
381 | if (error != 0) |
| 382 | return error; | |
| 383 | ||
| 384 | /* Check for end of file */ | |
| 385 | if (ip->i_size > uio->uio_offset) { | |
| 386 | filebytes = ip->i_size - uio->uio_offset; | |
| 387 | ||
| 388 | /* No special eof handling needed ? */ | |
| 389 | if (uio->uio_resid <= filebytes) { | |
| 390 | *workdone = 1; | |
| 391 | return ffs_rawread_main(vp, uio); | |
| 392 | } | |
| 393 | ||
| 394 | partialbytes = ((unsigned int) ip->i_size) % | |
| 395 | ip->i_fs->fs_bsize; | |
| 396 | blockbytes = (int) filebytes - partialbytes; | |
| 397 | if (blockbytes > 0) { | |
| 398 | skipbytes = uio->uio_resid - | |
| 399 | blockbytes; | |
| 400 | uio->uio_resid = blockbytes; | |
| 401 | error = ffs_rawread_main(vp, uio); | |
| 402 | uio->uio_resid += skipbytes; | |
| 403 | if (error != 0) | |
| 404 | return error; | |
| 405 | /* Read remaining part using buffer */ | |
| 406 | } | |
| 407 | } | |
| 408 | } | |
| 409 | } | |
| 410 | *workdone = 0; | |
| 411 | return 0; | |
| 412 | } | |
| 413 |