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