xref: /freebsd/sys/contrib/openzfs/include/os/linux/kernel/linux/vfs_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  * Copyright (C) 2015 Jörg Thalheim.
26  */
27 
28 #ifndef _ZFS_VFS_H
29 #define	_ZFS_VFS_H
30 
31 #include <sys/taskq.h>
32 #include <sys/cred.h>
33 #include <linux/backing-dev.h>
34 #include <linux/compat.h>
35 
36 /*
37  * 4.14 adds SB_* flag definitions, define them to MS_* equivalents
38  * if not set.
39  */
40 #ifndef	SB_RDONLY
41 #define	SB_RDONLY	MS_RDONLY
42 #endif
43 
44 #ifndef	SB_SILENT
45 #define	SB_SILENT	MS_SILENT
46 #endif
47 
48 #ifndef	SB_ACTIVE
49 #define	SB_ACTIVE	MS_ACTIVE
50 #endif
51 
52 #ifndef	SB_POSIXACL
53 #define	SB_POSIXACL	MS_POSIXACL
54 #endif
55 
56 #ifndef	SB_MANDLOCK
57 #define	SB_MANDLOCK	MS_MANDLOCK
58 #endif
59 
60 #ifndef	SB_NOATIME
61 #define	SB_NOATIME	MS_NOATIME
62 #endif
63 
64 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
65 static inline loff_t
lseek_execute(struct file * filp,struct inode * inode,loff_t offset,loff_t maxsize)66 lseek_execute(
67 	struct file *filp,
68 	struct inode *inode,
69 	loff_t offset,
70 	loff_t maxsize)
71 {
72 #ifdef FMODE_UNSIGNED_OFFSET
73 	if (offset < 0 && !(filp->f_mode & FMODE_UNSIGNED_OFFSET))
74 #else
75 	if (offset < 0 && !(filp->f_op->fop_flags & FOP_UNSIGNED_OFFSET))
76 #endif
77 		return (-EINVAL);
78 
79 	if (offset > maxsize)
80 		return (-EINVAL);
81 
82 	if (offset != filp->f_pos) {
83 		spin_lock(&filp->f_lock);
84 		filp->f_pos = offset;
85 #ifdef HAVE_FILE_F_VERSION
86 		filp->f_version = 0;
87 #endif
88 		spin_unlock(&filp->f_lock);
89 	}
90 
91 	return (offset);
92 }
93 #endif /* SEEK_HOLE && SEEK_DATA */
94 
95 #if defined(CONFIG_FS_POSIX_ACL)
96 /*
97  * These functions safely approximates the behavior of posix_acl_release()
98  * which cannot be used because it calls the GPL-only symbol kfree_rcu().
99  * The in-kernel version, which can access the RCU, frees the ACLs after
100  * the grace period expires.  Because we're unsure how long that grace
101  * period may be this implementation conservatively delays for 60 seconds.
102  * This is several orders of magnitude larger than expected grace period.
103  * At 60 seconds the kernel will also begin issuing RCU stall warnings.
104  */
105 
106 #include <linux/posix_acl.h>
107 
108 void zpl_posix_acl_release_impl(struct posix_acl *);
109 
110 static inline void
zpl_posix_acl_release(struct posix_acl * acl)111 zpl_posix_acl_release(struct posix_acl *acl)
112 {
113 	if ((acl == NULL) || (acl == ACL_NOT_CACHED))
114 		return;
115 	if (refcount_dec_and_test(&acl->a_refcount))
116 		zpl_posix_acl_release_impl(acl);
117 }
118 #endif /* CONFIG_FS_POSIX_ACL */
119 
zfs_uid_read_impl(struct inode * ip)120 static inline uid_t zfs_uid_read_impl(struct inode *ip)
121 {
122 	return (from_kuid(kcred->user_ns, ip->i_uid));
123 }
124 
zfs_uid_read(struct inode * ip)125 static inline uid_t zfs_uid_read(struct inode *ip)
126 {
127 	return (zfs_uid_read_impl(ip));
128 }
129 
zfs_gid_read_impl(struct inode * ip)130 static inline gid_t zfs_gid_read_impl(struct inode *ip)
131 {
132 	return (from_kgid(kcred->user_ns, ip->i_gid));
133 }
134 
zfs_gid_read(struct inode * ip)135 static inline gid_t zfs_gid_read(struct inode *ip)
136 {
137 	return (zfs_gid_read_impl(ip));
138 }
139 
zfs_uid_write(struct inode * ip,uid_t uid)140 static inline void zfs_uid_write(struct inode *ip, uid_t uid)
141 {
142 	ip->i_uid = make_kuid(kcred->user_ns, uid);
143 }
144 
zfs_gid_write(struct inode * ip,gid_t gid)145 static inline void zfs_gid_write(struct inode *ip, gid_t gid)
146 {
147 	ip->i_gid = make_kgid(kcred->user_ns, gid);
148 }
149 
150 /*
151  * 3.15 API change
152  */
153 #ifndef RENAME_NOREPLACE
154 #define	RENAME_NOREPLACE	(1 << 0) /* Don't overwrite target */
155 #endif
156 #ifndef RENAME_EXCHANGE
157 #define	RENAME_EXCHANGE		(1 << 1) /* Exchange source and dest */
158 #endif
159 #ifndef RENAME_WHITEOUT
160 #define	RENAME_WHITEOUT		(1 << 2) /* Whiteout source */
161 #endif
162 
163 /*
164  * 4.9 API change
165  */
166 #if !(defined(HAVE_SETATTR_PREPARE_NO_USERNS) || \
167     defined(HAVE_SETATTR_PREPARE_USERNS) || \
168     defined(HAVE_SETATTR_PREPARE_IDMAP))
169 static inline int
setattr_prepare(struct dentry * dentry,struct iattr * ia)170 setattr_prepare(struct dentry *dentry, struct iattr *ia)
171 {
172 	return (inode_change_ok(dentry->d_inode, ia));
173 }
174 #endif
175 
176 /*
177  * 4.11 API change
178  * These macros are defined by kernel 4.11.  We define them so that the same
179  * code builds under kernels < 4.11 and >= 4.11.  The macros are set to 0 so
180  * that it will create obvious failures if they are accidentally used when built
181  * against a kernel >= 4.11.
182  */
183 
184 #ifndef STATX_BASIC_STATS
185 #define	STATX_BASIC_STATS	0
186 #endif
187 
188 #ifndef AT_STATX_SYNC_AS_STAT
189 #define	AT_STATX_SYNC_AS_STAT	0
190 #endif
191 
192 /*
193  * 4.11 API change
194  * 4.11 takes struct path *, < 4.11 takes vfsmount *
195  */
196 
197 #if defined(HAVE_PATH_IOPS_GETATTR)
198 #define	ZPL_GETATTR_WRAPPER(func)					\
199 static int								\
200 func(const struct path *path, struct kstat *stat, u32 request_mask,	\
201     unsigned int query_flags)						\
202 {									\
203 	return (func##_impl(path, stat, request_mask, query_flags));	\
204 }
205 #elif defined(HAVE_USERNS_IOPS_GETATTR)
206 #define	ZPL_GETATTR_WRAPPER(func)					\
207 static int								\
208 func(struct user_namespace *user_ns, const struct path *path,	\
209     struct kstat *stat, u32 request_mask, unsigned int query_flags)	\
210 {									\
211 	return (func##_impl(user_ns, path, stat, request_mask, \
212 	    query_flags));	\
213 }
214 #elif defined(HAVE_IDMAP_IOPS_GETATTR)
215 #define	ZPL_GETATTR_WRAPPER(func)					\
216 static int								\
217 func(struct mnt_idmap *user_ns, const struct path *path,	\
218     struct kstat *stat, u32 request_mask, unsigned int query_flags)	\
219 {									\
220 	return (func##_impl(user_ns, path, stat, request_mask,	\
221 	    query_flags));	\
222 }
223 #else
224 #error
225 #endif
226 
227 /*
228  * Returns true when called in the context of a 32-bit system call.
229  */
230 static inline int
zpl_is_32bit_api(void)231 zpl_is_32bit_api(void)
232 {
233 #ifdef CONFIG_COMPAT
234 	return (in_compat_syscall());
235 #else
236 	return (BITS_PER_LONG == 32);
237 #endif
238 }
239 
240 /*
241  * 5.12 API change
242  * To support id-mapped mounts, generic_fillattr() was modified to
243  * accept a new struct user_namespace* as its first arg.
244  *
245  * 6.3 API change
246  * generic_fillattr() first arg is changed to struct mnt_idmap *
247  *
248  * 6.6 API change
249  * generic_fillattr() gets new second arg request_mask, a u32 type
250  *
251  */
252 #ifdef HAVE_GENERIC_FILLATTR_IDMAP
253 #define	zpl_generic_fillattr(idmap, ip, sp)	\
254     generic_fillattr(idmap, ip, sp)
255 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
256 #define	zpl_generic_fillattr(idmap, rqm, ip, sp)	\
257     generic_fillattr(idmap, rqm, ip, sp)
258 #elif defined(HAVE_GENERIC_FILLATTR_USERNS)
259 #define	zpl_generic_fillattr(user_ns, ip, sp)	\
260     generic_fillattr(user_ns, ip, sp)
261 #else
262 #define	zpl_generic_fillattr(user_ns, ip, sp)	generic_fillattr(ip, sp)
263 #endif
264 
265 #endif /* _ZFS_VFS_H */
266