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.63 (Berkeley) 10/23/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 off_t osize; 133 134 oip = VTOI(ovp); 135 tv = time; 136 if (ovp->v_type == VLNK && ovp->v_mount->mnt_maxsymlinklen > 0) { 137 #ifdef DIAGNOSTIC 138 if (length != 0) 139 panic("ffs_truncate: partial truncate of symlink"); 140 #endif 141 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); 142 oip->i_size = 0; 143 oip->i_flag |= ICHG|IUPD; 144 return (VOP_UPDATE(ovp, &tv, &tv, 1)); 145 } 146 if (oip->i_size <= length) { 147 oip->i_flag |= ICHG|IUPD; 148 return (VOP_UPDATE(ovp, &tv, &tv, 1)); 149 } 150 vnode_pager_setsize(ovp, (u_long)length); 151 /* 152 * Calculate index into inode's block list of 153 * last direct and indirect blocks (if any) 154 * which we want to keep. Lastblock is -1 when 155 * the file is truncated to 0. 156 */ 157 fs = oip->i_fs; 158 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 159 lastiblock[SINGLE] = lastblock - NDADDR; 160 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 161 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 162 nblocks = btodb(fs->fs_bsize); 163 /* 164 * Update the size of the file. If the file is not being 165 * truncated to a block boundry, the contents of the 166 * partial block following the end of the file must be 167 * zero'ed in case it ever become accessable again because 168 * of subsequent file growth. 169 */ 170 osize = oip->i_size; 171 offset = blkoff(fs, length); 172 if (offset == 0) { 173 oip->i_size = length; 174 } else { 175 lbn = lblkno(fs, length); 176 aflags = B_CLRBUF; 177 if (ap->a_flags & IO_SYNC) 178 aflags |= B_SYNC; 179 #ifdef QUOTA 180 if (error = getinoquota(oip)) 181 return (error); 182 #endif 183 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags)) 184 return (error); 185 oip->i_size = length; 186 size = blksize(fs, oip, lbn); 187 (void) vnode_pager_uncache(ovp); 188 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 189 allocbuf(bp, size); 190 if (ap->a_flags & IO_SYNC) 191 bwrite(bp); 192 else 193 bdwrite(bp); 194 } 195 /* 196 * Update file and block pointers on disk before we start freeing 197 * blocks. If we crash before free'ing blocks below, the blocks 198 * will be returned to the free list. lastiblock values are also 199 * normalized to -1 for calls to ffs_indirtrunc below. 200 */ 201 MALLOC(ip, struct inode *, sizeof(*ip), M_FFSNODE, M_WAITOK); 202 *ip = *oip; 203 ip->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 ITOV(ip)->v_data = ip; 224 for (level = TRIPLE; level >= SINGLE; level--) { 225 bn = ip->i_ib[level]; 226 if (bn != 0) { 227 error = ffs_indirtrunc(ip, indir_lbn[level], 228 fsbtodb(fs, bn), lastiblock[level], level, &count); 229 if (error) 230 allerror = error; 231 blocksreleased += count; 232 if (lastiblock[level] < 0) { 233 ip->i_ib[level] = 0; 234 ffs_blkfree(ip, bn, fs->fs_bsize); 235 blocksreleased += nblocks; 236 } 237 } 238 if (lastiblock[level] >= 0) 239 goto done; 240 } 241 242 /* 243 * All whole direct blocks or frags. 244 */ 245 for (i = NDADDR - 1; i > lastblock; i--) { 246 register long bsize; 247 248 bn = ip->i_db[i]; 249 if (bn == 0) 250 continue; 251 ip->i_db[i] = 0; 252 bsize = blksize(fs, ip, i); 253 ffs_blkfree(ip, bn, bsize); 254 blocksreleased += btodb(bsize); 255 } 256 if (lastblock < 0) 257 goto done; 258 259 /* 260 * Finally, look for a change in size of the 261 * last direct block; release any frags. 262 */ 263 bn = ip->i_db[lastblock]; 264 if (bn != 0) { 265 long oldspace, newspace; 266 267 /* 268 * Calculate amount of space we're giving 269 * back as old block size minus new block size. 270 */ 271 oldspace = blksize(fs, ip, lastblock); 272 ip->i_size = length; 273 newspace = blksize(fs, ip, lastblock); 274 if (newspace == 0) 275 panic("itrunc: newspace"); 276 if (oldspace - newspace > 0) { 277 /* 278 * Block number of space to be free'd is 279 * the old block # plus the number of frags 280 * required for the storage we're keeping. 281 */ 282 bn += numfrags(fs, newspace); 283 ffs_blkfree(ip, bn, oldspace - newspace); 284 blocksreleased += btodb(oldspace - newspace); 285 } 286 } 287 done: 288 #ifdef DIAGNOSTIC 289 for (level = SINGLE; level <= TRIPLE; level++) 290 if (ip->i_ib[level] != oip->i_ib[level]) 291 panic("itrunc1"); 292 for (i = 0; i < NDADDR; i++) 293 if (ip->i_db[i] != oip->i_db[i]) 294 panic("itrunc2"); 295 if (length == 0 && 296 (ovp->v_dirtyblkhd.le_next || ovp->v_cleanblkhd.le_next)) 297 panic("itrunc3"); 298 #endif /* DIAGNOSTIC */ 299 ITOV(ip)->v_data = oip; 300 FREE(ip, M_FFSNODE); 301 oip->i_blocks -= blocksreleased; 302 if (oip->i_blocks < 0) /* sanity */ 303 oip->i_blocks = 0; 304 oip->i_flag |= ICHG; 305 #ifdef QUOTA 306 if (!getinoquota(oip)) 307 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 308 #endif 309 return (allerror); 310 } 311 312 /* 313 * Release blocks associated with the inode ip and stored in the indirect 314 * block bn. Blocks are free'd in LIFO order up to (but not including) 315 * lastbn. If level is greater than SINGLE, the block is an indirect block 316 * and recursive calls to indirtrunc must be used to cleanse other indirect 317 * blocks. 318 * 319 * NB: triple indirect blocks are untested. 320 */ 321 static int 322 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 323 register struct inode *ip; 324 daddr_t lbn, lastbn; 325 daddr_t dbn; 326 int level; 327 long *countp; 328 { 329 register int i; 330 struct buf *bp; 331 register struct fs *fs = ip->i_fs; 332 register daddr_t *bap; 333 struct vnode *vp; 334 daddr_t *copy, nb, nlbn, last; 335 long blkcount, factor; 336 int nblocks, blocksreleased = 0; 337 int error = 0, allerror = 0; 338 339 /* 340 * Calculate index in current block of last 341 * block to be kept. -1 indicates the entire 342 * block so we need not calculate the index. 343 */ 344 factor = 1; 345 for (i = SINGLE; i < level; i++) 346 factor *= NINDIR(fs); 347 last = lastbn; 348 if (lastbn > 0) 349 last /= factor; 350 nblocks = btodb(fs->fs_bsize); 351 /* 352 * Get buffer of block pointers, zero those entries corresponding 353 * to blocks to be free'd, and update on disk copy first. Since 354 * double(triple) indirect before single(double) indirect, calls 355 * to bmap on these blocks will fail. However, we already have 356 * the on disk address, so we have to set the b_blkno field 357 * explicitly instead of letting bread do everything for us. 358 */ 359 vp = ITOV(ip); 360 bp = getblk(vp, lbn, (int)fs->fs_bsize); 361 if (bp->b_flags & (B_DONE | B_DELWRI)) { 362 /* Braces must be here in case trace evaluates to nothing. */ 363 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn); 364 } else { 365 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn); 366 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */ 367 bp->b_flags |= B_READ; 368 if (bp->b_bcount > bp->b_bufsize) 369 panic("ffs_indirtrunc: bad buffer size"); 370 bp->b_blkno = dbn; 371 VOP_STRATEGY(bp); 372 error = biowait(bp); 373 } 374 if (error) { 375 brelse(bp); 376 *countp = 0; 377 return (error); 378 } 379 380 bap = bp->b_un.b_daddr; 381 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 382 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 383 bzero((caddr_t)&bap[last + 1], 384 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 385 if (last == -1) 386 bp->b_flags |= B_INVAL; 387 error = bwrite(bp); 388 if (error) 389 allerror = error; 390 bap = copy; 391 392 /* 393 * Recursively free totally unused blocks. 394 */ 395 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 396 i--, nlbn += factor) { 397 nb = bap[i]; 398 if (nb == 0) 399 continue; 400 if (level > SINGLE) { 401 if (error = ffs_indirtrunc(ip, nlbn, 402 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount)) 403 allerror = error; 404 blocksreleased += blkcount; 405 } 406 ffs_blkfree(ip, nb, fs->fs_bsize); 407 blocksreleased += nblocks; 408 } 409 410 /* 411 * Recursively free last partial block. 412 */ 413 if (level > SINGLE && lastbn >= 0) { 414 last = lastbn % factor; 415 nb = bap[i]; 416 if (nb != 0) { 417 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 418 last, level - 1, &blkcount)) 419 allerror = error; 420 blocksreleased += blkcount; 421 } 422 } 423 FREE(copy, M_TEMP); 424 *countp = blocksreleased; 425 return (allerror); 426 } 427