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