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