update for new VM
[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 *
8986c97c 7 * @(#)ffs_inode.c 7.36 (Berkeley) %G%
da7c5cc6 8 */
5d5124a1 9
94368568
JB
10#include "param.h"
11#include "systm.h"
12#include "mount.h"
94368568 13#include "user.h"
71c4cb8d 14#include "proc.h"
7188ac27 15#include "file.h"
94368568 16#include "buf.h"
0b355a6e 17#include "cmap.h"
7188ac27 18#include "vnode.h"
4b61628b 19#include "../ufs/quota.h"
7188ac27
KM
20#include "../ufs/inode.h"
21#include "../ufs/fs.h"
22#include "../ufs/ufsmount.h"
94368568 23#include "kernel.h"
b30358ab 24#include "malloc.h"
5d5124a1 25
c22c66ff 26#define INOHSZ 512
3ebac878
RE
27#if ((INOHSZ&(INOHSZ-1)) == 0)
28#define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1))
29#else
a3a9487d 30#define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ)
3ebac878
RE
31#endif
32
a1e9dd57 33union ihead {
3ebac878
RE
34 union ihead *ih_head[2];
35 struct inode *ih_chain[2];
36} ihead[INOHSZ];
37
2bf2d153
KM
38int prtactive; /* 1 => print out reclaim of active vnodes */
39
5d5124a1 40/*
a1e9dd57 41 * Initialize hash links for inodes.
5d5124a1 42 */
1259a9f9 43ufs_init()
5d5124a1
BJ
44{
45 register int i;
a1e9dd57 46 register union ihead *ih = ihead;
5d5124a1 47
ff4fb102 48#ifndef lint
a1e9dd57
KM
49 if (VN_MAXPRIVATE < sizeof(struct inode))
50 panic("ihinit: too small");
ff4fb102 51#endif /* not lint */
3ebac878
RE
52 for (i = INOHSZ; --i >= 0; ih++) {
53 ih->ih_head[0] = ih;
54 ih->ih_head[1] = ih;
55 }
4b61628b
KM
56#ifdef QUOTA
57 dqinit();
58#endif /* QUOTA */
5d5124a1
BJ
59}
60
3ebac878 61/*
7188ac27 62 * Look up an vnode/inode by device,inumber.
5d5124a1
BJ
63 * If it is in core (in the inode structure),
64 * honor the locking protocol.
65 * If it is not in core, read it in from the
66 * specified device.
7188ac27 67 * Callers must check for mount points!!
5d5124a1
BJ
68 * In all cases, a pointer to a locked
69 * inode structure is returned.
5d5124a1 70 */
7188ac27
KM
71iget(xp, ino, ipp)
72 struct inode *xp;
7494ef16 73 ino_t ino;
7188ac27 74 struct inode **ipp;
5d5124a1 75{
7188ac27
KM
76 dev_t dev = xp->i_dev;
77 struct mount *mntp = ITOV(xp)->v_mount;
78 register struct fs *fs = VFSTOUFS(mntp)->um_fs;
1259a9f9 79 extern struct vnodeops ufs_vnodeops, spec_inodeops;
7188ac27
KM
80 register struct inode *ip, *iq;
81 register struct vnode *vp;
1259a9f9 82 struct vnode *nvp;
7188ac27 83 struct buf *bp;
1259a9f9 84 struct dinode *dp;
4b61628b
KM
85 union ihead *ih;
86 int i, error;
2e64ab65 87
3ebac878 88 ih = &ihead[INOHASH(dev, ino)];
1259a9f9 89loop:
a1e9dd57
KM
90 for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) {
91 if (ino != ip->i_number || dev != ip->i_dev)
92 continue;
a1e9dd57
KM
93 if ((ip->i_flag&ILOCKED) != 0) {
94 ip->i_flag |= IWANT;
95 sleep((caddr_t)ip, PINOD);
96 goto loop;
7188ac27 97 }
1259a9f9
KM
98 if (vget(ITOV(ip)))
99 goto loop;
a1e9dd57
KM
100 *ipp = ip;
101 return(0);
102 }
1259a9f9
KM
103 /*
104 * Allocate a new inode.
105 */
106 if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) {
7188ac27
KM
107 *ipp = 0;
108 return (error);
109 }
1259a9f9
KM
110 ip = VTOI(nvp);
111 ip->i_vnode = nvp;
112 ip->i_flag = 0;
113 ip->i_devvp = 0;
1259a9f9 114 ip->i_mode = 0;
c9ad8afc 115 ip->i_diroff = 0;
1259a9f9 116#ifdef QUOTA
4b61628b
KM
117 for (i = 0; i < MAXQUOTAS; i++)
118 ip->i_dquot[i] = NODQUOT;
1259a9f9
KM
119#endif
120 /*
121 * Put it onto its hash chain and lock it so that other requests for
122 * this inode will block if they arrive while we are sleeping waiting
123 * for old data structures to be purged or for the contents of the
124 * disk portion of this inode to be read.
125 */
126 ip->i_dev = dev;
127 ip->i_number = ino;
128 insque(ip, ih);
129 ILOCK(ip);
7188ac27
KM
130 /*
131 * Read in the disk contents for the inode.
132 */
133 if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
a937f856 134 (int)fs->fs_bsize, NOCRED, &bp)) {
bd4160ab
KM
135 /*
136 * The inode does not contain anything useful, so it would
137 * be misleading to leave it on its hash chain.
138 * Iput() will take care of putting it back on the free list.
139 */
140 remque(ip);
141 ip->i_forw = ip;
142 ip->i_back = ip;
7188ac27 143 /*
a1e9dd57 144 * Unlock and discard unneeded inode.
7188ac27 145 */
1259a9f9 146 iput(ip);
7188ac27
KM
147 brelse(bp);
148 *ipp = 0;
1259a9f9 149 return (error);
7188ac27 150 }
7188ac27
KM
151 dp = bp->b_un.b_dino;
152 dp += itoo(fs, ino);
1259a9f9
KM
153 ip->i_din = *dp;
154 brelse(bp);
155 /*
156 * Initialize the associated vnode
157 */
158 vp = ITOV(ip);
159 vp->v_type = IFTOVT(ip->i_mode);
4c7b0b09
KM
160 if (vp->v_type == VFIFO) {
161#ifdef FIFO
162 extern struct vnodeops fifo_inodeops;
163 vp->v_op = &fifo_inodeops;
164#else
165 iput(ip);
166 *ipp = 0;
167 return (EOPNOTSUPP);
168#endif /* FIFO */
169 }
1259a9f9 170 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1259a9f9 171 vp->v_op = &spec_inodeops;
43fc727e 172 if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
7188ac27 173 /*
1259a9f9 174 * Reinitialize aliased inode.
7188ac27 175 */
1259a9f9
KM
176 vp = nvp;
177 iq = VTOI(vp);
178 iq->i_vnode = vp;
a97f9198 179 iq->i_flag = 0;
1259a9f9
KM
180 ILOCK(iq);
181 iq->i_din = ip->i_din;
182 iq->i_dev = dev;
183 iq->i_number = ino;
184 insque(iq, ih);
7188ac27 185 /*
1259a9f9 186 * Discard unneeded vnode
7188ac27 187 */
1259a9f9
KM
188 ip->i_mode = 0;
189 iput(ip);
7188ac27 190 ip = iq;
5d5124a1 191 }
7188ac27 192 }
1259a9f9
KM
193 if (ino == ROOTINO)
194 vp->v_flag |= VROOT;
7188ac27
KM
195 /*
196 * Finish inode initialization.
197 */
198 ip->i_fs = fs;
199 ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
8fe1c702 200 VREF(ip->i_devvp);
afd7e202
KM
201 /*
202 * Set up a generation number for this inode if it does not
203 * already have one. This should only happen on old filesystems.
204 */
205 if (ip->i_gen == 0) {
206 if (++nextgennumber < (u_long)time.tv_sec)
207 nextgennumber = time.tv_sec;
208 ip->i_gen = nextgennumber;
82161bc8 209 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
afd7e202
KM
210 ip->i_flag |= IMOD;
211 }
7188ac27
KM
212 *ipp = ip;
213 return (0);
214}
3ebac878 215
5d5124a1 216/*
a1e9dd57 217 * Unlock and decrement the reference count of an inode structure.
5d5124a1
BJ
218 */
219iput(ip)
7494ef16 220 register struct inode *ip;
5d5124a1 221{
ff56f48a 222
5c2ba954 223 if ((ip->i_flag & ILOCKED) == 0)
ff56f48a 224 panic("iput");
a388503d 225 IUNLOCK(ip);
7188ac27 226 vrele(ITOV(ip));
ff56f48a
KM
227}
228
a1e9dd57
KM
229/*
230 * Last reference to an inode, write the inode out and if necessary,
231 * truncate and deallocate the file.
232 */
7188ac27
KM
233ufs_inactive(vp)
234 struct vnode *vp;
ff56f48a 235{
7188ac27 236 register struct inode *ip = VTOI(vp);
a1e9dd57 237 int mode, error = 0;
7188ac27 238
0811b508 239 if (prtactive && vp->v_usecount != 0)
e038406d 240 vprint("ufs_inactive: pushing active", vp);
b5ea418e
KM
241 /*
242 * Get rid of inodes related to stale file handles.
243 */
1259a9f9 244 if (ip->i_mode == 0) {
e038406d
KM
245 if ((vp->v_flag & VXLOCK) == 0)
246 vgone(vp);
1259a9f9
KM
247 return (0);
248 }
aed86454 249 ILOCK(ip);
82161bc8 250 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
4b61628b
KM
251#ifdef QUOTA
252 if (!getinoquota(ip))
253 (void) chkiq(ip, -1, NOCRED, 0);
254#endif
e038406d 255 error = itrunc(ip, (u_long)0, 0);
7188ac27
KM
256 mode = ip->i_mode;
257 ip->i_mode = 0;
e9cacd5b 258 ip->i_rdev = 0;
7188ac27
KM
259 ip->i_flag |= IUPD|ICHG;
260 ifree(ip, ip->i_number, mode);
7188ac27
KM
261 }
262 IUPDAT(ip, &time, &time, 0);
d11dbec7
KM
263 IUNLOCK(ip);
264 ip->i_flag = 0;
7188ac27 265 /*
a1e9dd57
KM
266 * If we are done with the inode, reclaim it
267 * so that it can be reused immediately.
7188ac27 268 */
4b61628b 269 if (vp->v_usecount == 0 && ip->i_mode == 0)
d11dbec7 270 vgone(vp);
7188ac27 271 return (error);
5d5124a1
BJ
272}
273
274/*
a1e9dd57
KM
275 * Reclaim an inode so that it can be used for other purposes.
276 */
277ufs_reclaim(vp)
278 register struct vnode *vp;
279{
ff4fb102 280 register struct inode *ip = VTOI(vp);
4b61628b 281 int i;
a1e9dd57 282
0811b508 283 if (prtactive && vp->v_usecount != 0)
e038406d 284 vprint("ufs_reclaim: pushing active", vp);
a1e9dd57
KM
285 /*
286 * Remove the inode from its hash chain.
287 */
288 remque(ip);
289 ip->i_forw = ip;
290 ip->i_back = ip;
291 /*
292 * Purge old data structures associated with the inode.
293 */
294 cache_purge(vp);
295 if (ip->i_devvp) {
296 vrele(ip->i_devvp);
297 ip->i_devvp = 0;
298 }
299#ifdef QUOTA
4b61628b
KM
300 for (i = 0; i < MAXQUOTAS; i++) {
301 if (ip->i_dquot[i] != NODQUOT) {
302 dqrele(vp, ip->i_dquot[i]);
303 ip->i_dquot[i] = NODQUOT;
304 }
305 }
a1e9dd57 306#endif
a1e9dd57
KM
307 ip->i_flag = 0;
308 return (0);
309}
310
311/*
312 * Check accessed and update flags on an inode structure.
313 * If any is on, update the inode with the current time.
314 * If waitfor is given, then must ensure I/O order,
315 * so wait for write to complete.
5d5124a1 316 */
c0bb1685 317iupdat(ip, ta, tm, waitfor)
7494ef16 318 register struct inode *ip;
b32450f4 319 struct timeval *ta, *tm;
7494ef16 320 int waitfor;
5d5124a1 321{
7188ac27
KM
322 struct buf *bp;
323 struct vnode *vp = ITOV(ip);
5d5124a1 324 struct dinode *dp;
ec67a3ce 325 register struct fs *fs;
5d5124a1 326
ec67a3ce 327 fs = ip->i_fs;
7188ac27
KM
328 if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
329 return (0);
82161bc8 330 if (vp->v_mount->mnt_flag & MNT_RDONLY)
7188ac27
KM
331 return (0);
332 error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
a937f856 333 (int)fs->fs_bsize, NOCRED, &bp);
7188ac27
KM
334 if (error) {
335 brelse(bp);
336 return (error);
337 }
338 if (ip->i_flag&IACC)
339 ip->i_atime = ta->tv_sec;
340 if (ip->i_flag&IUPD)
341 ip->i_mtime = tm->tv_sec;
342 if (ip->i_flag&ICHG)
343 ip->i_ctime = time.tv_sec;
344 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
345 dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
a1e9dd57 346 *dp = ip->i_din;
7188ac27
KM
347 if (waitfor) {
348 return (bwrite(bp));
349 } else {
350 bdwrite(bp);
351 return (0);
5d5124a1
BJ
352 }
353}
354
9c03b2c0
SL
355#define SINGLE 0 /* index of single indirect block */
356#define DOUBLE 1 /* index of double indirect block */
357#define TRIPLE 2 /* index of triple indirect block */
5d5124a1 358/*
a1e9dd57
KM
359 * Truncate the inode ip to at most length size. Free affected disk
360 * blocks -- the blocks of the file are removed in reverse order.
9c03b2c0
SL
361 *
362 * NB: triple indirect blocks are untested.
5d5124a1 363 */
e038406d 364itrunc(oip, length, flags)
28821bc5 365 register struct inode *oip;
4f083fd7 366 u_long length;
e038406d 367 int flags;
5d5124a1 368{
4f083fd7 369 register daddr_t lastblock;
a5e62f37 370 daddr_t bn, lbn, lastiblock[NIADDR];
6459ebe0 371 register struct fs *fs;
9c03b2c0 372 register struct inode *ip;
28821bc5 373 struct buf *bp;
7188ac27
KM
374 int offset, osize, size, level;
375 long count, nblocks, blocksreleased = 0;
28821bc5 376 register int i;
e038406d 377 int aflags, error, allerror;
9c03b2c0 378 struct inode tip;
4f083fd7 379
8986c97c 380 vnode_pager_setsize(ITOV(oip), length);
7b2e4f05
SL
381 if (oip->i_size <= length) {
382 oip->i_flag |= ICHG|IUPD;
7188ac27
KM
383 error = iupdat(oip, &time, &time, 1);
384 return (error);
7b2e4f05 385 }
c0bb1685 386 /*
9c03b2c0
SL
387 * Calculate index into inode's block list of
388 * last direct and indirect blocks (if any)
389 * which we want to keep. Lastblock is -1 when
390 * the file is truncated to 0.
c0bb1685 391 */
9c03b2c0 392 fs = oip->i_fs;
4f083fd7 393 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
9c03b2c0
SL
394 lastiblock[SINGLE] = lastblock - NDADDR;
395 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
396 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
08d9a8ec 397 nblocks = btodb(fs->fs_bsize);
6459ebe0 398 /*
28821bc5
KM
399 * Update the size of the file. If the file is not being
400 * truncated to a block boundry, the contents of the
401 * partial block following the end of the file must be
402 * zero'ed in case it ever become accessable again because
403 * of subsequent file growth.
404 */
405 osize = oip->i_size;
406 offset = blkoff(fs, length);
407 if (offset == 0) {
408 oip->i_size = length;
409 } else {
410 lbn = lblkno(fs, length);
e038406d
KM
411 aflags = B_CLRBUF;
412 if (flags & IO_SYNC)
413 aflags |= B_SYNC;
4b61628b
KM
414#ifdef QUOTA
415 if (error = getinoquota(oip))
416 return (error);
417#endif
e038406d 418 if (error = balloc(oip, lbn, offset, &bp, aflags))
7188ac27 419 return (error);
28821bc5
KM
420 oip->i_size = length;
421 size = blksize(fs, oip, lbn);
8986c97c 422 (void) vnode_pager_uncache(ITOV(oip));
a5e62f37 423 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
9cf42d55 424 allocbuf(bp, size);
e038406d
KM
425 if (flags & IO_SYNC)
426 bwrite(bp);
427 else
428 bdwrite(bp);
28821bc5
KM
429 }
430 /*
431 * Update file and block pointers
9c03b2c0
SL
432 * on disk before we start freeing blocks.
433 * If we crash before free'ing blocks below,
434 * the blocks will be returned to the free list.
435 * lastiblock values are also normalized to -1
436 * for calls to indirtrunc below.
6459ebe0 437 */
9c03b2c0 438 tip = *oip;
28821bc5 439 tip.i_size = osize;
9c03b2c0
SL
440 for (level = TRIPLE; level >= SINGLE; level--)
441 if (lastiblock[level] < 0) {
442 oip->i_ib[level] = 0;
443 lastiblock[level] = -1;
4f083fd7 444 }
9c03b2c0
SL
445 for (i = NDADDR - 1; i > lastblock; i--)
446 oip->i_db[i] = 0;
9c03b2c0 447 oip->i_flag |= ICHG|IUPD;
e038406d 448 vinvalbuf(ITOV(oip), (length > 0));
2fc1f182 449 allerror = iupdat(oip, &time, &time, MNT_WAIT);
9c03b2c0 450
6459ebe0 451 /*
9c03b2c0 452 * Indirect blocks first.
6459ebe0 453 */
28821bc5 454 ip = &tip;
9c03b2c0
SL
455 for (level = TRIPLE; level >= SINGLE; level--) {
456 bn = ip->i_ib[level];
4f083fd7 457 if (bn != 0) {
7188ac27
KM
458 error = indirtrunc(ip, bn, lastiblock[level], level,
459 &count);
460 if (error)
461 allerror = error;
462 blocksreleased += count;
9c03b2c0
SL
463 if (lastiblock[level] < 0) {
464 ip->i_ib[level] = 0;
ced3a252 465 blkfree(ip, bn, (off_t)fs->fs_bsize);
9c03b2c0 466 blocksreleased += nblocks;
9c03b2c0
SL
467 }
468 }
469 if (lastiblock[level] >= 0)
470 goto done;
4f083fd7 471 }
9c03b2c0 472
6459ebe0 473 /*
9c03b2c0 474 * All whole direct blocks or frags.
6459ebe0 475 */
4f083fd7 476 for (i = NDADDR - 1; i > lastblock; i--) {
8011f5df 477 register off_t bsize;
4f083fd7 478
6459ebe0 479 bn = ip->i_db[i];
4f083fd7 480 if (bn == 0)
5d5124a1 481 continue;
4f083fd7 482 ip->i_db[i] = 0;
0b355a6e 483 bsize = (off_t)blksize(fs, ip, i);
ced3a252 484 blkfree(ip, bn, bsize);
0b355a6e 485 blocksreleased += btodb(bsize);
4f083fd7 486 }
9c03b2c0
SL
487 if (lastblock < 0)
488 goto done;
489
4f083fd7
SL
490 /*
491 * Finally, look for a change in size of the
492 * last direct block; release any frags.
493 */
9c03b2c0
SL
494 bn = ip->i_db[lastblock];
495 if (bn != 0) {
8011f5df 496 off_t oldspace, newspace;
9c03b2c0 497
4f083fd7
SL
498 /*
499 * Calculate amount of space we're giving
500 * back as old block size minus new block size.
501 */
9c03b2c0 502 oldspace = blksize(fs, ip, lastblock);
4f083fd7 503 ip->i_size = length;
9c03b2c0
SL
504 newspace = blksize(fs, ip, lastblock);
505 if (newspace == 0)
506 panic("itrunc: newspace");
507 if (oldspace - newspace > 0) {
4f083fd7
SL
508 /*
509 * Block number of space to be free'd is
510 * the old block # plus the number of frags
511 * required for the storage we're keeping.
512 */
9c03b2c0 513 bn += numfrags(fs, newspace);
ced3a252 514 blkfree(ip, bn, oldspace - newspace);
08d9a8ec 515 blocksreleased += btodb(oldspace - newspace);
4f083fd7 516 }
5d5124a1 517 }
4f083fd7 518done:
9c03b2c0
SL
519/* BEGIN PARANOIA */
520 for (level = SINGLE; level <= TRIPLE; level++)
521 if (ip->i_ib[level] != oip->i_ib[level])
522 panic("itrunc1");
523 for (i = 0; i < NDADDR; i++)
524 if (ip->i_db[i] != oip->i_db[i])
525 panic("itrunc2");
526/* END PARANOIA */
08d9a8ec
SL
527 oip->i_blocks -= blocksreleased;
528 if (oip->i_blocks < 0) /* sanity */
529 oip->i_blocks = 0;
530 oip->i_flag |= ICHG;
b4567e9c 531#ifdef QUOTA
4b61628b
KM
532 if (!getinoquota(oip))
533 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
89045c38 534#endif
7188ac27 535 return (allerror);
5d5124a1
BJ
536}
537
4f083fd7
SL
538/*
539 * Release blocks associated with the inode ip and
540 * stored in the indirect block bn. Blocks are free'd
541 * in LIFO order up to (but not including) lastbn. If
9c03b2c0
SL
542 * level is greater than SINGLE, the block is an indirect
543 * block and recursive calls to indirtrunc must be used to
544 * cleanse other indirect blocks.
545 *
546 * NB: triple indirect blocks are untested.
4f083fd7 547 */
7188ac27 548indirtrunc(ip, bn, lastbn, level, countp)
6459ebe0 549 register struct inode *ip;
4f083fd7 550 daddr_t bn, lastbn;
9c03b2c0 551 int level;
7188ac27 552 long *countp;
5d5124a1 553{
4f083fd7 554 register int i;
b30358ab 555 struct buf *bp;
9c03b2c0 556 register struct fs *fs = ip->i_fs;
b30358ab
KM
557 register daddr_t *bap;
558 daddr_t *copy, nb, last;
7188ac27
KM
559 long blkcount, factor;
560 int nblocks, blocksreleased = 0;
561 int error, allerror = 0;
5d5124a1 562
9c03b2c0
SL
563 /*
564 * Calculate index in current block of last
565 * block to be kept. -1 indicates the entire
566 * block so we need not calculate the index.
567 */
568 factor = 1;
569 for (i = SINGLE; i < level; i++)
570 factor *= NINDIR(fs);
4f083fd7 571 last = lastbn;
9c03b2c0
SL
572 if (lastbn > 0)
573 last /= factor;
08d9a8ec 574 nblocks = btodb(fs->fs_bsize);
9c03b2c0
SL
575 /*
576 * Get buffer of block pointers, zero those
577 * entries corresponding to blocks to be free'd,
578 * and update on disk copy first.
579 */
ec67a3ce
MK
580#ifdef SECSIZE
581 bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize,
582 fs->fs_dbsize);
583#else SECSIZE
a937f856
KM
584 error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
585 NOCRED, &bp);
7188ac27 586 if (error) {
9c03b2c0 587 brelse(bp);
7188ac27
KM
588 *countp = 0;
589 return (error);
9c03b2c0
SL
590 }
591 bap = bp->b_un.b_daddr;
b30358ab
KM
592 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
593 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
9c03b2c0
SL
594 bzero((caddr_t)&bap[last + 1],
595 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
e038406d
KM
596 if (last == -1)
597 bp->b_flags |= B_INVAL;
7188ac27
KM
598 error = bwrite(bp);
599 if (error)
600 allerror = error;
b30358ab 601 bap = copy;
4f083fd7 602
9c03b2c0
SL
603 /*
604 * Recursively free totally unused blocks.
605 */
606 for (i = NINDIR(fs) - 1; i > last; i--) {
5d5124a1 607 nb = bap[i];
4f083fd7 608 if (nb == 0)
5d5124a1 609 continue;
7188ac27
KM
610 if (level > SINGLE) {
611 error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
612 &blkcount);
613 if (error)
614 allerror = error;
615 blocksreleased += blkcount;
616 }
ced3a252 617 blkfree(ip, nb, (off_t)fs->fs_bsize);
4f083fd7 618 blocksreleased += nblocks;
4f083fd7 619 }
9c03b2c0
SL
620
621 /*
622 * Recursively free last partial block.
623 */
624 if (level > SINGLE && lastbn >= 0) {
625 last = lastbn % factor;
4f083fd7 626 nb = bap[i];
7188ac27
KM
627 if (nb != 0) {
628 error = indirtrunc(ip, nb, last, level - 1, &blkcount);
629 if (error)
630 allerror = error;
631 blocksreleased += blkcount;
632 }
5d5124a1 633 }
b30358ab 634 FREE(copy, M_TEMP);
7188ac27
KM
635 *countp = blocksreleased;
636 return (allerror);
5d5124a1
BJ
637}
638
d6a210b8 639/*
7494ef16 640 * Lock an inode. If its already locked, set the WANT bit and sleep.
d6a210b8 641 */
7494ef16
BJ
642ilock(ip)
643 register struct inode *ip;
d6a210b8
BJ
644{
645
7188ac27
KM
646 while (ip->i_flag & ILOCKED) {
647 ip->i_flag |= IWANT;
71c4cb8d
KM
648 if (ip->i_spare0 == u.u_procp->p_pid)
649 panic("locking against myself");
650 ip->i_spare1 = u.u_procp->p_pid;
7188ac27
KM
651 (void) sleep((caddr_t)ip, PINOD);
652 }
71c4cb8d
KM
653 ip->i_spare1 = 0;
654 ip->i_spare0 = u.u_procp->p_pid;
655 u.u_spare[0]++;
7188ac27 656 ip->i_flag |= ILOCKED;
d6a210b8
BJ
657}
658
659/*
7494ef16 660 * Unlock an inode. If WANT bit is on, wakeup.
d6a210b8 661 */
ff56f48a 662iunlock(ip)
7494ef16 663 register struct inode *ip;
d6a210b8
BJ
664{
665
7188ac27 666 if ((ip->i_flag & ILOCKED) == 0)
e038406d 667 vprint("iunlock: unlocked inode", ITOV(ip));
71c4cb8d
KM
668 ip->i_spare0 = 0;
669 u.u_spare[0]--;
7188ac27
KM
670 ip->i_flag &= ~ILOCKED;
671 if (ip->i_flag&IWANT) {
672 ip->i_flag &= ~IWANT;
673 wakeup((caddr_t)ip);
674 }
675}