xref: /dragonfly/sys/sys/namecache.h (revision dcd37f7d)
1 /*
2  * Copyright (c) 2003,2004 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. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  * $DragonFly: src/sys/sys/namecache.h,v 1.31 2008/05/09 17:52:18 dillon Exp $
66  */
67 
68 #ifndef _SYS_NAMECACHE_H_
69 #define	_SYS_NAMECACHE_H_
70 
71 #ifndef _SYS_TYPES_H_
72 #include <sys/types.h>
73 #endif
74 #ifndef _SYS_QUEUE_H_
75 #include <sys/queue.h>
76 #endif
77 #ifndef _SYS_SPINLOCK_H_
78 #include <sys/spinlock.h>
79 #endif
80 #ifndef _SYS_THREAD_H_
81 #include <sys/thread.h>
82 #endif
83 
84 struct vnode;
85 struct ucred;
86 struct proc;
87 
88 TAILQ_HEAD(namecache_list, namecache);
89 
90 /*
91  * The namecache structure is used to manage the filesystem namespace.  Most
92  * vnodes cached by the system will reference one or more associated namecache
93  * structures.
94  *
95  * The DragonFly namecache maintains elements from active nodes to the root
96  * in all but the NFS server case and the removed file/directory case.
97  * NFS servers use fhtovp() and may have to regenerate the topology to
98  * the leaf on the fly.
99  *
100  * Because the namecache structure maintains the path through mount points,
101  * null, and union mounts, and other VFS overlays, several namecache
102  * structures may pass through the same vnode.  Also note that namespaces
103  * relating to non-existant (i.e. not-yet-created) files/directories may be
104  * locked.  Lock coherency is achieved by requiring that the particular
105  * namecache record whos parent represents the physical directory in which
106  * the namespace operation is to occur be the one that is locked.  In
107  * overlay cases, the (union, nullfs) VFS, or in namei when crossing a mount
108  * point, may have to obtain multiple namespace record locks to avoid
109  * confusion, but only the one representing the physical directory is passed
110  * into lower layer VOP calls.
111  *
112  * ncp locking is done using atomic ops on nc_exlocks, including a request
113  * flag for waiters.  nc_locktd is set after locking or cleared before
114  * the last unlock.  ncp locks are reentrant.
115  *
116  * Many new API VOP operations do not pass vnodes.  In these cases the
117  * operations vector is typically obtained via nc_mount->mnt_vn_use_ops.
118  */
119 struct namecache {
120     LIST_ENTRY(namecache) nc_hash;	/* hash chain (nc_parent,name) */
121     TAILQ_ENTRY(namecache) nc_entry;	/* scan via nc_parent->nc_list */
122     TAILQ_ENTRY(namecache) nc_vnode;	/* scan via vnode->v_namecache */
123     struct namecache_list  nc_list;	/* list of children */
124     struct nchash_head	*nc_head;
125     struct namecache	*nc_parent;	/* namecache entry for parent */
126     struct vnode	*nc_vp;		/* vnode representing name or NULL */
127     int			nc_refs;	/* ref count prevents deletion */
128     u_short		nc_flag;
129     u_char		nc_nlen;	/* The length of the name, 255 max */
130     u_char		nc_unused;
131     char		*nc_name;	/* Separately allocated seg name */
132     int			nc_error;
133     int			nc_timeout;	/* compared against ticks, or 0 */
134     u_int		nc_exlocks;	/* namespace locking */
135     struct thread	*nc_locktd;	/* namespace locking */
136     long		nc_namecache_gen; /* cmp against mnt_namecache_gen */
137 };
138 
139 /*
140  * Namecache handles include a mount reference allowing topologies
141  * to be replicated with mount overlays (nullfs mounts).
142  */
143 struct nchandle {
144     struct namecache *ncp;		/* ncp in underlying filesystem */
145     struct mount *mount;		/* mount pt (possible overlay) */
146 };
147 
148 #define ASSERT_NCH_LOCKED(nch)	KKASSERT(nch->ncp->nc_locktd == curthread)
149 
150 /*
151  * Flags in namecache.nc_flag (u_char)
152  */
153 #define NCF_UNUSED01	0x0001
154 #define NCF_WHITEOUT	0x0002	/* negative entry corresponds to whiteout */
155 #define NCF_UNRESOLVED	0x0004	/* invalid or unresolved entry */
156 #define NCF_ISMOUNTPT	0x0008	/* someone may have mounted on us here */
157 #define NCF_SF_NOCACHE	0x0010	/* track swapcache chflags from attr */
158 #define NCF_UF_CACHE	0x0020
159 #define NCF_SF_PNOCACHE	0x0040	/* track from parent */
160 #define NCF_UF_PCACHE	0x0080
161 #define NCF_ISSYMLINK	0x0100	/* represents a symlink */
162 #define NCF_ISDIR	0x0200	/* represents a directory */
163 #define NCF_DESTROYED	0x0400	/* name association is considered destroyed */
164 #define NCF_DEFEREDZAP	0x0800	/* zap defered due to lock unavailability */
165 
166 #define NC_EXLOCK_REQ	0x80000000	/* ex_lock state */
167 
168 /*
169  * cache_inval[_vp]() flags
170  */
171 #define CINV_DESTROY	0x0001	/* flag so cache_nlookup ignores the ncp */
172 #define CINV_UNUSED02	0x0002
173 #define CINV_CHILDREN	0x0004	/* recursively set children to unresolved */
174 
175 /*
176  * MP lock helper for namecache.
177  *
178  * CACHE_GETMPLOCK1() Conditionally gets the MP lock if cache_mpsafe
179  *		      is not set, otherwise does not.
180  *
181  * CACHE_GETMPLOCK2() Unconditionally gets the MP lock if it is not already
182  *		      held (e.g. from GETMPLOCK1).
183  *
184  * CACHE_RELMPLOCK()  Releases the MP lock if it was previously acquired
185  *		      by GETMPLOCK1 or GETMPLOCK2.
186  */
187 #define CACHE_MPLOCK_DECLARE		int have_mplock
188 
189 #define CACHE_GETMPLOCK1()						\
190 		do {							\
191 			if (cache_mpsafe) {				\
192 				have_mplock = 0;			\
193 			} else {					\
194 				get_mplock();				\
195 				have_mplock = 1;			\
196 			}						\
197 		} while (0)
198 
199 #define CACHE_GETMPLOCK2()						\
200 		do {							\
201 			if (have_mplock == 0) {				\
202 				have_mplock = 1;			\
203 				get_mplock();				\
204 			}						\
205 		} while(0)
206 
207 #define CACHE_RELMPLOCK()						\
208 		do {							\
209 			if (have_mplock) {				\
210 				have_mplock = 0;			\
211 				rel_mplock();				\
212 			}						\
213 		} while(0)
214 
215 #ifdef _KERNEL
216 
217 extern int cache_mpsafe;
218 
219 struct componentname;
220 struct nlcomponent;
221 struct mount;
222 
223 void	cache_lock(struct nchandle *nch);
224 void	cache_relock(struct nchandle *nch1, struct ucred *cred1,
225 			struct nchandle *nch2, struct ucred *cred2);
226 int	cache_lock_nonblock(struct nchandle *nch);
227 void	cache_unlock(struct nchandle *nch);
228 void	cache_setvp(struct nchandle *nch, struct vnode *vp);
229 void	cache_settimeout(struct nchandle *nch, int nticks);
230 void	cache_setunresolved(struct nchandle *nch);
231 void	cache_clrmountpt(struct nchandle *nch);
232 struct nchandle cache_nlookup(struct nchandle *nch, struct nlcomponent *nlc);
233 struct nchandle cache_nlookup_nonblock(struct nchandle *nch,
234 			struct nlcomponent *nlc);
235 void	cache_allocroot(struct nchandle *nch, struct mount *mp, struct vnode *vp);
236 struct mount *cache_findmount(struct nchandle *nch);
237 int	cache_inval(struct nchandle *nch, int flags);
238 int	cache_inval_vp(struct vnode *vp, int flags);
239 int	cache_inval_vp_nonblock(struct vnode *vp);
240 void	vfs_cache_setroot(struct vnode *vp, struct nchandle *nch);
241 
242 int	cache_resolve(struct nchandle *nch, struct ucred *cred);
243 void	cache_purge(struct vnode *vp);
244 void	cache_purgevfs (struct mount *mp);
245 int	cache_get_nonblock(struct nchandle *nch, struct nchandle *target);
246 void	cache_hysteresis(void);
247 void	cache_get(struct nchandle *nch, struct nchandle *target);
248 struct nchandle *cache_hold(struct nchandle *nch);
249 void	cache_copy(struct nchandle *nch, struct nchandle *target);
250 void	cache_changemount(struct nchandle *nch, struct mount *mp);
251 void	cache_put(struct nchandle *nch);
252 void	cache_drop(struct nchandle *nch);
253 void	cache_zero(struct nchandle *nch);
254 void	cache_rename(struct nchandle *fnch, struct nchandle *tnch);
255 int	cache_vget(struct nchandle *, struct ucred *, int, struct vnode **);
256 int	cache_vref(struct nchandle *, struct ucred *, struct vnode **);
257 int	cache_fromdvp(struct vnode *, struct ucred *, int, struct nchandle *);
258 int	cache_fullpath(struct proc *, struct nchandle *, char **, char **, int);
259 
260 #endif
261 
262 #endif
263