zero out usec fields (from Rick Macklem)
[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 *
b5ea418e 17 * @(#)ffs_inode.c 7.10 (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
7188ac27
KM
44#define INSFREE(ip) {\
45 if (ifreeh) { \
46 *ifreet = (ip); \
47 (ip)->i_freeb = ifreet; \
48 } else { \
49 ifreeh = (ip); \
50 (ip)->i_freeb = &ifreeh; \
51 } \
52 (ip)->i_freef = NULL; \
53 ifreet = &(ip)->i_freef; \
54}
55
3ebac878
RE
56union ihead { /* inode LRU cache, Chris Maltby */
57 union ihead *ih_head[2];
58 struct inode *ih_chain[2];
59} ihead[INOHSZ];
60
7188ac27 61struct inode *ifreeh, **ifreet, *bdevlisth;
5d5124a1
BJ
62
63/*
64 * Initialize hash links for inodes
65 * and build inode free list.
66 */
67ihinit()
68{
69 register int i;
75105cf0 70 register struct inode *ip = inode;
3ebac878 71 register union ihead *ih = ihead;
5d5124a1 72
3ebac878
RE
73 for (i = INOHSZ; --i >= 0; ih++) {
74 ih->ih_head[0] = ih;
75 ih->ih_head[1] = ih;
76 }
77 ifreeh = ip;
78 ifreet = &ip->i_freef;
79 ip->i_freeb = &ifreeh;
80 ip->i_forw = ip;
81 ip->i_back = ip;
7188ac27 82 ITOV(ip)->v_data = (qaddr_t)ip;
3ebac878
RE
83 for (i = ninode; --i > 0; ) {
84 ++ip;
85 ip->i_forw = ip;
86 ip->i_back = ip;
7188ac27 87 ITOV(ip)->v_data = (qaddr_t)ip;
3ebac878
RE
88 *ifreet = ip;
89 ip->i_freeb = ifreet;
90 ifreet = &ip->i_freef;
91 }
92 ip->i_freef = NULL;
5d5124a1
BJ
93}
94
3ebac878 95/*
7188ac27 96 * Look up an vnode/inode by device,inumber.
5d5124a1
BJ
97 * If it is in core (in the inode structure),
98 * honor the locking protocol.
99 * If it is not in core, read it in from the
100 * specified device.
7188ac27 101 * Callers must check for mount points!!
5d5124a1
BJ
102 * In all cases, a pointer to a locked
103 * inode structure is returned.
5d5124a1 104 */
7188ac27
KM
105iget(xp, ino, ipp)
106 struct inode *xp;
7494ef16 107 ino_t ino;
7188ac27 108 struct inode **ipp;
5d5124a1 109{
7188ac27
KM
110 dev_t dev = xp->i_dev;
111 struct mount *mntp = ITOV(xp)->v_mount;
112 register struct fs *fs = VFSTOUFS(mntp)->um_fs;
113 register struct inode *ip, *iq;
114 register struct vnode *vp;
115 struct inode *nip;
116 struct buf *bp;
117 struct dinode tdip, *dp;
118 union ihead *ih;
119 int error;
2e64ab65 120
5d5124a1 121loop:
3ebac878
RE
122 ih = &ihead[INOHASH(dev, ino)];
123 for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
7494ef16 124 if (ino == ip->i_number && dev == ip->i_dev) {
8ac1234a
SL
125 /*
126 * Following is essentially an inline expanded
127 * copy of igrab(), expanded inline for speed,
128 * and so that the test for a mounted on inode
129 * can be deferred until after we are sure that
130 * the inode isn't busy.
131 */
5c2ba954 132 if ((ip->i_flag&ILOCKED) != 0) {
5d5124a1
BJ
133 ip->i_flag |= IWANT;
134 sleep((caddr_t)ip, PINOD);
135 goto loop;
136 }
7188ac27
KM
137 vp = ITOV(ip);
138 if (vp->v_count == 0) { /* ino on free list */
3ebac878
RE
139 if (iq = ip->i_freef)
140 iq->i_freeb = ip->i_freeb;
141 else
142 ifreet = ip->i_freeb;
143 *ip->i_freeb = iq;
144 ip->i_freef = NULL;
145 ip->i_freeb = NULL;
146 }
aed86454 147 ILOCK(ip);
8fe1c702 148 VREF(vp);
7188ac27
KM
149 *ipp = ip;
150 return(0);
151 }
152 if (error = getnewino(dev, ino, &nip)) {
153 *ipp = 0;
154 return (error);
155 }
156 ip = nip;
157 /*
158 * Read in the disk contents for the inode.
159 */
160 if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
161 (int)fs->fs_bsize, &bp)) {
162 /*
163 * The inode doesn't contain anything useful, so it would
164 * be misleading to leave it on its hash chain. Iput() will
165 * take care of putting it back on the free list. We also
166 * lose its inumber, just in case.
167 */
168 remque(ip);
169 ip->i_forw = ip;
170 ip->i_back = ip;
171 ip->i_number = 0;
172 INSFREE(ip);
aed86454 173 iunlock(ip);
7188ac27
KM
174 ip->i_flag = 0;
175 brelse(bp);
176 *ipp = 0;
177 return(error);
178 }
179 /*
180 * Check to see if the new inode represents a block device
181 * for which we already have an inode (either because of
182 * bdevvp() or because of a different inode representing
183 * the same block device). If such an alias exists, put the
184 * just allocated inode back on the free list, and replace
185 * the contents of the existing inode with the contents of
186 * the new inode.
187 */
188 dp = bp->b_un.b_dino;
189 dp += itoo(fs, ino);
190 if ((dp->di_mode & IFMT) != IFBLK) {
191 ip->i_ic = dp->di_ic;
192 brelse(bp);
193 } else {
194again:
195 for (iq = bdevlisth; iq; iq = iq->i_devlst) {
196 if (dp->di_rdev != ITOV(iq)->v_rdev)
197 continue;
198 igrab(iq);
199 if (dp->di_rdev != ITOV(iq)->v_rdev) {
200 iput(iq);
201 goto again;
202 }
203 /*
204 * Discard unneeded inode.
205 */
206 remque(ip);
207 ip->i_forw = ip;
208 ip->i_back = ip;
209 ip->i_number = 0;
210 INSFREE(ip);
aed86454 211 iunlock(ip);
7188ac27
KM
212 ip->i_flag = 0;
213 /*
214 * Reinitialize aliased inode.
215 * We must release the buffer that we just read
216 * before doing the iupdat() to avoid a possible
217 * deadlock with updating an inode in the same
218 * disk block.
219 */
220 ip = iq;
221 vp = ITOV(iq);
222 tdip.di_ic = dp->di_ic;
223 brelse(bp);
224 error = iupdat(ip, &time, &time, 1);
225 ip->i_ic = tdip.di_ic;
226 remque(ip);
227 insque(ip, ih);
228 ip->i_dev = dev;
229 ip->i_number = ino;
230 if (ip->i_devvp) {
231 vrele(ip->i_devvp);
232 ip->i_devvp = 0;
233 }
234 cache_purge(vp);
235 break;
236 }
237 if (iq == 0) {
238 ip->i_ic = dp->di_ic;
239 brelse(bp);
240 ip->i_devlst = bdevlisth;
241 bdevlisth = ip;
5d5124a1 242 }
7188ac27
KM
243 }
244 /*
245 * Finish inode initialization.
246 */
247 ip->i_fs = fs;
248 ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
8fe1c702 249 VREF(ip->i_devvp);
7188ac27
KM
250 /*
251 * Initialize the associated vnode
252 */
253 vp = ITOV(ip);
254 vinit(vp, mntp, IFTOVT(ip->i_mode), &ufs_vnodeops);
255 if (vp->v_type == VCHR || vp->v_type == VBLK) {
256 vp->v_rdev = ip->i_rdev;
257 vp->v_op = &blk_vnodeops;
258 }
259 if (ino == ROOTINO)
260 vp->v_flag |= VROOT;
261#ifdef QUOTA
262 if (ip->i_mode != 0)
263 ip->i_dquot = inoquota(ip);
264#endif
afd7e202
KM
265 /*
266 * Set up a generation number for this inode if it does not
267 * already have one. This should only happen on old filesystems.
268 */
269 if (ip->i_gen == 0) {
270 if (++nextgennumber < (u_long)time.tv_sec)
271 nextgennumber = time.tv_sec;
272 ip->i_gen = nextgennumber;
273 if ((vp->v_mount->m_flag & M_RDONLY) == 0)
274 ip->i_flag |= IMOD;
275 }
7188ac27
KM
276 *ipp = ip;
277 return (0);
278}
3ebac878 279
7188ac27
KM
280/*
281 * Allocate a new inode.
282 *
283 * Put it onto its hash chain and lock it so that other requests for
284 * this inode will block if they arrive while we are sleeping waiting
285 * for old data structures to be purged or for the contents of the disk
286 * portion of this inode to be read.
287 */
288getnewino(dev, ino, ipp)
289 dev_t dev;
290 ino_t ino;
291 struct inode **ipp;
292{
293 union ihead *ih;
294 register struct inode *ip, *iq;
295 register struct vnode *vp;
296
297 /*
298 * Remove the next inode from the free list.
299 */
3ebac878 300 if ((ip = ifreeh) == NULL) {
945fbb1b 301 tablefull("inode");
7188ac27
KM
302 *ipp = 0;
303 return(ENFILE);
5d5124a1 304 }
7188ac27
KM
305 vp = ITOV(ip);
306 if (vp->v_count)
bed1bb6e 307 panic("free inode isn't");
3ebac878
RE
308 if (iq = ip->i_freef)
309 iq->i_freeb = &ifreeh;
310 ifreeh = iq;
311 ip->i_freef = NULL;
312 ip->i_freeb = NULL;
313 /*
314 * Now to take inode off the hash chain it was on
315 * (initially, or after an iflush, it is on a "hash chain"
7188ac27
KM
316 * consisting entirely of itself, and pointed to by no-one)
317 * and put it on the chain for its new (ino, dev) pair.
3ebac878 318 */
32dc2b7e 319 remque(ip);
5d5124a1
BJ
320 ip->i_dev = dev;
321 ip->i_number = ino;
7188ac27
KM
322 if (dev != NODEV) {
323 ih = &ihead[INOHASH(dev, ino)];
324 insque(ip, ih);
325 }
aed86454
KM
326 ip->i_flag = 0;
327 ILOCK(ip);
6459ebe0 328 ip->i_lastr = 0;
ec67a3ce 329#endif SECSIZE
5d5124a1 330 /*
7188ac27 331 * Purge old data structures associated with the inode.
5d5124a1 332 */
7188ac27
KM
333 cache_purge(vp);
334 if (ip->i_devvp) {
335 vrele(ip->i_devvp);
336 ip->i_devvp = 0;
5d5124a1 337 }
b4567e9c 338#ifdef QUOTA
7188ac27
KM
339 dqrele(ip->i_dquot);
340 ip->i_dquot = NODQUOT;
89045c38 341#endif
7188ac27
KM
342 if (vp->v_type == VBLK) {
343 if (bdevlisth == ip) {
344 bdevlisth = ip->i_devlst;
345 } else {
346 for (iq = bdevlisth; iq; iq = iq->i_devlst) {
347 if (iq->i_devlst != ip)
348 continue;
349 iq->i_devlst = ip->i_devlst;
350 break;
351 }
352 if (iq == NULL)
353 panic("missing bdev");
354 }
355 }
356 *ipp = ip;
357 return (0);
5d5124a1
BJ
358}
359
8ac1234a
SL
360/*
361 * Convert a pointer to an inode into a reference to an inode.
362 *
363 * This is basically the internal piece of iget (after the
364 * inode pointer is located) but without the test for mounted
365 * filesystems. It is caller's responsibility to check that
366 * the inode pointer is valid.
367 */
368igrab(ip)
369 register struct inode *ip;
370{
7188ac27
KM
371 register struct vnode *vp = ITOV(ip);
372
8ac1234a
SL
373 while ((ip->i_flag&ILOCKED) != 0) {
374 ip->i_flag |= IWANT;
375 sleep((caddr_t)ip, PINOD);
376 }
7188ac27 377 if (vp->v_count == 0) { /* ino on free list */
8ac1234a
SL
378 register struct inode *iq;
379
380 if (iq = ip->i_freef)
381 iq->i_freeb = ip->i_freeb;
382 else
383 ifreet = ip->i_freeb;
384 *ip->i_freeb = iq;
385 ip->i_freef = NULL;
386 ip->i_freeb = NULL;
387 }
8fe1c702 388 VREF(vp);
aed86454 389 ILOCK(ip);
8ac1234a
SL
390}
391
7188ac27
KM
392/*
393 * Create a vnode for a block device.
394 * Used for root filesystem, argdev, and swap areas.
395 */
396bdevvp(dev, vpp)
397 dev_t dev;
398 struct vnode **vpp;
399{
400 register struct inode *ip;
401 register struct vnode *vp;
402 struct inode *nip;
403 int error;
404
405 /*
406 * Check for the existence of an existing vnode.
407 */
408again:
409 for (ip = bdevlisth; ip; ip = ip->i_devlst) {
410 vp = ITOV(ip);
411 if (dev != vp->v_rdev)
412 continue;
413 igrab(ip);
414 if (dev != vp->v_rdev) {
415 iput(ip);
416 goto again;
417 }
418 IUNLOCK(ip);
419 *vpp = vp;
420 return (0);
421 }
422 if (error = getnewino(NODEV, (ino_t)0, &nip)) {
423 *vpp = 0;
424 return (error);
425 }
426 ip = nip;
427 ip->i_fs = 0;
428 ip->i_devlst = bdevlisth;
429 bdevlisth = ip;
430 vp = ITOV(ip);
431 vinit(vp, 0, VBLK, &blk_vnodeops);
432 vp->v_rdev = dev;
433 IUNLOCK(ip);
434 *vpp = vp;
435 return (0);
436}
437
5d5124a1
BJ
438/*
439 * Decrement reference count of
440 * an inode structure.
441 * On the last reference,
442 * write the inode out and if necessary,
443 * truncate and deallocate the file.
444 */
445iput(ip)
7494ef16 446 register struct inode *ip;
5d5124a1 447{
ff56f48a 448
5c2ba954 449 if ((ip->i_flag & ILOCKED) == 0)
ff56f48a 450 panic("iput");
a388503d 451 IUNLOCK(ip);
7188ac27 452 vrele(ITOV(ip));
ff56f48a
KM
453}
454
7188ac27
KM
455
456ufs_inactive(vp)
457 struct vnode *vp;
ff56f48a 458{
7188ac27
KM
459 register struct inode *ip = VTOI(vp);
460 int mode, error;
461
462 if (ITOV(ip)->v_count != 0)
463 panic("ufs_inactive: not inactive");
b5ea418e
KM
464 /*
465 * Get rid of inodes related to stale file handles.
466 */
467 if (ip->i_mode == 0)
468 goto freeit;
aed86454 469 ILOCK(ip);
7188ac27
KM
470 if (ip->i_nlink <= 0 && (ITOV(ip)->v_mount->m_flag&M_RDONLY) == 0) {
471 error = itrunc(ip, (u_long)0);
472 mode = ip->i_mode;
473 ip->i_mode = 0;
474 ip->i_rdev = 0;
475 ip->i_flag |= IUPD|ICHG;
476 ifree(ip, ip->i_number, mode);
b4567e9c 477#ifdef QUOTA
7188ac27
KM
478 (void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
479 dqrele(ip->i_dquot);
480 ip->i_dquot = NODQUOT;
89045c38 481#endif
7188ac27
KM
482 }
483 IUPDAT(ip, &time, &time, 0);
484 IUNLOCK(ip);
b5ea418e 485freeit:
7188ac27
KM
486 ip->i_flag = 0;
487 /*
488 * Put the inode on the end of the free list.
489 * Possibly in some cases it would be better to
490 * put the inode at the head of the free list,
491 * (eg: where i_mode == 0 || i_number == 0).
492 */
493 INSFREE(ip);
494 return (error);
5d5124a1
BJ
495}
496
497/*
498 * Check accessed and update flags on
499 * an inode structure.
500 * If any is on, update the inode
501 * with the current time.
c0bb1685
BJ
502 * If waitfor is given, then must insure
503 * i/o order so wait for write to complete.
5d5124a1 504 */
c0bb1685 505iupdat(ip, ta, tm, waitfor)
7494ef16 506 register struct inode *ip;
b32450f4 507 struct timeval *ta, *tm;
7494ef16 508 int waitfor;
5d5124a1 509{
7188ac27
KM
510 struct buf *bp;
511 struct vnode *vp = ITOV(ip);
5d5124a1 512 struct dinode *dp;
ec67a3ce 513 register struct fs *fs;
5d5124a1 514
ec67a3ce 515 fs = ip->i_fs;
7188ac27
KM
516 if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
517 return (0);
518 if (vp->v_mount->m_flag & M_RDONLY)
519 return (0);
520 error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
521 (int)fs->fs_bsize, &bp);
522 if (error) {
523 brelse(bp);
524 return (error);
525 }
526 if (ip->i_flag&IACC)
527 ip->i_atime = ta->tv_sec;
528 if (ip->i_flag&IUPD)
529 ip->i_mtime = tm->tv_sec;
530 if (ip->i_flag&ICHG)
531 ip->i_ctime = time.tv_sec;
532 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
533 dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
534 dp->di_ic = ip->i_ic;
535 if (waitfor) {
536 return (bwrite(bp));
537 } else {
538 bdwrite(bp);
539 return (0);
5d5124a1
BJ
540 }
541}
542
9c03b2c0
SL
543#define SINGLE 0 /* index of single indirect block */
544#define DOUBLE 1 /* index of double indirect block */
545#define TRIPLE 2 /* index of triple indirect block */
5d5124a1 546/*
528f664c
SL
547 * Truncate the inode ip to at most
548 * length size. Free affected disk
549 * blocks -- the blocks of the file
550 * are removed in reverse order.
9c03b2c0
SL
551 *
552 * NB: triple indirect blocks are untested.
5d5124a1 553 */
9c03b2c0 554itrunc(oip, length)
28821bc5 555 register struct inode *oip;
4f083fd7 556 u_long length;
5d5124a1 557{
4f083fd7 558 register daddr_t lastblock;
a5e62f37 559 daddr_t bn, lbn, lastiblock[NIADDR];
6459ebe0 560 register struct fs *fs;
9c03b2c0 561 register struct inode *ip;
28821bc5 562 struct buf *bp;
7188ac27
KM
563 int offset, osize, size, level;
564 long count, nblocks, blocksreleased = 0;
28821bc5 565 register int i;
7188ac27 566 int error, allerror = 0;
9c03b2c0 567 struct inode tip;
4f083fd7 568
7b2e4f05
SL
569 if (oip->i_size <= length) {
570 oip->i_flag |= ICHG|IUPD;
7188ac27
KM
571 error = iupdat(oip, &time, &time, 1);
572 return (error);
7b2e4f05 573 }
c0bb1685 574 /*
9c03b2c0
SL
575 * Calculate index into inode's block list of
576 * last direct and indirect blocks (if any)
577 * which we want to keep. Lastblock is -1 when
578 * the file is truncated to 0.
c0bb1685 579 */
9c03b2c0 580 fs = oip->i_fs;
4f083fd7 581 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
9c03b2c0
SL
582 lastiblock[SINGLE] = lastblock - NDADDR;
583 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
584 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
08d9a8ec 585 nblocks = btodb(fs->fs_bsize);
6459ebe0 586 /*
28821bc5
KM
587 * Update the size of the file. If the file is not being
588 * truncated to a block boundry, the contents of the
589 * partial block following the end of the file must be
590 * zero'ed in case it ever become accessable again because
591 * of subsequent file growth.
592 */
593 osize = oip->i_size;
594 offset = blkoff(fs, length);
595 if (offset == 0) {
596 oip->i_size = length;
597 } else {
598 lbn = lblkno(fs, length);
7188ac27
KM
599 error = balloc(oip, lbn, offset, &bn, B_CLRBUF);
600 if (error)
601 return (error);
602 if ((long)bn < 0)
603 panic("itrunc: hole");
28821bc5
KM
604 oip->i_size = length;
605 size = blksize(fs, oip, lbn);
ec67a3ce 606 count = howmany(size, CLBYTES);
7188ac27
KM
607 munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
608 error = bread(oip->i_devvp, bn, size, &bp);
609 if (error) {
28821bc5
KM
610 oip->i_size = osize;
611 brelse(bp);
7188ac27 612 return (error);
28821bc5 613 }
a5e62f37 614 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
28821bc5
KM
615 bdwrite(bp);
616 }
617 /*
618 * Update file and block pointers
9c03b2c0
SL
619 * on disk before we start freeing blocks.
620 * If we crash before free'ing blocks below,
621 * the blocks will be returned to the free list.
622 * lastiblock values are also normalized to -1
623 * for calls to indirtrunc below.
6459ebe0 624 */
9c03b2c0 625 tip = *oip;
28821bc5 626 tip.i_size = osize;
9c03b2c0
SL
627 for (level = TRIPLE; level >= SINGLE; level--)
628 if (lastiblock[level] < 0) {
629 oip->i_ib[level] = 0;
630 lastiblock[level] = -1;
4f083fd7 631 }
9c03b2c0
SL
632 for (i = NDADDR - 1; i > lastblock; i--)
633 oip->i_db[i] = 0;
9c03b2c0 634 oip->i_flag |= ICHG|IUPD;
7188ac27 635 allerror = syncip(oip);
9c03b2c0 636
6459ebe0 637 /*
9c03b2c0 638 * Indirect blocks first.
6459ebe0 639 */
28821bc5 640 ip = &tip;
9c03b2c0
SL
641 for (level = TRIPLE; level >= SINGLE; level--) {
642 bn = ip->i_ib[level];
4f083fd7 643 if (bn != 0) {
7188ac27
KM
644 error = indirtrunc(ip, bn, lastiblock[level], level,
645 &count);
646 if (error)
647 allerror = error;
648 blocksreleased += count;
9c03b2c0
SL
649 if (lastiblock[level] < 0) {
650 ip->i_ib[level] = 0;
ced3a252 651 blkfree(ip, bn, (off_t)fs->fs_bsize);
9c03b2c0 652 blocksreleased += nblocks;
9c03b2c0
SL
653 }
654 }
655 if (lastiblock[level] >= 0)
656 goto done;
4f083fd7 657 }
9c03b2c0 658
6459ebe0 659 /*
9c03b2c0 660 * All whole direct blocks or frags.
6459ebe0 661 */
4f083fd7 662 for (i = NDADDR - 1; i > lastblock; i--) {
8011f5df 663 register off_t bsize;
4f083fd7 664
6459ebe0 665 bn = ip->i_db[i];
4f083fd7 666 if (bn == 0)
5d5124a1 667 continue;
4f083fd7 668 ip->i_db[i] = 0;
0b355a6e 669 bsize = (off_t)blksize(fs, ip, i);
ced3a252 670 blkfree(ip, bn, bsize);
0b355a6e 671 blocksreleased += btodb(bsize);
4f083fd7 672 }
9c03b2c0
SL
673 if (lastblock < 0)
674 goto done;
675
4f083fd7
SL
676 /*
677 * Finally, look for a change in size of the
678 * last direct block; release any frags.
679 */
9c03b2c0
SL
680 bn = ip->i_db[lastblock];
681 if (bn != 0) {
8011f5df 682 off_t oldspace, newspace;
9c03b2c0 683
4f083fd7
SL
684 /*
685 * Calculate amount of space we're giving
686 * back as old block size minus new block size.
687 */
9c03b2c0 688 oldspace = blksize(fs, ip, lastblock);
4f083fd7 689 ip->i_size = length;
9c03b2c0
SL
690 newspace = blksize(fs, ip, lastblock);
691 if (newspace == 0)
692 panic("itrunc: newspace");
693 if (oldspace - newspace > 0) {
4f083fd7
SL
694 /*
695 * Block number of space to be free'd is
696 * the old block # plus the number of frags
697 * required for the storage we're keeping.
698 */
9c03b2c0 699 bn += numfrags(fs, newspace);
ced3a252 700 blkfree(ip, bn, oldspace - newspace);
08d9a8ec 701 blocksreleased += btodb(oldspace - newspace);
4f083fd7 702 }
5d5124a1 703 }
4f083fd7 704done:
9c03b2c0
SL
705/* BEGIN PARANOIA */
706 for (level = SINGLE; level <= TRIPLE; level++)
707 if (ip->i_ib[level] != oip->i_ib[level])
708 panic("itrunc1");
709 for (i = 0; i < NDADDR; i++)
710 if (ip->i_db[i] != oip->i_db[i])
711 panic("itrunc2");
712/* END PARANOIA */
08d9a8ec
SL
713 oip->i_blocks -= blocksreleased;
714 if (oip->i_blocks < 0) /* sanity */
715 oip->i_blocks = 0;
716 oip->i_flag |= ICHG;
b4567e9c 717#ifdef QUOTA
08d9a8ec 718 (void) chkdq(oip, -blocksreleased, 0);
89045c38 719#endif
7188ac27 720 return (allerror);
5d5124a1
BJ
721}
722
4f083fd7
SL
723/*
724 * Release blocks associated with the inode ip and
725 * stored in the indirect block bn. Blocks are free'd
726 * in LIFO order up to (but not including) lastbn. If
9c03b2c0
SL
727 * level is greater than SINGLE, the block is an indirect
728 * block and recursive calls to indirtrunc must be used to
729 * cleanse other indirect blocks.
730 *
731 * NB: triple indirect blocks are untested.
4f083fd7 732 */
7188ac27 733indirtrunc(ip, bn, lastbn, level, countp)
6459ebe0 734 register struct inode *ip;
4f083fd7 735 daddr_t bn, lastbn;
9c03b2c0 736 int level;
7188ac27 737 long *countp;
5d5124a1 738{
4f083fd7 739 register int i;
b30358ab 740 struct buf *bp;
9c03b2c0 741 register struct fs *fs = ip->i_fs;
b30358ab
KM
742 register daddr_t *bap;
743 daddr_t *copy, nb, last;
7188ac27
KM
744 long blkcount, factor;
745 int nblocks, blocksreleased = 0;
746 int error, allerror = 0;
5d5124a1 747
9c03b2c0
SL
748 /*
749 * Calculate index in current block of last
750 * block to be kept. -1 indicates the entire
751 * block so we need not calculate the index.
752 */
753 factor = 1;
754 for (i = SINGLE; i < level; i++)
755 factor *= NINDIR(fs);
4f083fd7 756 last = lastbn;
9c03b2c0
SL
757 if (lastbn > 0)
758 last /= factor;
08d9a8ec 759 nblocks = btodb(fs->fs_bsize);
9c03b2c0
SL
760 /*
761 * Get buffer of block pointers, zero those
762 * entries corresponding to blocks to be free'd,
763 * and update on disk copy first.
764 */
ec67a3ce
MK
765#ifdef SECSIZE
766 bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize,
767 fs->fs_dbsize);
768#else SECSIZE
7188ac27
KM
769 error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, &bp);
770 if (error) {
9c03b2c0 771 brelse(bp);
7188ac27
KM
772 *countp = 0;
773 return (error);
9c03b2c0
SL
774 }
775 bap = bp->b_un.b_daddr;
b30358ab
KM
776 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
777 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
9c03b2c0
SL
778 bzero((caddr_t)&bap[last + 1],
779 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
7188ac27
KM
780 error = bwrite(bp);
781 if (error)
782 allerror = error;
b30358ab 783 bap = copy;
4f083fd7 784
9c03b2c0
SL
785 /*
786 * Recursively free totally unused blocks.
787 */
788 for (i = NINDIR(fs) - 1; i > last; i--) {
5d5124a1 789 nb = bap[i];
4f083fd7 790 if (nb == 0)
5d5124a1 791 continue;
7188ac27
KM
792 if (level > SINGLE) {
793 error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
794 &blkcount);
795 if (error)
796 allerror = error;
797 blocksreleased += blkcount;
798 }
ced3a252 799 blkfree(ip, nb, (off_t)fs->fs_bsize);
4f083fd7 800 blocksreleased += nblocks;
4f083fd7 801 }
9c03b2c0
SL
802
803 /*
804 * Recursively free last partial block.
805 */
806 if (level > SINGLE && lastbn >= 0) {
807 last = lastbn % factor;
4f083fd7 808 nb = bap[i];
7188ac27
KM
809 if (nb != 0) {
810 error = indirtrunc(ip, nb, last, level - 1, &blkcount);
811 if (error)
812 allerror = error;
813 blocksreleased += blkcount;
814 }
5d5124a1 815 }
b30358ab 816 FREE(copy, M_TEMP);
7188ac27
KM
817 *countp = blocksreleased;
818 return (allerror);
5d5124a1
BJ
819}
820
3ebac878 821/*
ec67a3ce 822 * Remove any inodes in the inode cache belonging to dev.
3ebac878
RE
823 *
824 * There should not be any active ones, return error if any are found
ec67a3ce 825 * (nb: this is a user error, not a system err).
3ebac878 826 */
b4567e9c 827#ifdef QUOTA
4147b3f6 828iflush(dev, iq)
89045c38 829 dev_t dev;
4147b3f6 830 struct inode *iq;
89045c38 831#else
3ebac878
RE
832iflush(dev)
833 dev_t dev;
89045c38 834#endif
3ebac878 835{
32dc2b7e 836 register struct inode *ip;
3ebac878
RE
837
838 for (ip = inode; ip < inodeNINODE; ip++) {
b4567e9c 839#ifdef QUOTA
89045c38
RE
840 if (ip != iq && ip->i_dev == dev)
841#else
3ebac878 842 if (ip->i_dev == dev)
89045c38 843#endif
7188ac27 844 if (ITOV(ip)->v_count)
ec67a3ce 845 return (EBUSY);
3ebac878 846 else {
32dc2b7e 847 remque(ip);
3ebac878
RE
848 ip->i_forw = ip;
849 ip->i_back = ip;
850 /*
7188ac27 851 * as v_count == 0, the inode was on the free
3ebac878
RE
852 * list already, just leave it there, it will
853 * fall off the bottom eventually. We could
854 * perhaps move it to the head of the free
855 * list, but as umounts are done so
856 * infrequently, we would gain very little,
857 * while making the code bigger.
858 */
b4567e9c 859#ifdef QUOTA
89045c38
RE
860 dqrele(ip->i_dquot);
861 ip->i_dquot = NODQUOT;
862#endif
7188ac27
KM
863 if (ip->i_devvp) {
864 vrele(ip->i_devvp);
865 ip->i_devvp = 0;
866 }
3ebac878 867 }
3ebac878 868 }
ec67a3ce 869 return (0);
3ebac878
RE
870}
871
d6a210b8 872/*
7494ef16 873 * Lock an inode. If its already locked, set the WANT bit and sleep.
d6a210b8 874 */
7494ef16
BJ
875ilock(ip)
876 register struct inode *ip;
d6a210b8
BJ
877{
878
7188ac27
KM
879 while (ip->i_flag & ILOCKED) {
880 ip->i_flag |= IWANT;
881 (void) sleep((caddr_t)ip, PINOD);
882 }
883 ip->i_flag |= ILOCKED;
d6a210b8
BJ
884}
885
886/*
7494ef16 887 * Unlock an inode. If WANT bit is on, wakeup.
d6a210b8 888 */
ff56f48a 889iunlock(ip)
7494ef16 890 register struct inode *ip;
d6a210b8
BJ
891{
892
7188ac27
KM
893 if ((ip->i_flag & ILOCKED) == 0)
894 printf("unlocking unlocked inode %d on dev 0x%x\n",
895 ip->i_number, ip->i_dev);
896 ip->i_flag &= ~ILOCKED;
897 if (ip->i_flag&IWANT) {
898 ip->i_flag &= ~IWANT;
899 wakeup((caddr_t)ip);
900 }
901}
902
903/*
904 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
905 * The mode is shifted to select the owner/group/other fields. The
906 * super user is granted all permissions.
907 *
908 * NB: Called from vnode op table. It seems this could all be done
909 * using vattr's but...
910 */
911iaccess(ip, mode, cred)
912 register struct inode *ip;
913 register int mode;
914 struct ucred *cred;
915{
916 register gid_t *gp;
917 register struct vnode *vp = ITOV(ip);
918 int i;
919
920 /*
921 * If you're the super-user,
922 * you always get access.
923 */
924 if (cred->cr_uid == 0)
925 return (0);
926 /*
927 * Access check is based on only one of owner, group, public.
928 * If not owner, then check group. If not a member of the
929 * group, then check public access.
930 */
931 if (cred->cr_uid != ip->i_uid) {
932 mode >>= 3;
933 gp = cred->cr_groups;
934 for (i = 0; i < cred->cr_ngroups; i++, gp++)
935 if (ip->i_gid == *gp)
936 goto found;
937 mode >>= 3;
938found:
939 ;
940 }
941 if ((ip->i_mode & mode) != 0)
942 return (0);
943 return (EACCES);
d6a210b8 944}