Whamcloud - gitweb
LU-3254 llite: Update layout_gen only if layout change succeed
[fs/lustre-release.git] / lustre / llite / vvp_io.c
index 7adac17..e95edcf 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -36,6 +36,7 @@
  * Implementation of cl_io for VVP layer.
  *
  *   Author: Nikita Danilov <nikita.danilov@sun.com>
+ *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
  */
 
 #define DEBUG_SUBSYSTEM S_LLITE
 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
                                 const struct cl_io_slice *slice);
 
+/**
+ * True, if \a io is a normal io, False for splice_{read,write}
+ */
+int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
+{
+        struct vvp_io *vio = vvp_env_io(env);
+
+        LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
+
+        return vio->cui_io_subtype == IO_NORMAL;
+}
+
+/**
+ * For swapping layout. The file's layout may have changed.
+ * To avoid populating pages to a wrong stripe, we have to verify the
+ * correctness of layout. It works because swapping layout processes
+ * have to acquire group lock.
+ */
+static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
+                               struct inode *inode)
+{
+       struct ll_inode_info    *lli = ll_i2info(inode);
+       struct ccc_io           *cio = ccc_env_io(env);
+       bool rc = true;
+
+       switch (io->ci_type) {
+       case CIT_READ:
+       case CIT_WRITE:
+               /* don't need lock here to check lli_layout_gen as we have held
+                * extent lock and GROUP lock has to hold to swap layout */
+               if (ll_layout_version_get(lli) != cio->cui_layout_gen) {
+                       io->ci_need_restart = 1;
+                       /* this will return application a short read/write */
+                       io->ci_continue = 0;
+                       rc = false;
+               }
+       case CIT_FAULT:
+               /* fault is okay because we've already had a page. */
+       default:
+               break;
+       }
+
+       return rc;
+}
+
 /*****************************************************************************
  *
  * io operations.
  *
  */
 
+static int vvp_io_write_iter_init(const struct lu_env *env,
+                                 const struct cl_io_slice *ios)
+{
+       struct ccc_io *cio = cl2ccc_io(env, ios);
+
+       cl_page_list_init(&cio->u.write.cui_queue);
+       cio->u.write.cui_written = 0;
+       cio->u.write.cui_from = 0;
+       cio->u.write.cui_to = PAGE_SIZE;
+
+       return 0;
+}
+
+static void vvp_io_write_iter_fini(const struct lu_env *env,
+                                  const struct cl_io_slice *ios)
+{
+       struct ccc_io *cio = cl2ccc_io(env, ios);
+
+       LASSERT(cio->u.write.cui_queue.pl_nr == 0);
+}
+
 static int vvp_io_fault_iter_init(const struct lu_env *env,
                                   const struct cl_io_slice *ios)
 {
@@ -72,20 +139,64 @@ static int vvp_io_fault_iter_init(const struct lu_env *env,
 
 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
 {
-        struct cl_io     *io  = ios->cis_io;
-        struct cl_object *obj = io->ci_obj;
-
-        CLOBINVRNT(env, obj, ccc_object_invariant(obj));
-        if (io->ci_type == CIT_WRITE)
-                up(&ll_i2info(ccc_object_inode(obj))->lli_write_sem);
-        else {
-                struct vvp_io     *vio  = cl2vvp_io(env, ios);
-                struct ccc_io     *cio  = cl2ccc_io(env, ios);
-
-                if (vio->cui_ra_window_set)
-                        ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
-        }
-
+       struct cl_io     *io  = ios->cis_io;
+       struct cl_object *obj = io->ci_obj;
+       struct ccc_io    *cio = cl2ccc_io(env, ios);
+       struct inode     *inode = ccc_object_inode(obj);
+
+       CLOBINVRNT(env, obj, ccc_object_invariant(obj));
+
+       CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
+                          "restore needed %d\n",
+              PFID(lu_object_fid(&obj->co_lu)),
+              io->ci_ignore_layout, io->ci_verify_layout,
+              cio->cui_layout_gen, io->ci_restore_needed);
+
+       if (io->ci_restore_needed == 1) {
+               int     rc;
+
+               /* file was detected release, we need to restore it
+                * before finishing the io
+                */
+               rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
+               /* if restore registration failed, no restart,
+                * we will return -ENODATA */
+               /* The layout will change after restore, so we need to
+                * block on layout lock hold by the MDT
+                * as MDT will not send new layout in lvb (see LU-3124)
+                * we have to explicitly fetch it, all this will be done
+                * by ll_layout_refresh()
+                */
+               if (rc == 0) {
+                       io->ci_restore_needed = 0;
+                       io->ci_need_restart = 1;
+                       io->ci_verify_layout = 1;
+               } else {
+                       io->ci_restore_needed = 1;
+                       io->ci_need_restart = 0;
+                       io->ci_verify_layout = 0;
+                       io->ci_result = rc;
+               }
+       }
+
+       if (!io->ci_ignore_layout && io->ci_verify_layout) {
+               __u32 gen = 0;
+
+               /* check layout version */
+               ll_layout_refresh(inode, &gen);
+               io->ci_need_restart = cio->cui_layout_gen != gen;
+               if (io->ci_need_restart) {
+                       CDEBUG(D_VFSTRACE,
+                              DFID" layout changed from %d to %d.\n",
+                              PFID(lu_object_fid(&obj->co_lu)),
+                              cio->cui_layout_gen, gen);
+                       /* today successful restore is the only possible
+                        * case */
+                       /* restore was done, clear restoring state */
+                       ll_i2info(ccc_object_inode(obj))->lli_flags &=
+                               ~LLIF_FILE_RESTORING;
+               }
+       }
 }
 
 static void vvp_io_fault_fini(const struct lu_env *env,
@@ -104,7 +215,7 @@ static void vvp_io_fault_fini(const struct lu_env *env,
         vvp_io_fini(env, ios);
 }
 
-enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
+static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
 {
         /*
          * we only want to hold PW locks if the mmap() can generate
@@ -120,10 +231,10 @@ static int vvp_mmap_locks(const struct lu_env *env,
                           struct ccc_io *vio, struct cl_io *io)
 {
         struct ccc_thread_info *cti = ccc_env_info(env);
+        struct mm_struct       *mm = current->mm;
         struct vm_area_struct  *vma;
         struct cl_lock_descr   *descr = &cti->cti_descr;
         ldlm_policy_data_t      policy;
-        struct inode           *inode;
         unsigned long           addr;
         unsigned long           seg;
         ssize_t                 count;
@@ -132,7 +243,14 @@ static int vvp_mmap_locks(const struct lu_env *env,
 
         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
 
-        if (cl_io_is_sendfile(io))
+        if (!cl_is_normalio(env, io))
+                RETURN(0);
+
+        if (vio->cui_iov == NULL) /* nfs or loop back device write */
+                RETURN(0);
+
+        /* No MM (e.g. NFS)? No vmas too. */
+        if (mm == NULL)
                 RETURN(0);
 
         for (seg = 0; seg < vio->cui_nrsegs; seg++) {
@@ -145,17 +263,19 @@ static int vvp_mmap_locks(const struct lu_env *env,
 
                 count += addr & (~CFS_PAGE_MASK);
                 addr &= CFS_PAGE_MASK;
-                while((vma = our_vma(addr, count)) != NULL) {
-                        struct file *file = vma->vm_file;
-                        struct ll_file_data *fd;
 
-                        LASSERT(file);
-                        fd = LUSTRE_FPRIVATE(file);
+                down_read(&mm->mmap_sem);
+                while((vma = our_vma(mm, addr, count)) != NULL) {
+                        struct inode *inode = vma->vm_file->f_dentry->d_inode;
+                        int flags = CEF_MUST;
 
-                        inode = file->f_dentry->d_inode;
-                        if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK ||
-                            ll_i2sbi(inode)->ll_flags & LL_SBI_NOLCK))
-                                goto cont;
+                        if (ll_file_nolock(vma->vm_file)) {
+                                /*
+                                 * For no lock case, a lockless lock will be
+                                 * generated.
+                                 */
+                                flags = CEF_NEVER;
+                        }
 
                         /*
                          * XXX: Required lock mode can be weakened: CIT_WRITE
@@ -169,50 +289,27 @@ static int vvp_mmap_locks(const struct lu_env *env,
                                                     policy.l_extent.start);
                         descr->cld_end = cl_index(descr->cld_obj,
                                                   policy.l_extent.end);
-                        result = cl_io_lock_alloc_add(env, io, descr, CEF_MUST);
+                        descr->cld_enq_flags = flags;
+                        result = cl_io_lock_alloc_add(env, io, descr);
+
+                        CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
+                               descr->cld_mode, descr->cld_start,
+                               descr->cld_end);
+
                         if (result < 0)
                                 RETURN(result);
 
-                cont:
                         if (vma->vm_end - addr >= count)
                                 break;
+
                         count -= vma->vm_end - addr;
                         addr = vma->vm_end;
                 }
+                up_read(&mm->mmap_sem);
         }
         RETURN(0);
 }
 
-static void vvp_io_update_iov(const struct lu_env *env,
-                              struct ccc_io *vio, struct cl_io *io)
-{
-        int i;
-        size_t size = io->u.ci_rw.crw_count;
-
-        vio->cui_iov_olen = 0;
-        if (cl_io_is_sendfile(io) || size == vio->cui_tot_count)
-                return;
-
-        if (vio->cui_tot_nrsegs == 0)
-                vio->cui_tot_nrsegs =  vio->cui_nrsegs;
-
-        for (i = 0; i < vio->cui_tot_nrsegs; i++) {
-                struct iovec *iv = &vio->cui_iov[i];
-
-                if (iv->iov_len < size)
-                        size -= iv->iov_len;
-                else {
-                        if (iv->iov_len > size) {
-                                vio->cui_iov_olen = iv->iov_len;
-                                iv->iov_len = size;
-                        }
-                        break;
-                }
-        }
-
-        vio->cui_nrsegs = i + 1;
-}
-
 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
                           enum cl_lock_mode mode, loff_t start, loff_t end)
 {
@@ -221,10 +318,9 @@ static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
         int ast_flags = 0;
 
         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
-        LASSERT(vvp_env_io(env)->cui_oneshot == 0);
         ENTRY;
 
-        vvp_io_update_iov(env, cio, io);
+        ccc_io_update_iov(env, cio, io);
 
         if (io->u.ci_rw.crw_nonblock)
                 ast_flags |= CEF_NONBLOCK;
@@ -243,7 +339,7 @@ static int vvp_io_read_lock(const struct lu_env *env,
 
         ENTRY;
         /* XXX: Layer violation, we shouldn't see lsm at llite level. */
-        if (lli->lli_smd != NULL) /* lsm-less file, don't need to lock */
+       if (lli->lli_has_smd) /* lsm-less file doesn't need to lock */
                 result = vvp_io_rw_lock(env, io, CLM_READ,
                                         io->u.ci_rd.rd.crw_pos,
                                         io->u.ci_rd.rd.crw_pos +
@@ -272,174 +368,142 @@ static int vvp_io_write_lock(const struct lu_env *env,
         struct cl_io *io = ios->cis_io;
         loff_t start;
         loff_t end;
-        int    result;
-
-        if (cl2vvp_io(env, ios)->cui_oneshot == 0) {
-                if (io->u.ci_wr.wr_append) {
-                        start = 0;
-                        end   = OBD_OBJECT_EOF;
-                } else {
-                        start = io->u.ci_wr.wr.crw_pos;
-                        end   = start + io->u.ci_wr.wr.crw_count - 1;
-                }
-                result = vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
-        } else
-                result = 0;
-        return result;
+
+        if (io->u.ci_wr.wr_append) {
+                start = 0;
+                end   = OBD_OBJECT_EOF;
+        } else {
+                start = io->u.ci_wr.wr.crw_pos;
+                end   = start + io->u.ci_wr.wr.crw_count - 1;
+        }
+        return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
 }
 
-static int vvp_io_trunc_iter_init(const struct lu_env *env,
-                                  const struct cl_io_slice *ios)
+static int vvp_io_setattr_iter_init(const struct lu_env *env,
+                                   const struct cl_io_slice *ios)
 {
-        struct ccc_io *vio   = cl2ccc_io(env, ios);
-        struct inode  *inode = ccc_object_inode(ios->cis_obj);
-
-        /*
-         * We really need to get our PW lock before we change inode->i_size.
-         * If we don't we can race with other i_size updaters on our node,
-         * like ll_file_read.  We can also race with i_size propogation to
-         * other nodes through dirtying and writeback of final cached pages.
-         * This last one is especially bad for racing o_append users on other
-         * nodes.
-         */
-
-        UNLOCK_INODE_MUTEX(inode);
-        UP_WRITE_I_ALLOC_SEM(inode);
-        vio->u.trunc.cui_locks_released = 1;
-        return 0;
+       return 0;
 }
 
 /**
- * Implementation of cl_io_operations::vio_lock() method for CIT_TRUNC io.
+ * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
  *
  * Handles "lockless io" mode when extent locking is done by server.
  */
-static int vvp_io_trunc_lock(const struct lu_env *env,
-                             const struct cl_io_slice *ios)
+static int vvp_io_setattr_lock(const struct lu_env *env,
+                               const struct cl_io_slice *ios)
 {
-        struct ccc_io     *vio   = cl2ccc_io(env, ios);
-        struct cl_io      *io    = ios->cis_io;
-        size_t new_size          = io->u.ci_truncate.tr_size;
-        __u32 enqflags = new_size == 0 ? CEF_DISCARD_DATA : 0;
-        int result;
-
-        vio->u.trunc.cui_local_lock = TRUNC_EXTENT;
-        result = ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
-                                 new_size, OBD_OBJECT_EOF);
-        return result;
+       struct ccc_io *cio = ccc_env_io(env);
+       struct cl_io  *io  = ios->cis_io;
+       __u64 new_size;
+       __u32 enqflags = 0;
+
+        if (cl_io_is_trunc(io)) {
+                new_size = io->u.ci_setattr.sa_attr.lvb_size;
+                if (new_size == 0)
+                        enqflags = CEF_DISCARD_DATA;
+        } else {
+                if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
+                     io->u.ci_setattr.sa_attr.lvb_ctime) ||
+                    (io->u.ci_setattr.sa_attr.lvb_atime >=
+                     io->u.ci_setattr.sa_attr.lvb_ctime))
+                        return 0;
+                new_size = 0;
+        }
+        cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
+        return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
+                               new_size, OBD_OBJECT_EOF);
 }
 
-static int vvp_io_trunc_start(const struct lu_env *env,
-                              const struct cl_io_slice *ios)
+static int vvp_do_vmtruncate(struct inode *inode, size_t size)
 {
-        struct ccc_io        *cio   = cl2ccc_io(env, ios);
-        struct vvp_io        *vio   = cl2vvp_io(env, ios);
-        struct cl_io         *io    = ios->cis_io;
-        struct inode         *inode = ccc_object_inode(io->ci_obj);
-        struct cl_object     *obj   = ios->cis_obj;
-        size_t                size  = io->u.ci_truncate.tr_size;
-        pgoff_t               start = cl_index(obj, size);
-        int                   result;
-
-        LASSERT(cio->u.trunc.cui_locks_released);
-        LASSERT(vio->cui_oneshot == 0);
-
-        LOCK_INODE_MUTEX(inode);
-        DOWN_WRITE_I_ALLOC_SEM(inode);
-        cio->u.trunc.cui_locks_released = 0;
-
-        /*
-         * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
-         * is grabbed by ll_truncate() only over call to obd_adjust_kms().  If
-         * vmtruncate returns 0, then ll_truncate dropped ll_inode_size_lock()
-         */
-        ll_inode_size_lock(inode, 0);
-        result = vmtruncate(inode, size);
-        if (result != 0)
-                ll_inode_size_unlock(inode, 0);
-        /*
-         * If a page is partially truncated, keep it owned across truncate to
-         * prevent... races.
-         *
-         * XXX this properly belongs to osc, because races in question are OST
-         * specific.
-         */
-        if (cl_offset(obj, start) != size) {
-                struct cl_object_header *hdr;
-
-                hdr = cl_object_header(obj);
-                spin_lock(&hdr->coh_page_guard);
-                vio->cui_partpage = cl_page_lookup(hdr, start);
-                spin_unlock(&hdr->coh_page_guard);
-
-                if (vio->cui_partpage != NULL)
-                        /*
-                         * Wait for the transfer completion for a partially
-                         * truncated page to avoid dead-locking an OST with
-                         * the concurrent page-wise overlapping WRITE and
-                         * PUNCH requests. BUG:17397.
-                         *
-                         * Partial page is disowned in vvp_io_trunc_end().
-                         */
-                        cl_page_own(env, io, vio->cui_partpage);
-        } else
-                vio->cui_partpage = NULL;
-        return result;
+       int     result;
+
+       /*
+        * Only ll_inode_size_lock is taken at this level.
+        */
+       ll_inode_size_lock(inode);
+       result = inode_newsize_ok(inode, size);
+       if (result < 0) {
+               ll_inode_size_unlock(inode);
+               return result;
+       }
+       i_size_write(inode, size);
+
+       ll_truncate_pagecache(inode, size);
+       ll_inode_size_unlock(inode);
+       return result;
 }
 
-static void vvp_io_trunc_end(const struct lu_env *env,
-                             const struct cl_io_slice *ios)
+static int vvp_io_setattr_trunc(const struct lu_env *env,
+                                const struct cl_io_slice *ios,
+                                struct inode *inode, loff_t size)
 {
-        struct vvp_io *vio = cl2vvp_io(env, ios);
-
-        if (vio->cui_partpage != NULL) {
-                cl_page_disown(env, ios->cis_io, vio->cui_partpage);
-                cl_page_put(env, vio->cui_partpage);
-                vio->cui_partpage = NULL;
-        }
+       inode_dio_wait(inode);
+       return 0;
 }
 
-static void vvp_io_trunc_fini(const struct lu_env *env,
-                              const struct cl_io_slice *ios)
+static int vvp_io_setattr_time(const struct lu_env *env,
+                               const struct cl_io_slice *ios)
 {
-        struct ccc_io *cio   = ccc_env_io(env);
-        struct inode  *inode = ccc_object_inode(ios->cis_io->ci_obj);
+        struct cl_io       *io    = ios->cis_io;
+        struct cl_object   *obj   = io->ci_obj;
+        struct cl_attr     *attr  = ccc_env_thread_attr(env);
+        int result;
+        unsigned valid = CAT_CTIME;
 
-        if (cio->u.trunc.cui_locks_released) {
-                LOCK_INODE_MUTEX(inode);
-                DOWN_WRITE_I_ALLOC_SEM(inode);
-                cio->u.trunc.cui_locks_released = 0;
+        cl_object_attr_lock(obj);
+        attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
+        if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
+                attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
+                valid |= CAT_ATIME;
         }
-        vvp_io_fini(env, ios);
-}
+        if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
+                attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
+                valid |= CAT_MTIME;
+        }
+        result = cl_object_attr_set(env, obj, attr, valid);
+        cl_object_attr_unlock(obj);
 
-#ifdef HAVE_FILE_READV
-static ssize_t lustre_generic_file_read(struct file *file,
-                                        struct ccc_io *vio, loff_t *ppos)
-{
-        return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
+        return result;
 }
 
-static ssize_t lustre_generic_file_write(struct file *file,
-                                         struct ccc_io *vio, loff_t *ppos)
+static int vvp_io_setattr_start(const struct lu_env *env,
+                               const struct cl_io_slice *ios)
 {
-        return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
+       struct cl_io    *io    = ios->cis_io;
+       struct inode    *inode = ccc_object_inode(io->ci_obj);
+       int result = 0;
+
+       mutex_lock(&inode->i_mutex);
+       if (cl_io_is_trunc(io))
+               result = vvp_io_setattr_trunc(env, ios, inode,
+                                       io->u.ci_setattr.sa_attr.lvb_size);
+       if (result == 0)
+               result = vvp_io_setattr_time(env, ios);
+       return result;
 }
-#else
-static ssize_t lustre_generic_file_read(struct file *file,
-                                        struct ccc_io *vio, loff_t *ppos)
+
+static void vvp_io_setattr_end(const struct lu_env *env,
+                               const struct cl_io_slice *ios)
 {
-        return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
-                                     vio->cui_nrsegs, *ppos);
+       struct cl_io *io    = ios->cis_io;
+       struct inode *inode = ccc_object_inode(io->ci_obj);
+
+       if (cl_io_is_trunc(io)) {
+               /* Truncate in memory pages - they must be clean pages
+                * because osc has already notified to destroy osc_extents. */
+               vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
+               inode_dio_write_done(inode);
+       }
+       mutex_unlock(&inode->i_mutex);
 }
 
-static ssize_t lustre_generic_file_write(struct file *file,
-                                        struct ccc_io *vio, loff_t *ppos)
+static void vvp_io_setattr_fini(const struct lu_env *env,
+                               const struct cl_io_slice *ios)
 {
-        return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
-                                      vio->cui_nrsegs, *ppos);
+       vvp_io_fini(env, ios);
 }
-#endif
 
 static int vvp_io_read_start(const struct lu_env *env,
                              const struct cl_io_slice *ios)
@@ -454,20 +518,25 @@ static int vvp_io_read_start(const struct lu_env *env,
 
         int     result;
         loff_t  pos = io->u.ci_rd.rd.crw_pos;
-        size_t  cnt = io->u.ci_rd.rd.crw_count;
-        size_t  tot = cio->cui_tot_count;
+        long    cnt = io->u.ci_rd.rd.crw_count;
+        long    tot = cio->cui_tot_count;
+        int     exceed = 0;
 
         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
-        LASSERT(vio->cui_oneshot == 0);
 
         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
 
-        result = ccc_prep_size(env, obj, io, pos + tot - 1, 1);
+       if (!can_populate_pages(env, io, inode))
+               return 0;
+
+        result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
         if (result != 0)
                 return result;
+        else if (exceed != 0)
+                goto out;
 
         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
-                        "Read ino %lu, "LPSZ" bytes, offset %lld, size %llu\n",
+                        "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
                         inode->i_ino, cnt, pos, i_size_read(inode));
 
         /* turn off the kernel's read-ahead */
@@ -477,31 +546,236 @@ static int vvp_io_read_start(const struct lu_env *env,
         if (!vio->cui_ra_window_set) {
                 vio->cui_ra_window_set = 1;
                 bead->lrr_start = cl_index(obj, pos);
-                /*
-                 * XXX: explicit CFS_PAGE_SIZE
-                 */
-                bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
+               bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
                 ll_ra_read_in(file, bead);
         }
 
         /* BUG: 5972 */
         file_accessed(file);
-        if (cl_io_is_sendfile(io)) {
-                result = generic_file_sendfile(file, &pos, cnt,
-                                vio->u.read.cui_actor, vio->u.read.cui_target);
-        } else {
-                result = lustre_generic_file_read(file, cio, &pos);
+        switch (vio->cui_io_subtype) {
+        case IO_NORMAL:
+               LASSERT(cio->cui_iocb->ki_pos == pos);
+               result = generic_file_aio_read(cio->cui_iocb,
+                                              cio->cui_iov, cio->cui_nrsegs,
+                                              cio->cui_iocb->ki_pos);
+               break;
+        case IO_SPLICE:
+                result = generic_file_splice_read(file, &pos,
+                                vio->u.splice.cui_pipe, cnt,
+                                vio->u.splice.cui_flags);
+                /* LU-1109: do splice read stripe by stripe otherwise if it
+                 * may make nfsd stuck if this read occupied all internal pipe
+                 * buffers. */
+                io->ci_continue = 0;
+                break;
+        default:
+                CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
+                LBUG();
         }
 
-        if (result >= 0) {
-                if (result < cnt)
-                        io->ci_continue = 0;
-                io->ci_nob += result;
-                ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
-                                  cio->cui_fd, pos, result, 0);
-                result = 0;
-        }
-        return result;
+out:
+       if (result >= 0) {
+               if (result < cnt)
+                       io->ci_continue = 0;
+               io->ci_nob += result;
+               ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
+                                 pos, result, READ);
+               result = 0;
+       }
+
+       return result;
+}
+
+static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
+{
+       struct vvp_io *vio = cl2vvp_io(env, ios);
+       struct ccc_io *cio = cl2ccc_io(env, ios);
+
+       if (vio->cui_ra_window_set)
+               ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
+
+       vvp_io_fini(env, ios);
+}
+
+static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
+                             struct cl_page_list *plist, int from, int to)
+{
+       struct cl_2queue *queue = &io->ci_queue;
+       struct cl_page *page;
+       unsigned int bytes = 0;
+       int rc = 0;
+       ENTRY;
+
+       if (plist->pl_nr == 0)
+               RETURN(0);
+
+       if (from > 0 || to != PAGE_SIZE) {
+               page = cl_page_list_first(plist);
+               if (plist->pl_nr == 1) {
+                       cl_page_clip(env, page, from, to);
+               } else if (from > 0) {
+                       cl_page_clip(env, page, from, PAGE_SIZE);
+               } else {
+                       page = cl_page_list_last(plist);
+                       cl_page_clip(env, page, 0, to);
+               }
+       }
+
+       cl_2queue_init(queue);
+       cl_page_list_splice(plist, &queue->c2_qin);
+       rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
+
+       /* plist is not sorted any more */
+       cl_page_list_splice(&queue->c2_qin, plist);
+       cl_page_list_splice(&queue->c2_qout, plist);
+       cl_2queue_fini(env, queue);
+
+       if (rc == 0) {
+               /* calculate bytes */
+               bytes = plist->pl_nr << PAGE_SHIFT;
+               bytes -= from + PAGE_SIZE - to;
+
+               while (plist->pl_nr > 0) {
+                       page = cl_page_list_first(plist);
+                       cl_page_list_del(env, plist, page);
+
+                       cl_page_clip(env, page, 0, PAGE_SIZE);
+
+                       SetPageUptodate(cl_page_vmpage(page));
+                       cl_page_disown(env, io, page);
+
+                       /* held in ll_cl_init() */
+                       lu_ref_del(&page->cp_reference, "cl_io", io);
+                       cl_page_put(env, page);
+               }
+       }
+
+       RETURN(bytes > 0 ? bytes : rc);
+}
+
+static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
+                               struct cl_page *page)
+{
+       struct ccc_page *cp;
+       struct page *vmpage = page->cp_vmpage;
+       struct cl_object *clob = cl_io_top(io)->ci_obj;
+
+       SetPageUptodate(vmpage);
+       set_page_dirty(vmpage);
+
+       cp = cl2ccc_page(cl_object_page_slice(clob, page));
+       vvp_write_pending(cl2ccc(clob), cp);
+
+       cl_page_disown(env, io, page);
+
+       /* held in ll_cl_init() */
+       lu_ref_del(&page->cp_reference, "cl_io", io);
+       cl_page_put(env, page);
+}
+
+/* make sure the page list is contiguous */
+static bool page_list_sanity_check(struct cl_object *obj,
+                                  struct cl_page_list *plist)
+{
+       struct cl_page *page;
+       pgoff_t index = CL_PAGE_EOF;
+
+       cl_page_list_for_each(page, plist) {
+               struct ccc_page *cp = cl_object_page_slice(obj, page);
+
+               if (index == CL_PAGE_EOF) {
+                       index = ccc_index(cp);
+                       continue;
+               }
+
+               ++index;
+               if (index == ccc_index(cp))
+                       continue;
+
+               return false;
+       }
+       return true;
+}
+
+/* Return how many bytes have queued or written */
+int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
+{
+       struct cl_object *obj = io->ci_obj;
+       struct inode *inode = ccc_object_inode(obj);
+       struct ccc_io *cio = ccc_env_io(env);
+       struct cl_page_list *queue = &cio->u.write.cui_queue;
+       struct cl_page *page;
+       int rc = 0;
+       int bytes = 0;
+       unsigned int npages = cio->u.write.cui_queue.pl_nr;
+       ENTRY;
+
+       if (npages == 0)
+               RETURN(0);
+
+       CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
+               npages, cio->u.write.cui_from, cio->u.write.cui_to);
+
+       LASSERT(page_list_sanity_check(obj, queue));
+
+       /* submit IO with async write */
+       rc = cl_io_commit_async(env, io, queue,
+                               cio->u.write.cui_from, cio->u.write.cui_to,
+                               write_commit_callback);
+       npages -= queue->pl_nr; /* already committed pages */
+       if (npages > 0) {
+               /* calculate how many bytes were written */
+               bytes = npages << PAGE_SHIFT;
+
+               /* first page */
+               bytes -= cio->u.write.cui_from;
+               if (queue->pl_nr == 0) /* last page */
+                       bytes -= PAGE_SIZE - cio->u.write.cui_to;
+               LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
+
+               cio->u.write.cui_written += bytes;
+
+               CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
+                       npages, bytes, cio->u.write.cui_written);
+
+               /* the first page must have been written. */
+               cio->u.write.cui_from = 0;
+       }
+       LASSERT(page_list_sanity_check(obj, queue));
+       LASSERT(ergo(rc == 0, queue->pl_nr == 0));
+
+       /* out of quota, try sync write */
+       if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
+               rc = vvp_io_commit_sync(env, io, queue,
+                                       cio->u.write.cui_from,
+                                       cio->u.write.cui_to);
+               if (rc > 0) {
+                       cio->u.write.cui_written += rc;
+                       rc = 0;
+               }
+       }
+
+       /* update inode size */
+       ll_merge_lvb(env, inode);
+
+       /* Now the pages in queue were failed to commit, discard them
+        * unless they were dirtied before. */
+       while (queue->pl_nr > 0) {
+               page = cl_page_list_first(queue);
+               cl_page_list_del(env, queue, page);
+
+               if (!PageDirty(cl_page_vmpage(page)))
+                       cl_page_discard(env, io, page);
+
+               cl_page_disown(env, io, page);
+
+               /* held in ll_cl_init() */
+               lu_ref_del(&page->cp_reference, "cl_io", io);
+               cl_page_put(env, page);
+       }
+       cl_page_list_fini(env, queue);
+
+       RETURN(rc);
 }
 
 static int vvp_io_write_start(const struct lu_env *env,
@@ -511,52 +785,124 @@ static int vvp_io_write_start(const struct lu_env *env,
         struct cl_io       *io    = ios->cis_io;
         struct cl_object   *obj   = io->ci_obj;
         struct inode       *inode = ccc_object_inode(obj);
-        struct file        *file  = cio->cui_fd->fd_file;
         ssize_t result = 0;
         loff_t pos = io->u.ci_wr.wr.crw_pos;
         size_t cnt = io->u.ci_wr.wr.crw_count;
 
         ENTRY;
 
-        if (cl_io_is_append(io))
+       if (!can_populate_pages(env, io, inode))
+               RETURN(0);
+
+        if (cl_io_is_append(io)) {
                 /*
                  * PARALLEL IO This has to be changed for parallel IO doing
                  * out-of-order writes.
                  */
                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
+                cio->cui_iocb->ki_pos = pos;
+        } else {
+               LASSERT(cio->cui_iocb->ki_pos == pos);
+       }
 
-        CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + cnt);
+        CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
 
-        if (cl2vvp_io(env, ios)->cui_oneshot > 0)
+        if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
                 result = 0;
         else
-                result = lustre_generic_file_write(file, cio, &pos);
-
-        if (result > 0) {
-                if (result < cnt)
-                        io->ci_continue = 0;
-                io->ci_nob += result;
-                ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
-                                  cio->cui_fd, pos, result, 0);
-                result = 0;
+               result = generic_file_aio_write(cio->cui_iocb,
+                                               cio->cui_iov, cio->cui_nrsegs,
+                                               cio->cui_iocb->ki_pos);
+       if (result > 0) {
+               result = vvp_io_write_commit(env, io);
+               if (cio->u.write.cui_written > 0) {
+                       result = cio->u.write.cui_written;
+                       io->ci_nob += result;
+
+                       CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
+                               io->ci_nob, result);
+               }
+       }
+       if (result > 0) {
+               struct ll_inode_info *lli = ll_i2info(inode);
+
+               spin_lock(&lli->lli_lock);
+               lli->lli_flags |= LLIF_DATA_MODIFIED;
+               spin_unlock(&lli->lli_lock);
+
+               if (result < cnt)
+                       io->ci_continue = 0;
+               ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
+                                 cio->cui_fd, pos, result, WRITE);
+               result = 0;
+       }
+
+       RETURN(result);
+}
+
+static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
+{
+        struct vm_fault *vmf = cfio->fault.ft_vmf;
+
+        cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
+
+        if (vmf->page) {
+                LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
+                               vmf->virtual_address);
+                if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
+                        lock_page(vmf->page);
+                        cfio->fault.ft_flags &= VM_FAULT_LOCKED;
+                }
+
+                cfio->ft_vmpage = vmf->page;
+                return 0;
         }
-        RETURN(result);
+
+        if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
+                CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
+                return -EFAULT;
+        }
+
+        if (cfio->fault.ft_flags & VM_FAULT_OOM) {
+                CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
+                return -ENOMEM;
+        }
+
+        if (cfio->fault.ft_flags & VM_FAULT_RETRY)
+                return -EAGAIN;
+
+        CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
+        return -EINVAL;
+}
+
+static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
+                                   struct cl_page *page)
+{
+       struct ccc_page *cp;
+       struct cl_object *clob = cl_io_top(io)->ci_obj;
+
+       set_page_dirty(page->cp_vmpage);
+
+       cp = cl2ccc_page(cl_object_page_slice(clob, page));
+       vvp_write_pending(cl2ccc(clob), cp);
 }
 
 static int vvp_io_fault_start(const struct lu_env *env,
                               const struct cl_io_slice *ios)
 {
-        struct vvp_io       *vio     = cl2vvp_io(env, ios);
-        struct cl_io        *io      = ios->cis_io;
-        struct cl_object    *obj     = io->ci_obj;
-        struct inode        *inode   = ccc_object_inode(obj);
-        struct cl_fault_io  *fio     = &io->u.ci_fault;
-        struct vvp_fault_io *cfio    = &vio->u.fault;
-        cfs_page_t          *vmpage;
-        loff_t               offset;
-        int                  result  = 0;
-
-        LASSERT(vio->cui_oneshot == 0);
+       struct vvp_io       *vio     = cl2vvp_io(env, ios);
+       struct cl_io        *io      = ios->cis_io;
+       struct cl_object    *obj     = io->ci_obj;
+       struct inode        *inode   = ccc_object_inode(obj);
+       struct cl_fault_io  *fio     = &io->u.ci_fault;
+       struct vvp_fault_io *cfio    = &vio->u.fault;
+       loff_t               offset;
+       int                  result  = 0;
+       struct page          *vmpage  = NULL;
+       struct cl_page      *page;
+       loff_t               size;
+       pgoff_t              last_index;
+       ENTRY;
 
         if (fio->ft_executable &&
             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
@@ -567,376 +913,210 @@ static int vvp_io_fault_start(const struct lu_env *env,
         /* offset of the last byte on the page */
         offset = cl_offset(obj, fio->ft_index + 1) - 1;
         LASSERT(cl_index(obj, offset) == fio->ft_index);
-        result = ccc_prep_size(env, obj, io, offset, 0);
-        if (result != 0)
-                return result;
+        result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
+       if (result != 0)
+               RETURN(result);
+
+       /* must return locked page */
+       if (fio->ft_mkwrite) {
+               LASSERT(cfio->ft_vmpage != NULL);
+               lock_page(cfio->ft_vmpage);
+       } else {
+               result = vvp_io_kernel_fault(cfio);
+               if (result != 0)
+                       RETURN(result);
+       }
+
+       vmpage = cfio->ft_vmpage;
+       LASSERT(PageLocked(vmpage));
+
+       if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
+               ll_invalidate_page(vmpage);
+
+       size = i_size_read(inode);
+        /* Though we have already held a cl_lock upon this page, but
+         * it still can be truncated locally. */
+       if (unlikely((vmpage->mapping != inode->i_mapping) ||
+                    (page_offset(vmpage) > size))) {
+                CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
+
+                /* return +1 to stop cl_io_loop() and ll_fault() will catch
+                 * and retry. */
+                GOTO(out, result = +1);
+        }
 
-        vmpage = filemap_nopage(cfio->ft_vma, cfio->ft_address, cfio->ft_type);
-        if (vmpage != NOPAGE_SIGBUS && vmpage != NOPAGE_OOM)
-                LL_CDEBUG_PAGE(D_PAGE, vmpage,
-                               "got addr %lu type %lx\n",
-                               cfio->ft_address, (long)cfio->ft_type);
+       last_index = cl_index(obj, size - 1);
+
+       if (fio->ft_mkwrite ) {
+               /*
+                * Capture the size while holding the lli_trunc_sem from above
+                * we want to make sure that we complete the mkwrite action
+                * while holding this lock. We need to make sure that we are
+                * not past the end of the file.
+                */
+               if (last_index < fio->ft_index) {
+                       CDEBUG(D_PAGE,
+                               "llite: mkwrite and truncate race happened: "
+                               "%p: 0x%lx 0x%lx\n",
+                               vmpage->mapping,fio->ft_index,last_index);
+                       /*
+                        * We need to return if we are
+                        * passed the end of the file. This will propagate
+                        * up the call stack to ll_page_mkwrite where
+                        * we will return VM_FAULT_NOPAGE. Any non-negative
+                        * value returned here will be silently
+                        * converted to 0. If the vmpage->mapping is null
+                        * the error code would be converted back to ENODATA
+                        * in ll_page_mkwrite0. Thus we return -ENODATA
+                        * to handle both cases
+                        */
+                       GOTO(out, result = -ENODATA);
+               }
+       }
+
+       page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
+       if (IS_ERR(page))
+               GOTO(out, result = PTR_ERR(page));
+
+       /* if page is going to be written, we should add this page into cache
+        * earlier. */
+       if (fio->ft_mkwrite) {
+               wait_on_page_writeback(vmpage);
+               if (!PageDirty(vmpage)) {
+                       struct cl_page_list *plist = &io->ci_queue.c2_qin;
+                       struct ccc_page *cp = cl_object_page_slice(obj, page);
+                       int to = PAGE_SIZE;
+
+                       /* vvp_page_assume() calls wait_on_page_writeback(). */
+                       cl_page_assume(env, io, page);
+
+                       cl_page_list_init(plist);
+                       cl_page_list_add(plist, page);
+
+                       /* size fixup */
+                       if (last_index == ccc_index(cp))
+                               to = size & ~CFS_PAGE_MASK;
+
+                       /* Do not set Dirty bit here so that in case IO is
+                        * started before the page is really made dirty, we
+                        * still have chance to detect it. */
+                       result = cl_io_commit_async(env, io, plist, 0, to,
+                                                   mkwrite_commit_callback);
+                       LASSERT(cl_page_is_owned(page, io));
+                       cl_page_list_fini(env, plist);
+
+                       vmpage = NULL;
+                       if (result < 0) {
+                               cl_page_discard(env, io, page);
+                               cl_page_disown(env, io, page);
+
+                               cl_page_put(env, page);
+
+                               /* we're in big trouble, what can we do now? */
+                               if (result == -EDQUOT)
+                                       result = -ENOSPC;
+                               GOTO(out, result);
+                       } else
+                               cl_page_disown(env, io, page);
+               }
+       }
+
+       /*
+        * The ft_index is only used in the case of
+        * a mkwrite action. We need to check
+        * our assertions are correct, since
+        * we should have caught this above
+        */
+       LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
+       if (fio->ft_index == last_index)
+                /*
+                 * Last page is mapped partially.
+                 */
+                fio->ft_nob = size - cl_offset(obj, fio->ft_index);
         else
-                CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
-                       cfio->ft_address, (long)cfio->ft_type);
-
-        if (vmpage == NOPAGE_SIGBUS)
-                result = -EFAULT;
-        else if (vmpage == NOPAGE_OOM)
-                result = -ENOMEM;
-        else {
-                struct cl_page *page;
-                loff_t          size;
-                pgoff_t         last; /* last page in a file data region */
-
-                /* Temporarily lock vmpage to keep cl_page_find() happy. */
-                lock_page(vmpage);
-                page = cl_page_find(env, obj, fio->ft_index, vmpage,
-                                    CPT_CACHEABLE);
-                unlock_page(vmpage);
-                if (!IS_ERR(page)) {
-                        size = i_size_read(inode);
-                        last = cl_index(obj, size - 1);
-                        if (fio->ft_index == last)
-                                /*
-                                 * Last page is mapped partially.
-                                 */
-                                fio->ft_nob = size - cl_offset(obj,
-                                                               fio->ft_index);
-                        else
-                                fio->ft_nob = cl_page_size(obj);
-                        lu_ref_add(&page->cp_reference, "fault", io);
-                        fio->ft_page = page;
-                        /*
-                         * Certain 2.6 kernels return not-NULL from
-                         * filemap_nopage() when page is beyond the file size,
-                         * on the grounds that "An external ptracer can access
-                         * pages that normally aren't accessible.." Don't
-                         * propagate such page fault to the lower layers to
-                         * avoid side-effects like KMS updates.
-                         */
-                        if (fio->ft_index > last)
-                                result = +1;
-                } else
-                        result = PTR_ERR(page);
-        }
-        return result;
+                fio->ft_nob = cl_page_size(obj);
+
+        lu_ref_add(&page->cp_reference, "fault", io);
+        fio->ft_page = page;
+        EXIT;
+
+out:
+       /* return unlocked vmpage to avoid deadlocking */
+       if (vmpage != NULL)
+               unlock_page(vmpage);
+       cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
+       return result;
 }
 
-static void vvp_io_advance(const struct lu_env *env,
-                           const struct cl_io_slice *ios, size_t nob)
+static int vvp_io_fsync_start(const struct lu_env *env,
+                             const struct cl_io_slice *ios)
 {
-        struct ccc_io    *vio = cl2ccc_io(env, ios);
-        struct cl_io     *io  = ios->cis_io;
-        struct cl_object *obj = ios->cis_io->ci_obj;
-
-        CLOBINVRNT(env, obj, ccc_object_invariant(obj));
-
-        if (!cl_io_is_sendfile(io) && io->ci_continue) {
-                /* update the iov */
-                LASSERT(vio->cui_tot_nrsegs >= vio->cui_nrsegs);
-                LASSERT(vio->cui_tot_count  >= nob);
-
-                vio->cui_iov        += vio->cui_nrsegs;
-                vio->cui_tot_nrsegs -= vio->cui_nrsegs;
-                vio->cui_tot_count  -= nob;
-
-                if (vio->cui_iov_olen) {
-                        struct iovec *iv;
-
-                        vio->cui_iov--;
-                        vio->cui_tot_nrsegs++;
-                        iv = &vio->cui_iov[0];
-                        iv->iov_base += iv->iov_len;
-                        LASSERT(vio->cui_iov_olen > iv->iov_len);
-                        iv->iov_len = vio->cui_iov_olen - iv->iov_len;
-                }
-        }
+       /* we should mark TOWRITE bit to each dirty page in radix tree to
+        * verify pages have been written, but this is difficult because of
+        * race. */
+       return 0;
 }
 
 static int vvp_io_read_page(const struct lu_env *env,
                             const struct cl_io_slice *ios,
                             const struct cl_page_slice *slice)
 {
-        struct cl_io              *io     = ios->cis_io;
-        struct cl_object          *obj    = slice->cpl_obj;
-        struct ccc_page           *cp     = cl2ccc_page(slice);
-        struct cl_page            *page   = slice->cpl_page;
-        struct inode              *inode  = ccc_object_inode(obj);
-        struct ll_sb_info         *sbi    = ll_i2sbi(inode);
-        struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
-        struct ll_readahead_state *ras    = &fd->fd_ras;
-        cfs_page_t                *vmpage = cp->cpg_page;
-        struct cl_2queue          *queue  = &io->ci_queue;
-
-        CLOBINVRNT(env, obj, ccc_object_invariant(obj));
-        LASSERT(cl2vvp_io(env, ios)->cui_oneshot == 0);
-        LASSERT(slice->cpl_obj == obj);
-
-        ENTRY;
-
-        if (sbi->ll_ra_info.ra_max_pages)
-                ras_update(sbi, inode, ras, page->cp_index,
-                           cp->cpg_defer_uptodate);
-
-        /* Sanity check whether the page is protected by a lock. */
-        if (likely(!(fd->fd_flags & LL_FILE_IGNORE_LOCK))) {
-                int rc;
-
-                rc = cl_page_is_under_lock(env, io, page);
-                if (rc != -EBUSY) {
-                        CL_PAGE_HEADER(D_WARNING, env, page, "%s: %i\n",
-                                       rc == -ENODATA ? "without a lock" :
-                                       "match failed", rc);
-                        if (rc != -ENODATA)
-                                RETURN(rc);
-                }
-        }
+       struct cl_io              *io     = ios->cis_io;
+       struct ccc_page           *cp     = cl2ccc_page(slice);
+       struct cl_page            *page   = slice->cpl_page;
+       struct inode              *inode  = ccc_object_inode(slice->cpl_obj);
+       struct ll_sb_info         *sbi    = ll_i2sbi(inode);
+       struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
+       struct ll_readahead_state *ras    = &fd->fd_ras;
+       struct cl_2queue          *queue  = &io->ci_queue;
+
+       ENTRY;
+
+       if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
+           sbi->ll_ra_info.ra_max_pages > 0)
+               ras_update(sbi, inode, ras, ccc_index(cp),
+                          cp->cpg_defer_uptodate);
 
         if (cp->cpg_defer_uptodate) {
                 cp->cpg_ra_used = 1;
-                cl_page_export(env, page);
+                cl_page_export(env, page, 1);
         }
+
         /*
          * Add page into the queue even when it is marked uptodate above.
          * this will unlock it automatically as part of cl_page_list_disown().
          */
         cl_2queue_add(queue, page);
-        if (sbi->ll_ra_info.ra_max_pages)
-                ll_readahead(env, io, ras,
-                             vmpage->mapping, &queue->c2_qin, fd->fd_flags);
-
-        RETURN(0);
-}
-
-static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
-                            struct cl_page *page, struct ccc_page *cp,
-                            int to, enum cl_req_type crt)
-{
-        struct cl_2queue  *queue;
-        struct ccc_object *cobo   = cl2ccc(page->cp_obj);
-        struct cl_sync_io *anchor = &ccc_env_info(env)->cti_sync_io;
-
-        int writing = io->ci_type == CIT_WRITE;
-        int result;
-
-        LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
-
-        queue = &io->ci_queue;
-
-        cl_2queue_init_page(queue, page);
-
-        if (writing)
-                /* Do not pass llap here as it is sync write. */
-                vvp_write_pending(cobo, cp);
-
-        cl_sync_io_init(anchor, 1);
-        cp->cpg_sync_io = anchor;
-        cl_page_clip(env, page, 0, to);
-        result = cl_io_submit_rw(env, io, crt, queue, CRP_NORMAL);
-        if (result == 0)
-                result = cl_sync_io_wait(env, io, &queue->c2_qout, anchor);
-        else
-                cp->cpg_sync_io = NULL;
-        LASSERT(cl_page_is_owned(page, io));
-        cl_page_clip(env, page, 0, CFS_PAGE_SIZE);
-
-        if (crt == CRT_READ)
-                /*
-                 * in CRT_WRITE case page is left locked even in case of
-                 * error.
-                 */
-                cl_page_list_disown(env, io, &queue->c2_qin);
-        cl_2queue_fini(env, queue);
-
-        return result;
-}
-
-/**
- * Prepare partially written-to page for a write.
- */
-static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
-                                  struct cl_object *obj, struct cl_page *pg,
-                                  struct ccc_page *cp,
-                                  unsigned from, unsigned to)
-{
-        struct cl_attr *attr   = &ccc_env_info(env)->cti_attr;
-        loff_t          offset = cl_offset(obj, pg->cp_index);
-        int             result;
-
-        cl_object_attr_lock(obj);
-        result = cl_object_attr_get(env, obj, attr);
-        cl_object_attr_unlock(obj);
-        if (result == 0) {
-                /*
-                 * If are writing to a new page, no need to read old data.
-                 * The extent locking will have updated the KMS, and for our
-                 * purposes here we can treat it like i_size.
-                 */
-                if (attr->cat_kms <= offset) {
-                        char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
-
-                        memset(kaddr, 0, cl_page_size(obj));
-                        kunmap_atomic(kaddr, KM_USER0);
-                } else if (cp->cpg_defer_uptodate)
-                        cp->cpg_ra_used = 1;
-                else
-                        result = vvp_page_sync_io(env, io, pg, cp,
-                                                  CFS_PAGE_SIZE, CRT_READ);
-                /*
-                 * In older implementations, obdo_refresh_inode is called here
-                 * to update the inode because the write might modify the
-                 * object info at OST. However, this has been proven useless,
-                 * since LVB functions will be called when user space program
-                 * tries to retrieve inode attribute.  Also, see bug 15909 for
-                 * details. -jay
-                 */
-                if (result == 0)
-                        cl_page_export(env, pg);
-        }
-        return result;
-}
-
-static int vvp_io_prepare_write(const struct lu_env *env,
-                                const struct cl_io_slice *ios,
-                                const struct cl_page_slice *slice,
-                                unsigned from, unsigned to)
-{
-        struct cl_object *obj    = slice->cpl_obj;
-        struct ccc_page  *cp     = cl2ccc_page(slice);
-        struct cl_page   *pg     = slice->cpl_page;
-        cfs_page_t       *vmpage = cp->cpg_page;
-
-        int result;
+       if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
+           sbi->ll_ra_info.ra_max_pages > 0)
+               ll_readahead(env, io, &queue->c2_qin, ras,
+                            cp->cpg_defer_uptodate);
 
-        ENTRY;
-
-        LINVRNT(cl_page_is_vmlocked(env, pg));
-        LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
-
-        result = 0;
-
-        CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
-        if (!PageUptodate(vmpage)) {
-                /*
-                 * We're completely overwriting an existing page, so _don't_
-                 * set it up to date until commit_write
-                 */
-                if (from == 0 && to == CFS_PAGE_SIZE) {
-                        CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
-                        POISON_PAGE(page, 0x11);
-                } else
-                        result = vvp_io_prepare_partial(env, ios->cis_io, obj,
-                                                        pg, cp, from, to);
-        } else
-                CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
-        RETURN(result);
-}
-
-static int vvp_io_commit_write(const struct lu_env *env,
-                               const struct cl_io_slice *ios,
-                               const struct cl_page_slice *slice,
-                               unsigned from, unsigned to)
-{
-        struct cl_object  *obj    = slice->cpl_obj;
-        struct cl_io      *io     = ios->cis_io;
-        struct ccc_page   *cp     = cl2ccc_page(slice);
-        struct cl_page    *pg     = slice->cpl_page;
-        struct inode      *inode  = ccc_object_inode(obj);
-        struct ll_sb_info *sbi    = ll_i2sbi(inode);
-        cfs_page_t        *vmpage = cp->cpg_page;
-
-        int    result;
-        int    tallyop;
-        loff_t size;
-
-        ENTRY;
-
-        LINVRNT(cl_page_is_vmlocked(env, pg));
-        LASSERT(vmpage->mapping->host == inode);
-
-        LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
-        CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
-
-        /*
-         * queue a write for some time in the future the first time we
-         * dirty the page.
-         *
-         * This is different from what other file systems do: they usually
-         * just mark page (and some of its buffers) dirty and rely on
-         * balance_dirty_pages() to start a write-back. Lustre wants write-back
-         * to be started earlier for the following reasons:
-         *
-         *     (1) with a large number of clients we need to limit the amount
-         *     of cached data on the clients a lot;
-         *
-         *     (2) large compute jobs generally want compute-only then io-only
-         *     and the IO should complete as quickly as possible;
-         *
-         *     (3) IO is batched up to the RPC size and is async until the
-         *     client max cache is hit
-         *     (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
-         *
-         */
-        if (!PageDirty(vmpage)) {
-                tallyop = LPROC_LL_DIRTY_MISSES;
-                vvp_write_pending(cl2ccc(obj), cp);
-                set_page_dirty(vmpage);
-                result = cl_page_cache_add(env, io, pg, CRT_WRITE);
-                if (result == -EDQUOT)
-                        /*
-                         * Client ran out of disk space grant. Possible
-                         * strategies are:
-                         *
-                         *     (a) do a sync write, renewing grant;
-                         *
-                         *     (b) stop writing on this stripe, switch to the
-                         *     next one.
-                         *
-                         * (b) is a part of "parallel io" design that is the
-                         * ultimate goal. (a) is what "old" client did, and
-                         * what the new code continues to do for the time
-                         * being.
-                         */
-                        result = vvp_page_sync_io(env, io, pg, cp,
-                                                  to, CRT_WRITE);
-        } else {
-                tallyop = LPROC_LL_DIRTY_HITS;
-                result = 0;
-        }
-        ll_stats_ops_tally(sbi, tallyop, 1);
-
-        size = cl_offset(obj, pg->cp_index) + to;
-
-        if (result == 0) {
-                if (size > i_size_read(inode))
-                        i_size_write(inode, size);
-                cl_page_export(env, pg);
-        } else if (size > i_size_read(inode))
-                cl_page_discard(env, io, pg);
-        RETURN(result);
+       RETURN(0);
 }
 
 static const struct cl_io_operations vvp_io_ops = {
         .op = {
                 [CIT_READ] = {
-                        .cio_fini      = vvp_io_fini,
+                        .cio_fini      = vvp_io_read_fini,
                         .cio_lock      = vvp_io_read_lock,
                         .cio_start     = vvp_io_read_start,
-                        .cio_advance   = vvp_io_advance
+                        .cio_advance   = ccc_io_advance
                 },
                 [CIT_WRITE] = {
-                        .cio_fini      = vvp_io_fini,
-                        .cio_lock      = vvp_io_write_lock,
-                        .cio_start     = vvp_io_write_start,
-                        .cio_advance   = vvp_io_advance
+                       .cio_fini      = vvp_io_fini,
+                       .cio_iter_init = vvp_io_write_iter_init,
+                       .cio_iter_fini = vvp_io_write_iter_fini,
+                       .cio_lock      = vvp_io_write_lock,
+                       .cio_start     = vvp_io_write_start,
+                       .cio_advance   = ccc_io_advance
                 },
-                [CIT_TRUNC] = {
-                        .cio_fini       = vvp_io_trunc_fini,
-                        .cio_iter_init  = vvp_io_trunc_iter_init,
-                        .cio_lock       = vvp_io_trunc_lock,
-                        .cio_start      = vvp_io_trunc_start,
-                        .cio_end        = vvp_io_trunc_end
+                [CIT_SETATTR] = {
+                        .cio_fini       = vvp_io_setattr_fini,
+                        .cio_iter_init  = vvp_io_setattr_iter_init,
+                        .cio_lock       = vvp_io_setattr_lock,
+                        .cio_start      = vvp_io_setattr_start,
+                        .cio_end        = vvp_io_setattr_end
                 },
                 [CIT_FAULT] = {
                         .cio_fini      = vvp_io_fault_fini,
@@ -945,41 +1125,43 @@ static const struct cl_io_operations vvp_io_ops = {
                         .cio_start     = vvp_io_fault_start,
                         .cio_end       = ccc_io_end
                 },
+               [CIT_FSYNC] = {
+                       .cio_start  = vvp_io_fsync_start,
+                       .cio_fini   = vvp_io_fini
+               },
                 [CIT_MISC] = {
                         .cio_fini   = vvp_io_fini
                 }
         },
         .cio_read_page     = vvp_io_read_page,
-        .cio_prepare_write = vvp_io_prepare_write,
-        .cio_commit_write  = vvp_io_commit_write
 };
 
 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
                 struct cl_io *io)
 {
-        struct vvp_io      *vio   = vvp_env_io(env);
-        struct ccc_io      *cio   = ccc_env_io(env);
-        struct inode       *inode = ccc_object_inode(obj);
-        struct ll_sb_info  *sbi   = ll_i2sbi(inode);
+       struct vvp_io      *vio   = vvp_env_io(env);
+       struct ccc_io      *cio   = ccc_env_io(env);
+       struct inode       *inode = ccc_object_inode(obj);
         int                 result;
 
         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
         ENTRY;
 
+       CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
+                          "restore needed %d\n",
+              PFID(lu_object_fid(&obj->co_lu)),
+              io->ci_ignore_layout, io->ci_verify_layout,
+              cio->cui_layout_gen, io->ci_restore_needed);
+
         CL_IO_SLICE_CLEAN(cio, cui_cl);
         cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
-        vio->cui_oneshot = 0;
         vio->cui_ra_window_set = 0;
-        result = 0;
-        if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
-                int    op;
-                size_t count;
+       result = 0;
+       if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
+               size_t count;
+               struct ll_inode_info *lli = ll_i2info(inode);
 
                 count = io->u.ci_rw.crw_count;
-                op    = io->ci_type == CIT_READ ?
-                        LPROC_LL_READ_BYTES : LPROC_LL_WRITE_BYTES;
-                if (io->ci_type == CIT_WRITE)
-                        down(&ll_i2info(inode)->lli_write_sem);
                 /* "If nbyte is 0, read() will return 0 and have no other
                  *  results."  -- Single Unix Spec */
                 if (count == 0)
@@ -987,13 +1169,44 @@ int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
                 else {
                         cio->cui_tot_count = count;
                         cio->cui_tot_nrsegs = 0;
-                        ll_stats_ops_tally(sbi, op, count);
                 }
-        } else if (io->ci_type == CIT_TRUNC) {
-                /* lockless truncate? */
-                ll_stats_ops_tally(sbi, LPROC_LL_TRUNC, 1);
-        }
-        RETURN(result);
+
+               /* for read/write, we store the jobid in the inode, and
+                * it'll be fetched by osc when building RPC.
+                *
+                * it's not accurate if the file is shared by different
+                * jobs.
+                */
+               lustre_get_jobid(lli->lli_jobid);
+       } else if (io->ci_type == CIT_SETATTR) {
+               if (!cl_io_is_trunc(io))
+                       io->ci_lockreq = CILR_MANDATORY;
+       }
+
+       /* ignore layout change for generic CIT_MISC but not for glimpse.
+        * io context for glimpse must set ci_verify_layout to true,
+        * see cl_glimpse_size0() for details. */
+       if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
+               io->ci_ignore_layout = 1;
+
+       /* Enqueue layout lock and get layout version. We need to do this
+        * even for operations requiring to open file, such as read and write,
+        * because it might not grant layout lock in IT_OPEN. */
+       if (result == 0 && !io->ci_ignore_layout) {
+               result = ll_layout_refresh(inode, &cio->cui_layout_gen);
+               if (result == -ENOENT)
+                       /* If the inode on MDS has been removed, but the objects
+                        * on OSTs haven't been destroyed (async unlink), layout
+                        * fetch will return -ENOENT, we'd ingore this error
+                        * and continue with dirty flush. LU-3230. */
+                       result = 0;
+               if (result < 0)
+                       CERROR("%s: refresh file layout " DFID " error %d.\n",
+                               ll_get_fsname(inode->i_sb, NULL, 0),
+                               PFID(lu_object_fid(&obj->co_lu)), result);
+       }
+
+       RETURN(result);
 }
 
 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
@@ -1003,4 +1216,3 @@ static struct vvp_io *cl2vvp_io(const struct lu_env *env,
         cl2ccc_io(env, slice);
         return vvp_env_io(env);
 }
-