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