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