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