add execdir to mailer definition
[unix-history] / usr / src / sys / ufs / ffs / ffs_inode.c
CommitLineData
da7c5cc6 1/*
7188ac27
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
7188ac27 6 *
035e92f1 7 * @(#)ffs_inode.c 7.68 (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/*
832eaef9 42 * Update the access, modified, and inode change times as specified
a9013e03 43 * by the IACC, IUPD, and ICHG flags respectively. The IMOD flag
832eaef9
KM
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.
5d5124a1 49 */
0a52434b 50int
ebfa7d99
KM
51ffs_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;
5d5124a1 58{
7188ac27 59 struct buf *bp;
a9013e03 60 struct inode *ip;
5d5124a1 61 struct dinode *dp;
ec67a3ce 62 register struct fs *fs;
5d5124a1 63
7eccb7cc 64 ip = VTOI(ap->a_vp);
448180e1
KM
65 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) {
66 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
7188ac27 67 return (0);
448180e1 68 }
a9013e03
KM
69 if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
70 return (0);
7188ac27 71 if (ip->i_flag&IACC)
7e11a0c9 72 ip->i_atime.ts_sec = ap->a_ta->tv_sec;
baaa0677 73 if (ip->i_flag&IUPD) {
7e11a0c9 74 ip->i_mtime.ts_sec = ap->a_tm->tv_sec;
d9011ad6 75 ip->i_modrev++;
baaa0677 76 }
7188ac27 77 if (ip->i_flag&ICHG)
7e11a0c9 78 ip->i_ctime.ts_sec = time.tv_sec;
7188ac27 79 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
6cb9e1e3 80 fs = ip->i_fs;
9de329b9
KM
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 */
6cb9e1e3
KM
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 */
a9013e03
KM
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 }
7188ac27 94 dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
a1e9dd57 95 *dp = ip->i_din;
e1b76915 96 if (ap->a_waitfor)
7188ac27 97 return (bwrite(bp));
a9013e03 98 else {
7188ac27
KM
99 bdwrite(bp);
100 return (0);
5d5124a1
BJ
101 }
102}
103
9c03b2c0
SL
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 */
5d5124a1 107/*
035e92f1 108 * Truncate the inode oip to at most length size. Free affected disk
a1e9dd57 109 * blocks -- the blocks of the file are removed in reverse order.
5d5124a1 110 */
ebfa7d99
KM
111ffs_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;
5d5124a1 119{
406c9a0d 120 register struct vnode *ovp = ap->a_vp;
4f083fd7 121 register daddr_t lastblock;
a9013e03 122 register struct inode *oip;
d7313a2f 123 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
035e92f1 124 daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
ebfa7d99 125 off_t length = ap->a_length;
6459ebe0 126 register struct fs *fs;
28821bc5 127 struct buf *bp;
0308fc84 128 int offset, size, level;
d7313a2f 129 long count, nblocks, vflags, blocksreleased = 0;
d056f176 130 struct timeval tv;
28821bc5 131 register int i;
e038406d 132 int aflags, error, allerror;
0308fc84 133 off_t osize;
4f083fd7 134
406c9a0d 135 oip = VTOI(ovp);
d056f176 136 tv = time;
074ca965
KM
137 if (ovp->v_type == VLNK &&
138 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
e11d370a 139#ifdef DIAGNOSTIC
ebfa7d99 140 if (length != 0)
e11d370a
KM
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;
d056f176 146 return (VOP_UPDATE(ovp, &tv, &tv, 1));
e11d370a 147 }
ebfa7d99 148 if (oip->i_size <= length) {
7b2e4f05 149 oip->i_flag |= ICHG|IUPD;
d056f176 150 return (VOP_UPDATE(ovp, &tv, &tv, 1));
7b2e4f05 151 }
ebfa7d99 152 vnode_pager_setsize(ovp, (u_long)length);
c0bb1685 153 /*
9c03b2c0
SL
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.
c0bb1685 158 */
9c03b2c0 159 fs = oip->i_fs;
ebfa7d99 160 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
9c03b2c0
SL
161 lastiblock[SINGLE] = lastblock - NDADDR;
162 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
163 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
08d9a8ec 164 nblocks = btodb(fs->fs_bsize);
6459ebe0 165 /*
28821bc5
KM
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;
ebfa7d99 173 offset = blkoff(fs, length);
28821bc5 174 if (offset == 0) {
ebfa7d99 175 oip->i_size = length;
28821bc5 176 } else {
ebfa7d99 177 lbn = lblkno(fs, length);
e038406d 178 aflags = B_CLRBUF;
e1b76915 179 if (ap->a_flags & IO_SYNC)
e038406d 180 aflags |= B_SYNC;
4b61628b
KM
181#ifdef QUOTA
182 if (error = getinoquota(oip))
183 return (error);
184#endif
e1b76915 185 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags))
7188ac27 186 return (error);
ebfa7d99 187 oip->i_size = length;
28821bc5 188 size = blksize(fs, oip, lbn);
406c9a0d 189 (void) vnode_pager_uncache(ovp);
a5e62f37 190 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
9cf42d55 191 allocbuf(bp, size);
e1b76915 192 if (ap->a_flags & IO_SYNC)
e038406d
KM
193 bwrite(bp);
194 else
035e92f1 195 bawrite(bp);
28821bc5
KM
196 }
197 /*
0a52434b
KB
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.
6459ebe0 202 */
035e92f1 203 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
9c03b2c0
SL
204 for (level = TRIPLE; level >= SINGLE; level--)
205 if (lastiblock[level] < 0) {
206 oip->i_ib[level] = 0;
207 lastiblock[level] = -1;
4f083fd7 208 }
9c03b2c0
SL
209 for (i = NDADDR - 1; i > lastblock; i--)
210 oip->i_db[i] = 0;
9c03b2c0 211 oip->i_flag |= ICHG|IUPD;
d056f176 212 if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
e534e43a 213 allerror = error;
035e92f1
KM
214 /*
215 * Having written the new inode to disk, save its new configuration
216 * and put back the old block pointers long enough to process them.
217 * Note that we save the new block configuration so we can check it
218 * when we are done.
219 */
220 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
221 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
222 oip->i_size = osize;
223 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
224 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
9c03b2c0 225
6459ebe0 226 /*
9c03b2c0 227 * Indirect blocks first.
6459ebe0 228 */
d7313a2f
MS
229 indir_lbn[SINGLE] = -NDADDR;
230 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
231 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
9c03b2c0 232 for (level = TRIPLE; level >= SINGLE; level--) {
035e92f1 233 bn = oip->i_ib[level];
4f083fd7 234 if (bn != 0) {
035e92f1 235 error = ffs_indirtrunc(oip, indir_lbn[level],
007fca48 236 fsbtodb(fs, bn), lastiblock[level], level, &count);
7188ac27
KM
237 if (error)
238 allerror = error;
239 blocksreleased += count;
9c03b2c0 240 if (lastiblock[level] < 0) {
035e92f1
KM
241 oip->i_ib[level] = 0;
242 ffs_blkfree(oip, bn, fs->fs_bsize);
9c03b2c0 243 blocksreleased += nblocks;
9c03b2c0
SL
244 }
245 }
246 if (lastiblock[level] >= 0)
247 goto done;
4f083fd7 248 }
9c03b2c0 249
6459ebe0 250 /*
9c03b2c0 251 * All whole direct blocks or frags.
6459ebe0 252 */
4f083fd7 253 for (i = NDADDR - 1; i > lastblock; i--) {
0308fc84 254 register long bsize;
4f083fd7 255
035e92f1 256 bn = oip->i_db[i];
4f083fd7 257 if (bn == 0)
5d5124a1 258 continue;
035e92f1
KM
259 oip->i_db[i] = 0;
260 bsize = blksize(fs, oip, i);
261 ffs_blkfree(oip, bn, bsize);
0b355a6e 262 blocksreleased += btodb(bsize);
4f083fd7 263 }
9c03b2c0
SL
264 if (lastblock < 0)
265 goto done;
266
4f083fd7
SL
267 /*
268 * Finally, look for a change in size of the
269 * last direct block; release any frags.
270 */
035e92f1 271 bn = oip->i_db[lastblock];
9c03b2c0 272 if (bn != 0) {
0308fc84 273 long oldspace, newspace;
9c03b2c0 274
4f083fd7
SL
275 /*
276 * Calculate amount of space we're giving
277 * back as old block size minus new block size.
278 */
035e92f1
KM
279 oldspace = blksize(fs, oip, lastblock);
280 oip->i_size = length;
281 newspace = blksize(fs, oip, lastblock);
9c03b2c0
SL
282 if (newspace == 0)
283 panic("itrunc: newspace");
284 if (oldspace - newspace > 0) {
4f083fd7
SL
285 /*
286 * Block number of space to be free'd is
287 * the old block # plus the number of frags
288 * required for the storage we're keeping.
289 */
9c03b2c0 290 bn += numfrags(fs, newspace);
035e92f1 291 ffs_blkfree(oip, bn, oldspace - newspace);
08d9a8ec 292 blocksreleased += btodb(oldspace - newspace);
4f083fd7 293 }
5d5124a1 294 }
4f083fd7 295done:
aba1c8c0 296#ifdef DIAGNOSTIC
9c03b2c0 297 for (level = SINGLE; level <= TRIPLE; level++)
035e92f1 298 if (newblks[NDADDR + level] != oip->i_ib[level])
9c03b2c0
SL
299 panic("itrunc1");
300 for (i = 0; i < NDADDR; i++)
035e92f1 301 if (newblks[i] != oip->i_db[i])
9c03b2c0 302 panic("itrunc2");
aba1c8c0
KM
303 if (length == 0 &&
304 (ovp->v_dirtyblkhd.le_next || ovp->v_cleanblkhd.le_next))
305 panic("itrunc3");
306#endif /* DIAGNOSTIC */
035e92f1
KM
307 /*
308 * Put back the real size.
309 */
310 oip->i_size = length;
08d9a8ec
SL
311 oip->i_blocks -= blocksreleased;
312 if (oip->i_blocks < 0) /* sanity */
313 oip->i_blocks = 0;
314 oip->i_flag |= ICHG;
b4567e9c 315#ifdef QUOTA
4b61628b
KM
316 if (!getinoquota(oip))
317 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
89045c38 318#endif
7188ac27 319 return (allerror);
5d5124a1
BJ
320}
321
4f083fd7 322/*
0a52434b
KB
323 * Release blocks associated with the inode ip and stored in the indirect
324 * block bn. Blocks are free'd in LIFO order up to (but not including)
325 * lastbn. If level is greater than SINGLE, the block is an indirect block
326 * and recursive calls to indirtrunc must be used to cleanse other indirect
327 * blocks.
9c03b2c0
SL
328 *
329 * NB: triple indirect blocks are untested.
4f083fd7 330 */
0a52434b 331static int
007fca48 332ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
6459ebe0 333 register struct inode *ip;
d7313a2f 334 daddr_t lbn, lastbn;
007fca48 335 daddr_t dbn;
9c03b2c0 336 int level;
7188ac27 337 long *countp;
5d5124a1 338{
4f083fd7 339 register int i;
b30358ab 340 struct buf *bp;
9c03b2c0 341 register struct fs *fs = ip->i_fs;
b30358ab 342 register daddr_t *bap;
007fca48 343 struct vnode *vp;
d7313a2f 344 daddr_t *copy, nb, nlbn, last;
7188ac27
KM
345 long blkcount, factor;
346 int nblocks, blocksreleased = 0;
007fca48 347 int error = 0, allerror = 0;
5d5124a1 348
9c03b2c0
SL
349 /*
350 * Calculate index in current block of last
351 * block to be kept. -1 indicates the entire
352 * block so we need not calculate the index.
353 */
354 factor = 1;
355 for (i = SINGLE; i < level; i++)
356 factor *= NINDIR(fs);
4f083fd7 357 last = lastbn;
9c03b2c0
SL
358 if (lastbn > 0)
359 last /= factor;
08d9a8ec 360 nblocks = btodb(fs->fs_bsize);
9c03b2c0 361 /*
007fca48
MS
362 * Get buffer of block pointers, zero those entries corresponding
363 * to blocks to be free'd, and update on disk copy first. Since
364 * double(triple) indirect before single(double) indirect, calls
365 * to bmap on these blocks will fail. However, we already have
366 * the on disk address, so we have to set the b_blkno field
367 * explicitly instead of letting bread do everything for us.
9c03b2c0 368 */
ec67a3ce
MK
369#ifdef SECSIZE
370 bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize,
371 fs->fs_dbsize);
372#else SECSIZE
007fca48 373 vp = ITOV(ip);
646464db 374 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
007fca48
MS
375 if (bp->b_flags & (B_DONE | B_DELWRI)) {
376 /* Braces must be here in case trace evaluates to nothing. */
377 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
378 } else {
379 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
380 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
381 bp->b_flags |= B_READ;
382 if (bp->b_bcount > bp->b_bufsize)
383 panic("ffs_indirtrunc: bad buffer size");
384 bp->b_blkno = dbn;
385 VOP_STRATEGY(bp);
386 error = biowait(bp);
387 }
7188ac27 388 if (error) {
9c03b2c0 389 brelse(bp);
7188ac27
KM
390 *countp = 0;
391 return (error);
9c03b2c0 392 }
007fca48 393
9c03b2c0 394 bap = bp->b_un.b_daddr;
b30358ab
KM
395 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
396 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
9c03b2c0
SL
397 bzero((caddr_t)&bap[last + 1],
398 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
e038406d
KM
399 if (last == -1)
400 bp->b_flags |= B_INVAL;
7188ac27
KM
401 error = bwrite(bp);
402 if (error)
403 allerror = error;
b30358ab 404 bap = copy;
4f083fd7 405
9c03b2c0
SL
406 /*
407 * Recursively free totally unused blocks.
408 */
d7313a2f 409 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
007fca48 410 i--, nlbn += factor) {
5d5124a1 411 nb = bap[i];
4f083fd7 412 if (nb == 0)
5d5124a1 413 continue;
7188ac27 414 if (level > SINGLE) {
007fca48
MS
415 if (error = ffs_indirtrunc(ip, nlbn,
416 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount))
7188ac27
KM
417 allerror = error;
418 blocksreleased += blkcount;
419 }
0308fc84 420 ffs_blkfree(ip, nb, fs->fs_bsize);
4f083fd7 421 blocksreleased += nblocks;
4f083fd7 422 }
9c03b2c0
SL
423
424 /*
425 * Recursively free last partial block.
426 */
427 if (level > SINGLE && lastbn >= 0) {
428 last = lastbn % factor;
4f083fd7 429 nb = bap[i];
7188ac27 430 if (nb != 0) {
007fca48
MS
431 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
432 last, level - 1, &blkcount))
7188ac27
KM
433 allerror = error;
434 blocksreleased += blkcount;
435 }
5d5124a1 436 }
b30358ab 437 FREE(copy, M_TEMP);
7188ac27
KM
438 *countp = blocksreleased;
439 return (allerror);
5d5124a1 440}