1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2012, 2017, Intel Corporation.
12 * This file is part of Lustre, http://www.lustre.org/
14 * lustre/kunit/llog_test.c
16 * Author: Phil Schwan <phil@clusterfs.com>
17 * Author: Mikhail Pershin <mike.pershin@intel.com>
21 #define DEBUG_SUBSYSTEM S_CLASS
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/kthread.h>
26 #include <linux/delay.h>
27 #include <linux/random.h>
29 #include <obd_class.h>
30 #include <lustre_fid.h>
31 #include <lustre_log.h>
33 /* This is slightly more than the number of records that can fit into a
34 * single llog file, because the llog_log_header takes up some of the
35 * space in the first block that cannot be used for the bitmap. */
36 static int llog_test_recnum = (LLOG_MIN_CHUNK_SIZE * 8);
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_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
53 if (test_bit_le(i, LLOG_HDR_BITMAP(llh->lgh_hdr))) {
59 /* check the llog is sane at first, llh_count and lgh_last_idx*/
60 if (llh->lgh_hdr->llh_count != active_recs) {
61 CERROR("%s: handle->count is %d, but there are %d recs found\n",
62 test, llh->lgh_hdr->llh_count, active_recs);
66 if (llh->lgh_last_idx != LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index ||
67 (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_CAT) &&
68 llh->lgh_last_idx < last_idx)) {
69 CERROR("%s: lgh_last_idx is %d (%d in the header), last found %d\n",
70 test, llh->lgh_last_idx,
71 LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index, last_idx);
75 /* finally checks against expected value from the caller */
76 if (active_recs != num_recs) {
77 CERROR("%s: expected %d active recs after write, found %d\n",
78 test, num_recs, active_recs);
85 /* Test named-log create/open, close */
86 static int llog_test_1(const struct lu_env *env,
87 struct obd_device *obd, char *name)
89 struct llog_handle *llh;
90 struct llog_ctxt *ctxt;
96 CWARN("1a: create a log with name: %s\n", name);
97 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
100 rc = llog_open_create(env, ctxt, &llh, NULL, name);
102 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
105 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, &uuid);
107 CERROR("1a: can't init llog handle: %d\n", rc);
111 rc = verify_handle("1", llh, 1);
113 CWARN("1b: close newly-created log\n");
115 rc2 = llog_close(env, llh);
117 CERROR("1b: close log %s failed: %d\n", name, rc2);
126 static int test_2_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
127 struct llog_rec_hdr *rec, void *data)
129 return LLOG_DEL_RECORD;
132 /* Test named-log reopen; returns opened log on success */
133 static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
134 char *name, struct llog_handle **llh)
136 struct llog_ctxt *ctxt;
137 struct llog_handle *lgh;
138 struct llog_logid logid;
140 struct llog_mini_rec lmr;
144 CWARN("2a: re-open a log with name: %s\n", name);
145 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
148 rc = llog_open(env, ctxt, llh, NULL, name, LLOG_OPEN_EXISTS);
150 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
154 rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
156 CERROR("2a: can't init llog handle: %d\n", rc);
157 GOTO(out_close_llh, rc);
160 rc = verify_handle("2", *llh, 1);
162 GOTO(out_close_llh, rc);
164 CWARN("2b: create a log without specified NAME & LOGID\n");
165 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
167 CERROR("2b: create log failed\n");
168 GOTO(out_close_llh, rc);
170 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
172 CERROR("2b: can't init llog handle: %d\n", rc);
178 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
179 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
181 /* Check llog header values are correct after record add/cancel */
182 CWARN("2b: write 1 llog records, check llh_count\n");
183 rc = llog_write(env, lgh, &lmr.lmr_hdr, LLOG_NEXT_IDX);
187 /* in-memory values after record addition */
188 rc = verify_handle("2b", lgh, 2);
192 /* re-open llog to read on-disk values */
193 llog_close(env, lgh);
195 CWARN("2c: re-open the log by LOGID and verify llh_count\n");
196 rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
198 CERROR("2c: re-open log by LOGID failed\n");
199 GOTO(out_close_llh, rc);
202 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
204 CERROR("2c: can't init llog handle: %d\n", rc);
208 /* check values just read from disk */
209 rc = verify_handle("2c", lgh, 2);
213 rc = llog_process(env, lgh, test_2_cancel_cb, NULL, NULL);
217 /* in-memory values */
218 rc = verify_handle("2c", lgh, 1);
222 /* re-open llog to get on-disk values */
223 llog_close(env, lgh);
225 rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
227 CERROR("2c: re-open log by LOGID failed\n");
228 GOTO(out_close_llh, rc);
231 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
233 CERROR("2c: can't init llog handle: %d\n", rc);
237 /* on-disk values after llog re-open */
238 rc = verify_handle("2c", lgh, 1);
242 CWARN("2d: destroy this log\n");
243 rc = llog_destroy(env, lgh);
245 CERROR("2d: destroy log failed\n");
247 llog_close(env, lgh);
250 llog_close(env, *llh);
257 static int test_3_rec_num;
258 static off_t test_3_rec_off;
259 static int test_3_paddings;
260 static int test_3_start_idx;
264 * - check lgh_cur_offset correctness
265 * - check record index consistency
266 * - modify each record in-place
267 * - add new record during *last_idx processing
269 static int test3_check_n_add_cb(const struct lu_env *env,
270 struct llog_handle *lgh,
271 struct llog_rec_hdr *rec, void *data)
273 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
274 int *last_rec = data;
275 unsigned cur_idx = test_3_start_idx + test_3_rec_num;
278 if (lgh->lgh_hdr->llh_flags & LLOG_F_IS_FIXSIZE) {
279 LASSERT(lgh->lgh_hdr->llh_size > 0);
280 if (lgh->lgh_cur_offset != lgh->lgh_hdr->llh_hdr.lrh_len +
281 (cur_idx - 1) * lgh->lgh_hdr->llh_size)
282 CERROR("Wrong record offset in cur_off: %llu, should be %u\n",
284 lgh->lgh_hdr->llh_hdr.lrh_len +
285 (cur_idx - 1) * lgh->lgh_hdr->llh_size);
287 size_t chunk_size = lgh->lgh_hdr->llh_hdr.lrh_len;
290 * For variable size records the start offset is unknown, trust
291 * the first value and check others are consistent with it.
293 if (test_3_rec_off == 0)
294 test_3_rec_off = lgh->lgh_cur_offset;
296 if (lgh->lgh_cur_offset != test_3_rec_off) {
297 __u64 tmp = lgh->lgh_cur_offset;
299 /* there can be padding record */
300 if ((do_div(tmp, chunk_size) == 0) &&
301 (lgh->lgh_cur_offset - test_3_rec_off <
302 rec->lrh_len + LLOG_MIN_REC_SIZE)) {
303 test_3_rec_off = lgh->lgh_cur_offset;
306 CERROR("Wrong record offset in cur_off: %llu"
307 ", should be %lld (rec len %u)\n",
309 (long long)test_3_rec_off,
313 test_3_rec_off += rec->lrh_len;
316 cur_idx += test_3_paddings;
317 if (cur_idx != rec->lrh_index)
318 CERROR("Record with wrong index was read: %u, expected %u\n",
319 rec->lrh_index, cur_idx);
321 /* modify all records in place */
322 lgr->lgr_gen.conn_cnt = rec->lrh_index;
323 rc = llog_write(env, lgh, rec, rec->lrh_index);
325 CERROR("cb_test_3: cannot modify record while processing\n");
328 * Add new record to the llog at *last_rec position one by one to
329 * check that last block is re-read during processing
331 if (cur_idx == *last_rec || cur_idx == (*last_rec + 1)) {
332 rc = llog_write(env, lgh, rec, LLOG_NEXT_IDX);
334 CERROR("cb_test_3: cannot add new record while "
342 /* Check in-place modifications were done for all records*/
343 static int test3_check_cb(const struct lu_env *env, struct llog_handle *lgh,
344 struct llog_rec_hdr *rec, void *data)
346 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
348 if (lgr->lgr_gen.conn_cnt != rec->lrh_index) {
349 CERROR("cb_test_3: record %u is not modified\n",
357 static int llog_test3_process(const struct lu_env *env,
358 struct llog_handle *lgh,
359 llog_cb_t cb, int start)
361 struct llog_process_cat_data cd;
362 int last_idx; /* new record will be injected here */
365 CWARN("test3: processing records from index %d to the end\n",
367 cd.lpcd_read_mode = LLOG_READ_MODE_NORMAL;
368 cd.lpcd_first_idx = start - 1;
369 cd.lpcd_last_idx = 0;
370 test_3_rec_num = test_3_paddings = 0;
371 last_idx = lgh->lgh_last_idx;
372 rc = llog_process(env, lgh, cb, &last_idx, &cd);
375 CWARN("test3: total %u records processed with %u paddings\n",
376 test_3_rec_num, test_3_paddings);
377 return test_3_rec_num;
380 /* Test plain llog functionality */
381 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
382 struct llog_handle *llh)
385 struct llog_rec_hdr *hdr = (void *)buf;
387 int num_recs = 1; /* 1 for the header */
392 hdr->lrh_len = sizeof(struct llog_gen_rec);
393 hdr->lrh_type = LLOG_GEN_REC;
394 llh->lgh_hdr->llh_size = sizeof(struct llog_gen_rec);
395 llh->lgh_hdr->llh_flags |= LLOG_F_IS_FIXSIZE;
398 * Fill the llog with 64-bytes records, use 1023 records,
399 * so last chunk will be partially full. Don't change this
400 * value until record size is changed.
402 CWARN("3a: write 1023 fixed-size llog records\n");
403 for (i = 0; i < 1023; i++) {
404 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
406 CERROR("3a: write 1023 records failed at #%d: %d\n",
413 rc = verify_handle("3a", llh, num_recs);
418 * Test fixed-size records processing:
419 * - search the needed index
420 * - go through all records from that index
421 * - check all indices are growing monotonically and exist
422 * - modify each record
424 * NB: test3_check_n_add adds two new records while processing
425 * after last record. There were 1023 records created so the last chunk
426 * misses exactly one record. Therefore one of new records will be
427 * the last in the current chunk and second causes the new chunk to be
431 test_3_start_idx = 501;
433 rc = llog_test3_process(env, llh, test3_check_n_add_cb,
438 /* extra record is created during llog_process() */
439 if (rc != expected) {
440 CERROR("3a: process total %d records but expect %d\n",
447 /* test modification in place */
448 rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
452 if (rc != expected) {
453 CERROR("3a: process total %d records but expect %d\n",
458 CWARN("3b: write 566 variable size llog records\n");
461 * Drop llh_size to 0 to mark llog as variable-size and write
462 * header to make this change permanent.
464 llh->lgh_hdr->llh_flags &= ~LLOG_F_IS_FIXSIZE;
465 llog_write(env, llh, &llh->lgh_hdr->llh_hdr, LLOG_HEADER_IDX);
467 hdr->lrh_type = OBD_CFG_REC;
470 * there are 1025 64-bytes records in llog already,
471 * the last chunk contains single record, i.e. 64 bytes.
472 * Each pair of variable size records is 200 bytes, so
473 * we will have the following distribution per chunks:
474 * block 1: 64 + 80(80/120) + 80 + 48(pad) = 81 iterations
475 * block 2: 80(120/80) + 120 + 72(pad) = 81 itereations
476 * block 3: 80(80/120) + 80 + 112(pad) = 81 iterations
477 * -- the same as block 2 again and so on.
478 * block 7: 80(80/120) = 80 iterations and 192 bytes remain
479 * Total 6 * 81 + 80 = 566 itereations.
480 * Callback will add another 120 bytes in the end of the last chunk
481 * and another 120 bytes will cause padding (72 bytes) plus 120
482 * bytes in the new block.
484 for (i = 0; i < 566; i++) {
490 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
492 CERROR("3b: write 566 records failed at #%d: %d\n",
499 rc = verify_handle("3b", llh, num_recs);
503 test_3_start_idx = 1026;
505 rc = llog_test3_process(env, llh, test3_check_n_add_cb,
510 if (rc != expected) {
511 CERROR("3b: process total %d records but expect %d\n",
518 /* test modification in place */
519 rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
523 if (rc != expected) {
524 CERROR("3b: process total %d records but expect %d\n",
529 CWARN("3c: write records with variable size until BITMAP_SIZE, "
531 while (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
532 if ((num_recs % 2) == 0)
537 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
541 CERROR("3c: write recs failed at #%d: %d\n",
549 CWARN("3c: write record more than BITMAP size!\n");
552 CWARN("3c: wrote %d more records before end of llog is reached\n",
555 rc = verify_handle("3c", llh, num_recs);
560 /* Test catalogue additions */
561 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
563 struct llog_handle *cath, *llh;
565 int rc, rc2, i, buflen;
566 struct llog_mini_rec lmr;
567 struct llog_cookie cookie;
568 struct llog_ctxt *ctxt;
571 struct llog_rec_hdr *rec;
575 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
578 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
579 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
581 sprintf(name, "%x", llog_test_rand + 1);
582 CWARN("4a: create a catalog log with name: %s\n", name);
583 rc = llog_open_create(env, ctxt, &cath, NULL, name);
585 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
586 GOTO(ctxt_release, rc);
588 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
590 CERROR("4a: can't init llog handle: %d\n", rc);
595 cat_logid = cath->lgh_id;
597 CWARN("4b: write 1 record into the catalog\n");
598 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
600 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
604 rc = verify_handle("4b", cath, 2);
608 rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
612 /* estimate the max number of record for the plain llog
613 * cause it depends on disk size
615 llh = cath->u.chd.chd_current_log;
616 if (llh->lgh_max_size != 0) {
617 llog_test_recnum = (llh->lgh_max_size -
618 sizeof(struct llog_log_hdr)) / LLOG_MIN_REC_SIZE;
621 if (llog_test_recnum >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr))
622 llog_test_recnum = LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1;
624 CWARN("4c: cancel 1 log record\n");
625 rc = llog_cat_cancel_records(env, cath, 1, &cookie);
627 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
632 rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
636 CWARN("4d: write %d more log records\n", llog_test_recnum);
637 for (i = 0; i < llog_test_recnum; i++) {
638 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
640 CERROR("4d: write %d records failed at #%d: %d\n",
641 llog_test_recnum, i + 1, rc);
647 /* make sure new plain llog appears */
648 rc = verify_handle("4d", cath, 3);
652 CWARN("4e: add 5 large records, one record per block\n");
653 buflen = LLOG_MIN_CHUNK_SIZE;
654 OBD_ALLOC(buf, buflen);
656 GOTO(out, rc = -ENOMEM);
657 for (i = 0; i < 5; i++) {
659 rec->lrh_len = buflen;
660 rec->lrh_type = OBD_CFG_REC;
661 rc = llog_cat_add(env, cath, rec, NULL);
663 CERROR("4e: write 5 records failed at #%d: %d\n",
670 OBD_FREE(buf, buflen);
672 CWARN("4f: put newly-created catalog\n");
673 rc2 = llog_cat_close(env, cath);
675 CERROR("4: close log %s failed: %d\n", name, rc2);
684 static int cat_counter;
686 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
687 struct llog_rec_hdr *rec, void *data)
689 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
690 struct lu_fid fid = {0};
692 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
693 CERROR("invalid record in catalog\n");
697 logid_to_fid(&lir->lid_id, &fid);
699 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
700 rec->lrh_index, PFID(&fid),
701 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
708 static int plain_counter;
710 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
711 struct llog_rec_hdr *rec, void *data)
713 struct lu_fid fid = {0};
715 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
716 CERROR("log is not plain\n");
720 logid_to_fid(&llh->lgh_id, &fid);
722 CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
723 rec->lrh_index, PFID(&fid));
730 static int cancel_count;
732 static int llog_cancel_rec_cb(const struct lu_env *env,
733 struct llog_handle *llh,
734 struct llog_rec_hdr *rec, void *data)
736 struct llog_cookie cookie;
738 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
739 CERROR("log is not plain\n");
743 cookie.lgc_lgl = llh->lgh_id;
744 cookie.lgc_index = rec->lrh_index;
746 llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
748 if (cancel_count == llog_test_recnum)
749 RETURN(-LLOG_EEMPTY);
753 /* Test log and catalogue processing */
754 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
756 struct llog_handle *llh = NULL;
759 struct llog_mini_rec lmr;
760 struct llog_ctxt *ctxt;
764 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
767 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
768 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
770 CWARN("5a: re-open catalog by id\n");
771 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
773 CERROR("5a: llog_create with logid failed: %d\n", rc);
777 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
779 CERROR("5a: can't init llog handle: %d\n", rc);
783 CWARN("5b: print the catalog entries.. we expect 2\n");
785 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
787 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
790 if (cat_counter != 2) {
791 CERROR("5b: %d entries in catalog\n", cat_counter);
792 GOTO(out, rc = -EINVAL);
795 CWARN("5c: Cancel %d records, see one log zapped\n", llog_test_recnum);
797 rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
798 if (rc != -LLOG_EEMPTY) {
799 CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
803 CWARN("5c: print the catalog entries.. we expect 1\n");
805 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
807 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
810 if (cat_counter != 1) {
811 CERROR("5c: %d entries in catalog\n", cat_counter);
812 GOTO(out, rc = -EINVAL);
815 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
816 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
818 CERROR("5d: add record to the log with many canceled empty "
823 CWARN("5e: print plain log entries.. expect 6\n");
825 rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
827 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
830 if (plain_counter != 6) {
831 CERROR("5e: found %d records\n", plain_counter);
832 GOTO(out, rc = -EINVAL);
835 CWARN("5f: print plain log entries reversely.. expect 6\n");
837 rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
839 CERROR("5f: reversely process with plain_print_cb failed: "
843 if (plain_counter != 6) {
844 CERROR("5f: found %d records\n", plain_counter);
845 GOTO(out, rc = -EINVAL);
849 CWARN("5g: close re-opened catalog\n");
850 rc2 = llog_cat_close(env, llh);
852 CERROR("5g: close log %s failed: %d\n", name, rc2);
862 /* Test client api; open log by name and process */
863 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
866 struct obd_device *mgc_obd;
867 struct llog_ctxt *ctxt;
868 struct obd_uuid *mgs_uuid;
869 struct obd_export *exp;
870 struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
871 struct llog_handle *llh = NULL;
872 struct llog_ctxt *nctxt;
875 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
877 mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
879 CWARN("6a: re-open log %s using client API\n", name);
880 mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
881 if (mgc_obd == NULL) {
882 CERROR("6a: no MGC devices connected to %s found.\n",
884 GOTO(ctxt_release, rc = -ENOENT);
887 rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
888 NULL /* obd_connect_data */, NULL);
889 if (rc != -EALREADY) {
890 CERROR("6a: connect on connected MGC (%s) failed to return"
891 " -EALREADY\n", mgc_obd->obd_name);
894 GOTO(ctxt_release, rc = -EINVAL);
897 nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
898 rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
900 CERROR("6a: llog_open failed %d\n", rc);
904 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
906 CERROR("6a: llog_init_handle failed %d\n", rc);
910 plain_counter = 1; /* llog header is first record */
911 CWARN("6b: process log %s using client API\n", name);
912 rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
914 CERROR("6b: llog_process failed %d\n", rc);
915 CWARN("6b: processed %d records\n", plain_counter);
917 rc = verify_handle("6b", llh, plain_counter);
921 plain_counter = 1; /* llog header is first record */
922 CWARN("6c: process log %s reversely using client API\n", name);
923 rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
925 CERROR("6c: llog_reverse_process failed %d\n", rc);
926 CWARN("6c: processed %d records\n", plain_counter);
928 rc = verify_handle("6c", llh, plain_counter);
933 rc2 = llog_close(env, llh);
935 CERROR("6: llog_close failed: rc = %d\n", rc2);
940 llog_ctxt_put(nctxt);
947 struct llog_rec_hdr lrh; /* common header */
948 struct llog_logid_rec llr; /* LLOG_LOGID_MAGIC */
949 struct llog_unlink64_rec lur; /* MDS_UNLINK64_REC */
950 struct llog_setattr64_rec lsr64; /* MDS_SETATTR64_REC */
951 struct llog_setattr64_rec_v2 lsr64_v2; /* MDS_SETATTR64_REC */
952 struct llog_size_change_rec lscr; /* OST_SZ_REC */
953 struct llog_changelog_rec lcr; /* CHANGELOG_REC */
954 struct llog_changelog_user_rec2 lcur; /* CHANGELOG_USER_REC2 */
955 struct llog_gen_rec lgr; /* LLOG_GEN_REC */
958 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
959 struct llog_rec_hdr *rec, void *data)
961 struct lu_fid fid = {0};
963 logid_to_fid(&llh->lgh_id, &fid);
965 CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
966 rec->lrh_type, rec->lrh_index, PFID(&fid));
972 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
973 struct llog_rec_hdr *rec, void *data)
976 /* test LLOG_DEL_RECORD is working */
977 return LLOG_DEL_RECORD;
980 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
982 struct llog_handle *llh;
983 int rc = 0, i, process_count;
988 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
990 CERROR("7_sub: create log failed\n");
994 rc = llog_init_handle(env, llh,
995 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
998 CERROR("7_sub: can't init llog handle: %d\n", rc);
1001 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
1002 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1003 if (rc == -ENOSPC) {
1005 } else if (rc < 0) {
1006 CERROR("7_sub: write recs failed at #%d: %d\n",
1008 GOTO(out_close, rc);
1012 if (rc != -ENOSPC) {
1013 CWARN("7_sub: write record more than BITMAP size!\n");
1014 GOTO(out_close, rc = -EINVAL);
1017 rc = verify_handle("7_sub", llh, num_recs + 1);
1019 CERROR("7_sub: verify handle failed: %d\n", rc);
1020 GOTO(out_close, rc);
1022 if (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1)
1023 CWARN("7_sub: records are not aligned, written %d from %u\n",
1024 num_recs, LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
1027 rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
1029 CERROR("7_sub: llog process failed: %d\n", rc);
1030 GOTO(out_close, rc);
1032 process_count = plain_counter;
1033 if (process_count != num_recs) {
1034 CERROR("7_sub: processed %d records from %d total\n",
1035 process_count, num_recs);
1036 GOTO(out_close, rc = -EINVAL);
1040 rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
1041 if (rc && rc != LLOG_DEL_PLAIN) {
1042 CERROR("7_sub: reverse llog process failed: %d\n", rc);
1043 GOTO(out_close, rc);
1045 if (process_count != plain_counter) {
1046 CERROR("7_sub: Reverse/direct processing found different number of records: %d/%d\n",
1047 plain_counter, process_count);
1048 GOTO(out_close, rc = -EINVAL);
1050 if (llog_exist(llh)) {
1051 CERROR("7_sub: llog exists but should be zapped\n");
1052 GOTO(out_close, rc = -EEXIST);
1055 rc = verify_handle("7_sub", llh, 1);
1058 llog_destroy(env, llh);
1059 llog_close(env, llh);
1063 /* Test all llog records writing and processing */
1064 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
1066 struct llog_ctxt *ctxt;
1071 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1073 CWARN("7a: test llog_logid_rec\n");
1074 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1075 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1076 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1078 rc = llog_test_7_sub(env, ctxt);
1080 CERROR("7a: llog_logid_rec test failed\n");
1084 CWARN("7b: test llog_unlink64_rec\n");
1085 llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
1086 llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
1087 llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
1089 rc = llog_test_7_sub(env, ctxt);
1091 CERROR("7b: llog_unlink_rec test failed\n");
1095 CWARN("7c: test llog_setattr64_rec\n");
1096 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
1097 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
1098 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1100 rc = llog_test_7_sub(env, ctxt);
1102 CERROR("7c: llog_setattr64_rec test failed\n");
1106 CWARN("7d: test llog_size_change_rec\n");
1107 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1108 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1109 llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
1111 rc = llog_test_7_sub(env, ctxt);
1113 CERROR("7d: llog_size_change_rec test failed\n");
1117 CWARN("7e: test llog_changelog_rec\n");
1118 /* Direct access to cr_do_not_use: peculiar case for this test */
1119 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1120 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1121 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1123 rc = llog_test_7_sub(env, ctxt);
1125 CERROR("7e: llog_changelog_rec test failed\n");
1129 CWARN("7f: test llog_changelog_user_rec2\n");
1130 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1131 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1132 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC2;
1134 rc = llog_test_7_sub(env, ctxt);
1136 CERROR("7f: llog_changelog_user_rec test failed\n");
1140 CWARN("7g: test llog_gen_rec\n");
1141 llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
1142 llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
1143 llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
1145 rc = llog_test_7_sub(env, ctxt);
1147 CERROR("7g: llog_size_change_rec test failed\n");
1151 CWARN("7h: test llog_setattr64_rec_v2\n");
1152 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64_v2);
1153 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64_v2);
1154 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1156 rc = llog_test_7_sub(env, ctxt);
1158 CERROR("7h: llog_setattr64_rec_v2 test failed\n");
1162 llog_ctxt_put(ctxt);
1166 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
1167 struct llog_rec_hdr *rec, void *data)
1173 static int llog_zeroes(const struct lu_env *env, struct dt_object *o,
1174 __u64 start, __u64 end)
1178 struct dt_device *d;
1186 OBD_ALLOC(buf, end - start);
1191 d = lu2dt_dev(o->do_lu.lo_dev);
1194 rc = dt_attr_get(env, o, &la);
1198 CDEBUG(D_OTHER, "llog size %llu\n", la.la_size);
1199 rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
1200 if (la.la_size < end) {
1201 CERROR("llog size %llu is small for punch at [%llu;%llu]\n",
1202 la.la_size, start, end);
1206 th = dt_trans_create(env, d);
1208 GOTO(free, rc = PTR_ERR(th));
1211 lb.lb_len = end - start;
1212 rc = dt_declare_write(env, o, &lb, pos, th);
1216 rc = dt_trans_start_local(env, d, th);
1220 rc = dt_write(env, o, &lb, &pos, th);
1224 dt_trans_stop(env, d, th);
1226 OBD_FREE(buf, end - start);
1231 struct llog_test8_rec {
1232 struct llog_rec_hdr ltr_hdr;
1234 struct llog_rec_tail ltr_tail;
1235 } __attribute__((packed));
1237 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
1239 struct llog_handle *llh = NULL;
1243 struct llog_test8_rec ltr;
1244 struct llog_ctxt *ctxt;
1245 struct dt_object *obj = NULL;
1247 int reclen = sizeof(ltr);
1251 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1254 /* simulate generic llog records with 256-bytes size */
1255 ltr.ltr_hdr.lrh_len = ltr.ltr_tail.lrt_len = reclen;
1256 ltr.ltr_hdr.lrh_type = LLOG_OP_MAGIC;
1258 CWARN("8a: fill the first plain llog\n");
1259 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1261 CERROR("8a: llog_create with logid failed: %d\n", rc);
1265 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1267 CERROR("8a: can't init llog handle: %d\n", rc);
1272 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1274 CERROR("8a: process with test_8_cb failed: %d\n", rc);
1277 orig_counter = plain_counter;
1279 for (i = 0; i < 20; i++) {
1280 rc = llog_cat_add(env, llh, <r.ltr_hdr, NULL);
1282 CERROR("5a: add record failed\n");
1286 CWARN("8b: first llog "DFID"\n",
1287 PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1289 /* get llog index in catalog to clear it later */
1290 plain_pos = (llh->lgh_last_idx - 1) * sizeof(struct llog_logid_rec);
1291 /* destroy plain llog to don't leave it orphaned */
1292 list_del_init(&llh->u.chd.chd_current_log->u.phd.phd_entry);
1293 llog_destroy(env, llh->u.chd.chd_current_log);
1294 llog_close(env, llh->u.chd.chd_current_log);
1295 llh->u.chd.chd_current_log = NULL;
1297 rc2 = llog_cat_close(env, llh);
1299 CERROR("8a: close log %s failed: %d\n", name, rc2);
1305 CWARN("8b: fill the second plain llog\n");
1306 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1308 CERROR("8b: llog_create with logid failed: %d\n", rc);
1312 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1314 CERROR("8b: can't init llog handle: %d\n", rc);
1318 for (i = 0; i < 100; i++) {
1319 rc = llog_cat_add(env, llh, <r.ltr_hdr, NULL);
1321 CERROR("8b: add record failed\n");
1325 /* grab the current plain llog, we'll corrupt it later */
1326 obj = llh->u.chd.chd_current_log->lgh_obj;
1328 lu_object_get(&obj->do_lu);
1329 CWARN("8b: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1331 /* must lost all 20 records */
1332 CWARN("8b: clean first llog record in catalog\n");
1333 llog_zeroes(env, llh->lgh_obj, 8192 + plain_pos,
1334 8192 + plain_pos + sizeof(struct llog_logid_rec));
1336 rc2 = llog_cat_close(env, llh);
1338 CERROR("8b: close log %s failed: %d\n", name, rc2);
1344 /* lost 28 records, from 5 to 32 in block */
1345 CWARN("8c: corrupt first chunk in the middle\n");
1346 llog_zeroes(env, obj, 8192 + reclen * 4, 8192 + reclen * 10);
1347 /* lost whole chunk - 32 records */
1348 CWARN("8c: corrupt second chunk at start\n");
1349 llog_zeroes(env, obj, 16384, 16384 + reclen);
1351 CWARN("8d: count survived records\n");
1352 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1354 CERROR("8d: llog_create with logid failed: %d\n", rc);
1358 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1360 CERROR("8d: can't init llog handle: %d\n", rc);
1365 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1367 CERROR("8d: process with test_8_cb failed: %d\n", rc);
1371 /* if llog processing skips bad data as expected then 80
1372 * records from 120 should be lost
1374 if (orig_counter + 120 - 80 != plain_counter) {
1375 CERROR("found %d records (expected %d)\n", plain_counter,
1376 orig_counter + 120 - 80);
1381 CWARN("8d: close re-opened catalog\n");
1382 rc2 = llog_cat_close(env, llh);
1384 CERROR("8d: close log %s failed: %d\n", name, rc2);
1389 llog_ctxt_put(ctxt);
1392 dt_object_put(env, obj);
1397 static int llog_test_9_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
1399 struct llog_handle *llh;
1405 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1407 CERROR("9_sub: create log failed\n");
1411 rc = llog_init_handle(env, llh,
1412 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1415 CERROR("9_sub: can't init llog handle: %d\n", rc);
1416 GOTO(out_close, rc);
1419 logid_to_fid(&llh->lgh_id, &fid);
1420 fid_to_logid(&fid, &llog_records.llr.lid_id);
1421 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1423 CERROR("9_sub: write recs failed at #1: %d\n", rc);
1424 GOTO(out_close, rc);
1426 /* The below message is checked in sanity.sh test_60a (run-llog.sh) */
1427 CWARN("9_sub: record type %x in log "DFID_NOBRACE"\n",
1428 llog_records.lrh.lrh_type, PFID(&fid));
1430 llog_close(env, llh);
1434 /* Prepare different types of llog records for llog_reader test*/
1435 static int llog_test_9(const struct lu_env *env, struct obd_device *obd)
1437 struct llog_ctxt *ctxt;
1442 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1444 CWARN("9a: test llog_logid_rec\n");
1445 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1446 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1447 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1449 rc = llog_test_9_sub(env, ctxt);
1451 CERROR("9a: llog_logid_rec test failed\n");
1455 CWARN("9b: test llog_obd_cfg_rec\n");
1456 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1457 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1458 llog_records.lscr.lsc_hdr.lrh_type = OBD_CFG_REC;
1460 rc = llog_test_9_sub(env, ctxt);
1462 CERROR("9b: llog_obd_cfg_rec test failed\n");
1466 CWARN("9c: test llog_changelog_rec\n");
1467 /* Direct access to cr_do_not_use: peculiar case for this test */
1468 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1469 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1470 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1472 rc = llog_test_9_sub(env, ctxt);
1474 CERROR("9c: llog_changelog_rec test failed\n");
1478 CWARN("9d: test llog_changelog_user_rec2\n");
1479 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1480 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1481 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1483 rc = llog_test_9_sub(env, ctxt);
1485 CERROR("9d: llog_changelog_user_rec test failed\n");
1490 llog_ctxt_put(ctxt);
1494 struct llog_process_info {
1495 struct llog_handle *lpi_loghandle;
1500 struct completion lpi_completion;
1501 const struct lu_env *lpi_env;
1502 struct task_struct *lpi_reftask;
1506 static int llog_test_process_thread(void *arg)
1508 struct llog_process_info *lpi = arg;
1511 rc = llog_cat_process_or_fork(NULL, lpi->lpi_loghandle, lpi->lpi_cb,
1512 NULL, lpi->lpi_cbdata, 1, 0, true);
1514 complete(&lpi->lpi_completion);
1515 msleep(MSEC_PER_SEC / 2);
1518 CWARN("10h: Error during catalog processing %d\n", rc);
1522 static int cat_check_old_cb(const struct lu_env *env, struct llog_handle *llh,
1523 struct llog_rec_hdr *rec, void *data)
1525 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
1526 struct lu_fid fid = {0};
1527 struct lu_fid *prev_fid = data;
1529 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
1530 CERROR("invalid record in catalog\n");
1534 logid_to_fid(&lir->lid_id, &fid);
1536 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
1537 rec->lrh_index, PFID(&fid),
1538 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
1540 if (prev_fid->f_oid > fid.f_oid) {
1541 CWARN("processing old record, fail\n");
1542 prev_fid->f_oid = 0xbad;
1543 RETURN(-LLOG_EEMPTY);
1546 if (prev_fid->f_oid == 0) {
1547 cfs_fail_loc = CFS_FAIL_ONCE | OBD_FAIL_LLOG_PROCESS_TIMEOUT;
1548 cfs_fail_val = (unsigned int) (llh->lgh_id.lgl_oi.oi.oi_id &
1550 msleep(1 * MSEC_PER_SEC);
1557 /* test catalog wrap around */
1558 static int llog_test_10(const struct lu_env *env, struct obd_device *obd)
1560 struct llog_handle *cath;
1562 int rc, rc2, i, enospc, eok;
1563 struct llog_mini_rec lmr;
1564 struct llog_ctxt *ctxt;
1567 struct dt_device *dt;
1571 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1574 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1575 lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
1577 snprintf(name, sizeof(name), "%x", llog_test_rand + 2);
1578 CWARN("10a: create a catalog log with name: %s\n", name);
1579 rc = llog_open_create(env, ctxt, &cath, NULL, name);
1581 CERROR("10a: llog_create with name %s failed: %d\n", name, rc);
1582 GOTO(ctxt_release, rc);
1584 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
1586 CERROR("10a: can't init llog handle: %d\n", rc);
1590 cat_logid = cath->lgh_id;
1591 dt = lu2dt_dev(cath->lgh_obj->do_lu.lo_dev);
1594 * sync device to commit all recent LLOG changes to disk and avoid
1595 * to consume a huge space with delayed journal commit callbacks
1596 * particularly on low memory nodes or VMs
1598 rc = dt_sync(env, dt);
1600 CERROR("10c: sync failed: %d\n", rc);
1604 /* force catalog wrap for 5th plain LLOG */
1605 cfs_fail_loc = CFS_FAIL_SKIP|OBD_FAIL_CAT_RECORDS;
1608 CWARN("10b: write %d log records\n", llog_test_recnum);
1609 for (i = 0; i < llog_test_recnum; i++) {
1610 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1612 CERROR("10b: write %d records failed at #%d: %d\n",
1613 llog_test_recnum, i + 1, rc);
1618 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1619 rc = verify_handle("10b", cath, 3);
1624 * sync device to commit all recent LLOG changes to disk and avoid
1625 * to consume a huge space with delayed journal commit callbacks
1626 * particularly on low memory nodes or VMs
1628 rc = dt_sync(env, dt);
1630 CERROR("10b: sync failed: %d\n", rc);
1634 CWARN("10c: write %d more log records\n", 2 * llog_test_recnum);
1635 for (i = 0; i < 2 * llog_test_recnum; i++) {
1636 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1638 CERROR("10c: write %d records failed at #%d: %d\n",
1639 2*llog_test_recnum, i + 1, rc);
1644 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1645 rc = verify_handle("10c", cath, 5);
1650 * sync device to commit all recent LLOG changes to disk and avoid
1651 * to consume a huge space with delayed journal commit callbacks
1652 * particularly on low memory nodes or VMs
1654 rc = dt_sync(env, dt);
1656 CERROR("10c: sync failed: %d\n", rc);
1661 * fill last allocated plain LLOG and reach -ENOSPC condition
1662 * because no slot available in Catalog
1666 CWARN("10c: write %d more log records\n", llog_test_recnum);
1667 for (i = 0; i < llog_test_recnum; i++) {
1668 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1669 if (rc && rc != -ENOSPC) {
1670 CERROR("10c: write %d records failed at #%d: %d\n",
1671 llog_test_recnum, i + 1, rc);
1675 * after last added plain LLOG has filled up, all new
1676 * records add should fail with -ENOSPC
1678 if (rc == -ENOSPC) {
1686 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1687 CERROR("10c: all last records adds should have failed with"
1689 GOTO(out, rc = -EINVAL);
1692 CWARN("10c: wrote %d records then %d failed with ENOSPC\n", eok,
1695 /* make sure no new record in Catalog */
1696 rc = verify_handle("10c", cath, 5);
1700 /* Catalog should have reached its max size for test */
1701 rc = dt_attr_get(env, cath->lgh_obj, &la);
1703 CERROR("10c: failed to get catalog attrs: %d\n", rc);
1706 cat_max_size = la.la_size;
1709 * cancel all 1st plain llog records to empty it, this will also cause
1710 * its catalog entry to be freed for next forced wrap in 10e
1712 CWARN("10d: Cancel %d records, see one log zapped\n", llog_test_recnum);
1714 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1715 if (rc != -LLOG_EEMPTY) {
1716 CERROR("10d: process with llog_cancel_rec_cb failed: %d\n", rc);
1718 * need to indicate error if for any reason llog_test_recnum is
1726 CWARN("10d: print the catalog entries.. we expect 3\n");
1728 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1730 CERROR("10d: process with cat_print_cb failed: %d\n", rc);
1733 if (cat_counter != 3) {
1734 CERROR("10d: %d entries in catalog\n", cat_counter);
1735 GOTO(out, rc = -EINVAL);
1738 /* verify one down in catalog (+1 with hdr) */
1739 rc = verify_handle("10d", cath, 4);
1744 * sync device to commit all recent LLOG changes to disk and avoid
1745 * to consume a huge space with delayed journal commit callbacks
1746 * particularly on low memory nodes or VMs
1748 rc = dt_sync(env, dt);
1750 CERROR("10d: sync failed: %d\n", rc);
1756 CWARN("10e: write %d more log records\n", llog_test_recnum);
1757 for (i = 0; i < llog_test_recnum; i++) {
1758 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1759 if (rc && rc != -ENOSPC) {
1760 CERROR("10e: write %d records failed at #%d: %d\n",
1761 llog_test_recnum, i + 1, rc);
1765 * after last added plain LLOG has filled up, all new
1766 * records add should fail with -ENOSPC
1768 if (rc == -ENOSPC) {
1776 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1777 CERROR("10e: all last records adds should have failed with"
1779 GOTO(out, rc = -EINVAL);
1782 CWARN("10e: wrote %d records then %d failed with ENOSPC\n", eok,
1785 CWARN("10e: print the catalog entries.. we expect 4\n");
1787 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1790 CERROR("10e: process with cat_print_cb failed: %d\n", rc);
1793 if (cat_counter != 4) {
1794 CERROR("10e: %d entries in catalog\n", cat_counter);
1795 GOTO(out, rc = -EINVAL);
1798 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1799 rc = verify_handle("10e", cath, 5);
1803 /* verify catalog has wrap around */
1804 if (cath->lgh_last_idx > cath->lgh_hdr->llh_cat_idx) {
1805 CERROR("10e: catalog failed to wrap around\n");
1806 GOTO(out, rc = -EINVAL);
1809 rc = dt_attr_get(env, cath->lgh_obj, &la);
1811 CERROR("10e: failed to get catalog attrs: %d\n", rc);
1815 if (la.la_size != cat_max_size) {
1816 CERROR("10e: catalog size has changed after it has wrap around,"
1817 " current size = %llu, expected size = %llu\n",
1818 la.la_size, cat_max_size);
1819 GOTO(out, rc = -EINVAL);
1821 CWARN("10e: catalog successfully wrap around, last_idx %d, first %d\n",
1822 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1825 * sync device to commit all recent LLOG changes to disk and avoid
1826 * to consume a huge space with delayed journal commit callbacks
1827 * particularly on low memory nodes or VMs
1829 rc = dt_sync(env, dt);
1831 CERROR("10e: sync failed: %d\n", rc);
1836 * cancel more records to free one more slot in Catalog
1837 * see if it is re-allocated when adding more records
1839 CWARN("10f: Cancel %d records, see one log zapped\n", llog_test_recnum);
1841 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1842 if (rc != -LLOG_EEMPTY) {
1843 CERROR("10f: process with llog_cancel_rec_cb failed: %d\n", rc);
1845 * need to indicate error if for any reason llog_test_recnum is
1853 CWARN("10f: print the catalog entries.. we expect 3\n");
1855 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1858 CERROR("10f: process with cat_print_cb failed: %d\n", rc);
1861 if (cat_counter != 3) {
1862 CERROR("10f: %d entries in catalog\n", cat_counter);
1863 GOTO(out, rc = -EINVAL);
1866 /* verify one down in catalog (+1 with hdr) */
1867 rc = verify_handle("10f", cath, 4);
1872 * sync device to commit all recent LLOG changes to disk and avoid
1873 * to consume a huge space with delayed journal commit callbacks
1874 * particularly on low memory nodes or VMs
1876 rc = dt_sync(env, dt);
1878 CERROR("10f: sync failed: %d\n", rc);
1884 CWARN("10f: write %d more log records\n", llog_test_recnum);
1885 for (i = 0; i < llog_test_recnum; i++) {
1886 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1887 if (rc && rc != -ENOSPC) {
1888 CERROR("10f: write %d records failed at #%d: %d\n",
1889 llog_test_recnum, i + 1, rc);
1893 * after last added plain LLOG has filled up, all new
1894 * records add should fail with -ENOSPC
1896 if (rc == -ENOSPC) {
1904 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1905 CERROR("10f: all last records adds should have failed with"
1907 GOTO(out, rc = -EINVAL);
1910 CWARN("10f: wrote %d records then %d failed with ENOSPC\n", eok,
1913 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1914 rc = verify_handle("10f", cath, 5);
1918 /* verify lgh_last_idx = llh_cat_idx = 2 now */
1919 if (cath->lgh_last_idx != cath->lgh_hdr->llh_cat_idx ||
1920 cath->lgh_last_idx != 2) {
1921 CERROR("10f: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 2\n",
1922 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1923 GOTO(out, rc = -EINVAL);
1926 rc = dt_attr_get(env, cath->lgh_obj, &la);
1928 CERROR("10f: failed to get catalog attrs: %d\n", rc);
1932 if (la.la_size != cat_max_size) {
1933 CERROR("10f: catalog size has changed after it has wrap around,"
1934 " current size = %llu, expected size = %llu\n",
1935 la.la_size, cat_max_size);
1936 GOTO(out, rc = -EINVAL);
1940 * sync device to commit all recent LLOG changes to disk and avoid
1941 * to consume a huge space with delayed journal commit callbacks
1942 * particularly on low memory nodes or VMs
1944 rc = dt_sync(env, dt);
1946 CERROR("10f: sync failed: %d\n", rc);
1950 /* will llh_cat_idx also successfully wrap ? */
1953 * cancel all records in the plain LLOGs referenced by 2 last indexes in
1957 /* cancel more records to free one more slot in Catalog */
1958 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1960 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1961 if (rc != -LLOG_EEMPTY) {
1962 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1963 /* need to indicate error if for any reason llog_test_recnum is
1970 CWARN("10g: print the catalog entries.. we expect 3\n");
1972 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1975 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1978 if (cat_counter != 3) {
1979 CERROR("10g: %d entries in catalog\n", cat_counter);
1980 GOTO(out, rc = -EINVAL);
1983 /* verify one down in catalog (+1 with hdr) */
1984 rc = verify_handle("10g", cath, 4);
1989 * sync device to commit all recent LLOG changes to disk and avoid
1990 * to consume a huge space with delayed journal commit callbacks
1991 * particularly on low memory nodes or VMs
1993 rc = dt_sync(env, dt);
1995 CERROR("10g: sync failed: %d\n", rc);
1999 /* cancel more records to free one more slot in Catalog */
2000 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
2002 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
2003 if (rc != -LLOG_EEMPTY) {
2004 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
2006 * need to indicate error if for any reason llog_test_recnum is
2014 CWARN("10g: print the catalog entries.. we expect 2\n");
2016 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
2019 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
2022 if (cat_counter != 2) {
2023 CERROR("10g: %d entries in catalog\n", cat_counter);
2024 GOTO(out, rc = -EINVAL);
2027 /* verify one down in catalog (+1 with hdr) */
2028 rc = verify_handle("10g", cath, 3);
2032 /* verify lgh_last_idx = 2 and llh_cat_idx = 0 now */
2033 if (cath->lgh_hdr->llh_cat_idx != 0 ||
2034 cath->lgh_last_idx != 2) {
2035 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 0\n",
2036 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
2037 GOTO(out, rc = -EINVAL);
2041 * sync device to commit all recent LLOG changes to disk and avoid
2042 * to consume a huge space with delayed journal commit callbacks
2043 * particularly on low memory nodes or VMs
2045 rc = dt_sync(env, dt);
2047 CERROR("10g: sync failed: %d\n", rc);
2051 /* cancel more records to free one more slot in Catalog */
2052 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
2054 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
2055 if (rc != -LLOG_EEMPTY) {
2056 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
2058 * need to indicate error if for any reason llog_test_recnum is
2066 CWARN("10g: print the catalog entries.. we expect 1\n");
2068 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
2071 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
2074 if (cat_counter != 1) {
2075 CERROR("10g: %d entries in catalog\n", cat_counter);
2076 GOTO(out, rc = -EINVAL);
2079 /* verify one down in catalog (+1 with hdr) */
2080 rc = verify_handle("10g", cath, 2);
2084 /* verify lgh_last_idx = 2 and llh_cat_idx = 1 now */
2085 if (cath->lgh_hdr->llh_cat_idx != 1 ||
2086 cath->lgh_last_idx != 2) {
2087 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 1\n",
2088 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
2089 GOTO(out, rc = -EINVAL);
2092 CWARN("10g: llh_cat_idx has also successfully wrapped!\n");
2095 * catalog has only one valid entry other slots has outdated
2096 * records. Trying to race the llog_thread_process with llog_add
2097 * llog_thread_process read buffer and loop record on it.
2098 * llog_add adds a record and mark a record in bitmap.
2099 * llog_thread_process process record with old data.
2102 struct llog_process_info lpi;
2103 struct lu_fid test_fid = {0};
2105 lpi.lpi_loghandle = cath;
2106 lpi.lpi_cb = cat_check_old_cb;
2107 lpi.lpi_catdata = NULL;
2108 lpi.lpi_cbdata = &test_fid;
2109 init_completion(&lpi.lpi_completion);
2111 kthread_run(llog_test_process_thread, &lpi, "llog_test_process_thread");
2113 msleep(1 * MSEC_PER_SEC / 2);
2116 CWARN("10h: write %d more log records\n", llog_test_recnum);
2117 for (i = 0; i < llog_test_recnum; i++) {
2118 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
2119 if (rc && rc != -ENOSPC) {
2120 CERROR("10h: write %d records failed at #%d: %d\n",
2121 llog_test_recnum, i + 1, rc);
2125 * after last added plain LLOG has filled up, all new
2126 * records add should fail with -ENOSPC
2128 if (rc == -ENOSPC) {
2136 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
2137 CERROR("10h: all last records adds should have failed with"
2139 GOTO(out, rc = -EINVAL);
2142 CWARN("10h: wrote %d records then %d failed with ENOSPC\n", eok,
2145 wait_for_completion(&lpi.lpi_completion);
2147 if (lpi.lpi_rc != 0) {
2148 CERROR("10h: race happened, old record was processed\n");
2149 GOTO(out, rc = -EINVAL);
2156 CWARN("10: put newly-created catalog\n");
2157 rc2 = llog_cat_close(env, cath);
2159 CERROR("10: close log %s failed: %d\n", name, rc2);
2164 llog_ctxt_put(ctxt);
2169 * -------------------------------------------------------------------------
2170 * Tests above, boring obd functions below
2171 * -------------------------------------------------------------------------
2173 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
2175 struct llog_handle *llh = NULL;
2176 struct llog_ctxt *ctxt;
2181 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
2184 sprintf(name, "%x", llog_test_rand);
2186 rc = llog_test_1(env, obd, name);
2188 GOTO(cleanup_ctxt, rc);
2190 rc = llog_test_2(env, obd, name, &llh);
2192 GOTO(cleanup_ctxt, rc);
2194 rc = llog_test_3(env, obd, llh);
2198 rc = llog_test_4(env, obd);
2202 rc = llog_test_5(env, obd);
2206 rc = llog_test_6(env, obd, name);
2210 rc = llog_test_7(env, obd);
2214 rc = llog_test_8(env, obd);
2218 rc = llog_test_9(env, obd);
2222 rc = llog_test_10(env, obd);
2227 err = llog_destroy(env, llh);
2229 CERROR("cleanup: llog_destroy failed: %d\n", err);
2230 llog_close(env, llh);
2234 llog_ctxt_put(ctxt);
2238 static int llog_test_cleanup(struct obd_device *obd)
2240 struct obd_device *tgt;
2246 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2250 tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
2251 rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
2253 CERROR("failed to llog_test_llog_finish: %d\n", rc);
2258 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
2260 struct obd_device *tgt;
2261 struct llog_ctxt *ctxt;
2262 struct dt_object *o;
2268 if (lcfg->lcfg_bufcount < 2) {
2269 CERROR("requires a TARGET OBD name\n");
2273 if (lcfg->lcfg_buflens[1] < 1) {
2274 CERROR("requires a TARGET OBD name\n");
2279 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2280 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2281 CERROR("target device not attached or not set up (%s)\n",
2282 lustre_cfg_string(lcfg, 1));
2286 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2290 CWARN("Setup llog-test device over %s device\n",
2291 lustre_cfg_string(lcfg, 1));
2293 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2294 obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
2296 rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
2299 GOTO(cleanup_env, rc);
2301 /* use MGS llog dir for tests */
2302 ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
2305 llog_ctxt_put(ctxt);
2307 ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
2310 llog_ctxt_put(ctxt);
2312 llog_test_rand = get_random_u32();
2314 rc = llog_run_tests(&env, tgt);
2316 llog_test_cleanup(obd);
2323 static const struct obd_ops llog_obd_ops = {
2324 .o_owner = THIS_MODULE,
2325 .o_setup = llog_test_setup,
2326 .o_cleanup = llog_test_cleanup,
2329 static int __init llog_test_init(void)
2331 return class_register_type(&llog_obd_ops, NULL, false,
2335 static void __exit llog_test_exit(void)
2337 class_unregister_type("llog_test");
2340 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2341 MODULE_DESCRIPTION("Lustre Log test module");
2342 MODULE_VERSION(LUSTRE_VERSION_STRING);
2343 MODULE_LICENSE("GPL");
2345 module_init(llog_test_init);
2346 module_exit(llog_test_exit);