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