fix bug that can cause recursive .forward files to fail
[unix-history] / usr / src / sys / ufs / ffs / ffs_inode.c
CommitLineData
da7c5cc6 1/*
80409bdc
KB
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
7188ac27 6 *
dbb7672a 7 * @(#)ffs_inode.c 8.3 (Berkeley) %G%
da7c5cc6 8 */
5d5124a1 9
0a52434b
KB
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>
007fca48
MS
19#include <sys/trace.h>
20#include <sys/resourcevar.h>
5d5124a1 21
8847a2f7
KM
22#include <vm/vm.h>
23
0a52434b
KB
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>
c6f5111d 28
0a52434b
KB
29#include <ufs/ffs/fs.h>
30#include <ufs/ffs/ffs_extern.h>
3ebac878 31
007fca48
MS
32static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, daddr_t, int,
33 long *));
3ebac878 34
0a52434b
KB
35int
36ffs_init()
5d5124a1 37{
0a52434b 38 return (ufs_init());
5d5124a1
BJ
39}
40
a1e9dd57 41/*
dbb7672a
KB
42 * Update the access, modified, and inode change times as specified by the
43 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
44 * used to specify that the inode needs to be updated but that the times have
45 * already been set. The access and modified times are taken from the second
46 * and third parameters; the inode change time is always taken from the current
47 * time. If waitfor is set, then wait for the disk write of the inode to
48 * complete.
5d5124a1 49 */
0a52434b 50int
ebfa7d99
KM
51ffs_update(ap)
52 struct vop_update_args /* {
53 struct vnode *a_vp;
175b63e7
KB
54 struct timeval *a_access;
55 struct timeval *a_modify;
ebfa7d99
KM
56 int a_waitfor;
57 } */ *ap;
5d5124a1 58{
175b63e7 59 register struct fs *fs;
7188ac27 60 struct buf *bp;
a9013e03 61 struct inode *ip;
5d5124a1 62 struct dinode *dp;
ec67a3ce 63 register struct fs *fs;
5d5124a1 64
7eccb7cc 65 ip = VTOI(ap->a_vp);
448180e1 66 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) {
dbb7672a 67 ip->i_flag &= ~(IUPDATE | IACCESS | ICHANGE | IMODIFIED);
7188ac27 68 return (0);
448180e1 69 }
dbb7672a 70 if ((ip->i_flag & (IUPDATE | IACCESS | ICHANGE | IMODIFIED)) == 0)
a9013e03 71 return (0);
dbb7672a 72 if (ip->i_flag & IACCESS)
175b63e7 73 ip->i_atime.ts_sec = ap->a_access->tv_sec;
dbb7672a 74 if (ip->i_flag & IUPDATE) {
175b63e7 75 ip->i_mtime.ts_sec = ap->a_modify->tv_sec;
d9011ad6 76 ip->i_modrev++;
baaa0677 77 }
dbb7672a 78 if (ip->i_flag & ICHANGE)
7e11a0c9 79 ip->i_ctime.ts_sec = time.tv_sec;
dbb7672a 80 ip->i_flag &= ~(IUPDATE | IACCESS | ICHANGE | IMODIFIED);
6cb9e1e3 81 fs = ip->i_fs;
9de329b9
KM
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 */
6cb9e1e3
KM
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 */
a9013e03
KM
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 }
dbb7672a 95 dp = (struct dinode *)bp->b_data + itoo(fs, ip->i_number);
a1e9dd57 96 *dp = ip->i_din;
e1b76915 97 if (ap->a_waitfor)
7188ac27 98 return (bwrite(bp));
a9013e03 99 else {
7188ac27
KM
100 bdwrite(bp);
101 return (0);
5d5124a1
BJ
102 }
103}
104
9c03b2c0
SL
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 */
5d5124a1 108/*
035e92f1 109 * Truncate the inode oip to at most length size. Free affected disk
a1e9dd57 110 * blocks -- the blocks of the file are removed in reverse order.
5d5124a1 111 */
ebfa7d99
KM
112ffs_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;
5d5124a1 120{
406c9a0d 121 register struct vnode *ovp = ap->a_vp;
4f083fd7 122 register daddr_t lastblock;
a9013e03 123 register struct inode *oip;
d7313a2f 124 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
035e92f1 125 daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
ebfa7d99 126 off_t length = ap->a_length;
6459ebe0 127 register struct fs *fs;
28821bc5 128 struct buf *bp;
0308fc84 129 int offset, size, level;
d7313a2f 130 long count, nblocks, vflags, blocksreleased = 0;
d056f176 131 struct timeval tv;
28821bc5 132 register int i;
e038406d 133 int aflags, error, allerror;
0308fc84 134 off_t osize;
4f083fd7 135
406c9a0d 136 oip = VTOI(ovp);
d056f176 137 tv = time;
074ca965
KM
138 if (ovp->v_type == VLNK &&
139 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
e11d370a 140#ifdef DIAGNOSTIC
ebfa7d99 141 if (length != 0)
e11d370a
KM
142 panic("ffs_truncate: partial truncate of symlink");
143#endif
144 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
145 oip->i_size = 0;
dbb7672a 146 oip->i_flag |= IUPDATE | ICHANGE;
d056f176 147 return (VOP_UPDATE(ovp, &tv, &tv, 1));
e11d370a 148 }
29e4c95b 149 if (oip->i_size == length) {
dbb7672a 150 oip->i_flag |= IUPDATE | ICHANGE;
c3e9f24b 151 return (VOP_UPDATE(ovp, &tv, &tv, 0));
7b2e4f05 152 }
a3116541
KM
153#ifdef QUOTA
154 if (error = getinoquota(oip))
155 return (error);
156#endif
ebfa7d99 157 vnode_pager_setsize(ovp, (u_long)length);
a3116541
KM
158 fs = oip->i_fs;
159 osize = oip->i_size;
6459ebe0 160 /*
a3116541
KM
161 * Lengthen the size of the file. We must ensure that the
162 * last byte of the file is allocated. Since the smallest
163 * value of oszie is 0, length will be at least 1.
164 */
165 if (osize < length) {
166 offset = blkoff(fs, length - 1);
167 lbn = lblkno(fs, length - 1);
168 aflags = B_CLRBUF;
169 if (ap->a_flags & IO_SYNC)
170 aflags |= B_SYNC;
171 if (error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
172 aflags))
173 return (error);
174 oip->i_size = length;
175 (void) vnode_pager_uncache(ovp);
176 if (aflags & IO_SYNC)
177 bwrite(bp);
178 else
179 bawrite(bp);
dbb7672a 180 oip->i_flag |= IUPDATE | ICHANGE;
a3116541
KM
181 return (VOP_UPDATE(ovp, &tv, &tv, 1));
182 }
183 /*
184 * Shorten the size of the file. If the file is not being
28821bc5
KM
185 * truncated to a block boundry, the contents of the
186 * partial block following the end of the file must be
187 * zero'ed in case it ever become accessable again because
188 * of subsequent file growth.
189 */
ebfa7d99 190 offset = blkoff(fs, length);
a3116541 191 if (offset == 0) {
ebfa7d99 192 oip->i_size = length;
28821bc5 193 } else {
ebfa7d99 194 lbn = lblkno(fs, length);
e038406d 195 aflags = B_CLRBUF;
e1b76915 196 if (ap->a_flags & IO_SYNC)
e038406d 197 aflags |= B_SYNC;
a3116541
KM
198 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp,
199 aflags))
7188ac27 200 return (error);
ebfa7d99 201 oip->i_size = length;
28821bc5 202 size = blksize(fs, oip, lbn);
406c9a0d 203 (void) vnode_pager_uncache(ovp);
dbb7672a 204 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
a3116541
KM
205 allocbuf(bp, size);
206 if (aflags & IO_SYNC)
e038406d
KM
207 bwrite(bp);
208 else
035e92f1 209 bawrite(bp);
28821bc5 210 }
29e4c95b
KM
211 /*
212 * Calculate index into inode's block list of
213 * last direct and indirect blocks (if any)
214 * which we want to keep. Lastblock is -1 when
215 * the file is truncated to 0.
216 */
217 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
218 lastiblock[SINGLE] = lastblock - NDADDR;
219 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
220 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
221 nblocks = btodb(fs->fs_bsize);
28821bc5 222 /*
0a52434b
KB
223 * Update file and block pointers on disk before we start freeing
224 * blocks. If we crash before free'ing blocks below, the blocks
225 * will be returned to the free list. lastiblock values are also
226 * normalized to -1 for calls to ffs_indirtrunc below.
6459ebe0 227 */
035e92f1 228 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
9c03b2c0
SL
229 for (level = TRIPLE; level >= SINGLE; level--)
230 if (lastiblock[level] < 0) {
231 oip->i_ib[level] = 0;
232 lastiblock[level] = -1;
4f083fd7 233 }
9c03b2c0
SL
234 for (i = NDADDR - 1; i > lastblock; i--)
235 oip->i_db[i] = 0;
dbb7672a 236 oip->i_flag |= IUPDATE | ICHANGE;
d056f176 237 if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
e534e43a 238 allerror = error;
035e92f1
KM
239 /*
240 * Having written the new inode to disk, save its new configuration
241 * and put back the old block pointers long enough to process them.
242 * Note that we save the new block configuration so we can check it
243 * when we are done.
244 */
245 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
246 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
247 oip->i_size = osize;
248 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
249 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
9c03b2c0 250
6459ebe0 251 /*
9c03b2c0 252 * Indirect blocks first.
6459ebe0 253 */
d7313a2f
MS
254 indir_lbn[SINGLE] = -NDADDR;
255 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
256 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
9c03b2c0 257 for (level = TRIPLE; level >= SINGLE; level--) {
035e92f1 258 bn = oip->i_ib[level];
4f083fd7 259 if (bn != 0) {
035e92f1 260 error = ffs_indirtrunc(oip, indir_lbn[level],
007fca48 261 fsbtodb(fs, bn), lastiblock[level], level, &count);
7188ac27
KM
262 if (error)
263 allerror = error;
264 blocksreleased += count;
9c03b2c0 265 if (lastiblock[level] < 0) {
035e92f1
KM
266 oip->i_ib[level] = 0;
267 ffs_blkfree(oip, bn, fs->fs_bsize);
9c03b2c0 268 blocksreleased += nblocks;
9c03b2c0
SL
269 }
270 }
271 if (lastiblock[level] >= 0)
272 goto done;
4f083fd7 273 }
9c03b2c0 274
6459ebe0 275 /*
9c03b2c0 276 * All whole direct blocks or frags.
6459ebe0 277 */
4f083fd7 278 for (i = NDADDR - 1; i > lastblock; i--) {
0308fc84 279 register long bsize;
4f083fd7 280
035e92f1 281 bn = oip->i_db[i];
4f083fd7 282 if (bn == 0)
5d5124a1 283 continue;
035e92f1
KM
284 oip->i_db[i] = 0;
285 bsize = blksize(fs, oip, i);
286 ffs_blkfree(oip, bn, bsize);
0b355a6e 287 blocksreleased += btodb(bsize);
4f083fd7 288 }
9c03b2c0
SL
289 if (lastblock < 0)
290 goto done;
291
4f083fd7
SL
292 /*
293 * Finally, look for a change in size of the
294 * last direct block; release any frags.
295 */
035e92f1 296 bn = oip->i_db[lastblock];
9c03b2c0 297 if (bn != 0) {
0308fc84 298 long oldspace, newspace;
9c03b2c0 299
4f083fd7
SL
300 /*
301 * Calculate amount of space we're giving
302 * back as old block size minus new block size.
303 */
035e92f1
KM
304 oldspace = blksize(fs, oip, lastblock);
305 oip->i_size = length;
306 newspace = blksize(fs, oip, lastblock);
9c03b2c0
SL
307 if (newspace == 0)
308 panic("itrunc: newspace");
309 if (oldspace - newspace > 0) {
4f083fd7
SL
310 /*
311 * Block number of space to be free'd is
312 * the old block # plus the number of frags
313 * required for the storage we're keeping.
314 */
9c03b2c0 315 bn += numfrags(fs, newspace);
035e92f1 316 ffs_blkfree(oip, bn, oldspace - newspace);
08d9a8ec 317 blocksreleased += btodb(oldspace - newspace);
4f083fd7 318 }
5d5124a1 319 }
4f083fd7 320done:
aba1c8c0 321#ifdef DIAGNOSTIC
9c03b2c0 322 for (level = SINGLE; level <= TRIPLE; level++)
035e92f1 323 if (newblks[NDADDR + level] != oip->i_ib[level])
9c03b2c0
SL
324 panic("itrunc1");
325 for (i = 0; i < NDADDR; i++)
035e92f1 326 if (newblks[i] != oip->i_db[i])
9c03b2c0 327 panic("itrunc2");
aba1c8c0
KM
328 if (length == 0 &&
329 (ovp->v_dirtyblkhd.le_next || ovp->v_cleanblkhd.le_next))
330 panic("itrunc3");
331#endif /* DIAGNOSTIC */
035e92f1
KM
332 /*
333 * Put back the real size.
334 */
335 oip->i_size = length;
08d9a8ec
SL
336 oip->i_blocks -= blocksreleased;
337 if (oip->i_blocks < 0) /* sanity */
338 oip->i_blocks = 0;
dbb7672a 339 oip->i_flag |= ICHANGE;
b4567e9c 340#ifdef QUOTA
a3116541 341 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
89045c38 342#endif
7188ac27 343 return (allerror);
5d5124a1
BJ
344}
345
4f083fd7 346/*
0a52434b
KB
347 * Release blocks associated with the inode ip and stored in the indirect
348 * block bn. Blocks are free'd in LIFO order up to (but not including)
349 * lastbn. If level is greater than SINGLE, the block is an indirect block
350 * and recursive calls to indirtrunc must be used to cleanse other indirect
351 * blocks.
9c03b2c0
SL
352 *
353 * NB: triple indirect blocks are untested.
4f083fd7 354 */
0a52434b 355static int
007fca48 356ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
6459ebe0 357 register struct inode *ip;
d7313a2f 358 daddr_t lbn, lastbn;
007fca48 359 daddr_t dbn;
9c03b2c0 360 int level;
7188ac27 361 long *countp;
5d5124a1 362{
4f083fd7 363 register int i;
b30358ab 364 struct buf *bp;
9c03b2c0 365 register struct fs *fs = ip->i_fs;
b30358ab 366 register daddr_t *bap;
007fca48 367 struct vnode *vp;
d7313a2f 368 daddr_t *copy, nb, nlbn, last;
7188ac27
KM
369 long blkcount, factor;
370 int nblocks, blocksreleased = 0;
007fca48 371 int error = 0, allerror = 0;
5d5124a1 372
9c03b2c0
SL
373 /*
374 * Calculate index in current block of last
375 * block to be kept. -1 indicates the entire
376 * block so we need not calculate the index.
377 */
378 factor = 1;
379 for (i = SINGLE; i < level; i++)
380 factor *= NINDIR(fs);
4f083fd7 381 last = lastbn;
9c03b2c0
SL
382 if (lastbn > 0)
383 last /= factor;
08d9a8ec 384 nblocks = btodb(fs->fs_bsize);
9c03b2c0 385 /*
007fca48
MS
386 * Get buffer of block pointers, zero those entries corresponding
387 * to blocks to be free'd, and update on disk copy first. Since
388 * double(triple) indirect before single(double) indirect, calls
389 * to bmap on these blocks will fail. However, we already have
390 * the on disk address, so we have to set the b_blkno field
391 * explicitly instead of letting bread do everything for us.
9c03b2c0 392 */
ec67a3ce
MK
393#ifdef SECSIZE
394 bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize,
395 fs->fs_dbsize);
396#else SECSIZE
007fca48 397 vp = ITOV(ip);
646464db 398 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
007fca48
MS
399 if (bp->b_flags & (B_DONE | B_DELWRI)) {
400 /* Braces must be here in case trace evaluates to nothing. */
401 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
402 } else {
403 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
404 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
405 bp->b_flags |= B_READ;
406 if (bp->b_bcount > bp->b_bufsize)
407 panic("ffs_indirtrunc: bad buffer size");
408 bp->b_blkno = dbn;
409 VOP_STRATEGY(bp);
410 error = biowait(bp);
411 }
7188ac27 412 if (error) {
9c03b2c0 413 brelse(bp);
7188ac27
KM
414 *countp = 0;
415 return (error);
9c03b2c0 416 }
007fca48 417
dbb7672a 418 bap = (daddr_t *)bp->b_data;
b30358ab
KM
419 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
420 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
9c03b2c0
SL
421 bzero((caddr_t)&bap[last + 1],
422 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
e038406d
KM
423 if (last == -1)
424 bp->b_flags |= B_INVAL;
7188ac27
KM
425 error = bwrite(bp);
426 if (error)
427 allerror = error;
b30358ab 428 bap = copy;
4f083fd7 429
9c03b2c0
SL
430 /*
431 * Recursively free totally unused blocks.
432 */
d7313a2f 433 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
007fca48 434 i--, nlbn += factor) {
5d5124a1 435 nb = bap[i];
4f083fd7 436 if (nb == 0)
5d5124a1 437 continue;
7188ac27 438 if (level > SINGLE) {
007fca48
MS
439 if (error = ffs_indirtrunc(ip, nlbn,
440 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount))
7188ac27
KM
441 allerror = error;
442 blocksreleased += blkcount;
443 }
0308fc84 444 ffs_blkfree(ip, nb, fs->fs_bsize);
4f083fd7 445 blocksreleased += nblocks;
4f083fd7 446 }
9c03b2c0
SL
447
448 /*
449 * Recursively free last partial block.
450 */
451 if (level > SINGLE && lastbn >= 0) {
452 last = lastbn % factor;
4f083fd7 453 nb = bap[i];
7188ac27 454 if (nb != 0) {
007fca48
MS
455 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
456 last, level - 1, &blkcount))
7188ac27
KM
457 allerror = error;
458 blocksreleased += blkcount;
459 }
5d5124a1 460 }
b30358ab 461 FREE(copy, M_TEMP);
7188ac27
KM
462 *countp = blocksreleased;
463 return (allerror);
5d5124a1 464}