xref: /csrg-svn/sys/kern/vfs_lookup.c (revision 16046)
1*16046Skarels /*	vfs_lookup.c	6.6	84/02/15	*/
230Sbill 
330Sbill #include "../h/param.h"
430Sbill #include "../h/systm.h"
530Sbill #include "../h/inode.h"
66571Smckusic #include "../h/fs.h"
730Sbill #include "../h/mount.h"
830Sbill #include "../h/dir.h"
930Sbill #include "../h/user.h"
1030Sbill #include "../h/buf.h"
112275Swnj #include "../h/conf.h"
127825Sroot #include "../h/uio.h"
139166Ssam #include "../h/nami.h"
1415660Smckusick #include "../h/kernel.h"
1530Sbill 
167605Ssam struct	buf *blkatoff();
179166Ssam int	dirchk = 0;
1815798Smckusick 
1930Sbill /*
2015798Smckusick  * Structures associated with name cacheing.
2115798Smckusick  */
2215798Smckusick #define	NCHHASH		32	/* size of hash table */
2315798Smckusick 
2415798Smckusick #if	((NCHHASH)&((NCHHASH)-1)) != 0
2515798Smckusick #define	NHASH(h, i, d)	((unsigned)((h) + (i) + 13 * (int)(d)) % (NCHHASH))
2615798Smckusick #else
2715798Smckusick #define	NHASH(h, i, d)	((unsigned)((h) + (i) + 13 * (int)(d)) & ((NCHHASH)-1))
2815798Smckusick #endif
2915798Smckusick 
3015798Smckusick union	nchash	{
3115798Smckusick 	union	nchash	*nch_head[2];
3215798Smckusick 	struct	nch	*nch_chain[2];
3315798Smckusick } nchash[NCHHASH];
3415798Smckusick #define	nch_forw	nch_chain[0]
3515798Smckusick #define	nch_back	nch_chain[1]
3615798Smckusick 
3715809Smckusick struct	nch	*nchhead, **nchtail;	/* LRU chain pointers */
3815809Smckusick struct	nchstats nchstats;		/* cache effectiveness statistics */
3915798Smckusick 
4015798Smckusick /*
417534Sroot  * Convert a pathname into a pointer to a locked inode,
427534Sroot  * with side effects usable in creating and removing files.
437534Sroot  * This is a very central and rather complicated routine.
4430Sbill  *
457534Sroot  * The func argument gives the routine which returns successive
469166Ssam  * characters of the name to be translated.
477534Sroot  *
489166Ssam  * The flag argument is (LOOKUP, CREATE, DELETE) depending on whether
499166Ssam  * the name is to be (looked up, created, deleted).  If flag has
509166Ssam  * LOCKPARENT or'ed into it and the target of the pathname exists,
519166Ssam  * namei returns both the target and its parent directory locked.
529166Ssam  * If the file system is not maintained in a strict tree hierarchy,
539166Ssam  * this can result in a deadlock situation.  When creating and
549166Ssam  * LOCKPARENT is specified, the target may not be ".".  When deleting
559166Ssam  * and LOCKPARENT is specified, the target may be ".", but the caller
569166Ssam  * must check to insure it does an irele and iput instead of two iputs.
579166Ssam  *
589166Ssam  * The follow argument is 1 when symbolic links are to be followed
599166Ssam  * when they occur at the end of the name translation process.
609166Ssam  *
6115798Smckusick  * Name caching works as follows:
627534Sroot  *
6315798Smckusick  *	names found by directory scans are retained in a cache
6415798Smckusick  *	for future reference.  It is managed LRU, so frequently
6515798Smckusick  *	used names will hang around.  Cache is indexed by hash value
6615798Smckusick  *	obtained from (ino,dev,name) where ino & dev refer to the
6715798Smckusick  *	directory containing name.
6815798Smckusick  *
6915798Smckusick  *	For simplicity (and economy of storage), names longer than
7015798Smckusick  *	some (small) maximum length are not cached, they occur
7115798Smckusick  *	infrequently in any case, and are almost never of interest.
7215798Smckusick  *
7315798Smckusick  *	Upon reaching the last segment of a path, if the reference
7415798Smckusick  *	is for DELETE, or NOCACHE is set (rewrite), and the
7515798Smckusick  *	name is located in the cache, it will be dropped.
7615798Smckusick  *
7715798Smckusick  *	We must be sure never to enter the name ".." into the cache
7815798Smckusick  *	because of the extremely kludgey way that rename() alters
7915798Smckusick  *	".." in a situation like
8015798Smckusick  *		mv a/x b/x
8115798Smckusick  *	where x is a directory, and x/.. is the ".." in question.
8215798Smckusick  *
8315798Smckusick  * Overall outline of namei:
8415798Smckusick  *
857534Sroot  *	copy in name
867534Sroot  *	get starting directory
877534Sroot  * dirloop:
887534Sroot  *	check accessibility of directory
897534Sroot  * dirloop2:
907534Sroot  *	copy next component of name to u.u_dent
917534Sroot  *	handle degenerate case where name is null string
9215798Smckusick  *	look for name in cache, if found, then if at end of path
9315798Smckusick  *	  and deleting or creating, drop it, else to haveino
947534Sroot  *	search for name in directory, to found or notfound
957534Sroot  * notfound:
969166Ssam  *	if creating, return locked directory, leaving info on avail. slots
977534Sroot  *	else return error
987534Sroot  * found:
997534Sroot  *	if at end of path and deleting, return information to allow delete
10015798Smckusick  *	if at end of path and rewriting (create and LOCKPARENT), lock target
1019166Ssam  *	  inode and return info to allow rewrite
1027534Sroot  *	if .. and on mounted filesys, look in mount table for parent
10315798Smckusick  *	if not at end, if neither creating nor deleting, add name to cache
10415798Smckusick  * haveino:
1057534Sroot  *	if symbolic link, massage name in buffer and continue at dirloop
1067534Sroot  *	if more components of name, do next level at dirloop
1077534Sroot  *	return the answer as locked inode
1089166Ssam  *
1099166Ssam  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode,
1109166Ssam  *	 but unlocked.
11130Sbill  */
11230Sbill struct inode *
1135972Swnj namei(func, flag, follow)
1145972Swnj 	int (*func)(), flag, follow;
11530Sbill {
1167534Sroot 	register char *cp;		/* pointer into pathname argument */
1177534Sroot /* these variables refer to things which must be freed or unlocked */
1187534Sroot 	register struct inode *dp = 0;	/* the directory we are searching */
11915798Smckusick 	register struct nch *ncp;	/* cache slot for entry */
1207534Sroot 	register struct fs *fs;		/* file system that directory is in */
1217534Sroot 	register struct buf *bp = 0;	/* a buffer of directory entries */
1227534Sroot 	register struct direct *ep;	/* the current directory entry */
1237534Sroot 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
1247534Sroot 	register struct buf *nbp;	/* buffer storing path name argument */
1257534Sroot /* these variables hold information about the search for a slot */
1267534Sroot 	enum {NONE, COMPACT, FOUND} slotstatus;
1277534Sroot 	int slotoffset = -1;		/* offset of area with free space */
1287534Sroot 	int slotsize;			/* size of area at slotoffset */
1297534Sroot 	int slotfreespace;		/* amount of space free in slot */
1307534Sroot 	int slotneeded;			/* size of the entry we're seeking */
1317534Sroot /* */
13215660Smckusick 	int numdirpasses;		/* strategy for directory search */
13315660Smckusick 	int endsearch;			/* offset to end directory search */
1347534Sroot 	int prevoff;			/* u.u_offset of previous entry */
1357534Sroot 	int nlink = 0;			/* number of symbolic links taken */
1367534Sroot 	struct inode *pdp;		/* saved dp during symlink work */
1377534Sroot 	int i;
1389166Ssam 	int lockparent;
13915798Smckusick 	int docache;
14015798Smckusick 	unsigned hash;			/* value of name hash for entry */
14115798Smckusick 	union nchash *nhp;		/* cache chain head for entry */
14215798Smckusick 	int isdotdot;			/* != 0 if current name is ".." */
14330Sbill 
1449166Ssam 	lockparent = flag & LOCKPARENT;
14515798Smckusick 	docache = (flag & NOCACHE) ^ NOCACHE;
14615798Smckusick 	flag &= ~(LOCKPARENT|NOCACHE);
14715798Smckusick 	if (flag == DELETE)
14815798Smckusick 		docache = 0;
14930Sbill 	/*
1507534Sroot 	 * Get a buffer for the name to be translated, and copy the
1517534Sroot 	 * name into the buffer.
1525972Swnj 	 */
1536571Smckusic 	nbp = geteblk(MAXPATHLEN);
1547534Sroot 	for (cp = nbp->b_un.b_addr; *cp = (*func)(); ) {
15515798Smckusick 		if ((*cp&0377) == ('/'|0200) || (*cp&0200) && flag != LOOKUP) {
1566066Sroot 			u.u_error = EPERM;
1577534Sroot 			goto bad;
1586066Sroot 		}
1596066Sroot 		cp++;
1606571Smckusic 		if (cp >= nbp->b_un.b_addr + MAXPATHLEN) {
1615972Swnj 			u.u_error = ENOENT;
1627534Sroot 			goto bad;
1635972Swnj 		}
1645972Swnj 	}
1657534Sroot 	if (u.u_error)
1667534Sroot 		goto bad;
1677534Sroot 
1685972Swnj 	/*
1697534Sroot 	 * Get starting directory.
17030Sbill 	 */
1717534Sroot 	cp = nbp->b_un.b_addr;
1725972Swnj 	if (*cp == '/') {
1735972Swnj 		while (*cp == '/')
1745972Swnj 			cp++;
17530Sbill 		if ((dp = u.u_rdir) == NULL)
17630Sbill 			dp = rootdir;
1777534Sroot 	} else
1787534Sroot 		dp = u.u_cdir;
1797534Sroot 	fs = dp->i_fs;
1805972Swnj 	ilock(dp);
1815972Swnj 	dp->i_count++;
1827534Sroot 	u.u_pdir = (struct inode *)0xc0000000;		/* illegal */
1837534Sroot 
1847534Sroot 	/*
1857534Sroot 	 * We come to dirloop to search a new directory.
1867534Sroot 	 * The directory must be locked so that it can be
1877534Sroot 	 * iput, and fs must be already set to dp->i_fs.
1887534Sroot 	 */
1896571Smckusic dirloop:
19030Sbill 	/*
1917534Sroot 	 * Check accessiblity of directory.
19230Sbill 	 */
1937534Sroot 	if ((dp->i_mode&IFMT) != IFDIR) {
19430Sbill 		u.u_error = ENOTDIR;
1957534Sroot 		goto bad;
1967534Sroot 	}
1977534Sroot 	if (access(dp, IEXEC))
1987534Sroot 		goto bad;
1997534Sroot 
2006384Swnj dirloop2:
2017534Sroot 	/*
2027534Sroot 	 * Copy next component of name to u.u_dent.
2037534Sroot 	 */
20415798Smckusick 	hash = 0;
2057534Sroot 	for (i = 0; *cp != 0 && *cp != '/'; cp++) {
2066571Smckusic 		if (i >= MAXNAMLEN) {
2075972Swnj 			u.u_error = ENOENT;
2087534Sroot 			goto bad;
2095972Swnj 		}
2107534Sroot 		u.u_dent.d_name[i++] = *cp;
21115798Smckusick 		hash += (unsigned char)*cp * i;
2125972Swnj 	}
2136571Smckusic 	u.u_dent.d_namlen = i;
2147534Sroot 	u.u_dent.d_name[i] = 0;
2157534Sroot 
2167534Sroot 	/*
2177534Sroot 	 * Check for degenerate name (e.g. / or "")
2187534Sroot 	 * which is a way of talking about a directory,
2197534Sroot 	 * e.g. like "/." or ".".
2207534Sroot 	 */
2217534Sroot 	if (u.u_dent.d_name[0] == 0) {
22215798Smckusick 		if (flag != LOOKUP || lockparent) {
22314937Smckusick 			u.u_error = EISDIR;
2247534Sroot 			goto bad;
2255972Swnj 		}
2266571Smckusic 		brelse(nbp);
2276571Smckusic 		return (dp);
2285972Swnj 	}
2297534Sroot 
2306571Smckusic 	/*
23115798Smckusick 	 * We now have a segment name to search for, and a directory to search.
23215798Smckusick 	 *
23315798Smckusick 	 * Before tediously performing a linear scan of the directory,
23415798Smckusick 	 * check the name cache to see if the directory/name pair
23515798Smckusick 	 * we are looking for is known already.  We don't do this
23615798Smckusick 	 * if the segment name is long, simply so the cache can avoid
23715798Smckusick 	 * holding long names (which would either waste space, or
23815798Smckusick 	 * add greatly to the complexity).
23915798Smckusick 	 */
24015798Smckusick 	if (u.u_dent.d_namlen > NCHNAMLEN) {
24115798Smckusick 		nchstats.ncs_long++;
24215798Smckusick 		docache = 0;
24315798Smckusick 	} else {
24415798Smckusick 		nhp = &nchash[NHASH(hash, dp->i_number, dp->i_dev)];
24515798Smckusick 		for (ncp = nhp->nch_forw; ncp != (struct nch *)nhp;
24615798Smckusick 		    ncp = ncp->nc_forw) {
24715798Smckusick 			if (ncp->nc_ino == dp->i_number &&
24815798Smckusick 			    ncp->nc_dev == dp->i_dev &&
24915798Smckusick 			    ncp->nc_nlen == u.u_dent.d_namlen &&
25015798Smckusick 			    !bcmp(ncp->nc_name, u.u_dent.d_name, ncp->nc_nlen))
25115798Smckusick 				break;
25215798Smckusick 		}
25315798Smckusick 
25415798Smckusick 		if (ncp == (struct nch *)nhp) {
25515798Smckusick 			nchstats.ncs_miss++;
25615798Smckusick 			ncp = NULL;
25715798Smckusick 		} else {
25815798Smckusick 			if (*cp == '/' || docache) {
25915798Smckusick 
26015798Smckusick 				nchstats.ncs_goodhits++;
26115798Smckusick 
26215798Smckusick 					/*
26315798Smckusick 					 * move this slot to end of LRU
26415798Smckusick 					 * chain, if not already there
26515798Smckusick 					 */
26615798Smckusick 				if (ncp->nc_nxt) {
26715798Smckusick 						/* remove from LRU chain */
26815798Smckusick 					*ncp->nc_prev = ncp->nc_nxt;
26915798Smckusick 					ncp->nc_nxt->nc_prev = ncp->nc_prev;
27015798Smckusick 
27115798Smckusick 						/* and replace at end of it */
27215798Smckusick 					ncp->nc_nxt = NULL;
27315798Smckusick 					ncp->nc_prev = nchtail;
27415798Smckusick 					*nchtail = ncp;
27515798Smckusick 					nchtail = &ncp->nc_nxt;
27615798Smckusick 				}
27715798Smckusick 
27815798Smckusick 				pdp = dp;
27915798Smckusick 				dp = ncp->nc_ip;
28015798Smckusick 				if (dp == NULL)
28115798Smckusick 					panic("nami: null cache ino");
28215798Smckusick 				if (pdp != dp) {
28315798Smckusick 					ilock(dp);
28415798Smckusick 					dp->i_count++;
28515798Smckusick 					iunlock(pdp);
28615798Smckusick 				} else
28715798Smckusick 					dp->i_count++;
28815798Smckusick 
28915798Smckusick 				u.u_dent.d_ino = dp->i_number;
29015798Smckusick 				/* u_dent.d_reclen is garbage ... */
29115798Smckusick 
29215798Smckusick 				goto haveino;
29315798Smckusick 			}
29415798Smckusick 
29515798Smckusick 			/*
29615798Smckusick 			 * last segment and we are renaming or deleting
29715798Smckusick 			 * or otherwise don't want cache entry to exist
29815798Smckusick 			 */
29915798Smckusick 
30015798Smckusick 			nchstats.ncs_badhits++;
30115798Smckusick 
30215798Smckusick 				/* remove from LRU chain */
30315798Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
30415798Smckusick 			if (ncp->nc_nxt)
30515798Smckusick 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
30615798Smckusick 			else
30715798Smckusick 				nchtail = ncp->nc_prev;
30815798Smckusick 
30915798Smckusick 				/* remove from hash chain */
31015798Smckusick 			remque(ncp);
31115798Smckusick 
31215798Smckusick 				/* release ref on the inode */
31315798Smckusick 			irele(ncp->nc_ip);
31415798Smckusick 			ncp->nc_ip = NULL;
31515798Smckusick 
31615798Smckusick 				/* insert at head of LRU list (first to grab) */
31715798Smckusick 			ncp->nc_nxt = nchhead;
31815798Smckusick 			ncp->nc_prev = &nchhead;
31915798Smckusick 			nchhead->nc_prev = &ncp->nc_nxt;
32015798Smckusick 			nchhead = ncp;
32115798Smckusick 
32215798Smckusick 				/* and make a dummy hash chain */
32315798Smckusick 			ncp->nc_forw = ncp;
32415798Smckusick 			ncp->nc_back = ncp;
32515798Smckusick 
32615798Smckusick 			ncp = NULL;
32715798Smckusick 		}
32815798Smckusick 	}
32915798Smckusick 
33015798Smckusick 	/*
3317534Sroot 	 * Suppress search for slots unless creating
3327534Sroot 	 * file and at end of pathname, in which case
3337534Sroot 	 * we watch for a place to put the new file in
3347534Sroot 	 * case it doesn't already exist.
3356571Smckusic 	 */
3367534Sroot 	slotstatus = FOUND;
3379166Ssam 	if (flag == CREATE && *cp == 0) {
3387534Sroot 		slotstatus = NONE;
3397534Sroot 		slotfreespace = 0;
3407534Sroot 		slotneeded = DIRSIZ(&u.u_dent);
3417534Sroot 	}
34215660Smckusick 	/*
34315660Smckusick 	 * If this is the same directory that this process
34415660Smckusick 	 * previously searched, pick up where we last left off.
34515798Smckusick 	 * We cache only lookups as these are the most common
34615660Smckusick 	 * and have the greatest payoff. Caching CREATE has little
34715660Smckusick 	 * benefit as it usually must search the entire directory
34815660Smckusick 	 * to determine that the entry does not exist. Caching the
34915660Smckusick 	 * location of the last DELETE has not reduced profiling time
35015660Smckusick 	 * and hence has been removed in the interest of simplicity.
35115660Smckusick 	 */
35215660Smckusick 	if (flag != LOOKUP || dp->i_number != u.u_ncache.nc_inumber ||
35315660Smckusick 	    dp->i_dev != u.u_ncache.nc_dev) {
35415660Smckusick 		u.u_offset = 0;
35515660Smckusick 		numdirpasses = 1;
35615660Smckusick 	} else {
35715798Smckusick 		if ((dp->i_flag & ICHG) || dp->i_ctime >= u.u_ncache.nc_time) {
35815660Smckusick 			u.u_ncache.nc_prevoffset &= ~(DIRBLKSIZ - 1);
35915660Smckusick 			u.u_ncache.nc_time = time.tv_sec;
36015660Smckusick 		}
36115660Smckusick 		u.u_offset = u.u_ncache.nc_prevoffset;
36215660Smckusick 		entryoffsetinblock = blkoff(fs, u.u_offset);
36315660Smckusick 		if (entryoffsetinblock != 0) {
36415660Smckusick 			bp = blkatoff(dp, u.u_offset, (char **)0);
36515660Smckusick 			if (bp == 0)
36615660Smckusick 				goto bad;
36715660Smckusick 		}
36815660Smckusick 		numdirpasses = 2;
36915798Smckusick 		nchstats.ncs_2passes++;
37015660Smckusick 	}
37115660Smckusick 	endsearch = roundup(dp->i_size, DIRBLKSIZ);
3727534Sroot 
37315660Smckusick searchloop:
37415660Smckusick 	while (u.u_offset < endsearch) {
3755972Swnj 		/*
3765972Swnj 		 * If offset is on a block boundary,
3775972Swnj 		 * read the next directory block.
3785972Swnj 		 * Release previous if it exists.
3795972Swnj 		 */
3806571Smckusic 		if (blkoff(fs, u.u_offset) == 0) {
3815972Swnj 			if (bp != NULL)
3825972Swnj 				brelse(bp);
3837605Ssam 			bp = blkatoff(dp, u.u_offset, (char **)0);
3847534Sroot 			if (bp == 0)
3857534Sroot 				goto bad;
3867534Sroot 			entryoffsetinblock = 0;
3875972Swnj 		}
3887534Sroot 
3895972Swnj 		/*
3907534Sroot 		 * If still looking for a slot, and at a DIRBLKSIZE
3917534Sroot 		 * boundary, have to start looking for free space
3927534Sroot 		 * again.
3936571Smckusic 		 */
3947534Sroot 		if (slotstatus == NONE &&
3957534Sroot 		    (entryoffsetinblock&(DIRBLKSIZ-1)) == 0) {
3967534Sroot 			slotoffset = -1;
3977534Sroot 			slotfreespace = 0;
3987534Sroot 		}
3997534Sroot 
4007534Sroot 		/*
4017534Sroot 		 * Get pointer to next entry, and do consistency checking:
4027534Sroot 		 *	record length must be multiple of 4
4037605Ssam 		 *	record length must not be zero
4047534Sroot 		 *	entry must fit in rest of this DIRBLKSIZ block
4057534Sroot 		 *	record must be large enough to contain name
4067605Ssam 		 * When dirchk is set we also check:
4077605Ssam 		 *	name is not longer than MAXNAMLEN
4087534Sroot 		 *	name must be as long as advertised, and null terminated
4097605Ssam 		 * Checking last two conditions is done only when dirchk is
4107605Ssam 		 * set, to save time.
4117534Sroot 		 */
4127534Sroot 		ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock);
4137534Sroot 		i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
4147605Ssam 		if ((ep->d_reclen & 0x3) || ep->d_reclen == 0 ||
4157605Ssam 		    ep->d_reclen > i || DIRSIZ(ep) > ep->d_reclen ||
4167605Ssam 		    dirchk && (ep->d_namlen > MAXNAMLEN || dirbadname(ep))) {
4177534Sroot 			dirbad(dp, "mangled entry");
4186571Smckusic 			u.u_offset += i;
4197534Sroot 			entryoffsetinblock += i;
4206571Smckusic 			continue;
4216571Smckusic 		}
4227534Sroot 
4236571Smckusic 		/*
4247534Sroot 		 * If an appropriate sized slot has not yet been found,
4256571Smckusic 		 * check to see if one is available. Also accumulate space
4266571Smckusic 		 * in the current block so that we can determine if
4276571Smckusic 		 * compaction is viable.
4286571Smckusic 		 */
4297534Sroot 		if (slotstatus != FOUND) {
4307534Sroot 			int size = ep->d_reclen;
4317534Sroot 
4326571Smckusic 			if (ep->d_ino != 0)
4336571Smckusic 				size -= DIRSIZ(ep);
4346571Smckusic 			if (size > 0) {
4357534Sroot 				if (size >= slotneeded) {
4367534Sroot 					slotstatus = FOUND;
4377534Sroot 					slotoffset = u.u_offset;
4387534Sroot 					slotsize = ep->d_reclen;
4397534Sroot 				} else if (slotstatus == NONE) {
4407534Sroot 					slotfreespace += size;
4417534Sroot 					if (slotoffset == -1)
4427534Sroot 						slotoffset = u.u_offset;
4437534Sroot 					if (slotfreespace >= slotneeded) {
4447534Sroot 						slotstatus = COMPACT;
4457534Sroot 						slotsize =
4467534Sroot 						    u.u_offset+ep->d_reclen -
4477534Sroot 						      slotoffset;
4487534Sroot 					}
4496571Smckusic 				}
4506571Smckusic 			}
4516571Smckusic 		}
4527534Sroot 
4536571Smckusic 		/*
4547534Sroot 		 * Check for a name match.
4555972Swnj 		 */
4567534Sroot 		if (ep->d_ino) {
4577534Sroot 			if (ep->d_namlen == u.u_dent.d_namlen &&
4587534Sroot 			    !bcmp(u.u_dent.d_name, ep->d_name, ep->d_namlen))
4597534Sroot 				goto found;
4607534Sroot 		}
4617534Sroot 		prevoff = u.u_offset;
4626571Smckusic 		u.u_offset += ep->d_reclen;
4637534Sroot 		entryoffsetinblock += ep->d_reclen;
4647534Sroot 	}
46515798Smckusick /* notfound: */
46615660Smckusick 	/*
46715798Smckusick 	 * If we started in the middle of the directory and failed
46815660Smckusick 	 * to find our target, we must check the beginning as well.
46915660Smckusick 	 */
47015660Smckusick 	if (numdirpasses == 2) {
47115660Smckusick 		numdirpasses--;
47215660Smckusick 		u.u_offset = 0;
47315660Smckusick 		endsearch = u.u_ncache.nc_prevoffset;
47415660Smckusick 		goto searchloop;
47515660Smckusick 	}
4767534Sroot 	/*
4777534Sroot 	 * If creating, and at end of pathname and current
4789166Ssam 	 * directory has not been removed, then can consider
4799166Ssam 	 * allowing file to be created.
4807534Sroot 	 */
4819166Ssam 	if (flag == CREATE && *cp == 0 && dp->i_nlink != 0) {
4825972Swnj 		/*
4837534Sroot 		 * Access for write is interpreted as allowing
4847534Sroot 		 * creation of files in the directory.
4855972Swnj 		 */
4867534Sroot 		if (access(dp, IWRITE))
4877534Sroot 			goto bad;
4885972Swnj 		/*
4897534Sroot 		 * Return an indication of where the new directory
4907534Sroot 		 * entry should be put.  If we didn't find a slot,
4917534Sroot 		 * then set u.u_count to 0 indicating that the
4927534Sroot 		 * new slot belongs at the end of the directory.
4937534Sroot 		 * If we found a slot, then the new entry can be
4947534Sroot 		 * put in the range [u.u_offset..u.u_offset+u.u_count)
4955972Swnj 		 */
49615660Smckusick 		if (slotstatus == NONE) {
49715660Smckusick 			u.u_offset = roundup(dp->i_size, DIRBLKSIZ);
4987534Sroot 			u.u_count = 0;
49915660Smckusick 		} else {
5007534Sroot 			u.u_offset = slotoffset;
5017534Sroot 			u.u_count = slotsize;
5025972Swnj 		}
5037534Sroot 		dp->i_flag |= IUPD|ICHG;
5047534Sroot 		if (bp)
5057534Sroot 			brelse(bp);
5067534Sroot 		brelse(nbp);
5075972Swnj 		/*
5087534Sroot 		 * We return with the directory locked, so that
5097534Sroot 		 * the parameters we set up above will still be
5107534Sroot 		 * valid if we actually decide to do a direnter().
5117534Sroot 		 * We return NULL to indicate that the entry doesn't
5127534Sroot 		 * currently exist, leaving a pointer to the (locked)
5137534Sroot 		 * directory inode in u.u_pdir.
5145972Swnj 		 */
5157534Sroot 		u.u_pdir = dp;
5167534Sroot 		return (NULL);
5177534Sroot 	}
5187534Sroot 	u.u_error = ENOENT;
5197534Sroot 	goto bad;
5207534Sroot found:
52115798Smckusick 	if (numdirpasses == 2)
52215798Smckusick 		nchstats.ncs_pass2++;
5237534Sroot 	/*
5247534Sroot 	 * Check that directory length properly reflects presence
5257534Sroot 	 * of this entry.
5267534Sroot 	 */
5277605Ssam 	if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) {
5287534Sroot 		dirbad(dp, "i_size too small");
5297605Ssam 		dp->i_size = entryoffsetinblock + DIRSIZ(ep);
5307534Sroot 		dp->i_flag |= IUPD|ICHG;
5317534Sroot 	}
5327534Sroot 
5337534Sroot 	/*
53415660Smckusick 	 * Found component in pathname.
53515798Smckusick 	 * If the final component of path name, save information
53615660Smckusick 	 * in the cache as to where the entry was found.
5377534Sroot 	 */
53815660Smckusick 	if (*cp == '\0' && flag == LOOKUP) {
53915660Smckusick 		u.u_ncache.nc_prevoffset = u.u_offset;
54015660Smckusick 		u.u_ncache.nc_inumber = dp->i_number;
54115660Smckusick 		u.u_ncache.nc_dev = dp->i_dev;
54215660Smckusick 		u.u_ncache.nc_time = time.tv_sec;
54315660Smckusick 	}
54415660Smckusick 	/*
54515660Smckusick 	 * Save directory entry in u.u_dent,
54615660Smckusick 	 * and release directory buffer.
54715660Smckusick 	 */
5487825Sroot 	bcopy((caddr_t)ep, (caddr_t)&u.u_dent, (u_int)DIRSIZ(ep));
5497534Sroot 	brelse(bp);
5507534Sroot 	bp = NULL;
5517534Sroot 
5527534Sroot 	/*
5537534Sroot 	 * If deleting, and at end of pathname, return
5547534Sroot 	 * parameters which can be used to remove file.
5559166Ssam 	 * If the lockparent flag isn't set, we return only
5569166Ssam 	 * the directory (in u.u_pdir), otherwise we go
5579166Ssam 	 * on and lock the inode, being careful with ".".
5587534Sroot 	 */
5599166Ssam 	if (flag == DELETE && *cp == 0) {
5607534Sroot 		/*
5617534Sroot 		 * Write access to directory required to delete files.
5627534Sroot 		 */
5637534Sroot 		if (access(dp, IWRITE))
5647534Sroot 			goto bad;
5659166Ssam 		u.u_pdir = dp;		/* for dirremove() */
5667534Sroot 		/*
5677534Sroot 		 * Return pointer to current entry in u.u_offset,
5687534Sroot 		 * and distance past previous entry (if there
5697534Sroot 		 * is a previous entry in this block) in u.u_count.
5707534Sroot 		 * Save directory inode pointer in u.u_pdir for dirremove().
5717534Sroot 		 */
5727534Sroot 		if ((u.u_offset&(DIRBLKSIZ-1)) == 0)
5737534Sroot 			u.u_count = 0;
5747534Sroot 		else
5757534Sroot 			u.u_count = u.u_offset - prevoff;
5769166Ssam 		if (lockparent) {
5779166Ssam 			if (dp->i_number == u.u_dent.d_ino)
5789166Ssam 				dp->i_count++;
5799166Ssam 			else {
5809166Ssam 				dp = iget(dp->i_dev, fs, u.u_dent.d_ino);
5819166Ssam 				if (dp == NULL) {
5829166Ssam 					iput(u.u_pdir);
5839166Ssam 					goto bad;
5849166Ssam 				}
58515798Smckusick 				/*
586*16046Skarels 				 * If directory is "sticky", then user must own
58715798Smckusick 				 * the directory, or the file in it, else he
58815798Smckusick 				 * may not delete it (unless he's root). This
58915798Smckusick 				 * implements append-only directories.
59015798Smckusick 				 */
591*16046Skarels 				if ((u.u_pdir->i_mode & ISVTX) &&
59215798Smckusick 				    u.u_uid != 0 &&
59315798Smckusick 				    u.u_uid != u.u_pdir->i_uid &&
59415798Smckusick 				    dp->i_uid != u.u_uid) {
59515798Smckusick 					iput(u.u_pdir);
59615798Smckusick 					u.u_error = EPERM;
59715798Smckusick 					goto bad;
59815798Smckusick 				}
5999166Ssam 			}
6009166Ssam 		}
6017534Sroot 		brelse(nbp);
6027534Sroot 		return (dp);
6037534Sroot 	}
6047534Sroot 
6057534Sroot 	/*
6067534Sroot 	 * Special handling for ".." allowing chdir out of mounted
6077534Sroot 	 * file system: indirect .. in root inode to reevaluate
6087534Sroot 	 * in directory file system was mounted on.
6097534Sroot 	 */
61015798Smckusick 	isdotdot = 0;
61115798Smckusick 	if (bcmp(u.u_dent.d_name, "..", 3) == 0) {
61215798Smckusick 		isdotdot++;
6137534Sroot 		if (dp == u.u_rdir)
6147534Sroot 			u.u_dent.d_ino = dp->i_number;
6157534Sroot 		else if (u.u_dent.d_ino == ROOTINO &&
6167534Sroot 		   dp->i_number == ROOTINO) {
6177534Sroot 			for (i = 1; i < NMOUNT; i++)
6187534Sroot 			if (mount[i].m_bufp != NULL &&
6197534Sroot 			   mount[i].m_dev == dp->i_dev) {
6206571Smckusic 				iput(dp);
6217534Sroot 				dp = mount[i].m_inodp;
6225972Swnj 				ilock(dp);
6235972Swnj 				dp->i_count++;
6247534Sroot 				fs = dp->i_fs;
6257534Sroot 				cp -= 2;     /* back over .. */
6267534Sroot 				goto dirloop2;
6275972Swnj 			}
62830Sbill 		}
6297534Sroot 	}
6307534Sroot 
6317534Sroot 	/*
6329166Ssam 	 * If rewriting (rename), return the inode and the
6339166Ssam 	 * information required to rewrite the present directory
6349166Ssam 	 * Must get inode of directory entry to verify it's a
6359166Ssam 	 * regular file, or empty directory.
6369166Ssam 	 */
6379166Ssam 	if ((flag == CREATE && lockparent) && *cp == 0) {
6389166Ssam 		if (access(dp, IWRITE))
6399166Ssam 			goto bad;
6409166Ssam 		u.u_pdir = dp;		/* for dirrewrite() */
6419166Ssam 		/*
6429166Ssam 		 * Careful about locking second inode.
6439166Ssam 		 * This can only occur if the target is ".".
6449166Ssam 		 */
6459166Ssam 		if (dp->i_number == u.u_dent.d_ino) {
6469166Ssam 			u.u_error = EISDIR;		/* XXX */
6479166Ssam 			goto bad;
6489166Ssam 		}
6499166Ssam 		dp = iget(dp->i_dev, fs, u.u_dent.d_ino);
6509166Ssam 		if (dp == NULL) {
6519166Ssam 			iput(u.u_pdir);
6529166Ssam 			goto bad;
6539166Ssam 		}
6549166Ssam 		brelse(nbp);
6559166Ssam 		return (dp);
6569166Ssam 	}
6579166Ssam 
6589166Ssam 	/*
65912011Smckusick 	 * Check for symbolic link, which may require us to massage the
66012011Smckusick 	 * name before we continue translation.  We do not `iput' the
66112011Smckusick 	 * directory because we may need it again if the symbolic link
66212011Smckusick 	 * is relative to the current directory.  Instead we save it
66312011Smckusick 	 * unlocked as "pdp".  We must get the target inode before unlocking
66412011Smckusick 	 * the directory to insure that the inode will not be removed
66512011Smckusick 	 * before we get it.  We prevent deadlock by always fetching
66612011Smckusick 	 * inodes from the root, moving down the directory tree. Thus
66712011Smckusick 	 * when following backward pointers ".." we must unlock the
66812011Smckusick 	 * parent directory before getting the requested directory.
66912011Smckusick 	 * There is a potential race condition here if both the current
67012011Smckusick 	 * and parent directories are removed before the `iget' for the
67112011Smckusick 	 * inode associated with ".." returns.  We hope that this occurs
67212011Smckusick 	 * infrequently since we cannot avoid this race condition without
67312492Ssam 	 * implementing a sophisticated deadlock detection algorithm.
67412011Smckusick 	 * Note also that this simple deadlock detection scheme will not
67512011Smckusick 	 * work if the file system has any hard links other than ".."
67612011Smckusick 	 * that point backwards in the directory structure.
6777534Sroot 	 */
6787534Sroot 	pdp = dp;
67915798Smckusick 	if (isdotdot) {
68012011Smckusick 		iunlock(pdp);	/* race to get the inode */
68112011Smckusick 		dp = iget(dp->i_dev, fs, u.u_dent.d_ino);
68212011Smckusick 		if (dp == NULL)
68312011Smckusick 			goto bad2;
68412011Smckusick 	} else if (dp->i_number == u.u_dent.d_ino) {
68512011Smckusick 		dp->i_count++;	/* we want ourself, ie "." */
68612011Smckusick 	} else {
68712011Smckusick 		dp = iget(dp->i_dev, fs, u.u_dent.d_ino);
68812011Smckusick 		iunlock(pdp);
68912011Smckusick 		if (dp == NULL)
69012011Smckusick 			goto bad2;
69112011Smckusick 	}
69215798Smckusick 
69315798Smckusick 	/*
69415798Smckusick 	 * insert name into cache (if we want it, and it isn't "." or "..")
69515798Smckusick 	 *
69615798Smckusick 	 * all other cases where making a cache entry would be wrong
69715798Smckusick 	 * have already departed from the code sequence somewhere above.
69815798Smckusick 	 */
69915798Smckusick 	if (bcmp(u.u_dent.d_name, ".", 2) != 0 && !isdotdot && docache) {
70015798Smckusick 		if (ncp != NULL)
70115798Smckusick 			panic("nami: duplicating cache");
70215798Smckusick 
70315798Smckusick 			/*
70415798Smckusick 			 * free the cache slot at head of lru chain
70515798Smckusick 			 */
70615798Smckusick 		if (ncp = nchhead) {
70715798Smckusick 				/* remove from lru chain */
70815798Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
70915798Smckusick 			if (ncp->nc_nxt)
71015798Smckusick 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
71115798Smckusick 			else
71215798Smckusick 				nchtail = ncp->nc_prev;
71315798Smckusick 
71415798Smckusick 				/* remove from old hash chain */
71515798Smckusick 			remque(ncp);
71615798Smckusick 
71715798Smckusick 				/* drop hold on inode (if we had one) */
71815798Smckusick 			if (ncp->nc_ip)
71915798Smckusick 				irele(ncp->nc_ip);
72015798Smckusick 
72115798Smckusick 				/* grab the inode we just found */
72215798Smckusick 			ncp->nc_ip = dp;
72315798Smckusick 			dp->i_count++;
72415798Smckusick 
72515798Smckusick 				/* fill in cache info */
72615798Smckusick 			ncp->nc_ino = pdp->i_number;	/* parents inum */
72715798Smckusick 			ncp->nc_dev = pdp->i_dev;	/* & device */
72815798Smckusick 			ncp->nc_idev = dp->i_dev;	/* our device */
72915798Smckusick 			ncp->nc_nlen = u.u_dent.d_namlen;
73015798Smckusick 			bcopy(u.u_dent.d_name, ncp->nc_name, ncp->nc_nlen);
73115798Smckusick 
73215798Smckusick 				/* link at end of lru chain */
73315798Smckusick 			ncp->nc_nxt = NULL;
73415798Smckusick 			ncp->nc_prev = nchtail;
73515798Smckusick 			*nchtail = ncp;
73615798Smckusick 			nchtail = &ncp->nc_nxt;
73715798Smckusick 
73815798Smckusick 				/* and insert on hash chain */
73915798Smckusick 			insque(ncp, nhp);
74015798Smckusick 		}
74115798Smckusick 	}
74215798Smckusick 
74315798Smckusick haveino:
7447534Sroot 	fs = dp->i_fs;
7457534Sroot 
7467534Sroot 	/*
7477534Sroot 	 * Check for symbolic link
7487534Sroot 	 */
7497534Sroot 	if ((dp->i_mode & IFMT) == IFLNK && (follow || *cp == '/')) {
7507825Sroot 		u_int pathlen = strlen(cp) + 1;
7517534Sroot 
7527534Sroot 		if (dp->i_size + pathlen >= MAXPATHLEN - 1 ||
7537534Sroot 		    ++nlink > MAXSYMLINKS) {
7547534Sroot 			u.u_error = ELOOP;
7557534Sroot 			goto bad2;
7567534Sroot 		}
7578957Sroot 		ovbcopy(cp, nbp->b_un.b_addr + dp->i_size, pathlen);
7587751Sroot 		u.u_error =
7599166Ssam 		    rdwri(UIO_READ, dp, nbp->b_un.b_addr, (int)dp->i_size,
7607825Sroot 			0, 1, (int *)0);
7617534Sroot 		if (u.u_error)
7627534Sroot 			goto bad2;
7637534Sroot 		cp = nbp->b_un.b_addr;
7647534Sroot 		iput(dp);
7655972Swnj 		if (*cp == '/') {
7667534Sroot 			irele(pdp);
7675972Swnj 			while (*cp == '/')
7685972Swnj 				cp++;
7697534Sroot 			if ((dp = u.u_rdir) == NULL)
7707534Sroot 				dp = rootdir;
7717534Sroot 			ilock(dp);
7727534Sroot 			dp->i_count++;
7737534Sroot 		} else {
7747534Sroot 			dp = pdp;
7757534Sroot 			ilock(dp);
7765972Swnj 		}
7777534Sroot 		fs = dp->i_fs;
7787534Sroot 		goto dirloop;
77930Sbill 	}
7807534Sroot 
78130Sbill 	/*
7827534Sroot 	 * Not a symbolic link.  If more pathname,
7837534Sroot 	 * continue at next component, else return.
78430Sbill 	 */
7857534Sroot 	if (*cp == '/') {
7867534Sroot 		while (*cp == '/')
7877534Sroot 			cp++;
7889166Ssam 		irele(pdp);
7897534Sroot 		goto dirloop;
79030Sbill 	}
7915972Swnj 	brelse(nbp);
7929166Ssam 	if (lockparent)
7939166Ssam 		u.u_pdir = pdp;
7949166Ssam 	else
7959166Ssam 		irele(pdp);
7967534Sroot 	return (dp);
7977534Sroot bad2:
7987534Sroot 	irele(pdp);
7997534Sroot bad:
8007534Sroot 	if (bp)
8017534Sroot 		brelse(bp);
8027534Sroot 	if (dp)
8037534Sroot 		iput(dp);
8047534Sroot 	brelse(nbp);
8056571Smckusic 	return (NULL);
80630Sbill }
80730Sbill 
80815798Smckusick 
8097534Sroot dirbad(ip, how)
8107534Sroot 	struct inode *ip;
8117534Sroot 	char *how;
8127534Sroot {
8137534Sroot 
8147534Sroot 	printf("%s: bad dir ino %d at offset %d: %s\n",
8157534Sroot 	    ip->i_fs->fs_fsmnt, ip->i_number, u.u_offset, how);
8167534Sroot }
8177534Sroot 
8187534Sroot dirbadname(ep)
8197534Sroot 	register struct direct *ep;
8207534Sroot {
8217534Sroot 	register int i;
8227534Sroot 
8237534Sroot 	for (i = 0; i < ep->d_namlen; i++)
8247534Sroot 		if (ep->d_name[i] == 0)
8257534Sroot 			return (1);
8267534Sroot 	return (ep->d_name[i]);
8277534Sroot }
8287534Sroot 
82930Sbill /*
8307534Sroot  * Write a directory entry after a call to namei, using the parameters
8317534Sroot  * which it left in the u. area.  The argument ip is the inode which
8327534Sroot  * the new directory entry will refer to.  The u. area field u.u_pdir is
8337534Sroot  * a pointer to the directory to be written, which was left locked by
8347534Sroot  * namei.  Remaining parameters (u.u_offset, u.u_count) indicate
8357534Sroot  * how the space for the new entry is to be gotten.
8367534Sroot  */
8377534Sroot direnter(ip)
8387534Sroot 	struct inode *ip;
8395972Swnj {
8407534Sroot 	register struct direct *ep, *nep;
8417534Sroot 	struct buf *bp;
84211639Ssam 	int loc, spacefree, error = 0;
8438631Sroot 	u_int dsize;
8448631Sroot 	int newentrysize;
8457534Sroot 	char *dirbuf;
8465972Swnj 
8477534Sroot 	u.u_dent.d_ino = ip->i_number;
8487534Sroot 	u.u_segflg = 1;
8497534Sroot 	newentrysize = DIRSIZ(&u.u_dent);
8507534Sroot 	if (u.u_count == 0) {
8517534Sroot 		/*
8527534Sroot 		 * If u.u_count is 0, then namei could find no space in the
8537534Sroot 		 * directory.  In this case u.u_offset will be on a directory
8547534Sroot 		 * block boundary and we will write the new entry into a fresh
8557534Sroot 		 * block.
8567534Sroot 		 */
8577534Sroot 		if (u.u_offset&(DIRBLKSIZ-1))
8587534Sroot 			panic("wdir: newblk");
8597534Sroot 		u.u_dent.d_reclen = DIRBLKSIZ;
86010849Ssam 		error = rdwri(UIO_WRITE, u.u_pdir, (caddr_t)&u.u_dent,
8618631Sroot 		    newentrysize, u.u_offset, 1, (int *)0);
8627534Sroot 		iput(u.u_pdir);
86310849Ssam 		return (error);
8647534Sroot 	}
8657534Sroot 
8667534Sroot 	/*
8677534Sroot 	 * If u.u_count is non-zero, then namei found space for the
8687534Sroot 	 * new entry in the range u.u_offset to u.u_offset+u.u_count.
8697534Sroot 	 * in the directory.  To use this space, we may have to compact
8707534Sroot 	 * the entries located there, by copying them together towards
8717534Sroot 	 * the beginning of the block, leaving the free space in
8727534Sroot 	 * one usable chunk at the end.
8737534Sroot 	 */
8747534Sroot 
8757534Sroot 	/*
8767534Sroot 	 * Increase size of directory if entry eats into new space.
8777534Sroot 	 * This should never push the size past a new multiple of
8787534Sroot 	 * DIRBLKSIZE.
8797534Sroot 	 */
8809166Ssam 	if (u.u_offset + u.u_count > u.u_pdir->i_size)
8817534Sroot 		u.u_pdir->i_size = u.u_offset + u.u_count;
8827534Sroot 
8837534Sroot 	/*
8847534Sroot 	 * Get the block containing the space for the new directory
88510849Ssam 	 * entry.  Should return error by result instead of u.u_error.
8867534Sroot 	 */
8877605Ssam 	bp = blkatoff(u.u_pdir, u.u_offset, (char **)&dirbuf);
8889166Ssam 	if (bp == 0) {
8899166Ssam 		iput(u.u_pdir);
89010849Ssam 		return (u.u_error);
8919166Ssam 	}
8927534Sroot 
8937534Sroot 	/*
8947534Sroot 	 * Find space for the new entry.  In the simple case, the
8957534Sroot 	 * entry at offset base will have the space.  If it does
8967534Sroot 	 * not, then namei arranged that compacting the region
8977534Sroot 	 * u.u_offset to u.u_offset+u.u_count would yield the space.
8987534Sroot 	 */
8997534Sroot 	ep = (struct direct *)dirbuf;
9007534Sroot 	dsize = DIRSIZ(ep);
90111639Ssam 	spacefree = ep->d_reclen - dsize;
9027534Sroot 	for (loc = ep->d_reclen; loc < u.u_count; ) {
9037534Sroot 		nep = (struct direct *)(dirbuf + loc);
9047534Sroot 		if (ep->d_ino) {
9057534Sroot 			/* trim the existing slot */
9067534Sroot 			ep->d_reclen = dsize;
9077534Sroot 			ep = (struct direct *)((char *)ep + dsize);
9087534Sroot 		} else {
9097534Sroot 			/* overwrite; nothing there; header is ours */
91011639Ssam 			spacefree += dsize;
9117534Sroot 		}
9127534Sroot 		dsize = DIRSIZ(nep);
91311639Ssam 		spacefree += nep->d_reclen - dsize;
9147534Sroot 		loc += nep->d_reclen;
9157825Sroot 		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
9167534Sroot 	}
9177534Sroot 	/*
9187534Sroot 	 * Update the pointer fields in the previous entry (if any),
9197534Sroot 	 * copy in the new entry, and write out the block.
9207534Sroot 	 */
9217534Sroot 	if (ep->d_ino == 0) {
92211639Ssam 		if (spacefree + dsize < newentrysize)
9237534Sroot 			panic("wdir: compact1");
92411639Ssam 		u.u_dent.d_reclen = spacefree + dsize;
9257534Sroot 	} else {
92611639Ssam 		if (spacefree < newentrysize)
9277534Sroot 			panic("wdir: compact2");
92811639Ssam 		u.u_dent.d_reclen = spacefree;
9297534Sroot 		ep->d_reclen = dsize;
9307534Sroot 		ep = (struct direct *)((char *)ep + dsize);
9317534Sroot 	}
9328672Sroot 	bcopy((caddr_t)&u.u_dent, (caddr_t)ep, (u_int)newentrysize);
9337534Sroot 	bwrite(bp);
9347534Sroot 	u.u_pdir->i_flag |= IUPD|ICHG;
9357534Sroot 	iput(u.u_pdir);
93610849Ssam 	return (error);
9375972Swnj }
9386571Smckusic 
9399166Ssam /*
9409166Ssam  * Remove a directory entry after a call to namei, using the
9419166Ssam  * parameters which it left in the u. area.  The u. entry
9429166Ssam  * u_offset contains the offset into the directory of the
9439166Ssam  * entry to be eliminated.  The u_count field contains the
9449166Ssam  * size of the previous record in the directory.  If this
9459166Ssam  * is 0, the first entry is being deleted, so we need only
9469166Ssam  * zero the inode number to mark the entry as free.  If the
9479166Ssam  * entry isn't the first in the directory, we must reclaim
9489166Ssam  * the space of the now empty record by adding the record size
9499166Ssam  * to the size of the previous entry.
9509166Ssam  */
9517534Sroot dirremove()
9526571Smckusic {
9537534Sroot 	register struct inode *dp = u.u_pdir;
9547534Sroot 	register struct buf *bp;
9557534Sroot 	struct direct *ep;
9566571Smckusic 
9579269Ssam 	if (u.u_count == 0) {
9587534Sroot 		/*
9597534Sroot 		 * First entry in block: set d_ino to zero.
9607534Sroot 		 */
9619269Ssam 		u.u_dent.d_ino = 0;
9628619Sroot 		(void) rdwri(UIO_WRITE, dp, (caddr_t)&u.u_dent,
9638631Sroot 		    (int)DIRSIZ(&u.u_dent), u.u_offset, 1, (int *)0);
9649269Ssam 	} else {
9657534Sroot 		/*
9667534Sroot 		 * Collapse new free space into previous entry.
9677534Sroot 		 */
9687825Sroot 		bp = blkatoff(dp, (int)(u.u_offset - u.u_count), (char **)&ep);
9697534Sroot 		if (bp == 0)
9707534Sroot 			return (0);
9717534Sroot 		ep->d_reclen += u.u_dent.d_reclen;
9727534Sroot 		bwrite(bp);
9737534Sroot 		dp->i_flag |= IUPD|ICHG;
9747534Sroot 	}
9757534Sroot 	return (1);
9766571Smckusic }
9777534Sroot 
9787605Ssam /*
9799166Ssam  * Rewrite an existing directory entry to point at the inode
9809166Ssam  * supplied.  The parameters describing the directory entry are
9819166Ssam  * set up by a call to namei.
9829166Ssam  */
9839166Ssam dirrewrite(dp, ip)
9849166Ssam 	struct inode *dp, *ip;
9859166Ssam {
9869166Ssam 
9879166Ssam 	u.u_dent.d_ino = ip->i_number;
9889166Ssam 	u.u_error = rdwri(UIO_WRITE, dp, (caddr_t)&u.u_dent,
9899166Ssam 		(int)DIRSIZ(&u.u_dent), u.u_offset, 1, (int *)0);
9909166Ssam 	iput(dp);
9919166Ssam }
9929166Ssam 
9939166Ssam /*
9947605Ssam  * Return buffer with contents of block "offset"
9957605Ssam  * from the beginning of directory "ip".  If "res"
9967605Ssam  * is non-zero, fill it in with a pointer to the
9977605Ssam  * remaining space in the directory.
9987605Ssam  */
9997534Sroot struct buf *
10007605Ssam blkatoff(ip, offset, res)
10017534Sroot 	struct inode *ip;
10027534Sroot 	off_t offset;
10037534Sroot 	char **res;
10047534Sroot {
10057534Sroot 	register struct fs *fs = ip->i_fs;
10068672Sroot 	daddr_t lbn = lblkno(fs, offset);
10077534Sroot 	int base = blkoff(fs, offset);
10087534Sroot 	int bsize = blksize(fs, ip, lbn);
10098672Sroot 	daddr_t bn = fsbtodb(fs, bmap(ip, lbn, B_WRITE, base, bsize));
10107534Sroot 	register struct buf *bp;
10117534Sroot 
10127534Sroot 	if (u.u_error)
10137534Sroot 		return (0);
10147534Sroot 	bp = bread(ip->i_dev, bn, bsize);
10157534Sroot 	if (bp->b_flags & B_ERROR) {
10167534Sroot 		brelse(bp);
10177534Sroot 		return (0);
10187534Sroot 	}
10197534Sroot 	if (res)
10207534Sroot 		*res = bp->b_un.b_addr + base;
10217534Sroot 	return (bp);
10227534Sroot }
10239166Ssam 
10249166Ssam /*
10259166Ssam  * Check if a directory is empty or not.
10269166Ssam  * Inode supplied must be locked.
102712817Ssam  *
102812817Ssam  * Using a struct dirtemplate here is not precisely
102912817Ssam  * what we want, but better than using a struct direct.
103012817Ssam  *
103112817Ssam  * NB: does not handle corrupted directories.
10329166Ssam  */
10339166Ssam dirempty(ip)
10349863Ssam 	register struct inode *ip;
10359166Ssam {
10369166Ssam 	register off_t off;
103712817Ssam 	struct dirtemplate dbuf;
103812817Ssam 	register struct direct *dp = (struct direct *)&dbuf;
10399863Ssam 	int error, count;
104012817Ssam #define	MINDIRSIZ (sizeof (struct dirtemplate) / 2)
10419166Ssam 
10429166Ssam 	for (off = 0; off < ip->i_size; off += dp->d_reclen) {
104312817Ssam 		error = rdwri(UIO_READ, ip, (caddr_t)dp, MINDIRSIZ,
104412817Ssam 		    off, 1, &count);
104512817Ssam 		/*
104612817Ssam 		 * Since we read MINDIRSIZ, residual must
104712817Ssam 		 * be 0 unless we're at end of file.
104812817Ssam 		 */
104912817Ssam 		if (error || count != 0)
10509166Ssam 			return (0);
105112817Ssam 		/* skip empty entries */
10529166Ssam 		if (dp->d_ino == 0)
10539166Ssam 			continue;
105412817Ssam 		/* accept only "." and ".." */
105512817Ssam 		if (dp->d_namlen > 2)
105612817Ssam 			return (0);
10579166Ssam 		if (dp->d_name[0] != '.')
10589166Ssam 			return (0);
105912817Ssam 		/*
106012817Ssam 		 * At this point d_namlen must be 1 or 2.
106112817Ssam 		 * 1 implies ".", 2 implies ".." if second
106212817Ssam 		 * char is also "."
106312817Ssam 		 */
106412817Ssam 		if (dp->d_namlen == 1 || dp->d_name[1] == '.')
10659166Ssam 			continue;
10669166Ssam 		return (0);
10679166Ssam 	}
10689166Ssam 	return (1);
10699166Ssam }
107012815Smckusick 
107112815Smckusick /*
107212815Smckusick  * Check if source directory is in the path of the target directory.
107312815Smckusick  * Target is supplied locked, source is unlocked.
107412815Smckusick  * The target is always iput() before returning.
107512815Smckusick  */
107612815Smckusick checkpath(source, target)
107712815Smckusick 	struct inode *source, *target;
107812815Smckusick {
107912815Smckusick 	struct dirtemplate dirbuf;
108012815Smckusick 	register struct inode *ip;
108112815Smckusick 	int error = 0;
108212815Smckusick 
108312815Smckusick 	ip = target;
108412815Smckusick 	if (ip->i_number == source->i_number) {
108512815Smckusick 		error = EEXIST;
108612815Smckusick 		goto out;
108712815Smckusick 	}
108812815Smckusick 	if (ip->i_number == ROOTINO)
108912815Smckusick 		goto out;
109012815Smckusick 
109112815Smckusick 	for (;;) {
109212815Smckusick 		if ((ip->i_mode&IFMT) != IFDIR) {
109312815Smckusick 			error = ENOTDIR;
109412815Smckusick 			break;
109512815Smckusick 		}
109612815Smckusick 		error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf,
109712815Smckusick 			sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
109812815Smckusick 		if (error != 0)
109912815Smckusick 			break;
110012815Smckusick 		if (dirbuf.dotdot_namlen != 2 ||
110112815Smckusick 		    bcmp(dirbuf.dotdot_name, "..", 3) != 0) {
110212815Smckusick 			error = ENOTDIR;
110312815Smckusick 			break;
110412815Smckusick 		}
110512815Smckusick 		if (dirbuf.dotdot_ino == source->i_number) {
110612815Smckusick 			error = EINVAL;
110712815Smckusick 			break;
110812815Smckusick 		}
110912815Smckusick 		if (dirbuf.dotdot_ino == ROOTINO)
111012815Smckusick 			break;
111112815Smckusick 		iput(ip);
111212815Smckusick 		ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino);
111312815Smckusick 		if (ip == NULL) {
111412815Smckusick 			error = u.u_error;
111512815Smckusick 			break;
111612815Smckusick 		}
111712815Smckusick 	}
111812815Smckusick 
111912815Smckusick out:
112012815Smckusick 	if (error == ENOTDIR)
112112815Smckusick 		printf("checkpath: .. not a directory\n");
112212815Smckusick 	if (ip != NULL)
112312815Smckusick 		iput(ip);
112412815Smckusick 	return (error);
112512815Smckusick }
112615798Smckusick 
112715798Smckusick /*
112815798Smckusick  * Name cache initialization, from main() when we are booting
112915798Smckusick  */
113015798Smckusick nchinit()
113115798Smckusick {
113215798Smckusick 	register union nchash *nchp;
113315798Smckusick 	register struct nch *ncp;
113415798Smckusick 
113515798Smckusick 	nchhead = 0;
113615798Smckusick 	nchtail = &nchhead;
113715798Smckusick 
113815798Smckusick 	for (ncp = nch; ncp < &nch[nchsize]; ncp++) {
113915798Smckusick 		ncp->nc_forw = ncp;			/* hash chain */
114015798Smckusick 		ncp->nc_back = ncp;
114115798Smckusick 
114215798Smckusick 		ncp->nc_nxt = NULL;			/* lru chain */
114315798Smckusick 		*nchtail = ncp;
114415798Smckusick 		ncp->nc_prev = nchtail;
114515798Smckusick 		nchtail = &ncp->nc_nxt;
114615798Smckusick 
114715798Smckusick 		/* all else is zero already */
114815798Smckusick 	}
114915798Smckusick 
115015798Smckusick 	for (nchp = nchash; nchp < &nchash[NCHHASH]; nchp++) {
115115798Smckusick 		nchp->nch_head[0] = nchp;
115215798Smckusick 		nchp->nch_head[1] = nchp;
115315798Smckusick 	}
115415798Smckusick }
115515798Smckusick 
115615798Smckusick /*
115715798Smckusick  * Cache flush, called when filesys is umounted to
115815798Smckusick  * remove entries that would now be invalid
115915798Smckusick  *
116015798Smckusick  * The line "nxtcp = nchhead" near the end is to avoid potential problems
116115798Smckusick  * if the cache lru chain is modified while we are dumping the
116215798Smckusick  * inode.  This makes the algorithm O(n^2), but do you think I care?
116315798Smckusick  */
116415798Smckusick nchinval(dev)
116515798Smckusick 	register dev_t dev;
116615798Smckusick {
116715798Smckusick 	register struct nch *ncp, *nxtcp;
116815798Smckusick 
116915798Smckusick 	for (ncp = nchhead; ncp; ncp = nxtcp) {
117015798Smckusick 		nxtcp = ncp->nc_nxt;
117115798Smckusick 
117215798Smckusick 		if (ncp->nc_ip == NULL ||
117315798Smckusick 		    (ncp->nc_idev != dev && ncp->nc_dev != dev))
117415798Smckusick 			continue;
117515798Smckusick 
117615798Smckusick 		ncp->nc_idev = NODEV;
117715798Smckusick 		ncp->nc_dev = NODEV;
117815798Smckusick 		ncp->nc_ino = 0;
117915798Smckusick 
118015798Smckusick 			/* remove the entry from its hash chain */
118115798Smckusick 		remque(ncp);
118215798Smckusick 			/* and make a dummy one */
118315798Smckusick 		ncp->nc_forw = ncp;
118415798Smckusick 		ncp->nc_back = ncp;
118515798Smckusick 
118615798Smckusick 			/* delete this entry from LRU chain */
118715798Smckusick 		*ncp->nc_prev = nxtcp;
118815798Smckusick 		if (nxtcp)
118915798Smckusick 			nxtcp->nc_prev = ncp->nc_prev;
119015798Smckusick 		else
119115798Smckusick 			nchtail = ncp->nc_prev;
119215798Smckusick 
119315798Smckusick 			/* free the inode we had */
119415798Smckusick 		irele(ncp->nc_ip);
119515798Smckusick 		ncp->nc_ip = NULL;
119615798Smckusick 
119715798Smckusick 			/* cause rescan of list, it may have altered */
119815798Smckusick 		nxtcp = nchhead;
119915798Smckusick 			/* put the now-free entry at head of LRU */
120015798Smckusick 		ncp->nc_nxt = nxtcp;
120115798Smckusick 		ncp->nc_prev = &nchhead;
120215798Smckusick 		nxtcp->nc_prev = &ncp->nc_nxt;
120315798Smckusick 		nchhead = ncp;
120415798Smckusick 	}
120515798Smckusick }
1206