4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
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>
39 * Author: Mikhail Pershin <mike.pershin@intel.com>
42 #define DEBUG_SUBSYSTEM S_CLASS
44 #include <linux/module.h>
45 #include <linux/init.h>
47 #include <obd_class.h>
48 #include <lustre_fid.h>
49 #include <lustre_log.h>
51 /* This is slightly more than the number of records that can fit into a
52 * single llog file, because the llog_log_header takes up some of the
53 * space in the first block that cannot be used for the bitmap. */
54 #define LLOG_TEST_RECNUM (LLOG_CHUNK_SIZE * 8)
56 static int llog_test_rand;
57 static struct obd_uuid uuid = { .uuid = "test_uuid" };
58 static struct llog_logid cat_logid;
60 struct llog_mini_rec {
61 struct llog_rec_hdr lmr_hdr;
62 struct llog_rec_tail lmr_tail;
63 } __attribute__((packed));
65 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
71 for (i = 0; i < LLOG_BITMAP_BYTES * 8; i++) {
72 if (ext2_test_bit(i, llh->lgh_hdr->llh_bitmap)) {
78 if (active_recs != num_recs) {
79 CERROR("%s: expected %d active recs after write, found %d\n",
80 test, num_recs, active_recs);
84 if (llh->lgh_hdr->llh_count != num_recs) {
85 CERROR("%s: handle->count is %d, expected %d after write\n",
86 test, llh->lgh_hdr->llh_count, num_recs);
90 if (llh->lgh_last_idx < last_idx) {
91 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
92 test, llh->lgh_last_idx, last_idx);
99 /* Test named-log create/open, close */
100 static int llog_test_1(const struct lu_env *env,
101 struct obd_device *obd, char *name)
103 struct llog_handle *llh;
104 struct llog_ctxt *ctxt;
110 CWARN("1a: create a log with name: %s\n", name);
111 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
114 rc = llog_open_create(env, ctxt, &llh, NULL, name);
116 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
119 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, &uuid);
121 CERROR("1a: can't init llog handle: %d\n", rc);
125 rc = verify_handle("1", llh, 1);
127 CWARN("1b: close newly-created log\n");
129 rc2 = llog_close(env, llh);
131 CERROR("1b: close log %s failed: %d\n", name, rc2);
140 /* Test named-log reopen; returns opened log on success */
141 static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
142 char *name, struct llog_handle **llh)
144 struct llog_ctxt *ctxt;
145 struct llog_handle *loghandle;
146 struct llog_logid logid;
151 CWARN("2a: re-open a log with name: %s\n", name);
152 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
155 rc = llog_open(env, ctxt, llh, NULL, name, LLOG_OPEN_EXISTS);
157 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
161 rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
163 CERROR("2a: can't init llog handle: %d\n", rc);
164 GOTO(out_close_llh, rc);
167 rc = verify_handle("2", *llh, 1);
169 GOTO(out_close_llh, rc);
171 /* XXX: there is known issue with tests 2b, MGS is not able to create
172 * anonymous llog, exit now to allow following tests run.
173 * It is fixed in upcoming llog over OSD code */
176 CWARN("2b: create a log without specified NAME & LOGID\n");
177 rc = llog_open_create(env, ctxt, &loghandle, NULL, NULL);
179 CERROR("2b: create log failed\n");
180 GOTO(out_close_llh, rc);
182 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, &uuid);
184 CERROR("2b: can't init llog handle: %d\n", rc);
188 logid = loghandle->lgh_id;
189 llog_close(env, loghandle);
191 CWARN("2c: re-open the log by LOGID\n");
192 rc = llog_open(env, ctxt, &loghandle, &logid, NULL, LLOG_OPEN_EXISTS);
194 CERROR("2c: re-open log by LOGID failed\n");
195 GOTO(out_close_llh, rc);
198 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, &uuid);
200 CERROR("2c: can't init llog handle: %d\n", rc);
204 CWARN("2b: destroy this log\n");
205 rc = llog_destroy(env, loghandle);
207 CERROR("2d: destroy log failed\n");
209 llog_close(env, loghandle);
212 llog_close(env, *llh);
219 /* Test record writing, single and in bulk */
220 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
221 struct llog_handle *llh)
223 struct llog_gen_rec lgr;
225 int num_recs = 1; /* 1 for the header */
229 lgr.lgr_hdr.lrh_len = lgr.lgr_tail.lrt_len = sizeof(lgr);
230 lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
232 CWARN("3a: write one create_rec\n");
233 rc = llog_write(env, llh, &lgr.lgr_hdr, NULL, 0, NULL, -1);
236 CERROR("3a: write one log record failed: %d\n", rc);
240 rc = verify_handle("3a", llh, num_recs);
244 CWARN("3b: write 10 cfg log records with 8 bytes bufs\n");
245 for (i = 0; i < 10; i++) {
246 struct llog_rec_hdr hdr;
250 hdr.lrh_type = OBD_CFG_REC;
251 memset(buf, 0, sizeof buf);
252 rc = llog_write(env, llh, &hdr, NULL, 0, buf, -1);
254 CERROR("3b: write 10 records failed at #%d: %d\n",
261 rc = verify_handle("3b", llh, num_recs);
265 CWARN("3c: write 1000 more log records\n");
266 for (i = 0; i < 1000; i++) {
267 rc = llog_write(env, llh, &lgr.lgr_hdr, NULL, 0, NULL, -1);
269 CERROR("3c: write 1000 records failed at #%d: %d\n",
276 rc = verify_handle("3c", llh, num_recs);
280 CWARN("3d: write log more than BITMAP_SIZE, return -ENOSPC\n");
281 for (i = 0; i < LLOG_BITMAP_SIZE(llh->lgh_hdr) + 1; i++) {
282 struct llog_rec_hdr hdr;
286 memset(buf_odd, 0, sizeof buf_odd);
287 memset(buf_even, 0, sizeof buf_even);
290 hdr.lrh_type = OBD_CFG_REC;
291 rc = llog_write(env, llh, &hdr, NULL, 0, buf_even, -1);
294 hdr.lrh_type = OBD_CFG_REC;
295 rc = llog_write(env, llh, &hdr, NULL, 0, buf_odd, -1);
300 CERROR("3d: write recs failed at #%d: %d\n",
307 CWARN("3d: write record more than BITMAP size!\n");
310 CWARN("3d: wrote %d more records before end of llog is reached\n",
313 rc = verify_handle("3d", llh, num_recs);
318 /* Test catalogue additions */
319 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
321 struct llog_handle *cath;
323 int rc, rc2, i, buflen;
324 struct llog_mini_rec lmr;
325 struct llog_cookie cookie;
326 struct llog_ctxt *ctxt;
329 struct llog_rec_hdr rec;
333 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
336 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
337 lmr.lmr_hdr.lrh_type = 0xf00f00;
339 sprintf(name, "%x", llog_test_rand + 1);
340 CWARN("4a: create a catalog log with name: %s\n", name);
341 rc = llog_open_create(env, ctxt, &cath, NULL, name);
343 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
344 GOTO(ctxt_release, rc);
346 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
348 CERROR("4a: can't init llog handle: %d\n", rc);
353 cat_logid = cath->lgh_id;
355 CWARN("4b: write 1 record into the catalog\n");
356 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie, NULL);
358 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
362 rc = verify_handle("4b", cath, 2);
366 rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
370 CWARN("4c: cancel 1 log record\n");
371 rc = llog_cat_cancel_records(env, cath, 1, &cookie);
373 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
378 rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
382 CWARN("4d: write %d more log records\n", LLOG_TEST_RECNUM);
383 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
384 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL, NULL);
386 CERROR("4d: write %d records failed at #%d: %d\n",
387 LLOG_TEST_RECNUM, i + 1, rc);
393 /* make sure new plain llog appears */
394 rc = verify_handle("4d", cath, 3);
398 CWARN("4e: add 5 large records, one record per block\n");
399 buflen = LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
400 sizeof(struct llog_rec_tail);
401 OBD_ALLOC(buf, buflen);
403 GOTO(out, rc = -ENOMEM);
404 for (i = 0; i < 5; i++) {
405 rec.lrh_len = buflen;
406 rec.lrh_type = OBD_CFG_REC;
407 rc = llog_cat_add(env, cath, &rec, NULL, buf);
409 CERROR("4e: write 5 records failed at #%d: %d\n",
416 OBD_FREE(buf, buflen);
418 CWARN("4f: put newly-created catalog\n");
419 rc2 = llog_cat_close(env, cath);
421 CERROR("4: close log %s failed: %d\n", name, rc2);
430 static int cat_counter;
432 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
433 struct llog_rec_hdr *rec, void *data)
435 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
436 struct lu_fid fid = {0};
438 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
439 CERROR("invalid record in catalog\n");
443 logid_to_fid(&lir->lid_id, &fid);
445 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
446 rec->lrh_index, PFID(&fid),
447 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
454 static int plain_counter;
456 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
457 struct llog_rec_hdr *rec, void *data)
459 struct lu_fid fid = {0};
461 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
462 CERROR("log is not plain\n");
466 logid_to_fid(&llh->lgh_id, &fid);
468 CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
469 rec->lrh_index, PFID(&fid));
476 static int cancel_count;
478 static int llog_cancel_rec_cb(const struct lu_env *env,
479 struct llog_handle *llh,
480 struct llog_rec_hdr *rec, void *data)
482 struct llog_cookie cookie;
484 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
485 CERROR("log is not plain\n");
489 cookie.lgc_lgl = llh->lgh_id;
490 cookie.lgc_index = rec->lrh_index;
492 llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
494 if (cancel_count == LLOG_TEST_RECNUM)
495 RETURN(-LLOG_EEMPTY);
499 /* Test log and catalogue processing */
500 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
502 struct llog_handle *llh = NULL;
505 struct llog_mini_rec lmr;
506 struct llog_ctxt *ctxt;
510 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
513 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
514 lmr.lmr_hdr.lrh_type = 0xf00f00;
516 CWARN("5a: re-open catalog by id\n");
517 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
519 CERROR("5a: llog_create with logid failed: %d\n", rc);
523 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
525 CERROR("5a: can't init llog handle: %d\n", rc);
529 CWARN("5b: print the catalog entries.. we expect 2\n");
531 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
533 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
536 if (cat_counter != 2) {
537 CERROR("5b: %d entries in catalog\n", cat_counter);
538 GOTO(out, rc = -EINVAL);
541 CWARN("5c: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
543 rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
544 if (rc != -LLOG_EEMPTY) {
545 CERROR("5c: process with cat_cancel_cb failed: %d\n", rc);
549 CWARN("5c: print the catalog entries.. we expect 1\n");
551 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
553 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
556 if (cat_counter != 1) {
557 CERROR("5c: %d entries in catalog\n", cat_counter);
558 GOTO(out, rc = -EINVAL);
561 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
562 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL, NULL);
564 CERROR("5d: add record to the log with many canceled empty "
569 CWARN("5e: print plain log entries.. expect 6\n");
571 rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
573 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
576 if (plain_counter != 6) {
577 CERROR("5e: found %d records\n", plain_counter);
578 GOTO(out, rc = -EINVAL);
581 CWARN("5f: print plain log entries reversely.. expect 6\n");
583 rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
585 CERROR("5f: reversely process with plain_print_cb failed:"
589 if (plain_counter != 6) {
590 CERROR("5f: found %d records\n", plain_counter);
591 GOTO(out, rc = -EINVAL);
595 CWARN("5g: close re-opened catalog\n");
596 rc2 = llog_cat_close(env, llh);
598 CERROR("5g: close log %s failed: %d\n", name, rc2);
608 /* Test client api; open log by name and process */
609 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
612 struct obd_device *mgc_obd;
613 struct llog_ctxt *ctxt;
614 struct obd_uuid *mgs_uuid;
615 struct obd_export *exp;
616 struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
617 struct llog_handle *llh = NULL;
618 struct llog_ctxt *nctxt;
621 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
623 mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
625 CWARN("6a: re-open log %s using client API\n", name);
626 mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
627 if (mgc_obd == NULL) {
628 CERROR("6a: no MGC devices connected to %s found.\n",
630 GOTO(ctxt_release, rc = -ENOENT);
633 rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
634 NULL /* obd_connect_data */, NULL);
635 if (rc != -EALREADY) {
636 CERROR("6a: connect on connected MGC (%s) failed to return"
637 " -EALREADY", mgc_obd->obd_name);
640 GOTO(ctxt_release, rc = -EINVAL);
643 nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
644 rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
646 CERROR("6a: llog_open failed %d\n", rc);
650 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
652 CERROR("6a: llog_init_handle failed %d\n", rc);
656 plain_counter = 1; /* llog header is first record */
657 CWARN("6b: process log %s using client API\n", name);
658 rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
660 CERROR("6b: llog_process failed %d\n", rc);
661 CWARN("6b: processed %d records\n", plain_counter);
663 rc = verify_handle("6b", llh, plain_counter);
667 plain_counter = 1; /* llog header is first record */
668 CWARN("6c: process log %s reversely using client API\n", name);
669 rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
671 CERROR("6c: llog_reverse_process failed %d\n", rc);
672 CWARN("6c: processed %d records\n", plain_counter);
674 rc = verify_handle("6c", llh, plain_counter);
679 rc2 = llog_close(env, llh);
681 CERROR("6: llog_close failed: rc = %d\n", rc2);
686 llog_ctxt_put(nctxt);
693 struct llog_rec_hdr lrh; /* common header */
694 struct llog_logid_rec llr; /* LLOG_LOGID_MAGIC */
695 struct llog_unlink64_rec lur; /* MDS_UNLINK64_REC */
696 struct llog_setattr64_rec lsr64; /* MDS_SETATTR64_REC */
697 struct llog_size_change_rec lscr; /* OST_SZ_REC */
698 struct llog_changelog_rec lcr; /* CHANGELOG_REC */
699 struct llog_changelog_user_rec lcur; /* CHANGELOG_USER_REC */
700 struct llog_gen_rec lgr; /* LLOG_GEN_REC */
703 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
704 struct llog_rec_hdr *rec, void *data)
706 struct lu_fid fid = {0};
708 logid_to_fid(&llh->lgh_id, &fid);
710 CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
711 rec->lrh_type, rec->lrh_index, PFID(&fid));
717 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
718 struct llog_rec_hdr *rec, void *data)
721 /* test LLOG_DEL_RECORD is working */
722 return LLOG_DEL_RECORD;
725 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
727 struct llog_handle *llh;
728 int rc = 0, i, process_count;
733 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
735 CERROR("7_sub: create log failed\n");
739 rc = llog_init_handle(env, llh,
740 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
743 CERROR("7_sub: can't init llog handle: %d\n", rc);
746 for (i = 0; i < LLOG_BITMAP_SIZE(llh->lgh_hdr); i++) {
747 rc = llog_write(env, llh, &llog_records.lrh, NULL, 0,
752 CERROR("7_sub: write recs failed at #%d: %d\n",
759 CWARN("7_sub: write record more than BITMAP size!\n");
760 GOTO(out_close, rc = -EINVAL);
763 rc = verify_handle("7_sub", llh, num_recs + 1);
765 CERROR("7_sub: verify handle failed: %d\n", rc);
768 if (num_recs < LLOG_BITMAP_SIZE(llh->lgh_hdr) - 1)
769 CWARN("7_sub: records are not aligned, written %d from %u\n",
770 num_recs, LLOG_BITMAP_SIZE(llh->lgh_hdr) - 1);
773 rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
775 CERROR("7_sub: llog process failed: %d\n", rc);
778 process_count = plain_counter;
779 if (process_count != num_recs) {
780 CERROR("7_sub: processed %d records from %d total\n",
781 process_count, num_recs);
782 GOTO(out_close, rc = -EINVAL);
786 rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
787 if (rc && rc != LLOG_DEL_PLAIN) {
788 CERROR("7_sub: reverse llog process failed: %d\n", rc);
791 if (process_count != plain_counter) {
792 CERROR("7_sub: Reverse/direct processing found different"
793 "number of records: %d/%d\n",
794 plain_counter, process_count);
795 GOTO(out_close, rc = -EINVAL);
797 if (llog_exist(llh)) {
798 CERROR("7_sub: llog exists but should be zapped\n");
799 GOTO(out_close, rc = -EEXIST);
802 rc = verify_handle("7_sub", llh, 1);
805 llog_destroy(env, llh);
806 llog_close(env, llh);
810 /* Test all llog records writing and processing */
811 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
813 struct llog_ctxt *ctxt;
818 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
820 CWARN("7a: test llog_logid_rec\n");
821 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
822 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
823 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
825 rc = llog_test_7_sub(env, ctxt);
827 CERROR("7a: llog_logid_rec test failed\n");
831 CWARN("7b: test llog_unlink64_rec\n");
832 llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
833 llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
834 llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
836 rc = llog_test_7_sub(env, ctxt);
838 CERROR("7b: llog_unlink_rec test failed\n");
842 CWARN("7c: test llog_setattr64_rec\n");
843 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
844 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
845 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
847 rc = llog_test_7_sub(env, ctxt);
849 CERROR("7c: llog_setattr64_rec test failed\n");
853 CWARN("7d: test llog_size_change_rec\n");
854 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
855 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
856 llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
858 rc = llog_test_7_sub(env, ctxt);
860 CERROR("7d: llog_size_change_rec test failed\n");
864 CWARN("7e: test llog_changelog_rec\n");
865 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
866 llog_records.lcr.cr_tail.lrt_len = sizeof(llog_records.lcr);
867 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
869 rc = llog_test_7_sub(env, ctxt);
871 CERROR("7e: llog_changelog_rec test failed\n");
875 CWARN("7f: test llog_changelog_user_rec\n");
876 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
877 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
878 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
880 rc = llog_test_7_sub(env, ctxt);
882 CERROR("7f: llog_changelog_user_rec test failed\n");
886 CWARN("7g: test llog_gen_rec\n");
887 llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
888 llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
889 llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
891 rc = llog_test_7_sub(env, ctxt);
893 CERROR("7g: llog_size_change_rec test failed\n");
901 static int llog_truncate(const struct lu_env *env, struct dt_object *o)
910 d = lu2dt_dev(o->do_lu.lo_dev);
913 rc = dt_attr_get(env, o, &la, NULL);
917 CDEBUG(D_OTHER, "original size %Lu\n", la.la_size);
918 rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
919 if (la.la_size < rc) {
920 CERROR("too small llog: %Lu\n", la.la_size);
925 la.la_size = la.la_size - (sizeof(struct llog_mini_rec) * 2);
926 la.la_valid = LA_SIZE;
928 th = dt_trans_create(env, d);
932 rc = dt_declare_attr_set(env, o, &la, th);
936 rc = dt_declare_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
938 rc = dt_trans_start_local(env, d, th);
942 rc = dt_punch(env, o, la.la_size, OBD_OBJECT_EOF, th, BYPASS_CAPA);
946 rc = dt_attr_set(env, o, &la, th, BYPASS_CAPA);
951 dt_trans_stop(env, d, th);
956 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
957 struct llog_rec_hdr *rec, void *data)
963 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
965 struct llog_handle *llh = NULL;
969 struct llog_mini_rec lmr;
970 struct llog_ctxt *ctxt;
971 struct dt_object *obj = NULL;
975 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
978 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
979 lmr.lmr_hdr.lrh_type = 0xf00f00;
981 CWARN("8a: fill the first plain llog\n");
982 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
984 CERROR("8a: llog_create with logid failed: %d\n", rc);
988 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
990 CERROR("8a: can't init llog handle: %d\n", rc);
995 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
997 CERROR("5a: process with cat_cancel_cb failed: %d\n", rc);
1000 orig_counter = plain_counter;
1002 for (i = 0; i < 100; i++) {
1003 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL, NULL);
1005 CERROR("5a: add record failed\n");
1010 /* grab the current plain llog, we'll corrupt it later */
1011 obj = llh->u.chd.chd_current_log->lgh_obj;
1013 lu_object_get(&obj->do_lu);
1014 CWARN("8a: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1016 rc2 = llog_cat_close(env, llh);
1018 CERROR("8a: close log %s failed: %d\n", name, rc2);
1024 CWARN("8b: fill the second plain llog\n");
1025 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1027 CERROR("8b: llog_create with logid failed: %d\n", rc);
1031 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1033 CERROR("8b: can't init llog handle: %d\n", rc);
1037 for (i = 0; i < 100; i++) {
1038 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL, NULL);
1040 CERROR("8b: add record failed\n");
1044 CWARN("8b: second llog "DFID"\n",
1045 PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1047 rc2 = llog_cat_close(env, llh);
1049 CERROR("8b: close log %s failed: %d\n", name, rc2);
1055 CWARN("8c: drop two records from the first plain llog\n");
1056 llog_truncate(env, obj);
1058 CWARN("8d: count survived records\n");
1059 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1061 CERROR("8d: llog_create with logid failed: %d\n", rc);
1065 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1067 CERROR("8d: can't init llog handle: %d\n", rc);
1072 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1074 CERROR("8d: process with cat_cancel_cb failed: %d\n", rc);
1078 if (orig_counter + 200 - 2 != plain_counter) {
1079 CERROR("found %d records (expected %d)\n", plain_counter,
1080 orig_counter + 200 - 2);
1085 CWARN("8d: close re-opened catalog\n");
1086 rc2 = llog_cat_close(env, llh);
1088 CERROR("8d: close log %s failed: %d\n", name, rc2);
1093 llog_ctxt_put(ctxt);
1096 lu_object_put(env, &obj->do_lu);
1101 /* -------------------------------------------------------------------------
1102 * Tests above, boring obd functions below
1103 * ------------------------------------------------------------------------- */
1104 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
1106 struct llog_handle *llh = NULL;
1107 struct llog_ctxt *ctxt;
1112 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1115 sprintf(name, "%x", llog_test_rand);
1117 rc = llog_test_1(env, obd, name);
1119 GOTO(cleanup_ctxt, rc);
1121 rc = llog_test_2(env, obd, name, &llh);
1123 GOTO(cleanup_ctxt, rc);
1125 rc = llog_test_3(env, obd, llh);
1129 rc = llog_test_4(env, obd);
1133 rc = llog_test_5(env, obd);
1137 rc = llog_test_6(env, obd, name);
1141 rc = llog_test_7(env, obd);
1145 rc = llog_test_8(env, obd);
1150 err = llog_destroy(env, llh);
1152 CERROR("cleanup: llog_destroy failed: %d\n", err);
1153 llog_close(env, llh);
1157 llog_ctxt_put(ctxt);
1161 static int llog_test_cleanup(struct obd_device *obd)
1163 struct obd_device *tgt;
1169 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
1173 tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
1174 rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
1176 CERROR("failed to llog_test_llog_finish: %d\n", rc);
1181 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1183 struct obd_device *tgt;
1184 struct llog_ctxt *ctxt;
1185 struct dt_object *o;
1187 struct lu_context test_session;
1192 if (lcfg->lcfg_bufcount < 2) {
1193 CERROR("requires a TARGET OBD name\n");
1197 if (lcfg->lcfg_buflens[1] < 1) {
1198 CERROR("requires a TARGET OBD name\n");
1203 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1204 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1205 CERROR("target device not attached or not set up (%s)\n",
1206 lustre_cfg_string(lcfg, 1));
1210 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
1214 rc = lu_context_init(&test_session, LCT_SERVER_SESSION);
1216 GOTO(cleanup_env, rc);
1217 test_session.lc_thread = (struct ptlrpc_thread *)current;
1218 lu_context_enter(&test_session);
1219 env.le_ses = &test_session;
1221 CWARN("Setup llog-test device over %s device\n",
1222 lustre_cfg_string(lcfg, 1));
1224 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1225 obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
1227 rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
1230 GOTO(cleanup_session, rc);
1232 /* use MGS llog dir for tests */
1233 ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
1236 llog_ctxt_put(ctxt);
1238 ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
1241 llog_ctxt_put(ctxt);
1243 llog_test_rand = cfs_rand();
1245 rc = llog_run_tests(&env, tgt);
1247 llog_test_cleanup(obd);
1249 lu_context_exit(&test_session);
1250 lu_context_fini(&test_session);
1256 static struct obd_ops llog_obd_ops = {
1257 .o_owner = THIS_MODULE,
1258 .o_setup = llog_test_setup,
1259 .o_cleanup = llog_test_cleanup,
1262 static int __init llog_test_init(void)
1264 return class_register_type(&llog_obd_ops, NULL, true, NULL,
1265 #ifndef HAVE_ONLY_PROCFS_SEQ
1271 static void __exit llog_test_exit(void)
1273 class_unregister_type("llog_test");
1276 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1277 MODULE_DESCRIPTION("llog test module");
1278 MODULE_LICENSE("GPL");
1280 module_init(llog_test_init);
1281 module_exit(llog_test_exit);