Whamcloud - gitweb
8fb913195019b7f78c6b27716fb5b928ad78ed3f
[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/bio.h>
42 #include <linux/xattr.h>
43 #include <linux/workqueue.h>
44 #include <linux/blkdev.h>
45 #include <linux/slab.h>
46
47 #include <libcfs/linux/linux-fs.h>
48 #include <lustre_patchless_compat.h>
49 #include <obd_support.h>
50
51 #ifdef HAVE_FS_STRUCT_RWLOCK
52 # define LOCK_FS_STRUCT(fs)     write_lock(&(fs)->lock)
53 # define UNLOCK_FS_STRUCT(fs)   write_unlock(&(fs)->lock)
54 #else
55 # define LOCK_FS_STRUCT(fs)     spin_lock(&(fs)->lock)
56 # define UNLOCK_FS_STRUCT(fs)   spin_unlock(&(fs)->lock)
57 #endif
58
59 #ifdef HAVE_FS_STRUCT_SEQCOUNT
60 # define WRITE_FS_SEQ_BEGIN(fs) write_seqcount_begin(&(fs)->seq)
61 # define WRITE_FS_SEQ_END(fs)   write_seqcount_end(&(fs)->seq)
62 #else
63 # define WRITE_FS_SEQ_BEGIN(fs)
64 # define WRITE_FS_SEQ_END(fs)
65 #endif
66 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
67                                  struct dentry *dentry)
68 {
69         struct path path;
70         struct path old_pwd;
71
72         path.mnt = mnt;
73         path.dentry = dentry;
74         path_get(&path);
75         LOCK_FS_STRUCT(fs);
76         WRITE_FS_SEQ_BEGIN(fs);
77         old_pwd = fs->pwd;
78         fs->pwd = path;
79         WRITE_FS_SEQ_END(fs);
80         UNLOCK_FS_STRUCT(fs);
81
82         if (old_pwd.dentry)
83                 path_put(&old_pwd);
84 }
85
86 #define current_ngroups current_cred()->group_info->ngroups
87 #define current_groups current_cred()->group_info->small_block
88
89 /*
90  * OBD need working random driver, thus all our
91  * initialization routines must be called after device
92  * driver initialization
93  */
94 #ifndef MODULE
95 #undef module_init
96 #define module_init(a)     late_initcall(a)
97 #endif
98
99 #ifndef MODULE_ALIAS_FS
100 #define MODULE_ALIAS_FS(name)
101 #endif
102
103 #ifdef HAVE_GENERIC_PERMISSION_2ARGS
104 # define ll_generic_permission(inode, mask, flags, check_acl) \
105          generic_permission(inode, mask)
106 #elif defined HAVE_GENERIC_PERMISSION_4ARGS
107 # define ll_generic_permission(inode, mask, flags, check_acl) \
108          generic_permission(inode, mask, flags, check_acl)
109 #else
110 # define ll_generic_permission(inode, mask, flags, check_acl) \
111          generic_permission(inode, mask, check_acl)
112 #endif
113
114 #ifdef HAVE_4ARGS_VFS_SYMLINK
115 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
116                 vfs_symlink(dir, dentry, path, mode)
117 #else
118 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
119                        vfs_symlink(dir, dentry, path)
120 #endif
121
122 #if !defined(HAVE_FILE_LLSEEK_SIZE) || defined(HAVE_FILE_LLSEEK_SIZE_5ARGS)
123 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
124                 generic_file_llseek_size(file, offset, origin, maxbytes, eof);
125 #else
126 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
127                 generic_file_llseek_size(file, offset, origin, maxbytes);
128 #endif
129
130 #ifdef HAVE_INODE_DIO_WAIT
131 /* inode_dio_wait(i) use as-is for write lock */
132 # define inode_dio_write_done(i)        do {} while (0) /* for write unlock */
133 #else
134 # define inode_dio_wait(i)              down_write(&(i)->i_alloc_sem)
135 # define inode_dio_write_done(i)        up_write(&(i)->i_alloc_sem)
136 #endif
137
138 #ifndef HAVE_SIMPLE_SETATTR
139 #define simple_setattr(dentry, ops) inode_setattr((dentry)->d_inode, ops)
140 #endif
141
142 #ifndef HAVE_INIT_LIST_HEAD_RCU
143 static inline void INIT_LIST_HEAD_RCU(struct list_head *list)
144 {
145         WRITE_ONCE(list->next, list);
146         WRITE_ONCE(list->prev, list);
147 }
148 #endif
149
150 #ifndef HAVE_DQUOT_SUSPEND
151 # define ll_vfs_dq_init             vfs_dq_init
152 # define ll_vfs_dq_drop             vfs_dq_drop
153 # define ll_vfs_dq_transfer         vfs_dq_transfer
154 # define ll_vfs_dq_off(sb, remount) vfs_dq_off(sb, remount)
155 #else
156 # define ll_vfs_dq_init             dquot_initialize
157 # define ll_vfs_dq_drop             dquot_drop
158 # define ll_vfs_dq_transfer         dquot_transfer
159 # define ll_vfs_dq_off(sb, remount) dquot_suspend(sb, -1)
160 #endif
161
162 #ifndef HAVE_BLKDEV_GET_BY_DEV
163 # define blkdev_get_by_dev(dev, mode, holder) open_by_devnum(dev, mode)
164 #endif
165
166 #ifdef HAVE_BVEC_ITER
167 #define bio_idx(bio)                    (bio->bi_iter.bi_idx)
168 #define bio_set_sector(bio, sector)     (bio->bi_iter.bi_sector = sector)
169 #define bvl_to_page(bvl)                (bvl->bv_page)
170 #else
171 #define bio_idx(bio)                    (bio->bi_idx)
172 #define bio_set_sector(bio, sector)     (bio->bi_sector = sector)
173 #define bio_sectors(bio)                ((bio)->bi_size >> 9)
174 #ifndef HAVE_BIO_END_SECTOR
175 #define bio_end_sector(bio)             (bio->bi_sector + bio_sectors(bio))
176 #endif
177 #define bvl_to_page(bvl)                (bvl->bv_page)
178 #endif
179
180 #ifdef HAVE_BVEC_ITER
181 #define bio_start_sector(bio) (bio->bi_iter.bi_sector)
182 #else
183 #define bio_start_sector(bio) (bio->bi_sector)
184 #endif
185
186 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
187 #define blk_queue_max_segments(rq, seg)                      \
188         do { blk_queue_max_phys_segments(rq, seg);           \
189              blk_queue_max_hw_segments(rq, seg); } while (0)
190 #else
191 #define queue_max_phys_segments(rq)       queue_max_segments(rq)
192 #define queue_max_hw_segments(rq)         queue_max_segments(rq)
193 #endif
194
195 #ifdef HAVE_BLK_PLUG
196 #define DECLARE_PLUG(plug)      struct blk_plug plug
197 #else /* !HAVE_BLK_PLUG */
198 #define DECLARE_PLUG(name)
199 #define blk_start_plug(plug)    do {} while (0)
200 #define blk_finish_plug(plug)   do {} while (0)
201 #endif
202
203 #ifdef HAVE_KMAP_ATOMIC_HAS_1ARG
204 #define ll_kmap_atomic(a, b)    kmap_atomic(a)
205 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a)
206 #else
207 #define ll_kmap_atomic(a, b)    kmap_atomic(a, b)
208 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a, b)
209 #endif
210
211 #ifndef HAVE_CLEAR_INODE
212 #define clear_inode(i)          end_writeback(i)
213 #endif
214
215 #ifndef HAVE_DENTRY_D_CHILD
216 #define d_child                 d_u.d_child
217 #endif
218
219 #ifdef HAVE_DENTRY_D_U_D_ALIAS
220 #define d_alias                 d_u.d_alias
221 #endif
222
223 #ifndef DATA_FOR_LLITE_IS_LIST
224 #define ll_d_hlist_node hlist_node
225 #define ll_d_hlist_empty(list) hlist_empty(list)
226 #define ll_d_hlist_entry(ptr, type, name) hlist_entry(ptr.first, type, name)
227 #define ll_d_hlist_for_each(tmp, i_dentry) hlist_for_each(tmp, i_dentry)
228 # ifdef HAVE_HLIST_FOR_EACH_3ARG
229 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
230         p = NULL; hlist_for_each_entry(dentry, i_dentry, d_alias)
231 # else
232 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
233         hlist_for_each_entry(dentry, p, i_dentry, d_alias)
234 # endif
235 #define DECLARE_LL_D_HLIST_NODE_PTR(name) struct ll_d_hlist_node *name
236 #else
237 #define ll_d_hlist_node list_head
238 #define ll_d_hlist_empty(list) list_empty(list)
239 #define ll_d_hlist_entry(ptr, type, name) list_entry(ptr.next, type, name)
240 #define ll_d_hlist_for_each(tmp, i_dentry) list_for_each(tmp, i_dentry)
241 #define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
242         list_for_each_entry(dentry, i_dentry, d_alias)
243 #define DECLARE_LL_D_HLIST_NODE_PTR(name) /* nothing */
244 #endif /* !DATA_FOR_LLITE_IS_LIST */
245
246 #ifndef HAVE_D_IN_LOOKUP
247 static inline int d_in_lookup(struct dentry *dentry)
248 {
249         return false;
250 }
251 #endif
252
253 #ifndef QUOTA_OK
254 # define QUOTA_OK 0
255 #endif
256 #ifndef NO_QUOTA
257 # define NO_QUOTA (-EDQUOT)
258 #endif
259
260 #ifndef SEEK_DATA
261 #define SEEK_DATA      3       /* seek to the next data */
262 #endif
263 #ifndef SEEK_HOLE
264 #define SEEK_HOLE      4       /* seek to the next hole */
265 #endif
266
267 #ifndef FMODE_UNSIGNED_OFFSET
268 #define FMODE_UNSIGNED_OFFSET   ((__force fmode_t)0x2000)
269 #endif
270
271 #if !defined(_ASM_GENERIC_BITOPS_EXT2_NON_ATOMIC_H_) && !defined(ext2_set_bit)
272 # define ext2_set_bit             __test_and_set_bit_le
273 # define ext2_clear_bit           __test_and_clear_bit_le
274 # define ext2_test_bit            test_bit_le
275 # define ext2_find_first_zero_bit find_first_zero_bit_le
276 # define ext2_find_next_zero_bit  find_next_zero_bit_le
277 #endif
278
279 #ifdef ATTR_TIMES_SET
280 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
281 #else
282 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET)
283 #endif
284
285 #ifndef XATTR_NAME_POSIX_ACL_ACCESS
286 # define XATTR_NAME_POSIX_ACL_ACCESS POSIX_ACL_XATTR_ACCESS
287 #endif
288
289 #ifndef XATTR_NAME_POSIX_ACL_DEFAULT
290 # define XATTR_NAME_POSIX_ACL_DEFAULT POSIX_ACL_XATTR_DEFAULT
291 #endif
292
293 #ifndef HAVE_LM_XXX_LOCK_MANAGER_OPS
294 # define lm_compare_owner       fl_compare_owner
295 #endif
296
297 /*
298  * After 3.1, kernel's nameidata.intent.open.flags is different
299  * with lustre's lookup_intent.it_flags, as lustre's it_flags'
300  * lower bits equal to FMODE_xxx while kernel doesn't transliterate
301  * lower bits of nameidata.intent.open.flags to FMODE_xxx.
302  * */
303 #include <linux/version.h>
304 static inline int ll_namei_to_lookup_intent_flag(int flag)
305 {
306 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
307         flag = (flag & ~O_ACCMODE) | OPEN_FMODE(flag);
308 #endif
309         return flag;
310 }
311
312 #include <linux/fs.h>
313 #ifndef HAVE_PROTECT_I_NLINK
314 static inline void set_nlink(struct inode *inode, unsigned int nlink)
315 {
316         inode->i_nlink = nlink;
317 }
318 #endif
319
320 #ifdef HAVE_INODEOPS_USE_UMODE_T
321 # define ll_umode_t     umode_t
322 #else
323 # define ll_umode_t     int
324 #endif
325
326 #include <linux/dcache.h>
327 #ifndef HAVE_D_MAKE_ROOT
328 static inline struct dentry *d_make_root(struct inode *root)
329 {
330         struct dentry *res = d_alloc_root(root);
331
332         if (res == NULL && root)
333                 iput(root);
334
335         return res;
336 }
337 #endif
338
339 #ifdef HAVE_DIRTY_INODE_HAS_FLAG
340 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode), flag)
341 #else
342 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode))
343 #endif
344
345 #ifdef HAVE_FILE_F_INODE
346 # define set_file_inode(file, inode)    (file)->f_inode = inode
347 #else
348 # define set_file_inode(file, inode)
349 #endif
350
351 #ifndef HAVE_FILE_INODE
352 static inline struct inode *file_inode(const struct file *file)
353 {
354         return file->f_path.dentry->d_inode;
355 }
356 #endif
357
358 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
359 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
360 #else
361 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
362 #endif
363
364 #ifdef HAVE_VFS_RENAME_5ARGS
365 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
366 #elif defined HAVE_VFS_RENAME_6ARGS
367 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
368 #else
369 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
370 #endif
371
372 #ifdef HAVE_VFS_UNLINK_3ARGS
373 #define ll_vfs_unlink(a, b) vfs_unlink(a, b, NULL)
374 #else
375 #define ll_vfs_unlink(a, b) vfs_unlink(a, b)
376 #endif
377
378 #ifndef HAVE_INODE_OWNER_OR_CAPABLE
379 #define inode_owner_or_capable(inode) is_owner_or_cap(inode)
380 #endif
381
382 static inline int ll_vfs_getattr(struct path *path, struct kstat *st)
383 {
384         int rc;
385
386 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
387         rc = vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
388 #elif defined HAVE_VFS_GETATTR_2ARGS
389         rc = vfs_getattr(path, st);
390 #else
391         rc = vfs_getattr(path->mnt, path->dentry, st);
392 #endif
393         return rc;
394 }
395
396 #ifndef HAVE_D_IS_POSITIVE
397 static inline bool d_is_positive(const struct dentry *dentry)
398 {
399         return dentry->d_inode != NULL;
400 }
401 #endif
402
403 #ifdef HAVE_VFS_CREATE_USE_NAMEIDATA
404 # define LL_VFS_CREATE_FALSE NULL
405 #else
406 # define LL_VFS_CREATE_FALSE false
407 #endif
408
409 #ifndef HAVE_INODE_LOCK
410 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
411 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
412 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
413 #endif
414
415 #ifndef HAVE_RADIX_EXCEPTION_ENTRY
416 static inline int radix_tree_exceptional_entry(void *arg)
417 {
418         return 0;
419 }
420 #endif
421
422 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
423 static inline void truncate_inode_pages_final(struct address_space *map)
424 {
425         truncate_inode_pages(map, 0);
426 }
427 #endif
428
429 #ifndef HAVE_PTR_ERR_OR_ZERO
430 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
431 {
432         if (IS_ERR(ptr))
433                 return PTR_ERR(ptr);
434         else
435                 return 0;
436 }
437 #endif
438
439 #ifndef SIZE_MAX
440 #define SIZE_MAX        (~(size_t)0)
441 #endif
442
443 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
444 # define ll_security_inode_init_security(inode, dir, name, value, len, \
445                                          initxattrs, dentry)           \
446          security_inode_init_security(inode, dir, &((dentry)->d_name), \
447                                       initxattrs, dentry)
448 #elif defined HAVE_SECURITY_IINITSEC_QSTR
449 # define ll_security_inode_init_security(inode, dir, name, value, len, \
450                                          initxattrs, dentry)           \
451          security_inode_init_security(inode, dir, &((dentry)->d_name), \
452                                       name, value, len)
453 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK && !HAVE_SECURITY_IINITSEC_QSTR */
454 # define ll_security_inode_init_security(inode, dir, name, value, len, \
455                                          initxattrs, dentry)           \
456          security_inode_init_security(inode, dir, name, value, len)
457 #endif
458
459 #ifndef bio_for_each_segment_all /* since kernel version 3.9 */
460 #ifdef HAVE_BVEC_ITER
461 #define bio_for_each_segment_all(bv, bio, it) \
462         for (it = 0, bv = (bio)->bi_io_vec; it < (bio)->bi_vcnt; it++, bv++)
463 #else
464 #define bio_for_each_segment_all(bv, bio, it) bio_for_each_segment(bv, bio, it)
465 #endif
466 #endif
467
468 #ifdef HAVE_PID_NS_FOR_CHILDREN
469 # define ll_task_pid_ns(task)   ((task)->nsproxy->pid_ns_for_children)
470 #else
471 # define ll_task_pid_ns(task)   ((task)->nsproxy->pid_ns)
472 #endif
473
474 #ifdef HAVE_FULL_NAME_HASH_3ARGS
475 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
476 #else
477 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
478 #endif
479
480 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
481 # define posix_acl_xattr_header struct posix_acl_xattr_header
482 # define posix_acl_xattr_entry  struct posix_acl_xattr_entry
483 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
484 #else
485 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
486 #endif
487
488 #ifdef HAVE_IOP_XATTR
489 #ifdef HAVE_XATTR_HANDLER_FLAGS
490 #define ll_setxattr     generic_setxattr
491 #define ll_getxattr     generic_getxattr
492 #define ll_removexattr  generic_removexattr
493 #else
494 int ll_setxattr(struct dentry *dentry, const char *name,
495                 const void *value, size_t size, int flags);
496 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
497                     void *buf, size_t buf_size);
498 int ll_removexattr(struct dentry *dentry, const char *name);
499 #endif /* ! HAVE_XATTR_HANDLER_FLAGS */
500 #endif /* HAVE_IOP_XATTR */
501
502 #ifndef HAVE_VFS_SETXATTR
503 const struct xattr_handler *get_xattr_type(const char *name);
504
505 static inline int
506 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
507                const void *value, size_t size, int flags)
508 {
509 # ifdef HAVE_XATTR_HANDLER_FLAGS
510         const struct xattr_handler *handler;
511         int rc;
512
513         handler = get_xattr_type(name);
514         if (!handler)
515                 return -EOPNOTSUPP;
516
517 #  if defined(HAVE_XATTR_HANDLER_INODE_PARAM)
518         rc = handler->set(handler, dentry, inode, name, value, size, flags);
519 #  elif defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
520         rc = handler->set(handler, dentry, name, value, size, flags);
521 #  else
522         rc = handler->set(dentry, name, value, size, flags, handler->flags);
523 #  endif /* !HAVE_XATTR_HANDLER_INODE_PARAM */
524         return rc;
525 # else /* !HAVE_XATTR_HANDLER_FLAGS */
526         return ll_setxattr(dentry, name, value, size, flags);
527 # endif /* HAVE_XATTR_HANDLER_FLAGS */
528 }
529 #endif /* HAVE_VFS_SETXATTR */
530
531 #ifdef HAVE_IOP_SET_ACL
532 #ifdef CONFIG_FS_POSIX_ACL
533 #ifndef HAVE_POSIX_ACL_UPDATE_MODE
534 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
535                           struct posix_acl **acl)
536 {
537         umode_t mode = inode->i_mode;
538         int error;
539
540         error = posix_acl_equiv_mode(*acl, &mode);
541         if (error < 0)
542                 return error;
543         if (error == 0)
544                 *acl = NULL;
545         if (!in_group_p(inode->i_gid) &&
546             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
547                 mode &= ~S_ISGID;
548         *mode_p = mode;
549         return 0;
550 }
551 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
552 #endif
553 #endif
554
555 #ifndef HAVE_IOV_ITER_TRUNCATE
556 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
557 {
558         if (i->count > count)
559                 i->count = count;
560 }
561 #endif
562
563 #ifndef HAVE_IS_SXID
564 static inline bool is_sxid(umode_t mode)
565 {
566         return (mode & S_ISUID) || ((mode & S_ISGID) && (mode & S_IXGRP));
567 }
568 #endif
569
570 #ifndef IS_NOSEC
571 #define IS_NOSEC(inode) (!is_sxid(inode->i_mode))
572 #endif
573
574 /*
575  * mount MS_* flags split from superblock SB_* flags
576  * if the SB_* flags are not available use the MS_* flags
577  */
578 #if !defined(SB_RDONLY) && defined(MS_RDONLY)
579 # define SB_RDONLY MS_RDONLY
580 #endif
581 #if !defined(SB_ACTIVE) && defined(MS_ACTIVE)
582 # define SB_ACTIVE MS_ACTIVE
583 #endif
584 #if !defined(SB_NOSEC) && defined(MS_NOSEC)
585 # define SB_NOSEC MS_NOSEC
586 #endif
587 #if !defined(SB_POSIXACL) && defined(MS_POSIXACL)
588 # define SB_POSIXACL MS_POSIXACL
589 #endif
590 #if !defined(SB_NODIRATIME) && defined(MS_NODIRATIME)
591 # define SB_NODIRATIME MS_NODIRATIME
592 #endif
593
594 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
595 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
596 {
597         i->count = count;
598 }
599
600 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
601 {
602         return (struct iovec) {
603                 .iov_base = iter->iov->iov_base + iter->iov_offset,
604                 .iov_len = min(iter->count,
605                                iter->iov->iov_len - iter->iov_offset),
606         };
607 }
608
609 #define iov_for_each(iov, iter, start)                                  \
610         for (iter = (start);                                            \
611              (iter).count && ((iov = iov_iter_iovec(&(iter))), 1);      \
612              iov_iter_advance(&(iter), (iov).iov_len))
613
614 static inline ssize_t
615 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
616 {
617         struct iovec iov;
618         struct iov_iter i;
619         ssize_t bytes = 0;
620
621         iov_for_each(iov, i, *iter) {
622                 ssize_t res;
623
624                 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
625                 if (res <= 0) {
626                         if (bytes == 0)
627                                 bytes = res;
628                         break;
629                 }
630
631                 bytes += res;
632                 if (res < iov.iov_len)
633                         break;
634         }
635
636         if (bytes > 0)
637                 iov_iter_advance(iter, bytes);
638         return bytes;
639 }
640
641 static inline ssize_t
642 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
643 {
644         struct iovec iov;
645         struct iov_iter i;
646         ssize_t bytes = 0;
647
648         /* Since LLITE updates file size at the end of I/O in
649          * vvp_io_commit_write(), append write has to be done in atomic when
650          * there are multiple segments because otherwise each iteration to
651          * __generic_file_aio_write() will see original file size */
652         if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
653                 struct iovec *iov_copy;
654                 int count = 0;
655
656                 OBD_ALLOC(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
657                 if (!iov_copy)
658                         return -ENOMEM;
659
660                 iov_for_each(iov, i, *iter)
661                         iov_copy[count++] = iov;
662
663                 bytes = __generic_file_aio_write(iocb, iov_copy, count,
664                                                  &iocb->ki_pos);
665                 OBD_FREE(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
666
667                 if (bytes > 0)
668                         iov_iter_advance(iter, bytes);
669                 return bytes;
670         }
671
672         iov_for_each(iov, i, *iter) {
673                 ssize_t res;
674
675                 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
676                 if (res <= 0) {
677                         if (bytes == 0)
678                                 bytes = res;
679                         break;
680                 }
681
682                 bytes += res;
683                 if (res < iov.iov_len)
684                         break;
685         }
686
687         if (bytes > 0)
688                 iov_iter_advance(iter, bytes);
689         return bytes;
690 }
691 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
692
693 static inline void __user *get_vmf_address(struct vm_fault *vmf)
694 {
695 #ifdef HAVE_VM_FAULT_ADDRESS
696         return (void __user *)vmf->address;
697 #else
698         return vmf->virtual_address;
699 #endif
700 }
701
702 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
703 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
704 #else
705 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
706 #endif
707
708 #ifndef HAVE_CURRENT_TIME
709 static inline struct timespec current_time(struct inode *inode)
710 {
711         return CURRENT_TIME;
712 }
713 #endif
714
715 #ifndef time_after32
716 /**
717  * time_after32 - compare two 32-bit relative times
718  * @a: the time which may be after @b
719  * @b: the time which may be before @a
720  *
721  * time_after32(a, b) returns true if the time @a is after time @b.
722  * time_before32(b, a) returns true if the time @b is before time @a.
723  *
724  * Similar to time_after(), compare two 32-bit timestamps for relative
725  * times.  This is useful for comparing 32-bit seconds values that can't
726  * be converted to 64-bit values (e.g. due to disk format or wire protocol
727  * issues) when it is known that the times are less than 68 years apart.
728  */
729 #define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
730 #define time_before32(b, a)    time_after32(a, b)
731
732 #endif
733
734 #ifndef __GFP_COLD
735 #define __GFP_COLD 0
736 #endif
737
738 #ifndef alloc_workqueue
739 #define alloc_workqueue(name, flags, max_active) create_workqueue(name)
740 #endif
741
742 #ifndef smp_store_mb
743 #define smp_store_mb(var, value)        set_mb(var, value)
744 #endif
745
746 #ifndef READ_ONCE
747 #define READ_ONCE ACCESS_ONCE
748 #endif
749
750 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
751 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
752 {
753 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
754         return bi->interval_exp ? 1 << bi->interval_exp : 0;
755 #elif defined(HAVE_INTERVAL_BLK_INTEGRITY)
756         return bi->interval;
757 #else
758         return bi->sector_size;
759 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
760 }
761
762 static inline const char *blk_integrity_name(struct blk_integrity *bi)
763 {
764 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
765         return bi->profile->name;
766 #else
767         return bi->name;
768 #endif
769 }
770
771 static inline unsigned int bip_size(struct bio_integrity_payload *bip)
772 {
773 #ifdef HAVE_BIP_ITER_BIO_INTEGRITY_PAYLOAD
774         return bip->bip_iter.bi_size;
775 #else
776         return bip->bip_size;
777 #endif
778 }
779 #else /* !CONFIG_BLK_DEV_INTEGRITY */
780 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
781 {
782         return 0;
783 }
784 static inline const char *blk_integrity_name(struct blk_integrity *bi)
785 {
786         /* gcc8 dislikes when strcmp() is called against NULL */
787         return "";
788 }
789 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
790
791 #ifndef INTEGRITY_FLAG_READ
792 #define INTEGRITY_FLAG_READ BLK_INTEGRITY_VERIFY
793 #endif
794
795 #ifndef INTEGRITY_FLAG_WRITE
796 #define INTEGRITY_FLAG_WRITE BLK_INTEGRITY_GENERATE
797 #endif
798
799 static inline bool bdev_integrity_enabled(struct block_device *bdev, int rw)
800 {
801 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
802         struct blk_integrity *bi = bdev_get_integrity(bdev);
803
804         if (bi == NULL)
805                 return false;
806
807 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
808         if (rw == 0 && bi->profile->verify_fn != NULL &&
809             (bi->flags & INTEGRITY_FLAG_READ))
810                 return true;
811
812         if (rw == 1 && bi->profile->generate_fn != NULL &&
813             (bi->flags & INTEGRITY_FLAG_WRITE))
814                 return true;
815 #else
816         if (rw == 0 && bi->verify_fn != NULL &&
817             (bi->flags & INTEGRITY_FLAG_READ))
818                 return true;
819
820         if (rw == 1 && bi->generate_fn != NULL &&
821             (bi->flags & INTEGRITY_FLAG_WRITE))
822                 return true;
823 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
824 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
825
826         return false;
827 }
828
829 #ifdef HAVE_PAGEVEC_INIT_ONE_PARAM
830 #define ll_pagevec_init(pvec, n) pagevec_init(pvec)
831 #else
832 #define ll_pagevec_init(pvec, n) pagevec_init(pvec, n)
833 #endif
834
835 #ifdef HAVE_I_PAGES
836 #define page_tree i_pages
837 #else
838 #define i_pages tree_lock
839 #define xa_lock_irq(lockp) spin_lock_irq(lockp)
840 #define xa_unlock_irq(lockp) spin_unlock_irq(lockp)
841 #endif
842
843 #ifndef KMEM_CACHE_USERCOPY
844 #define kmem_cache_create_usercopy(name, size, align, flags, useroffset, \
845                                    usersize, ctor)                       \
846         kmem_cache_create(name, size, align, flags, ctor)
847 #endif
848
849 #endif /* _LUSTRE_COMPAT_H */