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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/obdclass/llog_test.c
33 * Author: Phil Schwan <phil@clusterfs.com>
34 * Author: Mikhail Pershin <mike.pershin@intel.com>
37 #define DEBUG_SUBSYSTEM S_CLASS
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/kthread.h>
42 #include <linux/delay.h>
43 #include <linux/random.h>
45 #include <obd_class.h>
46 #include <lustre_fid.h>
47 #include <lustre_log.h>
49 /* This is slightly more than the number of records that can fit into a
50 * single llog file, because the llog_log_header takes up some of the
51 * space in the first block that cannot be used for the bitmap. */
52 static int llog_test_recnum = (LLOG_MIN_CHUNK_SIZE * 8);
53 static int llog_test_rand;
54 static struct obd_uuid uuid = { .uuid = "test_uuid" };
55 static struct llog_logid cat_logid;
57 struct llog_mini_rec {
58 struct llog_rec_hdr lmr_hdr;
59 struct llog_rec_tail lmr_tail;
60 } __attribute__((packed));
62 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
68 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
69 if (test_bit_le(i, LLOG_HDR_BITMAP(llh->lgh_hdr))) {
75 /* check the llog is sane at first, llh_count and lgh_last_idx*/
76 if (llh->lgh_hdr->llh_count != active_recs) {
77 CERROR("%s: handle->count is %d, but there are %d recs found\n",
78 test, llh->lgh_hdr->llh_count, active_recs);
82 if (llh->lgh_last_idx != LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index ||
83 (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_CAT) &&
84 llh->lgh_last_idx < last_idx)) {
85 CERROR("%s: lgh_last_idx is %d (%d in the header), last found %d\n",
86 test, llh->lgh_last_idx,
87 LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index, last_idx);
91 /* finally checks against expected value from the caller */
92 if (active_recs != num_recs) {
93 CERROR("%s: expected %d active recs after write, found %d\n",
94 test, num_recs, active_recs);
101 /* Test named-log create/open, close */
102 static int llog_test_1(const struct lu_env *env,
103 struct obd_device *obd, char *name)
105 struct llog_handle *llh;
106 struct llog_ctxt *ctxt;
112 CWARN("1a: create a log with name: %s\n", name);
113 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
116 rc = llog_open_create(env, ctxt, &llh, NULL, name);
118 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
121 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, &uuid);
123 CERROR("1a: can't init llog handle: %d\n", rc);
127 rc = verify_handle("1", llh, 1);
129 CWARN("1b: close newly-created log\n");
131 rc2 = llog_close(env, llh);
133 CERROR("1b: close log %s failed: %d\n", name, rc2);
142 static int test_2_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
143 struct llog_rec_hdr *rec, void *data)
145 return LLOG_DEL_RECORD;
148 /* Test named-log reopen; returns opened log on success */
149 static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
150 char *name, struct llog_handle **llh)
152 struct llog_ctxt *ctxt;
153 struct llog_handle *lgh;
154 struct llog_logid logid;
156 struct llog_mini_rec lmr;
160 CWARN("2a: re-open a log with name: %s\n", name);
161 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
164 rc = llog_open(env, ctxt, llh, NULL, name, LLOG_OPEN_EXISTS);
166 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
170 rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
172 CERROR("2a: can't init llog handle: %d\n", rc);
173 GOTO(out_close_llh, rc);
176 rc = verify_handle("2", *llh, 1);
178 GOTO(out_close_llh, rc);
180 CWARN("2b: create a log without specified NAME & LOGID\n");
181 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
183 CERROR("2b: create log failed\n");
184 GOTO(out_close_llh, rc);
186 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
188 CERROR("2b: can't init llog handle: %d\n", rc);
194 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
195 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
197 /* Check llog header values are correct after record add/cancel */
198 CWARN("2b: write 1 llog records, check llh_count\n");
199 rc = llog_write(env, lgh, &lmr.lmr_hdr, LLOG_NEXT_IDX);
203 /* in-memory values after record addition */
204 rc = verify_handle("2b", lgh, 2);
208 /* re-open llog to read on-disk values */
209 llog_close(env, lgh);
211 CWARN("2c: re-open the log by LOGID and verify llh_count\n");
212 rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
214 CERROR("2c: re-open log by LOGID failed\n");
215 GOTO(out_close_llh, rc);
218 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
220 CERROR("2c: can't init llog handle: %d\n", rc);
224 /* check values just read from disk */
225 rc = verify_handle("2c", lgh, 2);
229 rc = llog_process(env, lgh, test_2_cancel_cb, NULL, NULL);
233 /* in-memory values */
234 rc = verify_handle("2c", lgh, 1);
238 /* re-open llog to get on-disk values */
239 llog_close(env, lgh);
241 rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
243 CERROR("2c: re-open log by LOGID failed\n");
244 GOTO(out_close_llh, rc);
247 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
249 CERROR("2c: can't init llog handle: %d\n", rc);
253 /* on-disk values after llog re-open */
254 rc = verify_handle("2c", lgh, 1);
258 CWARN("2d: destroy this log\n");
259 rc = llog_destroy(env, lgh);
261 CERROR("2d: destroy log failed\n");
263 llog_close(env, lgh);
266 llog_close(env, *llh);
273 static int test_3_rec_num;
274 static off_t test_3_rec_off;
275 static int test_3_paddings;
276 static int test_3_start_idx;
280 * - check lgh_cur_offset correctness
281 * - check record index consistency
282 * - modify each record in-place
283 * - add new record during *last_idx processing
285 static int test3_check_n_add_cb(const struct lu_env *env,
286 struct llog_handle *lgh,
287 struct llog_rec_hdr *rec, void *data)
289 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
290 int *last_rec = data;
291 unsigned cur_idx = test_3_start_idx + test_3_rec_num;
294 if (lgh->lgh_hdr->llh_flags & LLOG_F_IS_FIXSIZE) {
295 LASSERT(lgh->lgh_hdr->llh_size > 0);
296 if (lgh->lgh_cur_offset != lgh->lgh_hdr->llh_hdr.lrh_len +
297 (cur_idx - 1) * lgh->lgh_hdr->llh_size)
298 CERROR("Wrong record offset in cur_off: %llu, should be %u\n",
300 lgh->lgh_hdr->llh_hdr.lrh_len +
301 (cur_idx - 1) * lgh->lgh_hdr->llh_size);
303 size_t chunk_size = lgh->lgh_hdr->llh_hdr.lrh_len;
306 * For variable size records the start offset is unknown, trust
307 * the first value and check others are consistent with it.
309 if (test_3_rec_off == 0)
310 test_3_rec_off = lgh->lgh_cur_offset;
312 if (lgh->lgh_cur_offset != test_3_rec_off) {
313 __u64 tmp = lgh->lgh_cur_offset;
315 /* there can be padding record */
316 if ((do_div(tmp, chunk_size) == 0) &&
317 (lgh->lgh_cur_offset - test_3_rec_off <
318 rec->lrh_len + LLOG_MIN_REC_SIZE)) {
319 test_3_rec_off = lgh->lgh_cur_offset;
322 CERROR("Wrong record offset in cur_off: %llu"
323 ", should be %lld (rec len %u)\n",
325 (long long)test_3_rec_off,
329 test_3_rec_off += rec->lrh_len;
332 cur_idx += test_3_paddings;
333 if (cur_idx != rec->lrh_index)
334 CERROR("Record with wrong index was read: %u, expected %u\n",
335 rec->lrh_index, cur_idx);
337 /* modify all records in place */
338 lgr->lgr_gen.conn_cnt = rec->lrh_index;
339 rc = llog_write(env, lgh, rec, rec->lrh_index);
341 CERROR("cb_test_3: cannot modify record while processing\n");
344 * Add new record to the llog at *last_rec position one by one to
345 * check that last block is re-read during processing
347 if (cur_idx == *last_rec || cur_idx == (*last_rec + 1)) {
348 rc = llog_write(env, lgh, rec, LLOG_NEXT_IDX);
350 CERROR("cb_test_3: cannot add new record while "
358 /* Check in-place modifications were done for all records*/
359 static int test3_check_cb(const struct lu_env *env, struct llog_handle *lgh,
360 struct llog_rec_hdr *rec, void *data)
362 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
364 if (lgr->lgr_gen.conn_cnt != rec->lrh_index) {
365 CERROR("cb_test_3: record %u is not modified\n",
373 static int llog_test3_process(const struct lu_env *env,
374 struct llog_handle *lgh,
375 llog_cb_t cb, int start)
377 struct llog_process_cat_data cd;
378 int last_idx; /* new record will be injected here */
381 CWARN("test3: processing records from index %d to the end\n",
383 cd.lpcd_first_idx = start - 1;
384 cd.lpcd_last_idx = 0;
385 test_3_rec_num = test_3_paddings = 0;
386 last_idx = lgh->lgh_last_idx;
387 rc = llog_process(env, lgh, cb, &last_idx, &cd);
390 CWARN("test3: total %u records processed with %u paddings\n",
391 test_3_rec_num, test_3_paddings);
392 return test_3_rec_num;
395 /* Test plain llog functionality */
396 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
397 struct llog_handle *llh)
400 struct llog_rec_hdr *hdr = (void *)buf;
402 int num_recs = 1; /* 1 for the header */
407 hdr->lrh_len = sizeof(struct llog_gen_rec);
408 hdr->lrh_type = LLOG_GEN_REC;
409 llh->lgh_hdr->llh_size = sizeof(struct llog_gen_rec);
410 llh->lgh_hdr->llh_flags |= LLOG_F_IS_FIXSIZE;
413 * Fill the llog with 64-bytes records, use 1023 records,
414 * so last chunk will be partially full. Don't change this
415 * value until record size is changed.
417 CWARN("3a: write 1023 fixed-size llog records\n");
418 for (i = 0; i < 1023; i++) {
419 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
421 CERROR("3a: write 1023 records failed at #%d: %d\n",
428 rc = verify_handle("3a", llh, num_recs);
433 * Test fixed-size records processing:
434 * - search the needed index
435 * - go through all records from that index
436 * - check all indices are growing monotonically and exist
437 * - modify each record
439 * NB: test3_check_n_add adds two new records while processing
440 * after last record. There were 1023 records created so the last chunk
441 * misses exactly one record. Therefore one of new records will be
442 * the last in the current chunk and second causes the new chunk to be
446 test_3_start_idx = 501;
448 rc = llog_test3_process(env, llh, test3_check_n_add_cb,
453 /* extra record is created during llog_process() */
454 if (rc != expected) {
455 CERROR("3a: process total %d records but expect %d\n",
462 /* test modification in place */
463 rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
467 if (rc != expected) {
468 CERROR("3a: process total %d records but expect %d\n",
473 CWARN("3b: write 566 variable size llog records\n");
476 * Drop llh_size to 0 to mark llog as variable-size and write
477 * header to make this change permanent.
479 llh->lgh_hdr->llh_flags &= ~LLOG_F_IS_FIXSIZE;
480 llog_write(env, llh, &llh->lgh_hdr->llh_hdr, LLOG_HEADER_IDX);
482 hdr->lrh_type = OBD_CFG_REC;
485 * there are 1025 64-bytes records in llog already,
486 * the last chunk contains single record, i.e. 64 bytes.
487 * Each pair of variable size records is 200 bytes, so
488 * we will have the following distribution per chunks:
489 * block 1: 64 + 80(80/120) + 80 + 48(pad) = 81 iterations
490 * block 2: 80(120/80) + 120 + 72(pad) = 81 itereations
491 * block 3: 80(80/120) + 80 + 112(pad) = 81 iterations
492 * -- the same as block 2 again and so on.
493 * block 7: 80(80/120) = 80 iterations and 192 bytes remain
494 * Total 6 * 81 + 80 = 566 itereations.
495 * Callback will add another 120 bytes in the end of the last chunk
496 * and another 120 bytes will cause padding (72 bytes) plus 120
497 * bytes in the new block.
499 for (i = 0; i < 566; i++) {
505 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
507 CERROR("3b: write 566 records failed at #%d: %d\n",
514 rc = verify_handle("3b", llh, num_recs);
518 test_3_start_idx = 1026;
520 rc = llog_test3_process(env, llh, test3_check_n_add_cb,
525 if (rc != expected) {
526 CERROR("3b: process total %d records but expect %d\n",
533 /* test modification in place */
534 rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
538 if (rc != expected) {
539 CERROR("3b: process total %d records but expect %d\n",
544 CWARN("3c: write records with variable size until BITMAP_SIZE, "
546 while (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
547 if ((num_recs % 2) == 0)
552 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
556 CERROR("3c: write recs failed at #%d: %d\n",
564 CWARN("3c: write record more than BITMAP size!\n");
567 CWARN("3c: wrote %d more records before end of llog is reached\n",
570 rc = verify_handle("3c", llh, num_recs);
575 /* Test catalogue additions */
576 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
578 struct llog_handle *cath, *llh;
580 int rc, rc2, i, buflen;
581 struct llog_mini_rec lmr;
582 struct llog_cookie cookie;
583 struct llog_ctxt *ctxt;
586 struct llog_rec_hdr *rec;
590 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
593 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
594 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
596 sprintf(name, "%x", llog_test_rand + 1);
597 CWARN("4a: create a catalog log with name: %s\n", name);
598 rc = llog_open_create(env, ctxt, &cath, NULL, name);
600 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
601 GOTO(ctxt_release, rc);
603 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
605 CERROR("4a: can't init llog handle: %d\n", rc);
610 cat_logid = cath->lgh_id;
612 CWARN("4b: write 1 record into the catalog\n");
613 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
615 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
619 rc = verify_handle("4b", cath, 2);
623 rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
627 /* estimate the max number of record for the plain llog
628 * cause it depends on disk size
630 llh = cath->u.chd.chd_current_log;
631 if (llh->lgh_max_size != 0) {
632 llog_test_recnum = (llh->lgh_max_size -
633 sizeof(struct llog_log_hdr)) / LLOG_MIN_REC_SIZE;
636 if (llog_test_recnum >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr))
637 llog_test_recnum = LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1;
639 CWARN("4c: cancel 1 log record\n");
640 rc = llog_cat_cancel_records(env, cath, 1, &cookie);
642 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
647 rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
651 CWARN("4d: write %d more log records\n", llog_test_recnum);
652 for (i = 0; i < llog_test_recnum; i++) {
653 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
655 CERROR("4d: write %d records failed at #%d: %d\n",
656 llog_test_recnum, i + 1, rc);
662 /* make sure new plain llog appears */
663 rc = verify_handle("4d", cath, 3);
667 CWARN("4e: add 5 large records, one record per block\n");
668 buflen = LLOG_MIN_CHUNK_SIZE;
669 OBD_ALLOC(buf, buflen);
671 GOTO(out, rc = -ENOMEM);
672 for (i = 0; i < 5; i++) {
674 rec->lrh_len = buflen;
675 rec->lrh_type = OBD_CFG_REC;
676 rc = llog_cat_add(env, cath, rec, NULL);
678 CERROR("4e: write 5 records failed at #%d: %d\n",
685 OBD_FREE(buf, buflen);
687 CWARN("4f: put newly-created catalog\n");
688 rc2 = llog_cat_close(env, cath);
690 CERROR("4: close log %s failed: %d\n", name, rc2);
699 static int cat_counter;
701 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
702 struct llog_rec_hdr *rec, void *data)
704 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
705 struct lu_fid fid = {0};
707 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
708 CERROR("invalid record in catalog\n");
712 logid_to_fid(&lir->lid_id, &fid);
714 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
715 rec->lrh_index, PFID(&fid),
716 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
723 static int plain_counter;
725 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
726 struct llog_rec_hdr *rec, void *data)
728 struct lu_fid fid = {0};
730 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
731 CERROR("log is not plain\n");
735 logid_to_fid(&llh->lgh_id, &fid);
737 CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
738 rec->lrh_index, PFID(&fid));
745 static int cancel_count;
747 static int llog_cancel_rec_cb(const struct lu_env *env,
748 struct llog_handle *llh,
749 struct llog_rec_hdr *rec, void *data)
751 struct llog_cookie cookie;
753 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
754 CERROR("log is not plain\n");
758 cookie.lgc_lgl = llh->lgh_id;
759 cookie.lgc_index = rec->lrh_index;
761 llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
763 if (cancel_count == llog_test_recnum)
764 RETURN(-LLOG_EEMPTY);
768 /* Test log and catalogue processing */
769 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
771 struct llog_handle *llh = NULL;
774 struct llog_mini_rec lmr;
775 struct llog_ctxt *ctxt;
779 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
782 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
783 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
785 CWARN("5a: re-open catalog by id\n");
786 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
788 CERROR("5a: llog_create with logid failed: %d\n", rc);
792 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
794 CERROR("5a: can't init llog handle: %d\n", rc);
798 CWARN("5b: print the catalog entries.. we expect 2\n");
800 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
802 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
805 if (cat_counter != 2) {
806 CERROR("5b: %d entries in catalog\n", cat_counter);
807 GOTO(out, rc = -EINVAL);
810 CWARN("5c: Cancel %d records, see one log zapped\n", llog_test_recnum);
812 rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
813 if (rc != -LLOG_EEMPTY) {
814 CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
818 CWARN("5c: print the catalog entries.. we expect 1\n");
820 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
822 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
825 if (cat_counter != 1) {
826 CERROR("5c: %d entries in catalog\n", cat_counter);
827 GOTO(out, rc = -EINVAL);
830 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
831 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
833 CERROR("5d: add record to the log with many canceled empty "
838 CWARN("5e: print plain log entries.. expect 6\n");
840 rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
842 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
845 if (plain_counter != 6) {
846 CERROR("5e: found %d records\n", plain_counter);
847 GOTO(out, rc = -EINVAL);
850 CWARN("5f: print plain log entries reversely.. expect 6\n");
852 rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
854 CERROR("5f: reversely process with plain_print_cb failed: "
858 if (plain_counter != 6) {
859 CERROR("5f: found %d records\n", plain_counter);
860 GOTO(out, rc = -EINVAL);
864 CWARN("5g: close re-opened catalog\n");
865 rc2 = llog_cat_close(env, llh);
867 CERROR("5g: close log %s failed: %d\n", name, rc2);
877 /* Test client api; open log by name and process */
878 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
881 struct obd_device *mgc_obd;
882 struct llog_ctxt *ctxt;
883 struct obd_uuid *mgs_uuid;
884 struct obd_export *exp;
885 struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
886 struct llog_handle *llh = NULL;
887 struct llog_ctxt *nctxt;
890 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
892 mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
894 CWARN("6a: re-open log %s using client API\n", name);
895 mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
896 if (mgc_obd == NULL) {
897 CERROR("6a: no MGC devices connected to %s found.\n",
899 GOTO(ctxt_release, rc = -ENOENT);
902 rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
903 NULL /* obd_connect_data */, NULL);
904 if (rc != -EALREADY) {
905 CERROR("6a: connect on connected MGC (%s) failed to return"
906 " -EALREADY\n", mgc_obd->obd_name);
909 GOTO(ctxt_release, rc = -EINVAL);
912 nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
913 rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
915 CERROR("6a: llog_open failed %d\n", rc);
919 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
921 CERROR("6a: llog_init_handle failed %d\n", rc);
925 plain_counter = 1; /* llog header is first record */
926 CWARN("6b: process log %s using client API\n", name);
927 rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
929 CERROR("6b: llog_process failed %d\n", rc);
930 CWARN("6b: processed %d records\n", plain_counter);
932 rc = verify_handle("6b", llh, plain_counter);
936 plain_counter = 1; /* llog header is first record */
937 CWARN("6c: process log %s reversely using client API\n", name);
938 rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
940 CERROR("6c: llog_reverse_process failed %d\n", rc);
941 CWARN("6c: processed %d records\n", plain_counter);
943 rc = verify_handle("6c", llh, plain_counter);
948 rc2 = llog_close(env, llh);
950 CERROR("6: llog_close failed: rc = %d\n", rc2);
955 llog_ctxt_put(nctxt);
962 struct llog_rec_hdr lrh; /* common header */
963 struct llog_logid_rec llr; /* LLOG_LOGID_MAGIC */
964 struct llog_unlink64_rec lur; /* MDS_UNLINK64_REC */
965 struct llog_setattr64_rec lsr64; /* MDS_SETATTR64_REC */
966 struct llog_setattr64_rec_v2 lsr64_v2; /* MDS_SETATTR64_REC */
967 struct llog_size_change_rec lscr; /* OST_SZ_REC */
968 struct llog_changelog_rec lcr; /* CHANGELOG_REC */
969 struct llog_changelog_user_rec2 lcur; /* CHANGELOG_USER_REC2 */
970 struct llog_gen_rec lgr; /* LLOG_GEN_REC */
973 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
974 struct llog_rec_hdr *rec, void *data)
976 struct lu_fid fid = {0};
978 logid_to_fid(&llh->lgh_id, &fid);
980 CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
981 rec->lrh_type, rec->lrh_index, PFID(&fid));
987 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
988 struct llog_rec_hdr *rec, void *data)
991 /* test LLOG_DEL_RECORD is working */
992 return LLOG_DEL_RECORD;
995 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
997 struct llog_handle *llh;
998 int rc = 0, i, process_count;
1003 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1005 CERROR("7_sub: create log failed\n");
1009 rc = llog_init_handle(env, llh,
1010 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1013 CERROR("7_sub: can't init llog handle: %d\n", rc);
1014 GOTO(out_close, rc);
1016 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
1017 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1018 if (rc == -ENOSPC) {
1020 } else if (rc < 0) {
1021 CERROR("7_sub: write recs failed at #%d: %d\n",
1023 GOTO(out_close, rc);
1027 if (rc != -ENOSPC) {
1028 CWARN("7_sub: write record more than BITMAP size!\n");
1029 GOTO(out_close, rc = -EINVAL);
1032 rc = verify_handle("7_sub", llh, num_recs + 1);
1034 CERROR("7_sub: verify handle failed: %d\n", rc);
1035 GOTO(out_close, rc);
1037 if (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1)
1038 CWARN("7_sub: records are not aligned, written %d from %u\n",
1039 num_recs, LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
1042 rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
1044 CERROR("7_sub: llog process failed: %d\n", rc);
1045 GOTO(out_close, rc);
1047 process_count = plain_counter;
1048 if (process_count != num_recs) {
1049 CERROR("7_sub: processed %d records from %d total\n",
1050 process_count, num_recs);
1051 GOTO(out_close, rc = -EINVAL);
1055 rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
1056 if (rc && rc != LLOG_DEL_PLAIN) {
1057 CERROR("7_sub: reverse llog process failed: %d\n", rc);
1058 GOTO(out_close, rc);
1060 if (process_count != plain_counter) {
1061 CERROR("7_sub: Reverse/direct processing found different number of records: %d/%d\n",
1062 plain_counter, process_count);
1063 GOTO(out_close, rc = -EINVAL);
1065 if (llog_exist(llh)) {
1066 CERROR("7_sub: llog exists but should be zapped\n");
1067 GOTO(out_close, rc = -EEXIST);
1070 rc = verify_handle("7_sub", llh, 1);
1073 llog_destroy(env, llh);
1074 llog_close(env, llh);
1078 /* Test all llog records writing and processing */
1079 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
1081 struct llog_ctxt *ctxt;
1086 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1088 CWARN("7a: test llog_logid_rec\n");
1089 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1090 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1091 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1093 rc = llog_test_7_sub(env, ctxt);
1095 CERROR("7a: llog_logid_rec test failed\n");
1099 CWARN("7b: test llog_unlink64_rec\n");
1100 llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
1101 llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
1102 llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
1104 rc = llog_test_7_sub(env, ctxt);
1106 CERROR("7b: llog_unlink_rec test failed\n");
1110 CWARN("7c: test llog_setattr64_rec\n");
1111 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
1112 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
1113 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1115 rc = llog_test_7_sub(env, ctxt);
1117 CERROR("7c: llog_setattr64_rec test failed\n");
1121 CWARN("7d: test llog_size_change_rec\n");
1122 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1123 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1124 llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
1126 rc = llog_test_7_sub(env, ctxt);
1128 CERROR("7d: llog_size_change_rec test failed\n");
1132 CWARN("7e: test llog_changelog_rec\n");
1133 /* Direct access to cr_do_not_use: peculiar case for this test */
1134 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1135 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1136 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1138 rc = llog_test_7_sub(env, ctxt);
1140 CERROR("7e: llog_changelog_rec test failed\n");
1144 CWARN("7f: test llog_changelog_user_rec2\n");
1145 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1146 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1147 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC2;
1149 rc = llog_test_7_sub(env, ctxt);
1151 CERROR("7f: llog_changelog_user_rec test failed\n");
1155 CWARN("7g: test llog_gen_rec\n");
1156 llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
1157 llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
1158 llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
1160 rc = llog_test_7_sub(env, ctxt);
1162 CERROR("7g: llog_size_change_rec test failed\n");
1166 CWARN("7h: test llog_setattr64_rec_v2\n");
1167 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64_v2);
1168 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64_v2);
1169 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1171 rc = llog_test_7_sub(env, ctxt);
1173 CERROR("7h: llog_setattr64_rec_v2 test failed\n");
1177 llog_ctxt_put(ctxt);
1181 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
1182 struct llog_rec_hdr *rec, void *data)
1188 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
1190 struct llog_handle *llh = NULL;
1194 struct llog_mini_rec lmr;
1195 struct llog_ctxt *ctxt;
1196 struct dt_object *obj = NULL;
1200 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1203 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1204 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
1206 CWARN("8a: fill the first plain llog\n");
1207 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1209 CERROR("8a: llog_create with logid failed: %d\n", rc);
1213 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1215 CERROR("8a: can't init llog handle: %d\n", rc);
1220 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1222 CERROR("5a: process with test_8_cb failed: %d\n", rc);
1225 orig_counter = plain_counter;
1227 for (i = 0; i < 100; i++) {
1228 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1230 CERROR("5a: add record failed\n");
1235 /* grab the current plain llog, we'll corrupt it later */
1236 obj = llh->u.chd.chd_current_log->lgh_obj;
1238 lu_object_get(&obj->do_lu);
1239 CWARN("8a: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1241 rc2 = llog_cat_close(env, llh);
1243 CERROR("8a: close log %s failed: %d\n", name, rc2);
1249 CWARN("8b: fill the second plain llog\n");
1250 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1252 CERROR("8b: llog_create with logid failed: %d\n", rc);
1256 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1258 CERROR("8b: can't init llog handle: %d\n", rc);
1262 for (i = 0; i < 100; i++) {
1263 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1265 CERROR("8b: add record failed\n");
1269 CWARN("8b: second llog "DFID"\n",
1270 PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1272 rc2 = llog_cat_close(env, llh);
1274 CERROR("8b: close log %s failed: %d\n", name, rc2);
1280 /* Here was 8c: drop two records from the first plain llog
1281 * llog_truncate was bad idea cause it creates a wrong state,
1282 * lgh_last_idx is wrong and two records belongs to zeroed buffer
1285 CWARN("8d: count survived records\n");
1286 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1288 CERROR("8d: llog_create with logid failed: %d\n", rc);
1292 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1294 CERROR("8d: can't init llog handle: %d\n", rc);
1299 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1301 CERROR("8d: process with test_8_cb failed: %d\n", rc);
1305 if (orig_counter + 200 != plain_counter) {
1306 CERROR("found %d records (expected %d)\n", plain_counter,
1307 orig_counter + 200);
1312 CWARN("8d: close re-opened catalog\n");
1313 rc2 = llog_cat_close(env, llh);
1315 CERROR("8d: close log %s failed: %d\n", name, rc2);
1320 llog_ctxt_put(ctxt);
1323 dt_object_put(env, obj);
1328 static int llog_test_9_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
1330 struct llog_handle *llh;
1336 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1338 CERROR("9_sub: create log failed\n");
1342 rc = llog_init_handle(env, llh,
1343 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1346 CERROR("9_sub: can't init llog handle: %d\n", rc);
1347 GOTO(out_close, rc);
1350 logid_to_fid(&llh->lgh_id, &fid);
1351 fid_to_logid(&fid, &llog_records.llr.lid_id);
1352 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1354 CERROR("9_sub: write recs failed at #1: %d\n", rc);
1355 GOTO(out_close, rc);
1357 /* The below message is checked in sanity.sh test_60a (run-llog.sh) */
1358 CWARN("9_sub: record type %x in log "DFID_NOBRACE"\n",
1359 llog_records.lrh.lrh_type, PFID(&fid));
1361 llog_close(env, llh);
1365 /* Prepare different types of llog records for llog_reader test*/
1366 static int llog_test_9(const struct lu_env *env, struct obd_device *obd)
1368 struct llog_ctxt *ctxt;
1373 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1375 CWARN("9a: test llog_logid_rec\n");
1376 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1377 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1378 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1380 rc = llog_test_9_sub(env, ctxt);
1382 CERROR("9a: llog_logid_rec test failed\n");
1386 CWARN("9b: test llog_obd_cfg_rec\n");
1387 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1388 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1389 llog_records.lscr.lsc_hdr.lrh_type = OBD_CFG_REC;
1391 rc = llog_test_9_sub(env, ctxt);
1393 CERROR("9b: llog_obd_cfg_rec test failed\n");
1397 CWARN("9c: test llog_changelog_rec\n");
1398 /* Direct access to cr_do_not_use: peculiar case for this test */
1399 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1400 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1401 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1403 rc = llog_test_9_sub(env, ctxt);
1405 CERROR("9c: llog_changelog_rec test failed\n");
1409 CWARN("9d: test llog_changelog_user_rec2\n");
1410 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1411 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1412 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1414 rc = llog_test_9_sub(env, ctxt);
1416 CERROR("9d: llog_changelog_user_rec test failed\n");
1421 llog_ctxt_put(ctxt);
1425 struct llog_process_info {
1426 struct llog_handle *lpi_loghandle;
1431 struct completion lpi_completion;
1432 const struct lu_env *lpi_env;
1433 struct task_struct *lpi_reftask;
1437 static int llog_test_process_thread(void *arg)
1439 struct llog_process_info *lpi = arg;
1442 rc = llog_cat_process_or_fork(NULL, lpi->lpi_loghandle, lpi->lpi_cb,
1443 NULL, lpi->lpi_cbdata, 1, 0, true);
1445 complete(&lpi->lpi_completion);
1449 CWARN("10h: Error during catalog processing %d\n", rc);
1453 static int cat_check_old_cb(const struct lu_env *env, struct llog_handle *llh,
1454 struct llog_rec_hdr *rec, void *data)
1456 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
1457 struct lu_fid fid = {0};
1458 struct lu_fid *prev_fid = data;
1460 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
1461 CERROR("invalid record in catalog\n");
1465 logid_to_fid(&lir->lid_id, &fid);
1467 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
1468 rec->lrh_index, PFID(&fid),
1469 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
1471 if (prev_fid->f_oid > fid.f_oid) {
1472 CWARN("processing old record, fail\n");
1473 prev_fid->f_oid = 0xbad;
1474 RETURN(-LLOG_EEMPTY);
1477 if (prev_fid->f_oid == 0) {
1478 cfs_fail_loc = OBD_FAIL_ONCE | OBD_FAIL_LLOG_PROCESS_TIMEOUT;
1479 cfs_fail_val = (unsigned int) (llh->lgh_id.lgl_oi.oi.oi_id &
1481 msleep(1 * MSEC_PER_SEC);
1488 /* test catalog wrap around */
1489 static int llog_test_10(const struct lu_env *env, struct obd_device *obd)
1491 struct llog_handle *cath;
1493 int rc, rc2, i, enospc, eok;
1494 struct llog_mini_rec lmr;
1495 struct llog_ctxt *ctxt;
1498 struct dt_device *dt;
1502 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1505 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1506 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
1508 snprintf(name, sizeof(name), "%x", llog_test_rand + 2);
1509 CWARN("10a: create a catalog log with name: %s\n", name);
1510 rc = llog_open_create(env, ctxt, &cath, NULL, name);
1512 CERROR("10a: llog_create with name %s failed: %d\n", name, rc);
1513 GOTO(ctxt_release, rc);
1515 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
1517 CERROR("10a: can't init llog handle: %d\n", rc);
1521 cat_logid = cath->lgh_id;
1522 dt = lu2dt_dev(cath->lgh_obj->do_lu.lo_dev);
1525 * sync device to commit all recent LLOG changes to disk and avoid
1526 * to consume a huge space with delayed journal commit callbacks
1527 * particularly on low memory nodes or VMs
1529 rc = dt_sync(env, dt);
1531 CERROR("10c: sync failed: %d\n", rc);
1535 /* force catalog wrap for 5th plain LLOG */
1536 cfs_fail_loc = CFS_FAIL_SKIP|OBD_FAIL_CAT_RECORDS;
1539 CWARN("10b: write %d log records\n", llog_test_recnum);
1540 for (i = 0; i < llog_test_recnum; i++) {
1541 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1543 CERROR("10b: write %d records failed at #%d: %d\n",
1544 llog_test_recnum, i + 1, rc);
1549 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1550 rc = verify_handle("10b", cath, 3);
1555 * sync device to commit all recent LLOG changes to disk and avoid
1556 * to consume a huge space with delayed journal commit callbacks
1557 * particularly on low memory nodes or VMs
1559 rc = dt_sync(env, dt);
1561 CERROR("10b: sync failed: %d\n", rc);
1565 CWARN("10c: write %d more log records\n", 2 * llog_test_recnum);
1566 for (i = 0; i < 2 * llog_test_recnum; i++) {
1567 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1569 CERROR("10c: write %d records failed at #%d: %d\n",
1570 2*llog_test_recnum, i + 1, rc);
1575 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1576 rc = verify_handle("10c", cath, 5);
1581 * sync device to commit all recent LLOG changes to disk and avoid
1582 * to consume a huge space with delayed journal commit callbacks
1583 * particularly on low memory nodes or VMs
1585 rc = dt_sync(env, dt);
1587 CERROR("10c: sync failed: %d\n", rc);
1592 * fill last allocated plain LLOG and reach -ENOSPC condition
1593 * because no slot available in Catalog
1597 CWARN("10c: write %d more log records\n", llog_test_recnum);
1598 for (i = 0; i < llog_test_recnum; i++) {
1599 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1600 if (rc && rc != -ENOSPC) {
1601 CERROR("10c: write %d records failed at #%d: %d\n",
1602 llog_test_recnum, i + 1, rc);
1606 * after last added plain LLOG has filled up, all new
1607 * records add should fail with -ENOSPC
1609 if (rc == -ENOSPC) {
1617 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1618 CERROR("10c: all last records adds should have failed with"
1620 GOTO(out, rc = -EINVAL);
1623 CWARN("10c: wrote %d records then %d failed with ENOSPC\n", eok,
1626 /* make sure no new record in Catalog */
1627 rc = verify_handle("10c", cath, 5);
1631 /* Catalog should have reached its max size for test */
1632 rc = dt_attr_get(env, cath->lgh_obj, &la);
1634 CERROR("10c: failed to get catalog attrs: %d\n", rc);
1637 cat_max_size = la.la_size;
1640 * cancel all 1st plain llog records to empty it, this will also cause
1641 * its catalog entry to be freed for next forced wrap in 10e
1643 CWARN("10d: Cancel %d records, see one log zapped\n", llog_test_recnum);
1645 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1646 if (rc != -LLOG_EEMPTY) {
1647 CERROR("10d: process with llog_cancel_rec_cb failed: %d\n", rc);
1649 * need to indicate error if for any reason llog_test_recnum is
1657 CWARN("10d: print the catalog entries.. we expect 3\n");
1659 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1661 CERROR("10d: process with cat_print_cb failed: %d\n", rc);
1664 if (cat_counter != 3) {
1665 CERROR("10d: %d entries in catalog\n", cat_counter);
1666 GOTO(out, rc = -EINVAL);
1669 /* verify one down in catalog (+1 with hdr) */
1670 rc = verify_handle("10d", cath, 4);
1675 * sync device to commit all recent LLOG changes to disk and avoid
1676 * to consume a huge space with delayed journal commit callbacks
1677 * particularly on low memory nodes or VMs
1679 rc = dt_sync(env, dt);
1681 CERROR("10d: sync failed: %d\n", rc);
1687 CWARN("10e: write %d more log records\n", llog_test_recnum);
1688 for (i = 0; i < llog_test_recnum; i++) {
1689 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1690 if (rc && rc != -ENOSPC) {
1691 CERROR("10e: write %d records failed at #%d: %d\n",
1692 llog_test_recnum, i + 1, rc);
1696 * after last added plain LLOG has filled up, all new
1697 * records add should fail with -ENOSPC
1699 if (rc == -ENOSPC) {
1707 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1708 CERROR("10e: all last records adds should have failed with"
1710 GOTO(out, rc = -EINVAL);
1713 CWARN("10e: wrote %d records then %d failed with ENOSPC\n", eok,
1716 CWARN("10e: print the catalog entries.. we expect 4\n");
1718 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1721 CERROR("10e: process with cat_print_cb failed: %d\n", rc);
1724 if (cat_counter != 4) {
1725 CERROR("10e: %d entries in catalog\n", cat_counter);
1726 GOTO(out, rc = -EINVAL);
1729 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1730 rc = verify_handle("10e", cath, 5);
1734 /* verify catalog has wrap around */
1735 if (cath->lgh_last_idx > cath->lgh_hdr->llh_cat_idx) {
1736 CERROR("10e: catalog failed to wrap around\n");
1737 GOTO(out, rc = -EINVAL);
1740 rc = dt_attr_get(env, cath->lgh_obj, &la);
1742 CERROR("10e: failed to get catalog attrs: %d\n", rc);
1746 if (la.la_size != cat_max_size) {
1747 CERROR("10e: catalog size has changed after it has wrap around,"
1748 " current size = %llu, expected size = %llu\n",
1749 la.la_size, cat_max_size);
1750 GOTO(out, rc = -EINVAL);
1752 CWARN("10e: catalog successfully wrap around, last_idx %d, first %d\n",
1753 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1756 * sync device to commit all recent LLOG changes to disk and avoid
1757 * to consume a huge space with delayed journal commit callbacks
1758 * particularly on low memory nodes or VMs
1760 rc = dt_sync(env, dt);
1762 CERROR("10e: sync failed: %d\n", rc);
1767 * cancel more records to free one more slot in Catalog
1768 * see if it is re-allocated when adding more records
1770 CWARN("10f: Cancel %d records, see one log zapped\n", llog_test_recnum);
1772 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1773 if (rc != -LLOG_EEMPTY) {
1774 CERROR("10f: process with llog_cancel_rec_cb failed: %d\n", rc);
1776 * need to indicate error if for any reason llog_test_recnum is
1784 CWARN("10f: print the catalog entries.. we expect 3\n");
1786 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1789 CERROR("10f: process with cat_print_cb failed: %d\n", rc);
1792 if (cat_counter != 3) {
1793 CERROR("10f: %d entries in catalog\n", cat_counter);
1794 GOTO(out, rc = -EINVAL);
1797 /* verify one down in catalog (+1 with hdr) */
1798 rc = verify_handle("10f", cath, 4);
1803 * sync device to commit all recent LLOG changes to disk and avoid
1804 * to consume a huge space with delayed journal commit callbacks
1805 * particularly on low memory nodes or VMs
1807 rc = dt_sync(env, dt);
1809 CERROR("10f: sync failed: %d\n", rc);
1815 CWARN("10f: write %d more log records\n", llog_test_recnum);
1816 for (i = 0; i < llog_test_recnum; i++) {
1817 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1818 if (rc && rc != -ENOSPC) {
1819 CERROR("10f: write %d records failed at #%d: %d\n",
1820 llog_test_recnum, i + 1, rc);
1824 * after last added plain LLOG has filled up, all new
1825 * records add should fail with -ENOSPC
1827 if (rc == -ENOSPC) {
1835 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1836 CERROR("10f: all last records adds should have failed with"
1838 GOTO(out, rc = -EINVAL);
1841 CWARN("10f: wrote %d records then %d failed with ENOSPC\n", eok,
1844 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1845 rc = verify_handle("10f", cath, 5);
1849 /* verify lgh_last_idx = llh_cat_idx = 2 now */
1850 if (cath->lgh_last_idx != cath->lgh_hdr->llh_cat_idx ||
1851 cath->lgh_last_idx != 2) {
1852 CERROR("10f: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 2\n",
1853 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1854 GOTO(out, rc = -EINVAL);
1857 rc = dt_attr_get(env, cath->lgh_obj, &la);
1859 CERROR("10f: failed to get catalog attrs: %d\n", rc);
1863 if (la.la_size != cat_max_size) {
1864 CERROR("10f: catalog size has changed after it has wrap around,"
1865 " current size = %llu, expected size = %llu\n",
1866 la.la_size, cat_max_size);
1867 GOTO(out, rc = -EINVAL);
1871 * sync device to commit all recent LLOG changes to disk and avoid
1872 * to consume a huge space with delayed journal commit callbacks
1873 * particularly on low memory nodes or VMs
1875 rc = dt_sync(env, dt);
1877 CERROR("10f: sync failed: %d\n", rc);
1881 /* will llh_cat_idx also successfully wrap ? */
1884 * cancel all records in the plain LLOGs referenced by 2 last indexes in
1888 /* cancel more records to free one more slot in Catalog */
1889 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1891 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1892 if (rc != -LLOG_EEMPTY) {
1893 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1894 /* need to indicate error if for any reason llog_test_recnum is
1901 CWARN("10g: print the catalog entries.. we expect 3\n");
1903 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1906 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1909 if (cat_counter != 3) {
1910 CERROR("10g: %d entries in catalog\n", cat_counter);
1911 GOTO(out, rc = -EINVAL);
1914 /* verify one down in catalog (+1 with hdr) */
1915 rc = verify_handle("10g", cath, 4);
1920 * sync device to commit all recent LLOG changes to disk and avoid
1921 * to consume a huge space with delayed journal commit callbacks
1922 * particularly on low memory nodes or VMs
1924 rc = dt_sync(env, dt);
1926 CERROR("10g: sync failed: %d\n", rc);
1930 /* cancel more records to free one more slot in Catalog */
1931 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1933 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1934 if (rc != -LLOG_EEMPTY) {
1935 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1937 * need to indicate error if for any reason llog_test_recnum is
1945 CWARN("10g: print the catalog entries.. we expect 2\n");
1947 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1950 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1953 if (cat_counter != 2) {
1954 CERROR("10g: %d entries in catalog\n", cat_counter);
1955 GOTO(out, rc = -EINVAL);
1958 /* verify one down in catalog (+1 with hdr) */
1959 rc = verify_handle("10g", cath, 3);
1963 /* verify lgh_last_idx = 2 and llh_cat_idx = 0 now */
1964 if (cath->lgh_hdr->llh_cat_idx != 0 ||
1965 cath->lgh_last_idx != 2) {
1966 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 0\n",
1967 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1968 GOTO(out, rc = -EINVAL);
1972 * sync device to commit all recent LLOG changes to disk and avoid
1973 * to consume a huge space with delayed journal commit callbacks
1974 * particularly on low memory nodes or VMs
1976 rc = dt_sync(env, dt);
1978 CERROR("10g: sync failed: %d\n", rc);
1982 /* cancel more records to free one more slot in Catalog */
1983 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1985 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1986 if (rc != -LLOG_EEMPTY) {
1987 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1989 * need to indicate error if for any reason llog_test_recnum is
1997 CWARN("10g: print the catalog entries.. we expect 1\n");
1999 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
2002 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
2005 if (cat_counter != 1) {
2006 CERROR("10g: %d entries in catalog\n", cat_counter);
2007 GOTO(out, rc = -EINVAL);
2010 /* verify one down in catalog (+1 with hdr) */
2011 rc = verify_handle("10g", cath, 2);
2015 /* verify lgh_last_idx = 2 and llh_cat_idx = 1 now */
2016 if (cath->lgh_hdr->llh_cat_idx != 1 ||
2017 cath->lgh_last_idx != 2) {
2018 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 1\n",
2019 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
2020 GOTO(out, rc = -EINVAL);
2023 CWARN("10g: llh_cat_idx has also successfully wrapped!\n");
2026 * catalog has only one valid entry other slots has outdated
2027 * records. Trying to race the llog_thread_process with llog_add
2028 * llog_thread_process read buffer and loop record on it.
2029 * llog_add adds a record and mark a record in bitmap.
2030 * llog_thread_process process record with old data.
2033 struct llog_process_info lpi;
2034 struct lu_fid test_fid = {0};
2036 lpi.lpi_loghandle = cath;
2037 lpi.lpi_cb = cat_check_old_cb;
2038 lpi.lpi_catdata = NULL;
2039 lpi.lpi_cbdata = &test_fid;
2040 init_completion(&lpi.lpi_completion);
2042 kthread_run(llog_test_process_thread, &lpi, "llog_test_process_thread");
2044 msleep(1 * MSEC_PER_SEC / 2);
2047 CWARN("10h: write %d more log records\n", llog_test_recnum);
2048 for (i = 0; i < llog_test_recnum; i++) {
2049 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
2050 if (rc && rc != -ENOSPC) {
2051 CERROR("10h: write %d records failed at #%d: %d\n",
2052 llog_test_recnum, i + 1, rc);
2056 * after last added plain LLOG has filled up, all new
2057 * records add should fail with -ENOSPC
2059 if (rc == -ENOSPC) {
2067 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
2068 CERROR("10h: all last records adds should have failed with"
2070 GOTO(out, rc = -EINVAL);
2073 CWARN("10h: wrote %d records then %d failed with ENOSPC\n", eok,
2076 wait_for_completion(&lpi.lpi_completion);
2078 if (lpi.lpi_rc != 0) {
2079 CERROR("10h: race happened, old record was processed\n");
2080 GOTO(out, rc = -EINVAL);
2087 CWARN("10: put newly-created catalog\n");
2088 rc2 = llog_cat_close(env, cath);
2090 CERROR("10: close log %s failed: %d\n", name, rc2);
2095 llog_ctxt_put(ctxt);
2100 * -------------------------------------------------------------------------
2101 * Tests above, boring obd functions below
2102 * -------------------------------------------------------------------------
2104 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
2106 struct llog_handle *llh = NULL;
2107 struct llog_ctxt *ctxt;
2112 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
2115 sprintf(name, "%x", llog_test_rand);
2117 rc = llog_test_1(env, obd, name);
2119 GOTO(cleanup_ctxt, rc);
2121 rc = llog_test_2(env, obd, name, &llh);
2123 GOTO(cleanup_ctxt, rc);
2125 rc = llog_test_3(env, obd, llh);
2129 rc = llog_test_4(env, obd);
2133 rc = llog_test_5(env, obd);
2137 rc = llog_test_6(env, obd, name);
2141 rc = llog_test_7(env, obd);
2145 rc = llog_test_8(env, obd);
2149 rc = llog_test_9(env, obd);
2153 rc = llog_test_10(env, obd);
2158 err = llog_destroy(env, llh);
2160 CERROR("cleanup: llog_destroy failed: %d\n", err);
2161 llog_close(env, llh);
2165 llog_ctxt_put(ctxt);
2169 static int llog_test_cleanup(struct obd_device *obd)
2171 struct obd_device *tgt;
2177 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2181 tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
2182 rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
2184 CERROR("failed to llog_test_llog_finish: %d\n", rc);
2189 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
2191 struct obd_device *tgt;
2192 struct llog_ctxt *ctxt;
2193 struct dt_object *o;
2195 struct lu_context test_session;
2200 if (lcfg->lcfg_bufcount < 2) {
2201 CERROR("requires a TARGET OBD name\n");
2205 if (lcfg->lcfg_buflens[1] < 1) {
2206 CERROR("requires a TARGET OBD name\n");
2211 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2212 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2213 CERROR("target device not attached or not set up (%s)\n",
2214 lustre_cfg_string(lcfg, 1));
2218 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2222 rc = lu_context_init(&test_session, LCT_SERVER_SESSION);
2224 GOTO(cleanup_env, rc);
2225 test_session.lc_thread = (struct ptlrpc_thread *)current;
2226 lu_context_enter(&test_session);
2227 env.le_ses = &test_session;
2229 CWARN("Setup llog-test device over %s device\n",
2230 lustre_cfg_string(lcfg, 1));
2232 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2233 obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
2235 rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
2238 GOTO(cleanup_session, rc);
2240 /* use MGS llog dir for tests */
2241 ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
2244 llog_ctxt_put(ctxt);
2246 ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
2249 llog_ctxt_put(ctxt);
2251 llog_test_rand = prandom_u32();
2253 rc = llog_run_tests(&env, tgt);
2255 llog_test_cleanup(obd);
2257 lu_context_exit(&test_session);
2258 lu_context_fini(&test_session);
2264 static const struct obd_ops llog_obd_ops = {
2265 .o_owner = THIS_MODULE,
2266 .o_setup = llog_test_setup,
2267 .o_cleanup = llog_test_cleanup,
2270 static int __init llog_test_init(void)
2272 return class_register_type(&llog_obd_ops, NULL, false,
2276 static void __exit llog_test_exit(void)
2278 class_unregister_type("llog_test");
2281 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2282 MODULE_DESCRIPTION("Lustre Log test module");
2283 MODULE_VERSION(LUSTRE_VERSION_STRING);
2284 MODULE_LICENSE("GPL");
2286 module_init(llog_test_init);
2287 module_exit(llog_test_exit);