Whamcloud - gitweb
LU-4778 llog: update catlog when plain log destroyed
[fs/lustre-release.git] / lustre / obdclass / llog_test.c
index 1e41e31..a1051c2 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel, Inc.
+ * Copyright (c) 2012, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -158,7 +158,7 @@ static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
                GOTO(out_put, rc);
        }
 
-       llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
+       rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
        if (rc) {
                CERROR("2a: can't init llog handle: %d\n", rc);
                GOTO(out_close_llh, rc);
@@ -343,7 +343,7 @@ static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
                CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
                GOTO(ctxt_release, rc);
         }
-       llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
+       rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
        if (rc) {
                CERROR("4a: can't init llog handle: %d\n", rc);
                GOTO(out, rc);
@@ -352,11 +352,6 @@ static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
        num_recs++;
        cat_logid = cath->lgh_id;
 
-       /* XXX: there is known issue with tests 4b, MGS is not able to add
-        * anonymous plain llog, si we exit now to allow following tests run.
-        * It is fixed in upcoming llog over OSD code */
-       GOTO(out, rc);
-
        CWARN("4b: write 1 record into the catalog\n");
        rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie, NULL);
        if (rc != 1) {
@@ -438,15 +433,18 @@ static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
                        struct llog_rec_hdr *rec, void *data)
 {
        struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
+       struct lu_fid            fid = {0};
 
        if (rec->lrh_type != LLOG_LOGID_MAGIC) {
                CERROR("invalid record in catalog\n");
                RETURN(-EINVAL);
        }
 
-       CWARN("seeing record at index %d - "LPX64":%x in log "LPX64"\n",
-             rec->lrh_index, lir->lid_id.lgl_oid,
-             lir->lid_id.lgl_ogen, llh->lgh_id.lgl_oid);
+       logid_to_fid(&lir->lid_id, &fid);
+
+       CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
+             rec->lrh_index, PFID(&fid),
+             PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
 
        cat_counter++;
 
@@ -458,13 +456,17 @@ static int plain_counter;
 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
                          struct llog_rec_hdr *rec, void *data)
 {
+       struct lu_fid fid = {0};
+
        if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
                CERROR("log is not plain\n");
                RETURN(-EINVAL);
        }
 
-       CDEBUG(D_INFO, "seeing record at index %d in log "LPX64"\n",
-             rec->lrh_index, llh->lgh_id.lgl_oid);
+       logid_to_fid(&llh->lgh_id, &fid);
+
+       CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
+              rec->lrh_index, PFID(&fid));
 
        plain_counter++;
 
@@ -518,15 +520,12 @@ static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
                GOTO(out_put, rc);
        }
 
-       llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
+       rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
        if (rc) {
                CERROR("5a: can't init llog handle: %d\n", rc);
                GOTO(out, rc);
        }
 
-       /* XXX: depends on tests 4 which is not working yet */
-       GOTO(out, rc);
-
        CWARN("5b: print the catalog entries.. we expect 2\n");
        cat_counter = 0;
        rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
@@ -634,7 +633,7 @@ static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
        rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
                         NULL /* obd_connect_data */, NULL);
        if (rc != -EALREADY) {
-               CERROR("6a: connect on connected MDC (%s) failed to return"
+               CERROR("6a: connect on connected MGC (%s) failed to return"
                       " -EALREADY", mgc_obd->obd_name);
                if (rc == 0)
                        obd_disconnect(exp);
@@ -704,7 +703,7 @@ static union {
 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
                           struct llog_rec_hdr *rec, void *data)
 {
-       struct lu_fid fid;
+       struct lu_fid fid = {0};
 
        logid_to_fid(&llh->lgh_id, &fid);
 
@@ -731,7 +730,7 @@ static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
 
        ENTRY;
 
-       rc = llog_open_create(env, ctxt, &llh, NULL, "llt_test7");
+       rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
        if (rc) {
                CERROR("7_sub: create log failed\n");
                RETURN(rc);
@@ -785,7 +784,7 @@ static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
 
        plain_counter = 0;
        rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
-       if (rc) {
+       if (rc && rc != LLOG_DEL_PLAIN) {
                CERROR("7_sub: reverse llog process failed: %d\n", rc);
                GOTO(out_close, rc);
        }
@@ -899,13 +898,212 @@ out:
        RETURN(rc);
 }
 
+static int llog_truncate(const struct lu_env *env, struct dt_object *o)
+{
+       struct lu_attr           la;
+       struct thandle          *th;
+       struct dt_device        *d;
+       int                      rc;
+       ENTRY;
+
+       LASSERT(o);
+       d = lu2dt_dev(o->do_lu.lo_dev);
+       LASSERT(d);
+
+       rc = dt_attr_get(env, o, &la, NULL);
+       if (rc)
+               RETURN(rc);
+
+       CDEBUG(D_OTHER, "original size %Lu\n", la.la_size);
+       rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
+       if (la.la_size < rc) {
+               CERROR("too small llog: %Lu\n", la.la_size);
+               RETURN(0);
+       }
+
+       /* drop 2 records */
+       la.la_size = la.la_size - (sizeof(struct llog_mini_rec) * 2);
+       la.la_valid = LA_SIZE;
+
+       th = dt_trans_create(env, d);
+       if (IS_ERR(th))
+               RETURN(PTR_ERR(th));
+
+       rc = dt_declare_attr_set(env, o, &la, th);
+       if (rc)
+               GOTO(stop, rc);
+
+       rc = dt_declare_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
+
+       rc = dt_trans_start_local(env, d, th);
+       if (rc)
+               GOTO(stop, rc);
+
+       rc = dt_punch(env, o, la.la_size, OBD_OBJECT_EOF, th, BYPASS_CAPA);
+       if (rc)
+               GOTO(stop, rc);
+
+       rc = dt_attr_set(env, o, &la, th, BYPASS_CAPA);
+       if (rc)
+               GOTO(stop, rc);
+
+stop:
+       dt_trans_stop(env, d, th);
+
+       RETURN(rc);
+}
+
+static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
+                         struct llog_rec_hdr *rec, void *data)
+{
+       plain_counter++;
+       return 0;
+}
+
+static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
+{
+       struct llog_handle      *llh = NULL;
+       char                     name[10];
+       int                      rc, rc2, i;
+       int                      orig_counter;
+       struct llog_mini_rec     lmr;
+       struct llog_ctxt        *ctxt;
+       struct dt_object        *obj = NULL;
+
+       ENTRY;
+
+       ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
+       LASSERT(ctxt);
+
+       lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
+       lmr.lmr_hdr.lrh_type = 0xf00f00;
+
+       CWARN("8a: fill the first plain llog\n");
+       rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
+       if (rc) {
+               CERROR("8a: llog_create with logid failed: %d\n", rc);
+               GOTO(out_put, rc);
+       }
+
+       rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
+       if (rc) {
+               CERROR("8a: can't init llog handle: %d\n", rc);
+               GOTO(out, rc);
+       }
+
+       plain_counter = 0;
+       rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
+       if (rc != 0) {
+               CERROR("5a: process with cat_cancel_cb failed: %d\n", rc);
+               GOTO(out, rc);
+       }
+       orig_counter = plain_counter;
+
+       for (i = 0; i < 100; i++) {
+               rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL, NULL);
+               if (rc) {
+                       CERROR("5a: add record failed\n");
+                       GOTO(out, rc);
+               }
+       }
+
+       /* grab the current plain llog, we'll corrupt it later */
+       obj = llh->u.chd.chd_current_log->lgh_obj;
+       LASSERT(obj);
+       lu_object_get(&obj->do_lu);
+       CWARN("8a: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
+
+       rc2 = llog_cat_close(env, llh);
+       if (rc2) {
+               CERROR("8a: close log %s failed: %d\n", name, rc2);
+               if (rc == 0)
+                       rc = rc2;
+               GOTO(out_put, rc);
+       }
+
+       CWARN("8b: fill the second plain llog\n");
+       rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
+       if (rc) {
+               CERROR("8b: llog_create with logid failed: %d\n", rc);
+               GOTO(out_put, rc);
+       }
+
+       rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
+       if (rc) {
+               CERROR("8b: can't init llog handle: %d\n", rc);
+               GOTO(out, rc);
+       }
+
+       for (i = 0; i < 100; i++) {
+               rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL, NULL);
+               if (rc) {
+                       CERROR("8b: add record failed\n");
+                       GOTO(out, rc);
+               }
+       }
+       CWARN("8b: second llog "DFID"\n",
+               PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
+
+       rc2 = llog_cat_close(env, llh);
+       if (rc2) {
+               CERROR("8b: close log %s failed: %d\n", name, rc2);
+               if (rc == 0)
+                       rc = rc2;
+               GOTO(out_put, rc);
+       }
+
+       CWARN("8c: drop two records from the first plain llog\n");
+       llog_truncate(env, obj);
+
+       CWARN("8d: count survived records\n");
+       rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
+       if (rc) {
+               CERROR("8d: llog_create with logid failed: %d\n", rc);
+               GOTO(out_put, rc);
+       }
+
+       rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
+       if (rc) {
+               CERROR("8d: can't init llog handle: %d\n", rc);
+               GOTO(out, rc);
+       }
+
+       plain_counter = 0;
+       rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
+       if (rc != 0) {
+               CERROR("8d: process with cat_cancel_cb failed: %d\n", rc);
+               GOTO(out, rc);
+       }
+
+       if (orig_counter + 200 - 2 != plain_counter) {
+               CERROR("found %d records (expected %d)\n", plain_counter,
+                      orig_counter + 200 - 2);
+               rc = -EIO;
+       }
+
+out:
+       CWARN("8d: close re-opened catalog\n");
+       rc2 = llog_cat_close(env, llh);
+       if (rc2) {
+               CERROR("8d: close log %s failed: %d\n", name, rc2);
+               if (rc == 0)
+                       rc = rc2;
+       }
+out_put:
+       llog_ctxt_put(ctxt);
+
+       if (obj != NULL)
+               lu_object_put(env, &obj->do_lu);
+
+       RETURN(rc);
+}
+
 /* -------------------------------------------------------------------------
  * Tests above, boring obd functions below
  * ------------------------------------------------------------------------- */
 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
 {
        struct llog_handle      *llh = NULL;
-       struct lvfs_run_ctxt     saved;
        struct llog_ctxt        *ctxt;
        int                      rc, err;
        char                     name[10];
@@ -915,7 +1113,6 @@ static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
        LASSERT(ctxt);
 
        sprintf(name, "%x", llog_test_rand);
-       push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
 
        rc = llog_test_1(env, obd, name);
        if (rc)
@@ -945,6 +1142,10 @@ static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
        if (rc)
                GOTO(cleanup, rc);
 
+       rc = llog_test_8(env, obd);
+       if (rc)
+               GOTO(cleanup, rc);
+
 cleanup:
        err = llog_destroy(env, llh);
        if (err)
@@ -953,36 +1154,38 @@ cleanup:
        if (rc == 0)
                rc = err;
 cleanup_ctxt:
-       pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
        llog_ctxt_put(ctxt);
        return rc;
 }
 
-#ifdef LPROCFS
-static struct lprocfs_vars lprocfs_llog_test_obd_vars[] = { {0} };
-static struct lprocfs_vars lprocfs_llog_test_module_vars[] = { {0} };
-static void lprocfs_llog_test_init_vars(struct lprocfs_static_vars *lvars)
-{
-    lvars->module_vars  = lprocfs_llog_test_module_vars;
-    lvars->obd_vars     = lprocfs_llog_test_obd_vars;
-}
-#endif
-
 static int llog_test_cleanup(struct obd_device *obd)
 {
-       int rc;
+       struct obd_device       *tgt;
+       struct lu_env            env;
+       int                      rc;
+
+       ENTRY;
+
+       rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
+       if (rc)
+               RETURN(rc);
 
-       rc = llog_cleanup(NULL, llog_get_context(obd, LLOG_TEST_ORIG_CTXT));
+       tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
+       rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
        if (rc)
                CERROR("failed to llog_test_llog_finish: %d\n", rc);
-       return rc;
+       lu_env_fini(&env);
+       RETURN(rc);
 }
 
 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
 {
-        struct obd_device *tgt;
-       struct lu_env env;
-        int rc;
+       struct obd_device       *tgt;
+       struct llog_ctxt        *ctxt;
+       struct dt_object        *o;
+       struct lu_env            env;
+       struct lu_context        test_session;
+       int                      rc;
 
         ENTRY;
 
@@ -1008,19 +1211,43 @@ static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
        if (rc)
                RETURN(rc);
 
+       rc = lu_context_init(&test_session, LCT_SERVER_SESSION);
+       if (rc)
+               GOTO(cleanup_env, rc);
+       test_session.lc_thread = (struct ptlrpc_thread *)current;
+       lu_context_enter(&test_session);
+       env.le_ses = &test_session;
+
        CWARN("Setup llog-test device over %s device\n",
              lustre_cfg_string(lcfg, 1));
 
-       rc = llog_setup(&env, obd, &obd->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
-                       &llog_lvfs_ops);
+       OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
+       obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
+
+       rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
+                       &llog_osd_ops);
        if (rc)
-               GOTO(cleanup_env, rc);
+               GOTO(cleanup_session, rc);
+
+       /* use MGS llog dir for tests */
+       ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
+       LASSERT(ctxt);
+       o = ctxt->loc_dir;
+       llog_ctxt_put(ctxt);
+
+       ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
+       LASSERT(ctxt);
+       ctxt->loc_dir = o;
+       llog_ctxt_put(ctxt);
 
        llog_test_rand = cfs_rand();
 
-       rc = llog_run_tests(&env, obd);
+       rc = llog_run_tests(&env, tgt);
        if (rc)
                llog_test_cleanup(obd);
+cleanup_session:
+       lu_context_exit(&test_session);
+       lu_context_fini(&test_session);
 cleanup_env:
        lu_env_fini(&env);
        RETURN(rc);
@@ -1034,11 +1261,11 @@ static struct obd_ops llog_obd_ops = {
 
 static int __init llog_test_init(void)
 {
-       struct lprocfs_static_vars lvars;
-
-       lprocfs_llog_test_init_vars(&lvars);
-       return class_register_type(&llog_obd_ops, NULL,
-                                  lvars.module_vars, "llog_test", NULL);
+       return class_register_type(&llog_obd_ops, NULL, NULL,
+#ifndef HAVE_ONLY_PROCFS_SEQ
+                                       NULL,
+#endif
+                                       "llog_test", NULL);
 }
 
 static void __exit llog_test_exit(void)