Whamcloud - gitweb
LU-17243 build: compatibility updates for kernel 6.6
[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  *
31  * Implementation of cl_io for VVP layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include <obd.h>
40 #include <linux/pagevec.h>
41 #include <linux/memcontrol.h>
42 #include <linux/falloc.h>
43
44 #include "llite_internal.h"
45 #include "vvp_internal.h"
46 #include <lustre_compat.h>
47 #include <libcfs/linux/linux-misc.h>
48
49 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
50                                 const struct cl_io_slice *slice)
51 {
52         struct vvp_io *vio;
53
54         vio = container_of(slice, struct vvp_io, vui_cl);
55         LASSERT(vio == vvp_env_io(env));
56
57         return vio;
58 }
59
60 /* For swapping layout. The file's layout may have changed.
61  * To avoid populating pages to a wrong stripe, we have to verify the
62  * correctness of layout. It works because swapping layout processes
63  * have to acquire group lock.
64  */
65 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
66                                 struct inode *inode)
67 {
68         struct ll_inode_info    *lli = ll_i2info(inode);
69         struct vvp_io           *vio = vvp_env_io(env);
70         bool rc = true;
71
72         switch (io->ci_type) {
73         case CIT_READ:
74         case CIT_WRITE:
75                 /* don't need lock here to check lli_layout_gen as we have held
76                  * extent lock and GROUP lock has to hold to swap layout
77                  */
78                 if (ll_layout_version_get(lli) != vio->vui_layout_gen ||
79                     CFS_FAIL_CHECK_RESET(OBD_FAIL_LLITE_LOST_LAYOUT, 0)) {
80                         io->ci_need_restart = 1;
81                         /* this will cause a short read/write */
82                         io->ci_continue = 0;
83                         rc = false;
84                 }
85         case CIT_FAULT:
86                 /* fault is okay because we've already had a page. */
87         default:
88                 break;
89         }
90
91         return rc;
92 }
93
94 static void vvp_object_size_lock(struct cl_object *obj)
95 {
96         struct inode *inode = vvp_object_inode(obj);
97
98         ll_inode_size_lock(inode);
99         cl_object_attr_lock(obj);
100 }
101
102 static void vvp_object_size_unlock(struct cl_object *obj)
103 {
104         struct inode *inode = vvp_object_inode(obj);
105
106         cl_object_attr_unlock(obj);
107         ll_inode_size_unlock(inode);
108 }
109
110 /*
111  * Helper function that if necessary adjusts file size (inode->i_size), when
112  * position at the offset \a pos is accessed. File size can be arbitrary stale
113  * on a Lustre client, but client at least knows KMS. If accessed area is
114  * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
115  *
116  * Locking: i_size_lock is used to serialize changes to inode size and to
117  * protect consistency between inode size and cl_object
118  * attributes. cl_object_size_lock() protects consistency between cl_attr's of
119  * top-object and sub-objects.
120  */
121 static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj,
122                          struct cl_io *io, loff_t start, size_t bytes,
123                          int *exceed)
124 {
125         struct cl_attr *attr  = vvp_env_thread_attr(env);
126         struct inode *inode = vvp_object_inode(obj);
127         loff_t pos = start + bytes - 1;
128         loff_t kms;
129         int result;
130
131         /*
132          * Consistency guarantees: following possibilities exist for the
133          * relation between region being accessed and real file size at this
134          * moment:
135          *
136          *  (A): the region is completely inside of the file;
137          *
138          *  (B-x): x bytes of region are inside of the file, the rest is
139          *  outside;
140          *
141          *  (C): the region is completely outside of the file.
142          *
143          * This classification is stable under DLM lock already acquired by
144          * the caller, because to change the class, other client has to take
145          * DLM lock conflicting with our lock. Also, any updates to ->i_size
146          * by other threads on this client are serialized by
147          * ll_inode_size_lock(). This guarantees that short reads are handled
148          * correctly in the face of concurrent writes and truncates.
149          */
150         vvp_object_size_lock(obj);
151         result = cl_object_attr_get(env, obj, attr);
152         if (result == 0) {
153                 kms = attr->cat_kms;
154                 if (pos > kms || !attr->cat_kms_valid) {
155                         /*
156                          * A glimpse is necessary to determine whether we
157                          * return a short read (B) or some zeroes at the end
158                          * of the buffer (C)
159                          */
160                         vvp_object_size_unlock(obj);
161                         result = cl_glimpse_lock(env, io, inode, obj, 0);
162                         if (result == 0 && exceed != NULL) {
163                                 /* If objective page index exceed end-of-file
164                                  * page index, return directly. Do not expect
165                                  * kernel will check such case correctly.
166                                  */
167                                 loff_t size = i_size_read(inode);
168                                 unsigned long cur_index = start >>
169                                         PAGE_SHIFT;
170
171                                 if ((size == 0 && cur_index != 0) ||
172                                     (((size - 1) >> PAGE_SHIFT) <
173                                      cur_index))
174                                         *exceed = 1;
175                         }
176
177                         return result;
178                 }
179                 /*
180                  * region is within kms and, hence, within real file
181                  * size (A). We need to increase i_size to cover the
182                  * read region so that generic_file_read() will do its
183                  * job, but that doesn't mean the kms size is
184                  * _correct_, it is only the _minimum_ size. If
185                  * someone does a stat they will get the correct size
186                  * which will always be >= the kms value here.
187                  * b=11081
188                  */
189                 if (i_size_read(inode) < kms) {
190                         i_size_write(inode, kms);
191                         CDEBUG(D_VFSTRACE,
192                                DFID" updating i_size %llu\n",
193                                PFID(lu_object_fid(&obj->co_lu)),
194                                (__u64)i_size_read(inode));
195                 }
196         }
197
198         vvp_object_size_unlock(obj);
199
200         return result;
201 }
202
203 /* io operations. */
204 static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
205                                  __u32 enqflags, enum cl_lock_mode mode,
206                                  pgoff_t start, pgoff_t end)
207 {
208         struct vvp_io          *vio   = vvp_env_io(env);
209         struct cl_lock_descr   *descr = &vio->vui_link.cill_descr;
210         struct cl_object       *obj   = io->ci_obj;
211
212         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
213         ENTRY;
214
215         CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
216
217         memset(&vio->vui_link, 0, sizeof(vio->vui_link));
218
219         if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
220                 descr->cld_mode = CLM_GROUP;
221                 descr->cld_gid  = vio->vui_fd->fd_grouplock.lg_gid;
222                 enqflags |= CEF_LOCK_MATCH;
223         } else {
224                 descr->cld_mode  = mode;
225         }
226
227         descr->cld_obj   = obj;
228         descr->cld_start = start;
229         descr->cld_end   = end;
230         descr->cld_enq_flags = enqflags;
231
232         cl_io_lock_add(env, io, &vio->vui_link);
233
234         RETURN(0);
235 }
236
237 static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io,
238                            __u32 enqflags, enum cl_lock_mode mode,
239                            loff_t start, loff_t end)
240 {
241         return vvp_io_one_lock_index(env, io, enqflags, mode,
242                                      start >> PAGE_SHIFT, end >> PAGE_SHIFT);
243 }
244
245 static int vvp_io_write_iter_init(const struct lu_env *env,
246                                   const struct cl_io_slice *ios)
247 {
248         struct vvp_io *vio = cl2vvp_io(env, ios);
249
250         cl_page_list_init(&vio->u.readwrite.vui_queue);
251         vio->u.readwrite.vui_written = 0;
252         vio->u.readwrite.vui_from = 0;
253         vio->u.readwrite.vui_to = PAGE_SIZE;
254
255         return 0;
256 }
257
258 static int vvp_io_read_iter_init(const struct lu_env *env,
259                                  const struct cl_io_slice *ios)
260 {
261         struct vvp_io *vio = cl2vvp_io(env, ios);
262
263         vio->u.readwrite.vui_read = 0;
264
265         return 0;
266 }
267
268 static void vvp_io_write_iter_fini(const struct lu_env *env,
269                                    const struct cl_io_slice *ios)
270 {
271         struct vvp_io *vio = cl2vvp_io(env, ios);
272
273         LASSERT(vio->u.readwrite.vui_queue.pl_nr == 0);
274 }
275
276 static int vvp_io_fault_iter_init(const struct lu_env *env,
277                                   const struct cl_io_slice *ios)
278 {
279         struct vvp_io *vio   = cl2vvp_io(env, ios);
280         struct inode  *inode = vvp_object_inode(ios->cis_obj);
281
282         LASSERT(inode == file_inode(vio->vui_fd->fd_file));
283
284         return 0;
285 }
286
287 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
288 {
289         struct cl_io     *io  = ios->cis_io;
290         struct cl_object *obj = io->ci_obj;
291         struct vvp_io    *vio = cl2vvp_io(env, ios);
292         struct inode     *inode = vvp_object_inode(obj);
293         __u32             gen = 0;
294         int rc;
295
296         ENTRY;
297
298         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
299
300         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d need write layout %d, restore needed %d, invalidate_lock %d\n",
301                PFID(lu_object_fid(&obj->co_lu)),
302                io->ci_ignore_layout, io->ci_verify_layout,
303                vio->vui_layout_gen, io->ci_need_write_intent,
304                io->ci_restore_needed, io->ci_invalidate_page_cache);
305
306 #ifdef HAVE_INVALIDATE_LOCK
307         if (io->ci_invalidate_page_cache) {
308                 filemap_invalidate_unlock(inode->i_mapping);
309                 io->ci_invalidate_page_cache = 0;
310         }
311 #endif /* HAVE_INVALIDATE_LOCK */
312
313         if (io->ci_restore_needed) {
314                 /* file was detected release, we need to restore it
315                  * before finishing the io
316                  */
317                 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
318                 /* if restore registration failed, no restart,
319                  * we will return -ENODATA
320                  *
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 case */
353                         /* restore was done, clear restoring state */
354                         clear_bit(LLIF_FILE_RESTORING,
355                                   &ll_i2info(vvp_object_inode(obj))->lli_flags);
356                 }
357                 GOTO(out, 0);
358         }
359
360         /* dynamic layout change needed, send layout intent RPC. */
361         if (io->ci_need_write_intent || io->ci_need_pccro_clear) {
362                 enum layout_intent_opc opc = LAYOUT_INTENT_WRITE;
363
364                 io->ci_need_write_intent = 0;
365
366                 LASSERT(io->ci_type == CIT_WRITE || cl_io_is_fallocate(io) ||
367                         cl_io_is_trunc(io) || cl_io_is_mkwrite(io));
368
369                 CDEBUG(D_VFSTRACE, DFID" write layout, type %u "DEXT"\n",
370                        PFID(lu_object_fid(&obj->co_lu)), io->ci_type,
371                        PEXT(&io->ci_write_intent));
372
373                 if (cl_io_is_trunc(io))
374                         opc = LAYOUT_INTENT_TRUNC;
375
376                 if (io->ci_need_pccro_clear) {
377                         io->ci_need_pccro_clear = 0;
378                         opc = LAYOUT_INTENT_PCCRO_CLEAR;
379                 }
380
381                 rc = ll_layout_write_intent(inode, opc, &io->ci_write_intent);
382                 io->ci_result = rc;
383                 if (!rc)
384                         io->ci_need_restart = 1;
385                 GOTO(out, rc);
386         }
387
388         if (!io->ci_need_restart &&
389             !io->ci_ignore_layout && io->ci_verify_layout) {
390                 /* check layout version */
391                 ll_layout_refresh(inode, &gen);
392                 io->ci_need_restart = vio->vui_layout_gen != gen;
393                 if (io->ci_need_restart) {
394                         CDEBUG(D_VFSTRACE,
395                                DFID" layout changed from %d to %d.\n",
396                                PFID(lu_object_fid(&obj->co_lu)),
397                                vio->vui_layout_gen, gen);
398                 }
399                 GOTO(out, 0);
400         }
401 out:
402         EXIT;
403 }
404
405 static void vvp_io_fault_fini(const struct lu_env *env,
406                               const struct cl_io_slice *ios)
407 {
408         struct cl_io   *io   = ios->cis_io;
409         struct cl_page *page = io->u.ci_fault.ft_page;
410
411         CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
412
413         if (page != NULL) {
414                 lu_ref_del(&page->cp_reference, "fault", io);
415                 cl_page_put(env, page);
416                 io->u.ci_fault.ft_page = NULL;
417         }
418         vvp_io_fini(env, ios);
419 }
420
421 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
422 {
423         /* we only want to hold PW locks if the mmap() can generate
424          * writes back to the file and that only happens in shared
425          * writable vmas
426          */
427         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
428                 return CLM_WRITE;
429         return CLM_READ;
430 }
431
432 static int vvp_mmap_locks(const struct lu_env *env,
433                           struct vvp_io *vio, struct cl_io *io)
434 {
435         struct vvp_thread_info *vti = vvp_env_info(env);
436         struct mm_struct *mm = current->mm;
437         struct vm_area_struct *vma;
438         struct cl_lock_descr *descr = &vti->vti_descr;
439         union ldlm_policy_data policy;
440         struct iovec iov;
441         struct iov_iter i;
442         unsigned long addr;
443         ssize_t bytes;
444         int result = 0;
445
446         ENTRY;
447
448         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
449
450         /* nfs or loop back device write */
451         if (vio->vui_iter == NULL)
452                 RETURN(0);
453
454         /* No MM (e.g. NFS)? No vmas too. */
455         if (mm == NULL)
456                 RETURN(0);
457
458         if (!iter_is_iovec(vio->vui_iter) && !iov_iter_is_kvec(vio->vui_iter))
459                 RETURN(0);
460
461         for (i = *vio->vui_iter;
462              iov_iter_count(&i);
463              iov_iter_advance(&i, iov.iov_len)) {
464                 iov = iov_iter_iovec(&i);
465                 addr = (unsigned long)iov.iov_base;
466                 bytes = iov.iov_len;
467
468                 if (bytes == 0)
469                         continue;
470
471                 bytes += addr & ~PAGE_MASK;
472                 addr &= PAGE_MASK;
473
474                 mmap_read_lock(mm);
475                 while ((vma = our_vma(mm, addr, bytes)) != NULL) {
476                         struct dentry *de = file_dentry(vma->vm_file);
477                         struct inode *inode = de->d_inode;
478                         int flags = CEF_MUST;
479
480                         if (ll_file_nolock(vma->vm_file)) {
481                                 /* For no lock case is not allowed for mmap */
482                                 result = -EINVAL;
483                                 break;
484                         }
485
486                         /*
487                          * XXX: Required lock mode can be weakened: CIT_WRITE
488                          * io only ever reads user level buffer, and CIT_READ
489                          * only writes on it.
490                          */
491                         policy_from_vma(&policy, vma, addr, bytes);
492                         descr->cld_mode = vvp_mode_from_vma(vma);
493                         descr->cld_obj = ll_i2info(inode)->lli_clob;
494                         descr->cld_start = policy.l_extent.start >> PAGE_SHIFT;
495                         descr->cld_end = policy.l_extent.end >> PAGE_SHIFT;
496                         descr->cld_enq_flags = flags;
497                         result = cl_io_lock_alloc_add(env, io, descr);
498
499                         CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
500                                descr->cld_mode, descr->cld_start,
501                                descr->cld_end);
502
503                         if (result < 0)
504                                 break;
505
506                         if (vma->vm_end - addr >= bytes)
507                                 break;
508
509                         bytes -= vma->vm_end - addr;
510                         addr = vma->vm_end;
511                 }
512                 mmap_read_unlock(mm);
513                 if (result < 0)
514                         break;
515         }
516         RETURN(result);
517 }
518
519 static void vvp_io_advance(const struct lu_env *env,
520                            const struct cl_io_slice *ios, size_t bytes)
521 {
522         struct cl_object *obj = ios->cis_io->ci_obj;
523         struct vvp_io *vio = cl2vvp_io(env, ios);
524
525         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
526
527         /*
528          * Since 3.16(26978b8b4) vfs revert iov iter to
529          * original position even io succeed, so instead
530          * of relying on VFS, we move iov iter by ourselves.
531          */
532         iov_iter_advance(vio->vui_iter, bytes);
533         CDEBUG(D_VFSTRACE, "advancing %ld bytes\n", bytes);
534         vio->vui_tot_bytes -= bytes;
535         iov_iter_reexpand(vio->vui_iter, vio->vui_tot_bytes);
536 }
537
538 static void vvp_io_update_iov(const struct lu_env *env,
539                               struct vvp_io *vio, struct cl_io *io)
540 {
541         size_t size = io->u.ci_rw.crw_bytes;
542
543         if (!vio->vui_iter)
544                 return;
545
546         iov_iter_truncate(vio->vui_iter, size);
547 }
548
549 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
550                           enum cl_lock_mode mode, loff_t start, loff_t end)
551 {
552         struct vvp_io *vio = vvp_env_io(env);
553         int result;
554         int ast_flags = 0;
555
556         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
557         ENTRY;
558
559         vvp_io_update_iov(env, vio, io);
560
561         if (io->u.ci_rw.crw_nonblock)
562                 ast_flags |= CEF_NONBLOCK;
563         if (io->ci_lock_no_expand)
564                 ast_flags |= CEF_LOCK_NO_EXPAND;
565         if (vio->vui_fd) {
566                 int flags;
567
568                 /* Group lock held means no lockless any more */
569                 if (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)
570                         io->ci_dio_lock = 1;
571
572                 flags = iocb_ki_flags_get(vio->vui_iocb->ki_filp,
573                                           vio->vui_iocb);
574                 if (ll_file_nolock(vio->vui_fd->fd_file) ||
575                     (iocb_ki_flags_check(flags, DIRECT) &&
576                      !io->ci_dio_lock))
577                         ast_flags |= CEF_NEVER;
578         }
579
580         result = vvp_mmap_locks(env, vio, io);
581         if (result == 0)
582                 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
583
584         RETURN(result);
585 }
586
587 static int vvp_io_read_lock(const struct lu_env *env,
588                             const struct cl_io_slice *ios)
589 {
590         struct cl_io *io = ios->cis_io;
591         struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
592         int result;
593
594         ENTRY;
595         result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
596                                 rd->crw_pos + rd->crw_bytes - 1);
597         RETURN(result);
598 }
599
600 static int vvp_io_fault_lock(const struct lu_env *env,
601                              const struct cl_io_slice *ios)
602 {
603         struct cl_io *io   = ios->cis_io;
604         struct vvp_io *vio = cl2vvp_io(env, ios);
605         /* XXX LDLM_FL_CBPENDING */
606         return vvp_io_one_lock_index(env,
607                                      io, 0,
608                                      vvp_mode_from_vma(vio->u.fault.ft_vma),
609                                      io->u.ci_fault.ft_index,
610                                      io->u.ci_fault.ft_index);
611 }
612
613 static int vvp_io_write_lock(const struct lu_env *env,
614                              const struct cl_io_slice *ios)
615 {
616         struct cl_io *io = ios->cis_io;
617         loff_t start;
618         loff_t end;
619
620         if (io->u.ci_wr.wr_append) {
621                 start = 0;
622                 end   = OBD_OBJECT_EOF;
623         } else {
624                 start = io->u.ci_wr.wr.crw_pos;
625                 end   = start + io->u.ci_wr.wr.crw_bytes - 1;
626         }
627
628         RETURN(vvp_io_rw_lock(env, io, CLM_WRITE, start, end));
629 }
630
631 static int vvp_io_setattr_iter_init(const struct lu_env *env,
632                                     const struct cl_io_slice *ios)
633
634 {
635         return 0;
636 }
637
638 /*
639  * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
640  *
641  * Handles "lockless io" mode when extent locking is done by server.
642  */
643 static int vvp_io_setattr_lock(const struct lu_env *env,
644                                const struct cl_io_slice *ios)
645 {
646         struct cl_io  *io  = ios->cis_io;
647         __u64 lock_start = 0;
648         __u64 lock_end = OBD_OBJECT_EOF;
649         __u32 enqflags = 0;
650
651         if (cl_io_is_trunc(io)) {
652                 struct inode *inode = vvp_object_inode(io->ci_obj);
653
654                 /* set enqueue flags to CEF_MUST in case of encrypted file,
655                  * to prevent lockless truncate
656                  */
657                 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
658                         enqflags = CEF_MUST;
659                 else if (io->u.ci_setattr.sa_attr.lvb_size == 0)
660                         enqflags = CEF_DISCARD_DATA;
661         } else if (cl_io_is_fallocate(io)) {
662                 lock_start = io->u.ci_setattr.sa_falloc_offset;
663                 lock_end = io->u.ci_setattr.sa_falloc_end - 1;
664         } else {
665                 unsigned int valid = io->u.ci_setattr.sa_avalid;
666
667                 if (!(valid & TIMES_SET_FLAGS))
668                         return 0;
669
670                 if ((!(valid & ATTR_MTIME) ||
671                      io->u.ci_setattr.sa_attr.lvb_mtime >=
672                      io->u.ci_setattr.sa_attr.lvb_ctime) &&
673                     (!(valid & ATTR_ATIME) ||
674                      io->u.ci_setattr.sa_attr.lvb_atime >=
675                      io->u.ci_setattr.sa_attr.lvb_ctime))
676                         return 0;
677         }
678
679         return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
680                                lock_start, lock_end);
681 }
682
683 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
684 {
685         int result;
686
687         /* Only ll_inode_size_lock is taken at this level.  */
688         ll_inode_size_lock(inode);
689         result = inode_newsize_ok(inode, size);
690         if (result < 0) {
691                 ll_inode_size_unlock(inode);
692                 return result;
693         }
694         i_size_write(inode, size);
695
696         ll_truncate_pagecache(inode, size);
697         ll_inode_size_unlock(inode);
698         return result;
699 }
700
701 static int vvp_io_setattr_time(const struct lu_env *env,
702                                const struct cl_io_slice *ios)
703 {
704         struct cl_io *io    = ios->cis_io;
705         struct cl_object *obj   = io->ci_obj;
706         struct cl_attr *attr  = vvp_env_thread_attr(env);
707         int result;
708         unsigned int valid = CAT_CTIME;
709
710         cl_object_attr_lock(obj);
711         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
712         if (io->u.ci_setattr.sa_avalid & ATTR_ATIME_SET) {
713                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
714                 valid |= CAT_ATIME;
715         }
716         if (io->u.ci_setattr.sa_avalid & ATTR_MTIME_SET) {
717                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
718                 valid |= CAT_MTIME;
719         }
720         result = cl_object_attr_update(env, obj, attr, valid);
721         cl_object_attr_unlock(obj);
722
723         return result;
724 }
725
726 static int vvp_io_setattr_start(const struct lu_env *env,
727                                 const struct cl_io_slice *ios)
728 {
729         struct cl_io *io = ios->cis_io;
730         struct inode *inode = vvp_object_inode(io->ci_obj);
731         struct ll_inode_info *lli = ll_i2info(inode);
732         int mode = io->u.ci_setattr.sa_falloc_mode;
733
734         if (cl_io_is_trunc(io)) {
735                 trunc_sem_down_write(&lli->lli_trunc_sem);
736                 mutex_lock(&lli->lli_setattr_mutex);
737                 inode_dio_wait(inode);
738         } else if (cl_io_is_fallocate(io)) {
739                 loff_t size;
740
741                 trunc_sem_down_write(&lli->lli_trunc_sem);
742                 mutex_lock(&lli->lli_setattr_mutex);
743                 inode_dio_wait(inode);
744
745                 ll_merge_attr(env, inode);
746                 size = i_size_read(inode);
747                 if (io->u.ci_setattr.sa_falloc_end > size &&
748                     !(mode & FALLOC_FL_KEEP_SIZE)) {
749                         size = io->u.ci_setattr.sa_falloc_end;
750                         io->u.ci_setattr.sa_avalid |= ATTR_SIZE;
751                 }
752                 io->u.ci_setattr.sa_attr.lvb_size = size;
753         } else {
754                 mutex_lock(&lli->lli_setattr_mutex);
755         }
756
757         if (io->u.ci_setattr.sa_avalid & TIMES_SET_FLAGS)
758                 return vvp_io_setattr_time(env, ios);
759
760         return 0;
761 }
762
763 static void vvp_io_setattr_end(const struct lu_env *env,
764                                const struct cl_io_slice *ios)
765 {
766         struct cl_io *io = ios->cis_io;
767         struct inode *inode = vvp_object_inode(io->ci_obj);
768         struct ll_inode_info *lli = ll_i2info(inode);
769         loff_t size = io->u.ci_setattr.sa_attr.lvb_size;
770
771         if (cl_io_is_trunc(io)) {
772                 /* Truncate in memory pages - they must be clean pages
773                  * because osc has already notified to destroy osc_extents.
774                  */
775                 vvp_do_vmtruncate(inode, size);
776                 mutex_unlock(&lli->lli_setattr_mutex);
777                 trunc_sem_up_write(&lli->lli_trunc_sem);
778
779                 /* Update size and blocks for LSOM */
780                 if (!io->ci_ignore_layout)
781                         ll_merge_attr(env, inode);
782         } else if (cl_io_is_fallocate(io)) {
783                 int mode = io->u.ci_setattr.sa_falloc_mode;
784
785                 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
786                     size > i_size_read(inode)) {
787                         ll_inode_size_lock(inode);
788                         i_size_write(inode, size);
789                         ll_inode_size_unlock(inode);
790                 }
791                 inode_set_ctime_current(inode);
792                 mutex_unlock(&lli->lli_setattr_mutex);
793                 trunc_sem_up_write(&lli->lli_trunc_sem);
794         } else {
795                 mutex_unlock(&lli->lli_setattr_mutex);
796         }
797 }
798
799 static void vvp_io_setattr_fini(const struct lu_env *env,
800                                 const struct cl_io_slice *ios)
801 {
802         bool restore_needed = ios->cis_io->ci_restore_needed;
803         struct inode *inode = vvp_object_inode(ios->cis_obj);
804
805         vvp_io_fini(env, ios);
806
807         if (restore_needed && !ios->cis_io->ci_restore_needed) {
808                 /* restore finished, set data modified flag for HSM */
809                 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
810         }
811 }
812
813 static int vvp_io_read_start(const struct lu_env *env,
814                              const struct cl_io_slice *ios)
815 {
816         struct vvp_io *vio = cl2vvp_io(env, ios);
817         struct cl_io *io = ios->cis_io;
818         struct cl_object *obj = io->ci_obj;
819         struct inode *inode = vvp_object_inode(obj);
820         struct ll_inode_info *lli = ll_i2info(inode);
821         struct file *file = vio->vui_fd->fd_file;
822         loff_t pos = io->u.ci_rd.rd.crw_pos;
823         size_t crw_bytes = io->u.ci_rd.rd.crw_bytes;
824         size_t tot_bytes = vio->vui_tot_bytes;
825         struct ll_cl_context *lcc;
826         unsigned int seq;
827         int exceed = 0;
828         int result;
829         int total_bytes_read = 0;
830         struct iov_iter iter;
831         pgoff_t page_offset;
832         int flags;
833
834         ENTRY;
835
836         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
837
838         CDEBUG(D_VFSTRACE, "%s: read [%llu, %llu)\n",
839                 file_dentry(file)->d_name.name,
840                 pos, pos + crw_bytes);
841
842         trunc_sem_down_read(&lli->lli_trunc_sem);
843
844         if (io->ci_async_readahead) {
845                 file_accessed(file);
846                 RETURN(0);
847         }
848
849         if (!can_populate_pages(env, io, inode))
850                 RETURN(0);
851
852         flags = iocb_ki_flags_get(file, vio->vui_iocb);
853         if (!iocb_ki_flags_check(flags, DIRECT)) {
854                 result = cl_io_lru_reserve(env, io, pos, crw_bytes);
855                 if (result)
856                         RETURN(result);
857         }
858
859         /* Unless this is reading a sparse file, otherwise the lock has already
860          * been acquired so vvp_prep_size() is an empty op.
861          */
862         result = vvp_prep_size(env, obj, io, pos, crw_bytes, &exceed);
863         if (result != 0)
864                 RETURN(result);
865         else if (exceed != 0)
866                 GOTO(out, result);
867
868         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
869                          "Read ino %lu, %zu bytes, offset %lld, size %llu\n",
870                          inode->i_ino, crw_bytes, pos, i_size_read(inode));
871
872         /* initialize read-ahead window once per syscall */
873         if (!vio->vui_ra_valid) {
874                 vio->vui_ra_valid = true;
875                 vio->vui_ra_start_idx = pos >> PAGE_SHIFT;
876                 vio->vui_ra_pages = 0;
877                 page_offset = pos & ~PAGE_MASK;
878                 if (page_offset) {
879                         vio->vui_ra_pages++;
880                         if (tot_bytes > PAGE_SIZE - page_offset)
881                                 tot_bytes -= (PAGE_SIZE - page_offset);
882                         else
883                                 tot_bytes = 0;
884                 }
885                 vio->vui_ra_pages += (tot_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
886
887                 CDEBUG(D_READA, "tot %zu, ra_start %lu, ra_count %lu\n",
888                        vio->vui_tot_bytes, vio->vui_ra_start_idx,
889                        vio->vui_ra_pages);
890         }
891
892         /* BUG: 5972 */
893         file_accessed(file);
894         LASSERT(vio->vui_iocb->ki_pos == pos);
895         iter = *vio->vui_iter;
896
897         lcc = ll_cl_find(inode);
898         lcc->lcc_end_index = DIV_ROUND_UP(pos + iter.count, PAGE_SIZE);
899         CDEBUG(D_VFSTRACE, "count:%ld iocb pos:%lld\n", iter.count, pos);
900
901         /* this seqlock lets us notice if a page has been deleted on this inode
902          * during the fault process, allowing us to catch an erroneous short
903          * read or EIO. See LU-16160
904          */
905         do {
906                 seq = read_seqbegin(&ll_i2info(inode)->lli_page_inv_lock);
907                 result = generic_file_read_iter(vio->vui_iocb, &iter);
908                 if (result >= 0) {
909                         io->ci_bytes += result;
910                         total_bytes_read += result;
911                 }
912         /* got a short read or -EIO and we raced with page invalidation retry */
913         } while (read_seqretry(&ll_i2info(inode)->lli_page_inv_lock, seq) &&
914                  ((result >= 0 && iov_iter_count(&iter) > 0)
915                   || result == -EIO));
916
917 out:
918         if (result >= 0) {
919                 if (total_bytes_read < crw_bytes)
920                         io->ci_continue = 0;
921                 result = 0;
922         } else if (result == -EIOCBQUEUED) {
923                 io->ci_bytes += vio->u.readwrite.vui_read;
924                 vio->vui_iocb->ki_pos = pos + vio->u.readwrite.vui_read;
925         }
926
927         return result;
928 }
929
930 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
931                               struct cl_page_list *plist, int from, int to)
932 {
933         struct cl_2queue *queue = &io->ci_queue;
934         struct cl_page *page;
935         unsigned int bytes = 0;
936         int rc = 0;
937
938         ENTRY;
939
940         if (plist->pl_nr == 0)
941                 RETURN(0);
942
943         if (from > 0 || to != PAGE_SIZE) {
944                 page = cl_page_list_first(plist);
945                 if (plist->pl_nr == 1) {
946                         cl_page_clip(env, page, from, to);
947                 } else {
948                         if (from > 0)
949                                 cl_page_clip(env, page, from, PAGE_SIZE);
950                         if (to != PAGE_SIZE) {
951                                 page = cl_page_list_last(plist);
952                                 cl_page_clip(env, page, 0, to);
953                         }
954                 }
955         }
956
957         cl_2queue_init(queue);
958         cl_page_list_splice(plist, &queue->c2_qin);
959         rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
960
961         /* plist is not sorted any more */
962         cl_page_list_splice(&queue->c2_qin, plist);
963         cl_page_list_splice(&queue->c2_qout, plist);
964         cl_2queue_fini(env, queue);
965
966         if (rc == 0) {
967                 /* calculate bytes */
968                 bytes = plist->pl_nr << PAGE_SHIFT;
969                 bytes -= from + PAGE_SIZE - to;
970
971                 while (plist->pl_nr > 0) {
972                         page = cl_page_list_first(plist);
973                         cl_page_list_del(env, plist, page, true);
974
975                         cl_page_clip(env, page, 0, PAGE_SIZE);
976
977                         SetPageUptodate(cl_page_vmpage(page));
978                         cl_page_disown(env, io, page);
979
980                         /* held in ll_cl_init() */
981                         lu_ref_del(&page->cp_reference, "cl_io", io);
982                         cl_page_put(env, page);
983                 }
984         }
985
986         RETURN(bytes > 0 ? bytes : rc);
987 }
988
989 /*
990  * From kernel v4.19-rc5-248-g9b89a0355144 use XArrary
991  * Prior kernels use radix_tree for tags
992  */
993 static inline void ll_page_tag_dirty(struct page *page,
994                                      struct address_space *mapping)
995 {
996 #ifndef HAVE_RADIX_TREE_TAG_SET
997         __xa_set_mark(&mapping->i_pages, page_index(page), PAGECACHE_TAG_DIRTY);
998 #else
999         radix_tree_tag_set(&mapping->page_tree, page_index(page),
1000                            PAGECACHE_TAG_DIRTY);
1001 #endif
1002 }
1003
1004 /*
1005  * Kernels 4.2 - 4.5 pass memcg argument to account_page_dirtied()
1006  * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
1007  */
1008 static inline void ll_account_page_dirtied(struct page *page,
1009                                            struct address_space *mapping)
1010 {
1011 #ifdef HAVE_ACCOUNT_PAGE_DIRTIED_3ARGS
1012         struct mem_cgroup *memcg = mem_cgroup_begin_page_stat(page);
1013
1014         account_page_dirtied(page, mapping, memcg);
1015         mem_cgroup_end_page_stat(memcg);
1016 #elif defined(HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT)
1017         account_page_dirtied(page, mapping);
1018 #else
1019         vvp_account_page_dirtied(page, mapping);
1020 #endif
1021         ll_page_tag_dirty(page, mapping);
1022 }
1023
1024 /* Taken from kernel set_page_dirty, __set_page_dirty_nobuffers
1025  * Last change to this area: b93b016313b3ba8003c3b8bb71f569af91f19fc7
1026  *
1027  * Current with Linus tip of tree (7/13/2019):
1028  * v5.2-rc4-224-ge01e060fe0
1029  *
1030  * Backwards compat for 3.x, 5.x kernels relating to memcg handling
1031  * & rename of radix tree to xarray.
1032  */
1033 static void vvp_set_batch_dirty(struct folio_batch *fbatch)
1034 {
1035         struct page *page = fbatch_at_pg(fbatch, 0, 0);
1036         int count = folio_batch_count(fbatch);
1037         int i;
1038 #if !defined(HAVE_FOLIO_BATCH) || defined(HAVE_KALLSYMS_LOOKUP_NAME)
1039         int pg, npgs;
1040 #endif
1041 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
1042         struct address_space *mapping = page->mapping;
1043         unsigned long flags;
1044         unsigned long skip_pages = 0;
1045         int pgno;
1046         int dirtied = 0;
1047 #endif
1048
1049         ENTRY;
1050
1051         BUILD_BUG_ON(PAGEVEC_SIZE > BITS_PER_LONG);
1052         LASSERTF(page->mapping,
1053                  "mapping must be set. page %px, page->private (cl_page) %px\n",
1054                  page, (void *) page->private);
1055
1056         /*
1057          * kernels without HAVE_KALLSYMS_LOOKUP_NAME also don't have
1058          * account_dirty_page exported, and if we can't access that symbol,
1059          * we can't do page dirtying in batch (taking the xarray lock only once)
1060          * so we just fall back to a looped call to __set_page_dirty_nobuffers
1061          */
1062 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
1063         if (!vvp_account_page_dirtied) {
1064                 for (i = 0; i < count; i++) {
1065 #ifdef HAVE_FOLIO_BATCH
1066                         filemap_dirty_folio(page->mapping, fbatch->folios[i]);
1067 #else
1068                         npgs = fbatch_at_npgs(fbatch, i);
1069                         for (pg = 0; pg < npgs; pg++) {
1070                                 page = fbatch_at_pg(fbatch, i, pg);
1071                                 __set_page_dirty_nobuffers(page);
1072                         }
1073 #endif
1074                 }
1075                 EXIT;
1076         }
1077 #endif
1078
1079         /* account_page_dirtied is available directly or via kallsyms */
1080 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
1081         for (pgno = i = 0; i < count; i++) {
1082                 npgs = fbatch_at_npgs(fbatch, i);
1083                 for (pg = 0; pg < npgs; pg++) {
1084                         page = fbatch_at_pg(fbatch, i, pg);
1085
1086                         ClearPageReclaim(page);
1087
1088                         vvp_lock_page_memcg(page);
1089                         if (TestSetPageDirty(page)) {
1090                                 /* page is already dirty .. no extra work needed
1091                                  * set a flag for the i'th page to be skipped
1092                                  */
1093                                 vvp_unlock_page_memcg(page);
1094                                 skip_pages |= (1ul << pgno++);
1095                                 LASSERTF(pgno <= BITS_PER_LONG,
1096                                          "Limit exceeded pgno: %d/%d\n", pgno,
1097                                          BITS_PER_LONG);
1098                         }
1099                 }
1100         }
1101
1102         ll_xa_lock_irqsave(&mapping->i_pages, flags);
1103
1104         /* Notes on differences with __set_page_dirty_nobuffers:
1105          * 1. We don't need to call page_mapping because we know this is a page
1106          * cache page.
1107          * 2. We have the pages locked, so there is no need for the careful
1108          * mapping/mapping2 dance.
1109          * 3. No mapping is impossible. (Race w/truncate mentioned in
1110          * dirty_nobuffers should be impossible because we hold the page lock.)
1111          * 4. All mappings are the same because i/o is only to one file.
1112          */
1113         for (pgno = i = 0; i < count; i++) {
1114                 npgs = fbatch_at_npgs(fbatch, f);
1115                 for (pg = 0; pg < npgs; pg++) {
1116                         page = fbatch_at_pg(fbatch, i, pg);
1117                         /* if the i'th page was unlocked above, skip it here */
1118                         if ((skip_pages >> pgno++) & 1)
1119                                 continue;
1120
1121                         LASSERTF(page->mapping == mapping,
1122                                  "all pages must have the same mapping.  page %px, mapping %px, first mapping %px\n",
1123                                  page, page->mapping, mapping);
1124                         WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
1125                         ll_account_page_dirtied(page, mapping);
1126                         dirtied++;
1127                         vvp_unlock_page_memcg(page);
1128                 }
1129         }
1130         ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
1131
1132         CDEBUG(D_VFSTRACE, "mapping %p, count %d, dirtied %d\n", mapping,
1133                count, dirtied);
1134
1135         if (mapping->host && dirtied) {
1136                 /* !PageAnon && !swapper_space */
1137                 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1138         }
1139 #endif
1140         EXIT;
1141 }
1142
1143 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
1144                                   struct folio_batch *fbatch)
1145 {
1146         struct page *vmpage;
1147         struct cl_page *page;
1148         int pg, npgs;
1149         int count = 0;
1150         int i = 0;
1151
1152         ENTRY;
1153
1154         count = folio_batch_count(fbatch);
1155         LASSERT(count > 0);
1156
1157         for (i = 0; i < count; i++) {
1158                 npgs = fbatch_at_npgs(fbatch, i);
1159                 for (pg = 0; pg < npgs; pg++)
1160                         SetPageUptodate(fbatch_at_pg(fbatch, i, pg));
1161         }
1162
1163         vvp_set_batch_dirty(fbatch);
1164
1165         for (i = 0; i < count; i++) {
1166                 npgs = fbatch_at_npgs(fbatch, i);
1167                 for (pg = 0; pg < npgs; pg++) {
1168                         vmpage = fbatch_at_pg(fbatch, i, pg);
1169                         page = (struct cl_page *) vmpage->private;
1170                         cl_page_disown(env, io, page);
1171                         lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
1172                         cl_page_put(env, page);
1173                 }
1174         }
1175
1176         EXIT;
1177 }
1178
1179 /* make sure the page list is contiguous */
1180 static bool page_list_sanity_check(struct cl_object *obj,
1181                                    struct cl_page_list *plist)
1182 {
1183         struct cl_page *page;
1184         pgoff_t index = CL_PAGE_EOF;
1185
1186         cl_page_list_for_each(page, plist) {
1187                 if (index == CL_PAGE_EOF) {
1188                         index = cl_page_index(page);
1189                         continue;
1190                 }
1191
1192                 ++index;
1193                 if (index == cl_page_index(page))
1194                         continue;
1195
1196                 return false;
1197         }
1198         return true;
1199 }
1200
1201 /* Return how many bytes have queued or written */
1202 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
1203 {
1204         struct cl_object *obj = io->ci_obj;
1205         struct inode *inode = vvp_object_inode(obj);
1206         struct vvp_io *vio = vvp_env_io(env);
1207         struct cl_page_list *queue = &vio->u.readwrite.vui_queue;
1208         struct cl_page *page;
1209         int rc = 0;
1210         int bytes = 0;
1211         unsigned int npages = vio->u.readwrite.vui_queue.pl_nr;
1212
1213         ENTRY;
1214
1215         if (npages == 0)
1216                 RETURN(0);
1217
1218         CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
1219                 npages, vio->u.readwrite.vui_from, vio->u.readwrite.vui_to);
1220
1221         LASSERT(page_list_sanity_check(obj, queue));
1222
1223         /* submit IO with async write */
1224         rc = cl_io_commit_async(env, io, queue,
1225                                 vio->u.readwrite.vui_from,
1226                                 vio->u.readwrite.vui_to,
1227                                 write_commit_callback);
1228         npages -= queue->pl_nr; /* already committed pages */
1229         if (npages > 0) {
1230                 /* calculate how many bytes were written */
1231                 bytes = npages << PAGE_SHIFT;
1232
1233                 /* first page */
1234                 bytes -= vio->u.readwrite.vui_from;
1235                 if (queue->pl_nr == 0) /* last page */
1236                         bytes -= PAGE_SIZE - vio->u.readwrite.vui_to;
1237                 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
1238
1239                 vio->u.readwrite.vui_written += bytes;
1240
1241                 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
1242                         npages, bytes, vio->u.readwrite.vui_written);
1243
1244                 /* the first page must have been written. */
1245                 vio->u.readwrite.vui_from = 0;
1246         }
1247         LASSERT(page_list_sanity_check(obj, queue));
1248         LASSERT(ergo(rc == 0, queue->pl_nr == 0));
1249
1250         /* out of quota, try sync write */
1251         if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
1252                 struct ll_inode_info *lli = ll_i2info(inode);
1253
1254                 rc = vvp_io_commit_sync(env, io, queue,
1255                                         vio->u.readwrite.vui_from,
1256                                         vio->u.readwrite.vui_to);
1257                 if (rc > 0) {
1258                         vio->u.readwrite.vui_written += rc;
1259                         rc = 0;
1260                 }
1261                 if (lli->lli_clob != NULL)
1262                         lov_read_and_clear_async_rc(lli->lli_clob);
1263                 lli->lli_async_rc = 0;
1264         }
1265
1266         /* update inode size */
1267         ll_merge_attr(env, inode);
1268
1269         /* Now the pages in queue were failed to commit, discard them
1270          * unless they were dirtied before.
1271          */
1272         while (queue->pl_nr > 0) {
1273                 page = cl_page_list_first(queue);
1274                 cl_page_list_del(env, queue, page, true);
1275
1276                 if (!PageDirty(cl_page_vmpage(page)))
1277                         cl_page_discard(env, io, page);
1278
1279                 cl_page_disown(env, io, page);
1280
1281                 /* held in ll_cl_init() */
1282                 lu_ref_del(&page->cp_reference, "cl_io", io);
1283                 cl_page_put(env, page);
1284         }
1285         cl_page_list_fini(env, queue);
1286
1287         RETURN(rc);
1288 }
1289
1290 static int vvp_io_write_start(const struct lu_env *env,
1291                               const struct cl_io_slice *ios)
1292 {
1293         struct vvp_io *vio = cl2vvp_io(env, ios);
1294         struct cl_io *io = ios->cis_io;
1295         struct cl_object *obj = io->ci_obj;
1296         struct inode *inode = vvp_object_inode(obj);
1297         struct ll_inode_info *lli = ll_i2info(inode);
1298         struct file *file = vio->vui_fd->fd_file;
1299         ssize_t result = 0;
1300         loff_t pos = io->u.ci_wr.wr.crw_pos;
1301         size_t crw_bytes = io->u.ci_wr.wr.crw_bytes;
1302         bool lock_inode = !IS_NOSEC(inode);
1303         size_t ci_bytes = io->ci_bytes;
1304         struct iov_iter iter;
1305         size_t written = 0;
1306         int flags;
1307
1308         ENTRY;
1309
1310         trunc_sem_down_read(&lli->lli_trunc_sem);
1311
1312         if (!can_populate_pages(env, io, inode))
1313                 RETURN(0);
1314
1315         if (cl_io_is_append(io)) {
1316                 /*
1317                  * PARALLEL IO This has to be changed for parallel IO doing
1318                  * out-of-order writes.
1319                  */
1320                 ll_merge_attr(env, inode);
1321                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
1322                 vio->vui_iocb->ki_pos = pos;
1323         } else {
1324                 LASSERTF(vio->vui_iocb->ki_pos == pos,
1325                          "ki_pos %lld [%lld, %lld)\n",
1326                          vio->vui_iocb->ki_pos,
1327                          pos, pos + crw_bytes);
1328         }
1329
1330         CDEBUG(D_VFSTRACE, "%s: write [%llu, %llu)\n",
1331                file_dentry(file)->d_name.name, pos, pos + crw_bytes);
1332
1333         /* The maximum Lustre file size is variable, based on the OST maximum
1334          * object size and number of stripes.  This needs another check in
1335          * addition to the VFS checks earlier.
1336          */
1337         if (pos + crw_bytes > ll_file_maxbytes(inode)) {
1338                 CDEBUG(D_INODE,
1339                        "%s: file %s ("DFID") offset %llu > maxbytes %llu\n",
1340                        ll_i2sbi(inode)->ll_fsname,
1341                        file_dentry(file)->d_name.name,
1342                        PFID(ll_inode2fid(inode)), pos + crw_bytes,
1343                        ll_file_maxbytes(inode));
1344                 RETURN(-EFBIG);
1345         }
1346
1347         /* Tests to verify we take the i_mutex correctly */
1348         if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_SEC) && !lock_inode)
1349                 RETURN(-EINVAL);
1350
1351         if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_NOSEC) && lock_inode)
1352                 RETURN(-EINVAL);
1353
1354         flags = iocb_ki_flags_get(file, vio->vui_iocb);
1355         if (!iocb_ki_flags_check(flags, DIRECT)) {
1356                 result = cl_io_lru_reserve(env, io, pos, crw_bytes);
1357                 if (result)
1358                         RETURN(result);
1359         }
1360
1361         if (vio->vui_iter == NULL) {
1362                 /* from a temp io in ll_cl_init(). */
1363                 result = 0;
1364         } else {
1365                 /*
1366                  * When using the locked AIO function (generic_file_aio_write())
1367                  * testing has shown the inode mutex to be a limiting factor
1368                  * with multi-threaded single shared file performance. To get
1369                  * around this, we now use the lockless version. To maintain
1370                  * consistency, proper locking to protect against writes,
1371                  * trucates, etc. is handled in the higher layers of lustre.
1372                  */
1373                 lock_inode = !IS_NOSEC(inode);
1374                 iter = *vio->vui_iter;
1375
1376                 if (unlikely(lock_inode))
1377                         ll_inode_lock(inode);
1378                 result = __generic_file_write_iter(vio->vui_iocb, &iter);
1379                 if (unlikely(lock_inode))
1380                         ll_inode_unlock(inode);
1381
1382                 written = result;
1383                 if (result > 0)
1384 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1385                         result = generic_write_sync(vio->vui_iocb, result);
1386 #else
1387                 {
1388                         ssize_t err;
1389
1390                         err = generic_write_sync(vio->vui_iocb->ki_filp, pos,
1391                                                  result);
1392                         if (err < 0 && result > 0)
1393                                 result = err;
1394                 }
1395 #endif
1396         }
1397
1398         if (result > 0) {
1399                 result = vvp_io_write_commit(env, io);
1400                 /* Simulate short commit */
1401                 if (CFS_FAULT_CHECK(OBD_FAIL_LLITE_SHORT_COMMIT)) {
1402                         vio->u.readwrite.vui_written >>= 1;
1403                         if (vio->u.readwrite.vui_written > 0)
1404                                 io->ci_need_restart = 1;
1405                 }
1406                 if (vio->u.readwrite.vui_written > 0) {
1407                         result = vio->u.readwrite.vui_written;
1408                         CDEBUG(D_VFSTRACE, "%s: write bytes %zd, result: %zd\n",
1409                                 file_dentry(file)->d_name.name,
1410                                 io->ci_bytes, result);
1411                         io->ci_bytes += result;
1412                 } else {
1413                         io->ci_continue = 0;
1414                 }
1415         }
1416         if (vio->vui_iocb->ki_pos != (pos + io->ci_bytes - ci_bytes)) {
1417                 CDEBUG(D_VFSTRACE,
1418                        "%s: write position mismatch: ki_pos %lld vs. pos %lld, written %zd, commit %ld: rc = %zd\n",
1419                        file_dentry(file)->d_name.name,
1420                        vio->vui_iocb->ki_pos, pos + io->ci_bytes - ci_bytes,
1421                        written, io->ci_bytes - ci_bytes, result);
1422                 /*
1423                  * Rewind ki_pos and vui_iter to where it has
1424                  * successfully committed.
1425                  */
1426                 vio->vui_iocb->ki_pos = pos + io->ci_bytes - ci_bytes;
1427         }
1428         if (result > 0 || result == -EIOCBQUEUED) {
1429                 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
1430
1431                 if (result != -EIOCBQUEUED && result < crw_bytes)
1432                         io->ci_continue = 0;
1433                 if (result > 0)
1434                         result = 0;
1435                 /* move forward */
1436                 if (result == -EIOCBQUEUED) {
1437                         io->ci_bytes += vio->u.readwrite.vui_written;
1438                         vio->vui_iocb->ki_pos = pos +
1439                                         vio->u.readwrite.vui_written;
1440                 }
1441         }
1442
1443         RETURN(result);
1444 }
1445
1446 static void vvp_io_rw_end(const struct lu_env *env,
1447                           const struct cl_io_slice *ios)
1448 {
1449         struct inode            *inode = vvp_object_inode(ios->cis_obj);
1450         struct ll_inode_info    *lli = ll_i2info(inode);
1451
1452         trunc_sem_up_read(&lli->lli_trunc_sem);
1453 }
1454
1455 static void vvp_io_write_end(const struct lu_env *env,
1456                              const struct cl_io_slice *ios)
1457 {
1458         struct inode *inode = vvp_object_inode(ios->cis_obj);
1459         struct cl_io *io = ios->cis_io;
1460
1461         vvp_io_rw_end(env, ios);
1462
1463         /* Update size and blocks for LSOM (best effort) */
1464         if (!io->ci_ignore_layout && cl_io_is_sync_write(io))
1465                 ll_merge_attr_try(env, inode);
1466 }
1467
1468
1469 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1470 {
1471         struct vm_fault *vmf = cfio->ft_vmf;
1472
1473         cfio->ft_flags = ll_filemap_fault(cfio->ft_vma, vmf);
1474         cfio->ft_flags_valid = 1;
1475
1476         if (vmf->page) {
1477                 /* success, vmpage is locked */
1478                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1479                                get_vmf_address(vmf));
1480                 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1481                         lock_page(vmf->page);
1482                         cfio->ft_flags |= VM_FAULT_LOCKED;
1483                 }
1484
1485                 cfio->ft_vmpage = vmf->page;
1486
1487                 return 0;
1488         }
1489
1490         if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1491                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", get_vmf_address(vmf));
1492                 return -EFAULT;
1493         }
1494
1495         if (cfio->ft_flags & VM_FAULT_OOM) {
1496                 CDEBUG(D_PAGE, "got addr %p - OOM\n", get_vmf_address(vmf));
1497                 return -ENOMEM;
1498         }
1499
1500         if (cfio->ft_flags & VM_FAULT_RETRY)
1501                 return -EAGAIN;
1502
1503         CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1504
1505         return -EINVAL;
1506 }
1507
1508 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1509                                     struct folio_batch *fbatch)
1510 {
1511         vvp_set_batch_dirty(fbatch);
1512 }
1513
1514 static int vvp_io_fault_start(const struct lu_env *env,
1515                               const struct cl_io_slice *ios)
1516 {
1517         struct vvp_io *vio = cl2vvp_io(env, ios);
1518         struct cl_io *io = ios->cis_io;
1519         struct cl_object *obj = io->ci_obj;
1520         struct inode *inode = vvp_object_inode(obj);
1521         struct ll_inode_info *lli = ll_i2info(inode);
1522         struct cl_fault_io *fio = &io->u.ci_fault;
1523         struct vvp_fault_io *cfio = &vio->u.fault;
1524         loff_t offset;
1525         int result = 0;
1526         struct page *vmpage = NULL;
1527         struct cl_page *page;
1528         loff_t size;
1529         pgoff_t last_index;
1530
1531         ENTRY;
1532
1533         trunc_sem_down_read_nowait(&lli->lli_trunc_sem);
1534
1535         /* offset of the last byte on the page */
1536         offset = ((fio->ft_index + 1) << PAGE_SHIFT) - 1;
1537         LASSERT((offset >> PAGE_SHIFT) == fio->ft_index);
1538         result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1539         if (result != 0)
1540                 RETURN(result);
1541
1542         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_FAULT_PAUSE, cfs_fail_val);
1543
1544         /* must return locked page */
1545         if (fio->ft_mkwrite) {
1546                 LASSERT(cfio->ft_vmpage != NULL);
1547                 vmpage = cfio->ft_vmpage;
1548                 lock_page(vmpage);
1549                 /**
1550                  * page was turncated and lock was cancelled, return ENODATA
1551                  * so that VM_FAULT_NOPAGE will be returned to handle_mm_fault()
1552                  * XXX: cannot return VM_FAULT_RETRY to vfs since we cannot
1553                  * release mmap_lock and VM_FAULT_RETRY implies that the
1554                  * mmap_lock is released.
1555                  */
1556                 if (!PageUptodate(vmpage))
1557                         GOTO(out, result = -ENODATA);
1558         } else {
1559                 result = vvp_io_kernel_fault(cfio);
1560                 if (result != 0)
1561                         RETURN(result);
1562         }
1563
1564         vmpage = cfio->ft_vmpage;
1565         LASSERT(PageLocked(vmpage));
1566
1567         if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1568                 generic_error_remove_page(vmpage->mapping, vmpage);
1569
1570         size = i_size_read(inode);
1571         /* Though we have already held a cl_lock upon this page, but
1572          * it still can be truncated locally.
1573          */
1574         if (unlikely((vmpage->mapping != inode->i_mapping) ||
1575                      (page_offset(vmpage) > size))) {
1576                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1577
1578                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1579                  * and retry.
1580                  */
1581                 GOTO(out, result = + 1);
1582         }
1583
1584         last_index = (size - 1) >> PAGE_SHIFT;
1585
1586         if (fio->ft_mkwrite) {
1587                 /*
1588                  * Capture the size while holding the lli_trunc_sem from above
1589                  * we want to make sure that we complete the mkwrite action
1590                  * while holding this lock. We need to make sure that we are
1591                  * not past the end of the file.
1592                  */
1593                 if (last_index < fio->ft_index) {
1594                         CDEBUG(D_PAGE,
1595                                 "llite: mkwrite and truncate race happened: %p: 0x%lx 0x%lx\n",
1596                                 vmpage->mapping, fio->ft_index, last_index);
1597                         /*
1598                          * We need to return if we are
1599                          * passed the end of the file. This will propagate
1600                          * up the call stack to ll_page_mkwrite where
1601                          * we will return VM_FAULT_NOPAGE. Any non-negative
1602                          * value returned here will be silently
1603                          * converted to 0. If the vmpage->mapping is null
1604                          * the error code would be converted back to ENODATA
1605                          * in ll_page_mkwrite0. Thus we return -ENODATA
1606                          * to handle both cases
1607                          */
1608                         GOTO(out, result = -ENODATA);
1609                 }
1610         }
1611
1612         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1613         if (IS_ERR(page))
1614                 GOTO(out, result = PTR_ERR(page));
1615
1616         /* if page will be written, then add this page into cache earlier. */
1617         if (fio->ft_mkwrite) {
1618                 wait_on_page_writeback(vmpage);
1619                 if (!PageDirty(vmpage)) {
1620                         struct cl_page_list *plist = &vio->u.fault.ft_queue;
1621                         int to = PAGE_SIZE;
1622
1623                         /* vvp_page_assume() calls wait_on_page_writeback(). */
1624                         cl_page_assume(env, io, page);
1625
1626                         cl_page_list_init(plist);
1627                         cl_page_list_add(plist, page, true);
1628
1629                         /* size fixup */
1630                         if (last_index == cl_page_index(page))
1631                                 to = ((size - 1) & ~PAGE_MASK) + 1;
1632
1633                         /* Do not set Dirty bit here so that in case IO is
1634                          * started before the page is really made dirty, we
1635                          * still have chance to detect it.
1636                          */
1637                         result = cl_io_commit_async(env, io, plist, 0, to,
1638                                                     mkwrite_commit_callback);
1639                         /* Have overquota flag, trying sync write to check
1640                          * whether indeed out of quota
1641                          */
1642                         if (result == -EDQUOT) {
1643                                 cl_page_get(page);
1644                                 result = vvp_io_commit_sync(env, io,
1645                                                             plist, 0, to);
1646                                 if (result >= 0) {
1647                                         io->ci_noquota = 1;
1648                                         cl_page_own(env, io, page);
1649                                         cl_page_list_add(plist, page, true);
1650                                         lu_ref_add(&page->cp_reference,
1651                                                    "cl_io", io);
1652                                         result = cl_io_commit_async(env, io,
1653                                                 plist, 0, to,
1654                                                 mkwrite_commit_callback);
1655                                         io->ci_noquota = 0;
1656                                 } else {
1657                                         cl_page_put(env, page);
1658                                 }
1659                         }
1660
1661                         LASSERT(cl_page_is_owned(page, io));
1662                         cl_page_list_fini(env, plist);
1663
1664                         vmpage = NULL;
1665                         if (result < 0) {
1666                                 cl_page_discard(env, io, page);
1667                                 cl_page_disown(env, io, page);
1668
1669                                 cl_page_put(env, page);
1670
1671                                 /* we're in big trouble, what can we do now? */
1672                                 if (result == -EDQUOT)
1673                                         result = -ENOSPC;
1674                                 GOTO(out, result);
1675                         } else {
1676                                 cl_page_disown(env, io, page);
1677                         }
1678                 }
1679         }
1680
1681         /*
1682          * The ft_index is only used in the case of  mkwrite action. We need to
1683          * check our assertions are correct, since we should have caught this
1684          * above
1685          */
1686         LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1687         if (fio->ft_index == last_index)
1688                 /* Last page is mapped partially. */
1689                 fio->ft_bytes = size - (fio->ft_index << PAGE_SHIFT);
1690         else
1691                 fio->ft_bytes = PAGE_SIZE;
1692
1693         lu_ref_add(&page->cp_reference, "fault", io);
1694         fio->ft_page = page;
1695         EXIT;
1696
1697 out:
1698         /* return unlocked vmpage to avoid deadlocking */
1699         if (vmpage != NULL)
1700                 unlock_page(vmpage);
1701
1702         cfio->ft_flags &= ~VM_FAULT_LOCKED;
1703
1704         return result;
1705 }
1706
1707 static void vvp_io_fault_end(const struct lu_env *env,
1708                              const struct cl_io_slice *ios)
1709 {
1710         struct inode            *inode = vvp_object_inode(ios->cis_obj);
1711         struct ll_inode_info    *lli   = ll_i2info(inode);
1712
1713         CLOBINVRNT(env, ios->cis_io->ci_obj,
1714                    vvp_object_invariant(ios->cis_io->ci_obj));
1715         trunc_sem_up_read(&lli->lli_trunc_sem);
1716 }
1717
1718 static int vvp_io_fsync_start(const struct lu_env *env,
1719                               const struct cl_io_slice *ios)
1720 {
1721         /* mark TOWRITE bit to each dirty page in radix tree to verify pages
1722          * have been written, but this is difficult because of race.
1723          */
1724         return 0;
1725 }
1726
1727 static void vvp_io_fsync_end(const struct lu_env *env,
1728                              const struct cl_io_slice *ios)
1729 {
1730         struct inode *inode = vvp_object_inode(ios->cis_obj);
1731         struct cl_io *io = ios->cis_io;
1732
1733         /* Update size and blocks for LSOM (best effort) */
1734         if (!io->ci_ignore_layout)
1735                 ll_merge_attr_try(env, inode);
1736 }
1737
1738 static int vvp_io_read_ahead(const struct lu_env *env,
1739                              const struct cl_io_slice *ios,
1740                              pgoff_t start, struct cl_read_ahead *ra)
1741 {
1742         int result = 0;
1743
1744         ENTRY;
1745
1746         if (ios->cis_io->ci_type == CIT_READ ||
1747             ios->cis_io->ci_type == CIT_FAULT) {
1748                 struct vvp_io *vio = cl2vvp_io(env, ios);
1749
1750                 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1751                         ra->cra_end_idx = CL_PAGE_EOF;
1752                         result = 1; /* no need to call down */
1753                 }
1754         }
1755
1756         RETURN(result);
1757 }
1758
1759 static int vvp_io_lseek_lock(const struct lu_env *env,
1760                              const struct cl_io_slice *ios)
1761 {
1762         struct cl_io *io = ios->cis_io;
1763         __u64 lock_start = io->u.ci_lseek.ls_start;
1764         __u64 lock_end = OBD_OBJECT_EOF;
1765         __u32 enqflags = CEF_MUST; /* always take client lock */
1766
1767         return vvp_io_one_lock(env, io, enqflags, CLM_READ,
1768                                lock_start, lock_end);
1769 }
1770
1771 static int vvp_io_lseek_start(const struct lu_env *env,
1772                               const struct cl_io_slice *ios)
1773 {
1774         struct cl_io *io = ios->cis_io;
1775         struct inode *inode = vvp_object_inode(io->ci_obj);
1776         __u64 start = io->u.ci_lseek.ls_start;
1777
1778         ll_inode_lock(inode);
1779         inode_dio_wait(inode);
1780
1781         /* At the moment we have DLM lock so just update inode
1782          * to know the file size.
1783          */
1784         ll_merge_attr(env, inode);
1785         if (start >= i_size_read(inode)) {
1786                 io->u.ci_lseek.ls_result = -ENXIO;
1787                 return -ENXIO;
1788         }
1789         return 0;
1790 }
1791
1792 static void vvp_io_lseek_end(const struct lu_env *env,
1793                              const struct cl_io_slice *ios)
1794 {
1795         struct cl_io *io = ios->cis_io;
1796         struct inode *inode = vvp_object_inode(io->ci_obj);
1797
1798         if (io->u.ci_lseek.ls_result > i_size_read(inode))
1799                 io->u.ci_lseek.ls_result = -ENXIO;
1800
1801         ll_inode_unlock(inode);
1802 }
1803
1804 static const struct cl_io_operations vvp_io_ops = {
1805         .op = {
1806                 [CIT_READ] = {
1807                         .cio_fini       = vvp_io_fini,
1808                         .cio_iter_init = vvp_io_read_iter_init,
1809                         .cio_lock       = vvp_io_read_lock,
1810                         .cio_start      = vvp_io_read_start,
1811                         .cio_end        = vvp_io_rw_end,
1812                         .cio_advance    = vvp_io_advance,
1813                 },
1814                 [CIT_WRITE] = {
1815                         .cio_fini      = vvp_io_fini,
1816                         .cio_iter_init = vvp_io_write_iter_init,
1817                         .cio_iter_fini = vvp_io_write_iter_fini,
1818                         .cio_lock      = vvp_io_write_lock,
1819                         .cio_start     = vvp_io_write_start,
1820                         .cio_end       = vvp_io_write_end,
1821                         .cio_advance   = vvp_io_advance,
1822                 },
1823                 [CIT_SETATTR] = {
1824                         .cio_fini       = vvp_io_setattr_fini,
1825                         .cio_iter_init  = vvp_io_setattr_iter_init,
1826                         .cio_lock       = vvp_io_setattr_lock,
1827                         .cio_start      = vvp_io_setattr_start,
1828                         .cio_end        = vvp_io_setattr_end
1829                 },
1830                 [CIT_FAULT] = {
1831                         .cio_fini      = vvp_io_fault_fini,
1832                         .cio_iter_init = vvp_io_fault_iter_init,
1833                         .cio_lock      = vvp_io_fault_lock,
1834                         .cio_start     = vvp_io_fault_start,
1835                         .cio_end       = vvp_io_fault_end,
1836                 },
1837                 [CIT_FSYNC] = {
1838                         .cio_start      = vvp_io_fsync_start,
1839                         .cio_fini       = vvp_io_fini,
1840                         .cio_end        = vvp_io_fsync_end,
1841                 },
1842                 [CIT_GLIMPSE] = {
1843                         .cio_fini       = vvp_io_fini
1844                 },
1845                 [CIT_MISC] = {
1846                         .cio_fini       = vvp_io_fini
1847                 },
1848                 [CIT_LADVISE] = {
1849                         .cio_fini       = vvp_io_fini
1850                 },
1851                 [CIT_LSEEK] = {
1852                         .cio_fini      = vvp_io_fini,
1853                         .cio_lock      = vvp_io_lseek_lock,
1854                         .cio_start     = vvp_io_lseek_start,
1855                         .cio_end       = vvp_io_lseek_end,
1856                 },
1857         },
1858         .cio_read_ahead = vvp_io_read_ahead
1859 };
1860
1861 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1862                 struct cl_io *io)
1863 {
1864         struct vvp_io      *vio   = vvp_env_io(env);
1865         struct inode       *inode = vvp_object_inode(obj);
1866         int                 result;
1867
1868         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1869         ENTRY;
1870
1871         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d restore needed %d\n",
1872                PFID(lu_object_fid(&obj->co_lu)),
1873                io->ci_ignore_layout, io->ci_verify_layout,
1874                vio->vui_layout_gen, io->ci_restore_needed);
1875
1876         CL_IO_SLICE_CLEAN(vio, vui_cl);
1877         cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1878         vio->vui_ra_valid = false;
1879         result = 0;
1880         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1881                 size_t bytes;
1882                 struct ll_inode_info *lli = ll_i2info(inode);
1883
1884                 bytes = io->u.ci_rw.crw_bytes;
1885                 /* "If nbyte is 0, read() will return 0 and have no other
1886                  *  results."  -- Single Unix Spec
1887                  */
1888                 if (bytes == 0)
1889                         result = 1;
1890                 else
1891                         vio->vui_tot_bytes = bytes;
1892
1893                 /* for read/write, we store the process jobid/gid/uid in the
1894                  * inode, and it'll be fetched by osc when building RPC.
1895                  *
1896                  * it's not accurate if the file is shared by different
1897                  * jobs/user/group.
1898                  */
1899                 lustre_get_jobid(lli->lli_jobid, sizeof(lli->lli_jobid));
1900                 lli->lli_uid = from_kuid(&init_user_ns, current_uid());
1901                 lli->lli_gid = from_kgid(&init_user_ns, current_gid());
1902         } else if (io->ci_type == CIT_SETATTR) {
1903                 if (!cl_io_is_trunc(io))
1904                         io->ci_lockreq = CILR_MANDATORY;
1905         }
1906
1907         /* Enqueue layout lock and get layout version. We need to do this
1908          * even for operations requiring to open file, such as read and write,
1909          * because it might not grant layout lock in IT_OPEN.
1910          */
1911         if (result == 0 && !io->ci_ignore_layout) {
1912                 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1913                 if (result == -ENOENT)
1914                         /* If the inode on MDS has been removed, but the objects
1915                          * on OSTs haven't been destroyed (async unlink), layout
1916                          * fetch will return -ENOENT, we'd ingore this error
1917                          * and continue with dirty flush. LU-3230.
1918                          */
1919                         result = 0;
1920                 if (result < 0)
1921                         CERROR("%s: refresh file layout " DFID " error %d.\n",
1922                                ll_i2sbi(inode)->ll_fsname,
1923                                PFID(lu_object_fid(&obj->co_lu)), result);
1924         }
1925
1926 #ifdef HAVE_INVALIDATE_LOCK
1927         if (io->ci_invalidate_page_cache)
1928                 filemap_invalidate_lock(inode->i_mapping);
1929 #endif /* HAVE_INVALIDATE_LOCK */
1930
1931         io->ci_result = result < 0 ? result : 0;
1932         RETURN(result);
1933 }