1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95 34 * $Id: ffs_inode.c,v 1.46 1998/07/04 20:45:38 julian Exp $ 35 */ 36 37 #include "opt_quota.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/mount.h> 42 #include <sys/proc.h> 43 #include <sys/buf.h> 44 #include <sys/vnode.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/resourcevar.h> 48 49 #include <vm/vm.h> 50 #include <vm/vm_extern.h> 51 52 #include <ufs/ufs/quota.h> 53 #include <ufs/ufs/ufsmount.h> 54 #include <ufs/ufs/inode.h> 55 #include <ufs/ufs/ufs_extern.h> 56 57 #include <ufs/ffs/fs.h> 58 #include <ufs/ffs/ffs_extern.h> 59 60 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t, 61 ufs_daddr_t, int, long *)); 62 63 /* 64 * Update the access, modified, and inode change times as specified by the 65 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode 66 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by 67 * the timestamp update). The IN_LAZYMOD flag is set to force a write 68 * later if not now. If we write now, then clear both IN_MODIFIED and 69 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is 70 * set, then wait for the write to complete. 71 */ 72 int 73 ffs_update(vp, access, modify, waitfor) 74 struct vnode *vp; 75 struct timeval *access; 76 struct timeval *modify; 77 int waitfor; 78 { 79 register struct fs *fs; 80 struct buf *bp; 81 struct inode *ip; 82 int error; 83 84 ip = VTOI(vp); 85 if (((ip->i_flag & 86 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) && 87 (waitfor != MNT_WAIT)) 88 return (0); 89 ufs_itimes(vp); 90 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED); 91 if (vp->v_mount->mnt_flag & MNT_RDONLY) 92 return (0); 93 fs = ip->i_fs; 94 /* 95 * Ensure that uid and gid are correct. This is a temporary 96 * fix until fsck has been changed to do the update. 97 */ 98 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 99 ip->i_din.di_ouid = ip->i_uid; /* XXX */ 100 ip->i_din.di_ogid = ip->i_gid; /* XXX */ 101 } /* XXX */ 102 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 103 (int)fs->fs_bsize, NOCRED, &bp); 104 if (error) { 105 brelse(bp); 106 return (error); 107 } 108 if (DOINGSOFTDEP(vp)) 109 softdep_update_inodeblock(ip, bp, waitfor); 110 else if (ip->i_effnlink != ip->i_nlink) 111 panic("ffs_update: bad link cnt"); 112 *((struct dinode *)bp->b_data + 113 ino_to_fsbo(fs, ip->i_number)) = ip->i_din; 114 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) { 115 return (bwrite(bp)); 116 } else { 117 if (bp->b_bufsize == fs->fs_bsize) 118 bp->b_flags |= B_CLUSTEROK; 119 bdwrite(bp); 120 return (0); 121 } 122 } 123 124 #define SINGLE 0 /* index of single indirect block */ 125 #define DOUBLE 1 /* index of double indirect block */ 126 #define TRIPLE 2 /* index of triple indirect block */ 127 /* 128 * Truncate the inode oip to at most length size, freeing the 129 * disk blocks. 130 */ 131 int 132 ffs_truncate(vp, length, flags, cred, p) 133 struct vnode *vp; 134 off_t length; 135 int flags; 136 struct ucred *cred; 137 struct proc *p; 138 { 139 register struct vnode *ovp = vp; 140 ufs_daddr_t lastblock; 141 register struct inode *oip; 142 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 143 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; 144 register struct fs *fs; 145 struct buf *bp; 146 int offset, size, level; 147 long count, nblocks, vflags, blocksreleased = 0; 148 struct timeval tv; 149 register int i; 150 int aflags, error, allerror; 151 off_t osize; 152 153 oip = VTOI(ovp); 154 if (oip->i_size == length) 155 return (0); 156 fs = oip->i_fs; 157 if (length < 0) 158 return (EINVAL); 159 if (length > fs->fs_maxfilesize) 160 return (EFBIG); 161 getmicrotime(&tv); 162 if (ovp->v_type == VLNK && 163 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) { 164 #ifdef DIAGNOSTIC 165 if (length != 0) 166 panic("ffs_truncate: partial truncate of symlink"); 167 #endif 168 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); 169 oip->i_size = 0; 170 oip->i_flag |= IN_CHANGE | IN_UPDATE; 171 return (UFS_UPDATE(ovp, &tv, &tv, 1)); 172 } 173 if (oip->i_size == length) { 174 oip->i_flag |= IN_CHANGE | IN_UPDATE; 175 return (UFS_UPDATE(ovp, &tv, &tv, 0)); 176 } 177 #ifdef QUOTA 178 error = getinoquota(oip); 179 if (error) 180 return (error); 181 #endif 182 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0; 183 if (DOINGSOFTDEP(ovp)) { 184 if (length > 0) { 185 /* 186 * If a file is only partially truncated, then 187 * we have to clean up the data structures 188 * describing the allocation past the truncation 189 * point. Finding and deallocating those structures 190 * is a lot of work. Since partial truncation occurs 191 * rarely, we solve the problem by syncing the file 192 * so that it will have no data structures left. 193 */ 194 if ((error = VOP_FSYNC(ovp, cred, MNT_WAIT, 195 p)) != 0) 196 return (error); 197 } else { 198 #ifdef QUOTA 199 (void) chkdq(oip, -oip->i_blocks, NOCRED, 0); 200 #endif 201 softdep_setup_freeblocks(oip, length); 202 vinvalbuf(ovp, 0, cred, p, 0, 0); 203 oip->i_flag |= IN_CHANGE | IN_UPDATE; 204 return (ffs_update(ovp, &tv, &tv, 0)); 205 } 206 } 207 osize = oip->i_size; 208 /* 209 * Lengthen the size of the file. We must ensure that the 210 * last byte of the file is allocated. Since the smallest 211 * value of osize is 0, length will be at least 1. 212 */ 213 if (osize < length) { 214 vnode_pager_setsize(ovp, length); 215 aflags = B_CLRBUF; 216 if (flags & IO_SYNC) 217 aflags |= B_SYNC; 218 error = VOP_BALLOC(ovp, length - 1, 1, 219 cred, aflags, &bp); 220 if (error) 221 return (error); 222 oip->i_size = length; 223 if (bp->b_bufsize == fs->fs_bsize) 224 bp->b_flags |= B_CLUSTEROK; 225 if (aflags & B_SYNC) 226 bwrite(bp); 227 else if (ovp->v_mount->mnt_flag & MNT_ASYNC) 228 bdwrite(bp); 229 else 230 bawrite(bp); 231 oip->i_flag |= IN_CHANGE | IN_UPDATE; 232 return (UFS_UPDATE(ovp, &tv, &tv, 1)); 233 } 234 /* 235 * Shorten the size of the file. If the file is not being 236 * truncated to a block boundary, the contents of the 237 * partial block following the end of the file must be 238 * zero'ed in case it ever becomes accessible again because 239 * of subsequent file growth. Directories however are not 240 * zero'ed as they should grow back initialized to empty. 241 */ 242 offset = blkoff(fs, length); 243 if (offset == 0) { 244 oip->i_size = length; 245 } else { 246 lbn = lblkno(fs, length); 247 aflags = B_CLRBUF; 248 if (flags & IO_SYNC) 249 aflags |= B_SYNC; 250 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp); 251 if (error) { 252 return (error); 253 } 254 oip->i_size = length; 255 size = blksize(fs, oip, lbn); 256 if (ovp->v_type != VDIR) 257 bzero((char *)bp->b_data + offset, 258 (u_int)(size - offset)); 259 /* Kirk's code has reallocbuf(bp, size, 1) here */ 260 allocbuf(bp, size); 261 if (bp->b_bufsize == fs->fs_bsize) 262 bp->b_flags |= B_CLUSTEROK; 263 if (aflags & B_SYNC) 264 bwrite(bp); 265 else if (ovp->v_mount->mnt_flag & MNT_ASYNC) 266 bdwrite(bp); 267 else 268 bawrite(bp); 269 } 270 /* 271 * Calculate index into inode's block list of 272 * last direct and indirect blocks (if any) 273 * which we want to keep. Lastblock is -1 when 274 * the file is truncated to 0. 275 */ 276 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 277 lastiblock[SINGLE] = lastblock - NDADDR; 278 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 279 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 280 nblocks = btodb(fs->fs_bsize); 281 /* 282 * Update file and block pointers on disk before we start freeing 283 * blocks. If we crash before free'ing blocks below, the blocks 284 * will be returned to the free list. lastiblock values are also 285 * normalized to -1 for calls to ffs_indirtrunc below. 286 */ 287 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); 288 for (level = TRIPLE; level >= SINGLE; level--) 289 if (lastiblock[level] < 0) { 290 oip->i_ib[level] = 0; 291 lastiblock[level] = -1; 292 } 293 for (i = NDADDR - 1; i > lastblock; i--) 294 oip->i_db[i] = 0; 295 oip->i_flag |= IN_CHANGE | IN_UPDATE; 296 allerror = UFS_UPDATE(ovp, &tv, &tv, ((length > 0) ? 0 : 1)); 297 298 /* 299 * Having written the new inode to disk, save its new configuration 300 * and put back the old block pointers long enough to process them. 301 * Note that we save the new block configuration so we can check it 302 * when we are done. 303 */ 304 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); 305 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); 306 oip->i_size = osize; 307 308 error = vtruncbuf(ovp, cred, p, length, fs->fs_bsize); 309 if (error && (allerror == 0)) 310 allerror = error; 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 = oip->i_ib[level]; 320 if (bn != 0) { 321 error = ffs_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_ib[level] = 0; 328 ffs_blkfree(oip, bn, fs->fs_bsize); 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 register long bsize; 341 342 bn = oip->i_db[i]; 343 if (bn == 0) 344 continue; 345 oip->i_db[i] = 0; 346 bsize = blksize(fs, oip, i); 347 ffs_blkfree(oip, bn, bsize); 348 blocksreleased += btodb(bsize); 349 } 350 if (lastblock < 0) 351 goto done; 352 353 /* 354 * Finally, look for a change in size of the 355 * last direct block; release any frags. 356 */ 357 bn = oip->i_db[lastblock]; 358 if (bn != 0) { 359 long oldspace, newspace; 360 361 /* 362 * Calculate amount of space we're giving 363 * back as old block size minus new block size. 364 */ 365 oldspace = blksize(fs, oip, lastblock); 366 oip->i_size = length; 367 newspace = blksize(fs, oip, lastblock); 368 if (newspace == 0) 369 panic("ffs_truncate: newspace"); 370 if (oldspace - newspace > 0) { 371 /* 372 * Block number of space to be free'd is 373 * the old block # plus the number of frags 374 * required for the storage we're keeping. 375 */ 376 bn += numfrags(fs, newspace); 377 ffs_blkfree(oip, bn, oldspace - newspace); 378 blocksreleased += btodb(oldspace - newspace); 379 } 380 } 381 done: 382 #ifdef DIAGNOSTIC 383 for (level = SINGLE; level <= TRIPLE; level++) 384 if (newblks[NDADDR + level] != oip->i_ib[level]) 385 panic("ffs_truncate1"); 386 for (i = 0; i < NDADDR; i++) 387 if (newblks[i] != oip->i_db[i]) 388 panic("ffs_truncate2"); 389 if (length == 0 && 390 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first)) 391 panic("ffs_truncate3"); 392 #endif /* DIAGNOSTIC */ 393 /* 394 * Put back the real size. 395 */ 396 oip->i_size = length; 397 oip->i_blocks -= blocksreleased; 398 399 if (oip->i_blocks < 0) /* sanity */ 400 oip->i_blocks = 0; 401 oip->i_flag |= IN_CHANGE; 402 #ifdef QUOTA 403 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 404 #endif 405 return (allerror); 406 } 407 408 /* 409 * Release blocks associated with the inode ip and stored in the indirect 410 * block bn. Blocks are free'd in LIFO order up to (but not including) 411 * lastbn. If level is greater than SINGLE, the block is an indirect block 412 * and recursive calls to indirtrunc must be used to cleanse other indirect 413 * blocks. 414 * 415 * NB: triple indirect blocks are untested. 416 */ 417 static int 418 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 419 register struct inode *ip; 420 ufs_daddr_t lbn, lastbn; 421 ufs_daddr_t dbn; 422 int level; 423 long *countp; 424 { 425 register int i; 426 struct buf *bp; 427 register struct fs *fs = ip->i_fs; 428 register ufs_daddr_t *bap; 429 struct vnode *vp; 430 ufs_daddr_t *copy = NULL, nb, nlbn, last; 431 long blkcount, factor; 432 int nblocks, blocksreleased = 0; 433 int error = 0, allerror = 0; 434 435 /* 436 * Calculate index in current block of last 437 * block to be kept. -1 indicates the entire 438 * block so we need not calculate the index. 439 */ 440 factor = 1; 441 for (i = SINGLE; i < level; i++) 442 factor *= NINDIR(fs); 443 last = lastbn; 444 if (lastbn > 0) 445 last /= factor; 446 nblocks = btodb(fs->fs_bsize); 447 /* 448 * Get buffer of block pointers, zero those entries corresponding 449 * to blocks to be free'd, and update on disk copy first. Since 450 * double(triple) indirect before single(double) indirect, calls 451 * to bmap on these blocks will fail. However, we already have 452 * the on disk address, so we have to set the b_blkno field 453 * explicitly instead of letting bread do everything for us. 454 */ 455 vp = ITOV(ip); 456 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0); 457 if ((bp->b_flags & B_CACHE) == 0) { 458 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */ 459 bp->b_flags |= B_READ; 460 if (bp->b_bcount > bp->b_bufsize) 461 panic("ffs_indirtrunc: bad buffer size"); 462 bp->b_blkno = dbn; 463 vfs_busy_pages(bp, 0); 464 VOP_STRATEGY(bp->b_vp, bp); 465 error = biowait(bp); 466 } 467 if (error) { 468 brelse(bp); 469 *countp = 0; 470 return (error); 471 } 472 473 bap = (ufs_daddr_t *)bp->b_data; 474 if (lastbn != -1) { 475 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 476 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 477 bzero((caddr_t)&bap[last + 1], 478 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t)); 479 if ((vp->v_mount->mnt_flag & MNT_ASYNC) == 0) { 480 error = bwrite(bp); 481 if (error) 482 allerror = error; 483 } else { 484 bawrite(bp); 485 } 486 bap = copy; 487 } 488 489 /* 490 * Recursively free totally unused blocks. 491 */ 492 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 493 i--, nlbn += factor) { 494 nb = bap[i]; 495 if (nb == 0) 496 continue; 497 if (level > SINGLE) { 498 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 499 (ufs_daddr_t)-1, level - 1, &blkcount)) 500 allerror = error; 501 blocksreleased += blkcount; 502 } 503 ffs_blkfree(ip, nb, fs->fs_bsize); 504 blocksreleased += nblocks; 505 } 506 507 /* 508 * Recursively free last partial block. 509 */ 510 if (level > SINGLE && lastbn >= 0) { 511 last = lastbn % factor; 512 nb = bap[i]; 513 if (nb != 0) { 514 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 515 last, level - 1, &blkcount); 516 if (error) 517 allerror = error; 518 blocksreleased += blkcount; 519 } 520 } 521 if (copy != NULL) { 522 FREE(copy, M_TEMP); 523 } else { 524 bp->b_flags |= B_INVAL | B_NOCACHE; 525 brelse(bp); 526 } 527 528 *countp = blocksreleased; 529 return (allerror); 530 } 531