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