1 /* $NetBSD: ext2fs_readwrite.c,v 1.56 2010/04/23 15:38:46 pooka Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94 32 * Modified for ext2fs by Manuel Bouyer. 33 */ 34 35 /*- 36 * Copyright (c) 1997 Manuel Bouyer. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 * 58 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94 59 * Modified for ext2fs by Manuel Bouyer. 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.56 2010/04/23 15:38:46 pooka Exp $"); 64 65 #include <sys/param.h> 66 #include <sys/systm.h> 67 #include <sys/resourcevar.h> 68 #include <sys/kernel.h> 69 #include <sys/file.h> 70 #include <sys/stat.h> 71 #include <sys/buf.h> 72 #include <sys/proc.h> 73 #include <sys/mount.h> 74 #include <sys/vnode.h> 75 #include <sys/malloc.h> 76 #include <sys/signalvar.h> 77 #include <sys/kauth.h> 78 79 #include <ufs/ufs/inode.h> 80 #include <ufs/ufs/ufsmount.h> 81 #include <ufs/ufs/ufs_extern.h> 82 #include <ufs/ext2fs/ext2fs.h> 83 #include <ufs/ext2fs/ext2fs_extern.h> 84 85 86 #define doclusterread 0 /* XXX underway */ 87 #define doclusterwrite 0 88 89 /* 90 * Vnode op for reading. 91 */ 92 /* ARGSUSED */ 93 int 94 ext2fs_read(void *v) 95 { 96 struct vop_read_args /* { 97 struct vnode *a_vp; 98 struct uio *a_uio; 99 int a_ioflag; 100 kauth_cred_t a_cred; 101 } */ *ap = v; 102 struct vnode *vp; 103 struct inode *ip; 104 struct uio *uio; 105 struct m_ext2fs *fs; 106 struct buf *bp; 107 struct ufsmount *ump; 108 vsize_t bytelen; 109 daddr_t lbn, nextlbn; 110 off_t bytesinfile; 111 long size, xfersize, blkoffset; 112 int error; 113 114 vp = ap->a_vp; 115 ip = VTOI(vp); 116 ump = ip->i_ump; 117 uio = ap->a_uio; 118 error = 0; 119 120 #ifdef DIAGNOSTIC 121 if (uio->uio_rw != UIO_READ) 122 panic("%s: mode", "ext2fs_read"); 123 124 if (vp->v_type == VLNK) { 125 if (ext2fs_size(ip) < ump->um_maxsymlinklen || 126 (ump->um_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) 127 panic("%s: short symlink", "ext2fs_read"); 128 } else if (vp->v_type != VREG && vp->v_type != VDIR) 129 panic("%s: type %d", "ext2fs_read", vp->v_type); 130 #endif 131 fs = ip->i_e2fs; 132 if ((uint64_t)uio->uio_offset > ump->um_maxfilesize) 133 return (EFBIG); 134 if (uio->uio_resid == 0) 135 return (0); 136 if (uio->uio_offset >= ext2fs_size(ip)) 137 goto out; 138 139 if (vp->v_type == VREG) { 140 const int advice = IO_ADV_DECODE(ap->a_ioflag); 141 142 while (uio->uio_resid > 0) { 143 bytelen = MIN(ext2fs_size(ip) - uio->uio_offset, 144 uio->uio_resid); 145 if (bytelen == 0) 146 break; 147 148 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice, 149 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); 150 if (error) 151 break; 152 } 153 goto out; 154 } 155 156 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { 157 bytesinfile = ext2fs_size(ip) - uio->uio_offset; 158 if (bytesinfile <= 0) 159 break; 160 lbn = lblkno(fs, uio->uio_offset); 161 nextlbn = lbn + 1; 162 size = fs->e2fs_bsize; 163 blkoffset = blkoff(fs, uio->uio_offset); 164 xfersize = fs->e2fs_bsize - blkoffset; 165 if (uio->uio_resid < xfersize) 166 xfersize = uio->uio_resid; 167 if (bytesinfile < xfersize) 168 xfersize = bytesinfile; 169 170 if (lblktosize(fs, nextlbn) >= ext2fs_size(ip)) 171 error = bread(vp, lbn, size, NOCRED, 0, &bp); 172 else { 173 int nextsize = fs->e2fs_bsize; 174 error = breadn(vp, lbn, 175 size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp); 176 } 177 if (error) 178 break; 179 180 /* 181 * We should only get non-zero b_resid when an I/O error 182 * has occurred, which should cause us to break above. 183 * However, if the short read did not cause an error, 184 * then we want to ensure that we do not uiomove bad 185 * or uninitialized data. 186 */ 187 size -= bp->b_resid; 188 if (size < xfersize) { 189 if (size == 0) 190 break; 191 xfersize = size; 192 } 193 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio); 194 if (error) 195 break; 196 brelse(bp, 0); 197 } 198 if (bp != NULL) 199 brelse(bp, 0); 200 201 out: 202 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) { 203 ip->i_flag |= IN_ACCESS; 204 if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) 205 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT); 206 } 207 return (error); 208 } 209 210 /* 211 * Vnode op for writing. 212 */ 213 int 214 ext2fs_write(void *v) 215 { 216 struct vop_write_args /* { 217 struct vnode *a_vp; 218 struct uio *a_uio; 219 int a_ioflag; 220 kauth_cred_t a_cred; 221 } */ *ap = v; 222 struct vnode *vp; 223 struct uio *uio; 224 struct inode *ip; 225 struct m_ext2fs *fs; 226 struct buf *bp; 227 struct ufsmount *ump; 228 daddr_t lbn; 229 off_t osize; 230 int blkoffset, error, flags, ioflag, resid, xfersize; 231 vsize_t bytelen; 232 off_t oldoff = 0; /* XXX */ 233 bool async; 234 int extended = 0; 235 int advice; 236 237 ioflag = ap->a_ioflag; 238 advice = IO_ADV_DECODE(ioflag); 239 uio = ap->a_uio; 240 vp = ap->a_vp; 241 ip = VTOI(vp); 242 ump = ip->i_ump; 243 error = 0; 244 245 #ifdef DIAGNOSTIC 246 if (uio->uio_rw != UIO_WRITE) 247 panic("%s: mode", "ext2fs_write"); 248 #endif 249 250 switch (vp->v_type) { 251 case VREG: 252 if (ioflag & IO_APPEND) 253 uio->uio_offset = ext2fs_size(ip); 254 if ((ip->i_e2fs_flags & EXT2_APPEND) && 255 uio->uio_offset != ext2fs_size(ip)) 256 return (EPERM); 257 /* FALLTHROUGH */ 258 case VLNK: 259 break; 260 case VDIR: 261 if ((ioflag & IO_SYNC) == 0) 262 panic("%s: nonsync dir write", "ext2fs_write"); 263 break; 264 default: 265 panic("%s: type", "ext2fs_write"); 266 } 267 268 fs = ip->i_e2fs; 269 if (uio->uio_offset < 0 || 270 (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize) 271 return (EFBIG); 272 if (uio->uio_resid == 0) 273 return (0); 274 275 async = vp->v_mount->mnt_flag & MNT_ASYNC; 276 resid = uio->uio_resid; 277 osize = ext2fs_size(ip); 278 279 if (vp->v_type == VREG) { 280 while (uio->uio_resid > 0) { 281 oldoff = uio->uio_offset; 282 blkoffset = blkoff(fs, uio->uio_offset); 283 bytelen = MIN(fs->e2fs_bsize - blkoffset, 284 uio->uio_resid); 285 286 if (vp->v_size < oldoff + bytelen) { 287 uvm_vnp_setwritesize(vp, oldoff + bytelen); 288 } 289 error = ufs_balloc_range(vp, uio->uio_offset, 290 bytelen, ap->a_cred, 0); 291 if (error) 292 break; 293 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice, 294 UBC_WRITE | UBC_UNMAP_FLAG(vp)); 295 if (error) 296 break; 297 298 /* 299 * update UVM's notion of the size now that we've 300 * copied the data into the vnode's pages. 301 */ 302 303 if (vp->v_size < uio->uio_offset) { 304 uvm_vnp_setsize(vp, uio->uio_offset); 305 extended = 1; 306 } 307 308 /* 309 * flush what we just wrote if necessary. 310 * XXXUBC simplistic async flushing. 311 */ 312 313 if (!async && oldoff >> 16 != uio->uio_offset >> 16) { 314 mutex_enter(&vp->v_interlock); 315 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16, 316 (uio->uio_offset >> 16) << 16, PGO_CLEANIT); 317 } 318 } 319 if (error == 0 && ioflag & IO_SYNC) { 320 mutex_enter(&vp->v_interlock); 321 error = VOP_PUTPAGES(vp, trunc_page(oldoff), 322 round_page(blkroundup(fs, uio->uio_offset)), 323 PGO_CLEANIT | PGO_SYNCIO); 324 } 325 326 goto out; 327 } 328 329 flags = ioflag & IO_SYNC ? B_SYNC : 0; 330 for (error = 0; uio->uio_resid > 0;) { 331 lbn = lblkno(fs, uio->uio_offset); 332 blkoffset = blkoff(fs, uio->uio_offset); 333 xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid); 334 if (xfersize < fs->e2fs_bsize) 335 flags |= B_CLRBUF; 336 else 337 flags &= ~B_CLRBUF; 338 error = ext2fs_balloc(ip, 339 lbn, blkoffset + xfersize, ap->a_cred, &bp, flags); 340 if (error) 341 break; 342 if (ext2fs_size(ip) < uio->uio_offset + xfersize) { 343 error = ext2fs_setsize(ip, uio->uio_offset + xfersize); 344 if (error) 345 break; 346 } 347 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio); 348 349 /* 350 * update UVM's notion of the size now that we've 351 * copied the data into the vnode's pages. 352 */ 353 354 if (vp->v_size < uio->uio_offset) { 355 uvm_vnp_setsize(vp, uio->uio_offset); 356 extended = 1; 357 } 358 359 if (ioflag & IO_SYNC) 360 (void)bwrite(bp); 361 else if (xfersize + blkoffset == fs->e2fs_bsize) 362 bawrite(bp); 363 else 364 bdwrite(bp); 365 if (error || xfersize == 0) 366 break; 367 } 368 369 /* 370 * If we successfully wrote any data, and we are not the superuser 371 * we clear the setuid and setgid bits as a precaution against 372 * tampering. 373 */ 374 375 out: 376 ip->i_flag |= IN_CHANGE | IN_UPDATE; 377 if (resid > uio->uio_resid && ap->a_cred && 378 kauth_authorize_generic(ap->a_cred, KAUTH_GENERIC_ISSUSER, NULL)) 379 ip->i_e2fs_mode &= ~(ISUID | ISGID); 380 if (resid > uio->uio_resid) 381 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); 382 if (error) { 383 (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred); 384 uio->uio_offset -= resid - uio->uio_resid; 385 uio->uio_resid = resid; 386 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) 387 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT); 388 KASSERT(vp->v_size == ext2fs_size(ip)); 389 return (error); 390 } 391