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