Whamcloud - gitweb
LU-601 mdd: cleanup error messages and code style
[fs/lustre-release.git] / lustre / mdd / mdd_orphans.c
index 6030725..2fe96cd 100644 (file)
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011 Whamcloud, Inc.
+ *
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -54,6 +57,7 @@
 #include "mdd_internal.h"
 
 const char orph_index_name[] = "PENDING";
+const char *dotdot = "..";
 
 enum {
         ORPH_OP_UNLINK,
@@ -70,7 +74,8 @@ static struct dt_key* orph_key_fill(const struct lu_env *env,
         int rc;
 
         LASSERT(key);
-        rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT, fid_seq(lf),
+        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;
@@ -85,8 +90,8 @@ static struct dt_key* orph_key_fill_18(const struct lu_env *env,
         int rc;
 
         LASSERT(key);
-        rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_18, fid_seq(lf),
-                      fid_oid(lf));
+        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
@@ -98,20 +103,21 @@ static int orphan_key_to_fid(char *key, struct lu_fid *lf)
         int rc = 0;
         unsigned int op;
 
-        rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT, &lf->f_seq, &lf->f_oid,
+        rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT,
+                    (long long unsigned int *)&lf->f_seq, &lf->f_oid,
                     &lf->f_ver, &op);
         if (rc == 4)
                 return 0;
 
         /* build igif */
         rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT_18,
-                    &lf->f_seq, &lf->f_oid);
+                    (long long unsigned int *)&lf->f_seq, &lf->f_oid);
         if (rc == 2) {
                 lf->f_ver = 0;
                 return 0;
         }
 
-        CERROR("can not parse orphan file name %s\n",key);
+        CERROR("can not parse orphan file name %s\n", key);
         return -EINVAL;
 }
 
@@ -143,7 +149,7 @@ static inline int mdd_orphan_insert_obj(const struct lu_env *env,
         ENTRY;
 
         return  dor->do_index_ops->dio_insert(env, dor,
-                                              __mdd_fid_rec(env, lf),
+                                              (struct dt_rec *)lf,
                                               key, th,
                                               BYPASS_CAPA, 1);
 }
@@ -177,6 +183,41 @@ static inline void mdd_orphan_ref_del(const struct lu_env *env,
 }
 
 
+int orph_declare_index_insert(const struct lu_env *env,
+                              struct mdd_object *obj,
+                              struct thandle *th)
+{
+        struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+        int                rc;
+
+        rc = dt_declare_insert(env, mdd->mdd_orphans, NULL, NULL, th);
+        if (rc)
+                return rc;
+
+        rc = mdo_declare_ref_add(env, obj, th);
+        if (rc)
+                return rc;
+
+        if (!S_ISDIR(mdd_object_type(obj)))
+                return 0;
+
+        rc = mdo_declare_ref_add(env, obj, th);
+        if (rc)
+                return rc;
+
+        rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
+        if (rc)
+                return rc;
+
+        rc = mdo_declare_index_delete(env, obj, dotdot, th);
+        if (rc)
+                return rc;
+
+        rc = mdo_declare_index_insert(env, obj, NULL, dotdot, th);
+
+        return rc;
+}
+
 static int orph_index_insert(const struct lu_env *env,
                              struct mdd_object *obj,
                              __u32 op,
@@ -186,10 +227,13 @@ static int orph_index_insert(const struct lu_env *env,
         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);
-        const struct dt_key     *dotdot = (const struct dt_key *) "..";
         int rc;
         ENTRY;
 
+        LASSERT(mdd_write_locked(env, obj) != 0);
+        LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
+        LASSERT(obj->mod_count > 0);
+
         mdd_orphan_write_lock(env, mdd);
 
         rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
@@ -208,20 +252,25 @@ static int orph_index_insert(const struct lu_env *env,
         if (!dt_try_as_dir(env, next))
                 goto out;
         next->do_index_ops->dio_delete(env, next,
-                                       dotdot, th, BYPASS_CAPA);
+                                       (const struct dt_key *)dotdot,
+                                       th, BYPASS_CAPA);
 
         next->do_index_ops->dio_insert(env, next,
-                                       __mdd_fid_rec(env, lf_dor),
-                                       dotdot, th, BYPASS_CAPA, 1);
+                                       (struct dt_rec *)lf_dor,
+                                       (const struct dt_key *)dotdot,
+                                       th, BYPASS_CAPA, 1);
 
 out:
+        if (rc == 0)
+                obj->mod_flags |= ORPHAN_OBJ;
+
         mdd_orphan_write_unlock(env, mdd);
 
         RETURN(rc);
 }
 
 /**
- * destroy osd object on mdd and associated ost objects.
+ * Destroy OSD object on MDD and associated OST objects.
  *
  * \param obj orphan object
  * \param mdd used for sending llog msg to osts
@@ -235,28 +284,51 @@ static int orphan_object_kill(const struct lu_env *env,
                               struct thandle *th)
 {
         struct lu_attr *la = &mdd_env_info(env)->mti_la;
-        int rc;
+        int rc = 0;
+        ENTRY;
 
         /* No need to lock this object as its recovery phase, and
          * no other thread can access it. But we need to lock it
          * as its precondition for osd api we using. */
 
-        mdd_write_lock(env, obj, MOR_TGT_CHILD);
         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);
-                mdd_write_unlock(env, obj);
         } else {
                 /* regular file , cleanup linked ost objects */
                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
-                mdd_write_unlock(env, obj);
+                if (rc == 0)
+                        rc = mdd_lov_destroy(env, mdd, obj, la);
+        }
+        mdo_destroy(env, obj, th);
+        RETURN(rc);
+}
+
+int orph_declare_index_delete(const struct lu_env *env,
+                              struct mdd_object *obj,
+                              struct thandle *th)
+{
+        struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+        int                rc;
+
+        rc = dt_declare_delete(env, mdd->mdd_orphans, NULL, th);
+        if (rc)
+                return rc;
+
+        rc = mdo_declare_ref_del(env, obj, th);
+        if (rc)
+                return rc;
+
+        if (S_ISDIR(mdd_object_type(obj))) {
+                rc = mdo_declare_ref_del(env, obj, th);
                 if (rc)
-                        RETURN(rc);
+                        return rc;
 
-                mdd_lov_destroy(env, mdd, obj, la);
+                rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
         }
-        return 0;
+
+        return rc;
 }
 
 static int orph_index_delete(const struct lu_env *env,
@@ -271,6 +343,10 @@ static int orph_index_delete(const struct lu_env *env,
 
         ENTRY;
 
+        LASSERT(mdd_write_locked(env, obj) != 0);
+        LASSERT(obj->mod_flags & ORPHAN_OBJ);
+        LASSERT(obj->mod_count == 0);
+
         LASSERT(dor);
 
         key = orph_key_fill(env, mdo2fid(obj), op);
@@ -290,10 +366,11 @@ static int orph_index_delete(const struct lu_env *env,
                         mdo_ref_del(env, obj, th);
                         mdd_orphan_ref_del(env, mdd, th);
                 }
-        } else
+                obj->mod_flags &= ~ORPHAN_OBJ;
+        } else {
                 CERROR("could not delete object: rc = %d\n",rc);
+        }
 
-        obj->mod_flags &= ~ORPHAN_OBJ;
         mdd_orphan_write_unlock(env, mdd);
         RETURN(rc);
 }
@@ -305,29 +382,63 @@ static int orphan_object_destroy(const struct lu_env *env,
 {
         struct thandle *th = NULL;
         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
-        int rc;
+        struct md_attr *ma = &mdd_env_info(env)->mti_ma;
+        int rc = 0;
         ENTRY;
 
-        mdd_txn_param_build(env, mdd, MDD_TXN_UNLINK_OP);
-        th = mdd_trans_start(env, mdd);
+        /* init ma */
+        ma->ma_lmm_size = mdd_lov_mdsize(env, mdd);
+        ma->ma_lmm = mdd_max_lmm_get(env, mdd);
+        ma->ma_cookie_size = mdd_lov_cookiesize(env, mdd);
+        ma->ma_cookie = mdd_max_cookie_get(env, mdd);
+        ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
+        ma->ma_valid = 0;
+
+        th = mdd_trans_create(env, mdd);
         if (IS_ERR(th)) {
                 CERROR("Cannot get thandle\n");
                 RETURN(-ENOMEM);
         }
+        rc = orph_declare_index_delete(env, obj, th);
+        if (rc)
+                GOTO(stop, rc);
 
-        mdd_orphan_write_lock(env, mdd);
-        rc = mdd_orphan_delete_obj(env, mdd, key, th);
-        if (!rc)
-                orphan_object_kill(env, obj, mdd, th);
-        else
-                CERROR("could not delete object: rc = %d\n",rc);
+        rc = mdd_declare_object_kill(env, obj, ma, th);
+        if (rc)
+                GOTO(stop, rc);
 
-        mdd_orphan_write_unlock(env, mdd);
+        rc = mdd_trans_start(env, mdd, th);
+        if (rc)
+                GOTO(stop, rc);
+
+        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);
+                if (rc == 0)
+                        orphan_object_kill(env, obj, mdd, th);
+                else
+                        CERROR("could not delete object: rc = %d\n",rc);
+                mdd_orphan_write_unlock(env, mdd);
+        }
+        mdd_write_unlock(env, obj);
+
+stop:
         mdd_trans_stop(env, mdd, 0, th);
 
         RETURN(rc);
 }
 
+/**
+ * Delete unused orphan with FID \a lf from PENDING directory
+ *
+ * \param mdd  MDD device finishing recovery
+ * \param lf   FID of file or directory to delete
+ * \param key  cookie for this entry in index iterator
+ *
+ * \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,
@@ -343,17 +454,38 @@ static int orph_key_test_and_del(const struct lu_env *env,
 
         rc = -EBUSY;
         if (mdo->mod_count == 0) {
-                CWARN("Found orphan! Delete it\n");
+                CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
                 rc = orphan_object_destroy(env, mdo, key);
+                if (rc) /* so replay-single.sh test_37 works */
+                        CERROR("%s: error unlinking orphan "DFID" from "
+                               "PENDING: rc = %d\n",
+                               mdd->mdd_obd_dev->obd_name, PFID(lf), rc);
         } else {
-                CDEBUG(D_HA, "Found orphan, open count = %d\n", mdo->mod_count);
-                mdo->mod_flags |= ORPHAN_OBJ;
+                mdd_write_lock(env, mdo, MOR_TGT_CHILD);
+                if (likely(mdo->mod_count > 0)) {
+                        CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
+                               PFID(lf), mdo->mod_count);
+                        mdo->mod_flags |= ORPHAN_OBJ;
+                }
+                mdd_write_unlock(env, mdo);
         }
 
         mdd_object_put(env, mdo);
         return rc;
 }
 
+/**
+ * delete unreferenced files and directories in the PENDING directory
+ *
+ * Files that remain in PENDING after client->MDS recovery has completed
+ * have to be referenced (opened) by some client during recovery, or they
+ * will be deleted here (for clients that did not complete recovery).
+ *
+ * \param mdd  MDD device finishing recovery
+ *
+ * \retval 0   success
+ * \retval -ve error
+ */
 static int orph_index_iterate(const struct lu_env *env,
                               struct mdd_device *mdd)
 {
@@ -370,60 +502,89 @@ static int orph_index_iterate(const struct lu_env *env,
         ENTRY;
 
         /* In recovery phase, do not need for any lock here */
-
         iops = &dor->do_index_ops->dio_it;
-        it = iops->init(env, dor, BYPASS_CAPA);
-        if (it != NULL) {
-                result = iops->get(env, it, (const void *)"");
-                if (result > 0) {
-                        /* main cycle */
-                        do {
-
-                                key = (void *)iops->key(env, it);
-                                if (IS_ERR(key))
-                                        goto next;
-                                key_sz = iops->key_size(env, it);
-
-                                /* filter out "." and ".." entries from
-                                 * PENDING dir. */
-                                if (key_sz < 8)
-                                        goto next;
-
-                                memcpy(mti_key, key, key_sz);
-                                mti_key[key_sz] = 0;
-
-                                if (orphan_key_to_fid(mti_key, &fid))
-                                        goto next;
-                                if (!fid_is_sane(&fid))
-                                        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 *)mti_key);
-
-                                /* after index delete reset iterator */
-                                if (!rc)
-                                        result = iops->get(env, it,
-                                                           (const void *)"");
-                                else
-                                        result = iops->load(env, it, cookie);
-next:
-                                result = iops->next(env, it);
-                        } while (result == 0);
-                        result = 0;
-                } else if (result == 0)
-                        /* Index contains no zero key? */
-                        result = -EIO;
+        it = iops->init(env, dor, LUDA_64BITHASH, BYPASS_CAPA);
+        if (IS_ERR(it)) {
+                rc = PTR_ERR(it);
+                CERROR("%s: cannot clean PENDING: rc = %d\n",
+                       mdd->mdd_obd_dev->obd_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",
+                       mdd->mdd_obd_dev->obd_name);
+                /* Index contains no zero key? */
+                GOTO(out_put, rc = -EIO);
+        }
+
+        do {
+                key = (void *)iops->key(env, it);
+                if (IS_ERR(key)) {
+                        CERROR("%s: key failed when clean PENDING: rc = %ld\n",
+                               mdd->mdd_obd_dev->obd_name, PTR_ERR(key));
+                        goto next;
+                }
+                key_sz = iops->key_size(env, it);
+
+                /* filter out "." and ".." entries from PENDING dir. */
+                if (key_sz < 8)
+                        goto next;
+
+                memcpy(mti_key, key, key_sz);
+                mti_key[key_sz] = 0;
+
+                if (orphan_key_to_fid(mti_key, &fid))
+                        goto next;
+                if (!fid_is_sane(&fid)) {
+                        CERROR("%s: bad FID "DFID" cleaning PENDING\n",
+                               mdd->mdd_obd_dev->obd_name, PFID(&fid));
+                        goto next;
+                }
+
+                /* kill orphan object */
+                cookie = iops->store(env, it);
                 iops->put(env, it);
-                iops->fini(env, it);
-        } else
-                result = -ENOMEM;
+                rc = orph_key_test_and_del(env, mdd, &fid,
+                                          (struct dt_key *)mti_key);
+
+                /* after index delete reset iterator */
+                if (rc == 0)
+                        result = iops->get(env, it, (const void *)"");
+                else
+                        result = iops->load(env, it, cookie);
+next:
+                result = iops->next(env, it);
+        } while (result == 0);
+
+        GOTO(out_put, rc = 0);
+out_put:
+        iops->put(env, it);
+        iops->fini(env, it);
 
-        RETURN(result);
+out:
+        return rc;
 }
 
+/**
+ * open the PENDING directory for device \a mdd
+ *
+ * The PENDING directory persistently tracks files and directories that were
+ * unlinked from the namespace (nlink == 0) but are still held open by clients.
+ * Those inodes shouldn't be deleted if the MDS crashes, because the clients
+ * would not be able to recover and reopen those files.  Instead, these inodes
+ * are linked into the PENDING directory on disk, and only deleted if all
+ * clients close them, or the MDS finishes client recovery without any client
+ * reopening them (i.e. former clients didn't join recovery).
+ *  \param d   mdd device being started.
+ *
+ *  \retval 0  success
+ *  \retval  -ve index operation error.
+ *
+ */
 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
 {
         struct lu_fid fid;
@@ -459,27 +620,24 @@ void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
 }
 
 /**
- *  Iterate orphan index to cleanup orphan objects in case of recovery.
+ *  Iterate orphan index to cleanup orphan objects after recovery is done.
  *  \param d   mdd device in recovery.
- *
  */
-
 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
 {
         return orph_index_iterate(env, d);
 }
 
 /**
- *  delete an orphan \a obj from orphan index.
+ *  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
- *  \retva  -ve index operation error.
+ *  \retval  -ve index operation error.
  */
-
 int __mdd_orphan_add(const struct lu_env *env,
                      struct mdd_object *obj, struct thandle *th)
 {
@@ -494,9 +652,8 @@ int __mdd_orphan_add(const struct lu_env *env,
  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
  *
  *  \retval 0  success
- *  \retva  -ve index operation error.
+ *  \retval  -ve index operation error.
  */
-
 int __mdd_orphan_del(const struct lu_env *env,
                      struct mdd_object *obj, struct thandle *th)
 {