1 /* $NetBSD: namei.h,v 1.89 2014/06/03 21:16:37 joerg Exp $ */ 2 3 4 /* 5 * WARNING: GENERATED FILE. DO NOT EDIT 6 * (edit namei.src and run make namei in src/sys/sys) 7 * by: NetBSD: gennameih.awk,v 1.5 2009/12/23 14:17:19 pooka Exp 8 * from: NetBSD: namei.src,v 1.33 2014/06/03 21:16:15 joerg Exp 9 */ 10 11 /* 12 * Copyright (c) 1985, 1989, 1991, 1993 13 * The Regents of the University of California. All rights reserved. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)namei.h 8.5 (Berkeley) 8/20/94 40 */ 41 42 #ifndef _SYS_NAMEI_H_ 43 #define _SYS_NAMEI_H_ 44 45 #include <sys/queue.h> 46 #include <sys/mutex.h> 47 48 #ifdef _KERNEL 49 #include <sys/kauth.h> 50 51 /* 52 * Abstraction for a single pathname. 53 * 54 * This contains both the pathname string and (eventually) all 55 * metadata that determines how the path is to be interpreted. 56 * It is an opaque structure; the implementation is in vfs_lookup.c. 57 * 58 * To call namei, first set up a pathbuf with pathbuf_create or 59 * pathbuf_copyin, then do NDINIT(), then call namei, then AFTER THE 60 * STRUCT NAMEIDATA IS DEAD, call pathbuf_destroy. Don't destroy the 61 * pathbuf before you've finished using the nameidata, or mysterious 62 * bad things may happen. 63 * 64 * pathbuf_assimilate is like pathbuf_create but assumes ownership of 65 * the string buffer passed in, which MUST BE of size PATH_MAX and 66 * have been allocated with PNBUF_GET(). This should only be used when 67 * absolutely necessary; e.g. nfsd uses it for loading paths from 68 * mbufs. 69 */ 70 struct pathbuf; 71 72 struct pathbuf *pathbuf_create(const char *path); 73 struct pathbuf *pathbuf_assimilate(char *path); 74 int pathbuf_copyin(const char *userpath, struct pathbuf **ret); 75 void pathbuf_destroy(struct pathbuf *); 76 77 /* get a copy of the (current) path string */ 78 void pathbuf_copystring(const struct pathbuf *, char *buf, size_t maxlen); 79 80 /* hold a reference copy of the original path string */ 81 const char *pathbuf_stringcopy_get(struct pathbuf *); 82 void pathbuf_stringcopy_put(struct pathbuf *, const char *); 83 84 // XXX remove this 85 int pathbuf_maybe_copyin(const char *userpath, enum uio_seg seg, struct pathbuf **ret); 86 87 /* 88 * Encapsulation of namei parameters. 89 */ 90 struct nameidata { 91 /* 92 * Arguments to namei/lookup. 93 */ 94 struct vnode *ni_atdir; /* startup dir, cwd if null */ 95 struct pathbuf *ni_pathbuf; /* pathname container */ 96 char *ni_pnbuf; /* extra pathname buffer ref (XXX) */ 97 /* 98 * Arguments to lookup. 99 */ 100 struct vnode *ni_rootdir; /* logical root directory */ 101 struct vnode *ni_erootdir; /* emulation root directory */ 102 /* 103 * Results: returned from/manipulated by lookup 104 */ 105 struct vnode *ni_vp; /* vnode of result */ 106 struct vnode *ni_dvp; /* vnode of intermediate directory */ 107 /* 108 * Shared between namei and lookup/commit routines. 109 */ 110 size_t ni_pathlen; /* remaining chars in path */ 111 const char *ni_next; /* next location in pathname */ 112 unsigned int ni_loopcnt; /* count of symlinks encountered */ 113 /* 114 * Lookup parameters: this structure describes the subset of 115 * information from the nameidata structure that is passed 116 * through the VOP interface. 117 */ 118 struct componentname { 119 /* 120 * Arguments to lookup. 121 */ 122 uint32_t cn_nameiop; /* namei operation */ 123 uint32_t cn_flags; /* flags to namei */ 124 kauth_cred_t cn_cred; /* credentials */ 125 /* 126 * Shared between lookup and commit routines. 127 */ 128 const char *cn_nameptr; /* pointer to looked up name */ 129 size_t cn_namelen; /* length of looked up comp */ 130 size_t cn_consume; /* chars to consume in lookup */ 131 } ni_cnd; 132 }; 133 134 /* 135 * namei operations 136 */ 137 #define LOOKUP 0 /* perform name lookup only */ 138 #define CREATE 1 /* setup for file creation */ 139 #define DELETE 2 /* setup for file deletion */ 140 #define RENAME 3 /* setup for file renaming */ 141 #define OPMASK 3 /* mask for operation */ 142 /* 143 * namei operational modifier flags, stored in ni_cnd.cn_flags 144 */ 145 #define LOCKLEAF 0x00000004 /* lock inode on return */ 146 #define LOCKPARENT 0x00000008 /* want parent vnode returned locked */ 147 #define TRYEMULROOT 0x00000010 /* try relative to emulation root 148 first */ 149 #define NOCACHE 0x00000020 /* name must not be left in cache */ 150 #define FOLLOW 0x00000040 /* follow symbolic links */ 151 #define NOFOLLOW 0x00000000 /* do not follow symbolic links 152 (pseudo) */ 153 #define EMULROOTSET 0x00000080 /* emulation root already 154 in ni_erootdir */ 155 #define NOCHROOT 0x01000000 /* no chroot on abs path lookups */ 156 #define MODMASK 0x010000fc /* mask of operational modifiers */ 157 /* 158 * Namei parameter descriptors. 159 */ 160 #define NOCROSSMOUNT 0x0000100 /* do not cross mount points */ 161 #define RDONLY 0x0000200 /* lookup with read-only semantics */ 162 #define ISDOTDOT 0x0002000 /* current component name is .. */ 163 #define MAKEENTRY 0x0004000 /* entry is to be added to name cache */ 164 #define ISLASTCN 0x0008000 /* this is last component of pathname */ 165 #define ISWHITEOUT 0x0020000 /* found whiteout */ 166 #define DOWHITEOUT 0x0040000 /* do whiteouts */ 167 #define REQUIREDIR 0x0080000 /* must be a directory */ 168 #define CREATEDIR 0x0200000 /* trailing slashes are ok */ 169 #define INRENAME 0x0400000 /* operation is a part of ``rename'' */ 170 #define INRELOOKUP 0x0800000 /* set while inside relookup() */ 171 #define PARAMASK 0x0eee300 /* 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 /* 194 * This structure describes the elements in the cache of recent 195 * names looked up by namei. NCHNAMLEN is sized to make structure 196 * size a power of two to optimize allocations. Minimum reasonable 197 * size is 15. 198 */ 199 200 #define NCHNAMLEN 31 /* maximum name segment length we bother with */ 201 202 /* 203 * Namecache entry. This structure is arranged so that frequently 204 * accessed and mostly read-only data is toward the front, with 205 * infrequently accessed data and the lock towards the rear. The 206 * lock is then more likely to be in a seperate cache line. 207 */ 208 struct namecache { 209 LIST_ENTRY(namecache) nc_hash; /* hash chain */ 210 LIST_ENTRY(namecache) nc_vhash; /* directory hash chain */ 211 struct vnode *nc_dvp; /* vnode of parent of name */ 212 struct vnode *nc_vp; /* vnode the name refers to */ 213 int nc_flags; /* copy of componentname's ISWHITEOUT */ 214 char nc_nlen; /* length of name */ 215 char nc_name[NCHNAMLEN]; /* segment name */ 216 void *nc_gcqueue; /* queue for garbage collection */ 217 TAILQ_ENTRY(namecache) nc_lru; /* psuedo-lru chain */ 218 LIST_ENTRY(namecache) nc_dvlist; 219 LIST_ENTRY(namecache) nc_vlist; 220 kmutex_t nc_lock; /* lock on this entry */ 221 int nc_hittime; /* last time scored a hit */ 222 }; 223 224 #ifdef _KERNEL 225 #include <sys/pool.h> 226 227 struct mount; 228 struct cpu_info; 229 230 extern pool_cache_t pnbuf_cache; /* pathname buffer cache */ 231 232 #define PNBUF_GET() pool_cache_get(pnbuf_cache, PR_WAITOK) 233 #define PNBUF_PUT(pnb) pool_cache_put(pnbuf_cache, (pnb)) 234 235 /* 236 * Typesafe flags for namei_simple/nameiat_simple. 237 * 238 * This encoding is not optimal but serves the important purpose of 239 * not being type-compatible with the regular namei flags. 240 */ 241 struct namei_simple_flags_type; /* Opaque. */ 242 typedef const struct namei_simple_flags_type *namei_simple_flags_t; /* Gross. */ 243 extern const namei_simple_flags_t 244 NSM_NOFOLLOW_NOEMULROOT, 245 NSM_NOFOLLOW_TRYEMULROOT, 246 NSM_FOLLOW_NOEMULROOT, 247 NSM_FOLLOW_TRYEMULROOT; 248 249 /* 250 * namei(at)?_simple_* - the simple cases of namei, with no struct 251 * nameidata involved. 252 * 253 * namei_simple_kernel takes a kernel-space path as the first argument. 254 * namei_simple_user takes a user-space path as the first argument. 255 * The nameiat_simple_* variants handle relative path using the given 256 * directory vnode instead of current directory. 257 * 258 * A namei call can be converted to namei_simple_* if: 259 * - the second arg to NDINIT is LOOKUP; 260 * - it does not need the parent vnode, nd.ni_dvp; 261 * - the only flags it uses are (NO)FOLLOW and TRYEMULROOT; 262 * - it does not do anything else gross with the contents of nd. 263 */ 264 int namei_simple_kernel(const char *, namei_simple_flags_t, struct vnode **); 265 int namei_simple_user(const char *, namei_simple_flags_t, struct vnode **); 266 int nameiat_simple_kernel(struct vnode *, const char *, namei_simple_flags_t, 267 struct vnode **); 268 int nameiat_simple_user(struct vnode *, const char *, namei_simple_flags_t, 269 struct vnode **); 270 271 int namei(struct nameidata *); 272 uint32_t namei_hash(const char *, const char **); 273 int lookup_for_nfsd(struct nameidata *, struct vnode *, int neverfollow); 274 int lookup_for_nfsd_index(struct nameidata *, struct vnode *); 275 int relookup(struct vnode *, struct vnode **, struct componentname *, int); 276 void cache_purge1(struct vnode *, const char *, size_t, int); 277 #define PURGE_PARENTS 1 278 #define PURGE_CHILDREN 2 279 #define cache_purge(vp) cache_purge1((vp),NULL,0,PURGE_PARENTS|PURGE_CHILDREN) 280 int cache_lookup(struct vnode *, const char *, size_t, uint32_t, uint32_t, 281 int *, struct vnode **); 282 int cache_lookup_raw(struct vnode *, const char *, size_t, uint32_t, 283 int *, struct vnode **); 284 int cache_revlookup(struct vnode *, struct vnode **, char **, char *); 285 void cache_enter(struct vnode *, struct vnode *, 286 const char *, size_t, uint32_t); 287 void nchinit(void); 288 void nchreinit(void); 289 void namecache_count_pass2(void); 290 void namecache_count_2passes(void); 291 void cache_cpu_init(struct cpu_info *); 292 void cache_purgevfs(struct mount *); 293 void namecache_print(struct vnode *, void (*)(const char *, ...) 294 __printflike(1, 2)); 295 296 #endif 297 298 /* 299 * Stats on usefulness of namei caches. 300 * XXX: should be 64-bit counters. 301 */ 302 struct nchstats { 303 long ncs_goodhits; /* hits that we can really use */ 304 long ncs_neghits; /* negative hits that we can use */ 305 long ncs_badhits; /* hits we must drop */ 306 long ncs_falsehits; /* hits with id mismatch */ 307 long ncs_miss; /* misses */ 308 long ncs_long; /* long names that ignore cache */ 309 long ncs_pass2; /* names found with passes == 2 */ 310 long ncs_2passes; /* number of times we attempt it */ 311 long ncs_revhits; /* reverse-cache hits */ 312 long ncs_revmiss; /* reverse-cache misses */ 313 }; 314 315 struct nchstats_sysctl { 316 uint64_t ncs_goodhits; /* hits that we can really use */ 317 uint64_t ncs_neghits; /* negative hits that we can use */ 318 uint64_t ncs_badhits; /* hits we must drop */ 319 uint64_t ncs_falsehits; /* hits with id mismatch */ 320 uint64_t ncs_miss; /* misses */ 321 uint64_t ncs_long; /* long names that ignore cache */ 322 uint64_t ncs_pass2; /* names found with passes == 2 */ 323 uint64_t ncs_2passes; /* number of times we attempt it */ 324 uint64_t ncs_revhits; /* reverse-cache hits */ 325 uint64_t ncs_revmiss; /* reverse-cache misses */ 326 }; 327 328 #ifdef _KERNEL 329 extern struct nchstats nchstats; 330 #endif 331 /* #endif !_SYS_NAMEI_H_ (generated by gennameih.awk) */ 332 333 /* Definitions match above, but with NAMEI_ prefix */ 334 #define NAMEI_LOOKUP 0 335 #define NAMEI_CREATE 1 336 #define NAMEI_DELETE 2 337 #define NAMEI_RENAME 3 338 #define NAMEI_OPMASK 3 339 #define NAMEI_LOCKLEAF 0x00000004 340 #define NAMEI_LOCKPARENT 0x00000008 341 #define NAMEI_TRYEMULROOT 0x00000010 342 #define NAMEI_NOCACHE 0x00000020 343 #define NAMEI_FOLLOW 0x00000040 344 #define NAMEI_NOFOLLOW 0x00000000 345 #define NAMEI_EMULROOTSET 0x00000080 346 #define NAMEI_NOCHROOT 0x01000000 347 #define NAMEI_MODMASK 0x010000fc 348 #define NAMEI_NOCROSSMOUNT 0x0000100 349 #define NAMEI_RDONLY 0x0000200 350 #define NAMEI_ISDOTDOT 0x0002000 351 #define NAMEI_MAKEENTRY 0x0004000 352 #define NAMEI_ISLASTCN 0x0008000 353 #define NAMEI_ISWHITEOUT 0x0020000 354 #define NAMEI_DOWHITEOUT 0x0040000 355 #define NAMEI_REQUIREDIR 0x0080000 356 #define NAMEI_CREATEDIR 0x0200000 357 #define NAMEI_INRENAME 0x0400000 358 #define NAMEI_INRELOOKUP 0x0800000 359 #define NAMEI_PARAMASK 0x0eee300 360 361 #endif /* !_SYS_NAMEI_H_ */ 362