1 /*	$OpenBSD: namei.h,v 1.16 2005/07/20 17:01:14 miod Exp $	*/
2 /*	$NetBSD: namei.h,v 1.11 1996/02/09 18:25:20 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1985, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)namei.h	8.4 (Berkeley) 8/20/94
33  */
34 
35 #ifndef _SYS_NAMEI_H_
36 #define	_SYS_NAMEI_H_
37 
38 #include <sys/queue.h>
39 
40 /*
41  * Encapsulation of namei parameters.
42  */
43 struct nameidata {
44 	/*
45 	 * Arguments to namei/lookup.
46 	 */
47 	const char *ni_dirp;		/* pathname pointer */
48 	enum	uio_seg ni_segflg;	/* location of pathname */
49      /* u_long	ni_nameiop;		   namei operation */
50      /* u_long	ni_flags;		   flags to namei */
51      /* struct	proc *ni_proc;		   process requesting lookup */
52 	/*
53 	 * Arguments to lookup.
54 	 */
55      /* struct	ucred *ni_cred;		   credentials */
56 	struct	vnode *ni_startdir;	/* starting directory */
57 	struct	vnode *ni_rootdir;	/* logical root directory */
58 	/*
59 	 * Results: returned from/manipulated by lookup
60 	 */
61 	struct	vnode *ni_vp;		/* vnode of result */
62 	struct	vnode *ni_dvp;		/* vnode of intermediate directory */
63 	/*
64 	 * Shared between namei and lookup/commit routines.
65 	 */
66 	size_t	ni_pathlen;		/* remaining chars in path */
67 	char	*ni_next;		/* next location in pathname */
68 	u_long	ni_loopcnt;		/* count of symlinks encountered */
69 	/*
70 	 * Lookup parameters: this structure describes the subset of
71 	 * information from the nameidata structure that is passed
72 	 * through the VOP interface.
73 	 */
74 	struct componentname {
75 		/*
76 		 * Arguments to lookup.
77 		 */
78 		u_long	cn_nameiop;	/* namei operation */
79 		u_long	cn_flags;	/* flags to namei */
80 		struct	proc *cn_proc;	/* process requesting lookup */
81 		struct	ucred *cn_cred;	/* credentials */
82 		/*
83 		 * Shared between lookup and commit routines.
84 		 */
85 		char	*cn_pnbuf;	/* pathname buffer */
86 		char	*cn_nameptr;	/* pointer to looked up name */
87 		long	cn_namelen;	/* length of looked up component */
88 		u_long	cn_hash;	/* hash value of looked up name */
89 		long	cn_consume;	/* chars to consume in lookup() */
90 	} ni_cnd;
91 };
92 
93 #ifdef _KERNEL
94 /*
95  * namei operations
96  */
97 #define	LOOKUP		0	/* perform name lookup only */
98 #define	CREATE		1	/* setup for file creation */
99 #define	DELETE		2	/* setup for file deletion */
100 #define	RENAME		3	/* setup for file renaming */
101 #define	OPMASK		3	/* mask for operation */
102 /*
103  * namei operational modifier flags, stored in ni_cnd.flags
104  */
105 #define	LOCKLEAF	0x0004	/* lock inode on return */
106 #define	LOCKPARENT	0x0008	/* want parent vnode returned locked */
107 #define	WANTPARENT	0x0010	/* want parent vnode returned unlocked */
108 #define	NOCACHE		0x0020	/* name must not be left in cache */
109 #define	FOLLOW		0x0040	/* follow symbolic links */
110 #define	NOFOLLOW	0x0000	/* do not follow symbolic links (pseudo) */
111 #define	MODMASK		0x00fc	/* mask of operational modifiers */
112 /*
113  * Namei parameter descriptors.
114  *
115  * SAVENAME may be set by either the callers of namei or by VOP_LOOKUP.
116  * If the caller of namei sets the flag (for example execve wants to
117  * know the name of the program that is being executed), then it must
118  * free the buffer. If VOP_LOOKUP sets the flag, then the buffer must
119  * be freed by either the commit routine or the VOP_ABORT routine.
120  * SAVESTART is set only by the callers of namei. It implies SAVENAME
121  * plus the addition of saving the parent directory that contains the
122  * name in ni_startdir. It allows repeated calls to lookup for the
123  * name being sought. The caller is responsible for releasing the
124  * buffer and for vrele'ing ni_startdir.
125  */
126 #define	NOCROSSMOUNT	0x000100      /* do not cross mount points */
127 #define	RDONLY		0x000200      /* lookup with read-only semantics */
128 #define	HASBUF		0x000400      /* has allocated pathname buffer */
129 #define	SAVENAME	0x000800      /* save pathanme buffer */
130 #define	SAVESTART	0x001000      /* save starting directory */
131 #define ISDOTDOT	0x002000      /* current component name is .. */
132 #define MAKEENTRY	0x004000      /* entry is to be added to name cache */
133 #define ISLASTCN	0x008000      /* this is last component of pathname */
134 #define ISSYMLINK	0x010000      /* symlink needs interpretation */
135 #define	REQUIREDIR	0x080000      /* must be a directory */
136 #define STRIPSLASHES    0x100000      /* strip trailing slashes */
137 #define PDIRUNLOCK	0x200000      /* vfs_lookup() unlocked parent dir */
138 
139 /*
140  * Initialization of an nameidata structure.
141  */
142 #define NDINIT(ndp, op, flags, segflg, namep, p) { \
143 	(ndp)->ni_cnd.cn_nameiop = op; \
144 	(ndp)->ni_cnd.cn_flags = flags; \
145 	(ndp)->ni_segflg = segflg; \
146 	(ndp)->ni_dirp = namep; \
147 	(ndp)->ni_cnd.cn_proc = p; \
148 }
149 #endif
150 
151 /*
152  * This structure describes the elements in the cache of recent
153  * names looked up by namei. NCHNAMLEN is sized to make structure
154  * size a power of two to optimize malloc's. Minimum reasonable
155  * size is 15.
156  */
157 
158 #define	NCHNAMLEN	31	/* maximum name segment length we bother with */
159 
160 struct	namecache {
161 	LIST_ENTRY(namecache) nc_hash;	/* hash chain */
162 	LIST_ENTRY(namecache) nc_vhash;	/* (reverse) directory hash chain */
163 	TAILQ_ENTRY(namecache) nc_lru;	/* LRU chain */
164 	struct	vnode *nc_dvp;		/* vnode of parent of name */
165 	u_long	nc_dvpid;		/* capability number of nc_dvp */
166 	struct	vnode *nc_vp;		/* vnode the name refers to */
167 	u_long	nc_vpid;		/* capability number of nc_vp */
168 	char	nc_nlen;		/* length of name */
169 	char	nc_name[NCHNAMLEN];	/* segment name */
170 };
171 
172 #ifdef _KERNEL
173 extern u_long nextvnodeid;
174 int	namei(struct nameidata *ndp);
175 int	lookup(struct nameidata *ndp);
176 int	relookup(struct vnode *dvp, struct vnode **vpp,
177 		      struct componentname *cnp);
178 void cache_purge(struct vnode *);
179 int cache_lookup(struct vnode *, struct vnode **, struct componentname *);
180 void cache_enter(struct vnode *, struct vnode *, struct componentname *);
181 int cache_revlookup(struct vnode *, struct vnode **, char **, char *);
182 void nchinit(void);
183 struct mount;
184 void cache_purgevfs(struct mount *);
185 
186 extern struct pool namei_pool;
187 
188 #endif
189 
190 /*
191  * Stats on usefulness of namei caches.
192  */
193 struct	nchstats {
194 	long	ncs_goodhits;		/* hits that we can really use */
195 	long	ncs_neghits;		/* negative hits that we can use */
196 	long	ncs_badhits;		/* hits we must drop */
197 	long	ncs_falsehits;		/* hits with id mismatch */
198 	long	ncs_miss;		/* misses */
199 	long	ncs_long;		/* long names that ignore cache */
200 	long	ncs_pass2;		/* names found with passes == 2 */
201 	long	ncs_2passes;		/* number of times we attempt it */
202 	long    ncs_revhits;		/* reverse-cache hits */
203 	long    ncs_revmiss;		/* reverse-cache misses */
204 };
205 
206 /* These sysctl names are only really used by sysctl(8) */
207 #define KERN_NCHSTATS_GOODHITS		1
208 #define KERN_NCHSTATS_NEGHITS		2
209 #define KERN_NCHSTATS_BADHITS		3
210 #define KERN_NCHSTATS_FALSEHITS		4
211 #define KERN_NCHSTATS_MISS		5
212 #define KERN_NCHSTATS_LONG		6
213 #define KERN_NCHSTATS_PASS2		7
214 #define KERN_NCHSTATS_2PASSES		8
215 #define KERN_NCHSTATS_REVHITS           9
216 #define KERN_NCHSTATS_REVMISS           10
217 #define KERN_NCHSTATS_MAXID		11
218 
219 #define CTL_KERN_NCHSTATS_NAMES {		\
220 	{ 0, 0 },				\
221 	{ "good_hits", CTLTYPE_INT },		\
222 	{ "negative_hits", CTLTYPE_INT },	\
223 	{ "bad_hits", CTLTYPE_INT },		\
224 	{ "false_hits", CTLTYPE_INT },		\
225 	{ "misses", CTLTYPE_INT },		\
226 	{ "long_names", CTLTYPE_INT },		\
227 	{ "pass2", CTLTYPE_INT },		\
228 	{ "2passes", CTLTYPE_INT },		\
229 	{ "ncs_revhits", CTLTYPE_INT },		\
230 	{ "ncs_revmiss", CTLTYPE_INT },		\
231 }
232 #endif /* !_SYS_NAMEI_H_ */
233