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