Whamcloud - gitweb
LU-19098 hsm: don't print progname twice with lhsmtool
[fs/lustre-release.git] / lustre / osc / osc_io.c
index 47e56a7..4f9ecd0 100644 (file)
@@ -1,38 +1,19 @@
-/*
- * GPL HEADER START
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 only,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License version 2 for more details (a copy is included
- * in the LICENSE file that accompanied this code).
- *
- * You should have received a copy of the GNU General Public License
- * version 2 along with this program; If not, see
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * GPL HEADER END
- */
+// SPDX-License-Identifier: GPL-2.0
+
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
  * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
  * Copyright (c) 2011, 2017, Intel Corporation.
  */
+
 /*
  * This file is part of Lustre, http://www.lustre.org/
 /*
  * 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.
  *
  *
  * Implementation of cl_io for OSC layer.
  *
- *   Author: Nikita Danilov <nikita.danilov@sun.com>
- *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
+ * Author: Nikita Danilov <nikita.danilov@sun.com>
+ * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
  */
 
 #define DEBUG_SUBSYSTEM S_OSC
  */
 
 #define DEBUG_SUBSYSTEM S_OSC
@@ -43,6 +24,7 @@
 #include <linux/falloc.h>
 
 #include "osc_internal.h"
 #include <linux/falloc.h>
 
 #include "osc_internal.h"
+#include <lnet/lnet_rdma.h>
 
 /** \addtogroup osc
  *  @{
 
 /** \addtogroup osc
  *  @{
@@ -58,14 +40,16 @@ static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
 {
 }
 
 {
 }
 
-void osc_read_ahead_release(const struct lu_env *env, void *cbdata)
+void osc_read_ahead_release(const struct lu_env *env, struct cl_read_ahead *ra)
 {
 {
-       struct ldlm_lock *dlmlock = cbdata;
+       struct ldlm_lock *dlmlock = ra->cra_dlmlock;
+       struct osc_io *oio = ra->cra_oio;
        struct lustre_handle lockh;
 
        struct lustre_handle lockh;
 
+       oio->oi_is_readahead = 0;
        ldlm_lock2handle(dlmlock, &lockh);
        ldlm_lock_decref(&lockh, LCK_PR);
        ldlm_lock2handle(dlmlock, &lockh);
        ldlm_lock_decref(&lockh, LCK_PR);
-       LDLM_LOCK_PUT(dlmlock);
+       ldlm_lock_put(dlmlock);
 }
 EXPORT_SYMBOL(osc_read_ahead_release);
 
 }
 EXPORT_SYMBOL(osc_read_ahead_release);
 
@@ -73,28 +57,38 @@ 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_io_slice *ios,
                             pgoff_t start, struct cl_read_ahead *ra)
 {
-       struct osc_object       *osc = cl2osc(ios->cis_obj);
-       struct ldlm_lock        *dlmlock;
-       int                     result = -ENODATA;
+       struct osc_object *osc = cl2osc(ios->cis_obj);
+       struct osc_io *oio = cl2osc_io(env, ios);
+       struct ldlm_lock *dlmlock;
+       int result = -ENODATA;
+
        ENTRY;
 
        ENTRY;
 
+       oio->oi_is_readahead = true;
        dlmlock = osc_dlmlock_at_pgoff(env, osc, start, 0);
        if (dlmlock != NULL) {
        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;
                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);
                }
 
                ra->cra_rpc_pages = osc_cli(osc)->cl_max_pages_per_rpc;
                        ldlm_lock2handle(dlmlock, &lockh);
                        ldlm_lock_addref(&lockh, LCK_PR);
                        ldlm_lock_decref(&lockh, dlmlock->l_req_mode);
                }
 
                ra->cra_rpc_pages = osc_cli(osc)->cl_max_pages_per_rpc;
-               ra->cra_end_idx = cl_index(osc2cl(osc),
-                                          dlmlock->l_policy_data.l_extent.end);
+               ra->cra_end_idx =
+                       dlmlock->l_policy_data.l_extent.end >> PAGE_SHIFT;
                ra->cra_release = osc_read_ahead_release;
                ra->cra_release = osc_read_ahead_release;
-               ra->cra_cbdata = dlmlock;
+               ra->cra_dlmlock = dlmlock;
+               ra->cra_oio = oio;
                if (ra->cra_end_idx != CL_PAGE_EOF)
                        ra->cra_contention = true;
                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;
        }
 
                result = 0;
        }
 
@@ -108,15 +102,16 @@ static int osc_io_read_ahead(const struct lu_env *env,
  * or, if page is already submitted, changes osc flags through
  * osc_set_async_flags().
  */
  * or, if page is already submitted, changes osc flags through
  * osc_set_async_flags().
  */
-int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
-                 enum cl_req_type crt, struct cl_2queue *queue)
+int osc_io_submit(const struct lu_env *env, struct cl_io *io,
+                 const struct cl_io_slice *ios, enum cl_req_type crt,
+                 struct cl_2queue *queue)
 {
        struct cl_page    *page;
        struct cl_page    *tmp;
 {
        struct cl_page    *page;
        struct cl_page    *tmp;
+       struct cl_io      *top_io = cl_io_top(io);
        struct client_obd *cli  = NULL;
        struct osc_object *osc  = NULL; /* to keep gcc happy */
        struct osc_page   *opg;
        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;
        LIST_HEAD(list);
 
        struct cl_page_list *qin      = &queue->c2_qin;
@@ -128,6 +123,7 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
        unsigned int ppc_bits; /* pages per chunk bits */
        unsigned int ppc;
        bool sync_queue = false;
        unsigned int ppc_bits; /* pages per chunk bits */
        unsigned int ppc;
        bool sync_queue = false;
+       bool dio = false;
 
        LASSERT(qin->pl_nr > 0);
 
 
        LASSERT(qin->pl_nr > 0);
 
@@ -145,8 +141,12 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
                brw_flags |= OBD_BRW_NDELAY;
 
        page = cl_page_list_first(qin);
                brw_flags |= OBD_BRW_NDELAY;
 
        page = cl_page_list_first(qin);
-       if (page->cp_type == CPT_TRANSIENT)
+       if (page->cp_type == CPT_TRANSIENT) {
                brw_flags |= OBD_BRW_NOCACHE;
                brw_flags |= OBD_BRW_NOCACHE;
+               dio = true;
+       }
+       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
 
         /*
          * NOTE: here @page is a top-level page. This is done to avoid
@@ -155,13 +155,10 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
         cl_page_list_for_each_safe(page, tmp, qin) {
                 struct osc_async_page *oap;
 
         cl_page_list_for_each_safe(page, tmp, qin) {
                 struct osc_async_page *oap;
 
-                /* Top level IO. */
-                io = page->cp_owner;
-                LASSERT(io != NULL);
+               LASSERT(top_io != NULL);
 
                opg = osc_cl_page_osc(page, osc);
                oap = &opg->ops_oap;
 
                opg = osc_cl_page_osc(page, osc);
                oap = &opg->ops_oap;
-               LASSERT(osc == oap->oap_obj);
 
                if (!list_empty(&oap->oap_pending_item) ||
                    !list_empty(&oap->oap_rpc_item)) {
 
                if (!list_empty(&oap->oap_pending_item) ||
                    !list_empty(&oap->oap_rpc_item)) {
@@ -171,24 +168,24 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
                         break;
                 }
 
                         break;
                 }
 
-                result = cl_page_prep(env, io, page, crt);
-               if (result != 0) {
-                        LASSERT(result < 0);
-                        if (result != -EALREADY)
-                                break;
-                        /*
-                         * Handle -EALREADY error: for read case, the page is
-                         * already in UPTODATE state; for write, the page
-                         * is not dirty.
-                         */
-                        result = 0;
-                       continue;
-                }
+               if (!dio) {
+                       result = cl_page_prep(env, top_io, page, crt);
+                       if (result != 0) {
+                               LASSERT(result < 0);
+                               if (result != -EALREADY)
+                                       break;
+                               /*
+                                * Handle -EALREADY error: for read case, the
+                                * page is already in UPTODATE state; for
+                                * write, the page is not dirty.
+                                */
+                               result = 0;
+                               continue;
+                       }
+               }
 
 
-               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 (!dio)
+                       oap->oap_async_flags = ASYNC_URGENT|ASYNC_READY|ASYNC_COUNT_STABLE;
 
                osc_page_submit(env, opg, crt, brw_flags);
                list_add_tail(&oap->oap_pending_item, &list);
 
                osc_page_submit(env, opg, crt, brw_flags);
                list_add_tail(&oap->oap_pending_item, &list);
@@ -196,7 +193,7 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
                if (page->cp_sync_io != NULL)
                        cl_page_list_move(qout, qin, page);
                else /* async IO */
                if (page->cp_sync_io != NULL)
                        cl_page_list_move(qout, qin, page);
                else /* async IO */
-                       cl_page_list_del(env, qin, page);
+                       cl_page_list_del(env, qin, page, true);
 
                queued++;
                if (queued == max_pages) {
 
                queued++;
                if (queued == max_pages) {
@@ -216,7 +213,7 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
                }
 
                if (sync_queue) {
                }
 
                if (sync_queue) {
-                       result = osc_queue_sync_pages(env, io, osc, &list,
+                       result = osc_queue_sync_pages(env, top_io, osc, &list,
                                                      brw_flags);
                        if (result < 0)
                                break;
                                                      brw_flags);
                        if (result < 0)
                                break;
@@ -226,7 +223,8 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
        }
 
        if (queued > 0)
        }
 
        if (queued > 0)
-               result = osc_queue_sync_pages(env, io, osc, &list, brw_flags);
+               result = osc_queue_sync_pages(env, top_io, osc, &list,
+                                             brw_flags);
 
        /* Update c/mtime for sync write. LU-7310 */
        if (crt == CRT_WRITE && qout->pl_nr > 0 && result == 0) {
 
        /* Update c/mtime for sync write. LU-7310 */
        if (crt == CRT_WRITE && qout->pl_nr > 0 && result == 0) {
@@ -244,6 +242,143 @@ int osc_io_submit(const struct lu_env *env, const struct cl_io_slice *ios,
 }
 EXPORT_SYMBOL(osc_io_submit);
 
 }
 EXPORT_SYMBOL(osc_io_submit);
 
+static int __osc_dio_submit(const struct lu_env *env, struct cl_io *io,
+                           const struct cl_io_slice *ios, enum cl_req_type crt,
+                           struct cl_dio_pages *cdp, struct cl_2queue *queue)
+{
+       struct cl_page_list *qout     = &queue->c2_qout;
+       struct cl_page_list *qin      = &queue->c2_qin;
+       struct osc_object *osc  = cl2osc(ios->cis_obj);
+       struct cl_io      *top_io = cl_io_top(io);
+       struct client_obd *cli  = osc_cli(osc);
+       struct page       *vmpage;
+       LIST_HEAD(list);
+       /* pages per chunk bits */
+       unsigned int ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
+       unsigned int max_pages = cli->cl_max_pages_per_rpc;
+       unsigned int ppc = 1 << ppc_bits;
+       unsigned int total_queued = 0;
+       unsigned int queued = 0;
+       bool sync_queue = false;
+       int result = 0;
+       int brw_flags;
+       int from = -1;
+       int to = -1;
+       int i = 0;
+
+       LASSERT(qin->pl_nr > 0);
+
+       CDEBUG(D_CACHE|D_READA, "%d %d\n", qin->pl_nr, crt);
+
+       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;
+
+       vmpage = cdp->cdp_pages[0];
+       brw_flags |= OBD_BRW_NOCACHE;
+       if (lnet_is_rdma_only_page(vmpage))
+               brw_flags |= OBD_BRW_RDMA_ONLY;
+
+       /*
+        * NOTE: here @page is a top-level page. This is done to avoid
+        *       creation of sub-page-list.
+        */
+       for (i = 0; i < cdp->cdp_page_count; i++) {
+               struct cl_page *page = cdp->cdp_cl_pages[i];
+               struct osc_async_page *oap;
+               struct osc_page   *opg;
+
+               LASSERT(top_io != NULL);
+
+               opg = osc_cl_page_osc(page, osc);
+               oap = &opg->ops_oap;
+               if (from == -1)
+                       from = i;
+
+               osc_page_submit(env, opg, crt, brw_flags);
+               list_add_tail(&oap->oap_pending_item, &list);
+
+               cl_page_list_move(qout, qin, page);
+
+               queued++;
+               total_queued++;
+               if (queued == max_pages) {
+                       sync_queue = true;
+               } else if (crt == CRT_WRITE) {
+                       unsigned int next_chunks;
+                       unsigned int 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;
+               }
+
+               to = i;
+               if (sync_queue) {
+                       result = osc_queue_dio_pages(env, top_io, osc, cdp,
+                                                    &list, from, to,
+                                                    brw_flags);
+                       if (result < 0)
+                               break;
+                       from = -1;
+                       queued = 0;
+                       sync_queue = false;
+               }
+       }
+
+       if (queued > 0) {
+               LASSERT(to != -1);
+               result = osc_queue_dio_pages(env, top_io, osc, cdp, &list,
+                                            from, to, brw_flags);
+       }
+
+       /* Update c/mtime for sync write. LU-7310 */
+       if (crt == CRT_WRITE && total_queued > 0 && result == 0) {
+               struct cl_attr *attr = &osc_env_info(env)->oti_attr;
+               struct cl_object *obj   = ios->cis_obj;
+
+               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);
+       }
+
+       CDEBUG(D_INFO, "%d/%u %d\n", total_queued, cdp->cdp_page_count,
+              result);
+       return total_queued > 0 ? 0 : result;
+}
+
+int osc_dio_submit(const struct lu_env *env, struct cl_io *io,
+                 const struct cl_io_slice *ios, enum cl_req_type crt,
+                 struct cl_dio_pages *cdp)
+{
+       struct cl_2queue *queue;
+       int rc = 0;
+
+       cl_dio_pages_2queue(cdp);
+       queue = &cdp->cdp_queue;
+
+       rc = __osc_dio_submit(env, io, ios, crt, cdp, queue);
+
+       /* if submit failed, no pages were sent */
+       LASSERT(ergo(rc != 0, list_empty(&queue->c2_qout.pl_pages)));
+       while (queue->c2_qout.pl_nr > 0) {
+               struct cl_page *page;
+
+               page = cl_page_list_first(&queue->c2_qout);
+               cl_page_list_del(env, &queue->c2_qout, page, false);
+       }
+
+       RETURN(rc);
+}
+EXPORT_SYMBOL(osc_dio_submit);
+
 /**
  * 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.
 /**
  * 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.
@@ -255,13 +390,13 @@ void osc_page_touch_at(const struct lu_env *env, struct cl_object *obj,
 {
        struct lov_oinfo  *loi  = cl2osc(obj)->oo_oinfo;
        struct cl_attr    *attr = &osc_env_info(env)->oti_attr;
 {
        struct lov_oinfo  *loi  = cl2osc(obj)->oo_oinfo;
        struct cl_attr    *attr = &osc_env_info(env)->oti_attr;
-       int valid;
+       enum cl_attr_valid valid;
        __u64 kms;
 
        ENTRY;
 
        /* offset within stripe */
        __u64 kms;
 
        ENTRY;
 
        /* offset within stripe */
-       kms = cl_offset(obj, idx) + to;
+       kms = (idx << PAGE_SHIFT) + to;
 
        cl_object_attr_lock(obj);
        CDEBUG(D_INODE, "stripe KMS %sincreasing %llu->%llu %llu\n",
 
        cl_object_attr_lock(obj);
        CDEBUG(D_INODE, "stripe KMS %sincreasing %llu->%llu %llu\n",
@@ -289,13 +424,13 @@ int osc_io_commit_async(const struct lu_env *env,
                        struct cl_page_list *qin, int from, int to,
                        cl_commit_cbt cb)
 {
                        struct cl_page_list *qin, int from, int to,
                        cl_commit_cbt cb)
 {
-       struct cl_io    *io = ios->cis_io;
-       struct osc_io   *oio = cl2osc_io(env, ios);
+       struct cl_io *io = ios->cis_io;
+       struct osc_io *oio = cl2osc_io(env, ios);
        struct osc_object *osc = cl2osc(ios->cis_obj);
        struct osc_object *osc = cl2osc(ios->cis_obj);
-       struct cl_page  *page;
-       struct cl_page  *last_page;
+       struct cl_page *page;
+       struct cl_page *last_page;
        struct osc_page *opg;
        struct osc_page *opg;
-       struct pagevec  *pvec = &osc_env_info(env)->oti_pagevec;
+       struct folio_batch *fbatch = &osc_env_info(env)->oti_fbatch;
        int result = 0;
        ENTRY;
 
        int result = 0;
        ENTRY;
 
@@ -315,7 +450,7 @@ int osc_io_commit_async(const struct lu_env *env,
                }
        }
 
                }
        }
 
-       ll_pagevec_init(pvec, 0);
+       ll_folio_batch_init(fbatch, 0);
 
        while (qin->pl_nr > 0) {
                struct osc_async_page *oap;
 
        while (qin->pl_nr > 0) {
                struct osc_async_page *oap;
@@ -324,9 +459,6 @@ int osc_io_commit_async(const struct lu_env *env,
                opg = osc_cl_page_osc(page, osc);
                oap = &opg->ops_oap;
 
                opg = osc_cl_page_osc(page, osc);
                oap = &opg->ops_oap;
 
-               LASSERTF(osc == oap->oap_obj,
-                        "obj mismatch: %p / %p\n", osc, oap->oap_obj);
-
                if (!list_empty(&oap->oap_rpc_item)) {
                        CDEBUG(D_CACHE, "Busy oap %p page %p for submit.\n",
                               oap, opg);
                if (!list_empty(&oap->oap_rpc_item)) {
                        CDEBUG(D_CACHE, "Busy oap %p page %p for submit.\n",
                               oap, opg);
@@ -336,7 +468,7 @@ int osc_io_commit_async(const struct lu_env *env,
 
                /* The page may be already in dirty cache. */
                if (list_empty(&oap->oap_pending_item)) {
 
                /* The page may be already in dirty cache. */
                if (list_empty(&oap->oap_pending_item)) {
-                       result = osc_page_cache_add(env, opg, io, cb);
+                       result = osc_page_cache_add(env, osc, opg, io, cb);
                        if (result != 0)
                                break;
                }
                        if (result != 0)
                                break;
                }
@@ -344,18 +476,23 @@ int osc_io_commit_async(const struct lu_env *env,
                osc_page_touch_at(env, osc2cl(osc), osc_index(opg),
                                  page == last_page ? to : PAGE_SIZE);
 
                osc_page_touch_at(env, osc2cl(osc), osc_index(opg),
                                  page == last_page ? to : PAGE_SIZE);
 
-               cl_page_list_del(env, qin, page);
+               cl_page_list_del(env, qin, page, true);
 
                /* if there are no more slots, do the callback & reinit */
 
                /* if there are no more slots, do the callback & reinit */
-               if (pagevec_add(pvec, page->cp_vmpage) == 0) {
-                       (*cb)(env, io, pvec);
-                       pagevec_reinit(pvec);
+               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 pagevecs */
-       if (pagevec_count(pvec) != 0)
-               (*cb)(env, io, pvec);
+
+       /* 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. */
 
        /* Can't access these pages any more. Page can be in transfer and
         * complete at any time. */
@@ -373,6 +510,18 @@ int osc_io_commit_async(const struct lu_env *env,
 }
 EXPORT_SYMBOL(osc_io_commit_async);
 
 }
 EXPORT_SYMBOL(osc_io_commit_async);
 
+void osc_io_extent_release(const struct lu_env *env,
+                          const struct cl_io_slice *ios)
+{
+       struct osc_io *oio = cl2osc_io(env, ios);
+
+       if (oio->oi_active != NULL) {
+               osc_extent_release(env, oio->oi_active);
+               oio->oi_active = NULL;
+       }
+}
+EXPORT_SYMBOL(osc_io_extent_release);
+
 static bool osc_import_not_healthy(struct obd_import *imp)
 {
        return imp->imp_invalid || imp->imp_deactive ||
 static bool osc_import_not_healthy(struct obd_import *imp)
 {
        return imp->imp_invalid || imp->imp_deactive ||
@@ -386,6 +535,7 @@ int osc_io_iter_init(const struct lu_env *env, const struct cl_io_slice *ios)
        struct obd_import *imp = osc_cli(osc)->cl_import;
        struct osc_io *oio = osc_env_io(env);
        int rc = -EIO;
        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);
        ENTRY;
 
        spin_lock(&imp->imp_lock);
@@ -395,7 +545,7 @@ int osc_io_iter_init(const struct lu_env *env, const struct cl_io_slice *ios)
         */
        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)) {
         */
        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 = -EWOULDBLOCK;
+               rc = -EAGAIN;
        } else if (likely(!imp->imp_invalid)) {
                atomic_inc(&osc->oo_nr_ios);
                oio->oi_is_active = 1;
        } else if (likely(!imp->imp_invalid)) {
                atomic_inc(&osc->oo_nr_ios);
                oio->oi_is_active = 1;
@@ -403,35 +553,13 @@ int osc_io_iter_init(const struct lu_env *env, const struct cl_io_slice *ios)
        }
        spin_unlock(&imp->imp_lock);
 
        }
        spin_unlock(&imp->imp_lock);
 
-       if (cfs_capable(CFS_CAP_SYS_RESOURCE))
+       if (capable(CAP_SYS_RESOURCE))
                oio->oi_cap_sys_resource = 1;
 
        RETURN(rc);
 }
 EXPORT_SYMBOL(osc_io_iter_init);
 
                oio->oi_cap_sys_resource = 1;
 
        RETURN(rc);
 }
 EXPORT_SYMBOL(osc_io_iter_init);
 
-int osc_io_rw_iter_init(const struct lu_env *env,
-                       const struct cl_io_slice *ios)
-{
-       struct cl_io *io = ios->cis_io;
-       struct osc_io *oio = osc_env_io(env);
-       struct osc_object *osc = cl2osc(ios->cis_obj);
-       unsigned long npages;
-       ENTRY;
-
-       if (cl_io_is_append(io))
-               RETURN(osc_io_iter_init(env, ios));
-
-       npages = io->u.ci_rw.crw_count >> PAGE_SHIFT;
-       if (io->u.ci_rw.crw_pos & ~PAGE_MASK)
-               ++npages;
-
-       oio->oi_lru_reserved = osc_lru_reserve(osc_cli(osc), npages);
-
-       RETURN(osc_io_iter_init(env, ios));
-}
-EXPORT_SYMBOL(osc_io_rw_iter_init);
-
 void osc_io_iter_fini(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)
 {
@@ -443,7 +571,7 @@ void osc_io_iter_fini(const struct lu_env *env,
                oio->oi_is_active = 0;
                LASSERT(atomic_read(&osc->oo_nr_ios) > 0);
                if (atomic_dec_and_test(&osc->oo_nr_ios))
                oio->oi_is_active = 0;
                LASSERT(atomic_read(&osc->oo_nr_ios) > 0);
                if (atomic_dec_and_test(&osc->oo_nr_ios))
-                       wake_up_all(&osc->oo_io_waitq);
+                       wake_up(&osc->oo_io_waitq);
        }
 }
 EXPORT_SYMBOL(osc_io_iter_fini);
        }
 }
 EXPORT_SYMBOL(osc_io_iter_fini);
@@ -459,6 +587,7 @@ void osc_io_rw_iter_fini(const struct lu_env *env,
                oio->oi_lru_reserved = 0;
        }
        oio->oi_write_osclock = NULL;
                oio->oi_lru_reserved = 0;
        }
        oio->oi_write_osclock = NULL;
+       oio->oi_read_osclock = NULL;
 
        osc_io_iter_fini(env, ios);
 }
 
        osc_io_iter_fini(env, ios);
 }
@@ -473,7 +602,7 @@ int osc_io_fault_start(const struct lu_env *env, const struct cl_io_slice *ios)
        io  = ios->cis_io;
        fio = &io->u.ci_fault;
        CDEBUG(D_INFO, "%lu %d %zu\n",
        io  = ios->cis_io;
        fio = &io->u.ci_fault;
        CDEBUG(D_INFO, "%lu %d %zu\n",
-               fio->ft_index, fio->ft_writable, fio->ft_nob);
+               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.
        /*
         * If mapping is writeable, adjust kms to cover this page,
         * but do not extend kms beyond actual file size.
@@ -481,7 +610,7 @@ int osc_io_fault_start(const struct lu_env *env, const struct cl_io_slice *ios)
         */
        if (fio->ft_writable)
                osc_page_touch_at(env, ios->cis_obj,
         */
        if (fio->ft_writable)
                osc_page_touch_at(env, ios->cis_obj,
-                                 fio->ft_index, fio->ft_nob);
+                                 fio->ft_index, fio->ft_bytes);
        RETURN(0);
 }
 EXPORT_SYMBOL(osc_io_fault_start);
        RETURN(0);
 }
 EXPORT_SYMBOL(osc_io_fault_start);
@@ -500,22 +629,26 @@ static int osc_async_upcall(void *a, int rc)
  * Checks that there are no pages being written in the extent being truncated.
  */
 static bool trunc_check_cb(const struct lu_env *env, struct cl_io *io,
  * Checks that there are no pages being written in the extent being truncated.
  */
 static bool trunc_check_cb(const struct lu_env *env, struct cl_io *io,
-                         struct osc_page *ops , void *cbdata)
+                          void **pvec, int count, void *cbdata)
 {
 {
-       struct cl_page *page = ops->ops_cl.cpl_page;
-       struct osc_async_page *oap;
-       __u64 start = *(__u64 *)cbdata;
+       int i;
 
 
-       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);
+       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;
 
 
-       if (PageLocked(page->cp_vmpage))
-               CDEBUG(D_CACHE, "page %p index %lu locked for %d.\n",
-                      ops, osc_index(ops), oap->oap_cmd & OBD_BRW_RWMASK);
+               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);
+       }
        return true;
 }
 
        return true;
 }
 
@@ -526,9 +659,9 @@ static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
        int     partial;
        pgoff_t start;
 
        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.
 
         /*
          * Complain if there are pages in the truncated region.
@@ -538,6 +671,38 @@ static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
                                trunc_check_cb, (void *)&size);
 }
 
                                trunc_check_cb, (void *)&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)
+{
+       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);
+}
+EXPORT_SYMBOL(osc_punch_start);
+
+static inline void osc_set_projid_info(const struct lu_env *env,
+                                      struct cl_object *obj, struct obdo *oa)
+{
+       if (!(oa->o_valid & OBD_MD_FLPROJID))
+               cl_req_projid_set(env, obj, &oa->o_projid);
+}
+
 static int osc_io_setattr_start(const struct lu_env *env,
                                 const struct cl_io_slice *slice)
 {
 static int osc_io_setattr_start(const struct lu_env *env,
                                 const struct cl_io_slice *slice)
 {
@@ -552,36 +717,29 @@ static int osc_io_setattr_start(const struct lu_env *env,
        enum op_xvalid ia_xvalid = io->u.ci_setattr.sa_xvalid;
        int result = 0;
        __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
        enum op_xvalid ia_xvalid = io->u.ci_setattr.sa_xvalid;
        int result = 0;
        __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
-       __u64 end = OBD_OBJECT_EOF;
-       bool io_is_falloc = false;
+       bool io_is_falloc = cl_io_is_fallocate(io);
 
        ENTRY;
        /* truncate cache dirty pages first */
 
        ENTRY;
        /* truncate cache dirty pages first */
-       if (cl_io_is_trunc(io)) {
+       if (cl_io_is_trunc(io))
                result = osc_cache_truncate_start(env, cl2osc(obj), size,
                                                  &oio->oi_trunc);
                result = osc_cache_truncate_start(env, cl2osc(obj), size,
                                                  &oio->oi_trunc);
-       } else if (cl_io_is_fallocate(io)) {
-               io_is_falloc = true;
-               size = io->u.ci_setattr.sa_falloc_offset;
-               end = io->u.ci_setattr.sa_falloc_end;
-       }
+       /* flush local pages prior punching/zero-range them on server */
+       if (io_is_falloc &&
+           (io->u.ci_setattr.sa_falloc_mode &
+            (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)))
+               result = osc_punch_start(env, io, obj);
 
        if (result == 0 && oio->oi_lockless == 0) {
                cl_object_attr_lock(obj);
                result = cl_object_attr_get(env, obj, attr);
                if (result == 0) {
                        struct ost_lvb *lvb = &io->u.ci_setattr.sa_attr;
 
        if (result == 0 && oio->oi_lockless == 0) {
                cl_object_attr_lock(obj);
                result = cl_object_attr_get(env, obj, attr);
                if (result == 0) {
                        struct ost_lvb *lvb = &io->u.ci_setattr.sa_attr;
-                       unsigned int cl_valid = 0;
+                       enum cl_attr_valid cl_valid = 0;
 
                        if (ia_avalid & ATTR_SIZE) {
 
                        if (ia_avalid & ATTR_SIZE) {
-                               if (io_is_falloc) {
-                                       attr->cat_size =
-                                             io->u.ci_setattr.sa_attr.lvb_size;
-                                       attr->cat_kms = attr->cat_size;
-                               } else {
-                                       attr->cat_size = size;
-                                       attr->cat_kms = size;
-                               }
+                               attr->cat_size = size;
+                               attr->cat_kms = size;
                                cl_valid = (CAT_SIZE | CAT_KMS);
                        }
                        if (ia_avalid & ATTR_MTIME_SET) {
                                cl_valid = (CAT_SIZE | CAT_KMS);
                        }
                        if (ia_avalid & ATTR_MTIME_SET) {
@@ -604,6 +762,7 @@ static int osc_io_setattr_start(const struct lu_env *env,
        memset(oa, 0, sizeof(*oa));
        if (result == 0) {
                oa->o_oi = loi->loi_oi;
        memset(oa, 0, sizeof(*oa));
        if (result == 0) {
                oa->o_oi = loi->loi_oi;
+               osc_set_projid_info(env, obj, oa);
                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;
                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;
@@ -621,17 +780,8 @@ static int osc_io_setattr_start(const struct lu_env *env,
                        oa->o_valid |= OBD_MD_FLMTIME;
                        oa->o_mtime = attr->cat_mtime;
                }
                        oa->o_valid |= OBD_MD_FLMTIME;
                        oa->o_mtime = attr->cat_mtime;
                }
-               if (ia_avalid & ATTR_SIZE) {
-                       if (io_is_falloc) {
-                               oa->o_size = size;
-                               oa->o_blocks = end;
-                               oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
-                       } else {
-                               oa->o_size = size;
-                               oa->o_blocks = OBD_OBJECT_EOF;
-                               oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
-                       }
 
 
+               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 (oio->oi_lockless) {
                                oa->o_flags = OBD_FL_SRVLOCK;
                                oa->o_valid |= OBD_MD_FLFLAGS;
@@ -656,10 +806,30 @@ static int osc_io_setattr_start(const struct lu_env *env,
                if (io_is_falloc) {
                        int falloc_mode = io->u.ci_setattr.sa_falloc_mode;
 
                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_attr_uid;
+                       oa->o_gid = io->u.ci_setattr.sa_attr_gid;
+                       oa->o_projid = io->u.ci_setattr.sa_attr_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) {
                        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_uid = io->u.ci_setattr.sa_attr_uid;
+                       oa->o_gid = io->u.ci_setattr.sa_attr_gid;
+                       oa->o_projid = io->u.ci_setattr.sa_attr_projid;
+                       oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
+                                      OBD_MD_FLUID | OBD_MD_FLGID |
+                                      OBD_MD_FLPROJID;
                        result = osc_punch_send(osc_export(cl2osc(obj)),
                                                oa, osc_async_upcall, cbargs);
                } else {
                        result = osc_punch_send(osc_export(cl2osc(obj)),
                                                oa, osc_async_upcall, cbargs);
                } else {
@@ -682,7 +852,7 @@ void osc_io_setattr_end(const struct lu_env *env,
        struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
        struct cl_attr  *attr = &osc_env_info(env)->oti_attr;
        struct obdo *oa = &oio->oi_oa;
        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;
+       enum cl_attr_valid cl_valid = 0;
        int result = 0;
 
        if (cbargs->opc_rpc_sent) {
        int result = 0;
 
        if (cbargs->opc_rpc_sent) {
@@ -690,18 +860,6 @@ void osc_io_setattr_end(const struct lu_env *env,
                result = io->ci_result = cbargs->opc_rc;
        }
 
                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));
-                       LASSERT(cl_io_is_trunc(io) || cl_io_is_fallocate(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 (cl_io_is_trunc(io)) {
                __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
 
@@ -785,6 +943,7 @@ static int osc_io_data_version_start(const struct lu_env *env,
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
        oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
        oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+       osc_set_projid_info(env, slice->cis_obj, oa);
 
        if (dv->dv_flags & (LL_DV_RD_FLUSH | LL_DV_WR_FLUSH)) {
                oa->o_valid |= OBD_MD_FLFLAGS;
 
        if (dv->dv_flags & (LL_DV_RD_FLUSH | LL_DV_WR_FLUSH)) {
                oa->o_valid |= OBD_MD_FLFLAGS;
@@ -822,8 +981,12 @@ 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;
                                    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 osc_io *oio = cl2osc_io(env, slice);
+       struct cl_object *obj = slice->cis_obj;
        struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
        struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
+       struct cl_attr *attr = &osc_env_info(env)->oti_attr;
+       struct obdo *oa = &oio->oi_oa;
+       enum cl_attr_valid cl_valid = 0;
 
        ENTRY;
        wait_for_completion(&cbargs->opc_sync);
 
        ENTRY;
        wait_for_completion(&cbargs->opc_sync);
@@ -832,14 +995,30 @@ static void osc_io_data_version_end(const struct lu_env *env,
                slice->cis_io->ci_result = cbargs->opc_rc;
        } else {
                slice->cis_io->ci_result = 0;
                slice->cis_io->ci_result = cbargs->opc_rc;
        } else {
                slice->cis_io->ci_result = 0;
-               if (!(oio->oi_oa.o_valid &
+               if (!(oa->o_valid &
                      (OBD_MD_LAYOUT_VERSION | OBD_MD_FLDATAVERSION)))
                      (OBD_MD_LAYOUT_VERSION | OBD_MD_FLDATAVERSION)))
-                       slice->cis_io->ci_result = -ENOTSUPP;
+                       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;
+                       }
 
 
-               if (oio->oi_oa.o_valid & OBD_MD_LAYOUT_VERSION)
-                       dv->dv_layout_version = oio->oi_oa.o_layout_version;
-               if (oio->oi_oa.o_valid & OBD_MD_FLDATAVERSION)
-                       dv->dv_data_version = oio->oi_oa.o_data_version;
+                       cl_object_attr_lock(obj);
+                       cl_object_attr_update(env, obj, attr, cl_valid);
+                       cl_object_attr_unlock(obj);
+               }
        }
 
        EXIT;
        }
 
        EXIT;
@@ -872,7 +1051,7 @@ int osc_io_write_start(const struct lu_env *env,
        int rc = 0;
        ENTRY;
 
        int rc = 0;
        ENTRY;
 
-       OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_DELAY_SETTIME, 1);
+       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_lock(obj);
        attr->cat_mtime = attr->cat_ctime = ktime_get_real_seconds();
        rc = cl_object_attr_update(env, obj, attr, CAT_MTIME | CAT_CTIME);
@@ -895,6 +1074,7 @@ int osc_fsync_ost(const struct lu_env *env, struct osc_object *obj,
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
        oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
        oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+       osc_set_projid_info(env, osc2cl(obj), oa);
 
        /* reload size abd blocks for start and end of sync range */
        oa->o_size = fio->fi_start;
 
        /* reload size abd blocks for start and end of sync range */
        oa->o_size = fio->fi_start;
@@ -910,41 +1090,69 @@ int osc_fsync_ost(const struct lu_env *env, struct osc_object *obj,
 }
 EXPORT_SYMBOL(osc_fsync_ost);
 
 }
 EXPORT_SYMBOL(osc_fsync_ost);
 
-int osc_io_fsync_start(const struct lu_env *env,
-                      const struct cl_io_slice *slice)
+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_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;
 
        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 (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 (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
                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);
                rc = osc_fsync_ost(env, osc, fio);
-               if (result == 0)
+               if (result == 0) {
+                       cbargs->opc_rpc_sent = 1;
                        result = rc;
                        result = rc;
+               }
        }
 
        RETURN(result);
        }
 
        RETURN(result);
@@ -954,16 +1162,17 @@ 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;
                      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);
        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)
 
                wait_for_completion(&cbargs->opc_sync);
                if (result == 0)
@@ -1002,19 +1211,20 @@ static int osc_io_ladvise_start(const struct lu_env *env,
        memset(ladvise_hdr, 0, buf_size);
        ladvise_hdr->lah_magic = LADVISE_MAGIC;
        ladvise_hdr->lah_count = num_advise;
        memset(ladvise_hdr, 0, buf_size);
        ladvise_hdr->lah_magic = LADVISE_MAGIC;
        ladvise_hdr->lah_count = num_advise;
-       ladvise_hdr->lah_flags = lio->li_flags;
+       ladvise_hdr->lah_flags = lio->lio_flags;
 
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
 
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
-       oa->o_valid = OBD_MD_FLID;
-       obdo_set_parent_fid(oa, lio->li_fid);
+       oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+       osc_set_projid_info(env, obj, oa);
+       obdo_set_parent_fid(oa, lio->lio_fid);
 
        ladvise = ladvise_hdr->lah_advise;
 
        ladvise = ladvise_hdr->lah_advise;
-       ladvise->lla_start = lio->li_start;
-       ladvise->lla_end = lio->li_end;
-       ladvise->lla_advice = lio->li_advice;
+       ladvise->lla_start = lio->lio_start;
+       ladvise->lla_end = lio->lio_end;
+       ladvise->lla_advice = lio->lio_advice;
 
 
-       if (lio->li_flags & LF_ASYNC) {
+       if (lio->lio_flags & LF_ASYNC) {
                result = osc_ladvise_base(osc_export(cl2osc(obj)), oa,
                                          ladvise_hdr, NULL, NULL, NULL);
        } else {
                result = osc_ladvise_base(osc_export(cl2osc(obj)), oa,
                                          ladvise_hdr, NULL, NULL, NULL);
        } else {
@@ -1036,7 +1246,7 @@ static void osc_io_ladvise_end(const struct lu_env *env,
        int                      result = 0;
        struct cl_ladvise_io    *lio = &io->u.ci_ladvise;
 
        int                      result = 0;
        struct cl_ladvise_io    *lio = &io->u.ci_ladvise;
 
-       if ((!(lio->li_flags & LF_ASYNC)) && cbargs->opc_rpc_sent) {
+       if ((!(lio->lio_flags & LF_ASYNC)) && cbargs->opc_rpc_sent) {
                wait_for_completion(&cbargs->opc_sync);
                result = cbargs->opc_rc;
        }
                wait_for_completion(&cbargs->opc_sync);
                result = cbargs->opc_rc;
        }
@@ -1131,6 +1341,7 @@ int osc_io_lseek_start(const struct lu_env *env,
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
        oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
        memset(oa, 0, sizeof(*oa));
        oa->o_oi = loi->loi_oi;
        oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+       osc_set_projid_info(env, obj, oa);
        oa->o_size = lsio->ls_start;
        oa->o_mode = lsio->ls_whence;
        if (oio->oi_lockless) {
        oa->o_size = lsio->ls_start;
        oa->o_mode = lsio->ls_whence;
        if (oio->oi_lockless) {
@@ -1178,16 +1389,42 @@ void osc_io_lseek_end(const struct lu_env *env,
 }
 EXPORT_SYMBOL(osc_io_lseek_end);
 
 }
 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] = {
 static const struct cl_io_operations osc_io_ops = {
        .op = {
                [CIT_READ] = {
-                       .cio_iter_init = osc_io_rw_iter_init,
+                       .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_fini = osc_io_rw_iter_fini,
                        .cio_start  = osc_io_read_start,
                        .cio_fini   = osc_io_fini
                },
                [CIT_WRITE] = {
-                       .cio_iter_init = osc_io_rw_iter_init,
+                       .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_iter_fini = osc_io_rw_iter_fini,
                        .cio_start  = osc_io_write_start,
                        .cio_end    = osc_io_end,
@@ -1230,8 +1467,11 @@ static const struct cl_io_operations osc_io_ops = {
                }
        },
        .cio_read_ahead             = osc_io_read_ahead,
                }
        },
        .cio_read_ahead             = osc_io_read_ahead,
+       .cio_lru_reserve            = osc_io_lru_reserve,
        .cio_submit                 = osc_io_submit,
        .cio_submit                 = osc_io_submit,
-       .cio_commit_async           = osc_io_commit_async
+       .cio_dio_submit             = osc_dio_submit,
+       .cio_commit_async           = osc_io_commit_async,
+       .cio_extent_release         = osc_io_extent_release
 };
 
 /*****************************************************************************
 };
 
 /*****************************************************************************
@@ -1243,11 +1483,17 @@ static const struct cl_io_operations osc_io_ops = {
 int osc_io_init(const struct lu_env *env,
                 struct cl_object *obj, struct cl_io *io)
 {
 int osc_io_init(const struct lu_env *env,
                 struct cl_object *obj, struct cl_io *io)
 {
-        struct osc_io *oio = osc_env_io(env);
+       struct osc_io *oio = osc_env_io(env);
+       struct osc_object *osc = cl2osc(obj);
+       struct obd_export *exp = osc_export(osc);
 
 
-        CL_IO_SLICE_CLEAN(oio, oi_cl);
-        cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
-        return 0;
+       CL_IO_SLICE_CLEAN(oio, oi_cl);
+       cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
+
+       if (!exp_connect_unaligned_dio(exp))
+               cl_io_top(io)->ci_allow_unaligned_dio = false;
+
+       return 0;
 }
 
 /** @} osc */
 }
 
 /** @} osc */