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