xref: /freebsd/sys/sys/namei.h (revision 4b9d6057)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1985, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _SYS_NAMEI_H_
33 #define	_SYS_NAMEI_H_
34 
35 #include <sys/caprights.h>
36 #include <sys/filedesc.h>
37 #include <sys/queue.h>
38 #include <sys/_seqc.h>
39 #include <sys/_uio.h>
40 
41 enum nameiop { LOOKUP, CREATE, DELETE, RENAME };
42 
43 struct componentname {
44 	/*
45 	 * Arguments to lookup.
46 	 */
47 	u_int64_t cn_flags;	/* flags to namei */
48 	struct	ucred *cn_cred;	/* credentials */
49 	enum nameiop cn_nameiop;	/* namei operation */
50 	int	cn_lkflags;	/* Lock flags LK_EXCLUSIVE or LK_SHARED */
51 	/*
52 	 * Shared between lookup and commit routines.
53 	 */
54 	char	*cn_pnbuf;	/* pathname buffer */
55 	char	*cn_nameptr;	/* pointer to looked up name */
56 	long	cn_namelen;	/* length of looked up component */
57 };
58 
59 struct nameicap_tracker;
60 TAILQ_HEAD(nameicap_tracker_head, nameicap_tracker);
61 
62 /*
63  * Encapsulation of namei parameters.
64  */
65 struct nameidata {
66 	/*
67 	 * Arguments to namei/lookup.
68 	 */
69 	const	char *ni_dirp;		/* pathname pointer */
70 	enum	uio_seg ni_segflg;	/* location of pathname */
71 	cap_rights_t *ni_rightsneeded;	/* rights required to look up vnode */
72 	/*
73 	 * Arguments to lookup.
74 	 */
75 	struct  vnode *ni_startdir;	/* starting directory */
76 	struct	vnode *ni_rootdir;	/* logical root directory */
77 	struct	vnode *ni_topdir;	/* logical top directory */
78 	int	ni_dirfd;		/* starting directory for *at functions */
79 	int	ni_lcf;			/* local call flags */
80 	/*
81 	 * Results: returned from namei
82 	 */
83 	struct filecaps ni_filecaps;	/* rights the *at base has */
84 	/*
85 	 * Results: returned from/manipulated by lookup
86 	 */
87 	struct	vnode *ni_vp;		/* vnode of result */
88 	struct	vnode *ni_dvp;		/* vnode of intermediate directory */
89 	/*
90 	 * Results: flags returned from namei
91 	 */
92 	u_int	ni_resflags;
93 	/*
94 	 * Debug for validating API use by the callers.
95 	 */
96 	u_short	ni_debugflags;
97 	/*
98 	 * Shared between namei and lookup/commit routines.
99 	 */
100 	u_short	ni_loopcnt;		/* count of symlinks encountered */
101 	size_t	ni_pathlen;		/* remaining chars in path */
102 	char	*ni_next;		/* next location in pathname */
103 	/*
104 	 * Lookup parameters: this structure describes the subset of
105 	 * information from the nameidata structure that is passed
106 	 * through the VOP interface.
107 	 */
108 	struct componentname ni_cnd;
109 	struct nameicap_tracker_head ni_cap_tracker;
110 	/*
111 	 * Private helper data for UFS, must be at the end.  See
112 	 * NDINIT_PREFILL().
113 	 */
114 	seqc_t	ni_dvp_seqc;
115 	seqc_t	ni_vp_seqc;
116 };
117 
118 #ifdef _KERNEL
119 
120 enum cache_fpl_status { CACHE_FPL_STATUS_DESTROYED, CACHE_FPL_STATUS_ABORTED,
121     CACHE_FPL_STATUS_PARTIAL, CACHE_FPL_STATUS_HANDLED, CACHE_FPL_STATUS_UNSET };
122 int	cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
123     struct pwd **pwdp);
124 
125 /*
126  * Flags for namei.
127  *
128  * If modifying the list make sure to check whether NDVALIDATE needs updating.
129  */
130 
131 /*
132  * Debug.
133  */
134 #define	NAMEI_DBG_INITED	0x0001
135 #define	NAMEI_DBG_CALLED	0x0002
136 #define	NAMEI_DBG_HADSTARTDIR	0x0004
137 
138 /*
139  * namei operational modifier flags, stored in ni_cnd.flags
140  */
141 #define	NC_NOMAKEENTRY	0x0001	/* name must not be added to cache */
142 #define	NC_KEEPPOSENTRY	0x0002	/* don't evict a positive entry */
143 #define	NOCACHE		NC_NOMAKEENTRY	/* for compatibility with older code */
144 #define	LOCKLEAF	0x0004	/* lock vnode on return */
145 #define	LOCKPARENT	0x0008	/* want parent vnode returned locked */
146 #define	WANTPARENT	0x0010	/* want parent vnode returned unlocked */
147 #define	FAILIFEXISTS	0x0020	/* return EEXIST if found */
148 #define	FOLLOW		0x0040	/* follow symbolic links */
149 #define	EMPTYPATH	0x0080	/* Allow empty path for *at */
150 #define	LOCKSHARED	0x0100	/* Shared lock leaf */
151 #define	NOFOLLOW	0x0000	/* do not follow symbolic links (pseudo) */
152 #define	RBENEATH	0x100000000ULL /* No escape, even tmp, from start dir */
153 #define	MODMASK		0xf000001ffULL	/* mask of operational modifiers */
154 
155 /*
156  * Namei parameter descriptors.
157  */
158 #define	RDONLY		0x00000200 /* lookup with read-only semantics */
159 #define	ISRESTARTED	0x00000400 /* restarted namei */
160 /* UNUSED		0x00000800 */
161 #define	ISWHITEOUT	0x00001000 /* found whiteout */
162 #define	DOWHITEOUT	0x00002000 /* do whiteouts */
163 #define	WILLBEDIR	0x00004000 /* new files will be dirs; allow trailing / */
164 #define	ISOPEN		0x00008000 /* caller is opening; return a real vnode. */
165 #define	NOCROSSMOUNT	0x00010000 /* do not cross mount points */
166 #define	NOMACCHECK	0x00020000 /* do not perform MAC checks */
167 #define	AUDITVNODE1	0x00040000 /* audit the looked up vnode information */
168 #define	AUDITVNODE2	0x00080000 /* audit the looked up vnode information */
169 #define	NOCAPCHECK	0x00100000 /* do not perform capability checks */
170 #define	OPENREAD	0x00200000 /* open for reading */
171 #define	OPENWRITE	0x00400000 /* open for writing */
172 #define	WANTIOCTLCAPS	0x00800000 /* leave ioctl caps for the caller */
173 /* UNUSED		0x01000000 */
174 #define	NOEXECCHECK	0x02000000 /* do not perform exec check on dir */
175 #define	MAKEENTRY	0x04000000 /* entry is to be added to name cache */
176 #define	ISSYMLINK	0x08000000 /* symlink needs interpretation */
177 #define	ISLASTCN	0x10000000 /* this is last component of pathname */
178 #define	ISDOTDOT	0x20000000 /* current component name is .. */
179 #define	TRAILINGSLASH	0x40000000 /* path ended in a slash */
180 #define	PARAMASK	0x7ffffe00 /* mask of parameter descriptors */
181 
182 /*
183  * Flags which must not be passed in by callers.
184  */
185 #define NAMEI_INTERNAL_FLAGS	\
186 	(NOEXECCHECK | MAKEENTRY | ISSYMLINK | ISLASTCN | ISDOTDOT | \
187 	 TRAILINGSLASH | ISRESTARTED)
188 
189 /*
190  * Namei results flags
191  */
192 #define	NIRES_ABS	0x00000001 /* Path was absolute */
193 #define	NIRES_STRICTREL	0x00000002 /* Restricted lookup result */
194 #define	NIRES_EMPTYPATH	0x00000004 /* EMPTYPATH used */
195 
196 /*
197  * Flags in ni_lcf, valid for the duration of the namei call.
198  */
199 #define	NI_LCF_STRICTRELATIVE	0x0001	/* relative lookup only */
200 #define	NI_LCF_CAP_DOTDOT	0x0002	/* ".." in strictrelative case */
201 
202 /*
203  * Initialization of a nameidata structure.
204  */
205 #define	NDINIT(ndp, op, flags, segflg, namep)				\
206 	NDINIT_ALL(ndp, op, flags, segflg, namep, AT_FDCWD, NULL, &cap_no_rights)
207 #define	NDINIT_AT(ndp, op, flags, segflg, namep, dirfd)			\
208 	NDINIT_ALL(ndp, op, flags, segflg, namep, dirfd, NULL, &cap_no_rights)
209 #define	NDINIT_ATRIGHTS(ndp, op, flags, segflg, namep, dirfd, rightsp) 	\
210 	NDINIT_ALL(ndp, op, flags, segflg, namep, dirfd, NULL, rightsp)
211 #define	NDINIT_ATVP(ndp, op, flags, segflg, namep, vp)			\
212 	NDINIT_ALL(ndp, op, flags, segflg, namep, AT_FDCWD, vp, &cap_no_rights)
213 
214 /*
215  * Note the constant pattern may *hide* bugs.
216  * Note also that we enable debug checks for non-TIED KLDs
217  * so that they can run on an INVARIANTS kernel without tripping over
218  * assertions on ni_debugflags state.
219  */
220 #if defined(INVARIANTS) || (defined(KLD_MODULE) && !defined(KLD_TIED))
221 #define NDINIT_PREFILL(arg)	memset(arg, 0xff, offsetof(struct nameidata,	\
222     ni_dvp_seqc))
223 #define NDINIT_DBG(arg)		{ (arg)->ni_debugflags = NAMEI_DBG_INITED; }
224 #define NDREINIT_DBG(arg)	{						\
225 	if (((arg)->ni_debugflags & NAMEI_DBG_INITED) == 0)			\
226 		panic("namei data not inited");					\
227 	if (((arg)->ni_debugflags & NAMEI_DBG_HADSTARTDIR) != 0)		\
228 		panic("NDREINIT on namei data with NAMEI_DBG_HADSTARTDIR");	\
229 	(arg)->ni_debugflags = NAMEI_DBG_INITED;				\
230 }
231 #else
232 #define NDINIT_PREFILL(arg)	do { } while (0)
233 #define NDINIT_DBG(arg)		do { } while (0)
234 #define NDREINIT_DBG(arg)	do { } while (0)
235 #endif
236 
237 #define NDINIT_ALL(ndp, op, flags, segflg, namep, dirfd, startdir, rightsp)	\
238 do {										\
239 	struct nameidata *_ndp = (ndp);						\
240 	cap_rights_t *_rightsp = (rightsp);					\
241 	MPASS(_rightsp != NULL);						\
242 	NDINIT_PREFILL(_ndp);							\
243 	NDINIT_DBG(_ndp);							\
244 	_ndp->ni_cnd.cn_nameiop = op;						\
245 	_ndp->ni_cnd.cn_flags = flags;						\
246 	_ndp->ni_segflg = segflg;						\
247 	_ndp->ni_dirp = namep;							\
248 	_ndp->ni_dirfd = dirfd;							\
249 	_ndp->ni_startdir = startdir;						\
250 	_ndp->ni_resflags = 0;							\
251 	filecaps_init(&_ndp->ni_filecaps);					\
252 	_ndp->ni_rightsneeded = _rightsp;					\
253 } while (0)
254 
255 #define NDREINIT(ndp)	do {							\
256 	struct nameidata *_ndp = (ndp);						\
257 	NDREINIT_DBG(_ndp);							\
258 	filecaps_free(&_ndp->ni_filecaps);					\
259 	_ndp->ni_resflags = 0;							\
260 	_ndp->ni_startdir = NULL;						\
261 } while (0)
262 
263 #define	NDPREINIT(ndp) do {							\
264 	(ndp)->ni_dvp_seqc = SEQC_MOD;						\
265 	(ndp)->ni_vp_seqc = SEQC_MOD;						\
266 } while (0)
267 
268 #define NDFREE_IOCTLCAPS(ndp) do {						\
269 	struct nameidata *_ndp = (ndp);						\
270 	filecaps_free(&_ndp->ni_filecaps);					\
271 } while (0)
272 
273 #define	NDFREE_PNBUF(ndp) do {							\
274 	struct nameidata *_ndp = (ndp);						\
275 	MPASS(_ndp->ni_cnd.cn_pnbuf != NULL);					\
276 	uma_zfree(namei_zone, _ndp->ni_cnd.cn_pnbuf);				\
277 	_ndp->ni_cnd.cn_pnbuf = NULL;						\
278 } while (0)
279 
280 int	namei(struct nameidata *ndp);
281 int	vfs_lookup(struct nameidata *ndp);
282 int	vfs_relookup(struct vnode *dvp, struct vnode **vpp,
283 	    struct componentname *cnp, bool refstart);
284 
285 #define namei_setup_rootdir(ndp, cnp, pwd) do {					\
286 	if (__predict_true((cnp->cn_flags & ISRESTARTED) == 0))			\
287 		ndp->ni_rootdir = pwd->pwd_adir;				\
288 	else									\
289 		ndp->ni_rootdir = pwd->pwd_rdir;				\
290 } while (0)
291 #endif
292 
293 /*
294  * Stats on usefulness of namei caches.
295  */
296 struct nchstats {
297 	long	ncs_goodhits;		/* hits that we can really use */
298 	long	ncs_neghits;		/* negative hits that we can use */
299 	long	ncs_badhits;		/* hits we must drop */
300 	long	ncs_falsehits;		/* hits with id mismatch */
301 	long	ncs_miss;		/* misses */
302 	long	ncs_long;		/* long names that ignore cache */
303 	long	ncs_pass2;		/* names found with passes == 2 */
304 	long	ncs_2passes;		/* number of times we attempt it */
305 };
306 
307 extern struct nchstats nchstats;
308 
309 #endif /* !_SYS_NAMEI_H_ */
310