Whamcloud - gitweb
b=14230
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
index 4aabd13..a19234b 100644 (file)
@@ -40,7 +40,6 @@
 #include <linux/module.h>
 /*
  * struct OBD_{ALLOC,FREE}*()
- * MDT_FAIL_CHECK
  */
 #include <obd_support.h>
 /* struct ptlrpc_request */
@@ -286,10 +285,10 @@ static int mdt_getstatus(struct mdt_thread_info *info)
         if (rc)
                 RETURN(err_serious(rc));
 
-        if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
                 RETURN(err_serious(-ENOMEM));
 
-        repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
+        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
         if (rc != 0)
                 RETURN(rc);
@@ -304,7 +303,7 @@ static int mdt_getstatus(struct mdt_thread_info *info)
                 if (IS_ERR(root))
                         RETURN(PTR_ERR(root));
 
-                capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
+                capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
                 LASSERT(capa);
                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
 
@@ -334,10 +333,10 @@ static int mdt_statfs(struct mdt_thread_info *info)
         if (rc)
                 RETURN(err_serious(rc));
 
-        if (MDT_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
                 rc = err_serious(-ENOMEM);
         } else {
-                osfs = req_capsule_server_get(&info->mti_pill,&RMF_OBD_STATFS);
+                osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
                 rc = next->md_ops->mdo_statfs(info->mti_env, next,
                                               &info->mti_u.ksfs);
                 statfs_pack(osfs, &info->mti_u.ksfs);
@@ -349,8 +348,8 @@ void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
 {
         struct mdt_body *b;
         struct lu_attr *attr = &info->mti_attr.ma_attr;
-        
-        b = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
+
+        b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
 
         /* Check if Size-on-MDS is enabled. */
         if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
@@ -388,7 +387,13 @@ void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
         if (fid) {
                 b->fid1 = *fid;
                 b->valid |= OBD_MD_FLID;
-                CDEBUG(D_INODE, ""DFID": nlink=%d, mode=%o, size="LPU64"\n",
+
+                /* FIXME: these should be fixed when new igif ready.*/
+                b->ino  =  fid_oid(fid);       /* 1.6 compatibility */
+                b->generation = fid_ver(fid);  /* 1.6 compatibility */
+                b->valid |= OBD_MD_FLGENER;    /* 1.6 compatibility */
+
+                CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
                                 PFID(fid), b->nlink, b->mode, b->size);
         }
 
@@ -412,14 +417,14 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
         struct md_attr          *ma = &info->mti_attr;
         struct lu_attr          *la = &ma->ma_attr;
-        struct req_capsule      *pill = &info->mti_pill;
+        struct req_capsule      *pill = info->mti_pill;
         const struct lu_env     *env = info->mti_env;
         struct mdt_body         *repbody;
         struct lu_buf           *buffer = &info->mti_buf;
         int                     rc;
         ENTRY;
 
-        if (unlikely(MDT_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
                 RETURN(err_serious(-ENOMEM));
 
         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
@@ -450,6 +455,12 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
                 ma->ma_need = MA_LOV | MA_INODE;
         }
 
+        if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
+            reqbody->valid & OBD_MD_FLDIREA  &&
+            lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
+                /* get default stripe info for this dir. */
+                ma->ma_need |= MA_LOV_DEF;
+        }
         rc = mo_attr_get(env, next, ma);
         if (unlikely(rc)) {
                 CERROR("getattr error for "DFID": %d\n",
@@ -554,7 +565,7 @@ static int mdt_getattr_internal(struct mdt_thread_info *info,
             info->mti_mdt->mdt_opts.mo_mds_capa) {
                 struct lustre_capa *capa;
 
-                capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
+                capa = req_capsule_server_get(pill, &RMF_CAPA1);
                 LASSERT(capa);
                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
                 rc = mo_capa_get(env, next, capa, 0);
@@ -581,27 +592,26 @@ static int mdt_renew_capa(struct mdt_thread_info *info)
         if (!obj || !mdt->mdt_opts.mo_mds_capa)
                 RETURN(0);
 
-        body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
+        body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(body != NULL);
 
-        c = req_capsule_client_get(&info->mti_pill, &RMF_CAPA1);
+        c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
         LASSERT(c);
 
-        capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
+        capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
         LASSERT(capa);
 
         *capa = *c;
         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
         if (rc == 0)
                 body->valid |= OBD_MD_FLOSSCAPA;
-
         RETURN(rc);
 }
 
 static int mdt_getattr(struct mdt_thread_info *info)
 {
         struct mdt_object       *obj = info->mti_object;
-        struct req_capsule      *pill = &info->mti_pill;
+        struct req_capsule      *pill = info->mti_pill;
         struct mdt_body         *reqbody;
         struct mdt_body         *repbody;
         mode_t                   mode;
@@ -613,14 +623,11 @@ static int mdt_getattr(struct mdt_thread_info *info)
         LASSERT(reqbody);
 
         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
-                rc = req_capsule_pack(pill);
+                rc = req_capsule_server_pack(pill);
                 if (unlikely(rc))
-                        rc = err_serious(rc);
-                else {
-                        rc = mdt_renew_capa(info);
-                        mdt_shrink_reply(info);
-                }
-                GOTO(out, rc);
+                        RETURN(err_serious(rc));
+                rc = mdt_renew_capa(info);
+                GOTO(out_shrink, rc);
         }
 
         LASSERT(obj != NULL);
@@ -635,9 +642,9 @@ static int mdt_getattr(struct mdt_thread_info *info)
 
         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
 
-        rc = req_capsule_pack(pill);
+        rc = req_capsule_server_pack(pill);
         if (unlikely(rc != 0))
-                GOTO(out, rc = err_serious(rc));
+                RETURN(err_serious(rc));
 
         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
         LASSERT(repbody != NULL);
@@ -665,14 +672,13 @@ static int mdt_getattr(struct mdt_thread_info *info)
         EXIT;
 out_shrink:
         mdt_shrink_reply(info);
-out:
         return rc;
 }
 
 static int mdt_is_subdir(struct mdt_thread_info *info)
 {
         struct mdt_object     *o = info->mti_object;
-        struct req_capsule    *pill = &info->mti_pill;
+        struct req_capsule    *pill = info->mti_pill;
         const struct mdt_body *body = info->mti_body;
         struct mdt_body       *repbody;
         int                    rc;
@@ -725,11 +731,10 @@ static int mdt_raw_lookup(struct mdt_thread_info *info,
         } else
                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
 
-        repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
+        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
 #endif
         if (rc == 0) {
-                repbody = req_capsule_server_get(&info->mti_pill,
-                                                 &RMF_MDT_BODY);
+                repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
                 repbody->fid1 = *child_fid;
                 repbody->valid = OBD_MD_FLID;
         }
@@ -769,11 +774,11 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
 
         LASSERT(parent != NULL);
-        name = req_capsule_client_get(&info->mti_pill, &RMF_NAME);
+        name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
         if (name == NULL)
                 RETURN(err_serious(-EFAULT));
 
-        namelen = req_capsule_get_size(&info->mti_pill, &RMF_NAME, 
+        namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
                                        RCL_CLIENT) - 1;
         LASSERT(namelen >= 0);
 
@@ -781,7 +786,7 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
          * otherwise do not allow empty name, that is the name must contain
          * at least one character and the terminating '\0'*/
         if (namelen == 0) {
-                reqbody =req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
+                reqbody =req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
                 LASSERT(fid_is_sane(&reqbody->fid2));
                 name = NULL;
 
@@ -855,7 +860,7 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
                         if (unlikely(rc != 0))
                                 mdt_object_unlock(info, child, lhc, 1);
                 }
-                GOTO(out, rc);
+                RETURN(rc);
         }
 
         /* step 1: lock parent */
@@ -959,7 +964,7 @@ relock:
                          * Pack Size-on-MDS inode attributes to the body if
                          * update lock is given.
                          */
-                        repbody = req_capsule_server_get(&info->mti_pill,
+                        repbody = req_capsule_server_get(info->mti_pill,
                                                          &RMF_MDT_BODY);
                         ma = &info->mti_attr.ma_attr;
                         if (lock->l_policy_data.l_inodebits.bits &
@@ -973,7 +978,6 @@ out_child:
         mdt_object_put(info->mti_env, child);
 out_parent:
         mdt_object_unlock(info, parent, lhp, 1);
-out:
         return rc;
 }
 
@@ -986,9 +990,9 @@ static int mdt_getattr_name(struct mdt_thread_info *info)
         int rc;
         ENTRY;
 
-        reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
+        reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(reqbody != NULL);
-        repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
+        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(repbody != NULL);
 
         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
@@ -998,7 +1002,7 @@ static int mdt_getattr_name(struct mdt_thread_info *info)
 
         rc = mdt_init_ucred(info, reqbody);
         if (unlikely(rc))
-                GOTO(out, rc);
+                GOTO(out_shrink, rc);
 
         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
@@ -1007,7 +1011,7 @@ static int mdt_getattr_name(struct mdt_thread_info *info)
         }
         mdt_exit_ucred(info);
         EXIT;
-out:
+out_shrink:
         mdt_shrink_reply(info);
         return rc;
 }
@@ -1027,20 +1031,20 @@ static int mdt_set_info(struct mdt_thread_info *info)
         int keylen, rc = 0;
         ENTRY;
 
-        rc = lustre_pack_reply(req, 1, NULL, NULL);
+        rc = req_capsule_server_pack(info->mti_pill);
         if (rc)
                 RETURN(rc);
 
-        key = req_capsule_client_get(&info->mti_pill, &RMF_SETINFO_KEY);
+        key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
         if (key == NULL) {
                 DEBUG_REQ(D_HA, req, "no set_info key");
                 RETURN(-EFAULT);
         }
 
-        keylen = req_capsule_get_size(&info->mti_pill, &RMF_SETINFO_KEY,
+        keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
                                       RCL_CLIENT);
 
-        val = req_capsule_client_get(&info->mti_pill, &RMF_SETINFO_VAL);
+        val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
         if (val == NULL) {
                 DEBUG_REQ(D_HA, req, "no set_info val");
                 RETURN(-EFAULT);
@@ -1108,7 +1112,7 @@ static int mdt_sendpage(struct mdt_thread_info *info,
         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
                                     MDS_BULK_PORTAL);
         if (desc == NULL)
-                GOTO(out, rc = -ENOMEM);
+                RETURN(-ENOMEM);
 
         for (i = 0, tmpcount = rdpg->rp_count;
                 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
@@ -1121,7 +1125,7 @@ static int mdt_sendpage(struct mdt_thread_info *info,
         if (rc)
                 GOTO(free_desc, rc);
 
-        if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
                 GOTO(abort_bulk, rc);
 
         *lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
@@ -1149,7 +1153,6 @@ abort_bulk:
         ptlrpc_abort_bulk(desc);
 free_desc:
         ptlrpc_free_bulk(desc);
-out:
         return rc;
 }
 
@@ -1254,7 +1257,7 @@ static int mdt_writepage(struct mdt_thread_info *info)
         ENTRY;
 
 
-        reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
+        reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
         if (reqbody == NULL)
                 RETURN(err_serious(-EFAULT));
 
@@ -1263,7 +1266,7 @@ static int mdt_writepage(struct mdt_thread_info *info)
                 RETURN(err_serious(-ENOMEM));
 
         /* allocate the page for the desc */
-        page = alloc_pages(GFP_KERNEL, 0);
+        page = cfs_alloc_page(CFS_ALLOC_STD);
         if (page == NULL)
                 GOTO(desc_cleanup, rc = -ENOMEM);
 
@@ -1317,7 +1320,7 @@ static int mdt_writepage(struct mdt_thread_info *info)
 cleanup_lwi:
         OBD_FREE_PTR(lwi);
 cleanup_page:
-        __free_pages(page, 0);
+        cfs_free_page(page);
 desc_cleanup:
         ptlrpc_free_bulk(desc);
         RETURN(rc);
@@ -1334,18 +1337,14 @@ static int mdt_readpage(struct mdt_thread_info *info)
         int                i;
         ENTRY;
 
-        if (MDT_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
                 RETURN(err_serious(-ENOMEM));
 
-        reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
-        repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
+        reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
+        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         if (reqbody == NULL || repbody == NULL)
                 RETURN(err_serious(-EFAULT));
 
-        rc = mdt_check_ucred(info);
-        if (rc)
-                RETURN(err_serious(rc));
-
         /*
          * prepare @rdpg before calling lower layers and transfer itself. Here
          * reqbody->size contains offset of where to start to read and
@@ -1364,7 +1363,7 @@ static int mdt_readpage(struct mdt_thread_info *info)
                 RETURN(-ENOMEM);
 
         for (i = 0; i < rdpg->rp_npages; ++i) {
-                rdpg->rp_pages[i] = alloc_pages(GFP_KERNEL, 0);
+                rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
                 if (rdpg->rp_pages[i] == NULL)
                         GOTO(free_rdpg, rc = -ENOMEM);
         }
@@ -1382,10 +1381,11 @@ free_rdpg:
 
         for (i = 0; i < rdpg->rp_npages; i++)
                 if (rdpg->rp_pages[i] != NULL)
-                        __free_pages(rdpg->rp_pages[i], 0);
+                        cfs_free_page(rdpg->rp_pages[i]);
         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
 
-        MDT_FAIL_RETURN(OBD_FAIL_MDS_SENDPAGE, 0);
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
+                RETURN(0);
 
         return rc;
 }
@@ -1394,26 +1394,21 @@ static int mdt_reint_internal(struct mdt_thread_info *info,
                               struct mdt_lock_handle *lhc,
                               __u32 op)
 {
-        struct req_capsule      *pill = &info->mti_pill;
+        struct req_capsule      *pill = info->mti_pill;
         struct mdt_device       *mdt = info->mti_mdt;
-        struct ptlrpc_request   *req = mdt_info_req(info);
         struct mdt_body         *repbody;
-        int                      need_shrink = 0;
-        int                      rc;
+        int                      rc = 0;
         ENTRY;
 
         /* pack reply */
-        if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
+        if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
                                      mdt->mdt_max_mdsize);
-                need_shrink = 1;
-        }
-        if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
+        if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
                                      mdt->mdt_max_cookiesize);
-                need_shrink = 1;
-        }
-        rc = req_capsule_pack(pill);
+
+        rc = req_capsule_server_pack(pill);
         if (rc != 0) {
                 CERROR("Can't pack response, rc %d\n", rc);
                 RETURN(err_serious(rc));
@@ -1426,7 +1421,7 @@ static int mdt_reint_internal(struct mdt_thread_info *info,
                 repbody->aclsize = 0;
         }
 
-        if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
                 GOTO(out_shrink, rc = err_serious(-EFAULT));
 
         rc = mdt_reint_unpack(info, op);
@@ -1443,44 +1438,32 @@ static int mdt_reint_internal(struct mdt_thread_info *info,
         if (rc != 0)
                 GOTO(out_ucred, rc = err_serious(rc));
 
-        if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
-                struct mdt_client_data *mcd;
-
-                mcd = req->rq_export->exp_mdt_data.med_mcd;
-                if (req_xid_is_last(req)) {
-                        need_shrink = 0;
-                        mdt_reconstruct(info, lhc);
-                        rc = lustre_msg_get_status(req->rq_repmsg);
-                        GOTO(out_ucred, rc);
-                }
-                DEBUG_REQ(D_HA, req, "no reply for RESENT (xid "LPD64")",
-                          mcd->mcd_last_xid);
+        if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
+                rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
+                GOTO(out_ucred, rc);
         }
-
-        need_shrink = 0;
         rc = mdt_reint_rec(info, lhc);
         EXIT;
 out_ucred:
         mdt_exit_ucred(info);
 out_shrink:
-        if (need_shrink)
-                mdt_shrink_reply(info);
+        mdt_shrink_reply(info);
         return rc;
 }
 
 static long mdt_reint_opcode(struct mdt_thread_info *info,
                              const struct req_format **fmt)
 {
-        __u32 *ptr;
+        struct mdt_rec_reint *rec;
         long opc;
 
         opc = err_serious(-EFAULT);
-        ptr = req_capsule_client_get(&info->mti_pill, &RMF_REINT_OPC);
-        if (ptr != NULL) {
-                opc = *ptr;
+        rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
+        if (rec != NULL) {
+                opc = rec->rr_opcode;
                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
                 if (opc < REINT_MAX && fmt[opc] != NULL)
-                        req_capsule_extend(&info->mti_pill, fmt[opc]);
+                        req_capsule_extend(info->mti_pill, fmt[opc]);
                 else {
                         CERROR("Unsupported opc: %ld\n", opc);
                         opc = err_serious(opc);
@@ -1495,16 +1478,23 @@ static int mdt_reint(struct mdt_thread_info *info)
         int  rc;
 
         static const struct req_format *reint_fmts[REINT_MAX] = {
-                [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
-                [REINT_CREATE]  = &RQF_MDS_REINT_CREATE,
-                [REINT_LINK]    = &RQF_MDS_REINT_LINK,
-                [REINT_UNLINK]  = &RQF_MDS_REINT_UNLINK,
-                [REINT_RENAME]  = &RQF_MDS_REINT_RENAME,
-                [REINT_OPEN]    = &RQF_MDS_REINT_OPEN
+                [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
+                [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
+                [REINT_LINK]     = &RQF_MDS_REINT_LINK,
+                [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
+                [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
+                [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
+                [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
         };
 
         ENTRY;
 
+        if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_REINT_NET,
+                                 OBD_FAIL_MDS_REINT_NET)) {
+                info->mti_fail_id = OBD_FAIL_MDS_REINT_NET;
+                RETURN(0);
+        }
+
         opc = mdt_reint_opcode(info, reint_fmts);
         if (opc >= 0) {
                 /*
@@ -1536,7 +1526,7 @@ static int mdt_object_sync(struct mdt_thread_info *info)
 
 static int mdt_sync(struct mdt_thread_info *info)
 {
-        struct req_capsule *pill = &info->mti_pill;
+        struct req_capsule *pill = info->mti_pill;
         struct mdt_body *body;
         int rc;
         ENTRY;
@@ -1548,12 +1538,12 @@ static int mdt_sync(struct mdt_thread_info *info)
         if (body == NULL)
                 RETURN(err_serious(-EINVAL));
 
-        if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
                 RETURN(err_serious(-ENOMEM));
 
         if (fid_seq(&body->fid1) == 0) {
                 /* sync the whole device */
-                rc = req_capsule_pack(pill);
+                rc = req_capsule_server_pack(pill);
                 if (rc == 0)
                         rc = mdt_device_sync(info);
                 else
@@ -1603,6 +1593,9 @@ static int mdt_obd_ping(struct mdt_thread_info *info)
 {
         int rc;
         ENTRY;
+
+        req_capsule_set(info->mti_pill, &RQF_OBD_PING);
+
         rc = target_handle_ping(mdt_info_req(info));
         if (rc < 0)
                 rc = err_serious(rc);
@@ -1654,7 +1647,9 @@ static int mdt_enqueue(struct mdt_thread_info *info)
          * bits get corrupted somewhere in mdt_intent_policy().
          */
         req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
-        LASSERT(req_bits != 0);
+        /* This is disabled because we need to support liblustre flock.
+         * LASSERT(req_bits != 0);
+         */
 
         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
                                   req, info->mti_dlm_req, &cbs);
@@ -1722,6 +1717,7 @@ struct mdt_object *mdt_object_find(const struct lu_env *env,
         struct mdt_object *m;
         ENTRY;
 
+        CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
         o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
         if (unlikely(IS_ERR(o)))
                 m = (struct mdt_object *)o;
@@ -1932,17 +1928,21 @@ static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
         const struct lu_env      *env;
         struct req_capsule       *pill;
         int                       rc;
+        ENTRY;
 
         env = info->mti_env;
-        pill = &info->mti_pill;
+        pill = info->mti_pill;
 
         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
         if (body == NULL)
-                return -EFAULT;
+                RETURN(-EFAULT);
+
+        if (!(body->valid & OBD_MD_FLID))
+                RETURN(0);
 
         if (!fid_is_sane(&body->fid1)) {
                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
-                return -EINVAL;
+                RETURN(-EINVAL);
         }
 
         /*
@@ -1973,16 +1973,14 @@ static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
         } else
                 rc = PTR_ERR(obj);
 
-        return rc;
+        RETURN(rc);
 }
 
 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
 {
-        struct req_capsule *pill;
+        struct req_capsule *pill = info->mti_pill;
         int rc;
-
         ENTRY;
-        pill = &info->mti_pill;
 
         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
                 rc = mdt_body_unpack(info, flags);
@@ -2000,7 +1998,7 @@ static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
                                              mdt->mdt_max_cookiesize);
 
-                rc = req_capsule_pack(pill);
+                rc = req_capsule_server_pack(pill);
         }
         RETURN(rc);
 }
@@ -2034,9 +2032,9 @@ static int mdt_req_handle(struct mdt_thread_info *info,
         LASSERT(current->journal_info == NULL);
 
         /*
-         * Do not use *_FAIL_CHECK_ONCE() macros, because they will stop
+         * Mask out OBD_FAIL_ONCE, because that will stop
          * correct handling of failed req later in ldlm due to doing
-         * obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED without actually
+         * obd_fail_loc |= OBD_FAIL_ONCE without actually
          * correct actions like it is done in target_send_reply_msg().
          */
         if (h->mh_fail_id != 0) {
@@ -2044,7 +2042,8 @@ static int mdt_req_handle(struct mdt_thread_info *info,
                  * Set to info->mti_fail_id to handler fail_id, it will be used
                  * later, and better than use default fail_id.
                  */
-                if (OBD_FAIL_CHECK(h->mh_fail_id)) {
+                if (OBD_FAIL_CHECK_RESET(h->mh_fail_id && OBD_FAIL_MASK_LOC,
+                                         h->mh_fail_id & ~OBD_FAILED)) {
                         info->mti_fail_id = h->mh_fail_id;
                         RETURN(0);
                 }
@@ -2055,7 +2054,7 @@ static int mdt_req_handle(struct mdt_thread_info *info,
         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
 
         if (h->mh_fmt != NULL) {
-                req_capsule_set(&info->mti_pill, h->mh_fmt);
+                req_capsule_set(info->mti_pill, h->mh_fmt);
                 rc = mdt_unpack_req_pack_rep(info, flags);
         }
 
@@ -2069,14 +2068,13 @@ static int mdt_req_handle(struct mdt_thread_info *info,
 
                 LASSERT(h->mh_fmt != NULL);
 
-                dlm_req = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
+                dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
                 if (dlm_req != NULL) {
                         if (info->mti_mdt->mdt_opts.mo_compat_resname)
                                 rc = mdt_lock_resname_compat(info->mti_mdt,
                                                              dlm_req);
                         info->mti_dlm_req = dlm_req;
                 } else {
-                        CERROR("Can't unpack dlm request\n");
                         rc = -EFAULT;
                 }
         }
@@ -2116,7 +2114,7 @@ static int mdt_req_handle(struct mdt_thread_info *info,
             info->mti_mdt->mdt_opts.mo_compat_resname) {
                 struct ldlm_reply *dlmrep;
 
-                dlmrep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
+                dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
                 if (dlmrep != NULL)
                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
         }
@@ -2160,11 +2158,8 @@ static void mdt_thread_info_init(struct ptlrpc_request *req,
         int i;
         struct md_capainfo *ci;
 
-        info->mti_rep_buf_nr = ARRAY_SIZE(info->mti_rep_buf_size);
-        for (i = 0; i < ARRAY_SIZE(info->mti_rep_buf_size); i++)
-                info->mti_rep_buf_size[i] = -1;
-        req_capsule_init(&info->mti_pill, req, RCL_SERVER,
-                         info->mti_rep_buf_size);
+        req_capsule_init(&req->rq_pill, req, RCL_SERVER);
+        info->mti_pill = &req->rq_pill;
 
         /* lock handle */
         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
@@ -2174,7 +2169,7 @@ static void mdt_thread_info_init(struct ptlrpc_request *req,
         if (req->rq_export) {
                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
                 info->mti_exp = req->rq_export;
-        }         else
+        } else
                 info->mti_mdt = NULL;
         info->mti_env = req->rq_svc_thread->t_env;
         ci = md_capainfo(info->mti_env);
@@ -2200,7 +2195,7 @@ static void mdt_thread_info_fini(struct mdt_thread_info *info)
 {
         int i;
 
-        req_capsule_fini(&info->mti_pill);
+        req_capsule_fini(info->mti_pill);
         if (info->mti_object != NULL) {
                 mdt_object_put(info->mti_env, info->mti_object);
                 info->mti_object = NULL;
@@ -2330,7 +2325,8 @@ static int mdt_handle0(struct ptlrpc_request *req,
 
         ENTRY;
 
-        MDT_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
+        if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
+                RETURN(0);
 
         LASSERT(current->journal_info == NULL);
 
@@ -2619,8 +2615,8 @@ static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
                 return;
 
-        dlmreq = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
-        remote_hdl = dlmreq->lock_handle1;
+        dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
+        remote_hdl = dlmreq->lock_handle[0];
 
         spin_lock(&exp->exp_ldlm_data.led_lock);
         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
@@ -2633,7 +2629,8 @@ static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
                         lh->mlh_reg_mode = lock->l_granted_mode;
 
                         LDLM_DEBUG(lock, "restoring lock cookie");
-                        DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
+                        DEBUG_REQ(D_DLMTRACE, req,
+                                  "restoring lock cookie "LPX64,
                                   lh->mlh_reg_lh.cookie);
                         if (old_lock)
                                 *old_lock = LDLM_LOCK_GET(lock);
@@ -2657,7 +2654,7 @@ static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
          */
         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
 
-        DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
+        DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
                   remote_hdl.cookie);
 }
 
@@ -2676,10 +2673,10 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
         int                     rc;
         ENTRY;
 
-        reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
+        reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(reqbody);
 
-        repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
+        repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
         LASSERT(repbody);
 
         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
@@ -2696,15 +2693,15 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
                 break;
         default:
                 CERROR("Unhandled till now");
-                GOTO(out, rc = -EINVAL);
+                GOTO(out_shrink, rc = -EINVAL);
         }
 
         rc = mdt_init_ucred(info, reqbody);
         if (rc)
-                GOTO(out, rc);
+                GOTO(out_shrink, rc);
 
-        req = info->mti_pill.rc_req;
-        ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
+        req = info->mti_pill->rc_req;
+        ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
 
         /* Get lock from request for possible resent case. */
@@ -2725,7 +2722,7 @@ static int mdt_intent_getattr(enum mdt_it_code opcode,
         EXIT;
 out_ucred:
         mdt_exit_ucred(info);
-out:
+out_shrink:
         mdt_shrink_reply(info);
         return rc;
 }
@@ -2749,12 +2746,12 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
 
         opc = mdt_reint_opcode(info, intent_fmts);
         if (opc < 0)
-                GOTO(out, rc = opc);
+                RETURN(opc);
 
         if (mdt_it_flavor[opcode].it_reint != opc) {
                 CERROR("Reint code %ld doesn't match intent: %d\n",
                        opc, opcode);
-                GOTO(out, rc = err_serious(-EPROTO));
+                RETURN(err_serious(-EPROTO));
         }
 
         /* Get lock from request for possible resent case. */
@@ -2764,9 +2761,9 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
 
         /* Check whether the reply has been packed successfully. */
         if (mdt_info_req(info)->rq_repmsg != NULL)
-                rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
+                rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
         if (rep == NULL)
-                GOTO(out, rc = err_serious(-EFAULT));
+                RETURN(err_serious(-EFAULT));
 
         /* MDC expects this in any case */
         if (rc != 0)
@@ -2777,15 +2774,13 @@ static int mdt_intent_reint(enum mdt_it_code opcode,
                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
                 rep->lock_policy_res2 = 0;
                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
-                GOTO(out, rc);
+                RETURN(rc);
         }
         rep->lock_policy_res2 = clear_serious(rc);
 
         lhc->mlh_reg_lh.cookie = 0ull;
         rc = ELDLM_LOCK_ABORTED;
-        EXIT;
-out:
-        return rc;
+        RETURN(rc);
 }
 
 static int mdt_intent_code(long itcode)
@@ -2841,7 +2836,7 @@ static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
         if (opc < 0)
                 RETURN(-EINVAL);
 
-        pill = &info->mti_pill;
+        pill = info->mti_pill;
         flv  = &mdt_it_flavor[opc];
 
         if (flv->it_fmt != NULL)
@@ -2852,13 +2847,14 @@ static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
                 struct ptlrpc_request *req = mdt_info_req(info);
                 if (flv->it_flags & MUTABOR &&
                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
-                        rc = -EROFS;
+                        RETURN(-EROFS);
         }
         if (rc == 0 && flv->it_act != NULL) {
                 /* execute policy */
                 rc = flv->it_act(opc, info, lockp, flags);
-        } else
+        } else {
                 rc = -EOPNOTSUPP;
+        }
         RETURN(rc);
 }
 
@@ -2879,7 +2875,7 @@ static int mdt_intent_policy(struct ldlm_namespace *ns,
         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
                                   &mdt_thread_key);
         LASSERT(info != NULL);
-        pill = &info->mti_pill;
+        pill = info->mti_pill;
         LASSERT(pill->rc_req == req);
 
         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
@@ -2914,7 +2910,7 @@ static int mdt_intent_policy(struct ldlm_namespace *ns,
         } else {
                 /* No intent was provided */
                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
-                rc = req_capsule_pack(pill);
+                rc = req_capsule_server_pack(pill);
                 if (rc)
                         rc = err_serious(rc);
         }
@@ -3524,14 +3520,37 @@ static void mdt_stack_fini(const struct lu_env *env,
 
         lu_site_purge(env, top->ld_site, ~0);
         while (d != NULL) {
-                struct obd_type *type;
                 struct lu_device_type *ldt = d->ld_type;
 
                 /* each fini() returns next device in stack of layers
-                 * so we can avoid the recursion */
+                 * so we can avoid the recursion */
                 n = ldt->ldt_ops->ldto_device_fini(env, d);
                 lu_device_put(d);
-                ldt->ldt_ops->ldto_device_free(env, d);
+
+                /* switch to the next device in the layer */
+                d = n;
+        }
+        /* purge again. */
+        lu_site_purge(env, top->ld_site, ~0);
+
+        if (!list_empty(&top->ld_site->ls_lru) || top->ld_site->ls_total != 0) {
+                /*
+                 * Uh-oh, objects still exist.
+                 */
+                static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
+
+                lu_site_print(env, top->ld_site, &cookie, lu_cdebug_printer);
+        }
+
+        d = top;
+        while (d != NULL) {
+                struct obd_type *type;
+                struct lu_device_type *ldt = d->ld_type;
+
+                /* each free() returns next device in stack of layers
+                 * so we can avoid the recursion */
+                n = ldt->ldt_ops->ldto_device_free(env, d);
                 type = ldt->ldt_obd_type;
                 type->typ_refcnt--;
                 class_put_type(type);
@@ -3669,15 +3688,12 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
         ENTRY;
 
         ping_evictor_stop();
-        
+
         target_recovery_fini(obd);
         mdt_stop_ptlrpc_service(m);
 
         mdt_fs_cleanup(env, m);
 
-        upcall_cache_cleanup(m->mdt_rmtacl_cache);
-        m->mdt_rmtacl_cache = NULL;
-
         upcall_cache_cleanup(m->mdt_identity_cache);
         m->mdt_identity_cache = NULL;
 
@@ -3693,10 +3709,7 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
         ptlrpc_lprocfs_unregister_obd(d->ld_obd);
         lprocfs_obd_cleanup(d->ld_obd);
 
-        if (m->mdt_rootsquash_info) {
-                OBD_FREE_PTR(m->mdt_rootsquash_info);
-                m->mdt_rootsquash_info = NULL;
-        }
+        sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
 
         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
         del_timer(&m->mdt_ck_timer);
@@ -3706,15 +3719,6 @@ static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
 
         if (ls) {
-                if (!list_empty(&ls->ls_lru) || ls->ls_total != 0) {
-                        /*
-                         * Uh-oh, objects still exist.
-                         */
-                        static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
-
-                        lu_site_print(env, ls, &cookie, lu_cdebug_printer);
-                }
-
                 lu_site_fini(ls);
                 OBD_FREE_PTR(ls);
                 d->ld_site = NULL;
@@ -3729,6 +3733,15 @@ static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
 {
         char *p = options;
 
+#ifdef CONFIG_FS_POSIX_ACL
+        /* ACLs should be enabled by default (b=13829) */
+        m->mdt_opts.mo_acl = 1;
+        LCONSOLE_INFO("Enabling ACL\n");
+#else
+        m->mdt_opts.mo_acl = 0;
+        LCONSOLE_INFO("Disabling ACL\n");
+#endif
+
         if (!options)
                 return;
 
@@ -3747,16 +3760,6 @@ static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
                            (memcmp(options, "nouser_xattr", len) == 0)) {
                         m->mdt_opts.mo_user_xattr = 0;
                         LCONSOLE_INFO("Disabling user_xattr\n");
-                } else if ((len == sizeof("acl") - 1) &&
-                           (memcmp(options, "acl", len) == 0)) {
-#ifdef CONFIG_FS_POSIX_ACL
-                        m->mdt_opts.mo_acl = 1;
-                        LCONSOLE_INFO("Enabling ACL\n");
-#else
-                        m->mdt_opts.mo_acl = 0;
-                        CWARN("ignoring unsupported acl mount option\n");
-                        LCONSOLE_INFO("Disabling ACL\n");
-#endif
                 } else if ((len == sizeof("noacl") - 1) &&
                            (memcmp(options, "noacl", len) == 0)) {
                         m->mdt_opts.mo_acl = 0;
@@ -3780,6 +3783,7 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         struct lustre_mount_info  *lmi;
         struct lustre_sb_info     *lsi;
         struct lu_site            *s;
+        const char                *identity_upcall = "NONE";
         int                        rc;
         ENTRY;
 
@@ -3806,6 +3810,9 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
                 server_put_mount_2(dev, lmi->lmi_mnt);
         }
 
+        m->mdt_sptlrpc_lock = RW_LOCK_UNLOCKED;
+        sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
+
         spin_lock_init(&m->mdt_ioepoch_lock);
         m->mdt_opts.mo_compat_resname = 0;
         m->mdt_capa_timeout = CAPA_TIMEOUT;
@@ -3830,7 +3837,7 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
                 GOTO(err_free_site, rc);
         }
 
-        lprocfs_init_vars(mdt, &lvars);
+        lprocfs_mdt_init_vars(&lvars);
         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
         if (rc) {
                 CERROR("Can't init lprocfs, rc %d\n", rc);
@@ -3882,7 +3889,8 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
                  LUSTRE_MDT_NAME"-%p", m);
         m->mdt_namespace = ldlm_namespace_new(info->mti_u.ns_name,
-                                              LDLM_NAMESPACE_SERVER);
+                                              LDLM_NAMESPACE_SERVER,
+                                              LDLM_NAMESPACE_GREEDY);
         if (m->mdt_namespace == NULL)
                 GOTO(err_fini_seq, rc = -ENOMEM);
 
@@ -3890,8 +3898,12 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
         /* set obd_namespace for compatibility with old code */
         obd->obd_namespace = m->mdt_namespace;
 
-        m->mdt_identity_cache = upcall_cache_init(obd->obd_name,
-                                                  "NONE",
+        /* XXX: to support suppgid for ACL, we enable identity_upcall
+         * by default, otherwise, maybe got unexpected -EACCESS. */
+        if (m->mdt_opts.mo_acl)
+                identity_upcall = MDT_IDENTITY_UPCALL_PATH;
+
+        m->mdt_identity_cache = upcall_cache_init(obd->obd_name, identity_upcall,
                                                   &mdt_identity_upcall_cache_ops);
         if (IS_ERR(m->mdt_identity_cache)) {
                 rc = PTR_ERR(m->mdt_identity_cache);
@@ -3899,15 +3911,6 @@ static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
                 GOTO(err_free_ns, rc);
         }
 
-        m->mdt_rmtacl_cache = upcall_cache_init(obd->obd_name,
-                                                MDT_RMTACL_UPCALL_PATH,
-                                                &mdt_rmtacl_upcall_cache_ops);
-        if (IS_ERR(m->mdt_rmtacl_cache)) {
-                rc = PTR_ERR(m->mdt_rmtacl_cache);
-                m->mdt_rmtacl_cache = NULL;
-                GOTO(err_free_ns, rc);
-        }
-
         m->mdt_ck_timer.function = mdt_ck_timer_callback;
         m->mdt_ck_timer.data = (unsigned long)m;
         init_timer(&m->mdt_ck_timer);
@@ -3951,8 +3954,6 @@ err_capa:
         del_timer(&m->mdt_ck_timer);
         mdt_ck_thread_stop(m);
 err_free_ns:
-        upcall_cache_cleanup(m->mdt_rmtacl_cache);
-        m->mdt_rmtacl_cache = NULL;
         upcall_cache_cleanup(m->mdt_identity_cache);
         m->mdt_identity_cache = NULL;
         ldlm_namespace_free(m->mdt_namespace, 0);
@@ -3986,11 +3987,39 @@ static int mdt_process_config(const struct lu_env *env,
         ENTRY;
 
         switch (cfg->lcfg_command) {
+        case LCFG_SPTLRPC_CONF: {
+                struct sptlrpc_conf_log *log;
+                struct sptlrpc_rule_set  tmp_rset;
+
+                log = sptlrpc_conf_log_extract(cfg);
+                if (IS_ERR(log)) {
+                        rc = PTR_ERR(log);
+                        break;
+                }
+
+                sptlrpc_rule_set_init(&tmp_rset);
+
+                rc = sptlrpc_rule_set_from_log(&tmp_rset, log);
+                if (rc) {
+                        CERROR("mdt %p: failed get sptlrpc rules: %d\n", m, rc);
+                        break;
+                }
+
+                write_lock(&m->mdt_sptlrpc_lock);
+                sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
+                m->mdt_sptlrpc_rset = tmp_rset;
+                write_unlock(&m->mdt_sptlrpc_lock);
+
+                sptlrpc_target_update_exp_flavor(
+                                md2lu_dev(&m->mdt_md_dev)->ld_obd, &tmp_rset);
+
+                break;
+        }
         case LCFG_PARAM: {
                 struct lprocfs_static_vars lvars;
                 struct obd_device *obd = d->ld_obd;
 
-                lprocfs_init_vars(mdt, &lvars);
+                lprocfs_mdt_init_vars(&lvars);
                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
                 if (rc)
                         /* others are passed further */
@@ -4162,6 +4191,12 @@ static int mdt_connect_internal(struct obd_export *exp,
                 return -EBADE;
         }
 
+        if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
+                CWARN("%s: MDS requires FID support, but client not\n",
+                      mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
+                return -EBADE;
+        }
+
         return 0;
 }
 
@@ -4171,9 +4206,11 @@ static int mdt_obd_connect(const struct lu_env *env,
                            struct obd_uuid *cluuid,
                            struct obd_connect_data *data)
 {
+        struct mdt_thread_info *info;
         struct mdt_client_data *mcd;
         struct obd_export      *exp;
         struct mdt_device      *mdt;
+        struct ptlrpc_request  *req;
         int                     rc;
         ENTRY;
 
@@ -4181,6 +4218,8 @@ static int mdt_obd_connect(const struct lu_env *env,
         if (!conn || !obd || !cluuid)
                 RETURN(-EINVAL);
 
+        info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
+        req = info->mti_pill->rc_req;
         mdt = mdt_dev(obd->obd_lu_dev);
 
         rc = class_connect(conn, obd, cluuid);
@@ -4190,6 +4229,26 @@ static int mdt_obd_connect(const struct lu_env *env,
         exp = class_conn2export(conn);
         LASSERT(exp != NULL);
 
+        CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
+
+        spin_lock(&exp->exp_lock);
+        exp->exp_sp_peer = req->rq_sp_from;
+
+        read_lock(&mdt->mdt_sptlrpc_lock);
+        sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset, exp->exp_sp_peer,
+                                req->rq_peer.nid, &exp->exp_flvr);
+        read_unlock(&mdt->mdt_sptlrpc_lock);
+
+        if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
+                CERROR("invalid rpc flavor %x, expect %x, from %s\n",
+                       req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
+                       libcfs_nid2str(req->rq_peer.nid));
+                exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
+                spin_unlock(&exp->exp_lock);
+                RETURN(-EACCES);
+        }
+        spin_unlock(&exp->exp_lock);
+
         rc = mdt_connect_internal(exp, mdt, data);
         if (rc == 0) {
                 OBD_ALLOC_PTR(mcd);
@@ -4218,16 +4277,47 @@ static int mdt_obd_connect(const struct lu_env *env,
         RETURN(rc);
 }
 
-static int mdt_obd_reconnect(struct obd_export *exp, struct obd_device *obd,
+static int mdt_obd_reconnect(const struct lu_env *env,
+                             struct obd_export *exp, struct obd_device *obd,
                              struct obd_uuid *cluuid,
                              struct obd_connect_data *data)
 {
-        int rc;
+        struct mdt_thread_info *info;
+        struct mdt_device      *mdt;
+        struct ptlrpc_request  *req;
+        int                     rc;
         ENTRY;
 
         if (exp == NULL || obd == NULL || cluuid == NULL)
                 RETURN(-EINVAL);
 
+        info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
+        req = info->mti_pill->rc_req;
+        mdt = mdt_dev(obd->obd_lu_dev);
+
+        CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
+
+        spin_lock(&exp->exp_lock);
+        if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
+                exp->exp_sp_peer = req->rq_sp_from;
+
+                read_lock(&mdt->mdt_sptlrpc_lock);
+                sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset,
+                                        exp->exp_sp_peer,
+                                        req->rq_peer.nid, &exp->exp_flvr);
+                read_unlock(&mdt->mdt_sptlrpc_lock);
+
+                if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
+                        CERROR("invalid rpc flavor %x, expect %x, from %s\n",
+                               req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
+                               libcfs_nid2str(req->rq_peer.nid));
+                        exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
+                        spin_unlock(&exp->exp_lock);
+                        RETURN(-EACCES);
+                }
+        }
+        spin_unlock(&exp->exp_lock);
+
         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
 
         RETURN(rc);
@@ -4274,6 +4364,8 @@ static int mdt_init_export(struct obd_export *exp)
 
         INIT_LIST_HEAD(&med->med_open_head);
         spin_lock_init(&med->med_open_lock);
+        sema_init(&med->med_idmap_sem, 1);
+        med->med_idmap = NULL;
         spin_lock(&exp->exp_lock);
         exp->exp_connecting = 1;
         spin_unlock(&exp->exp_lock);
@@ -4367,6 +4459,18 @@ out:
         return rc;
 }
 
+static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
+{
+        if (flag & CONFIG_LOG)
+                m->mdt_fl_cfglog = 1;
+        if (flag & CONFIG_SYNC)
+                m->mdt_fl_synced = 1;
+
+        if (m->mdt_fl_cfglog /* bz11778: && m->mdt_fl_synced */)
+                /* Open for clients */
+                m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
+}
+
 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
                       enum md_upcall_event ev)
 {
@@ -4383,12 +4487,17 @@ static int mdt_upcall(const struct lu_env *env, struct md_device *md,
                                         &m->mdt_max_cookiesize);
                         CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
                                      m->mdt_max_mdsize, m->mdt_max_cookiesize);
+                        mdt_allow_cli(m, CONFIG_SYNC);
                         break;
                 case MD_NO_TRANS:
                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
                         mti->mti_no_need_trans = 1;
                         CDEBUG(D_INFO, "disable mdt trans for this thread\n");
                         break;
+                case MD_LOV_CONFIG:
+                        /* Check that MDT is not yet configured */
+                        LASSERT(!m->mdt_fl_cfglog);
+                        break;
                 default:
                         CERROR("invalid event\n");
                         rc = -EINVAL;
@@ -4399,15 +4508,16 @@ static int mdt_upcall(const struct lu_env *env, struct md_device *md,
 
 static int mdt_obd_notify(struct obd_device *host,
                           struct obd_device *watched,
-                          enum obd_notify_event ev, void *owner)
+                          enum obd_notify_event ev, void *data)
 {
         ENTRY;
 
         switch (ev) {
         case OBD_NOTIFY_CONFIG:
-                host->obd_no_conn = 0;
+                mdt_allow_cli(mdt_dev(host->obd_lu_dev), (unsigned int)data);
+                break;
         default:
-                CDEBUG(D_INFO, "Notification 0x%x\n", ev);
+                CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
         }
         RETURN(0);
 }
@@ -4456,11 +4566,11 @@ int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
         struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
         int rc, lost;
         ENTRY;
-        /* if some clients didn't participate in recovery then we can possibly 
+        /* if some clients didn't participate in recovery then we can possibly
          * lost sequence. Now we should increase sequence for safe value */
         lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
         mdt_seq_adjust(env, mdt, lost);
-        
+
         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
         RETURN(rc);
 }
@@ -4500,11 +4610,14 @@ static struct lu_device* mdt_device_fini(const struct lu_env *env,
         RETURN(NULL);
 }
 
-static void mdt_device_free(const struct lu_env *env, struct lu_device *d)
+static struct lu_device *mdt_device_free(const struct lu_env *env,
+                                         struct lu_device *d)
 {
         struct mdt_device *m = mdt_dev(d);
+        ENTRY;
 
         OBD_FREE_PTR(m);
+        RETURN(NULL);
 }
 
 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
@@ -4531,59 +4644,14 @@ static struct lu_device *mdt_device_alloc(const struct lu_env *env,
         return l;
 }
 
-/*
- * context key constructor/destructor
- */
-static void *mdt_key_init(const struct lu_context *ctx,
-                          struct lu_context_key *key)
-{
-        struct mdt_thread_info *info;
-
-        /*
-         * check that no high order allocations are incurred.
-         */
-        CLASSERT(CFS_PAGE_SIZE >= sizeof *info);
-        OBD_ALLOC_PTR(info);
-        if (info == NULL)
-                info = ERR_PTR(-ENOMEM);
-        return info;
-}
+/* context key constructor/destructor: mdt_key_init, mdt_key_fini */
+LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
 
-static void mdt_key_fini(const struct lu_context *ctx,
-                         struct lu_context_key *key, void *data)
-{
-        struct mdt_thread_info *info = data;
-        OBD_FREE_PTR(info);
-}
+/* context key: mdt_thread_key */
+LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
 
-struct lu_context_key mdt_thread_key = {
-        .lct_tags = LCT_MD_THREAD,
-        .lct_init = mdt_key_init,
-        .lct_fini = mdt_key_fini
-};
-
-static void *mdt_txn_key_init(const struct lu_context *ctx,
-                              struct lu_context_key *key)
-{
-        struct mdt_txn_info *txi;
-
-        /*
-         * check that no high order allocations are incurred.
-         */
-        CLASSERT(CFS_PAGE_SIZE >= sizeof *txi);
-        OBD_ALLOC_PTR(txi);
-        if (txi == NULL)
-                txi = ERR_PTR(-ENOMEM);
-        memset(txi, 0, sizeof(*txi));
-        return txi;
-}
-
-static void mdt_txn_key_fini(const struct lu_context *ctx,
-                             struct lu_context_key *key, void *data)
-{
-        struct mdt_txn_info *txi = data;
-        OBD_FREE_PTR(txi);
-}
+/* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
+LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
 
 struct lu_context_key mdt_txn_key = {
         .lct_tags = LCT_TX_HANDLE,
@@ -4596,24 +4664,8 @@ struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
         return md_ucred(info->mti_env);
 }
 
-static int mdt_type_init(struct lu_device_type *t)
-{
-        int rc;
-
-        LU_CONTEXT_KEY_INIT(&mdt_thread_key);
-        rc = lu_context_key_register(&mdt_thread_key);
-        if (rc == 0) {
-                LU_CONTEXT_KEY_INIT(&mdt_txn_key);
-                rc = lu_context_key_register(&mdt_txn_key);
-        }
-        return rc;
-}
-
-static void mdt_type_fini(struct lu_device_type *t)
-{
-        lu_context_key_degister(&mdt_thread_key);
-        lu_context_key_degister(&mdt_txn_key);
-}
+/* type constructor/destructor: mdt_type_init, mdt_type_fini */
+LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
 
 static struct lu_device_type_operations mdt_device_type_ops = {
         .ldto_init = mdt_type_init,
@@ -4637,7 +4689,7 @@ static int __init mdt_mod_init(void)
         int rc;
 
         mdt_num_threads = MDT_NUM_THREADS;
-        lprocfs_init_vars(mdt, &lvars);
+        lprocfs_mdt_init_vars(&lvars);
         rc = class_register_type(&mdt_obd_device_ops, NULL,
                                  lvars.module_vars, LUSTRE_MDT_NAME,
                                  &mdt_device_type);
@@ -4693,7 +4745,6 @@ DEF_MDT_HNDL_F(0,                         SET_INFO,     mdt_set_info),
 DEF_MDT_HNDL_F(0           |HABEO_REFERO, GETSTATUS,    mdt_getstatus),
 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETATTR,      mdt_getattr),
 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
-DEF_MDT_HNDL_F(HABEO_CORPUS|MUTABOR,      SETXATTR,     mdt_setxattr),
 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETXATTR,     mdt_getxattr),
 DEF_MDT_HNDL_F(0           |HABEO_REFERO, STATFS,       mdt_statfs),
 DEF_MDT_HNDL_F(0           |MUTABOR,      REINT,        mdt_reint),
@@ -4702,8 +4753,8 @@ DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING, mdt_done_writing),
 DEF_MDT_HNDL_F(0           |HABEO_REFERO, PIN,          mdt_pin),
 DEF_MDT_HNDL_0(0,                         SYNC,         mdt_sync),
 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
-DEF_MDT_HNDL_0(0,                         QUOTACHECK,   mdt_quotacheck_handle),
-DEF_MDT_HNDL_0(0,                         QUOTACTL,     mdt_quotactl_handle)
+DEF_MDT_HNDL_F(0,                         QUOTACHECK,   mdt_quotacheck_handle),
+DEF_MDT_HNDL_F(0,                         QUOTACTL,     mdt_quotactl_handle)
 };
 
 #define DEF_OBD_HNDL(flags, name, fn)                   \
@@ -4816,6 +4867,11 @@ static struct mdt_opc_slice mdt_xmds_handlers[] = {
                 .mos_hs        = mdt_obd_ops
         },
         {
+                .mos_opc_start = SEC_CTX_INIT,
+                .mos_opc_end   = SEC_LAST_OPC,
+                .mos_hs        = mdt_sec_ctx_ops
+        },
+        {
                 .mos_hs        = NULL
         }
 };