xref: /freebsd/sys/contrib/openzfs/include/os/linux/kernel/linux/xattr_compat.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
25  */
26 
27 #ifndef _ZFS_XATTR_H
28 #define	_ZFS_XATTR_H
29 
30 #include <linux/posix_acl_xattr.h>
31 
32 /*
33  * 2.6.35 API change,
34  * The const keyword was added to the 'struct xattr_handler' in the
35  * generic Linux super_block structure.  To handle this we define an
36  * appropriate xattr_handler_t typedef which can be used.  This was
37  * the preferred solution because it keeps the code clean and readable.
38  */
39 typedef const struct xattr_handler	xattr_handler_t;
40 
41 /*
42  * 4.5 API change,
43  */
44 #define	ZPL_XATTR_LIST_WRAPPER(fn)					\
45 static bool								\
46 fn(struct dentry *dentry)						\
47 {									\
48 	return (!!__ ## fn(dentry->d_inode, NULL, 0, NULL, 0));		\
49 }
50 
51 #ifdef HAVE_XATTR_GET_DENTRY_INODE_FLAGS
52 /*
53  * Android API change,
54  * The xattr_handler->get() callback also takes a flags arg.
55  */
56 #define	ZPL_XATTR_GET_WRAPPER(fn)					\
57 static int								\
58 fn(const struct xattr_handler *handler, struct dentry *dentry,		\
59     struct inode *inode, const char *name, void *buffer,		\
60     size_t size, int flags)						\
61 {									\
62 	return (__ ## fn(inode, name, buffer, size));			\
63 }
64 #else
65 #define	ZPL_XATTR_GET_WRAPPER(fn)					\
66 static int								\
67 fn(const struct xattr_handler *handler, struct dentry *dentry,		\
68     struct inode *inode, const char *name, void *buffer, size_t size)	\
69 {									\
70 	return (__ ## fn(inode, name, buffer, size));			\
71 }
72 #endif
73 
74 /*
75  * 6.3 API change,
76  * The xattr_handler->set() callback was changed to take the
77  * struct mnt_idmap* as the first arg, to support idmapped
78  * mounts.
79  */
80 #if defined(HAVE_XATTR_SET_IDMAP)
81 #define	ZPL_XATTR_SET_WRAPPER(fn)					\
82 static int								\
83 fn(const struct xattr_handler *handler, struct mnt_idmap *user_ns,	\
84     struct dentry *dentry, struct inode *inode, const char *name,	\
85     const void *buffer, size_t size, int flags)	\
86 {									\
87 	return (__ ## fn(user_ns, inode, name, buffer, size, flags));	\
88 }
89 /*
90  * 5.12 API change,
91  * The xattr_handler->set() callback was changed to take the
92  * struct user_namespace* as the first arg, to support idmapped
93  * mounts.
94  */
95 #elif defined(HAVE_XATTR_SET_USERNS)
96 #define	ZPL_XATTR_SET_WRAPPER(fn)					\
97 static int								\
98 fn(const struct xattr_handler *handler, struct user_namespace *user_ns, \
99     struct dentry *dentry, struct inode *inode, const char *name,	\
100     const void *buffer, size_t size, int flags)	\
101 {									\
102 	return (__ ## fn(user_ns, inode, name, buffer, size, flags));	\
103 }
104 /*
105  * 4.7 API change,
106  * The xattr_handler->set() callback was changed to take a both dentry and
107  * inode, because the dentry might not be attached to an inode yet.
108  */
109 #elif defined(HAVE_XATTR_SET_DENTRY_INODE)
110 #define	ZPL_XATTR_SET_WRAPPER(fn)					\
111 static int								\
112 fn(const struct xattr_handler *handler, struct dentry *dentry,		\
113     struct inode *inode, const char *name, const void *buffer,		\
114     size_t size, int flags)						\
115 {									\
116 	return (__ ## fn(kcred->user_ns, inode, name, buffer, size, flags));\
117 }
118 #else
119 #error "Unsupported kernel"
120 #endif
121 
122 /*
123  * Linux 3.7 API change. posix_acl_{from,to}_xattr gained the user_ns
124  * parameter.  All callers are expected to pass the &init_user_ns which
125  * is available through the init credential (kcred).
126  */
127 static inline struct posix_acl *
zpl_acl_from_xattr(const void * value,int size)128 zpl_acl_from_xattr(const void *value, int size)
129 {
130 	return (posix_acl_from_xattr(kcred->user_ns, value, size));
131 }
132 
133 static inline int
zpl_acl_to_xattr(struct posix_acl * acl,void * value,int size)134 zpl_acl_to_xattr(struct posix_acl *acl, void *value, int size)
135 {
136 	return (posix_acl_to_xattr(kcred->user_ns, acl, value, size));
137 }
138 
139 #endif /* _ZFS_XATTR_H */
140