Whamcloud - gitweb
dc4e29489a29ebdf949d6d9d0a614443292770d1
[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 /**
53  * True, if \a io is a normal io, False for splice_{read,write}
54  */
55 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
56 {
57         struct vvp_io *vio = vvp_env_io(env);
58
59         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
60
61         return vio->cui_io_subtype == IO_NORMAL;
62 }
63
64 /**
65  * For swapping layout. The file's layout may have changed.
66  * To avoid populating pages to a wrong stripe, we have to verify the
67  * correctness of layout. It works because swapping layout processes
68  * have to acquire group lock.
69  */
70 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
71                                 struct inode *inode)
72 {
73         struct ll_inode_info    *lli = ll_i2info(inode);
74         struct ccc_io           *cio = ccc_env_io(env);
75         bool rc = true;
76
77         switch (io->ci_type) {
78         case CIT_READ:
79         case CIT_WRITE:
80                 /* don't need lock here to check lli_layout_gen as we have held
81                  * extent lock and GROUP lock has to hold to swap layout */
82                 if (ll_layout_version_get(lli) != cio->cui_layout_gen) {
83                         io->ci_need_restart = 1;
84                         /* this will return application a short read/write */
85                         io->ci_continue = 0;
86                         rc = false;
87                 }
88         case CIT_FAULT:
89                 /* fault is okay because we've already had a page. */
90         default:
91                 break;
92         }
93
94         return rc;
95 }
96
97 /*****************************************************************************
98  *
99  * io operations.
100  *
101  */
102
103 static int vvp_io_write_iter_init(const struct lu_env *env,
104                                   const struct cl_io_slice *ios)
105 {
106         struct ccc_io *cio = cl2ccc_io(env, ios);
107
108         cl_page_list_init(&cio->u.write.cui_queue);
109         cio->u.write.cui_written = 0;
110         cio->u.write.cui_from = 0;
111         cio->u.write.cui_to = PAGE_SIZE;
112
113         return 0;
114 }
115
116 static void vvp_io_write_iter_fini(const struct lu_env *env,
117                                    const struct cl_io_slice *ios)
118 {
119         struct ccc_io *cio = cl2ccc_io(env, ios);
120
121         LASSERT(cio->u.write.cui_queue.pl_nr == 0);
122 }
123
124 static int vvp_io_fault_iter_init(const struct lu_env *env,
125                                   const struct cl_io_slice *ios)
126 {
127         struct vvp_io *vio   = cl2vvp_io(env, ios);
128         struct inode  *inode = vvp_object_inode(ios->cis_obj);
129
130         LASSERT(inode ==
131                 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
132         vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
133         return 0;
134 }
135
136 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
137 {
138         struct cl_io     *io  = ios->cis_io;
139         struct cl_object *obj = io->ci_obj;
140         struct ccc_io    *cio = cl2ccc_io(env, ios);
141         struct inode     *inode = vvp_object_inode(obj);
142
143         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
144
145         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
146                            "restore needed %d\n",
147                PFID(lu_object_fid(&obj->co_lu)),
148                io->ci_ignore_layout, io->ci_verify_layout,
149                cio->cui_layout_gen, io->ci_restore_needed);
150
151         if (io->ci_restore_needed == 1) {
152                 int     rc;
153
154                 /* file was detected release, we need to restore it
155                  * before finishing the io
156                  */
157                 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
158                 /* if restore registration failed, no restart,
159                  * we will return -ENODATA */
160                 /* The layout will change after restore, so we need to
161                  * block on layout lock hold by the MDT
162                  * as MDT will not send new layout in lvb (see LU-3124)
163                  * we have to explicitly fetch it, all this will be done
164                  * by ll_layout_refresh()
165                  */
166                 if (rc == 0) {
167                         io->ci_restore_needed = 0;
168                         io->ci_need_restart = 1;
169                         io->ci_verify_layout = 1;
170                 } else {
171                         io->ci_restore_needed = 1;
172                         io->ci_need_restart = 0;
173                         io->ci_verify_layout = 0;
174                         io->ci_result = rc;
175                 }
176         }
177
178         if (!io->ci_ignore_layout && io->ci_verify_layout) {
179                 __u32 gen = 0;
180
181                 /* check layout version */
182                 ll_layout_refresh(inode, &gen);
183                 io->ci_need_restart = cio->cui_layout_gen != gen;
184                 if (io->ci_need_restart) {
185                         CDEBUG(D_VFSTRACE,
186                                DFID" layout changed from %d to %d.\n",
187                                PFID(lu_object_fid(&obj->co_lu)),
188                                cio->cui_layout_gen, gen);
189                         /* today successful restore is the only possible
190                          * case */
191                         /* restore was done, clear restoring state */
192                         ll_i2info(vvp_object_inode(obj))->lli_flags &=
193                                 ~LLIF_FILE_RESTORING;
194                 }
195         }
196 }
197
198 static void vvp_io_fault_fini(const struct lu_env *env,
199                               const struct cl_io_slice *ios)
200 {
201         struct cl_io   *io   = ios->cis_io;
202         struct cl_page *page = io->u.ci_fault.ft_page;
203
204         CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
205
206         if (page != NULL) {
207                 lu_ref_del(&page->cp_reference, "fault", io);
208                 cl_page_put(env, page);
209                 io->u.ci_fault.ft_page = NULL;
210         }
211         vvp_io_fini(env, ios);
212 }
213
214 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
215 {
216         /*
217          * we only want to hold PW locks if the mmap() can generate
218          * writes back to the file and that only happens in shared
219          * writable vmas
220          */
221         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
222                 return CLM_WRITE;
223         return CLM_READ;
224 }
225
226 static int vvp_mmap_locks(const struct lu_env *env,
227                           struct ccc_io *vio, struct cl_io *io)
228 {
229         struct ccc_thread_info *cti = ccc_env_info(env);
230         struct mm_struct       *mm = current->mm;
231         struct vm_area_struct  *vma;
232         struct cl_lock_descr   *descr = &cti->cti_descr;
233         ldlm_policy_data_t      policy;
234         unsigned long           addr;
235         unsigned long           seg;
236         ssize_t                 count;
237         int                     result = 0;
238         ENTRY;
239
240         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
241
242         if (!cl_is_normalio(env, io))
243                 RETURN(0);
244
245         if (vio->cui_iov == NULL) /* nfs or loop back device write */
246                 RETURN(0);
247
248         /* No MM (e.g. NFS)? No vmas too. */
249         if (mm == NULL)
250                 RETURN(0);
251
252         for (seg = 0; seg < vio->cui_nrsegs; seg++) {
253                 const struct iovec *iv = &vio->cui_iov[seg];
254
255                 addr = (unsigned long)iv->iov_base;
256                 count = iv->iov_len;
257                 if (count == 0)
258                         continue;
259
260                 count += addr & (~CFS_PAGE_MASK);
261                 addr &= CFS_PAGE_MASK;
262
263                 down_read(&mm->mmap_sem);
264                 while((vma = our_vma(mm, addr, count)) != NULL) {
265                         struct inode *inode = vma->vm_file->f_dentry->d_inode;
266                         int flags = CEF_MUST;
267
268                         if (ll_file_nolock(vma->vm_file)) {
269                                 /*
270                                  * For no lock case is not allowed for mmap
271                                  */
272                                 result = -EINVAL;
273                                 break;
274                         }
275
276                         /*
277                          * XXX: Required lock mode can be weakened: CIT_WRITE
278                          * io only ever reads user level buffer, and CIT_READ
279                          * only writes on it.
280                          */
281                         policy_from_vma(&policy, vma, addr, count);
282                         descr->cld_mode = vvp_mode_from_vma(vma);
283                         descr->cld_obj = ll_i2info(inode)->lli_clob;
284                         descr->cld_start = cl_index(descr->cld_obj,
285                                                     policy.l_extent.start);
286                         descr->cld_end = cl_index(descr->cld_obj,
287                                                   policy.l_extent.end);
288                         descr->cld_enq_flags = flags;
289                         result = cl_io_lock_alloc_add(env, io, descr);
290
291                         CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
292                                descr->cld_mode, descr->cld_start,
293                                descr->cld_end);
294
295                         if (result < 0)
296                                 break;
297
298                         if (vma->vm_end - addr >= count)
299                                 break;
300
301                         count -= vma->vm_end - addr;
302                         addr = vma->vm_end;
303                 }
304                 up_read(&mm->mmap_sem);
305                 if (result < 0)
306                         break;
307         }
308         RETURN(result);
309 }
310
311 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
312                           enum cl_lock_mode mode, loff_t start, loff_t end)
313 {
314         struct ccc_io *cio = ccc_env_io(env);
315         int result;
316         int ast_flags = 0;
317
318         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
319         ENTRY;
320
321         ccc_io_update_iov(env, cio, io);
322
323         if (io->u.ci_rw.crw_nonblock)
324                 ast_flags |= CEF_NONBLOCK;
325         result = vvp_mmap_locks(env, cio, io);
326         if (result == 0)
327                 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
328         RETURN(result);
329 }
330
331 static int vvp_io_read_lock(const struct lu_env *env,
332                             const struct cl_io_slice *ios)
333 {
334         struct cl_io            *io  = ios->cis_io;
335         struct cl_io_rw_common  *rd = &io->u.ci_rd.rd;
336         int result;
337
338         ENTRY;
339         result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
340                                 rd->crw_pos + rd->crw_count - 1);
341         RETURN(result);
342 }
343
344 static int vvp_io_fault_lock(const struct lu_env *env,
345                              const struct cl_io_slice *ios)
346 {
347         struct cl_io *io   = ios->cis_io;
348         struct vvp_io *vio = cl2vvp_io(env, ios);
349         /*
350          * XXX LDLM_FL_CBPENDING
351          */
352         return ccc_io_one_lock_index
353                 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
354                  io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
355 }
356
357 static int vvp_io_write_lock(const struct lu_env *env,
358                              const struct cl_io_slice *ios)
359 {
360         struct cl_io *io = ios->cis_io;
361         loff_t start;
362         loff_t end;
363
364         if (io->u.ci_wr.wr_append) {
365                 start = 0;
366                 end   = OBD_OBJECT_EOF;
367         } else {
368                 start = io->u.ci_wr.wr.crw_pos;
369                 end   = start + io->u.ci_wr.wr.crw_count - 1;
370         }
371         return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
372 }
373
374 static int vvp_io_setattr_iter_init(const struct lu_env *env,
375                                     const struct cl_io_slice *ios)
376 {
377         return 0;
378 }
379
380 /**
381  * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
382  *
383  * Handles "lockless io" mode when extent locking is done by server.
384  */
385 static int vvp_io_setattr_lock(const struct lu_env *env,
386                                const struct cl_io_slice *ios)
387 {
388         struct ccc_io *cio = ccc_env_io(env);
389         struct cl_io  *io  = ios->cis_io;
390         __u64 new_size;
391         __u32 enqflags = 0;
392
393         if (cl_io_is_trunc(io)) {
394                 new_size = io->u.ci_setattr.sa_attr.lvb_size;
395                 if (new_size == 0)
396                         enqflags = CEF_DISCARD_DATA;
397         } else {
398                 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
399                      io->u.ci_setattr.sa_attr.lvb_ctime) ||
400                     (io->u.ci_setattr.sa_attr.lvb_atime >=
401                      io->u.ci_setattr.sa_attr.lvb_ctime))
402                         return 0;
403                 new_size = 0;
404         }
405         cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
406         return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
407                                new_size, OBD_OBJECT_EOF);
408 }
409
410 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
411 {
412         int     result;
413
414         /*
415          * Only ll_inode_size_lock is taken at this level.
416          */
417         ll_inode_size_lock(inode);
418         result = inode_newsize_ok(inode, size);
419         if (result < 0) {
420                 ll_inode_size_unlock(inode);
421                 return result;
422         }
423         i_size_write(inode, size);
424
425         ll_truncate_pagecache(inode, size);
426         ll_inode_size_unlock(inode);
427         return result;
428 }
429
430 static int vvp_io_setattr_trunc(const struct lu_env *env,
431                                 const struct cl_io_slice *ios,
432                                 struct inode *inode, loff_t size)
433 {
434         inode_dio_wait(inode);
435         return 0;
436 }
437
438 static int vvp_io_setattr_time(const struct lu_env *env,
439                                const struct cl_io_slice *ios)
440 {
441         struct cl_io       *io    = ios->cis_io;
442         struct cl_object   *obj   = io->ci_obj;
443         struct cl_attr     *attr  = ccc_env_thread_attr(env);
444         int result;
445         unsigned valid = CAT_CTIME;
446
447         cl_object_attr_lock(obj);
448         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
449         if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
450                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
451                 valid |= CAT_ATIME;
452         }
453         if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
454                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
455                 valid |= CAT_MTIME;
456         }
457         result = cl_object_attr_set(env, obj, attr, valid);
458         cl_object_attr_unlock(obj);
459
460         return result;
461 }
462
463 static int vvp_io_setattr_start(const struct lu_env *env,
464                                 const struct cl_io_slice *ios)
465 {
466         struct cl_io    *io    = ios->cis_io;
467         struct inode    *inode = vvp_object_inode(io->ci_obj);
468         int result = 0;
469
470         mutex_lock(&inode->i_mutex);
471         if (cl_io_is_trunc(io))
472                 result = vvp_io_setattr_trunc(env, ios, inode,
473                                         io->u.ci_setattr.sa_attr.lvb_size);
474         if (result == 0)
475                 result = vvp_io_setattr_time(env, ios);
476         return result;
477 }
478
479 static void vvp_io_setattr_end(const struct lu_env *env,
480                                const struct cl_io_slice *ios)
481 {
482         struct cl_io *io    = ios->cis_io;
483         struct inode *inode = vvp_object_inode(io->ci_obj);
484
485         if (cl_io_is_trunc(io)) {
486                 /* Truncate in memory pages - they must be clean pages
487                  * because osc has already notified to destroy osc_extents. */
488                 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
489                 inode_dio_write_done(inode);
490         }
491         mutex_unlock(&inode->i_mutex);
492 }
493
494 static void vvp_io_setattr_fini(const struct lu_env *env,
495                                 const struct cl_io_slice *ios)
496 {
497         vvp_io_fini(env, ios);
498 }
499
500 static int vvp_io_read_start(const struct lu_env *env,
501                              const struct cl_io_slice *ios)
502 {
503         struct vvp_io     *vio   = cl2vvp_io(env, ios);
504         struct ccc_io     *cio   = cl2ccc_io(env, ios);
505         struct cl_io      *io    = ios->cis_io;
506         struct cl_object  *obj   = io->ci_obj;
507         struct inode      *inode = vvp_object_inode(obj);
508         struct file       *file  = cio->cui_fd->fd_file;
509
510         int     result;
511         loff_t  pos = io->u.ci_rd.rd.crw_pos;
512         long    cnt = io->u.ci_rd.rd.crw_count;
513         long    tot = cio->cui_tot_count;
514         int     exceed = 0;
515
516         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
517
518         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
519
520         if (!can_populate_pages(env, io, inode))
521                 return 0;
522
523         result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
524         if (result != 0)
525                 return result;
526         else if (exceed != 0)
527                 goto out;
528
529         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
530                         "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
531                         inode->i_ino, cnt, pos, i_size_read(inode));
532
533         /* turn off the kernel's read-ahead */
534         cio->cui_fd->fd_file->f_ra.ra_pages = 0;
535
536         /* initialize read-ahead window once per syscall */
537         if (!vio->cui_ra_valid) {
538                 vio->cui_ra_valid = true;
539                 vio->cui_ra_start = cl_index(obj, pos);
540                 vio->cui_ra_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
541                 ll_ras_enter(file);
542         }
543
544         /* BUG: 5972 */
545         file_accessed(file);
546         switch (vio->cui_io_subtype) {
547         case IO_NORMAL:
548                 LASSERT(cio->cui_iocb->ki_pos == pos);
549                 result = generic_file_aio_read(cio->cui_iocb,
550                                                cio->cui_iov, cio->cui_nrsegs,
551                                                cio->cui_iocb->ki_pos);
552                 break;
553         case IO_SPLICE:
554                 result = generic_file_splice_read(file, &pos,
555                                 vio->u.splice.cui_pipe, cnt,
556                                 vio->u.splice.cui_flags);
557                 /* LU-1109: do splice read stripe by stripe otherwise if it
558                  * may make nfsd stuck if this read occupied all internal pipe
559                  * buffers. */
560                 io->ci_continue = 0;
561                 break;
562         default:
563                 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
564                 LBUG();
565         }
566
567 out:
568         if (result >= 0) {
569                 if (result < cnt)
570                         io->ci_continue = 0;
571                 io->ci_nob += result;
572                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
573                                   pos, result, READ);
574                 result = 0;
575         }
576
577         return result;
578 }
579
580 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
581                               struct cl_page_list *plist, int from, int to)
582 {
583         struct cl_2queue *queue = &io->ci_queue;
584         struct cl_page *page;
585         unsigned int bytes = 0;
586         int rc = 0;
587         ENTRY;
588
589         if (plist->pl_nr == 0)
590                 RETURN(0);
591
592         if (from > 0 || to != PAGE_SIZE) {
593                 page = cl_page_list_first(plist);
594                 if (plist->pl_nr == 1) {
595                         cl_page_clip(env, page, from, to);
596                 } else {
597                         if (from > 0)
598                                 cl_page_clip(env, page, from, PAGE_SIZE);
599                         if (to != PAGE_SIZE) {
600                                 page = cl_page_list_last(plist);
601                                 cl_page_clip(env, page, 0, to);
602                         }
603                 }
604         }
605
606         cl_2queue_init(queue);
607         cl_page_list_splice(plist, &queue->c2_qin);
608         rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
609
610         /* plist is not sorted any more */
611         cl_page_list_splice(&queue->c2_qin, plist);
612         cl_page_list_splice(&queue->c2_qout, plist);
613         cl_2queue_fini(env, queue);
614
615         if (rc == 0) {
616                 /* calculate bytes */
617                 bytes = plist->pl_nr << PAGE_SHIFT;
618                 bytes -= from + PAGE_SIZE - to;
619
620                 while (plist->pl_nr > 0) {
621                         page = cl_page_list_first(plist);
622                         cl_page_list_del(env, plist, page);
623
624                         cl_page_clip(env, page, 0, PAGE_SIZE);
625
626                         SetPageUptodate(cl_page_vmpage(page));
627                         cl_page_disown(env, io, page);
628
629                         /* held in ll_cl_init() */
630                         lu_ref_del(&page->cp_reference, "cl_io", io);
631                         cl_page_put(env, page);
632                 }
633         }
634
635         RETURN(bytes > 0 ? bytes : rc);
636 }
637
638 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
639                                 struct cl_page *page)
640 {
641         struct vvp_page *vpg;
642         struct page *vmpage = page->cp_vmpage;
643         struct cl_object *clob = cl_io_top(io)->ci_obj;
644
645         SetPageUptodate(vmpage);
646         set_page_dirty(vmpage);
647
648         vpg = cl2vvp_page(cl_object_page_slice(clob, page));
649         vvp_write_pending(cl2vvp(clob), vpg);
650
651         cl_page_disown(env, io, page);
652
653         /* held in ll_cl_init() */
654         lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
655         cl_page_put(env, page);
656 }
657
658 /* make sure the page list is contiguous */
659 static bool page_list_sanity_check(struct cl_object *obj,
660                                    struct cl_page_list *plist)
661 {
662         struct cl_page *page;
663         pgoff_t index = CL_PAGE_EOF;
664
665         cl_page_list_for_each(page, plist) {
666                 struct vvp_page *vpg = cl_object_page_slice(obj, page);
667
668                 if (index == CL_PAGE_EOF) {
669                         index = vvp_index(vpg);
670                         continue;
671                 }
672
673                 ++index;
674                 if (index == vvp_index(vpg))
675                         continue;
676
677                 return false;
678         }
679         return true;
680 }
681
682 /* Return how many bytes have queued or written */
683 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
684 {
685         struct cl_object *obj = io->ci_obj;
686         struct inode *inode = vvp_object_inode(obj);
687         struct ccc_io *cio = ccc_env_io(env);
688         struct cl_page_list *queue = &cio->u.write.cui_queue;
689         struct cl_page *page;
690         int rc = 0;
691         int bytes = 0;
692         unsigned int npages = cio->u.write.cui_queue.pl_nr;
693         ENTRY;
694
695         if (npages == 0)
696                 RETURN(0);
697
698         CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
699                 npages, cio->u.write.cui_from, cio->u.write.cui_to);
700
701         LASSERT(page_list_sanity_check(obj, queue));
702
703         /* submit IO with async write */
704         rc = cl_io_commit_async(env, io, queue,
705                                 cio->u.write.cui_from, cio->u.write.cui_to,
706                                 write_commit_callback);
707         npages -= queue->pl_nr; /* already committed pages */
708         if (npages > 0) {
709                 /* calculate how many bytes were written */
710                 bytes = npages << PAGE_SHIFT;
711
712                 /* first page */
713                 bytes -= cio->u.write.cui_from;
714                 if (queue->pl_nr == 0) /* last page */
715                         bytes -= PAGE_SIZE - cio->u.write.cui_to;
716                 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
717
718                 cio->u.write.cui_written += bytes;
719
720                 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
721                         npages, bytes, cio->u.write.cui_written);
722
723                 /* the first page must have been written. */
724                 cio->u.write.cui_from = 0;
725         }
726         LASSERT(page_list_sanity_check(obj, queue));
727         LASSERT(ergo(rc == 0, queue->pl_nr == 0));
728
729         /* out of quota, try sync write */
730         if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
731                 rc = vvp_io_commit_sync(env, io, queue,
732                                         cio->u.write.cui_from,
733                                         cio->u.write.cui_to);
734                 if (rc > 0) {
735                         cio->u.write.cui_written += rc;
736                         rc = 0;
737                 }
738         }
739
740         /* update inode size */
741         ll_merge_attr(env, inode);
742
743         /* Now the pages in queue were failed to commit, discard them
744          * unless they were dirtied before. */
745         while (queue->pl_nr > 0) {
746                 page = cl_page_list_first(queue);
747                 cl_page_list_del(env, queue, page);
748
749                 if (!PageDirty(cl_page_vmpage(page)))
750                         cl_page_discard(env, io, page);
751
752                 cl_page_disown(env, io, page);
753
754                 /* held in ll_cl_init() */
755                 lu_ref_del(&page->cp_reference, "cl_io", io);
756                 cl_page_put(env, page);
757         }
758         cl_page_list_fini(env, queue);
759
760         RETURN(rc);
761 }
762
763 static int vvp_io_write_start(const struct lu_env *env,
764                               const struct cl_io_slice *ios)
765 {
766         struct ccc_io      *cio   = cl2ccc_io(env, ios);
767         struct cl_io       *io    = ios->cis_io;
768         struct cl_object   *obj   = io->ci_obj;
769         struct inode       *inode = vvp_object_inode(obj);
770         ssize_t result = 0;
771         loff_t pos = io->u.ci_wr.wr.crw_pos;
772         size_t cnt = io->u.ci_wr.wr.crw_count;
773
774         ENTRY;
775
776         if (!can_populate_pages(env, io, inode))
777                 RETURN(0);
778
779         if (cl_io_is_append(io)) {
780                 /*
781                  * PARALLEL IO This has to be changed for parallel IO doing
782                  * out-of-order writes.
783                  */
784                 ll_merge_attr(env, inode);
785                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
786                 cio->cui_iocb->ki_pos = pos;
787         } else {
788                 LASSERT(cio->cui_iocb->ki_pos == pos);
789         }
790
791         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
792
793         if (cio->cui_iov == NULL) {
794                 /* from a temp io in ll_cl_init(). */
795                 result = 0;
796         } else {
797                 /*
798                  * When using the locked AIO function (generic_file_aio_write())
799                  * testing has shown the inode mutex to be a limiting factor
800                  * with multi-threaded single shared file performance. To get
801                  * around this, we now use the lockless version. To maintain
802                  * consistency, proper locking to protect against writes,
803                  * trucates, etc. is handled in the higher layers of lustre.
804                  */
805                 result = __generic_file_aio_write(cio->cui_iocb,
806                                                   cio->cui_iov, cio->cui_nrsegs,
807                                                   &cio->cui_iocb->ki_pos);
808                 if (result > 0 || result == -EIOCBQUEUED) {
809                         ssize_t err;
810
811                         err = generic_write_sync(cio->cui_iocb->ki_filp,
812                                                  pos, result);
813                         if (err < 0 && result > 0)
814                                 result = err;
815                 }
816
817         }
818         if (result > 0) {
819                 result = vvp_io_write_commit(env, io);
820                 if (cio->u.write.cui_written > 0) {
821                         result = cio->u.write.cui_written;
822                         io->ci_nob += result;
823
824                         CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
825                                 io->ci_nob, result);
826                 }
827         }
828         if (result > 0) {
829                 struct ll_inode_info *lli = ll_i2info(inode);
830
831                 spin_lock(&lli->lli_lock);
832                 lli->lli_flags |= LLIF_DATA_MODIFIED;
833                 spin_unlock(&lli->lli_lock);
834
835                 if (result < cnt)
836                         io->ci_continue = 0;
837                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
838                                   cio->cui_fd, pos, result, WRITE);
839                 result = 0;
840         }
841
842         RETURN(result);
843 }
844
845 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
846 {
847         struct vm_fault *vmf = cfio->fault.ft_vmf;
848
849         cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
850         cfio->fault.ft_flags_valid = 1;
851
852         if (vmf->page) {
853                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
854                                vmf->virtual_address);
855                 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
856                         lock_page(vmf->page);
857                         cfio->fault.ft_flags |= VM_FAULT_LOCKED;
858                 }
859
860                 cfio->ft_vmpage = vmf->page;
861                 return 0;
862         }
863
864         if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
865                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
866                 return -EFAULT;
867         }
868
869         if (cfio->fault.ft_flags & VM_FAULT_OOM) {
870                 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
871                 return -ENOMEM;
872         }
873
874         if (cfio->fault.ft_flags & VM_FAULT_RETRY)
875                 return -EAGAIN;
876
877         CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
878         return -EINVAL;
879 }
880
881 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
882                                     struct cl_page *page)
883 {
884         struct vvp_page *vpg;
885         struct cl_object *clob = cl_io_top(io)->ci_obj;
886
887         set_page_dirty(page->cp_vmpage);
888
889         vpg = cl2vvp_page(cl_object_page_slice(clob, page));
890         vvp_write_pending(cl2vvp(clob), vpg);
891 }
892
893 static int vvp_io_fault_start(const struct lu_env *env,
894                               const struct cl_io_slice *ios)
895 {
896         struct vvp_io       *vio     = cl2vvp_io(env, ios);
897         struct cl_io        *io      = ios->cis_io;
898         struct cl_object    *obj     = io->ci_obj;
899         struct inode        *inode   = vvp_object_inode(obj);
900         struct cl_fault_io  *fio     = &io->u.ci_fault;
901         struct vvp_fault_io *cfio    = &vio->u.fault;
902         loff_t               offset;
903         int                  result  = 0;
904         struct page          *vmpage  = NULL;
905         struct cl_page      *page;
906         loff_t               size;
907         pgoff_t              last_index;
908         ENTRY;
909
910         if (fio->ft_executable &&
911             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
912                 CWARN("binary "DFID
913                       " changed while waiting for the page fault lock\n",
914                       PFID(lu_object_fid(&obj->co_lu)));
915
916         /* offset of the last byte on the page */
917         offset = cl_offset(obj, fio->ft_index + 1) - 1;
918         LASSERT(cl_index(obj, offset) == fio->ft_index);
919         result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
920         if (result != 0)
921                 RETURN(result);
922
923         /* must return locked page */
924         if (fio->ft_mkwrite) {
925                 LASSERT(cfio->ft_vmpage != NULL);
926                 lock_page(cfio->ft_vmpage);
927         } else {
928                 result = vvp_io_kernel_fault(cfio);
929                 if (result != 0)
930                         RETURN(result);
931         }
932
933         vmpage = cfio->ft_vmpage;
934         LASSERT(PageLocked(vmpage));
935
936         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
937                 ll_invalidate_page(vmpage);
938
939         size = i_size_read(inode);
940         /* Though we have already held a cl_lock upon this page, but
941          * it still can be truncated locally. */
942         if (unlikely((vmpage->mapping != inode->i_mapping) ||
943                      (page_offset(vmpage) > size))) {
944                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
945
946                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
947                  * and retry. */
948                 GOTO(out, result = +1);
949         }
950
951         last_index = cl_index(obj, size - 1);
952
953         if (fio->ft_mkwrite ) {
954                 /*
955                  * Capture the size while holding the lli_trunc_sem from above
956                  * we want to make sure that we complete the mkwrite action
957                  * while holding this lock. We need to make sure that we are
958                  * not past the end of the file.
959                  */
960                 if (last_index < fio->ft_index) {
961                         CDEBUG(D_PAGE,
962                                 "llite: mkwrite and truncate race happened: "
963                                 "%p: 0x%lx 0x%lx\n",
964                                 vmpage->mapping,fio->ft_index,last_index);
965                         /*
966                          * We need to return if we are
967                          * passed the end of the file. This will propagate
968                          * up the call stack to ll_page_mkwrite where
969                          * we will return VM_FAULT_NOPAGE. Any non-negative
970                          * value returned here will be silently
971                          * converted to 0. If the vmpage->mapping is null
972                          * the error code would be converted back to ENODATA
973                          * in ll_page_mkwrite0. Thus we return -ENODATA
974                          * to handle both cases
975                          */
976                         GOTO(out, result = -ENODATA);
977                 }
978         }
979
980         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
981         if (IS_ERR(page))
982                 GOTO(out, result = PTR_ERR(page));
983
984         /* if page is going to be written, we should add this page into cache
985          * earlier. */
986         if (fio->ft_mkwrite) {
987                 wait_on_page_writeback(vmpage);
988                 if (!PageDirty(vmpage)) {
989                         struct cl_page_list *plist = &io->ci_queue.c2_qin;
990                         struct vvp_page *vpg = cl_object_page_slice(obj, page);
991                         int to = PAGE_SIZE;
992
993                         /* vvp_page_assume() calls wait_on_page_writeback(). */
994                         cl_page_assume(env, io, page);
995
996                         cl_page_list_init(plist);
997                         cl_page_list_add(plist, page);
998
999                         /* size fixup */
1000                         if (last_index == vvp_index(vpg))
1001                                 to = size & ~CFS_PAGE_MASK;
1002
1003                         /* Do not set Dirty bit here so that in case IO is
1004                          * started before the page is really made dirty, we
1005                          * still have chance to detect it. */
1006                         result = cl_io_commit_async(env, io, plist, 0, to,
1007                                                     mkwrite_commit_callback);
1008                         LASSERT(cl_page_is_owned(page, io));
1009                         cl_page_list_fini(env, plist);
1010
1011                         vmpage = NULL;
1012                         if (result < 0) {
1013                                 cl_page_discard(env, io, page);
1014                                 cl_page_disown(env, io, page);
1015
1016                                 cl_page_put(env, page);
1017
1018                                 /* we're in big trouble, what can we do now? */
1019                                 if (result == -EDQUOT)
1020                                         result = -ENOSPC;
1021                                 GOTO(out, result);
1022                         } else
1023                                 cl_page_disown(env, io, page);
1024                 }
1025         }
1026
1027         /*
1028          * The ft_index is only used in the case of
1029          * a mkwrite action. We need to check
1030          * our assertions are correct, since
1031          * we should have caught this above
1032          */
1033         LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1034         if (fio->ft_index == last_index)
1035                 /*
1036                  * Last page is mapped partially.
1037                  */
1038                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1039         else
1040                 fio->ft_nob = cl_page_size(obj);
1041
1042         lu_ref_add(&page->cp_reference, "fault", io);
1043         fio->ft_page = page;
1044         EXIT;
1045
1046 out:
1047         /* return unlocked vmpage to avoid deadlocking */
1048         if (vmpage != NULL)
1049                 unlock_page(vmpage);
1050         cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1051         return result;
1052 }
1053
1054 static int vvp_io_fsync_start(const struct lu_env *env,
1055                               const struct cl_io_slice *ios)
1056 {
1057         /* we should mark TOWRITE bit to each dirty page in radix tree to
1058          * verify pages have been written, but this is difficult because of
1059          * race. */
1060         return 0;
1061 }
1062
1063 static int vvp_io_read_page(const struct lu_env *env,
1064                             const struct cl_io_slice *ios,
1065                             const struct cl_page_slice *slice)
1066 {
1067         struct cl_io              *io     = ios->cis_io;
1068         struct vvp_page           *vpg    = cl2vvp_page(slice);
1069         struct cl_page            *page   = slice->cpl_page;
1070         struct inode              *inode  = vvp_object_inode(slice->cpl_obj);
1071         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1072         struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
1073         struct ll_readahead_state *ras    = &fd->fd_ras;
1074         struct cl_2queue          *queue  = &io->ci_queue;
1075
1076         ENTRY;
1077
1078         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1079             sbi->ll_ra_info.ra_max_pages > 0)
1080                 ras_update(sbi, inode, ras, vvp_index(vpg),
1081                            vpg->vpg_defer_uptodate);
1082
1083         if (vpg->vpg_defer_uptodate) {
1084                 vpg->vpg_ra_used = 1;
1085                 cl_page_export(env, page, 1);
1086         }
1087
1088         /*
1089          * Add page into the queue even when it is marked uptodate above.
1090          * this will unlock it automatically as part of cl_page_list_disown().
1091          */
1092         cl_2queue_add(queue, page);
1093         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1094             sbi->ll_ra_info.ra_max_pages > 0)
1095                 ll_readahead(env, io, &queue->c2_qin, ras,
1096                              vpg->vpg_defer_uptodate);
1097
1098         RETURN(0);
1099 }
1100
1101 static const struct cl_io_operations vvp_io_ops = {
1102         .op = {
1103                 [CIT_READ] = {
1104                         .cio_fini       = vvp_io_fini,
1105                         .cio_lock       = vvp_io_read_lock,
1106                         .cio_start      = vvp_io_read_start,
1107                         .cio_advance    = ccc_io_advance,
1108                 },
1109                 [CIT_WRITE] = {
1110                         .cio_fini      = vvp_io_fini,
1111                         .cio_iter_init = vvp_io_write_iter_init,
1112                         .cio_iter_fini = vvp_io_write_iter_fini,
1113                         .cio_lock      = vvp_io_write_lock,
1114                         .cio_start     = vvp_io_write_start,
1115                         .cio_advance   = ccc_io_advance
1116                 },
1117                 [CIT_SETATTR] = {
1118                         .cio_fini       = vvp_io_setattr_fini,
1119                         .cio_iter_init  = vvp_io_setattr_iter_init,
1120                         .cio_lock       = vvp_io_setattr_lock,
1121                         .cio_start      = vvp_io_setattr_start,
1122                         .cio_end        = vvp_io_setattr_end
1123                 },
1124                 [CIT_FAULT] = {
1125                         .cio_fini      = vvp_io_fault_fini,
1126                         .cio_iter_init = vvp_io_fault_iter_init,
1127                         .cio_lock      = vvp_io_fault_lock,
1128                         .cio_start     = vvp_io_fault_start,
1129                         .cio_end       = ccc_io_end
1130                 },
1131                 [CIT_FSYNC] = {
1132                         .cio_start  = vvp_io_fsync_start,
1133                         .cio_fini   = vvp_io_fini
1134                 },
1135                 [CIT_MISC] = {
1136                         .cio_fini   = vvp_io_fini
1137                 }
1138         },
1139         .cio_read_page     = vvp_io_read_page,
1140 };
1141
1142 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1143                 struct cl_io *io)
1144 {
1145         struct vvp_io      *vio   = vvp_env_io(env);
1146         struct ccc_io      *cio   = ccc_env_io(env);
1147         struct inode       *inode = vvp_object_inode(obj);
1148         int                 result;
1149
1150         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1151         ENTRY;
1152
1153         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1154                            "restore needed %d\n",
1155                PFID(lu_object_fid(&obj->co_lu)),
1156                io->ci_ignore_layout, io->ci_verify_layout,
1157                cio->cui_layout_gen, io->ci_restore_needed);
1158
1159         CL_IO_SLICE_CLEAN(cio, cui_cl);
1160         cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1161         vio->cui_ra_valid = false;
1162         result = 0;
1163         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1164                 size_t count;
1165                 struct ll_inode_info *lli = ll_i2info(inode);
1166
1167                 count = io->u.ci_rw.crw_count;
1168                 /* "If nbyte is 0, read() will return 0 and have no other
1169                  *  results."  -- Single Unix Spec */
1170                 if (count == 0)
1171                         result = 1;
1172                 else {
1173                         cio->cui_tot_count = count;
1174                         cio->cui_tot_nrsegs = 0;
1175                 }
1176
1177                 /* for read/write, we store the jobid in the inode, and
1178                  * it'll be fetched by osc when building RPC.
1179                  *
1180                  * it's not accurate if the file is shared by different
1181                  * jobs.
1182                  */
1183                 lustre_get_jobid(lli->lli_jobid);
1184         } else if (io->ci_type == CIT_SETATTR) {
1185                 if (!cl_io_is_trunc(io))
1186                         io->ci_lockreq = CILR_MANDATORY;
1187         }
1188
1189         /* ignore layout change for generic CIT_MISC but not for glimpse.
1190          * io context for glimpse must set ci_verify_layout to true,
1191          * see cl_glimpse_size0() for details. */
1192         if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1193                 io->ci_ignore_layout = 1;
1194
1195         /* Enqueue layout lock and get layout version. We need to do this
1196          * even for operations requiring to open file, such as read and write,
1197          * because it might not grant layout lock in IT_OPEN. */
1198         if (result == 0 && !io->ci_ignore_layout) {
1199                 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1200                 if (result == -ENOENT)
1201                         /* If the inode on MDS has been removed, but the objects
1202                          * on OSTs haven't been destroyed (async unlink), layout
1203                          * fetch will return -ENOENT, we'd ingore this error
1204                          * and continue with dirty flush. LU-3230. */
1205                         result = 0;
1206                 if (result < 0)
1207                         CERROR("%s: refresh file layout " DFID " error %d.\n",
1208                                 ll_get_fsname(inode->i_sb, NULL, 0),
1209                                 PFID(lu_object_fid(&obj->co_lu)), result);
1210         }
1211
1212         RETURN(result);
1213 }
1214
1215 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1216                                 const struct cl_io_slice *slice)
1217 {
1218         /* Caling just for assertion */
1219         cl2ccc_io(env, slice);
1220         return vvp_env_io(env);
1221 }