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