Whamcloud - gitweb
EX-3687 osp: do force disconnect if import is not ready
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
index 6455708..7f54b59 100644 (file)
@@ -63,6 +63,7 @@
 #include <lustre_barrier.h>
 #include <obd_cksum.h>
 #include <llog_swab.h>
+#include <lustre_crypto.h>
 
 #include "mdt_internal.h"
 
@@ -270,6 +271,41 @@ static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o
         EXIT;
 }
 
+/**
+ * Check whether \a o is directory stripe object.
+ *
+ * \param[in]  info    thread environment
+ * \param[in]  o       MDT object
+ *
+ * \retval 1   is directory stripe.
+ * \retval 0   isn't directory stripe.
+ * \retval < 1  error code
+ */
+static int mdt_is_dir_stripe(struct mdt_thread_info *info,
+                               struct mdt_object *o)
+{
+       struct md_attr *ma = &info->mti_attr;
+       struct lmv_mds_md_v1 *lmv;
+       int rc;
+
+       rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
+       if (rc < 0)
+               return rc;
+
+       if (!(ma->ma_valid & MA_LMV))
+               return 0;
+
+       lmv = &ma->ma_lmv->lmv_md_v1;
+
+       if (!lmv_is_sane2(lmv))
+               return -EBADF;
+
+       if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE)
+               return 1;
+
+       return 0;
+}
+
 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
                              struct lu_fid *fid)
 {
@@ -1808,29 +1844,152 @@ out:
 
 static int mdt_raw_lookup(struct mdt_thread_info *info,
                          struct mdt_object *parent,
-                         const struct lu_name *lname,
-                         struct ldlm_reply *ldlm_rep)
+                         const struct lu_name *lname)
 {
-       struct lu_fid   *child_fid = &info->mti_tmp_fid1;
-       int              rc;
+       struct lu_fid *fid = &info->mti_tmp_fid1;
+       struct mdt_body *repbody;
+       bool is_dotdot = false;
+       bool is_old_parent_stripe = false;
+       bool is_new_parent_checked = false;
+       int rc;
+
        ENTRY;
 
        LASSERT(!info->mti_cross_ref);
+       /* Always allow to lookup ".." */
+       if (lname->ln_namelen == 2 &&
+           lname->ln_name[0] == '.' && lname->ln_name[1] == '.') {
+               info->mti_spec.sp_permitted = 1;
+               is_dotdot = true;
+               if (mdt_is_dir_stripe(info, parent) == 1)
+                       is_old_parent_stripe = true;
+       }
 
+       mdt_object_get(info->mti_env, parent);
+lookup:
        /* Only got the fid of this obj by name */
-       fid_zero(child_fid);
-       rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
-                       lname, child_fid, &info->mti_spec);
-       if (rc == 0) {
-               struct mdt_body *repbody;
+       fid_zero(fid);
+       rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname, fid,
+                       &info->mti_spec);
+       mdt_object_put(info->mti_env, parent);
+       if (rc)
+               RETURN(rc);
+
+       /* getattr_name("..") should return master object FID for striped dir */
+       if (is_dotdot && (is_old_parent_stripe || !is_new_parent_checked)) {
+               parent = mdt_object_find(info->mti_env, info->mti_mdt, fid);
+               if (IS_ERR(parent))
+                       RETURN(PTR_ERR(parent));
+
+               /* old client getattr_name("..") with stripe FID */
+               if (unlikely(is_old_parent_stripe)) {
+                       is_old_parent_stripe = false;
+                       goto lookup;
+               }
+
+               /* ".." may be a stripe */
+               if (unlikely(mdt_is_dir_stripe(info, parent) == 1)) {
+                       is_new_parent_checked = true;
+                       goto lookup;
+               }
+
+               mdt_object_put(info->mti_env, parent);
+       }
+
+       repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+       repbody->mbo_fid1 = *fid;
+       repbody->mbo_valid = OBD_MD_FLID;
+
+       RETURN(rc);
+}
+
+/**
+ * Find name matching hash
+ *
+ * We search \a child LinkEA for a name whose hash matches \a lname
+ * (it contains an encoded hash).
+ *
+ * \param info mdt thread info
+ * \param lname encoded hash to find
+ * \param parent parent object
+ * \param child object to search with LinkEA
+ * \param force_check true to check hash even if LinkEA has only one entry
+ *
+ * \retval 1 match found
+ * \retval 0 no match found
+ * \retval -ev negative errno upon error
+ */
+int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
+                           struct mdt_object *parent, struct mdt_object *child,
+                           bool force_check)
+{
+       /* Here, lname is an encoded hash of on-disk name, and
+        * client is doing access without encryption key.
+        * So we need to get LinkEA, check parent fid is correct and
+        * compare name hash with the one in the request.
+        */
+       struct lu_buf *buf = &info->mti_big_buf;
+       struct lu_name name;
+       struct lu_fid pfid;
+       struct linkea_data ldata = { NULL };
+       struct link_ea_header *leh;
+       struct link_ea_entry *lee;
+       struct lu_buf link = { 0 };
+       char *hash = NULL;
+       int reclen, count, rc;
+
+       ENTRY;
+
+       if (lname->ln_namelen < LLCRYPT_FNAME_DIGEST_SIZE)
+               RETURN(-EINVAL);
+
+       buf = lu_buf_check_and_alloc(buf, PATH_MAX);
+       if (!buf->lb_buf)
+               RETURN(-ENOMEM);
+
+       ldata.ld_buf = buf;
+       rc = mdt_links_read(info, child, &ldata);
+       if (rc < 0)
+               RETURN(rc);
 
-               repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
-               repbody->mbo_fid1 = *child_fid;
-               repbody->mbo_valid = OBD_MD_FLID;
-               mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
-       } else if (rc == -ENOENT) {
-               mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
+       leh = buf->lb_buf;
+       if (force_check || leh->leh_reccount > 1) {
+               hash = kmalloc(lname->ln_namelen, GFP_NOFS);
+               if (!hash)
+                       RETURN(-ENOMEM);
+               rc = critical_decode(lname->ln_name, lname->ln_namelen, hash);
+       }
+       lee = (struct link_ea_entry *)(leh + 1);
+       for (count = 0; count < leh->leh_reccount; count++) {
+               linkea_entry_unpack(lee, &reclen, &name, &pfid);
+               if (!force_check && leh->leh_reccount == 1) {
+                       /* if there is only one rec, it has to be it */
+                       *lname = name;
+                       break;
+               }
+               if (!parent || lu_fid_eq(&pfid, mdt_object_fid(parent))) {
+                       lu_buf_check_and_alloc(&link, name.ln_namelen);
+                       if (!link.lb_buf)
+                               GOTO(out_match, rc = -ENOMEM);
+                       rc = critical_decode(name.ln_name, name.ln_namelen,
+                                            link.lb_buf);
+
+                       if (memcmp(LLCRYPT_FNAME_DIGEST(link.lb_buf, rc),
+                                  hash, LLCRYPT_FNAME_DIGEST_SIZE) == 0) {
+                               *lname = name;
+                               break;
+                       }
+               }
+               lee = (struct link_ea_entry *) ((char *)lee + reclen);
        }
+       if (count == leh->leh_reccount)
+               rc = 0;
+       else
+               rc = 1;
+
+out_match:
+       lu_buf_free(&link);
+       kfree(hash);
 
        RETURN(rc);
 }
@@ -1931,7 +2090,30 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
        lname = &info->mti_name;
        mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
 
-       if (lu_name_is_valid(lname)) {
+       if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
+               reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
+               if (unlikely(reqbody == NULL))
+                       RETURN(err_serious(-EPROTO));
+
+               *child_fid = reqbody->mbo_fid2;
+               if (unlikely(!fid_is_sane(child_fid)))
+                       RETURN(err_serious(-EINVAL));
+
+               if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
+                       mdt_object_get(info->mti_env, parent);
+                       child = parent;
+               } else {
+                       child = mdt_object_find(info->mti_env, info->mti_mdt,
+                                               child_fid);
+                       if (IS_ERR(child))
+                               RETURN(PTR_ERR(child));
+               }
+
+               CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
+                      "ldlm_rep = %p\n",
+                      PFID(mdt_object_fid(parent)),
+                      PFID(&reqbody->mbo_fid2), ldlm_rep);
+       } else if (lu_name_is_valid(lname)) {
                if (mdt_object_remote(parent)) {
                        CERROR("%s: parent "DFID" is on remote target\n",
                               mdt_obd_name(info->mti_mdt),
@@ -1983,22 +2165,19 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
 
        mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
 
-       if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
+       if (unlikely(!mdt_object_exists(parent)) &&
+           !(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
+           lu_name_is_valid(lname)) {
                LU_OBJECT_DEBUG(D_INODE, info->mti_env,
                                &parent->mot_obj,
                                "Parent doesn't exist!");
                GOTO(out_child, rc = -ESTALE);
        }
 
-       if (lu_name_is_valid(lname)) {
-               /* Always allow to lookup ".." */
-               if (unlikely(lname->ln_namelen == 2 &&
-                            lname->ln_name[0] == '.' &&
-                            lname->ln_name[1] == '.'))
-                       info->mti_spec.sp_permitted = 1;
-
+       if (!(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
+           lu_name_is_valid(lname)) {
                if (info->mti_body->mbo_valid == OBD_MD_FLID) {
-                       rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
+                       rc = mdt_raw_lookup(info, parent, lname);
 
                        RETURN(rc);
                }
@@ -2034,6 +2213,19 @@ static int mdt_getattr_name_lock(struct mdt_thread_info *info,
        /* step 3: lock child regardless if it is local or remote. */
        LASSERT(child);
 
+       if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
+               /* Here, lname is an encoded hash of on-disk name, and
+                * client is doing access without encryption key.
+                * So we need to compare name hash with the one in the request.
+                */
+               if (!find_name_matching_hash(info, lname, parent,
+                                            child, true)) {
+                       mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
+                       mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
+                       GOTO(out_child, rc = -ENOENT);
+               }
+       }
+
        OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
        if (!mdt_object_exists(child)) {
                LU_OBJECT_DEBUG(D_INODE, info->mti_env,
@@ -2302,7 +2494,7 @@ static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
        if (la->la_flags & LUSTRE_IMMUTABLE_FL)
                        rc = -EACCES;
 
-       if (md_capable(uc, CAP_DAC_OVERRIDE))
+       if (cap_raised(uc->uc_cap, CAP_DAC_OVERRIDE))
                RETURN(0);
        if (uc->uc_fsuid == la->la_uid) {
                if ((la->la_mode & S_IWUSR) == 0)
@@ -5464,6 +5656,8 @@ TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
                                         OST_PUNCH,     mdt_punch_hdl,
                                                        mdt_hp_punch),
 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC,    mdt_data_sync),
+TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_FALLOCATE,
+                                                       mdt_fallocate_hdl),
 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
 };
 
@@ -6455,7 +6649,8 @@ static int mdt_export_cleanup(struct obd_export *exp)
                                rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
 
                        /* Don't unlink orphan on failover umount, LU-184 */
-                       if (exp->exp_flags & OBD_OPT_FAILOVER) {
+                       if (exp->exp_flags & OBD_OPT_FAILOVER ||
+                           exp->exp_obd->obd_stopping) {
                                ma->ma_valid = MA_FLAGS;
                                ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
                        }
@@ -6743,7 +6938,6 @@ static int mdt_path_current(struct mdt_thread_info *info,
        struct lu_name *tmpname = &info->mti_name;
        struct lu_fid *tmpfid = &info->mti_tmp_fid1;
        struct lu_buf *buf = &info->mti_big_buf;
-       struct md_attr *ma = &info->mti_attr;
        struct linkea_data ldata = { NULL };
        bool first = true;
        struct mdt_object *mdt_obj;
@@ -6816,22 +7010,13 @@ static int mdt_path_current(struct mdt_thread_info *info,
                }
 
                /* Check if it is slave stripes */
-               rc = mdt_stripe_get(info, mdt_obj, ma, XATTR_NAME_LMV);
+               rc = mdt_is_dir_stripe(info, mdt_obj);
                mdt_object_put(info->mti_env, mdt_obj);
                if (rc < 0)
                        GOTO(out, rc);
-
-               if (ma->ma_valid & MA_LMV) {
-                       struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
-
-                       if (!lmv_is_sane2(lmv))
-                               GOTO(out, rc = -EBADF);
-
-                       /* For slave stripes, get its master */
-                       if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE) {
-                               fp->gf_fid = *tmpfid;
-                               continue;
-                       }
+               if (rc == 1) {
+                       fp->gf_fid = *tmpfid;
+                       continue;
                }
 
                /* Pack the name in the end of the buffer */
@@ -6948,12 +7133,24 @@ static int mdt_fid2path(struct mdt_thread_info *info,
                RETURN(rc);
        }
 
-       if (mdt_object_remote(obj))
+       if (mdt_object_remote(obj)) {
                rc = -EREMOTE;
-       else if (!mdt_object_exists(obj))
+       } else if (!mdt_object_exists(obj)) {
                rc = -ENOENT;
-       else
-               rc = 0;
+       } else {
+               struct lu_attr la = { 0 };
+               struct dt_object *dt = mdt_obj2dt(obj);
+
+               if (dt && dt->do_ops && dt->do_ops->do_attr_get)
+                       dt_attr_get(info->mti_env, mdt_obj2dt(obj), &la);
+               if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL)
+                       /* path resolution cannot be carried out on server
+                        * side for encrypted files
+                        */
+                       rc = -ENODATA;
+               else
+                       rc = 0;
+       }
 
        if (rc < 0) {
                mdt_object_put(info->mti_env, obj);
@@ -7130,13 +7327,14 @@ static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                struct obd_ioctl_data *data = karg;
 
                CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
-               if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT)
+               if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT) {
                        obd->obd_abort_recov_mdt = 1;
-               else /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
+                       wake_up(&obd->obd_next_transno_waitq);
+               } else { /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
                        /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
                        obd->obd_abort_recovery = 1;
-
-               target_stop_recovery_thread(obd);
+                       target_stop_recovery_thread(obd);
+               }
                rc = 0;
                break;
        }