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