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