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