Whamcloud - gitweb
LU-3105 osd: remove capa related stuff from servers
[fs/lustre-release.git] / lustre / obdclass / llog_test.c
index b41ab6f..902bbaa 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) 2012, 2013, Intel Corporation.
+ * Copyright (c) 2012, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -230,7 +230,7 @@ static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
        lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
 
        CWARN("3a: write one create_rec\n");
-       rc = llog_write(env, llh,  &lgr.lgr_hdr, NULL, 0, NULL, -1);
+       rc = llog_write(env, llh,  &lgr.lgr_hdr, LLOG_NEXT_IDX);
        num_recs++;
        if (rc < 0) {
                CERROR("3a: write one log record failed: %d\n", rc);
@@ -241,30 +241,9 @@ static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
        if (rc)
                RETURN(rc);
 
-       CWARN("3b: write 10 cfg log records with 8 bytes bufs\n");
-       for (i = 0; i < 10; i++) {
-               struct llog_rec_hdr     hdr;
-               char                    buf[8];
-
-               hdr.lrh_len = 8;
-               hdr.lrh_type = OBD_CFG_REC;
-               memset(buf, 0, sizeof buf);
-               rc = llog_write(env, llh, &hdr, NULL, 0, buf, -1);
-               if (rc < 0) {
-                       CERROR("3b: write 10 records failed at #%d: %d\n",
-                              i + 1, rc);
-                       RETURN(rc);
-               }
-               num_recs++;
-       }
-
-       rc = verify_handle("3b", llh, num_recs);
-       if (rc)
-               RETURN(rc);
-
        CWARN("3c: write 1000 more log records\n");
        for (i = 0; i < 1000; i++) {
-               rc = llog_write(env, llh, &lgr.lgr_hdr, NULL, 0, NULL, -1);
+               rc = llog_write(env, llh, &lgr.lgr_hdr, LLOG_NEXT_IDX);
                if (rc < 0) {
                        CERROR("3c: write 1000 records failed at #%d: %d\n",
                               i + 1, rc);
@@ -277,23 +256,20 @@ static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
        if (rc)
                RETURN(rc);
 
-       CWARN("3d: write log more than BITMAP_SIZE, return -ENOSPC\n");
+       CWARN("3d: write records with variable size until BITMAP_SIZE, "
+             "return -ENOSPC\n");
        for (i = 0; i < LLOG_BITMAP_SIZE(llh->lgh_hdr) + 1; i++) {
-               struct llog_rec_hdr     hdr;
-               char                    buf_even[24];
-               char                    buf_odd[32];
-
-               memset(buf_odd, 0, sizeof buf_odd);
-               memset(buf_even, 0, sizeof buf_even);
-               if ((i % 2) == 0) {
-                       hdr.lrh_len = 24;
-                       hdr.lrh_type = OBD_CFG_REC;
-                       rc = llog_write(env, llh, &hdr, NULL, 0, buf_even, -1);
-               } else {
-                       hdr.lrh_len = 32;
-                       hdr.lrh_type = OBD_CFG_REC;
-                       rc = llog_write(env, llh, &hdr, NULL, 0, buf_odd, -1);
-               }
+               char                     buf[64];
+               struct llog_rec_hdr     *hdr = (void *)&buf;
+
+               memset(buf, 0, sizeof buf);
+               if ((i % 2) == 0)
+                       hdr->lrh_len = 40;
+               else
+                       hdr->lrh_len = 64;
+               hdr->lrh_type = OBD_CFG_REC;
+               rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
+
                if (rc == -ENOSPC) {
                        break;
                } else if (rc < 0) {
@@ -326,7 +302,7 @@ static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
        struct llog_ctxt        *ctxt;
        int                      num_recs = 0;
        char                    *buf;
-       struct llog_rec_hdr      rec;
+       struct llog_rec_hdr     *rec;
 
        ENTRY;
 
@@ -353,7 +329,7 @@ static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
        cat_logid = cath->lgh_id;
 
        CWARN("4b: write 1 record into the catalog\n");
-       rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie, NULL);
+       rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
        if (rc != 1) {
                CERROR("4b: write 1 catalog record failed at: %d\n", rc);
                GOTO(out, rc);
@@ -381,7 +357,7 @@ static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
 
        CWARN("4d: write %d more log records\n", LLOG_TEST_RECNUM);
        for (i = 0; i < LLOG_TEST_RECNUM; i++) {
-               rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL, NULL);
+               rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
                if (rc) {
                        CERROR("4d: write %d records failed at #%d: %d\n",
                               LLOG_TEST_RECNUM, i + 1, rc);
@@ -396,15 +372,15 @@ static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
                GOTO(out, rc);
 
        CWARN("4e: add 5 large records, one record per block\n");
-       buflen = LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
-                sizeof(struct llog_rec_tail);
+       buflen = LLOG_CHUNK_SIZE;
        OBD_ALLOC(buf, buflen);
        if (buf == NULL)
                GOTO(out, rc = -ENOMEM);
        for (i = 0; i < 5; i++) {
-               rec.lrh_len = buflen;
-               rec.lrh_type = OBD_CFG_REC;
-               rc = llog_cat_add(env, cath, &rec, NULL, buf);
+               rec = (void *)buf;
+               rec->lrh_len = buflen;
+               rec->lrh_type = OBD_CFG_REC;
+               rc = llog_cat_add(env, cath, rec, NULL);
                if (rc) {
                        CERROR("4e: write 5 records failed at #%d: %d\n",
                               i + 1, rc);
@@ -542,7 +518,7 @@ static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
        cancel_count = 0;
        rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
        if (rc != -LLOG_EEMPTY) {
-               CERROR("5c: process with cat_cancel_cb failed: %d\n", rc);
+               CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
                GOTO(out, rc);
        }
 
@@ -559,7 +535,7 @@ static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
        }
 
        CWARN("5d: add 1 record to the log with many canceled empty pages\n");
-       rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL, NULL);
+       rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
        if (rc) {
                CERROR("5d: add record to the log with many canceled empty "
                       "pages failed\n");
@@ -582,7 +558,7 @@ static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
        plain_counter = 0;
        rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
        if (rc) {
-               CERROR("5f: reversely process with plain_print_cb failed:"
+               CERROR("5f: reversely process with plain_print_cb failed: "
                       "%d\n", rc);
                GOTO(out, rc);
        }
@@ -634,7 +610,7 @@ static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
                         NULL /* obd_connect_data */, NULL);
        if (rc != -EALREADY) {
                CERROR("6a: connect on connected MGC (%s) failed to return"
-                      " -EALREADY", mgc_obd->obd_name);
+                      " -EALREADY\n", mgc_obd->obd_name);
                if (rc == 0)
                        obd_disconnect(exp);
                GOTO(ctxt_release, rc = -EINVAL);
@@ -744,8 +720,7 @@ static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
                GOTO(out_close, rc);
        }
        for (i = 0; i < LLOG_BITMAP_SIZE(llh->lgh_hdr); i++) {
-               rc = llog_write(env, llh, &llog_records.lrh, NULL, 0,
-                               NULL, -1);
+               rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
                if (rc == -ENOSPC) {
                        break;
                } else if (rc < 0) {
@@ -784,7 +759,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);
        }
@@ -862,8 +837,9 @@ static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
        }
 
        CWARN("7e: test llog_changelog_rec\n");
+       /* Direct access to cr_do_not_use: peculiar case for this test */
        llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
-       llog_records.lcr.cr_tail.lrt_len = sizeof(llog_records.lcr);
+       llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
        llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
 
        rc = llog_test_7_sub(env, ctxt);
@@ -898,6 +874,206 @@ 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);
+       if (rc)
+               RETURN(rc);
+
+       CDEBUG(D_OTHER, "original size "LPU64"\n", la.la_size);
+       rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
+       if (la.la_size < rc) {
+               CERROR("too small llog: "LPU64"\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);
+       if (rc)
+               GOTO(stop, rc);
+
+       rc = dt_attr_set(env, o, &la, th);
+       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 test_8_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);
+               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);
+               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 test_8_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
  * ------------------------------------------------------------------------- */
@@ -942,6 +1118,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)
@@ -954,16 +1134,6 @@ cleanup_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)
 {
        struct obd_device       *tgt;
@@ -1067,11 +1237,8 @@ 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, true, NULL,
+                                  "llog_test", NULL);
 }
 
 static void __exit llog_test_exit(void)