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 Lustre, http://www.lustre.org/
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * A kernel module which tests the llog API from the OBD setup function.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_CLASS
30 #include <linux/module.h>
31 #include <linux/init.h>
33 #include <linux/obd_class.h>
34 #include <linux/lustre_log.h>
35 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
37 static int llog_test_rand;
38 static struct obd_uuid uuid = { .uuid = "test_uuid" };
39 static struct llog_logid cat_logid;
41 struct llog_mini_rec {
42 struct llog_rec_hdr lmr_hdr;
43 struct llog_rec_tail lmr_tail;
44 } __attribute__((packed));
46 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
52 for (i = 0; i < LLOG_BITMAP_BYTES * 8; i++) {
53 if (ext2_test_bit(i, llh->lgh_hdr->llh_bitmap)) {
59 if (active_recs != num_recs) {
60 CERROR("%s: expected %d active recs after write, found %d\n",
61 test, num_recs, active_recs);
65 if (llh->lgh_hdr->llh_count != num_recs) {
66 CERROR("%s: handle->count is %d, expected %d after write\n",
67 test, llh->lgh_hdr->llh_count, num_recs);
71 if (llh->lgh_last_idx < last_idx) {
72 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
73 test, llh->lgh_last_idx, last_idx);
80 /* Test named-log create/open, close */
81 static int llog_test_1(struct obd_device *obd, char *name)
83 struct llog_handle *llh;
84 struct llog_ctxt *ctxt;
89 CWARN("1a: create a log with name: %s\n", name);
90 ctxt = llog_get_context(&obd->obd_llogs, LLOG_TEST_ORIG_CTXT);
93 rc = llog_open(ctxt, &llh, NULL, name, OBD_LLOG_FL_CREATE);
95 CERROR("1a: llog_open with name %s failed: %d\n", name, rc);
98 llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
100 if ((rc = verify_handle("1", llh, 1)))
104 CWARN("1b: close newly-created log\n");
105 rc2 = llog_close(llh);
107 CERROR("1b: close log %s failed: %d\n", name, rc2);
114 /* Test named-log reopen; returns opened log on success */
115 static int llog_test_2(struct obd_device *obd, char *name,
116 struct llog_handle **llh)
118 struct llog_handle *loghandle;
119 struct llog_logid logid;
121 struct llog_ctxt *ctxt;
124 ctxt = llog_get_context(&obd->obd_llogs, LLOG_TEST_ORIG_CTXT);
125 CWARN("2a: re-open a log with name: %s\n", name);
126 rc = llog_open(ctxt, llh, NULL, name, 0);
128 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
131 llog_init_handle(*llh, LLOG_F_IS_PLAIN, &uuid);
133 if ((rc = verify_handle("2", *llh, 1)))
136 CWARN("2b: create a log without specified NAME & LOGID\n");
137 rc = llog_open(ctxt, &loghandle, NULL, NULL, OBD_LLOG_FL_CREATE);
139 CERROR("2b: create log failed\n");
142 llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
143 logid = loghandle->lgh_id;
144 llog_close(loghandle);
146 CWARN("2b: re-open the log by LOGID\n");
147 rc = llog_open(ctxt, &loghandle, &logid, NULL, 0);
149 CERROR("2b: re-open log by LOGID failed\n");
152 llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
154 CWARN("2b: destroy this log\n");
155 rc = llog_destroy(loghandle);
157 CERROR("2b: destroy log failed\n");
160 llog_free_handle(loghandle);
165 /* Test record writing, single and in bulk */
166 static int llog_test_3(struct obd_device *obd, struct llog_handle *llh)
168 struct llog_create_rec lcr;
170 int num_recs = 1; /* 1 for the header */
173 lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = sizeof(lcr);
174 lcr.lcr_hdr.lrh_type = OST_SZ_REC;
176 CWARN("3a: write one create_rec\n");
177 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
180 CERROR("3a: write one log record failed: %d\n", rc);
184 if ((rc = verify_handle("3a", llh, num_recs)))
187 CWARN("3b: write 10 cfg log records with 8 bytes bufs\n");
188 for (i = 0; i < 10; i++) {
189 struct llog_rec_hdr hdr;
192 hdr.lrh_type = OBD_CFG_REC;
193 memset(buf, 0, sizeof buf);
194 rc = llog_write_rec(llh, &hdr, NULL, 0, buf, -1);
196 CERROR("3b: write 10 records failed at #%d: %d\n",
201 if ((rc = verify_handle("3c", llh, num_recs)))
205 if ((rc = verify_handle("3b", llh, num_recs)))
208 CWARN("3c: write 1000 more log records\n");
209 for (i = 0; i < 1000; i++) {
210 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
212 CERROR("3c: write 1000 records failed at #%d: %d\n",
217 if ((rc = verify_handle("3b", llh, num_recs)))
221 if ((rc = verify_handle("3c", llh, num_recs)))
227 /* Test catalogue additions */
228 static int llog_test_4(struct obd_device *obd)
230 struct llog_handle *cath;
233 struct llog_mini_rec lmr;
234 struct llog_cookie cookie;
235 struct llog_ctxt *ctxt;
238 struct llog_rec_hdr rec;
241 memset(&cookie, 0, sizeof(cookie));
242 ctxt = llog_get_context(&obd->obd_llogs, LLOG_TEST_ORIG_CTXT);
243 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
244 lmr.lmr_hdr.lrh_type = 0xf00f00;
246 sprintf(name, "%x", llog_test_rand+1);
247 CWARN("4a: create a catalog log with name: %s\n", name);
248 rc = llog_open(ctxt, &cath, NULL, name, OBD_LLOG_FL_CREATE);
250 CERROR("1a: llog_open with name %s failed: %d\n", name, rc);
253 llog_init_handle(cath, LLOG_F_IS_CAT, &uuid);
255 cat_logid = cath->lgh_id;
257 CWARN("4b: write 1 record into the catalog\n");
258 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, &cookie, NULL, NULL, NULL);
260 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
264 if ((rc = verify_handle("4b", cath, 2)))
267 if ((rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs)))
270 CWARN("4c: cancel 1 log record\n");
271 rc = llog_cat_cancel_records(cath, 1, &cookie);
273 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
278 if ((rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs)))
281 CWARN("4d: write 40,000 more log records\n");
282 for (i = 0; i < 40000; i++) {
283 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, NULL, NULL, NULL, NULL);
285 CERROR("4d: write 40000 records failed at #%d: %d\n",
292 CWARN("4e: add 5 large records, one record per block\n");
293 buflen = LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr)
294 - sizeof(struct llog_rec_tail);
295 OBD_ALLOC(buf, buflen);
297 GOTO(out, rc = -ENOMEM);
298 for (i = 0; i < 5; i++) {
299 rec.lrh_len = buflen;
300 rec.lrh_type = OBD_CFG_REC;
301 rc = llog_cat_add_rec(cath, &rec, NULL, buf, NULL, NULL);
303 CERROR("4e: write 5 records failed at #%d: %d\n",
305 OBD_FREE(buf, buflen);
310 OBD_FREE(buf, buflen);
313 CWARN("4f: put newly-created catalog\n");
314 rc = llog_cat_put(cath);
316 CERROR("1b: close log %s failed: %d\n", name, rc);
320 static int cat_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
323 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
325 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
326 CERROR("invalid record in catalog\n");
330 CWARN("seeing record at index %d - "LPX64":%x in log "LPX64"\n",
331 rec->lrh_index, lir->lid_id.lgl_oid,
332 lir->lid_id.lgl_ogen, llh->lgh_id.lgl_oid);
336 static int plain_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
339 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
340 CERROR("log is not plain\n");
344 CWARN("seeing record at index %d in log "LPX64"\n",
345 rec->lrh_index, llh->lgh_id.lgl_oid);
349 static int llog_cancel_rec_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
352 struct llog_cookie cookie;
355 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
356 CERROR("log is not plain\n");
360 cookie.lgc_lgl = llh->lgh_id;
361 cookie.lgc_index = rec->lrh_index;
363 llog_cat_cancel_records(llh->u.phd.phd_cat_handle, 1, &cookie);
370 /* Test log and catalogue processing */
371 static int llog_test_5(struct obd_device *obd)
373 struct llog_handle *llh = NULL;
376 struct llog_mini_rec lmr;
377 struct llog_ctxt *ctxt;
380 ctxt = llog_get_context(&obd->obd_llogs, LLOG_TEST_ORIG_CTXT);
381 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
382 lmr.lmr_hdr.lrh_type = 0xf00f00;
384 CWARN("5a: re-open catalog by id\n");
385 rc = llog_open(ctxt, &llh, &cat_logid, NULL, 0);
387 CERROR("5a: llog_open with logid failed: %d\n", rc);
390 llog_init_handle(llh, LLOG_F_IS_CAT, &uuid);
392 CWARN("5b: print the catalog entries.. we expect 2\n");
393 rc = llog_process(llh, (llog_cb_t)cat_print_cb, "test 5", NULL);
395 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
399 CWARN("5c: Cancel 40000 records, see one log zapped\n");
400 rc = llog_cat_process(llh, llog_cancel_rec_cb, "foobar");
402 CERROR("5c: process with cat_cancel_cb failed: %d\n", rc);
406 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
407 rc = llog_cat_add_rec(llh, &lmr.lmr_hdr, NULL, NULL, NULL, NULL);
409 CERROR("5d: add record to the log with many canceled empty\
414 CWARN("5b: print the catalog entries.. we expect 1\n");
415 rc = llog_process(llh, (llog_cb_t)cat_print_cb, "test 5", NULL);
417 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
421 CWARN("5e: print plain log entries.. expect 6\n");
422 rc = llog_cat_process(llh, plain_print_cb, "foobar");
424 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
428 CWARN("5f: print plain log entries reversely.. expect 6\n");
429 rc = llog_cat_reverse_process(llh, plain_print_cb, "foobar");
431 CERROR("5f: reversely process with plain_print_cb failed: %d\n",
437 CWARN("5: close re-opened catalog\n");
439 rc = llog_cat_put(llh);
441 CERROR("1b: close log %s failed: %d\n", name, rc);
445 /* Test client api; open log by name and process */
446 static int llog_test_6(struct obd_device *obd, char *name)
448 struct obd_device *mdc_obd;
449 struct llog_ctxt *ctxt;
450 struct obd_uuid *mds_uuid;
451 struct lustre_handle exph = {0, };
452 struct obd_export *exp;
453 struct obd_uuid uuid = {"LLOG_TEST6_UUID"};
454 struct llog_handle *llh = NULL;
455 struct llog_ctxt *nctxt;
458 ctxt = llog_get_context(&obd->obd_llogs, LLOG_TEST_ORIG_CTXT);
459 mds_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
461 CWARN("6a: re-open log %s using client API\n", name);
462 mdc_obd = class_find_client_obd(mds_uuid, OBD_MDC_DEVICENAME, NULL);
463 if (mdc_obd == NULL) {
464 CERROR("6: no MDC devices connected to %s found.\n",
469 rc = obd_connect(&exph, mdc_obd, &uuid, NULL, 0);
471 CERROR("6: failed to connect to MDC: %s\n", mdc_obd->obd_name);
474 exp = class_conn2export(&exph);
476 nctxt = llog_get_context(&mdc_obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
477 rc = llog_open(nctxt, &llh, NULL, name, 0);
479 CERROR("6: llog_open failed %d\n", rc);
483 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
485 CERROR("6: llog_init_handle failed %d\n", rc);
489 rc = llog_process(llh, (llog_cb_t)plain_print_cb, NULL, NULL);
491 CERROR("6: llog_process failed %d\n", rc);
493 rc = llog_reverse_process(llh, (llog_cb_t)plain_print_cb, NULL, NULL);
495 CERROR("6: llog_reverse_process failed %d\n", rc);
498 rc = llog_close(llh);
500 CERROR("6: llog_close failed: rc = %d\n", rc);
503 rc = obd_disconnect(exp, 0);
508 static int llog_test_7(struct obd_device *obd)
510 struct llog_ctxt *ctxt;
511 struct llog_handle *llh;
512 struct llog_create_rec lcr;
517 ctxt = llog_get_context(&obd->obd_llogs, LLOG_TEST_ORIG_CTXT);
518 sprintf(name, "%x", llog_test_rand+2);
519 CWARN("7: create a log with name: %s\n", name);
522 rc = llog_open(ctxt, &llh, NULL, name, OBD_LLOG_FL_CREATE);
524 CERROR("7: llog_open with name %s failed: %d\n", name, rc);
527 llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
529 lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = cpu_to_le32(sizeof(lcr));
530 lcr.lcr_hdr.lrh_type = cpu_to_le32(OST_SZ_REC);
531 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
533 CERROR("7: write one log record failed: %d\n", rc);
537 rc = llog_destroy(llh);
539 CERROR("7: llog_destroy failed: %d\n", rc);
541 llog_free_handle(llh);
545 /* -------------------------------------------------------------------------
546 * Tests above, boring obd functions below
547 * ------------------------------------------------------------------------- */
548 static int llog_run_tests(struct obd_device *obd)
550 struct llog_ctxt *ctxt;
551 struct llog_handle *llh;
552 struct lvfs_run_ctxt saved;
553 int rc, err, cleanup_phase = 0;
557 ctxt = llog_get_context(&obd->obd_llogs, LLOG_TEST_ORIG_CTXT);
558 sprintf(name, "%x", llog_test_rand);
559 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
561 rc = llog_test_1(obd, name);
565 rc = llog_test_2(obd, name, &llh);
568 cleanup_phase = 1; /* close llh */
570 rc = llog_test_3(obd, llh);
574 rc = llog_test_4(obd);
578 rc = llog_test_5(obd);
582 rc = llog_test_6(obd, name);
586 rc = llog_test_7(obd);
591 switch (cleanup_phase) {
593 err = llog_close(llh);
595 CERROR("cleanup: llog_close failed: %d\n", err);
599 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
606 static int llog_test_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
607 struct obd_device *tgt, int count,
608 struct llog_catid *logid)
613 rc = obd_llog_setup(obd, llogs, LLOG_TEST_ORIG_CTXT, tgt, 0, NULL,
618 static int llog_test_llog_finish(struct obd_device *obd,
619 struct obd_llogs *llogs, int count)
624 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_TEST_ORIG_CTXT));
628 static int llog_test_cleanup(struct obd_device *obd, int flags)
630 int rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
632 CERROR("failed to llog_test_llog_finish: %d\n", rc);
637 static int llog_test_setup(struct obd_device *obd, obd_count len, void *buf)
639 struct lustre_cfg *lcfg = buf;
640 struct obd_device *tgt;
644 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
645 CERROR("requires a TARGET OBD name\n");
649 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
650 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
651 CERROR("target device not attached or not set up (%s)\n",
652 lustre_cfg_string(lcfg, 1));
656 rc = obd_llog_init(obd, &obd->obd_llogs, tgt, 0, NULL);
660 llog_test_rand = ll_insecure_random_int();
662 rc = llog_run_tests(obd);
664 llog_test_cleanup(obd, 0);
669 static int llog_test_attach(struct obd_device *dev, obd_count len, void *data)
671 struct lprocfs_static_vars lvars;
673 lprocfs_init_vars(llog_test, &lvars);
674 return lprocfs_obd_attach(dev, lvars.obd_vars);
677 static int llog_test_detach(struct obd_device *dev)
679 return lprocfs_obd_detach(dev);
682 static struct obd_ops llog_obd_ops = {
683 .o_owner = THIS_MODULE,
684 .o_attach = llog_test_attach,
685 .o_detach = llog_test_detach,
686 .o_setup = llog_test_setup,
687 .o_cleanup = llog_test_cleanup,
688 .o_llog_init = llog_test_llog_init,
689 .o_llog_finish = llog_test_llog_finish,
692 static struct lprocfs_vars lprocfs_obd_vars[] = { {0} };
693 static struct lprocfs_vars lprocfs_module_vars[] = { {0} };
694 LPROCFS_INIT_VARS(llog_test, lprocfs_module_vars, lprocfs_obd_vars)
696 static int __init llog_test_init(void)
698 struct lprocfs_static_vars lvars;
700 lprocfs_init_vars(llog_test, &lvars);
702 return class_register_type(&llog_obd_ops, NULL,
707 static void __exit llog_test_exit(void)
709 class_unregister_type("llog_test");
712 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
713 MODULE_DESCRIPTION("llog test module");
714 MODULE_LICENSE("GPL");
716 module_init(llog_test_init);
717 module_exit(llog_test_exit);