Whamcloud - gitweb
LU-10934 llite: integrate statx() API with Lustre
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef _LUSTRE_COMPAT_H
34 #define _LUSTRE_COMPAT_H
35
36 #include <linux/aio.h>
37 #include <linux/fs.h>
38 #include <linux/fs_struct.h>
39 #include <linux/namei.h>
40 #include <linux/pagemap.h>
41 #include <linux/posix_acl_xattr.h>
42 #include <linux/bio.h>
43 #include <linux/xattr.h>
44 #include <linux/workqueue.h>
45 #include <linux/blkdev.h>
46 #include <linux/slab.h>
47
48 #include <libcfs/linux/linux-fs.h>
49 #include <obd_support.h>
50
51 #ifdef HAVE_4ARGS_VFS_SYMLINK
52 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
53                 vfs_symlink(dir, dentry, path, mode)
54 #else
55 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
56                        vfs_symlink(dir, dentry, path)
57 #endif
58
59 #ifdef HAVE_BVEC_ITER
60 #define bio_idx(bio)                    (bio->bi_iter.bi_idx)
61 #define bio_set_sector(bio, sector)     (bio->bi_iter.bi_sector = sector)
62 #define bvl_to_page(bvl)                (bvl->bv_page)
63 #else
64 #define bio_idx(bio)                    (bio->bi_idx)
65 #define bio_set_sector(bio, sector)     (bio->bi_sector = sector)
66 #define bio_sectors(bio)                ((bio)->bi_size >> 9)
67 #define bvl_to_page(bvl)                (bvl->bv_page)
68 #endif
69
70 #ifdef HAVE_BVEC_ITER
71 #define bio_start_sector(bio) (bio->bi_iter.bi_sector)
72 #else
73 #define bio_start_sector(bio) (bio->bi_sector)
74 #endif
75
76 #ifndef HAVE_DENTRY_D_CHILD
77 #define d_child                 d_u.d_child
78 #endif
79
80 #ifdef HAVE_DENTRY_D_U_D_ALIAS
81 #define d_alias                 d_u.d_alias
82 #endif
83
84 #ifndef HAVE_D_IN_LOOKUP
85 static inline int d_in_lookup(struct dentry *dentry)
86 {
87         return false;
88 }
89 #endif
90
91 #ifndef HAVE_VM_FAULT_T
92 #define vm_fault_t int
93 #endif
94
95 #ifndef HAVE_FOP_ITERATE_SHARED
96 #define iterate_shared iterate
97 #endif
98
99 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
100 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
101 #else
102 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
103 #endif
104
105 #ifdef HAVE_VFS_RENAME_5ARGS
106 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
107 #elif defined HAVE_VFS_RENAME_6ARGS
108 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
109 #else
110 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
111 #endif
112
113 #ifdef HAVE_VFS_UNLINK_3ARGS
114 #define ll_vfs_unlink(a, b) vfs_unlink(a, b, NULL)
115 #else
116 #define ll_vfs_unlink(a, b) vfs_unlink(a, b)
117 #endif
118
119 static inline int ll_vfs_getattr(struct path *path, struct kstat *st,
120                                  u32 request_mask, unsigned int flags)
121 {
122         int rc;
123
124 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
125         rc = vfs_getattr(path, st, request_mask, flags);
126 #else
127         rc = vfs_getattr(path, st);
128 #endif
129         return rc;
130 }
131
132 #ifndef HAVE_D_IS_POSITIVE
133 static inline bool d_is_positive(const struct dentry *dentry)
134 {
135         return dentry->d_inode != NULL;
136 }
137 #endif
138
139 #ifndef HAVE_INODE_LOCK
140 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
141 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
142 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
143 #endif
144
145 /* Old kernels lacked both Xarray support and the page cache
146  * using Xarrays. Our back ported Xarray support introduces
147  * the real xa_is_value() but we need a wrapper as well for
148  * the page cache interaction. Lets keep xa_is_value() separate
149  * in old kernels for Xarray support and page cache handling.
150  */
151 #ifdef HAVE_RADIX_TREE_EXCEPTIONAL_ENTRY
152 static inline bool ll_xa_is_value(void *entry)
153 {
154         return radix_tree_exceptional_entry(entry);
155 }
156 #else
157 #define ll_xa_is_value  xa_is_value
158 #endif
159
160 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
161 static inline void truncate_inode_pages_final(struct address_space *map)
162 {
163         truncate_inode_pages(map, 0);
164 }
165 #endif
166
167 #ifndef HAVE_PTR_ERR_OR_ZERO
168 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
169 {
170         if (IS_ERR(ptr))
171                 return PTR_ERR(ptr);
172         else
173                 return 0;
174 }
175 #endif
176
177 #ifdef HAVE_PID_NS_FOR_CHILDREN
178 # define ll_task_pid_ns(task) \
179          ((task)->nsproxy ? ((task)->nsproxy->pid_ns_for_children) : NULL)
180 #else
181 # define ll_task_pid_ns(task) \
182          ((task)->nsproxy ? ((task)->nsproxy->pid_ns) : NULL)
183 #endif
184
185 #ifdef HAVE_FULL_NAME_HASH_3ARGS
186 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
187 #else
188 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
189 #endif
190
191 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
192 # define posix_acl_xattr_header struct posix_acl_xattr_header
193 # define posix_acl_xattr_entry  struct posix_acl_xattr_entry
194 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
195 #else
196 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
197 #endif
198
199 #ifdef HAVE_IOP_XATTR
200 #define ll_setxattr     generic_setxattr
201 #define ll_getxattr     generic_getxattr
202 #define ll_removexattr  generic_removexattr
203 #endif /* HAVE_IOP_XATTR */
204
205 #ifndef HAVE_POSIX_ACL_VALID_USER_NS
206 #define posix_acl_valid(a,b)            posix_acl_valid(b)
207 #endif
208
209 #ifdef HAVE_IOP_SET_ACL
210 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
211 #ifndef HAVE_POSIX_ACL_UPDATE_MODE
212 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
213                           struct posix_acl **acl)
214 {
215         umode_t mode = inode->i_mode;
216         int error;
217
218         error = posix_acl_equiv_mode(*acl, &mode);
219         if (error < 0)
220                 return error;
221         if (error == 0)
222                 *acl = NULL;
223         if (!in_group_p(inode->i_gid) &&
224             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
225                 mode &= ~S_ISGID;
226         *mode_p = mode;
227         return 0;
228 }
229 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
230 #endif
231 #endif
232
233 #ifndef HAVE_IOV_ITER_TRUNCATE
234 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
235 {
236         if (i->count > count)
237                 i->count = count;
238 }
239 #endif
240
241 /*
242  * mount MS_* flags split from superblock SB_* flags
243  * if the SB_* flags are not available use the MS_* flags
244  */
245 #if !defined(SB_RDONLY) && defined(MS_RDONLY)
246 # define SB_RDONLY MS_RDONLY
247 #endif
248 #if !defined(SB_ACTIVE) && defined(MS_ACTIVE)
249 # define SB_ACTIVE MS_ACTIVE
250 #endif
251 #if !defined(SB_NOSEC) && defined(MS_NOSEC)
252 # define SB_NOSEC MS_NOSEC
253 #endif
254 #if !defined(SB_POSIXACL) && defined(MS_POSIXACL)
255 # define SB_POSIXACL MS_POSIXACL
256 #endif
257 #if !defined(SB_NODIRATIME) && defined(MS_NODIRATIME)
258 # define SB_NODIRATIME MS_NODIRATIME
259 #endif
260
261 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
262 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
263 {
264         i->count = count;
265 }
266
267 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
268 {
269         return (struct iovec) {
270                 .iov_base = iter->iov->iov_base + iter->iov_offset,
271                 .iov_len = min(iter->count,
272                                iter->iov->iov_len - iter->iov_offset),
273         };
274 }
275
276 #define iov_for_each(iov, iter, start)                                  \
277         for (iter = (start);                                            \
278              (iter).count && ((iov = iov_iter_iovec(&(iter))), 1);      \
279              iov_iter_advance(&(iter), (iov).iov_len))
280
281 static inline ssize_t
282 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
283 {
284         struct iovec iov;
285         struct iov_iter i;
286         ssize_t bytes = 0;
287
288         iov_for_each(iov, i, *iter) {
289                 ssize_t res;
290
291                 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
292                 if (res <= 0) {
293                         if (bytes == 0)
294                                 bytes = res;
295                         break;
296                 }
297
298                 bytes += res;
299                 if (res < iov.iov_len)
300                         break;
301         }
302
303         if (bytes > 0)
304                 iov_iter_advance(iter, bytes);
305         return bytes;
306 }
307
308 static inline ssize_t
309 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
310 {
311         struct iovec iov;
312         struct iov_iter i;
313         ssize_t bytes = 0;
314
315         /* Since LLITE updates file size at the end of I/O in
316          * vvp_io_commit_write(), append write has to be done in atomic when
317          * there are multiple segments because otherwise each iteration to
318          * __generic_file_aio_write() will see original file size */
319         if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
320                 struct iovec *iov_copy;
321                 int count = 0;
322
323                 OBD_ALLOC_PTR_ARRAY(iov_copy, iter->nr_segs);
324                 if (!iov_copy)
325                         return -ENOMEM;
326
327                 iov_for_each(iov, i, *iter)
328                         iov_copy[count++] = iov;
329
330                 bytes = __generic_file_aio_write(iocb, iov_copy, count,
331                                                  &iocb->ki_pos);
332                 OBD_FREE_PTR_ARRAY(iov_copy, iter->nr_segs);
333
334                 if (bytes > 0)
335                         iov_iter_advance(iter, bytes);
336                 return bytes;
337         }
338
339         iov_for_each(iov, i, *iter) {
340                 ssize_t res;
341
342                 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
343                 if (res <= 0) {
344                         if (bytes == 0)
345                                 bytes = res;
346                         break;
347                 }
348
349                 bytes += res;
350                 if (res < iov.iov_len)
351                         break;
352         }
353
354         if (bytes > 0)
355                 iov_iter_advance(iter, bytes);
356         return bytes;
357 }
358 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
359
360 static inline void __user *get_vmf_address(struct vm_fault *vmf)
361 {
362 #ifdef HAVE_VM_FAULT_ADDRESS
363         return (void __user *)vmf->address;
364 #else
365         return vmf->virtual_address;
366 #endif
367 }
368
369 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
370 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
371 #else
372 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
373 #endif
374
375 #ifndef HAVE_CURRENT_TIME
376 static inline struct timespec current_time(struct inode *inode)
377 {
378         return CURRENT_TIME;
379 }
380 #endif
381
382 #ifndef time_after32
383 /**
384  * time_after32 - compare two 32-bit relative times
385  * @a: the time which may be after @b
386  * @b: the time which may be before @a
387  *
388  * Needed for kernels earlier than v4.14-rc1~134^2
389  *
390  * time_after32(a, b) returns true if the time @a is after time @b.
391  * time_before32(b, a) returns true if the time @b is before time @a.
392  *
393  * Similar to time_after(), compare two 32-bit timestamps for relative
394  * times.  This is useful for comparing 32-bit seconds values that can't
395  * be converted to 64-bit values (e.g. due to disk format or wire protocol
396  * issues) when it is known that the times are less than 68 years apart.
397  */
398 #define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
399 #define time_before32(b, a)    time_after32(a, b)
400
401 #endif
402
403 #ifndef smp_store_mb
404 #define smp_store_mb(var, value)        set_mb(var, value)
405 #endif
406
407 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
408 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
409 {
410 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
411         return bi->interval_exp ? 1 << bi->interval_exp : 0;
412 #elif defined(HAVE_INTERVAL_BLK_INTEGRITY)
413         return bi->interval;
414 #else
415         return bi->sector_size;
416 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
417 }
418
419 static inline const char *blk_integrity_name(struct blk_integrity *bi)
420 {
421 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
422         return bi->profile->name;
423 #else
424         return bi->name;
425 #endif
426 }
427
428 static inline unsigned int bip_size(struct bio_integrity_payload *bip)
429 {
430 #ifdef HAVE_BIP_ITER_BIO_INTEGRITY_PAYLOAD
431         return bip->bip_iter.bi_size;
432 #else
433         return bip->bip_size;
434 #endif
435 }
436 #else /* !CONFIG_BLK_DEV_INTEGRITY */
437 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
438 {
439         return 0;
440 }
441 static inline const char *blk_integrity_name(struct blk_integrity *bi)
442 {
443         /* gcc8 dislikes when strcmp() is called against NULL */
444         return "";
445 }
446 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
447
448 #ifndef INTEGRITY_FLAG_READ
449 #define INTEGRITY_FLAG_READ BLK_INTEGRITY_VERIFY
450 #endif
451
452 #ifndef INTEGRITY_FLAG_WRITE
453 #define INTEGRITY_FLAG_WRITE BLK_INTEGRITY_GENERATE
454 #endif
455
456 static inline bool bdev_integrity_enabled(struct block_device *bdev, int rw)
457 {
458 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
459         struct blk_integrity *bi = bdev_get_integrity(bdev);
460
461         if (bi == NULL)
462                 return false;
463
464 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
465         if (rw == 0 && bi->profile->verify_fn != NULL &&
466             (bi->flags & INTEGRITY_FLAG_READ))
467                 return true;
468
469         if (rw == 1 && bi->profile->generate_fn != NULL &&
470             (bi->flags & INTEGRITY_FLAG_WRITE))
471                 return true;
472 #else
473         if (rw == 0 && bi->verify_fn != NULL &&
474             (bi->flags & INTEGRITY_FLAG_READ))
475                 return true;
476
477         if (rw == 1 && bi->generate_fn != NULL &&
478             (bi->flags & INTEGRITY_FLAG_WRITE))
479                 return true;
480 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
481 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
482
483         return false;
484 }
485
486 #ifdef HAVE_PAGEVEC_INIT_ONE_PARAM
487 #define ll_pagevec_init(pvec, n) pagevec_init(pvec)
488 #else
489 #define ll_pagevec_init(pvec, n) pagevec_init(pvec, n)
490 #endif
491
492 #ifdef HAVE_D_COUNT
493 #  define ll_d_count(d)         d_count(d)
494 #else
495 #  define ll_d_count(d)         ((d)->d_count)
496 #endif /* HAVE_D_COUNT */
497
498 #ifndef HAVE_IN_COMPAT_SYSCALL
499 #define in_compat_syscall       is_compat_task
500 #endif
501
502 #ifdef HAVE_I_PAGES
503 #define page_tree i_pages
504 #define ll_xa_lock_irqsave(lockp, flags) xa_lock_irqsave(lockp, flags)
505 #define ll_xa_unlock_irqrestore(lockp, flags) xa_unlock_irqrestore(lockp, flags)
506 #else
507 #define i_pages tree_lock
508 #define ll_xa_lock_irqsave(lockp, flags) spin_lock_irqsave(lockp, flags)
509 #define ll_xa_unlock_irqrestore(lockp, flags) spin_unlock_irqrestore(lockp, flags)
510 #endif
511
512 #ifndef HAVE_LOCK_PAGE_MEMCG
513 #define lock_page_memcg(page) do {} while (0)
514 #define unlock_page_memcg(page) do {} while (0)
515 #endif
516
517 #ifndef KMEM_CACHE_USERCOPY
518 #define kmem_cache_create_usercopy(name, size, align, flags, useroffset, \
519                                    usersize, ctor)                       \
520         kmem_cache_create(name, size, align, flags, ctor)
521 #endif
522
523 #ifndef HAVE_LINUX_SELINUX_IS_ENABLED
524 #define selinux_is_enabled() 1
525 #endif
526
527 static inline int ll_vfs_getxattr(struct dentry *dentry, struct inode *inode,
528                                   const char *name,
529                                   void *value, size_t size)
530 {
531 #ifndef HAVE_VFS_SETXATTR
532         if (unlikely(!inode->i_op->getxattr))
533                 return -ENODATA;
534
535         return inode->i_op->getxattr(dentry, name, value, size);
536 #else
537         return __vfs_getxattr(dentry, inode, name, value, size);
538 #endif
539 }
540
541 static inline int ll_vfs_setxattr(struct dentry *dentry, struct inode *inode,
542                                   const char *name,
543                                   const void *value, size_t size, int flags)
544 {
545 #ifndef HAVE_VFS_SETXATTR
546         if (unlikely(!inode->i_op->setxattr))
547                 return -EOPNOTSUPP;
548
549         return inode->i_op->setxattr(dentry, name, value, size, flags);
550 #else
551         return __vfs_setxattr(dentry, inode, name, value, size, flags);
552 #endif
553 }
554
555 static inline int ll_vfs_removexattr(struct dentry *dentry, struct inode *inode,
556                                      const char *name)
557 {
558 #ifndef HAVE_VFS_SETXATTR
559         if (unlikely(!inode->i_op->setxattr))
560                 return -EOPNOTSUPP;
561
562         return inode->i_op->removexattr(dentry, name);
563 #else
564         return __vfs_removexattr(dentry, name);
565 #endif
566 }
567
568 #endif /* _LUSTRE_COMPAT_H */