xref: /linux/include/linux/dcache.h (revision 96a377ba007a6a1ee9fa9f0ba48812fe9284f0ce)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_DCACHE_H
3 #define __LINUX_DCACHE_H
4 
5 #include <linux/atomic.h>
6 #include <linux/list.h>
7 #include <linux/math.h>
8 #include <linux/rculist.h>
9 #include <linux/rculist_bl.h>
10 #include <linux/spinlock.h>
11 #include <linux/seqlock.h>
12 #include <linux/cache.h>
13 #include <linux/rcupdate.h>
14 #include <linux/lockref.h>
15 #include <linux/stringhash.h>
16 #include <linux/wait.h>
17 
18 struct path;
19 struct file;
20 struct vfsmount;
21 
22 /*
23  * linux/include/linux/dcache.h
24  *
25  * Dirent cache data structures
26  *
27  * (C) Copyright 1997 Thomas Schoebel-Theuer,
28  * with heavy changes by Linus Torvalds
29  */
30 
31 #define IS_ROOT(x) ((x) == (x)->d_parent)
32 
33 /* The hash is always the low bits of hash_len */
34 #ifdef __LITTLE_ENDIAN
35  #define HASH_LEN_DECLARE u32 hash; u32 len
36  #define bytemask_from_count(cnt)	(~(~0ul << (cnt)*8))
37 #else
38  #define HASH_LEN_DECLARE u32 len; u32 hash
39  #define bytemask_from_count(cnt)	(~(~0ul >> (cnt)*8))
40 #endif
41 
42 /*
43  * "quick string" -- eases parameter passing, but more importantly
44  * saves "metadata" about the string (ie length and the hash).
45  *
46  * hash comes first so it snuggles against d_parent in the
47  * dentry.
48  */
49 struct qstr {
50 	union {
51 		struct {
52 			HASH_LEN_DECLARE;
53 		};
54 		u64 hash_len;
55 	};
56 	const unsigned char *name;
57 };
58 
59 #define QSTR_INIT(n,l) { { { .len = l } }, .name = n }
60 #define QSTR_LEN(n,l) (struct qstr)QSTR_INIT(n,l)
61 #define QSTR(n) QSTR_LEN(n, strlen(n))
62 
63 extern const struct qstr empty_name;
64 extern const struct qstr slash_name;
65 extern const struct qstr dotdot_name;
66 
67 /*
68  * Try to keep struct dentry aligned on 64 byte cachelines (this will
69  * give reasonable cacheline footprint with larger lines without the
70  * large memory footprint increase).
71  */
72 #ifdef CONFIG_64BIT
73 # define DNAME_INLINE_WORDS 5 /* 192 bytes */
74 #else
75 # ifdef CONFIG_SMP
76 #  define DNAME_INLINE_WORDS 9 /* 128 bytes */
77 # else
78 #  define DNAME_INLINE_WORDS 11 /* 128 bytes */
79 # endif
80 #endif
81 
82 #define DNAME_INLINE_LEN (DNAME_INLINE_WORDS*sizeof(unsigned long))
83 
84 union shortname_store {
85 	unsigned char string[DNAME_INLINE_LEN];
86 	unsigned long words[DNAME_INLINE_WORDS];
87 };
88 
89 #define d_lock	d_lockref.lock
90 #define d_iname d_shortname.string
91 struct completion_list;
92 
93 struct dentry {
94 	/* RCU lookup touched fields */
95 	unsigned int d_flags;		/* protected by d_lock */
96 	seqcount_spinlock_t d_seq;	/* per dentry seqlock */
97 	struct hlist_bl_node d_hash;	/* lookup hash list */
98 	struct dentry *d_parent;	/* parent directory */
99 	union {
100 	struct qstr __d_name;		/* for use ONLY in fs/dcache.c */
101 	const struct qstr d_name;
102 	};
103 	struct inode *d_inode;		/* Where the name belongs to - NULL is
104 					 * negative */
105 	union shortname_store d_shortname;
106 	/* --- cacheline 1 boundary (64 bytes) was 32 bytes ago --- */
107 
108 	/* Ref lookup also touches following */
109 	const struct dentry_operations *d_op;
110 	struct super_block *d_sb;	/* The root of the dentry tree */
111 	unsigned long d_time;		/* used by d_revalidate */
112 	void *d_fsdata;			/* fs-specific data */
113 	/* --- cacheline 2 boundary (128 bytes) --- */
114 	struct lockref d_lockref;	/* per-dentry lock and refcount
115 					 * keep separate from RCU lookup area if
116 					 * possible!
117 					 */
118 
119 	/* lockdep tracking of DCACHE_PAR_LOOKUP locks */
120 	struct lockdep_map		dep_map;
121 	struct list_head d_lru;		/* LRU list */
122 	struct hlist_node d_sib;	/* child of parent list */
123 	struct hlist_head d_children;	/* our children */
124 	/*
125 	 * the following members can share memory - their uses are
126 	 * mutually exclusive.
127 	 */
128 	union {
129 		/* positives: inode alias list */
130 		struct hlist_node d_alias;
131 		/* in-lookup ones (all negative, live): hash chain */
132 		struct hlist_bl_node d_in_lookup_hash;
133 		/* killed ones: (already negative) used to schedule freeing */
134 	 	struct rcu_head d_rcu;
135 		/*
136 		 * live non-in-lookup negatives: used if shrink_dcache_tree()
137 		 * races with eviction by another thread and needs to wait for
138 		 * this dentry to get killed .  Remains NULL for almost all
139 		 * negative dentries.
140 		 */
141 		struct completion_list *waiters;
142 	};
143 };
144 
145 /*
146  * dentry->d_lock spinlock nesting subclasses:
147  *
148  * 0: normal
149  * 1: nested
150  */
151 enum dentry_d_lock_class
152 {
153 	DENTRY_D_LOCK_NORMAL, /* implicitly used by plain spin_lock() APIs. */
154 	DENTRY_D_LOCK_NESTED
155 };
156 
157 enum d_real_type {
158 	D_REAL_DATA,
159 	D_REAL_METADATA,
160 };
161 
162 struct dentry_operations {
163 	int (*d_revalidate)(struct inode *, const struct qstr *,
164 			    struct dentry *, unsigned int);
165 	int (*d_weak_revalidate)(struct dentry *, unsigned int);
166 	int (*d_hash)(const struct dentry *, struct qstr *);
167 	int (*d_compare)(const struct dentry *,
168 			unsigned int, const char *, const struct qstr *);
169 	int (*d_delete)(const struct dentry *);
170 	int (*d_init)(struct dentry *);
171 	void (*d_release)(struct dentry *);
172 	void (*d_prune)(struct dentry *);
173 	void (*d_iput)(struct dentry *, struct inode *);
174 	char *(*d_dname)(struct dentry *, char *, int);
175 	struct vfsmount *(*d_automount)(struct path *);
176 	int (*d_manage)(const struct path *, bool);
177 	struct dentry *(*d_real)(struct dentry *, enum d_real_type type);
178 	bool (*d_unalias_trylock)(const struct dentry *);
179 	void (*d_unalias_unlock)(const struct dentry *);
180 } ____cacheline_aligned;
181 
182 /*
183  * Locking rules for dentry_operations callbacks are to be found in
184  * Documentation/filesystems/locking.rst. Keep it updated!
185  *
186  * FUrther descriptions are found in Documentation/filesystems/vfs.rst.
187  * Keep it updated too!
188  */
189 
190 /* d_flags entries */
191 enum dentry_flags {
192 	DCACHE_OP_HASH			= BIT(0),
193 	DCACHE_OP_COMPARE		= BIT(1),
194 	DCACHE_OP_REVALIDATE		= BIT(2),
195 	DCACHE_OP_DELETE		= BIT(3),
196 	DCACHE_OP_PRUNE			= BIT(4),
197 	/*
198 	 * This dentry is possibly not currently connected to the dcache tree,
199 	 * in which case its parent will either be itself, or will have this
200 	 * flag as well.  nfsd will not use a dentry with this bit set, but will
201 	 * first endeavour to clear the bit either by discovering that it is
202 	 * connected, or by performing lookup operations.  Any filesystem which
203 	 * supports nfsd_operations MUST have a lookup function which, if it
204 	 * finds a directory inode with a DCACHE_DISCONNECTED dentry, will
205 	 * d_move that dentry into place and return that dentry rather than the
206 	 * passed one, typically using d_splice_alias.
207 	 */
208 	DCACHE_DISCONNECTED		= BIT(5),
209 	DCACHE_REFERENCED		= BIT(6),	/* Recently used, don't discard. */
210 	DCACHE_DONTCACHE		= BIT(7),	/* Purge from memory on final dput() */
211 	DCACHE_CANT_MOUNT		= BIT(8),
212 	DCACHE_LOOKUP_WAITERS		= BIT(9),	/* A thread is waiting for
213 							 * PAR_LOOKUP to clear
214 							 */
215 	DCACHE_SHRINK_LIST		= BIT(10),
216 	DCACHE_OP_WEAK_REVALIDATE	= BIT(11),
217 	/*
218 	 * this dentry has been "silly renamed" and has to be deleted on the
219 	 * last dput()
220 	 */
221 	DCACHE_NFSFS_RENAMED		= BIT(12),
222 	DCACHE_FSNOTIFY_PARENT_WATCHED	= BIT(13),	/* Parent inode is watched by some fsnotify listener */
223 	DCACHE_DENTRY_KILLED		= BIT(14),
224 	DCACHE_MOUNTED			= BIT(15),	/* is a mountpoint */
225 	DCACHE_NEED_AUTOMOUNT		= BIT(16),	/* handle automount on this dir */
226 	DCACHE_MANAGE_TRANSIT		= BIT(17),	/* manage transit from this dirent */
227 	DCACHE_LRU_LIST			= BIT(18),
228 	DCACHE_ENTRY_TYPE		= (7 << 19),	/* bits 19..21 are for storing type: */
229 	DCACHE_MISS_TYPE		= (0 << 19),	/* Negative dentry */
230 	DCACHE_WHITEOUT_TYPE		= (1 << 19),	/* Whiteout dentry (stop pathwalk) */
231 	DCACHE_DIRECTORY_TYPE		= (2 << 19),	/* Normal directory */
232 	DCACHE_AUTODIR_TYPE		= (3 << 19),	/* Lookupless directory (presumed automount) */
233 	DCACHE_REGULAR_TYPE		= (4 << 19),	/* Regular file type */
234 	DCACHE_SPECIAL_TYPE		= (5 << 19),	/* Other file type */
235 	DCACHE_SYMLINK_TYPE		= (6 << 19),	/* Symlink */
236 	DCACHE_NOKEY_NAME		= BIT(22),	/* Encrypted name encoded without key */
237 	DCACHE_OP_REAL			= BIT(23),
238 	DCACHE_PAR_LOOKUP		= BIT(24),	/* being looked up (with parent locked shared) */
239 	DCACHE_DENTRY_CURSOR		= BIT(25),
240 	DCACHE_NORCU			= BIT(26),	/* No RCU delay for freeing */
241 	DCACHE_PERSISTENT		= BIT(27),
242 /* 28, 29, 30 free */
243 	DCACHE_PRIVATE			= BIT(31)	/* fs-specific flag */
244 };
245 
246 #define DCACHE_MANAGED_DENTRY \
247 	(DCACHE_MOUNTED|DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT)
248 
249 extern seqlock_t rename_lock;
250 
251 /*
252  * These are the low-level FS interfaces to the dcache..
253  */
254 extern void d_instantiate(struct dentry *, struct inode *);
255 extern void d_instantiate_new(struct dentry *, struct inode *);
256 extern void __d_drop(struct dentry *dentry);
257 extern void d_drop(struct dentry *dentry);
258 extern void d_delete(struct dentry *);
259 
260 /* allocate/de-allocate */
261 extern struct dentry * d_alloc(struct dentry *, const struct qstr *);
262 extern struct dentry * d_alloc_anon(struct super_block *);
263 extern struct dentry * d_alloc_parallel(struct dentry *, const struct qstr *);
264 extern struct dentry * d_alloc_trylock(struct dentry *, struct qstr *);
265 extern struct dentry * d_splice_alias(struct inode *, struct dentry *);
266 struct dentry *d_duplicate(struct dentry *dentry);
267 /* weird procfs mess; *NOT* exported */
268 extern struct dentry * d_splice_alias_ops(struct inode *, struct dentry *,
269 					  const struct dentry_operations *);
270 extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
271 extern bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
272 			const struct qstr *name);
273 extern struct dentry *d_find_any_alias(struct inode *inode);
274 extern struct dentry * d_obtain_alias(struct inode *);
275 extern struct dentry * d_obtain_root(struct inode *);
276 extern void shrink_dcache_sb(struct super_block *);
277 extern void shrink_dcache_parent(struct dentry *);
278 extern void d_invalidate(struct dentry *);
279 
280 /* only used at mount-time */
281 extern struct dentry * d_make_root(struct inode *);
282 
283 extern void d_mark_tmpfile(struct file *, struct inode *);
284 int d_mark_tmpfile_name(struct file *file, const struct qstr *name);
285 extern void d_tmpfile(struct file *, struct inode *);
286 
287 extern struct dentry *d_find_alias(struct inode *);
288 extern void d_prune_aliases(struct inode *);
289 extern bool __move_to_shrink_list(struct dentry *, struct list_head *);
290 extern void shrink_dentry_list(struct list_head *);
291 
292 extern struct dentry *d_find_alias_rcu(struct inode *);
293 
294 /* test whether we have any submounts in a subdir tree */
295 extern int path_has_submounts(const struct path *);
296 
297 /*
298  * This adds the entry to the hash queues.
299  */
300 extern void d_rehash(struct dentry *);
301 
302 extern void d_add(struct dentry *, struct inode *);
303 
304 /* used for rename() and baskets */
305 extern void d_move(struct dentry *, struct dentry *);
306 extern void d_exchange(struct dentry *, struct dentry *);
307 extern struct dentry *d_ancestor(struct dentry *, struct dentry *);
308 
309 extern struct dentry *d_lookup(const struct dentry *, const struct qstr *);
310 
d_count(const struct dentry * dentry)311 static inline unsigned d_count(const struct dentry *dentry)
312 {
313 	return dentry->d_lockref.count;
314 }
315 
316 ino_t d_parent_ino(struct dentry *dentry);
317 
318 /*
319  * helper function for dentry_operations.d_dname() members
320  */
321 extern __printf(3, 4)
322 char *dynamic_dname(char *, int, const char *, ...);
323 
324 extern char *__d_path(const struct path *, const struct path *, char *, int);
325 extern char *d_absolute_path(const struct path *, char *, int);
326 extern char *d_path(const struct path *, char *, int);
327 extern char *dentry_path_raw(const struct dentry *, char *, int);
328 extern char *dentry_path(const struct dentry *, char *, int);
329 
330 /* Allocation counts.. */
331 
332 /**
333  * dget_dlock -	get a reference to a dentry
334  * @dentry: dentry to get a reference to
335  *
336  * Given a live dentry, increment the reference count and return the dentry.
337  * Caller must hold @dentry->d_lock.  Making sure that dentry is alive is
338  * caller's resonsibility.  There are many conditions sufficient to guarantee
339  * that; e.g. anything with non-negative refcount is alive, so's anything
340  * hashed, anything positive, anyone's parent, etc.
341  */
dget_dlock(struct dentry * dentry)342 static inline struct dentry *dget_dlock(struct dentry *dentry)
343 {
344 	dentry->d_lockref.count++;
345 	return dentry;
346 }
347 
348 
349 /**
350  * dget - get a reference to a dentry
351  * @dentry: dentry to get a reference to
352  *
353  * Given a dentry or %NULL pointer increment the reference count
354  * if appropriate and return the dentry.  A dentry will not be
355  * destroyed when it has references.  Conversely, a dentry with
356  * no references can disappear for any number of reasons, starting
357  * with memory pressure.  In other words, that primitive is
358  * used to clone an existing reference; using it on something with
359  * zero refcount is a bug.
360  *
361  * NOTE: it will spin if @dentry->d_lock is held.  From the deadlock
362  * avoidance point of view it is equivalent to spin_lock()/increment
363  * refcount/spin_unlock(), so calling it under @dentry->d_lock is
364  * always a bug; so's calling it under ->d_lock on any of its descendents.
365  *
366  */
dget(struct dentry * dentry)367 static inline struct dentry *dget(struct dentry *dentry)
368 {
369 	if (dentry)
370 		lockref_get(&dentry->d_lockref);
371 	return dentry;
372 }
373 
374 /* dentry->d_inode->i_lock must be held by caller */
dget_alias_ilocked(struct dentry * dentry)375 static inline bool dget_alias_ilocked(struct dentry *dentry)
376 {
377 	if (likely(!(READ_ONCE(dentry->d_flags) & DCACHE_NORCU))) {
378 		lockref_get(&dentry->d_lockref);
379 		return true;
380 	}
381 	// NORCU dentries with zero refcount MUST NOT be grabbed
382 	spin_lock(&dentry->d_lock);
383 	if (dentry->d_lockref.count > 0) {
384 		dget_dlock(dentry);
385 		spin_unlock(&dentry->d_lock);
386 		return true;
387 	}
388 	spin_unlock(&dentry->d_lock);
389 	return false;
390 }
391 
392 extern struct dentry *dget_parent(struct dentry *dentry);
393 
394 /**
395  * d_unhashed - is dentry hashed
396  * @dentry: entry to check
397  *
398  * Returns true if the dentry passed is not currently hashed.
399  */
d_unhashed(const struct dentry * dentry)400 static inline int d_unhashed(const struct dentry *dentry)
401 {
402 	return hlist_bl_unhashed(&dentry->d_hash);
403 }
404 
d_unlinked(const struct dentry * dentry)405 static inline int d_unlinked(const struct dentry *dentry)
406 {
407 	return d_unhashed(dentry) && !IS_ROOT(dentry);
408 }
409 
cant_mount(const struct dentry * dentry)410 static inline int cant_mount(const struct dentry *dentry)
411 {
412 	return (dentry->d_flags & DCACHE_CANT_MOUNT);
413 }
414 
dont_mount(struct dentry * dentry)415 static inline void dont_mount(struct dentry *dentry)
416 {
417 	spin_lock(&dentry->d_lock);
418 	dentry->d_flags |= DCACHE_CANT_MOUNT;
419 	spin_unlock(&dentry->d_lock);
420 }
421 
422 extern void __d_lookup_unhash_wake(struct dentry *dentry);
423 
d_in_lookup(const struct dentry * dentry)424 static inline int d_in_lookup(const struct dentry *dentry)
425 {
426 	return dentry->d_flags & DCACHE_PAR_LOOKUP;
427 }
428 
d_lookup_done(struct dentry * dentry)429 static inline void d_lookup_done(struct dentry *dentry)
430 {
431 	if (unlikely(d_in_lookup(dentry)))
432 		__d_lookup_unhash_wake(dentry);
433 }
434 
435 extern void dput(struct dentry *);
436 
d_managed(const struct dentry * dentry)437 static inline bool d_managed(const struct dentry *dentry)
438 {
439 	return dentry->d_flags & DCACHE_MANAGED_DENTRY;
440 }
441 
d_mountpoint(const struct dentry * dentry)442 static inline bool d_mountpoint(const struct dentry *dentry)
443 {
444 	return dentry->d_flags & DCACHE_MOUNTED;
445 }
446 
447 /*
448  * Directory cache entry type accessor functions.
449  */
__d_entry_type(const struct dentry * dentry)450 static inline unsigned __d_entry_type(const struct dentry *dentry)
451 {
452 	return dentry->d_flags & DCACHE_ENTRY_TYPE;
453 }
454 
d_is_miss(const struct dentry * dentry)455 static inline bool d_is_miss(const struct dentry *dentry)
456 {
457 	return __d_entry_type(dentry) == DCACHE_MISS_TYPE;
458 }
459 
d_is_whiteout(const struct dentry * dentry)460 static inline bool d_is_whiteout(const struct dentry *dentry)
461 {
462 	return __d_entry_type(dentry) == DCACHE_WHITEOUT_TYPE;
463 }
464 
d_can_lookup(const struct dentry * dentry)465 static inline bool d_can_lookup(const struct dentry *dentry)
466 {
467 	return __d_entry_type(dentry) == DCACHE_DIRECTORY_TYPE;
468 }
469 
d_is_autodir(const struct dentry * dentry)470 static inline bool d_is_autodir(const struct dentry *dentry)
471 {
472 	return __d_entry_type(dentry) == DCACHE_AUTODIR_TYPE;
473 }
474 
d_is_dir(const struct dentry * dentry)475 static inline bool d_is_dir(const struct dentry *dentry)
476 {
477 	return d_can_lookup(dentry) || d_is_autodir(dentry);
478 }
479 
d_is_symlink(const struct dentry * dentry)480 static inline bool d_is_symlink(const struct dentry *dentry)
481 {
482 	return __d_entry_type(dentry) == DCACHE_SYMLINK_TYPE;
483 }
484 
d_is_reg(const struct dentry * dentry)485 static inline bool d_is_reg(const struct dentry *dentry)
486 {
487 	return __d_entry_type(dentry) == DCACHE_REGULAR_TYPE;
488 }
489 
d_is_special(const struct dentry * dentry)490 static inline bool d_is_special(const struct dentry *dentry)
491 {
492 	return __d_entry_type(dentry) == DCACHE_SPECIAL_TYPE;
493 }
494 
d_is_file(const struct dentry * dentry)495 static inline bool d_is_file(const struct dentry *dentry)
496 {
497 	return d_is_reg(dentry) || d_is_special(dentry);
498 }
499 
d_is_negative(const struct dentry * dentry)500 static inline bool d_is_negative(const struct dentry *dentry)
501 {
502 	// TODO: check d_is_whiteout(dentry) also.
503 	return d_is_miss(dentry);
504 }
505 
d_flags_negative(unsigned flags)506 static inline bool d_flags_negative(unsigned flags)
507 {
508 	return (flags & DCACHE_ENTRY_TYPE) == DCACHE_MISS_TYPE;
509 }
510 
d_is_positive(const struct dentry * dentry)511 static inline bool d_is_positive(const struct dentry *dentry)
512 {
513 	return !d_is_negative(dentry);
514 }
515 
516 /**
517  * d_really_is_negative - Determine if a dentry is really negative (ignoring fallthroughs)
518  * @dentry: The dentry in question
519  *
520  * Returns true if the dentry represents either an absent name or a name that
521  * doesn't map to an inode (ie. ->d_inode is NULL).  The dentry could represent
522  * a true miss, a whiteout that isn't represented by a 0,0 chardev or a
523  * fallthrough marker in an opaque directory.
524  *
525  * Note!  (1) This should be used *only* by a filesystem to examine its own
526  * dentries.  It should not be used to look at some other filesystem's
527  * dentries.  (2) It should also be used in combination with d_inode() to get
528  * the inode.  (3) The dentry may have something attached to ->d_lower and the
529  * type field of the flags may be set to something other than miss or whiteout.
530  */
d_really_is_negative(const struct dentry * dentry)531 static inline bool d_really_is_negative(const struct dentry *dentry)
532 {
533 	return dentry->d_inode == NULL;
534 }
535 
536 /**
537  * d_really_is_positive - Determine if a dentry is really positive (ignoring fallthroughs)
538  * @dentry: The dentry in question
539  *
540  * Returns true if the dentry represents a name that maps to an inode
541  * (ie. ->d_inode is not NULL).  The dentry might still represent a whiteout if
542  * that is represented on medium as a 0,0 chardev.
543  *
544  * Note!  (1) This should be used *only* by a filesystem to examine its own
545  * dentries.  It should not be used to look at some other filesystem's
546  * dentries.  (2) It should also be used in combination with d_inode() to get
547  * the inode.
548  */
d_really_is_positive(const struct dentry * dentry)549 static inline bool d_really_is_positive(const struct dentry *dentry)
550 {
551 	return dentry->d_inode != NULL;
552 }
553 
simple_positive(const struct dentry * dentry)554 static inline int simple_positive(const struct dentry *dentry)
555 {
556 	return d_really_is_positive(dentry) && !d_unhashed(dentry);
557 }
558 
559 unsigned long vfs_pressure_ratio(unsigned long val);
560 
561 /**
562  * d_lookup_release - release ownership of DCACHE_PAR_LOOKUP lock
563  * @dentry: dentry that is locked
564  *
565  * If an in-lookup dentry is to be passed to another thread which
566  * will drop the in-lookup lock, then d_lookup_release() must be called
567  * to tell lockdep that this thread no lock holds the lock.  The
568  * thread that receives the lock must call d_lookup_acquire() to
569  * acquire the lock.
570  */
d_lookup_release(struct dentry * dentry)571 static inline void d_lookup_release(struct dentry *dentry)
572 {
573 	if (d_in_lookup(dentry))
574 		lock_map_release(&dentry->dep_map);
575 }
576 
577 /**
578  * d_lookup_acquire - acquire ownership of DCACHE_PAR_LOOKUP lock
579  * @dentry: dentry that is locked
580  *
581  * If an in-lookup dentry was passed to this thread, the
582  * d_lookup_acquire() must be called to tell lockdep that this
583  * thread now owns the DCACHE_PAR_LOOKUP lock.
584  */
d_lookup_acquire(struct dentry * dentry)585 static inline void d_lookup_acquire(struct dentry *dentry)
586 {
587 	if (d_in_lookup(dentry))
588 		lock_map_acquire_try(&dentry->dep_map);
589 }
590 
591 /**
592  * d_inode - Get the actual inode of this dentry
593  * @dentry: The dentry to query
594  *
595  * This is the helper normal filesystems should use to get at their own inodes
596  * in their own dentries and ignore the layering superimposed upon them.
597  */
d_inode(const struct dentry * dentry)598 static inline struct inode *d_inode(const struct dentry *dentry)
599 {
600 	return dentry->d_inode;
601 }
602 
603 /**
604  * d_inode_rcu - Get the actual inode of this dentry with READ_ONCE()
605  * @dentry: The dentry to query
606  *
607  * This is the helper normal filesystems should use to get at their own inodes
608  * in their own dentries and ignore the layering superimposed upon them.
609  */
d_inode_rcu(const struct dentry * dentry)610 static inline struct inode *d_inode_rcu(const struct dentry *dentry)
611 {
612 	return READ_ONCE(dentry->d_inode);
613 }
614 
615 /**
616  * d_backing_inode - Get upper or lower inode we should be using
617  * @upper: The upper layer
618  *
619  * This is the helper that should be used to get at the inode that will be used
620  * if this dentry were to be opened as a file.  The inode may be on the upper
621  * dentry or it may be on a lower dentry pinned by the upper.
622  *
623  * Normal filesystems should not use this to access their own inodes.
624  */
d_backing_inode(const struct dentry * upper)625 static inline struct inode *d_backing_inode(const struct dentry *upper)
626 {
627 	struct inode *inode = upper->d_inode;
628 
629 	return inode;
630 }
631 
632 /**
633  * d_real - Return the real dentry
634  * @dentry: the dentry to query
635  * @type: the type of real dentry (data or metadata)
636  *
637  * If dentry is on a union/overlay, then return the underlying, real dentry.
638  * Otherwise return the dentry itself.
639  *
640  * See also: Documentation/filesystems/vfs.rst
641  */
d_real(struct dentry * dentry,enum d_real_type type)642 static inline struct dentry *d_real(struct dentry *dentry, enum d_real_type type)
643 {
644 	if (unlikely(dentry->d_flags & DCACHE_OP_REAL))
645 		return dentry->d_op->d_real(dentry, type);
646 	else
647 		return dentry;
648 }
649 
650 /**
651  * d_real_inode - Return the real inode hosting the data
652  * @dentry: The dentry to query
653  *
654  * If dentry is on a union/overlay, then return the underlying, real inode.
655  * Otherwise return d_inode().
656  */
d_real_inode(const struct dentry * dentry)657 static inline struct inode *d_real_inode(const struct dentry *dentry)
658 {
659 	/* This usage of d_real() results in const dentry */
660 	return d_inode(d_real((struct dentry *) dentry, D_REAL_DATA));
661 }
662 
663 struct name_snapshot {
664 	struct qstr name;
665 	union shortname_store inline_name;
666 };
667 void take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
668 void release_dentry_name_snapshot(struct name_snapshot *);
669 
d_first_child(const struct dentry * dentry)670 static inline struct dentry *d_first_child(const struct dentry *dentry)
671 {
672 	return hlist_entry_safe(dentry->d_children.first, struct dentry, d_sib);
673 }
674 
d_next_sibling(const struct dentry * dentry)675 static inline struct dentry *d_next_sibling(const struct dentry *dentry)
676 {
677 	return hlist_entry_safe(dentry->d_sib.next, struct dentry, d_sib);
678 }
679 
680 void set_default_d_op(struct super_block *, const struct dentry_operations *);
681 struct dentry *d_make_persistent(struct dentry *, struct inode *);
682 void d_make_discardable(struct dentry *dentry);
683 
684 /* inode->i_lock must be held over that */
685 #define for_each_alias(dentry, inode) \
686 	hlist_for_each_entry(dentry, &(inode)->i_dentry, d_alias)
687 
688 #endif	/* __LINUX_DCACHE_H */
689