Whamcloud - gitweb
LU-7927 llite: Deadlock between ll_setattr and write/ll_fsync
[fs/lustre-release.git] / lustre / llite / vvp_io.c
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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_io for VVP layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44
45 #include <obd.h>
46 #include "llite_internal.h"
47 #include "vvp_internal.h"
48
49 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
50                                 const struct cl_io_slice *slice)
51 {
52         struct vvp_io *vio;
53
54         vio = container_of(slice, struct vvp_io, vui_cl);
55         LASSERT(vio == vvp_env_io(env));
56
57         return vio;
58 }
59
60 /**
61  * True, if \a io is a normal io, False for splice_{read,write}
62  */
63 static int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
64 {
65         struct vvp_io *vio = vvp_env_io(env);
66
67         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
68
69         return vio->vui_io_subtype == IO_NORMAL;
70 }
71
72 /**
73  * For swapping layout. The file's layout may have changed.
74  * To avoid populating pages to a wrong stripe, we have to verify the
75  * correctness of layout. It works because swapping layout processes
76  * have to acquire group lock.
77  */
78 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
79                                 struct inode *inode)
80 {
81         struct ll_inode_info    *lli = ll_i2info(inode);
82         struct vvp_io           *vio = vvp_env_io(env);
83         bool rc = true;
84
85         switch (io->ci_type) {
86         case CIT_READ:
87         case CIT_WRITE:
88                 /* don't need lock here to check lli_layout_gen as we have held
89                  * extent lock and GROUP lock has to hold to swap layout */
90                 if (ll_layout_version_get(lli) != vio->vui_layout_gen ||
91                     OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_LOST_LAYOUT, 0)) {
92                         io->ci_need_restart = 1;
93                         /* this will cause a short read/write */
94                         io->ci_continue = 0;
95                         rc = false;
96                 }
97         case CIT_FAULT:
98                 /* fault is okay because we've already had a page. */
99         default:
100                 break;
101         }
102
103         return rc;
104 }
105
106 static void vvp_object_size_lock(struct cl_object *obj)
107 {
108         struct inode *inode = vvp_object_inode(obj);
109
110         ll_inode_size_lock(inode);
111         cl_object_attr_lock(obj);
112 }
113
114 static void vvp_object_size_unlock(struct cl_object *obj)
115 {
116         struct inode *inode = vvp_object_inode(obj);
117
118         cl_object_attr_unlock(obj);
119         ll_inode_size_unlock(inode);
120 }
121
122 /**
123  * Helper function that if necessary adjusts file size (inode->i_size), when
124  * position at the offset \a pos is accessed. File size can be arbitrary stale
125  * on a Lustre client, but client at least knows KMS. If accessed area is
126  * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
127  *
128  * Locking: i_size_lock is used to serialize changes to inode size and to
129  * protect consistency between inode size and cl_object
130  * attributes. cl_object_size_lock() protects consistency between cl_attr's of
131  * top-object and sub-objects.
132  */
133 static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj,
134                          struct cl_io *io, loff_t start, size_t count,
135                          int *exceed)
136 {
137         struct cl_attr *attr  = vvp_env_thread_attr(env);
138         struct inode   *inode = vvp_object_inode(obj);
139         loff_t          pos   = start + count - 1;
140         loff_t kms;
141         int result;
142
143         /*
144          * Consistency guarantees: following possibilities exist for the
145          * relation between region being accessed and real file size at this
146          * moment:
147          *
148          *  (A): the region is completely inside of the file;
149          *
150          *  (B-x): x bytes of region are inside of the file, the rest is
151          *  outside;
152          *
153          *  (C): the region is completely outside of the file.
154          *
155          * This classification is stable under DLM lock already acquired by
156          * the caller, because to change the class, other client has to take
157          * DLM lock conflicting with our lock. Also, any updates to ->i_size
158          * by other threads on this client are serialized by
159          * ll_inode_size_lock(). This guarantees that short reads are handled
160          * correctly in the face of concurrent writes and truncates.
161          */
162         vvp_object_size_lock(obj);
163         result = cl_object_attr_get(env, obj, attr);
164         if (result == 0) {
165                 kms = attr->cat_kms;
166                 if (pos > kms) {
167                         /*
168                          * A glimpse is necessary to determine whether we
169                          * return a short read (B) or some zeroes at the end
170                          * of the buffer (C)
171                          */
172                         vvp_object_size_unlock(obj);
173                         result = cl_glimpse_lock(env, io, inode, obj, 0);
174                         if (result == 0 && exceed != NULL) {
175                                 /* If objective page index exceed end-of-file
176                                  * page index, return directly. Do not expect
177                                  * kernel will check such case correctly.
178                                  * linux-2.6.18-128.1.1 miss to do that.
179                                  * --bug 17336 */
180                                 loff_t size = i_size_read(inode);
181                                 unsigned long cur_index = start >>
182                                         PAGE_CACHE_SHIFT;
183
184                                 if ((size == 0 && cur_index != 0) ||
185                                     (((size - 1) >> PAGE_CACHE_SHIFT) <
186                                      cur_index))
187                                         *exceed = 1;
188                         }
189
190                         return result;
191                 } else {
192                         /*
193                          * region is within kms and, hence, within real file
194                          * size (A). We need to increase i_size to cover the
195                          * read region so that generic_file_read() will do its
196                          * job, but that doesn't mean the kms size is
197                          * _correct_, it is only the _minimum_ size. If
198                          * someone does a stat they will get the correct size
199                          * which will always be >= the kms value here.
200                          * b=11081
201                          */
202                         if (i_size_read(inode) < kms) {
203                                 i_size_write(inode, kms);
204                                 CDEBUG(D_VFSTRACE,
205                                        DFID" updating i_size %llu\n",
206                                        PFID(lu_object_fid(&obj->co_lu)),
207                                        (__u64)i_size_read(inode));
208                         }
209                 }
210         }
211
212         vvp_object_size_unlock(obj);
213
214         return result;
215 }
216
217 /*****************************************************************************
218  *
219  * io operations.
220  *
221  */
222
223 static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
224                                  __u32 enqflags, enum cl_lock_mode mode,
225                                  pgoff_t start, pgoff_t end)
226 {
227         struct vvp_io          *vio   = vvp_env_io(env);
228         struct cl_lock_descr   *descr = &vio->vui_link.cill_descr;
229         struct cl_object       *obj   = io->ci_obj;
230
231         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
232         ENTRY;
233
234         CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
235
236         memset(&vio->vui_link, 0, sizeof vio->vui_link);
237
238         if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
239                 descr->cld_mode = CLM_GROUP;
240                 descr->cld_gid  = vio->vui_fd->fd_grouplock.lg_gid;
241                 enqflags |= CEF_LOCK_MATCH;
242         } else {
243                 descr->cld_mode  = mode;
244         }
245
246         descr->cld_obj   = obj;
247         descr->cld_start = start;
248         descr->cld_end   = end;
249         descr->cld_enq_flags = enqflags;
250
251         cl_io_lock_add(env, io, &vio->vui_link);
252
253         RETURN(0);
254 }
255
256 static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io,
257                            __u32 enqflags, enum cl_lock_mode mode,
258                            loff_t start, loff_t end)
259 {
260         struct cl_object *obj = io->ci_obj;
261
262         return vvp_io_one_lock_index(env, io, enqflags, mode,
263                                      cl_index(obj, start), cl_index(obj, end));
264 }
265
266 static int vvp_io_write_iter_init(const struct lu_env *env,
267                                   const struct cl_io_slice *ios)
268 {
269         struct vvp_io *vio = cl2vvp_io(env, ios);
270
271         cl_page_list_init(&vio->u.write.vui_queue);
272         vio->u.write.vui_written = 0;
273         vio->u.write.vui_from = 0;
274         vio->u.write.vui_to = PAGE_SIZE;
275
276         return 0;
277 }
278
279 static void vvp_io_write_iter_fini(const struct lu_env *env,
280                                    const struct cl_io_slice *ios)
281 {
282         struct vvp_io *vio = cl2vvp_io(env, ios);
283
284         LASSERT(vio->u.write.vui_queue.pl_nr == 0);
285 }
286
287 static int vvp_io_fault_iter_init(const struct lu_env *env,
288                                   const struct cl_io_slice *ios)
289 {
290         struct vvp_io *vio   = cl2vvp_io(env, ios);
291         struct inode  *inode = vvp_object_inode(ios->cis_obj);
292
293         LASSERT(inode == file_inode(vio->vui_fd->fd_file));
294         vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
295
296         return 0;
297 }
298
299 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
300 {
301         struct cl_io     *io  = ios->cis_io;
302         struct cl_object *obj = io->ci_obj;
303         struct vvp_io    *vio = cl2vvp_io(env, ios);
304         struct inode     *inode = vvp_object_inode(obj);
305
306         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
307
308         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
309                            "restore needed %d\n",
310                PFID(lu_object_fid(&obj->co_lu)),
311                io->ci_ignore_layout, io->ci_verify_layout,
312                vio->vui_layout_gen, io->ci_restore_needed);
313
314         if (io->ci_restore_needed) {
315                 int     rc;
316
317                 /* file was detected release, we need to restore it
318                  * before finishing the io
319                  */
320                 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
321                 /* if restore registration failed, no restart,
322                  * we will return -ENODATA */
323                 /* The layout will change after restore, so we need to
324                  * block on layout lock held by the MDT
325                  * as MDT will not send new layout in lvb (see LU-3124)
326                  * we have to explicitly fetch it, all this will be done
327                  * by ll_layout_refresh()
328                  */
329                 if (rc == 0) {
330                         io->ci_restore_needed = 0;
331                         io->ci_need_restart = 1;
332                         io->ci_verify_layout = 1;
333                 } else {
334                         io->ci_restore_needed = 1;
335                         io->ci_need_restart = 0;
336                         io->ci_verify_layout = 0;
337                         io->ci_result = rc;
338                 }
339         }
340
341         if (!io->ci_ignore_layout && io->ci_verify_layout) {
342                 __u32 gen = 0;
343
344                 /* check layout version */
345                 ll_layout_refresh(inode, &gen);
346                 io->ci_need_restart = vio->vui_layout_gen != gen;
347                 if (io->ci_need_restart) {
348                         CDEBUG(D_VFSTRACE,
349                                DFID" layout changed from %d to %d.\n",
350                                PFID(lu_object_fid(&obj->co_lu)),
351                                vio->vui_layout_gen, gen);
352                         /* today successful restore is the only possible
353                          * case */
354                         /* restore was done, clear restoring state */
355                         ll_file_clear_flag(ll_i2info(vvp_object_inode(obj)),
356                                            LLIF_FILE_RESTORING);
357                 }
358         }
359 }
360
361 static void vvp_io_fault_fini(const struct lu_env *env,
362                               const struct cl_io_slice *ios)
363 {
364         struct cl_io   *io   = ios->cis_io;
365         struct cl_page *page = io->u.ci_fault.ft_page;
366
367         CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
368
369         if (page != NULL) {
370                 lu_ref_del(&page->cp_reference, "fault", io);
371                 cl_page_put(env, page);
372                 io->u.ci_fault.ft_page = NULL;
373         }
374         vvp_io_fini(env, ios);
375 }
376
377 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
378 {
379         /*
380          * we only want to hold PW locks if the mmap() can generate
381          * writes back to the file and that only happens in shared
382          * writable vmas
383          */
384         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
385                 return CLM_WRITE;
386         return CLM_READ;
387 }
388
389 static int vvp_mmap_locks(const struct lu_env *env,
390                           struct vvp_io *vio, struct cl_io *io)
391 {
392         struct vvp_thread_info *vti = vvp_env_info(env);
393         struct mm_struct *mm = current->mm;
394         struct vm_area_struct *vma;
395         struct cl_lock_descr *descr = &vti->vti_descr;
396         union ldlm_policy_data policy;
397         struct iovec iov;
398         struct iov_iter i;
399         int result = 0;
400         ENTRY;
401
402         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
403
404         if (!cl_is_normalio(env, io))
405                 RETURN(0);
406
407         /* nfs or loop back device write */
408         if (vio->vui_iter == NULL)
409                 RETURN(0);
410
411         /* No MM (e.g. NFS)? No vmas too. */
412         if (mm == NULL)
413                 RETURN(0);
414
415         iov_for_each(iov, i, *(vio->vui_iter)) {
416                 unsigned long addr = (unsigned long)iov.iov_base;
417                 size_t count = iov.iov_len;
418
419                 if (count == 0)
420                         continue;
421
422                 count += addr & ~PAGE_MASK;
423                 addr &= PAGE_MASK;
424
425                 down_read(&mm->mmap_sem);
426                 while((vma = our_vma(mm, addr, count)) != NULL) {
427                         struct dentry *de = file_dentry(vma->vm_file);
428                         struct inode *inode = de->d_inode;
429                         int flags = CEF_MUST;
430
431                         if (ll_file_nolock(vma->vm_file)) {
432                                 /*
433                                  * For no lock case is not allowed for mmap
434                                  */
435                                 result = -EINVAL;
436                                 break;
437                         }
438
439                         /*
440                          * XXX: Required lock mode can be weakened: CIT_WRITE
441                          * io only ever reads user level buffer, and CIT_READ
442                          * only writes on it.
443                          */
444                         policy_from_vma(&policy, vma, addr, count);
445                         descr->cld_mode = vvp_mode_from_vma(vma);
446                         descr->cld_obj = ll_i2info(inode)->lli_clob;
447                         descr->cld_start = cl_index(descr->cld_obj,
448                                                     policy.l_extent.start);
449                         descr->cld_end = cl_index(descr->cld_obj,
450                                                   policy.l_extent.end);
451                         descr->cld_enq_flags = flags;
452                         result = cl_io_lock_alloc_add(env, io, descr);
453
454                         CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
455                                descr->cld_mode, descr->cld_start,
456                                descr->cld_end);
457
458                         if (result < 0)
459                                 break;
460
461                         if (vma->vm_end - addr >= count)
462                                 break;
463
464                         count -= vma->vm_end - addr;
465                         addr = vma->vm_end;
466                 }
467                 up_read(&mm->mmap_sem);
468                 if (result < 0)
469                         break;
470         }
471         RETURN(result);
472 }
473
474 static void vvp_io_advance(const struct lu_env *env,
475                            const struct cl_io_slice *ios,
476                            size_t nob)
477 {
478         struct vvp_io    *vio = cl2vvp_io(env, ios);
479         struct cl_io     *io  = ios->cis_io;
480         struct cl_object *obj = ios->cis_io->ci_obj;
481
482         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
483
484         if (!cl_is_normalio(env, io))
485                 return;
486
487         vio->vui_tot_count -= nob;
488         iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);
489 }
490
491 static void vvp_io_update_iov(const struct lu_env *env,
492                               struct vvp_io *vio, struct cl_io *io)
493 {
494         size_t size = io->u.ci_rw.crw_count;
495
496         if (!cl_is_normalio(env, io) || vio->vui_iter == NULL)
497                 return;
498
499         iov_iter_truncate(vio->vui_iter, size);
500 }
501
502 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
503                           enum cl_lock_mode mode, loff_t start, loff_t end)
504 {
505         struct vvp_io *vio = vvp_env_io(env);
506         int result;
507         int ast_flags = 0;
508
509         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
510         ENTRY;
511
512         vvp_io_update_iov(env, vio, io);
513
514         if (io->u.ci_rw.crw_nonblock)
515                 ast_flags |= CEF_NONBLOCK;
516
517         result = vvp_mmap_locks(env, vio, io);
518         if (result == 0)
519                 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
520
521         RETURN(result);
522 }
523
524 static int vvp_io_read_lock(const struct lu_env *env,
525                             const struct cl_io_slice *ios)
526 {
527         struct cl_io            *io  = ios->cis_io;
528         struct cl_io_rw_common  *rd = &io->u.ci_rd.rd;
529         int result;
530
531         ENTRY;
532         result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
533                                 rd->crw_pos + rd->crw_count - 1);
534         RETURN(result);
535 }
536
537 static int vvp_io_fault_lock(const struct lu_env *env,
538                              const struct cl_io_slice *ios)
539 {
540         struct cl_io *io   = ios->cis_io;
541         struct vvp_io *vio = cl2vvp_io(env, ios);
542         /*
543          * XXX LDLM_FL_CBPENDING
544          */
545         return vvp_io_one_lock_index(env,
546                                      io, 0,
547                                      vvp_mode_from_vma(vio->u.fault.ft_vma),
548                                      io->u.ci_fault.ft_index,
549                                      io->u.ci_fault.ft_index);
550 }
551
552 static int vvp_io_write_lock(const struct lu_env *env,
553                              const struct cl_io_slice *ios)
554 {
555         struct cl_io *io = ios->cis_io;
556         loff_t start;
557         loff_t end;
558
559         if (io->u.ci_wr.wr_append) {
560                 start = 0;
561                 end   = OBD_OBJECT_EOF;
562         } else {
563                 start = io->u.ci_wr.wr.crw_pos;
564                 end   = start + io->u.ci_wr.wr.crw_count - 1;
565         }
566         return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
567 }
568
569 static int vvp_io_setattr_iter_init(const struct lu_env *env,
570                                     const struct cl_io_slice *ios)
571 {
572         return 0;
573 }
574
575 /**
576  * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
577  *
578  * Handles "lockless io" mode when extent locking is done by server.
579  */
580 static int vvp_io_setattr_lock(const struct lu_env *env,
581                                const struct cl_io_slice *ios)
582 {
583         struct cl_io  *io  = ios->cis_io;
584         __u64 new_size;
585         __u32 enqflags = 0;
586
587         if (cl_io_is_trunc(io)) {
588                 new_size = io->u.ci_setattr.sa_attr.lvb_size;
589                 if (new_size == 0)
590                         enqflags = CEF_DISCARD_DATA;
591         } else {
592                 unsigned int valid = io->u.ci_setattr.sa_valid;
593
594                 if (!(valid & TIMES_SET_FLAGS))
595                         return 0;
596
597                 if ((!(valid & ATTR_MTIME) ||
598                      io->u.ci_setattr.sa_attr.lvb_mtime >=
599                      io->u.ci_setattr.sa_attr.lvb_ctime) &&
600                     (!(valid & ATTR_ATIME) ||
601                      io->u.ci_setattr.sa_attr.lvb_atime >=
602                      io->u.ci_setattr.sa_attr.lvb_ctime))
603                         return 0;
604
605                 new_size = 0;
606         }
607
608         return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
609                                new_size, OBD_OBJECT_EOF);
610 }
611
612 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
613 {
614         int     result;
615
616         /*
617          * Only ll_inode_size_lock is taken at this level.
618          */
619         ll_inode_size_lock(inode);
620         result = inode_newsize_ok(inode, size);
621         if (result < 0) {
622                 ll_inode_size_unlock(inode);
623                 return result;
624         }
625         i_size_write(inode, size);
626
627         ll_truncate_pagecache(inode, size);
628         ll_inode_size_unlock(inode);
629         return result;
630 }
631
632 static int vvp_io_setattr_time(const struct lu_env *env,
633                                const struct cl_io_slice *ios)
634 {
635         struct cl_io       *io    = ios->cis_io;
636         struct cl_object   *obj   = io->ci_obj;
637         struct cl_attr     *attr  = vvp_env_thread_attr(env);
638         int result;
639         unsigned valid = CAT_CTIME;
640
641         cl_object_attr_lock(obj);
642         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
643         if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
644                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
645                 valid |= CAT_ATIME;
646         }
647         if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
648                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
649                 valid |= CAT_MTIME;
650         }
651         result = cl_object_attr_update(env, obj, attr, valid);
652         cl_object_attr_unlock(obj);
653
654         return result;
655 }
656
657 static int vvp_io_setattr_start(const struct lu_env *env,
658                                 const struct cl_io_slice *ios)
659 {
660         struct cl_io            *io    = ios->cis_io;
661         struct inode            *inode = vvp_object_inode(io->ci_obj);
662         struct ll_inode_info    *lli   = ll_i2info(inode);
663
664         if (cl_io_is_trunc(io)) {
665                 down_write(&lli->lli_trunc_sem);
666                 inode_lock(inode);
667                 inode_dio_wait(inode);
668         } else {
669                 inode_lock(inode);
670         }
671
672         if (io->u.ci_setattr.sa_valid & TIMES_SET_FLAGS)
673                 return vvp_io_setattr_time(env, ios);
674
675         return 0;
676 }
677
678 static void vvp_io_setattr_end(const struct lu_env *env,
679                                const struct cl_io_slice *ios)
680 {
681         struct cl_io            *io    = ios->cis_io;
682         struct inode            *inode = vvp_object_inode(io->ci_obj);
683         struct ll_inode_info    *lli   = ll_i2info(inode);
684
685         if (cl_io_is_trunc(io)) {
686                 /* Truncate in memory pages - they must be clean pages
687                  * because osc has already notified to destroy osc_extents. */
688                 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
689                 inode_dio_write_done(inode);
690                 inode_unlock(inode);
691                 up_write(&lli->lli_trunc_sem);
692         } else {
693                 inode_unlock(inode);
694         }
695 }
696
697 static void vvp_io_setattr_fini(const struct lu_env *env,
698                                 const struct cl_io_slice *ios)
699 {
700         bool restore_needed = ios->cis_io->ci_restore_needed;
701         struct inode *inode = vvp_object_inode(ios->cis_obj);
702
703         vvp_io_fini(env, ios);
704
705         if (restore_needed && !ios->cis_io->ci_restore_needed) {
706                 /* restore finished, set data modified flag for HSM */
707                 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
708         }
709 }
710
711 static int vvp_io_read_start(const struct lu_env *env,
712                              const struct cl_io_slice *ios)
713 {
714         struct vvp_io           *vio   = cl2vvp_io(env, ios);
715         struct cl_io            *io    = ios->cis_io;
716         struct cl_object        *obj   = io->ci_obj;
717         struct inode            *inode = vvp_object_inode(obj);
718         struct ll_inode_info    *lli   = ll_i2info(inode);
719         struct file             *file  = vio->vui_fd->fd_file;
720
721         int     result;
722         loff_t  pos = io->u.ci_rd.rd.crw_pos;
723         long    cnt = io->u.ci_rd.rd.crw_count;
724         long    tot = vio->vui_tot_count;
725         int     exceed = 0;
726
727         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
728
729         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
730
731         if (vio->vui_io_subtype == IO_NORMAL)
732                 down_read(&lli->lli_trunc_sem);
733
734         if (!can_populate_pages(env, io, inode))
735                 return 0;
736
737         result = vvp_prep_size(env, obj, io, pos, tot, &exceed);
738         if (result != 0)
739                 return result;
740         else if (exceed != 0)
741                 goto out;
742
743         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
744                         "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
745                         inode->i_ino, cnt, pos, i_size_read(inode));
746
747         /* turn off the kernel's read-ahead */
748         vio->vui_fd->fd_file->f_ra.ra_pages = 0;
749
750         /* initialize read-ahead window once per syscall */
751         if (!vio->vui_ra_valid) {
752                 vio->vui_ra_valid = true;
753                 vio->vui_ra_start = cl_index(obj, pos);
754                 vio->vui_ra_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
755                 ll_ras_enter(file);
756         }
757
758         /* BUG: 5972 */
759         file_accessed(file);
760         switch (vio->vui_io_subtype) {
761         case IO_NORMAL:
762                 LASSERT(vio->vui_iocb->ki_pos == pos);
763                 result = generic_file_read_iter(vio->vui_iocb, vio->vui_iter);
764                 break;
765         case IO_SPLICE:
766                 result = generic_file_splice_read(file, &pos,
767                                                   vio->u.splice.vui_pipe, cnt,
768                                                   vio->u.splice.vui_flags);
769                 /* LU-1109: do splice read stripe by stripe otherwise if it
770                  * may make nfsd stuck if this read occupied all internal pipe
771                  * buffers. */
772                 io->ci_continue = 0;
773                 break;
774         default:
775                 CERROR("Wrong IO type %u\n", vio->vui_io_subtype);
776                 LBUG();
777         }
778
779 out:
780         if (result >= 0) {
781                 if (result < cnt)
782                         io->ci_continue = 0;
783                 io->ci_nob += result;
784                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, vio->vui_fd,
785                                   pos, result, READ);
786                 result = 0;
787         }
788
789         return result;
790 }
791
792 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
793                               struct cl_page_list *plist, int from, int to)
794 {
795         struct cl_2queue *queue = &io->ci_queue;
796         struct cl_page *page;
797         unsigned int bytes = 0;
798         int rc = 0;
799         ENTRY;
800
801         if (plist->pl_nr == 0)
802                 RETURN(0);
803
804         if (from > 0 || to != PAGE_SIZE) {
805                 page = cl_page_list_first(plist);
806                 if (plist->pl_nr == 1) {
807                         cl_page_clip(env, page, from, to);
808                 } else {
809                         if (from > 0)
810                                 cl_page_clip(env, page, from, PAGE_SIZE);
811                         if (to != PAGE_SIZE) {
812                                 page = cl_page_list_last(plist);
813                                 cl_page_clip(env, page, 0, to);
814                         }
815                 }
816         }
817
818         cl_2queue_init(queue);
819         cl_page_list_splice(plist, &queue->c2_qin);
820         rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
821
822         /* plist is not sorted any more */
823         cl_page_list_splice(&queue->c2_qin, plist);
824         cl_page_list_splice(&queue->c2_qout, plist);
825         cl_2queue_fini(env, queue);
826
827         if (rc == 0) {
828                 /* calculate bytes */
829                 bytes = plist->pl_nr << PAGE_SHIFT;
830                 bytes -= from + PAGE_SIZE - to;
831
832                 while (plist->pl_nr > 0) {
833                         page = cl_page_list_first(plist);
834                         cl_page_list_del(env, plist, page);
835
836                         cl_page_clip(env, page, 0, PAGE_SIZE);
837
838                         SetPageUptodate(cl_page_vmpage(page));
839                         cl_page_disown(env, io, page);
840
841                         /* held in ll_cl_init() */
842                         lu_ref_del(&page->cp_reference, "cl_io", io);
843                         cl_page_put(env, page);
844                 }
845         }
846
847         RETURN(bytes > 0 ? bytes : rc);
848 }
849
850 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
851                                 struct cl_page *page)
852 {
853         struct page *vmpage = page->cp_vmpage;
854
855         SetPageUptodate(vmpage);
856         set_page_dirty(vmpage);
857
858         cl_page_disown(env, io, page);
859
860         /* held in ll_cl_init() */
861         lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
862         cl_page_put(env, page);
863 }
864
865 /* make sure the page list is contiguous */
866 static bool page_list_sanity_check(struct cl_object *obj,
867                                    struct cl_page_list *plist)
868 {
869         struct cl_page *page;
870         pgoff_t index = CL_PAGE_EOF;
871
872         cl_page_list_for_each(page, plist) {
873                 struct vvp_page *vpg = cl_object_page_slice(obj, page);
874
875                 if (index == CL_PAGE_EOF) {
876                         index = vvp_index(vpg);
877                         continue;
878                 }
879
880                 ++index;
881                 if (index == vvp_index(vpg))
882                         continue;
883
884                 return false;
885         }
886         return true;
887 }
888
889 /* Return how many bytes have queued or written */
890 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
891 {
892         struct cl_object *obj = io->ci_obj;
893         struct inode *inode = vvp_object_inode(obj);
894         struct vvp_io *vio = vvp_env_io(env);
895         struct cl_page_list *queue = &vio->u.write.vui_queue;
896         struct cl_page *page;
897         int rc = 0;
898         int bytes = 0;
899         unsigned int npages = vio->u.write.vui_queue.pl_nr;
900         ENTRY;
901
902         if (npages == 0)
903                 RETURN(0);
904
905         CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
906                 npages, vio->u.write.vui_from, vio->u.write.vui_to);
907
908         LASSERT(page_list_sanity_check(obj, queue));
909
910         /* submit IO with async write */
911         rc = cl_io_commit_async(env, io, queue,
912                                 vio->u.write.vui_from, vio->u.write.vui_to,
913                                 write_commit_callback);
914         npages -= queue->pl_nr; /* already committed pages */
915         if (npages > 0) {
916                 /* calculate how many bytes were written */
917                 bytes = npages << PAGE_SHIFT;
918
919                 /* first page */
920                 bytes -= vio->u.write.vui_from;
921                 if (queue->pl_nr == 0) /* last page */
922                         bytes -= PAGE_SIZE - vio->u.write.vui_to;
923                 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
924
925                 vio->u.write.vui_written += bytes;
926
927                 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
928                         npages, bytes, vio->u.write.vui_written);
929
930                 /* the first page must have been written. */
931                 vio->u.write.vui_from = 0;
932         }
933         LASSERT(page_list_sanity_check(obj, queue));
934         LASSERT(ergo(rc == 0, queue->pl_nr == 0));
935
936         /* out of quota, try sync write */
937         if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
938                 rc = vvp_io_commit_sync(env, io, queue,
939                                         vio->u.write.vui_from,
940                                         vio->u.write.vui_to);
941                 if (rc > 0) {
942                         vio->u.write.vui_written += rc;
943                         rc = 0;
944                 }
945         }
946
947         /* update inode size */
948         ll_merge_attr(env, inode);
949
950         /* Now the pages in queue were failed to commit, discard them
951          * unless they were dirtied before. */
952         while (queue->pl_nr > 0) {
953                 page = cl_page_list_first(queue);
954                 cl_page_list_del(env, queue, page);
955
956                 if (!PageDirty(cl_page_vmpage(page)))
957                         cl_page_discard(env, io, page);
958
959                 cl_page_disown(env, io, page);
960
961                 /* held in ll_cl_init() */
962                 lu_ref_del(&page->cp_reference, "cl_io", io);
963                 cl_page_put(env, page);
964         }
965         cl_page_list_fini(env, queue);
966
967         RETURN(rc);
968 }
969
970 static int vvp_io_write_start(const struct lu_env *env,
971                               const struct cl_io_slice *ios)
972 {
973         struct vvp_io           *vio   = cl2vvp_io(env, ios);
974         struct cl_io            *io    = ios->cis_io;
975         struct cl_object        *obj   = io->ci_obj;
976         struct inode            *inode = vvp_object_inode(obj);
977         struct ll_inode_info    *lli   = ll_i2info(inode);
978         ssize_t                  result = 0;
979         loff_t                   pos = io->u.ci_wr.wr.crw_pos;
980         size_t                   cnt = io->u.ci_wr.wr.crw_count;
981
982         ENTRY;
983
984         if (vio->vui_io_subtype == IO_NORMAL)
985                 down_read(&lli->lli_trunc_sem);
986
987         if (!can_populate_pages(env, io, inode))
988                 RETURN(0);
989
990         if (cl_io_is_append(io)) {
991                 /*
992                  * PARALLEL IO This has to be changed for parallel IO doing
993                  * out-of-order writes.
994                  */
995                 ll_merge_attr(env, inode);
996                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
997                 vio->vui_iocb->ki_pos = pos;
998         } else {
999                 LASSERT(vio->vui_iocb->ki_pos == pos);
1000         }
1001
1002         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
1003
1004         /* The maximum Lustre file size is variable, based on the OST maximum
1005          * object size and number of stripes.  This needs another check in
1006          * addition to the VFS checks earlier. */
1007         if (pos + cnt > ll_file_maxbytes(inode)) {
1008                 CDEBUG(D_INODE,
1009                        "%s: file "DFID" offset %llu > maxbytes %llu\n",
1010                        ll_get_fsname(inode->i_sb, NULL, 0),
1011                        PFID(ll_inode2fid(inode)), pos + cnt,
1012                        ll_file_maxbytes(inode));
1013                 RETURN(-EFBIG);
1014         }
1015
1016         if (vio->vui_iter == NULL) {
1017                 /* from a temp io in ll_cl_init(). */
1018                 result = 0;
1019         } else {
1020                 /*
1021                  * When using the locked AIO function (generic_file_aio_write())
1022                  * testing has shown the inode mutex to be a limiting factor
1023                  * with multi-threaded single shared file performance. To get
1024                  * around this, we now use the lockless version. To maintain
1025                  * consistency, proper locking to protect against writes,
1026                  * trucates, etc. is handled in the higher layers of lustre.
1027                  */
1028                 result = generic_file_write_iter(vio->vui_iocb, vio->vui_iter);
1029                 if (result > 0 || result == -EIOCBQUEUED) {
1030                         ssize_t err;
1031
1032                         err = generic_write_sync(vio->vui_iocb->ki_filp,
1033                                                  pos, result);
1034                         if (err < 0 && result > 0)
1035                                 result = err;
1036                 }
1037
1038         }
1039         if (result > 0) {
1040                 result = vvp_io_write_commit(env, io);
1041                 if (vio->u.write.vui_written > 0) {
1042                         result = vio->u.write.vui_written;
1043                         io->ci_nob += result;
1044
1045                         CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
1046                                 io->ci_nob, result);
1047                 }
1048         }
1049         if (result > 0) {
1050                 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1051
1052                 if (result < cnt)
1053                         io->ci_continue = 0;
1054                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
1055                                   vio->vui_fd, pos, result, WRITE);
1056                 result = 0;
1057         }
1058
1059         RETURN(result);
1060 }
1061
1062 static void vvp_io_rw_end(const struct lu_env *env,
1063                           const struct cl_io_slice *ios)
1064 {
1065         struct vvp_io           *vio = cl2vvp_io(env, ios);
1066         struct inode            *inode = vvp_object_inode(ios->cis_obj);
1067         struct ll_inode_info    *lli = ll_i2info(inode);
1068
1069         if (vio->vui_io_subtype == IO_NORMAL)
1070                 up_read(&lli->lli_trunc_sem);
1071 }
1072
1073 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1074 {
1075         struct vm_fault *vmf = cfio->ft_vmf;
1076
1077         cfio->ft_flags = filemap_fault(cfio->ft_vma, vmf);
1078         cfio->ft_flags_valid = 1;
1079
1080         if (vmf->page) {
1081                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1082                                vmf->virtual_address);
1083                 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1084                         lock_page(vmf->page);
1085                         cfio->ft_flags |= VM_FAULT_LOCKED;
1086                 }
1087
1088                 cfio->ft_vmpage = vmf->page;
1089
1090                 return 0;
1091         }
1092
1093         if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1094                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
1095                 return -EFAULT;
1096         }
1097
1098         if (cfio->ft_flags & VM_FAULT_OOM) {
1099                 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
1100                 return -ENOMEM;
1101         }
1102
1103         if (cfio->ft_flags & VM_FAULT_RETRY)
1104                 return -EAGAIN;
1105
1106         CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1107
1108         return -EINVAL;
1109 }
1110
1111 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1112                                     struct cl_page *page)
1113 {
1114         set_page_dirty(page->cp_vmpage);
1115 }
1116
1117 static int vvp_io_fault_start(const struct lu_env *env,
1118                               const struct cl_io_slice *ios)
1119 {
1120         struct vvp_io           *vio   = cl2vvp_io(env, ios);
1121         struct cl_io            *io    = ios->cis_io;
1122         struct cl_object        *obj   = io->ci_obj;
1123         struct inode            *inode = vvp_object_inode(obj);
1124         struct ll_inode_info    *lli   = ll_i2info(inode);
1125         struct cl_fault_io      *fio   = &io->u.ci_fault;
1126         struct vvp_fault_io     *cfio  = &vio->u.fault;
1127         loff_t                   offset;
1128         int                      result = 0;
1129         struct page             *vmpage = NULL;
1130         struct cl_page          *page;
1131         loff_t                   size;
1132         pgoff_t                  last_index;
1133         ENTRY;
1134
1135         down_read(&lli->lli_trunc_sem);
1136
1137         /* offset of the last byte on the page */
1138         offset = cl_offset(obj, fio->ft_index + 1) - 1;
1139         LASSERT(cl_index(obj, offset) == fio->ft_index);
1140         result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1141         if (result != 0)
1142                 RETURN(result);
1143
1144         /* must return locked page */
1145         if (fio->ft_mkwrite) {
1146                 LASSERT(cfio->ft_vmpage != NULL);
1147                 lock_page(cfio->ft_vmpage);
1148         } else {
1149                 result = vvp_io_kernel_fault(cfio);
1150                 if (result != 0)
1151                         RETURN(result);
1152         }
1153
1154         vmpage = cfio->ft_vmpage;
1155         LASSERT(PageLocked(vmpage));
1156
1157         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1158                 ll_invalidate_page(vmpage);
1159
1160         size = i_size_read(inode);
1161         /* Though we have already held a cl_lock upon this page, but
1162          * it still can be truncated locally. */
1163         if (unlikely((vmpage->mapping != inode->i_mapping) ||
1164                      (page_offset(vmpage) > size))) {
1165                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1166
1167                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1168                  * and retry. */
1169                 GOTO(out, result = +1);
1170         }
1171
1172         last_index = cl_index(obj, size - 1);
1173
1174         if (fio->ft_mkwrite ) {
1175                 /*
1176                  * Capture the size while holding the lli_trunc_sem from above
1177                  * we want to make sure that we complete the mkwrite action
1178                  * while holding this lock. We need to make sure that we are
1179                  * not past the end of the file.
1180                  */
1181                 if (last_index < fio->ft_index) {
1182                         CDEBUG(D_PAGE,
1183                                 "llite: mkwrite and truncate race happened: "
1184                                 "%p: 0x%lx 0x%lx\n",
1185                                 vmpage->mapping,fio->ft_index,last_index);
1186                         /*
1187                          * We need to return if we are
1188                          * passed the end of the file. This will propagate
1189                          * up the call stack to ll_page_mkwrite where
1190                          * we will return VM_FAULT_NOPAGE. Any non-negative
1191                          * value returned here will be silently
1192                          * converted to 0. If the vmpage->mapping is null
1193                          * the error code would be converted back to ENODATA
1194                          * in ll_page_mkwrite0. Thus we return -ENODATA
1195                          * to handle both cases
1196                          */
1197                         GOTO(out, result = -ENODATA);
1198                 }
1199         }
1200
1201         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1202         if (IS_ERR(page))
1203                 GOTO(out, result = PTR_ERR(page));
1204
1205         /* if page is going to be written, we should add this page into cache
1206          * earlier. */
1207         if (fio->ft_mkwrite) {
1208                 wait_on_page_writeback(vmpage);
1209                 if (!PageDirty(vmpage)) {
1210                         struct cl_page_list *plist = &io->ci_queue.c2_qin;
1211                         struct vvp_page *vpg = cl_object_page_slice(obj, page);
1212                         int to = PAGE_SIZE;
1213
1214                         /* vvp_page_assume() calls wait_on_page_writeback(). */
1215                         cl_page_assume(env, io, page);
1216
1217                         cl_page_list_init(plist);
1218                         cl_page_list_add(plist, page);
1219
1220                         /* size fixup */
1221                         if (last_index == vvp_index(vpg))
1222                                 to = size & ~PAGE_MASK;
1223
1224                         /* Do not set Dirty bit here so that in case IO is
1225                          * started before the page is really made dirty, we
1226                          * still have chance to detect it. */
1227                         result = cl_io_commit_async(env, io, plist, 0, to,
1228                                                     mkwrite_commit_callback);
1229                         LASSERT(cl_page_is_owned(page, io));
1230                         cl_page_list_fini(env, plist);
1231
1232                         vmpage = NULL;
1233                         if (result < 0) {
1234                                 cl_page_discard(env, io, page);
1235                                 cl_page_disown(env, io, page);
1236
1237                                 cl_page_put(env, page);
1238
1239                                 /* we're in big trouble, what can we do now? */
1240                                 if (result == -EDQUOT)
1241                                         result = -ENOSPC;
1242                                 GOTO(out, result);
1243                         } else
1244                                 cl_page_disown(env, io, page);
1245                 }
1246         }
1247
1248         /*
1249          * The ft_index is only used in the case of
1250          * a mkwrite action. We need to check
1251          * our assertions are correct, since
1252          * we should have caught this above
1253          */
1254         LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1255         if (fio->ft_index == last_index)
1256                 /*
1257                  * Last page is mapped partially.
1258                  */
1259                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1260         else
1261                 fio->ft_nob = cl_page_size(obj);
1262
1263         lu_ref_add(&page->cp_reference, "fault", io);
1264         fio->ft_page = page;
1265         EXIT;
1266
1267 out:
1268         /* return unlocked vmpage to avoid deadlocking */
1269         if (vmpage != NULL)
1270                 unlock_page(vmpage);
1271
1272         cfio->ft_flags &= ~VM_FAULT_LOCKED;
1273
1274         return result;
1275 }
1276
1277 static void vvp_io_fault_end(const struct lu_env *env,
1278                              const struct cl_io_slice *ios)
1279 {
1280         struct inode            *inode = vvp_object_inode(ios->cis_obj);
1281         struct ll_inode_info    *lli   = ll_i2info(inode);
1282
1283         CLOBINVRNT(env, ios->cis_io->ci_obj,
1284                    vvp_object_invariant(ios->cis_io->ci_obj));
1285         up_read(&lli->lli_trunc_sem);
1286 }
1287
1288 static int vvp_io_fsync_start(const struct lu_env *env,
1289                               const struct cl_io_slice *ios)
1290 {
1291         /* we should mark TOWRITE bit to each dirty page in radix tree to
1292          * verify pages have been written, but this is difficult because of
1293          * race. */
1294         return 0;
1295 }
1296
1297 static int vvp_io_read_ahead(const struct lu_env *env,
1298                              const struct cl_io_slice *ios,
1299                              pgoff_t start, struct cl_read_ahead *ra)
1300 {
1301         int result = 0;
1302         ENTRY;
1303
1304         if (ios->cis_io->ci_type == CIT_READ ||
1305             ios->cis_io->ci_type == CIT_FAULT) {
1306                 struct vvp_io *vio = cl2vvp_io(env, ios);
1307
1308                 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1309                         ra->cra_end = CL_PAGE_EOF;
1310                         result = +1; /* no need to call down */
1311                 }
1312         }
1313
1314         RETURN(result);
1315 }
1316
1317 static const struct cl_io_operations vvp_io_ops = {
1318         .op = {
1319                 [CIT_READ] = {
1320                         .cio_fini       = vvp_io_fini,
1321                         .cio_lock       = vvp_io_read_lock,
1322                         .cio_start      = vvp_io_read_start,
1323                         .cio_end        = vvp_io_rw_end,
1324                         .cio_advance    = vvp_io_advance,
1325                 },
1326                 [CIT_WRITE] = {
1327                         .cio_fini      = vvp_io_fini,
1328                         .cio_iter_init = vvp_io_write_iter_init,
1329                         .cio_iter_fini = vvp_io_write_iter_fini,
1330                         .cio_lock      = vvp_io_write_lock,
1331                         .cio_start     = vvp_io_write_start,
1332                         .cio_end       = vvp_io_rw_end,
1333                         .cio_advance   = vvp_io_advance,
1334                 },
1335                 [CIT_SETATTR] = {
1336                         .cio_fini       = vvp_io_setattr_fini,
1337                         .cio_iter_init  = vvp_io_setattr_iter_init,
1338                         .cio_lock       = vvp_io_setattr_lock,
1339                         .cio_start      = vvp_io_setattr_start,
1340                         .cio_end        = vvp_io_setattr_end
1341                 },
1342                 [CIT_FAULT] = {
1343                         .cio_fini      = vvp_io_fault_fini,
1344                         .cio_iter_init = vvp_io_fault_iter_init,
1345                         .cio_lock      = vvp_io_fault_lock,
1346                         .cio_start     = vvp_io_fault_start,
1347                         .cio_end       = vvp_io_fault_end,
1348                 },
1349                 [CIT_FSYNC] = {
1350                         .cio_start      = vvp_io_fsync_start,
1351                         .cio_fini       = vvp_io_fini
1352                 },
1353                 [CIT_MISC] = {
1354                         .cio_fini       = vvp_io_fini
1355                 },
1356                 [CIT_LADVISE] = {
1357                         .cio_fini       = vvp_io_fini
1358                 },
1359         },
1360         .cio_read_ahead = vvp_io_read_ahead
1361 };
1362
1363 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1364                 struct cl_io *io)
1365 {
1366         struct vvp_io      *vio   = vvp_env_io(env);
1367         struct inode       *inode = vvp_object_inode(obj);
1368         int                 result;
1369
1370         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1371         ENTRY;
1372
1373         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1374                "restore needed %d\n",
1375                PFID(lu_object_fid(&obj->co_lu)),
1376                io->ci_ignore_layout, io->ci_verify_layout,
1377                vio->vui_layout_gen, io->ci_restore_needed);
1378
1379         CL_IO_SLICE_CLEAN(vio, vui_cl);
1380         cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1381         vio->vui_ra_valid = false;
1382         result = 0;
1383         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1384                 size_t count;
1385                 struct ll_inode_info *lli = ll_i2info(inode);
1386
1387                 count = io->u.ci_rw.crw_count;
1388                 /* "If nbyte is 0, read() will return 0 and have no other
1389                  *  results."  -- Single Unix Spec */
1390                 if (count == 0)
1391                         result = 1;
1392                 else
1393                         vio->vui_tot_count = count;
1394
1395                 /* for read/write, we store the jobid in the inode, and
1396                  * it'll be fetched by osc when building RPC.
1397                  *
1398                  * it's not accurate if the file is shared by different
1399                  * jobs.
1400                  */
1401                 lustre_get_jobid(lli->lli_jobid);
1402         } else if (io->ci_type == CIT_SETATTR) {
1403                 if (!cl_io_is_trunc(io))
1404                         io->ci_lockreq = CILR_MANDATORY;
1405         }
1406
1407         /* Enqueue layout lock and get layout version. We need to do this
1408          * even for operations requiring to open file, such as read and write,
1409          * because it might not grant layout lock in IT_OPEN. */
1410         if (result == 0 && !io->ci_ignore_layout) {
1411                 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1412                 if (result == -ENOENT)
1413                         /* If the inode on MDS has been removed, but the objects
1414                          * on OSTs haven't been destroyed (async unlink), layout
1415                          * fetch will return -ENOENT, we'd ingore this error
1416                          * and continue with dirty flush. LU-3230. */
1417                         result = 0;
1418                 if (result < 0)
1419                         CERROR("%s: refresh file layout " DFID " error %d.\n",
1420                                 ll_get_fsname(inode->i_sb, NULL, 0),
1421                                 PFID(lu_object_fid(&obj->co_lu)), result);
1422         }
1423
1424         RETURN(result);
1425 }