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