- struct dt_object *dt_obj = mdd->mdd_orphans;
- struct dt_it *it;
- const struct dt_it_ops *iops;
- struct orph_key *key = orph_key_empty(env, 0);
- int result;
- ENTRY;
-
- iops = &dt_obj->do_index_ops->dio_it;
- it = iops->init(env, dt_obj, 1, BYPASS_CAPA);
- if (it != NULL) {
- result = iops->get(env, it, (const void *)key);
- if (result > 0) {
- int i;
- /* main cycle */
- for (result = 0, i = 0; result == +1; ++i) {
- key = (void *)iops->key(env, it);
- fid_be_to_cpu(&key->ok_fid, &key->ok_fid);
- orph_key_test_and_del(env, mdd, key);
- result = iops->next(env, it);
- }
- } else if (result == 0)
- /* Index contains no zero key? */
- result = -EIO;
-
- iops->put(env, it);
- iops->fini(env, it);
- } else
- result = -ENOMEM;
-
- RETURN(result);
+ struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+ struct dt_object *dor = mdd->mdd_orphans;
+ struct dt_key *key;
+ int rc = 0;
+
+ ENTRY;
+
+ LASSERT(mdd_write_locked(env, obj) != 0);
+ LASSERT(obj->mod_flags & ORPHAN_OBJ);
+ LASSERT(obj->mod_count == 0);
+
+ LASSERT(dor);
+
+ key = mdd_orphan_key_fill(env, mdo2fid(obj));
+ dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
+
+ if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ORPHAN_DELETE))
+ goto ref_del;
+
+ 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);
+ }
+
+ref_del:
+ 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);
+ }
+
+ dt_write_unlock(env, mdd->mdd_orphans);
+ RETURN(rc);
+}
+
+
+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);
+ bool orphan_exists = true;
+ int rc = 0;
+ ENTRY;
+
+ th = mdd_trans_create(env, mdd);
+ if (IS_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);
+ }
+
+ mdd_write_lock(env, obj, MOR_TGT_CHILD);
+ rc = mdd_orphan_declare_delete(env, obj, th);
+ if (rc == -ENOENT)
+ orphan_exists = false;
+ else if (rc)
+ GOTO(unlock, rc);
+
+ if (orphan_exists) {
+ rc = mdo_declare_destroy(env, obj, th);
+ if (rc)
+ GOTO(unlock, rc);
+ }
+
+ rc = mdd_trans_start(env, mdd, th);
+ if (rc)
+ GOTO(unlock, rc);
+
+ if (likely(obj->mod_count == 0)) {
+ dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
+ rc = dt_delete(env, mdd->mdd_orphans, key, th);
+ if (rc) {
+ CERROR("%s: could not delete orphan "DFID": rc = %d\n",
+ mdd2obd_dev(mdd)->obd_name, PFID(mdo2fid(obj)),
+ rc);
+ } else if (orphan_exists) {
+ 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);
+ }
+ rc = mdo_destroy(env, obj, th);
+ } else {
+ CWARN("%s: orphan %s "DFID" doesn't exist\n",
+ mdd2obd_dev(mdd)->obd_name, (char *)key,
+ PFID(mdo2fid(obj)));
+ }
+ dt_write_unlock(env, mdd->mdd_orphans);
+ }
+unlock:
+ mdd_write_unlock(env, obj);
+
+ rc = mdd_trans_stop(env, mdd, 0, th);
+
+ RETURN(rc);