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