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