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.
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.
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]
23 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
24 * Copyright (C) 2015 Jörg Thalheim.
30 #include <sys/taskq.h>
32 #include <linux/backing-dev.h>
33 #include <linux/compat.h>
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.
41 #ifndef HAVE_INSERT_INODE_LOCKED
43 insert_inode_locked(struct inode
*ip
)
45 insert_inode_hash(ip
);
48 #endif /* HAVE_INSERT_INODE_LOCKED */
52 * Add truncate_setsize() if it is not exported by the Linux kernel.
54 * Truncate the inode and pages associated with the inode. The pages are
55 * unmapped and removed from cache.
57 #ifndef HAVE_TRUNCATE_SETSIZE
59 truncate_setsize(struct inode
*ip
, loff_t
new)
61 struct address_space
*mapping
= ip
->i_mapping
;
63 i_size_write(ip
, new);
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);
69 #endif /* HAVE_TRUNCATE_SETSIZE */
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.
77 #if defined(HAVE_SUPER_SETUP_BDI_NAME)
78 extern atomic_long_t zfs_bdi_seq
;
81 zpl_bdi_setup(struct super_block
*sb
, char *name
)
83 return super_setup_bdi_name(sb
, "%.28s-%ld", name
,
84 atomic_long_inc_return(&zfs_bdi_seq
));
87 zpl_bdi_destroy(struct super_block
*sb
)
90 #elif defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER)
92 zpl_bdi_setup(struct super_block
*sb
, char *name
)
94 struct backing_dev_info
*bdi
;
97 bdi
= kmem_zalloc(sizeof (struct backing_dev_info
), KM_SLEEP
);
98 error
= bdi_setup_and_register(bdi
, name
);
100 kmem_free(bdi
, sizeof (struct backing_dev_info
));
109 zpl_bdi_destroy(struct super_block
*sb
)
111 struct backing_dev_info
*bdi
= sb
->s_bdi
;
114 kmem_free(bdi
, sizeof (struct backing_dev_info
));
117 #elif defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
119 zpl_bdi_setup(struct super_block
*sb
, char *name
)
121 struct backing_dev_info
*bdi
;
124 bdi
= kmem_zalloc(sizeof (struct backing_dev_info
), KM_SLEEP
);
125 error
= bdi_setup_and_register(bdi
, name
, BDI_CAP_MAP_COPY
);
127 kmem_free(sb
->s_bdi
, sizeof (struct backing_dev_info
));
136 zpl_bdi_destroy(struct super_block
*sb
)
138 struct backing_dev_info
*bdi
= sb
->s_bdi
;
141 kmem_free(bdi
, sizeof (struct backing_dev_info
));
145 extern atomic_long_t zfs_bdi_seq
;
148 zpl_bdi_setup(struct super_block
*sb
, char *name
)
150 struct backing_dev_info
*bdi
;
153 bdi
= kmem_zalloc(sizeof (struct backing_dev_info
), KM_SLEEP
);
155 bdi
->capabilities
= BDI_CAP_MAP_COPY
;
157 error
= bdi_init(bdi
);
159 kmem_free(bdi
, sizeof (struct backing_dev_info
));
163 error
= bdi_register(bdi
, NULL
, "%.28s-%ld", name
,
164 atomic_long_inc_return(&zfs_bdi_seq
));
167 kmem_free(bdi
, sizeof (struct backing_dev_info
));
176 zpl_bdi_destroy(struct super_block
*sb
)
178 struct backing_dev_info
*bdi
= sb
->s_bdi
;
181 kmem_free(bdi
, sizeof (struct backing_dev_info
));
187 * 4.14 adds SB_* flag definitions, define them to MS_* equivalents
191 #define SB_RDONLY MS_RDONLY
195 #define SB_SILENT MS_SILENT
199 #define SB_ACTIVE MS_ACTIVE
203 #define SB_POSIXACL MS_POSIXACL
207 #define SB_MANDLOCK MS_MANDLOCK
211 #define SB_NOATIME MS_NOATIME
216 * LOOKUP_RCU flag introduced to distinguish rcu-walk from ref-walk cases.
219 #define LOOKUP_RCU 0x0
220 #endif /* LOOKUP_RCU */
223 * 3.2-rc1 API change,
224 * Add set_nlink() if it is not exported by the Linux kernel.
226 * i_nlink is read-only in Linux 3.2, but it can be set directly in
229 #ifndef HAVE_SET_NLINK
231 set_nlink(struct inode
*inode
, unsigned int nlink
)
233 inode
->i_nlink
= nlink
;
235 #endif /* HAVE_SET_NLINK */
239 * The VFS .create, .mkdir and .mknod callbacks were updated to take a
240 * umode_t type rather than an int. To cleanly handle both definitions
241 * the zpl_umode_t type is introduced and set accordingly.
243 #ifdef HAVE_MKDIR_UMODE_T
244 typedef umode_t zpl_umode_t
;
246 typedef int zpl_umode_t
;
251 * The clear_inode() function replaces end_writeback() and introduces an
252 * ordering change regarding when the inode_sync_wait() occurs. See the
253 * configure check in config/kernel-clear-inode.m4 for full details.
255 #if defined(HAVE_EVICT_INODE) && !defined(HAVE_CLEAR_INODE)
256 #define clear_inode(ip) end_writeback(ip)
257 #endif /* HAVE_EVICT_INODE && !HAVE_CLEAR_INODE */
261 * The sget() helper function now takes the mount flags as an argument.
263 #ifdef HAVE_5ARG_SGET
264 #define zpl_sget(type, cmp, set, fl, mtd) sget(type, cmp, set, fl, mtd)
266 #define zpl_sget(type, cmp, set, fl, mtd) sget(type, cmp, set, mtd)
267 #endif /* HAVE_5ARG_SGET */
269 #if defined(SEEK_HOLE) && defined(SEEK_DATA) && !defined(HAVE_LSEEK_EXECUTE)
277 if (offset
< 0 && !(filp
->f_mode
& FMODE_UNSIGNED_OFFSET
))
280 if (offset
> maxsize
)
283 if (offset
!= filp
->f_pos
) {
284 spin_lock(&filp
->f_lock
);
285 filp
->f_pos
= offset
;
287 spin_unlock(&filp
->f_lock
);
292 #endif /* SEEK_HOLE && SEEK_DATA && !HAVE_LSEEK_EXECUTE */
294 #if defined(CONFIG_FS_POSIX_ACL)
296 * These functions safely approximates the behavior of posix_acl_release()
297 * which cannot be used because it calls the GPL-only symbol kfree_rcu().
298 * The in-kernel version, which can access the RCU, frees the ACLs after
299 * the grace period expires. Because we're unsure how long that grace
300 * period may be this implementation conservatively delays for 60 seconds.
301 * This is several orders of magnitude larger than expected grace period.
302 * At 60 seconds the kernel will also begin issuing RCU stall warnings.
305 #include <linux/posix_acl.h>
307 #if defined(HAVE_POSIX_ACL_RELEASE) && !defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY)
308 #define zpl_posix_acl_release(arg) posix_acl_release(arg)
310 void zpl_posix_acl_release_impl(struct posix_acl
*);
313 zpl_posix_acl_release(struct posix_acl
*acl
)
315 if ((acl
== NULL
) || (acl
== ACL_NOT_CACHED
))
317 #ifdef HAVE_ACL_REFCOUNT
318 if (refcount_dec_and_test(&acl
->a_refcount
))
319 zpl_posix_acl_release_impl(acl
);
321 if (atomic_dec_and_test(&acl
->a_refcount
))
322 zpl_posix_acl_release_impl(acl
);
325 #endif /* HAVE_POSIX_ACL_RELEASE */
327 #ifdef HAVE_SET_CACHED_ACL_USABLE
328 #define zpl_set_cached_acl(ip, ty, n) set_cached_acl(ip, ty, n)
329 #define zpl_forget_cached_acl(ip, ty) forget_cached_acl(ip, ty)
332 zpl_set_cached_acl(struct inode
*ip
, int type
, struct posix_acl
*newer
)
334 struct posix_acl
*older
= NULL
;
336 spin_lock(&ip
->i_lock
);
338 if ((newer
!= ACL_NOT_CACHED
) && (newer
!= NULL
))
339 posix_acl_dup(newer
);
342 case ACL_TYPE_ACCESS
:
344 rcu_assign_pointer(ip
->i_acl
, newer
);
346 case ACL_TYPE_DEFAULT
:
347 older
= ip
->i_default_acl
;
348 rcu_assign_pointer(ip
->i_default_acl
, newer
);
352 spin_unlock(&ip
->i_lock
);
354 zpl_posix_acl_release(older
);
358 zpl_forget_cached_acl(struct inode
*ip
, int type
)
360 zpl_set_cached_acl(ip
, type
, (struct posix_acl
*)ACL_NOT_CACHED
);
362 #endif /* HAVE_SET_CACHED_ACL_USABLE */
364 #ifndef HAVE___POSIX_ACL_CHMOD
365 #ifdef HAVE_POSIX_ACL_CHMOD
366 #define __posix_acl_chmod(acl, gfp, mode) posix_acl_chmod(acl, gfp, mode)
367 #define __posix_acl_create(acl, gfp, mode) posix_acl_create(acl, gfp, mode)
370 __posix_acl_chmod(struct posix_acl
**acl
, int flags
, umode_t umode
)
372 struct posix_acl
*oldacl
= *acl
;
376 *acl
= posix_acl_clone(*acl
, flags
);
377 zpl_posix_acl_release(oldacl
);
382 error
= posix_acl_chmod_masq(*acl
, mode
);
384 zpl_posix_acl_release(*acl
);
392 __posix_acl_create(struct posix_acl
**acl
, int flags
, umode_t
*umodep
)
394 struct posix_acl
*oldacl
= *acl
;
395 mode_t mode
= *umodep
;
398 *acl
= posix_acl_clone(*acl
, flags
);
399 zpl_posix_acl_release(oldacl
);
404 error
= posix_acl_create_masq(*acl
, &mode
);
408 zpl_posix_acl_release(*acl
);
414 #endif /* HAVE_POSIX_ACL_CHMOD */
415 #endif /* HAVE___POSIX_ACL_CHMOD */
417 #ifdef HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T
418 typedef umode_t zpl_equivmode_t
;
420 typedef mode_t zpl_equivmode_t
;
421 #endif /* HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T */
425 * posix_acl_valid() now must be passed a namespace, the namespace from
426 * from super block associated with the given inode is used for this purpose.
428 #ifdef HAVE_POSIX_ACL_VALID_WITH_NS
429 #define zpl_posix_acl_valid(ip, acl) posix_acl_valid(ip->i_sb->s_user_ns, acl)
431 #define zpl_posix_acl_valid(ip, acl) posix_acl_valid(acl)
434 #endif /* CONFIG_FS_POSIX_ACL */
438 * The is_owner_or_cap() function was renamed to inode_owner_or_capable().
440 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
441 #define zpl_inode_owner_or_capable(ip) inode_owner_or_capable(ip)
443 #define zpl_inode_owner_or_capable(ip) is_owner_or_cap(ip)
444 #endif /* HAVE_INODE_OWNER_OR_CAPABLE */
448 * struct access f->f_dentry->d_inode was replaced by accessor function
451 #ifndef HAVE_FILE_INODE
452 static inline struct inode
*file_inode(const struct file
*f
)
454 return (f
->f_dentry
->d_inode
);
456 #endif /* HAVE_FILE_INODE */
460 * struct access file->f_path.dentry was replaced by accessor function
463 #ifndef HAVE_FILE_DENTRY
464 static inline struct dentry
*file_dentry(const struct file
*f
)
466 return (f
->f_path
.dentry
);
468 #endif /* HAVE_FILE_DENTRY */
470 #ifdef HAVE_KUID_HELPERS
471 static inline uid_t
zfs_uid_read_impl(struct inode
*ip
)
473 #ifdef HAVE_SUPER_USER_NS
474 return (from_kuid(ip
->i_sb
->s_user_ns
, ip
->i_uid
));
476 return (from_kuid(kcred
->user_ns
, ip
->i_uid
));
480 static inline uid_t
zfs_uid_read(struct inode
*ip
)
482 return (zfs_uid_read_impl(ip
));
485 static inline gid_t
zfs_gid_read_impl(struct inode
*ip
)
487 #ifdef HAVE_SUPER_USER_NS
488 return (from_kgid(ip
->i_sb
->s_user_ns
, ip
->i_gid
));
490 return (from_kgid(kcred
->user_ns
, ip
->i_gid
));
494 static inline gid_t
zfs_gid_read(struct inode
*ip
)
496 return (zfs_gid_read_impl(ip
));
499 static inline void zfs_uid_write(struct inode
*ip
, uid_t uid
)
501 #ifdef HAVE_SUPER_USER_NS
502 ip
->i_uid
= make_kuid(ip
->i_sb
->s_user_ns
, uid
);
504 ip
->i_uid
= make_kuid(kcred
->user_ns
, uid
);
508 static inline void zfs_gid_write(struct inode
*ip
, gid_t gid
)
510 #ifdef HAVE_SUPER_USER_NS
511 ip
->i_gid
= make_kgid(ip
->i_sb
->s_user_ns
, gid
);
513 ip
->i_gid
= make_kgid(kcred
->user_ns
, gid
);
518 static inline uid_t
zfs_uid_read(struct inode
*ip
)
523 static inline gid_t
zfs_gid_read(struct inode
*ip
)
528 static inline void zfs_uid_write(struct inode
*ip
, uid_t uid
)
533 static inline void zfs_gid_write(struct inode
*ip
, gid_t gid
)
542 #ifdef HAVE_FOLLOW_DOWN_ONE
543 #define zpl_follow_down_one(path) follow_down_one(path)
544 #define zpl_follow_up(path) follow_up(path)
546 #define zpl_follow_down_one(path) follow_down(path)
547 #define zpl_follow_up(path) follow_up(path)
553 #ifndef HAVE_SETATTR_PREPARE
555 setattr_prepare(struct dentry
*dentry
, struct iattr
*ia
)
557 return (inode_change_ok(dentry
->d_inode
, ia
));
563 * These macros are defined by kernel 4.11. We define them so that the same
564 * code builds under kernels < 4.11 and >= 4.11. The macros are set to 0 so
565 * that it will create obvious failures if they are accidentally used when built
566 * against a kernel >= 4.11.
569 #ifndef STATX_BASIC_STATS
570 #define STATX_BASIC_STATS 0
573 #ifndef AT_STATX_SYNC_AS_STAT
574 #define AT_STATX_SYNC_AS_STAT 0
579 * 4.11 takes struct path *, < 4.11 takes vfsmount *
582 #ifdef HAVE_VFSMOUNT_IOPS_GETATTR
583 #define ZPL_GETATTR_WRAPPER(func) \
585 func(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) \
587 struct path path = { .mnt = mnt, .dentry = dentry }; \
588 return func##_impl(&path, stat, STATX_BASIC_STATS, \
589 AT_STATX_SYNC_AS_STAT); \
591 #elif defined(HAVE_PATH_IOPS_GETATTR)
592 #define ZPL_GETATTR_WRAPPER(func) \
594 func(const struct path *path, struct kstat *stat, u32 request_mask, \
595 unsigned int query_flags) \
597 return (func##_impl(path, stat, request_mask, query_flags)); \
605 * Preferred interface to get the current FS time.
607 #if !defined(HAVE_CURRENT_TIME)
608 static inline struct timespec
609 current_time(struct inode
*ip
)
611 return (timespec_trunc(current_kernel_time(), ip
->i_sb
->s_time_gran
));
617 * Added iversion interface for managing inode version field.
619 #ifdef HAVE_INODE_SET_IVERSION
620 #include <linux/iversion.h>
623 inode_set_iversion(struct inode
*ip
, u64 val
)
630 * Returns true when called in the context of a 32-bit system call.
633 zpl_is_32bit_api(void)
636 #ifdef HAVE_IN_COMPAT_SYSCALL
637 return (in_compat_syscall());
639 return (is_compat_task());
642 return (BITS_PER_LONG
== 32);
646 #endif /* _ZFS_VFS_H */