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