namei.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* $OpenBSD: namei.h,v 1.28 2013/03/27 01:56:50 tedu Exp $ */
  2. /* $NetBSD: namei.h,v 1.11 1996/02/09 18:25:20 christos Exp $ */
  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.4 (Berkeley) 8/20/94
  32. */
  33. #ifndef _SYS_NAMEI_H_
  34. #define _SYS_NAMEI_H_
  35. #include <sys/queue.h>
  36. #include <sys/tree.h>
  37. #include <sys/uio.h>
  38. struct namecache;
  39. struct namecache_rb_cache;
  40. RB_PROTOTYPE(namecache_rb_cache, namecache, n_rbcache, namecache_compare);
  41. /*
  42. * Encapsulation of namei parameters.
  43. */
  44. struct nameidata {
  45. /*
  46. * Arguments to namei/lookup.
  47. */
  48. const char *ni_dirp; /* pathname pointer */
  49. int ni_dirfd; /* dirfd from *at() functions */
  50. enum uio_seg ni_segflg; /* location of pathname */
  51. /* u_long ni_nameiop; namei operation */
  52. /* u_long ni_flags; flags to namei */
  53. /* struct proc *ni_proc; process requesting lookup */
  54. /*
  55. * Arguments to lookup.
  56. */
  57. /* struct ucred *ni_cred; credentials */
  58. struct vnode *ni_startdir; /* starting directory */
  59. struct vnode *ni_rootdir; /* logical root directory */
  60. /*
  61. * Results: returned from/manipulated by lookup
  62. */
  63. struct vnode *ni_vp; /* vnode of result */
  64. struct vnode *ni_dvp; /* vnode of intermediate directory */
  65. /*
  66. * Shared between namei and lookup/commit routines.
  67. */
  68. size_t ni_pathlen; /* remaining chars in path */
  69. char *ni_next; /* next location in pathname */
  70. u_long ni_loopcnt; /* count of symlinks encountered */
  71. /*
  72. * Lookup parameters: this structure describes the subset of
  73. * information from the nameidata structure that is passed
  74. * through the VOP interface.
  75. */
  76. struct componentname {
  77. /*
  78. * Arguments to lookup.
  79. */
  80. u_long cn_nameiop; /* namei operation */
  81. u_long cn_flags; /* flags to namei */
  82. struct proc *cn_proc; /* process requesting lookup */
  83. struct ucred *cn_cred; /* credentials */
  84. /*
  85. * Shared between lookup and commit routines.
  86. */
  87. char *cn_pnbuf; /* pathname buffer */
  88. char *cn_nameptr; /* pointer to looked up name */
  89. long cn_namelen; /* length of looked up component */
  90. long cn_consume; /* chars to consume in lookup() */
  91. } ni_cnd;
  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. * Initialization of an nameidata structure.
  140. */
  141. #define NDINITAT(ndp, op, flags, segflg, dirfd, namep, p) { \
  142. (ndp)->ni_cnd.cn_nameiop = op; \
  143. (ndp)->ni_cnd.cn_flags = flags; \
  144. (ndp)->ni_segflg = segflg; \
  145. (ndp)->ni_dirfd = dirfd; \
  146. (ndp)->ni_dirp = namep; \
  147. (ndp)->ni_cnd.cn_proc = p; \
  148. }
  149. #define NDINIT(ndp, op, flags, segflp, namep, p) \
  150. NDINITAT(ndp, op, flags, segflp, AT_FDCWD, namep, p)
  151. /* Defined for users of NDINIT(). */
  152. #define AT_FDCWD -100
  153. #endif
  154. /*
  155. * This structure describes the elements in the cache of recent
  156. * names looked up by namei.
  157. */
  158. #define NAMECACHE_MAXLEN 31 /* maximum name segment length we bother with */
  159. struct namecache {
  160. TAILQ_ENTRY(namecache) nc_lru; /* Regular Entry LRU chain */
  161. TAILQ_ENTRY(namecache) nc_neg; /* Negative Entry LRU chain */
  162. RB_ENTRY(namecache) n_rbcache; /* Namecache rb tree from vnode */
  163. TAILQ_ENTRY(namecache) nc_me; /* ncp's referring to me */
  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[NAMECACHE_MAXLEN]; /* segment name */
  170. };
  171. #ifdef _KERNEL
  172. int namei(struct nameidata *ndp);
  173. int vfs_lookup(struct nameidata *ndp);
  174. int vfs_relookup(struct vnode *dvp, struct vnode **vpp,
  175. struct componentname *cnp);
  176. void cache_purge(struct vnode *);
  177. int cache_lookup(struct vnode *, struct vnode **, struct componentname *);
  178. void cache_enter(struct vnode *, struct vnode *, struct componentname *);
  179. int cache_revlookup(struct vnode *, struct vnode **, char **, char *);
  180. void nchinit(void);
  181. struct mount;
  182. void cache_purgevfs(struct mount *);
  183. extern struct pool namei_pool;
  184. #endif
  185. /*
  186. * Stats on usefulness of namei caches.
  187. */
  188. struct nchstats {
  189. u_int64_t ncs_goodhits; /* hits that we can really use */
  190. u_int64_t ncs_neghits; /* negative hits that we can use */
  191. u_int64_t ncs_badhits; /* hits we must drop */
  192. u_int64_t ncs_falsehits; /* hits with id mismatch */
  193. u_int64_t ncs_miss; /* misses */
  194. u_int64_t ncs_long; /* long names that ignore cache */
  195. u_int64_t ncs_pass2; /* names found with passes == 2 */
  196. u_int64_t ncs_2passes; /* number of times we attempt it */
  197. u_int64_t ncs_revhits; /* reverse-cache hits */
  198. u_int64_t ncs_revmiss; /* reverse-cache misses */
  199. u_int64_t ncs_dothits; /* hits on '.' lookups */
  200. u_int64_t ncs_dotdothits; /* hits on '..' lookups */
  201. };
  202. /* These sysctl names are only really used by sysctl(8) */
  203. #define KERN_NCHSTATS_GOODHITS 1
  204. #define KERN_NCHSTATS_NEGHITS 2
  205. #define KERN_NCHSTATS_BADHITS 3
  206. #define KERN_NCHSTATS_FALSEHITS 4
  207. #define KERN_NCHSTATS_MISS 5
  208. #define KERN_NCHSTATS_LONG 6
  209. #define KERN_NCHSTATS_PASS2 7
  210. #define KERN_NCHSTATS_2PASSES 8
  211. #define KERN_NCHSTATS_REVHITS 9
  212. #define KERN_NCHSTATS_REVMISS 10
  213. #define KERN_NCHSTATS_DOTHITS 11
  214. #define KERN_NCHSTATS_DOTDOTHITS 12
  215. #define KERN_NCHSTATS_MAXID 13
  216. #define CTL_KERN_NCHSTATS_NAMES { \
  217. { 0, 0 }, \
  218. { "good_hits", CTLTYPE_QUAD }, \
  219. { "negative_hits", CTLTYPE_QUAD }, \
  220. { "bad_hits", CTLTYPE_QUAD }, \
  221. { "false_hits", CTLTYPE_QUAD }, \
  222. { "misses", CTLTYPE_QUAD }, \
  223. { "long_names", CTLTYPE_QUAD }, \
  224. { "pass2", CTLTYPE_QUAD }, \
  225. { "2passes", CTLTYPE_QUAD }, \
  226. { "ncs_revhits", CTLTYPE_QUAD }, \
  227. { "ncs_revmiss", CTLTYPE_QUAD }, \
  228. { "ncs_dothits", CTLTYPE_QUAD }, \
  229. { "nch_dotdothits", CTLTYPE_QUAD }, \
  230. }
  231. #endif /* !_SYS_NAMEI_H_ */