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