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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95 30 * $FreeBSD: src/sys/ufs/ffs/ffs_inode.c,v 1.56.2.5 2002/02/05 18:35:03 dillon Exp $ 31 */ 32 33 #include "opt_quota.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/mount.h> 38 #include <sys/proc.h> 39 #include <sys/buf.h> 40 #include <sys/vnode.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/resourcevar.h> 44 #include <sys/vmmeter.h> 45 46 #include <vm/vm.h> 47 #include <vm/vm_extern.h> 48 49 #include "quota.h" 50 #include "ufsmount.h" 51 #include "inode.h" 52 #include "ufs_extern.h" 53 54 #include "fs.h" 55 #include "ffs_extern.h" 56 57 #include <vm/vm_page2.h> 58 #include <sys/buf2.h> 59 60 static int ffs_indirtrunc (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(struct vnode *vp, int waitfor) 74 { 75 struct fs *fs; 76 struct buf *bp; 77 struct inode *ip; 78 int error; 79 80 ufs_itimes(vp); 81 ip = VTOI(vp); 82 if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0) 83 return (0); 84 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED); 85 fs = ip->i_fs; 86 if (fs->fs_ronly) 87 return (0); 88 89 /* 90 * The vnode type is usually set to VBAD if an unrecoverable I/O 91 * error has occured (such as when reading the inode). Clear the 92 * modified bits but do not write anything out in this case. 93 */ 94 if (vp->v_type == VBAD) 95 return (0); 96 /* 97 * Ensure that uid and gid are correct. This is a temporary 98 * fix until fsck has been changed to do the update. 99 */ 100 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 101 ip->i_din.di_ouid = ip->i_uid; /* XXX */ 102 ip->i_din.di_ogid = ip->i_gid; /* XXX */ 103 } /* XXX */ 104 error = bread(ip->i_devvp, 105 fsbtodoff(fs, ino_to_fsba(fs, ip->i_number)), 106 (int)fs->fs_bsize, &bp); 107 if (error) { 108 brelse(bp); 109 return (error); 110 } 111 if (DOINGSOFTDEP(vp)) 112 softdep_update_inodeblock(ip, bp, waitfor); 113 else if (ip->i_effnlink != ip->i_nlink) 114 panic("ffs_update: bad link cnt"); 115 *((struct ufs1_dinode *)bp->b_data + 116 ino_to_fsbo(fs, ip->i_number)) = ip->i_din; 117 if (waitfor && !DOINGASYNC(vp)) { 118 return (bwrite(bp)); 119 } else if (vm_page_count_severe() || buf_dirty_count_severe()) { 120 return (bwrite(bp)); 121 } else { 122 if (bp->b_bufsize == fs->fs_bsize) 123 bp->b_flags |= B_CLUSTEROK; 124 bdwrite(bp); 125 return (0); 126 } 127 } 128 129 #define SINGLE 0 /* index of single indirect block */ 130 #define DOUBLE 1 /* index of double indirect block */ 131 #define TRIPLE 2 /* index of triple indirect block */ 132 /* 133 * Truncate the inode oip to at most length size, freeing the 134 * disk blocks. 135 */ 136 int 137 ffs_truncate(struct vnode *vp, off_t length, int flags, struct ucred *cred) 138 { 139 struct vnode *ovp = vp; 140 ufs_daddr_t lastblock; 141 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 struct fs *fs; 145 struct buf *bp; 146 int offset, size, level; 147 long count, nblocks, blocksreleased = 0; 148 int i; 149 int aflags, error, allerror; 150 off_t osize; 151 152 oip = VTOI(ovp); 153 fs = oip->i_fs; 154 if (length < 0) 155 return (EINVAL); 156 if (length > fs->fs_maxfilesize) 157 return (EFBIG); 158 if (ovp->v_type == VLNK && 159 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) { 160 #ifdef DIAGNOSTIC 161 if (length != 0) 162 panic("ffs_truncate: partial truncate of symlink"); 163 #endif /* DIAGNOSTIC */ 164 bzero((char *)&oip->i_shortlink, (uint)oip->i_size); 165 oip->i_size = 0; 166 oip->i_flag |= IN_CHANGE | IN_UPDATE; 167 return (ffs_update(ovp, 1)); 168 } 169 if (oip->i_size == length) { 170 oip->i_flag |= IN_CHANGE | IN_UPDATE; 171 return (ffs_update(ovp, 0)); 172 } 173 if (fs->fs_ronly) 174 panic("ffs_truncate: read-only filesystem"); 175 #ifdef QUOTA 176 error = ufs_getinoquota(oip); 177 if (error) 178 return (error); 179 #endif 180 if (DOINGSOFTDEP(ovp)) { 181 if (length > 0 || softdep_slowdown(ovp)) { 182 /* 183 * If a file is only partially truncated, then 184 * we have to clean up the data structures 185 * describing the allocation past the truncation 186 * point. Finding and deallocating those structures 187 * is a lot of work. Since partial truncation occurs 188 * rarely, we solve the problem by syncing the file 189 * so that it will have no data structures left. 190 */ 191 if ((error = VOP_FSYNC(ovp, MNT_WAIT, 0)) != 0) 192 return (error); 193 } else { 194 #ifdef QUOTA 195 (void) ufs_chkdq(oip, -oip->i_blocks, NOCRED, 0); 196 #endif 197 softdep_setup_freeblocks(oip, length); 198 vinvalbuf(ovp, 0, 0, 0); 199 nvnode_pager_setsize(ovp, 0, fs->fs_bsize, 0); 200 oip->i_flag |= IN_CHANGE | IN_UPDATE; 201 return (ffs_update(ovp, 0)); 202 } 203 } 204 osize = oip->i_size; 205 206 /* 207 * Lengthen the size of the file. We must ensure that the 208 * last byte of the file is allocated. Since the smallest 209 * value of osize is 0, length will be at least 1. 210 * 211 * nvextendbuf() only breads the old buffer. The blocksize 212 * of the new buffer must be specified so it knows how large 213 * to make the VM object. 214 */ 215 if (osize < length) { 216 nvextendbuf(vp, osize, length, 217 blkoffsize(fs, oip, osize), /* oblksize */ 218 blkoffresize(fs, length), /* nblksize */ 219 blkoff(fs, osize), 220 blkoff(fs, length), 221 0); 222 223 aflags = B_CLRBUF; 224 if (flags & IO_SYNC) 225 aflags |= B_SYNC; 226 /* BALLOC will reallocate the fragment at the old EOF */ 227 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp); 228 if (error) 229 return (error); 230 oip->i_size = length; 231 if (bp->b_bufsize == fs->fs_bsize) 232 bp->b_flags |= B_CLUSTEROK; 233 if (aflags & B_SYNC) 234 bwrite(bp); 235 else 236 bawrite(bp); 237 oip->i_flag |= IN_CHANGE | IN_UPDATE; 238 return (ffs_update(ovp, 1)); 239 } 240 241 /* 242 * Shorten the size of the file. 243 * 244 * NOTE: The block size specified in nvtruncbuf() is the blocksize 245 * of the buffer containing length prior to any reallocation 246 * of the block. 247 */ 248 allerror = nvtruncbuf(ovp, length, blkoffsize(fs, oip, length), 249 blkoff(fs, length), 0); 250 offset = blkoff(fs, length); 251 if (offset == 0) { 252 oip->i_size = length; 253 } else { 254 lbn = lblkno(fs, length); 255 aflags = B_CLRBUF; 256 if (flags & IO_SYNC) 257 aflags |= B_SYNC; 258 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp); 259 if (error) 260 return (error); 261 262 /* 263 * When we are doing soft updates and the UFS_BALLOC 264 * above fills in a direct block hole with a full sized 265 * block that will be truncated down to a fragment below, 266 * we must flush out the block dependency with an FSYNC 267 * so that we do not get a soft updates inconsistency 268 * when we create the fragment below. 269 * 270 * nvtruncbuf() may have re-dirtied the underlying block 271 * as part of its truncation zeroing code. To avoid a 272 * 'locking against myself' panic in the second fsync we 273 * can simply undirty the bp since the redirtying was 274 * related to areas of the buffer that we are going to 275 * throw away anyway, and we will b*write() the remainder 276 * anyway down below. 277 */ 278 if (DOINGSOFTDEP(ovp) && lbn < NDADDR && 279 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize) { 280 bundirty(bp); 281 error = VOP_FSYNC(ovp, MNT_WAIT, 0); 282 if (error) { 283 bdwrite(bp); 284 return (error); 285 } 286 } 287 oip->i_size = length; 288 size = blksize(fs, oip, lbn); 289 #if 0 290 /* remove - nvtruncbuf deals with this */ 291 if (ovp->v_type != VDIR) 292 bzero((char *)bp->b_data + offset, 293 (uint)(size - offset)); 294 #endif 295 /* Kirk's code has reallocbuf(bp, size, 1) here */ 296 allocbuf(bp, size); 297 if (bp->b_bufsize == fs->fs_bsize) 298 bp->b_flags |= B_CLUSTEROK; 299 if (aflags & B_SYNC) 300 bwrite(bp); 301 else 302 bawrite(bp); 303 } 304 /* 305 * Calculate index into inode's block list of 306 * last direct and indirect blocks (if any) 307 * which we want to keep. Lastblock is -1 when 308 * the file is truncated to 0. 309 */ 310 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 311 lastiblock[SINGLE] = lastblock - NDADDR; 312 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 313 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 314 nblocks = btodb(fs->fs_bsize); 315 316 /* 317 * Update file and block pointers on disk before we start freeing 318 * blocks. If we crash before free'ing blocks below, the blocks 319 * will be returned to the free list. lastiblock values are also 320 * normalized to -1 for calls to ffs_indirtrunc below. 321 */ 322 for (level = TRIPLE; level >= SINGLE; level--) { 323 oldblks[NDADDR + level] = oip->i_ib[level]; 324 if (lastiblock[level] < 0) { 325 oip->i_ib[level] = 0; 326 lastiblock[level] = -1; 327 } 328 } 329 for (i = 0; i < NDADDR; i++) { 330 oldblks[i] = oip->i_db[i]; 331 if (i > lastblock) 332 oip->i_db[i] = 0; 333 } 334 oip->i_flag |= IN_CHANGE | IN_UPDATE; 335 error = ffs_update(ovp, 1); 336 if (error && allerror == 0) 337 allerror = error; 338 339 /* 340 * Having written the new inode to disk, save its new configuration 341 * and put back the old block pointers long enough to process them. 342 * Note that we save the new block configuration so we can check it 343 * when we are done. 344 */ 345 for (i = 0; i < NDADDR; i++) { 346 newblks[i] = oip->i_db[i]; 347 oip->i_db[i] = oldblks[i]; 348 } 349 for (i = 0; i < NIADDR; i++) { 350 newblks[NDADDR + i] = oip->i_ib[i]; 351 oip->i_ib[i] = oldblks[NDADDR + i]; 352 } 353 oip->i_size = osize; 354 355 if (error && allerror == 0) 356 allerror = error; 357 358 /* 359 * Indirect blocks first. 360 */ 361 indir_lbn[SINGLE] = -NDADDR; 362 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; 363 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 364 for (level = TRIPLE; level >= SINGLE; level--) { 365 bn = oip->i_ib[level]; 366 if (bn != 0) { 367 error = ffs_indirtrunc(oip, indir_lbn[level], 368 fsbtodb(fs, bn), lastiblock[level], level, &count); 369 if (error) 370 allerror = error; 371 blocksreleased += count; 372 if (lastiblock[level] < 0) { 373 oip->i_ib[level] = 0; 374 ffs_blkfree(oip, bn, fs->fs_bsize); 375 blocksreleased += nblocks; 376 } 377 } 378 if (lastiblock[level] >= 0) 379 goto done; 380 } 381 382 /* 383 * All whole direct blocks or frags. 384 */ 385 for (i = NDADDR - 1; i > lastblock; i--) { 386 long bsize; 387 388 bn = oip->i_db[i]; 389 if (bn == 0) 390 continue; 391 oip->i_db[i] = 0; 392 bsize = blksize(fs, oip, i); 393 ffs_blkfree(oip, bn, bsize); 394 blocksreleased += btodb(bsize); 395 } 396 if (lastblock < 0) 397 goto done; 398 399 /* 400 * Finally, look for a change in size of the 401 * last direct block; release any frags. 402 */ 403 bn = oip->i_db[lastblock]; 404 if (bn != 0) { 405 long oldspace, newspace; 406 407 /* 408 * Calculate amount of space we're giving 409 * back as old block size minus new block size. 410 */ 411 oldspace = blksize(fs, oip, lastblock); 412 oip->i_size = length; 413 newspace = blksize(fs, oip, lastblock); 414 if (newspace == 0) 415 panic("ffs_truncate: newspace"); 416 if (oldspace - newspace > 0) { 417 /* 418 * Block number of space to be free'd is 419 * the old block # plus the number of frags 420 * required for the storage we're keeping. 421 */ 422 bn += numfrags(fs, newspace); 423 ffs_blkfree(oip, bn, oldspace - newspace); 424 blocksreleased += btodb(oldspace - newspace); 425 } 426 } 427 done: 428 #ifdef DIAGNOSTIC 429 for (level = SINGLE; level <= TRIPLE; level++) 430 if (newblks[NDADDR + level] != oip->i_ib[level]) 431 panic("ffs_truncate1"); 432 for (i = 0; i < NDADDR; i++) 433 if (newblks[i] != oip->i_db[i]) 434 panic("ffs_truncate2"); 435 if (length == 0 && !RB_EMPTY(&ovp->v_rbdirty_tree)) 436 panic("ffs_truncate3"); 437 #endif /* DIAGNOSTIC */ 438 /* 439 * Put back the real size. 440 */ 441 oip->i_size = length; 442 oip->i_blocks -= blocksreleased; 443 444 if (oip->i_blocks < 0) /* sanity */ 445 oip->i_blocks = 0; 446 oip->i_flag |= IN_CHANGE; 447 #ifdef QUOTA 448 (void) ufs_chkdq(oip, -blocksreleased, NOCRED, 0); 449 #endif 450 return (allerror); 451 } 452 453 /* 454 * Release blocks associated with the inode ip and stored in the indirect 455 * block bn. Blocks are free'd in LIFO order up to (but not including) 456 * lastbn. If level is greater than SINGLE, the block is an indirect block 457 * and recursive calls to indirtrunc must be used to cleanse other indirect 458 * blocks. 459 * 460 * NB: triple indirect blocks are untested. 461 */ 462 static int 463 ffs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, ufs_daddr_t dbn, 464 ufs_daddr_t lastbn, int level, long *countp) 465 { 466 int i; 467 struct buf *bp; 468 struct fs *fs = ip->i_fs; 469 ufs_daddr_t *bap; 470 struct vnode *vp; 471 ufs_daddr_t *copy = NULL, nb, nlbn, last; 472 long blkcount, factor; 473 int nblocks, blocksreleased = 0; 474 int error = 0, allerror = 0; 475 476 /* 477 * Calculate index in current block of last 478 * block to be kept. -1 indicates the entire 479 * block so we need not calculate the index. 480 */ 481 factor = 1; 482 for (i = SINGLE; i < level; i++) 483 factor *= NINDIR(fs); 484 last = lastbn; 485 if (lastbn > 0) 486 last /= factor; 487 nblocks = btodb(fs->fs_bsize); 488 /* 489 * Get buffer of block pointers, zero those entries corresponding 490 * to blocks to be free'd, and update on disk copy first. Since 491 * double(triple) indirect before single(double) indirect, calls 492 * to bmap on these blocks will fail. However, we already have 493 * the on disk address, so we have to set the bio_offset field 494 * explicitly instead of letting bread do everything for us. 495 */ 496 vp = ITOV(ip); 497 bp = getblk(vp, lblktodoff(fs, lbn), (int)fs->fs_bsize, 0, 0); 498 if ((bp->b_flags & B_CACHE) == 0) { 499 bp->b_flags &= ~(B_ERROR|B_INVAL); 500 bp->b_cmd = BUF_CMD_READ; 501 if (bp->b_bcount > bp->b_bufsize) 502 panic("ffs_indirtrunc: bad buffer size"); 503 /* 504 * BIO is bio2 which chains back to bio1. We wait 505 * on bio1. 506 */ 507 bp->b_bio2.bio_offset = dbtodoff(fs, dbn); 508 bp->b_bio1.bio_done = biodone_sync; 509 bp->b_bio1.bio_flags |= BIO_SYNC; 510 vfs_busy_pages(vp, bp); 511 /* 512 * Access the block device layer using the device vnode 513 * and the translated block number (bio2) instead of the 514 * file vnode (vp) and logical block number (bio1). 515 * 516 * Even though we are bypassing the vnode layer, we still 517 * want the vnode state to indicate that an I/O on its behalf 518 * is in progress. 519 */ 520 bio_start_transaction(&bp->b_bio1, &vp->v_track_read); 521 vn_strategy(ip->i_devvp, &bp->b_bio2); 522 error = biowait(&bp->b_bio1, "biord"); 523 } 524 if (error) { 525 brelse(bp); 526 *countp = 0; 527 return (error); 528 } 529 530 bap = (ufs_daddr_t *)bp->b_data; 531 if (lastbn != -1) { 532 copy = kmalloc(fs->fs_bsize, M_TEMP, M_WAITOK); 533 bcopy((caddr_t)bap, (caddr_t)copy, (uint)fs->fs_bsize); 534 bzero((caddr_t)&bap[last + 1], 535 (uint)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t)); 536 if (DOINGASYNC(vp)) { 537 bawrite(bp); 538 } else { 539 error = bwrite(bp); 540 if (error) 541 allerror = error; 542 } 543 bap = copy; 544 } 545 546 /* 547 * Recursively free totally unused blocks. 548 */ 549 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 550 i--, nlbn += factor) { 551 nb = bap[i]; 552 if (nb == 0) 553 continue; 554 if (level > SINGLE) { 555 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 556 (ufs_daddr_t)-1, level - 1, &blkcount)) != 0) 557 allerror = error; 558 blocksreleased += blkcount; 559 } 560 ffs_blkfree(ip, nb, fs->fs_bsize); 561 blocksreleased += nblocks; 562 } 563 564 /* 565 * Recursively free last partial block. 566 */ 567 if (level > SINGLE && lastbn >= 0) { 568 last = lastbn % factor; 569 nb = bap[i]; 570 if (nb != 0) { 571 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 572 last, level - 1, &blkcount); 573 if (error) 574 allerror = error; 575 blocksreleased += blkcount; 576 } 577 } 578 if (copy != NULL) { 579 kfree(copy, M_TEMP); 580 } else { 581 bp->b_flags |= B_INVAL | B_NOCACHE; 582 brelse(bp); 583 } 584 585 *countp = blocksreleased; 586 return (allerror); 587 } 588