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