Whamcloud - gitweb
LU-2139 osc: Use SOFT_SYNC to urge server commit
[fs/lustre-release.git] / lustre / osc / osc_request.c
index 4092d93..2aa6b30 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2011, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -179,13 +179,14 @@ static inline void osc_pack_capa(struct ptlrpc_request *req,
 static inline void osc_pack_req_body(struct ptlrpc_request *req,
                                      struct obd_info *oinfo)
 {
-        struct ost_body *body;
+       struct ost_body *body;
 
-        body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
-        LASSERT(body);
+       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
+       LASSERT(body);
 
-        lustre_set_wire_obdo(&body->oa, oinfo->oi_oa);
-        osc_pack_capa(req, body, oinfo->oi_capa);
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa,
+                            oinfo->oi_oa);
+       osc_pack_capa(req, body, oinfo->oi_capa);
 }
 
 static inline void osc_set_capa_size(struct ptlrpc_request *req,
@@ -211,8 +212,9 @@ static int osc_getattr_interpret(const struct lu_env *env,
 
         body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
         if (body) {
-                CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
-                lustre_get_wire_obdo(aa->aa_oi->oi_oa, &body->oa);
+               CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
+               lustre_get_wire_obdo(&req->rq_import->imp_connect_data,
+                                    aa->aa_oi->oi_oa, &body->oa);
 
                /* This should really be sent by the OST */
                aa->aa_oi->oi_oa->o_blksize = DT_MAX_BRW_SIZE;
@@ -290,8 +292,9 @@ static int osc_getattr(const struct lu_env *env, struct obd_export *exp,
         if (body == NULL)
                 GOTO(out, rc = -EPROTO);
 
-        CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
-        lustre_get_wire_obdo(oinfo->oi_oa, &body->oa);
+       CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
+       lustre_get_wire_obdo(&req->rq_import->imp_connect_data, oinfo->oi_oa,
+                            &body->oa);
 
        oinfo->oi_oa->o_blksize = cli_brw_size(exp->exp_obd);
        oinfo->oi_oa->o_valid |= OBD_MD_FLBLKSZ;
@@ -335,7 +338,8 @@ static int osc_setattr(const struct lu_env *env, struct obd_export *exp,
         if (body == NULL)
                 GOTO(out, rc = -EPROTO);
 
-        lustre_get_wire_obdo(oinfo->oi_oa, &body->oa);
+       lustre_get_wire_obdo(&req->rq_import->imp_connect_data, oinfo->oi_oa,
+                            &body->oa);
 
         EXIT;
 out:
@@ -357,7 +361,8 @@ static int osc_setattr_interpret(const struct lu_env *env,
         if (body == NULL)
                 GOTO(out, rc = -EPROTO);
 
-        lustre_get_wire_obdo(sa->sa_oa, &body->oa);
+       lustre_get_wire_obdo(&req->rq_import->imp_connect_data, sa->sa_oa,
+                            &body->oa);
 out:
         rc = sa->sa_upcall(sa->sa_cookie, rc);
         RETURN(rc);
@@ -453,7 +458,8 @@ int osc_real_create(struct obd_export *exp, struct obdo *oa,
 
         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
         LASSERT(body);
-        lustre_set_wire_obdo(&body->oa, oa);
+
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
 
         ptlrpc_request_set_replen(req);
 
@@ -473,7 +479,8 @@ int osc_real_create(struct obd_export *exp, struct obdo *oa,
         if (body == NULL)
                 GOTO(out_req, rc = -EPROTO);
 
-        lustre_get_wire_obdo(oa, &body->oa);
+       CDEBUG(D_INFO, "oa flags %x\n", oa->o_flags);
+       lustre_get_wire_obdo(&req->rq_import->imp_connect_data, oa, &body->oa);
 
        oa->o_blksize = cli_brw_size(exp->exp_obd);
        oa->o_valid |= OBD_MD_FLBLKSZ;
@@ -528,10 +535,11 @@ int osc_punch_base(struct obd_export *exp, struct obd_info *oinfo,
         req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
         ptlrpc_at_set_req_timeout(req);
 
-        body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
-        LASSERT(body);
-        lustre_set_wire_obdo(&body->oa, oinfo->oi_oa);
-        osc_pack_capa(req, body, oinfo->oi_capa);
+       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
+       LASSERT(body);
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa,
+                            oinfo->oi_oa);
+       osc_pack_capa(req, body, oinfo->oi_capa);
 
         ptlrpc_request_set_replen(req);
 
@@ -604,11 +612,12 @@ int osc_sync_base(struct obd_export *exp, struct obd_info *oinfo,
                 RETURN(rc);
         }
 
-        /* overload the size and blocks fields in the oa with start/end */
-        body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
-        LASSERT(body);
-        lustre_set_wire_obdo(&body->oa, oinfo->oi_oa);
-        osc_pack_capa(req, body, oinfo->oi_capa);
+       /* overload the size and blocks fields in the oa with start/end */
+       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
+       LASSERT(body);
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa,
+                            oinfo->oi_oa);
+       osc_pack_capa(req, body, oinfo->oi_capa);
 
         ptlrpc_request_set_replen(req);
         req->rq_interpret_reply = osc_sync_interpret;
@@ -649,8 +658,8 @@ static int osc_sync(const struct lu_env *env, struct obd_export *exp,
  * @objid. Found locks are added into @cancel list. Returns the amount of
  * locks added to @cancels list. */
 static int osc_resource_get_unused(struct obd_export *exp, struct obdo *oa,
-                                   cfs_list_t *cancels,
-                                   ldlm_mode_t mode, int lock_flags)
+                                  cfs_list_t *cancels,
+                                  ldlm_mode_t mode, __u64 lock_flags)
 {
         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
         struct ldlm_res_id res_id;
@@ -681,32 +690,32 @@ static int osc_resource_get_unused(struct obd_export *exp, struct obdo *oa,
 }
 
 static int osc_destroy_interpret(const struct lu_env *env,
-                                 struct ptlrpc_request *req, void *data,
-                                 int rc)
+                                struct ptlrpc_request *req, void *data,
+                                int rc)
 {
-        struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
+       struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
 
-        cfs_atomic_dec(&cli->cl_destroy_in_flight);
-        cfs_waitq_signal(&cli->cl_destroy_waitq);
-        return 0;
+       cfs_atomic_dec(&cli->cl_destroy_in_flight);
+       wake_up(&cli->cl_destroy_waitq);
+       return 0;
 }
 
 static int osc_can_send_destroy(struct client_obd *cli)
 {
-        if (cfs_atomic_inc_return(&cli->cl_destroy_in_flight) <=
-            cli->cl_max_rpcs_in_flight) {
-                /* The destroy request can be sent */
-                return 1;
-        }
-        if (cfs_atomic_dec_return(&cli->cl_destroy_in_flight) <
-            cli->cl_max_rpcs_in_flight) {
-                /*
-                 * The counter has been modified between the two atomic
-                 * operations.
-                 */
-                cfs_waitq_signal(&cli->cl_destroy_waitq);
-        }
-        return 0;
+       if (cfs_atomic_inc_return(&cli->cl_destroy_in_flight) <=
+           cli->cl_max_rpcs_in_flight) {
+               /* The destroy request can be sent */
+               return 1;
+       }
+       if (cfs_atomic_dec_return(&cli->cl_destroy_in_flight) <
+           cli->cl_max_rpcs_in_flight) {
+               /*
+                * The counter has been modified between the two atomic
+                * operations.
+                */
+               wake_up(&cli->cl_destroy_waitq);
+       }
+       return 0;
 }
 
 int osc_create(const struct lu_env *env, struct obd_export *exp,
@@ -781,11 +790,11 @@ static int osc_destroy(const struct lu_env *env, struct obd_export *exp,
         req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
         ptlrpc_at_set_req_timeout(req);
 
-        if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE)
-                oa->o_lcookie = *oti->oti_logcookies;
-        body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
-        LASSERT(body);
-        lustre_set_wire_obdo(&body->oa, oa);
+       if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE)
+               oa->o_lcookie = *oti->oti_logcookies;
+       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
+       LASSERT(body);
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
 
         osc_pack_capa(req, body, (struct obd_capa *)capa);
         ptlrpc_request_set_replen(req);
@@ -849,7 +858,7 @@ static void osc_announce_cached(struct client_obd *cli, struct obdo *oa,
                oa->o_undirty = 0;
        } else {
                long max_in_flight = (cli->cl_max_pages_per_rpc <<
-                                     CFS_PAGE_SHIFT)*
+                                     PAGE_CACHE_SHIFT) *
                                     (cli->cl_max_rpcs_in_flight + 1);
                 oa->o_undirty = max(cli->cl_dirty_max, max_in_flight);
         }
@@ -931,11 +940,11 @@ static void osc_shrink_grant_local(struct client_obd *cli, struct obdo *oa)
 static int osc_shrink_grant(struct client_obd *cli)
 {
        __u64 target_bytes = (cli->cl_max_rpcs_in_flight + 1) *
-                            (cli->cl_max_pages_per_rpc << CFS_PAGE_SHIFT);
+                            (cli->cl_max_pages_per_rpc << PAGE_CACHE_SHIFT);
 
        client_obd_list_lock(&cli->cl_loi_list_lock);
        if (cli->cl_avail_grant <= target_bytes)
-               target_bytes = cli->cl_max_pages_per_rpc << CFS_PAGE_SHIFT;
+               target_bytes = cli->cl_max_pages_per_rpc << PAGE_CACHE_SHIFT;
        client_obd_list_unlock(&cli->cl_loi_list_lock);
 
        return osc_shrink_grant_to_target(cli, target_bytes);
@@ -951,8 +960,8 @@ int osc_shrink_grant_to_target(struct client_obd *cli, __u64 target_bytes)
        /* Don't shrink if we are already above or below the desired limit
         * We don't want to shrink below a single RPC, as that will negatively
         * impact block allocation and long-term performance. */
-       if (target_bytes < cli->cl_max_pages_per_rpc << CFS_PAGE_SHIFT)
-               target_bytes = cli->cl_max_pages_per_rpc << CFS_PAGE_SHIFT;
+       if (target_bytes < cli->cl_max_pages_per_rpc << PAGE_CACHE_SHIFT)
+               target_bytes = cli->cl_max_pages_per_rpc << PAGE_CACHE_SHIFT;
 
        if (target_bytes >= cli->cl_avail_grant) {
                client_obd_list_unlock(&cli->cl_loi_list_lock);
@@ -999,7 +1008,7 @@ static int osc_should_shrink_grant(struct client_obd *client)
                /* Get the current RPC size directly, instead of going via:
                 * cli_brw_size(obd->u.cli.cl_import->imp_obd->obd_self_export)
                 * Keep comment here so that it can be found by searching. */
-               int brw_size = client->cl_max_pages_per_rpc << CFS_PAGE_SHIFT;
+               int brw_size = client->cl_max_pages_per_rpc << PAGE_CACHE_SHIFT;
 
                if (client->cl_import->imp_state == LUSTRE_IMP_FULL &&
                    client->cl_avail_grant > brw_size)
@@ -1073,7 +1082,7 @@ static void osc_init_grant(struct client_obd *cli, struct obd_connect_data *ocd)
         }
 
        /* determine the appropriate chunk size used by osc_extent. */
-       cli->cl_chunkbits = max_t(int, CFS_PAGE_SHIFT, ocd->ocd_blocksize);
+       cli->cl_chunkbits = max_t(int, PAGE_CACHE_SHIFT, ocd->ocd_blocksize);
        client_obd_list_unlock(&cli->cl_loi_list_lock);
 
        CDEBUG(D_CACHE, "%s, setting cl_avail_grant: %ld cl_lost_grant: %ld."
@@ -1099,29 +1108,29 @@ static void handle_short_read(int nob_read, obd_count page_count,
         while (nob_read > 0) {
                 LASSERT (page_count > 0);
 
-                if (pga[i]->count > nob_read) {
-                        /* EOF inside this page */
-                        ptr = cfs_kmap(pga[i]->pg) +
-                                (pga[i]->off & ~CFS_PAGE_MASK);
-                        memset(ptr + nob_read, 0, pga[i]->count - nob_read);
-                        cfs_kunmap(pga[i]->pg);
-                        page_count--;
-                        i++;
-                        break;
-                }
+               if (pga[i]->count > nob_read) {
+                       /* EOF inside this page */
+                       ptr = kmap(pga[i]->pg) +
+                               (pga[i]->off & ~CFS_PAGE_MASK);
+                       memset(ptr + nob_read, 0, pga[i]->count - nob_read);
+                       kunmap(pga[i]->pg);
+                       page_count--;
+                       i++;
+                       break;
+               }
 
                 nob_read -= pga[i]->count;
                 page_count--;
                 i++;
         }
 
-        /* zero remaining pages */
-        while (page_count-- > 0) {
-                ptr = cfs_kmap(pga[i]->pg) + (pga[i]->off & ~CFS_PAGE_MASK);
-                memset(ptr, 0, pga[i]->count);
-                cfs_kunmap(pga[i]->pg);
-                i++;
-        }
+       /* zero remaining pages */
+       while (page_count-- > 0) {
+               ptr = kmap(pga[i]->pg) + (pga[i]->off & ~CFS_PAGE_MASK);
+               memset(ptr, 0, pga[i]->count);
+               kunmap(pga[i]->pg);
+               i++;
+       }
 }
 
 static int check_write_rcs(struct ptlrpc_request *req,
@@ -1163,8 +1172,9 @@ static int check_write_rcs(struct ptlrpc_request *req,
 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
 {
         if (p1->flag != p2->flag) {
-                unsigned mask = ~(OBD_BRW_FROM_GRANT| OBD_BRW_NOCACHE|
-                                  OBD_BRW_SYNC|OBD_BRW_ASYNC|OBD_BRW_NOQUOTA);
+               unsigned mask = ~(OBD_BRW_FROM_GRANT | OBD_BRW_NOCACHE |
+                                 OBD_BRW_SYNC       | OBD_BRW_ASYNC   |
+                                 OBD_BRW_NOQUOTA    | OBD_BRW_SOFT_SYNC);
 
                 /* warn if we try to combine flags that we don't know to be
                  * safe to combine */
@@ -1206,10 +1216,10 @@ static obd_count osc_checksum_bulk(int nob, obd_count pg_count,
                 * simulate an OST->client data error */
                if (i == 0 && opc == OST_READ &&
                    OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE)) {
-                       unsigned char *ptr = cfs_kmap(pga[i]->pg);
+                       unsigned char *ptr = kmap(pga[i]->pg);
                        int off = pga[i]->off & ~CFS_PAGE_MASK;
                        memcpy(ptr + off, "bad1", min(4, nob));
-                       cfs_kunmap(pga[i]->pg);
+                       kunmap(pga[i]->pg);
                }
                cfs_crypto_hash_update_page(hdesc, pga[i]->pg,
                                  pga[i]->off & ~CFS_PAGE_MASK,
@@ -1308,7 +1318,7 @@ static int osc_brw_prep_request(int cmd, struct client_obd *cli,struct obdo *oa,
         niobuf = req_capsule_client_get(pill, &RMF_NIOBUF_REMOTE);
         LASSERT(body != NULL && ioobj != NULL && niobuf != NULL);
 
-        lustre_set_wire_obdo(&body->oa, oa);
+       lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
 
        obdo_to_ioobj(oa, ioobj);
        ioobj->ioo_bufcnt = niocount;
@@ -1327,13 +1337,13 @@ static int osc_brw_prep_request(int cmd, struct client_obd *cli,struct obdo *oa,
 
                 LASSERT(pg->count > 0);
                 /* make sure there is no gap in the middle of page array */
-                LASSERTF(page_count == 1 ||
-                         (ergo(i == 0, poff + pg->count == CFS_PAGE_SIZE) &&
-                          ergo(i > 0 && i < page_count - 1,
-                               poff == 0 && pg->count == CFS_PAGE_SIZE)   &&
-                          ergo(i == page_count - 1, poff == 0)),
-                         "i: %d/%d pg: %p off: "LPU64", count: %u\n",
-                         i, page_count, pg, pg->off, pg->count);
+               LASSERTF(page_count == 1 ||
+                        (ergo(i == 0, poff + pg->count == PAGE_CACHE_SIZE) &&
+                         ergo(i > 0 && i < page_count - 1,
+                              poff == 0 && pg->count == PAGE_CACHE_SIZE)   &&
+                         ergo(i == page_count - 1, poff == 0)),
+                        "i: %d/%d pg: %p off: "LPU64", count: %u\n",
+                        i, page_count, pg, pg->off, pg->count);
 #ifdef __linux__
                 LASSERTF(i == 0 || pg->off > pg_prev->off,
                          "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
@@ -1639,8 +1649,9 @@ static int osc_brw_fini_request(struct ptlrpc_request *req, int rc)
                 rc = 0;
         }
 out:
-        if (rc >= 0)
-                lustre_get_wire_obdo(aa->aa_oa, &body->oa);
+       if (rc >= 0)
+               lustre_get_wire_obdo(&req->rq_import->imp_connect_data,
+                                    aa->aa_oa, &body->oa);
 
         RETURN(rc);
 }
@@ -1650,16 +1661,16 @@ static int osc_brw_internal(int cmd, struct obd_export *exp, struct obdo *oa,
                             obd_count page_count, struct brw_page **pga,
                             struct obd_capa *ocapa)
 {
-        struct ptlrpc_request *req;
-        int                    rc;
-        cfs_waitq_t            waitq;
-        int                    generation, resends = 0;
-        struct l_wait_info     lwi;
+       struct ptlrpc_request *req;
+       int                    rc;
+       wait_queue_head_t            waitq;
+       int                    generation, resends = 0;
+       struct l_wait_info     lwi;
 
-        ENTRY;
+       ENTRY;
 
-        cfs_waitq_init(&waitq);
-        generation = exp->exp_obd->u.cli.cl_import->imp_generation;
+       init_waitqueue_head(&waitq);
+       generation = exp->exp_obd->u.cli.cl_import->imp_generation;
 
 restart_bulk:
         rc = osc_brw_prep_request(cmd, &exp->exp_obd->u.cli, oa, lsm,
@@ -1834,7 +1845,7 @@ static obd_count max_unfragmented_pages(struct brw_page **pg, obd_count pages)
                 if (pages == 0)         /* that's all */
                         return count;
 
-                if (offset + pg[i]->count < CFS_PAGE_SIZE)
+               if (offset + pg[i]->count < PAGE_CACHE_SIZE)
                         return count;   /* doesn't end on page boundary */
 
                 i++;
@@ -2052,10 +2063,13 @@ static void brw_commit(struct ptlrpc_request *req)
         * this called via the rq_commit_cb, I need to ensure
         * osc_dec_unstable_pages is still called. Otherwise unstable
         * pages may be leaked. */
-       if (req->rq_unstable)
+       if (req->rq_unstable) {
+               spin_unlock(&req->rq_lock);
                osc_dec_unstable_pages(req);
-       else
+               spin_lock(&req->rq_lock);
+       } else {
                req->rq_committed = 1;
+       }
        spin_unlock(&req->rq_lock);
 }
 
@@ -2067,21 +2081,26 @@ static void brw_commit(struct ptlrpc_request *req)
 int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
                  cfs_list_t *ext_list, int cmd, pdl_policy_t pol)
 {
-       struct ptlrpc_request *req = NULL;
-       struct osc_extent *ext;
+       struct ptlrpc_request           *req = NULL;
+       struct osc_extent               *ext;
+       struct brw_page                 **pga = NULL;
+       struct osc_brw_async_args       *aa = NULL;
+       struct obdo                     *oa = NULL;
+       struct osc_async_page           *oap;
+       struct osc_async_page           *tmp;
+       struct cl_req                   *clerq = NULL;
+       enum cl_req_type                crt = (cmd & OBD_BRW_WRITE) ? CRT_WRITE :
+                                                                     CRT_READ;
+       struct ldlm_lock                *lock = NULL;
+       struct cl_req_attr              *crattr = NULL;
+       obd_off                         starting_offset = OBD_OBJECT_EOF;
+       obd_off                         ending_offset = 0;
+       int                             mpflag = 0;
+       int                             mem_tight = 0;
+       int                             page_count = 0;
+       int                             i;
+       int                             rc;
        CFS_LIST_HEAD(rpc_list);
-       struct brw_page **pga = NULL;
-       struct osc_brw_async_args *aa = NULL;
-        struct obdo *oa = NULL;
-        struct osc_async_page *oap;
-        struct osc_async_page *tmp;
-        struct cl_req *clerq = NULL;
-        enum cl_req_type crt = (cmd & OBD_BRW_WRITE) ? CRT_WRITE : CRT_READ;
-        struct ldlm_lock *lock = NULL;
-        struct cl_req_attr crattr;
-       obd_off starting_offset = OBD_OBJECT_EOF;
-       obd_off ending_offset = 0;
-       int i, rc, mpflag = 0, mem_tight = 0, page_count = 0;
 
        ENTRY;
        LASSERT(!cfs_list_empty(ext_list));
@@ -2102,14 +2121,17 @@ int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
                                                oap->oap_count;
                        else
                                LASSERT(oap->oap_page_off + oap->oap_count ==
-                                       CFS_PAGE_SIZE);
+                                       PAGE_CACHE_SIZE);
                }
        }
 
        if (mem_tight)
                mpflag = cfs_memory_pressure_get_and_set();
 
-       memset(&crattr, 0, sizeof crattr);
+       OBD_ALLOC(crattr, sizeof(*crattr));
+       if (crattr == NULL)
+               GOTO(out, rc = -ENOMEM);
+
        OBD_ALLOC(pga, sizeof(*pga) * page_count);
        if (pga == NULL)
                GOTO(out, rc = -ENOMEM);
@@ -2123,42 +2145,40 @@ int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
                struct cl_page *page = oap2cl_page(oap);
                if (clerq == NULL) {
                        clerq = cl_req_alloc(env, page, crt,
-                                            1 /* only 1-object rpcs for
-                                               * now */);
+                                            1 /* only 1-object rpcs for now */);
                        if (IS_ERR(clerq))
                                GOTO(out, rc = PTR_ERR(clerq));
                        lock = oap->oap_ldlm_lock;
                }
                if (mem_tight)
                        oap->oap_brw_flags |= OBD_BRW_MEMALLOC;
-                pga[i] = &oap->oap_brw_page;
-                pga[i]->off = oap->oap_obj_off + oap->oap_page_off;
-                CDEBUG(0, "put page %p index %lu oap %p flg %x to pga\n",
-                       pga[i]->pg, cfs_page_index(oap->oap_page), oap, pga[i]->flag);
-                i++;
-                cl_req_page_add(env, clerq, page);
-        }
+               pga[i] = &oap->oap_brw_page;
+               pga[i]->off = oap->oap_obj_off + oap->oap_page_off;
+               CDEBUG(0, "put page %p index %lu oap %p flg %x to pga\n",
+                      pga[i]->pg, page_index(oap->oap_page), oap,
+                      pga[i]->flag);
+               i++;
+               cl_req_page_add(env, clerq, page);
+       }
 
-        /* always get the data for the obdo for the rpc */
+       /* always get the data for the obdo for the rpc */
        LASSERT(clerq != NULL);
-       crattr.cra_oa = oa;
-       crattr.cra_capa = NULL;
-       memset(crattr.cra_jobid, 0, JOBSTATS_JOBID_SIZE);
-        cl_req_attr_set(env, clerq, &crattr, ~0ULL);
-        if (lock) {
-                oa->o_handle = lock->l_remote_handle;
-                oa->o_valid |= OBD_MD_FLHANDLE;
-        }
+       crattr->cra_oa = oa;
+       cl_req_attr_set(env, clerq, crattr, ~0ULL);
+       if (lock) {
+               oa->o_handle = lock->l_remote_handle;
+               oa->o_valid |= OBD_MD_FLHANDLE;
+       }
 
-        rc = cl_req_prep(env, clerq);
-        if (rc != 0) {
-                CERROR("cl_req_prep failed: %d\n", rc);
+       rc = cl_req_prep(env, clerq);
+       if (rc != 0) {
+               CERROR("cl_req_prep failed: %d\n", rc);
                GOTO(out, rc);
        }
 
        sort_brw_pages(pga, page_count);
        rc = osc_brw_prep_request(cmd, cli, oa, NULL, page_count,
-                       pga, &req, crattr.cra_capa, 1, 0);
+                       pga, &req, crattr->cra_capa, 1, 0);
        if (rc != 0) {
                CERROR("prep_req failed: %d\n", rc);
                GOTO(out, rc);
@@ -2168,17 +2188,17 @@ int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
        req->rq_interpret_reply = brw_interpret;
 
        if (mem_tight != 0)
-                req->rq_memalloc = 1;
+               req->rq_memalloc = 1;
 
-        /* Need to update the timestamps after the request is built in case
-         * we race with setattr (locally or in queue at OST).  If OST gets
-         * later setattr before earlier BRW (as determined by the request xid),
-         * the OST will not use BRW timestamps.  Sadly, there is no obvious
-         * way to do this in a single call.  bug 10150 */
-        cl_req_attr_set(env, clerq, &crattr,
-                        OBD_MD_FLMTIME|OBD_MD_FLCTIME|OBD_MD_FLATIME);
+       /* Need to update the timestamps after the request is built in case
+        * we race with setattr (locally or in queue at OST).  If OST gets
+        * later setattr before earlier BRW (as determined by the request xid),
+        * the OST will not use BRW timestamps.  Sadly, there is no obvious
+        * way to do this in a single call.  bug 10150 */
+       cl_req_attr_set(env, clerq, crattr,
+                       OBD_MD_FLMTIME|OBD_MD_FLCTIME|OBD_MD_FLATIME);
 
-       lustre_msg_set_jobid(req->rq_reqmsg, crattr.cra_jobid);
+       lustre_msg_set_jobid(req->rq_reqmsg, crattr->cra_jobid);
 
        CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
        aa = ptlrpc_req_async_args(req);
@@ -2205,7 +2225,7 @@ int osc_build_rpc(const struct lu_env *env, struct client_obd *cli,
                tmp->oap_request = ptlrpc_request_addref(req);
 
        client_obd_list_lock(&cli->cl_loi_list_lock);
-       starting_offset >>= CFS_PAGE_SHIFT;
+       starting_offset >>= PAGE_CACHE_SHIFT;
        if (cmd == OBD_BRW_READ) {
                cli->cl_r_in_flight++;
                lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
@@ -2245,16 +2265,20 @@ out:
        if (mem_tight != 0)
                cfs_memory_pressure_restore(mpflag);
 
-       capa_put(crattr.cra_capa);
+       if (crattr != NULL) {
+               capa_put(crattr->cra_capa);
+               OBD_FREE(crattr, sizeof(*crattr));
+       }
+
        if (rc != 0) {
                LASSERT(req == NULL);
 
-                if (oa)
-                        OBDO_FREE(oa);
-                if (pga)
-                        OBD_FREE(pga, sizeof(*pga) * page_count);
-                /* this should happen rarely and is pretty bad, it makes the
-                 * pending list not follow the dirty order */
+               if (oa)
+                       OBDO_FREE(oa);
+               if (pga)
+                       OBD_FREE(pga, sizeof(*pga) * page_count);
+               /* this should happen rarely and is pretty bad, it makes the
+                * pending list not follow the dirty order */
                while (!cfs_list_empty(ext_list)) {
                        ext = cfs_list_entry(ext_list->next, struct osc_extent,
                                             oe_link);
@@ -2354,6 +2378,8 @@ static int osc_enqueue_fini(struct ptlrpc_request *req, struct ost_lvb *lvb,
                                                      &RMF_DLM_REP);
 
                         LASSERT(rep != NULL);
+                       rep->lock_policy_res1 =
+                               ptlrpc_status_ntoh(rep->lock_policy_res1);
                         if (rep->lock_policy_res1)
                                 rc = rep->lock_policy_res1;
                 }
@@ -2434,8 +2460,8 @@ static int osc_enqueue_interpret(const struct lu_env *env,
 }
 
 void osc_update_enqueue(struct lustre_handle *lov_lockhp,
-                        struct lov_oinfo *loi, int flags,
-                        struct ost_lvb *lvb, __u32 mode, int rc)
+                       struct lov_oinfo *loi, __u64 flags,
+                       struct ost_lvb *lvb, __u32 mode, int rc)
 {
         struct ldlm_lock *lock = ldlm_handle2lock(lov_lockhp);
 
@@ -2489,19 +2515,19 @@ struct ptlrpc_request_set *PTLRPCD_SET = (void *)1;
  * release locks just after they are obtained. */
 int osc_enqueue_base(struct obd_export *exp, struct ldlm_res_id *res_id,
                     __u64 *flags, ldlm_policy_data_t *policy,
-                     struct ost_lvb *lvb, int kms_valid,
-                     obd_enqueue_update_f upcall, void *cookie,
-                     struct ldlm_enqueue_info *einfo,
-                     struct lustre_handle *lockh,
-                     struct ptlrpc_request_set *rqset, int async, int agl)
+                    struct ost_lvb *lvb, int kms_valid,
+                    obd_enqueue_update_f upcall, void *cookie,
+                    struct ldlm_enqueue_info *einfo,
+                    struct lustre_handle *lockh,
+                    struct ptlrpc_request_set *rqset, int async, int agl)
 {
-        struct obd_device *obd = exp->exp_obd;
-        struct ptlrpc_request *req = NULL;
-        int intent = *flags & LDLM_FL_HAS_INTENT;
-        int match_lvb = (agl != 0 ? 0 : LDLM_FL_LVB_READY);
-        ldlm_mode_t mode;
-        int rc;
-        ENTRY;
+       struct obd_device *obd = exp->exp_obd;
+       struct ptlrpc_request *req = NULL;
+       int intent = *flags & LDLM_FL_HAS_INTENT;
+       __u64 match_lvb = (agl != 0 ? 0 : LDLM_FL_LVB_READY);
+       ldlm_mode_t mode;
+       int rc;
+       ENTRY;
 
         /* Filesystem lock extents are extended to page boundaries so that
          * dealing with the page cache is a little smoother.  */
@@ -2537,7 +2563,7 @@ int osc_enqueue_base(struct obd_export *exp, struct ldlm_res_id *res_id,
         if (mode) {
                 struct ldlm_lock *matched = ldlm_handle2lock(lockh);
 
-                if ((agl != 0) && !(matched->l_flags & LDLM_FL_LVB_READY)) {
+               if ((agl != 0) && !ldlm_is_lvb_ready(matched)) {
                         /* For AGL, if enqueue RPC is sent but the lock is not
                          * granted, then skip to process this strpe.
                          * Return -ECANCELED to tell the caller. */
@@ -2650,14 +2676,14 @@ static int osc_enqueue(struct obd_export *exp, struct obd_info *oinfo,
 }
 
 int osc_match_base(struct obd_export *exp, struct ldlm_res_id *res_id,
-                   __u32 type, ldlm_policy_data_t *policy, __u32 mode,
-                   int *flags, void *data, struct lustre_handle *lockh,
-                   int unref)
+                  __u32 type, ldlm_policy_data_t *policy, __u32 mode,
+                  __u64 *flags, void *data, struct lustre_handle *lockh,
+                  int unref)
 {
-        struct obd_device *obd = exp->exp_obd;
-        int lflags = *flags;
-        ldlm_mode_t rc;
-        ENTRY;
+       struct obd_device *obd = exp->exp_obd;
+       __u64 lflags = *flags;
+       ldlm_mode_t rc;
+       ENTRY;
 
         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_MATCH))
                 RETURN(-EIO);
@@ -2889,7 +2915,7 @@ static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
         /* we only need the header part from user space to get lmm_magic and
          * lmm_stripe_count, (the header part is common to v1 and v3) */
         lum_size = sizeof(struct lov_user_md_v1);
-        if (cfs_copy_from_user(&lum, lump, lum_size))
+       if (copy_from_user(&lum, lump, lum_size))
                 RETURN(-EFAULT);
 
         if ((lum.lmm_magic != LOV_USER_MAGIC_V1) &&
@@ -2921,15 +2947,15 @@ static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
        }
 
        lumk->lmm_oi = lsm->lsm_oi;
-        lumk->lmm_stripe_count = 1;
+       lumk->lmm_stripe_count = 1;
 
-        if (cfs_copy_to_user(lump, lumk, lum_size))
-                rc = -EFAULT;
+       if (copy_to_user(lump, lumk, lum_size))
+               rc = -EFAULT;
 
-        if (lumk != &lum)
-                OBD_FREE(lumk, lum_size);
+       if (lumk != &lum)
+               OBD_FREE(lumk, lum_size);
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 
@@ -2941,10 +2967,10 @@ static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
         int err = 0;
         ENTRY;
 
-        if (!cfs_try_module_get(THIS_MODULE)) {
-                CERROR("Can't get module. Is it alive?");
-                return -EINVAL;
-        }
+       if (!try_module_get(THIS_MODULE)) {
+               CERROR("Can't get module. Is it alive?");
+               return -EINVAL;
+       }
         switch (cmd) {
         case OBD_IOC_LOV_GET_CONFIG: {
                 char *buf;
@@ -2979,7 +3005,7 @@ static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
 
                 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
 
-                err = cfs_copy_to_user((void *)uarg, buf, len);
+               err = copy_to_user((void *)uarg, buf, len);
                 if (err)
                         err = -EFAULT;
                 obd_ioctl_freedata(buf, len);
@@ -3009,14 +3035,14 @@ static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
         case OBD_IOC_PING_TARGET:
                 err = ptlrpc_obd_ping(obd);
                 GOTO(out, err);
-        default:
-                CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n",
-                       cmd, cfs_curproc_comm());
-                GOTO(out, err = -ENOTTY);
-        }
+       default:
+               CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n",
+                      cmd, current_comm());
+               GOTO(out, err = -ENOTTY);
+       }
 out:
-        cfs_module_put(THIS_MODULE);
-        return err;
+       module_put(THIS_MODULE);
+       return err;
 }
 
 static int osc_get_info(const struct lu_env *env, struct obd_export *exp,
@@ -3069,15 +3095,52 @@ static int osc_get_info(const struct lu_env *env, struct obd_export *exp,
                 ptlrpc_req_finished(req);
                 RETURN(rc);
         } else if (KEY_IS(KEY_FIEMAP)) {
-                struct ptlrpc_request *req;
-                struct ll_user_fiemap *reply;
-                char *tmp;
-                int rc;
+               struct ll_fiemap_info_key *fm_key =
+                               (struct ll_fiemap_info_key *)key;
+               struct ldlm_res_id       res_id;
+               ldlm_policy_data_t       policy;
+               struct lustre_handle     lockh;
+               ldlm_mode_t              mode = 0;
+               struct ptlrpc_request   *req;
+               struct ll_user_fiemap   *reply;
+               char                    *tmp;
+               int                      rc;
+
+               if (!(fm_key->fiemap.fm_flags & FIEMAP_FLAG_SYNC))
+                       goto skip_locking;
+
+               policy.l_extent.start = fm_key->fiemap.fm_start &
+                                               CFS_PAGE_MASK;
+
+               if (OBD_OBJECT_EOF - fm_key->fiemap.fm_length <=
+                   fm_key->fiemap.fm_start + PAGE_CACHE_SIZE - 1)
+                       policy.l_extent.end = OBD_OBJECT_EOF;
+               else
+                       policy.l_extent.end = (fm_key->fiemap.fm_start +
+                               fm_key->fiemap.fm_length +
+                               PAGE_CACHE_SIZE - 1) & CFS_PAGE_MASK;
+
+               ostid_build_res_name(&fm_key->oa.o_oi, &res_id);
+               mode = ldlm_lock_match(exp->exp_obd->obd_namespace,
+                                      LDLM_FL_BLOCK_GRANTED |
+                                      LDLM_FL_LVB_READY,
+                                      &res_id, LDLM_EXTENT, &policy,
+                                      LCK_PR | LCK_PW, &lockh, 0);
+               if (mode) { /* lock is cached on client */
+                       if (mode != LCK_PR) {
+                               ldlm_lock_addref(&lockh, LCK_PR);
+                               ldlm_lock_decref(&lockh, LCK_PW);
+                       }
+               } else { /* no cached lock, needs acquire lock on server side */
+                       fm_key->oa.o_valid |= OBD_MD_FLFLAGS;
+                       fm_key->oa.o_flags |= OBD_FL_SRVLOCK;
+               }
 
+skip_locking:
                 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
                                            &RQF_OST_GET_INFO_FIEMAP);
                 if (req == NULL)
-                        RETURN(-ENOMEM);
+                       GOTO(drop_lock, rc = -ENOMEM);
 
                 req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_KEY,
                                      RCL_CLIENT, keylen);
@@ -3089,7 +3152,7 @@ static int osc_get_info(const struct lu_env *env, struct obd_export *exp,
                 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
                 if (rc) {
                         ptlrpc_request_free(req);
-                        RETURN(rc);
+                       GOTO(drop_lock, rc);
                 }
 
                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_KEY);
@@ -3100,16 +3163,18 @@ static int osc_get_info(const struct lu_env *env, struct obd_export *exp,
                 ptlrpc_request_set_replen(req);
                 rc = ptlrpc_queue_wait(req);
                 if (rc)
-                        GOTO(out1, rc);
+                       GOTO(fini_req, rc);
 
                 reply = req_capsule_server_get(&req->rq_pill, &RMF_FIEMAP_VAL);
                 if (reply == NULL)
-                        GOTO(out1, rc = -EPROTO);
+                       GOTO(fini_req, rc = -EPROTO);
 
                 memcpy(val, reply, *vallen);
-        out1:
+fini_req:
                 ptlrpc_req_finished(req);
-
+drop_lock:
+               if (mode)
+                       ldlm_lock_decref(&lockh, LCK_PR);
                 RETURN(rc);
         }
 
@@ -3647,6 +3712,8 @@ int __init osc_init(void)
         CDEBUG(D_INFO, "Lustre OSC module (%p).\n", &osc_caches);
 
         rc = lu_kmem_init(osc_caches);
+       if (rc)
+               RETURN(rc);
 
         lprocfs_osc_init_vars(&lvars);