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