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