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$ 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/file.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 50 #include <vm/vm.h> 51 #include <vm/vm_param.h> 52 #include <vm/vm_extern.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/ffs/fs.h> 60 #include <ufs/ffs/ffs_extern.h> 61 62 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t, 63 ufs_daddr_t, int, long *)); 64 65 /* 66 * Update the access, modified, and inode change times as specified by the 67 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. The IN_MODIFIED 68 * flag is used to specify that the inode needs to be updated even if none 69 * of the times needs to be updated. The access and modified times are taken 70 * from the second and third parameters; the inode change time is always 71 * taken from the current time. If waitfor is set, then wait for the disk 72 * write of the inode to complete. 73 */ 74 int 75 ffs_update(ap) 76 struct vop_update_args /* { 77 struct vnode *a_vp; 78 struct timeval *a_access; 79 struct timeval *a_modify; 80 int a_waitfor; 81 } */ *ap; 82 { 83 register struct fs *fs; 84 struct buf *bp; 85 struct inode *ip; 86 int error; 87 time_t tv_sec; 88 89 ip = VTOI(ap->a_vp); 90 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) { 91 ip->i_flag &= 92 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE); 93 return (0); 94 } 95 if ((ip->i_flag & 96 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) 97 return (0); 98 /* 99 * Use a copy of the current time to get consistent timestamps 100 * (a_access and a_modify are sometimes aliases for &time). 101 * 102 * XXX in 2.0, a_access and a_modify are often pointers to the 103 * same copy of `time'. This is not as good. Some callers forget 104 * to make a copy; others make a copy too early (before the i/o 105 * has completed)... 106 * 107 * XXX there should be a function or macro for reading the time 108 * (e.g., some machines may require splclock()). 109 */ 110 tv_sec = time.tv_sec; 111 if (ip->i_flag & IN_ACCESS) 112 ip->i_atime = 113 (ap->a_access == &time ? tv_sec : ap->a_access->tv_sec); 114 if (ip->i_flag & IN_UPDATE) { 115 ip->i_mtime = 116 (ap->a_modify == &time ? tv_sec : ap->a_modify->tv_sec); 117 ip->i_modrev++; 118 } 119 if (ip->i_flag & IN_CHANGE) 120 ip->i_ctime = tv_sec; 121 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE); 122 fs = ip->i_fs; 123 /* 124 * Ensure that uid and gid are correct. This is a temporary 125 * fix until fsck has been changed to do the update. 126 */ 127 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 128 ip->i_din.di_ouid = ip->i_uid; /* XXX */ 129 ip->i_din.di_ogid = ip->i_gid; /* XXX */ 130 } /* XXX */ 131 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 132 (int)fs->fs_bsize, NOCRED, &bp); 133 if (error) { 134 brelse(bp); 135 return (error); 136 } 137 *((struct dinode *)bp->b_data + 138 ino_to_fsbo(fs, ip->i_number)) = ip->i_din; 139 if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 140 return (bwrite(bp)); 141 else { 142 bp->b_flags |= B_CLUSTEROK; 143 bdwrite(bp); 144 return (0); 145 } 146 } 147 148 #define SINGLE 0 /* index of single indirect block */ 149 #define DOUBLE 1 /* index of double indirect block */ 150 #define TRIPLE 2 /* index of triple indirect block */ 151 /* 152 * Truncate the inode oip to at most length size, freeing the 153 * disk blocks. 154 */ 155 int 156 ffs_truncate(ap) 157 struct vop_truncate_args /* { 158 struct vnode *a_vp; 159 off_t a_length; 160 int a_flags; 161 struct ucred *a_cred; 162 struct proc *a_p; 163 } */ *ap; 164 { 165 register struct vnode *ovp = ap->a_vp; 166 ufs_daddr_t lastblock; 167 register struct inode *oip; 168 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 169 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; 170 off_t length = ap->a_length; 171 register struct fs *fs; 172 struct buf *bp; 173 int offset, size, level; 174 long count, nblocks, vflags, blocksreleased = 0; 175 struct timeval tv; 176 register int i; 177 int aflags, error, allerror; 178 off_t osize; 179 180 oip = VTOI(ovp); 181 fs = oip->i_fs; 182 if (length < 0) 183 return (EINVAL); 184 if (length > fs->fs_maxfilesize) 185 return (EFBIG); 186 tv = time; 187 if (ovp->v_type == VLNK && 188 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) { 189 #ifdef DIAGNOSTIC 190 if (length != 0) 191 panic("ffs_truncate: partial truncate of symlink"); 192 #endif 193 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); 194 oip->i_size = 0; 195 oip->i_flag |= IN_CHANGE | IN_UPDATE; 196 return (VOP_UPDATE(ovp, &tv, &tv, 1)); 197 } 198 if (oip->i_size == length) { 199 oip->i_flag |= IN_CHANGE | IN_UPDATE; 200 return (VOP_UPDATE(ovp, &tv, &tv, 0)); 201 } 202 #ifdef QUOTA 203 error = getinoquota(oip); 204 if (error) 205 return (error); 206 #endif 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 offset = blkoff(fs, length - 1); 215 lbn = lblkno(fs, length - 1); 216 aflags = B_CLRBUF; 217 if (ap->a_flags & IO_SYNC) 218 aflags |= B_SYNC; 219 error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, 220 &bp, aflags); 221 if (error) 222 return (error); 223 oip->i_size = length; 224 vnode_pager_setsize(ovp, length); 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 (VOP_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 boundry, the contents of the 237 * partial block following the end of the file must be 238 * zero'ed in case it ever become accessable again because 239 * of subsequent file growth. 240 */ 241 offset = blkoff(fs, length); 242 if (offset == 0) { 243 oip->i_size = length; 244 } else { 245 lbn = lblkno(fs, length); 246 aflags = B_CLRBUF; 247 if (ap->a_flags & IO_SYNC) 248 aflags |= B_SYNC; 249 error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags); 250 if (error) 251 return (error); 252 oip->i_size = length; 253 size = blksize(fs, oip, lbn); 254 bzero((char *)bp->b_data + offset, (u_int)(size - offset)); 255 allocbuf(bp, size); 256 if (aflags & B_SYNC) 257 bwrite(bp); 258 else if (ovp->v_mount->mnt_flag & MNT_ASYNC) 259 bdwrite(bp); 260 else 261 bawrite(bp); 262 } 263 vnode_pager_setsize(ovp, length); 264 /* 265 * Calculate index into inode's block list of 266 * last direct and indirect blocks (if any) 267 * which we want to keep. Lastblock is -1 when 268 * the file is truncated to 0. 269 */ 270 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 271 lastiblock[SINGLE] = lastblock - NDADDR; 272 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 273 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 274 nblocks = btodb(fs->fs_bsize); 275 /* 276 * Update file and block pointers on disk before we start freeing 277 * blocks. If we crash before free'ing blocks below, the blocks 278 * will be returned to the free list. lastiblock values are also 279 * normalized to -1 for calls to ffs_indirtrunc below. 280 */ 281 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); 282 for (level = TRIPLE; level >= SINGLE; level--) 283 if (lastiblock[level] < 0) { 284 oip->i_ib[level] = 0; 285 lastiblock[level] = -1; 286 } 287 for (i = NDADDR - 1; i > lastblock; i--) 288 oip->i_db[i] = 0; 289 oip->i_flag |= IN_CHANGE | IN_UPDATE; 290 error = VOP_UPDATE(ovp, &tv, &tv, ((length > 0) ? 0 : 1)); 291 if (error) 292 allerror = error; 293 /* 294 * Having written the new inode to disk, save its new configuration 295 * and put back the old block pointers long enough to process them. 296 * Note that we save the new block configuration so we can check it 297 * when we are done. 298 */ 299 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); 300 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); 301 oip->i_size = osize; 302 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA; 303 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0); 304 305 /* 306 * Indirect blocks first. 307 */ 308 indir_lbn[SINGLE] = -NDADDR; 309 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; 310 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 311 for (level = TRIPLE; level >= SINGLE; level--) { 312 bn = oip->i_ib[level]; 313 if (bn != 0) { 314 error = ffs_indirtrunc(oip, indir_lbn[level], 315 fsbtodb(fs, bn), lastiblock[level], level, &count); 316 if (error) 317 allerror = error; 318 blocksreleased += count; 319 if (lastiblock[level] < 0) { 320 oip->i_ib[level] = 0; 321 ffs_blkfree(oip, bn, fs->fs_bsize); 322 blocksreleased += nblocks; 323 } 324 } 325 if (lastiblock[level] >= 0) 326 goto done; 327 } 328 329 /* 330 * All whole direct blocks or frags. 331 */ 332 for (i = NDADDR - 1; i > lastblock; i--) { 333 register long bsize; 334 335 bn = oip->i_db[i]; 336 if (bn == 0) 337 continue; 338 oip->i_db[i] = 0; 339 bsize = blksize(fs, oip, i); 340 ffs_blkfree(oip, bn, bsize); 341 blocksreleased += btodb(bsize); 342 } 343 if (lastblock < 0) 344 goto done; 345 346 /* 347 * Finally, look for a change in size of the 348 * last direct block; release any frags. 349 */ 350 bn = oip->i_db[lastblock]; 351 if (bn != 0) { 352 long oldspace, newspace; 353 354 /* 355 * Calculate amount of space we're giving 356 * back as old block size minus new block size. 357 */ 358 oldspace = blksize(fs, oip, lastblock); 359 oip->i_size = length; 360 newspace = blksize(fs, oip, lastblock); 361 if (newspace == 0) 362 panic("ffs_truncate: newspace"); 363 if (oldspace - newspace > 0) { 364 /* 365 * Block number of space to be free'd is 366 * the old block # plus the number of frags 367 * required for the storage we're keeping. 368 */ 369 bn += numfrags(fs, newspace); 370 ffs_blkfree(oip, bn, oldspace - newspace); 371 blocksreleased += btodb(oldspace - newspace); 372 } 373 } 374 done: 375 #ifdef DIAGNOSTIC 376 for (level = SINGLE; level <= TRIPLE; level++) 377 if (newblks[NDADDR + level] != oip->i_ib[level]) 378 panic("ffs_truncate1"); 379 for (i = 0; i < NDADDR; i++) 380 if (newblks[i] != oip->i_db[i]) 381 panic("ffs_truncate2"); 382 if (length == 0 && 383 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first)) 384 panic("ffs_truncate3"); 385 #endif /* DIAGNOSTIC */ 386 /* 387 * Put back the real size. 388 */ 389 oip->i_size = length; 390 oip->i_blocks -= blocksreleased; 391 if (oip->i_blocks < 0) /* sanity */ 392 oip->i_blocks = 0; 393 oip->i_flag |= IN_CHANGE; 394 vnode_pager_setsize(ovp, length); 395 #ifdef QUOTA 396 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 397 #endif 398 return (allerror); 399 } 400 401 /* 402 * Release blocks associated with the inode ip and stored in the indirect 403 * block bn. Blocks are free'd in LIFO order up to (but not including) 404 * lastbn. If level is greater than SINGLE, the block is an indirect block 405 * and recursive calls to indirtrunc must be used to cleanse other indirect 406 * blocks. 407 * 408 * NB: triple indirect blocks are untested. 409 */ 410 static int 411 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 412 register struct inode *ip; 413 ufs_daddr_t lbn, lastbn; 414 ufs_daddr_t dbn; 415 int level; 416 long *countp; 417 { 418 register int i; 419 struct buf *bp; 420 register struct fs *fs = ip->i_fs; 421 register ufs_daddr_t *bap; 422 struct vnode *vp; 423 ufs_daddr_t *copy = NULL, nb, nlbn, last; 424 long blkcount, factor; 425 int nblocks, blocksreleased = 0; 426 int error = 0, allerror = 0; 427 428 /* 429 * Calculate index in current block of last 430 * block to be kept. -1 indicates the entire 431 * block so we need not calculate the index. 432 */ 433 factor = 1; 434 for (i = SINGLE; i < level; i++) 435 factor *= NINDIR(fs); 436 last = lastbn; 437 if (lastbn > 0) 438 last /= factor; 439 nblocks = btodb(fs->fs_bsize); 440 /* 441 * Get buffer of block pointers, zero those entries corresponding 442 * to blocks to be free'd, and update on disk copy first. Since 443 * double(triple) indirect before single(double) indirect, calls 444 * to bmap on these blocks will fail. However, we already have 445 * the on disk address, so we have to set the b_blkno field 446 * explicitly instead of letting bread do everything for us. 447 */ 448 vp = ITOV(ip); 449 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0); 450 if ((bp->b_flags & B_CACHE) == 0) { 451 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */ 452 bp->b_flags |= B_READ; 453 if (bp->b_bcount > bp->b_bufsize) 454 panic("ffs_indirtrunc: bad buffer size"); 455 bp->b_blkno = dbn; 456 vfs_busy_pages(bp, 0); 457 VOP_STRATEGY(bp); 458 error = biowait(bp); 459 } 460 if (error) { 461 brelse(bp); 462 *countp = 0; 463 return (error); 464 } 465 466 bap = (ufs_daddr_t *)bp->b_data; 467 if (lastbn != -1) { 468 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 469 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 470 bzero((caddr_t)&bap[last + 1], 471 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t)); 472 if ((vp->v_mount->mnt_flag & MNT_ASYNC) == 0) { 473 error = bwrite(bp); 474 if (error) 475 allerror = error; 476 } else { 477 bawrite(bp); 478 } 479 bap = copy; 480 } 481 482 /* 483 * Recursively free totally unused blocks. 484 */ 485 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 486 i--, nlbn += factor) { 487 nb = bap[i]; 488 if (nb == 0) 489 continue; 490 if (level > SINGLE) { 491 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 492 (ufs_daddr_t)-1, level - 1, &blkcount)) 493 allerror = error; 494 blocksreleased += blkcount; 495 } 496 ffs_blkfree(ip, nb, fs->fs_bsize); 497 blocksreleased += nblocks; 498 } 499 500 /* 501 * Recursively free last partial block. 502 */ 503 if (level > SINGLE && lastbn >= 0) { 504 last = lastbn % factor; 505 nb = bap[i]; 506 if (nb != 0) { 507 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 508 last, level - 1, &blkcount); 509 if (error) 510 allerror = error; 511 blocksreleased += blkcount; 512 } 513 } 514 if (copy != NULL) { 515 FREE(copy, M_TEMP); 516 } else { 517 bp->b_flags |= B_INVAL | B_NOCACHE; 518 brelse(bp); 519 } 520 521 *countp = blocksreleased; 522 return (allerror); 523 } 524