date and time created 93/06/01 16:44:56 by bostic
[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 *
29e4c95b 7 * @(#)ffs_inode.c 7.70 (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 }
29e4c95b 148 if (oip->i_size == length) {
7b2e4f05 149 oip->i_flag |= ICHG|IUPD;
c3e9f24b 150 return (VOP_UPDATE(ovp, &tv, &tv, 0));
7b2e4f05 151 }
ebfa7d99 152 vnode_pager_setsize(ovp, (u_long)length);
6459ebe0 153 /*
28821bc5
KM
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 */
29e4c95b 160 fs = oip->i_fs;
28821bc5 161 osize = oip->i_size;
ebfa7d99 162 offset = blkoff(fs, length);
29e4c95b 163 if (offset == 0 && osize > length) {
ebfa7d99 164 oip->i_size = length;
28821bc5 165 } else {
ebfa7d99 166 lbn = lblkno(fs, length);
e038406d 167 aflags = B_CLRBUF;
e1b76915 168 if (ap->a_flags & IO_SYNC)
e038406d 169 aflags |= B_SYNC;
4b61628b
KM
170#ifdef QUOTA
171 if (error = getinoquota(oip))
172 return (error);
173#endif
e1b76915 174 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags))
7188ac27 175 return (error);
ebfa7d99 176 oip->i_size = length;
28821bc5 177 size = blksize(fs, oip, lbn);
406c9a0d 178 (void) vnode_pager_uncache(ovp);
29e4c95b
KM
179 if (osize > length) {
180 bzero(bp->b_un.b_addr + offset, (u_int)(size - offset));
181 allocbuf(bp, size);
182 }
e1b76915 183 if (ap->a_flags & IO_SYNC)
e038406d
KM
184 bwrite(bp);
185 else
035e92f1 186 bawrite(bp);
29e4c95b
KM
187 if (osize < length) {
188 oip->i_flag |= ICHG|IUPD;
189 return (VOP_UPDATE(ovp, &tv, &tv, 1));
190 }
28821bc5 191 }
29e4c95b
KM
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);
28821bc5 203 /*
0a52434b
KB
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.
6459ebe0 208 */
035e92f1 209 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
9c03b2c0
SL
210 for (level = TRIPLE; level >= SINGLE; level--)
211 if (lastiblock[level] < 0) {
212 oip->i_ib[level] = 0;
213 lastiblock[level] = -1;
4f083fd7 214 }
9c03b2c0
SL
215 for (i = NDADDR - 1; i > lastblock; i--)
216 oip->i_db[i] = 0;
9c03b2c0 217 oip->i_flag |= ICHG|IUPD;
d056f176 218 if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
e534e43a 219 allerror = error;
035e92f1
KM
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);
9c03b2c0 231
6459ebe0 232 /*
9c03b2c0 233 * Indirect blocks first.
6459ebe0 234 */
d7313a2f
MS
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;
9c03b2c0 238 for (level = TRIPLE; level >= SINGLE; level--) {
035e92f1 239 bn = oip->i_ib[level];
4f083fd7 240 if (bn != 0) {
035e92f1 241 error = ffs_indirtrunc(oip, indir_lbn[level],
007fca48 242 fsbtodb(fs, bn), lastiblock[level], level, &count);
7188ac27
KM
243 if (error)
244 allerror = error;
245 blocksreleased += count;
9c03b2c0 246 if (lastiblock[level] < 0) {
035e92f1
KM
247 oip->i_ib[level] = 0;
248 ffs_blkfree(oip, bn, fs->fs_bsize);
9c03b2c0 249 blocksreleased += nblocks;
9c03b2c0
SL
250 }
251 }
252 if (lastiblock[level] >= 0)
253 goto done;
4f083fd7 254 }
9c03b2c0 255
6459ebe0 256 /*
9c03b2c0 257 * All whole direct blocks or frags.
6459ebe0 258 */
4f083fd7 259 for (i = NDADDR - 1; i > lastblock; i--) {
0308fc84 260 register long bsize;
4f083fd7 261
035e92f1 262 bn = oip->i_db[i];
4f083fd7 263 if (bn == 0)
5d5124a1 264 continue;
035e92f1
KM
265 oip->i_db[i] = 0;
266 bsize = blksize(fs, oip, i);
267 ffs_blkfree(oip, bn, bsize);
0b355a6e 268 blocksreleased += btodb(bsize);
4f083fd7 269 }
9c03b2c0
SL
270 if (lastblock < 0)
271 goto done;
272
4f083fd7
SL
273 /*
274 * Finally, look for a change in size of the
275 * last direct block; release any frags.
276 */
035e92f1 277 bn = oip->i_db[lastblock];
9c03b2c0 278 if (bn != 0) {
0308fc84 279 long oldspace, newspace;
9c03b2c0 280
4f083fd7
SL
281 /*
282 * Calculate amount of space we're giving
283 * back as old block size minus new block size.
284 */
035e92f1
KM
285 oldspace = blksize(fs, oip, lastblock);
286 oip->i_size = length;
287 newspace = blksize(fs, oip, lastblock);
9c03b2c0
SL
288 if (newspace == 0)
289 panic("itrunc: newspace");
290 if (oldspace - newspace > 0) {
4f083fd7
SL
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 */
9c03b2c0 296 bn += numfrags(fs, newspace);
035e92f1 297 ffs_blkfree(oip, bn, oldspace - newspace);
08d9a8ec 298 blocksreleased += btodb(oldspace - newspace);
4f083fd7 299 }
5d5124a1 300 }
4f083fd7 301done:
aba1c8c0 302#ifdef DIAGNOSTIC
9c03b2c0 303 for (level = SINGLE; level <= TRIPLE; level++)
035e92f1 304 if (newblks[NDADDR + level] != oip->i_ib[level])
9c03b2c0
SL
305 panic("itrunc1");
306 for (i = 0; i < NDADDR; i++)
035e92f1 307 if (newblks[i] != oip->i_db[i])
9c03b2c0 308 panic("itrunc2");
aba1c8c0
KM
309 if (length == 0 &&
310 (ovp->v_dirtyblkhd.le_next || ovp->v_cleanblkhd.le_next))
311 panic("itrunc3");
312#endif /* DIAGNOSTIC */
035e92f1
KM
313 /*
314 * Put back the real size.
315 */
316 oip->i_size = length;
08d9a8ec
SL
317 oip->i_blocks -= blocksreleased;
318 if (oip->i_blocks < 0) /* sanity */
319 oip->i_blocks = 0;
320 oip->i_flag |= ICHG;
b4567e9c 321#ifdef QUOTA
4b61628b
KM
322 if (!getinoquota(oip))
323 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
89045c38 324#endif
7188ac27 325 return (allerror);
5d5124a1
BJ
326}
327
4f083fd7 328/*
0a52434b
KB
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.
9c03b2c0
SL
334 *
335 * NB: triple indirect blocks are untested.
4f083fd7 336 */
0a52434b 337static int
007fca48 338ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
6459ebe0 339 register struct inode *ip;
d7313a2f 340 daddr_t lbn, lastbn;
007fca48 341 daddr_t dbn;
9c03b2c0 342 int level;
7188ac27 343 long *countp;
5d5124a1 344{
4f083fd7 345 register int i;
b30358ab 346 struct buf *bp;
9c03b2c0 347 register struct fs *fs = ip->i_fs;
b30358ab 348 register daddr_t *bap;
007fca48 349 struct vnode *vp;
d7313a2f 350 daddr_t *copy, nb, nlbn, last;
7188ac27
KM
351 long blkcount, factor;
352 int nblocks, blocksreleased = 0;
007fca48 353 int error = 0, allerror = 0;
5d5124a1 354
9c03b2c0
SL
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);
4f083fd7 363 last = lastbn;
9c03b2c0
SL
364 if (lastbn > 0)
365 last /= factor;
08d9a8ec 366 nblocks = btodb(fs->fs_bsize);
9c03b2c0 367 /*
007fca48
MS
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.
9c03b2c0 374 */
ec67a3ce
MK
375#ifdef SECSIZE
376 bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize,
377 fs->fs_dbsize);
378#else SECSIZE
007fca48 379 vp = ITOV(ip);
646464db 380 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
007fca48
MS
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 }
7188ac27 394 if (error) {
9c03b2c0 395 brelse(bp);
7188ac27
KM
396 *countp = 0;
397 return (error);
9c03b2c0 398 }
007fca48 399
9c03b2c0 400 bap = bp->b_un.b_daddr;
b30358ab
KM
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);
9c03b2c0
SL
403 bzero((caddr_t)&bap[last + 1],
404 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
e038406d
KM
405 if (last == -1)
406 bp->b_flags |= B_INVAL;
7188ac27
KM
407 error = bwrite(bp);
408 if (error)
409 allerror = error;
b30358ab 410 bap = copy;
4f083fd7 411
9c03b2c0
SL
412 /*
413 * Recursively free totally unused blocks.
414 */
d7313a2f 415 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
007fca48 416 i--, nlbn += factor) {
5d5124a1 417 nb = bap[i];
4f083fd7 418 if (nb == 0)
5d5124a1 419 continue;
7188ac27 420 if (level > SINGLE) {
007fca48
MS
421 if (error = ffs_indirtrunc(ip, nlbn,
422 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount))
7188ac27
KM
423 allerror = error;
424 blocksreleased += blkcount;
425 }
0308fc84 426 ffs_blkfree(ip, nb, fs->fs_bsize);
4f083fd7 427 blocksreleased += nblocks;
4f083fd7 428 }
9c03b2c0
SL
429
430 /*
431 * Recursively free last partial block.
432 */
433 if (level > SINGLE && lastbn >= 0) {
434 last = lastbn % factor;
4f083fd7 435 nb = bap[i];
7188ac27 436 if (nb != 0) {
007fca48
MS
437 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
438 last, level - 1, &blkcount))
7188ac27
KM
439 allerror = error;
440 blocksreleased += blkcount;
441 }
5d5124a1 442 }
b30358ab 443 FREE(copy, M_TEMP);
7188ac27
KM
444 *countp = blocksreleased;
445 return (allerror);
5d5124a1 446}