M_SUPERBLK rolled into M_UFSMNT
[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 *
7ba38242 7 * @(#)ffs_inode.c 7.43 (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>
5d5124a1 19
0a52434b
KB
20#include <ufs/ufs/quota.h>
21#include <ufs/ufs/inode.h>
22#include <ufs/ufs/ufsmount.h>
23#include <ufs/ufs/ufs_extern.h>
c6f5111d 24
0a52434b
KB
25#include <ufs/ffs/fs.h>
26#include <ufs/ffs/ffs_extern.h>
3ebac878 27
0a52434b 28static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, int, long *));
3ebac878 29
0a52434b 30extern u_long nextgennumber;
2bf2d153 31
0a52434b
KB
32int
33ffs_init()
5d5124a1 34{
0a52434b 35 return (ufs_init());
5d5124a1
BJ
36}
37
3ebac878 38/*
832eaef9
KM
39 * Look up a UFS dinode number to find its incore vnode.
40 * If it is not in core, read it in from the specified device.
41 * If it is in core, wait for the lock bit to clear, then
42 * return the inode locked. Detection and handling of mount
43 * points must be done by the calling routine.
5d5124a1 44 */
a9013e03
KM
45ffs_vget(mntp, ino, vpp)
46 struct mount *mntp;
7494ef16 47 ino_t ino;
a9013e03 48 struct vnode **vpp;
5d5124a1 49{
0a52434b
KB
50 register struct fs *fs;
51 register struct inode *ip;
a9013e03 52 struct ufsmount *ump;
7188ac27 53 struct buf *bp;
1259a9f9 54 struct dinode *dp;
0a52434b 55 struct vnode *vp;
4b61628b 56 union ihead *ih;
0a52434b 57 dev_t dev;
4b61628b 58 int i, error;
2e64ab65 59
a9013e03
KM
60 ump = VFSTOUFS(mntp);
61 dev = ump->um_dev;
62 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
0a52434b
KB
63 return (0);
64
65 /* Allocate a new vnode/inode. */
66 if (error = getnewvnode(VT_UFS, mntp, &ffs_vnodeops, &vp)) {
a9013e03 67 *vpp = NULL;
7188ac27
KM
68 return (error);
69 }
0a52434b
KB
70 ip = VTOI(vp);
71 ip->i_vnode = vp;
1259a9f9
KM
72 ip->i_flag = 0;
73 ip->i_devvp = 0;
1259a9f9 74 ip->i_mode = 0;
c9ad8afc 75 ip->i_diroff = 0;
4754ee14 76 ip->i_lockf = 0;
a9013e03
KM
77 ip->i_dev = dev;
78 ip->i_number = ino;
1259a9f9 79#ifdef QUOTA
4b61628b
KM
80 for (i = 0; i < MAXQUOTAS; i++)
81 ip->i_dquot[i] = NODQUOT;
1259a9f9
KM
82#endif
83 /*
84 * Put it onto its hash chain and lock it so that other requests for
85 * this inode will block if they arrive while we are sleeping waiting
86 * for old data structures to be purged or for the contents of the
87 * disk portion of this inode to be read.
88 */
0a52434b
KB
89 ufs_ihashins(ip);
90
91 /* Read in the disk contents for the inode, copy into the inode. */
a9013e03
KM
92 fs = ump->um_fs;
93 if (error = bread(ump->um_devvp, fsbtodb(fs, itod(fs, ino)),
a937f856 94 (int)fs->fs_bsize, NOCRED, &bp)) {
bd4160ab
KM
95 /*
96 * The inode does not contain anything useful, so it would
0a52434b
KB
97 * be misleading to leave it on its hash chain. Iput() will
98 * return it to the free list.
bd4160ab
KM
99 */
100 remque(ip);
101 ip->i_forw = ip;
102 ip->i_back = ip;
0a52434b
KB
103
104 /* Unlock and discard unneeded inode. */
105 ufs_iput(ip);
7188ac27 106 brelse(bp);
a9013e03 107 *vpp = NULL;
1259a9f9 108 return (error);
7188ac27 109 }
7188ac27
KM
110 dp = bp->b_un.b_dino;
111 dp += itoo(fs, ino);
1259a9f9
KM
112 ip->i_din = *dp;
113 brelse(bp);
0a52434b 114
1259a9f9 115 /*
0a52434b
KB
116 * Initialize the vnode from the inode, check for aliases. In all
117 * cases re-init ip, the underlying vnode/inode may have changed.
1259a9f9 118 */
7ba38242 119 if (error = ufs_vinit(mntp, &ffs_specops, FFS_FIFOOPS, &vp)) {
0a52434b 120 ufs_iput(ip);
a9013e03 121 *vpp = NULL;
0a52434b 122 return (error);
7188ac27 123 }
a9013e03
KM
124 /*
125 * Finish inode initialization now that aliasing has been resolved.
126 */
0a52434b 127 ip = VTOI(vp);
a9013e03
KM
128 ip->i_fs = fs;
129 ip->i_devvp = ump->um_devvp;
130 VREF(ip->i_devvp);
0a52434b 131
afd7e202
KM
132 /*
133 * Set up a generation number for this inode if it does not
134 * already have one. This should only happen on old filesystems.
135 */
136 if (ip->i_gen == 0) {
137 if (++nextgennumber < (u_long)time.tv_sec)
138 nextgennumber = time.tv_sec;
139 ip->i_gen = nextgennumber;
82161bc8 140 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
afd7e202
KM
141 ip->i_flag |= IMOD;
142 }
a9013e03 143 *vpp = vp;
7188ac27
KM
144 return (0);
145}
3ebac878 146
a1e9dd57 147/*
832eaef9 148 * Update the access, modified, and inode change times as specified
a9013e03 149 * by the IACC, IUPD, and ICHG flags respectively. The IMOD flag
832eaef9
KM
150 * is used to specify that the inode needs to be updated but that
151 * the times have already been set. The access and modified times
152 * are taken from the second and third parameters; the inode change
153 * time is always taken from the current time. If waitfor is set,
154 * then wait for the disk write of the inode to complete.
5d5124a1 155 */
0a52434b 156int
a9013e03
KM
157ffs_update(vp, ta, tm, waitfor)
158 register struct vnode *vp;
b32450f4 159 struct timeval *ta, *tm;
7494ef16 160 int waitfor;
5d5124a1 161{
7188ac27 162 struct buf *bp;
a9013e03 163 struct inode *ip;
5d5124a1 164 struct dinode *dp;
ec67a3ce 165 register struct fs *fs;
5d5124a1 166
82161bc8 167 if (vp->v_mount->mnt_flag & MNT_RDONLY)
7188ac27 168 return (0);
a9013e03
KM
169 ip = VTOI(vp);
170 if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
171 return (0);
7188ac27
KM
172 if (ip->i_flag&IACC)
173 ip->i_atime = ta->tv_sec;
174 if (ip->i_flag&IUPD)
175 ip->i_mtime = tm->tv_sec;
176 if (ip->i_flag&ICHG)
177 ip->i_ctime = time.tv_sec;
178 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
a9013e03
KM
179
180 fs = ip->i_fs;
181 if (error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
182 (int)fs->fs_bsize, NOCRED, &bp)) {
183 brelse(bp);
184 return (error);
185 }
7188ac27 186 dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
a1e9dd57 187 *dp = ip->i_din;
a9013e03 188 if (waitfor)
7188ac27 189 return (bwrite(bp));
a9013e03 190 else {
7188ac27
KM
191 bdwrite(bp);
192 return (0);
5d5124a1
BJ
193 }
194}
195
9c03b2c0
SL
196#define SINGLE 0 /* index of single indirect block */
197#define DOUBLE 1 /* index of double indirect block */
198#define TRIPLE 2 /* index of triple indirect block */
5d5124a1 199/*
a1e9dd57
KM
200 * Truncate the inode ip to at most length size. Free affected disk
201 * blocks -- the blocks of the file are removed in reverse order.
9c03b2c0
SL
202 *
203 * NB: triple indirect blocks are untested.
5d5124a1 204 */
a9013e03
KM
205ffs_truncate(ovp, length, flags)
206 register struct vnode *ovp;
4f083fd7 207 u_long length;
e038406d 208 int flags;
5d5124a1 209{
4f083fd7 210 register daddr_t lastblock;
a9013e03 211 register struct inode *oip;
a5e62f37 212 daddr_t bn, lbn, lastiblock[NIADDR];
6459ebe0 213 register struct fs *fs;
9c03b2c0 214 register struct inode *ip;
28821bc5 215 struct buf *bp;
7188ac27
KM
216 int offset, osize, size, level;
217 long count, nblocks, blocksreleased = 0;
28821bc5 218 register int i;
e038406d 219 int aflags, error, allerror;
9c03b2c0 220 struct inode tip;
4f083fd7 221
a9013e03
KM
222 vnode_pager_setsize(ovp, length);
223 oip = VTOI(ovp);
7b2e4f05
SL
224 if (oip->i_size <= length) {
225 oip->i_flag |= ICHG|IUPD;
a9013e03 226 error = ffs_update(ovp, &time, &time, 1);
7188ac27 227 return (error);
7b2e4f05 228 }
c0bb1685 229 /*
9c03b2c0
SL
230 * Calculate index into inode's block list of
231 * last direct and indirect blocks (if any)
232 * which we want to keep. Lastblock is -1 when
233 * the file is truncated to 0.
c0bb1685 234 */
9c03b2c0 235 fs = oip->i_fs;
4f083fd7 236 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
9c03b2c0
SL
237 lastiblock[SINGLE] = lastblock - NDADDR;
238 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
239 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
08d9a8ec 240 nblocks = btodb(fs->fs_bsize);
6459ebe0 241 /*
28821bc5
KM
242 * Update the size of the file. If the file is not being
243 * truncated to a block boundry, the contents of the
244 * partial block following the end of the file must be
245 * zero'ed in case it ever become accessable again because
246 * of subsequent file growth.
247 */
248 osize = oip->i_size;
249 offset = blkoff(fs, length);
250 if (offset == 0) {
251 oip->i_size = length;
252 } else {
253 lbn = lblkno(fs, length);
e038406d
KM
254 aflags = B_CLRBUF;
255 if (flags & IO_SYNC)
256 aflags |= B_SYNC;
4b61628b
KM
257#ifdef QUOTA
258 if (error = getinoquota(oip))
259 return (error);
260#endif
0a52434b 261 if (error = ffs_balloc(oip, lbn, offset, &bp, aflags))
7188ac27 262 return (error);
28821bc5
KM
263 oip->i_size = length;
264 size = blksize(fs, oip, lbn);
8986c97c 265 (void) vnode_pager_uncache(ITOV(oip));
a5e62f37 266 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
9cf42d55 267 allocbuf(bp, size);
e038406d
KM
268 if (flags & IO_SYNC)
269 bwrite(bp);
270 else
271 bdwrite(bp);
28821bc5
KM
272 }
273 /*
0a52434b
KB
274 * Update file and block pointers on disk before we start freeing
275 * blocks. If we crash before free'ing blocks below, the blocks
276 * will be returned to the free list. lastiblock values are also
277 * normalized to -1 for calls to ffs_indirtrunc below.
6459ebe0 278 */
9c03b2c0 279 tip = *oip;
28821bc5 280 tip.i_size = osize;
9c03b2c0
SL
281 for (level = TRIPLE; level >= SINGLE; level--)
282 if (lastiblock[level] < 0) {
283 oip->i_ib[level] = 0;
284 lastiblock[level] = -1;
4f083fd7 285 }
9c03b2c0
SL
286 for (i = NDADDR - 1; i > lastblock; i--)
287 oip->i_db[i] = 0;
9c03b2c0 288 oip->i_flag |= ICHG|IUPD;
e038406d 289 vinvalbuf(ITOV(oip), (length > 0));
a9013e03 290 allerror = ffs_update(ovp, &time, &time, MNT_WAIT);
9c03b2c0 291
6459ebe0 292 /*
9c03b2c0 293 * Indirect blocks first.
6459ebe0 294 */
28821bc5 295 ip = &tip;
9c03b2c0
SL
296 for (level = TRIPLE; level >= SINGLE; level--) {
297 bn = ip->i_ib[level];
4f083fd7 298 if (bn != 0) {
0a52434b
KB
299 error = ffs_indirtrunc(ip,
300 bn, lastiblock[level], level, &count);
7188ac27
KM
301 if (error)
302 allerror = error;
303 blocksreleased += count;
9c03b2c0
SL
304 if (lastiblock[level] < 0) {
305 ip->i_ib[level] = 0;
0a52434b 306 ffs_blkfree(ip, bn, (off_t)fs->fs_bsize);
9c03b2c0 307 blocksreleased += nblocks;
9c03b2c0
SL
308 }
309 }
310 if (lastiblock[level] >= 0)
311 goto done;
4f083fd7 312 }
9c03b2c0 313
6459ebe0 314 /*
9c03b2c0 315 * All whole direct blocks or frags.
6459ebe0 316 */
4f083fd7 317 for (i = NDADDR - 1; i > lastblock; i--) {
8011f5df 318 register off_t bsize;
4f083fd7 319
6459ebe0 320 bn = ip->i_db[i];
4f083fd7 321 if (bn == 0)
5d5124a1 322 continue;
4f083fd7 323 ip->i_db[i] = 0;
0b355a6e 324 bsize = (off_t)blksize(fs, ip, i);
0a52434b 325 ffs_blkfree(ip, bn, bsize);
0b355a6e 326 blocksreleased += btodb(bsize);
4f083fd7 327 }
9c03b2c0
SL
328 if (lastblock < 0)
329 goto done;
330
4f083fd7
SL
331 /*
332 * Finally, look for a change in size of the
333 * last direct block; release any frags.
334 */
9c03b2c0
SL
335 bn = ip->i_db[lastblock];
336 if (bn != 0) {
8011f5df 337 off_t oldspace, newspace;
9c03b2c0 338
4f083fd7
SL
339 /*
340 * Calculate amount of space we're giving
341 * back as old block size minus new block size.
342 */
9c03b2c0 343 oldspace = blksize(fs, ip, lastblock);
4f083fd7 344 ip->i_size = length;
9c03b2c0
SL
345 newspace = blksize(fs, ip, lastblock);
346 if (newspace == 0)
347 panic("itrunc: newspace");
348 if (oldspace - newspace > 0) {
4f083fd7
SL
349 /*
350 * Block number of space to be free'd is
351 * the old block # plus the number of frags
352 * required for the storage we're keeping.
353 */
9c03b2c0 354 bn += numfrags(fs, newspace);
0a52434b 355 ffs_blkfree(ip, bn, oldspace - newspace);
08d9a8ec 356 blocksreleased += btodb(oldspace - newspace);
4f083fd7 357 }
5d5124a1 358 }
4f083fd7 359done:
9c03b2c0
SL
360/* BEGIN PARANOIA */
361 for (level = SINGLE; level <= TRIPLE; level++)
362 if (ip->i_ib[level] != oip->i_ib[level])
363 panic("itrunc1");
364 for (i = 0; i < NDADDR; i++)
365 if (ip->i_db[i] != oip->i_db[i])
366 panic("itrunc2");
367/* END PARANOIA */
08d9a8ec
SL
368 oip->i_blocks -= blocksreleased;
369 if (oip->i_blocks < 0) /* sanity */
370 oip->i_blocks = 0;
371 oip->i_flag |= ICHG;
b4567e9c 372#ifdef QUOTA
4b61628b
KM
373 if (!getinoquota(oip))
374 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
89045c38 375#endif
7188ac27 376 return (allerror);
5d5124a1
BJ
377}
378
4f083fd7 379/*
0a52434b
KB
380 * Release blocks associated with the inode ip and stored in the indirect
381 * block bn. Blocks are free'd in LIFO order up to (but not including)
382 * lastbn. If level is greater than SINGLE, the block is an indirect block
383 * and recursive calls to indirtrunc must be used to cleanse other indirect
384 * blocks.
9c03b2c0
SL
385 *
386 * NB: triple indirect blocks are untested.
4f083fd7 387 */
0a52434b
KB
388static int
389ffs_indirtrunc(ip, bn, lastbn, level, countp)
6459ebe0 390 register struct inode *ip;
4f083fd7 391 daddr_t bn, lastbn;
9c03b2c0 392 int level;
7188ac27 393 long *countp;
5d5124a1 394{
4f083fd7 395 register int i;
b30358ab 396 struct buf *bp;
9c03b2c0 397 register struct fs *fs = ip->i_fs;
b30358ab
KM
398 register daddr_t *bap;
399 daddr_t *copy, nb, last;
7188ac27
KM
400 long blkcount, factor;
401 int nblocks, blocksreleased = 0;
402 int error, allerror = 0;
5d5124a1 403
9c03b2c0
SL
404 /*
405 * Calculate index in current block of last
406 * block to be kept. -1 indicates the entire
407 * block so we need not calculate the index.
408 */
409 factor = 1;
410 for (i = SINGLE; i < level; i++)
411 factor *= NINDIR(fs);
4f083fd7 412 last = lastbn;
9c03b2c0
SL
413 if (lastbn > 0)
414 last /= factor;
08d9a8ec 415 nblocks = btodb(fs->fs_bsize);
9c03b2c0
SL
416 /*
417 * Get buffer of block pointers, zero those
418 * entries corresponding to blocks to be free'd,
419 * and update on disk copy first.
420 */
ec67a3ce
MK
421#ifdef SECSIZE
422 bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize,
423 fs->fs_dbsize);
424#else SECSIZE
a937f856
KM
425 error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
426 NOCRED, &bp);
7188ac27 427 if (error) {
9c03b2c0 428 brelse(bp);
7188ac27
KM
429 *countp = 0;
430 return (error);
9c03b2c0
SL
431 }
432 bap = bp->b_un.b_daddr;
b30358ab
KM
433 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
434 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
9c03b2c0
SL
435 bzero((caddr_t)&bap[last + 1],
436 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
e038406d
KM
437 if (last == -1)
438 bp->b_flags |= B_INVAL;
7188ac27
KM
439 error = bwrite(bp);
440 if (error)
441 allerror = error;
b30358ab 442 bap = copy;
4f083fd7 443
9c03b2c0
SL
444 /*
445 * Recursively free totally unused blocks.
446 */
447 for (i = NINDIR(fs) - 1; i > last; i--) {
5d5124a1 448 nb = bap[i];
4f083fd7 449 if (nb == 0)
5d5124a1 450 continue;
7188ac27 451 if (level > SINGLE) {
0a52434b
KB
452 if (error = ffs_indirtrunc(ip,
453 nb, (daddr_t)-1, level - 1, &blkcount))
7188ac27
KM
454 allerror = error;
455 blocksreleased += blkcount;
456 }
0a52434b 457 ffs_blkfree(ip, nb, (off_t)fs->fs_bsize);
4f083fd7 458 blocksreleased += nblocks;
4f083fd7 459 }
9c03b2c0
SL
460
461 /*
462 * Recursively free last partial block.
463 */
464 if (level > SINGLE && lastbn >= 0) {
465 last = lastbn % factor;
4f083fd7 466 nb = bap[i];
7188ac27 467 if (nb != 0) {
0a52434b
KB
468 if (error =
469 ffs_indirtrunc(ip, nb, last, level - 1, &blkcount))
7188ac27
KM
470 allerror = error;
471 blocksreleased += blkcount;
472 }
5d5124a1 473 }
b30358ab 474 FREE(copy, M_TEMP);
7188ac27
KM
475 *countp = blocksreleased;
476 return (allerror);
5d5124a1 477}