1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy #ifndef _ZFS_DCACHE_H
27eda14cbcSMatt Macy #define	_ZFS_DCACHE_H
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <linux/dcache.h>
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy #define	dname(dentry)	((char *)((dentry)->d_name.name))
32eda14cbcSMatt Macy #define	dlen(dentry)	((int)((dentry)->d_name.len))
33eda14cbcSMatt Macy 
34eda14cbcSMatt Macy #ifndef HAVE_D_MAKE_ROOT
35eda14cbcSMatt Macy #define	d_make_root(inode)	d_alloc_root(inode)
36eda14cbcSMatt Macy #endif /* HAVE_D_MAKE_ROOT */
37eda14cbcSMatt Macy 
3815f0b8c3SMartin Matuska #ifdef HAVE_DENTRY_D_U_ALIASES
3915f0b8c3SMartin Matuska #define	d_alias			d_u.d_alias
4015f0b8c3SMartin Matuska #endif
4115f0b8c3SMartin Matuska 
42eda14cbcSMatt Macy /*
432a58b312SMartin Matuska  * Starting from Linux 5.13, flush_dcache_page() becomes an inline function
442a58b312SMartin Matuska  * and under some configurations, may indirectly referencing GPL-only
45*3494f7c0SMartin Matuska  * symbols, e.g., cpu_feature_keys on powerpc and PageHuge on riscv.
46*3494f7c0SMartin Matuska  * Override this function when it is detected being GPL-only.
472a58b312SMartin Matuska  */
482a58b312SMartin Matuska #if defined __powerpc__ && defined HAVE_FLUSH_DCACHE_PAGE_GPL_ONLY
492a58b312SMartin Matuska #include <linux/simd_powerpc.h>
502a58b312SMartin Matuska #define	flush_dcache_page(page)	do {					\
512a58b312SMartin Matuska 		if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE) &&	\
522a58b312SMartin Matuska 		    test_bit(PG_dcache_clean, &(page)->flags))		\
532a58b312SMartin Matuska 			clear_bit(PG_dcache_clean, &(page)->flags);	\
542a58b312SMartin Matuska 	} while (0)
552a58b312SMartin Matuska #endif
56*3494f7c0SMartin Matuska /*
57*3494f7c0SMartin Matuska  * For riscv implementation, the use of PageHuge can be safely removed.
58*3494f7c0SMartin Matuska  * Because it handles pages allocated by HugeTLB, while flush_dcache_page
59*3494f7c0SMartin Matuska  * in zfs module is only called on kernel pages.
60*3494f7c0SMartin Matuska  */
61*3494f7c0SMartin Matuska #if defined __riscv && defined HAVE_FLUSH_DCACHE_PAGE_GPL_ONLY
62*3494f7c0SMartin Matuska #define	flush_dcache_page(page)	do {					\
63*3494f7c0SMartin Matuska 		if (test_bit(PG_dcache_clean, &(page)->flags))		\
64*3494f7c0SMartin Matuska 			clear_bit(PG_dcache_clean, &(page)->flags);	\
65*3494f7c0SMartin Matuska 	} while (0)
66*3494f7c0SMartin Matuska #endif
672a58b312SMartin Matuska 
682a58b312SMartin Matuska /*
69eda14cbcSMatt Macy  * 2.6.30 API change,
70eda14cbcSMatt Macy  * The const keyword was added to the 'struct dentry_operations' in
71eda14cbcSMatt Macy  * the dentry structure.  To handle this we define an appropriate
72eda14cbcSMatt Macy  * dentry_operations_t typedef which can be used.
73eda14cbcSMatt Macy  */
74eda14cbcSMatt Macy typedef const struct dentry_operations	dentry_operations_t;
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy /*
77eda14cbcSMatt Macy  * 2.6.38 API addition,
78eda14cbcSMatt Macy  * Added d_clear_d_op() helper function which clears some flags and the
79eda14cbcSMatt Macy  * registered dentry->d_op table.  This is required because d_set_d_op()
80eda14cbcSMatt Macy  * issues a warning when the dentry operations table is already set.
81eda14cbcSMatt Macy  * For the .zfs control directory to work properly we must be able to
82eda14cbcSMatt Macy  * override the default operations table and register custom .d_automount
83eda14cbcSMatt Macy  * and .d_revalidate callbacks.
84eda14cbcSMatt Macy  */
85eda14cbcSMatt Macy static inline void
d_clear_d_op(struct dentry * dentry)86eda14cbcSMatt Macy d_clear_d_op(struct dentry *dentry)
87eda14cbcSMatt Macy {
88eda14cbcSMatt Macy 	dentry->d_op = NULL;
89eda14cbcSMatt Macy 	dentry->d_flags &= ~(
90eda14cbcSMatt Macy 	    DCACHE_OP_HASH | DCACHE_OP_COMPARE |
91eda14cbcSMatt Macy 	    DCACHE_OP_REVALIDATE | DCACHE_OP_DELETE);
92eda14cbcSMatt Macy }
93eda14cbcSMatt Macy 
94dbd5678dSMartin Matuska /*
95dbd5678dSMartin Matuska  * Walk and invalidate all dentry aliases of an inode
96dbd5678dSMartin Matuska  * unless it's a mountpoint
97dbd5678dSMartin Matuska  */
98dbd5678dSMartin Matuska static inline void
zpl_d_drop_aliases(struct inode * inode)99dbd5678dSMartin Matuska zpl_d_drop_aliases(struct inode *inode)
100dbd5678dSMartin Matuska {
101dbd5678dSMartin Matuska 	struct dentry *dentry;
102dbd5678dSMartin Matuska 	spin_lock(&inode->i_lock);
103dbd5678dSMartin Matuska 	hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
104dbd5678dSMartin Matuska 		if (!IS_ROOT(dentry) && !d_mountpoint(dentry) &&
105dbd5678dSMartin Matuska 		    (dentry->d_inode == inode)) {
106dbd5678dSMartin Matuska 			d_drop(dentry);
107dbd5678dSMartin Matuska 		}
108dbd5678dSMartin Matuska 	}
109dbd5678dSMartin Matuska 	spin_unlock(&inode->i_lock);
110dbd5678dSMartin Matuska }
111eda14cbcSMatt Macy #endif /* _ZFS_DCACHE_H */
112