Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lustre / osc / osc_io.c
index bf93df5..947af47 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2013, Intel Corporation.
+ * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * Implementation of cl_io for OSC layer.
  *
 
 #define DEBUG_SUBSYSTEM S_OSC
 
-#include "osc_cl_internal.h"
+#include <lustre_obdo.h>
+#include <lustre_osc.h>
+#include <linux/pagevec.h>
+#include <linux/falloc.h>
+
+#include "osc_internal.h"
+#include <lnet/lnet_rdma.h>
 
-/** \addtogroup osc 
- *  @{ 
+/** \addtogroup osc
+ *  @{
  */
 
 /*****************************************************************************
  *
- * Type conversions.
+ * io operations.
  *
  */
 
-static struct osc_req *cl2osc_req(const struct cl_req_slice *slice)
+static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
 {
-        LINVRNT(slice->crs_dev->cd_lu_dev.ld_type == &osc_device_type);
-        return container_of0(slice, struct osc_req, or_cl);
 }
 
-static struct osc_io *cl2osc_io(const struct lu_env *env,
-                                const struct cl_io_slice *slice)
+void osc_read_ahead_release(const struct lu_env *env, struct cl_read_ahead *ra)
 {
-        struct osc_io *oio = container_of0(slice, struct osc_io, oi_cl);
-        LINVRNT(oio == osc_env_io(env));
-        return oio;
+       struct ldlm_lock *dlmlock = ra->cra_dlmlock;
+       struct osc_io *oio = ra->cra_oio;
+       struct lustre_handle lockh;
+
+       oio->oi_is_readahead = 0;
+       ldlm_lock2handle(dlmlock, &lockh);
+       ldlm_lock_decref(&lockh, LCK_PR);
+       LDLM_LOCK_PUT(dlmlock);
 }
+EXPORT_SYMBOL(osc_read_ahead_release);
 
-static struct osc_page *osc_cl_page_osc(struct cl_page *page)
+static int osc_io_read_ahead(const struct lu_env *env,
+                            const struct cl_io_slice *ios,
+                            pgoff_t start, struct cl_read_ahead *ra)
 {
-        const struct cl_page_slice *slice;
-
-        slice = cl_page_at(page, &osc_device_type);
-        LASSERT(slice != NULL);
+       struct osc_object *osc = cl2osc(ios->cis_obj);
+       struct osc_io *oio = cl2osc_io(env, ios);
+       struct ldlm_lock *dlmlock;
+       int result = -ENODATA;
 
-        return cl2osc_page(slice);
-}
+       ENTRY;
 
+       oio->oi_is_readahead = true;
+       dlmlock = osc_dlmlock_at_pgoff(env, osc, start, 0);
+       if (dlmlock != NULL) {
+               struct lov_oinfo *oinfo = osc->oo_oinfo;
+
+               LASSERT(dlmlock->l_ast_data == osc);
+               if (dlmlock->l_req_mode != LCK_PR) {
+                       struct lustre_handle lockh;
+                       ldlm_lock2handle(dlmlock, &lockh);
+                       ldlm_lock_addref(&lockh, LCK_PR);
+                       ldlm_lock_decref(&lockh, dlmlock->l_req_mode);
+               }
 
-/*****************************************************************************
- *
- * io operations.
- *
- */
+               ra->cra_rpc_pages = osc_cli(osc)->cl_max_pages_per_rpc;
+               ra->cra_end_idx =
+                       dlmlock->l_policy_data.l_extent.end >> PAGE_SHIFT;
+               ra->cra_release = osc_read_ahead_release;
+               ra->cra_dlmlock = dlmlock;
+               ra->cra_oio = oio;
+               if (ra->cra_end_idx != CL_PAGE_EOF)
+                       ra->cra_contention = true;
+               ra->cra_end_idx = min_t(pgoff_t,
+                                       ra->cra_end_idx,
+                                       (oinfo->loi_kms - 1) >> PAGE_SHIFT);
+               result = 0;
+       }
 
-static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
-{
+       RETURN(result);
 }
 
 /**
@@ -95,36 +119,47 @@ static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
  * or, if page is already submitted, changes osc flags through
  * osc_set_async_flags().
  */
-static int osc_io_submit(const struct lu_env *env,
-                         const struct cl_io_slice *ios,
-                        enum cl_req_type crt, struct cl_2queue *queue)
-{
-        struct cl_page    *page;
-        struct cl_page    *tmp;
-        struct client_obd *cli  = NULL;
-        struct osc_object *osc  = NULL; /* to keep gcc happy */
-        struct osc_page   *opg;
-        struct cl_io      *io;
-       CFS_LIST_HEAD     (list);
+int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
+                 enum cl_req_type crt, struct cl_2queue *queue)
+{
+       struct cl_page    *page;
+       struct cl_page    *tmp;
+       struct client_obd *cli  = NULL;
+       struct osc_object *osc  = NULL; /* to keep gcc happy */
+       struct osc_page   *opg;
+       struct cl_io      *io;
+       LIST_HEAD(list);
 
        struct cl_page_list *qin      = &queue->c2_qin;
        struct cl_page_list *qout     = &queue->c2_qout;
-       int queued = 0;
+       unsigned int queued = 0;
        int result = 0;
-       int cmd;
        int brw_flags;
-       int max_pages;
+       unsigned int max_pages;
+       unsigned int ppc_bits; /* pages per chunk bits */
+       unsigned int ppc;
+       bool sync_queue = false;
 
        LASSERT(qin->pl_nr > 0);
 
-       CDEBUG(D_CACHE, "%d %d\n", qin->pl_nr, crt);
+       CDEBUG(D_CACHE|D_READA, "%d %d\n", qin->pl_nr, crt);
 
        osc = cl2osc(ios->cis_obj);
        cli = osc_cli(osc);
        max_pages = cli->cl_max_pages_per_rpc;
+       ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
+       ppc = 1 << ppc_bits;
 
-       cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
        brw_flags = osc_io_srvlock(cl2osc_io(env, ios)) ? OBD_BRW_SRVLOCK : 0;
+       brw_flags |= crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
+       if (crt == CRT_READ && ios->cis_io->ci_ndelay)
+               brw_flags |= OBD_BRW_NDELAY;
+
+       page = cl_page_list_first(qin);
+       if (page->cp_type == CPT_TRANSIENT)
+               brw_flags |= OBD_BRW_NOCACHE;
+       if (lnet_is_rdma_only_page(page->cp_vmpage))
+               brw_flags |= OBD_BRW_RDMA_ONLY;
 
         /*
          * NOTE: here @page is a top-level page. This is done to avoid
@@ -137,12 +172,11 @@ static int osc_io_submit(const struct lu_env *env,
                 io = page->cp_owner;
                 LASSERT(io != NULL);
 
-                opg = osc_cl_page_osc(page);
-                oap = &opg->ops_oap;
-               LASSERT(osc == oap->oap_obj);
+               opg = osc_cl_page_osc(page, osc);
+               oap = &opg->ops_oap;
 
-               if (!cfs_list_empty(&oap->oap_pending_item) ||
-                   !cfs_list_empty(&oap->oap_rpc_item)) {
+               if (!list_empty(&oap->oap_pending_item) ||
+                   !list_empty(&oap->oap_rpc_item)) {
                        CDEBUG(D_CACHE, "Busy oap %p page %p for submit.\n",
                               oap, opg);
                         result = -EBUSY;
@@ -163,176 +197,300 @@ static int osc_io_submit(const struct lu_env *env,
                        continue;
                 }
 
-               cl_page_list_move(qout, qin, page);
-               spin_lock(&oap->oap_lock);
-               oap->oap_async_flags = ASYNC_URGENT|ASYNC_READY;
-               oap->oap_async_flags |= ASYNC_COUNT_STABLE;
-               spin_unlock(&oap->oap_lock);
+               if (page->cp_type != CPT_TRANSIENT) {
+                       oap->oap_async_flags = ASYNC_URGENT|ASYNC_READY|ASYNC_COUNT_STABLE;
+               }
 
                osc_page_submit(env, opg, crt, brw_flags);
-               cfs_list_add_tail(&oap->oap_pending_item, &list);
-               if (++queued == max_pages) {
-                       queued = 0;
-                       result = osc_queue_sync_pages(env, osc, &list, cmd,
+               list_add_tail(&oap->oap_pending_item, &list);
+
+               if (page->cp_sync_io != NULL)
+                       cl_page_list_move(qout, qin, page);
+               else /* async IO */
+                       cl_page_list_del(env, qin, page, true);
+
+               queued++;
+               if (queued == max_pages) {
+                       sync_queue = true;
+               } else if (crt == CRT_WRITE) {
+                       unsigned int chunks;
+                       unsigned int next_chunks;
+
+                       chunks = (queued + ppc - 1) >> ppc_bits;
+                       /* chunk number if add another page */
+                       next_chunks = (queued + ppc) >> ppc_bits;
+
+                       /* next page will excceed write chunk limit */
+                       if (chunks == osc_max_write_chunks(cli) &&
+                           next_chunks > chunks)
+                               sync_queue = true;
+               }
+
+               if (sync_queue) {
+                       result = osc_queue_sync_pages(env, io, osc, &list,
                                                      brw_flags);
                        if (result < 0)
                                break;
+                       queued = 0;
+                       sync_queue = false;
                }
        }
 
        if (queued > 0)
-               result = osc_queue_sync_pages(env, osc, &list, cmd, brw_flags);
+               result = osc_queue_sync_pages(env, io, osc, &list, brw_flags);
 
-       CDEBUG(D_INFO, "%d/%d %d\n", qin->pl_nr, qout->pl_nr, result);
-       return qout->pl_nr > 0 ? 0 : result;
-}
-
-static void osc_page_touch_at(const struct lu_env *env,
-                              struct cl_object *obj, pgoff_t idx, unsigned to)
-{
-        struct lov_oinfo  *loi  = cl2osc(obj)->oo_oinfo;
-        struct cl_attr    *attr = &osc_env_info(env)->oti_attr;
-        int valid;
-        __u64 kms;
+       /* Update c/mtime for sync write. LU-7310 */
+       if (crt == CRT_WRITE && qout->pl_nr > 0 && result == 0) {
+               struct cl_object *obj   = ios->cis_obj;
+               struct cl_attr *attr = &osc_env_info(env)->oti_attr;
 
-        /* offset within stripe */
-        kms = cl_offset(obj, idx) + to;
+               cl_object_attr_lock(obj);
+               attr->cat_mtime = attr->cat_ctime = ktime_get_real_seconds();
+               cl_object_attr_update(env, obj, attr, CAT_MTIME | CAT_CTIME);
+               cl_object_attr_unlock(obj);
+       }
 
-        cl_object_attr_lock(obj);
-        /*
-         * XXX old code used
-         *
-         *         ll_inode_size_lock(inode, 0); lov_stripe_lock(lsm);
-         *
-         * here
-         */
-        CDEBUG(D_INODE, "stripe KMS %sincreasing "LPU64"->"LPU64" "LPU64"\n",
-               kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
-               loi->loi_lvb.lvb_size);
-
-        valid = 0;
-        if (kms > loi->loi_kms) {
-                attr->cat_kms = kms;
-                valid |= CAT_KMS;
-        }
-        if (kms > loi->loi_lvb.lvb_size) {
-                attr->cat_size = kms;
-                valid |= CAT_SIZE;
-        }
-        cl_object_attr_set(env, obj, attr, valid);
-        cl_object_attr_unlock(obj);
+       CDEBUG(D_INFO, "%d/%d %d\n", qin->pl_nr, qout->pl_nr, result);
+       return qout->pl_nr > 0 ? 0 : result;
 }
+EXPORT_SYMBOL(osc_io_submit);
 
 /**
- * This is called when a page is accessed within file in a way that creates
- * new page, if one were missing (i.e., if there were a hole at that place in
- * the file, or accessed page is beyond the current file size). Examples:
- * ->commit_write() and ->nopage() methods.
+ * This is called to update the attributes when modifying a specific page,
+ * both when making new pages and when doing updates to existing cached pages.
  *
  * Expand stripe KMS if necessary.
  */
-static void osc_page_touch(const struct lu_env *env,
-                           struct osc_page *opage, unsigned to)
+void osc_page_touch_at(const struct lu_env *env, struct cl_object *obj,
+                      pgoff_t idx, size_t to)
 {
-        struct cl_page    *page = opage->ops_cl.cpl_page;
-        struct cl_object  *obj  = opage->ops_cl.cpl_obj;
+       struct lov_oinfo  *loi  = cl2osc(obj)->oo_oinfo;
+       struct cl_attr    *attr = &osc_env_info(env)->oti_attr;
+       int valid;
+       __u64 kms;
 
-        osc_page_touch_at(env, obj, page->cp_index, to);
+       ENTRY;
+
+       /* offset within stripe */
+       kms = (idx << PAGE_SHIFT) + to;
+
+       cl_object_attr_lock(obj);
+       CDEBUG(D_INODE, "stripe KMS %sincreasing %llu->%llu %llu\n",
+              kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
+              loi->loi_lvb.lvb_size);
+
+       attr->cat_mtime = attr->cat_ctime = ktime_get_real_seconds();
+       valid = CAT_MTIME | CAT_CTIME;
+       if (kms > loi->loi_kms) {
+               attr->cat_kms = kms;
+               valid |= CAT_KMS;
+       }
+       if (kms > loi->loi_lvb.lvb_size) {
+               attr->cat_size = kms;
+               valid |= CAT_SIZE;
+       }
+       cl_object_attr_update(env, obj, attr, valid);
+       cl_object_attr_unlock(obj);
+
+       EXIT;
 }
 
-/**
- * Implements cl_io_operations::cio_prepare_write() method for osc layer.
- *
- * \retval -EIO transfer initiated against this osc will most likely fail
- * \retval 0    transfer initiated against this osc will most likely succeed.
- *
- * The reason for this check is to immediately return an error to the caller
- * in the case of a deactivated import. Note, that import can be deactivated
- * later, while pages, dirtied by this IO, are still in the cache, but this is
- * irrelevant, because that would still return an error to the application (if
- * it does fsync), but many applications don't do fsync because of performance
- * issues, and we wanted to return an -EIO at write time to notify the
- * application.
- */
-static int osc_io_prepare_write(const struct lu_env *env,
-                                const struct cl_io_slice *ios,
-                                const struct cl_page_slice *slice,
-                                unsigned from, unsigned to)
+int osc_io_commit_async(const struct lu_env *env,
+                       const struct cl_io_slice *ios,
+                       struct cl_page_list *qin, int from, int to,
+                       cl_commit_cbt cb)
 {
-        struct osc_device *dev = lu2osc_dev(slice->cpl_obj->co_lu.lo_dev);
-        struct obd_import *imp = class_exp2cliimp(dev->od_exp);
-        struct osc_io     *oio = cl2osc_io(env, ios);
-        int result = 0;
-        ENTRY;
+       struct cl_io *io = ios->cis_io;
+       struct osc_io *oio = cl2osc_io(env, ios);
+       struct osc_object *osc = cl2osc(ios->cis_obj);
+       struct cl_page *page;
+       struct cl_page *last_page;
+       struct osc_page *opg;
+       struct folio_batch *fbatch = &osc_env_info(env)->oti_fbatch;
+       int result = 0;
+       ENTRY;
 
-        /*
-         * This implements OBD_BRW_CHECK logic from old client.
-         */
+       LASSERT(qin->pl_nr > 0);
+
+       /* Handle partial page cases */
+       last_page = cl_page_list_last(qin);
+       if (oio->oi_lockless) {
+               page = cl_page_list_first(qin);
+               if (page == last_page) {
+                       cl_page_clip(env, page, from, to);
+               } else {
+                       if (from != 0)
+                               cl_page_clip(env, page, from, PAGE_SIZE);
+                       if (to != PAGE_SIZE)
+                               cl_page_clip(env, last_page, 0, to);
+               }
+       }
+
+       ll_folio_batch_init(fbatch, 0);
 
-        if (imp == NULL || imp->imp_invalid)
-                result = -EIO;
-        if (result == 0 && oio->oi_lockless)
-                /* this page contains `invalid' data, but who cares?
-                 * nobody can access the invalid data.
-                 * in osc_io_commit_write(), we're going to write exact
-                 * [from, to) bytes of this page to OST. -jay */
-                cl_page_export(env, slice->cpl_page, 1);
+       while (qin->pl_nr > 0) {
+               struct osc_async_page *oap;
+
+               page = cl_page_list_first(qin);
+               opg = osc_cl_page_osc(page, osc);
+               oap = &opg->ops_oap;
+
+               if (!list_empty(&oap->oap_rpc_item)) {
+                       CDEBUG(D_CACHE, "Busy oap %p page %p for submit.\n",
+                              oap, opg);
+                       result = -EBUSY;
+                       break;
+               }
+
+               /* The page may be already in dirty cache. */
+               if (list_empty(&oap->oap_pending_item)) {
+                       result = osc_page_cache_add(env, osc, opg, io, cb);
+                       if (result != 0)
+                               break;
+               }
 
-        RETURN(result);
+               osc_page_touch_at(env, osc2cl(osc), osc_index(opg),
+                                 page == last_page ? to : PAGE_SIZE);
+
+               cl_page_list_del(env, qin, page, true);
+
+               /* if there are no more slots, do the callback & reinit */
+               if (!folio_batch_add_page(fbatch, page->cp_vmpage)) {
+                       (*cb)(env, io, fbatch);
+                       folio_batch_reinit(fbatch);
+               }
+       }
+       /* The shrink interval is in seconds, so we can update it once per
+        * write, rather than once per page.
+        */
+       osc_update_next_shrink(osc_cli(osc));
+
+
+       /* Clean up any partially full folio_batches */
+       if (folio_batch_count(fbatch) != 0)
+               (*cb)(env, io, fbatch);
+
+       /* Can't access these pages any more. Page can be in transfer and
+        * complete at any time. */
+
+       /* for sync write, kernel will wait for this page to be flushed before
+        * osc_io_end() is called, so release it earlier.
+        * for mkwrite(), it's known there is no further pages. */
+       if (cl_io_is_sync_write(io) && oio->oi_active != NULL) {
+               osc_extent_release(env, oio->oi_active);
+               oio->oi_active = NULL;
+       }
+
+       CDEBUG(D_INFO, "%d %d\n", qin->pl_nr, result);
+       RETURN(result);
 }
+EXPORT_SYMBOL(osc_io_commit_async);
 
-static int osc_io_commit_write(const struct lu_env *env,
-                               const struct cl_io_slice *ios,
-                               const struct cl_page_slice *slice,
-                               unsigned from, unsigned to)
+void osc_io_extent_release(const struct lu_env *env,
+                          const struct cl_io_slice *ios)
 {
-        struct osc_io         *oio = cl2osc_io(env, ios);
-        struct osc_page       *opg = cl2osc_page(slice);
-        struct osc_object     *obj = cl2osc(opg->ops_cl.cpl_obj);
-        struct osc_async_page *oap = &opg->ops_oap;
-        ENTRY;
+       struct osc_io *oio = cl2osc_io(env, ios);
 
-        LASSERT(to > 0);
-        /*
-         * XXX instead of calling osc_page_touch() here and in
-         * osc_io_fault_start() it might be more logical to introduce
-         * cl_page_touch() method, that generic cl_io_commit_write() and page
-         * fault code calls.
-         */
-        osc_page_touch(env, cl2osc_page(slice), to);
-        if (!client_is_remote(osc_export(obj)) &&
-            cfs_capable(CFS_CAP_SYS_RESOURCE))
-                oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
+       if (oio->oi_active != NULL) {
+               osc_extent_release(env, oio->oi_active);
+               oio->oi_active = NULL;
+       }
+}
+EXPORT_SYMBOL(osc_io_extent_release);
 
-        if (oio->oi_lockless)
-                /* see osc_io_prepare_write() for lockless io handling. */
-                cl_page_clip(env, slice->cpl_page, from, to);
+static bool osc_import_not_healthy(struct obd_import *imp)
+{
+       return imp->imp_invalid || imp->imp_deactive ||
+              !(imp->imp_state == LUSTRE_IMP_FULL ||
+                imp->imp_state == LUSTRE_IMP_IDLE);
+}
 
-        RETURN(0);
+int osc_io_iter_init(const struct lu_env *env, const struct cl_io_slice *ios)
+{
+       struct osc_object *osc = cl2osc(ios->cis_obj);
+       struct obd_import *imp = osc_cli(osc)->cl_import;
+       struct osc_io *oio = osc_env_io(env);
+       int rc = -EIO;
+
+       ENTRY;
+
+       spin_lock(&imp->imp_lock);
+       /**
+        * check whether this OSC device is available for non-delay read,
+        * fast switching mirror if we haven't tried all mirrors.
+        */
+       if (ios->cis_io->ci_type == CIT_READ && ios->cis_io->ci_ndelay &&
+           !ios->cis_io->ci_tried_all_mirrors && osc_import_not_healthy(imp)) {
+               rc = -EAGAIN;
+       } else if (likely(!imp->imp_invalid)) {
+               atomic_inc(&osc->oo_nr_ios);
+               oio->oi_is_active = 1;
+               rc = 0;
+       }
+       spin_unlock(&imp->imp_lock);
+
+       if (capable(CAP_SYS_RESOURCE))
+               oio->oi_cap_sys_resource = 1;
+
+       RETURN(rc);
 }
+EXPORT_SYMBOL(osc_io_iter_init);
 
-static int osc_io_fault_start(const struct lu_env *env,
-                              const struct cl_io_slice *ios)
+void osc_io_iter_fini(const struct lu_env *env,
+                     const struct cl_io_slice *ios)
 {
-        struct cl_io       *io;
-        struct cl_fault_io *fio;
+       struct osc_io *oio = osc_env_io(env);
 
-        ENTRY;
+       if (oio->oi_is_active) {
+               struct osc_object *osc = cl2osc(ios->cis_obj);
 
-        io  = ios->cis_io;
-        fio = &io->u.ci_fault;
-        CDEBUG(D_INFO, "%lu %d %d\n",
-               fio->ft_index, fio->ft_writable, fio->ft_nob);
-        /*
-         * If mapping is writeable, adjust kms to cover this page,
-         * but do not extend kms beyond actual file size.
-         * See bug 10919.
-         */
-        if (fio->ft_writable)
-                osc_page_touch_at(env, ios->cis_obj,
-                                  fio->ft_index, fio->ft_nob);
-        RETURN(0);
+               oio->oi_is_active = 0;
+               LASSERT(atomic_read(&osc->oo_nr_ios) > 0);
+               if (atomic_dec_and_test(&osc->oo_nr_ios))
+                       wake_up(&osc->oo_io_waitq);
+       }
+}
+EXPORT_SYMBOL(osc_io_iter_fini);
+
+void osc_io_rw_iter_fini(const struct lu_env *env,
+                        const struct cl_io_slice *ios)
+{
+       struct osc_io *oio = osc_env_io(env);
+       struct osc_object *osc = cl2osc(ios->cis_obj);
+
+       if (oio->oi_lru_reserved > 0) {
+               osc_lru_unreserve(osc_cli(osc), oio->oi_lru_reserved);
+               oio->oi_lru_reserved = 0;
+       }
+       oio->oi_write_osclock = NULL;
+       oio->oi_read_osclock = NULL;
+
+       osc_io_iter_fini(env, ios);
+}
+EXPORT_SYMBOL(osc_io_rw_iter_fini);
+
+int osc_io_fault_start(const struct lu_env *env, const struct cl_io_slice *ios)
+{
+       struct cl_io       *io;
+       struct cl_fault_io *fio;
+       ENTRY;
+
+       io  = ios->cis_io;
+       fio = &io->u.ci_fault;
+       CDEBUG(D_INFO, "%lu %d %zu\n",
+               fio->ft_index, fio->ft_writable, fio->ft_bytes);
+       /*
+        * If mapping is writeable, adjust kms to cover this page,
+        * but do not extend kms beyond actual file size.
+        * See bug 10919.
+        */
+       if (fio->ft_writable)
+               osc_page_touch_at(env, ios->cis_obj,
+                                 fio->ft_index, fio->ft_bytes);
+       RETURN(0);
 }
+EXPORT_SYMBOL(osc_io_fault_start);
+
 
 static int osc_async_upcall(void *a, int rc)
 {
@@ -343,39 +501,31 @@ static int osc_async_upcall(void *a, int rc)
         return 0;
 }
 
-#if defined(__KERNEL__)
 /**
  * Checks that there are no pages being written in the extent being truncated.
  */
-static int trunc_check_cb(const struct lu_env *env, struct cl_io *io,
-                         struct cl_page *page, void *cbdata)
+static bool trunc_check_cb(const struct lu_env *env, struct cl_io *io,
+                          void **pvec, int count, void *cbdata)
 {
-       const struct cl_page_slice *slice;
-       struct osc_page *ops;
-       struct osc_async_page *oap;
-       __u64 start = *(__u64 *)cbdata;
-
-       slice = cl_page_at(page, &osc_device_type);
-       LASSERT(slice != NULL);
-       ops = cl2osc_page(slice);
-       oap = &ops->ops_oap;
-
-       if (oap->oap_cmd & OBD_BRW_WRITE &&
-           !cfs_list_empty(&oap->oap_pending_item))
-               CL_PAGE_DEBUG(D_ERROR, env, page, "exists " LPU64 "/%s.\n",
-                               start, current->comm);
-
-#ifdef __linux__
-       {
-               struct page *vmpage = cl_page_vmpage(env, page);
-               if (PageLocked(vmpage))
-                       CDEBUG(D_CACHE, "page %p index %lu locked for %d.\n",
-                              ops, page->cp_index,
-                              (oap->oap_cmd & OBD_BRW_RWMASK));
+       int i;
+
+       for (i = 0; i < count; i++) {
+               struct osc_page *ops = pvec[i];
+               struct cl_page *page = ops->ops_cl.cpl_page;
+               struct osc_async_page *oap;
+               __u64 start = *(__u64 *)cbdata;
+
+               oap = &ops->ops_oap;
+               if (oap->oap_cmd & OBD_BRW_WRITE &&
+                   !list_empty(&oap->oap_pending_item))
+                       CL_PAGE_DEBUG(D_ERROR, env, page, "exists %llu/%s.\n",
+                                     start, current->comm);
+
+               if (PageLocked(page->cp_vmpage))
+                       CDEBUG(D_CACHE, "page %p index %lu locked for cmd=%d\n",
+                              ops, osc_index(ops), oap->oap_cmd);
        }
-#endif
-
-       return CLP_GANG_OKAY;
+       return true;
 }
 
 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
@@ -385,42 +535,68 @@ static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
        int     partial;
        pgoff_t start;
 
-        clob    = oio->oi_cl.cis_obj;
-        start   = cl_index(clob, size);
-        partial = cl_offset(clob, start) < size;
+       clob = oio->oi_cl.cis_obj;
+       start = size >> PAGE_SHIFT;
+       partial = (start << PAGE_SHIFT) < size;
 
         /*
          * Complain if there are pages in the truncated region.
          */
-       cl_page_gang_lookup(env, clob, io, start + partial, CL_PAGE_EOF,
-                           trunc_check_cb, (void *)&size);
+       osc_page_gang_lookup(env, io, cl2osc(clob),
+                               start + partial, CL_PAGE_EOF,
+                               trunc_check_cb, (void *)&size);
 }
-#else /* __KERNEL__ */
-static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
-                           struct osc_io *oio, __u64 size)
+
+/**
+ * Flush affected pages prior punch.
+ * We shouldn't discard them locally first because that could be data loss
+ * if server doesn't support fallocate punch, we also need these data to be
+ * flushed first to prevent re-ordering with the punch
+ */
+int osc_punch_start(const struct lu_env *env, struct cl_io *io,
+                   struct cl_object *obj)
 {
-       return;
+       struct osc_object *osc = cl2osc(obj);
+       pgoff_t pg_start = io->u.ci_setattr.sa_falloc_offset >> PAGE_SHIFT;
+       pgoff_t pg_end = (io->u.ci_setattr.sa_falloc_end - 1) >> PAGE_SHIFT;
+       int rc;
+
+       ENTRY;
+       rc = osc_cache_writeback_range(env, osc, pg_start, pg_end, 1, 0);
+       if (rc < 0)
+               RETURN(rc);
+
+       osc_page_gang_lookup(env, io, osc, pg_start, pg_end, osc_discard_cb,
+                            osc);
+       RETURN(0);
 }
-#endif
+EXPORT_SYMBOL(osc_punch_start);
 
 static int osc_io_setattr_start(const struct lu_env *env,
                                 const struct cl_io_slice *slice)
 {
-        struct cl_io            *io     = slice->cis_io;
-        struct osc_io           *oio    = cl2osc_io(env, slice);
-        struct cl_object        *obj    = slice->cis_obj;
-        struct lov_oinfo        *loi    = cl2osc(obj)->oo_oinfo;
-        struct cl_attr          *attr   = &osc_env_info(env)->oti_attr;
-        struct obdo             *oa     = &oio->oi_oa;
+       struct cl_io *io = slice->cis_io;
+       struct osc_io *oio = cl2osc_io(env, slice);
+       struct cl_object *obj = slice->cis_obj;
+       struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
+       struct cl_attr *attr = &osc_env_info(env)->oti_attr;
+       struct obdo *oa = &oio->oi_oa;
        struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
-       __u64                    size   = io->u.ci_setattr.sa_attr.lvb_size;
-       unsigned int             ia_valid = io->u.ci_setattr.sa_valid;
-       int                      result = 0;
-       struct obd_info          oinfo = { { { 0 } } };
+       unsigned int ia_avalid = io->u.ci_setattr.sa_avalid;
+       enum op_xvalid ia_xvalid = io->u.ci_setattr.sa_xvalid;
+       int result = 0;
+       __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
+       bool io_is_falloc = cl_io_is_fallocate(io);
 
+       ENTRY;
        /* truncate cache dirty pages first */
        if (cl_io_is_trunc(io))
-               result = osc_cache_truncate_start(env, oio, cl2osc(obj), size);
+               result = osc_cache_truncate_start(env, cl2osc(obj), size,
+                                                 &oio->oi_trunc);
+       /* flush local pages prior punching them on server */
+       if (io_is_falloc &&
+           io->u.ci_setattr.sa_falloc_mode & FALLOC_FL_PUNCH_HOLE)
+               result = osc_punch_start(env, io, obj);
 
        if (result == 0 && oio->oi_lockless == 0) {
                cl_object_attr_lock(obj);
@@ -429,148 +605,328 @@ static int osc_io_setattr_start(const struct lu_env *env,
                        struct ost_lvb *lvb = &io->u.ci_setattr.sa_attr;
                        unsigned int cl_valid = 0;
 
-                       if (ia_valid & ATTR_SIZE) {
-                               attr->cat_size = attr->cat_kms = size;
+                       if (ia_avalid & ATTR_SIZE) {
+                               attr->cat_size = size;
+                               attr->cat_kms = size;
                                cl_valid = (CAT_SIZE | CAT_KMS);
                        }
-                       if (ia_valid & ATTR_MTIME_SET) {
+                       if (ia_avalid & ATTR_MTIME_SET) {
                                attr->cat_mtime = lvb->lvb_mtime;
                                cl_valid |= CAT_MTIME;
                        }
-                       if (ia_valid & ATTR_ATIME_SET) {
+                       if (ia_avalid & ATTR_ATIME_SET) {
                                attr->cat_atime = lvb->lvb_atime;
                                cl_valid |= CAT_ATIME;
                        }
-                       if (ia_valid & ATTR_CTIME_SET) {
+                       if (ia_xvalid & OP_XVALID_CTIME_SET) {
                                attr->cat_ctime = lvb->lvb_ctime;
-                                cl_valid |= CAT_CTIME;
-                        }
-                        result = cl_object_attr_set(env, obj, attr, cl_valid);
-                }
-                cl_object_attr_unlock(obj);
-        }
-        memset(oa, 0, sizeof(*oa));
-        if (result == 0) {
+                               cl_valid |= CAT_CTIME;
+                       }
+                       result = cl_object_attr_update(env, obj, attr,
+                                                      cl_valid);
+               }
+               cl_object_attr_unlock(obj);
+       }
+       memset(oa, 0, sizeof(*oa));
+       if (result == 0) {
                oa->o_oi = loi->loi_oi;
-               oa->o_mtime = attr->cat_mtime;
-               oa->o_atime = attr->cat_atime;
-               oa->o_ctime = attr->cat_ctime;
-               oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLATIME |
-                       OBD_MD_FLCTIME | OBD_MD_FLMTIME;
-                if (ia_valid & ATTR_SIZE) {
-                        oa->o_size = size;
-                        oa->o_blocks = OBD_OBJECT_EOF;
-                        oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
-
-                        if (oio->oi_lockless) {
-                                oa->o_flags = OBD_FL_SRVLOCK;
-                                oa->o_valid |= OBD_MD_FLFLAGS;
-                        }
-                } else {
-                        LASSERT(oio->oi_lockless == 0);
-                }
+               obdo_set_parent_fid(oa, io->u.ci_setattr.sa_parent_fid);
+               oa->o_stripe_idx = io->u.ci_setattr.sa_stripe_index;
+               oa->o_layout = io->u.ci_setattr.sa_layout;
+               oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP |
+                       OBD_MD_FLOSTLAYOUT;
+               if (ia_avalid & ATTR_CTIME) {
+                       oa->o_valid |= OBD_MD_FLCTIME;
+                       oa->o_ctime = attr->cat_ctime;
+               }
+               if (ia_avalid & ATTR_ATIME) {
+                       oa->o_valid |= OBD_MD_FLATIME;
+                       oa->o_atime = attr->cat_atime;
+               }
+               if (ia_avalid & ATTR_MTIME) {
+                       oa->o_valid |= OBD_MD_FLMTIME;
+                       oa->o_mtime = attr->cat_mtime;
+               }
+
+               if (ia_avalid & ATTR_SIZE || io_is_falloc) {
+                       if (oio->oi_lockless) {
+                               oa->o_flags = OBD_FL_SRVLOCK;
+                               oa->o_valid |= OBD_MD_FLFLAGS;
+                       }
+
+                       if (io->ci_layout_version > 0) {
+                               /* verify layout version */
+                               oa->o_valid |= OBD_MD_LAYOUT_VERSION;
+                               oa->o_layout_version = io->ci_layout_version;
+                       }
+               } else {
+                       LASSERT(oio->oi_lockless == 0);
+               }
+
+               if (ia_xvalid & OP_XVALID_FLAGS) {
+                       oa->o_flags = io->u.ci_setattr.sa_attr_flags;
+                       oa->o_valid |= OBD_MD_FLFLAGS;
+               }
 
-                oinfo.oi_oa = oa;
-                oinfo.oi_capa = io->u.ci_setattr.sa_capa;
                init_completion(&cbargs->opc_sync);
 
-                if (ia_valid & ATTR_SIZE)
-                        result = osc_punch_base(osc_export(cl2osc(obj)),
-                                               &oinfo, osc_async_upcall,
-                                                cbargs, PTLRPCD_SET);
-                else
-                        result = osc_setattr_async_base(osc_export(cl2osc(obj)),
-                                                        &oinfo, NULL,
-                                                       osc_async_upcall,
-                                                        cbargs, PTLRPCD_SET);
+               if (io_is_falloc) {
+                       int falloc_mode = io->u.ci_setattr.sa_falloc_mode;
+
+                       oa->o_size = io->u.ci_setattr.sa_falloc_offset;
+                       oa->o_blocks = io->u.ci_setattr.sa_falloc_end;
+                       oa->o_uid = io->u.ci_setattr.sa_falloc_uid;
+                       oa->o_gid = io->u.ci_setattr.sa_falloc_gid;
+                       oa->o_projid = io->u.ci_setattr.sa_falloc_projid;
+                       oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
+                               OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLPROJID;
+
+                       CDEBUG(D_INODE,
+                              "size %llu blocks %llu uid %u gid %u prjid %u\n",
+                              oa->o_size, oa->o_blocks, oa->o_uid, oa->o_gid,
+                              oa->o_projid);
+                       result = osc_fallocate_base(osc_export(cl2osc(obj)),
+                                                   oa, osc_async_upcall,
+                                                   cbargs, falloc_mode);
+               } else if (ia_avalid & ATTR_SIZE) {
+                       oa->o_size = size;
+                       oa->o_blocks = OBD_OBJECT_EOF;
+                       oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
+                       result = osc_punch_send(osc_export(cl2osc(obj)),
+                                               oa, osc_async_upcall, cbargs);
+               } else {
+                       result = osc_setattr_async(osc_export(cl2osc(obj)),
+                                                  oa, osc_async_upcall,
+                                                  cbargs, PTLRPCD_SET);
+               }
                cbargs->opc_rpc_sent = result == 0;
-        }
-        return result;
+       }
+
+       RETURN(result);
 }
 
-static void osc_io_setattr_end(const struct lu_env *env,
-                               const struct cl_io_slice *slice)
+void osc_io_setattr_end(const struct lu_env *env,
+                       const struct cl_io_slice *slice)
 {
        struct cl_io     *io  = slice->cis_io;
        struct osc_io    *oio = cl2osc_io(env, slice);
        struct cl_object *obj = slice->cis_obj;
        struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
-        int result = 0;
+       struct cl_attr  *attr = &osc_env_info(env)->oti_attr;
+       struct obdo *oa = &oio->oi_oa;
+       unsigned int cl_valid = 0;
+       int result = 0;
 
        if (cbargs->opc_rpc_sent) {
                wait_for_completion(&cbargs->opc_sync);
                result = io->ci_result = cbargs->opc_rc;
        }
-        if (result == 0) {
-                if (oio->oi_lockless) {
-                        /* lockless truncate */
-                        struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
-
-                        LASSERT(cl_io_is_trunc(io));
-                        /* XXX: Need a lock. */
-                        osd->od_stats.os_lockless_truncates++;
-                }
-        }
 
        if (cl_io_is_trunc(io)) {
                __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
+
+               if (result == 0) {
+                       cl_object_attr_lock(obj);
+                       if (oa->o_valid & OBD_MD_FLBLOCKS) {
+                               attr->cat_blocks = oa->o_blocks;
+                               cl_valid |= CAT_BLOCKS;
+                       }
+
+                       cl_object_attr_update(env, obj, attr, cl_valid);
+                       cl_object_attr_unlock(obj);
+               }
                osc_trunc_check(env, io, oio, size);
-               if (oio->oi_trunc != NULL) {
-                       osc_cache_truncate_end(env, oio, cl2osc(obj));
-                       oio->oi_trunc = NULL;
+               osc_cache_truncate_end(env, oio->oi_trunc);
+               oio->oi_trunc = NULL;
+       }
+
+       if (cl_io_is_fallocate(io)) {
+               if (result == 0) {
+                       cl_object_attr_lock(obj);
+                       /* update blocks */
+                       if (oa->o_valid & OBD_MD_FLBLOCKS) {
+                               attr->cat_blocks = oa->o_blocks;
+                               cl_valid |= CAT_BLOCKS;
+                       }
+
+                       cl_object_attr_update(env, obj, attr, cl_valid);
+                       cl_object_attr_unlock(obj);
                }
        }
 }
+EXPORT_SYMBOL(osc_io_setattr_end);
 
-static int osc_io_read_start(const struct lu_env *env,
-                             const struct cl_io_slice *slice)
+struct osc_data_version_args {
+       struct osc_io *dva_oio;
+};
+
+static int
+osc_data_version_interpret(const struct lu_env *env, struct ptlrpc_request *req,
+                          void *args, int rc)
+{
+       struct osc_data_version_args *dva = args;
+       struct osc_io *oio = dva->dva_oio;
+       const struct ost_body *body;
+
+       ENTRY;
+       if (rc < 0)
+               GOTO(out, rc);
+
+       body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
+       if (body == NULL)
+               GOTO(out, rc = -EPROTO);
+
+       lustre_get_wire_obdo(&req->rq_import->imp_connect_data, &oio->oi_oa,
+                            &body->oa);
+       EXIT;
+out:
+       oio->oi_cbarg.opc_rc = rc;
+       complete(&oio->oi_cbarg.opc_sync);
+
+       return 0;
+}
+
+static int osc_io_data_version_start(const struct lu_env *env,
+                                    const struct cl_io_slice *slice)
+{
+       struct cl_data_version_io *dv   = &slice->cis_io->u.ci_data_version;
+       struct osc_io           *oio    = cl2osc_io(env, slice);
+       struct obdo             *oa     = &oio->oi_oa;
+       struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       struct osc_object       *obj    = cl2osc(slice->cis_obj);
+       struct lov_oinfo        *loi    = obj->oo_oinfo;
+       struct obd_export       *exp    = osc_export(obj);
+       struct ptlrpc_request   *req;
+       struct ost_body         *body;
+       struct osc_data_version_args *dva;
+       int rc;
+
+       ENTRY;
+       memset(oa, 0, sizeof(*oa));
+       oa->o_oi = loi->loi_oi;
+       oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+
+       if (dv->dv_flags & (LL_DV_RD_FLUSH | LL_DV_WR_FLUSH)) {
+               oa->o_valid |= OBD_MD_FLFLAGS;
+               oa->o_flags |= OBD_FL_SRVLOCK;
+               if (dv->dv_flags & LL_DV_WR_FLUSH)
+                       oa->o_flags |= OBD_FL_FLUSH;
+       }
+
+       init_completion(&cbargs->opc_sync);
+
+       req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_OST_GETATTR);
+       if (req == NULL)
+               RETURN(-ENOMEM);
+
+       rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GETATTR);
+       if (rc < 0) {
+               ptlrpc_request_free(req);
+               RETURN(rc);
+       }
+
+       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
+
+       ptlrpc_request_set_replen(req);
+       req->rq_interpret_reply = osc_data_version_interpret;
+       dva = ptlrpc_req_async_args(dva, req);
+       dva->dva_oio = oio;
+
+       ptlrpcd_add_req(req);
+
+       RETURN(0);
+}
+
+static void osc_io_data_version_end(const struct lu_env *env,
+                                   const struct cl_io_slice *slice)
+{
+       struct cl_data_version_io *dv = &slice->cis_io->u.ci_data_version;
+       struct osc_io *oio = cl2osc_io(env, slice);
+       struct cl_object *obj = slice->cis_obj;
+       struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       struct cl_attr *attr = &osc_env_info(env)->oti_attr;
+       struct obdo *oa = &oio->oi_oa;
+       unsigned int cl_valid = 0;
+
+       ENTRY;
+       wait_for_completion(&cbargs->opc_sync);
+
+       if (cbargs->opc_rc != 0) {
+               slice->cis_io->ci_result = cbargs->opc_rc;
+       } else {
+               slice->cis_io->ci_result = 0;
+               if (!(oa->o_valid &
+                     (OBD_MD_LAYOUT_VERSION | OBD_MD_FLDATAVERSION)))
+                       slice->cis_io->ci_result = -EOPNOTSUPP;
+
+               if (oa->o_valid & OBD_MD_LAYOUT_VERSION)
+                       dv->dv_layout_version = oa->o_layout_version;
+               if (oa->o_valid & OBD_MD_FLDATAVERSION)
+                       dv->dv_data_version = oa->o_data_version;
+
+               if (dv->dv_flags & LL_DV_SZ_UPDATE) {
+                       if (oa->o_valid & OBD_MD_FLSIZE) {
+                               attr->cat_size = oa->o_size;
+                               cl_valid |= CAT_SIZE;
+                       }
+
+                       if (oa->o_valid & OBD_MD_FLBLOCKS) {
+                               attr->cat_blocks = oa->o_blocks;
+                               cl_valid |= CAT_BLOCKS;
+                       }
+
+                       cl_object_attr_lock(obj);
+                       cl_object_attr_update(env, obj, attr, cl_valid);
+                       cl_object_attr_unlock(obj);
+               }
+       }
+
+       EXIT;
+}
+
+int osc_io_read_start(const struct lu_env *env,
+                     const struct cl_io_slice *slice)
 {
-       struct osc_io    *oio  = cl2osc_io(env, slice);
        struct cl_object *obj  = slice->cis_obj;
        struct cl_attr   *attr = &osc_env_info(env)->oti_attr;
        int rc = 0;
        ENTRY;
 
-       if (oio->oi_lockless == 0 && !slice->cis_io->ci_noatime) {
+       if (!slice->cis_io->ci_noatime) {
                cl_object_attr_lock(obj);
-               attr->cat_atime = LTIME_S(CFS_CURRENT_TIME);
-               rc = cl_object_attr_set(env, obj, attr, CAT_ATIME);
+               attr->cat_atime = ktime_get_real_seconds();
+               rc = cl_object_attr_update(env, obj, attr, CAT_ATIME);
                cl_object_attr_unlock(obj);
        }
 
        RETURN(rc);
 }
+EXPORT_SYMBOL(osc_io_read_start);
 
-static int osc_io_write_start(const struct lu_env *env,
-                              const struct cl_io_slice *slice)
-{
-        struct osc_io    *oio   = cl2osc_io(env, slice);
-        struct cl_object *obj   = slice->cis_obj;
-        struct cl_attr   *attr  = &osc_env_info(env)->oti_attr;
-        int              result = 0;
-        ENTRY;
-
-        if (oio->oi_lockless == 0) {
-               OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_DELAY_SETTIME, 1);
-                cl_object_attr_lock(obj);
-                result = cl_object_attr_get(env, obj, attr);
-                if (result == 0) {
-                        attr->cat_mtime = attr->cat_ctime =
-                                LTIME_S(CFS_CURRENT_TIME);
-                        result = cl_object_attr_set(env, obj, attr,
-                                                    CAT_MTIME | CAT_CTIME);
-                }
-                cl_object_attr_unlock(obj);
-        }
-        RETURN(result);
+int osc_io_write_start(const struct lu_env *env,
+                      const struct cl_io_slice *slice)
+{
+       struct cl_object *obj   = slice->cis_obj;
+       struct cl_attr   *attr  = &osc_env_info(env)->oti_attr;
+       int rc = 0;
+       ENTRY;
+
+       CFS_FAIL_TIMEOUT(OBD_FAIL_OSC_DELAY_SETTIME, 1);
+       cl_object_attr_lock(obj);
+       attr->cat_mtime = attr->cat_ctime = ktime_get_real_seconds();
+       rc = cl_object_attr_update(env, obj, attr, CAT_MTIME | CAT_CTIME);
+       cl_object_attr_unlock(obj);
+
+       RETURN(rc);
 }
+EXPORT_SYMBOL(osc_io_write_start);
 
-static int osc_fsync_ost(const struct lu_env *env, struct osc_object *obj,
-                        struct cl_fsync_io *fio)
+int osc_fsync_ost(const struct lu_env *env, struct osc_object *obj,
+                 struct cl_fsync_io *fio)
 {
        struct osc_io    *oio   = osc_env_io(env);
        struct obdo      *oa    = &oio->oi_oa;
-       struct obd_info  *oinfo = &oio->oi_info;
        struct lov_oinfo *loi   = obj->oo_oinfo;
        struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
        int rc = 0;
@@ -587,70 +943,96 @@ static int osc_fsync_ost(const struct lu_env *env, struct osc_object *obj,
 
        obdo_set_parent_fid(oa, fio->fi_fid);
 
-       memset(oinfo, 0, sizeof(*oinfo));
-       oinfo->oi_oa = oa;
-       oinfo->oi_capa = fio->fi_capa;
        init_completion(&cbargs->opc_sync);
 
-       rc = osc_sync_base(osc_export(obj), oinfo, osc_async_upcall, cbargs,
-                          PTLRPCD_SET);
+       rc = osc_sync_base(obj, oa, osc_async_upcall, cbargs, PTLRPCD_SET);
        RETURN(rc);
 }
+EXPORT_SYMBOL(osc_fsync_ost);
 
 static int osc_io_fsync_start(const struct lu_env *env,
                              const struct cl_io_slice *slice)
 {
-       struct cl_io       *io  = slice->cis_io;
+       struct cl_io *io = slice->cis_io;
        struct cl_fsync_io *fio = &io->u.ci_fsync;
-       struct cl_object   *obj = slice->cis_obj;
-       struct osc_object  *osc = cl2osc(obj);
-       pgoff_t start  = cl_index(obj, fio->fi_start);
-       pgoff_t end    = cl_index(obj, fio->fi_end);
-       int     result = 0;
+       struct cl_object *obj = slice->cis_obj;
+       struct osc_object *osc = cl2osc(obj);
+       pgoff_t start = fio->fi_start >> PAGE_SHIFT;
+       pgoff_t end = fio->fi_end >> PAGE_SHIFT;
+       int result = 0;
+
        ENTRY;
 
+       if (fio->fi_mode == CL_FSYNC_RECLAIM) {
+               struct client_obd *cli = osc_cli(osc);
+
+               if (!atomic_long_read(&cli->cl_unstable_count)) {
+                       /* Stop flush when there are no unstable pages? */
+                       CDEBUG(D_CACHE, "unstable count is zero\n");
+                       RETURN(0);
+               }
+       }
+
        if (fio->fi_end == OBD_OBJECT_EOF)
                end = CL_PAGE_EOF;
 
        result = osc_cache_writeback_range(env, osc, start, end, 0,
                                           fio->fi_mode == CL_FSYNC_DISCARD);
+       if (result < 0 && fio->fi_mode == CL_FSYNC_DISCARD) {
+               CDEBUG(D_CACHE,
+                      "%s: ignore error %d on discarding "DFID":[%lu-%lu]\n",
+                      cli_name(osc_cli(osc)), result, PFID(fio->fi_fid),
+                      start, end);
+               result = 0;
+       }
        if (result > 0) {
                fio->fi_nr_written += result;
                result = 0;
        }
-       if (fio->fi_mode == CL_FSYNC_ALL) {
+       if (fio->fi_mode == CL_FSYNC_ALL || fio->fi_mode == CL_FSYNC_RECLAIM) {
+               struct osc_io *oio = cl2osc_io(env, slice);
+               struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
                int rc;
 
                /* we have to wait for writeback to finish before we can
                 * send OST_SYNC RPC. This is bad because it causes extents
                 * to be written osc by osc. However, we usually start
                 * writeback before CL_FSYNC_ALL so this won't have any real
-                * problem. */
-               rc = osc_cache_wait_range(env, osc, start, end);
-               if (result == 0)
-                       result = rc;
+                * problem.
+                * We do not have to wait for waitback to finish in the memory
+                * reclaim environment.
+                */
+               if (fio->fi_mode == CL_FSYNC_ALL) {
+                       rc = osc_cache_wait_range(env, osc, start, end);
+                       if (result == 0)
+                               result = rc;
+               }
+
                rc = osc_fsync_ost(env, osc, fio);
-               if (result == 0)
+               if (result == 0) {
+                       cbargs->opc_rpc_sent = 1;
                        result = rc;
+               }
        }
 
        RETURN(result);
 }
 
-static void osc_io_fsync_end(const struct lu_env *env,
-                            const struct cl_io_slice *slice)
+void osc_io_fsync_end(const struct lu_env *env,
+                     const struct cl_io_slice *slice)
 {
        struct cl_fsync_io *fio = &slice->cis_io->u.ci_fsync;
-       struct cl_object   *obj = slice->cis_obj;
-       pgoff_t start = cl_index(obj, fio->fi_start);
-       pgoff_t end   = cl_index(obj, fio->fi_end);
+       struct cl_object *obj = slice->cis_obj;
+       struct osc_io *oio = cl2osc_io(env, slice);
+       struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       pgoff_t start = fio->fi_start >> PAGE_SHIFT;
+       pgoff_t end   = fio->fi_end >> PAGE_SHIFT;
        int result = 0;
 
        if (fio->fi_mode == CL_FSYNC_LOCAL) {
                result = osc_cache_wait_range(env, cl2osc(obj), start, end);
-       } else if (fio->fi_mode == CL_FSYNC_ALL) {
-               struct osc_io           *oio    = cl2osc_io(env, slice);
-               struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       } else if (cbargs->opc_rpc_sent && (fio->fi_mode == CL_FSYNC_ALL ||
+                                           fio->fi_mode == CL_FSYNC_RECLAIM)) {
 
                wait_for_completion(&cbargs->opc_sync);
                if (result == 0)
@@ -658,9 +1040,79 @@ static void osc_io_fsync_end(const struct lu_env *env,
        }
        slice->cis_io->ci_result = result;
 }
+EXPORT_SYMBOL(osc_io_fsync_end);
 
-static void osc_io_end(const struct lu_env *env,
-                      const struct cl_io_slice *slice)
+static int osc_io_ladvise_start(const struct lu_env *env,
+                               const struct cl_io_slice *slice)
+{
+       int                      result = 0;
+       struct cl_io            *io = slice->cis_io;
+       struct osc_io           *oio = cl2osc_io(env, slice);
+       struct cl_object        *obj = slice->cis_obj;
+       struct lov_oinfo        *loi = cl2osc(obj)->oo_oinfo;
+       struct cl_ladvise_io    *lio = &io->u.ci_ladvise;
+       struct obdo             *oa = &oio->oi_oa;
+       struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       struct lu_ladvise       *ladvise;
+       struct ladvise_hdr      *ladvise_hdr;
+       int                      buf_size;
+       int                      num_advise = 1;
+       ENTRY;
+
+       /* TODO: add multiple ladvise support in CLIO */
+       buf_size = offsetof(typeof(*ladvise_hdr), lah_advise[num_advise]);
+       if (osc_env_info(env)->oti_ladvise_buf.lb_len < buf_size)
+               lu_buf_realloc(&osc_env_info(env)->oti_ladvise_buf, buf_size);
+
+       ladvise_hdr = osc_env_info(env)->oti_ladvise_buf.lb_buf;
+       if (ladvise_hdr == NULL)
+               RETURN(-ENOMEM);
+
+       memset(ladvise_hdr, 0, buf_size);
+       ladvise_hdr->lah_magic = LADVISE_MAGIC;
+       ladvise_hdr->lah_count = num_advise;
+       ladvise_hdr->lah_flags = lio->lio_flags;
+
+       memset(oa, 0, sizeof(*oa));
+       oa->o_oi = loi->loi_oi;
+       oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+       obdo_set_parent_fid(oa, lio->lio_fid);
+
+       ladvise = ladvise_hdr->lah_advise;
+       ladvise->lla_start = lio->lio_start;
+       ladvise->lla_end = lio->lio_end;
+       ladvise->lla_advice = lio->lio_advice;
+
+       if (lio->lio_flags & LF_ASYNC) {
+               result = osc_ladvise_base(osc_export(cl2osc(obj)), oa,
+                                         ladvise_hdr, NULL, NULL, NULL);
+       } else {
+               init_completion(&cbargs->opc_sync);
+               result = osc_ladvise_base(osc_export(cl2osc(obj)), oa,
+                                         ladvise_hdr, osc_async_upcall,
+                                         cbargs, PTLRPCD_SET);
+               cbargs->opc_rpc_sent = result == 0;
+       }
+       RETURN(result);
+}
+
+static void osc_io_ladvise_end(const struct lu_env *env,
+                              const struct cl_io_slice *slice)
+{
+       struct cl_io            *io = slice->cis_io;
+       struct osc_io           *oio = cl2osc_io(env, slice);
+       struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       int                      result = 0;
+       struct cl_ladvise_io    *lio = &io->u.ci_ladvise;
+
+       if ((!(lio->lio_flags & LF_ASYNC)) && cbargs->opc_rpc_sent) {
+               wait_for_completion(&cbargs->opc_sync);
+               result = cbargs->opc_rc;
+       }
+       slice->cis_io->ci_result = result;
+}
+
+void osc_io_end(const struct lu_env *env, const struct cl_io_slice *slice)
 {
        struct osc_io *oio = cl2osc_io(env, slice);
 
@@ -669,23 +1121,186 @@ static void osc_io_end(const struct lu_env *env,
                oio->oi_active = NULL;
        }
 }
+EXPORT_SYMBOL(osc_io_end);
+
+struct osc_lseek_args {
+       struct osc_io *lsa_oio;
+};
+
+static int osc_lseek_interpret(const struct lu_env *env,
+                              struct ptlrpc_request *req,
+                              void *arg, int rc)
+{
+       struct ost_body *reply;
+       struct osc_lseek_args *lsa = arg;
+       struct osc_io *oio = lsa->lsa_oio;
+       struct cl_io *io = oio->oi_cl.cis_io;
+       struct cl_lseek_io *lsio = &io->u.ci_lseek;
+
+       ENTRY;
+
+       if (rc != 0)
+               GOTO(out, rc);
+
+       reply = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
+       if (reply == NULL)
+               GOTO(out, rc = -EPROTO);
+
+       lsio->ls_result = reply->oa.o_size;
+out:
+       osc_async_upcall(&oio->oi_cbarg, rc);
+       RETURN(rc);
+}
+
+int osc_io_lseek_start(const struct lu_env *env,
+                      const struct cl_io_slice *slice)
+{
+       struct cl_io *io = slice->cis_io;
+       struct osc_io *oio = cl2osc_io(env, slice);
+       struct cl_object *obj = slice->cis_obj;
+       struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
+       struct cl_lseek_io *lsio = &io->u.ci_lseek;
+       struct obdo *oa = &oio->oi_oa;
+       struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       struct obd_export *exp = osc_export(cl2osc(obj));
+       struct ptlrpc_request *req;
+       struct ost_body *body;
+       struct osc_lseek_args *lsa;
+       int rc = 0;
+
+       ENTRY;
+
+       /* No negative values at this point */
+       LASSERT(lsio->ls_start >= 0);
+       LASSERT(lsio->ls_whence == SEEK_HOLE || lsio->ls_whence == SEEK_DATA);
+
+       /* with IO lock taken we have object size in LVB and can check
+        * boundaries prior sending LSEEK RPC
+        */
+       if (lsio->ls_start >= loi->loi_lvb.lvb_size) {
+               /* consider area beyond end of object as hole */
+               if (lsio->ls_whence == SEEK_HOLE)
+                       lsio->ls_result = lsio->ls_start;
+               else
+                       lsio->ls_result = -ENXIO;
+               RETURN(0);
+       }
+
+       /* if LSEEK RPC is not supported by server, consider whole stripe
+        * object is data with hole after end of object
+        */
+       if (!exp_connect_lseek(exp)) {
+               if (lsio->ls_whence == SEEK_HOLE)
+                       lsio->ls_result = loi->loi_lvb.lvb_size;
+               else
+                       lsio->ls_result = lsio->ls_start;
+               RETURN(0);
+       }
+
+       memset(oa, 0, sizeof(*oa));
+       oa->o_oi = loi->loi_oi;
+       oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+       oa->o_size = lsio->ls_start;
+       oa->o_mode = lsio->ls_whence;
+       if (oio->oi_lockless) {
+               oa->o_flags = OBD_FL_SRVLOCK;
+               oa->o_valid |= OBD_MD_FLFLAGS;
+       }
+
+       init_completion(&cbargs->opc_sync);
+       req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_OST_SEEK);
+       if (req == NULL)
+               RETURN(-ENOMEM);
+
+       rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_SEEK);
+       if (rc < 0) {
+               ptlrpc_request_free(req);
+               RETURN(rc);
+       }
+
+       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
+       ptlrpc_request_set_replen(req);
+       req->rq_interpret_reply = osc_lseek_interpret;
+       lsa = ptlrpc_req_async_args(lsa, req);
+       lsa->lsa_oio = oio;
+
+       ptlrpcd_add_req(req);
+       cbargs->opc_rpc_sent = 1;
+
+       RETURN(0);
+}
+EXPORT_SYMBOL(osc_io_lseek_start);
+
+void osc_io_lseek_end(const struct lu_env *env,
+                     const struct cl_io_slice *slice)
+{
+       struct osc_io *oio = cl2osc_io(env, slice);
+       struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       int rc = 0;
+
+       if (cbargs->opc_rpc_sent) {
+               wait_for_completion(&cbargs->opc_sync);
+               rc = cbargs->opc_rc;
+       }
+       slice->cis_io->ci_result = rc;
+}
+EXPORT_SYMBOL(osc_io_lseek_end);
+
+int osc_io_lru_reserve(const struct lu_env *env,
+                      const struct cl_io_slice *ios,
+                      loff_t pos, size_t bytes)
+{
+       struct osc_object *osc = cl2osc(ios->cis_obj);
+       struct osc_io *oio = osc_env_io(env);
+       unsigned long npages = 0;
+       size_t page_offset;
+
+       ENTRY;
+
+       page_offset = pos & ~PAGE_MASK;
+       if (page_offset) {
+               ++npages;
+               if (bytes > PAGE_SIZE - page_offset)
+                       bytes -= (PAGE_SIZE - page_offset);
+               else
+                       bytes = 0;
+       }
+       npages += (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+       oio->oi_lru_reserved = osc_lru_reserve(osc_cli(osc), npages);
+
+       RETURN(0);
+}
+EXPORT_SYMBOL(osc_io_lru_reserve);
 
 static const struct cl_io_operations osc_io_ops = {
-        .op = {
-                [CIT_READ] = {
-                        .cio_start  = osc_io_read_start,
-                        .cio_fini   = osc_io_fini
-                },
-                [CIT_WRITE] = {
-                        .cio_start  = osc_io_write_start,
+       .op = {
+               [CIT_READ] = {
+                       .cio_iter_init = osc_io_iter_init,
+                       .cio_iter_fini = osc_io_rw_iter_fini,
+                       .cio_start  = osc_io_read_start,
+                       .cio_fini   = osc_io_fini
+               },
+               [CIT_WRITE] = {
+                       .cio_iter_init = osc_io_iter_init,
+                       .cio_iter_fini = osc_io_rw_iter_fini,
+                       .cio_start  = osc_io_write_start,
                        .cio_end    = osc_io_end,
-                        .cio_fini   = osc_io_fini
-                },
-                [CIT_SETATTR] = {
-                        .cio_start  = osc_io_setattr_start,
-                        .cio_end    = osc_io_setattr_end
-                },
-                [CIT_FAULT] = {
+                       .cio_fini   = osc_io_fini
+               },
+               [CIT_SETATTR] = {
+                       .cio_iter_init = osc_io_iter_init,
+                       .cio_iter_fini = osc_io_iter_fini,
+                       .cio_start  = osc_io_setattr_start,
+                       .cio_end    = osc_io_setattr_end
+               },
+               [CIT_DATA_VERSION] = {
+                       .cio_start  = osc_io_data_version_start,
+                       .cio_end    = osc_io_data_version_end,
+               },
+               [CIT_FAULT] = {
+                       .cio_iter_init = osc_io_iter_init,
+                       .cio_iter_fini = osc_io_iter_fini,
                        .cio_start  = osc_io_fault_start,
                        .cio_end    = osc_io_end,
                        .cio_fini   = osc_io_fini
@@ -695,20 +1310,25 @@ static const struct cl_io_operations osc_io_ops = {
                        .cio_end    = osc_io_fsync_end,
                        .cio_fini   = osc_io_fini
                },
-                [CIT_MISC] = {
-                        .cio_fini   = osc_io_fini
-                }
-        },
-        .req_op = {
-                 [CRT_READ] = {
-                         .cio_submit    = osc_io_submit
-                 },
-                 [CRT_WRITE] = {
-                         .cio_submit    = osc_io_submit
-                 }
-         },
-        .cio_prepare_write = osc_io_prepare_write,
-        .cio_commit_write  = osc_io_commit_write
+               [CIT_LADVISE] = {
+                       .cio_start  = osc_io_ladvise_start,
+                       .cio_end    = osc_io_ladvise_end,
+                       .cio_fini   = osc_io_fini
+               },
+               [CIT_LSEEK] = {
+                       .cio_start  = osc_io_lseek_start,
+                       .cio_end    = osc_io_lseek_end,
+                       .cio_fini   = osc_io_fini
+               },
+               [CIT_MISC] = {
+                       .cio_fini   = osc_io_fini
+               }
+       },
+       .cio_read_ahead             = osc_io_read_ahead,
+       .cio_lru_reserve            = osc_io_lru_reserve,
+       .cio_submit                 = osc_io_submit,
+       .cio_commit_async           = osc_io_commit_async,
+       .cio_extent_release         = osc_io_extent_release
 };
 
 /*****************************************************************************
@@ -717,129 +1337,19 @@ static const struct cl_io_operations osc_io_ops = {
  *
  */
 
-static int osc_req_prep(const struct lu_env *env,
-                        const struct cl_req_slice *slice)
-{
-        return 0;
-}
-
-static void osc_req_completion(const struct lu_env *env,
-                               const struct cl_req_slice *slice, int ioret)
-{
-        struct osc_req *or;
-
-        or = cl2osc_req(slice);
-        OBD_SLAB_FREE_PTR(or, osc_req_kmem);
-}
-
-/**
- * Implementation of struct cl_req_operations::cro_attr_set() for osc
- * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
- * fields.
- */
-static void osc_req_attr_set(const struct lu_env *env,
-                            const struct cl_req_slice *slice,
-                            const struct cl_object *obj,
-                            struct cl_req_attr *attr, obd_valid flags)
-{
-       struct lov_oinfo *oinfo;
-       struct cl_req    *clerq;
-       struct cl_page   *apage; /* _some_ page in @clerq */
-       struct cl_lock   *lock;  /* _some_ lock protecting @apage */
-       struct osc_lock  *olck;
-       struct osc_page  *opg;
-       struct obdo      *oa;
-       struct ost_lvb   *lvb;
-
-       oinfo   = cl2osc(obj)->oo_oinfo;
-       lvb     = &oinfo->loi_lvb;
-       oa      = attr->cra_oa;
-
-       if ((flags & OBD_MD_FLMTIME) != 0) {
-               oa->o_mtime = lvb->lvb_mtime;
-               oa->o_valid |= OBD_MD_FLMTIME;
-       }
-       if ((flags & OBD_MD_FLATIME) != 0) {
-               oa->o_atime = lvb->lvb_atime;
-               oa->o_valid |= OBD_MD_FLATIME;
-       }
-       if ((flags & OBD_MD_FLCTIME) != 0) {
-               oa->o_ctime = lvb->lvb_ctime;
-               oa->o_valid |= OBD_MD_FLCTIME;
-       }
-       if (flags & OBD_MD_FLGROUP) {
-               ostid_set_seq(&oa->o_oi, ostid_seq(&oinfo->loi_oi));
-               oa->o_valid |= OBD_MD_FLGROUP;
-       }
-       if (flags & OBD_MD_FLID) {
-               ostid_set_id(&oa->o_oi, ostid_id(&oinfo->loi_oi));
-               oa->o_valid |= OBD_MD_FLID;
-       }
-       if (flags & OBD_MD_FLHANDLE) {
-                clerq = slice->crs_req;
-                LASSERT(!cfs_list_empty(&clerq->crq_pages));
-                apage = container_of(clerq->crq_pages.next,
-                                     struct cl_page, cp_flight);
-                opg = osc_cl_page_osc(apage);
-                apage = opg->ops_cl.cpl_page; /* now apage is a sub-page */
-                lock = cl_lock_at_page(env, apage->cp_obj, apage, NULL, 1, 1);
-                if (lock == NULL) {
-                        struct cl_object_header *head;
-                        struct cl_lock          *scan;
-
-                        head = cl_object_header(apage->cp_obj);
-                        cfs_list_for_each_entry(scan, &head->coh_locks,
-                                                cll_linkage)
-                                CL_LOCK_DEBUG(D_ERROR, env, scan,
-                                              "no cover page!\n");
-                        CL_PAGE_DEBUG(D_ERROR, env, apage,
-                                      "dump uncover page!\n");
-                        libcfs_debug_dumpstack(NULL);
-                        LBUG();
-                }
-
-                olck = osc_lock_at(lock);
-                LASSERT(olck != NULL);
-                LASSERT(ergo(opg->ops_srvlock, olck->ols_lock == NULL));
-                /* check for lockless io. */
-                if (olck->ols_lock != NULL) {
-                        oa->o_handle = olck->ols_lock->l_remote_handle;
-                        oa->o_valid |= OBD_MD_FLHANDLE;
-                }
-                cl_lock_put(env, lock);
-        }
-}
-
-static const struct cl_req_operations osc_req_ops = {
-        .cro_prep       = osc_req_prep,
-        .cro_attr_set   = osc_req_attr_set,
-        .cro_completion = osc_req_completion
-};
-
-
 int osc_io_init(const struct lu_env *env,
                 struct cl_object *obj, struct cl_io *io)
 {
+       struct obd_export *exp = osc_export(cl2osc(obj));
         struct osc_io *oio = osc_env_io(env);
 
         CL_IO_SLICE_CLEAN(oio, oi_cl);
         cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
-        return 0;
-}
 
-int osc_req_init(const struct lu_env *env, struct cl_device *dev,
-                 struct cl_req *req)
-{
-        struct osc_req *or;
-        int result;
+       if (!exp_connect_unaligned_dio(exp))
+               cl_io_top(io)->ci_allow_unaligned_dio = false;
 
-       OBD_SLAB_ALLOC_PTR_GFP(or, osc_req_kmem, __GFP_IO);
-        if (or != NULL) {
-                cl_req_slice_add(req, &or->or_cl, dev, &osc_req_ops);
-                result = 0;
-        } else
-                result = -ENOMEM;
-        return result;
+        return 0;
 }
 
 /** @} osc */