1 /* $OpenBSD: ext2fs_inode.c,v 1.14 2001/07/16 03:44:22 csapuntz Exp $ */ 2 /* $NetBSD: ext2fs_inode.c,v 1.1 1997/06/11 09:33:56 bouyer 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 <vm/vm.h> 53 54 #include <ufs/ufs/quota.h> 55 #include <ufs/ufs/inode.h> 56 #include <ufs/ufs/ufsmount.h> 57 #include <ufs/ufs/ufs_extern.h> 58 59 #include <ufs/ext2fs/ext2fs.h> 60 #include <ufs/ext2fs/ext2fs_extern.h> 61 62 static int ext2fs_indirtrunc __P((struct inode *, daddr_t, daddr_t, 63 daddr_t, int, long *)); 64 65 int 66 ext2fs_init(vfsp) 67 struct vfsconf *vfsp; 68 { 69 return (ufs_init(vfsp)); 70 } 71 72 /* 73 * Last reference to an inode. If necessary, write or delete it. 74 */ 75 int 76 ext2fs_inactive(v) 77 void *v; 78 { 79 struct vop_inactive_args /* { 80 struct vnode *a_vp; 81 } */ *ap = v; 82 register struct vnode *vp = ap->a_vp; 83 register struct inode *ip = VTOI(vp); 84 struct timespec ts; 85 int error = 0; 86 extern int prtactive; 87 88 if (prtactive && vp->v_usecount != 0) 89 vprint("ext2fs_inactive: pushing active", vp); 90 /* Get rid of inodes related to stale file handles. */ 91 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0) 92 goto out; 93 94 if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 95 (void) ext2fs_truncate(ip, (off_t)0, 0, NOCRED); 96 TIMEVAL_TO_TIMESPEC(&time, &ts); 97 ip->i_e2fs_dtime = ts.tv_sec; 98 ip->i_flag |= IN_CHANGE | IN_UPDATE; 99 ext2fs_inode_free(ip, ip->i_number, ip->i_e2fs_mode); 100 } 101 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 102 ext2fs_update(ip, NULL, NULL, 0); 103 } 104 out: 105 VOP_UNLOCK(vp, 0, ap->a_p); 106 /* 107 * If we are done with the inode, reclaim it 108 * so that it can be reused immediately. 109 */ 110 if (ip->i_e2fs_dtime != 0) 111 vrecycle(vp, (struct simplelock *)0, ap->a_p); 112 return (error); 113 } 114 115 116 /* 117 * Update the access, modified, and inode change times as specified by the 118 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is 119 * used to specify that the inode needs to be updated but that the times have 120 * already been set. The access and modified times are taken from the second 121 * and third parameters; the inode change time is always taken from the current 122 * time. If waitfor is set, then wait for the disk write of the inode to 123 * complete. 124 */ 125 int 126 ext2fs_update(struct inode *ip, struct timespec *atime, struct timespec *mtime, 127 int waitfor) 128 { 129 struct m_ext2fs *fs; 130 struct buf *bp; 131 int error; 132 struct timespec ts; 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 bcopy(&ip->i_din.e2fs_din, 152 ((struct ext2fs_dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)), 153 sizeof(struct ext2fs_dinode)); 154 if (waitfor) 155 return (bwrite(bp)); 156 else { 157 bdwrite(bp); 158 return (0); 159 } 160 } 161 162 #define SINGLE 0 /* index of single indirect block */ 163 #define DOUBLE 1 /* index of double indirect block */ 164 #define TRIPLE 2 /* index of triple indirect block */ 165 /* 166 * Truncate the inode oip to at most length size, freeing the 167 * disk blocks. 168 */ 169 int 170 ext2fs_truncate(struct inode *oip, off_t length, int flags, struct ucred *cred) 171 { 172 struct vnode *ovp = ITOV(oip); 173 daddr_t lastblock; 174 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 175 daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; 176 register struct m_ext2fs *fs; 177 struct buf *bp; 178 int offset, size, level; 179 long count, nblocks, vflags, blocksreleased = 0; 180 register int i; 181 int aflags, error, allerror; 182 off_t osize; 183 184 if (length < 0) 185 return (EINVAL); 186 187 if (ovp->v_type != VREG && 188 ovp->v_type != VDIR && 189 ovp->v_type != VLNK) 190 return (0); 191 192 if (ovp->v_type == VLNK && 193 (oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen || 194 (ovp->v_mount->mnt_maxsymlinklen == 0 && 195 oip->i_e2fs_nblock == 0))) { 196 #ifdef DIAGNOSTIC 197 if (length != 0) 198 panic("ext2fs_truncate: partial truncate of symlink"); 199 #endif 200 bzero((char *)&oip->i_din.e2fs_din.e2di_shortlink, 201 (u_int)oip->i_e2fs_size); 202 oip->i_e2fs_size = 0; 203 oip->i_flag |= IN_CHANGE | IN_UPDATE; 204 return (ext2fs_update(oip, NULL, NULL, 1)); 205 } 206 if (oip->i_e2fs_size == length) { 207 oip->i_flag |= IN_CHANGE | IN_UPDATE; 208 return (ext2fs_update(oip, NULL, NULL, 0)); 209 } 210 fs = oip->i_e2fs; 211 osize = oip->i_e2fs_size; 212 /* 213 * Lengthen the size of the file. We must ensure that the 214 * last byte of the file is allocated. Since the smallest 215 * value of osize is 0, length will be at least 1. 216 */ 217 if (osize < length) { 218 #if 0 /* XXX */ 219 if (length > fs->fs_maxfilesize) 220 return (EFBIG); 221 #endif 222 offset = blkoff(fs, length - 1); 223 lbn = lblkno(fs, length - 1); 224 aflags = B_CLRBUF; 225 if (flags & IO_SYNC) 226 aflags |= B_SYNC; 227 error = ext2fs_buf_alloc(oip, lbn, offset + 1, cred, &bp, 228 aflags); 229 if (error) 230 return (error); 231 oip->i_e2fs_size = length; 232 uvm_vnp_setsize(ovp, length); 233 uvm_vnp_uncache(ovp); 234 if (aflags & B_SYNC) 235 bwrite(bp); 236 else 237 bawrite(bp); 238 oip->i_flag |= IN_CHANGE | IN_UPDATE; 239 return (ext2fs_update(oip, NULL, NULL, 1)); 240 } 241 /* 242 * Shorten the size of the file. If the file is not being 243 * truncated to a block boundry, the contents of the 244 * partial block following the end of the file must be 245 * zero'ed in case it ever become accessable again because 246 * of subsequent file growth. 247 */ 248 offset = blkoff(fs, length); 249 if (offset == 0) { 250 oip->i_e2fs_size = length; 251 } else { 252 lbn = lblkno(fs, length); 253 aflags = B_CLRBUF; 254 if (flags & IO_SYNC) 255 aflags |= B_SYNC; 256 error = ext2fs_buf_alloc(oip, lbn, offset, cred, &bp, 257 aflags); 258 if (error) 259 return (error); 260 oip->i_e2fs_size = length; 261 size = fs->e2fs_bsize; 262 uvm_vnp_setsize(ovp, length); 263 uvm_vnp_uncache(ovp); 264 bzero((char *)bp->b_data + offset, (u_int)(size - offset)); 265 allocbuf(bp, size); 266 if (aflags & B_SYNC) 267 bwrite(bp); 268 else 269 bawrite(bp); 270 } 271 /* 272 * Calculate index into inode's block list of 273 * last direct and indirect blocks (if any) 274 * which we want to keep. Lastblock is -1 when 275 * the file is truncated to 0. 276 */ 277 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1; 278 lastiblock[SINGLE] = lastblock - NDADDR; 279 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 280 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 281 nblocks = btodb(fs->e2fs_bsize); 282 /* 283 * Update file and block pointers on disk before we start freeing 284 * blocks. If we crash before free'ing blocks below, the blocks 285 * will be returned to the free list. lastiblock values are also 286 * normalized to -1 for calls to ext2fs_indirtrunc below. 287 */ 288 bcopy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks); 289 for (level = TRIPLE; level >= SINGLE; level--) 290 if (lastiblock[level] < 0) { 291 oip->i_e2fs_blocks[NDADDR + level] = 0; 292 lastiblock[level] = -1; 293 } 294 for (i = NDADDR - 1; i > lastblock; i--) 295 oip->i_e2fs_blocks[i] = 0; 296 oip->i_flag |= IN_CHANGE | IN_UPDATE; 297 if ((error = ext2fs_update(oip, NULL, NULL, 1)) != 0) 298 allerror = error; 299 /* 300 * Having written the new inode to disk, save its new configuration 301 * and put back the old block pointers long enough to process them. 302 * Note that we save the new block configuration so we can check it 303 * when we are done. 304 */ 305 bcopy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)newblks, sizeof newblks); 306 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks); 307 oip->i_e2fs_size = osize; 308 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA; 309 allerror = vinvalbuf(ovp, vflags, cred, curproc, 0, 0); 310 311 /* 312 * Indirect blocks first. 313 */ 314 indir_lbn[SINGLE] = -NDADDR; 315 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1; 316 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 317 for (level = TRIPLE; level >= SINGLE; level--) { 318 bn = oip->i_e2fs_blocks[NDADDR + level]; 319 if (bn != 0) { 320 error = ext2fs_indirtrunc(oip, indir_lbn[level], 321 fsbtodb(fs, bn), lastiblock[level], level, &count); 322 if (error) 323 allerror = error; 324 blocksreleased += count; 325 if (lastiblock[level] < 0) { 326 oip->i_e2fs_blocks[NDADDR + level] = 0; 327 ext2fs_blkfree(oip, bn); 328 blocksreleased += nblocks; 329 } 330 } 331 if (lastiblock[level] >= 0) 332 goto done; 333 } 334 335 /* 336 * All whole direct blocks or frags. 337 */ 338 for (i = NDADDR - 1; i > lastblock; i--) { 339 bn = oip->i_e2fs_blocks[i]; 340 if (bn == 0) 341 continue; 342 oip->i_e2fs_blocks[i] = 0; 343 ext2fs_blkfree(oip, bn); 344 blocksreleased += btodb(fs->e2fs_bsize); 345 } 346 if (lastblock < 0) 347 goto done; 348 349 done: 350 #ifdef DIAGNOSTIC 351 for (level = SINGLE; level <= TRIPLE; level++) 352 if (newblks[NDADDR + level] != oip->i_e2fs_blocks[NDADDR + level]) 353 panic("itrunc1"); 354 for (i = 0; i < NDADDR; i++) 355 if (newblks[i] != oip->i_e2fs_blocks[i]) 356 panic("itrunc2"); 357 if (length == 0 && 358 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first)) 359 panic("itrunc3"); 360 #endif /* DIAGNOSTIC */ 361 /* 362 * Put back the real size. 363 */ 364 oip->i_e2fs_size = length; 365 oip->i_e2fs_nblock -= blocksreleased; 366 if (oip->i_e2fs_nblock < 0) /* sanity */ 367 oip->i_e2fs_nblock = 0; 368 oip->i_flag |= IN_CHANGE; 369 return (allerror); 370 } 371 372 /* 373 * Release blocks associated with the inode ip and stored in the indirect 374 * block bn. Blocks are free'd in LIFO order up to (but not including) 375 * lastbn. If level is greater than SINGLE, the block is an indirect block 376 * and recursive calls to indirtrunc must be used to cleanse other indirect 377 * blocks. 378 * 379 * NB: triple indirect blocks are untested. 380 */ 381 static int 382 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 383 register struct inode *ip; 384 daddr_t lbn, lastbn; 385 daddr_t dbn; 386 int level; 387 long *countp; 388 { 389 register int i; 390 struct buf *bp; 391 register struct m_ext2fs *fs = ip->i_e2fs; 392 register daddr_t *bap; 393 struct vnode *vp; 394 daddr_t *copy = NULL, nb, nlbn, last; 395 long blkcount, factor; 396 int nblocks, blocksreleased = 0; 397 int error = 0, allerror = 0; 398 399 /* 400 * Calculate index in current block of last 401 * block to be kept. -1 indicates the entire 402 * block so we need not calculate the index. 403 */ 404 factor = 1; 405 for (i = SINGLE; i < level; i++) 406 factor *= NINDIR(fs); 407 last = lastbn; 408 if (lastbn > 0) 409 last /= factor; 410 nblocks = btodb(fs->e2fs_bsize); 411 /* 412 * Get buffer of block pointers, zero those entries corresponding 413 * to blocks to be free'd, and update on disk copy first. Since 414 * double(triple) indirect before single(double) indirect, calls 415 * to bmap on these blocks will fail. However, we already have 416 * the on disk address, so we have to set the b_blkno field 417 * explicitly instead of letting bread do everything for us. 418 */ 419 vp = ITOV(ip); 420 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0); 421 if (!(bp->b_flags & (B_DONE | B_DELWRI))) { 422 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */ 423 bp->b_flags |= B_READ; 424 if (bp->b_bcount > bp->b_bufsize) 425 panic("ext2fs_indirtrunc: bad buffer size"); 426 bp->b_blkno = dbn; 427 VOP_STRATEGY(bp); 428 error = biowait(bp); 429 } 430 if (error) { 431 brelse(bp); 432 *countp = 0; 433 return (error); 434 } 435 436 bap = (daddr_t *)bp->b_data; 437 if (lastbn != -1) { 438 MALLOC(copy, daddr_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK); 439 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->e2fs_bsize); 440 bzero((caddr_t)&bap[last + 1], 441 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t)); 442 error = bwrite(bp); 443 if (error) 444 allerror = error; 445 bap = copy; 446 } 447 448 /* 449 * Recursively free totally unused blocks. 450 */ 451 for (i = NINDIR(fs) - 1, 452 nlbn = lbn + 1 - i * factor; i > last; 453 i--, nlbn += factor) { 454 nb = bap[i]; 455 if (nb == 0) 456 continue; 457 if (level > SINGLE) { 458 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 459 (daddr_t)-1, level - 1, 460 &blkcount); 461 if (error) 462 allerror = error; 463 blocksreleased += blkcount; 464 } 465 ext2fs_blkfree(ip, nb); 466 blocksreleased += nblocks; 467 } 468 469 /* 470 * Recursively free last partial block. 471 */ 472 if (level > SINGLE && lastbn >= 0) { 473 last = lastbn % factor; 474 nb = bap[i]; 475 if (nb != 0) { 476 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 477 last, level - 1, &blkcount); 478 if (error) 479 allerror = error; 480 blocksreleased += blkcount; 481 } 482 } 483 484 if (copy != NULL) { 485 FREE(copy, M_TEMP); 486 } else { 487 bp->b_flags |= B_INVAL; 488 brelse(bp); 489 } 490 491 *countp = blocksreleased; 492 return (allerror); 493 } 494