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