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