xref: /linux-6.15/include/linux/dcache.h (revision b2b4483b)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __LINUX_DCACHE_H
31da177e4SLinus Torvalds #define __LINUX_DCACHE_H
41da177e4SLinus Torvalds 
560063497SArun Sharma #include <linux/atomic.h>
61da177e4SLinus Torvalds #include <linux/list.h>
7aa6159abSAndy Shevchenko #include <linux/math.h>
882524746SFranck Bui-Huu #include <linux/rculist.h>
9ceb5bdc2SNick Piggin #include <linux/rculist_bl.h>
101da177e4SLinus Torvalds #include <linux/spinlock.h>
1131e6b01fSNick Piggin #include <linux/seqlock.h>
121da177e4SLinus Torvalds #include <linux/cache.h>
131da177e4SLinus Torvalds #include <linux/rcupdate.h>
1498474236SWaiman Long #include <linux/lockref.h>
15f4bcbe79SGeorge Spelvin #include <linux/stringhash.h>
16f9411ebeSIngo Molnar #include <linux/wait.h>
171da177e4SLinus Torvalds 
18cf28b486SJan Blunck struct path;
19863f144fSMiklos Szeredi struct file;
201da177e4SLinus Torvalds struct vfsmount;
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * linux/include/linux/dcache.h
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Dirent cache data structures
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * (C) Copyright 1997 Thomas Schoebel-Theuer,
281da177e4SLinus Torvalds  * with heavy changes by Linus Torvalds
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #define IS_ROOT(x) ((x) == (x)->d_parent)
321da177e4SLinus Torvalds 
3326fe5750SLinus Torvalds /* The hash is always the low bits of hash_len */
3426fe5750SLinus Torvalds #ifdef __LITTLE_ENDIAN
352bd03e49SAndrew Morton  #define HASH_LEN_DECLARE u32 hash; u32 len
36a5c21dceSWill Deacon  #define bytemask_from_count(cnt)	(~(~0ul << (cnt)*8))
3726fe5750SLinus Torvalds #else
382bd03e49SAndrew Morton  #define HASH_LEN_DECLARE u32 len; u32 hash
39a5c21dceSWill Deacon  #define bytemask_from_count(cnt)	(~(~0ul >> (cnt)*8))
4026fe5750SLinus Torvalds #endif
4126fe5750SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * "quick string" -- eases parameter passing, but more importantly
441da177e4SLinus Torvalds  * saves "metadata" about the string (ie length and the hash).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * hash comes first so it snuggles against d_parent in the
471da177e4SLinus Torvalds  * dentry.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds struct qstr {
5026fe5750SLinus Torvalds 	union {
5126fe5750SLinus Torvalds 		struct {
5226fe5750SLinus Torvalds 			HASH_LEN_DECLARE;
5326fe5750SLinus Torvalds 		};
5426fe5750SLinus Torvalds 		u64 hash_len;
5526fe5750SLinus Torvalds 	};
561da177e4SLinus Torvalds 	const unsigned char *name;
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
5926fe5750SLinus Torvalds #define QSTR_INIT(n,l) { { { .len = l } }, .name = n }
60c1feab95SAl Viro #define QSTR(n) (struct qstr)QSTR_INIT(n, strlen(n))
6126fe5750SLinus Torvalds 
62cdf01226SDavid Howells extern const struct qstr empty_name;
63cdf01226SDavid Howells extern const struct qstr slash_name;
6480e5d1ffSAl Viro extern const struct qstr dotdot_name;
65cdf01226SDavid Howells 
66c2452f32SNick Piggin /*
67c2452f32SNick Piggin  * Try to keep struct dentry aligned on 64 byte cachelines (this will
68c2452f32SNick Piggin  * give reasonable cacheline footprint with larger lines without the
69c2452f32SNick Piggin  * large memory footprint increase).
70c2452f32SNick Piggin  */
71c2452f32SNick Piggin #ifdef CONFIG_64BIT
7261bc24acSAl Viro # define DNAME_INLINE_WORDS 5 /* 192 bytes */
73c2452f32SNick Piggin #else
7444a7d7a8SNick Piggin # ifdef CONFIG_SMP
7561bc24acSAl Viro #  define DNAME_INLINE_WORDS 9 /* 128 bytes */
76da549bddSAl Viro # else
7761bc24acSAl Viro #  define DNAME_INLINE_WORDS 11 /* 128 bytes */
7844a7d7a8SNick Piggin # endif
79c2452f32SNick Piggin #endif
801da177e4SLinus Torvalds 
8161bc24acSAl Viro #define DNAME_INLINE_LEN (DNAME_INLINE_WORDS*sizeof(unsigned long))
8261bc24acSAl Viro 
8358cf9c38SAl Viro union shortname_store {
8458cf9c38SAl Viro 	unsigned char string[DNAME_INLINE_LEN];
8558cf9c38SAl Viro 	unsigned long words[DNAME_INLINE_WORDS];
8658cf9c38SAl Viro };
8758cf9c38SAl Viro 
8898474236SWaiman Long #define d_lock	d_lockref.lock
8958cf9c38SAl Viro #define d_iname d_shortname.string
9098474236SWaiman Long 
911da177e4SLinus Torvalds struct dentry {
9244a7d7a8SNick Piggin 	/* RCU lookup touched fields */
931da177e4SLinus Torvalds 	unsigned int d_flags;		/* protected by d_lock */
9426475371SAhmed S. Darwish 	seqcount_spinlock_t d_seq;	/* per dentry seqlock */
95ceb5bdc2SNick Piggin 	struct hlist_bl_node d_hash;	/* lookup hash list */
961da177e4SLinus Torvalds 	struct dentry *d_parent;	/* parent directory */
971da177e4SLinus Torvalds 	struct qstr d_name;
9844a7d7a8SNick Piggin 	struct inode *d_inode;		/* Where the name belongs to - NULL is
9944a7d7a8SNick Piggin 					 * negative */
10058cf9c38SAl Viro 	union shortname_store d_shortname;
10118a5daf0SMateusz Guzik 	/* --- cacheline 1 boundary (64 bytes) was 32 bytes ago --- */
10244a7d7a8SNick Piggin 
10344a7d7a8SNick Piggin 	/* Ref lookup also touches following */
10444a7d7a8SNick Piggin 	const struct dentry_operations *d_op;
10544a7d7a8SNick Piggin 	struct super_block *d_sb;	/* The root of the dentry tree */
10644a7d7a8SNick Piggin 	unsigned long d_time;		/* used by d_revalidate */
10744a7d7a8SNick Piggin 	void *d_fsdata;			/* fs-specific data */
10818a5daf0SMateusz Guzik 	/* --- cacheline 2 boundary (128 bytes) --- */
10918a5daf0SMateusz Guzik 	struct lockref d_lockref;	/* per-dentry lock and refcount
11018a5daf0SMateusz Guzik 					 * keep separate from RCU lookup area if
11118a5daf0SMateusz Guzik 					 * possible!
11218a5daf0SMateusz Guzik 					 */
1131da177e4SLinus Torvalds 
114d9171b93SAl Viro 	union {
1151da177e4SLinus Torvalds 		struct list_head d_lru;		/* LRU list */
116d9171b93SAl Viro 		wait_queue_head_t *d_wait;	/* in-lookup ones only */
117d9171b93SAl Viro 	};
118da549bddSAl Viro 	struct hlist_node d_sib;	/* child of parent list */
119da549bddSAl Viro 	struct hlist_head d_children;	/* our children */
1205160ee6fSEric Dumazet 	/*
121946e51f2SAl Viro 	 * d_alias and d_rcu can share memory
1225160ee6fSEric Dumazet 	 */
1235160ee6fSEric Dumazet 	union {
124946e51f2SAl Viro 		struct hlist_node d_alias;	/* inode alias list */
12594bdd655SAl Viro 		struct hlist_bl_node d_in_lookup_hash;	/* only for in-lookup ones */
1265160ee6fSEric Dumazet 	 	struct rcu_head d_rcu;
1275160ee6fSEric Dumazet 	} d_u;
128acfde6e8SAl Viro };
1291da177e4SLinus Torvalds 
130a90b9c05SIngo Molnar /*
131a90b9c05SIngo Molnar  * dentry->d_lock spinlock nesting subclasses:
132a90b9c05SIngo Molnar  *
133a90b9c05SIngo Molnar  * 0: normal
134a90b9c05SIngo Molnar  * 1: nested
135a90b9c05SIngo Molnar  */
136a90b9c05SIngo Molnar enum dentry_d_lock_class
137a90b9c05SIngo Molnar {
138a90b9c05SIngo Molnar 	DENTRY_D_LOCK_NORMAL, /* implicitly used by plain spin_lock() APIs. */
139a90b9c05SIngo Molnar 	DENTRY_D_LOCK_NESTED
140a90b9c05SIngo Molnar };
141a90b9c05SIngo Molnar 
14211b3f8aeSAmir Goldstein enum d_real_type {
14311b3f8aeSAmir Goldstein 	D_REAL_DATA,
14411b3f8aeSAmir Goldstein 	D_REAL_METADATA,
14511b3f8aeSAmir Goldstein };
14611b3f8aeSAmir Goldstein 
1471da177e4SLinus Torvalds struct dentry_operations {
1485be1fa8aSAl Viro 	int (*d_revalidate)(struct inode *, const struct qstr *,
1495be1fa8aSAl Viro 			    struct dentry *, unsigned int);
150ecf3d1f1SJeff Layton 	int (*d_weak_revalidate)(struct dentry *, unsigned int);
151da53be12SLinus Torvalds 	int (*d_hash)(const struct dentry *, struct qstr *);
1526fa67e70SAl Viro 	int (*d_compare)(const struct dentry *,
153621e155aSNick Piggin 			unsigned int, const char *, const struct qstr *);
154fe15ce44SNick Piggin 	int (*d_delete)(const struct dentry *);
155285b102dSMiklos Szeredi 	int (*d_init)(struct dentry *);
1561da177e4SLinus Torvalds 	void (*d_release)(struct dentry *);
157f0023bc6SSage Weil 	void (*d_prune)(struct dentry *);
1581da177e4SLinus Torvalds 	void (*d_iput)(struct dentry *, struct inode *);
159c23fbb6bSEric Dumazet 	char *(*d_dname)(struct dentry *, char *, int);
1609875cf80SDavid Howells 	struct vfsmount *(*d_automount)(struct path *);
161fb5f51c7SIan Kent 	int (*d_manage)(const struct path *, bool);
16211b3f8aeSAmir Goldstein 	struct dentry *(*d_real)(struct dentry *, enum d_real_type type);
16330d61efeSAl Viro 	bool (*d_unalias_trylock)(const struct dentry *);
16430d61efeSAl Viro 	void (*d_unalias_unlock)(const struct dentry *);
16544a7d7a8SNick Piggin } ____cacheline_aligned;
1661da177e4SLinus Torvalds 
1675eef7fa9SNick Piggin /*
1685eef7fa9SNick Piggin  * Locking rules for dentry_operations callbacks are to be found in
169ec23eb54SMauro Carvalho Chehab  * Documentation/filesystems/locking.rst. Keep it updated!
1705eef7fa9SNick Piggin  *
1715c437fa2SMauro Carvalho Chehab  * FUrther descriptions are found in Documentation/filesystems/vfs.rst.
172621e155aSNick Piggin  * Keep it updated too!
1731da177e4SLinus Torvalds  */
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* d_flags entries */
176*b2b4483bSOmar Sandoval enum dentry_flags {
177*b2b4483bSOmar Sandoval 	DCACHE_OP_HASH			= BIT(0),
178*b2b4483bSOmar Sandoval 	DCACHE_OP_COMPARE		= BIT(1),
179*b2b4483bSOmar Sandoval 	DCACHE_OP_REVALIDATE		= BIT(2),
180*b2b4483bSOmar Sandoval 	DCACHE_OP_DELETE		= BIT(3),
181*b2b4483bSOmar Sandoval 	DCACHE_OP_PRUNE			= BIT(4),
182*b2b4483bSOmar Sandoval 	/*
183*b2b4483bSOmar Sandoval 	 * This dentry is possibly not currently connected to the dcache tree,
184*b2b4483bSOmar Sandoval 	 * in which case its parent will either be itself, or will have this
185*b2b4483bSOmar Sandoval 	 * flag as well.  nfsd will not use a dentry with this bit set, but will
186*b2b4483bSOmar Sandoval 	 * first endeavour to clear the bit either by discovering that it is
187*b2b4483bSOmar Sandoval 	 * connected, or by performing lookup operations.  Any filesystem which
188*b2b4483bSOmar Sandoval 	 * supports nfsd_operations MUST have a lookup function which, if it
189*b2b4483bSOmar Sandoval 	 * finds a directory inode with a DCACHE_DISCONNECTED dentry, will
190*b2b4483bSOmar Sandoval 	 * d_move that dentry into place and return that dentry rather than the
191*b2b4483bSOmar Sandoval 	 * passed one, typically using d_splice_alias.
192*b2b4483bSOmar Sandoval 	 */
193*b2b4483bSOmar Sandoval 	DCACHE_DISCONNECTED		= BIT(5),
194*b2b4483bSOmar Sandoval 	DCACHE_REFERENCED		= BIT(6),	/* Recently used, don't discard. */
195*b2b4483bSOmar Sandoval 	DCACHE_DONTCACHE		= BIT(7),	/* Purge from memory on final dput() */
196*b2b4483bSOmar Sandoval 	DCACHE_CANT_MOUNT		= BIT(8),
197*b2b4483bSOmar Sandoval 	DCACHE_GENOCIDE			= BIT(9),
198*b2b4483bSOmar Sandoval 	DCACHE_SHRINK_LIST		= BIT(10),
199*b2b4483bSOmar Sandoval 	DCACHE_OP_WEAK_REVALIDATE	= BIT(11),
200*b2b4483bSOmar Sandoval 	/*
201*b2b4483bSOmar Sandoval 	 * this dentry has been "silly renamed" and has to be deleted on the
202*b2b4483bSOmar Sandoval 	 * last dput()
203*b2b4483bSOmar Sandoval 	 */
204*b2b4483bSOmar Sandoval 	DCACHE_NFSFS_RENAMED		= BIT(12),
205*b2b4483bSOmar Sandoval 	DCACHE_FSNOTIFY_PARENT_WATCHED	= BIT(13),	/* Parent inode is watched by some fsnotify listener */
206*b2b4483bSOmar Sandoval 	DCACHE_DENTRY_KILLED		= BIT(14),
207*b2b4483bSOmar Sandoval 	DCACHE_MOUNTED			= BIT(15),	/* is a mountpoint */
208*b2b4483bSOmar Sandoval 	DCACHE_NEED_AUTOMOUNT		= BIT(16),	/* handle automount on this dir */
209*b2b4483bSOmar Sandoval 	DCACHE_MANAGE_TRANSIT		= BIT(17),	/* manage transit from this dirent */
210*b2b4483bSOmar Sandoval 	DCACHE_LRU_LIST			= BIT(18),
211*b2b4483bSOmar Sandoval 	DCACHE_ENTRY_TYPE		= (7 << 19),	/* bits 19..21 are for storing type: */
212*b2b4483bSOmar Sandoval 	DCACHE_MISS_TYPE		= (0 << 19),	/* Negative dentry */
213*b2b4483bSOmar Sandoval 	DCACHE_WHITEOUT_TYPE		= (1 << 19),	/* Whiteout dentry (stop pathwalk) */
214*b2b4483bSOmar Sandoval 	DCACHE_DIRECTORY_TYPE		= (2 << 19),	/* Normal directory */
215*b2b4483bSOmar Sandoval 	DCACHE_AUTODIR_TYPE		= (3 << 19),	/* Lookupless directory (presumed automount) */
216*b2b4483bSOmar Sandoval 	DCACHE_REGULAR_TYPE		= (4 << 19),	/* Regular file type */
217*b2b4483bSOmar Sandoval 	DCACHE_SPECIAL_TYPE		= (5 << 19),	/* Other file type */
218*b2b4483bSOmar Sandoval 	DCACHE_SYMLINK_TYPE		= (6 << 19),	/* Symlink */
219*b2b4483bSOmar Sandoval 	DCACHE_NOKEY_NAME		= BIT(22),	/* Encrypted name encoded without key */
220*b2b4483bSOmar Sandoval 	DCACHE_OP_REAL			= BIT(23),
221*b2b4483bSOmar Sandoval 	DCACHE_PAR_LOOKUP		= BIT(24),	/* being looked up (with parent locked shared) */
222*b2b4483bSOmar Sandoval 	DCACHE_DENTRY_CURSOR		= BIT(25),
223*b2b4483bSOmar Sandoval 	DCACHE_NORCU			= BIT(26),	/* No RCU delay for freeing */
224*b2b4483bSOmar Sandoval };
2255f57cbccSNick Piggin 
2269875cf80SDavid Howells #define DCACHE_MANAGED_DENTRY \
227cc53ce53SDavid Howells 	(DCACHE_MOUNTED|DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT)
2289875cf80SDavid Howells 
22974c3cbe3SAl Viro extern seqlock_t rename_lock;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /*
2321da177e4SLinus Torvalds  * These are the low-level FS interfaces to the dcache..
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds extern void d_instantiate(struct dentry *, struct inode *);
2351e2e547aSAl Viro extern void d_instantiate_new(struct dentry *, struct inode *);
236789680d1SNick Piggin extern void __d_drop(struct dentry *dentry);
237789680d1SNick Piggin extern void d_drop(struct dentry *dentry);
2381da177e4SLinus Torvalds extern void d_delete(struct dentry *);
239fb045adbSNick Piggin extern void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /* allocate/de-allocate */
2421da177e4SLinus Torvalds extern struct dentry * d_alloc(struct dentry *, const struct qstr *);
243f9c34674SMiklos Szeredi extern struct dentry * d_alloc_anon(struct super_block *);
244d9171b93SAl Viro extern struct dentry * d_alloc_parallel(struct dentry *, const struct qstr *,
245d9171b93SAl Viro 					wait_queue_head_t *);
2461da177e4SLinus Torvalds extern struct dentry * d_splice_alias(struct inode *, struct dentry *);
247e45b590bSChristoph Hellwig extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
2484f48d5daSXiubo Li extern bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
2494f48d5daSXiubo Li 			const struct qstr *name);
25046f72b34SSage Weil extern struct dentry *d_find_any_alias(struct inode *inode);
2514ea3ada2SChristoph Hellwig extern struct dentry * d_obtain_alias(struct inode *);
2521a0a397eSJ. Bruce Fields extern struct dentry * d_obtain_root(struct inode *);
2531da177e4SLinus Torvalds extern void shrink_dcache_sb(struct super_block *);
2541da177e4SLinus Torvalds extern void shrink_dcache_parent(struct dentry *);
2555542aa2fSEric W. Biederman extern void d_invalidate(struct dentry *);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /* only used at mount-time */
258adc0e91aSAl Viro extern struct dentry * d_make_root(struct inode *);
2591da177e4SLinus Torvalds 
260771eb4feSKent Overstreet extern void d_mark_tmpfile(struct file *, struct inode *);
261863f144fSMiklos Szeredi extern void d_tmpfile(struct file *, struct inode *);
26260545d0dSAl Viro 
2631da177e4SLinus Torvalds extern struct dentry *d_find_alias(struct inode *);
2641da177e4SLinus Torvalds extern void d_prune_aliases(struct inode *);
2651da177e4SLinus Torvalds 
266bca585d2SAl Viro extern struct dentry *d_find_alias_rcu(struct inode *);
267bca585d2SAl Viro 
2681da177e4SLinus Torvalds /* test whether we have any submounts in a subdir tree */
26901619491SIan Kent extern int path_has_submounts(const struct path *);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /*
2721da177e4SLinus Torvalds  * This adds the entry to the hash queues.
2731da177e4SLinus Torvalds  */
2741da177e4SLinus Torvalds extern void d_rehash(struct dentry *);
2751da177e4SLinus Torvalds 
27634d0d19dSAl Viro extern void d_add(struct dentry *, struct inode *);
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds /* used for rename() and baskets */
2791da177e4SLinus Torvalds extern void d_move(struct dentry *, struct dentry *);
280da1ce067SMiklos Szeredi extern void d_exchange(struct dentry *, struct dentry *);
281e2761a11SOGAWA Hirofumi extern struct dentry *d_ancestor(struct dentry *, struct dentry *);
2821da177e4SLinus Torvalds 
283da2d8455SAl Viro extern struct dentry *d_lookup(const struct dentry *, const struct qstr *);
2843e7e241fSEric W. Biederman extern struct dentry *d_hash_and_lookup(struct dentry *, struct qstr *);
28531e6b01fSNick Piggin 
d_count(const struct dentry * dentry)28624924a20SPeng Tao static inline unsigned d_count(const struct dentry *dentry)
28784d08fa8SAl Viro {
28898474236SWaiman Long 	return dentry->d_lockref.count;
28984d08fa8SAl Viro }
29084d08fa8SAl Viro 
291f378ec4eSMateusz Guzik ino_t d_parent_ino(struct dentry *dentry);
292f378ec4eSMateusz Guzik 
293c23fbb6bSEric Dumazet /*
294c23fbb6bSEric Dumazet  * helper function for dentry_operations.d_dname() members
295c23fbb6bSEric Dumazet  */
2960f60d288SAl Viro extern __printf(3, 4)
2970f60d288SAl Viro char *dynamic_dname(char *, int, const char *, ...);
298c23fbb6bSEric Dumazet 
29902125a82SAl Viro extern char *__d_path(const struct path *, const struct path *, char *, int);
30002125a82SAl Viro extern char *d_absolute_path(const struct path *, char *, int);
30120d4fdc1SJan Engelhardt extern char *d_path(const struct path *, char *, int);
302a2bbe664SAl Viro extern char *dentry_path_raw(const struct dentry *, char *, int);
303a2bbe664SAl Viro extern char *dentry_path(const struct dentry *, char *, int);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /* Allocation counts.. */
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /**
3081b6ae9f6SVegard Nossum  * dget_dlock -	get a reference to a dentry
3091da177e4SLinus Torvalds  * @dentry: dentry to get a reference to
3101da177e4SLinus Torvalds  *
3111b6ae9f6SVegard Nossum  * Given a live dentry, increment the reference count and return the dentry.
3121b6ae9f6SVegard Nossum  * Caller must hold @dentry->d_lock.  Making sure that dentry is alive is
3131b6ae9f6SVegard Nossum  * caller's resonsibility.  There are many conditions sufficient to guarantee
3141b6ae9f6SVegard Nossum  * that; e.g. anything with non-negative refcount is alive, so's anything
3151b6ae9f6SVegard Nossum  * hashed, anything positive, anyone's parent, etc.
3161da177e4SLinus Torvalds  */
dget_dlock(struct dentry * dentry)317b7ab39f6SNick Piggin static inline struct dentry *dget_dlock(struct dentry *dentry)
318b7ab39f6SNick Piggin {
31998474236SWaiman Long 	dentry->d_lockref.count++;
320b7ab39f6SNick Piggin 	return dentry;
321b7ab39f6SNick Piggin }
3222fd6b7f5SNick Piggin 
3231b6ae9f6SVegard Nossum 
3241b6ae9f6SVegard Nossum /**
3251b6ae9f6SVegard Nossum  * dget - get a reference to a dentry
3261b6ae9f6SVegard Nossum  * @dentry: dentry to get a reference to
3271b6ae9f6SVegard Nossum  *
3281b6ae9f6SVegard Nossum  * Given a dentry or %NULL pointer increment the reference count
3291b6ae9f6SVegard Nossum  * if appropriate and return the dentry.  A dentry will not be
3301b6ae9f6SVegard Nossum  * destroyed when it has references.  Conversely, a dentry with
3311b6ae9f6SVegard Nossum  * no references can disappear for any number of reasons, starting
3321b6ae9f6SVegard Nossum  * with memory pressure.  In other words, that primitive is
3331b6ae9f6SVegard Nossum  * used to clone an existing reference; using it on something with
3341b6ae9f6SVegard Nossum  * zero refcount is a bug.
3351b6ae9f6SVegard Nossum  *
3361b6ae9f6SVegard Nossum  * NOTE: it will spin if @dentry->d_lock is held.  From the deadlock
3371b6ae9f6SVegard Nossum  * avoidance point of view it is equivalent to spin_lock()/increment
3381b6ae9f6SVegard Nossum  * refcount/spin_unlock(), so calling it under @dentry->d_lock is
3391b6ae9f6SVegard Nossum  * always a bug; so's calling it under ->d_lock on any of its descendents.
3401b6ae9f6SVegard Nossum  *
3411b6ae9f6SVegard Nossum  */
dget(struct dentry * dentry)3421da177e4SLinus Torvalds static inline struct dentry *dget(struct dentry *dentry)
3431da177e4SLinus Torvalds {
34498474236SWaiman Long 	if (dentry)
34598474236SWaiman Long 		lockref_get(&dentry->d_lockref);
3461da177e4SLinus Torvalds 	return dentry;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
349b7ab39f6SNick Piggin extern struct dentry *dget_parent(struct dentry *dentry);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds /**
3521da177e4SLinus Torvalds  * d_unhashed - is dentry hashed
3531da177e4SLinus Torvalds  * @dentry: entry to check
3541da177e4SLinus Torvalds  *
3551da177e4SLinus Torvalds  * Returns true if the dentry passed is not currently hashed.
3561da177e4SLinus Torvalds  */
d_unhashed(const struct dentry * dentry)357f0d3b3deSAl Viro static inline int d_unhashed(const struct dentry *dentry)
3581da177e4SLinus Torvalds {
359dea3667bSLinus Torvalds 	return hlist_bl_unhashed(&dentry->d_hash);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
d_unlinked(const struct dentry * dentry)362f0d3b3deSAl Viro static inline int d_unlinked(const struct dentry *dentry)
363f3da392eSAlexey Dobriyan {
364f3da392eSAlexey Dobriyan 	return d_unhashed(dentry) && !IS_ROOT(dentry);
365f3da392eSAlexey Dobriyan }
366f3da392eSAlexey Dobriyan 
cant_mount(const struct dentry * dentry)367f0d3b3deSAl Viro static inline int cant_mount(const struct dentry *dentry)
368d83c49f3SAl Viro {
369d83c49f3SAl Viro 	return (dentry->d_flags & DCACHE_CANT_MOUNT);
370d83c49f3SAl Viro }
371d83c49f3SAl Viro 
dont_mount(struct dentry * dentry)372d83c49f3SAl Viro static inline void dont_mount(struct dentry *dentry)
373d83c49f3SAl Viro {
374d83c49f3SAl Viro 	spin_lock(&dentry->d_lock);
375d83c49f3SAl Viro 	dentry->d_flags |= DCACHE_CANT_MOUNT;
376d83c49f3SAl Viro 	spin_unlock(&dentry->d_lock);
377d83c49f3SAl Viro }
378d83c49f3SAl Viro 
37945f78b0aSSebastian Andrzej Siewior extern void __d_lookup_unhash_wake(struct dentry *dentry);
38085c7f810SAl Viro 
d_in_lookup(const struct dentry * dentry)3814ded097bSNeilBrown static inline int d_in_lookup(const struct dentry *dentry)
38285c7f810SAl Viro {
38385c7f810SAl Viro 	return dentry->d_flags & DCACHE_PAR_LOOKUP;
38485c7f810SAl Viro }
38585c7f810SAl Viro 
d_lookup_done(struct dentry * dentry)38685c7f810SAl Viro static inline void d_lookup_done(struct dentry *dentry)
38785c7f810SAl Viro {
38845f78b0aSSebastian Andrzej Siewior 	if (unlikely(d_in_lookup(dentry)))
38945f78b0aSSebastian Andrzej Siewior 		__d_lookup_unhash_wake(dentry);
39085c7f810SAl Viro }
39185c7f810SAl Viro 
3921da177e4SLinus Torvalds extern void dput(struct dentry *);
3931da177e4SLinus Torvalds 
d_managed(const struct dentry * dentry)394f0d3b3deSAl Viro static inline bool d_managed(const struct dentry *dentry)
395cc53ce53SDavid Howells {
396cc53ce53SDavid Howells 	return dentry->d_flags & DCACHE_MANAGED_DENTRY;
397cc53ce53SDavid Howells }
398cc53ce53SDavid Howells 
d_mountpoint(const struct dentry * dentry)399f0d3b3deSAl Viro static inline bool d_mountpoint(const struct dentry *dentry)
4001da177e4SLinus Torvalds {
4015f57cbccSNick Piggin 	return dentry->d_flags & DCACHE_MOUNTED;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
404b18825a7SDavid Howells /*
405b18825a7SDavid Howells  * Directory cache entry type accessor functions.
406b18825a7SDavid Howells  */
__d_entry_type(const struct dentry * dentry)407b18825a7SDavid Howells static inline unsigned __d_entry_type(const struct dentry *dentry)
408b18825a7SDavid Howells {
409a528aca7SAl Viro 	return dentry->d_flags & DCACHE_ENTRY_TYPE;
410b18825a7SDavid Howells }
411b18825a7SDavid Howells 
d_is_miss(const struct dentry * dentry)412e7f7d225SDavid Howells static inline bool d_is_miss(const struct dentry *dentry)
413e7f7d225SDavid Howells {
414e7f7d225SDavid Howells 	return __d_entry_type(dentry) == DCACHE_MISS_TYPE;
415e7f7d225SDavid Howells }
416e7f7d225SDavid Howells 
d_is_whiteout(const struct dentry * dentry)417e7f7d225SDavid Howells static inline bool d_is_whiteout(const struct dentry *dentry)
418e7f7d225SDavid Howells {
419e7f7d225SDavid Howells 	return __d_entry_type(dentry) == DCACHE_WHITEOUT_TYPE;
420e7f7d225SDavid Howells }
421e7f7d225SDavid Howells 
d_can_lookup(const struct dentry * dentry)42244b1d530SMiklos Szeredi static inline bool d_can_lookup(const struct dentry *dentry)
423b18825a7SDavid Howells {
424b18825a7SDavid Howells 	return __d_entry_type(dentry) == DCACHE_DIRECTORY_TYPE;
425b18825a7SDavid Howells }
426b18825a7SDavid Howells 
d_is_autodir(const struct dentry * dentry)427b18825a7SDavid Howells static inline bool d_is_autodir(const struct dentry *dentry)
428b18825a7SDavid Howells {
429b18825a7SDavid Howells 	return __d_entry_type(dentry) == DCACHE_AUTODIR_TYPE;
430b18825a7SDavid Howells }
431b18825a7SDavid Howells 
d_is_dir(const struct dentry * dentry)43244b1d530SMiklos Szeredi static inline bool d_is_dir(const struct dentry *dentry)
43344b1d530SMiklos Szeredi {
43444b1d530SMiklos Szeredi 	return d_can_lookup(dentry) || d_is_autodir(dentry);
43544b1d530SMiklos Szeredi }
43644b1d530SMiklos Szeredi 
d_is_symlink(const struct dentry * dentry)437b18825a7SDavid Howells static inline bool d_is_symlink(const struct dentry *dentry)
438b18825a7SDavid Howells {
439b18825a7SDavid Howells 	return __d_entry_type(dentry) == DCACHE_SYMLINK_TYPE;
440b18825a7SDavid Howells }
441b18825a7SDavid Howells 
d_is_reg(const struct dentry * dentry)44244bdb5e5SDavid Howells static inline bool d_is_reg(const struct dentry *dentry)
44344bdb5e5SDavid Howells {
44444bdb5e5SDavid Howells 	return __d_entry_type(dentry) == DCACHE_REGULAR_TYPE;
44544bdb5e5SDavid Howells }
44644bdb5e5SDavid Howells 
d_is_special(const struct dentry * dentry)44744bdb5e5SDavid Howells static inline bool d_is_special(const struct dentry *dentry)
44844bdb5e5SDavid Howells {
44944bdb5e5SDavid Howells 	return __d_entry_type(dentry) == DCACHE_SPECIAL_TYPE;
45044bdb5e5SDavid Howells }
45144bdb5e5SDavid Howells 
d_is_file(const struct dentry * dentry)452b18825a7SDavid Howells static inline bool d_is_file(const struct dentry *dentry)
453b18825a7SDavid Howells {
45444bdb5e5SDavid Howells 	return d_is_reg(dentry) || d_is_special(dentry);
455b18825a7SDavid Howells }
456b18825a7SDavid Howells 
d_is_negative(const struct dentry * dentry)457b18825a7SDavid Howells static inline bool d_is_negative(const struct dentry *dentry)
458b18825a7SDavid Howells {
459e7f7d225SDavid Howells 	// TODO: check d_is_whiteout(dentry) also.
460e7f7d225SDavid Howells 	return d_is_miss(dentry);
461b18825a7SDavid Howells }
462b18825a7SDavid Howells 
d_flags_negative(unsigned flags)463d41efb52SAl Viro static inline bool d_flags_negative(unsigned flags)
464d41efb52SAl Viro {
465d41efb52SAl Viro 	return (flags & DCACHE_ENTRY_TYPE) == DCACHE_MISS_TYPE;
466d41efb52SAl Viro }
467d41efb52SAl Viro 
d_is_positive(const struct dentry * dentry)468b18825a7SDavid Howells static inline bool d_is_positive(const struct dentry *dentry)
469b18825a7SDavid Howells {
470b18825a7SDavid Howells 	return !d_is_negative(dentry);
471b18825a7SDavid Howells }
472b18825a7SDavid Howells 
473525d27b2SDavid Howells /**
474525d27b2SDavid Howells  * d_really_is_negative - Determine if a dentry is really negative (ignoring fallthroughs)
475525d27b2SDavid Howells  * @dentry: The dentry in question
476525d27b2SDavid Howells  *
477525d27b2SDavid Howells  * Returns true if the dentry represents either an absent name or a name that
478525d27b2SDavid Howells  * doesn't map to an inode (ie. ->d_inode is NULL).  The dentry could represent
479525d27b2SDavid Howells  * a true miss, a whiteout that isn't represented by a 0,0 chardev or a
480525d27b2SDavid Howells  * fallthrough marker in an opaque directory.
481525d27b2SDavid Howells  *
482525d27b2SDavid Howells  * Note!  (1) This should be used *only* by a filesystem to examine its own
483525d27b2SDavid Howells  * dentries.  It should not be used to look at some other filesystem's
484525d27b2SDavid Howells  * dentries.  (2) It should also be used in combination with d_inode() to get
485525d27b2SDavid Howells  * the inode.  (3) The dentry may have something attached to ->d_lower and the
486525d27b2SDavid Howells  * type field of the flags may be set to something other than miss or whiteout.
487525d27b2SDavid Howells  */
d_really_is_negative(const struct dentry * dentry)488525d27b2SDavid Howells static inline bool d_really_is_negative(const struct dentry *dentry)
489525d27b2SDavid Howells {
490525d27b2SDavid Howells 	return dentry->d_inode == NULL;
491525d27b2SDavid Howells }
492525d27b2SDavid Howells 
493525d27b2SDavid Howells /**
494525d27b2SDavid Howells  * d_really_is_positive - Determine if a dentry is really positive (ignoring fallthroughs)
495525d27b2SDavid Howells  * @dentry: The dentry in question
496525d27b2SDavid Howells  *
497525d27b2SDavid Howells  * Returns true if the dentry represents a name that maps to an inode
498525d27b2SDavid Howells  * (ie. ->d_inode is not NULL).  The dentry might still represent a whiteout if
499525d27b2SDavid Howells  * that is represented on medium as a 0,0 chardev.
500525d27b2SDavid Howells  *
501525d27b2SDavid Howells  * Note!  (1) This should be used *only* by a filesystem to examine its own
502525d27b2SDavid Howells  * dentries.  It should not be used to look at some other filesystem's
503525d27b2SDavid Howells  * dentries.  (2) It should also be used in combination with d_inode() to get
504525d27b2SDavid Howells  * the inode.
505525d27b2SDavid Howells  */
d_really_is_positive(const struct dentry * dentry)506525d27b2SDavid Howells static inline bool d_really_is_positive(const struct dentry *dentry)
507525d27b2SDavid Howells {
508525d27b2SDavid Howells 	return dentry->d_inode != NULL;
509525d27b2SDavid Howells }
510525d27b2SDavid Howells 
simple_positive(const struct dentry * dentry)5114ded097bSNeilBrown static inline int simple_positive(const struct dentry *dentry)
512dc3f4198SAl Viro {
513dc3f4198SAl Viro 	return d_really_is_positive(dentry) && !d_unhashed(dentry);
514dc3f4198SAl Viro }
515dc3f4198SAl Viro 
51652e66823SKaixiong Yu unsigned long vfs_pressure_ratio(unsigned long val);
517155e35d4SDavid Howells 
518155e35d4SDavid Howells /**
519155e35d4SDavid Howells  * d_inode - Get the actual inode of this dentry
520155e35d4SDavid Howells  * @dentry: The dentry to query
521155e35d4SDavid Howells  *
522155e35d4SDavid Howells  * This is the helper normal filesystems should use to get at their own inodes
523155e35d4SDavid Howells  * in their own dentries and ignore the layering superimposed upon them.
524155e35d4SDavid Howells  */
d_inode(const struct dentry * dentry)525155e35d4SDavid Howells static inline struct inode *d_inode(const struct dentry *dentry)
526155e35d4SDavid Howells {
527155e35d4SDavid Howells 	return dentry->d_inode;
528155e35d4SDavid Howells }
529155e35d4SDavid Howells 
530155e35d4SDavid Howells /**
53166702eb5SMark Rutland  * d_inode_rcu - Get the actual inode of this dentry with READ_ONCE()
532155e35d4SDavid Howells  * @dentry: The dentry to query
533155e35d4SDavid Howells  *
534155e35d4SDavid Howells  * This is the helper normal filesystems should use to get at their own inodes
535155e35d4SDavid Howells  * in their own dentries and ignore the layering superimposed upon them.
536155e35d4SDavid Howells  */
d_inode_rcu(const struct dentry * dentry)537155e35d4SDavid Howells static inline struct inode *d_inode_rcu(const struct dentry *dentry)
538155e35d4SDavid Howells {
53966702eb5SMark Rutland 	return READ_ONCE(dentry->d_inode);
540155e35d4SDavid Howells }
541155e35d4SDavid Howells 
542155e35d4SDavid Howells /**
543155e35d4SDavid Howells  * d_backing_inode - Get upper or lower inode we should be using
544155e35d4SDavid Howells  * @upper: The upper layer
545155e35d4SDavid Howells  *
546155e35d4SDavid Howells  * This is the helper that should be used to get at the inode that will be used
547155e35d4SDavid Howells  * if this dentry were to be opened as a file.  The inode may be on the upper
548155e35d4SDavid Howells  * dentry or it may be on a lower dentry pinned by the upper.
549155e35d4SDavid Howells  *
550155e35d4SDavid Howells  * Normal filesystems should not use this to access their own inodes.
551155e35d4SDavid Howells  */
d_backing_inode(const struct dentry * upper)552155e35d4SDavid Howells static inline struct inode *d_backing_inode(const struct dentry *upper)
553155e35d4SDavid Howells {
554155e35d4SDavid Howells 	struct inode *inode = upper->d_inode;
555155e35d4SDavid Howells 
556155e35d4SDavid Howells 	return inode;
557155e35d4SDavid Howells }
558155e35d4SDavid Howells 
559155e35d4SDavid Howells /**
560e698b8a4SMiklos Szeredi  * d_real - Return the real dentry
561e698b8a4SMiklos Szeredi  * @dentry: the dentry to query
56211b3f8aeSAmir Goldstein  * @type: the type of real dentry (data or metadata)
563e698b8a4SMiklos Szeredi  *
56403440c4eSMasahiro Yamada  * If dentry is on a union/overlay, then return the underlying, real dentry.
565e698b8a4SMiklos Szeredi  * Otherwise return the dentry itself.
566e698b8a4SMiklos Szeredi  *
5675c437fa2SMauro Carvalho Chehab  * See also: Documentation/filesystems/vfs.rst
568e698b8a4SMiklos Szeredi  */
d_real(struct dentry * dentry,enum d_real_type type)56911b3f8aeSAmir Goldstein static inline struct dentry *d_real(struct dentry *dentry, enum d_real_type type)
570d101a125SMiklos Szeredi {
571d101a125SMiklos Szeredi 	if (unlikely(dentry->d_flags & DCACHE_OP_REAL))
57211b3f8aeSAmir Goldstein 		return dentry->d_op->d_real(dentry, type);
573d101a125SMiklos Szeredi 	else
574d101a125SMiklos Szeredi 		return dentry;
575d101a125SMiklos Szeredi }
576d101a125SMiklos Szeredi 
577a1180844SMiklos Szeredi /**
57811b3f8aeSAmir Goldstein  * d_real_inode - Return the real inode hosting the data
579a1180844SMiklos Szeredi  * @dentry: The dentry to query
580a1180844SMiklos Szeredi  *
58103440c4eSMasahiro Yamada  * If dentry is on a union/overlay, then return the underlying, real inode.
582a1180844SMiklos Szeredi  * Otherwise return d_inode().
583a1180844SMiklos Szeredi  */
d_real_inode(const struct dentry * dentry)5847b1742ebSMiklos Szeredi static inline struct inode *d_real_inode(const struct dentry *dentry)
585a1180844SMiklos Szeredi {
5867b1742ebSMiklos Szeredi 	/* This usage of d_real() results in const dentry */
58711b3f8aeSAmir Goldstein 	return d_inode(d_real((struct dentry *) dentry, D_REAL_DATA));
588a1180844SMiklos Szeredi }
589a1180844SMiklos Szeredi 
59049d31c2fSAl Viro struct name_snapshot {
591230c6402SAl Viro 	struct qstr name;
59258cf9c38SAl Viro 	union shortname_store inline_name;
59349d31c2fSAl Viro };
59449d31c2fSAl Viro void take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
59549d31c2fSAl Viro void release_dentry_name_snapshot(struct name_snapshot *);
59654d5ca87SMiklos Szeredi 
d_first_child(const struct dentry * dentry)597da549bddSAl Viro static inline struct dentry *d_first_child(const struct dentry *dentry)
598da549bddSAl Viro {
599da549bddSAl Viro 	return hlist_entry_safe(dentry->d_children.first, struct dentry, d_sib);
600da549bddSAl Viro }
601da549bddSAl Viro 
d_next_sibling(const struct dentry * dentry)602da549bddSAl Viro static inline struct dentry *d_next_sibling(const struct dentry *dentry)
603da549bddSAl Viro {
604da549bddSAl Viro 	return hlist_entry_safe(dentry->d_sib.next, struct dentry, d_sib);
605da549bddSAl Viro }
606da549bddSAl Viro 
6071da177e4SLinus Torvalds #endif	/* __LINUX_DCACHE_H */
608