1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2003 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
25 * A kernel module which tests the llog API from the OBD setup function.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_CLASS
33 #include <linux/module.h>
34 #include <linux/init.h>
36 #include <obd_class.h>
37 #include <lustre_log.h>
39 static int llog_test_rand;
40 static struct obd_uuid uuid = { .uuid = "test_uuid" };
41 static struct llog_logid cat_logid;
43 struct llog_mini_rec {
44 struct llog_rec_hdr lmr_hdr;
45 struct llog_rec_tail lmr_tail;
46 } __attribute__((packed));
48 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
54 for (i = 0; i < LLOG_BITMAP_BYTES * 8; i++) {
55 if (ext2_test_bit(i, llh->lgh_hdr->llh_bitmap)) {
61 if (active_recs != num_recs) {
62 CERROR("%s: expected %d active recs after write, found %d\n",
63 test, num_recs, active_recs);
67 if (llh->lgh_hdr->llh_count != num_recs) {
68 CERROR("%s: handle->count is %d, expected %d after write\n",
69 test, llh->lgh_hdr->llh_count, num_recs);
73 if (llh->lgh_last_idx < last_idx) {
74 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
75 test, llh->lgh_last_idx, last_idx);
82 /* Test named-log create/open, close */
83 static int llog_test_1(struct obd_device *obd, char *name)
85 struct llog_handle *llh;
86 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
91 CWARN("1a: create a log with name: %s\n", name);
94 rc = llog_create(ctxt, &llh, NULL, name);
96 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
100 llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
102 if ((rc = verify_handle("1", llh, 1)))
106 CWARN("1b: close newly-created log\n");
107 rc2 = llog_close(llh);
110 CERROR("1b: close log %s failed: %d\n", name, rc2);
117 /* Test named-log reopen; returns opened log on success */
118 static int llog_test_2(struct obd_device *obd, char *name,
119 struct llog_handle **llh)
121 struct llog_handle *loghandle;
122 struct llog_logid logid;
124 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
127 CWARN("2a: re-open a log with name: %s\n", name);
128 rc = llog_create(ctxt, llh, NULL, name);
130 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
133 llog_init_handle(*llh, LLOG_F_IS_PLAIN, &uuid);
135 if ((rc = verify_handle("2", *llh, 1)))
138 CWARN("2b: create a log without specified NAME & LOGID\n");
139 rc = llog_create(ctxt, &loghandle, NULL, NULL);
141 CERROR("2b: create log failed\n");
144 llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
145 logid = loghandle->lgh_id;
146 llog_close(loghandle);
148 CWARN("2b: re-open the log by LOGID\n");
149 rc = llog_create(ctxt, &loghandle, &logid, NULL);
151 CERROR("2b: re-open log by LOGID failed\n");
154 llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
156 CWARN("2b: destroy this log\n");
157 rc = llog_destroy(loghandle);
159 CERROR("2b: destroy log failed\n");
162 llog_free_handle(loghandle);
169 /* Test record writing, single and in bulk */
170 static int llog_test_3(struct obd_device *obd, struct llog_handle *llh)
172 struct llog_create_rec lcr;
174 int num_recs = 1; /* 1 for the header */
177 lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = sizeof(lcr);
178 lcr.lcr_hdr.lrh_type = OST_SZ_REC;
180 CWARN("3a: write one create_rec\n");
181 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
184 CERROR("3a: write one log record failed: %d\n", rc);
188 if ((rc = verify_handle("3a", llh, num_recs)))
191 CWARN("3b: write 10 cfg log records with 8 bytes bufs\n");
192 for (i = 0; i < 10; i++) {
193 struct llog_rec_hdr hdr;
196 hdr.lrh_type = OBD_CFG_REC;
197 memset(buf, 0, sizeof buf);
198 rc = llog_write_rec(llh, &hdr, NULL, 0, buf, -1);
200 CERROR("3b: write 10 records failed at #%d: %d\n",
205 if ((rc = verify_handle("3c", llh, num_recs)))
209 if ((rc = verify_handle("3b", llh, num_recs)))
212 CWARN("3c: write 1000 more log records\n");
213 for (i = 0; i < 1000; i++) {
214 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
216 CERROR("3c: write 1000 records failed at #%d: %d\n",
221 if ((rc = verify_handle("3b", llh, num_recs)))
225 if ((rc = verify_handle("3c", llh, num_recs)))
231 /* Test catalogue additions */
232 static int llog_test_4(struct obd_device *obd)
234 struct llog_handle *cath;
237 struct llog_mini_rec lmr;
238 struct llog_cookie cookie;
239 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
242 struct llog_rec_hdr rec;
246 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
247 lmr.lmr_hdr.lrh_type = 0xf00f00;
249 sprintf(name, "%x", llog_test_rand+1);
250 CWARN("4a: create a catalog log with name: %s\n", name);
251 rc = llog_create(ctxt, &cath, NULL, name);
253 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
256 llog_init_handle(cath, LLOG_F_IS_CAT, &uuid);
258 cat_logid = cath->lgh_id;
260 CWARN("4b: write 1 record into the catalog\n");
261 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, &cookie, NULL);
263 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
267 if ((rc = verify_handle("4b", cath, 2)))
268 GOTO(ctxt_release, rc);
270 if ((rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs)))
271 GOTO(ctxt_release, rc);
273 CWARN("4c: cancel 1 log record\n");
274 rc = llog_cat_cancel_records(cath, 1, &cookie);
276 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
281 if ((rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs)))
282 GOTO(ctxt_release, rc);
284 CWARN("4d: write 40,000 more log records\n");
285 for (i = 0; i < 40000; i++) {
286 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, NULL, NULL);
288 CERROR("4d: write 40000 records failed at #%d: %d\n",
295 CWARN("4e: add 5 large records, one record per block\n");
296 buflen = LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr)
297 - sizeof(struct llog_rec_tail);
298 OBD_ALLOC(buf, buflen);
300 GOTO(out, rc = -ENOMEM);
301 for (i = 0; i < 5; i++) {
302 rec.lrh_len = buflen;
303 rec.lrh_type = OBD_CFG_REC;
304 rc = llog_cat_add_rec(cath, &rec, NULL, buf);
306 CERROR("4e: write 5 records failed at #%d: %d\n",
308 OBD_FREE(buf, buflen);
313 OBD_FREE(buf, buflen);
316 CWARN("4f: put newly-created catalog\n");
317 rc = llog_cat_put(cath);
321 CERROR("1b: close log %s failed: %d\n", name, rc);
325 static int cat_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
328 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
330 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
331 CERROR("invalid record in catalog\n");
335 CWARN("seeing record at index %d - "LPX64":%x in log "LPX64"\n",
336 rec->lrh_index, lir->lid_id.lgl_oid,
337 lir->lid_id.lgl_ogen, llh->lgh_id.lgl_oid);
341 static int plain_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
344 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
345 CERROR("log is not plain\n");
349 CWARN("seeing record at index %d in log "LPX64"\n",
350 rec->lrh_index, llh->lgh_id.lgl_oid);
354 static int llog_cancel_rec_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
357 struct llog_cookie cookie;
360 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
361 CERROR("log is not plain\n");
365 cookie.lgc_lgl = llh->lgh_id;
366 cookie.lgc_index = rec->lrh_index;
368 llog_cat_cancel_records(llh->u.phd.phd_cat_handle, 1, &cookie);
375 /* Test log and catalogue processing */
376 static int llog_test_5(struct obd_device *obd)
378 struct llog_handle *llh = NULL;
381 struct llog_mini_rec lmr;
382 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
386 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
387 lmr.lmr_hdr.lrh_type = 0xf00f00;
389 CWARN("5a: re-open catalog by id\n");
390 rc = llog_create(ctxt, &llh, &cat_logid, NULL);
392 CERROR("5a: llog_create with logid failed: %d\n", rc);
395 llog_init_handle(llh, LLOG_F_IS_CAT, &uuid);
397 CWARN("5b: print the catalog entries.. we expect 2\n");
398 rc = llog_process(llh, cat_print_cb, "test 5", NULL);
400 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
404 CWARN("5c: Cancel 40000 records, see one log zapped\n");
405 rc = llog_cat_process(llh, llog_cancel_rec_cb, "foobar");
407 CERROR("5c: process with cat_cancel_cb failed: %d\n", rc);
411 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
412 rc = llog_cat_add_rec(llh, &lmr.lmr_hdr, NULL, NULL);
414 CERROR("5d: add record to the log with many canceled empty\
419 CWARN("5b: print the catalog entries.. we expect 1\n");
420 rc = llog_process(llh, cat_print_cb, "test 5", NULL);
422 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
426 CWARN("5e: print plain log entries.. expect 6\n");
427 rc = llog_cat_process(llh, plain_print_cb, "foobar");
429 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
433 CWARN("5f: print plain log entries reversely.. expect 6\n");
434 rc = llog_cat_reverse_process(llh, plain_print_cb, "foobar");
436 CERROR("5f: reversely process with plain_print_cb failed: %d\n", rc);
441 CWARN("5: close re-opened catalog\n");
443 rc = llog_cat_put(llh);
445 CERROR("1b: close log %s failed: %d\n", name, rc);
451 /* Test client api; open log by name and process */
452 static int llog_test_6(struct obd_device *obd, char *name)
454 struct obd_device *mdc_obd;
455 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
456 struct obd_uuid *mds_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
457 struct lustre_handle exph = {0, };
458 struct obd_export *exp;
459 struct obd_uuid uuid = {"LLOG_TEST6_UUID"};
460 struct llog_handle *llh = NULL;
461 struct llog_ctxt *nctxt;
464 CWARN("6a: re-open log %s using client API\n", name);
465 mdc_obd = class_find_client_obd(mds_uuid, LUSTRE_MDC_NAME, NULL);
466 if (mdc_obd == NULL) {
467 CERROR("6: no MDC devices connected to %s found.\n",
469 GOTO(ctxt_release, rc = -ENOENT);
472 rc = obd_connect(NULL, &exph, mdc_obd, &uuid,
473 NULL /* obd_connect_data */, NULL);
475 CERROR("6: failed to connect to MDC: %s\n", mdc_obd->obd_name);
476 GOTO(ctxt_release, rc);
478 exp = class_conn2export(&exph);
480 nctxt = llog_get_context(mdc_obd, LLOG_CONFIG_REPL_CTXT);
481 rc = llog_create(nctxt, &llh, NULL, name);
483 CERROR("6: llog_create failed %d\n", rc);
484 llog_ctxt_put(nctxt);
485 GOTO(ctxt_release, rc);
488 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
490 CERROR("6: llog_init_handle failed %d\n", rc);
494 rc = llog_process(llh, plain_print_cb, NULL, NULL);
496 CERROR("6: llog_process failed %d\n", rc);
498 rc = llog_reverse_process(llh, plain_print_cb, NULL, NULL);
500 CERROR("6: llog_reverse_process failed %d\n", rc);
503 rc = llog_close(llh);
504 llog_ctxt_put(nctxt);
506 CERROR("6: llog_close failed: rc = %d\n", rc);
508 rc = obd_disconnect(exp);
514 static int llog_test_7(struct obd_device *obd)
516 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
517 struct llog_handle *llh;
518 struct llog_create_rec lcr;
523 sprintf(name, "%x", llog_test_rand+2);
524 CWARN("7: create a log with name: %s\n", name);
527 rc = llog_create(ctxt, &llh, NULL, name);
529 CERROR("7: llog_create with name %s failed: %d\n", name, rc);
530 GOTO(ctxt_release, rc);
532 llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
534 lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = sizeof(lcr);
535 lcr.lcr_hdr.lrh_type = OST_SZ_REC;
536 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
538 CERROR("7: write one log record failed: %d\n", rc);
539 GOTO(ctxt_release, rc);
542 rc = llog_destroy(llh);
544 CERROR("7: llog_destroy failed: %d\n", rc);
546 llog_free_handle(llh);
552 /* -------------------------------------------------------------------------
553 * Tests above, boring obd functions below
554 * ------------------------------------------------------------------------- */
555 static int llog_run_tests(struct obd_device *obd)
557 struct llog_handle *llh;
558 struct lvfs_run_ctxt saved;
559 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
560 int rc, err, cleanup_phase = 0;
564 sprintf(name, "%x", llog_test_rand);
565 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
567 rc = llog_test_1(obd, name);
571 rc = llog_test_2(obd, name, &llh);
574 cleanup_phase = 1; /* close llh */
576 rc = llog_test_3(obd, llh);
580 rc = llog_test_4(obd);
584 rc = llog_test_5(obd);
588 rc = llog_test_6(obd, name);
592 rc = llog_test_7(obd);
597 switch (cleanup_phase) {
599 err = llog_close(llh);
601 CERROR("cleanup: llog_close failed: %d\n", err);
605 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
612 static int llog_test_llog_init(struct obd_device *obd, int group,
613 struct obd_device *tgt, int count,
614 struct llog_catid *logid, struct obd_uuid *uuid)
619 rc = llog_setup(obd, &obd->obd_olg, LLOG_TEST_ORIG_CTXT, tgt, 0, NULL,
624 static int llog_test_llog_finish(struct obd_device *obd, int count)
629 rc = llog_cleanup(llog_get_context(obd, LLOG_TEST_ORIG_CTXT));
633 static struct lprocfs_vars lprocfs_llog_test_obd_vars[] = { {0} };
634 static struct lprocfs_vars lprocfs_llog_test_module_vars[] = { {0} };
635 static void lprocfs_llog_test_init_vars(struct lprocfs_static_vars *lvars)
637 lvars->module_vars = lprocfs_llog_test_module_vars;
638 lvars->obd_vars = lprocfs_llog_test_obd_vars;
642 static int llog_test_cleanup(struct obd_device *obd)
644 int rc = obd_llog_finish(obd, 0);
646 CERROR("failed to llog_test_llog_finish: %d\n", rc);
648 lprocfs_obd_cleanup(obd);
653 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
655 struct lprocfs_static_vars lvars;
656 struct obd_device *tgt;
660 if (lcfg->lcfg_bufcount < 2) {
661 CERROR("requires a TARGET OBD name\n");
665 if (lcfg->lcfg_buflens[1] < 1) {
666 CERROR("requires a TARGET OBD name\n");
670 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
671 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
672 CERROR("target device not attached or not set up (%s)\n",
673 lustre_cfg_string(lcfg, 1));
677 rc = obd_llog_init(obd, OBD_LLOG_GROUP, tgt, 0, NULL, NULL);
681 llog_test_rand = ll_rand();
683 rc = llog_run_tests(obd);
685 llog_test_cleanup(obd);
687 lprocfs_llog_test_init_vars(&lvars);
688 lprocfs_obd_setup(obd, lvars.obd_vars);
693 static struct obd_ops llog_obd_ops = {
694 .o_owner = THIS_MODULE,
695 .o_setup = llog_test_setup,
696 .o_cleanup = llog_test_cleanup,
697 .o_llog_init = llog_test_llog_init,
698 .o_llog_finish = llog_test_llog_finish,
701 static int __init llog_test_init(void)
703 struct lprocfs_static_vars lvars;
705 lprocfs_llog_test_init_vars(&lvars);
706 return class_register_type(&llog_obd_ops, NULL,
707 lvars.module_vars,"llog_test", NULL);
710 static void __exit llog_test_exit(void)
712 class_unregister_type("llog_test");
715 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
716 MODULE_DESCRIPTION("llog test module");
717 MODULE_LICENSE("GPL");
719 module_init(llog_test_init);
720 module_exit(llog_test_exit);