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