1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ffs_inode.c 7.48 (Berkeley) 04/21/92 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 20 #include <ufs/ufs/quota.h> 21 #include <ufs/ufs/inode.h> 22 #include <ufs/ufs/ufsmount.h> 23 #include <ufs/ufs/ufs_extern.h> 24 25 #include <ufs/ffs/fs.h> 26 #include <ufs/ffs/ffs_extern.h> 27 28 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, int, long *)); 29 30 extern u_long nextgennumber; 31 32 int 33 ffs_init() 34 { 35 return (ufs_init()); 36 } 37 38 /* 39 * Look up a UFS dinode number to find its incore vnode. 40 * If it is not in core, read it in from the specified device. 41 * If it is in core, wait for the lock bit to clear, then 42 * return the inode locked. Detection and handling of mount 43 * points must be done by the calling routine. 44 */ 45 ffs_vget(mntp, ino, vpp) 46 struct mount *mntp; 47 ino_t ino; 48 struct vnode **vpp; 49 { 50 register struct fs *fs; 51 register struct inode *ip; 52 struct ufsmount *ump; 53 struct buf *bp; 54 struct dinode *dp; 55 struct vnode *vp; 56 union ihead *ih; 57 dev_t dev; 58 int i, type, error; 59 60 ump = VFSTOUFS(mntp); 61 dev = ump->um_dev; 62 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 63 return (0); 64 65 /* Allocate a new vnode/inode. */ 66 if (error = getnewvnode(VT_UFS, mntp, &ffs_vnodeops, &vp)) { 67 *vpp = NULL; 68 return (error); 69 } 70 type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */ 71 MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK); 72 vp->v_data = ip; 73 ip->i_vnode = vp; 74 ip->i_flag = 0; 75 ip->i_devvp = 0; 76 ip->i_mode = 0; 77 ip->i_diroff = 0; 78 ip->i_lockf = 0; 79 ip->i_fs = fs = ump->um_fs; 80 ip->i_dev = dev; 81 ip->i_number = ino; 82 #ifdef QUOTA 83 for (i = 0; i < MAXQUOTAS; i++) 84 ip->i_dquot[i] = NODQUOT; 85 #endif 86 /* 87 * Put it onto its hash chain and lock it so that other requests for 88 * this inode will block if they arrive while we are sleeping waiting 89 * for old data structures to be purged or for the contents of the 90 * disk portion of this inode to be read. 91 */ 92 ufs_ihashins(ip); 93 94 /* Read in the disk contents for the inode, copy into the inode. */ 95 if (error = bread(ump->um_devvp, fsbtodb(fs, itod(fs, ino)), 96 (int)fs->fs_bsize, NOCRED, &bp)) { 97 /* 98 * The inode does not contain anything useful, so it would 99 * be misleading to leave it on its hash chain. It will be 100 * returned to the free list by ufs_iput(). 101 */ 102 remque(ip); 103 ip->i_forw = ip; 104 ip->i_back = ip; 105 106 /* Unlock and discard unneeded inode. */ 107 ufs_iput(ip); 108 brelse(bp); 109 *vpp = NULL; 110 return (error); 111 } 112 dp = bp->b_un.b_dino; 113 dp += itoo(fs, ino); 114 ip->i_din = *dp; 115 brelse(bp); 116 117 /* 118 * Initialize the vnode from the inode, check for aliases. 119 * Note that the underlying vnode may have changed. 120 */ 121 if (error = ufs_vinit(mntp, &ffs_specops, FFS_FIFOOPS, &vp)) { 122 ufs_iput(ip); 123 *vpp = NULL; 124 return (error); 125 } 126 /* 127 * Finish inode initialization now that aliasing has been resolved. 128 */ 129 ip->i_devvp = ump->um_devvp; 130 VREF(ip->i_devvp); 131 /* 132 * Set up a generation number for this inode if it does not 133 * already have one. This should only happen on old filesystems. 134 */ 135 if (ip->i_gen == 0) { 136 if (++nextgennumber < (u_long)time.tv_sec) 137 nextgennumber = time.tv_sec; 138 ip->i_gen = nextgennumber; 139 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 140 ip->i_flag |= IMOD; 141 } 142 *vpp = vp; 143 return (0); 144 } 145 146 /* 147 * Update the access, modified, and inode change times as specified 148 * by the IACC, IUPD, and ICHG flags respectively. The IMOD flag 149 * is used to specify that the inode needs to be updated but that 150 * the times have already been set. The access and modified times 151 * are taken from the second and third parameters; the inode change 152 * time is always taken from the current time. If waitfor is set, 153 * then wait for the disk write of the inode to complete. 154 */ 155 int 156 ffs_update(vp, ta, tm, waitfor) 157 register struct vnode *vp; 158 struct timeval *ta, *tm; 159 int waitfor; 160 { 161 struct buf *bp; 162 struct inode *ip; 163 struct dinode *dp; 164 register struct fs *fs; 165 int error; 166 167 if (vp->v_mount->mnt_flag & MNT_RDONLY) 168 return (0); 169 ip = VTOI(vp); 170 if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 171 return (0); 172 if (ip->i_flag&IACC) 173 ip->i_atime = ta->tv_sec; 174 if (ip->i_flag&IUPD) { 175 ip->i_mtime = tm->tv_sec; 176 INCRQUAD(ip->i_modrev); 177 } 178 if (ip->i_flag&ICHG) 179 ip->i_ctime = time.tv_sec; 180 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 181 182 fs = ip->i_fs; 183 if (error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), 184 (int)fs->fs_bsize, NOCRED, &bp)) { 185 brelse(bp); 186 return (error); 187 } 188 dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 189 *dp = ip->i_din; 190 if (waitfor) 191 return (bwrite(bp)); 192 else { 193 bdwrite(bp); 194 return (0); 195 } 196 } 197 198 #define SINGLE 0 /* index of single indirect block */ 199 #define DOUBLE 1 /* index of double indirect block */ 200 #define TRIPLE 2 /* index of triple indirect block */ 201 /* 202 * Truncate the inode ip to at most length size. Free affected disk 203 * blocks -- the blocks of the file are removed in reverse order. 204 * 205 * NB: triple indirect blocks are untested. 206 */ 207 ffs_truncate(ovp, length, flags, cred) 208 register struct vnode *ovp; 209 off_t length; 210 int flags; 211 struct ucred *cred; 212 { 213 register daddr_t lastblock; 214 register struct inode *oip; 215 daddr_t bn, lbn, lastiblock[NIADDR]; 216 register struct fs *fs; 217 register struct inode *ip; 218 struct buf *bp; 219 int offset, size, level; 220 long count, nblocks, blocksreleased = 0; 221 register int i; 222 int aflags, error, allerror; 223 struct inode tip; 224 off_t osize; 225 226 vnode_pager_setsize(ovp, (u_long)length); 227 oip = VTOI(ovp); 228 if (oip->i_size <= length) { 229 oip->i_flag |= ICHG|IUPD; 230 error = ffs_update(ovp, &time, &time, 1); 231 return (error); 232 } 233 /* 234 * Calculate index into inode's block list of 235 * last direct and indirect blocks (if any) 236 * which we want to keep. Lastblock is -1 when 237 * the file is truncated to 0. 238 */ 239 fs = oip->i_fs; 240 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 241 lastiblock[SINGLE] = lastblock - NDADDR; 242 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 243 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 244 nblocks = btodb(fs->fs_bsize); 245 /* 246 * Update the size of the file. If the file is not being 247 * truncated to a block boundry, the contents of the 248 * partial block following the end of the file must be 249 * zero'ed in case it ever become accessable again because 250 * of subsequent file growth. 251 */ 252 osize = oip->i_size; 253 offset = blkoff(fs, length); 254 if (offset == 0) { 255 oip->i_size = length; 256 } else { 257 lbn = lblkno(fs, length); 258 aflags = B_CLRBUF; 259 if (flags & IO_SYNC) 260 aflags |= B_SYNC; 261 #ifdef QUOTA 262 if (error = getinoquota(oip)) 263 return (error); 264 #endif 265 if (error = ffs_balloc(oip, lbn, offset, cred, &bp, aflags)) 266 return (error); 267 oip->i_size = length; 268 size = blksize(fs, oip, lbn); 269 (void) vnode_pager_uncache(ovp); 270 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 271 allocbuf(bp, size); 272 if (flags & IO_SYNC) 273 bwrite(bp); 274 else 275 bdwrite(bp); 276 } 277 /* 278 * Update file and block pointers on disk before we start freeing 279 * blocks. If we crash before free'ing blocks below, the blocks 280 * will be returned to the free list. lastiblock values are also 281 * normalized to -1 for calls to ffs_indirtrunc below. 282 */ 283 tip = *oip; 284 tip.i_size = osize; 285 for (level = TRIPLE; level >= SINGLE; level--) 286 if (lastiblock[level] < 0) { 287 oip->i_ib[level] = 0; 288 lastiblock[level] = -1; 289 } 290 for (i = NDADDR - 1; i > lastblock; i--) 291 oip->i_db[i] = 0; 292 oip->i_flag |= ICHG|IUPD; 293 vinvalbuf(ovp, (length > 0)); 294 allerror = ffs_update(ovp, &time, &time, MNT_WAIT); 295 296 /* 297 * Indirect blocks first. 298 */ 299 ip = &tip; 300 for (level = TRIPLE; level >= SINGLE; level--) { 301 bn = ip->i_ib[level]; 302 if (bn != 0) { 303 error = ffs_indirtrunc(ip, 304 bn, lastiblock[level], level, &count); 305 if (error) 306 allerror = error; 307 blocksreleased += count; 308 if (lastiblock[level] < 0) { 309 ip->i_ib[level] = 0; 310 ffs_blkfree(ip, bn, fs->fs_bsize); 311 blocksreleased += nblocks; 312 } 313 } 314 if (lastiblock[level] >= 0) 315 goto done; 316 } 317 318 /* 319 * All whole direct blocks or frags. 320 */ 321 for (i = NDADDR - 1; i > lastblock; i--) { 322 register long bsize; 323 324 bn = ip->i_db[i]; 325 if (bn == 0) 326 continue; 327 ip->i_db[i] = 0; 328 bsize = blksize(fs, ip, i); 329 ffs_blkfree(ip, bn, bsize); 330 blocksreleased += btodb(bsize); 331 } 332 if (lastblock < 0) 333 goto done; 334 335 /* 336 * Finally, look for a change in size of the 337 * last direct block; release any frags. 338 */ 339 bn = ip->i_db[lastblock]; 340 if (bn != 0) { 341 long oldspace, newspace; 342 343 /* 344 * Calculate amount of space we're giving 345 * back as old block size minus new block size. 346 */ 347 oldspace = blksize(fs, ip, lastblock); 348 ip->i_size = length; 349 newspace = blksize(fs, ip, lastblock); 350 if (newspace == 0) 351 panic("itrunc: newspace"); 352 if (oldspace - newspace > 0) { 353 /* 354 * Block number of space to be free'd is 355 * the old block # plus the number of frags 356 * required for the storage we're keeping. 357 */ 358 bn += numfrags(fs, newspace); 359 ffs_blkfree(ip, bn, oldspace - newspace); 360 blocksreleased += btodb(oldspace - newspace); 361 } 362 } 363 done: 364 /* BEGIN PARANOIA */ 365 for (level = SINGLE; level <= TRIPLE; level++) 366 if (ip->i_ib[level] != oip->i_ib[level]) 367 panic("itrunc1"); 368 for (i = 0; i < NDADDR; i++) 369 if (ip->i_db[i] != oip->i_db[i]) 370 panic("itrunc2"); 371 /* END PARANOIA */ 372 oip->i_blocks -= blocksreleased; 373 if (oip->i_blocks < 0) /* sanity */ 374 oip->i_blocks = 0; 375 oip->i_flag |= ICHG; 376 #ifdef QUOTA 377 if (!getinoquota(oip)) 378 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 379 #endif 380 return (allerror); 381 } 382 383 /* 384 * Release blocks associated with the inode ip and stored in the indirect 385 * block bn. Blocks are free'd in LIFO order up to (but not including) 386 * lastbn. If level is greater than SINGLE, the block is an indirect block 387 * and recursive calls to indirtrunc must be used to cleanse other indirect 388 * blocks. 389 * 390 * NB: triple indirect blocks are untested. 391 */ 392 static int 393 ffs_indirtrunc(ip, bn, lastbn, level, countp) 394 register struct inode *ip; 395 daddr_t bn, lastbn; 396 int level; 397 long *countp; 398 { 399 register int i; 400 struct buf *bp; 401 register struct fs *fs = ip->i_fs; 402 register daddr_t *bap; 403 daddr_t *copy, nb, last; 404 long blkcount, factor; 405 int nblocks, blocksreleased = 0; 406 int error, allerror = 0; 407 408 /* 409 * Calculate index in current block of last 410 * block to be kept. -1 indicates the entire 411 * block so we need not calculate the index. 412 */ 413 factor = 1; 414 for (i = SINGLE; i < level; i++) 415 factor *= NINDIR(fs); 416 last = lastbn; 417 if (lastbn > 0) 418 last /= factor; 419 nblocks = btodb(fs->fs_bsize); 420 /* 421 * Get buffer of block pointers, zero those 422 * entries corresponding to blocks to be free'd, 423 * and update on disk copy first. 424 */ 425 error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, 426 NOCRED, &bp); 427 if (error) { 428 brelse(bp); 429 *countp = 0; 430 return (error); 431 } 432 bap = bp->b_un.b_daddr; 433 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 434 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 435 bzero((caddr_t)&bap[last + 1], 436 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 437 if (last == -1) 438 bp->b_flags |= B_INVAL; 439 error = bwrite(bp); 440 if (error) 441 allerror = error; 442 bap = copy; 443 444 /* 445 * Recursively free totally unused blocks. 446 */ 447 for (i = NINDIR(fs) - 1; i > last; i--) { 448 nb = bap[i]; 449 if (nb == 0) 450 continue; 451 if (level > SINGLE) { 452 if (error = ffs_indirtrunc(ip, 453 nb, (daddr_t)-1, level - 1, &blkcount)) 454 allerror = error; 455 blocksreleased += blkcount; 456 } 457 ffs_blkfree(ip, nb, fs->fs_bsize); 458 blocksreleased += nblocks; 459 } 460 461 /* 462 * Recursively free last partial block. 463 */ 464 if (level > SINGLE && lastbn >= 0) { 465 last = lastbn % factor; 466 nb = bap[i]; 467 if (nb != 0) { 468 if (error = 469 ffs_indirtrunc(ip, nb, last, level - 1, &blkcount)) 470 allerror = error; 471 blocksreleased += blkcount; 472 } 473 } 474 FREE(copy, M_TEMP); 475 *countp = blocksreleased; 476 return (allerror); 477 } 478