xref: /csrg-svn/sys/kern/vfs_lookup.c (revision 27268)
123401Smckusick /*
223401Smckusick  * Copyright (c) 1982 Regents of the University of California.
323401Smckusick  * All rights reserved.  The Berkeley software License Agreement
423401Smckusick  * specifies the terms and conditions for redistribution.
523401Smckusick  *
6*27268Smckusick  *	@(#)vfs_lookup.c	6.29 (Berkeley) 04/23/86
723401Smckusick  */
830Sbill 
917100Sbloom #include "param.h"
1017100Sbloom #include "systm.h"
1117100Sbloom #include "inode.h"
1217100Sbloom #include "fs.h"
1317100Sbloom #include "mount.h"
1417100Sbloom #include "dir.h"
1517100Sbloom #include "user.h"
1617100Sbloom #include "buf.h"
1717100Sbloom #include "conf.h"
1817100Sbloom #include "uio.h"
1917100Sbloom #include "kernel.h"
2030Sbill 
217605Ssam struct	buf *blkatoff();
2216681Smckusick struct	buf *freenamebuf;
239166Ssam int	dirchk = 0;
2415798Smckusick 
2530Sbill /*
2615798Smckusick  * Structures associated with name cacheing.
2715798Smckusick  */
2815798Smckusick #define	NCHHASH		32	/* size of hash table */
2915798Smckusick 
3015798Smckusick #if	((NCHHASH)&((NCHHASH)-1)) != 0
3115798Smckusick #define	NHASH(h, i, d)	((unsigned)((h) + (i) + 13 * (int)(d)) % (NCHHASH))
3215798Smckusick #else
3315798Smckusick #define	NHASH(h, i, d)	((unsigned)((h) + (i) + 13 * (int)(d)) & ((NCHHASH)-1))
3415798Smckusick #endif
3515798Smckusick 
3626273Skarels union nchash {
3726273Skarels 	union	nchash *nch_head[2];
3826306Skarels 	struct	namecache *nch_chain[2];
3915798Smckusick } nchash[NCHHASH];
4015798Smckusick #define	nch_forw	nch_chain[0]
4115798Smckusick #define	nch_back	nch_chain[1]
4215798Smckusick 
4326306Skarels struct	namecache *nchhead, **nchtail;	/* LRU chain pointers */
4415809Smckusick struct	nchstats nchstats;		/* cache effectiveness statistics */
4515798Smckusick 
4615798Smckusick /*
47*27268Smckusick  * Convert a pathname into a pointer to a locked inode.
487534Sroot  * This is a very central and rather complicated routine.
49*27268Smckusick  * If the file system is not maintained in a strict tree hierarchy,
50*27268Smckusick  * this can result in a deadlock situation (see comments in code below).
5130Sbill  *
52*27268Smckusick  * The flag argument is LOOKUP, CREATE, or DELETE depending on whether
53*27268Smckusick  * the name is to be looked up, created, or deleted. When CREATE or
54*27268Smckusick  * DELETE is specified, information usable in creating or deleteing a
55*27268Smckusick  * directory entry is also calculated. If flag has LOCKPARENT or'ed
56*27268Smckusick  * into it and the target of the pathname exists, namei returns both
57*27268Smckusick  * the target and its parent directory locked. When creating and
589166Ssam  * LOCKPARENT is specified, the target may not be ".".  When deleting
599166Ssam  * and LOCKPARENT is specified, the target may be ".", but the caller
609166Ssam  * must check to insure it does an irele and iput instead of two iputs.
619166Ssam  *
6216688Smckusick  * The FOLLOW flag is set when symbolic links are to be followed
639166Ssam  * when they occur at the end of the name translation process.
64*27268Smckusick  * Symbolic links are always followed for all other pathname
65*27268Smckusick  * components other than the last.
669166Ssam  *
67*27268Smckusick  * The segflg defines whether the name is to be copied from user
68*27268Smckusick  * space or kernel space.
69*27268Smckusick  *
7015798Smckusick  * Name caching works as follows:
717534Sroot  *
7226273Skarels  * Names found by directory scans are retained in a cache
7326273Skarels  * for future reference.  It is managed LRU, so frequently
7426273Skarels  * used names will hang around.  Cache is indexed by hash value
7526273Skarels  * obtained from (ino,dev,name) where ino & dev refer to the
7626273Skarels  * directory containing name.
7715798Smckusick  *
7826273Skarels  * For simplicity (and economy of storage), names longer than
79*27268Smckusick  * a maximum length of NCHNAMLEN are not cached; they occur
8026273Skarels  * infrequently in any case, and are almost never of interest.
8115798Smckusick  *
8226273Skarels  * Upon reaching the last segment of a path, if the reference
8326273Skarels  * is for DELETE, or NOCACHE is set (rewrite), and the
8426273Skarels  * name is located in the cache, it will be dropped.
8515798Smckusick  *
8615798Smckusick  * Overall outline of namei:
8715798Smckusick  *
887534Sroot  *	copy in name
897534Sroot  *	get starting directory
907534Sroot  * dirloop:
917534Sroot  *	check accessibility of directory
927534Sroot  * dirloop2:
9316688Smckusick  *	copy next component of name to ndp->ni_dent
947534Sroot  *	handle degenerate case where name is null string
9515798Smckusick  *	look for name in cache, if found, then if at end of path
9615798Smckusick  *	  and deleting or creating, drop it, else to haveino
977534Sroot  *	search for name in directory, to found or notfound
987534Sroot  * notfound:
999166Ssam  *	if creating, return locked directory, leaving info on avail. slots
1007534Sroot  *	else return error
1017534Sroot  * found:
1027534Sroot  *	if at end of path and deleting, return information to allow delete
103*27268Smckusick  *	if at end of path and rewriting (CREATE and LOCKPARENT), lock target
1049166Ssam  *	  inode and return info to allow rewrite
1057534Sroot  *	if .. and on mounted filesys, look in mount table for parent
106*27268Smckusick  *	if not at end, add name to cache; if at end and neither creating
107*27268Smckusick  *	  nor deleting, add name to cache
10815798Smckusick  * haveino:
1097534Sroot  *	if symbolic link, massage name in buffer and continue at dirloop
1107534Sroot  *	if more components of name, do next level at dirloop
1117534Sroot  *	return the answer as locked inode
1129166Ssam  *
1139166Ssam  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode,
1149166Ssam  *	 but unlocked.
11530Sbill  */
11630Sbill struct inode *
11716688Smckusick namei(ndp)
11816688Smckusick 	register struct nameidata *ndp;
11930Sbill {
1207534Sroot 	register char *cp;		/* pointer into pathname argument */
1217534Sroot /* these variables refer to things which must be freed or unlocked */
1227534Sroot 	register struct inode *dp = 0;	/* the directory we are searching */
12326306Skarels 	register struct namecache *ncp;	/* cache slot for entry */
1247534Sroot 	register struct fs *fs;		/* file system that directory is in */
1257534Sroot 	register struct buf *bp = 0;	/* a buffer of directory entries */
1267534Sroot 	register struct direct *ep;	/* the current directory entry */
1277534Sroot 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
1287534Sroot 	register struct buf *nbp;	/* buffer storing path name argument */
1297534Sroot /* these variables hold information about the search for a slot */
1307534Sroot 	enum {NONE, COMPACT, FOUND} slotstatus;
1317534Sroot 	int slotoffset = -1;		/* offset of area with free space */
1327534Sroot 	int slotsize;			/* size of area at slotoffset */
1337534Sroot 	int slotfreespace;		/* amount of space free in slot */
1347534Sroot 	int slotneeded;			/* size of the entry we're seeking */
1357534Sroot /* */
13615660Smckusick 	int numdirpasses;		/* strategy for directory search */
13715660Smckusick 	int endsearch;			/* offset to end directory search */
13816688Smckusick 	int prevoff;			/* ndp->ni_offset of previous entry */
1397534Sroot 	int nlink = 0;			/* number of symbolic links taken */
1407534Sroot 	struct inode *pdp;		/* saved dp during symlink work */
14116688Smckusick 	int error, i;
1429166Ssam 	int lockparent;
14318109Smckusick 	int docache;			/* == 0 do not cache last component */
14418109Smckusick 	int makeentry;			/* != 0 if name to be added to cache */
14515798Smckusick 	unsigned hash;			/* value of name hash for entry */
14615798Smckusick 	union nchash *nhp;		/* cache chain head for entry */
14715798Smckusick 	int isdotdot;			/* != 0 if current name is ".." */
14816688Smckusick 	int flag;			/* op ie, LOOKUP, CREATE, or DELETE */
14918027Smckusick 	off_t enduseful;		/* pointer past last used dir slot */
15030Sbill 
15116688Smckusick 	lockparent = ndp->ni_nameiop & LOCKPARENT;
15216688Smckusick 	docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
15316688Smckusick 	flag = ndp->ni_nameiop &~ (LOCKPARENT|NOCACHE|FOLLOW);
15418109Smckusick 	if (flag == DELETE || lockparent)
15515798Smckusick 		docache = 0;
15630Sbill 	/*
1577534Sroot 	 * Get a buffer for the name to be translated, and copy the
1587534Sroot 	 * name into the buffer.
1595972Swnj 	 */
16016681Smckusick 	nbp = freenamebuf;
16116681Smckusick 	if (nbp == NULL)
16216681Smckusick 		nbp = geteblk(MAXPATHLEN);
16316681Smckusick 	else
16416681Smckusick 		freenamebuf = nbp->av_forw;
16516688Smckusick 	if (ndp->ni_segflg == UIO_SYSSPACE)
16616705Smckusick 		error = copystr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN,
16716705Smckusick 		    (u_int *)0);
16816688Smckusick 	else
16916705Smckusick 		error = copyinstr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN,
17016705Smckusick 		    (u_int *)0);
17116688Smckusick 	if (error) {
17216688Smckusick 		u.u_error = error;
17316688Smckusick 		goto bad;
1745972Swnj 	}
1757534Sroot 
1765972Swnj 	/*
1777534Sroot 	 * Get starting directory.
17830Sbill 	 */
1797534Sroot 	cp = nbp->b_un.b_addr;
1805972Swnj 	if (*cp == '/') {
1815972Swnj 		while (*cp == '/')
1825972Swnj 			cp++;
18330Sbill 		if ((dp = u.u_rdir) == NULL)
18430Sbill 			dp = rootdir;
1857534Sroot 	} else
1867534Sroot 		dp = u.u_cdir;
1877534Sroot 	fs = dp->i_fs;
18816666Smckusick 	ILOCK(dp);
1895972Swnj 	dp->i_count++;
19016688Smckusick 	ndp->ni_pdir = (struct inode *)0xc0000000;		/* illegal */
19118027Smckusick 	ndp->ni_endoff = 0;
1927534Sroot 
1937534Sroot 	/*
1947534Sroot 	 * We come to dirloop to search a new directory.
1957534Sroot 	 * The directory must be locked so that it can be
1967534Sroot 	 * iput, and fs must be already set to dp->i_fs.
1977534Sroot 	 */
1986571Smckusic dirloop:
19930Sbill 	/*
2007534Sroot 	 * Check accessiblity of directory.
20130Sbill 	 */
2027534Sroot 	if ((dp->i_mode&IFMT) != IFDIR) {
20330Sbill 		u.u_error = ENOTDIR;
2047534Sroot 		goto bad;
2057534Sroot 	}
2067534Sroot 	if (access(dp, IEXEC))
2077534Sroot 		goto bad;
2087534Sroot 
2096384Swnj dirloop2:
2107534Sroot 	/*
21116688Smckusick 	 * Copy next component of name to ndp->ni_dent.
2127534Sroot 	 */
21315798Smckusick 	hash = 0;
2147534Sroot 	for (i = 0; *cp != 0 && *cp != '/'; cp++) {
2156571Smckusic 		if (i >= MAXNAMLEN) {
21621014Smckusick 			u.u_error = ENAMETOOLONG;
2177534Sroot 			goto bad;
2185972Swnj 		}
21921014Smckusick 		if (*cp & 0200)
22021014Smckusick 			if ((*cp&0377) == ('/'|0200) || flag != DELETE) {
22121014Smckusick 				u.u_error = EINVAL;
22221014Smckusick 				goto bad;
22321014Smckusick 			}
22416688Smckusick 		ndp->ni_dent.d_name[i++] = *cp;
22515798Smckusick 		hash += (unsigned char)*cp * i;
2265972Swnj 	}
22716688Smckusick 	ndp->ni_dent.d_namlen = i;
22816688Smckusick 	ndp->ni_dent.d_name[i] = '\0';
22916658Smckusick 	isdotdot = (i == 2 &&
23016688Smckusick 		ndp->ni_dent.d_name[0] == '.' && ndp->ni_dent.d_name[1] == '.');
23118109Smckusick 	makeentry = 1;
23218109Smckusick 	if (*cp == '\0' && docache == 0)
23318109Smckusick 		makeentry = 0;
2347534Sroot 
2357534Sroot 	/*
2367534Sroot 	 * Check for degenerate name (e.g. / or "")
2377534Sroot 	 * which is a way of talking about a directory,
2387534Sroot 	 * e.g. like "/." or ".".
2397534Sroot 	 */
24016688Smckusick 	if (ndp->ni_dent.d_name[0] == '\0') {
24115798Smckusick 		if (flag != LOOKUP || lockparent) {
24214937Smckusick 			u.u_error = EISDIR;
2437534Sroot 			goto bad;
2445972Swnj 		}
24516681Smckusick 		nbp->av_forw = freenamebuf;
24616681Smckusick 		freenamebuf = nbp;
2476571Smckusic 		return (dp);
2485972Swnj 	}
2497534Sroot 
2506571Smckusic 	/*
25115798Smckusick 	 * We now have a segment name to search for, and a directory to search.
25215798Smckusick 	 *
25315798Smckusick 	 * Before tediously performing a linear scan of the directory,
25415798Smckusick 	 * check the name cache to see if the directory/name pair
25515798Smckusick 	 * we are looking for is known already.  We don't do this
25615798Smckusick 	 * if the segment name is long, simply so the cache can avoid
25715798Smckusick 	 * holding long names (which would either waste space, or
25815798Smckusick 	 * add greatly to the complexity).
25915798Smckusick 	 */
26016688Smckusick 	if (ndp->ni_dent.d_namlen > NCHNAMLEN) {
26115798Smckusick 		nchstats.ncs_long++;
26218109Smckusick 		makeentry = 0;
26315798Smckusick 	} else {
26415798Smckusick 		nhp = &nchash[NHASH(hash, dp->i_number, dp->i_dev)];
26526306Skarels 		for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
26615798Smckusick 		    ncp = ncp->nc_forw) {
26715798Smckusick 			if (ncp->nc_ino == dp->i_number &&
26815798Smckusick 			    ncp->nc_dev == dp->i_dev &&
26916688Smckusick 			    ncp->nc_nlen == ndp->ni_dent.d_namlen &&
27016688Smckusick 			    !bcmp(ncp->nc_name, ndp->ni_dent.d_name,
27126273Skarels 				(unsigned)ncp->nc_nlen))
27215798Smckusick 				break;
27315798Smckusick 		}
27426306Skarels 		if (ncp == (struct namecache *)nhp) {
27515798Smckusick 			nchstats.ncs_miss++;
27615798Smckusick 			ncp = NULL;
27715798Smckusick 		} else {
27826306Skarels 			if (ncp->nc_id != ncp->nc_ip->i_id)
27916643Ssam 				nchstats.ncs_falsehits++;
28026306Skarels 			else if (!makeentry)
28116658Smckusick 				nchstats.ncs_badhits++;
28226306Skarels 			else {
28326273Skarels 				/*
28426273Skarels 				 * move this slot to end of LRU
28526273Skarels 				 * chain, if not already there
28626273Skarels 				 */
28715798Smckusick 				if (ncp->nc_nxt) {
28826273Skarels 					/* remove from LRU chain */
28915798Smckusick 					*ncp->nc_prev = ncp->nc_nxt;
29015798Smckusick 					ncp->nc_nxt->nc_prev = ncp->nc_prev;
29115798Smckusick 
29226273Skarels 					/* and replace at end of it */
29315798Smckusick 					ncp->nc_nxt = NULL;
29415798Smckusick 					ncp->nc_prev = nchtail;
29515798Smckusick 					*nchtail = ncp;
29615798Smckusick 					nchtail = &ncp->nc_nxt;
29715798Smckusick 				}
29815798Smckusick 
29916658Smckusick 				/*
30016658Smckusick 				 * Get the next inode in the path.
30116666Smckusick 				 * See comment above other `IUNLOCK' code for
30216658Smckusick 				 * an explaination of the locking protocol.
30316658Smckusick 				 */
30415798Smckusick 				pdp = dp;
30518109Smckusick 				if (!isdotdot || dp != u.u_rdir)
30618109Smckusick 					dp = ncp->nc_ip;
30715798Smckusick 				if (dp == NULL)
30815798Smckusick 					panic("nami: null cache ino");
30926306Skarels 				if (pdp == dp)
31016643Ssam 					dp->i_count++;
31126306Skarels 				else if (isdotdot) {
31218109Smckusick 					IUNLOCK(pdp);
31318109Smckusick 					igrab(dp);
31418109Smckusick 				} else {
31518109Smckusick 					igrab(dp);
31618109Smckusick 					IUNLOCK(pdp);
31716643Ssam 				}
31815798Smckusick 
31916658Smckusick 				/*
32016658Smckusick 				 * Verify that the inode that we got
32116658Smckusick 				 * did not change while we were waiting
32216658Smckusick 				 * for it to be locked.
32316658Smckusick 				 */
32416658Smckusick 				if (ncp->nc_id != ncp->nc_ip->i_id) {
32516658Smckusick 					iput(dp);
32616666Smckusick 					ILOCK(pdp);
32716658Smckusick 					dp = pdp;
32816658Smckusick 					nchstats.ncs_falsehits++;
32916658Smckusick 				} else {
33016688Smckusick 					ndp->ni_dent.d_ino = dp->i_number;
33116688Smckusick 					/* ni_dent.d_reclen is garbage ... */
33216658Smckusick 					nchstats.ncs_goodhits++;
33316658Smckusick 					goto haveino;
33416658Smckusick 				}
33516658Smckusick 			}
33615798Smckusick 
33715798Smckusick 			/*
33816643Ssam 			 * Last component and we are renaming or deleting,
33916643Ssam 			 * the cache entry is invalid, or otherwise don't
34016643Ssam 			 * want cache entry to exist.
34115798Smckusick 			 */
34226273Skarels 			/* remove from LRU chain */
34315798Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
34415798Smckusick 			if (ncp->nc_nxt)
34515798Smckusick 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
34615798Smckusick 			else
34715798Smckusick 				nchtail = ncp->nc_prev;
34826273Skarels 			remque(ncp);		/* remove from hash chain */
34926273Skarels 			/* insert at head of LRU list (first to grab) */
35015798Smckusick 			ncp->nc_nxt = nchhead;
35115798Smckusick 			ncp->nc_prev = &nchhead;
35215798Smckusick 			nchhead->nc_prev = &ncp->nc_nxt;
35315798Smckusick 			nchhead = ncp;
35426273Skarels 			/* and make a dummy hash chain */
35515798Smckusick 			ncp->nc_forw = ncp;
35615798Smckusick 			ncp->nc_back = ncp;
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 {
388*27268Smckusick 		if ((dp->i_flag & (ICHG|IMOD)) ||
389*27268Smckusick 		    dp->i_ctime >= u.u_ncache.nc_time) {
39017698Smckusick 			if (u.u_ncache.nc_prevoffset > dp->i_size)
39117698Smckusick 				u.u_ncache.nc_prevoffset = 0;
39217698Smckusick 			else
39317698Smckusick 				u.u_ncache.nc_prevoffset &= ~(DIRBLKSIZ - 1);
39415660Smckusick 			u.u_ncache.nc_time = time.tv_sec;
39515660Smckusick 		}
39616688Smckusick 		ndp->ni_offset = u.u_ncache.nc_prevoffset;
39716688Smckusick 		entryoffsetinblock = blkoff(fs, ndp->ni_offset);
39815660Smckusick 		if (entryoffsetinblock != 0) {
39916688Smckusick 			bp = blkatoff(dp, ndp->ni_offset, (char **)0);
40015660Smckusick 			if (bp == 0)
40115660Smckusick 				goto bad;
40215660Smckusick 		}
40315660Smckusick 		numdirpasses = 2;
40415798Smckusick 		nchstats.ncs_2passes++;
40515660Smckusick 	}
40615660Smckusick 	endsearch = roundup(dp->i_size, DIRBLKSIZ);
40718027Smckusick 	enduseful = 0;
4087534Sroot 
40915660Smckusick searchloop:
41016688Smckusick 	while (ndp->ni_offset < endsearch) {
4115972Swnj 		/*
4125972Swnj 		 * If offset is on a block boundary,
4135972Swnj 		 * read the next directory block.
4145972Swnj 		 * Release previous if it exists.
4155972Swnj 		 */
41616688Smckusick 		if (blkoff(fs, ndp->ni_offset) == 0) {
4175972Swnj 			if (bp != NULL)
4185972Swnj 				brelse(bp);
41916688Smckusick 			bp = blkatoff(dp, ndp->ni_offset, (char **)0);
4207534Sroot 			if (bp == 0)
4217534Sroot 				goto bad;
4227534Sroot 			entryoffsetinblock = 0;
4235972Swnj 		}
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 		/*
43416657Smckusick 		 * Get pointer to next entry.
43516657Smckusick 		 * Full validation checks are slow, so we only check
43616657Smckusick 		 * enough to insure forward progress through the
43716657Smckusick 		 * directory. Complete checks can be run by patching
43816657Smckusick 		 * "dirchk" to be true.
4397534Sroot 		 */
4407534Sroot 		ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock);
44126273Skarels 		if (ep->d_reclen == 0 ||
44216657Smckusick 		    dirchk && dirbadentry(ep, entryoffsetinblock)) {
44316688Smckusick 			dirbad(dp, ndp->ni_offset, "mangled entry");
44416657Smckusick 			i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
44516688Smckusick 			ndp->ni_offset += i;
4467534Sroot 			entryoffsetinblock += i;
4476571Smckusic 			continue;
4486571Smckusic 		}
4497534Sroot 
4506571Smckusic 		/*
4517534Sroot 		 * If an appropriate sized slot has not yet been found,
4526571Smckusic 		 * check to see if one is available. Also accumulate space
4536571Smckusic 		 * in the current block so that we can determine if
4546571Smckusic 		 * compaction is viable.
4556571Smckusic 		 */
4567534Sroot 		if (slotstatus != FOUND) {
4577534Sroot 			int size = ep->d_reclen;
4587534Sroot 
4596571Smckusic 			if (ep->d_ino != 0)
4606571Smckusic 				size -= DIRSIZ(ep);
4616571Smckusic 			if (size > 0) {
4627534Sroot 				if (size >= slotneeded) {
4637534Sroot 					slotstatus = FOUND;
46416688Smckusick 					slotoffset = ndp->ni_offset;
4657534Sroot 					slotsize = ep->d_reclen;
4667534Sroot 				} else if (slotstatus == NONE) {
4677534Sroot 					slotfreespace += size;
4687534Sroot 					if (slotoffset == -1)
46916688Smckusick 						slotoffset = ndp->ni_offset;
4707534Sroot 					if (slotfreespace >= slotneeded) {
4717534Sroot 						slotstatus = COMPACT;
47216688Smckusick 						slotsize = ndp->ni_offset +
47316688Smckusick 						      ep->d_reclen - slotoffset;
4747534Sroot 					}
4756571Smckusic 				}
4766571Smckusic 			}
4776571Smckusic 		}
4787534Sroot 
4796571Smckusic 		/*
4807534Sroot 		 * Check for a name match.
4815972Swnj 		 */
4827534Sroot 		if (ep->d_ino) {
48316688Smckusick 			if (ep->d_namlen == ndp->ni_dent.d_namlen &&
48416688Smckusick 			    !bcmp(ndp->ni_dent.d_name, ep->d_name,
48526273Skarels 				(unsigned)ep->d_namlen))
4867534Sroot 				goto found;
4877534Sroot 		}
48816688Smckusick 		prevoff = ndp->ni_offset;
48916688Smckusick 		ndp->ni_offset += ep->d_reclen;
4907534Sroot 		entryoffsetinblock += ep->d_reclen;
49118027Smckusick 		if (ep->d_ino)
49218027Smckusick 			enduseful = ndp->ni_offset;
4937534Sroot 	}
49415798Smckusick /* notfound: */
49515660Smckusick 	/*
49615798Smckusick 	 * If we started in the middle of the directory and failed
49715660Smckusick 	 * to find our target, we must check the beginning as well.
49815660Smckusick 	 */
49915660Smckusick 	if (numdirpasses == 2) {
50015660Smckusick 		numdirpasses--;
50116688Smckusick 		ndp->ni_offset = 0;
50215660Smckusick 		endsearch = u.u_ncache.nc_prevoffset;
50315660Smckusick 		goto searchloop;
50415660Smckusick 	}
5057534Sroot 	/*
5067534Sroot 	 * If creating, and at end of pathname and current
5079166Ssam 	 * directory has not been removed, then can consider
5089166Ssam 	 * allowing file to be created.
5097534Sroot 	 */
5109166Ssam 	if (flag == CREATE && *cp == 0 && dp->i_nlink != 0) {
5115972Swnj 		/*
5127534Sroot 		 * Access for write is interpreted as allowing
5137534Sroot 		 * creation of files in the directory.
5145972Swnj 		 */
5157534Sroot 		if (access(dp, IWRITE))
5167534Sroot 			goto bad;
5175972Swnj 		/*
5187534Sroot 		 * Return an indication of where the new directory
5197534Sroot 		 * entry should be put.  If we didn't find a slot,
52016688Smckusick 		 * then set ndp->ni_count to 0 indicating that the new
52116688Smckusick 		 * slot belongs at the end of the directory. If we found
52216688Smckusick 		 * a slot, then the new entry can be put in the range
52316688Smckusick 		 * [ndp->ni_offset .. ndp->ni_offset + ndp->ni_count)
5245972Swnj 		 */
52515660Smckusick 		if (slotstatus == NONE) {
52616688Smckusick 			ndp->ni_offset = roundup(dp->i_size, DIRBLKSIZ);
52716688Smckusick 			ndp->ni_count = 0;
52818027Smckusick 			enduseful = ndp->ni_offset;
52915660Smckusick 		} else {
53016688Smckusick 			ndp->ni_offset = slotoffset;
53116688Smckusick 			ndp->ni_count = slotsize;
53218027Smckusick 			if (enduseful < slotoffset + slotsize)
53318027Smckusick 				enduseful = slotoffset + slotsize;
5345972Swnj 		}
53518027Smckusick 		ndp->ni_endoff = roundup(enduseful, DIRBLKSIZ);
5367534Sroot 		dp->i_flag |= IUPD|ICHG;
5377534Sroot 		if (bp)
5387534Sroot 			brelse(bp);
53916681Smckusick 		nbp->av_forw = freenamebuf;
54016681Smckusick 		freenamebuf = nbp;
5415972Swnj 		/*
5427534Sroot 		 * We return with the directory locked, so that
5437534Sroot 		 * the parameters we set up above will still be
5447534Sroot 		 * valid if we actually decide to do a direnter().
5457534Sroot 		 * We return NULL to indicate that the entry doesn't
5467534Sroot 		 * currently exist, leaving a pointer to the (locked)
54716688Smckusick 		 * directory inode in ndp->ni_pdir.
5485972Swnj 		 */
54916688Smckusick 		ndp->ni_pdir = dp;
5507534Sroot 		return (NULL);
5517534Sroot 	}
5527534Sroot 	u.u_error = ENOENT;
5537534Sroot 	goto bad;
5547534Sroot found:
55515798Smckusick 	if (numdirpasses == 2)
55615798Smckusick 		nchstats.ncs_pass2++;
5577534Sroot 	/*
5587534Sroot 	 * Check that directory length properly reflects presence
5597534Sroot 	 * of this entry.
5607534Sroot 	 */
5617605Ssam 	if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) {
56216688Smckusick 		dirbad(dp, ndp->ni_offset, "i_size too small");
5637605Ssam 		dp->i_size = entryoffsetinblock + DIRSIZ(ep);
5647534Sroot 		dp->i_flag |= IUPD|ICHG;
5657534Sroot 	}
5667534Sroot 
5677534Sroot 	/*
56815660Smckusick 	 * Found component in pathname.
56915798Smckusick 	 * If the final component of path name, save information
57015660Smckusick 	 * in the cache as to where the entry was found.
5717534Sroot 	 */
57215660Smckusick 	if (*cp == '\0' && flag == LOOKUP) {
57316688Smckusick 		u.u_ncache.nc_prevoffset = ndp->ni_offset;
57415660Smckusick 		u.u_ncache.nc_inumber = dp->i_number;
57515660Smckusick 		u.u_ncache.nc_dev = dp->i_dev;
57615660Smckusick 		u.u_ncache.nc_time = time.tv_sec;
57715660Smckusick 	}
57815660Smckusick 	/*
57918109Smckusick 	 * Save directory entry's inode number and reclen in ndp->ni_dent,
58015660Smckusick 	 * and release directory buffer.
58115660Smckusick 	 */
58218109Smckusick 	ndp->ni_dent.d_ino = ep->d_ino;
58318109Smckusick 	ndp->ni_dent.d_reclen = ep->d_reclen;
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) {
64718109Smckusick 		if (dp == u.u_rdir) {
64816688Smckusick 			ndp->ni_dent.d_ino = dp->i_number;
64918109Smckusick 			makeentry = 0;
65018109Smckusick 		} else if (ndp->ni_dent.d_ino == ROOTINO &&
6517534Sroot 		   dp->i_number == ROOTINO) {
6527534Sroot 			for (i = 1; i < NMOUNT; i++)
6537534Sroot 			if (mount[i].m_bufp != NULL &&
6547534Sroot 			   mount[i].m_dev == dp->i_dev) {
6556571Smckusic 				iput(dp);
6567534Sroot 				dp = mount[i].m_inodp;
65716666Smckusick 				ILOCK(dp);
6585972Swnj 				dp->i_count++;
6597534Sroot 				fs = dp->i_fs;
6607534Sroot 				cp -= 2;     /* back over .. */
6617534Sroot 				goto dirloop2;
6625972Swnj 			}
66330Sbill 		}
6647534Sroot 	}
6657534Sroot 
6667534Sroot 	/*
6679166Ssam 	 * If rewriting (rename), return the inode and the
6689166Ssam 	 * information required to rewrite the present directory
6699166Ssam 	 * Must get inode of directory entry to verify it's a
6709166Ssam 	 * regular file, or empty directory.
6719166Ssam 	 */
6729166Ssam 	if ((flag == CREATE && lockparent) && *cp == 0) {
6739166Ssam 		if (access(dp, IWRITE))
6749166Ssam 			goto bad;
67516688Smckusick 		ndp->ni_pdir = dp;		/* for dirrewrite() */
6769166Ssam 		/*
6779166Ssam 		 * Careful about locking second inode.
6789166Ssam 		 * This can only occur if the target is ".".
6799166Ssam 		 */
68016688Smckusick 		if (dp->i_number == ndp->ni_dent.d_ino) {
6819166Ssam 			u.u_error = EISDIR;		/* XXX */
6829166Ssam 			goto bad;
6839166Ssam 		}
68416688Smckusick 		dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino);
6859166Ssam 		if (dp == NULL) {
68616688Smckusick 			iput(ndp->ni_pdir);
6879166Ssam 			goto bad;
6889166Ssam 		}
68916681Smckusick 		nbp->av_forw = freenamebuf;
69016681Smckusick 		freenamebuf = nbp;
6919166Ssam 		return (dp);
6929166Ssam 	}
6939166Ssam 
6949166Ssam 	/*
69512011Smckusick 	 * Check for symbolic link, which may require us to massage the
69612011Smckusick 	 * name before we continue translation.  We do not `iput' the
69712011Smckusick 	 * directory because we may need it again if the symbolic link
69812011Smckusick 	 * is relative to the current directory.  Instead we save it
69912011Smckusick 	 * unlocked as "pdp".  We must get the target inode before unlocking
70012011Smckusick 	 * the directory to insure that the inode will not be removed
70112011Smckusick 	 * before we get it.  We prevent deadlock by always fetching
70212011Smckusick 	 * inodes from the root, moving down the directory tree. Thus
70312011Smckusick 	 * when following backward pointers ".." we must unlock the
70412011Smckusick 	 * parent directory before getting the requested directory.
70512011Smckusick 	 * There is a potential race condition here if both the current
70612011Smckusick 	 * and parent directories are removed before the `iget' for the
70712011Smckusick 	 * inode associated with ".." returns.  We hope that this occurs
70812011Smckusick 	 * infrequently since we cannot avoid this race condition without
70912492Ssam 	 * implementing a sophisticated deadlock detection algorithm.
71012011Smckusick 	 * Note also that this simple deadlock detection scheme will not
71112011Smckusick 	 * work if the file system has any hard links other than ".."
71212011Smckusick 	 * that point backwards in the directory structure.
7137534Sroot 	 */
7147534Sroot 	pdp = dp;
71515798Smckusick 	if (isdotdot) {
71616666Smckusick 		IUNLOCK(pdp);	/* race to get the inode */
71716688Smckusick 		dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino);
71812011Smckusick 		if (dp == NULL)
71912011Smckusick 			goto bad2;
72016688Smckusick 	} else if (dp->i_number == ndp->ni_dent.d_ino) {
72112011Smckusick 		dp->i_count++;	/* we want ourself, ie "." */
72212011Smckusick 	} else {
72316688Smckusick 		dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino);
72416666Smckusick 		IUNLOCK(pdp);
72512011Smckusick 		if (dp == NULL)
72612011Smckusick 			goto bad2;
72712011Smckusick 	}
72815798Smckusick 
72915798Smckusick 	/*
73026273Skarels 	 * Insert name into cache if appropriate.
73115798Smckusick 	 */
73218109Smckusick 	if (makeentry) {
73315798Smckusick 		if (ncp != NULL)
73415798Smckusick 			panic("nami: duplicating cache");
73526273Skarels 		/*
73626273Skarels 		 * Free the cache slot at head of lru chain.
73726273Skarels 		 */
73815798Smckusick 		if (ncp = nchhead) {
73926273Skarels 			/* remove from lru chain */
74015798Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
74115798Smckusick 			if (ncp->nc_nxt)
74215798Smckusick 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
74315798Smckusick 			else
74415798Smckusick 				nchtail = ncp->nc_prev;
74526273Skarels 			remque(ncp);		/* remove from old hash chain */
74626273Skarels 			/* grab the inode we just found */
74715798Smckusick 			ncp->nc_ip = dp;
74826273Skarels 			/* fill in cache info */
74915798Smckusick 			ncp->nc_ino = pdp->i_number;	/* parents inum */
75015798Smckusick 			ncp->nc_dev = pdp->i_dev;	/* & device */
75115798Smckusick 			ncp->nc_idev = dp->i_dev;	/* our device */
75216643Ssam 			ncp->nc_id = dp->i_id;		/* identifier */
75316688Smckusick 			ncp->nc_nlen = ndp->ni_dent.d_namlen;
75426273Skarels 			bcopy(ndp->ni_dent.d_name, ncp->nc_name,
75526273Skarels 			    (unsigned)ncp->nc_nlen);
75626273Skarels 			/* link at end of lru chain */
75715798Smckusick 			ncp->nc_nxt = NULL;
75815798Smckusick 			ncp->nc_prev = nchtail;
75915798Smckusick 			*nchtail = ncp;
76015798Smckusick 			nchtail = &ncp->nc_nxt;
76126273Skarels 			/* and insert on hash chain */
76215798Smckusick 			insque(ncp, nhp);
76315798Smckusick 		}
76415798Smckusick 	}
76515798Smckusick 
76615798Smckusick haveino:
7677534Sroot 	fs = dp->i_fs;
7687534Sroot 
7697534Sroot 	/*
7707534Sroot 	 * Check for symbolic link
7717534Sroot 	 */
77216688Smckusick 	if ((dp->i_mode & IFMT) == IFLNK &&
77316688Smckusick 	    ((ndp->ni_nameiop & FOLLOW) || *cp == '/')) {
7747825Sroot 		u_int pathlen = strlen(cp) + 1;
7757534Sroot 
77621014Smckusick 		if (dp->i_size + pathlen >= MAXPATHLEN - 1) {
77721014Smckusick 			u.u_error = ENAMETOOLONG;
77821014Smckusick 			goto bad2;
77921014Smckusick 		}
78021014Smckusick 		if (++nlink > MAXSYMLINKS) {
7817534Sroot 			u.u_error = ELOOP;
7827534Sroot 			goto bad2;
7837534Sroot 		}
7848957Sroot 		ovbcopy(cp, nbp->b_un.b_addr + dp->i_size, pathlen);
7857751Sroot 		u.u_error =
7869166Ssam 		    rdwri(UIO_READ, dp, nbp->b_un.b_addr, (int)dp->i_size,
78726360Skarels 			(off_t)0, 1, (int *)0);
7887534Sroot 		if (u.u_error)
7897534Sroot 			goto bad2;
7907534Sroot 		cp = nbp->b_un.b_addr;
7917534Sroot 		iput(dp);
7925972Swnj 		if (*cp == '/') {
7937534Sroot 			irele(pdp);
7945972Swnj 			while (*cp == '/')
7955972Swnj 				cp++;
7967534Sroot 			if ((dp = u.u_rdir) == NULL)
7977534Sroot 				dp = rootdir;
79816666Smckusick 			ILOCK(dp);
7997534Sroot 			dp->i_count++;
8007534Sroot 		} else {
8017534Sroot 			dp = pdp;
80216666Smckusick 			ILOCK(dp);
8035972Swnj 		}
8047534Sroot 		fs = dp->i_fs;
8057534Sroot 		goto dirloop;
80630Sbill 	}
8077534Sroot 
80830Sbill 	/*
8097534Sroot 	 * Not a symbolic link.  If more pathname,
8107534Sroot 	 * continue at next component, else return.
81130Sbill 	 */
8127534Sroot 	if (*cp == '/') {
8137534Sroot 		while (*cp == '/')
8147534Sroot 			cp++;
8159166Ssam 		irele(pdp);
8167534Sroot 		goto dirloop;
81730Sbill 	}
81816681Smckusick 	nbp->av_forw = freenamebuf;
81916681Smckusick 	freenamebuf = nbp;
8209166Ssam 	if (lockparent)
82116688Smckusick 		ndp->ni_pdir = pdp;
8229166Ssam 	else
8239166Ssam 		irele(pdp);
8247534Sroot 	return (dp);
8257534Sroot bad2:
8267534Sroot 	irele(pdp);
8277534Sroot bad:
8287534Sroot 	if (bp)
8297534Sroot 		brelse(bp);
8307534Sroot 	if (dp)
8317534Sroot 		iput(dp);
83216681Smckusick 	nbp->av_forw = freenamebuf;
83316681Smckusick 	freenamebuf = nbp;
8346571Smckusic 	return (NULL);
83530Sbill }
83630Sbill 
83715798Smckusick 
83816688Smckusick dirbad(ip, offset, how)
8397534Sroot 	struct inode *ip;
84016688Smckusick 	off_t offset;
8417534Sroot 	char *how;
8427534Sroot {
8437534Sroot 
8447534Sroot 	printf("%s: bad dir ino %d at offset %d: %s\n",
84516688Smckusick 	    ip->i_fs->fs_fsmnt, ip->i_number, offset, how);
8467534Sroot }
8477534Sroot 
84816657Smckusick /*
84916657Smckusick  * Do consistency checking on a directory entry:
85016657Smckusick  *	record length must be multiple of 4
85116657Smckusick  *	entry must fit in rest of its DIRBLKSIZ block
85216657Smckusick  *	record must be large enough to contain entry
85316657Smckusick  *	name is not longer than MAXNAMLEN
85416657Smckusick  *	name must be as long as advertised, and null terminated
85516657Smckusick  */
85616657Smckusick dirbadentry(ep, entryoffsetinblock)
8577534Sroot 	register struct direct *ep;
85816657Smckusick 	int entryoffsetinblock;
8597534Sroot {
8607534Sroot 	register int i;
8617534Sroot 
86226273Skarels 	if ((ep->d_reclen & 0x3) != 0 ||
86316657Smckusick 	    ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
86416657Smckusick 	    ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN)
86516657Smckusick 		return (1);
8667534Sroot 	for (i = 0; i < ep->d_namlen; i++)
86716688Smckusick 		if (ep->d_name[i] == '\0')
8687534Sroot 			return (1);
8697534Sroot 	return (ep->d_name[i]);
8707534Sroot }
8717534Sroot 
87230Sbill /*
8737534Sroot  * Write a directory entry after a call to namei, using the parameters
8747534Sroot  * which it left in the u. area.  The argument ip is the inode which
87516688Smckusick  * the new directory entry will refer to.  The u. area field ndp->ni_pdir is
8767534Sroot  * a pointer to the directory to be written, which was left locked by
87716688Smckusick  * namei.  Remaining parameters (ndp->ni_offset, ndp->ni_count) indicate
8787534Sroot  * how the space for the new entry is to be gotten.
8797534Sroot  */
88016688Smckusick direnter(ip, ndp)
8817534Sroot 	struct inode *ip;
88216688Smckusick 	register struct nameidata *ndp;
8835972Swnj {
8847534Sroot 	register struct direct *ep, *nep;
88518027Smckusick 	register struct inode *dp = ndp->ni_pdir;
8867534Sroot 	struct buf *bp;
88711639Ssam 	int loc, spacefree, error = 0;
8888631Sroot 	u_int dsize;
8898631Sroot 	int newentrysize;
8907534Sroot 	char *dirbuf;
8915972Swnj 
89216688Smckusick 	ndp->ni_dent.d_ino = ip->i_number;
89316688Smckusick 	newentrysize = DIRSIZ(&ndp->ni_dent);
89416688Smckusick 	if (ndp->ni_count == 0) {
8957534Sroot 		/*
89616688Smckusick 		 * If ndp->ni_count is 0, then namei could find no space in the
89716688Smckusick 		 * directory. In this case ndp->ni_offset will be on a directory
8987534Sroot 		 * block boundary and we will write the new entry into a fresh
8997534Sroot 		 * block.
9007534Sroot 		 */
90116688Smckusick 		if (ndp->ni_offset&(DIRBLKSIZ-1))
9027534Sroot 			panic("wdir: newblk");
90316688Smckusick 		ndp->ni_dent.d_reclen = DIRBLKSIZ;
90418027Smckusick 		error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent,
90516688Smckusick 		    newentrysize, ndp->ni_offset, 1, (int *)0);
90618104Smckusick 		if (DIRBLKSIZ > dp->i_fs->fs_fsize)
90718104Smckusick 			panic("wdir: blksize"); /* XXX - should grow w/bmap() */
90818104Smckusick 		else
90918104Smckusick 			dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
91018027Smckusick 		iput(dp);
91110849Ssam 		return (error);
9127534Sroot 	}
9137534Sroot 
9147534Sroot 	/*
91516688Smckusick 	 * If ndp->ni_count is non-zero, then namei found space for the new
91616688Smckusick 	 * entry in the range ndp->ni_offset to ndp->ni_offset + ndp->ni_count.
9177534Sroot 	 * in the directory.  To use this space, we may have to compact
9187534Sroot 	 * the entries located there, by copying them together towards
9197534Sroot 	 * the beginning of the block, leaving the free space in
9207534Sroot 	 * one usable chunk at the end.
9217534Sroot 	 */
9227534Sroot 
9237534Sroot 	/*
9247534Sroot 	 * Increase size of directory if entry eats into new space.
9257534Sroot 	 * This should never push the size past a new multiple of
9267534Sroot 	 * DIRBLKSIZE.
92718104Smckusick 	 *
92818104Smckusick 	 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
9297534Sroot 	 */
93018027Smckusick 	if (ndp->ni_offset + ndp->ni_count > dp->i_size)
93118027Smckusick 		dp->i_size = ndp->ni_offset + ndp->ni_count;
9327534Sroot 	/*
9337534Sroot 	 * Get the block containing the space for the new directory
93410849Ssam 	 * entry.  Should return error by result instead of u.u_error.
9357534Sroot 	 */
93618027Smckusick 	bp = blkatoff(dp, ndp->ni_offset, (char **)&dirbuf);
9379166Ssam 	if (bp == 0) {
93818027Smckusick 		iput(dp);
93910849Ssam 		return (u.u_error);
9409166Ssam 	}
9417534Sroot 	/*
9427534Sroot 	 * Find space for the new entry.  In the simple case, the
9437534Sroot 	 * entry at offset base will have the space.  If it does
9447534Sroot 	 * not, then namei arranged that compacting the region
94516688Smckusick 	 * ndp->ni_offset to ndp->ni_offset+ndp->ni_count would yield the space.
9467534Sroot 	 */
9477534Sroot 	ep = (struct direct *)dirbuf;
9487534Sroot 	dsize = DIRSIZ(ep);
94911639Ssam 	spacefree = ep->d_reclen - dsize;
95016688Smckusick 	for (loc = ep->d_reclen; loc < ndp->ni_count; ) {
9517534Sroot 		nep = (struct direct *)(dirbuf + loc);
9527534Sroot 		if (ep->d_ino) {
9537534Sroot 			/* trim the existing slot */
9547534Sroot 			ep->d_reclen = dsize;
9557534Sroot 			ep = (struct direct *)((char *)ep + dsize);
9567534Sroot 		} else {
9577534Sroot 			/* overwrite; nothing there; header is ours */
95811639Ssam 			spacefree += dsize;
9597534Sroot 		}
9607534Sroot 		dsize = DIRSIZ(nep);
96111639Ssam 		spacefree += nep->d_reclen - dsize;
9627534Sroot 		loc += nep->d_reclen;
9637825Sroot 		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
9647534Sroot 	}
9657534Sroot 	/*
9667534Sroot 	 * Update the pointer fields in the previous entry (if any),
9677534Sroot 	 * copy in the new entry, and write out the block.
9687534Sroot 	 */
9697534Sroot 	if (ep->d_ino == 0) {
97011639Ssam 		if (spacefree + dsize < newentrysize)
9717534Sroot 			panic("wdir: compact1");
97216688Smckusick 		ndp->ni_dent.d_reclen = spacefree + dsize;
9737534Sroot 	} else {
97411639Ssam 		if (spacefree < newentrysize)
9757534Sroot 			panic("wdir: compact2");
97616688Smckusick 		ndp->ni_dent.d_reclen = spacefree;
9777534Sroot 		ep->d_reclen = dsize;
9787534Sroot 		ep = (struct direct *)((char *)ep + dsize);
9797534Sroot 	}
98016688Smckusick 	bcopy((caddr_t)&ndp->ni_dent, (caddr_t)ep, (u_int)newentrysize);
9817534Sroot 	bwrite(bp);
98218027Smckusick 	dp->i_flag |= IUPD|ICHG;
98318027Smckusick 	if (ndp->ni_endoff && ndp->ni_endoff < dp->i_size)
98426273Skarels 		itrunc(dp, (u_long)ndp->ni_endoff);
98518027Smckusick 	iput(dp);
98610849Ssam 	return (error);
9875972Swnj }
9886571Smckusic 
9899166Ssam /*
9909166Ssam  * Remove a directory entry after a call to namei, using the
9919166Ssam  * parameters which it left in the u. area.  The u. entry
99216688Smckusick  * ni_offset contains the offset into the directory of the
99316688Smckusick  * entry to be eliminated.  The ni_count field contains the
9949166Ssam  * size of the previous record in the directory.  If this
9959166Ssam  * is 0, the first entry is being deleted, so we need only
9969166Ssam  * zero the inode number to mark the entry as free.  If the
9979166Ssam  * entry isn't the first in the directory, we must reclaim
9989166Ssam  * the space of the now empty record by adding the record size
9999166Ssam  * to the size of the previous entry.
10009166Ssam  */
100116688Smckusick dirremove(ndp)
100216688Smckusick 	register struct nameidata *ndp;
10036571Smckusic {
100416688Smckusick 	register struct inode *dp = ndp->ni_pdir;
10057534Sroot 	register struct buf *bp;
10067534Sroot 	struct direct *ep;
10076571Smckusic 
100816688Smckusick 	if (ndp->ni_count == 0) {
10097534Sroot 		/*
10107534Sroot 		 * First entry in block: set d_ino to zero.
10117534Sroot 		 */
101216688Smckusick 		ndp->ni_dent.d_ino = 0;
101316688Smckusick 		(void) rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent,
101416688Smckusick 		    (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0);
10159269Ssam 	} else {
10167534Sroot 		/*
10177534Sroot 		 * Collapse new free space into previous entry.
10187534Sroot 		 */
101926360Skarels 		bp = blkatoff(dp, ndp->ni_offset - ndp->ni_count, (char **)&ep);
10207534Sroot 		if (bp == 0)
10217534Sroot 			return (0);
102216688Smckusick 		ep->d_reclen += ndp->ni_dent.d_reclen;
10237534Sroot 		bwrite(bp);
10247534Sroot 		dp->i_flag |= IUPD|ICHG;
10257534Sroot 	}
10267534Sroot 	return (1);
10276571Smckusic }
10287534Sroot 
10297605Ssam /*
10309166Ssam  * Rewrite an existing directory entry to point at the inode
10319166Ssam  * supplied.  The parameters describing the directory entry are
10329166Ssam  * set up by a call to namei.
10339166Ssam  */
103416688Smckusick dirrewrite(dp, ip, ndp)
10359166Ssam 	struct inode *dp, *ip;
103616688Smckusick 	struct nameidata *ndp;
10379166Ssam {
10389166Ssam 
103916688Smckusick 	ndp->ni_dent.d_ino = ip->i_number;
104016688Smckusick 	u.u_error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent,
104116688Smckusick 		(int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0);
10429166Ssam 	iput(dp);
10439166Ssam }
10449166Ssam 
10459166Ssam /*
10467605Ssam  * Return buffer with contents of block "offset"
10477605Ssam  * from the beginning of directory "ip".  If "res"
10487605Ssam  * is non-zero, fill it in with a pointer to the
10497605Ssam  * remaining space in the directory.
10507605Ssam  */
10517534Sroot struct buf *
10527605Ssam blkatoff(ip, offset, res)
10537534Sroot 	struct inode *ip;
10547534Sroot 	off_t offset;
10557534Sroot 	char **res;
10567534Sroot {
10577534Sroot 	register struct fs *fs = ip->i_fs;
10588672Sroot 	daddr_t lbn = lblkno(fs, offset);
10597534Sroot 	int bsize = blksize(fs, ip, lbn);
10607534Sroot 	register struct buf *bp;
106118663Smckusick 	daddr_t bn;
10627534Sroot 
106318663Smckusick 	bn = bmap(ip, lbn, B_READ, bsize);
10647534Sroot 	if (u.u_error)
10657534Sroot 		return (0);
106618663Smckusick 	if (bn == (daddr_t)-1) {
106718663Smckusick 		dirbad(ip, offset, "hole in dir");
106818663Smckusick 		return (0);
106918663Smckusick 	}
107018663Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, bn), bsize);
10717534Sroot 	if (bp->b_flags & B_ERROR) {
10727534Sroot 		brelse(bp);
10737534Sroot 		return (0);
10747534Sroot 	}
10757534Sroot 	if (res)
107618663Smckusick 		*res = bp->b_un.b_addr + blkoff(fs, offset);
10777534Sroot 	return (bp);
10787534Sroot }
10799166Ssam 
10809166Ssam /*
10819166Ssam  * Check if a directory is empty or not.
10829166Ssam  * Inode supplied must be locked.
108312817Ssam  *
108412817Ssam  * Using a struct dirtemplate here is not precisely
108512817Ssam  * what we want, but better than using a struct direct.
108612817Ssam  *
108712817Ssam  * NB: does not handle corrupted directories.
10889166Ssam  */
108916777Smckusick dirempty(ip, parentino)
10909863Ssam 	register struct inode *ip;
109116777Smckusick 	ino_t parentino;
10929166Ssam {
10939166Ssam 	register off_t off;
109412817Ssam 	struct dirtemplate dbuf;
109512817Ssam 	register struct direct *dp = (struct direct *)&dbuf;
10969863Ssam 	int error, count;
109712817Ssam #define	MINDIRSIZ (sizeof (struct dirtemplate) / 2)
10989166Ssam 
10999166Ssam 	for (off = 0; off < ip->i_size; off += dp->d_reclen) {
110012817Ssam 		error = rdwri(UIO_READ, ip, (caddr_t)dp, MINDIRSIZ,
110112817Ssam 		    off, 1, &count);
110212817Ssam 		/*
110312817Ssam 		 * Since we read MINDIRSIZ, residual must
110412817Ssam 		 * be 0 unless we're at end of file.
110512817Ssam 		 */
110612817Ssam 		if (error || count != 0)
11079166Ssam 			return (0);
110824534Smckusick 		/* avoid infinite loops */
110926273Skarels 		if (dp->d_reclen == 0)
111024534Smckusick 			return (0);
111112817Ssam 		/* skip empty entries */
11129166Ssam 		if (dp->d_ino == 0)
11139166Ssam 			continue;
111412817Ssam 		/* accept only "." and ".." */
111512817Ssam 		if (dp->d_namlen > 2)
111612817Ssam 			return (0);
11179166Ssam 		if (dp->d_name[0] != '.')
11189166Ssam 			return (0);
111912817Ssam 		/*
112012817Ssam 		 * At this point d_namlen must be 1 or 2.
112112817Ssam 		 * 1 implies ".", 2 implies ".." if second
112212817Ssam 		 * char is also "."
112312817Ssam 		 */
112416777Smckusick 		if (dp->d_namlen == 1)
11259166Ssam 			continue;
112616777Smckusick 		if (dp->d_name[1] == '.' && dp->d_ino == parentino)
112716777Smckusick 			continue;
11289166Ssam 		return (0);
11299166Ssam 	}
11309166Ssam 	return (1);
11319166Ssam }
113212815Smckusick 
113312815Smckusick /*
113412815Smckusick  * Check if source directory is in the path of the target directory.
113512815Smckusick  * Target is supplied locked, source is unlocked.
113612815Smckusick  * The target is always iput() before returning.
113712815Smckusick  */
113812815Smckusick checkpath(source, target)
113912815Smckusick 	struct inode *source, *target;
114012815Smckusick {
114112815Smckusick 	struct dirtemplate dirbuf;
114212815Smckusick 	register struct inode *ip;
114312815Smckusick 	int error = 0;
114412815Smckusick 
114512815Smckusick 	ip = target;
114612815Smckusick 	if (ip->i_number == source->i_number) {
114712815Smckusick 		error = EEXIST;
114812815Smckusick 		goto out;
114912815Smckusick 	}
115012815Smckusick 	if (ip->i_number == ROOTINO)
115112815Smckusick 		goto out;
115212815Smckusick 
115312815Smckusick 	for (;;) {
115412815Smckusick 		if ((ip->i_mode&IFMT) != IFDIR) {
115512815Smckusick 			error = ENOTDIR;
115612815Smckusick 			break;
115712815Smckusick 		}
115812815Smckusick 		error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf,
115912815Smckusick 			sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
116012815Smckusick 		if (error != 0)
116112815Smckusick 			break;
116212815Smckusick 		if (dirbuf.dotdot_namlen != 2 ||
116316658Smckusick 		    dirbuf.dotdot_name[0] != '.' ||
116416658Smckusick 		    dirbuf.dotdot_name[1] != '.') {
116512815Smckusick 			error = ENOTDIR;
116612815Smckusick 			break;
116712815Smckusick 		}
116812815Smckusick 		if (dirbuf.dotdot_ino == source->i_number) {
116912815Smckusick 			error = EINVAL;
117012815Smckusick 			break;
117112815Smckusick 		}
117212815Smckusick 		if (dirbuf.dotdot_ino == ROOTINO)
117312815Smckusick 			break;
117412815Smckusick 		iput(ip);
117512815Smckusick 		ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino);
117612815Smckusick 		if (ip == NULL) {
117712815Smckusick 			error = u.u_error;
117812815Smckusick 			break;
117912815Smckusick 		}
118012815Smckusick 	}
118112815Smckusick 
118212815Smckusick out:
118312815Smckusick 	if (error == ENOTDIR)
118412815Smckusick 		printf("checkpath: .. not a directory\n");
118512815Smckusick 	if (ip != NULL)
118612815Smckusick 		iput(ip);
118712815Smckusick 	return (error);
118812815Smckusick }
118915798Smckusick 
119015798Smckusick /*
119115798Smckusick  * Name cache initialization, from main() when we are booting
119215798Smckusick  */
119315798Smckusick nchinit()
119415798Smckusick {
119515798Smckusick 	register union nchash *nchp;
119626306Skarels 	register struct namecache *ncp;
119715798Smckusick 
119815798Smckusick 	nchhead = 0;
119915798Smckusick 	nchtail = &nchhead;
120026306Skarels 	for (ncp = namecache; ncp < &namecache[nchsize]; ncp++) {
120115798Smckusick 		ncp->nc_forw = ncp;			/* hash chain */
120215798Smckusick 		ncp->nc_back = ncp;
120315798Smckusick 		ncp->nc_nxt = NULL;			/* lru chain */
120415798Smckusick 		*nchtail = ncp;
120515798Smckusick 		ncp->nc_prev = nchtail;
120615798Smckusick 		nchtail = &ncp->nc_nxt;
120715798Smckusick 		/* all else is zero already */
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 {
122626306Skarels 	register struct namecache *ncp, *nxtcp;
122715798Smckusick 
122815798Smckusick 	for (ncp = nchhead; ncp; ncp = nxtcp) {
122915798Smckusick 		nxtcp = ncp->nc_nxt;
123015798Smckusick 		if (ncp->nc_ip == NULL ||
123115798Smckusick 		    (ncp->nc_idev != dev && ncp->nc_dev != dev))
123215798Smckusick 			continue;
123326273Skarels 		/* free the resources we had */
123415798Smckusick 		ncp->nc_idev = NODEV;
123515798Smckusick 		ncp->nc_dev = NODEV;
123616658Smckusick 		ncp->nc_id = NULL;
123715798Smckusick 		ncp->nc_ino = 0;
123816658Smckusick 		ncp->nc_ip = NULL;
123926273Skarels 		remque(ncp);		/* remove entry from its hash chain */
124026273Skarels 		ncp->nc_forw = ncp;	/* and make a dummy one */
124115798Smckusick 		ncp->nc_back = ncp;
124226273Skarels 		/* delete this entry from LRU chain */
124315798Smckusick 		*ncp->nc_prev = nxtcp;
124415798Smckusick 		if (nxtcp)
124515798Smckusick 			nxtcp->nc_prev = ncp->nc_prev;
124615798Smckusick 		else
124715798Smckusick 			nchtail = ncp->nc_prev;
124826273Skarels 		/* cause rescan of list, it may have altered */
124915798Smckusick 		nxtcp = nchhead;
125026273Skarels 		/* put the now-free entry at head of LRU */
125115798Smckusick 		ncp->nc_nxt = nxtcp;
125215798Smckusick 		ncp->nc_prev = &nchhead;
125315798Smckusick 		nxtcp->nc_prev = &ncp->nc_nxt;
125415798Smckusick 		nchhead = ncp;
125515798Smckusick 	}
125615798Smckusick }
125717704Smckusick 
125817704Smckusick /*
125917704Smckusick  * Name cache invalidation of all entries.
126017704Smckusick  */
126117704Smckusick cacheinvalall()
126217704Smckusick {
126326306Skarels 	register struct namecache *ncp;
126417704Smckusick 
126526306Skarels 	for (ncp = namecache; ncp < &namecache[nchsize]; ncp++)
126617704Smckusick 		ncp->nc_id = 0;
126717704Smckusick }
1268