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