xref: /dragonfly/sys/sys/namecache.h (revision 417b10864204d0b13caea9c2d7bc7f35c004600a)
1 /*
2  * Copyright (c) 2003,2004-2019 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Copyright (c) 1989, 1993
35  *        The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  */
61 
62 #ifndef _SYS_NAMECACHE_H_
63 #define   _SYS_NAMECACHE_H_
64 
65 #ifndef _SYS_TYPES_H_
66 #include <sys/types.h>
67 #endif
68 #ifndef _SYS_LOCK_H_
69 #include <sys/lock.h>
70 #endif
71 #ifndef _SYS_QUEUE_H_
72 #include <sys/queue.h>
73 #endif
74 #ifndef _SYS_SPINLOCK_H_
75 #include <sys/spinlock.h>
76 #endif
77 
78 struct vnode;
79 struct ucred;
80 struct proc;
81 
82 TAILQ_HEAD(namecache_list, namecache);
83 
84 /*
85  * The namecache structure is used to manage the filesystem namespace.  Most
86  * vnodes cached by the system will reference one or more associated namecache
87  * structures.
88  *
89  * The DragonFly namecache maintains elements from active nodes to the root
90  * in all but the NFS server case and the removed file/directory case.
91  * NFS servers use fhtovp() and may have to regenerate the topology to
92  * the leaf on the fly.
93  *
94  * Because the namecache structure maintains the path through mount points,
95  * null, and union mounts, and other VFS overlays, several namecache
96  * structures may pass through the same vnode.  Also note that namespaces
97  * relating to non-existant (i.e. not-yet-created) files/directories may be
98  * locked.  Lock coherency is achieved by requiring that the particular
99  * namecache record whos parent represents the physical directory in which
100  * the namespace operation is to occur be the one that is locked.  In
101  * overlay cases, the (union, nullfs) VFS, or in namei when crossing a mount
102  * point, may have to obtain multiple namespace record locks to avoid
103  * confusion, but only the one representing the physical directory is passed
104  * into lower layer VOP calls.
105  *
106  * ncp locking is done using atomic ops on nc_lockstatus, including a request
107  * flag for waiters.  nc_locktd is set after locking or cleared before
108  * the last unlock.  ncp locks are reentrant.
109  *
110  * Many new API VOP operations do not pass vnodes.  In these cases the
111  * operations vector is typically obtained via nc_mount->mnt_vn_use_ops.
112  *
113  * nc_refs -
114  *        This is naturally 1, plus 1 more if resolved (either positively
115  *        or negatively), plus 1 ref for each child in ncp->nc_list, plus
116  *        thread-held and mountcache refs.
117  *
118  * nc_generation -
119  *        This is adjusted by 2 whenever a locked ncp's resolve state,
120  *        destroyed state, name/association, or link state changes, and
121  *        then adjusted by 2 again when the operation is complete.  Thus,
122  *        if (nc_generation & 2) != 0 or (nc_generation & ~1) has changed,
123  *        an unlocked accessor should cycle a lock and retry.
124  */
125 struct namecache {
126     TAILQ_ENTRY(namecache) nc_hash;     /* hash chain (nc_parent,name) */
127     TAILQ_ENTRY(namecache) nc_entry;    /* scan via nc_parent->nc_list */
128     TAILQ_ENTRY(namecache) nc_vnode;    /* scan via vnode->v_namecache */
129     struct namecache_list  nc_list;     /* list of children */
130     struct nchash_head        *nc_head;
131     struct namecache          *nc_parent;         /* namecache entry for parent */
132     struct vnode    *nc_vp;             /* vnode representing name or NULL */
133     u_short                   nc_flag;
134     u_char                    nc_nlen;  /* The length of the name, 255 max */
135     u_char                    nc_unused;
136     char            *nc_name; /* Separately allocated seg name */
137     int                       nc_error;
138     int                       nc_timeout;         /* compared against ticks, or 0 */
139     int                       nc_negcpu;          /* which ncneg list are we on? */
140     struct {
141               u_int nc_namecache_gen; /* mount generation (autoclear) */
142               u_int nc_generation;      /* see notes above */
143               int             nc_refs;  /* ref count prevents deletion */
144     } __cachealign;
145     struct {
146               struct lock nc_lock;
147     } __cachealign;
148 };
149 
150 /*
151  * Namecache handles include a mount reference allowing topologies
152  * to be replicated with mount overlays (nullfs mounts).
153  */
154 struct nchandle {
155     struct namecache *ncp;              /* ncp in underlying filesystem */
156     struct mount *mount;                /* mount pt (possible overlay) */
157 };
158 
159 /*
160  * Flags in namecache.nc_flag (u_short)
161  */
162 #define NCF_NOTX    0x0001    /* 'x' bit not set in user, group, or world */
163 #define NCF_WHITEOUT          0x0002    /* negative entry corresponds to whiteout */
164 #define NCF_UNRESOLVED        0x0004    /* invalid or unresolved entry */
165 #define NCF_ISMOUNTPT         0x0008    /* someone may have mounted on us here */
166 #define NCF_SF_NOCACHE        0x0010    /* track swapcache chflags from attr */
167 #define NCF_UF_CACHE          0x0020
168 #define NCF_SF_PNOCACHE       0x0040    /* track from parent */
169 #define NCF_UF_PCACHE         0x0080
170 #define NCF_ISSYMLINK         0x0100    /* represents a symlink */
171 #define NCF_ISDIR   0x0200    /* represents a directory */
172 #define NCF_DESTROYED         0x0400    /* name association is considered destroyed */
173 #define NCF_DEFEREDZAP        0x0800    /* zap defered due to lock unavailability */
174 #define NCF_WXOK    0x1000    /* world-searchable (nlookup shortcut) */
175 #define NCF_DUMMY   0x2000    /* dummy ncp, iterations ignore it */
176 
177 /*
178  * cache_inval[_vp]() flags
179  */
180 #define CINV_DESTROY          0x0001    /* flag so cache_nlookup ignores the ncp */
181 #define CINV_UNUSED02         0x0002
182 #define CINV_CHILDREN         0x0004    /* recursively set children to unresolved */
183 
184 #ifdef _KERNEL
185 
186 struct componentname;
187 struct nlcomponent;
188 struct mount;
189 
190 void      cache_clearmntcache(struct mount *mp);
191 void      cache_lock(struct nchandle *nch);
192 void      cache_lock_maybe_shared(struct nchandle *nch, int excl);
193 void      cache_lock4_tondlocked(
194                         struct nchandle *fncpd, struct nchandle *fncp,
195                         struct nchandle *tncpd, struct nchandle *tncp,
196                         struct ucred *fcred, struct ucred *tcred);
197 int       cache_lock_nonblock(struct nchandle *nch);
198 void      cache_unlock(struct nchandle *nch);
199 int       cache_lockstatus(struct nchandle *nch);
200 void      cache_setvp(struct nchandle *nch, struct vnode *vp);
201 void      cache_settimeout(struct nchandle *nch, int nticks);
202 void      cache_setunresolved(struct nchandle *nch);
203 void      cache_clrmountpt(struct nchandle *nch);
204 struct nchandle cache_nlookup(struct nchandle *nch,
205                               struct nlcomponent *nlc);
206 struct nchandle cache_nlookup_nonblock(struct nchandle *nch,
207                               struct nlcomponent *nlc);
208 struct nchandle cache_nlookup_nonlocked(struct nchandle *nch,
209                               struct nlcomponent *nlc);
210 int       cache_nlookup_maybe_shared(struct nchandle *nch,
211                               struct nlcomponent *nlc, int excl,
212                               struct nchandle *nchres);
213 void      cache_allocroot(struct nchandle *nch, struct mount *mp, struct vnode *vp);
214 struct mount *cache_findmount(struct nchandle *nch);
215 void      cache_dropmount(struct mount *mp);
216 void      cache_ismounting(struct mount *mp);
217 void      cache_unmounting(struct mount *mp);
218 int       cache_inval(struct nchandle *nch, int flags);
219 int       cache_inval_vp(struct vnode *vp, int flags);
220 int       cache_inval_vp_nonblock(struct vnode *vp);
221 void      cache_inval_vp_quick(struct vnode *vp);
222 void      cache_inval_wxok(struct vnode *vp);
223 void      vfs_cache_setroot(struct vnode *vp, struct nchandle *nch);
224 
225 int       cache_resolve(struct nchandle *nch, u_int *genp, struct ucred *cred);
226 int       cache_resolve_dvp(struct nchandle *nch, struct ucred *cred,
227                               struct vnode **dvpp);
228 void      cache_purge(struct vnode *vp);
229 void      cache_purgevfs (struct mount *mp);
230 void      cache_hysteresis(int critpath);
231 void      cache_get(struct nchandle *nch, struct nchandle *target);
232 int       cache_get_nonblock(struct nchandle *nch, int elmno,
233                               struct nchandle *target);
234 void      cache_get_maybe_shared(struct nchandle *nch,
235                               struct nchandle *target, int excl);
236 struct nchandle *cache_hold(struct nchandle *nch);
237 void      cache_copy(struct nchandle *nch, struct nchandle *target);
238 void      cache_changemount(struct nchandle *nch, struct mount *mp);
239 void      cache_put(struct nchandle *nch);
240 void      cache_drop(struct nchandle *nch);
241 void      cache_drop_and_cache(struct nchandle *nch, int elmno);
242 void      cache_zero(struct nchandle *nch);
243 void      cache_rename(struct nchandle *fnch, struct nchandle *tnch);
244 void      cache_unlink(struct nchandle *nch);
245 int       cache_isopen(struct nchandle *nch);
246 int       cache_vget(struct nchandle *, struct ucred *, int, struct vnode **);
247 int       cache_vref(struct nchandle *, struct ucred *, struct vnode **);
248 int       cache_fromdvp(struct vnode *, struct ucred *, int, struct nchandle *);
249 int       cache_fullpath(struct proc *, struct nchandle *, struct nchandle *,
250                               char **, char **, int);
251 void      vfscache_rollup_cpu(struct globaldata *gd);
252 struct vnode *cache_dvpref(struct namecache *ncp);
253 
254 #endif
255 
256 #endif
257