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.47 (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) 208 register struct vnode *ovp; 209 off_t length; 210 int flags; 211 { 212 register daddr_t lastblock; 213 register struct inode *oip; 214 daddr_t bn, lbn, lastiblock[NIADDR]; 215 register struct fs *fs; 216 register struct inode *ip; 217 struct buf *bp; 218 int offset, size, level; 219 long count, nblocks, blocksreleased = 0; 220 register int i; 221 int aflags, error, allerror; 222 struct inode tip; 223 off_t osize; 224 225 vnode_pager_setsize(ovp, (u_long)length); 226 oip = VTOI(ovp); 227 if (oip->i_size <= length) { 228 oip->i_flag |= ICHG|IUPD; 229 error = ffs_update(ovp, &time, &time, 1); 230 return (error); 231 } 232 /* 233 * Calculate index into inode's block list of 234 * last direct and indirect blocks (if any) 235 * which we want to keep. Lastblock is -1 when 236 * the file is truncated to 0. 237 */ 238 fs = oip->i_fs; 239 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 240 lastiblock[SINGLE] = lastblock - NDADDR; 241 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 242 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 243 nblocks = btodb(fs->fs_bsize); 244 /* 245 * Update the size of the file. If the file is not being 246 * truncated to a block boundry, the contents of the 247 * partial block following the end of the file must be 248 * zero'ed in case it ever become accessable again because 249 * of subsequent file growth. 250 */ 251 osize = oip->i_size; 252 offset = blkoff(fs, length); 253 if (offset == 0) { 254 oip->i_size = length; 255 } else { 256 lbn = lblkno(fs, length); 257 aflags = B_CLRBUF; 258 if (flags & IO_SYNC) 259 aflags |= B_SYNC; 260 #ifdef QUOTA 261 if (error = getinoquota(oip)) 262 return (error); 263 #endif 264 if (error = ffs_balloc(oip, lbn, offset, &bp, aflags)) 265 return (error); 266 oip->i_size = length; 267 size = blksize(fs, oip, lbn); 268 (void) vnode_pager_uncache(ovp); 269 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 270 allocbuf(bp, size); 271 if (flags & IO_SYNC) 272 bwrite(bp); 273 else 274 bdwrite(bp); 275 } 276 /* 277 * Update file and block pointers on disk before we start freeing 278 * blocks. If we crash before free'ing blocks below, the blocks 279 * will be returned to the free list. lastiblock values are also 280 * normalized to -1 for calls to ffs_indirtrunc below. 281 */ 282 tip = *oip; 283 tip.i_size = osize; 284 for (level = TRIPLE; level >= SINGLE; level--) 285 if (lastiblock[level] < 0) { 286 oip->i_ib[level] = 0; 287 lastiblock[level] = -1; 288 } 289 for (i = NDADDR - 1; i > lastblock; i--) 290 oip->i_db[i] = 0; 291 oip->i_flag |= ICHG|IUPD; 292 vinvalbuf(ovp, (length > 0)); 293 allerror = ffs_update(ovp, &time, &time, MNT_WAIT); 294 295 /* 296 * Indirect blocks first. 297 */ 298 ip = &tip; 299 for (level = TRIPLE; level >= SINGLE; level--) { 300 bn = ip->i_ib[level]; 301 if (bn != 0) { 302 error = ffs_indirtrunc(ip, 303 bn, lastiblock[level], level, &count); 304 if (error) 305 allerror = error; 306 blocksreleased += count; 307 if (lastiblock[level] < 0) { 308 ip->i_ib[level] = 0; 309 ffs_blkfree(ip, bn, fs->fs_bsize); 310 blocksreleased += nblocks; 311 } 312 } 313 if (lastiblock[level] >= 0) 314 goto done; 315 } 316 317 /* 318 * All whole direct blocks or frags. 319 */ 320 for (i = NDADDR - 1; i > lastblock; i--) { 321 register long bsize; 322 323 bn = ip->i_db[i]; 324 if (bn == 0) 325 continue; 326 ip->i_db[i] = 0; 327 bsize = blksize(fs, ip, i); 328 ffs_blkfree(ip, bn, bsize); 329 blocksreleased += btodb(bsize); 330 } 331 if (lastblock < 0) 332 goto done; 333 334 /* 335 * Finally, look for a change in size of the 336 * last direct block; release any frags. 337 */ 338 bn = ip->i_db[lastblock]; 339 if (bn != 0) { 340 long oldspace, newspace; 341 342 /* 343 * Calculate amount of space we're giving 344 * back as old block size minus new block size. 345 */ 346 oldspace = blksize(fs, ip, lastblock); 347 ip->i_size = length; 348 newspace = blksize(fs, ip, lastblock); 349 if (newspace == 0) 350 panic("itrunc: newspace"); 351 if (oldspace - newspace > 0) { 352 /* 353 * Block number of space to be free'd is 354 * the old block # plus the number of frags 355 * required for the storage we're keeping. 356 */ 357 bn += numfrags(fs, newspace); 358 ffs_blkfree(ip, bn, oldspace - newspace); 359 blocksreleased += btodb(oldspace - newspace); 360 } 361 } 362 done: 363 /* BEGIN PARANOIA */ 364 for (level = SINGLE; level <= TRIPLE; level++) 365 if (ip->i_ib[level] != oip->i_ib[level]) 366 panic("itrunc1"); 367 for (i = 0; i < NDADDR; i++) 368 if (ip->i_db[i] != oip->i_db[i]) 369 panic("itrunc2"); 370 /* END PARANOIA */ 371 oip->i_blocks -= blocksreleased; 372 if (oip->i_blocks < 0) /* sanity */ 373 oip->i_blocks = 0; 374 oip->i_flag |= ICHG; 375 #ifdef QUOTA 376 if (!getinoquota(oip)) 377 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 378 #endif 379 return (allerror); 380 } 381 382 /* 383 * Release blocks associated with the inode ip and stored in the indirect 384 * block bn. Blocks are free'd in LIFO order up to (but not including) 385 * lastbn. If level is greater than SINGLE, the block is an indirect block 386 * and recursive calls to indirtrunc must be used to cleanse other indirect 387 * blocks. 388 * 389 * NB: triple indirect blocks are untested. 390 */ 391 static int 392 ffs_indirtrunc(ip, bn, lastbn, level, countp) 393 register struct inode *ip; 394 daddr_t bn, lastbn; 395 int level; 396 long *countp; 397 { 398 register int i; 399 struct buf *bp; 400 register struct fs *fs = ip->i_fs; 401 register daddr_t *bap; 402 daddr_t *copy, nb, last; 403 long blkcount, factor; 404 int nblocks, blocksreleased = 0; 405 int error, allerror = 0; 406 407 /* 408 * Calculate index in current block of last 409 * block to be kept. -1 indicates the entire 410 * block so we need not calculate the index. 411 */ 412 factor = 1; 413 for (i = SINGLE; i < level; i++) 414 factor *= NINDIR(fs); 415 last = lastbn; 416 if (lastbn > 0) 417 last /= factor; 418 nblocks = btodb(fs->fs_bsize); 419 /* 420 * Get buffer of block pointers, zero those 421 * entries corresponding to blocks to be free'd, 422 * and update on disk copy first. 423 */ 424 error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, 425 NOCRED, &bp); 426 if (error) { 427 brelse(bp); 428 *countp = 0; 429 return (error); 430 } 431 bap = bp->b_un.b_daddr; 432 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 433 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 434 bzero((caddr_t)&bap[last + 1], 435 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 436 if (last == -1) 437 bp->b_flags |= B_INVAL; 438 error = bwrite(bp); 439 if (error) 440 allerror = error; 441 bap = copy; 442 443 /* 444 * Recursively free totally unused blocks. 445 */ 446 for (i = NINDIR(fs) - 1; i > last; i--) { 447 nb = bap[i]; 448 if (nb == 0) 449 continue; 450 if (level > SINGLE) { 451 if (error = ffs_indirtrunc(ip, 452 nb, (daddr_t)-1, level - 1, &blkcount)) 453 allerror = error; 454 blocksreleased += blkcount; 455 } 456 ffs_blkfree(ip, nb, fs->fs_bsize); 457 blocksreleased += nblocks; 458 } 459 460 /* 461 * Recursively free last partial block. 462 */ 463 if (level > SINGLE && lastbn >= 0) { 464 last = lastbn % factor; 465 nb = bap[i]; 466 if (nb != 0) { 467 if (error = 468 ffs_indirtrunc(ip, nb, last, level - 1, &blkcount)) 469 allerror = error; 470 blocksreleased += blkcount; 471 } 472 } 473 FREE(copy, M_TEMP); 474 *countp = blocksreleased; 475 return (allerror); 476 } 477