+/* Update object linkEA */
+struct mdt_lock_list {
+ struct mdt_object *mll_obj;
+ struct mdt_lock_handle mll_lh;
+ struct list_head mll_list;
+};
+
+static void mdt_unlock_list(struct mdt_thread_info *info,
+ struct list_head *list, int rc)
+{
+ struct mdt_lock_list *mll;
+ struct mdt_lock_list *mll2;
+
+ list_for_each_entry_safe(mll, mll2, list, mll_list) {
+ mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
+ list_del(&mll->mll_list);
+ OBD_FREE_PTR(mll);
+ }
+}
+
+static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
+ struct mdt_object *obj,
+ struct mdt_object *pobj,
+ struct list_head *lock_list)
+{
+ struct lu_buf *buf = &info->mti_big_buf;
+ struct linkea_data ldata = { NULL };
+ int count;
+ int retry_count;
+ int rc;
+ ENTRY;
+
+ if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
+ RETURN(0);
+
+ buf = lu_buf_check_and_alloc(buf, PATH_MAX);
+ if (buf->lb_buf == NULL)
+ RETURN(-ENOMEM);
+
+ ldata.ld_buf = buf;
+ rc = mdt_links_read(info, obj, &ldata);
+ if (rc != 0) {
+ if (rc == -ENOENT || rc == -ENODATA)
+ rc = 0;
+ RETURN(rc);
+ }
+
+ /* ignore the migrating parent(@pobj) */
+ retry_count = ldata.ld_leh->leh_reccount - 1;
+
+again:
+ LASSERT(ldata.ld_leh != NULL);
+ ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
+ for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
+ struct mdt_device *mdt = info->mti_mdt;
+ struct mdt_object *mdt_pobj;
+ struct mdt_lock_list *mll;
+ struct lu_name name;
+ struct lu_fid fid;
+ __u64 ibits;
+
+ linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
+ &name, &fid);
+ mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
+ if (IS_ERR(mdt_pobj)) {
+ CWARN("%s: cannot find obj "DFID": rc = %ld\n",
+ mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
+ goto next;
+ }
+
+ if (!mdt_object_exists(mdt_pobj)) {
+ CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
+ mdt_obd_name(mdt), PFID(&fid));
+ mdt_object_put(info->mti_env, mdt_pobj);
+ goto next;
+ }
+
+ /* Check if the object already exists in the list */
+ list_for_each_entry(mll, lock_list, mll_list) {
+ if (mll->mll_obj == mdt_pobj) {
+ mdt_object_put(info->mti_env, mdt_pobj);
+ goto next;
+ }
+ }
+
+ if (mdt_pobj == pobj) {
+ CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
+ mdt_obd_name(mdt), PFID(&fid));
+ mdt_object_put(info->mti_env, mdt_pobj);
+ goto next;
+ }
+
+ OBD_ALLOC_PTR(mll);
+ if (mll == NULL) {
+ mdt_object_put(info->mti_env, mdt_pobj);
+ GOTO(out, rc = -ENOMEM);
+ }
+
+ /* Since this needs to lock all of objects in linkea, to avoid
+ * deadlocks, because it does not follow parent-child order as
+ * other MDT operation, let's use try_lock here and if the lock
+ * cannot be gotten because of conflicting locks, then drop all
+ * current locks, send an AST to the client, and start again. */
+ mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
+ ibits = 0;
+ rc = mdt_object_lock_try(info, mdt_pobj, &mll->mll_lh, &ibits,
+ MDS_INODELOCK_UPDATE, true);
+ if (!(ibits & MDS_INODELOCK_UPDATE)) {
+ mdt_unlock_list(info, lock_list, rc);
+
+ CDEBUG(D_INFO, "%s: busy lock on "DFID" %s retry %d\n",
+ mdt_obd_name(mdt), PFID(&fid), name.ln_name,
+ retry_count);
+
+ if (retry_count == 0) {
+ mdt_object_put(info->mti_env, mdt_pobj);
+ OBD_FREE_PTR(mll);
+ GOTO(out, rc = -EBUSY);
+ }
+
+ rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
+ MDS_INODELOCK_UPDATE);
+ if (rc != 0) {
+ mdt_object_put(info->mti_env, mdt_pobj);
+ OBD_FREE_PTR(mll);
+ GOTO(out, rc);
+ }
+
+ if (mdt_object_remote(mdt_pobj)) {
+ struct ldlm_lock *lock;
+
+ /* For remote object, Set lock to cb_atomic,
+ * so lock can be released in blocking_ast()
+ * immediately, then the next try_lock will
+ * have better chance to succeds */
+ lock =
+ ldlm_handle2lock(&mll->mll_lh.mlh_rreg_lh);
+ LASSERT(lock != NULL);
+ lock_res_and_lock(lock);
+ ldlm_set_atomic_cb(lock);
+ unlock_res_and_lock(lock);
+ LDLM_LOCK_PUT(lock);
+ }
+ mdt_object_unlock_put(info, mdt_pobj, &mll->mll_lh, rc);
+ OBD_FREE_PTR(mll);
+ retry_count--;
+ goto again;
+ }
+ rc = 0;
+ INIT_LIST_HEAD(&mll->mll_list);
+ mll->mll_obj = mdt_pobj;
+ list_add_tail(&mll->mll_list, lock_list);
+next:
+ ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
+ ldata.ld_reclen);
+ }
+out:
+ if (rc != 0)
+ mdt_unlock_list(info, lock_list, rc);
+ RETURN(rc);
+}
+
+/* migrate files from one MDT to another MDT */
+static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
+ struct mdt_lock_handle *lhc)
+{
+ struct mdt_reint_record *rr = &info->mti_rr;
+ struct md_attr *ma = &info->mti_attr;
+ struct mdt_object *msrcdir;
+ struct mdt_object *mold;
+ struct mdt_object *mnew = NULL;
+ struct mdt_lock_handle *lh_dirp;
+ struct mdt_lock_handle *lh_childp;
+ struct mdt_lock_handle *lh_tgtp = NULL;
+ struct lu_fid *old_fid = &info->mti_tmp_fid1;
+ struct list_head lock_list;
+ __u64 lock_ibits;
+ struct ldlm_lock *lease = NULL;
+ bool lock_open_sem = false;
+ int rc;
+ ENTRY;
+
+ CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
+ PNAME(&rr->rr_name), PFID(rr->rr_fid2));
+
+ /* 1: lock the source dir. */
+ msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
+ if (IS_ERR(msrcdir)) {
+ CDEBUG(D_OTHER, "%s: cannot find source dir "DFID" : rc = %d\n",
+ mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
+ (int)PTR_ERR(msrcdir));
+ RETURN(PTR_ERR(msrcdir));
+ }
+
+ lh_dirp = &info->mti_lh[MDT_LH_PARENT];
+ mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
+ rc = mdt_reint_object_lock(info, msrcdir, lh_dirp, MDS_INODELOCK_UPDATE,
+ true);
+ if (rc)
+ GOTO(out_put_parent, rc);
+
+ if (!mdt_object_remote(msrcdir)) {
+ rc = mdt_version_get_check_save(info, msrcdir, 0);
+ if (rc)
+ GOTO(out_unlock_parent, rc);
+ }
+
+ /* 2: sanity check and find the object to be migrated. */
+ fid_zero(old_fid);
+ rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
+ if (rc != 0)
+ GOTO(out_unlock_parent, rc);
+
+ if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
+ GOTO(out_unlock_parent, rc = -EINVAL);
+
+ if (!fid_is_md_operative(old_fid))
+ GOTO(out_unlock_parent, rc = -EPERM);
+
+ if (lu_fid_eq(old_fid, &info->mti_mdt->mdt_md_root_fid))
+ GOTO(out_unlock_parent, rc = -EPERM);
+
+ mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
+ if (IS_ERR(mold))
+ GOTO(out_unlock_parent, rc = PTR_ERR(mold));
+
+ if (mdt_object_remote(mold)) {
+ CDEBUG(D_OTHER, "%s: source "DFID" is on the remote MDT\n",
+ mdt_obd_name(info->mti_mdt), PFID(old_fid));
+ GOTO(out_put_child, rc = -EREMOTE);
+ }
+
+ if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
+ !mdt_object_remote(msrcdir)) {
+ CDEBUG(D_OTHER, "%s: parent "DFID" is still on the same"
+ " MDT, which should be migrated first:"
+ " rc = %d\n", mdt_obd_name(info->mti_mdt),
+ PFID(mdt_object_fid(msrcdir)), -EPERM);
+ GOTO(out_put_child, rc = -EPERM);
+ }
+
+ rc = mdt_remote_permission(info);
+ if (rc != 0)
+ GOTO(out_put_child, rc);
+
+ /* 3: iterate the linkea of the object and lock all of the objects */
+ INIT_LIST_HEAD(&lock_list);
+ rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
+ if (rc != 0)
+ GOTO(out_put_child, rc);
+
+ if (info->mti_spec.sp_migrate_close) {
+ struct close_data *data;
+ struct mdt_body *repbody;
+ bool lease_broken = false;
+
+ if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
+ RCL_CLIENT) ||
+ !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
+ RCL_CLIENT))
+ GOTO(out_lease, rc = -EPROTO);
+
+ data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
+ if (data == NULL)
+ GOTO(out_lease, rc = -EPROTO);
+
+ lease = ldlm_handle2lock(&data->cd_handle);
+ if (lease == NULL)
+ GOTO(out_lease, rc = -ESTALE);
+
+ /* try to hold open_sem so that nobody else can open the file */
+ if (!down_write_trylock(&mold->mot_open_sem)) {
+ ldlm_lock_cancel(lease);
+ GOTO(out_lease, rc = -EBUSY);
+ }
+
+ lock_open_sem = true;
+ /* Check if the lease open lease has already canceled */
+ lock_res_and_lock(lease);
+ lease_broken = ldlm_is_cancel(lease);
+ unlock_res_and_lock(lease);
+
+ LDLM_DEBUG(lease, DFID " lease broken? %d",
+ PFID(mdt_object_fid(mold)), lease_broken);
+
+ /* Cancel server side lease. Client side counterpart should
+ * have been cancelled. It's okay to cancel it now as we've
+ * held mot_open_sem. */
+ ldlm_lock_cancel(lease);
+
+ if (lease_broken)
+ GOTO(out_lease, rc = -EAGAIN);
+out_lease:
+ rc = mdt_close_internal(info, mdt_info_req(info), NULL);
+ repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
+ repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
+ if (rc != 0)
+ GOTO(out_unlock_list, rc);
+ }
+
+ /* 4: lock of the object migrated object */
+ lh_childp = &info->mti_lh[MDT_LH_OLD];
+ mdt_lock_reg_init(lh_childp, LCK_EX);
+ lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
+ MDS_INODELOCK_LAYOUT;
+ if (mdt_object_remote(msrcdir)) {
+ /* Enqueue lookup lock from the parent MDT */
+ rc = mdt_remote_object_lock(info, msrcdir, mdt_object_fid(mold),
+ &lh_childp->mlh_rreg_lh,
+ lh_childp->mlh_rreg_mode,
+ MDS_INODELOCK_LOOKUP, false);
+ if (rc != ELDLM_OK)
+ GOTO(out_unlock_list, rc);
+
+ lock_ibits &= ~MDS_INODELOCK_LOOKUP;
+ }
+
+ rc = mdt_reint_object_lock(info, mold, lh_childp, lock_ibits, true);
+ if (rc != 0)
+ GOTO(out_unlock_child, rc);
+
+ /* Migration is incompatible with HSM. */
+ ma->ma_need = MA_HSM;
+ ma->ma_valid = 0;
+ rc = mdt_attr_get_complex(info, mold, ma);
+ if (rc != 0)
+ GOTO(out_unlock_child, rc);
+
+ if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0) {
+ rc = -ENOSYS;
+ CDEBUG(D_OTHER,
+ "%s: cannot migrate HSM archived file "DFID": rc = %d\n",
+ mdt_obd_name(info->mti_mdt), PFID(old_fid), rc);
+ GOTO(out_unlock_child, rc);
+ }
+
+ ma->ma_need = MA_LMV;
+ ma->ma_valid = 0;
+ ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
+ ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
+ rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
+ if (rc != 0)
+ GOTO(out_unlock_child, rc);
+
+ if ((ma->ma_valid & MA_LMV)) {
+ struct lmv_mds_md_v1 *lmm1;
+
+ lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
+ lmm1 = &ma->ma_lmv->lmv_md_v1;
+ if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
+ CDEBUG(D_OTHER, "%s: can not migrate striped dir "DFID
+ ": rc = %d\n", mdt_obd_name(info->mti_mdt),
+ PFID(mdt_object_fid(mold)), -EPERM);
+ GOTO(out_unlock_child, rc = -EPERM);
+ }
+
+ if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
+ GOTO(out_unlock_child, rc = -EINVAL);
+
+ mnew = mdt_object_find(info->mti_env, info->mti_mdt,
+ &lmm1->lmv_stripe_fids[1]);
+ if (IS_ERR(mnew))
+ GOTO(out_unlock_child, rc = PTR_ERR(mnew));
+
+ if (!mdt_object_remote(mnew)) {
+ CDEBUG(D_OTHER,
+ "%s: "DFID" being migrated is on this MDT:"
+ " rc = %d\n", mdt_obd_name(info->mti_mdt),
+ PFID(rr->rr_fid2), -EPERM);
+ GOTO(out_put_new, rc = -EPERM);
+ }
+
+ lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
+ mdt_lock_reg_init(lh_tgtp, LCK_EX);
+ rc = mdt_remote_object_lock(info, mnew,
+ mdt_object_fid(mnew),
+ &lh_tgtp->mlh_rreg_lh,
+ lh_tgtp->mlh_rreg_mode,
+ MDS_INODELOCK_UPDATE, false);
+ if (rc != 0) {
+ lh_tgtp = NULL;
+ GOTO(out_put_new, rc);
+ }
+ } else {
+ mnew = mdt_object_find(info->mti_env, info->mti_mdt,
+ rr->rr_fid2);
+ if (IS_ERR(mnew))
+ GOTO(out_unlock_child, rc = PTR_ERR(mnew));
+ if (!mdt_object_remote(mnew)) {
+ CDEBUG(D_OTHER, "%s: Migration "DFID" is on this MDT:"
+ " rc = %d\n", mdt_obd_name(info->mti_mdt),
+ PFID(rr->rr_fid2), -EXDEV);
+ GOTO(out_put_new, rc = -EXDEV);
+ }
+ }
+
+ /* 5: migrate it */
+ mdt_reint_init_ma(info, ma);
+
+ mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
+ OBD_FAIL_MDS_REINT_RENAME_WRITE);
+
+ rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
+ mdt_object_child(mold), &rr->rr_name,
+ mdt_object_child(mnew), ma);
+ if (rc != 0)
+ GOTO(out_unlock_new, rc);
+
+out_unlock_new:
+ if (lh_tgtp != NULL)
+ mdt_object_unlock(info, mnew, lh_tgtp, rc);
+out_put_new:
+ if (mnew)
+ mdt_object_put(info->mti_env, mnew);
+out_unlock_child:
+ mdt_object_unlock(info, mold, lh_childp, rc);
+out_unlock_list:
+ /* we don't really modify linkea objects, so we can safely decref these
+ * locks, and this can avoid saving them as COS locks, which may prevent
+ * subsequent migrate. */
+ mdt_unlock_list(info, &lock_list, 1);
+ if (lease != NULL) {
+ ldlm_reprocess_all(lease->l_resource);
+ LDLM_LOCK_PUT(lease);
+ }
+
+ if (lock_open_sem)
+ up_write(&mold->mot_open_sem);
+out_put_child:
+ mdt_object_put(info->mti_env, mold);
+out_unlock_parent:
+ mdt_object_unlock(info, msrcdir, lh_dirp, rc);
+out_put_parent:
+ mdt_object_put(info->mti_env, msrcdir);
+
+ RETURN(rc);
+}
+
+static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
+ const struct lu_fid *fid,
+ int idx)
+{
+ struct mdt_object *dir;
+ int rc;
+ ENTRY;
+
+ dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
+ if (IS_ERR(dir))
+ RETURN(dir);
+
+ /* check early, the real version will be saved after locking */
+ rc = mdt_version_get_check(info, dir, idx);
+ if (rc)
+ GOTO(out_put, rc);
+
+ RETURN(dir);
+out_put:
+ mdt_object_put(info->mti_env, dir);
+ return ERR_PTR(rc);
+}
+
+static int mdt_object_lock_save(struct mdt_thread_info *info,
+ struct mdt_object *dir,
+ struct mdt_lock_handle *lh,
+ int idx, bool cos_incompat)
+{
+ int rc;
+
+ /* we lock the target dir if it is local */
+ rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
+ cos_incompat);
+ if (rc != 0)
+ return rc;
+
+ /* get and save correct version after locking */
+ mdt_version_get_save(info, dir, idx);
+ return 0;
+}
+