xref: /netbsd-src/sys/sys/namei.src (revision 9bac47c1a91eb8976b7ac8c9228cb03859e734d9)
1/*	$NetBSD: namei.src,v 1.65 2024/07/01 00:58:05 christos Exp $	*/
2
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 *	@(#)namei.h	8.5 (Berkeley) 8/20/94
32 */
33
34#ifndef _SYS_NAMEI_H_
35#define	_SYS_NAMEI_H_
36
37#include <sys/queue.h>
38#include <sys/mutex.h>
39
40#if defined(_KERNEL) || defined(_MODULE)
41#include <sys/kauth.h>
42#include <sys/rwlock.h>
43
44/*
45 * Abstraction for a single pathname.
46 *
47 * This contains both the pathname string and (eventually) all
48 * metadata that determines how the path is to be interpreted.
49 * It is an opaque structure; the implementation is in vfs_lookup.c.
50 *
51 * To call namei, first set up a pathbuf with pathbuf_create or
52 * pathbuf_copyin, then do NDINIT(), then call namei, then AFTER THE
53 * STRUCT NAMEIDATA IS DEAD, call pathbuf_destroy. Don't destroy the
54 * pathbuf before you've finished using the nameidata, or mysterious
55 * bad things may happen.
56 *
57 * pathbuf_assimilate is like pathbuf_create but assumes ownership of
58 * the string buffer passed in, which MUST BE of size PATH_MAX and
59 * have been allocated with PNBUF_GET(). This should only be used when
60 * absolutely necessary; e.g. nfsd uses it for loading paths from
61 * mbufs.
62 */
63struct pathbuf;
64
65struct pathbuf *pathbuf_create(const char *path);
66struct pathbuf *pathbuf_assimilate(char *path);
67int pathbuf_copyin(const char *userpath, struct pathbuf **ret);
68void pathbuf_destroy(struct pathbuf *);
69
70/* get a copy of the (current) path string */
71void pathbuf_copystring(const struct pathbuf *, char *buf, size_t maxlen);
72
73/* hold a reference copy of the original path string */
74const char *pathbuf_stringcopy_get(struct pathbuf *);
75void pathbuf_stringcopy_put(struct pathbuf *, const char *);
76
77// XXX remove this
78int pathbuf_maybe_copyin(const char *userpath, enum uio_seg seg, struct pathbuf **ret);
79
80/*
81 * Lookup parameters: this structure describes the subset of
82 * information from the nameidata structure that is passed
83 * through the VOP interface.
84 */
85struct componentname {
86	/*
87	 * Arguments to lookup.
88	 */
89	uint32_t	cn_nameiop;	/* namei operation */
90	uint32_t	cn_flags;	/* flags to namei */
91	kauth_cred_t 	cn_cred;	/* credentials */
92	/*
93	 * Shared between lookup and commit routines.
94	 */
95	const char 	*cn_nameptr;	/* pointer to looked up name */
96	size_t		cn_namelen;	/* length of looked up comp */
97};
98
99/*
100 * Encapsulation of namei parameters.
101 */
102struct nameidata {
103	/*
104	 * Arguments to namei/lookup.
105	 */
106	struct vnode *ni_atdir;		/* startup dir, cwd if null */
107	struct pathbuf *ni_pathbuf;	/* pathname container */
108	char *ni_pnbuf;			/* extra pathname buffer ref (XXX) */
109	/*
110	 * Arguments to lookup.
111	 */
112	struct	vnode *ni_rootdir;	/* logical root directory */
113	struct	vnode *ni_erootdir;	/* emulation root directory */
114	/*
115	 * Results: returned from/manipulated by lookup
116	 */
117	struct	vnode *ni_vp;		/* vnode of result */
118	struct	vnode *ni_dvp;		/* vnode of intermediate directory */
119	/*
120	 * Shared between namei and lookup/commit routines.
121	 */
122	size_t		ni_pathlen;	/* remaining chars in path */
123	const char	*ni_next;	/* next location in pathname */
124	unsigned int	ni_loopcnt;	/* count of symlinks encountered */
125	/*
126	 * Lookup parameters: this structure describes the subset of
127	 * information from the nameidata structure that is passed
128	 * through the VOP interface.
129	 */
130	struct componentname ni_cnd;
131};
132
133/*
134 * namei operations
135 */
136NAMEIFL	LOOKUP		0	/* perform name lookup only */
137NAMEIFL	CREATE		1	/* setup for file creation */
138NAMEIFL	DELETE		2	/* setup for file deletion */
139NAMEIFL	RENAME		3	/* setup for file renaming */
140NAMEIFL	OPMASK		3	/* mask for operation */
141/*
142 * namei operational modifier flags, stored in ni_cnd.cn_flags
143 */
144NAMEIFL	LOCKLEAF	0x00000004	/* lock inode on return */
145NAMEIFL	LOCKPARENT	0x00000008	/* want parent vnode returned locked */
146NAMEIFL	TRYEMULROOT	0x00000010	/* try relative to emulation root
147					   first */
148NAMEIFL	NOCACHE		0x00000020	/* name must not be left in cache */
149NAMEIFL	FOLLOW		0x00000040	/* follow symbolic links */
150NAMEIFL	NOFOLLOW	0x00000000	/* do not follow symbolic links
151					   (pseudo) */
152NAMEIFL	EMULROOTSET	0x00000080	/* emulation root already
153					   in ni_erootdir */
154NAMEIFL	LOCKSHARED	0x00000100	/* want shared locks if possible */
155NAMEIFL	NOCHROOT	0x01000000	/* no chroot on abs path lookups */
156NAMEIFL	NONEXCLHACK	0x02000000	/* open wwith O_CREAT but not O_EXCL */
157NAMEIFL	MODMASK		0x030001fc	/* mask of operational modifiers */
158/*
159 * Namei parameter descriptors.
160 */
161NAMEIFL	NOCROSSMOUNT	0x0000800	/* do not cross mount points */
162NAMEIFL	RDONLY		0x0001000	/* lookup with read-only semantics */
163NAMEIFL	ISDOTDOT	0x0002000	/* current component name is .. */
164NAMEIFL	MAKEENTRY	0x0004000	/* entry is to be added to name cache */
165NAMEIFL	ISLASTCN	0x0008000	/* this is last component of pathname */
166NAMEIFL	WILLBEDIR	0x0010000	/* new files will be dirs */
167NAMEIFL	ISWHITEOUT	0x0020000	/* found whiteout */
168NAMEIFL	DOWHITEOUT	0x0040000	/* do whiteouts */
169NAMEIFL	REQUIREDIR	0x0080000	/* must be a directory */
170NAMEIFL	CREATEDIR	0x0200000	/* trailing slashes are ok */
171NAMEIFL	PARAMASK	0x02ff800	/* mask of parameter descriptors */
172
173/*
174 * Initialization of a nameidata structure.
175 */
176#define NDINIT(ndp, op, flags, pathbuf) { \
177	(ndp)->ni_cnd.cn_nameiop = op; \
178	(ndp)->ni_cnd.cn_flags = flags; \
179	(ndp)->ni_atdir = NULL; \
180	(ndp)->ni_pathbuf = pathbuf; \
181	(ndp)->ni_cnd.cn_cred = kauth_cred_get(); \
182}
183
184/*
185 * Use this to set the start directory for openat()-type operations.
186 */
187#define NDAT(ndp, dir) {			\
188	(ndp)->ni_atdir = (dir);		\
189}
190
191#endif
192
193#ifdef __NAMECACHE_PRIVATE
194#include <sys/rbtree.h>
195
196/*
197 * For simplicity (and economy of storage), names longer than
198 * a maximum length of NCHNAMLEN are stored in non-pooled storage.
199 */
200#define	NCHNAMLEN	sizeof(((struct namecache *)NULL)->nc_name)
201
202/*
203 * The uintptr_t-sized key value computed for each name consists of name
204 * length and a hash value.  On 32-bit platforms the top NC_NLEN_BITS of
205 * the 32-bit hash value is lobbed off.
206 */
207
208#define	NC_NLEN_BITS	11
209#define	NC_NLEN_MASK	((1 << NC_NLEN_BITS) - 1)
210#define	NC_NLEN(ncp)	((ncp)->nc_key & NC_NLEN_MASK)
211
212/*
213 * Namecache entry.
214 *
215 * This structure describes the elements in the cache of recent names looked
216 * up by namei.  It's carefully sized to take up 128 bytes on _LP64 and 64
217 * bytes on 32-bit machines, to make good use of space and the CPU caches.
218 *
219 * Items used during RB tree lookup (nc_tree, nc_key) are clustered at the
220 * start of the structure to minimise cache misses during lookup.
221 *
222 * Field markings and their corresponding locks:
223 *
224 * -  stable throughout the lifetime of the namecache entry
225 * d  protected by nc_dvp->vi_nc_lock
226 * v  protected by nc_vp->vi_nc_listlock
227 * l  protected by cache_lru_lock
228 */
229struct namecache {
230	struct	rb_node nc_tree;	/* d  red-black tree, must be first */
231	uintptr_t nc_key;		/* -  hashed key value */
232	TAILQ_ENTRY(namecache) nc_list;	/* v  nc_vp's list of cache entries */
233	TAILQ_ENTRY(namecache) nc_lru;	/* l  pseudo-lru chain */
234	struct	vnode *nc_dvp;		/* -  vnode of parent of name */
235	struct	vnode *nc_vp;		/* -  vnode the name refers to */
236	u_char	nc_lrulist;		/* l  LRU list entry is on */
237	u_char	nc_whiteout;		/* -  whiteout indicator */
238#ifdef _LP64
239	char	nc_name[46];		/* -  segment name */
240#else
241	char	nc_name[22];		/* -  segment name */
242#endif
243};
244#endif /* __NAMECACHE_PRIVATE */
245
246#ifdef _KERNEL
247#include <sys/kmem.h>
248
249struct mount;
250struct cpu_info;
251
252#define	PNBUF_GET()	((char *)kmem_alloc(MAXPATHLEN, KM_SLEEP))
253#define	PNBUF_PUT(pnb)	kmem_free((pnb), MAXPATHLEN)
254
255/*
256 * Typesafe flags for namei_simple/nameiat_simple.
257 *
258 * This encoding is not optimal but serves the important purpose of
259 * not being type-compatible with the regular namei flags.
260 */
261struct namei_simple_flags_type; /* Opaque. */
262typedef const struct namei_simple_flags_type *namei_simple_flags_t; /* Gross. */
263extern const namei_simple_flags_t
264	NSM_NOFOLLOW_NOEMULROOT,
265	NSM_NOFOLLOW_TRYEMULROOT,
266	NSM_FOLLOW_NOEMULROOT,
267	NSM_FOLLOW_TRYEMULROOT;
268
269/*
270 * namei(at)?_simple_* - the simple cases of namei, with no struct
271 *                       nameidata involved.
272 *
273 * namei_simple_kernel takes a kernel-space path as the first argument.
274 * namei_simple_user takes a user-space path as the first argument.
275 * The nameiat_simple* variants handle relative path using the given
276 * directory vnode instead of current directory.
277 *
278 * A namei call can be converted to namei_simple_* if:
279 *    - the second arg to NDINIT is LOOKUP;
280 *    - it does not need the parent vnode, nd.ni_dvp;
281 *    - the only flags it uses are (NO)FOLLOW and TRYEMULROOT;
282 *    - it does not do anything else gross with the contents of nd.
283 */
284int namei_simple_kernel(const char *, namei_simple_flags_t, struct vnode **);
285int namei_simple_user(const char *, namei_simple_flags_t, struct vnode **);
286int nameiat_simple(struct vnode *, struct pathbuf *, namei_simple_flags_t,
287    struct vnode **);
288int nameiat_simple_kernel(struct vnode *, const char *, namei_simple_flags_t,
289    struct vnode **);
290int nameiat_simple_user(struct vnode *, const char *, namei_simple_flags_t,
291    struct vnode **);
292
293int	namei(struct nameidata *);
294uint32_t namei_hash(const char *, const char **);
295int	lookup_for_nfsd(struct nameidata *, struct vnode *, int neverfollow);
296int	lookup_for_nfsd_index(struct nameidata *, struct vnode *);
297int	relookup(struct vnode *, struct vnode **, struct componentname *, int);
298void	cache_purge1(struct vnode *, const char *, size_t, int);
299#define	PURGE_PARENTS	1
300#define	PURGE_CHILDREN	2
301#define	cache_purge(vp)	cache_purge1((vp),NULL,0,PURGE_PARENTS|PURGE_CHILDREN)
302bool	cache_lookup(struct vnode *, const char *, size_t, uint32_t, uint32_t,
303			int *, struct vnode **);
304bool	cache_lookup_raw(struct vnode *, const char *, size_t, uint32_t,
305			int *, struct vnode **);
306bool	cache_lookup_linked(struct vnode *, const char *, size_t,
307			    struct vnode **, krwlock_t **, kauth_cred_t);
308int	cache_revlookup(struct vnode *, struct vnode **, char **, char *,
309			bool, accmode_t);
310int	cache_diraccess(struct vnode *, int);
311void	cache_enter(struct vnode *, struct vnode *,
312			const char *, size_t, uint32_t);
313void	cache_enter_id(struct vnode *, mode_t, uid_t, gid_t, bool);
314bool	cache_have_id(struct vnode *);
315void	cache_vnode_init(struct vnode * );
316void	cache_vnode_fini(struct vnode * );
317void	cache_cpu_init(struct cpu_info *);
318void	cache_enter_mount(struct vnode *, struct vnode *);
319bool	cache_cross_mount(struct vnode **, krwlock_t **);
320bool	cache_lookup_mount(struct vnode *, struct vnode **);
321
322void	nchinit(void);
323void	namecache_count_pass2(void);
324void	namecache_count_2passes(void);
325void	cache_purgevfs(struct mount *);
326void	namecache_print(struct vnode *, void (*)(const char *, ...)
327    __printflike(1, 2));
328
329#endif
330
331/*
332 * Stats on usefulness of namei caches.  A couple of structures are
333 * used for counting, with members having the same names but different
334 * types.  Containerize member names with the preprocessor to avoid
335 * cut-'n'-paste.
336 */
337#define	_NAMEI_CACHE_STATS(type) {					\
338	type	ncs_goodhits;	/* hits that we can really use */	\
339	type	ncs_neghits;	/* negative hits that we can use */	\
340	type	ncs_badhits;	/* hits we must drop */			\
341	type	ncs_falsehits;	/* hits with id mismatch */		\
342	type	ncs_miss;	/* misses */				\
343	type	ncs_long;	/* long names that ignore cache */	\
344	type	ncs_pass2;	/* names found with passes == 2 */	\
345	type	ncs_2passes;	/* number of times we attempt it */	\
346	type	ncs_revhits;	/* reverse-cache hits */		\
347	type	ncs_revmiss;	/* reverse-cache misses */		\
348	type	ncs_denied;	/* access denied */			\
349}
350
351/*
352 * Sysctl deals with a uint64_t version of the stats and summary
353 * totals are kept that way.
354 */
355struct	nchstats _NAMEI_CACHE_STATS(uint64_t);
356
357/* #endif !_SYS_NAMEI_H_ (generated by gennameih.awk) */
358