Whamcloud - gitweb
LU-13321 tests: force even DNE file distribution
[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 {
121         int rc;
122
123 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
124         rc = vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
125 #else
126         rc = vfs_getattr(path, st);
127 #endif
128         return rc;
129 }
130
131 #ifndef HAVE_D_IS_POSITIVE
132 static inline bool d_is_positive(const struct dentry *dentry)
133 {
134         return dentry->d_inode != NULL;
135 }
136 #endif
137
138 #ifndef HAVE_INODE_LOCK
139 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
140 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
141 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
142 #endif
143
144 #ifndef HAVE_XA_IS_VALUE
145 static inline bool xa_is_value(void *entry)
146 {
147         return radix_tree_exceptional_entry(entry);
148 }
149 #endif
150
151 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
152 static inline void truncate_inode_pages_final(struct address_space *map)
153 {
154         truncate_inode_pages(map, 0);
155 }
156 #endif
157
158 #ifndef HAVE_PTR_ERR_OR_ZERO
159 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
160 {
161         if (IS_ERR(ptr))
162                 return PTR_ERR(ptr);
163         else
164                 return 0;
165 }
166 #endif
167
168 #ifdef HAVE_PID_NS_FOR_CHILDREN
169 # define ll_task_pid_ns(task) \
170          ((task)->nsproxy ? ((task)->nsproxy->pid_ns_for_children) : NULL)
171 #else
172 # define ll_task_pid_ns(task) \
173          ((task)->nsproxy ? ((task)->nsproxy->pid_ns) : NULL)
174 #endif
175
176 #ifdef HAVE_FULL_NAME_HASH_3ARGS
177 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
178 #else
179 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
180 #endif
181
182 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
183 # define posix_acl_xattr_header struct posix_acl_xattr_header
184 # define posix_acl_xattr_entry  struct posix_acl_xattr_entry
185 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
186 #else
187 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
188 #endif
189
190 #ifdef HAVE_IOP_XATTR
191 #define ll_setxattr     generic_setxattr
192 #define ll_getxattr     generic_getxattr
193 #define ll_removexattr  generic_removexattr
194 #endif /* HAVE_IOP_XATTR */
195
196 #ifndef HAVE_VFS_SETXATTR
197 const struct xattr_handler *get_xattr_type(const char *name);
198
199 static inline int
200 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
201                const void *value, size_t size, int flags)
202 {
203         const struct xattr_handler *handler;
204         int rc;
205
206         handler = get_xattr_type(name);
207         if (!handler)
208                 return -EOPNOTSUPP;
209
210 #  if defined(HAVE_XATTR_HANDLER_INODE_PARAM)
211         rc = handler->set(handler, dentry, inode, name, value, size, flags);
212 #  elif defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
213         rc = handler->set(handler, dentry, name, value, size, flags);
214 #  else
215         rc = handler->set(dentry, name, value, size, flags, handler->flags);
216 #  endif /* !HAVE_XATTR_HANDLER_INODE_PARAM */
217         return rc;
218 }
219 #endif /* HAVE_VFS_SETXATTR */
220
221 #ifndef HAVE_POSIX_ACL_VALID_USER_NS
222 #define posix_acl_valid(a,b)            posix_acl_valid(b)
223 #endif
224
225 #ifdef HAVE_IOP_SET_ACL
226 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
227 #ifndef HAVE_POSIX_ACL_UPDATE_MODE
228 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
229                           struct posix_acl **acl)
230 {
231         umode_t mode = inode->i_mode;
232         int error;
233
234         error = posix_acl_equiv_mode(*acl, &mode);
235         if (error < 0)
236                 return error;
237         if (error == 0)
238                 *acl = NULL;
239         if (!in_group_p(inode->i_gid) &&
240             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
241                 mode &= ~S_ISGID;
242         *mode_p = mode;
243         return 0;
244 }
245 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
246 #endif
247 #endif
248
249 #ifndef HAVE_IOV_ITER_TRUNCATE
250 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
251 {
252         if (i->count > count)
253                 i->count = count;
254 }
255 #endif
256
257 /*
258  * mount MS_* flags split from superblock SB_* flags
259  * if the SB_* flags are not available use the MS_* flags
260  */
261 #if !defined(SB_RDONLY) && defined(MS_RDONLY)
262 # define SB_RDONLY MS_RDONLY
263 #endif
264 #if !defined(SB_ACTIVE) && defined(MS_ACTIVE)
265 # define SB_ACTIVE MS_ACTIVE
266 #endif
267 #if !defined(SB_NOSEC) && defined(MS_NOSEC)
268 # define SB_NOSEC MS_NOSEC
269 #endif
270 #if !defined(SB_POSIXACL) && defined(MS_POSIXACL)
271 # define SB_POSIXACL MS_POSIXACL
272 #endif
273 #if !defined(SB_NODIRATIME) && defined(MS_NODIRATIME)
274 # define SB_NODIRATIME MS_NODIRATIME
275 #endif
276
277 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
278 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
279 {
280         i->count = count;
281 }
282
283 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
284 {
285         return (struct iovec) {
286                 .iov_base = iter->iov->iov_base + iter->iov_offset,
287                 .iov_len = min(iter->count,
288                                iter->iov->iov_len - iter->iov_offset),
289         };
290 }
291
292 #define iov_for_each(iov, iter, start)                                  \
293         for (iter = (start);                                            \
294              (iter).count && ((iov = iov_iter_iovec(&(iter))), 1);      \
295              iov_iter_advance(&(iter), (iov).iov_len))
296
297 static inline ssize_t
298 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
299 {
300         struct iovec iov;
301         struct iov_iter i;
302         ssize_t bytes = 0;
303
304         iov_for_each(iov, i, *iter) {
305                 ssize_t res;
306
307                 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
308                 if (res <= 0) {
309                         if (bytes == 0)
310                                 bytes = res;
311                         break;
312                 }
313
314                 bytes += res;
315                 if (res < iov.iov_len)
316                         break;
317         }
318
319         if (bytes > 0)
320                 iov_iter_advance(iter, bytes);
321         return bytes;
322 }
323
324 static inline ssize_t
325 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
326 {
327         struct iovec iov;
328         struct iov_iter i;
329         ssize_t bytes = 0;
330
331         /* Since LLITE updates file size at the end of I/O in
332          * vvp_io_commit_write(), append write has to be done in atomic when
333          * there are multiple segments because otherwise each iteration to
334          * __generic_file_aio_write() will see original file size */
335         if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
336                 struct iovec *iov_copy;
337                 int count = 0;
338
339                 OBD_ALLOC(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
340                 if (!iov_copy)
341                         return -ENOMEM;
342
343                 iov_for_each(iov, i, *iter)
344                         iov_copy[count++] = iov;
345
346                 bytes = __generic_file_aio_write(iocb, iov_copy, count,
347                                                  &iocb->ki_pos);
348                 OBD_FREE(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
349
350                 if (bytes > 0)
351                         iov_iter_advance(iter, bytes);
352                 return bytes;
353         }
354
355         iov_for_each(iov, i, *iter) {
356                 ssize_t res;
357
358                 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
359                 if (res <= 0) {
360                         if (bytes == 0)
361                                 bytes = res;
362                         break;
363                 }
364
365                 bytes += res;
366                 if (res < iov.iov_len)
367                         break;
368         }
369
370         if (bytes > 0)
371                 iov_iter_advance(iter, bytes);
372         return bytes;
373 }
374 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
375
376 static inline void __user *get_vmf_address(struct vm_fault *vmf)
377 {
378 #ifdef HAVE_VM_FAULT_ADDRESS
379         return (void __user *)vmf->address;
380 #else
381         return vmf->virtual_address;
382 #endif
383 }
384
385 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
386 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
387 #else
388 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
389 #endif
390
391 #ifndef HAVE_CURRENT_TIME
392 static inline struct timespec current_time(struct inode *inode)
393 {
394         return CURRENT_TIME;
395 }
396 #endif
397
398 #ifndef time_after32
399 /**
400  * time_after32 - compare two 32-bit relative times
401  * @a: the time which may be after @b
402  * @b: the time which may be before @a
403  *
404  * Needed for kernels earlier than v4.14-rc1~134^2
405  *
406  * time_after32(a, b) returns true if the time @a is after time @b.
407  * time_before32(b, a) returns true if the time @b is before time @a.
408  *
409  * Similar to time_after(), compare two 32-bit timestamps for relative
410  * times.  This is useful for comparing 32-bit seconds values that can't
411  * be converted to 64-bit values (e.g. due to disk format or wire protocol
412  * issues) when it is known that the times are less than 68 years apart.
413  */
414 #define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
415 #define time_before32(b, a)    time_after32(a, b)
416
417 #endif
418
419 #ifndef smp_store_mb
420 #define smp_store_mb(var, value)        set_mb(var, value)
421 #endif
422
423 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
424 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
425 {
426 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
427         return bi->interval_exp ? 1 << bi->interval_exp : 0;
428 #elif defined(HAVE_INTERVAL_BLK_INTEGRITY)
429         return bi->interval;
430 #else
431         return bi->sector_size;
432 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
433 }
434
435 static inline const char *blk_integrity_name(struct blk_integrity *bi)
436 {
437 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
438         return bi->profile->name;
439 #else
440         return bi->name;
441 #endif
442 }
443
444 static inline unsigned int bip_size(struct bio_integrity_payload *bip)
445 {
446 #ifdef HAVE_BIP_ITER_BIO_INTEGRITY_PAYLOAD
447         return bip->bip_iter.bi_size;
448 #else
449         return bip->bip_size;
450 #endif
451 }
452 #else /* !CONFIG_BLK_DEV_INTEGRITY */
453 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
454 {
455         return 0;
456 }
457 static inline const char *blk_integrity_name(struct blk_integrity *bi)
458 {
459         /* gcc8 dislikes when strcmp() is called against NULL */
460         return "";
461 }
462 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
463
464 #ifndef INTEGRITY_FLAG_READ
465 #define INTEGRITY_FLAG_READ BLK_INTEGRITY_VERIFY
466 #endif
467
468 #ifndef INTEGRITY_FLAG_WRITE
469 #define INTEGRITY_FLAG_WRITE BLK_INTEGRITY_GENERATE
470 #endif
471
472 static inline bool bdev_integrity_enabled(struct block_device *bdev, int rw)
473 {
474 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
475         struct blk_integrity *bi = bdev_get_integrity(bdev);
476
477         if (bi == NULL)
478                 return false;
479
480 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
481         if (rw == 0 && bi->profile->verify_fn != NULL &&
482             (bi->flags & INTEGRITY_FLAG_READ))
483                 return true;
484
485         if (rw == 1 && bi->profile->generate_fn != NULL &&
486             (bi->flags & INTEGRITY_FLAG_WRITE))
487                 return true;
488 #else
489         if (rw == 0 && bi->verify_fn != NULL &&
490             (bi->flags & INTEGRITY_FLAG_READ))
491                 return true;
492
493         if (rw == 1 && bi->generate_fn != NULL &&
494             (bi->flags & INTEGRITY_FLAG_WRITE))
495                 return true;
496 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
497 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
498
499         return false;
500 }
501
502 #ifdef HAVE_PAGEVEC_INIT_ONE_PARAM
503 #define ll_pagevec_init(pvec, n) pagevec_init(pvec)
504 #else
505 #define ll_pagevec_init(pvec, n) pagevec_init(pvec, n)
506 #endif
507
508 #ifdef HAVE_D_COUNT
509 #  define ll_d_count(d)         d_count(d)
510 #else
511 #  define ll_d_count(d)         ((d)->d_count)
512 #endif /* HAVE_D_COUNT */
513
514 #ifndef HAVE_IN_COMPAT_SYSCALL
515 #define in_compat_syscall       is_compat_task
516 #endif
517
518 #ifdef HAVE_I_PAGES
519 #define page_tree i_pages
520 #else
521 #define i_pages tree_lock
522 #endif
523
524 #ifndef xa_lock_irqsave
525 #define xa_lock_irqsave(lockp, flags) spin_lock_irqsave(lockp, flags)
526 #define xa_unlock_irqrestore(lockp, flags) spin_unlock_irqrestore(lockp, flags)
527 #endif
528
529 #ifndef HAVE_LOCK_PAGE_MEMCG
530 #define lock_page_memcg(page) do {} while (0)
531 #define unlock_page_memcg(page) do {} while (0)
532 #endif
533
534 #ifndef KMEM_CACHE_USERCOPY
535 #define kmem_cache_create_usercopy(name, size, align, flags, useroffset, \
536                                    usersize, ctor)                       \
537         kmem_cache_create(name, size, align, flags, ctor)
538 #endif
539
540 #ifndef HAVE_LINUX_SELINUX_IS_ENABLED
541 #define selinux_is_enabled() 1
542 #endif
543
544 #endif /* _LUSTRE_COMPAT_H */