1 /* $NetBSD: ext2fs_inode.c,v 1.39 2004/01/25 18:06:49 hannken Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 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 * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/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 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by Manuel Bouyer. 49 * 4. The name of the author may not be used to endorse or promote products 50 * derived from this software without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94 65 * Modified for ext2fs by Manuel Bouyer. 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.39 2004/01/25 18:06:49 hannken Exp $"); 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mount.h> 74 #include <sys/proc.h> 75 #include <sys/file.h> 76 #include <sys/buf.h> 77 #include <sys/vnode.h> 78 #include <sys/kernel.h> 79 #include <sys/malloc.h> 80 #include <sys/trace.h> 81 #include <sys/resourcevar.h> 82 83 #include <ufs/ufs/inode.h> 84 #include <ufs/ufs/ufsmount.h> 85 #include <ufs/ufs/ufs_extern.h> 86 87 #include <ufs/ext2fs/ext2fs.h> 88 #include <ufs/ext2fs/ext2fs_extern.h> 89 90 extern int prtactive; 91 92 static int ext2fs_indirtrunc __P((struct inode *, daddr_t, daddr_t, 93 daddr_t, int, long *)); 94 95 /* 96 * Last reference to an inode. If necessary, write or delete it. 97 */ 98 int 99 ext2fs_inactive(v) 100 void *v; 101 { 102 struct vop_inactive_args /* { 103 struct vnode *a_vp; 104 struct proc *a_p; 105 } */ *ap = v; 106 struct vnode *vp = ap->a_vp; 107 struct inode *ip = VTOI(vp); 108 struct mount *mp; 109 struct proc *p = ap->a_p; 110 struct timespec ts; 111 int error = 0; 112 113 if (prtactive && vp->v_usecount != 0) 114 vprint("ext2fs_inactive: pushing active", vp); 115 /* Get rid of inodes related to stale file handles. */ 116 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0) 117 goto out; 118 119 error = 0; 120 if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 121 vn_start_write(vp, &mp, V_WAIT | V_LOWER); 122 if (ip->i_e2fs_size != 0) { 123 error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL); 124 } 125 TIMEVAL_TO_TIMESPEC(&time, &ts); 126 ip->i_e2fs_dtime = ts.tv_sec; 127 ip->i_flag |= IN_CHANGE | IN_UPDATE; 128 VOP_VFREE(vp, ip->i_number, ip->i_e2fs_mode); 129 vn_finished_write(mp, V_LOWER); 130 } 131 if (ip->i_flag & 132 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) { 133 vn_start_write(vp, &mp, V_WAIT | V_LOWER); 134 VOP_UPDATE(vp, NULL, NULL, 0); 135 vn_finished_write(mp, V_LOWER); 136 } 137 out: 138 VOP_UNLOCK(vp, 0); 139 /* 140 * If we are done with the inode, reclaim it 141 * so that it can be reused immediately. 142 */ 143 if (ip->i_e2fs_dtime != 0) 144 vrecycle(vp, NULL, p); 145 return (error); 146 } 147 148 149 /* 150 * Update the access, modified, and inode change times as specified by the 151 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is 152 * used to specify that the inode needs to be updated but that the times have 153 * already been set. The access and modified times are taken from the second 154 * and third parameters; the inode change time is always taken from the current 155 * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk 156 * write of the inode to complete. 157 */ 158 int 159 ext2fs_update(v) 160 void *v; 161 { 162 struct vop_update_args /* { 163 struct vnode *a_vp; 164 struct timespec *a_access; 165 struct timespec *a_modify; 166 int a_flags; 167 } */ *ap = v; 168 struct m_ext2fs *fs; 169 struct buf *bp; 170 struct inode *ip; 171 int error; 172 struct timespec ts; 173 caddr_t cp; 174 int flags; 175 176 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) 177 return (0); 178 ip = VTOI(ap->a_vp); 179 TIMEVAL_TO_TIMESPEC(&time, &ts); 180 EXT2FS_ITIMES(ip, 181 ap->a_access ? ap->a_access : &ts, 182 ap->a_modify ? ap->a_modify : &ts, &ts); 183 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED); 184 if (flags == 0) 185 return (0); 186 fs = ip->i_e2fs; 187 188 error = bread(ip->i_devvp, 189 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 190 (int)fs->e2fs_bsize, NOCRED, &bp); 191 if (error) { 192 brelse(bp); 193 return (error); 194 } 195 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED); 196 cp = (caddr_t)bp->b_data + 197 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE); 198 e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp); 199 if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 && 200 (flags & IN_MODIFIED) != 0 && 201 (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 202 return (bwrite(bp)); 203 else { 204 bdwrite(bp); 205 return (0); 206 } 207 } 208 209 #define SINGLE 0 /* index of single indirect block */ 210 #define DOUBLE 1 /* index of double indirect block */ 211 #define TRIPLE 2 /* index of triple indirect block */ 212 /* 213 * Truncate the inode oip to at most length size, freeing the 214 * disk blocks. 215 */ 216 int 217 ext2fs_truncate(v) 218 void *v; 219 { 220 struct vop_truncate_args /* { 221 struct vnode *a_vp; 222 off_t a_length; 223 int a_flags; 224 struct ucred *a_cred; 225 struct proc *a_p; 226 } */ *ap = v; 227 struct vnode *ovp = ap->a_vp; 228 daddr_t lastblock; 229 struct inode *oip; 230 daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR]; 231 /* XXX ondisk32 */ 232 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; 233 off_t length = ap->a_length; 234 struct m_ext2fs *fs; 235 int offset, size, level; 236 long count, nblocks, blocksreleased = 0; 237 int i; 238 int error, allerror = 0; 239 off_t osize; 240 241 if (length < 0) 242 return (EINVAL); 243 244 oip = VTOI(ovp); 245 if (ovp->v_type == VLNK && 246 (oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen || 247 (ovp->v_mount->mnt_maxsymlinklen == 0 && 248 oip->i_e2fs_nblock == 0))) { 249 #ifdef DIAGNOSTIC 250 if (length != 0) 251 panic("ext2fs_truncate: partial truncate of symlink"); 252 #endif 253 memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0, 254 (u_int)oip->i_e2fs_size); 255 oip->i_e2fs_size = 0; 256 oip->i_flag |= IN_CHANGE | IN_UPDATE; 257 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT)); 258 } 259 if (oip->i_e2fs_size == length) { 260 oip->i_flag |= IN_CHANGE | IN_UPDATE; 261 return (VOP_UPDATE(ovp, NULL, NULL, 0)); 262 } 263 fs = oip->i_e2fs; 264 osize = oip->i_e2fs_size; 265 /* 266 * Lengthen the size of the file. We must ensure that the 267 * last byte of the file is allocated. Since the smallest 268 * value of osize is 0, length will be at least 1. 269 */ 270 if (osize < length) { 271 #if 0 /* XXX */ 272 if (length > fs->fs_maxfilesize) 273 return (EFBIG); 274 #endif 275 ufs_balloc_range(ovp, length - 1, 1, ap->a_cred, 276 ap->a_flags & IO_SYNC ? B_SYNC : 0); 277 oip->i_flag |= IN_CHANGE | IN_UPDATE; 278 return (VOP_UPDATE(ovp, NULL, NULL, 1)); 279 } 280 /* 281 * Shorten the size of the file. If the file is not being 282 * truncated to a block boundry, the contents of the 283 * partial block following the end of the file must be 284 * zero'ed in case it ever become accessible again because 285 * of subsequent file growth. 286 */ 287 offset = blkoff(fs, length); 288 if (offset != 0) { 289 size = fs->e2fs_bsize; 290 291 /* XXXUBC we should handle more than just VREG */ 292 uvm_vnp_zerorange(ovp, length, size - offset); 293 } 294 oip->i_e2fs_size = length; 295 uvm_vnp_setsize(ovp, length); 296 297 /* 298 * Calculate index into inode's block list of 299 * last direct and indirect blocks (if any) 300 * which we want to keep. Lastblock is -1 when 301 * the file is truncated to 0. 302 */ 303 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1; 304 lastiblock[SINGLE] = lastblock - NDADDR; 305 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 306 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 307 nblocks = btodb(fs->e2fs_bsize); 308 /* 309 * Update file and block pointers on disk before we start freeing 310 * blocks. If we crash before free'ing blocks below, the blocks 311 * will be returned to the free list. lastiblock values are also 312 * normalized to -1 for calls to ext2fs_indirtrunc below. 313 */ 314 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks); 315 for (level = TRIPLE; level >= SINGLE; level--) 316 if (lastiblock[level] < 0) { 317 oip->i_e2fs_blocks[NDADDR + level] = 0; 318 lastiblock[level] = -1; 319 } 320 for (i = NDADDR - 1; i > lastblock; i--) 321 oip->i_e2fs_blocks[i] = 0; 322 oip->i_flag |= IN_CHANGE | IN_UPDATE; 323 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT); 324 if (error && !allerror) 325 allerror = error; 326 327 /* 328 * Having written the new inode to disk, save its new configuration 329 * and put back the old block pointers long enough to process them. 330 * Note that we save the new block configuration so we can check it 331 * when we are done. 332 */ 333 334 memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks); 335 memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks); 336 oip->i_e2fs_size = osize; 337 error = vtruncbuf(ovp, lastblock + 1, 0, 0); 338 if (error && !allerror) 339 allerror = error; 340 341 /* 342 * Indirect blocks first. 343 */ 344 indir_lbn[SINGLE] = -NDADDR; 345 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1; 346 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 347 for (level = TRIPLE; level >= SINGLE; level--) { 348 /* XXX ondisk32 */ 349 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]); 350 if (bn != 0) { 351 error = ext2fs_indirtrunc(oip, indir_lbn[level], 352 fsbtodb(fs, bn), lastiblock[level], level, &count); 353 if (error) 354 allerror = error; 355 blocksreleased += count; 356 if (lastiblock[level] < 0) { 357 oip->i_e2fs_blocks[NDADDR + level] = 0; 358 ext2fs_blkfree(oip, bn); 359 blocksreleased += nblocks; 360 } 361 } 362 if (lastiblock[level] >= 0) 363 goto done; 364 } 365 366 /* 367 * All whole direct blocks or frags. 368 */ 369 for (i = NDADDR - 1; i > lastblock; i--) { 370 /* XXX ondisk32 */ 371 bn = fs2h32(oip->i_e2fs_blocks[i]); 372 if (bn == 0) 373 continue; 374 oip->i_e2fs_blocks[i] = 0; 375 ext2fs_blkfree(oip, bn); 376 blocksreleased += btodb(fs->e2fs_bsize); 377 } 378 379 done: 380 #ifdef DIAGNOSTIC 381 for (level = SINGLE; level <= TRIPLE; level++) 382 if (newblks[NDADDR + level] != 383 oip->i_e2fs_blocks[NDADDR + level]) 384 panic("ext2fs_truncate1"); 385 for (i = 0; i < NDADDR; i++) 386 if (newblks[i] != oip->i_e2fs_blocks[i]) 387 panic("ext2fs_truncate2"); 388 if (length == 0 && 389 (!LIST_EMPTY(&ovp->v_cleanblkhd) || 390 !LIST_EMPTY(&ovp->v_dirtyblkhd))) 391 panic("ext2fs_truncate3"); 392 #endif /* DIAGNOSTIC */ 393 /* 394 * Put back the real size. 395 */ 396 oip->i_e2fs_size = length; 397 oip->i_e2fs_nblock -= blocksreleased; 398 oip->i_flag |= IN_CHANGE; 399 return (allerror); 400 } 401 402 /* 403 * Release blocks associated with the inode ip and stored in the indirect 404 * block bn. Blocks are free'd in LIFO order up to (but not including) 405 * lastbn. If level is greater than SINGLE, the block is an indirect block 406 * and recursive calls to indirtrunc must be used to cleanse other indirect 407 * blocks. 408 * 409 * NB: triple indirect blocks are untested. 410 */ 411 static int 412 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 413 struct inode *ip; 414 daddr_t lbn, lastbn; 415 daddr_t dbn; 416 int level; 417 long *countp; 418 { 419 int i; 420 struct buf *bp; 421 struct m_ext2fs *fs = ip->i_e2fs; 422 int32_t *bap; /* XXX ondisk32 */ 423 struct vnode *vp; 424 daddr_t nb, nlbn, last; 425 int32_t *copy = NULL; /* XXX ondisk32 */ 426 long blkcount, factor; 427 int nblocks, blocksreleased = 0; 428 int error = 0, allerror = 0; 429 430 /* 431 * Calculate index in current block of last 432 * block to be kept. -1 indicates the entire 433 * block so we need not calculate the index. 434 */ 435 factor = 1; 436 for (i = SINGLE; i < level; i++) 437 factor *= NINDIR(fs); 438 last = lastbn; 439 if (lastbn > 0) 440 last /= factor; 441 nblocks = btodb(fs->e2fs_bsize); 442 /* 443 * Get buffer of block pointers, zero those entries corresponding 444 * to blocks to be free'd, and update on disk copy first. Since 445 * double(triple) indirect before single(double) indirect, calls 446 * to bmap on these blocks will fail. However, we already have 447 * the on disk address, so we have to set the b_blkno field 448 * explicitly instead of letting bread do everything for us. 449 */ 450 vp = ITOV(ip); 451 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0); 452 if (bp->b_flags & (B_DONE | B_DELWRI)) { 453 /* Braces must be here in case trace evaluates to nothing. */ 454 trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn); 455 } else { 456 trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn); 457 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */ 458 bp->b_flags |= B_READ; 459 if (bp->b_bcount > bp->b_bufsize) 460 panic("ext2fs_indirtrunc: bad buffer size"); 461 bp->b_blkno = dbn; 462 VOP_STRATEGY(vp, bp); 463 error = biowait(bp); 464 } 465 if (error) { 466 brelse(bp); 467 *countp = 0; 468 return (error); 469 } 470 471 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */ 472 if (lastbn >= 0) { 473 /* XXX ondisk32 */ 474 MALLOC(copy, int32_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK); 475 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize); 476 memset((caddr_t)&bap[last + 1], 0, 477 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t)); 478 error = bwrite(bp); 479 if (error) 480 allerror = error; 481 bap = copy; 482 } 483 484 /* 485 * Recursively free totally unused blocks. 486 */ 487 for (i = NINDIR(fs) - 1, 488 nlbn = lbn + 1 - i * factor; i > last; 489 i--, nlbn += factor) { 490 /* XXX ondisk32 */ 491 nb = fs2h32(bap[i]); 492 if (nb == 0) 493 continue; 494 if (level > SINGLE) { 495 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 496 (daddr_t)-1, level - 1, 497 &blkcount); 498 if (error) 499 allerror = error; 500 blocksreleased += blkcount; 501 } 502 ext2fs_blkfree(ip, nb); 503 blocksreleased += nblocks; 504 } 505 506 /* 507 * Recursively free last partial block. 508 */ 509 if (level > SINGLE && lastbn >= 0) { 510 last = lastbn % factor; 511 /* XXX ondisk32 */ 512 nb = fs2h32(bap[i]); 513 if (nb != 0) { 514 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 515 last, level - 1, &blkcount); 516 if (error) 517 allerror = error; 518 blocksreleased += blkcount; 519 } 520 } 521 522 if (copy != NULL) { 523 FREE(copy, M_TEMP); 524 } else { 525 bp->b_flags |= B_INVAL; 526 brelse(bp); 527 } 528 529 *countp = blocksreleased; 530 return (allerror); 531 } 532