Whamcloud - gitweb
LU-10030 clio: don't use spare bits in iattr.ia_valid
[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 #define LTIME_S(time)                   (time.tv_sec)
103
104 #ifdef HAVE_GENERIC_PERMISSION_2ARGS
105 # define ll_generic_permission(inode, mask, flags, check_acl) \
106          generic_permission(inode, mask)
107 #elif defined HAVE_GENERIC_PERMISSION_4ARGS
108 # define ll_generic_permission(inode, mask, flags, check_acl) \
109          generic_permission(inode, mask, flags, check_acl)
110 #else
111 # define ll_generic_permission(inode, mask, flags, check_acl) \
112          generic_permission(inode, mask, check_acl)
113 #endif
114
115 #ifdef HAVE_4ARGS_VFS_SYMLINK
116 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
117                 vfs_symlink(dir, dentry, path, mode)
118 #else
119 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
120                        vfs_symlink(dir, dentry, path)
121 #endif
122
123 #if !defined(HAVE_FILE_LLSEEK_SIZE) || defined(HAVE_FILE_LLSEEK_SIZE_5ARGS)
124 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
125                 generic_file_llseek_size(file, offset, origin, maxbytes, eof);
126 #else
127 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
128                 generic_file_llseek_size(file, offset, origin, maxbytes);
129 #endif
130
131 #ifdef HAVE_INODE_DIO_WAIT
132 /* inode_dio_wait(i) use as-is for write lock */
133 # define inode_dio_write_done(i)        do {} while (0) /* for write unlock */
134 #else
135 # define inode_dio_wait(i)              down_write(&(i)->i_alloc_sem)
136 # define inode_dio_write_done(i)        up_write(&(i)->i_alloc_sem)
137 #endif
138
139 #ifndef FS_HAS_FIEMAP
140 #define FS_HAS_FIEMAP                   (0)
141 #endif
142
143 #ifndef HAVE_SIMPLE_SETATTR
144 #define simple_setattr(dentry, ops) inode_setattr((dentry)->d_inode, ops)
145 #endif
146
147 #ifndef SLAB_DESTROY_BY_RCU
148 #define SLAB_DESTROY_BY_RCU 0
149 #endif
150
151 #ifndef HAVE_DQUOT_SUSPEND
152 # define ll_vfs_dq_init             vfs_dq_init
153 # define ll_vfs_dq_drop             vfs_dq_drop
154 # define ll_vfs_dq_transfer         vfs_dq_transfer
155 # define ll_vfs_dq_off(sb, remount) vfs_dq_off(sb, remount)
156 #else
157 # define ll_vfs_dq_init             dquot_initialize
158 # define ll_vfs_dq_drop             dquot_drop
159 # define ll_vfs_dq_transfer         dquot_transfer
160 # define ll_vfs_dq_off(sb, remount) dquot_suspend(sb, -1)
161 #endif
162
163 #ifndef HAVE_BLKDEV_GET_BY_DEV
164 # define blkdev_get_by_dev(dev, mode, holder) open_by_devnum(dev, mode)
165 #endif
166
167 #ifdef HAVE_BVEC_ITER
168 #define bio_idx(bio)                    (bio->bi_iter.bi_idx)
169 #define bio_set_sector(bio, sector)     (bio->bi_iter.bi_sector = sector)
170 #define bvl_to_page(bvl)                (bvl->bv_page)
171 #else
172 #define bio_idx(bio)                    (bio->bi_idx)
173 #define bio_set_sector(bio, sector)     (bio->bi_sector = sector)
174 #define bio_sectors(bio)                ((bio)->bi_size >> 9)
175 #ifndef HAVE_BIO_END_SECTOR
176 #define bio_end_sector(bio)             (bio->bi_sector + bio_sectors(bio))
177 #endif
178 #define bvl_to_page(bvl)                (bvl->bv_page)
179 #endif
180
181 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
182 #define blk_queue_max_segments(rq, seg)                      \
183         do { blk_queue_max_phys_segments(rq, seg);           \
184              blk_queue_max_hw_segments(rq, seg); } while (0)
185 #else
186 #define queue_max_phys_segments(rq)       queue_max_segments(rq)
187 #define queue_max_hw_segments(rq)         queue_max_segments(rq)
188 #endif
189
190 #ifdef HAVE_BLK_PLUG
191 #define DECLARE_PLUG(plug)      struct blk_plug plug
192 #else /* !HAVE_BLK_PLUG */
193 #define DECLARE_PLUG(name)
194 #define blk_start_plug(plug)    do {} while (0)
195 #define blk_finish_plug(plug)   do {} while (0)
196 #endif
197
198 #ifdef HAVE_KMAP_ATOMIC_HAS_1ARG
199 #define ll_kmap_atomic(a, b)    kmap_atomic(a)
200 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a)
201 #else
202 #define ll_kmap_atomic(a, b)    kmap_atomic(a, b)
203 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a, b)
204 #endif
205
206 #ifndef HAVE_CLEAR_INODE
207 #define clear_inode(i)          end_writeback(i)
208 #endif
209
210 #ifndef HAVE_DENTRY_D_CHILD
211 #define d_child                 d_u.d_child
212 #endif
213
214 #ifdef HAVE_DENTRY_D_U_D_ALIAS
215 #define d_alias                 d_u.d_alias
216 #endif
217
218 #ifndef DATA_FOR_LLITE_IS_LIST
219 #define ll_d_hlist_node hlist_node
220 #define ll_d_hlist_empty(list) hlist_empty(list)
221 #define ll_d_hlist_entry(ptr, type, name) hlist_entry(ptr.first, type, name)
222 #define ll_d_hlist_for_each(tmp, i_dentry) hlist_for_each(tmp, i_dentry)
223 # ifdef HAVE_HLIST_FOR_EACH_3ARG
224 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
225         p = NULL; hlist_for_each_entry(dentry, i_dentry, d_alias)
226 # else
227 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
228         hlist_for_each_entry(dentry, p, i_dentry, d_alias)
229 # endif
230 #define DECLARE_LL_D_HLIST_NODE_PTR(name) struct ll_d_hlist_node *name
231 #else
232 #define ll_d_hlist_node list_head
233 #define ll_d_hlist_empty(list) list_empty(list)
234 #define ll_d_hlist_entry(ptr, type, name) list_entry(ptr.next, type, name)
235 #define ll_d_hlist_for_each(tmp, i_dentry) list_for_each(tmp, i_dentry)
236 #define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
237         list_for_each_entry(dentry, i_dentry, d_alias)
238 #define DECLARE_LL_D_HLIST_NODE_PTR(name) /* nothing */
239 #endif /* !DATA_FOR_LLITE_IS_LIST */
240
241 #ifndef QUOTA_OK
242 # define QUOTA_OK 0
243 #endif
244 #ifndef NO_QUOTA
245 # define NO_QUOTA (-EDQUOT)
246 #endif
247
248 #ifndef SEEK_DATA
249 #define SEEK_DATA      3       /* seek to the next data */
250 #endif
251 #ifndef SEEK_HOLE
252 #define SEEK_HOLE      4       /* seek to the next hole */
253 #endif
254
255 #ifndef FMODE_UNSIGNED_OFFSET
256 #define FMODE_UNSIGNED_OFFSET   ((__force fmode_t)0x2000)
257 #endif
258
259 #if !defined(_ASM_GENERIC_BITOPS_EXT2_NON_ATOMIC_H_) && !defined(ext2_set_bit)
260 # define ext2_set_bit             __test_and_set_bit_le
261 # define ext2_clear_bit           __test_and_clear_bit_le
262 # define ext2_test_bit            test_bit_le
263 # define ext2_find_first_zero_bit find_first_zero_bit_le
264 # define ext2_find_next_zero_bit  find_next_zero_bit_le
265 #endif
266
267 #ifdef ATTR_TIMES_SET
268 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
269 #else
270 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET)
271 #endif
272
273 #ifndef XATTR_NAME_POSIX_ACL_ACCESS
274 # define XATTR_NAME_POSIX_ACL_ACCESS POSIX_ACL_XATTR_ACCESS
275 #endif
276
277 #ifndef XATTR_NAME_POSIX_ACL_DEFAULT
278 # define XATTR_NAME_POSIX_ACL_DEFAULT POSIX_ACL_XATTR_DEFAULT
279 #endif
280
281 #ifndef HAVE_LM_XXX_LOCK_MANAGER_OPS
282 # define lm_compare_owner       fl_compare_owner
283 #endif
284
285 /*
286  * After 3.1, kernel's nameidata.intent.open.flags is different
287  * with lustre's lookup_intent.it_flags, as lustre's it_flags'
288  * lower bits equal to FMODE_xxx while kernel doesn't transliterate
289  * lower bits of nameidata.intent.open.flags to FMODE_xxx.
290  * */
291 #include <linux/version.h>
292 static inline int ll_namei_to_lookup_intent_flag(int flag)
293 {
294 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
295         flag = (flag & ~O_ACCMODE) | OPEN_FMODE(flag);
296 #endif
297         return flag;
298 }
299
300 #include <linux/fs.h>
301 #ifndef HAVE_PROTECT_I_NLINK
302 static inline void set_nlink(struct inode *inode, unsigned int nlink)
303 {
304         inode->i_nlink = nlink;
305 }
306 #endif
307
308 #ifdef HAVE_INODEOPS_USE_UMODE_T
309 # define ll_umode_t     umode_t
310 #else
311 # define ll_umode_t     int
312 #endif
313
314 #include <linux/dcache.h>
315 #ifndef HAVE_D_MAKE_ROOT
316 static inline struct dentry *d_make_root(struct inode *root)
317 {
318         struct dentry *res = d_alloc_root(root);
319
320         if (res == NULL && root)
321                 iput(root);
322
323         return res;
324 }
325 #endif
326
327 #ifdef HAVE_DIRTY_INODE_HAS_FLAG
328 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode), flag)
329 #else
330 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode))
331 #endif
332
333 #ifdef HAVE_FILE_F_INODE
334 # define set_file_inode(file, inode)    (file)->f_inode = inode
335 #else
336 # define set_file_inode(file, inode)
337 #endif
338
339 #ifndef HAVE_FILE_INODE
340 static inline struct inode *file_inode(const struct file *file)
341 {
342         return file->f_path.dentry->d_inode;
343 }
344 #endif
345
346 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
347 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
348 #else
349 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
350 #endif
351
352 #ifdef HAVE_VFS_RENAME_5ARGS
353 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
354 #elif defined HAVE_VFS_RENAME_6ARGS
355 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
356 #else
357 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
358 #endif
359
360 #ifdef HAVE_VFS_UNLINK_3ARGS
361 #define ll_vfs_unlink(a, b) vfs_unlink(a, b, NULL)
362 #else
363 #define ll_vfs_unlink(a, b) vfs_unlink(a, b)
364 #endif
365
366 #ifndef HAVE_INODE_LOCK
367 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
368 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
369 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
370 #endif
371
372 #ifndef HAVE_RADIX_EXCEPTION_ENTRY
373 static inline int radix_tree_exceptional_entry(void *arg)
374 {
375         return 0;
376 }
377 #endif
378
379 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
380 static inline void truncate_inode_pages_final(struct address_space *map)
381 {
382         truncate_inode_pages(map, 0);
383                 /* Workaround for LU-118 */
384         if (map->nrpages) {
385                 spin_lock_irq(&map->tree_lock);
386                 spin_unlock_irq(&map->tree_lock);
387         }       /* Workaround end */
388 }
389 #endif
390
391 #ifndef HAVE_PTR_ERR_OR_ZERO
392 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
393 {
394         if (IS_ERR(ptr))
395                 return PTR_ERR(ptr);
396         else
397                 return 0;
398 }
399 #endif
400
401 #ifndef SIZE_MAX
402 #define SIZE_MAX        (~(size_t)0)
403 #endif
404
405 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
406 # define ll_security_inode_init_security(inode, dir, name, value, len, \
407                                          initxattrs, dentry)           \
408          security_inode_init_security(inode, dir, &((dentry)->d_name), \
409                                       initxattrs, dentry)
410 #elif defined HAVE_SECURITY_IINITSEC_QSTR
411 # define ll_security_inode_init_security(inode, dir, name, value, len, \
412                                          initxattrs, dentry)           \
413          security_inode_init_security(inode, dir, &((dentry)->d_name), \
414                                       name, value, len)
415 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK && !HAVE_SECURITY_IINITSEC_QSTR */
416 # define ll_security_inode_init_security(inode, dir, name, value, len, \
417                                          initxattrs, dentry)           \
418          security_inode_init_security(inode, dir, name, value, len)
419 #endif
420
421 #ifndef bio_for_each_segment_all /* since kernel version 3.9 */
422 #ifdef HAVE_BVEC_ITER
423 #define bio_for_each_segment_all(bv, bio, it) \
424         for (it = 0, bv = (bio)->bi_io_vec; it < (bio)->bi_vcnt; it++, bv++)
425 #else
426 #define bio_for_each_segment_all(bv, bio, it) bio_for_each_segment(bv, bio, it)
427 #endif
428 #endif
429
430 #ifdef HAVE_PID_NS_FOR_CHILDREN
431 # define ll_task_pid_ns(task)   ((task)->nsproxy->pid_ns_for_children)
432 #else
433 # define ll_task_pid_ns(task)   ((task)->nsproxy->pid_ns)
434 #endif
435
436 #ifdef HAVE_FULL_NAME_HASH_3ARGS
437 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
438 #else
439 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
440 #endif
441
442 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
443 # define posix_acl_xattr_header struct posix_acl_xattr_header
444 # define posix_acl_xattr_entry  struct posix_acl_xattr_entry
445 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
446 #else
447 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
448 #endif
449
450 #ifdef HAVE_IOP_XATTR
451 #ifdef HAVE_XATTR_HANDLER_FLAGS
452 #define ll_setxattr     generic_setxattr
453 #define ll_getxattr     generic_getxattr
454 #define ll_removexattr  generic_removexattr
455 #else
456 int ll_setxattr(struct dentry *dentry, const char *name,
457                 const void *value, size_t size, int flags);
458 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
459                     void *buf, size_t buf_size);
460 int ll_removexattr(struct dentry *dentry, const char *name);
461 #endif /* ! HAVE_XATTR_HANDLER_FLAGS */
462 #endif /* HAVE_IOP_XATTR */
463
464 #ifndef HAVE_VFS_SETXATTR
465 const struct xattr_handler *get_xattr_type(const char *name);
466
467 static inline int
468 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
469                const void *value, size_t size, int flags)
470 {
471 # ifdef HAVE_XATTR_HANDLER_FLAGS
472         const struct xattr_handler *handler;
473         int rc;
474
475         handler = get_xattr_type(name);
476         if (!handler)
477                 return -EOPNOTSUPP;
478
479 #  if defined(HAVE_XATTR_HANDLER_INODE_PARAM)
480         rc = handler->set(handler, dentry, inode, name, value, size, flags);
481 #  elif defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
482         rc = handler->set(handler, dentry, name, value, size, flags);
483 #  else
484         rc = handler->set(dentry, name, value, size, flags, handler->flags);
485 #  endif /* !HAVE_XATTR_HANDLER_INODE_PARAM */
486         return rc;
487 # else /* !HAVE_XATTR_HANDLER_FLAGS */
488         return ll_setxattr(dentry, name, value, size, flags);
489 # endif /* HAVE_XATTR_HANDLER_FLAGS */
490 }
491 #endif /* HAVE_VFS_SETXATTR */
492
493 #ifdef HAVE_IOP_SET_ACL
494 #ifdef CONFIG_FS_POSIX_ACL
495 #ifndef HAVE_POSIX_ACL_UPDATE_MODE
496 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
497                           struct posix_acl **acl)
498 {
499         umode_t mode = inode->i_mode;
500         int error;
501
502         error = posix_acl_equiv_mode(*acl, &mode);
503         if (error < 0)
504                 return error;
505         if (error == 0)
506                 *acl = NULL;
507         if (!in_group_p(inode->i_gid) &&
508             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
509                 mode &= ~S_ISGID;
510         *mode_p = mode;
511         return 0;
512 }
513 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
514 #endif
515 #endif
516
517 #ifndef HAVE_IOV_ITER_TRUNCATE
518 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
519 {
520         if (i->count > count)
521                 i->count = count;
522 }
523 #endif
524
525 #ifndef HAVE_IS_SXID
526 static inline bool is_sxid(umode_t mode)
527 {
528         return (mode & S_ISUID) || ((mode & S_ISGID) && (mode & S_IXGRP));
529 }
530 #endif
531
532 #ifndef IS_NOSEC
533 #define IS_NOSEC(inode) (!is_sxid(inode->i_mode))
534 #endif
535
536 #ifndef MS_NOSEC
537 static inline void inode_has_no_xattr(struct inode *inode)
538 {
539         return;
540 }
541 #endif
542
543 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
544 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
545 {
546         i->count = count;
547 }
548
549 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
550 {
551         return (struct iovec) {
552                 .iov_base = iter->iov->iov_base + iter->iov_offset,
553                 .iov_len = min(iter->count,
554                                iter->iov->iov_len - iter->iov_offset),
555         };
556 }
557
558 #define iov_for_each(iov, iter, start)                                  \
559         for (iter = (start);                                            \
560              (iter).count && ((iov = iov_iter_iovec(&(iter))), 1);      \
561              iov_iter_advance(&(iter), (iov).iov_len))
562
563 static inline ssize_t
564 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
565 {
566         struct iovec iov;
567         struct iov_iter i;
568         ssize_t bytes = 0;
569
570         iov_for_each(iov, i, *iter) {
571                 ssize_t res;
572
573                 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
574                 if (res <= 0) {
575                         if (bytes == 0)
576                                 bytes = res;
577                         break;
578                 }
579
580                 bytes += res;
581                 if (res < iov.iov_len)
582                         break;
583         }
584
585         if (bytes > 0)
586                 iov_iter_advance(iter, bytes);
587         return bytes;
588 }
589
590 static inline ssize_t
591 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
592 {
593         struct iovec iov;
594         struct iov_iter i;
595         ssize_t bytes = 0;
596
597         /* Since LLITE updates file size at the end of I/O in
598          * vvp_io_commit_write(), append write has to be done in atomic when
599          * there are multiple segments because otherwise each iteration to
600          * __generic_file_aio_write() will see original file size */
601         if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
602                 struct iovec *iov_copy;
603                 int count = 0;
604
605                 OBD_ALLOC(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
606                 if (!iov_copy)
607                         return -ENOMEM;
608
609                 iov_for_each(iov, i, *iter)
610                         iov_copy[count++] = iov;
611
612                 bytes = __generic_file_aio_write(iocb, iov_copy, count,
613                                                  &iocb->ki_pos);
614                 OBD_FREE(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
615
616                 if (bytes > 0)
617                         iov_iter_advance(iter, bytes);
618                 return bytes;
619         }
620
621         iov_for_each(iov, i, *iter) {
622                 ssize_t res;
623
624                 res = __generic_file_aio_write(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 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
641
642 static inline void __user *get_vmf_address(struct vm_fault *vmf)
643 {
644 #ifdef HAVE_VM_FAULT_ADDRESS
645         return (void __user *)vmf->address;
646 #else
647         return vmf->virtual_address;
648 #endif
649 }
650
651 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
652 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
653 #else
654 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
655 #endif
656
657 #ifndef HAVE_CURRENT_TIME
658 static inline struct timespec current_time(struct inode *inode)
659 {
660         return CURRENT_TIME;
661 }
662 #endif
663
664 #ifndef time_after32
665 /**
666  * time_after32 - compare two 32-bit relative times
667  * @a: the time which may be after @b
668  * @b: the time which may be before @a
669  *
670  * time_after32(a, b) returns true if the time @a is after time @b.
671  * time_before32(b, a) returns true if the time @b is before time @a.
672  *
673  * Similar to time_after(), compare two 32-bit timestamps for relative
674  * times.  This is useful for comparing 32-bit seconds values that can't
675  * be converted to 64-bit values (e.g. due to disk format or wire protocol
676  * issues) when it is known that the times are less than 68 years apart.
677  */
678 #define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
679 #define time_before32(b, a)    time_after32(a, b)
680
681 #endif
682
683 #ifndef __GFP_COLD
684 #define __GFP_COLD 0
685 #endif
686
687 #ifndef alloc_workqueue
688 #define alloc_workqueue(name, flags, max_active) create_workqueue(name)
689 #endif
690
691 #ifndef READ_ONCE
692 #define READ_ONCE ACCESS_ONCE
693 #endif
694
695 #ifdef HAVE_BLK_INTEGRITY_ENABLED
696 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
697 {
698 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
699         return bi->interval_exp ? 1 << bi->interval_exp : 0;
700 #elif defined(HAVE_INTERVAL_BLK_INTEGRITY)
701         return bi->interval;
702 #else
703         return bi->sector_size;
704 #endif
705 }
706
707 static inline const char *blk_integrity_name(struct blk_integrity *bi)
708 {
709 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
710         return bi->profile->name;
711 #else
712         return bi->name;
713 #endif
714 }
715 #else
716 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
717 {
718         return 0;
719 }
720 static inline const char *blk_integrity_name(struct blk_integrity *bi)
721 {
722         /* gcc8 dislikes when strcmp() is called against NULL */
723         return "";
724 }
725 #endif
726
727 #endif /* _LUSTRE_COMPAT_H */