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.53 (Berkeley) 06/04/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 <vm/vm.h> 21 22 #include <ufs/ufs/quota.h> 23 #include <ufs/ufs/inode.h> 24 #include <ufs/ufs/ufsmount.h> 25 #include <ufs/ufs/ufs_extern.h> 26 27 #include <ufs/ffs/fs.h> 28 #include <ufs/ffs/ffs_extern.h> 29 30 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, int, long *)); 31 32 extern u_long nextgennumber; 33 34 int 35 ffs_init() 36 { 37 return (ufs_init()); 38 } 39 40 /* 41 * Look up a UFS dinode number to find its incore vnode. 42 * If it is not in core, read it in from the specified device. 43 * If it is in core, wait for the lock bit to clear, then 44 * return the inode locked. Detection and handling of mount 45 * points must be done by the calling routine. 46 */ 47 ffs_vget (ap) 48 struct vop_vget_args *ap; 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(ap->a_mp); 61 dev = ump->um_dev; 62 if ((*ap->a_vpp = ufs_ihashget(dev, ap->a_ino)) != NULL) 63 return (0); 64 65 /* Allocate a new vnode/inode. */ 66 if (error = getnewvnode(VT_UFS, ap->a_mp, ffs_vnodeop_p, &vp)) { 67 *ap->a_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 = ap->a_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, ap->a_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 *ap->a_vpp = NULL; 110 return (error); 111 } 112 dp = bp->b_un.b_dino; 113 dp += itoo(fs, ap->a_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(ap->a_mp, ffs_specop_p, FFS_FIFOOPS, &vp)) { 122 ufs_iput(ip); 123 *ap->a_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 /* 143 * Ensure that uid and gid are correct. This is a temporary 144 * fix until fsck has been changed to do the update. 145 */ 146 ip->i_uid = ip->i_din.di_ouid; 147 ip->i_gid = ip->i_din.di_ogid; 148 149 *ap->a_vpp = vp; 150 return (0); 151 } 152 153 /* 154 * Update the access, modified, and inode change times as specified 155 * by the IACC, IUPD, and ICHG flags respectively. The IMOD flag 156 * is used to specify that the inode needs to be updated but that 157 * the times have already been set. The access and modified times 158 * are taken from the second and third parameters; the inode change 159 * time is always taken from the current time. If waitfor is set, 160 * then wait for the disk write of the inode to complete. 161 */ 162 int 163 ffs_update (ap) 164 struct vop_update_args *ap; 165 { 166 struct buf *bp; 167 struct inode *ip; 168 struct dinode *dp; 169 register struct fs *fs; 170 int error; 171 172 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) 173 return (0); 174 ip = VTOI(ap->a_vp); 175 if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 176 return (0); 177 if (ip->i_flag&IACC) 178 ip->i_atime.tv_sec = ap->a_ta->tv_sec; 179 if (ip->i_flag&IUPD) { 180 ip->i_mtime.tv_sec = ap->a_tm->tv_sec; 181 INCRQUAD(ip->i_modrev); 182 } 183 if (ip->i_flag&ICHG) 184 ip->i_ctime.tv_sec = time.tv_sec; 185 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 186 /* 187 * Ensure that uid and gid are correct. This is a temporary 188 * fix until fsck has been changed to do the update. 189 */ 190 ip->i_din.di_ouid = ip->i_uid; 191 ip->i_din.di_ogid = ip->i_gid; 192 193 fs = ip->i_fs; 194 if (error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), 195 (int)fs->fs_bsize, NOCRED, &bp)) { 196 brelse(bp); 197 return (error); 198 } 199 dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 200 *dp = ip->i_din; 201 if (ap->a_waitfor) 202 return (bwrite(bp)); 203 else { 204 bdwrite(bp); 205 return (0); 206 } 207 } 208 209 #define SINGLE 0 /* index of single indirect block */ 210 #define DOUBLE 1 /* index of double indirect block */ 211 #define TRIPLE 2 /* index of triple indirect block */ 212 /* 213 * Truncate the inode ip to at most length size. Free affected disk 214 * blocks -- the blocks of the file are removed in reverse order. 215 * 216 * NB: triple indirect blocks are untested. 217 */ 218 ffs_truncate (ap) 219 struct vop_truncate_args *ap; 220 { 221 USES_VOP_UPDATE; 222 register struct vnode *ovp = ap->a_vp; 223 register daddr_t lastblock; 224 register struct inode *oip; 225 daddr_t bn, lbn, lastiblock[NIADDR]; 226 register struct fs *fs; 227 register struct inode *ip; 228 struct buf *bp; 229 int offset, size, level; 230 long count, nblocks, blocksreleased = 0; 231 register int i; 232 int aflags, error, allerror; 233 struct inode tip; 234 off_t osize; 235 236 vnode_pager_setsize(ovp, (u_long)ap->a_length); 237 oip = VTOI(ovp); 238 if (oip->i_size <= ap->a_length) { 239 oip->i_flag |= ICHG|IUPD; 240 error = VOP_UPDATE(ovp, &time, &time, 1); 241 return (error); 242 } 243 /* 244 * Calculate index into inode's block list of 245 * last direct and indirect blocks (if any) 246 * which we want to keep. Lastblock is -1 when 247 * the file is truncated to 0. 248 */ 249 fs = oip->i_fs; 250 lastblock = lblkno(fs, ap->a_length + fs->fs_bsize - 1) - 1; 251 lastiblock[SINGLE] = lastblock - NDADDR; 252 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 253 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 254 nblocks = btodb(fs->fs_bsize); 255 /* 256 * Update the size of the file. If the file is not being 257 * truncated to a block boundry, the contents of the 258 * partial block following the end of the file must be 259 * zero'ed in case it ever become accessable again because 260 * of subsequent file growth. 261 */ 262 osize = oip->i_size; 263 offset = blkoff(fs, ap->a_length); 264 if (offset == 0) { 265 oip->i_size = ap->a_length; 266 } else { 267 lbn = lblkno(fs, ap->a_length); 268 aflags = B_CLRBUF; 269 if (ap->a_flags & IO_SYNC) 270 aflags |= B_SYNC; 271 #ifdef QUOTA 272 if (error = getinoquota(oip)) 273 return (error); 274 #endif 275 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags)) 276 return (error); 277 oip->i_size = ap->a_length; 278 size = blksize(fs, oip, lbn); 279 (void) vnode_pager_uncache(ovp); 280 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 281 allocbuf(bp, size); 282 if (ap->a_flags & IO_SYNC) 283 bwrite(bp); 284 else 285 bdwrite(bp); 286 } 287 /* 288 * Update file and block pointers on disk before we start freeing 289 * blocks. If we crash before free'ing blocks below, the blocks 290 * will be returned to the free list. lastiblock values are also 291 * normalized to -1 for calls to ffs_indirtrunc below. 292 */ 293 tip = *oip; 294 tip.i_size = osize; 295 for (level = TRIPLE; level >= SINGLE; level--) 296 if (lastiblock[level] < 0) { 297 oip->i_ib[level] = 0; 298 lastiblock[level] = -1; 299 } 300 for (i = NDADDR - 1; i > lastblock; i--) 301 oip->i_db[i] = 0; 302 oip->i_flag |= ICHG|IUPD; 303 vinvalbuf(ovp, (ap->a_length > 0)); 304 allerror = VOP_UPDATE(ovp, &time, &time, MNT_WAIT); 305 306 /* 307 * Indirect blocks first. 308 */ 309 ip = &tip; 310 for (level = TRIPLE; level >= SINGLE; level--) { 311 bn = ip->i_ib[level]; 312 if (bn != 0) { 313 error = ffs_indirtrunc(ip, 314 bn, lastiblock[level], level, &count); 315 if (error) 316 allerror = error; 317 blocksreleased += count; 318 if (lastiblock[level] < 0) { 319 ip->i_ib[level] = 0; 320 ffs_blkfree(ip, bn, fs->fs_bsize); 321 blocksreleased += nblocks; 322 } 323 } 324 if (lastiblock[level] >= 0) 325 goto done; 326 } 327 328 /* 329 * All whole direct blocks or frags. 330 */ 331 for (i = NDADDR - 1; i > lastblock; i--) { 332 register long bsize; 333 334 bn = ip->i_db[i]; 335 if (bn == 0) 336 continue; 337 ip->i_db[i] = 0; 338 bsize = blksize(fs, ip, i); 339 ffs_blkfree(ip, bn, bsize); 340 blocksreleased += btodb(bsize); 341 } 342 if (lastblock < 0) 343 goto done; 344 345 /* 346 * Finally, look for a change in size of the 347 * last direct block; release any frags. 348 */ 349 bn = ip->i_db[lastblock]; 350 if (bn != 0) { 351 long oldspace, newspace; 352 353 /* 354 * Calculate amount of space we're giving 355 * back as old block size minus new block size. 356 */ 357 oldspace = blksize(fs, ip, lastblock); 358 ip->i_size = ap->a_length; 359 newspace = blksize(fs, ip, lastblock); 360 if (newspace == 0) 361 panic("itrunc: newspace"); 362 if (oldspace - newspace > 0) { 363 /* 364 * Block number of space to be free'd is 365 * the old block # plus the number of frags 366 * required for the storage we're keeping. 367 */ 368 bn += numfrags(fs, newspace); 369 ffs_blkfree(ip, bn, oldspace - newspace); 370 blocksreleased += btodb(oldspace - newspace); 371 } 372 } 373 done: 374 /* BEGIN PARANOIA */ 375 for (level = SINGLE; level <= TRIPLE; level++) 376 if (ip->i_ib[level] != oip->i_ib[level]) 377 panic("itrunc1"); 378 for (i = 0; i < NDADDR; i++) 379 if (ip->i_db[i] != oip->i_db[i]) 380 panic("itrunc2"); 381 /* END PARANOIA */ 382 oip->i_blocks -= blocksreleased; 383 if (oip->i_blocks < 0) /* sanity */ 384 oip->i_blocks = 0; 385 oip->i_flag |= ICHG; 386 #ifdef QUOTA 387 if (!getinoquota(oip)) 388 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 389 #endif 390 return (allerror); 391 } 392 393 /* 394 * Release blocks associated with the inode ip and stored in the indirect 395 * block bn. Blocks are free'd in LIFO order up to (but not including) 396 * lastbn. If level is greater than SINGLE, the block is an indirect block 397 * and recursive calls to indirtrunc must be used to cleanse other indirect 398 * blocks. 399 * 400 * NB: triple indirect blocks are untested. 401 */ 402 static int 403 ffs_indirtrunc(ip, bn, lastbn, level, countp) 404 register struct inode *ip; 405 daddr_t bn, lastbn; 406 int level; 407 long *countp; 408 { 409 register int i; 410 struct buf *bp; 411 register struct fs *fs = ip->i_fs; 412 register daddr_t *bap; 413 daddr_t *copy, nb, last; 414 long blkcount, factor; 415 int nblocks, blocksreleased = 0; 416 int error, allerror = 0; 417 418 /* 419 * Calculate index in current block of last 420 * block to be kept. -1 indicates the entire 421 * block so we need not calculate the index. 422 */ 423 factor = 1; 424 for (i = SINGLE; i < level; i++) 425 factor *= NINDIR(fs); 426 last = lastbn; 427 if (lastbn > 0) 428 last /= factor; 429 nblocks = btodb(fs->fs_bsize); 430 /* 431 * Get buffer of block pointers, zero those 432 * entries corresponding to blocks to be free'd, 433 * and update on disk copy first. 434 */ 435 error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, 436 NOCRED, &bp); 437 if (error) { 438 brelse(bp); 439 *countp = 0; 440 return (error); 441 } 442 bap = bp->b_un.b_daddr; 443 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 444 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 445 bzero((caddr_t)&bap[last + 1], 446 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 447 if (last == -1) 448 bp->b_flags |= B_INVAL; 449 error = bwrite(bp); 450 if (error) 451 allerror = error; 452 bap = copy; 453 454 /* 455 * Recursively free totally unused blocks. 456 */ 457 for (i = NINDIR(fs) - 1; i > last; i--) { 458 nb = bap[i]; 459 if (nb == 0) 460 continue; 461 if (level > SINGLE) { 462 if (error = ffs_indirtrunc(ip, 463 nb, (daddr_t)-1, level - 1, &blkcount)) 464 allerror = error; 465 blocksreleased += blkcount; 466 } 467 ffs_blkfree(ip, nb, fs->fs_bsize); 468 blocksreleased += nblocks; 469 } 470 471 /* 472 * Recursively free last partial block. 473 */ 474 if (level > SINGLE && lastbn >= 0) { 475 last = lastbn % factor; 476 nb = bap[i]; 477 if (nb != 0) { 478 if (error = 479 ffs_indirtrunc(ip, nb, last, level - 1, &blkcount)) 480 allerror = error; 481 blocksreleased += blkcount; 482 } 483 } 484 FREE(copy, M_TEMP); 485 *countp = blocksreleased; 486 return (allerror); 487 } 488