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