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