]> git.proxmox.com Git - mirror_zfs.git/blame - include/linux/vfs_compat.h
Linux 4.16 compat: use correct *_dec_and_test()
[mirror_zfs.git] / include / linux / vfs_compat.h
CommitLineData
7268e1be
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
534759fa 24 * Copyright (C) 2015 Jörg Thalheim.
7268e1be
BB
25 */
26
27#ifndef _ZFS_VFS_H
d1d7e268 28#define _ZFS_VFS_H
7268e1be 29
194e5623 30#include <sys/taskq.h>
82a1b2d6 31#include <sys/cred.h>
e80da864 32#include <linux/backing-dev.h>
194e5623 33
bdf4328b
BB
34/*
35 * 2.6.28 API change,
36 * Added insert_inode_locked() helper function, prior to this most callers
37 * used insert_inode_hash(). The older method doesn't check for collisions
38 * in the inode_hashtable but it still acceptible for use.
39 */
40#ifndef HAVE_INSERT_INODE_LOCKED
41static inline int
42insert_inode_locked(struct inode *ip)
43{
44 insert_inode_hash(ip);
45 return (0);
46}
47#endif /* HAVE_INSERT_INODE_LOCKED */
7268e1be 48
b3129792
PJ
49/*
50 * 2.6.35 API change,
51 * Add truncate_setsize() if it is not exported by the Linux kernel.
52 *
53 * Truncate the inode and pages associated with the inode. The pages are
54 * unmapped and removed from cache.
55 */
56#ifndef HAVE_TRUNCATE_SETSIZE
57static inline void
58truncate_setsize(struct inode *ip, loff_t new)
59{
60 struct address_space *mapping = ip->i_mapping;
61
62 i_size_write(ip, new);
63
64 unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
65 truncate_inode_pages(mapping, new);
66 unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
67}
68#endif /* HAVE_TRUNCATE_SETSIZE */
69
76659dc1 70/*
8c45def2
BB
71 * 2.6.32 - 2.6.33, bdi_setup_and_register() is not available.
72 * 2.6.34 - 3.19, bdi_setup_and_register() takes 3 arguments.
7dae2c81
BB
73 * 4.0 - 4.11, bdi_setup_and_register() takes 2 arguments.
74 * 4.12 - x.y, super_setup_bdi_name() new interface.
76659dc1 75 */
7dae2c81 76#if defined(HAVE_SUPER_SETUP_BDI_NAME)
3e6c9433 77extern atomic_long_t zfs_bdi_seq;
78
8c45def2 79static inline int
7dae2c81 80zpl_bdi_setup(struct super_block *sb, char *name)
8c45def2 81{
3e6c9433 82 return super_setup_bdi_name(sb, "%.28s-%ld", name,
83 atomic_long_inc_return(&zfs_bdi_seq));
7dae2c81
BB
84}
85static inline void
86zpl_bdi_destroy(struct super_block *sb)
87{
88}
89#elif defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER)
90static inline int
91zpl_bdi_setup(struct super_block *sb, char *name)
92{
93 struct backing_dev_info *bdi;
94 int error;
95
96 bdi = kmem_zalloc(sizeof (struct backing_dev_info), KM_SLEEP);
97 error = bdi_setup_and_register(bdi, name);
98 if (error) {
99 kmem_free(bdi, sizeof (struct backing_dev_info));
100 return (error);
101 }
102
103 sb->s_bdi = bdi;
104
105 return (0);
106}
107static inline void
108zpl_bdi_destroy(struct super_block *sb)
109{
110 struct backing_dev_info *bdi = sb->s_bdi;
111
112 bdi_destroy(bdi);
113 kmem_free(bdi, sizeof (struct backing_dev_info));
114 sb->s_bdi = NULL;
8c45def2
BB
115}
116#elif defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
117static inline int
7dae2c81
BB
118zpl_bdi_setup(struct super_block *sb, char *name)
119{
120 struct backing_dev_info *bdi;
121 int error;
122
123 bdi = kmem_zalloc(sizeof (struct backing_dev_info), KM_SLEEP);
124 error = bdi_setup_and_register(bdi, name, BDI_CAP_MAP_COPY);
125 if (error) {
126 kmem_free(sb->s_bdi, sizeof (struct backing_dev_info));
127 return (error);
128 }
129
130 sb->s_bdi = bdi;
131
132 return (0);
133}
134static inline void
135zpl_bdi_destroy(struct super_block *sb)
8c45def2 136{
7dae2c81
BB
137 struct backing_dev_info *bdi = sb->s_bdi;
138
139 bdi_destroy(bdi);
140 kmem_free(bdi, sizeof (struct backing_dev_info));
141 sb->s_bdi = NULL;
8c45def2
BB
142}
143#else
5547c2f1
BB
144extern atomic_long_t zfs_bdi_seq;
145
146static inline int
7dae2c81 147zpl_bdi_setup(struct super_block *sb, char *name)
5547c2f1 148{
7dae2c81 149 struct backing_dev_info *bdi;
5547c2f1
BB
150 int error;
151
7dae2c81 152 bdi = kmem_zalloc(sizeof (struct backing_dev_info), KM_SLEEP);
5547c2f1 153 bdi->name = name;
8c45def2
BB
154 bdi->capabilities = BDI_CAP_MAP_COPY;
155
5547c2f1 156 error = bdi_init(bdi);
7dae2c81
BB
157 if (error) {
158 kmem_free(bdi, sizeof (struct backing_dev_info));
5547c2f1 159 return (error);
7dae2c81 160 }
5547c2f1 161
3e6c9433 162 error = bdi_register(bdi, NULL, "%.28s-%ld", name,
5547c2f1
BB
163 atomic_long_inc_return(&zfs_bdi_seq));
164 if (error) {
165 bdi_destroy(bdi);
7dae2c81 166 kmem_free(bdi, sizeof (struct backing_dev_info));
5547c2f1
BB
167 return (error);
168 }
169
7dae2c81
BB
170 sb->s_bdi = bdi;
171
172 return (0);
173}
174static inline void
175zpl_bdi_destroy(struct super_block *sb)
176{
177 struct backing_dev_info *bdi = sb->s_bdi;
178
179 bdi_destroy(bdi);
180 kmem_free(bdi, sizeof (struct backing_dev_info));
181 sb->s_bdi = NULL;
5547c2f1 182}
8c45def2 183#endif
76659dc1 184
7b3e34ba
BB
185/*
186 * 2.6.38 API change,
187 * LOOKUP_RCU flag introduced to distinguish rcu-walk from ref-walk cases.
188 */
189#ifndef LOOKUP_RCU
d1d7e268 190#define LOOKUP_RCU 0x0
7b3e34ba
BB
191#endif /* LOOKUP_RCU */
192
28eb9213
DH
193/*
194 * 3.2-rc1 API change,
195 * Add set_nlink() if it is not exported by the Linux kernel.
196 *
197 * i_nlink is read-only in Linux 3.2, but it can be set directly in
198 * earlier kernels.
199 */
200#ifndef HAVE_SET_NLINK
201static inline void
202set_nlink(struct inode *inode, unsigned int nlink)
203{
204 inode->i_nlink = nlink;
205}
206#endif /* HAVE_SET_NLINK */
207
b39d3b9f
BB
208/*
209 * 3.3 API change,
210 * The VFS .create, .mkdir and .mknod callbacks were updated to take a
211 * umode_t type rather than an int. To cleanly handle both definitions
212 * the zpl_umode_t type is introduced and set accordingly.
213 */
95f5c63b 214#ifdef HAVE_MKDIR_UMODE_T
b39d3b9f
BB
215typedef umode_t zpl_umode_t;
216#else
217typedef int zpl_umode_t;
218#endif
219
739a1a82
RY
220/*
221 * 3.5 API change,
222 * The clear_inode() function replaces end_writeback() and introduces an
223 * ordering change regarding when the inode_sync_wait() occurs. See the
224 * configure check in config/kernel-clear-inode.m4 for full details.
225 */
226#if defined(HAVE_EVICT_INODE) && !defined(HAVE_CLEAR_INODE)
d1d7e268 227#define clear_inode(ip) end_writeback(ip)
739a1a82
RY
228#endif /* HAVE_EVICT_INODE && !HAVE_CLEAR_INODE */
229
3c203610
YS
230/*
231 * 3.6 API change,
232 * The sget() helper function now takes the mount flags as an argument.
233 */
234#ifdef HAVE_5ARG_SGET
d1d7e268 235#define zpl_sget(type, cmp, set, fl, mtd) sget(type, cmp, set, fl, mtd)
3c203610 236#else
d1d7e268 237#define zpl_sget(type, cmp, set, fl, mtd) sget(type, cmp, set, mtd)
3c203610
YS
238#endif /* HAVE_5ARG_SGET */
239
802e7b5f
LD
240#if defined(SEEK_HOLE) && defined(SEEK_DATA) && !defined(HAVE_LSEEK_EXECUTE)
241static inline loff_t
d1d7e268
MK
242lseek_execute(
243 struct file *filp,
244 struct inode *inode,
245 loff_t offset,
246 loff_t maxsize)
802e7b5f
LD
247{
248 if (offset < 0 && !(filp->f_mode & FMODE_UNSIGNED_OFFSET))
249 return (-EINVAL);
250
251 if (offset > maxsize)
252 return (-EINVAL);
253
254 if (offset != filp->f_pos) {
255 spin_lock(&filp->f_lock);
256 filp->f_pos = offset;
257 filp->f_version = 0;
258 spin_unlock(&filp->f_lock);
259 }
260
261 return (offset);
262}
263#endif /* SEEK_HOLE && SEEK_DATA && !HAVE_LSEEK_EXECUTE */
264
b695c34e 265#if defined(CONFIG_FS_POSIX_ACL)
023699cd
MM
266/*
267 * These functions safely approximates the behavior of posix_acl_release()
268 * which cannot be used because it calls the GPL-only symbol kfree_rcu().
269 * The in-kernel version, which can access the RCU, frees the ACLs after
270 * the grace period expires. Because we're unsure how long that grace
271 * period may be this implementation conservatively delays for 60 seconds.
272 * This is several orders of magnitude larger than expected grace period.
273 * At 60 seconds the kernel will also begin issuing RCU stall warnings.
274 */
3713b733
BB
275#ifdef refcount_t
276#undef refcount_t
277#endif
278
023699cd 279#include <linux/posix_acl.h>
023699cd
MM
280
281#if defined(HAVE_POSIX_ACL_RELEASE) && !defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY)
023699cd 282#define zpl_posix_acl_release(arg) posix_acl_release(arg)
023699cd 283#else
8e71ab99 284void zpl_posix_acl_release_impl(struct posix_acl *);
023699cd
MM
285
286static inline void
287zpl_posix_acl_release(struct posix_acl *acl)
288{
289 if ((acl == NULL) || (acl == ACL_NOT_CACHED))
290 return;
e5ba614d
TH
291#ifdef HAVE_ACL_REFCOUNT
292 if (refcount_dec_and_test(&acl->a_refcount))
293 zpl_posix_acl_release_impl(acl);
294#else
8e71ab99
CC
295 if (atomic_dec_and_test(&acl->a_refcount))
296 zpl_posix_acl_release_impl(acl);
e5ba614d 297#endif
023699cd 298}
3779913b 299#endif /* HAVE_POSIX_ACL_RELEASE */
023699cd 300
3779913b
CC
301#ifdef HAVE_SET_CACHED_ACL_USABLE
302#define zpl_set_cached_acl(ip, ty, n) set_cached_acl(ip, ty, n)
303#define zpl_forget_cached_acl(ip, ty) forget_cached_acl(ip, ty)
304#else
023699cd 305static inline void
4ea3f864
GM
306zpl_set_cached_acl(struct inode *ip, int type, struct posix_acl *newer)
307{
023699cd
MM
308 struct posix_acl *older = NULL;
309
310 spin_lock(&ip->i_lock);
311
312 if ((newer != ACL_NOT_CACHED) && (newer != NULL))
313 posix_acl_dup(newer);
314
d1d7e268 315 switch (type) {
023699cd
MM
316 case ACL_TYPE_ACCESS:
317 older = ip->i_acl;
d1d7e268 318 rcu_assign_pointer(ip->i_acl, newer);
023699cd
MM
319 break;
320 case ACL_TYPE_DEFAULT:
321 older = ip->i_default_acl;
d1d7e268 322 rcu_assign_pointer(ip->i_default_acl, newer);
023699cd
MM
323 break;
324 }
325
326 spin_unlock(&ip->i_lock);
327
328 zpl_posix_acl_release(older);
023699cd
MM
329}
330
331static inline void
4ea3f864
GM
332zpl_forget_cached_acl(struct inode *ip, int type)
333{
023699cd
MM
334 zpl_set_cached_acl(ip, type, (struct posix_acl *)ACL_NOT_CACHED);
335}
3779913b 336#endif /* HAVE_SET_CACHED_ACL_USABLE */
023699cd 337
408ec0d2
CC
338#ifndef HAVE___POSIX_ACL_CHMOD
339#ifdef HAVE_POSIX_ACL_CHMOD
340#define __posix_acl_chmod(acl, gfp, mode) posix_acl_chmod(acl, gfp, mode)
341#define __posix_acl_create(acl, gfp, mode) posix_acl_create(acl, gfp, mode)
342#else
023699cd 343static inline int
4ea3f864
GM
344__posix_acl_chmod(struct posix_acl **acl, int flags, umode_t umode)
345{
023699cd
MM
346 struct posix_acl *oldacl = *acl;
347 mode_t mode = umode;
348 int error;
349
350 *acl = posix_acl_clone(*acl, flags);
351 zpl_posix_acl_release(oldacl);
352
353 if (!(*acl))
354 return (-ENOMEM);
355
356 error = posix_acl_chmod_masq(*acl, mode);
357 if (error) {
358 zpl_posix_acl_release(*acl);
359 *acl = NULL;
360 }
361
d1d7e268 362 return (error);
023699cd
MM
363}
364
365static inline int
4ea3f864
GM
366__posix_acl_create(struct posix_acl **acl, int flags, umode_t *umodep)
367{
023699cd
MM
368 struct posix_acl *oldacl = *acl;
369 mode_t mode = *umodep;
370 int error;
371
372 *acl = posix_acl_clone(*acl, flags);
373 zpl_posix_acl_release(oldacl);
374
375 if (!(*acl))
376 return (-ENOMEM);
377
378 error = posix_acl_create_masq(*acl, &mode);
379 *umodep = mode;
380
381 if (error < 0) {
382 zpl_posix_acl_release(*acl);
383 *acl = NULL;
384 }
385
386 return (error);
387}
388#endif /* HAVE_POSIX_ACL_CHMOD */
408ec0d2 389#endif /* HAVE___POSIX_ACL_CHMOD */
023699cd 390
62a05896
CW
391#ifdef HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T
392typedef umode_t zpl_equivmode_t;
393#else
394typedef mode_t zpl_equivmode_t;
395#endif /* HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T */
4b908d32
BB
396
397/*
398 * 4.8 API change,
399 * posix_acl_valid() now must be passed a namespace, the namespace from
400 * from super block associated with the given inode is used for this purpose.
401 */
402#ifdef HAVE_POSIX_ACL_VALID_WITH_NS
403#define zpl_posix_acl_valid(ip, acl) posix_acl_valid(ip->i_sb->s_user_ns, acl)
404#else
405#define zpl_posix_acl_valid(ip, acl) posix_acl_valid(acl)
406#endif
407
3713b733
BB
408#define refcount_t zfs_refcount_t
409
62a05896
CW
410#endif /* CONFIG_FS_POSIX_ACL */
411
62a05896
CW
412/*
413 * 2.6.38 API change,
414 * The is_owner_or_cap() function was renamed to inode_owner_or_capable().
415 */
416#ifdef HAVE_INODE_OWNER_OR_CAPABLE
417#define zpl_inode_owner_or_capable(ip) inode_owner_or_capable(ip)
023699cd 418#else
62a05896
CW
419#define zpl_inode_owner_or_capable(ip) is_owner_or_cap(ip)
420#endif /* HAVE_INODE_OWNER_OR_CAPABLE */
023699cd 421
534759fa
JT
422/*
423 * 3.19 API change
424 * struct access f->f_dentry->d_inode was replaced by accessor function
425 * file_inode(f)
426 */
427#ifndef HAVE_FILE_INODE
428static inline struct inode *file_inode(const struct file *f)
429{
430 return (f->f_dentry->d_inode);
431}
432#endif /* HAVE_FILE_INODE */
433
d9c97ec0
CH
434/*
435 * 4.1 API change
436 * struct access file->f_path.dentry was replaced by accessor function
437 * file_dentry(f)
438 */
439#ifndef HAVE_FILE_DENTRY
440static inline struct dentry *file_dentry(const struct file *f)
441{
442 return (f->f_path.dentry);
443}
444#endif /* HAVE_FILE_DENTRY */
445
82a1b2d6
NB
446#ifdef HAVE_KUID_HELPERS
447static inline uid_t zfs_uid_read_impl(struct inode *ip)
448{
938cfeb0
NB
449#ifdef HAVE_SUPER_USER_NS
450 return (from_kuid(ip->i_sb->s_user_ns, ip->i_uid));
451#else
82a1b2d6 452 return (from_kuid(kcred->user_ns, ip->i_uid));
938cfeb0 453#endif
82a1b2d6
NB
454}
455
456static inline uid_t zfs_uid_read(struct inode *ip)
457{
458 return (zfs_uid_read_impl(ip));
459}
460
461static inline gid_t zfs_gid_read_impl(struct inode *ip)
462{
938cfeb0
NB
463#ifdef HAVE_SUPER_USER_NS
464 return (from_kgid(ip->i_sb->s_user_ns, ip->i_gid));
465#else
82a1b2d6 466 return (from_kgid(kcred->user_ns, ip->i_gid));
938cfeb0 467#endif
82a1b2d6
NB
468}
469
470static inline gid_t zfs_gid_read(struct inode *ip)
471{
472 return (zfs_gid_read_impl(ip));
473}
474
475static inline void zfs_uid_write(struct inode *ip, uid_t uid)
476{
938cfeb0
NB
477#ifdef HAVE_SUPER_USER_NS
478 ip->i_uid = make_kuid(ip->i_sb->s_user_ns, uid);
479#else
82a1b2d6 480 ip->i_uid = make_kuid(kcred->user_ns, uid);
938cfeb0 481#endif
82a1b2d6
NB
482}
483
484static inline void zfs_gid_write(struct inode *ip, gid_t gid)
485{
938cfeb0
NB
486#ifdef HAVE_SUPER_USER_NS
487 ip->i_gid = make_kgid(ip->i_sb->s_user_ns, gid);
488#else
82a1b2d6 489 ip->i_gid = make_kgid(kcred->user_ns, gid);
938cfeb0 490#endif
82a1b2d6 491}
938cfeb0 492
82a1b2d6
NB
493#else
494static inline uid_t zfs_uid_read(struct inode *ip)
495{
496 return (ip->i_uid);
497}
498
499static inline gid_t zfs_gid_read(struct inode *ip)
500{
501 return (ip->i_gid);
502}
503
504static inline void zfs_uid_write(struct inode *ip, uid_t uid)
505{
506 ip->i_uid = uid;
507}
508
509static inline void zfs_gid_write(struct inode *ip, gid_t gid)
510{
511 ip->i_gid = gid;
512}
513#endif
514
278bee93
BB
515/*
516 * 2.6.38 API change
517 */
518#ifdef HAVE_FOLLOW_DOWN_ONE
519#define zpl_follow_down_one(path) follow_down_one(path)
520#define zpl_follow_up(path) follow_up(path)
521#else
522#define zpl_follow_down_one(path) follow_down(path)
523#define zpl_follow_up(path) follow_up(path)
524#endif
525
3b0ba3ba
BB
526/*
527 * 4.9 API change
528 */
529#ifndef HAVE_SETATTR_PREPARE
530static inline int
531setattr_prepare(struct dentry *dentry, struct iattr *ia)
532{
533 return (inode_change_ok(dentry->d_inode, ia));
534}
535#endif
536
a3478c07
OF
537/*
538 * 4.11 API change
539 * These macros are defined by kernel 4.11. We define them so that the same
540 * code builds under kernels < 4.11 and >= 4.11. The macros are set to 0 so
541 * that it will create obvious failures if they are accidentally used when built
542 * against a kernel >= 4.11.
543 */
544
545#ifndef STATX_BASIC_STATS
546#define STATX_BASIC_STATS 0
547#endif
548
549#ifndef AT_STATX_SYNC_AS_STAT
550#define AT_STATX_SYNC_AS_STAT 0
551#endif
552
553/*
554 * 4.11 API change
555 * 4.11 takes struct path *, < 4.11 takes vfsmount *
556 */
557
558#ifdef HAVE_VFSMOUNT_IOPS_GETATTR
559#define ZPL_GETATTR_WRAPPER(func) \
560static int \
561func(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) \
562{ \
563 struct path path = { .mnt = mnt, .dentry = dentry }; \
564 return func##_impl(&path, stat, STATX_BASIC_STATS, \
565 AT_STATX_SYNC_AS_STAT); \
566}
567#elif defined(HAVE_PATH_IOPS_GETATTR)
568#define ZPL_GETATTR_WRAPPER(func) \
569static int \
570func(const struct path *path, struct kstat *stat, u32 request_mask, \
571 unsigned int query_flags) \
572{ \
573 return (func##_impl(path, stat, request_mask, query_flags)); \
574}
575#else
576#error
577#endif
578
2946a1a1
BB
579/*
580 * 4.9 API change
581 * Preferred interface to get the current FS time.
582 */
583#if !defined(HAVE_CURRENT_TIME)
584static inline struct timespec
585current_time(struct inode *ip)
586{
587 return (timespec_trunc(current_kernel_time(), ip->i_sb->s_time_gran));
588}
589#endif
a3478c07 590
310e63df
BB
591/*
592 * 4.16 API change
593 * Added iversion interface for managing inode version field.
594 */
595#ifdef HAVE_INODE_SET_IVERSION
596#include <linux/iversion.h>
597#else
598static inline void
599inode_set_iversion(struct inode *ip, u64 val)
600{
601 ip->i_version = val;
602}
603#endif
604
7268e1be 605#endif /* _ZFS_VFS_H */