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