Whamcloud - gitweb
LU-3086 build: fix 'uninitialized variables' errors
[fs/lustre-release.git] / lustre / mdt / mdt_open.c
index a614bf3..456f403 100644 (file)
@@ -108,29 +108,34 @@ void mdt_mfd_free(struct mdt_file_data *mfd)
 static int mdt_create_data(struct mdt_thread_info *info,
                            struct mdt_object *p, struct mdt_object *o)
 {
-        struct md_op_spec     *spec = &info->mti_spec;
-        struct md_attr        *ma   = &info->mti_attr;
-        int                    rc   = 0;
-        ENTRY;
+       struct md_op_spec     *spec = &info->mti_spec;
+       struct md_attr        *ma   = &info->mti_attr;
+       int                    rc   = 0;
+       ENTRY;
 
-        if (!md_should_create(spec->sp_cr_flags))
-                RETURN(0);
+       if (!md_should_create(spec->sp_cr_flags))
+               RETURN(0);
 
-        ma->ma_need = MA_INODE | MA_LOV;
-        ma->ma_valid = 0;
+       ma->ma_need = MA_INODE | MA_LOV;
+       ma->ma_valid = 0;
        mutex_lock(&o->mot_lov_mutex);
-        if (!(o->mot_flags & MOF_LOV_CREATED)) {
-                rc = mdo_create_data(info->mti_env,
-                                     p ? mdt_object_child(p) : NULL,
-                                     mdt_object_child(o), spec, ma);
+       if (!(o->mot_flags & MOF_LOV_CREATED)) {
+               if (p != NULL && (fid_is_obf(mdt_object_fid(p)) ||
+                                 fid_is_dot_lustre(mdt_object_fid(p))))
+                       GOTO(unlock, rc = -EPERM);
+
+               rc = mdo_create_data(info->mti_env,
+                                    p ? mdt_object_child(p) : NULL,
+                                    mdt_object_child(o), spec, ma);
                if (rc == 0)
                        rc = mdt_attr_get_complex(info, o, ma);
 
-                if (rc == 0 && ma->ma_valid & MA_LOV)
-                        o->mot_flags |= MOF_LOV_CREATED;
-        }
+               if (rc == 0 && ma->ma_valid & MA_LOV)
+                       o->mot_flags |= MOF_LOV_CREATED;
+       }
+unlock:
        mutex_unlock(&o->mot_lov_mutex);
-        RETURN(rc);
+       RETURN(rc);
 }
 
 static int mdt_ioepoch_opened(struct mdt_object *mo)
@@ -577,10 +582,9 @@ static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
                        struct obd_export *exp = req->rq_export;
 
                        CERROR("%s: replay trans "LPU64" NID %s: rc = %d\n",
-                               mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name,
-                               info->mti_transno,
-                               libcfs_nid2str(exp->exp_connection->c_peer.nid),
-                               rc);
+                              mdt_obd_name(mdt), info->mti_transno,
+                              libcfs_nid2str(exp->exp_connection->c_peer.nid),
+                              rc);
                        RETURN_EXIT;
                }
        } else if (info->mti_transno == 0) {
@@ -592,18 +596,18 @@ static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
        }
        spin_unlock(&mdt->mdt_lut.lut_translock);
 
-        CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
-                        info->mti_transno,
-                        req->rq_export->exp_obd->obd_last_committed);
+       CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
+              info->mti_transno,
+              req->rq_export->exp_obd->obd_last_committed);
 
-        req->rq_transno = info->mti_transno;
-        lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
+       req->rq_transno = info->mti_transno;
+       lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
 
-        /* update lcd in memory only for resent cases */
-        ted = &req->rq_export->exp_target_data;
-        LASSERT(ted);
+       /* update lcd in memory only for resent cases */
+       ted = &req->rq_export->exp_target_data;
+       LASSERT(ted);
        mutex_lock(&ted->ted_lcd_lock);
-        lcd = ted->ted_lcd;
+       lcd = ted->ted_lcd;
        if (info->mti_transno < lcd->lcd_last_transno &&
            info->mti_transno != 0) {
                /* This should happen during replay. Do not update
@@ -612,8 +616,8 @@ static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
                 * be checked correctly by xid */
                mutex_unlock(&ted->ted_lcd_lock);
                CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
-                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name,
-                       info->mti_transno, lcd->lcd_last_transno);
+                      mdt_obd_name(mdt), info->mti_transno,
+                      lcd->lcd_last_transno);
                RETURN_EXIT;
        }
 
@@ -704,7 +708,13 @@ static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
                         repbody->ioepoch = o->mot_ioepoch;
                 }
         } else if (flags & MDS_FMODE_EXEC) {
-                rc = mdt_write_deny(o);
+               /* if file is released, we can't deny write because we must
+                * restore (write) it to access it.*/
+               if ((ma->ma_valid & MA_HSM) &&
+                   (ma->ma_hsm.mh_flags & HS_RELEASED))
+                       rc = 0;
+               else
+                       rc = mdt_write_deny(o);
         }
         if (rc)
                 RETURN(rc);
@@ -827,9 +837,13 @@ int mdt_finish_open(struct mdt_thread_info *info,
         * that looks like it was actually almost succesful and a failure at the
         * same time */
        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
-               mdt_set_disposition(info, rep, DISP_OPEN_LOCK | \
-                                   DISP_OPEN_OPEN | DISP_LOOKUP_NEG | \
-                                   DISP_LOOKUP_POS);
+               mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
+                                              DISP_LOOKUP_NEG |
+                                              DISP_LOOKUP_POS);
+
+               if (flags & MDS_OPEN_LOCK)
+                       mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
+
                RETURN(-ENOENT);
        }
 
@@ -846,7 +860,7 @@ int mdt_finish_open(struct mdt_thread_info *info,
                 }
         }
 #ifdef CONFIG_FS_POSIX_ACL
-        else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
+       else if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
                 const struct lu_env *env = info->mti_env;
                 struct md_object *next = mdt_object_child(o);
                 struct lu_buf *buf = &info->mti_buf;
@@ -876,8 +890,8 @@ int mdt_finish_open(struct mdt_thread_info *info,
         }
 #endif
 
-        if (info->mti_mdt->mdt_opts.mo_mds_capa &&
-            exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
+       if (info->mti_mdt->mdt_opts.mo_mds_capa &&
+           exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
                 struct lustre_capa *capa;
 
                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
@@ -888,9 +902,9 @@ int mdt_finish_open(struct mdt_thread_info *info,
                         RETURN(rc);
                 repbody->valid |= OBD_MD_FLMDSCAPA;
         }
-        if (info->mti_mdt->mdt_opts.mo_oss_capa &&
-            exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
-            S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
+       if (info->mti_mdt->mdt_opts.mo_oss_capa &&
+           exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
+           S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
                 struct lustre_capa *capa;
 
                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
@@ -906,11 +920,11 @@ int mdt_finish_open(struct mdt_thread_info *info,
          * If we are following a symlink, don't open; and do not return open
          * handle for special nodes as client required.
          */
-        if (islnk || (!isreg && !isdir &&
-            (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
-                lustre_msg_set_transno(req->rq_repmsg, 0);
-                RETURN(0);
-        }
+       if (islnk || (!isreg && !isdir &&
+           (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
+               lustre_msg_set_transno(req->rq_repmsg, 0);
+               RETURN(0);
+       }
 
         /*
          * We need to return the existing object's fid back, so it is done here,
@@ -996,7 +1010,7 @@ void mdt_reconstruct_open(struct mdt_thread_info *info,
         ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
         ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
                                                RCL_SERVER);
-        ma->ma_need = MA_INODE;
+       ma->ma_need = MA_INODE | MA_HSM;
         if (ma->ma_lmm_size > 0)
                 ma->ma_need |= MA_LOV;
 
@@ -1042,25 +1056,35 @@ void mdt_reconstruct_open(struct mdt_thread_info *info,
                         mdt_export_evict(exp);
                         RETURN_EXIT;
                 }
-                rc = mdt_object_exists(child);
-                if (rc > 0) {
-
-                        mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
-                       rc = mdt_attr_get_complex(info, child, ma);
-                        if (rc == 0)
-                              rc = mdt_finish_open(info, parent, child,
-                                                   flags, 1, ldlm_rep);
-                } else if (rc < 0) {
-                        /* the child object was created on remote server */
-                        repbody->fid1 = *rr->rr_fid2;
-                        repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
-                        rc = 0;
-                } else if (rc == 0) {
-                        /* the child does not exist, we should do regular open */
-                        mdt_object_put(env, parent);
-                        mdt_object_put(env, child);
-                        GOTO(regular_open, 0);
-                }
+
+               if (unlikely(mdt_object_remote(child))) {
+                       /* the child object was created on remote server */
+                       if (!mdt_is_dne_client(exp)) {
+                               /* Return -EIO for old client */
+                               mdt_object_put(env, parent);
+                               mdt_object_put(env, child);
+                               GOTO(out, rc = -EIO);
+                       }
+                       repbody->fid1 = *rr->rr_fid2;
+                       repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
+                       rc = 0;
+               } else {
+                       if (mdt_object_exists(child)) {
+                               mdt_set_capainfo(info, 1, rr->rr_fid2,
+                                                BYPASS_CAPA);
+                               rc = mdt_attr_get_complex(info, child, ma);
+                               if (rc == 0)
+                                       rc = mdt_finish_open(info, parent,
+                                                            child, flags,
+                                                            1, ldlm_rep);
+                       } else {
+                               /* the child does not exist, we should do
+                                * regular open */
+                               mdt_object_put(env, parent);
+                               mdt_object_put(env, child);
+                               GOTO(regular_open, 0);
+                       }
+               }
                 mdt_object_put(env, parent);
                 mdt_object_put(env, child);
                 GOTO(out, rc);
@@ -1091,30 +1115,156 @@ int mdt_open_by_fid(struct mdt_thread_info* info,
         if (IS_ERR(o))
                 RETURN(rc = PTR_ERR(o));
 
-        rc = mdt_object_exists(o);
-        if (rc > 0) {
-                mdt_set_disposition(info, rep, (DISP_IT_EXECD |
-                                                DISP_LOOKUP_EXECD |
-                                                DISP_LOOKUP_POS));
-
-               rc = mdt_attr_get_complex(info, o, ma);
-                if (rc == 0)
-                        rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
-        } else if (rc == 0) {
-                rc = -ENOENT;
-        } else  {
+       if (unlikely(mdt_object_remote(o))) {
                 /* the child object was created on remote server */
                 struct mdt_body *repbody;
+
+               mdt_set_disposition(info, rep, (DISP_IT_EXECD |
+                                               DISP_LOOKUP_EXECD |
+                                               DISP_LOOKUP_POS));
                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
                 repbody->fid1 = *rr->rr_fid2;
                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
                 rc = 0;
-        }
+       } else {
+               if (mdt_object_exists(o)) {
+                       mdt_set_disposition(info, rep, (DISP_IT_EXECD |
+                                                       DISP_LOOKUP_EXECD |
+                                                       DISP_LOOKUP_POS));
+
+                       rc = mdt_attr_get_complex(info, o, ma);
+                       if (rc == 0)
+                               rc = mdt_finish_open(info, NULL, o, flags, 0,
+                                                    rep);
+               } else {
+                       rc = -ENOENT;
+               }
+       }
 
         mdt_object_put(info->mti_env, o);
         RETURN(rc);
 }
 
+/* lock object for open */
+static int mdt_object_open_lock(struct mdt_thread_info *info,
+                               struct mdt_object *obj,
+                               struct mdt_lock_handle *lhc,
+                               __u64 *ibits)
+{
+       struct md_attr  *ma = &info->mti_attr;
+       __u64            open_flags = info->mti_spec.sp_cr_flags;
+       ldlm_mode_t      lm = LCK_CR;
+       bool             try_layout = false;
+       bool             create_layout = false;
+       int              rc = 0;
+       ENTRY;
+
+       *ibits = 0;
+       if (open_flags & MDS_OPEN_LOCK) {
+               if (open_flags & FMODE_WRITE)
+                       lm = LCK_CW;
+               /* if file is released, we can't deny write because we must
+                * restore (write) it to access it. */
+               else if ((open_flags & MDS_FMODE_EXEC) &&
+                        !((ma->ma_valid & MA_HSM) &&
+                          (ma->ma_hsm.mh_flags & HS_RELEASED)))
+                       lm = LCK_PR;
+               else
+                       lm = LCK_CR;
+
+               *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
+       }
+
+       if (S_ISREG(lu_object_attr(&obj->mot_obj.mo_lu))) {
+               if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
+                   md_should_create(open_flags))
+                       create_layout = true;
+               if (exp_connect_layout(info->mti_exp) && !create_layout &&
+                   ma->ma_need & MA_LOV)
+                       try_layout = true;
+       }
+
+       mdt_lock_handle_init(lhc);
+       mdt_lock_reg_init(lhc, lm);
+
+       /* one problem to return layout lock on open is that it may result
+        * in too many layout locks cached on the client side. */
+       if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
+               /* return lookup lock to validate inode at the client side,
+                * this is pretty important otherwise mdt will return layout
+                * lock for each open.
+                * However this is a double-edged sword because changing
+                * permission will revoke huge # of LOOKUP locks. */
+               *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
+               if (!mdt_object_lock_try(info, obj, lhc, *ibits,
+                                        MDT_CROSS_LOCK)) {
+                       *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
+                       if (*ibits != 0)
+                               rc = mdt_object_lock(info, obj, lhc, *ibits,
+                                               MDT_CROSS_LOCK);
+               }
+       } else if (*ibits != 0) {
+               rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
+       }
+
+       CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
+               ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
+               PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
+
+       /* will change layout, revoke layout locks by enqueuing EX lock. */
+       if (rc == 0 && create_layout) {
+               struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
+
+               CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
+                       ", open_flags = "LPO64"\n",
+                       PFID(mdt_object_fid(obj)), open_flags);
+
+               LASSERT(!try_layout);
+               mdt_lock_handle_init(ll);
+               mdt_lock_reg_init(ll, LCK_EX);
+               rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
+                                       MDT_LOCAL_LOCK);
+
+               OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
+       }
+
+       RETURN(rc);
+}
+
+static void mdt_object_open_unlock(struct mdt_thread_info *info,
+                                  struct mdt_object *obj,
+                                  struct mdt_lock_handle *lhc,
+                                  __u64 ibits, int rc)
+{
+       __u64 open_flags = info->mti_spec.sp_cr_flags;
+       struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
+
+       /* Release local layout lock - the layout lock put in MDT_LH_LAYOUT
+        * will never return to client side. */
+       if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
+               LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
+               mdt_object_unlock(info, obj, ll, 1);
+       }
+
+       /* Cross-ref case, the lock should be returned to the client */
+       if (ibits == 0 || rc == -EREMOTE)
+               return;
+
+       if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
+               /* for the open request, the lock will only return to client
+                * if open or layout lock is granted. */
+               rc = 1;
+       }
+
+       if (rc != 0) {
+               struct ldlm_reply       *ldlm_rep;
+
+               ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
+               mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
+               mdt_object_unlock(info, obj, lhc, 1);
+       }
+}
+
 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
                         struct mdt_lock_handle *lhc)
 {
@@ -1126,7 +1276,7 @@ int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
         struct mdt_object       *parent= NULL;
         struct mdt_object       *o;
         int                      rc;
-        ldlm_mode_t              lm;
+       __u64                    ibits = 0;
         ENTRY;
 
        if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
@@ -1148,34 +1298,26 @@ int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
         if (IS_ERR(o))
                 RETURN(rc = PTR_ERR(o));
 
-        rc = mdt_object_exists(o);
-        if (rc == 0) {
-                mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
-                                    DISP_LOOKUP_NEG));
-                GOTO(out, rc = -ENOENT);
-        } else if (rc < 0) {
-                CERROR("NFS remote open shouldn't happen.\n");
-                GOTO(out, rc);
-        }
-        mdt_set_disposition(info, rep, (DISP_IT_EXECD |
-                                       DISP_LOOKUP_EXECD));
+       if (mdt_object_remote(o)) {
+               CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
+                      mdt_obd_name(info->mti_mdt),
+                      PFID(rr->rr_fid2));
+               GOTO(out, rc = -EREMOTE);
+       } else if (!mdt_object_exists(o)) {
+               mdt_set_disposition(info, rep,
+                                   DISP_IT_EXECD |
+                                   DISP_LOOKUP_EXECD |
+                                   DISP_LOOKUP_NEG);
+               GOTO(out, rc = -ENOENT);
+       }
 
-        if (flags & FMODE_WRITE)
-                lm = LCK_CW;
-        else if (flags & MDS_FMODE_EXEC)
-                lm = LCK_PR;
-        else
-                lm = LCK_CR;
+       mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
 
-        mdt_lock_handle_init(lhc);
-        mdt_lock_reg_init(lhc, lm);
-        rc = mdt_object_lock(info, o, lhc,
-                             MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
-                             MDT_CROSS_LOCK);
+       rc = mdt_attr_get_complex(info, o, ma);
         if (rc)
                 GOTO(out, rc);
 
-       rc = mdt_attr_get_complex(info, o, ma);
+       rc = mdt_object_open_lock(info, o, lhc, &ibits);
         if (rc)
                 GOTO(out, rc);
 
@@ -1191,18 +1333,15 @@ int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
         }
 
         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
-
-        if (!(flags & MDS_OPEN_LOCK) || rc)
-                mdt_object_unlock(info, o, lhc, 1);
-
        if (!rc) {
                mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
                if (flags & MDS_OPEN_LOCK)
                        mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
        }
-
         GOTO(out, rc);
+
 out:
+       mdt_object_open_unlock(info, o, lhc, ibits, rc);
         mdt_object_put(env, o);
         if (parent != NULL)
                 mdt_object_put(env, parent);
@@ -1216,9 +1355,10 @@ int mdt_pin(struct mdt_thread_info* info)
 }
 
 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
-int mdt_cross_open(struct mdt_thread_info* info,
-                   const struct lu_fid *fid,
-                   struct ldlm_reply *rep, __u32 flags)
+static int mdt_cross_open(struct mdt_thread_info *info,
+                         const struct lu_fid *parent_fid,
+                         const struct lu_fid *fid,
+                         struct ldlm_reply *rep, __u32 flags)
 {
         struct md_attr    *ma = &info->mti_attr;
         struct mdt_object *o;
@@ -1229,35 +1369,46 @@ int mdt_cross_open(struct mdt_thread_info* info,
         if (IS_ERR(o))
                 RETURN(rc = PTR_ERR(o));
 
-        rc = mdt_object_exists(o);
-        if (rc > 0) {
-                /* Do permission check for cross-open. */
-                rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
-                                   NULL, flags | MDS_OPEN_CROSS);
-                if (rc)
-                        goto out;
-
-                mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
-               rc = mdt_attr_get_complex(info, o, ma);
-                if (rc == 0)
-                        rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
-        } else if (rc == 0) {
-                /*
-                 * Something is wrong here. lookup was positive but there is
-                 * no object!
-                 */
-                CERROR("Cross-ref object doesn't exist!\n");
+       if (mdt_object_remote(o)) {
+               /* Something is wrong here, the object is on another MDS! */
+               CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
+                      mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
+               LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
+                               &o->mot_obj.mo_lu,
+                               "Object isn't on this server! FLD error?\n");
                 rc = -EFAULT;
-        } else  {
-                /* Something is wrong here, the object is on another MDS! */
-                CERROR("The object isn't on this server! FLD error?\n");
-                LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
-                                &o->mot_obj.mo_lu,
-                                "Object isn't on this server! FLD error?\n");
+       } else {
+               if (mdt_object_exists(o)) {
+                       /* Do permission check for cross-open. */
+                       rc = mo_permission(info->mti_env, NULL,
+                                          mdt_object_child(o),
+                                          NULL, flags | MDS_OPEN_CROSS);
+                       if (rc)
+                               goto out;
+
+                       mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
+                       rc = mdt_attr_get_complex(info, o, ma);
+                       if (rc != 0)
+                               GOTO(out, rc);
 
-                rc = -EFAULT;
-        }
+                       /* Do not create lov object if the fid is opened
+                        * under OBF */
+                       if (S_ISREG(ma->ma_attr.la_mode) &&
+                           !(ma->ma_valid & MA_LOV) && (flags & FMODE_WRITE) &&
+                           fid_is_obf(parent_fid))
+                               GOTO(out, rc = -EPERM);
 
+                       rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
+               } else {
+                       /*
+                        * Something is wrong here. lookup was positive but
+                        * there is no object!
+                        */
+                       CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
+                             mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
+                       rc = -EFAULT;
+               }
+        }
 out:
         mdt_object_put(info->mti_env, o);
         RETURN(rc);
@@ -1275,6 +1426,7 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
         struct md_attr          *ma = &info->mti_attr;
         __u64                    create_flags = info->mti_spec.sp_cr_flags;
+       __u64                    ibits;
         struct mdt_reint_record *rr = &info->mti_rr;
         struct lu_name          *lname;
         int                      result, rc;
@@ -1315,8 +1467,15 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
                PFID(rr->rr_fid1), rr->rr_name,
                PFID(rr->rr_fid2), create_flags,
                ma->ma_attr.la_mode, msg_flags);
-
-       if (req_is_replay(req) ||
+       if (info->mti_cross_ref) {
+               /* This is cross-ref open */
+               mdt_set_disposition(info, ldlm_rep,
+                           (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
+                            DISP_LOOKUP_POS));
+               result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
+                                       ldlm_rep, create_flags);
+               GOTO(out, result);
+       } else if (req_is_replay(req) ||
            (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
                /* This is a replay request or from liblustre with ea. */
                result = mdt_open_by_fid(info, ldlm_rep);
@@ -1335,14 +1494,24 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
                        GOTO(out, result = -EFAULT);
                }
                CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
-       } else if ((rr->rr_namelen == 0 && !info->mti_cross_ref &&
-                   create_flags & MDS_OPEN_LOCK) ||
+       } else if ((rr->rr_namelen == 0 && create_flags & MDS_OPEN_LOCK) ||
                   (create_flags & MDS_OPEN_BY_FID)) {
                result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
-               if (result != -ENOENT && !(create_flags & MDS_OPEN_CREAT))
+               /* If result is 0 then open by FID has found the file
+                * and there is nothing left for us to do here.  More
+                * generally if it is anything other than -ENOENT or
+                * -EREMOTE then we return that now.  If -ENOENT and
+                * MDS_OPEN_CREAT is set then we must create the file
+                * below.  If -EREMOTE then we need to return a LOOKUP
+                * lock to the client, which we do below.  Hence this
+                * odd looking condition.  See LU-2523. */
+               if (!(result == -ENOENT && (create_flags & MDS_OPEN_CREAT)) &&
+                   result != -EREMOTE)
                        GOTO(out, result);
+
                if (unlikely(rr->rr_namelen == 0))
                        GOTO(out, result = -EINVAL);
+
                CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
        }
 
@@ -1352,14 +1521,6 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
         mdt_set_disposition(info, ldlm_rep,
                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
 
-        if (info->mti_cross_ref) {
-                /* This is cross-ref open */
-                mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
-                result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
-                                        create_flags);
-                GOTO(out, result);
-        }
-
         lh = &info->mti_lh[MDT_LH_PARENT];
         mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
                           LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
@@ -1423,8 +1584,9 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
 
         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
         if (result == -ENOENT) {
-                if (mdt_object_obf(parent))
-                        GOTO(out_child, result = -EPERM);
+               /* Create under OBF and .lustre is not permitted */
+               if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
+                       GOTO(out_child, result = -EPERM);
 
                 /* save versions in reply */
                 mdt_version_get_save(info, parent, 0);
@@ -1467,12 +1629,10 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
                 }
                 created = 1;
         } else {
-                /* We have to get attr & lov ea for this object */
-               result = mdt_attr_get_complex(info, child, ma);
                 /*
                  * The object is on remote node, return its FID for remote open.
                  */
-                if (result == -EREMOTE) {
+               if (mdt_object_remote(child)) {
                         /*
                          * Check if this lock already was sent to client and
                          * this is resent case. For resent case do not take lock
@@ -1507,70 +1667,66 @@ int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
                         repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
                         if (rc != 0)
                                 result = rc;
+                       else
+                               result = -EREMOTE;
                         GOTO(out_child, result);
-                }
+               } else {
+                       if (mdt_object_exists(child)) {
+                               /* We have to get attr & LOV EA & HSM for this
+                                * object */
+                               ma->ma_need |= MA_HSM;
+                               result = mdt_attr_get_complex(info, child, ma);
+                       } else {
+                               /*object non-exist!!!*/
+                               LBUG();
+                       }
+               }
         }
 
         LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
 
-        /* get openlock if this is not replay and if a client requested it */
-        if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
-                ldlm_mode_t lm;
-
-                if (create_flags & FMODE_WRITE)
-                        lm = LCK_CW;
-                else if (create_flags & MDS_FMODE_EXEC)
-                        lm = LCK_PR;
-                else
-                        lm = LCK_CR;
-                mdt_lock_handle_init(lhc);
-                mdt_lock_reg_init(lhc, lm);
-                rc = mdt_object_lock(info, child, lhc,
-                                     MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
-                                     MDT_CROSS_LOCK);
-                if (rc) {
-                        result = rc;
-                        GOTO(out_child, result);
-                } else {
-                        result = -EREMOTE;
-                        mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
-                }
-        }
+       /* get openlock if this is not replay and if a client requested it */
+       if (!req_is_replay(req)) {
+               rc = mdt_object_open_lock(info, child, lhc, &ibits);
+               if (rc != 0)
+                       GOTO(out_child, result = rc);
+               else if (create_flags & MDS_OPEN_LOCK)
+                       mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
+       }
 
-        /* Try to open it now. */
-        rc = mdt_finish_open(info, parent, child, create_flags,
-                             created, ldlm_rep);
-        if (rc) {
-                result = rc;
-               if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
-                        /* openlock was acquired and mdt_finish_open failed -
-                           drop the openlock */
-                        mdt_object_unlock(info, child, lhc, 1);
-                       mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
-               }
-                if (created) {
-                        ma->ma_need = 0;
-                        ma->ma_valid = 0;
-                        ma->ma_cookie_size = 0;
-                        rc = mdo_unlink(info->mti_env,
-                                        mdt_object_child(parent),
-                                        mdt_object_child(child),
-                                        lname,
-                                        &info->mti_attr);
-                        if (rc != 0)
-                                CERROR("Error in cleanup of open\n");
+       /* Try to open it now. */
+       rc = mdt_finish_open(info, parent, child, create_flags,
+                            created, ldlm_rep);
+       if (rc) {
+               result = rc;
+               /* openlock will be released if mdt_finish_open failed */
+               mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
+               if (created) {
+                       ma->ma_need = 0;
+                       ma->ma_valid = 0;
+                       ma->ma_cookie_size = 0;
+                       rc = mdo_unlink(info->mti_env,
+                                       mdt_object_child(parent),
+                                       mdt_object_child(child),
+                                       lname,
+                                       &info->mti_attr, 0);
+                       if (rc != 0)
+                               CERROR("%s: "DFID" cleanup of open: rc = %d\n",
+                                      mdt_obd_name(info->mti_mdt),
+                                      PFID(mdt_object_fid(child)), rc);
                        mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
-                }
-        }
+               }
+       }
         EXIT;
 out_child:
+       mdt_object_open_unlock(info, child, lhc, ibits, result);
         mdt_object_put(info->mti_env, child);
 out_parent:
         mdt_object_unlock_put(info, parent, lh, result || !created);
 out:
-        if (result && result != -EREMOTE)
-                lustre_msg_set_transno(req->rq_repmsg, 0);
-        return result;
+       if (result)
+               lustre_msg_set_transno(req->rq_repmsg, 0);
+       return result;
 }
 
 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
@@ -1613,6 +1769,24 @@ int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
                 rc = mo_attr_set(info->mti_env, next, ma);
         }
 
+       /* If file data is modified, add the dirty flag.
+        *
+        * If MDS_CLOSE_CLEANUP is set, this file is being closed due to an
+        * eviction, file could have been modified and now dirty
+        * regarding to HSM archive, check this!
+        * The logic here is to mark a file dirty if there's a chance it was
+        * dirtied before the client was evicted, so that we don't have to wait
+        * for a release attempt before finding out the file was actually dirty
+        * and fail the release. Aggressively marking it dirty here will cause
+        * the policy engine to attempt to re-archive it; when rearchiving, we
+        * can compare the current version to the LMA data_version and make the
+        * archive request into a noop if it's not actually dirty.
+        */
+       if ((ma->ma_attr_flags & MDS_DATA_MODIFIED) ||
+           ((ma->ma_attr_flags & MDS_CLOSE_CLEANUP) &&
+            (mode & (FMODE_WRITE|MDS_FMODE_TRUNC))))
+               rc = mdt_add_dirty_flag(info, o, ma);
+
         ma->ma_need |= MA_INODE;
         ma->ma_valid &= ~MA_INODE;