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