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