xref: /csrg-svn/sys/kern/vfs_lookup.c (revision 18027)
1*18027Smckusick /*	vfs_lookup.c	6.18	85/02/20	*/
230Sbill 
317100Sbloom #include "param.h"
417100Sbloom #include "systm.h"
517100Sbloom #include "inode.h"
617100Sbloom #include "fs.h"
717100Sbloom #include "mount.h"
817100Sbloom #include "dir.h"
917100Sbloom #include "user.h"
1017100Sbloom #include "buf.h"
1117100Sbloom #include "conf.h"
1217100Sbloom #include "uio.h"
1317100Sbloom #include "kernel.h"
1430Sbill 
157605Ssam struct	buf *blkatoff();
1616681Smckusick struct	buf *freenamebuf;
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  *
4516688Smckusick  * The segflg defines whether the name is to be copied from user
4616688Smckusick  * space or kernel space.
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  *
5816688Smckusick  * The FOLLOW flag is set 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:
9016688Smckusick  *	copy next component of name to ndp->ni_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 *
11316688Smckusick namei(ndp)
11416688Smckusick 	register struct nameidata *ndp;
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 */
13416688Smckusick 	int prevoff;			/* ndp->ni_offset of previous entry */
1357534Sroot 	int nlink = 0;			/* number of symbolic links taken */
1367534Sroot 	struct inode *pdp;		/* saved dp during symlink work */
13716688Smckusick 	int error, 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 ".." */
14316688Smckusick 	int flag;			/* op ie, LOOKUP, CREATE, or DELETE */
144*18027Smckusick 	off_t enduseful;		/* pointer past last used dir slot */
14530Sbill 
14616688Smckusick 	lockparent = ndp->ni_nameiop & LOCKPARENT;
14716688Smckusick 	docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
14816688Smckusick 	flag = ndp->ni_nameiop &~ (LOCKPARENT|NOCACHE|FOLLOW);
14915798Smckusick 	if (flag == DELETE)
15015798Smckusick 		docache = 0;
15130Sbill 	/*
1527534Sroot 	 * Get a buffer for the name to be translated, and copy the
1537534Sroot 	 * name into the buffer.
1545972Swnj 	 */
15516681Smckusick 	nbp = freenamebuf;
15616681Smckusick 	if (nbp == NULL)
15716681Smckusick 		nbp = geteblk(MAXPATHLEN);
15816681Smckusick 	else
15916681Smckusick 		freenamebuf = nbp->av_forw;
16016688Smckusick 	if (ndp->ni_segflg == UIO_SYSSPACE)
16116705Smckusick 		error = copystr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN,
16216705Smckusick 		    (u_int *)0);
16316688Smckusick 	else
16416705Smckusick 		error = copyinstr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN,
16516705Smckusick 		    (u_int *)0);
16616688Smckusick 	if (error) {
16716688Smckusick 		u.u_error = error;
16816688Smckusick 		goto bad;
1695972Swnj 	}
1707534Sroot 
1715972Swnj 	/*
1727534Sroot 	 * Get starting directory.
17330Sbill 	 */
1747534Sroot 	cp = nbp->b_un.b_addr;
1755972Swnj 	if (*cp == '/') {
1765972Swnj 		while (*cp == '/')
1775972Swnj 			cp++;
17830Sbill 		if ((dp = u.u_rdir) == NULL)
17930Sbill 			dp = rootdir;
1807534Sroot 	} else
1817534Sroot 		dp = u.u_cdir;
1827534Sroot 	fs = dp->i_fs;
18316666Smckusick 	ILOCK(dp);
1845972Swnj 	dp->i_count++;
18516688Smckusick 	ndp->ni_pdir = (struct inode *)0xc0000000;		/* illegal */
186*18027Smckusick 	ndp->ni_endoff = 0;
1877534Sroot 
1887534Sroot 	/*
1897534Sroot 	 * We come to dirloop to search a new directory.
1907534Sroot 	 * The directory must be locked so that it can be
1917534Sroot 	 * iput, and fs must be already set to dp->i_fs.
1927534Sroot 	 */
1936571Smckusic dirloop:
19430Sbill 	/*
1957534Sroot 	 * Check accessiblity of directory.
19630Sbill 	 */
1977534Sroot 	if ((dp->i_mode&IFMT) != IFDIR) {
19830Sbill 		u.u_error = ENOTDIR;
1997534Sroot 		goto bad;
2007534Sroot 	}
2017534Sroot 	if (access(dp, IEXEC))
2027534Sroot 		goto bad;
2037534Sroot 
2046384Swnj dirloop2:
2057534Sroot 	/*
20616688Smckusick 	 * Copy next component of name to ndp->ni_dent.
2077534Sroot 	 */
20815798Smckusick 	hash = 0;
2097534Sroot 	for (i = 0; *cp != 0 && *cp != '/'; cp++) {
2106571Smckusic 		if (i >= MAXNAMLEN) {
2115972Swnj 			u.u_error = ENOENT;
2127534Sroot 			goto bad;
2135972Swnj 		}
21416688Smckusick 		if ((*cp&0377) == ('/'|0200) || (*cp&0200) && flag != DELETE) {
21516688Smckusick 			u.u_error = EPERM;
21616688Smckusick 			goto bad;
21716688Smckusick 		}
21816688Smckusick 		ndp->ni_dent.d_name[i++] = *cp;
21915798Smckusick 		hash += (unsigned char)*cp * i;
2205972Swnj 	}
22116688Smckusick 	ndp->ni_dent.d_namlen = i;
22216688Smckusick 	ndp->ni_dent.d_name[i] = '\0';
22316658Smckusick 	isdotdot = (i == 2 &&
22416688Smckusick 		ndp->ni_dent.d_name[0] == '.' && ndp->ni_dent.d_name[1] == '.');
2257534Sroot 
2267534Sroot 	/*
2277534Sroot 	 * Check for degenerate name (e.g. / or "")
2287534Sroot 	 * which is a way of talking about a directory,
2297534Sroot 	 * e.g. like "/." or ".".
2307534Sroot 	 */
23116688Smckusick 	if (ndp->ni_dent.d_name[0] == '\0') {
23215798Smckusick 		if (flag != LOOKUP || lockparent) {
23314937Smckusick 			u.u_error = EISDIR;
2347534Sroot 			goto bad;
2355972Swnj 		}
23616681Smckusick 		nbp->av_forw = freenamebuf;
23716681Smckusick 		freenamebuf = nbp;
2386571Smckusic 		return (dp);
2395972Swnj 	}
2407534Sroot 
2416571Smckusic 	/*
24215798Smckusick 	 * We now have a segment name to search for, and a directory to search.
24315798Smckusick 	 *
24415798Smckusick 	 * Before tediously performing a linear scan of the directory,
24515798Smckusick 	 * check the name cache to see if the directory/name pair
24615798Smckusick 	 * we are looking for is known already.  We don't do this
24715798Smckusick 	 * if the segment name is long, simply so the cache can avoid
24815798Smckusick 	 * holding long names (which would either waste space, or
24915798Smckusick 	 * add greatly to the complexity).
25015798Smckusick 	 */
25116688Smckusick 	if (ndp->ni_dent.d_namlen > NCHNAMLEN) {
25215798Smckusick 		nchstats.ncs_long++;
25315798Smckusick 		docache = 0;
25415798Smckusick 	} else {
25515798Smckusick 		nhp = &nchash[NHASH(hash, dp->i_number, dp->i_dev)];
25615798Smckusick 		for (ncp = nhp->nch_forw; ncp != (struct nch *)nhp;
25715798Smckusick 		    ncp = ncp->nc_forw) {
25815798Smckusick 			if (ncp->nc_ino == dp->i_number &&
25915798Smckusick 			    ncp->nc_dev == dp->i_dev &&
26016688Smckusick 			    ncp->nc_nlen == ndp->ni_dent.d_namlen &&
26116688Smckusick 			    !bcmp(ncp->nc_name, ndp->ni_dent.d_name,
26216688Smckusick 				ncp->nc_nlen))
26315798Smckusick 				break;
26415798Smckusick 		}
26515798Smckusick 
26615798Smckusick 		if (ncp == (struct nch *)nhp) {
26715798Smckusick 			nchstats.ncs_miss++;
26815798Smckusick 			ncp = NULL;
26915798Smckusick 		} else {
27016658Smckusick 			if (ncp->nc_id != ncp->nc_ip->i_id) {
27116643Ssam 				nchstats.ncs_falsehits++;
27216658Smckusick 			} else if (*cp == '\0' && !docache) {
27316658Smckusick 				nchstats.ncs_badhits++;
27416658Smckusick 			} else {
27515798Smckusick 
27615798Smckusick 					/*
27715798Smckusick 					 * move this slot to end of LRU
27815798Smckusick 					 * chain, if not already there
27915798Smckusick 					 */
28015798Smckusick 				if (ncp->nc_nxt) {
28115798Smckusick 						/* remove from LRU chain */
28215798Smckusick 					*ncp->nc_prev = ncp->nc_nxt;
28315798Smckusick 					ncp->nc_nxt->nc_prev = ncp->nc_prev;
28415798Smckusick 
28515798Smckusick 						/* and replace at end of it */
28615798Smckusick 					ncp->nc_nxt = NULL;
28715798Smckusick 					ncp->nc_prev = nchtail;
28815798Smckusick 					*nchtail = ncp;
28915798Smckusick 					nchtail = &ncp->nc_nxt;
29015798Smckusick 				}
29115798Smckusick 
29216658Smckusick 				/*
29316658Smckusick 				 * Get the next inode in the path.
29416666Smckusick 				 * See comment above other `IUNLOCK' code for
29516658Smckusick 				 * an explaination of the locking protocol.
29616658Smckusick 				 */
29715798Smckusick 				pdp = dp;
29815798Smckusick 				dp = ncp->nc_ip;
29915798Smckusick 				if (dp == NULL)
30015798Smckusick 					panic("nami: null cache ino");
30116643Ssam 				if (pdp == dp)
30216643Ssam 					dp->i_count++;
30316666Smckusick 				else {
30416658Smckusick 					if (isdotdot) {
30516666Smckusick 						IUNLOCK(pdp);
30616658Smckusick 						igrab(dp);
30716658Smckusick 					} else {
30816658Smckusick 						igrab(dp);
30916666Smckusick 						IUNLOCK(pdp);
31016658Smckusick 					}
31116643Ssam 				}
31215798Smckusick 
31316658Smckusick 				/*
31416658Smckusick 				 * Verify that the inode that we got
31516658Smckusick 				 * did not change while we were waiting
31616658Smckusick 				 * for it to be locked.
31716658Smckusick 				 */
31816658Smckusick 				if (ncp->nc_id != ncp->nc_ip->i_id) {
31916658Smckusick 					iput(dp);
32016666Smckusick 					ILOCK(pdp);
32116658Smckusick 					dp = pdp;
32216658Smckusick 					nchstats.ncs_falsehits++;
32316658Smckusick 				} else {
32416688Smckusick 					ndp->ni_dent.d_ino = dp->i_number;
32516688Smckusick 					/* ni_dent.d_reclen is garbage ... */
32616658Smckusick 					nchstats.ncs_goodhits++;
32716658Smckusick 					goto haveino;
32816658Smckusick 				}
32916658Smckusick 			}
33015798Smckusick 
33115798Smckusick 			/*
33216643Ssam 			 * Last component and we are renaming or deleting,
33316643Ssam 			 * the cache entry is invalid, or otherwise don't
33416643Ssam 			 * want cache entry to exist.
33515798Smckusick 			 */
33615798Smckusick 
33715798Smckusick 				/* remove from LRU chain */
33815798Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
33915798Smckusick 			if (ncp->nc_nxt)
34015798Smckusick 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
34115798Smckusick 			else
34215798Smckusick 				nchtail = ncp->nc_prev;
34315798Smckusick 
34415798Smckusick 				/* remove from hash chain */
34515798Smckusick 			remque(ncp);
34615798Smckusick 
34715798Smckusick 				/* insert at head of LRU list (first to grab) */
34815798Smckusick 			ncp->nc_nxt = nchhead;
34915798Smckusick 			ncp->nc_prev = &nchhead;
35015798Smckusick 			nchhead->nc_prev = &ncp->nc_nxt;
35115798Smckusick 			nchhead = ncp;
35215798Smckusick 
35315798Smckusick 				/* and make a dummy hash chain */
35415798Smckusick 			ncp->nc_forw = ncp;
35515798Smckusick 			ncp->nc_back = ncp;
35615798Smckusick 
35715798Smckusick 			ncp = NULL;
35815798Smckusick 		}
35915798Smckusick 	}
36015798Smckusick 
36115798Smckusick 	/*
3627534Sroot 	 * Suppress search for slots unless creating
3637534Sroot 	 * file and at end of pathname, in which case
3647534Sroot 	 * we watch for a place to put the new file in
3657534Sroot 	 * case it doesn't already exist.
3666571Smckusic 	 */
3677534Sroot 	slotstatus = FOUND;
3689166Ssam 	if (flag == CREATE && *cp == 0) {
3697534Sroot 		slotstatus = NONE;
3707534Sroot 		slotfreespace = 0;
37116688Smckusick 		slotneeded = DIRSIZ(&ndp->ni_dent);
3727534Sroot 	}
37315660Smckusick 	/*
37415660Smckusick 	 * If this is the same directory that this process
37515660Smckusick 	 * previously searched, pick up where we last left off.
37615798Smckusick 	 * We cache only lookups as these are the most common
37715660Smckusick 	 * and have the greatest payoff. Caching CREATE has little
37815660Smckusick 	 * benefit as it usually must search the entire directory
37915660Smckusick 	 * to determine that the entry does not exist. Caching the
38015660Smckusick 	 * location of the last DELETE has not reduced profiling time
38115660Smckusick 	 * and hence has been removed in the interest of simplicity.
38215660Smckusick 	 */
38315660Smckusick 	if (flag != LOOKUP || dp->i_number != u.u_ncache.nc_inumber ||
38415660Smckusick 	    dp->i_dev != u.u_ncache.nc_dev) {
38516688Smckusick 		ndp->ni_offset = 0;
38615660Smckusick 		numdirpasses = 1;
38715660Smckusick 	} else {
38815798Smckusick 		if ((dp->i_flag & ICHG) || dp->i_ctime >= u.u_ncache.nc_time) {
38917698Smckusick 			if (u.u_ncache.nc_prevoffset > dp->i_size)
39017698Smckusick 				u.u_ncache.nc_prevoffset = 0;
39117698Smckusick 			else
39217698Smckusick 				u.u_ncache.nc_prevoffset &= ~(DIRBLKSIZ - 1);
39315660Smckusick 			u.u_ncache.nc_time = time.tv_sec;
39415660Smckusick 		}
39516688Smckusick 		ndp->ni_offset = u.u_ncache.nc_prevoffset;
39616688Smckusick 		entryoffsetinblock = blkoff(fs, ndp->ni_offset);
39715660Smckusick 		if (entryoffsetinblock != 0) {
39816688Smckusick 			bp = blkatoff(dp, ndp->ni_offset, (char **)0);
39915660Smckusick 			if (bp == 0)
40015660Smckusick 				goto bad;
40115660Smckusick 		}
40215660Smckusick 		numdirpasses = 2;
40315798Smckusick 		nchstats.ncs_2passes++;
40415660Smckusick 	}
40515660Smckusick 	endsearch = roundup(dp->i_size, DIRBLKSIZ);
406*18027Smckusick 	enduseful = 0;
4077534Sroot 
40815660Smckusick searchloop:
40916688Smckusick 	while (ndp->ni_offset < endsearch) {
4105972Swnj 		/*
4115972Swnj 		 * If offset is on a block boundary,
4125972Swnj 		 * read the next directory block.
4135972Swnj 		 * Release previous if it exists.
4145972Swnj 		 */
41516688Smckusick 		if (blkoff(fs, ndp->ni_offset) == 0) {
4165972Swnj 			if (bp != NULL)
4175972Swnj 				brelse(bp);
41816688Smckusick 			bp = blkatoff(dp, ndp->ni_offset, (char **)0);
4197534Sroot 			if (bp == 0)
4207534Sroot 				goto bad;
4217534Sroot 			entryoffsetinblock = 0;
4225972Swnj 		}
4237534Sroot 
4245972Swnj 		/*
4257534Sroot 		 * If still looking for a slot, and at a DIRBLKSIZE
42616657Smckusick 		 * boundary, have to start looking for free space again.
4276571Smckusic 		 */
4287534Sroot 		if (slotstatus == NONE &&
4297534Sroot 		    (entryoffsetinblock&(DIRBLKSIZ-1)) == 0) {
4307534Sroot 			slotoffset = -1;
4317534Sroot 			slotfreespace = 0;
4327534Sroot 		}
4337534Sroot 
4347534Sroot 		/*
43516657Smckusick 		 * Get pointer to next entry.
43616657Smckusick 		 * Full validation checks are slow, so we only check
43716657Smckusick 		 * enough to insure forward progress through the
43816657Smckusick 		 * directory. Complete checks can be run by patching
43916657Smckusick 		 * "dirchk" to be true.
4407534Sroot 		 */
4417534Sroot 		ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock);
44216657Smckusick 		if (ep->d_reclen <= 0 ||
44316657Smckusick 		    dirchk && dirbadentry(ep, entryoffsetinblock)) {
44416688Smckusick 			dirbad(dp, ndp->ni_offset, "mangled entry");
44516657Smckusick 			i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
44616688Smckusick 			ndp->ni_offset += i;
4477534Sroot 			entryoffsetinblock += i;
4486571Smckusic 			continue;
4496571Smckusic 		}
4507534Sroot 
4516571Smckusic 		/*
4527534Sroot 		 * If an appropriate sized slot has not yet been found,
4536571Smckusic 		 * check to see if one is available. Also accumulate space
4546571Smckusic 		 * in the current block so that we can determine if
4556571Smckusic 		 * compaction is viable.
4566571Smckusic 		 */
4577534Sroot 		if (slotstatus != FOUND) {
4587534Sroot 			int size = ep->d_reclen;
4597534Sroot 
4606571Smckusic 			if (ep->d_ino != 0)
4616571Smckusic 				size -= DIRSIZ(ep);
4626571Smckusic 			if (size > 0) {
4637534Sroot 				if (size >= slotneeded) {
4647534Sroot 					slotstatus = FOUND;
46516688Smckusick 					slotoffset = ndp->ni_offset;
4667534Sroot 					slotsize = ep->d_reclen;
4677534Sroot 				} else if (slotstatus == NONE) {
4687534Sroot 					slotfreespace += size;
4697534Sroot 					if (slotoffset == -1)
47016688Smckusick 						slotoffset = ndp->ni_offset;
4717534Sroot 					if (slotfreespace >= slotneeded) {
4727534Sroot 						slotstatus = COMPACT;
47316688Smckusick 						slotsize = ndp->ni_offset +
47416688Smckusick 						      ep->d_reclen - slotoffset;
4757534Sroot 					}
4766571Smckusic 				}
4776571Smckusic 			}
4786571Smckusic 		}
4797534Sroot 
4806571Smckusic 		/*
4817534Sroot 		 * Check for a name match.
4825972Swnj 		 */
4837534Sroot 		if (ep->d_ino) {
48416688Smckusick 			if (ep->d_namlen == ndp->ni_dent.d_namlen &&
48516688Smckusick 			    !bcmp(ndp->ni_dent.d_name, ep->d_name,
48616688Smckusick 				ep->d_namlen))
4877534Sroot 				goto found;
4887534Sroot 		}
48916688Smckusick 		prevoff = ndp->ni_offset;
49016688Smckusick 		ndp->ni_offset += ep->d_reclen;
4917534Sroot 		entryoffsetinblock += ep->d_reclen;
492*18027Smckusick 		if (ep->d_ino)
493*18027Smckusick 			enduseful = ndp->ni_offset;
4947534Sroot 	}
49515798Smckusick /* notfound: */
49615660Smckusick 	/*
49715798Smckusick 	 * If we started in the middle of the directory and failed
49815660Smckusick 	 * to find our target, we must check the beginning as well.
49915660Smckusick 	 */
50015660Smckusick 	if (numdirpasses == 2) {
50115660Smckusick 		numdirpasses--;
50216688Smckusick 		ndp->ni_offset = 0;
50315660Smckusick 		endsearch = u.u_ncache.nc_prevoffset;
50415660Smckusick 		goto searchloop;
50515660Smckusick 	}
5067534Sroot 	/*
5077534Sroot 	 * If creating, and at end of pathname and current
5089166Ssam 	 * directory has not been removed, then can consider
5099166Ssam 	 * allowing file to be created.
5107534Sroot 	 */
5119166Ssam 	if (flag == CREATE && *cp == 0 && dp->i_nlink != 0) {
5125972Swnj 		/*
5137534Sroot 		 * Access for write is interpreted as allowing
5147534Sroot 		 * creation of files in the directory.
5155972Swnj 		 */
5167534Sroot 		if (access(dp, IWRITE))
5177534Sroot 			goto bad;
5185972Swnj 		/*
5197534Sroot 		 * Return an indication of where the new directory
5207534Sroot 		 * entry should be put.  If we didn't find a slot,
52116688Smckusick 		 * then set ndp->ni_count to 0 indicating that the new
52216688Smckusick 		 * slot belongs at the end of the directory. If we found
52316688Smckusick 		 * a slot, then the new entry can be put in the range
52416688Smckusick 		 * [ndp->ni_offset .. ndp->ni_offset + ndp->ni_count)
5255972Swnj 		 */
52615660Smckusick 		if (slotstatus == NONE) {
52716688Smckusick 			ndp->ni_offset = roundup(dp->i_size, DIRBLKSIZ);
52816688Smckusick 			ndp->ni_count = 0;
529*18027Smckusick 			enduseful = ndp->ni_offset;
53015660Smckusick 		} else {
53116688Smckusick 			ndp->ni_offset = slotoffset;
53216688Smckusick 			ndp->ni_count = slotsize;
533*18027Smckusick 			if (enduseful < slotoffset + slotsize)
534*18027Smckusick 				enduseful = slotoffset + slotsize;
5355972Swnj 		}
536*18027Smckusick 		ndp->ni_endoff = roundup(enduseful, DIRBLKSIZ);
5377534Sroot 		dp->i_flag |= IUPD|ICHG;
5387534Sroot 		if (bp)
5397534Sroot 			brelse(bp);
54016681Smckusick 		nbp->av_forw = freenamebuf;
54116681Smckusick 		freenamebuf = nbp;
5425972Swnj 		/*
5437534Sroot 		 * We return with the directory locked, so that
5447534Sroot 		 * the parameters we set up above will still be
5457534Sroot 		 * valid if we actually decide to do a direnter().
5467534Sroot 		 * We return NULL to indicate that the entry doesn't
5477534Sroot 		 * currently exist, leaving a pointer to the (locked)
54816688Smckusick 		 * directory inode in ndp->ni_pdir.
5495972Swnj 		 */
55016688Smckusick 		ndp->ni_pdir = dp;
5517534Sroot 		return (NULL);
5527534Sroot 	}
5537534Sroot 	u.u_error = ENOENT;
5547534Sroot 	goto bad;
5557534Sroot found:
55615798Smckusick 	if (numdirpasses == 2)
55715798Smckusick 		nchstats.ncs_pass2++;
5587534Sroot 	/*
5597534Sroot 	 * Check that directory length properly reflects presence
5607534Sroot 	 * of this entry.
5617534Sroot 	 */
5627605Ssam 	if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) {
56316688Smckusick 		dirbad(dp, ndp->ni_offset, "i_size too small");
5647605Ssam 		dp->i_size = entryoffsetinblock + DIRSIZ(ep);
5657534Sroot 		dp->i_flag |= IUPD|ICHG;
5667534Sroot 	}
5677534Sroot 
5687534Sroot 	/*
56915660Smckusick 	 * Found component in pathname.
57015798Smckusick 	 * If the final component of path name, save information
57115660Smckusick 	 * in the cache as to where the entry was found.
5727534Sroot 	 */
57315660Smckusick 	if (*cp == '\0' && flag == LOOKUP) {
57416688Smckusick 		u.u_ncache.nc_prevoffset = ndp->ni_offset;
57515660Smckusick 		u.u_ncache.nc_inumber = dp->i_number;
57615660Smckusick 		u.u_ncache.nc_dev = dp->i_dev;
57715660Smckusick 		u.u_ncache.nc_time = time.tv_sec;
57815660Smckusick 	}
57915660Smckusick 	/*
58016688Smckusick 	 * Save directory entry in ndp->ni_dent,
58115660Smckusick 	 * and release directory buffer.
58215660Smckusick 	 */
58316688Smckusick 	bcopy((caddr_t)ep, (caddr_t)&ndp->ni_dent, (u_int)DIRSIZ(ep));
5847534Sroot 	brelse(bp);
5857534Sroot 	bp = NULL;
5867534Sroot 
5877534Sroot 	/*
5887534Sroot 	 * If deleting, and at end of pathname, return
5897534Sroot 	 * parameters which can be used to remove file.
5909166Ssam 	 * If the lockparent flag isn't set, we return only
59116688Smckusick 	 * the directory (in ndp->ni_pdir), otherwise we go
5929166Ssam 	 * on and lock the inode, being careful with ".".
5937534Sroot 	 */
5949166Ssam 	if (flag == DELETE && *cp == 0) {
5957534Sroot 		/*
5967534Sroot 		 * Write access to directory required to delete files.
5977534Sroot 		 */
5987534Sroot 		if (access(dp, IWRITE))
5997534Sroot 			goto bad;
60016688Smckusick 		ndp->ni_pdir = dp;		/* for dirremove() */
6017534Sroot 		/*
60216688Smckusick 		 * Return pointer to current entry in ndp->ni_offset,
6037534Sroot 		 * and distance past previous entry (if there
60416688Smckusick 		 * is a previous entry in this block) in ndp->ni_count.
60516688Smckusick 		 * Save directory inode pointer in ndp->ni_pdir for dirremove().
6067534Sroot 		 */
60716688Smckusick 		if ((ndp->ni_offset&(DIRBLKSIZ-1)) == 0)
60816688Smckusick 			ndp->ni_count = 0;
6097534Sroot 		else
61016688Smckusick 			ndp->ni_count = ndp->ni_offset - prevoff;
6119166Ssam 		if (lockparent) {
61216688Smckusick 			if (dp->i_number == ndp->ni_dent.d_ino)
6139166Ssam 				dp->i_count++;
6149166Ssam 			else {
61516688Smckusick 				dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino);
6169166Ssam 				if (dp == NULL) {
61716688Smckusick 					iput(ndp->ni_pdir);
6189166Ssam 					goto bad;
6199166Ssam 				}
62015798Smckusick 				/*
62116046Skarels 				 * If directory is "sticky", then user must own
62215798Smckusick 				 * the directory, or the file in it, else he
62315798Smckusick 				 * may not delete it (unless he's root). This
62415798Smckusick 				 * implements append-only directories.
62515798Smckusick 				 */
62616688Smckusick 				if ((ndp->ni_pdir->i_mode & ISVTX) &&
62715798Smckusick 				    u.u_uid != 0 &&
62816688Smckusick 				    u.u_uid != ndp->ni_pdir->i_uid &&
62915798Smckusick 				    dp->i_uid != u.u_uid) {
63016688Smckusick 					iput(ndp->ni_pdir);
63115798Smckusick 					u.u_error = EPERM;
63215798Smckusick 					goto bad;
63315798Smckusick 				}
6349166Ssam 			}
6359166Ssam 		}
63616681Smckusick 		nbp->av_forw = freenamebuf;
63716681Smckusick 		freenamebuf = nbp;
6387534Sroot 		return (dp);
6397534Sroot 	}
6407534Sroot 
6417534Sroot 	/*
6427534Sroot 	 * Special handling for ".." allowing chdir out of mounted
6437534Sroot 	 * file system: indirect .. in root inode to reevaluate
6447534Sroot 	 * in directory file system was mounted on.
6457534Sroot 	 */
64616658Smckusick 	if (isdotdot) {
6477534Sroot 		if (dp == u.u_rdir)
64816688Smckusick 			ndp->ni_dent.d_ino = dp->i_number;
64916688Smckusick 		else if (ndp->ni_dent.d_ino == ROOTINO &&
6507534Sroot 		   dp->i_number == ROOTINO) {
6517534Sroot 			for (i = 1; i < NMOUNT; i++)
6527534Sroot 			if (mount[i].m_bufp != NULL &&
6537534Sroot 			   mount[i].m_dev == dp->i_dev) {
6546571Smckusic 				iput(dp);
6557534Sroot 				dp = mount[i].m_inodp;
65616666Smckusick 				ILOCK(dp);
6575972Swnj 				dp->i_count++;
6587534Sroot 				fs = dp->i_fs;
6597534Sroot 				cp -= 2;     /* back over .. */
6607534Sroot 				goto dirloop2;
6615972Swnj 			}
66230Sbill 		}
6637534Sroot 	}
6647534Sroot 
6657534Sroot 	/*
6669166Ssam 	 * If rewriting (rename), return the inode and the
6679166Ssam 	 * information required to rewrite the present directory
6689166Ssam 	 * Must get inode of directory entry to verify it's a
6699166Ssam 	 * regular file, or empty directory.
6709166Ssam 	 */
6719166Ssam 	if ((flag == CREATE && lockparent) && *cp == 0) {
6729166Ssam 		if (access(dp, IWRITE))
6739166Ssam 			goto bad;
67416688Smckusick 		ndp->ni_pdir = dp;		/* for dirrewrite() */
6759166Ssam 		/*
6769166Ssam 		 * Careful about locking second inode.
6779166Ssam 		 * This can only occur if the target is ".".
6789166Ssam 		 */
67916688Smckusick 		if (dp->i_number == ndp->ni_dent.d_ino) {
6809166Ssam 			u.u_error = EISDIR;		/* XXX */
6819166Ssam 			goto bad;
6829166Ssam 		}
68316688Smckusick 		dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino);
6849166Ssam 		if (dp == NULL) {
68516688Smckusick 			iput(ndp->ni_pdir);
6869166Ssam 			goto bad;
6879166Ssam 		}
68816681Smckusick 		nbp->av_forw = freenamebuf;
68916681Smckusick 		freenamebuf = nbp;
6909166Ssam 		return (dp);
6919166Ssam 	}
6929166Ssam 
6939166Ssam 	/*
69412011Smckusick 	 * Check for symbolic link, which may require us to massage the
69512011Smckusick 	 * name before we continue translation.  We do not `iput' the
69612011Smckusick 	 * directory because we may need it again if the symbolic link
69712011Smckusick 	 * is relative to the current directory.  Instead we save it
69812011Smckusick 	 * unlocked as "pdp".  We must get the target inode before unlocking
69912011Smckusick 	 * the directory to insure that the inode will not be removed
70012011Smckusick 	 * before we get it.  We prevent deadlock by always fetching
70112011Smckusick 	 * inodes from the root, moving down the directory tree. Thus
70212011Smckusick 	 * when following backward pointers ".." we must unlock the
70312011Smckusick 	 * parent directory before getting the requested directory.
70412011Smckusick 	 * There is a potential race condition here if both the current
70512011Smckusick 	 * and parent directories are removed before the `iget' for the
70612011Smckusick 	 * inode associated with ".." returns.  We hope that this occurs
70712011Smckusick 	 * infrequently since we cannot avoid this race condition without
70812492Ssam 	 * implementing a sophisticated deadlock detection algorithm.
70912011Smckusick 	 * Note also that this simple deadlock detection scheme will not
71012011Smckusick 	 * work if the file system has any hard links other than ".."
71112011Smckusick 	 * that point backwards in the directory structure.
7127534Sroot 	 */
7137534Sroot 	pdp = dp;
71415798Smckusick 	if (isdotdot) {
71516666Smckusick 		IUNLOCK(pdp);	/* race to get the inode */
71616688Smckusick 		dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino);
71712011Smckusick 		if (dp == NULL)
71812011Smckusick 			goto bad2;
71916688Smckusick 	} else if (dp->i_number == ndp->ni_dent.d_ino) {
72012011Smckusick 		dp->i_count++;	/* we want ourself, ie "." */
72112011Smckusick 	} else {
72216688Smckusick 		dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino);
72316666Smckusick 		IUNLOCK(pdp);
72412011Smckusick 		if (dp == NULL)
72512011Smckusick 			goto bad2;
72612011Smckusick 	}
72715798Smckusick 
72815798Smckusick 	/*
72915798Smckusick 	 * insert name into cache (if we want it, and it isn't "." or "..")
73015798Smckusick 	 *
73115798Smckusick 	 * all other cases where making a cache entry would be wrong
73215798Smckusick 	 * have already departed from the code sequence somewhere above.
73315798Smckusick 	 */
73416643Ssam 	if (docache) {
73515798Smckusick 		if (ncp != NULL)
73615798Smckusick 			panic("nami: duplicating cache");
73715798Smckusick 
73815798Smckusick 			/*
73915798Smckusick 			 * free the cache slot at head of lru chain
74015798Smckusick 			 */
74115798Smckusick 		if (ncp = nchhead) {
74215798Smckusick 				/* remove from lru chain */
74315798Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
74415798Smckusick 			if (ncp->nc_nxt)
74515798Smckusick 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
74615798Smckusick 			else
74715798Smckusick 				nchtail = ncp->nc_prev;
74815798Smckusick 
74915798Smckusick 				/* remove from old hash chain */
75015798Smckusick 			remque(ncp);
75115798Smckusick 
75215798Smckusick 				/* grab the inode we just found */
75315798Smckusick 			ncp->nc_ip = dp;
75415798Smckusick 
75515798Smckusick 				/* fill in cache info */
75615798Smckusick 			ncp->nc_ino = pdp->i_number;	/* parents inum */
75715798Smckusick 			ncp->nc_dev = pdp->i_dev;	/* & device */
75815798Smckusick 			ncp->nc_idev = dp->i_dev;	/* our device */
75916643Ssam 			ncp->nc_id = dp->i_id;		/* identifier */
76016688Smckusick 			ncp->nc_nlen = ndp->ni_dent.d_namlen;
76116688Smckusick 			bcopy(ndp->ni_dent.d_name, ncp->nc_name, ncp->nc_nlen);
76215798Smckusick 
76315798Smckusick 				/* link at end of lru chain */
76415798Smckusick 			ncp->nc_nxt = NULL;
76515798Smckusick 			ncp->nc_prev = nchtail;
76615798Smckusick 			*nchtail = ncp;
76715798Smckusick 			nchtail = &ncp->nc_nxt;
76815798Smckusick 
76915798Smckusick 				/* and insert on hash chain */
77015798Smckusick 			insque(ncp, nhp);
77115798Smckusick 		}
77215798Smckusick 	}
77315798Smckusick 
77415798Smckusick haveino:
7757534Sroot 	fs = dp->i_fs;
7767534Sroot 
7777534Sroot 	/*
7787534Sroot 	 * Check for symbolic link
7797534Sroot 	 */
78016688Smckusick 	if ((dp->i_mode & IFMT) == IFLNK &&
78116688Smckusick 	    ((ndp->ni_nameiop & FOLLOW) || *cp == '/')) {
7827825Sroot 		u_int pathlen = strlen(cp) + 1;
7837534Sroot 
7847534Sroot 		if (dp->i_size + pathlen >= MAXPATHLEN - 1 ||
7857534Sroot 		    ++nlink > MAXSYMLINKS) {
7867534Sroot 			u.u_error = ELOOP;
7877534Sroot 			goto bad2;
7887534Sroot 		}
7898957Sroot 		ovbcopy(cp, nbp->b_un.b_addr + dp->i_size, pathlen);
7907751Sroot 		u.u_error =
7919166Ssam 		    rdwri(UIO_READ, dp, nbp->b_un.b_addr, (int)dp->i_size,
7927825Sroot 			0, 1, (int *)0);
7937534Sroot 		if (u.u_error)
7947534Sroot 			goto bad2;
7957534Sroot 		cp = nbp->b_un.b_addr;
7967534Sroot 		iput(dp);
7975972Swnj 		if (*cp == '/') {
7987534Sroot 			irele(pdp);
7995972Swnj 			while (*cp == '/')
8005972Swnj 				cp++;
8017534Sroot 			if ((dp = u.u_rdir) == NULL)
8027534Sroot 				dp = rootdir;
80316666Smckusick 			ILOCK(dp);
8047534Sroot 			dp->i_count++;
8057534Sroot 		} else {
8067534Sroot 			dp = pdp;
80716666Smckusick 			ILOCK(dp);
8085972Swnj 		}
8097534Sroot 		fs = dp->i_fs;
8107534Sroot 		goto dirloop;
81130Sbill 	}
8127534Sroot 
81330Sbill 	/*
8147534Sroot 	 * Not a symbolic link.  If more pathname,
8157534Sroot 	 * continue at next component, else return.
81630Sbill 	 */
8177534Sroot 	if (*cp == '/') {
8187534Sroot 		while (*cp == '/')
8197534Sroot 			cp++;
8209166Ssam 		irele(pdp);
8217534Sroot 		goto dirloop;
82230Sbill 	}
82316681Smckusick 	nbp->av_forw = freenamebuf;
82416681Smckusick 	freenamebuf = nbp;
8259166Ssam 	if (lockparent)
82616688Smckusick 		ndp->ni_pdir = pdp;
8279166Ssam 	else
8289166Ssam 		irele(pdp);
8297534Sroot 	return (dp);
8307534Sroot bad2:
8317534Sroot 	irele(pdp);
8327534Sroot bad:
8337534Sroot 	if (bp)
8347534Sroot 		brelse(bp);
8357534Sroot 	if (dp)
8367534Sroot 		iput(dp);
83716681Smckusick 	nbp->av_forw = freenamebuf;
83816681Smckusick 	freenamebuf = nbp;
8396571Smckusic 	return (NULL);
84030Sbill }
84130Sbill 
84215798Smckusick 
84316688Smckusick dirbad(ip, offset, how)
8447534Sroot 	struct inode *ip;
84516688Smckusick 	off_t offset;
8467534Sroot 	char *how;
8477534Sroot {
8487534Sroot 
8497534Sroot 	printf("%s: bad dir ino %d at offset %d: %s\n",
85016688Smckusick 	    ip->i_fs->fs_fsmnt, ip->i_number, offset, how);
8517534Sroot }
8527534Sroot 
85316657Smckusick /*
85416657Smckusick  * Do consistency checking on a directory entry:
85516657Smckusick  *	record length must be multiple of 4
85616657Smckusick  *	record length must not be non-negative
85716657Smckusick  *	entry must fit in rest of its DIRBLKSIZ block
85816657Smckusick  *	record must be large enough to contain entry
85916657Smckusick  *	name is not longer than MAXNAMLEN
86016657Smckusick  *	name must be as long as advertised, and null terminated
86116657Smckusick  */
86216657Smckusick dirbadentry(ep, entryoffsetinblock)
8637534Sroot 	register struct direct *ep;
86416657Smckusick 	int entryoffsetinblock;
8657534Sroot {
8667534Sroot 	register int i;
8677534Sroot 
86816657Smckusick 	if ((ep->d_reclen & 0x3) != 0 || ep->d_reclen <= 0 ||
86916657Smckusick 	    ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
87016657Smckusick 	    ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN)
87116657Smckusick 		return (1);
8727534Sroot 	for (i = 0; i < ep->d_namlen; i++)
87316688Smckusick 		if (ep->d_name[i] == '\0')
8747534Sroot 			return (1);
8757534Sroot 	return (ep->d_name[i]);
8767534Sroot }
8777534Sroot 
87830Sbill /*
8797534Sroot  * Write a directory entry after a call to namei, using the parameters
8807534Sroot  * which it left in the u. area.  The argument ip is the inode which
88116688Smckusick  * the new directory entry will refer to.  The u. area field ndp->ni_pdir is
8827534Sroot  * a pointer to the directory to be written, which was left locked by
88316688Smckusick  * namei.  Remaining parameters (ndp->ni_offset, ndp->ni_count) indicate
8847534Sroot  * how the space for the new entry is to be gotten.
8857534Sroot  */
88616688Smckusick direnter(ip, ndp)
8877534Sroot 	struct inode *ip;
88816688Smckusick 	register struct nameidata *ndp;
8895972Swnj {
8907534Sroot 	register struct direct *ep, *nep;
891*18027Smckusick 	register struct inode *dp = ndp->ni_pdir;
8927534Sroot 	struct buf *bp;
89311639Ssam 	int loc, spacefree, error = 0;
8948631Sroot 	u_int dsize;
8958631Sroot 	int newentrysize;
8967534Sroot 	char *dirbuf;
8975972Swnj 
89816688Smckusick 	ndp->ni_dent.d_ino = ip->i_number;
89916688Smckusick 	newentrysize = DIRSIZ(&ndp->ni_dent);
90016688Smckusick 	if (ndp->ni_count == 0) {
9017534Sroot 		/*
90216688Smckusick 		 * If ndp->ni_count is 0, then namei could find no space in the
90316688Smckusick 		 * directory. In this case ndp->ni_offset will be on a directory
9047534Sroot 		 * block boundary and we will write the new entry into a fresh
9057534Sroot 		 * block.
9067534Sroot 		 */
90716688Smckusick 		if (ndp->ni_offset&(DIRBLKSIZ-1))
9087534Sroot 			panic("wdir: newblk");
90916688Smckusick 		ndp->ni_dent.d_reclen = DIRBLKSIZ;
910*18027Smckusick 		error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent,
91116688Smckusick 		    newentrysize, ndp->ni_offset, 1, (int *)0);
912*18027Smckusick 		iput(dp);
91310849Ssam 		return (error);
9147534Sroot 	}
9157534Sroot 
9167534Sroot 	/*
91716688Smckusick 	 * If ndp->ni_count is non-zero, then namei found space for the new
91816688Smckusick 	 * entry in the range ndp->ni_offset to ndp->ni_offset + ndp->ni_count.
9197534Sroot 	 * in the directory.  To use this space, we may have to compact
9207534Sroot 	 * the entries located there, by copying them together towards
9217534Sroot 	 * the beginning of the block, leaving the free space in
9227534Sroot 	 * one usable chunk at the end.
9237534Sroot 	 */
9247534Sroot 
9257534Sroot 	/*
9267534Sroot 	 * Increase size of directory if entry eats into new space.
9277534Sroot 	 * This should never push the size past a new multiple of
9287534Sroot 	 * DIRBLKSIZE.
9297534Sroot 	 */
930*18027Smckusick 	if (ndp->ni_offset + ndp->ni_count > dp->i_size)
931*18027Smckusick 		dp->i_size = ndp->ni_offset + ndp->ni_count;
9327534Sroot 
9337534Sroot 	/*
9347534Sroot 	 * Get the block containing the space for the new directory
93510849Ssam 	 * entry.  Should return error by result instead of u.u_error.
9367534Sroot 	 */
937*18027Smckusick 	bp = blkatoff(dp, ndp->ni_offset, (char **)&dirbuf);
9389166Ssam 	if (bp == 0) {
939*18027Smckusick 		iput(dp);
94010849Ssam 		return (u.u_error);
9419166Ssam 	}
9427534Sroot 
9437534Sroot 	/*
9447534Sroot 	 * Find space for the new entry.  In the simple case, the
9457534Sroot 	 * entry at offset base will have the space.  If it does
9467534Sroot 	 * not, then namei arranged that compacting the region
94716688Smckusick 	 * ndp->ni_offset to ndp->ni_offset+ndp->ni_count would yield the space.
9487534Sroot 	 */
9497534Sroot 	ep = (struct direct *)dirbuf;
9507534Sroot 	dsize = DIRSIZ(ep);
95111639Ssam 	spacefree = ep->d_reclen - dsize;
95216688Smckusick 	for (loc = ep->d_reclen; loc < ndp->ni_count; ) {
9537534Sroot 		nep = (struct direct *)(dirbuf + loc);
9547534Sroot 		if (ep->d_ino) {
9557534Sroot 			/* trim the existing slot */
9567534Sroot 			ep->d_reclen = dsize;
9577534Sroot 			ep = (struct direct *)((char *)ep + dsize);
9587534Sroot 		} else {
9597534Sroot 			/* overwrite; nothing there; header is ours */
96011639Ssam 			spacefree += dsize;
9617534Sroot 		}
9627534Sroot 		dsize = DIRSIZ(nep);
96311639Ssam 		spacefree += nep->d_reclen - dsize;
9647534Sroot 		loc += nep->d_reclen;
9657825Sroot 		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
9667534Sroot 	}
9677534Sroot 	/*
9687534Sroot 	 * Update the pointer fields in the previous entry (if any),
9697534Sroot 	 * copy in the new entry, and write out the block.
9707534Sroot 	 */
9717534Sroot 	if (ep->d_ino == 0) {
97211639Ssam 		if (spacefree + dsize < newentrysize)
9737534Sroot 			panic("wdir: compact1");
97416688Smckusick 		ndp->ni_dent.d_reclen = spacefree + dsize;
9757534Sroot 	} else {
97611639Ssam 		if (spacefree < newentrysize)
9777534Sroot 			panic("wdir: compact2");
97816688Smckusick 		ndp->ni_dent.d_reclen = spacefree;
9797534Sroot 		ep->d_reclen = dsize;
9807534Sroot 		ep = (struct direct *)((char *)ep + dsize);
9817534Sroot 	}
98216688Smckusick 	bcopy((caddr_t)&ndp->ni_dent, (caddr_t)ep, (u_int)newentrysize);
9837534Sroot 	bwrite(bp);
984*18027Smckusick 	dp->i_flag |= IUPD|ICHG;
985*18027Smckusick 	if (ndp->ni_endoff && ndp->ni_endoff < dp->i_size)
986*18027Smckusick 		itrunc(dp, ndp->ni_endoff);
987*18027Smckusick 	iput(dp);
98810849Ssam 	return (error);
9895972Swnj }
9906571Smckusic 
9919166Ssam /*
9929166Ssam  * Remove a directory entry after a call to namei, using the
9939166Ssam  * parameters which it left in the u. area.  The u. entry
99416688Smckusick  * ni_offset contains the offset into the directory of the
99516688Smckusick  * entry to be eliminated.  The ni_count field contains the
9969166Ssam  * size of the previous record in the directory.  If this
9979166Ssam  * is 0, the first entry is being deleted, so we need only
9989166Ssam  * zero the inode number to mark the entry as free.  If the
9999166Ssam  * entry isn't the first in the directory, we must reclaim
10009166Ssam  * the space of the now empty record by adding the record size
10019166Ssam  * to the size of the previous entry.
10029166Ssam  */
100316688Smckusick dirremove(ndp)
100416688Smckusick 	register struct nameidata *ndp;
10056571Smckusic {
100616688Smckusick 	register struct inode *dp = ndp->ni_pdir;
10077534Sroot 	register struct buf *bp;
10087534Sroot 	struct direct *ep;
10096571Smckusic 
101016688Smckusick 	if (ndp->ni_count == 0) {
10117534Sroot 		/*
10127534Sroot 		 * First entry in block: set d_ino to zero.
10137534Sroot 		 */
101416688Smckusick 		ndp->ni_dent.d_ino = 0;
101516688Smckusick 		(void) rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent,
101616688Smckusick 		    (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0);
10179269Ssam 	} else {
10187534Sroot 		/*
10197534Sroot 		 * Collapse new free space into previous entry.
10207534Sroot 		 */
102116688Smckusick 		bp = blkatoff(dp, (int)(ndp->ni_offset - ndp->ni_count),
102216688Smckusick 			(char **)&ep);
10237534Sroot 		if (bp == 0)
10247534Sroot 			return (0);
102516688Smckusick 		ep->d_reclen += ndp->ni_dent.d_reclen;
10267534Sroot 		bwrite(bp);
10277534Sroot 		dp->i_flag |= IUPD|ICHG;
10287534Sroot 	}
10297534Sroot 	return (1);
10306571Smckusic }
10317534Sroot 
10327605Ssam /*
10339166Ssam  * Rewrite an existing directory entry to point at the inode
10349166Ssam  * supplied.  The parameters describing the directory entry are
10359166Ssam  * set up by a call to namei.
10369166Ssam  */
103716688Smckusick dirrewrite(dp, ip, ndp)
10389166Ssam 	struct inode *dp, *ip;
103916688Smckusick 	struct nameidata *ndp;
10409166Ssam {
10419166Ssam 
104216688Smckusick 	ndp->ni_dent.d_ino = ip->i_number;
104316688Smckusick 	u.u_error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent,
104416688Smckusick 		(int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0);
10459166Ssam 	iput(dp);
10469166Ssam }
10479166Ssam 
10489166Ssam /*
10497605Ssam  * Return buffer with contents of block "offset"
10507605Ssam  * from the beginning of directory "ip".  If "res"
10517605Ssam  * is non-zero, fill it in with a pointer to the
10527605Ssam  * remaining space in the directory.
10537605Ssam  */
10547534Sroot struct buf *
10557605Ssam blkatoff(ip, offset, res)
10567534Sroot 	struct inode *ip;
10577534Sroot 	off_t offset;
10587534Sroot 	char **res;
10597534Sroot {
10607534Sroot 	register struct fs *fs = ip->i_fs;
10618672Sroot 	daddr_t lbn = lblkno(fs, offset);
10627534Sroot 	int base = blkoff(fs, offset);
10637534Sroot 	int bsize = blksize(fs, ip, lbn);
10648672Sroot 	daddr_t bn = fsbtodb(fs, bmap(ip, lbn, B_WRITE, base, bsize));
10657534Sroot 	register struct buf *bp;
10667534Sroot 
10677534Sroot 	if (u.u_error)
10687534Sroot 		return (0);
10697534Sroot 	bp = bread(ip->i_dev, bn, bsize);
10707534Sroot 	if (bp->b_flags & B_ERROR) {
10717534Sroot 		brelse(bp);
10727534Sroot 		return (0);
10737534Sroot 	}
10747534Sroot 	if (res)
10757534Sroot 		*res = bp->b_un.b_addr + base;
10767534Sroot 	return (bp);
10777534Sroot }
10789166Ssam 
10799166Ssam /*
10809166Ssam  * Check if a directory is empty or not.
10819166Ssam  * Inode supplied must be locked.
108212817Ssam  *
108312817Ssam  * Using a struct dirtemplate here is not precisely
108412817Ssam  * what we want, but better than using a struct direct.
108512817Ssam  *
108612817Ssam  * NB: does not handle corrupted directories.
10879166Ssam  */
108816777Smckusick dirempty(ip, parentino)
10899863Ssam 	register struct inode *ip;
109016777Smckusick 	ino_t parentino;
10919166Ssam {
10929166Ssam 	register off_t off;
109312817Ssam 	struct dirtemplate dbuf;
109412817Ssam 	register struct direct *dp = (struct direct *)&dbuf;
10959863Ssam 	int error, count;
109612817Ssam #define	MINDIRSIZ (sizeof (struct dirtemplate) / 2)
10979166Ssam 
10989166Ssam 	for (off = 0; off < ip->i_size; off += dp->d_reclen) {
109912817Ssam 		error = rdwri(UIO_READ, ip, (caddr_t)dp, MINDIRSIZ,
110012817Ssam 		    off, 1, &count);
110112817Ssam 		/*
110212817Ssam 		 * Since we read MINDIRSIZ, residual must
110312817Ssam 		 * be 0 unless we're at end of file.
110412817Ssam 		 */
110512817Ssam 		if (error || count != 0)
11069166Ssam 			return (0);
110712817Ssam 		/* skip empty entries */
11089166Ssam 		if (dp->d_ino == 0)
11099166Ssam 			continue;
111012817Ssam 		/* accept only "." and ".." */
111112817Ssam 		if (dp->d_namlen > 2)
111212817Ssam 			return (0);
11139166Ssam 		if (dp->d_name[0] != '.')
11149166Ssam 			return (0);
111512817Ssam 		/*
111612817Ssam 		 * At this point d_namlen must be 1 or 2.
111712817Ssam 		 * 1 implies ".", 2 implies ".." if second
111812817Ssam 		 * char is also "."
111912817Ssam 		 */
112016777Smckusick 		if (dp->d_namlen == 1)
11219166Ssam 			continue;
112216777Smckusick 		if (dp->d_name[1] == '.' && dp->d_ino == parentino)
112316777Smckusick 			continue;
11249166Ssam 		return (0);
11259166Ssam 	}
11269166Ssam 	return (1);
11279166Ssam }
112812815Smckusick 
112912815Smckusick /*
113012815Smckusick  * Check if source directory is in the path of the target directory.
113112815Smckusick  * Target is supplied locked, source is unlocked.
113212815Smckusick  * The target is always iput() before returning.
113312815Smckusick  */
113412815Smckusick checkpath(source, target)
113512815Smckusick 	struct inode *source, *target;
113612815Smckusick {
113712815Smckusick 	struct dirtemplate dirbuf;
113812815Smckusick 	register struct inode *ip;
113912815Smckusick 	int error = 0;
114012815Smckusick 
114112815Smckusick 	ip = target;
114212815Smckusick 	if (ip->i_number == source->i_number) {
114312815Smckusick 		error = EEXIST;
114412815Smckusick 		goto out;
114512815Smckusick 	}
114612815Smckusick 	if (ip->i_number == ROOTINO)
114712815Smckusick 		goto out;
114812815Smckusick 
114912815Smckusick 	for (;;) {
115012815Smckusick 		if ((ip->i_mode&IFMT) != IFDIR) {
115112815Smckusick 			error = ENOTDIR;
115212815Smckusick 			break;
115312815Smckusick 		}
115412815Smckusick 		error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf,
115512815Smckusick 			sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
115612815Smckusick 		if (error != 0)
115712815Smckusick 			break;
115812815Smckusick 		if (dirbuf.dotdot_namlen != 2 ||
115916658Smckusick 		    dirbuf.dotdot_name[0] != '.' ||
116016658Smckusick 		    dirbuf.dotdot_name[1] != '.') {
116112815Smckusick 			error = ENOTDIR;
116212815Smckusick 			break;
116312815Smckusick 		}
116412815Smckusick 		if (dirbuf.dotdot_ino == source->i_number) {
116512815Smckusick 			error = EINVAL;
116612815Smckusick 			break;
116712815Smckusick 		}
116812815Smckusick 		if (dirbuf.dotdot_ino == ROOTINO)
116912815Smckusick 			break;
117012815Smckusick 		iput(ip);
117112815Smckusick 		ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino);
117212815Smckusick 		if (ip == NULL) {
117312815Smckusick 			error = u.u_error;
117412815Smckusick 			break;
117512815Smckusick 		}
117612815Smckusick 	}
117712815Smckusick 
117812815Smckusick out:
117912815Smckusick 	if (error == ENOTDIR)
118012815Smckusick 		printf("checkpath: .. not a directory\n");
118112815Smckusick 	if (ip != NULL)
118212815Smckusick 		iput(ip);
118312815Smckusick 	return (error);
118412815Smckusick }
118515798Smckusick 
118615798Smckusick /*
118715798Smckusick  * Name cache initialization, from main() when we are booting
118815798Smckusick  */
118915798Smckusick nchinit()
119015798Smckusick {
119115798Smckusick 	register union nchash *nchp;
119215798Smckusick 	register struct nch *ncp;
119315798Smckusick 
119415798Smckusick 	nchhead = 0;
119515798Smckusick 	nchtail = &nchhead;
119615798Smckusick 
119715798Smckusick 	for (ncp = nch; ncp < &nch[nchsize]; ncp++) {
119815798Smckusick 		ncp->nc_forw = ncp;			/* hash chain */
119915798Smckusick 		ncp->nc_back = ncp;
120015798Smckusick 
120115798Smckusick 		ncp->nc_nxt = NULL;			/* lru chain */
120215798Smckusick 		*nchtail = ncp;
120315798Smckusick 		ncp->nc_prev = nchtail;
120415798Smckusick 		nchtail = &ncp->nc_nxt;
120515798Smckusick 
120615798Smckusick 		/* all else is zero already */
120715798Smckusick 	}
120815798Smckusick 
120915798Smckusick 	for (nchp = nchash; nchp < &nchash[NCHHASH]; nchp++) {
121015798Smckusick 		nchp->nch_head[0] = nchp;
121115798Smckusick 		nchp->nch_head[1] = nchp;
121215798Smckusick 	}
121315798Smckusick }
121415798Smckusick 
121515798Smckusick /*
121615798Smckusick  * Cache flush, called when filesys is umounted to
121715798Smckusick  * remove entries that would now be invalid
121815798Smckusick  *
121915798Smckusick  * The line "nxtcp = nchhead" near the end is to avoid potential problems
122015798Smckusick  * if the cache lru chain is modified while we are dumping the
122115798Smckusick  * inode.  This makes the algorithm O(n^2), but do you think I care?
122215798Smckusick  */
122315798Smckusick nchinval(dev)
122415798Smckusick 	register dev_t dev;
122515798Smckusick {
122615798Smckusick 	register struct nch *ncp, *nxtcp;
122715798Smckusick 
122815798Smckusick 	for (ncp = nchhead; ncp; ncp = nxtcp) {
122915798Smckusick 		nxtcp = ncp->nc_nxt;
123015798Smckusick 
123115798Smckusick 		if (ncp->nc_ip == NULL ||
123215798Smckusick 		    (ncp->nc_idev != dev && ncp->nc_dev != dev))
123315798Smckusick 			continue;
123415798Smckusick 
123516658Smckusick 			/* free the resources we had */
123615798Smckusick 		ncp->nc_idev = NODEV;
123715798Smckusick 		ncp->nc_dev = NODEV;
123816658Smckusick 		ncp->nc_id = NULL;
123915798Smckusick 		ncp->nc_ino = 0;
124016658Smckusick 		ncp->nc_ip = NULL;
124115798Smckusick 
124216658Smckusick 
124315798Smckusick 			/* remove the entry from its hash chain */
124415798Smckusick 		remque(ncp);
124515798Smckusick 			/* and make a dummy one */
124615798Smckusick 		ncp->nc_forw = ncp;
124715798Smckusick 		ncp->nc_back = ncp;
124815798Smckusick 
124915798Smckusick 			/* delete this entry from LRU chain */
125015798Smckusick 		*ncp->nc_prev = nxtcp;
125115798Smckusick 		if (nxtcp)
125215798Smckusick 			nxtcp->nc_prev = ncp->nc_prev;
125315798Smckusick 		else
125415798Smckusick 			nchtail = ncp->nc_prev;
125515798Smckusick 
125615798Smckusick 			/* cause rescan of list, it may have altered */
125715798Smckusick 		nxtcp = nchhead;
125815798Smckusick 			/* put the now-free entry at head of LRU */
125915798Smckusick 		ncp->nc_nxt = nxtcp;
126015798Smckusick 		ncp->nc_prev = &nchhead;
126115798Smckusick 		nxtcp->nc_prev = &ncp->nc_nxt;
126215798Smckusick 		nchhead = ncp;
126315798Smckusick 	}
126415798Smckusick }
126517704Smckusick 
126617704Smckusick /*
126717704Smckusick  * Name cache invalidation of all entries.
126817704Smckusick  */
126917704Smckusick cacheinvalall()
127017704Smckusick {
127117704Smckusick 	register struct nch *ncp;
127217704Smckusick 
127317704Smckusick 	for (ncp = nch; ncp < &nch[nchsize]; ncp++) {
127417704Smckusick 		ncp->nc_id = 0;
127517704Smckusick 	}
127617704Smckusick }
1277