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