1 /*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. 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 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95 30 * $FreeBSD: src/sys/ufs/ufs/ufs_readwrite.c,v 1.65.2.14 2003/04/04 22:21:29 tegge Exp $ 31 */ 32 33 #define BLKSIZE(a, b, c) blksize(a, b, c) 34 #define FS struct fs 35 #define I_FS i_fs 36 37 #include <vm/vm.h> 38 #include <vm/vm_object.h> 39 #include <vm/vm_pager.h> 40 #include <vm/vm_map.h> 41 #include <vm/vnode_pager.h> 42 #include <sys/event.h> 43 #include <sys/vmmeter.h> 44 #include <sys/sysctl.h> 45 #include <vm/vm_page2.h> 46 47 #include "opt_directio.h" 48 49 #define VN_KNOTE(vp, b) KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, (b)) 50 51 #ifdef DIRECTIO 52 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone); 53 #endif 54 55 SYSCTL_DECL(_vfs_ffs); 56 57 /* 58 * Vnode op for reading. 59 * 60 * ffs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 61 * struct ucred *a_cred) 62 */ 63 /* ARGSUSED */ 64 int 65 ffs_read(struct vop_read_args *ap) 66 { 67 struct vnode *vp; 68 struct inode *ip; 69 struct uio *uio; 70 FS *fs; 71 struct buf *bp; 72 off_t bytesinfile; 73 int xfersize, blkoffset; 74 int error, orig_resid; 75 int seqcount; 76 int ioflag; 77 78 vp = ap->a_vp; 79 seqcount = ap->a_ioflag >> 16; 80 ip = VTOI(vp); 81 uio = ap->a_uio; 82 ioflag = ap->a_ioflag; 83 #ifdef DIRECTIO 84 if ((ioflag & IO_DIRECT) != 0) { 85 int workdone; 86 87 error = ffs_rawread(vp, uio, &workdone); 88 if (error || workdone) 89 return error; 90 } 91 #endif 92 93 #ifdef DIAGNOSTIC 94 if (uio->uio_rw != UIO_READ) 95 panic("ffs_read: mode"); 96 97 if (vp->v_type == VLNK) { 98 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen) 99 panic("ffs_read: short symlink"); 100 } else if (vp->v_type != VREG && vp->v_type != VDIR) 101 panic("ffs_read: type %d", vp->v_type); 102 #endif 103 fs = ip->I_FS; 104 if ((uint64_t)uio->uio_offset > fs->fs_maxfilesize) 105 return (EFBIG); 106 107 orig_resid = uio->uio_resid; 108 if (orig_resid <= 0) 109 return (0); 110 111 bytesinfile = ip->i_size - uio->uio_offset; 112 if (bytesinfile <= 0) { 113 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 114 ip->i_flag |= IN_ACCESS; 115 return 0; 116 } 117 118 /* 119 * Ok so we couldn't do it all in one vm trick... 120 * so cycle around trying smaller bites.. 121 */ 122 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { 123 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0) 124 break; 125 126 error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL, 127 &bp, seqcount); 128 if (error) 129 break; 130 131 /* 132 * If IO_DIRECT then set B_DIRECT for the buffer. This 133 * will cause us to attempt to release the buffer later on 134 * and will cause the buffer cache to attempt to free the 135 * underlying pages. 136 */ 137 if (ioflag & IO_DIRECT) 138 bp->b_flags |= B_DIRECT; 139 140 /* 141 * We should only get non-zero b_resid when an I/O error 142 * has occurred, which should cause us to break above. 143 * However, if the short read did not cause an error, 144 * then we want to ensure that we do not uiomove bad 145 * or uninitialized data. 146 * 147 * XXX b_resid is only valid when an actual I/O has occured 148 * and may be incorrect if the buffer is B_CACHE or if the 149 * last op on the buffer was a failed write. This KASSERT 150 * is a precursor to removing it from the UFS code. 151 */ 152 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0")); 153 154 /* 155 * Calculate how much data we can copy 156 */ 157 blkoffset = blkoff(fs, uio->uio_offset); 158 xfersize = bp->b_bufsize - blkoffset; 159 if (xfersize > uio->uio_resid) 160 xfersize = uio->uio_resid; 161 if (xfersize > bytesinfile) 162 xfersize = bytesinfile; 163 if (xfersize <= 0) { 164 panic("ufs_readwrite: impossible xfersize: %d", 165 xfersize); 166 } 167 168 /* 169 * otherwise use the general form 170 */ 171 error = uiomovebp(bp, bp->b_data + blkoffset, xfersize, uio); 172 173 if (error) 174 break; 175 176 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 177 (LIST_FIRST(&bp->b_dep) == NULL)) { 178 /* 179 * If there are no dependencies, and it's VMIO, 180 * then we don't need the buf, mark it available 181 * for freeing. The VM has the data. 182 */ 183 bp->b_flags |= B_RELBUF; 184 brelse(bp); 185 } else { 186 /* 187 * Otherwise let whoever 188 * made the request take care of 189 * freeing it. We just queue 190 * it onto another list. 191 */ 192 bqrelse(bp); 193 } 194 } 195 196 /* 197 * This can only happen in the case of an error 198 * because the loop above resets bp to NULL on each iteration 199 * and on normal completion has not set a new value into it. 200 * so it must have come from a 'break' statement 201 */ 202 if (bp != NULL) { 203 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 204 (LIST_FIRST(&bp->b_dep) == NULL)) { 205 bp->b_flags |= B_RELBUF; 206 brelse(bp); 207 } else { 208 bqrelse(bp); 209 } 210 } 211 212 if ((error == 0 || uio->uio_resid != orig_resid) && 213 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 214 ip->i_flag |= IN_ACCESS; 215 return (error); 216 } 217 218 /* 219 * Vnode op for writing. 220 * 221 * ffs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 222 * struct ucred *a_cred) 223 */ 224 int 225 ffs_write(struct vop_write_args *ap) 226 { 227 struct vnode *vp; 228 struct uio *uio; 229 struct inode *ip; 230 FS *fs; 231 struct buf *bp; 232 ufs_daddr_t lbn; 233 off_t osize; 234 off_t nsize; 235 int seqcount; 236 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize; 237 struct thread *td; 238 239 extended = 0; 240 seqcount = ap->a_ioflag >> 16; 241 ioflag = ap->a_ioflag; 242 uio = ap->a_uio; 243 vp = ap->a_vp; 244 ip = VTOI(vp); 245 246 #ifdef DIAGNOSTIC 247 if (uio->uio_rw != UIO_WRITE) 248 panic("ffs_write: mode"); 249 #endif 250 251 switch (vp->v_type) { 252 case VREG: 253 if (ioflag & IO_APPEND) 254 uio->uio_offset = ip->i_size; 255 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) 256 return (EPERM); 257 /* FALLTHROUGH */ 258 case VLNK: 259 break; 260 case VDIR: 261 panic("ffs_write: dir write"); 262 break; 263 default: 264 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type, 265 (int)uio->uio_offset, 266 (int)uio->uio_resid 267 ); 268 } 269 270 fs = ip->I_FS; 271 if (uio->uio_offset < 0 || 272 (uint64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) { 273 return (EFBIG); 274 } 275 /* 276 * Maybe this should be above the vnode op call, but so long as 277 * file servers have no limits, I don't think it matters. 278 */ 279 td = uio->uio_td; 280 if (vp->v_type == VREG && td && td->td_proc && 281 uio->uio_offset + uio->uio_resid > 282 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 283 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ); 284 return (EFBIG); 285 } 286 287 resid = uio->uio_resid; 288 osize = ip->i_size; 289 290 /* 291 * NOTE! These B_ flags are actually balloc-only flags, not buffer 292 * flags. They are similar to the BA_ flags in fbsd. 293 */ 294 if (seqcount > B_SEQMAX) 295 flags = B_SEQMAX << B_SEQSHIFT; 296 else 297 flags = seqcount << B_SEQSHIFT; 298 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp)) 299 flags |= B_SYNC; 300 301 for (error = 0; uio->uio_resid > 0;) { 302 lbn = lblkno(fs, uio->uio_offset); 303 blkoffset = blkoff(fs, uio->uio_offset); 304 xfersize = fs->fs_bsize - blkoffset; 305 if (uio->uio_resid < xfersize) 306 xfersize = uio->uio_resid; 307 308 if (uio->uio_offset + xfersize > ip->i_size) { 309 nsize = uio->uio_offset + xfersize; 310 nvnode_pager_setsize(vp, nsize, 311 blkoffresize(fs, nsize), blkoff(fs, nsize)); 312 } 313 314 #if 0 315 /* 316 * If doing a dummy write to flush the buffer for a 317 * putpages we must perform a read-before-write to 318 * fill in any missing spots and clear any invalid 319 * areas. Otherwise a multi-page buffer may not properly 320 * flush. 321 * 322 * We must clear any invalid areas 323 */ 324 if (uio->uio_segflg == UIO_NOCOPY) { 325 error = ffs_blkatoff(vp, uio->uio_offset, NULL, &bp); 326 if (error) 327 break; 328 bqrelse(bp); 329 } 330 #endif 331 332 /* 333 * We must clear invalid areas. 334 */ 335 if (xfersize < fs->fs_bsize || uio->uio_segflg == UIO_NOCOPY) 336 flags |= B_CLRBUF; 337 else 338 flags &= ~B_CLRBUF; 339 /* XXX is uio->uio_offset the right thing here? */ 340 error = VOP_BALLOC(vp, uio->uio_offset, xfersize, 341 ap->a_cred, flags, &bp); 342 if (error != 0) 343 break; 344 /* 345 * If the buffer is not valid and we did not clear garbage 346 * out above, we have to do so here even though the write 347 * covers the entire buffer in order to avoid a mmap()/write 348 * race where another process may see the garbage prior to 349 * the uiomove() for a write replacing it. 350 */ 351 if ((bp->b_flags & B_CACHE) == 0 && (flags & B_CLRBUF) == 0) 352 vfs_bio_clrbuf(bp); 353 if (ioflag & IO_DIRECT) 354 bp->b_flags |= B_DIRECT; 355 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL)) 356 bp->b_flags |= B_NOCACHE; 357 358 if (uio->uio_offset + xfersize > ip->i_size) { 359 ip->i_size = uio->uio_offset + xfersize; 360 extended = 1; 361 } 362 363 size = BLKSIZE(fs, ip, lbn) - bp->b_resid; 364 if (size < xfersize) 365 xfersize = size; 366 367 error = uiomovebp(bp, bp->b_data + blkoffset, xfersize, uio); 368 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 369 (LIST_FIRST(&bp->b_dep) == NULL)) { 370 bp->b_flags |= B_RELBUF; 371 } 372 373 /* 374 * If IO_SYNC each buffer is written synchronously. Otherwise 375 * if we have a severe page deficiency write the buffer 376 * asynchronously. Otherwise try to cluster, and if that 377 * doesn't do it then either do an async write (if O_DIRECT), 378 * or a delayed write (if not). 379 */ 380 381 if (ioflag & IO_SYNC) { 382 (void)bwrite(bp); 383 } else if (vm_page_count_severe() || 384 buf_dirty_count_severe() || 385 (ioflag & IO_ASYNC)) { 386 bp->b_flags |= B_CLUSTEROK; 387 bawrite(bp); 388 } else if (xfersize + blkoffset == fs->fs_bsize) { 389 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) { 390 bp->b_flags |= B_CLUSTEROK; 391 cluster_write(bp, (off_t)ip->i_size, fs->fs_bsize, seqcount); 392 } else { 393 bawrite(bp); 394 } 395 } else if (ioflag & IO_DIRECT) { 396 bp->b_flags |= B_CLUSTEROK; 397 bawrite(bp); 398 } else { 399 bp->b_flags |= B_CLUSTEROK; 400 bdwrite(bp); 401 } 402 if (error || xfersize == 0) 403 break; 404 ip->i_flag |= IN_CHANGE | IN_UPDATE; 405 } 406 /* 407 * If we successfully wrote any data, and we are not the superuser 408 * we clear the setuid and setgid bits as a precaution against 409 * tampering. 410 */ 411 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0) 412 ip->i_mode &= ~(ISUID | ISGID); 413 if (resid > uio->uio_resid) 414 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); 415 if (error) { 416 if (ioflag & IO_UNIT) { 417 (void)ffs_truncate(vp, osize, ioflag & IO_SYNC, 418 ap->a_cred); 419 uio->uio_offset -= resid - uio->uio_resid; 420 uio->uio_resid = resid; 421 } 422 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) { 423 error = ffs_update(vp, 1); 424 } 425 426 return (error); 427 } 428 429