Whamcloud - gitweb
LU-7787 mdd: clean up orphan object handling 47/30547/8
authorAndreas Dilger <andreas.dilger@intel.com>
Thu, 14 Dec 2017 21:53:58 +0000 (14:53 -0700)
committerOleg Drokin <oleg.drokin@intel.com>
Sat, 3 Mar 2018 04:30:02 +0000 (04:30 +0000)
There was a potential problem in the orphan object naming because
it had an embedded space in the filename before the "operation",
which might cause issues if they are accessed for other reasons.
It turns out that there is no need for the "operation" to be
embedded into the filename, since it was always ORPH_OP_UNLINK.

Use standard DFID formatting for the orphan object names, which
is a bit shorter and more efficient on disk, without the embedded
operation type.

Remove the use of "ORPH_OP_UNLINK" in the code, except in the
compatibility code for handling orphans left over after upgrades
from older Lustre versions.  This can be removed at some point
in the future when there are no longer upgrades from pre-2.11
versions.

Rename the orphan handling functions to start with mdd_orphan_*
for consistency with other MDD functions:
orph_index_init -> mdd_orphan_index_init
orph_index_iterate -> mdd_orphan_index_iterate
orph_index_fini -> mdd_orphan_index_fini
orph_declare_index_insert -> mdd_orphan_declare_insert
orph_declare_index_insert -> mdd_orphan_declare_insert
orph_key_test_and_del -> mdd_orphan_key_test_and_delete
orph_key_fill -> mdd_orphan_key_fill
orph_key_fill_18 -> mdd_orphan_key_fill_20
__mdd_orphan_add -> mdd_orphan_insert
__mdd_orphan_del -> mdd_orphan_delete
__mdd_orphan_cleanup -> mdd_orphan_cleanup_thread

Remove single-line wrapper functions to clarify actual code:
mdd_orphan_write_lock -> dt_write_lock
mdd_orphan_write_unlock -> dt_write_unlock
mdd_orphan_delete_obj -> dt_delete
mdd_orphan_ref_add -> dt_ref_add
mdd_orphan_ref_del -> dt_ref_del

Signed-off-by: Andreas Dilger <andreas.dilger@intel.com>
Change-Id: Ica90cc03c3212103c39cba11c4566584bf9cab07
Reviewed-on: https://review.whamcloud.com/30547
Tested-by: Jenkins
Tested-by: Maloo <hpdd-maloo@intel.com>
Reviewed-by: Fan Yong <fan.yong@intel.com>
Reviewed-by: Lai Siyao <lai.siyao@intel.com>
Reviewed-by: Oleg Drokin <oleg.drokin@intel.com>
lustre/mdd/mdd_device.c
lustre/mdd/mdd_dir.c
lustre/mdd/mdd_internal.h
lustre/mdd/mdd_object.c
lustre/mdd/mdd_orphans.c

index 842bc38..a347c5a 100644 (file)
@@ -890,7 +890,7 @@ static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
        lfsck_degister(env, m->mdd_bottom);
        mdd_hsm_actions_llog_fini(env, m);
        mdd_changelog_fini(env, m);
-       orph_index_fini(env, m);
+       mdd_orphan_index_fini(env, m);
        mdd_dot_lustre_cleanup(env, m);
        if (mdd2obd_dev(m)->u.obt.obt_nodemap_config_file) {
                nm_config_file_deregister_tgt(env,
@@ -934,7 +934,7 @@ static int mdd_process_config(const struct lu_env *env,
                 break;
        case LCFG_PRE_CLEANUP:
                rc = next->ld_ops->ldo_process_config(env, next, cfg);
-               mdd_generic_thread_stop(&m->mdd_orph_cleanup_thread);
+               mdd_generic_thread_stop(&m->mdd_orphan_cleanup_thread);
                break;
        case LCFG_CLEANUP:
                rc = next->ld_ops->ldo_process_config(env, next, cfg);
@@ -950,22 +950,21 @@ out:
 }
 
 static int mdd_recovery_complete(const struct lu_env *env,
-                                 struct lu_device *d)
+                                struct lu_device *d)
 {
-        struct mdd_device *mdd = lu2mdd_dev(d);
+       struct mdd_device *mdd = lu2mdd_dev(d);
        struct lu_device *next;
-        int rc;
-        ENTRY;
+       int rc;
+       ENTRY;
 
-        LASSERT(mdd != NULL);
+       LASSERT(mdd != NULL);
        next = &mdd->mdd_child->dd_lu_dev;
 
-        /* XXX: orphans handling. */
        if (!mdd->mdd_bottom->dd_rdonly)
                mdd_orphan_cleanup(env, mdd);
-        rc = next->ld_ops->ldo_recovery_complete(env, next);
+       rc = next->ld_ops->ldo_recovery_complete(env, next);
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
@@ -1062,7 +1061,7 @@ static int mdd_prepare(const struct lu_env *env,
                mdd->mdd_root_fid = fid;
        }
 
-       rc = orph_index_init(env, mdd);
+       rc = mdd_orphan_index_init(env, mdd);
        if (rc < 0)
                GOTO(out_dot, rc);
 
@@ -1115,7 +1114,7 @@ out_hsm:
 out_changelog:
        mdd_changelog_fini(env, mdd);
 out_orph:
-       orph_index_fini(env, mdd);
+       mdd_orphan_index_fini(env, mdd);
 out_dot:
        if (mdd_seq_site(mdd)->ss_node_id == 0)
                mdd_dot_lustre_cleanup(env, mdd);
index a8554f7..74988ec 100644 (file)
@@ -1620,7 +1620,7 @@ static int mdd_declare_finish_unlink(const struct lu_env *env,
                                     struct mdd_object *obj,
                                     struct thandle *handle)
 {
-       int     rc;
+       int rc;
 
        /* Sigh, we do not know if the unlink object will become orphan in
         * declare phase, but fortunately the flags here does not matter
@@ -1633,7 +1633,7 @@ static int mdd_declare_finish_unlink(const struct lu_env *env,
        if (rc != 0)
                return rc;
 
-       rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
+       rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
        if (rc != 0)
                return rc;
 
@@ -1658,7 +1658,7 @@ int mdd_finish_unlink(const struct lu_env *env,
                 * will be deleted during mdd_close() */
                obj->mod_flags |= DEAD_OBJ;
                if (obj->mod_count) {
-                       rc = __mdd_orphan_add(env, obj, th);
+                       rc = mdd_orphan_insert(env, obj, th);
                        if (rc == 0)
                                CDEBUG(D_HA, "Object "DFID" is inserted into "
                                        "orphan list, open count = %d\n",
@@ -1672,7 +1672,7 @@ int mdd_finish_unlink(const struct lu_env *env,
                                        obj->mod_count);
 
                        /* mark object as an orphan here, not
-                        * before __mdd_orphan_add() as racing
+                        * before mdd_orphan_insert() as racing
                         * mdd_la_get() may propagate ORPHAN_OBJ
                         * causing the asserition */
                        rc = mdd_mark_orphan_object(env, obj, th, false);
@@ -2335,11 +2335,11 @@ static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
        }
 
        if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
-               rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
+               rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
                if (rc)
                        GOTO(out, rc);
        } else {
-               struct lu_attr  *la = &mdd_env_info(env)->mti_la_for_fix;
+               struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
 
                rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
                                              name->ln_name, handle);
@@ -2693,11 +2693,11 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
        if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
                mdd_write_lock(env, son, MOR_TGT_CHILD);
                son->mod_flags |= VOLATILE_OBJ;
-               rc = __mdd_orphan_add(env, son, handle);
+               rc = mdd_orphan_insert(env, son, handle);
                GOTO(out_volatile, rc);
        } else {
                rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
-                                       attr->la_mode, name, handle);
+                                     attr->la_mode, name, handle);
                if (rc != 0)
                        GOTO(err_created, rc);
 
@@ -2716,7 +2716,7 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
 err_insert:
        if (rc != 0) {
                if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
-                       rc2 = __mdd_orphan_del(env, son, handle);
+                       rc2 = mdd_orphan_delete(env, son, handle);
                else
                        rc2 = __mdd_index_delete(env, mdd_pobj, name,
                                                 S_ISDIR(attr->la_mode),
@@ -4278,7 +4278,7 @@ static int mdd_migrate_update_name(const struct lu_env *env,
        if (rc != 0)
                GOTO(out_unlock, rc);
 
-       rc = __mdd_orphan_add(env, mdd_sobj, handle);
+       rc = mdd_orphan_insert(env, mdd_sobj, handle);
        if (rc != 0)
                GOTO(out_unlock, rc);
 
index f438eb7..c7519e4 100644 (file)
@@ -143,7 +143,7 @@ struct mdd_device {
        unsigned int                     mdd_sync_permission;
        int                              mdd_connects;
        struct local_oid_storage        *mdd_los;
-       struct mdd_generic_thread        mdd_orph_cleanup_thread;
+       struct mdd_generic_thread        mdd_orphan_cleanup_thread;
 };
 
 enum mod_flags {
@@ -195,8 +195,6 @@ struct mdd_thread_info {
        struct lu_seq_range       mti_range;
 };
 
-extern const char orph_index_name[];
-
 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
               struct lu_attr *la);
 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
@@ -273,16 +271,16 @@ const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
                                        const void *area, ssize_t len);
 
 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
-int __mdd_orphan_add(const struct lu_env *, struct mdd_object *,
-                     struct thandle *);
-int __mdd_orphan_del(const struct lu_env *, struct mdd_object *,
-                     struct thandle *);
-int orph_index_init(const struct lu_env *env, struct mdd_device *mdd);
-void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd);
-int orph_declare_index_insert(const struct lu_env *, struct mdd_object *,
-                             umode_t mode, struct thandle *);
-int orph_declare_index_delete(const struct lu_env *, struct mdd_object *,
-                              struct thandle *);
+int mdd_orphan_insert(const struct lu_env *env, struct mdd_object *obj,
+                     struct thandle *thandle);
+int mdd_orphan_delete(const struct lu_env *env, struct mdd_object *obj,
+                     struct thandle *thandle);
+int mdd_orphan_index_init(const struct lu_env *env, struct mdd_device *mdd);
+void mdd_orphan_index_fini(const struct lu_env *env, struct mdd_device *mdd);
+int mdd_orphan_declare_insert(const struct lu_env *env, struct mdd_object *obj,
+                             umode_t mode, struct thandle *thandle);
+int mdd_orphan_declare_delete(const struct lu_env *env, struct mdd_object *obj,
+                             struct thandle *thandle);
 
 /* mdd_lproc.c */
 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
index d55cfd7..40760f0 100644 (file)
@@ -2957,16 +2957,14 @@ out:
        return rc;
 }
 
-static int mdd_declare_close(const struct lu_env *env,
-                             struct mdd_object *obj,
-                             struct md_attr *ma,
-                             struct thandle *handle)
+static int mdd_declare_close(const struct lu_env *env, struct mdd_object *obj,
+                            struct md_attr *ma, struct thandle *handle)
 {
-        int rc;
+       int rc;
 
-        rc = orph_declare_index_delete(env, obj, handle);
-        if (rc)
-                return rc;
+       rc = mdd_orphan_declare_delete(env, obj, handle);
+       if (rc)
+               return rc;
 
        return mdo_declare_destroy(env, obj, handle);
 }
@@ -3094,7 +3092,7 @@ cont:
        if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
                /* remove link to object from orphan index */
                LASSERT(handle != NULL);
-               rc = __mdd_orphan_del(env, mdd_obj, handle);
+               rc = mdd_orphan_delete(env, mdd_obj, handle);
                if (rc != 0) {
                        CERROR("%s: unable to delete "DFID" from orphan list: "
                               "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
index 1f5f819..4f6c32c 100644 (file)
 #include <lustre_fid.h>
 #include "mdd_internal.h"
 
-const char orph_index_name[] = "PENDING";
+static const char mdd_orphan_index_name[] = "PENDING";
 static const char dotdot[] = "..";
 
 enum {
-        ORPH_OP_UNLINK,
-        ORPH_OP_TRUNCATE
+       ORPH_OP_UNLINK,
 };
 
-#define ORPHAN_FILE_NAME_FORMAT         "%016llx:%08x:%08x:%2x"
-#define ORPHAN_FILE_NAME_FORMAT_18      "%llx:%08x"
+/* obsolete after 2.11, needed for upgrades from older 2.x versions */
+#define ORPHAN_FILE_NAME_FORMAT_20      "%016llx:%08x:%08x:%2x"
 
-static struct dt_key* orph_key_fill(const struct lu_env *env,
-                                    const struct lu_fid *lf, __u32 op)
+static struct dt_key *mdd_orphan_key_fill(const struct lu_env *env,
+                                         const struct lu_fid *lf)
 {
        char *key = mdd_env_info(env)->mti_key;
-        int rc;
-
-        LASSERT(key);
-        rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT,
-                      (long long unsigned int)fid_seq(lf),
-                      fid_oid(lf), fid_ver(lf), op);
-        if (rc > 0)
-                return (struct dt_key*) key;
-        else
-                return ERR_PTR(rc);
+
+       LASSERT(key);
+       snprintf(key, sizeof(mdd_env_info(env)->mti_key),
+                DFID_NOBRACE, PFID(lf));
+
+       return (struct dt_key *)key;
 }
 
-static struct dt_key* orph_key_fill_18(const struct lu_env *env,
-                                       const struct lu_fid *lf)
+/* compatibility with orphan files created in versions before 2.11 */
+static struct dt_key *mdd_orphan_key_fill_20(const struct lu_env *env,
+                                            const struct lu_fid *lf)
 {
        char *key = mdd_env_info(env)->mti_key;
-        int rc;
-
-        LASSERT(key);
-        rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_18,
-                      (unsigned long long)fid_seq(lf), fid_oid(lf));
-        if (rc > 0)
-                return (struct dt_key*) key;
-        else
-                return ERR_PTR(rc);
-}
 
-static inline void mdd_orphan_write_lock(const struct lu_env *env,
-                                        struct mdd_device *mdd)
-{
-       struct dt_object *dor = mdd->mdd_orphans;
-       dt_write_lock(env, dor, MOR_TGT_ORPHAN);
-}
+       LASSERT(key);
+       snprintf(key, sizeof(mdd_env_info(env)->mti_key),
+                ORPHAN_FILE_NAME_FORMAT_20,
+                fid_seq(lf), fid_oid(lf), fid_ver(lf), ORPH_OP_UNLINK);
 
-static inline void mdd_orphan_write_unlock(const struct lu_env *env,
-                                          struct mdd_device *mdd)
-{
-       struct dt_object *dor = mdd->mdd_orphans;
-       dt_write_unlock(env, dor);
+       return (struct dt_key *)key;
 }
 
 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
                                        struct mdd_device *mdd,
                                        struct mdd_object *obj,
-                                       __u32 op, struct thandle *th)
+                                       struct thandle *th)
 {
-       struct dt_insert_rec    *rec    = &mdd_env_info(env)->mti_dt_rec;
-       struct dt_object        *dor    = mdd->mdd_orphans;
-       const struct lu_fid     *lf     = mdo2fid(obj);
-       struct dt_key           *key    = orph_key_fill(env, lf, op);
+       struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
+       struct dt_object *dor = mdd->mdd_orphans;
+       const struct lu_fid *lf = mdo2fid(obj);
+       struct dt_key *key = mdd_orphan_key_fill(env, lf);
 
        rec->rec_fid = lf;
        rec->rec_type = mdd_object_type(obj);
@@ -117,43 +97,15 @@ static inline int mdd_orphan_insert_obj(const struct lu_env *env,
        return dt_insert(env, dor, (const struct dt_rec *)rec, key, th, 1);
 }
 
-static inline int mdd_orphan_delete_obj(const struct lu_env *env,
-                                        struct mdd_device  *mdd ,
-                                        struct dt_key *key,
-                                        struct thandle *th)
-{
-       struct dt_object *dor = mdd->mdd_orphans;
-
-       return dt_delete(env, dor, key, th);
-}
-
-static inline int mdd_orphan_ref_add(const struct lu_env *env,
-                                    struct mdd_device *mdd,
-                                    struct thandle *th)
-{
-       struct dt_object *dor = mdd->mdd_orphans;
-       return dt_ref_add(env, dor, th);
-}
-
-static inline int mdd_orphan_ref_del(const struct lu_env *env,
-                                    struct mdd_device *mdd,
-                                    struct thandle *th)
-{
-       struct dt_object *dor = mdd->mdd_orphans;
-       return dt_ref_del(env, dor, th);
-}
-
-
-int orph_declare_index_insert(const struct lu_env *env,
-                             struct mdd_object *obj,
+int mdd_orphan_declare_insert(const struct lu_env *env, struct mdd_object *obj,
                              umode_t mode, struct thandle *th)
 {
-       struct dt_insert_rec    *rec = &mdd_env_info(env)->mti_dt_rec;
-       struct mdd_device       *mdd = mdo2mdd(&obj->mod_obj);
-       struct dt_key           *key;
-       int                     rc;
+       struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
+       struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+       struct dt_key *key;
+       int rc;
 
-       key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
+       key = mdd_orphan_key_fill(env, mdo2fid(obj));
 
        rec->rec_fid = mdo2fid(obj);
        rec->rec_type = mode;
@@ -188,37 +140,45 @@ int orph_declare_index_insert(const struct lu_env *env,
        return rc;
 }
 
-static int orph_index_insert(const struct lu_env *env,
-                            struct mdd_object *obj,
-                            __u32 op, struct thandle *th)
+/**
+ *  add an orphan \a obj to the orphan index.
+ *  \param obj file or directory.
+ *  \param th  transaction for index insert.
+ *
+ *  \pre obj nlink == 0 && obj->mod_count != 0
+ *
+ *  \retval 0  success
+ *  \retval  -ve index operation error.
+ */
+int mdd_orphan_insert(const struct lu_env *env, struct mdd_object *obj,
+                     struct thandle *th)
 {
-       struct mdd_device       *mdd    = mdo2mdd(&obj->mod_obj);
-       struct dt_object        *dor    = mdd->mdd_orphans;
-       const struct lu_fid     *lf_dor = lu_object_fid(&dor->do_lu);
-       struct dt_object        *next   = mdd_object_child(obj);
-       struct dt_insert_rec    *rec    = &mdd_env_info(env)->mti_dt_rec;
-       int                      rc;
-        ENTRY;
+       struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+       struct dt_object *dor = mdd->mdd_orphans;
+       const struct lu_fid *lf_dor = lu_object_fid(&dor->do_lu);
+       struct dt_object *next = mdd_object_child(obj);
+       struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
+       int rc;
+       ENTRY;
 
-        LASSERT(mdd_write_locked(env, obj) != 0);
-        LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
+       LASSERT(mdd_write_locked(env, obj) != 0);
+       LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
 
-        mdd_orphan_write_lock(env, mdd);
+       dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
 
-        rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
-        if (rc)
-                GOTO(out, rc);
+       rc = mdd_orphan_insert_obj(env, mdd, obj, th);
+       if (rc)
+               GOTO(out, rc);
 
-        mdo_ref_add(env, obj, th);
-        if (!S_ISDIR(mdd_object_type(obj)))
+       mdo_ref_add(env, obj, th);
+       if (!S_ISDIR(mdd_object_type(obj)))
                GOTO(out, rc = 0);
 
-        mdo_ref_add(env, obj, th);
-        mdd_orphan_ref_add(env, mdd, th);
+       mdo_ref_add(env, obj, th);
+       dt_ref_add(env, mdd->mdd_orphans, th);
 
-        /* try best to fixup directory, dont return errors
-         * from here */
-        if (!dt_try_as_dir(env, next))
+       /* try best to fixup directory, do not return errors from here */
+       if (!dt_try_as_dir(env, next))
                GOTO(out, rc = 0);
 
        dt_delete(env, next, (const struct dt_key *)dotdot, th);
@@ -229,28 +189,26 @@ static int orph_index_insert(const struct lu_env *env,
                  (const struct dt_key *)dotdot, th, 1);
 
 out:
-        if (rc == 0)
-                obj->mod_flags |= ORPHAN_OBJ;
+       if (rc == 0)
+               obj->mod_flags |= ORPHAN_OBJ;
 
-        mdd_orphan_write_unlock(env, mdd);
+       dt_write_unlock(env, mdd->mdd_orphans);
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
-int orph_declare_index_delete(const struct lu_env *env,
-                              struct mdd_object *obj,
-
-                              struct thandle *th)
+int mdd_orphan_declare_delete(const struct lu_env *env, struct mdd_object *obj,
+                             struct thandle *th)
 {
-        struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
-       struct dt_key     *key;
-        int                rc;
+       struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+       struct dt_key *key;
+       int rc;
 
-       key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
+       key = mdd_orphan_key_fill(env, mdo2fid(obj));
 
        rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
-        if (rc)
-                return rc;
+       if (rc)
+               return rc;
 
         rc = mdo_declare_ref_del(env, obj, th);
         if (rc)
@@ -267,53 +225,61 @@ int orph_declare_index_delete(const struct lu_env *env,
         return rc;
 }
 
-static int orph_index_delete(const struct lu_env *env,
-                             struct mdd_object *obj,
-                             __u32 op,
-                             struct thandle *th)
+/**
+ *  delete an orphan \a obj from orphan index.
+ *  \param obj file or directory.
+ *  \param th  transaction for index deletion and object destruction.
+ *
+ *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
+ *
+ *  \retval 0  success
+ *  \retval  -ve index operation error.
+ */
+int mdd_orphan_delete(const struct lu_env *env, struct mdd_object *obj,
+                     struct thandle *th)
 {
-        struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
-        struct dt_object *dor = mdd->mdd_orphans;
-        struct dt_key *key;
-        int rc;
-
-        ENTRY;
+       struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+       struct dt_object *dor = mdd->mdd_orphans;
+       struct dt_key *key;
+       int rc;
 
-        LASSERT(mdd_write_locked(env, obj) != 0);
-        LASSERT(obj->mod_flags & ORPHAN_OBJ);
-        LASSERT(obj->mod_count == 0);
+       ENTRY;
 
-        LASSERT(dor);
+       LASSERT(mdd_write_locked(env, obj) != 0);
+       LASSERT(obj->mod_flags & ORPHAN_OBJ);
+       LASSERT(obj->mod_count == 0);
 
-        key = orph_key_fill(env, mdo2fid(obj), op);
-        mdd_orphan_write_lock(env, mdd);
+       LASSERT(dor);
 
-        rc = mdd_orphan_delete_obj(env, mdd, key, th);
+       key = mdd_orphan_key_fill(env, mdo2fid(obj));
+       dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
 
-        if (rc == -ENOENT) {
-                key = orph_key_fill_18(env, mdo2fid(obj));
-                rc = mdd_orphan_delete_obj(env, mdd, key, th);
-        }
+       rc = dt_delete(env, mdd->mdd_orphans, key, th);
+       if (rc == -ENOENT) {
+               key = mdd_orphan_key_fill_20(env, mdo2fid(obj));
+               rc = dt_delete(env, mdd->mdd_orphans, key, th);
+       }
 
-        if (!rc) {
-                /* lov objects will be destroyed by caller */
-                mdo_ref_del(env, obj, th);
-                if (S_ISDIR(mdd_object_type(obj))) {
-                        mdo_ref_del(env, obj, th);
-                        mdd_orphan_ref_del(env, mdd, th);
-                }
-                obj->mod_flags &= ~ORPHAN_OBJ;
-        } else {
-                CERROR("could not delete object: rc = %d\n",rc);
-        }
+       if (!rc) {
+               /* lov objects will be destroyed by caller */
+               mdo_ref_del(env, obj, th);
+               if (S_ISDIR(mdd_object_type(obj))) {
+                       mdo_ref_del(env, obj, th);
+                       dt_ref_del(env, mdd->mdd_orphans, th);
+               }
+               obj->mod_flags &= ~ORPHAN_OBJ;
+       } else {
+               CERROR("%s: could not delete orphan object "DFID": rc = %d\n",
+                      mdd2obd_dev(mdd)->obd_name, PFID(mdo2fid(obj)), rc);
+       }
 
-        mdd_orphan_write_unlock(env, mdd);
-        RETURN(rc);
+       dt_write_unlock(env, mdd->mdd_orphans);
+       RETURN(rc);
 }
 
 
-static int orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
-                         struct dt_key *key)
+static int mdd_orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
+                             struct dt_key *key)
 {
        struct thandle *th = NULL;
        struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
@@ -322,11 +288,14 @@ static int orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
 
        th = mdd_trans_create(env, mdd);
        if (IS_ERR(th)) {
-               CERROR("Cannot get thandle\n");
-               RETURN(PTR_ERR(th));
+               rc = PTR_ERR(th);
+               if (rc != -EINPROGRESS)
+                       CERROR("%s: cannot get orphan thandle: rc = %d\n",
+                              mdd2obd_dev(mdd)->obd_name, rc);
+               RETURN(rc);
        }
 
-       rc = orph_declare_index_delete(env, obj, th);
+       rc = mdd_orphan_declare_delete(env, obj, th);
        if (rc)
                GOTO(stop, rc);
 
@@ -340,18 +309,21 @@ static int orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
 
        mdd_write_lock(env, obj, MOR_TGT_CHILD);
        if (likely(obj->mod_count == 0)) {
-               mdd_orphan_write_lock(env, mdd);
-               rc = mdd_orphan_delete_obj(env, mdd, key, th);
+               dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
+               rc = dt_delete(env, mdd->mdd_orphans, key, th);
                if (rc == 0) {
                        mdo_ref_del(env, obj, th);
                        if (S_ISDIR(mdd_object_type(obj))) {
                                mdo_ref_del(env, obj, th);
-                               mdd_orphan_ref_del(env, mdd, th);
+                               dt_ref_del(env, mdd->mdd_orphans, th);
                        }
                        rc = mdo_destroy(env, obj, th);
-               } else
-                       CERROR("could not delete object: rc = %d\n", rc);
-               mdd_orphan_write_unlock(env, mdd);
+               } else {
+                       CERROR("%s: could not delete orphan "DFID": rc = %d\n",
+                              mdd2obd_dev(mdd)->obd_name, PFID(mdo2fid(obj)),
+                              rc);
+               }
+               dt_write_unlock(env, mdd->mdd_orphans);
        }
        mdd_write_unlock(env, obj);
 
@@ -371,9 +343,9 @@ stop:
  * \retval 0   success
  * \retval -ve error
  */
-static int orph_key_test_and_del(const struct lu_env *env,
-                                struct mdd_device *mdd, struct lu_fid *lf,
-                                struct dt_key *key)
+static int mdd_orphan_key_test_and_delete(const struct lu_env *env,
+                                         struct mdd_device *mdd,
+                                         struct lu_fid *lf, struct dt_key *key)
 {
        struct mdd_object *mdo;
        int rc;
@@ -386,10 +358,9 @@ static int orph_key_test_and_del(const struct lu_env *env,
        rc = -EBUSY;
        if (mdo->mod_count == 0) {
                CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
-               rc = orphan_destroy(env, mdo, key);
+               rc = mdd_orphan_destroy(env, mdo, key);
                if (rc) /* so replay-single.sh test_37 works */
-                       CERROR("%s: error unlinking orphan "DFID" from "
-                              "PENDING: rc = %d\n",
+                       CERROR("%s: error unlinking orphan "DFID": rc = %d\n",
                               mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
         } else {
                 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
@@ -417,38 +388,38 @@ static int orph_key_test_and_del(const struct lu_env *env,
  * \retval 0   success
  * \retval -ve error
  */
-static int orph_index_iterate(const struct lu_env *env,
-                             struct mdd_generic_thread *thread)
+static int mdd_orphan_index_iterate(const struct lu_env *env,
+                                   struct mdd_generic_thread *thread)
 {
        struct mdd_device *mdd = (struct mdd_device *)thread->mgt_data;
        struct dt_object *dor = mdd->mdd_orphans;
        struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
        const struct dt_it_ops *iops;
-       struct dt_it     *it;
-       struct lu_fid     fid;
-        int               key_sz = 0;
-        int               rc;
-        __u64             cookie;
-        ENTRY;
-
-        iops = &dor->do_index_ops->dio_it;
+       struct dt_it *it;
+       struct lu_fid fid;
+       int key_sz = 0;
+       int rc;
+       __u64 cookie;
+       ENTRY;
+
+       iops = &dor->do_index_ops->dio_it;
        it = iops->init(env, dor, LUDA_64BITHASH);
-        if (IS_ERR(it)) {
-                rc = PTR_ERR(it);
-                CERROR("%s: cannot clean PENDING: rc = %d\n",
-                      mdd2obd_dev(mdd)->obd_name, rc);
-                GOTO(out, rc);
-        }
+       if (IS_ERR(it)) {
+               rc = PTR_ERR(it);
+               CERROR("%s: cannot clean '%s': rc = %d\n",
+                      mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
+               GOTO(out, rc);
+       }
 
-        rc = iops->load(env, it, 0);
-        if (rc < 0)
-                GOTO(out_put, rc);
-        if (rc == 0) {
-                CERROR("%s: error loading iterator to clean PENDING\n",
-                      mdd2obd_dev(mdd)->obd_name);
-                /* Index contains no zero key? */
-                GOTO(out_put, rc = -EIO);
-        }
+       rc = iops->load(env, it, 0);
+       if (rc < 0)
+               GOTO(out_put, rc);
+       if (rc == 0) {
+               CERROR("%s: error loading iterator to clean '%s'\n",
+                      mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name);
+               /* Index contains no zero key? */
+               GOTO(out_put, rc = -EIO);
+       }
 
        do {
                if (thread->mgt_abort)
@@ -468,16 +439,17 @@ static int orph_index_iterate(const struct lu_env *env,
 
                fid_le_to_cpu(&fid, &ent->lde_fid);
                if (!fid_is_sane(&fid)) {
-                       CERROR("%s: bad FID "DFID" cleaning PENDING\n",
-                              mdd2obd_dev(mdd)->obd_name, PFID(&fid));
+                       CERROR("%s: bad FID "DFID" cleaning '%s'\n",
+                              mdd2obd_dev(mdd)->obd_name, PFID(&fid),
+                              mdd_orphan_index_name);
                        goto next;
                }
 
                /* kill orphan object */
                cookie = iops->store(env, it);
                iops->put(env, it);
-               rc = orph_key_test_and_del(env, mdd, &fid,
-                                          (struct dt_key *)ent->lde_name);
+               rc = mdd_orphan_key_test_and_delete(env, mdd, &fid,
+                                               (struct dt_key *)ent->lde_name);
 
                /* after index delete reset iterator */
                if (rc == 0)
@@ -513,18 +485,18 @@ out:
  *  \retval  -ve index operation error.
  *
  */
-int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
+int mdd_orphan_index_init(const struct lu_env *env, struct mdd_device *mdd)
 {
-       struct lu_fid            fid;
-       struct dt_object        *d;
-       int                      rc = 0;
+       struct lu_fid fid;
+       struct dt_object *d;
+       int rc = 0;
 
        ENTRY;
 
        /* create PENDING dir */
        fid_zero(&fid);
        rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
-                                  orph_index_name, S_IFDIR | S_IRUGO |
+                                  mdd_orphan_index_name, S_IFDIR | S_IRUGO |
                                   S_IWUSR | S_IXUGO, &fid);
        if (rc < 0)
                RETURN(rc);
@@ -534,8 +506,8 @@ int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
                RETURN(PTR_ERR(d));
        LASSERT(lu_object_exists(&d->do_lu));
        if (!dt_try_as_dir(env, d)) {
-               CERROR("%s: \"%s\" is not an index: rc = %d\n",
-                      mdd2obd_dev(mdd)->obd_name, orph_index_name, rc);
+               CERROR("%s: orphan dir '%s' is not an index: rc = %d\n",
+                      mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
                dt_object_put(env, d);
                RETURN(-ENOTDIR);
        }
@@ -543,7 +515,7 @@ int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
        RETURN(0);
 }
 
-void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
+void mdd_orphan_index_fini(const struct lu_env *env, struct mdd_device *mdd)
 {
        ENTRY;
        if (mdd->mdd_orphans != NULL) {
@@ -553,11 +525,11 @@ void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
        EXIT;
 }
 
-static int __mdd_orphan_cleanup(void *args)
+static int mdd_orphan_cleanup_thread(void *args)
 {
        struct mdd_generic_thread *thread = (struct mdd_generic_thread *)args;
-       struct lu_env             *env = NULL;
-       int                        rc;
+       struct lu_env *env = NULL;
+       int rc;
        ENTRY;
 
        complete(&thread->mgt_started);
@@ -570,7 +542,7 @@ static int __mdd_orphan_cleanup(void *args)
        if (rc)
                GOTO(out, rc);
 
-       rc = orph_index_iterate(env, thread);
+       rc = mdd_orphan_index_iterate(env, thread);
 
        lu_env_fini(env);
        GOTO(out, rc);
@@ -594,48 +566,16 @@ int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
        if (name == NULL)
                goto out;
 
-       snprintf(name, MTI_NAME_MAXLEN, "orph_cleanup_%s",
-                mdd2obd_dev(d)->obd_name);
+       snprintf(name, MTI_NAME_MAXLEN, "orph_%s", mdd2obd_dev(d)->obd_name);
 
-       rc = mdd_generic_thread_start(&d->mdd_orph_cleanup_thread,
-                                     __mdd_orphan_cleanup, (void *)d, name);
+       rc = mdd_generic_thread_start(&d->mdd_orphan_cleanup_thread,
+                                     mdd_orphan_cleanup_thread, d, name);
 out:
        if (rc)
-               CERROR("%s: start orphan cleanup thread failed:%d\n",
+               CERROR("%s: start orphan cleanup thread failed: rc = %d\n",
                       mdd2obd_dev(d)->obd_name, rc);
        if (name)
                OBD_FREE(name, MTI_NAME_MAXLEN);
-       return rc;
-}
-
-/**
- *  add an orphan \a obj to the orphan index.
- *  \param obj file or directory.
- *  \param th  transaction for index insert.
- *
- *  \pre obj nlink == 0 && obj->mod_count != 0
- *
- *  \retval 0  success
- *  \retval  -ve index operation error.
- */
-int __mdd_orphan_add(const struct lu_env *env,
-                     struct mdd_object *obj, struct thandle *th)
-{
-        return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
-}
 
-/**
- *  delete an orphan \a obj from orphan index.
- *  \param obj file or directory.
- *  \param th  transaction for index deletion and object destruction.
- *
- *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
- *
- *  \retval 0  success
- *  \retval  -ve index operation error.
- */
-int __mdd_orphan_del(const struct lu_env *env,
-                     struct mdd_object *obj, struct thandle *th)
-{
-        return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);
+       return rc;
 }