1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog_test.c
38 * Author: Phil Schwan <phil@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_CLASS
46 #include <linux/module.h>
47 #include <linux/init.h>
49 #include <obd_class.h>
50 #include <lustre_log.h>
52 static int llog_test_rand;
53 static struct obd_uuid uuid = { .uuid = "test_uuid" };
54 static struct llog_logid cat_logid;
56 struct llog_mini_rec {
57 struct llog_rec_hdr lmr_hdr;
58 struct llog_rec_tail lmr_tail;
59 } __attribute__((packed));
61 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
67 for (i = 0; i < LLOG_BITMAP_BYTES * 8; i++) {
68 if (ext2_test_bit(i, llh->lgh_hdr->llh_bitmap)) {
74 if (active_recs != num_recs) {
75 CERROR("%s: expected %d active recs after write, found %d\n",
76 test, num_recs, active_recs);
80 if (llh->lgh_hdr->llh_count != num_recs) {
81 CERROR("%s: handle->count is %d, expected %d after write\n",
82 test, llh->lgh_hdr->llh_count, num_recs);
86 if (llh->lgh_last_idx < last_idx) {
87 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
88 test, llh->lgh_last_idx, last_idx);
95 /* Test named-log create/open, close */
96 static int llog_test_1(struct obd_device *obd, char *name)
98 struct llog_handle *llh;
99 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
104 CWARN("1a: create a log with name: %s\n", name);
107 rc = llog_create(ctxt, &llh, NULL, name);
109 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
113 llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
115 if ((rc = verify_handle("1", llh, 1)))
119 CWARN("1b: close newly-created log\n");
120 rc2 = llog_close(llh);
123 CERROR("1b: close log %s failed: %d\n", name, rc2);
130 /* Test named-log reopen; returns opened log on success */
131 static int llog_test_2(struct obd_device *obd, char *name,
132 struct llog_handle **llh)
134 struct llog_handle *loghandle;
135 struct llog_logid logid;
137 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
140 CWARN("2a: re-open a log with name: %s\n", name);
141 rc = llog_create(ctxt, llh, NULL, name);
143 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
146 llog_init_handle(*llh, LLOG_F_IS_PLAIN, &uuid);
148 if ((rc = verify_handle("2", *llh, 1)))
151 CWARN("2b: create a log without specified NAME & LOGID\n");
152 rc = llog_create(ctxt, &loghandle, NULL, NULL);
154 CERROR("2b: create log failed\n");
157 llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
158 logid = loghandle->lgh_id;
159 llog_close(loghandle);
161 CWARN("2b: re-open the log by LOGID\n");
162 rc = llog_create(ctxt, &loghandle, &logid, NULL);
164 CERROR("2b: re-open log by LOGID failed\n");
167 llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
169 CWARN("2b: destroy this log\n");
170 rc = llog_destroy(loghandle);
172 CERROR("2b: destroy log failed\n");
175 llog_free_handle(loghandle);
182 /* Test record writing, single and in bulk */
183 static int llog_test_3(struct obd_device *obd, struct llog_handle *llh)
185 struct llog_create_rec lcr;
187 int num_recs = 1; /* 1 for the header */
190 lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = sizeof(lcr);
191 lcr.lcr_hdr.lrh_type = OST_SZ_REC;
193 CWARN("3a: write one create_rec\n");
194 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
197 CERROR("3a: write one log record failed: %d\n", rc);
201 if ((rc = verify_handle("3a", llh, num_recs)))
204 CWARN("3b: write 10 cfg log records with 8 bytes bufs\n");
205 for (i = 0; i < 10; i++) {
206 struct llog_rec_hdr hdr;
209 hdr.lrh_type = OBD_CFG_REC;
210 memset(buf, 0, sizeof buf);
211 rc = llog_write_rec(llh, &hdr, NULL, 0, buf, -1);
213 CERROR("3b: write 10 records failed at #%d: %d\n",
218 if ((rc = verify_handle("3c", llh, num_recs)))
222 if ((rc = verify_handle("3b", llh, num_recs)))
225 CWARN("3c: write 1000 more log records\n");
226 for (i = 0; i < 1000; i++) {
227 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
229 CERROR("3c: write 1000 records failed at #%d: %d\n",
234 if ((rc = verify_handle("3b", llh, num_recs)))
238 if ((rc = verify_handle("3c", llh, num_recs)))
241 CWARN("3d: write log more than BITMAP_SIZE, return -ENOSPC\n");
242 for (i = 0; i < LLOG_BITMAP_SIZE(llh->lgh_hdr) + 1; i++) {
243 struct llog_rec_hdr hdr;
247 memset(buf_odd, 0, sizeof buf_odd);
248 memset(buf_even, 0, sizeof buf_even);
251 hdr.lrh_type = OBD_CFG_REC;
252 rc = llog_write_rec(llh, &hdr, NULL, 0, buf_even, -1);
255 hdr.lrh_type = OBD_CFG_REC;
256 rc = llog_write_rec(llh, &hdr, NULL, 0, buf_odd, -1);
262 CERROR("3c: write recs failed at #%d: %d\n",
270 CWARN("3d: write record more than BITMAP size!\n");
273 if ((rc = verify_handle("3d", llh, num_recs)))
279 /* Test catalogue additions */
280 static int llog_test_4(struct obd_device *obd)
282 struct llog_handle *cath;
285 struct llog_mini_rec lmr;
286 struct llog_cookie cookie;
287 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
290 struct llog_rec_hdr rec;
294 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
295 lmr.lmr_hdr.lrh_type = 0xf00f00;
297 sprintf(name, "%x", llog_test_rand+1);
298 CWARN("4a: create a catalog log with name: %s\n", name);
299 rc = llog_create(ctxt, &cath, NULL, name);
301 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
304 llog_init_handle(cath, LLOG_F_IS_CAT, &uuid);
306 cat_logid = cath->lgh_id;
308 CWARN("4b: write 1 record into the catalog\n");
309 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, &cookie, NULL);
311 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
315 if ((rc = verify_handle("4b", cath, 2)))
316 GOTO(ctxt_release, rc);
318 if ((rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs)))
319 GOTO(ctxt_release, rc);
321 CWARN("4c: cancel 1 log record\n");
322 rc = llog_cat_cancel_records(cath, 1, &cookie);
324 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
329 if ((rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs)))
330 GOTO(ctxt_release, rc);
332 CWARN("4d: write 40,000 more log records\n");
333 for (i = 0; i < 40000; i++) {
334 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, NULL, NULL);
336 CERROR("4d: write 40000 records failed at #%d: %d\n",
343 CWARN("4e: add 5 large records, one record per block\n");
344 buflen = LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr)
345 - sizeof(struct llog_rec_tail);
346 OBD_ALLOC(buf, buflen);
348 GOTO(out, rc = -ENOMEM);
349 for (i = 0; i < 5; i++) {
350 rec.lrh_len = buflen;
351 rec.lrh_type = OBD_CFG_REC;
352 rc = llog_cat_add_rec(cath, &rec, NULL, buf);
354 CERROR("4e: write 5 records failed at #%d: %d\n",
356 OBD_FREE(buf, buflen);
361 OBD_FREE(buf, buflen);
364 CWARN("4f: put newly-created catalog\n");
365 rc = llog_cat_put(cath);
369 CERROR("1b: close log %s failed: %d\n", name, rc);
373 static int cat_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
376 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
378 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
379 CERROR("invalid record in catalog\n");
383 CWARN("seeing record at index %d - "LPX64":%x in log "LPX64"\n",
384 rec->lrh_index, lir->lid_id.lgl_oid,
385 lir->lid_id.lgl_ogen, llh->lgh_id.lgl_oid);
389 static int plain_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
392 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
393 CERROR("log is not plain\n");
397 CWARN("seeing record at index %d in log "LPX64"\n",
398 rec->lrh_index, llh->lgh_id.lgl_oid);
402 static int llog_cancel_rec_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
405 struct llog_cookie cookie;
408 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
409 CERROR("log is not plain\n");
413 cookie.lgc_lgl = llh->lgh_id;
414 cookie.lgc_index = rec->lrh_index;
416 llog_cat_cancel_records(llh->u.phd.phd_cat_handle, 1, &cookie);
423 /* Test log and catalogue processing */
424 static int llog_test_5(struct obd_device *obd)
426 struct llog_handle *llh = NULL;
429 struct llog_mini_rec lmr;
430 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
434 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
435 lmr.lmr_hdr.lrh_type = 0xf00f00;
437 CWARN("5a: re-open catalog by id\n");
438 rc = llog_create(ctxt, &llh, &cat_logid, NULL);
440 CERROR("5a: llog_create with logid failed: %d\n", rc);
443 llog_init_handle(llh, LLOG_F_IS_CAT, &uuid);
445 CWARN("5b: print the catalog entries.. we expect 2\n");
446 rc = llog_process(llh, cat_print_cb, "test 5", NULL);
448 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
452 CWARN("5c: Cancel 40000 records, see one log zapped\n");
453 rc = llog_cat_process(llh, llog_cancel_rec_cb, "foobar");
455 CERROR("5c: process with cat_cancel_cb failed: %d\n", rc);
459 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
460 rc = llog_cat_add_rec(llh, &lmr.lmr_hdr, NULL, NULL);
462 CERROR("5d: add record to the log with many canceled empty\
467 CWARN("5b: print the catalog entries.. we expect 1\n");
468 rc = llog_process(llh, cat_print_cb, "test 5", NULL);
470 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
474 CWARN("5e: print plain log entries.. expect 6\n");
475 rc = llog_cat_process(llh, plain_print_cb, "foobar");
477 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
481 CWARN("5f: print plain log entries reversely.. expect 6\n");
482 rc = llog_cat_reverse_process(llh, plain_print_cb, "foobar");
484 CERROR("5f: reversely process with plain_print_cb failed: %d\n", rc);
489 CWARN("5: close re-opened catalog\n");
491 rc = llog_cat_put(llh);
493 CERROR("1b: close log %s failed: %d\n", name, rc);
499 /* Test client api; open log by name and process */
500 static int llog_test_6(struct obd_device *obd, char *name)
502 struct obd_device *mdc_obd;
503 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
504 struct obd_uuid *mds_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
505 struct lustre_handle exph = {0, };
506 struct obd_export *exp;
507 struct obd_uuid uuid = {"LLOG_TEST6_UUID"};
508 struct llog_handle *llh = NULL;
509 struct llog_ctxt *nctxt;
512 CWARN("6a: re-open log %s using client API\n", name);
513 mdc_obd = class_find_client_obd(mds_uuid, LUSTRE_MDC_NAME, NULL);
514 if (mdc_obd == NULL) {
515 CERROR("6: no MDC devices connected to %s found.\n",
517 GOTO(ctxt_release, rc = -ENOENT);
520 rc = obd_connect(&exph, mdc_obd, &uuid, NULL /* obd_connect_data */,
523 CERROR("6: failed to connect to MDC: %s\n", mdc_obd->obd_name);
524 GOTO(ctxt_release, rc);
526 exp = class_conn2export(&exph);
528 nctxt = llog_get_context(mdc_obd, LLOG_CONFIG_REPL_CTXT);
529 rc = llog_create(nctxt, &llh, NULL, name);
531 CERROR("6: llog_create failed %d\n", rc);
532 llog_ctxt_put(nctxt);
533 GOTO(ctxt_release, rc);
536 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
538 CERROR("6: llog_init_handle failed %d\n", rc);
542 rc = llog_process(llh, plain_print_cb, NULL, NULL);
544 CERROR("6: llog_process failed %d\n", rc);
546 rc = llog_reverse_process(llh, plain_print_cb, NULL, NULL);
548 CERROR("6: llog_reverse_process failed %d\n", rc);
551 rc = llog_close(llh);
552 llog_ctxt_put(nctxt);
554 CERROR("6: llog_close failed: rc = %d\n", rc);
556 rc = obd_disconnect(exp);
562 static int llog_test_7(struct obd_device *obd)
564 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
565 struct llog_handle *llh;
566 struct llog_create_rec lcr;
571 sprintf(name, "%x", llog_test_rand+2);
572 CWARN("7: create a log with name: %s\n", name);
575 rc = llog_create(ctxt, &llh, NULL, name);
577 CERROR("7: llog_create with name %s failed: %d\n", name, rc);
578 GOTO(ctxt_release, rc);
580 llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
582 lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = sizeof(lcr);
583 lcr.lcr_hdr.lrh_type = OST_SZ_REC;
584 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
586 CERROR("7: write one log record failed: %d\n", rc);
587 GOTO(ctxt_release, rc);
590 rc = llog_destroy(llh);
592 CERROR("7: llog_destroy failed: %d\n", rc);
594 llog_free_handle(llh);
600 /* -------------------------------------------------------------------------
601 * Tests above, boring obd functions below
602 * ------------------------------------------------------------------------- */
603 static int llog_run_tests(struct obd_device *obd)
605 struct llog_handle *llh;
606 struct lvfs_run_ctxt saved;
607 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
608 int rc, err, cleanup_phase = 0;
612 sprintf(name, "%x", llog_test_rand);
613 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
615 rc = llog_test_1(obd, name);
619 rc = llog_test_2(obd, name, &llh);
622 cleanup_phase = 1; /* close llh */
624 rc = llog_test_3(obd, llh);
628 rc = llog_test_4(obd);
632 rc = llog_test_5(obd);
636 rc = llog_test_6(obd, name);
640 rc = llog_test_7(obd);
645 switch (cleanup_phase) {
647 err = llog_close(llh);
649 CERROR("cleanup: llog_close failed: %d\n", err);
653 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
660 static int llog_test_llog_init(struct obd_device *obd, struct obd_device *tgt,
661 int count, struct llog_catid *logid,
662 struct obd_uuid *uuid)
667 rc = llog_setup(obd, LLOG_TEST_ORIG_CTXT, tgt, 0, NULL, &llog_lvfs_ops);
671 static int llog_test_llog_finish(struct obd_device *obd, int count)
676 rc = llog_cleanup(llog_get_context(obd, LLOG_TEST_ORIG_CTXT));
680 static int llog_test_cleanup(struct obd_device *obd)
682 int rc = obd_llog_finish(obd, 0);
684 CERROR("failed to llog_test_llog_finish: %d\n", rc);
686 lprocfs_obd_cleanup(obd);
692 static struct lprocfs_vars lprocfs_llog_test_obd_vars[] = { {0} };
693 static struct lprocfs_vars lprocfs_llog_test_module_vars[] = { {0} };
694 static void lprocfs_llog_test_init_vars(struct lprocfs_static_vars *lvars)
696 lvars->module_vars = lprocfs_llog_test_module_vars;
697 lvars->obd_vars = lprocfs_llog_test_obd_vars;
701 static int llog_test_setup(struct obd_device *obd, obd_count len, void *buf)
703 struct lprocfs_static_vars lvars;
704 struct lustre_cfg *lcfg = buf;
705 struct obd_device *tgt;
709 if (lcfg->lcfg_bufcount < 2) {
710 CERROR("requires a TARGET OBD name\n");
714 if (lcfg->lcfg_buflens[1] < 1) {
715 CERROR("requires a TARGET OBD name\n");
719 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
720 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
721 CERROR("target device not attached or not set up (%s)\n",
722 lustre_cfg_string(lcfg, 1));
726 rc = obd_llog_init(obd, tgt, 0, NULL, NULL);
730 llog_test_rand = ll_rand();
732 rc = llog_run_tests(obd);
734 llog_test_cleanup(obd);
736 lprocfs_llog_test_init_vars(&lvars);
737 lprocfs_obd_setup(obd, lvars.obd_vars);
742 static struct obd_ops llog_obd_ops = {
743 .o_owner = THIS_MODULE,
744 .o_setup = llog_test_setup,
745 .o_cleanup = llog_test_cleanup,
746 .o_llog_init = llog_test_llog_init,
747 .o_llog_finish = llog_test_llog_finish,
750 static int __init llog_test_init(void)
752 struct lprocfs_static_vars lvars;
754 lprocfs_llog_test_init_vars(&lvars);
755 return class_register_type(&llog_obd_ops,lvars.module_vars,"llog_test");
758 static void __exit llog_test_exit(void)
760 class_unregister_type("llog_test");
763 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
764 MODULE_DESCRIPTION("llog test module");
765 MODULE_LICENSE("GPL");
767 module_init(llog_test_init);
768 module_exit(llog_test_exit);