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