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