Annotation of src/sys/sys/namei.src, Revision 1.47.2.3
1.47.2.3! ad 1: /* $NetBSD: namei.src,v 1.47.2.2 2020/01/13 08:51:06 ad Exp $ */
1.1 pooka 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>
1.7 ad 38: #include <sys/mutex.h>
1.8 ad 39:
40: #ifdef _KERNEL
1.7 ad 41: #include <sys/kauth.h>
42:
1.1 pooka 43: /*
1.15 dholland 44: * Abstraction for a single pathname.
45: *
46: * This contains both the pathname string and (eventually) all
47: * metadata that determines how the path is to be interpreted.
48: * It is an opaque structure; the implementation is in vfs_lookup.c.
49: *
50: * To call namei, first set up a pathbuf with pathbuf_create or
51: * pathbuf_copyin, then do NDINIT(), then call namei, then AFTER THE
52: * STRUCT NAMEIDATA IS DEAD, call pathbuf_destroy. Don't destroy the
53: * pathbuf before you've finished using the nameidata, or mysterious
54: * bad things may happen.
1.16 dholland 55: *
56: * pathbuf_assimilate is like pathbuf_create but assumes ownership of
57: * the string buffer passed in, which MUST BE of size PATH_MAX and
58: * have been allocated with PNBUF_GET(). This should only be used when
59: * absolutely necessary; e.g. nfsd uses it for loading paths from
60: * mbufs.
1.15 dholland 61: */
62: struct pathbuf;
63:
64: struct pathbuf *pathbuf_create(const char *path);
1.16 dholland 65: struct pathbuf *pathbuf_assimilate(char *path);
1.15 dholland 66: int pathbuf_copyin(const char *userpath, struct pathbuf **ret);
67: void pathbuf_destroy(struct pathbuf *);
68:
69: /* get a copy of the (current) path string */
70: void pathbuf_copystring(const struct pathbuf *, char *buf, size_t maxlen);
71:
72: /* hold a reference copy of the original path string */
73: const char *pathbuf_stringcopy_get(struct pathbuf *);
74: void pathbuf_stringcopy_put(struct pathbuf *, const char *);
75:
76: // XXX remove this
77: int pathbuf_maybe_copyin(const char *userpath, enum uio_seg seg, struct pathbuf **ret);
78:
79: /*
1.35 dennis 80: * Lookup parameters: this structure describes the subset of
81: * information from the nameidata structure that is passed
82: * through the VOP interface.
83: */
84: struct componentname {
85: /*
86: * Arguments to lookup.
87: */
88: uint32_t cn_nameiop; /* namei operation */
89: uint32_t cn_flags; /* flags to namei */
90: kauth_cred_t cn_cred; /* credentials */
91: /*
92: * Shared between lookup and commit routines.
93: */
94: const char *cn_nameptr; /* pointer to looked up name */
95: size_t cn_namelen; /* length of looked up comp */
96: size_t cn_consume; /* chars to consume in lookup */
97: };
98:
99: /*
1.1 pooka 100: * Encapsulation of namei parameters.
101: */
102: struct nameidata {
103: /*
104: * Arguments to namei/lookup.
105: */
1.30 dholland 106: struct vnode *ni_atdir; /* startup dir, cwd if null */
1.15 dholland 107: struct pathbuf *ni_pathbuf; /* pathname container */
1.16 dholland 108: char *ni_pnbuf; /* extra pathname buffer ref (XXX) */
1.1 pooka 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: */
1.3 pooka 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 */
1.1 pooka 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: */
1.35 dennis 130: struct componentname ni_cnd;
1.1 pooka 131: };
132:
133: /*
134: * namei operations
135: */
136: NAMEIFL LOOKUP 0 /* perform name lookup only */
137: NAMEIFL CREATE 1 /* setup for file creation */
138: NAMEIFL DELETE 2 /* setup for file deletion */
139: NAMEIFL RENAME 3 /* setup for file renaming */
140: NAMEIFL OPMASK 3 /* mask for operation */
141: /*
142: * namei operational modifier flags, stored in ni_cnd.cn_flags
143: */
1.10 enami 144: NAMEIFL LOCKLEAF 0x00000004 /* lock inode on return */
145: NAMEIFL LOCKPARENT 0x00000008 /* want parent vnode returned locked */
146: NAMEIFL TRYEMULROOT 0x00000010 /* try relative to emulation root
147: first */
148: NAMEIFL NOCACHE 0x00000020 /* name must not be left in cache */
149: NAMEIFL FOLLOW 0x00000040 /* follow symbolic links */
150: NAMEIFL NOFOLLOW 0x00000000 /* do not follow symbolic links
151: (pseudo) */
152: NAMEIFL EMULROOTSET 0x00000080 /* emulation root already
153: in ni_erootdir */
154: NAMEIFL NOCHROOT 0x01000000 /* no chroot on abs path lookups */
1.27 dholland 155: NAMEIFL MODMASK 0x010000fc /* mask of operational modifiers */
1.1 pooka 156: /*
157: * Namei parameter descriptors.
158: */
159: NAMEIFL NOCROSSMOUNT 0x0000100 /* do not cross mount points */
160: NAMEIFL RDONLY 0x0000200 /* lookup with read-only semantics */
161: NAMEIFL ISDOTDOT 0x0002000 /* current component name is .. */
162: NAMEIFL MAKEENTRY 0x0004000 /* entry is to be added to name cache */
163: NAMEIFL ISLASTCN 0x0008000 /* this is last component of pathname */
164: NAMEIFL ISWHITEOUT 0x0020000 /* found whiteout */
165: NAMEIFL DOWHITEOUT 0x0040000 /* do whiteouts */
166: NAMEIFL REQUIREDIR 0x0080000 /* must be a directory */
167: NAMEIFL CREATEDIR 0x0200000 /* trailing slashes are ok */
1.37 riastrad 168: NAMEIFL PARAMASK 0x02ee300 /* mask of parameter descriptors */
1.7 ad 169:
1.1 pooka 170: /*
1.26 dholland 171: * Initialization of a nameidata structure.
1.1 pooka 172: */
1.15 dholland 173: #define NDINIT(ndp, op, flags, pathbuf) { \
1.1 pooka 174: (ndp)->ni_cnd.cn_nameiop = op; \
175: (ndp)->ni_cnd.cn_flags = flags; \
1.30 dholland 176: (ndp)->ni_atdir = NULL; \
1.15 dholland 177: (ndp)->ni_pathbuf = pathbuf; \
1.6 pooka 178: (ndp)->ni_cnd.cn_cred = kauth_cred_get(); \
1.1 pooka 179: }
1.26 dholland 180:
181: /*
182: * Use this to set the start directory for openat()-type operations.
183: */
184: #define NDAT(ndp, dir) { \
1.30 dholland 185: (ndp)->ni_atdir = (dir); \
1.26 dholland 186: }
187:
1.1 pooka 188: #endif
189:
1.43 christos 190: #ifdef __NAMECACHE_PRIVATE
1.47.2.1 ad 191: #include <sys/rbtree.h>
192:
1.1 pooka 193: /*
1.43 christos 194: * For simplicity (and economy of storage), names longer than
195: * a maximum length of NCHNAMLEN are stored in non-pooled storage.
1.1 pooka 196: */
1.47.2.1 ad 197: #define NCHNAMLEN sizeof(((struct namecache *)NULL)->nc_name)
1.1 pooka 198:
1.7 ad 199: /*
1.43 christos 200: * Namecache entry.
1.39 riastrad 201: *
1.47.2.1 ad 202: * This structure describes the elements in the cache of recent names looked
203: * up by namei. It's carefully sized to take up 128 bytes on _LP64, to make
204: * good use of space and the CPU caches.
205: *
206: * Field markings and their corresponding locks:
207: *
208: * - stable throught the lifetime of the namecache entry
1.47.2.3! ad 209: * d protected by nc_dvp->vi_ncdlock
! 210: * v protected by nc_dvp->vi_ncvlock
! 211: * l protected by cache_lru_lock
1.47.2.2 ad 212: * u accesses are unlocked, no serialization applied
1.39 riastrad 213: */
214: struct namecache {
1.47.2.1 ad 215: struct rb_node nc_node; /* d red-black tree node */
216: TAILQ_ENTRY(namecache) nc_lru; /* l pseudo-lru chain */
1.47.2.3! ad 217: TAILQ_ENTRY(namecache) nc_vlist;/* v vp's list of cache entries */
1.47.2.1 ad 218: struct vnode *nc_dvp; /* - vnode of parent of name */
219: struct vnode *nc_vp; /* - vnode the name refers to */
1.47.2.3! ad 220: int nc_lrulist; /* l which LRU list its on */
1.47.2.1 ad 221: u_short nc_nlen; /* - length of name */
222: bool nc_whiteout; /* - true if a whiteout */
223: char nc_name[49]; /* - segment name */
1.1 pooka 224: };
1.43 christos 225: #endif
1.1 pooka 226:
227: #ifdef _KERNEL
228: #include <sys/pool.h>
229:
230: struct mount;
1.7 ad 231: struct cpu_info;
1.1 pooka 232:
1.4 pooka 233: extern pool_cache_t pnbuf_cache; /* pathname buffer cache */
1.1 pooka 234:
1.35 dennis 235: #define PNBUF_GET() ((char *)pool_cache_get(pnbuf_cache, PR_WAITOK))
236: #define PNBUF_PUT(pnb) pool_cache_put(pnbuf_cache, (void *)(pnb))
1.1 pooka 237:
1.11 dholland 238: /*
1.31 dholland 239: * Typesafe flags for namei_simple/nameiat_simple.
1.11 dholland 240: *
241: * This encoding is not optimal but serves the important purpose of
242: * not being type-compatible with the regular namei flags.
243: */
244: struct namei_simple_flags_type; /* Opaque. */
245: typedef const struct namei_simple_flags_type *namei_simple_flags_t; /* Gross. */
246: extern const namei_simple_flags_t
247: NSM_NOFOLLOW_NOEMULROOT,
248: NSM_NOFOLLOW_TRYEMULROOT,
249: NSM_FOLLOW_NOEMULROOT,
250: NSM_FOLLOW_TRYEMULROOT;
251:
252: /*
1.31 dholland 253: * namei(at)?_simple_* - the simple cases of namei, with no struct
254: * nameidata involved.
1.11 dholland 255: *
256: * namei_simple_kernel takes a kernel-space path as the first argument.
257: * namei_simple_user takes a user-space path as the first argument.
1.31 dholland 258: * The nameiat_simple_* variants handle relative path using the given
259: * directory vnode instead of current directory.
1.11 dholland 260: *
261: * A namei call can be converted to namei_simple_* if:
262: * - the second arg to NDINIT is LOOKUP;
263: * - it does not need the parent vnode, nd.ni_dvp;
264: * - the only flags it uses are (NO)FOLLOW and TRYEMULROOT;
265: * - it does not do anything else gross with the contents of nd.
266: */
267: int namei_simple_kernel(const char *, namei_simple_flags_t, struct vnode **);
268: int namei_simple_user(const char *, namei_simple_flags_t, struct vnode **);
1.31 dholland 269: int nameiat_simple_kernel(struct vnode *, const char *, namei_simple_flags_t,
270: struct vnode **);
271: int nameiat_simple_user(struct vnode *, const char *, namei_simple_flags_t,
272: struct vnode **);
1.11 dholland 273:
1.1 pooka 274: int namei(struct nameidata *);
275: uint32_t namei_hash(const char *, const char **);
1.13 dholland 276: int lookup_for_nfsd(struct nameidata *, struct vnode *, int neverfollow);
1.18 dholland 277: int lookup_for_nfsd_index(struct nameidata *, struct vnode *);
1.20 dholland 278: int relookup(struct vnode *, struct vnode **, struct componentname *, int);
1.29 dholland 279: void cache_purge1(struct vnode *, const char *, size_t, int);
1.1 pooka 280: #define PURGE_PARENTS 1
281: #define PURGE_CHILDREN 2
1.29 dholland 282: #define cache_purge(vp) cache_purge1((vp),NULL,0,PURGE_PARENTS|PURGE_CHILDREN)
1.38 riastrad 283: bool cache_lookup(struct vnode *, const char *, size_t, uint32_t, uint32_t,
1.28 dholland 284: int *, struct vnode **);
1.38 riastrad 285: bool cache_lookup_raw(struct vnode *, const char *, size_t, uint32_t,
1.28 dholland 286: int *, struct vnode **);
1.1 pooka 287: int cache_revlookup(struct vnode *, struct vnode **, char **, char *);
1.29 dholland 288: void cache_enter(struct vnode *, struct vnode *,
289: const char *, size_t, uint32_t);
1.47.2.1 ad 290: void cache_vnode_init(struct vnode * );
291: void cache_vnode_fini(struct vnode * );
292: void cache_cpu_init(struct cpu_info *);
293:
1.1 pooka 294: void nchinit(void);
1.32 joerg 295: void namecache_count_pass2(void);
296: void namecache_count_2passes(void);
1.1 pooka 297: void cache_purgevfs(struct mount *);
1.25 dholland 298: void namecache_print(struct vnode *, void (*)(const char *, ...)
299: __printflike(1, 2));
1.1 pooka 300:
301: #endif
302:
303: /*
1.34 dennis 304: * Stats on usefulness of namei caches. A couple of structures are
305: * used for counting, with members having the same names but different
306: * types. Containerize member names with the preprocessor to avoid
307: * cut-'n'-paste. A (U) in the comment documents values that are
308: * incremented unlocked; we may treat these specially.
309: */
310: #define _NAMEI_CACHE_STATS(type) { \
311: type ncs_goodhits; /* hits that we can really use (U) */ \
312: type ncs_neghits; /* negative hits that we can use */ \
313: type ncs_badhits; /* hits we must drop */ \
314: type ncs_falsehits; /* hits with id mismatch (U) */ \
315: type ncs_miss; /* misses */ \
316: type ncs_long; /* long names that ignore cache */ \
317: type ncs_pass2; /* names found with passes == 2 (U) */ \
318: type ncs_2passes; /* number of times we attempt it (U) */ \
319: type ncs_revhits; /* reverse-cache hits */ \
320: type ncs_revmiss; /* reverse-cache misses */ \
321: }
322:
323: /*
324: * Sysctl deals with a uint64_t version of the stats and summary
325: * totals are kept that way.
1.1 pooka 326: */
1.34 dennis 327: struct nchstats _NAMEI_CACHE_STATS(uint64_t);
1.1 pooka 328:
329: /* #endif !_SYS_NAMEI_H_ (generated by gennameih.awk) */
CVSweb <webmaster@jp.NetBSD.org>