Whamcloud - gitweb
LU-16364 llite: Move d_u.d_alias compat define
[fs/lustre-release.git] / lustre / include / lustre_compat.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #ifndef _LUSTRE_COMPAT_H
33 #define _LUSTRE_COMPAT_H
34
35 #include <linux/aio.h>
36 #include <linux/fs.h>
37 #include <linux/namei.h>
38 #include <linux/pagemap.h>
39 #include <linux/posix_acl_xattr.h>
40 #include <linux/bio.h>
41 #include <linux/xattr.h>
42 #include <linux/workqueue.h>
43 #include <linux/blkdev.h>
44 #include <linux/slab.h>
45 #include <linux/security.h>
46 #include <libcfs/linux/linux-fs.h>
47 #include <obd_support.h>
48
49 #ifdef HAVE_4ARGS_VFS_SYMLINK
50 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
51                 vfs_symlink(dir, dentry, path, mode)
52 #else
53 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
54                        vfs_symlink(dir, dentry, path)
55 #endif
56
57 #ifdef HAVE_BVEC_ITER
58 #define bio_idx(bio)                    (bio->bi_iter.bi_idx)
59 #define bio_set_sector(bio, sector)     (bio->bi_iter.bi_sector = sector)
60 #define bvl_to_page(bvl)                (bvl->bv_page)
61 #else
62 #define bio_idx(bio)                    (bio->bi_idx)
63 #define bio_set_sector(bio, sector)     (bio->bi_sector = sector)
64 #define bio_sectors(bio)                ((bio)->bi_size >> 9)
65 #define bvl_to_page(bvl)                (bvl->bv_page)
66 #endif
67
68 #ifdef HAVE_BVEC_ITER
69 #define bio_start_sector(bio) (bio->bi_iter.bi_sector)
70 #else
71 #define bio_start_sector(bio) (bio->bi_sector)
72 #endif
73
74 #ifndef HAVE_DENTRY_D_CHILD
75 #define d_child                 d_u.d_child
76 #endif
77
78 #ifndef HAVE_D_IN_LOOKUP
79 static inline int d_in_lookup(struct dentry *dentry)
80 {
81         return false;
82 }
83 #endif
84
85 #ifndef HAVE_VM_FAULT_T
86 #define vm_fault_t int
87 #endif
88
89 #ifndef HAVE_FOP_ITERATE_SHARED
90 #define iterate_shared iterate
91 #endif
92
93 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
94 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
95 #else
96 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
97 #endif
98
99 #ifdef HAVE_VFS_RENAME_5ARGS
100 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
101 #elif defined HAVE_VFS_RENAME_6ARGS
102 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
103 #else
104 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
105 #endif
106
107 #ifdef HAVE_USER_NAMESPACE_ARG
108 #define vfs_unlink(ns, dir, de) vfs_unlink(ns, dir, de, NULL)
109 #elif defined HAVE_VFS_UNLINK_3ARGS
110 #define vfs_unlink(ns, dir, de) vfs_unlink(dir, de, NULL)
111 #else
112 #define vfs_unlink(ns, dir, de) vfs_unlink(dir, de)
113 #endif
114
115 static inline int ll_vfs_getattr(struct path *path, struct kstat *st,
116                                  u32 request_mask, unsigned int flags)
117 {
118         int rc;
119
120 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
121         rc = vfs_getattr(path, st, request_mask, flags);
122 #else
123         rc = vfs_getattr(path, st);
124 #endif
125         return rc;
126 }
127
128 #ifndef HAVE_D_IS_POSITIVE
129 static inline bool d_is_positive(const struct dentry *dentry)
130 {
131         return dentry->d_inode != NULL;
132 }
133 #endif
134
135 #ifndef HAVE_INODE_LOCK
136 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
137 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
138 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
139 #endif
140
141 /* Old kernels lacked both Xarray support and the page cache
142  * using Xarrays. Our back ported Xarray support introduces
143  * the real xa_is_value() but we need a wrapper as well for
144  * the page cache interaction. Lets keep xa_is_value() separate
145  * in old kernels for Xarray support and page cache handling.
146  */
147 #ifndef HAVE_XARRAY_SUPPORT
148 static inline bool ll_xa_is_value(void *entry)
149 {
150         return radix_tree_exceptional_entry(entry);
151 }
152 #else
153 #define ll_xa_is_value  xa_is_value
154 #endif
155
156 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
157 static inline void truncate_inode_pages_final(struct address_space *map)
158 {
159         truncate_inode_pages(map, 0);
160 }
161 #endif
162
163 #ifndef HAVE_PTR_ERR_OR_ZERO
164 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
165 {
166         if (IS_ERR(ptr))
167                 return PTR_ERR(ptr);
168         else
169                 return 0;
170 }
171 #endif
172
173 #ifdef HAVE_PID_NS_FOR_CHILDREN
174 # define ll_task_pid_ns(task) \
175          ((task)->nsproxy ? ((task)->nsproxy->pid_ns_for_children) : NULL)
176 #else
177 # define ll_task_pid_ns(task) \
178          ((task)->nsproxy ? ((task)->nsproxy->pid_ns) : NULL)
179 #endif
180
181 #ifdef HAVE_FULL_NAME_HASH_3ARGS
182 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
183 #else
184 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
185 #endif
186
187 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
188 # define posix_acl_xattr_header struct posix_acl_xattr_header
189 # define posix_acl_xattr_entry  struct posix_acl_xattr_entry
190 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
191 #else
192 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
193 #endif
194
195 #ifdef HAVE_IOP_XATTR
196 #define ll_setxattr     generic_setxattr
197 #define ll_getxattr     generic_getxattr
198 #define ll_removexattr  generic_removexattr
199 #endif /* HAVE_IOP_XATTR */
200
201 #ifndef HAVE_POSIX_ACL_VALID_USER_NS
202 #define posix_acl_valid(a,b)            posix_acl_valid(b)
203 #endif
204
205 #ifdef HAVE_IOP_SET_ACL
206 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
207 #if !defined(HAVE_USER_NAMESPACE_ARG) && !defined(HAVE_POSIX_ACL_UPDATE_MODE)
208 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
209                           struct posix_acl **acl)
210 {
211         umode_t mode = inode->i_mode;
212         int error;
213
214         error = posix_acl_equiv_mode(*acl, &mode);
215         if (error < 0)
216                 return error;
217         if (error == 0)
218                 *acl = NULL;
219         if (!in_group_p(inode->i_gid) &&
220             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
221                 mode &= ~S_ISGID;
222         *mode_p = mode;
223         return 0;
224 }
225 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
226 #endif
227 #endif
228
229 #ifndef HAVE_IOV_ITER_TRUNCATE
230 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
231 {
232         if (i->count > count)
233                 i->count = count;
234 }
235 #endif
236
237 /*
238  * mount MS_* flags split from superblock SB_* flags
239  * if the SB_* flags are not available use the MS_* flags
240  */
241 #if !defined(SB_RDONLY) && defined(MS_RDONLY)
242 # define SB_RDONLY MS_RDONLY
243 #endif
244 #if !defined(SB_ACTIVE) && defined(MS_ACTIVE)
245 # define SB_ACTIVE MS_ACTIVE
246 #endif
247 #if !defined(SB_NOSEC) && defined(MS_NOSEC)
248 # define SB_NOSEC MS_NOSEC
249 #endif
250 #if !defined(SB_POSIXACL) && defined(MS_POSIXACL)
251 # define SB_POSIXACL MS_POSIXACL
252 #endif
253 #if !defined(SB_NODIRATIME) && defined(MS_NODIRATIME)
254 # define SB_NODIRATIME MS_NODIRATIME
255 #endif
256
257 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
258 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
259 {
260         i->count = count;
261 }
262
263 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
264 {
265         return (struct iovec) {
266                 .iov_base = iter->iov->iov_base + iter->iov_offset,
267                 .iov_len = min(iter->count,
268                                iter->iov->iov_len - iter->iov_offset),
269         };
270 }
271
272 #define iov_for_each(iov, iter, start)                                  \
273         for (iter = (start);                                            \
274              (iter).count && ((iov = iov_iter_iovec(&(iter))), 1);      \
275              iov_iter_advance(&(iter), (iov).iov_len))
276
277 static inline ssize_t
278 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
279 {
280         struct iovec iov;
281         struct iov_iter i;
282         ssize_t bytes = 0;
283
284         iov_for_each(iov, i, *iter) {
285                 ssize_t res;
286
287                 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
288                 if (res <= 0) {
289                         if (bytes == 0)
290                                 bytes = res;
291                         break;
292                 }
293
294                 bytes += res;
295                 if (res < iov.iov_len)
296                         break;
297         }
298
299         if (bytes > 0)
300                 iov_iter_advance(iter, bytes);
301         return bytes;
302 }
303
304 static inline ssize_t
305 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
306 {
307         struct iovec iov;
308         struct iov_iter i;
309         ssize_t bytes = 0;
310
311         /* Since LLITE updates file size at the end of I/O in
312          * vvp_io_commit_write(), append write has to be done in atomic when
313          * there are multiple segments because otherwise each iteration to
314          * __generic_file_aio_write() will see original file size */
315         if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
316                 struct iovec *iov_copy;
317                 int count = 0;
318
319                 OBD_ALLOC_PTR_ARRAY(iov_copy, iter->nr_segs);
320                 if (!iov_copy)
321                         return -ENOMEM;
322
323                 iov_for_each(iov, i, *iter)
324                         iov_copy[count++] = iov;
325
326                 bytes = __generic_file_aio_write(iocb, iov_copy, count,
327                                                  &iocb->ki_pos);
328                 OBD_FREE_PTR_ARRAY(iov_copy, iter->nr_segs);
329
330                 if (bytes > 0)
331                         iov_iter_advance(iter, bytes);
332                 return bytes;
333         }
334
335         iov_for_each(iov, i, *iter) {
336                 ssize_t res;
337
338                 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
339                 if (res <= 0) {
340                         if (bytes == 0)
341                                 bytes = res;
342                         break;
343                 }
344
345                 bytes += res;
346                 if (res < iov.iov_len)
347                         break;
348         }
349
350         if (bytes > 0)
351                 iov_iter_advance(iter, bytes);
352         return bytes;
353 }
354 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
355
356 static inline void __user *get_vmf_address(struct vm_fault *vmf)
357 {
358 #ifdef HAVE_VM_FAULT_ADDRESS
359         return (void __user *)vmf->address;
360 #else
361         return vmf->virtual_address;
362 #endif
363 }
364
365 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
366 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
367 #else
368 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
369 #endif
370
371 #ifndef HAVE_CURRENT_TIME
372 static inline struct timespec current_time(struct inode *inode)
373 {
374         return CURRENT_TIME;
375 }
376 #endif
377
378 #ifndef time_after32
379 /**
380  * time_after32 - compare two 32-bit relative times
381  * @a: the time which may be after @b
382  * @b: the time which may be before @a
383  *
384  * Needed for kernels earlier than v4.14-rc1~134^2
385  *
386  * time_after32(a, b) returns true if the time @a is after time @b.
387  * time_before32(b, a) returns true if the time @b is before time @a.
388  *
389  * Similar to time_after(), compare two 32-bit timestamps for relative
390  * times.  This is useful for comparing 32-bit seconds values that can't
391  * be converted to 64-bit values (e.g. due to disk format or wire protocol
392  * issues) when it is known that the times are less than 68 years apart.
393  */
394 #define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
395 #define time_before32(b, a)    time_after32(a, b)
396
397 #endif
398
399 #ifndef smp_store_mb
400 #define smp_store_mb(var, value)        set_mb(var, value)
401 #endif
402
403 #ifdef HAVE_PAGEVEC_INIT_ONE_PARAM
404 #define ll_pagevec_init(pvec, n) pagevec_init(pvec)
405 #else
406 #define ll_pagevec_init(pvec, n) pagevec_init(pvec, n)
407 #endif
408
409 #ifdef HAVE_D_COUNT
410 #  define ll_d_count(d)         d_count(d)
411 #else
412 #  define ll_d_count(d)         ((d)->d_count)
413 #endif /* HAVE_D_COUNT */
414
415 #ifndef HAVE_IN_COMPAT_SYSCALL
416 #define in_compat_syscall       is_compat_task
417 #endif
418
419 #ifdef HAVE_I_PAGES
420 #define page_tree i_pages
421 #define ll_xa_lock_irqsave(lockp, flags) xa_lock_irqsave(lockp, flags)
422 #define ll_xa_unlock_irqrestore(lockp, flags) xa_unlock_irqrestore(lockp, flags)
423 #else
424 #define i_pages tree_lock
425 #define ll_xa_lock_irqsave(lockp, flags) spin_lock_irqsave(lockp, flags)
426 #define ll_xa_unlock_irqrestore(lockp, flags) spin_unlock_irqrestore(lockp, flags)
427 #endif
428
429 #ifndef HAVE_LOCK_PAGE_MEMCG
430 #define lock_page_memcg(page) do {} while (0)
431 #define unlock_page_memcg(page) do {} while (0)
432 #endif
433
434 #ifndef KMEM_CACHE_USERCOPY
435 #define kmem_cache_create_usercopy(name, size, align, flags, useroffset, \
436                                    usersize, ctor)                       \
437         kmem_cache_create(name, size, align, flags, ctor)
438 #endif
439
440 #ifndef HAVE_LINUX_SELINUX_IS_ENABLED
441 #define selinux_is_enabled() 1
442 #endif
443
444 static inline int ll_vfs_getxattr(struct dentry *dentry, struct inode *inode,
445                                   const char *name,
446                                   void *value, size_t size)
447 {
448 #ifdef HAVE_USER_NAMESPACE_ARG
449         return vfs_getxattr(&init_user_ns, dentry, name, value, size);
450 #elif defined(HAVE_VFS_SETXATTR)
451         return __vfs_getxattr(dentry, inode, name, value, size);
452 #else
453         if (unlikely(!inode->i_op->getxattr))
454                 return -ENODATA;
455
456         return inode->i_op->getxattr(dentry, name, value, size);
457 #endif
458 }
459
460 static inline int ll_vfs_setxattr(struct dentry *dentry, struct inode *inode,
461                                   const char *name,
462                                   const void *value, size_t size, int flags)
463 {
464 #ifdef HAVE_USER_NAMESPACE_ARG
465         return vfs_setxattr(&init_user_ns, dentry, name, value, size, flags);
466 #elif defined(HAVE_VFS_SETXATTR)
467         return __vfs_setxattr(dentry, inode, name, value, size, flags);
468 #else
469         if (unlikely(!inode->i_op->setxattr))
470                 return -EOPNOTSUPP;
471
472         return inode->i_op->setxattr(dentry, name, value, size, flags);
473 #endif
474 }
475
476 static inline int ll_vfs_removexattr(struct dentry *dentry, struct inode *inode,
477                                      const char *name)
478 {
479 #ifdef HAVE_USER_NAMESPACE_ARG
480         return vfs_removexattr(&init_user_ns, dentry, name);
481 #elif defined(HAVE_VFS_SETXATTR)
482         return __vfs_removexattr(dentry, name);
483 #else
484         if (unlikely(!inode->i_op->setxattr))
485                 return -EOPNOTSUPP;
486
487         return inode->i_op->removexattr(dentry, name);
488 #endif
489 }
490
491 #ifndef FALLOC_FL_COLLAPSE_RANGE
492 #define FALLOC_FL_COLLAPSE_RANGE 0x08 /* remove a range of a file */
493 #endif
494
495 #ifndef FALLOC_FL_ZERO_RANGE
496 #define FALLOC_FL_ZERO_RANGE 0x10 /* convert range to zeros */
497 #endif
498
499 #ifndef FALLOC_FL_INSERT_RANGE
500 #define FALLOC_FL_INSERT_RANGE 0x20 /* insert space within file */
501 #endif
502
503 #ifndef raw_cpu_ptr
504 #define raw_cpu_ptr(p) __this_cpu_ptr(p)
505 #endif
506
507 #ifndef HAVE_IS_ROOT_INODE
508 static inline bool is_root_inode(struct inode *inode)
509 {
510         return inode == inode->i_sb->s_root->d_inode;
511 }
512 #endif
513
514 #ifndef HAVE_REGISTER_SHRINKER_RET
515 #define register_shrinker(_s) (register_shrinker(_s), 0)
516 #endif
517
518 #ifndef fallthrough
519 # if defined(__GNUC__) && __GNUC__ >= 7
520 #  define fallthrough  __attribute__((fallthrough)) /* fallthrough */
521 # else
522 #  define fallthrough do {} while (0)  /* fallthrough */
523 # endif
524 #endif
525
526 static inline void ll_security_release_secctx(char *secdata, u32 seclen)
527 {
528 #ifdef HAVE_SEC_RELEASE_SECCTX_1ARG
529         struct lsmcontext context = { };
530
531         lsmcontext_init(&context, secdata, seclen, 0);
532         return security_release_secctx(&context);
533 #else
534         return security_release_secctx(secdata, seclen);
535 #endif
536 }
537
538 #ifndef HAVE_USER_NAMESPACE_ARG
539 #define posix_acl_update_mode(ns, inode, mode, acl) \
540         posix_acl_update_mode(inode, mode, acl)
541 #define notify_change(ns, de, attr, inode)      notify_change(de, attr, inode)
542 #define inode_owner_or_capable(ns, inode)       inode_owner_or_capable(inode)
543 #define vfs_create(ns, dir, de, mode, ex)       vfs_create(dir, de, mode, ex)
544 #define vfs_mkdir(ns, dir, de, mode)            vfs_mkdir(dir, de, mode)
545 #define ll_set_acl(ns, inode, acl, type)        ll_set_acl(inode, acl, type)
546 #endif
547
548 #endif /* _LUSTRE_COMPAT_H */