Whamcloud - gitweb
7aa3cfd66967e5c54c57d1384576b4323290ecd8
[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 = OBD_OBJECT_EOF;
344
345                 io->ci_need_write_intent = 0;
346
347                 LASSERT(io->ci_type == CIT_WRITE ||
348                         cl_io_is_trunc(io) || cl_io_is_mkwrite(io));
349
350                 if (io->ci_type == CIT_WRITE) {
351                         if (!cl_io_is_append(io)) {
352                                 start = io->u.ci_rw.crw_pos;
353                                 end = start + io->u.ci_rw.crw_count;
354                         }
355                 } else if (cl_io_is_trunc(io)) {
356                         end = io->u.ci_setattr.sa_attr.lvb_size;
357                 } else { /* mkwrite */
358                         pgoff_t index = io->u.ci_fault.ft_index;
359
360                         start = cl_offset(io->ci_obj, index);
361                         end = cl_offset(io->ci_obj, index + 1);
362                 }
363
364                 CDEBUG(D_VFSTRACE, DFID" type %d [%llx, %llx)\n",
365                        PFID(lu_object_fid(&obj->co_lu)), io->ci_type,
366                        start, end);
367                 rc = ll_layout_write_intent(inode, start, end);
368                 io->ci_result = rc;
369                 if (!rc)
370                         io->ci_need_restart = 1;
371         }
372
373         if (!io->ci_ignore_layout && io->ci_verify_layout) {
374                 __u32 gen = 0;
375
376                 /* check layout version */
377                 ll_layout_refresh(inode, &gen);
378                 io->ci_need_restart = vio->vui_layout_gen != gen;
379                 if (io->ci_need_restart) {
380                         CDEBUG(D_VFSTRACE,
381                                DFID" layout changed from %d to %d.\n",
382                                PFID(lu_object_fid(&obj->co_lu)),
383                                vio->vui_layout_gen, gen);
384                         /* today successful restore is the only possible
385                          * case */
386                         /* restore was done, clear restoring state */
387                         ll_file_clear_flag(ll_i2info(vvp_object_inode(obj)),
388                                            LLIF_FILE_RESTORING);
389                 }
390         }
391 }
392
393 static void vvp_io_fault_fini(const struct lu_env *env,
394                               const struct cl_io_slice *ios)
395 {
396         struct cl_io   *io   = ios->cis_io;
397         struct cl_page *page = io->u.ci_fault.ft_page;
398
399         CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
400
401         if (page != NULL) {
402                 lu_ref_del(&page->cp_reference, "fault", io);
403                 cl_page_put(env, page);
404                 io->u.ci_fault.ft_page = NULL;
405         }
406         vvp_io_fini(env, ios);
407 }
408
409 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
410 {
411         /*
412          * we only want to hold PW locks if the mmap() can generate
413          * writes back to the file and that only happens in shared
414          * writable vmas
415          */
416         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
417                 return CLM_WRITE;
418         return CLM_READ;
419 }
420
421 static int vvp_mmap_locks(const struct lu_env *env,
422                           struct vvp_io *vio, struct cl_io *io)
423 {
424         struct vvp_thread_info *vti = vvp_env_info(env);
425         struct mm_struct *mm = current->mm;
426         struct vm_area_struct *vma;
427         struct cl_lock_descr *descr = &vti->vti_descr;
428         union ldlm_policy_data policy;
429         struct iovec iov;
430         struct iov_iter i;
431         int result = 0;
432         ENTRY;
433
434         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
435
436         if (!cl_is_normalio(env, io))
437                 RETURN(0);
438
439         /* nfs or loop back device write */
440         if (vio->vui_iter == NULL)
441                 RETURN(0);
442
443         /* No MM (e.g. NFS)? No vmas too. */
444         if (mm == NULL)
445                 RETURN(0);
446
447         iov_for_each(iov, i, *(vio->vui_iter)) {
448                 unsigned long addr = (unsigned long)iov.iov_base;
449                 size_t count = iov.iov_len;
450
451                 if (count == 0)
452                         continue;
453
454                 count += addr & ~PAGE_MASK;
455                 addr &= PAGE_MASK;
456
457                 down_read(&mm->mmap_sem);
458                 while((vma = our_vma(mm, addr, count)) != NULL) {
459                         struct dentry *de = file_dentry(vma->vm_file);
460                         struct inode *inode = de->d_inode;
461                         int flags = CEF_MUST;
462
463                         if (ll_file_nolock(vma->vm_file)) {
464                                 /*
465                                  * For no lock case is not allowed for mmap
466                                  */
467                                 result = -EINVAL;
468                                 break;
469                         }
470
471                         /*
472                          * XXX: Required lock mode can be weakened: CIT_WRITE
473                          * io only ever reads user level buffer, and CIT_READ
474                          * only writes on it.
475                          */
476                         policy_from_vma(&policy, vma, addr, count);
477                         descr->cld_mode = vvp_mode_from_vma(vma);
478                         descr->cld_obj = ll_i2info(inode)->lli_clob;
479                         descr->cld_start = cl_index(descr->cld_obj,
480                                                     policy.l_extent.start);
481                         descr->cld_end = cl_index(descr->cld_obj,
482                                                   policy.l_extent.end);
483                         descr->cld_enq_flags = flags;
484                         result = cl_io_lock_alloc_add(env, io, descr);
485
486                         CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
487                                descr->cld_mode, descr->cld_start,
488                                descr->cld_end);
489
490                         if (result < 0)
491                                 break;
492
493                         if (vma->vm_end - addr >= count)
494                                 break;
495
496                         count -= vma->vm_end - addr;
497                         addr = vma->vm_end;
498                 }
499                 up_read(&mm->mmap_sem);
500                 if (result < 0)
501                         break;
502         }
503         RETURN(result);
504 }
505
506 static void vvp_io_advance(const struct lu_env *env,
507                            const struct cl_io_slice *ios,
508                            size_t nob)
509 {
510         struct vvp_io    *vio = cl2vvp_io(env, ios);
511         struct cl_io     *io  = ios->cis_io;
512         struct cl_object *obj = ios->cis_io->ci_obj;
513
514         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
515
516         if (!cl_is_normalio(env, io))
517                 return;
518
519         vio->vui_tot_count -= nob;
520         iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);
521 }
522
523 static void vvp_io_update_iov(const struct lu_env *env,
524                               struct vvp_io *vio, struct cl_io *io)
525 {
526         size_t size = io->u.ci_rw.crw_count;
527
528         if (!cl_is_normalio(env, io) || vio->vui_iter == NULL)
529                 return;
530
531         iov_iter_truncate(vio->vui_iter, size);
532 }
533
534 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
535                           enum cl_lock_mode mode, loff_t start, loff_t end)
536 {
537         struct vvp_io *vio = vvp_env_io(env);
538         int result;
539         int ast_flags = 0;
540
541         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
542         ENTRY;
543
544         vvp_io_update_iov(env, vio, io);
545
546         if (io->u.ci_rw.crw_nonblock)
547                 ast_flags |= CEF_NONBLOCK;
548
549         result = vvp_mmap_locks(env, vio, io);
550         if (result == 0)
551                 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
552
553         RETURN(result);
554 }
555
556 static int vvp_io_read_lock(const struct lu_env *env,
557                             const struct cl_io_slice *ios)
558 {
559         struct cl_io            *io  = ios->cis_io;
560         struct cl_io_rw_common  *rd = &io->u.ci_rd.rd;
561         int result;
562
563         ENTRY;
564         result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
565                                 rd->crw_pos + rd->crw_count - 1);
566         RETURN(result);
567 }
568
569 static int vvp_io_fault_lock(const struct lu_env *env,
570                              const struct cl_io_slice *ios)
571 {
572         struct cl_io *io   = ios->cis_io;
573         struct vvp_io *vio = cl2vvp_io(env, ios);
574         /*
575          * XXX LDLM_FL_CBPENDING
576          */
577         return vvp_io_one_lock_index(env,
578                                      io, 0,
579                                      vvp_mode_from_vma(vio->u.fault.ft_vma),
580                                      io->u.ci_fault.ft_index,
581                                      io->u.ci_fault.ft_index);
582 }
583
584 static int vvp_io_write_lock(const struct lu_env *env,
585                              const struct cl_io_slice *ios)
586 {
587         struct cl_io *io = ios->cis_io;
588         loff_t start;
589         loff_t end;
590
591         if (io->u.ci_wr.wr_append) {
592                 start = 0;
593                 end   = OBD_OBJECT_EOF;
594         } else {
595                 start = io->u.ci_wr.wr.crw_pos;
596                 end   = start + io->u.ci_wr.wr.crw_count - 1;
597         }
598         return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
599 }
600
601 static int vvp_io_setattr_iter_init(const struct lu_env *env,
602                                     const struct cl_io_slice *ios)
603 {
604         return 0;
605 }
606
607 /**
608  * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
609  *
610  * Handles "lockless io" mode when extent locking is done by server.
611  */
612 static int vvp_io_setattr_lock(const struct lu_env *env,
613                                const struct cl_io_slice *ios)
614 {
615         struct cl_io  *io  = ios->cis_io;
616         __u64 new_size;
617         __u32 enqflags = 0;
618
619         if (cl_io_is_trunc(io)) {
620                 new_size = io->u.ci_setattr.sa_attr.lvb_size;
621                 if (new_size == 0)
622                         enqflags = CEF_DISCARD_DATA;
623         } else {
624                 unsigned int valid = io->u.ci_setattr.sa_valid;
625
626                 if (!(valid & TIMES_SET_FLAGS))
627                         return 0;
628
629                 if ((!(valid & ATTR_MTIME) ||
630                      io->u.ci_setattr.sa_attr.lvb_mtime >=
631                      io->u.ci_setattr.sa_attr.lvb_ctime) &&
632                     (!(valid & ATTR_ATIME) ||
633                      io->u.ci_setattr.sa_attr.lvb_atime >=
634                      io->u.ci_setattr.sa_attr.lvb_ctime))
635                         return 0;
636
637                 new_size = 0;
638         }
639
640         return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
641                                new_size, OBD_OBJECT_EOF);
642 }
643
644 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
645 {
646         int     result;
647
648         /*
649          * Only ll_inode_size_lock is taken at this level.
650          */
651         ll_inode_size_lock(inode);
652         result = inode_newsize_ok(inode, size);
653         if (result < 0) {
654                 ll_inode_size_unlock(inode);
655                 return result;
656         }
657         i_size_write(inode, size);
658
659         ll_truncate_pagecache(inode, size);
660         ll_inode_size_unlock(inode);
661         return result;
662 }
663
664 static int vvp_io_setattr_time(const struct lu_env *env,
665                                const struct cl_io_slice *ios)
666 {
667         struct cl_io       *io    = ios->cis_io;
668         struct cl_object   *obj   = io->ci_obj;
669         struct cl_attr     *attr  = vvp_env_thread_attr(env);
670         int result;
671         unsigned valid = CAT_CTIME;
672
673         cl_object_attr_lock(obj);
674         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
675         if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
676                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
677                 valid |= CAT_ATIME;
678         }
679         if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
680                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
681                 valid |= CAT_MTIME;
682         }
683         result = cl_object_attr_update(env, obj, attr, valid);
684         cl_object_attr_unlock(obj);
685
686         return result;
687 }
688
689 static int vvp_io_setattr_start(const struct lu_env *env,
690                                 const struct cl_io_slice *ios)
691 {
692         struct cl_io            *io    = ios->cis_io;
693         struct inode            *inode = vvp_object_inode(io->ci_obj);
694         struct ll_inode_info    *lli   = ll_i2info(inode);
695
696         if (cl_io_is_trunc(io)) {
697                 down_write(&lli->lli_trunc_sem);
698                 inode_lock(inode);
699                 inode_dio_wait(inode);
700         } else {
701                 inode_lock(inode);
702         }
703
704         if (io->u.ci_setattr.sa_valid & TIMES_SET_FLAGS)
705                 return vvp_io_setattr_time(env, ios);
706
707         return 0;
708 }
709
710 static void vvp_io_setattr_end(const struct lu_env *env,
711                                const struct cl_io_slice *ios)
712 {
713         struct cl_io            *io    = ios->cis_io;
714         struct inode            *inode = vvp_object_inode(io->ci_obj);
715         struct ll_inode_info    *lli   = ll_i2info(inode);
716
717         if (cl_io_is_trunc(io)) {
718                 /* Truncate in memory pages - they must be clean pages
719                  * because osc has already notified to destroy osc_extents. */
720                 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
721                 inode_dio_write_done(inode);
722                 inode_unlock(inode);
723                 up_write(&lli->lli_trunc_sem);
724         } else {
725                 inode_unlock(inode);
726         }
727 }
728
729 static void vvp_io_setattr_fini(const struct lu_env *env,
730                                 const struct cl_io_slice *ios)
731 {
732         bool restore_needed = ios->cis_io->ci_restore_needed;
733         struct inode *inode = vvp_object_inode(ios->cis_obj);
734
735         vvp_io_fini(env, ios);
736
737         if (restore_needed && !ios->cis_io->ci_restore_needed) {
738                 /* restore finished, set data modified flag for HSM */
739                 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
740         }
741 }
742
743 static int vvp_io_read_start(const struct lu_env *env,
744                              const struct cl_io_slice *ios)
745 {
746         struct vvp_io           *vio   = cl2vvp_io(env, ios);
747         struct cl_io            *io    = ios->cis_io;
748         struct cl_object        *obj   = io->ci_obj;
749         struct inode            *inode = vvp_object_inode(obj);
750         struct ll_inode_info    *lli   = ll_i2info(inode);
751         struct file             *file  = vio->vui_fd->fd_file;
752
753         int     result;
754         loff_t  pos = io->u.ci_rd.rd.crw_pos;
755         long    cnt = io->u.ci_rd.rd.crw_count;
756         long    tot = vio->vui_tot_count;
757         int     exceed = 0;
758
759         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
760
761         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
762
763         if (vio->vui_io_subtype == IO_NORMAL)
764                 down_read(&lli->lli_trunc_sem);
765
766         if (!can_populate_pages(env, io, inode))
767                 return 0;
768
769         result = vvp_prep_size(env, obj, io, pos, tot, &exceed);
770         if (result != 0)
771                 return result;
772         else if (exceed != 0)
773                 goto out;
774
775         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
776                         "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
777                         inode->i_ino, cnt, pos, i_size_read(inode));
778
779         /* turn off the kernel's read-ahead */
780         vio->vui_fd->fd_file->f_ra.ra_pages = 0;
781
782         /* initialize read-ahead window once per syscall */
783         if (!vio->vui_ra_valid) {
784                 vio->vui_ra_valid = true;
785                 vio->vui_ra_start = cl_index(obj, pos);
786                 vio->vui_ra_count = cl_index(obj, tot + PAGE_SIZE - 1);
787                 ll_ras_enter(file);
788         }
789
790         /* BUG: 5972 */
791         file_accessed(file);
792         switch (vio->vui_io_subtype) {
793         case IO_NORMAL:
794                 LASSERT(vio->vui_iocb->ki_pos == pos);
795                 result = generic_file_read_iter(vio->vui_iocb, vio->vui_iter);
796                 break;
797         case IO_SPLICE:
798                 result = generic_file_splice_read(file, &pos,
799                                                   vio->u.splice.vui_pipe, cnt,
800                                                   vio->u.splice.vui_flags);
801                 /* LU-1109: do splice read stripe by stripe otherwise if it
802                  * may make nfsd stuck if this read occupied all internal pipe
803                  * buffers. */
804                 io->ci_continue = 0;
805                 break;
806         default:
807                 CERROR("Wrong IO type %u\n", vio->vui_io_subtype);
808                 LBUG();
809         }
810
811 out:
812         if (result >= 0) {
813                 if (result < cnt)
814                         io->ci_continue = 0;
815                 io->ci_nob += result;
816                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, vio->vui_fd,
817                                   pos, result, READ);
818                 result = 0;
819         }
820
821         return result;
822 }
823
824 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
825                               struct cl_page_list *plist, int from, int to)
826 {
827         struct cl_2queue *queue = &io->ci_queue;
828         struct cl_page *page;
829         unsigned int bytes = 0;
830         int rc = 0;
831         ENTRY;
832
833         if (plist->pl_nr == 0)
834                 RETURN(0);
835
836         if (from > 0 || to != PAGE_SIZE) {
837                 page = cl_page_list_first(plist);
838                 if (plist->pl_nr == 1) {
839                         cl_page_clip(env, page, from, to);
840                 } else {
841                         if (from > 0)
842                                 cl_page_clip(env, page, from, PAGE_SIZE);
843                         if (to != PAGE_SIZE) {
844                                 page = cl_page_list_last(plist);
845                                 cl_page_clip(env, page, 0, to);
846                         }
847                 }
848         }
849
850         cl_2queue_init(queue);
851         cl_page_list_splice(plist, &queue->c2_qin);
852         rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
853
854         /* plist is not sorted any more */
855         cl_page_list_splice(&queue->c2_qin, plist);
856         cl_page_list_splice(&queue->c2_qout, plist);
857         cl_2queue_fini(env, queue);
858
859         if (rc == 0) {
860                 /* calculate bytes */
861                 bytes = plist->pl_nr << PAGE_SHIFT;
862                 bytes -= from + PAGE_SIZE - to;
863
864                 while (plist->pl_nr > 0) {
865                         page = cl_page_list_first(plist);
866                         cl_page_list_del(env, plist, page);
867
868                         cl_page_clip(env, page, 0, PAGE_SIZE);
869
870                         SetPageUptodate(cl_page_vmpage(page));
871                         cl_page_disown(env, io, page);
872
873                         /* held in ll_cl_init() */
874                         lu_ref_del(&page->cp_reference, "cl_io", io);
875                         cl_page_put(env, page);
876                 }
877         }
878
879         RETURN(bytes > 0 ? bytes : rc);
880 }
881
882 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
883                                 struct cl_page *page)
884 {
885         struct page *vmpage = page->cp_vmpage;
886
887         SetPageUptodate(vmpage);
888         set_page_dirty(vmpage);
889
890         cl_page_disown(env, io, page);
891
892         /* held in ll_cl_init() */
893         lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
894         cl_page_put(env, page);
895 }
896
897 /* make sure the page list is contiguous */
898 static bool page_list_sanity_check(struct cl_object *obj,
899                                    struct cl_page_list *plist)
900 {
901         struct cl_page *page;
902         pgoff_t index = CL_PAGE_EOF;
903
904         cl_page_list_for_each(page, plist) {
905                 struct vvp_page *vpg = cl_object_page_slice(obj, page);
906
907                 if (index == CL_PAGE_EOF) {
908                         index = vvp_index(vpg);
909                         continue;
910                 }
911
912                 ++index;
913                 if (index == vvp_index(vpg))
914                         continue;
915
916                 return false;
917         }
918         return true;
919 }
920
921 /* Return how many bytes have queued or written */
922 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
923 {
924         struct cl_object *obj = io->ci_obj;
925         struct inode *inode = vvp_object_inode(obj);
926         struct vvp_io *vio = vvp_env_io(env);
927         struct cl_page_list *queue = &vio->u.write.vui_queue;
928         struct cl_page *page;
929         int rc = 0;
930         int bytes = 0;
931         unsigned int npages = vio->u.write.vui_queue.pl_nr;
932         ENTRY;
933
934         if (npages == 0)
935                 RETURN(0);
936
937         CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
938                 npages, vio->u.write.vui_from, vio->u.write.vui_to);
939
940         LASSERT(page_list_sanity_check(obj, queue));
941
942         /* submit IO with async write */
943         rc = cl_io_commit_async(env, io, queue,
944                                 vio->u.write.vui_from, vio->u.write.vui_to,
945                                 write_commit_callback);
946         npages -= queue->pl_nr; /* already committed pages */
947         if (npages > 0) {
948                 /* calculate how many bytes were written */
949                 bytes = npages << PAGE_SHIFT;
950
951                 /* first page */
952                 bytes -= vio->u.write.vui_from;
953                 if (queue->pl_nr == 0) /* last page */
954                         bytes -= PAGE_SIZE - vio->u.write.vui_to;
955                 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
956
957                 vio->u.write.vui_written += bytes;
958
959                 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
960                         npages, bytes, vio->u.write.vui_written);
961
962                 /* the first page must have been written. */
963                 vio->u.write.vui_from = 0;
964         }
965         LASSERT(page_list_sanity_check(obj, queue));
966         LASSERT(ergo(rc == 0, queue->pl_nr == 0));
967
968         /* out of quota, try sync write */
969         if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
970                 rc = vvp_io_commit_sync(env, io, queue,
971                                         vio->u.write.vui_from,
972                                         vio->u.write.vui_to);
973                 if (rc > 0) {
974                         vio->u.write.vui_written += rc;
975                         rc = 0;
976                 }
977         }
978
979         /* update inode size */
980         ll_merge_attr(env, inode);
981
982         /* Now the pages in queue were failed to commit, discard them
983          * unless they were dirtied before. */
984         while (queue->pl_nr > 0) {
985                 page = cl_page_list_first(queue);
986                 cl_page_list_del(env, queue, page);
987
988                 if (!PageDirty(cl_page_vmpage(page)))
989                         cl_page_discard(env, io, page);
990
991                 cl_page_disown(env, io, page);
992
993                 /* held in ll_cl_init() */
994                 lu_ref_del(&page->cp_reference, "cl_io", io);
995                 cl_page_put(env, page);
996         }
997         cl_page_list_fini(env, queue);
998
999         RETURN(rc);
1000 }
1001
1002 static int vvp_io_write_start(const struct lu_env *env,
1003                               const struct cl_io_slice *ios)
1004 {
1005         struct vvp_io           *vio   = cl2vvp_io(env, ios);
1006         struct cl_io            *io    = ios->cis_io;
1007         struct cl_object        *obj   = io->ci_obj;
1008         struct inode            *inode = vvp_object_inode(obj);
1009         struct ll_inode_info    *lli   = ll_i2info(inode);
1010         ssize_t                  result = 0;
1011         loff_t                   pos = io->u.ci_wr.wr.crw_pos;
1012         size_t                   cnt = io->u.ci_wr.wr.crw_count;
1013
1014         ENTRY;
1015
1016         if (vio->vui_io_subtype == IO_NORMAL)
1017                 down_read(&lli->lli_trunc_sem);
1018
1019         if (!can_populate_pages(env, io, inode))
1020                 RETURN(0);
1021
1022         if (cl_io_is_append(io)) {
1023                 /*
1024                  * PARALLEL IO This has to be changed for parallel IO doing
1025                  * out-of-order writes.
1026                  */
1027                 ll_merge_attr(env, inode);
1028                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
1029                 vio->vui_iocb->ki_pos = pos;
1030         } else {
1031                 LASSERT(vio->vui_iocb->ki_pos == pos);
1032         }
1033
1034         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
1035
1036         /* The maximum Lustre file size is variable, based on the OST maximum
1037          * object size and number of stripes.  This needs another check in
1038          * addition to the VFS checks earlier. */
1039         if (pos + cnt > ll_file_maxbytes(inode)) {
1040                 CDEBUG(D_INODE,
1041                        "%s: file "DFID" offset %llu > maxbytes %llu\n",
1042                        ll_get_fsname(inode->i_sb, NULL, 0),
1043                        PFID(ll_inode2fid(inode)), pos + cnt,
1044                        ll_file_maxbytes(inode));
1045                 RETURN(-EFBIG);
1046         }
1047
1048         if (vio->vui_iter == NULL) {
1049                 /* from a temp io in ll_cl_init(). */
1050                 result = 0;
1051         } else {
1052                 /*
1053                  * When using the locked AIO function (generic_file_aio_write())
1054                  * testing has shown the inode mutex to be a limiting factor
1055                  * with multi-threaded single shared file performance. To get
1056                  * around this, we now use the lockless version. To maintain
1057                  * consistency, proper locking to protect against writes,
1058                  * trucates, etc. is handled in the higher layers of lustre.
1059                  */
1060                 bool lock_node = !IS_NOSEC(inode);
1061
1062                 if (lock_node)
1063                         inode_lock(inode);
1064                 result = __generic_file_write_iter(vio->vui_iocb,
1065                                                    vio->vui_iter);
1066                 if (lock_node)
1067                         inode_unlock(inode);
1068
1069                 if (result > 0 || result == -EIOCBQUEUED)
1070 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1071                         result = generic_write_sync(vio->vui_iocb, result);
1072 #else
1073                 {
1074                         ssize_t err;
1075
1076                         err = generic_write_sync(vio->vui_iocb->ki_filp, pos,
1077                                                  result);
1078                         if (err < 0 && result > 0)
1079                                 result = err;
1080                 }
1081 #endif
1082
1083         }
1084         if (result > 0) {
1085                 result = vvp_io_write_commit(env, io);
1086                 if (vio->u.write.vui_written > 0) {
1087                         result = vio->u.write.vui_written;
1088                         io->ci_nob += result;
1089
1090                         CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
1091                                 io->ci_nob, result);
1092                 }
1093         }
1094         if (result > 0) {
1095                 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1096
1097                 if (result < cnt)
1098                         io->ci_continue = 0;
1099                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
1100                                   vio->vui_fd, pos, result, WRITE);
1101                 result = 0;
1102         }
1103
1104         RETURN(result);
1105 }
1106
1107 static void vvp_io_rw_end(const struct lu_env *env,
1108                           const struct cl_io_slice *ios)
1109 {
1110         struct vvp_io           *vio = cl2vvp_io(env, ios);
1111         struct inode            *inode = vvp_object_inode(ios->cis_obj);
1112         struct ll_inode_info    *lli = ll_i2info(inode);
1113
1114         if (vio->vui_io_subtype == IO_NORMAL)
1115                 up_read(&lli->lli_trunc_sem);
1116 }
1117
1118 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1119 {
1120         struct vm_fault *vmf = cfio->ft_vmf;
1121
1122         cfio->ft_flags = filemap_fault(cfio->ft_vma, vmf);
1123         cfio->ft_flags_valid = 1;
1124
1125         if (vmf->page) {
1126                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1127                                vmf->virtual_address);
1128                 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1129                         lock_page(vmf->page);
1130                         cfio->ft_flags |= VM_FAULT_LOCKED;
1131                 }
1132
1133                 cfio->ft_vmpage = vmf->page;
1134
1135                 return 0;
1136         }
1137
1138         if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1139                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
1140                 return -EFAULT;
1141         }
1142
1143         if (cfio->ft_flags & VM_FAULT_OOM) {
1144                 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
1145                 return -ENOMEM;
1146         }
1147
1148         if (cfio->ft_flags & VM_FAULT_RETRY)
1149                 return -EAGAIN;
1150
1151         CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1152
1153         return -EINVAL;
1154 }
1155
1156 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1157                                     struct cl_page *page)
1158 {
1159         set_page_dirty(page->cp_vmpage);
1160 }
1161
1162 static int vvp_io_fault_start(const struct lu_env *env,
1163                               const struct cl_io_slice *ios)
1164 {
1165         struct vvp_io           *vio   = cl2vvp_io(env, ios);
1166         struct cl_io            *io    = ios->cis_io;
1167         struct cl_object        *obj   = io->ci_obj;
1168         struct inode            *inode = vvp_object_inode(obj);
1169         struct ll_inode_info    *lli   = ll_i2info(inode);
1170         struct cl_fault_io      *fio   = &io->u.ci_fault;
1171         struct vvp_fault_io     *cfio  = &vio->u.fault;
1172         loff_t                   offset;
1173         int                      result = 0;
1174         struct page             *vmpage = NULL;
1175         struct cl_page          *page;
1176         loff_t                   size;
1177         pgoff_t                  last_index;
1178         ENTRY;
1179
1180         down_read(&lli->lli_trunc_sem);
1181
1182         /* offset of the last byte on the page */
1183         offset = cl_offset(obj, fio->ft_index + 1) - 1;
1184         LASSERT(cl_index(obj, offset) == fio->ft_index);
1185         result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1186         if (result != 0)
1187                 RETURN(result);
1188
1189         /* must return locked page */
1190         if (fio->ft_mkwrite) {
1191                 LASSERT(cfio->ft_vmpage != NULL);
1192                 lock_page(cfio->ft_vmpage);
1193         } else {
1194                 result = vvp_io_kernel_fault(cfio);
1195                 if (result != 0)
1196                         RETURN(result);
1197         }
1198
1199         vmpage = cfio->ft_vmpage;
1200         LASSERT(PageLocked(vmpage));
1201
1202         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1203                 ll_invalidate_page(vmpage);
1204
1205         size = i_size_read(inode);
1206         /* Though we have already held a cl_lock upon this page, but
1207          * it still can be truncated locally. */
1208         if (unlikely((vmpage->mapping != inode->i_mapping) ||
1209                      (page_offset(vmpage) > size))) {
1210                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1211
1212                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1213                  * and retry. */
1214                 GOTO(out, result = +1);
1215         }
1216
1217         last_index = cl_index(obj, size - 1);
1218
1219         if (fio->ft_mkwrite ) {
1220                 /*
1221                  * Capture the size while holding the lli_trunc_sem from above
1222                  * we want to make sure that we complete the mkwrite action
1223                  * while holding this lock. We need to make sure that we are
1224                  * not past the end of the file.
1225                  */
1226                 if (last_index < fio->ft_index) {
1227                         CDEBUG(D_PAGE,
1228                                 "llite: mkwrite and truncate race happened: "
1229                                 "%p: 0x%lx 0x%lx\n",
1230                                 vmpage->mapping,fio->ft_index,last_index);
1231                         /*
1232                          * We need to return if we are
1233                          * passed the end of the file. This will propagate
1234                          * up the call stack to ll_page_mkwrite where
1235                          * we will return VM_FAULT_NOPAGE. Any non-negative
1236                          * value returned here will be silently
1237                          * converted to 0. If the vmpage->mapping is null
1238                          * the error code would be converted back to ENODATA
1239                          * in ll_page_mkwrite0. Thus we return -ENODATA
1240                          * to handle both cases
1241                          */
1242                         GOTO(out, result = -ENODATA);
1243                 }
1244         }
1245
1246         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1247         if (IS_ERR(page))
1248                 GOTO(out, result = PTR_ERR(page));
1249
1250         /* if page is going to be written, we should add this page into cache
1251          * earlier. */
1252         if (fio->ft_mkwrite) {
1253                 wait_on_page_writeback(vmpage);
1254                 if (!PageDirty(vmpage)) {
1255                         struct cl_page_list *plist = &io->ci_queue.c2_qin;
1256                         struct vvp_page *vpg = cl_object_page_slice(obj, page);
1257                         int to = PAGE_SIZE;
1258
1259                         /* vvp_page_assume() calls wait_on_page_writeback(). */
1260                         cl_page_assume(env, io, page);
1261
1262                         cl_page_list_init(plist);
1263                         cl_page_list_add(plist, page);
1264
1265                         /* size fixup */
1266                         if (last_index == vvp_index(vpg))
1267                                 to = size & ~PAGE_MASK;
1268
1269                         /* Do not set Dirty bit here so that in case IO is
1270                          * started before the page is really made dirty, we
1271                          * still have chance to detect it. */
1272                         result = cl_io_commit_async(env, io, plist, 0, to,
1273                                                     mkwrite_commit_callback);
1274                         LASSERT(cl_page_is_owned(page, io));
1275                         cl_page_list_fini(env, plist);
1276
1277                         vmpage = NULL;
1278                         if (result < 0) {
1279                                 cl_page_discard(env, io, page);
1280                                 cl_page_disown(env, io, page);
1281
1282                                 cl_page_put(env, page);
1283
1284                                 /* we're in big trouble, what can we do now? */
1285                                 if (result == -EDQUOT)
1286                                         result = -ENOSPC;
1287                                 GOTO(out, result);
1288                         } else
1289                                 cl_page_disown(env, io, page);
1290                 }
1291         }
1292
1293         /*
1294          * The ft_index is only used in the case of
1295          * a mkwrite action. We need to check
1296          * our assertions are correct, since
1297          * we should have caught this above
1298          */
1299         LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1300         if (fio->ft_index == last_index)
1301                 /*
1302                  * Last page is mapped partially.
1303                  */
1304                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1305         else
1306                 fio->ft_nob = cl_page_size(obj);
1307
1308         lu_ref_add(&page->cp_reference, "fault", io);
1309         fio->ft_page = page;
1310         EXIT;
1311
1312 out:
1313         /* return unlocked vmpage to avoid deadlocking */
1314         if (vmpage != NULL)
1315                 unlock_page(vmpage);
1316
1317         cfio->ft_flags &= ~VM_FAULT_LOCKED;
1318
1319         return result;
1320 }
1321
1322 static void vvp_io_fault_end(const struct lu_env *env,
1323                              const struct cl_io_slice *ios)
1324 {
1325         struct inode            *inode = vvp_object_inode(ios->cis_obj);
1326         struct ll_inode_info    *lli   = ll_i2info(inode);
1327
1328         CLOBINVRNT(env, ios->cis_io->ci_obj,
1329                    vvp_object_invariant(ios->cis_io->ci_obj));
1330         up_read(&lli->lli_trunc_sem);
1331 }
1332
1333 static int vvp_io_fsync_start(const struct lu_env *env,
1334                               const struct cl_io_slice *ios)
1335 {
1336         /* we should mark TOWRITE bit to each dirty page in radix tree to
1337          * verify pages have been written, but this is difficult because of
1338          * race. */
1339         return 0;
1340 }
1341
1342 static int vvp_io_read_ahead(const struct lu_env *env,
1343                              const struct cl_io_slice *ios,
1344                              pgoff_t start, struct cl_read_ahead *ra)
1345 {
1346         int result = 0;
1347         ENTRY;
1348
1349         if (ios->cis_io->ci_type == CIT_READ ||
1350             ios->cis_io->ci_type == CIT_FAULT) {
1351                 struct vvp_io *vio = cl2vvp_io(env, ios);
1352
1353                 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1354                         ra->cra_end = CL_PAGE_EOF;
1355                         result = +1; /* no need to call down */
1356                 }
1357         }
1358
1359         RETURN(result);
1360 }
1361
1362 static const struct cl_io_operations vvp_io_ops = {
1363         .op = {
1364                 [CIT_READ] = {
1365                         .cio_fini       = vvp_io_fini,
1366                         .cio_lock       = vvp_io_read_lock,
1367                         .cio_start      = vvp_io_read_start,
1368                         .cio_end        = vvp_io_rw_end,
1369                         .cio_advance    = vvp_io_advance,
1370                 },
1371                 [CIT_WRITE] = {
1372                         .cio_fini      = vvp_io_fini,
1373                         .cio_iter_init = vvp_io_write_iter_init,
1374                         .cio_iter_fini = vvp_io_write_iter_fini,
1375                         .cio_lock      = vvp_io_write_lock,
1376                         .cio_start     = vvp_io_write_start,
1377                         .cio_end       = vvp_io_rw_end,
1378                         .cio_advance   = vvp_io_advance,
1379                 },
1380                 [CIT_SETATTR] = {
1381                         .cio_fini       = vvp_io_setattr_fini,
1382                         .cio_iter_init  = vvp_io_setattr_iter_init,
1383                         .cio_lock       = vvp_io_setattr_lock,
1384                         .cio_start      = vvp_io_setattr_start,
1385                         .cio_end        = vvp_io_setattr_end
1386                 },
1387                 [CIT_FAULT] = {
1388                         .cio_fini      = vvp_io_fault_fini,
1389                         .cio_iter_init = vvp_io_fault_iter_init,
1390                         .cio_lock      = vvp_io_fault_lock,
1391                         .cio_start     = vvp_io_fault_start,
1392                         .cio_end       = vvp_io_fault_end,
1393                 },
1394                 [CIT_FSYNC] = {
1395                         .cio_start      = vvp_io_fsync_start,
1396                         .cio_fini       = vvp_io_fini
1397                 },
1398                 [CIT_MISC] = {
1399                         .cio_fini       = vvp_io_fini
1400                 },
1401                 [CIT_LADVISE] = {
1402                         .cio_fini       = vvp_io_fini
1403                 },
1404         },
1405         .cio_read_ahead = vvp_io_read_ahead
1406 };
1407
1408 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1409                 struct cl_io *io)
1410 {
1411         struct vvp_io      *vio   = vvp_env_io(env);
1412         struct inode       *inode = vvp_object_inode(obj);
1413         int                 result;
1414
1415         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1416         ENTRY;
1417
1418         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1419                "restore needed %d\n",
1420                PFID(lu_object_fid(&obj->co_lu)),
1421                io->ci_ignore_layout, io->ci_verify_layout,
1422                vio->vui_layout_gen, io->ci_restore_needed);
1423
1424         CL_IO_SLICE_CLEAN(vio, vui_cl);
1425         cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1426         vio->vui_ra_valid = false;
1427         result = 0;
1428         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1429                 size_t count;
1430                 struct ll_inode_info *lli = ll_i2info(inode);
1431
1432                 count = io->u.ci_rw.crw_count;
1433                 /* "If nbyte is 0, read() will return 0 and have no other
1434                  *  results."  -- Single Unix Spec */
1435                 if (count == 0)
1436                         result = 1;
1437                 else
1438                         vio->vui_tot_count = count;
1439
1440                 /* for read/write, we store the jobid in the inode, and
1441                  * it'll be fetched by osc when building RPC.
1442                  *
1443                  * it's not accurate if the file is shared by different
1444                  * jobs.
1445                  */
1446                 lustre_get_jobid(lli->lli_jobid);
1447         } else if (io->ci_type == CIT_SETATTR) {
1448                 if (!cl_io_is_trunc(io))
1449                         io->ci_lockreq = CILR_MANDATORY;
1450         }
1451
1452         /* Enqueue layout lock and get layout version. We need to do this
1453          * even for operations requiring to open file, such as read and write,
1454          * because it might not grant layout lock in IT_OPEN. */
1455         if (result == 0 && !io->ci_ignore_layout) {
1456                 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1457                 if (result == -ENOENT)
1458                         /* If the inode on MDS has been removed, but the objects
1459                          * on OSTs haven't been destroyed (async unlink), layout
1460                          * fetch will return -ENOENT, we'd ingore this error
1461                          * and continue with dirty flush. LU-3230. */
1462                         result = 0;
1463                 if (result < 0)
1464                         CERROR("%s: refresh file layout " DFID " error %d.\n",
1465                                 ll_get_fsname(inode->i_sb, NULL, 0),
1466                                 PFID(lu_object_fid(&obj->co_lu)), result);
1467         }
1468
1469         RETURN(result);
1470 }