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