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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/llog_test.c
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Mikhail Pershin <mike.pershin@intel.com>
38 #define DEBUG_SUBSYSTEM S_CLASS
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/kthread.h>
43 #include <linux/delay.h>
44 #include <linux/random.h>
46 #include <obd_class.h>
47 #include <lustre_fid.h>
48 #include <lustre_log.h>
50 /* This is slightly more than the number of records that can fit into a
51 * single llog file, because the llog_log_header takes up some of the
52 * space in the first block that cannot be used for the bitmap. */
53 static int llog_test_recnum = (LLOG_MIN_CHUNK_SIZE * 8);
54 static int llog_test_rand;
55 static struct obd_uuid uuid = { .uuid = "test_uuid" };
56 static struct llog_logid cat_logid;
58 struct llog_mini_rec {
59 struct llog_rec_hdr lmr_hdr;
60 struct llog_rec_tail lmr_tail;
61 } __attribute__((packed));
63 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
69 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
70 if (ext2_test_bit(i, LLOG_HDR_BITMAP(llh->lgh_hdr))) {
76 /* check the llog is sane at first, llh_count and lgh_last_idx*/
77 if (llh->lgh_hdr->llh_count != active_recs) {
78 CERROR("%s: handle->count is %d, but there are %d recs found\n",
79 test, llh->lgh_hdr->llh_count, active_recs);
83 if (llh->lgh_last_idx != LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index ||
84 (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_CAT) &&
85 llh->lgh_last_idx < last_idx)) {
86 CERROR("%s: lgh_last_idx is %d (%d in the header), last found %d\n",
87 test, llh->lgh_last_idx,
88 LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index, last_idx);
92 /* finally checks against expected value from the caller */
93 if (active_recs != num_recs) {
94 CERROR("%s: expected %d active recs after write, found %d\n",
95 test, num_recs, active_recs);
102 /* Test named-log create/open, close */
103 static int llog_test_1(const struct lu_env *env,
104 struct obd_device *obd, char *name)
106 struct llog_handle *llh;
107 struct llog_ctxt *ctxt;
113 CWARN("1a: create a log with name: %s\n", name);
114 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
117 rc = llog_open_create(env, ctxt, &llh, NULL, name);
119 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
122 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, &uuid);
124 CERROR("1a: can't init llog handle: %d\n", rc);
128 rc = verify_handle("1", llh, 1);
130 CWARN("1b: close newly-created log\n");
132 rc2 = llog_close(env, llh);
134 CERROR("1b: close log %s failed: %d\n", name, rc2);
143 static int test_2_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
144 struct llog_rec_hdr *rec, void *data)
146 return LLOG_DEL_RECORD;
149 /* Test named-log reopen; returns opened log on success */
150 static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
151 char *name, struct llog_handle **llh)
153 struct llog_ctxt *ctxt;
154 struct llog_handle *lgh;
155 struct llog_logid logid;
157 struct llog_mini_rec lmr;
161 CWARN("2a: re-open a log with name: %s\n", name);
162 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
165 rc = llog_open(env, ctxt, llh, NULL, name, LLOG_OPEN_EXISTS);
167 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
171 rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
173 CERROR("2a: can't init llog handle: %d\n", rc);
174 GOTO(out_close_llh, rc);
177 rc = verify_handle("2", *llh, 1);
179 GOTO(out_close_llh, rc);
181 CWARN("2b: create a log without specified NAME & LOGID\n");
182 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
184 CERROR("2b: create log failed\n");
185 GOTO(out_close_llh, rc);
187 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
189 CERROR("2b: can't init llog handle: %d\n", rc);
195 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
196 lmr.lmr_hdr.lrh_type = 0xf02f02;
198 /* Check llog header values are correct after record add/cancel */
199 CWARN("2b: write 1 llog records, check llh_count\n");
200 rc = llog_write(env, lgh, &lmr.lmr_hdr, LLOG_NEXT_IDX);
204 /* in-memory values after record addition */
205 rc = verify_handle("2b", lgh, 2);
209 /* re-open llog to read on-disk values */
210 llog_close(env, lgh);
212 CWARN("2c: re-open the log by LOGID and verify llh_count\n");
213 rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
215 CERROR("2c: re-open log by LOGID failed\n");
216 GOTO(out_close_llh, rc);
219 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
221 CERROR("2c: can't init llog handle: %d\n", rc);
225 /* check values just read from disk */
226 rc = verify_handle("2c", lgh, 2);
230 rc = llog_process(env, lgh, test_2_cancel_cb, NULL, NULL);
234 /* in-memory values */
235 rc = verify_handle("2c", lgh, 1);
239 /* re-open llog to get on-disk values */
240 llog_close(env, lgh);
242 rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
244 CERROR("2c: re-open log by LOGID failed\n");
245 GOTO(out_close_llh, rc);
248 rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
250 CERROR("2c: can't init llog handle: %d\n", rc);
254 /* on-disk values after llog re-open */
255 rc = verify_handle("2c", lgh, 1);
259 CWARN("2d: destroy this log\n");
260 rc = llog_destroy(env, lgh);
262 CERROR("2d: destroy log failed\n");
264 llog_close(env, lgh);
267 llog_close(env, *llh);
274 static int test_3_rec_num;
275 static off_t test_3_rec_off;
276 static int test_3_paddings;
277 static int test_3_start_idx;
281 * - check lgh_cur_offset correctness
282 * - check record index consistency
283 * - modify each record in-place
284 * - add new record during *last_idx processing
286 static int test3_check_n_add_cb(const struct lu_env *env,
287 struct llog_handle *lgh,
288 struct llog_rec_hdr *rec, void *data)
290 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
291 int *last_rec = data;
292 unsigned cur_idx = test_3_start_idx + test_3_rec_num;
295 if (lgh->lgh_hdr->llh_flags & LLOG_F_IS_FIXSIZE) {
296 LASSERT(lgh->lgh_hdr->llh_size > 0);
297 if (lgh->lgh_cur_offset != lgh->lgh_hdr->llh_hdr.lrh_len +
298 (cur_idx - 1) * lgh->lgh_hdr->llh_size)
299 CERROR("Wrong record offset in cur_off: %llu, should be %u\n",
301 lgh->lgh_hdr->llh_hdr.lrh_len +
302 (cur_idx - 1) * lgh->lgh_hdr->llh_size);
304 size_t chunk_size = lgh->lgh_hdr->llh_hdr.lrh_len;
307 * For variable size records the start offset is unknown, trust
308 * the first value and check others are consistent with it.
310 if (test_3_rec_off == 0)
311 test_3_rec_off = lgh->lgh_cur_offset;
313 if (lgh->lgh_cur_offset != test_3_rec_off) {
314 __u64 tmp = lgh->lgh_cur_offset;
316 /* there can be padding record */
317 if ((do_div(tmp, chunk_size) == 0) &&
318 (lgh->lgh_cur_offset - test_3_rec_off <
319 rec->lrh_len + LLOG_MIN_REC_SIZE)) {
320 test_3_rec_off = lgh->lgh_cur_offset;
323 CERROR("Wrong record offset in cur_off: %llu"
324 ", should be %lld (rec len %u)\n",
326 (long long)test_3_rec_off,
330 test_3_rec_off += rec->lrh_len;
333 cur_idx += test_3_paddings;
334 if (cur_idx != rec->lrh_index)
335 CERROR("Record with wrong index was read: %u, expected %u\n",
336 rec->lrh_index, cur_idx);
338 /* modify all records in place */
339 lgr->lgr_gen.conn_cnt = rec->lrh_index;
340 rc = llog_write(env, lgh, rec, rec->lrh_index);
342 CERROR("cb_test_3: cannot modify record while processing\n");
345 * Add new record to the llog at *last_rec position one by one to
346 * check that last block is re-read during processing
348 if (cur_idx == *last_rec || cur_idx == (*last_rec + 1)) {
349 rc = llog_write(env, lgh, rec, LLOG_NEXT_IDX);
351 CERROR("cb_test_3: cannot add new record while "
359 /* Check in-place modifications were done for all records*/
360 static int test3_check_cb(const struct lu_env *env, struct llog_handle *lgh,
361 struct llog_rec_hdr *rec, void *data)
363 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
365 if (lgr->lgr_gen.conn_cnt != rec->lrh_index) {
366 CERROR("cb_test_3: record %u is not modified\n",
374 static int llog_test3_process(const struct lu_env *env,
375 struct llog_handle *lgh,
376 llog_cb_t cb, int start)
378 struct llog_process_cat_data cd;
379 int last_idx; /* new record will be injected here */
382 CWARN("test3: processing records from index %d to the end\n",
384 cd.lpcd_first_idx = start - 1;
385 cd.lpcd_last_idx = 0;
386 test_3_rec_num = test_3_paddings = 0;
387 last_idx = lgh->lgh_last_idx;
388 rc = llog_process(env, lgh, cb, &last_idx, &cd);
391 CWARN("test3: total %u records processed with %u paddings\n",
392 test_3_rec_num, test_3_paddings);
393 return test_3_rec_num;
396 /* Test plain llog functionality */
397 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
398 struct llog_handle *llh)
401 struct llog_rec_hdr *hdr = (void *)buf;
403 int num_recs = 1; /* 1 for the header */
408 hdr->lrh_len = sizeof(struct llog_gen_rec);
409 hdr->lrh_type = LLOG_GEN_REC;
410 llh->lgh_hdr->llh_size = sizeof(struct llog_gen_rec);
411 llh->lgh_hdr->llh_flags |= LLOG_F_IS_FIXSIZE;
414 * Fill the llog with 64-bytes records, use 1023 records,
415 * so last chunk will be partially full. Don't change this
416 * value until record size is changed.
418 CWARN("3a: write 1023 fixed-size llog records\n");
419 for (i = 0; i < 1023; i++) {
420 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
422 CERROR("3a: write 1023 records failed at #%d: %d\n",
429 rc = verify_handle("3a", llh, num_recs);
434 * Test fixed-size records processing:
435 * - search the needed index
436 * - go through all records from that index
437 * - check all indices are growing monotonically and exist
438 * - modify each record
440 * NB: test3_check_n_add adds two new records while processing
441 * after last record. There were 1023 records created so the last chunk
442 * misses exactly one record. Therefore one of new records will be
443 * the last in the current chunk and second causes the new chunk to be
447 test_3_start_idx = 501;
449 rc = llog_test3_process(env, llh, test3_check_n_add_cb,
454 /* extra record is created during llog_process() */
455 if (rc != expected) {
456 CERROR("3a: process total %d records but expect %d\n",
463 /* test modification in place */
464 rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
468 if (rc != expected) {
469 CERROR("3a: process total %d records but expect %d\n",
474 CWARN("3b: write 566 variable size llog records\n");
477 * Drop llh_size to 0 to mark llog as variable-size and write
478 * header to make this change permanent.
480 llh->lgh_hdr->llh_flags &= ~LLOG_F_IS_FIXSIZE;
481 llog_write(env, llh, &llh->lgh_hdr->llh_hdr, LLOG_HEADER_IDX);
483 hdr->lrh_type = OBD_CFG_REC;
486 * there are 1025 64-bytes records in llog already,
487 * the last chunk contains single record, i.e. 64 bytes.
488 * Each pair of variable size records is 200 bytes, so
489 * we will have the following distribution per chunks:
490 * block 1: 64 + 80(80/120) + 80 + 48(pad) = 81 iterations
491 * block 2: 80(120/80) + 120 + 72(pad) = 81 itereations
492 * block 3: 80(80/120) + 80 + 112(pad) = 81 iterations
493 * -- the same as block 2 again and so on.
494 * block 7: 80(80/120) = 80 iterations and 192 bytes remain
495 * Total 6 * 81 + 80 = 566 itereations.
496 * Callback will add another 120 bytes in the end of the last chunk
497 * and another 120 bytes will cause padding (72 bytes) plus 120
498 * bytes in the new block.
500 for (i = 0; i < 566; i++) {
506 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
508 CERROR("3b: write 566 records failed at #%d: %d\n",
515 rc = verify_handle("3b", llh, num_recs);
519 test_3_start_idx = 1026;
521 rc = llog_test3_process(env, llh, test3_check_n_add_cb,
526 if (rc != expected) {
527 CERROR("3b: process total %d records but expect %d\n",
534 /* test modification in place */
535 rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
539 if (rc != expected) {
540 CERROR("3b: process total %d records but expect %d\n",
545 CWARN("3c: write records with variable size until BITMAP_SIZE, "
547 while (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
548 if ((num_recs % 2) == 0)
553 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
557 CERROR("3c: write recs failed at #%d: %d\n",
565 CWARN("3c: write record more than BITMAP size!\n");
568 CWARN("3c: wrote %d more records before end of llog is reached\n",
571 rc = verify_handle("3c", llh, num_recs);
576 /* Test catalogue additions */
577 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
579 struct llog_handle *cath, *llh;
581 int rc, rc2, i, buflen;
582 struct llog_mini_rec lmr;
583 struct llog_cookie cookie;
584 struct llog_ctxt *ctxt;
587 struct llog_rec_hdr *rec;
591 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
594 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
595 lmr.lmr_hdr.lrh_type = 0xf00f00;
597 sprintf(name, "%x", llog_test_rand + 1);
598 CWARN("4a: create a catalog log with name: %s\n", name);
599 rc = llog_open_create(env, ctxt, &cath, NULL, name);
601 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
602 GOTO(ctxt_release, rc);
604 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
606 CERROR("4a: can't init llog handle: %d\n", rc);
611 cat_logid = cath->lgh_id;
613 CWARN("4b: write 1 record into the catalog\n");
614 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
616 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
620 rc = verify_handle("4b", cath, 2);
624 rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
628 /* estimate the max number of record for the plain llog
629 * cause it depends on disk size
631 llh = cath->u.chd.chd_current_log;
632 if (llh->lgh_max_size != 0) {
633 llog_test_recnum = (llh->lgh_max_size -
634 sizeof(struct llog_log_hdr)) / LLOG_MIN_REC_SIZE;
637 if (llog_test_recnum >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr))
638 llog_test_recnum = LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1;
640 CWARN("4c: cancel 1 log record\n");
641 rc = llog_cat_cancel_records(env, cath, 1, &cookie);
643 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
648 rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
652 CWARN("4d: write %d more log records\n", llog_test_recnum);
653 for (i = 0; i < llog_test_recnum; i++) {
654 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
656 CERROR("4d: write %d records failed at #%d: %d\n",
657 llog_test_recnum, i + 1, rc);
663 /* make sure new plain llog appears */
664 rc = verify_handle("4d", cath, 3);
668 CWARN("4e: add 5 large records, one record per block\n");
669 buflen = LLOG_MIN_CHUNK_SIZE;
670 OBD_ALLOC(buf, buflen);
672 GOTO(out, rc = -ENOMEM);
673 for (i = 0; i < 5; i++) {
675 rec->lrh_len = buflen;
676 rec->lrh_type = OBD_CFG_REC;
677 rc = llog_cat_add(env, cath, rec, NULL);
679 CERROR("4e: write 5 records failed at #%d: %d\n",
686 OBD_FREE(buf, buflen);
688 CWARN("4f: put newly-created catalog\n");
689 rc2 = llog_cat_close(env, cath);
691 CERROR("4: close log %s failed: %d\n", name, rc2);
700 static int cat_counter;
702 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
703 struct llog_rec_hdr *rec, void *data)
705 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
706 struct lu_fid fid = {0};
708 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
709 CERROR("invalid record in catalog\n");
713 logid_to_fid(&lir->lid_id, &fid);
715 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
716 rec->lrh_index, PFID(&fid),
717 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
724 static int plain_counter;
726 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
727 struct llog_rec_hdr *rec, void *data)
729 struct lu_fid fid = {0};
731 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
732 CERROR("log is not plain\n");
736 logid_to_fid(&llh->lgh_id, &fid);
738 CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
739 rec->lrh_index, PFID(&fid));
746 static int cancel_count;
748 static int llog_cancel_rec_cb(const struct lu_env *env,
749 struct llog_handle *llh,
750 struct llog_rec_hdr *rec, void *data)
752 struct llog_cookie cookie;
754 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
755 CERROR("log is not plain\n");
759 cookie.lgc_lgl = llh->lgh_id;
760 cookie.lgc_index = rec->lrh_index;
762 llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
764 if (cancel_count == llog_test_recnum)
765 RETURN(-LLOG_EEMPTY);
769 /* Test log and catalogue processing */
770 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
772 struct llog_handle *llh = NULL;
775 struct llog_mini_rec lmr;
776 struct llog_ctxt *ctxt;
780 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
783 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
784 lmr.lmr_hdr.lrh_type = 0xf00f00;
786 CWARN("5a: re-open catalog by id\n");
787 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
789 CERROR("5a: llog_create with logid failed: %d\n", rc);
793 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
795 CERROR("5a: can't init llog handle: %d\n", rc);
799 CWARN("5b: print the catalog entries.. we expect 2\n");
801 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
803 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
806 if (cat_counter != 2) {
807 CERROR("5b: %d entries in catalog\n", cat_counter);
808 GOTO(out, rc = -EINVAL);
811 CWARN("5c: Cancel %d records, see one log zapped\n", llog_test_recnum);
813 rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
814 if (rc != -LLOG_EEMPTY) {
815 CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
819 CWARN("5c: print the catalog entries.. we expect 1\n");
821 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
823 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
826 if (cat_counter != 1) {
827 CERROR("5c: %d entries in catalog\n", cat_counter);
828 GOTO(out, rc = -EINVAL);
831 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
832 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
834 CERROR("5d: add record to the log with many canceled empty "
839 CWARN("5e: print plain log entries.. expect 6\n");
841 rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
843 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
846 if (plain_counter != 6) {
847 CERROR("5e: found %d records\n", plain_counter);
848 GOTO(out, rc = -EINVAL);
851 CWARN("5f: print plain log entries reversely.. expect 6\n");
853 rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
855 CERROR("5f: reversely process with plain_print_cb failed: "
859 if (plain_counter != 6) {
860 CERROR("5f: found %d records\n", plain_counter);
861 GOTO(out, rc = -EINVAL);
865 CWARN("5g: close re-opened catalog\n");
866 rc2 = llog_cat_close(env, llh);
868 CERROR("5g: close log %s failed: %d\n", name, rc2);
878 /* Test client api; open log by name and process */
879 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
882 struct obd_device *mgc_obd;
883 struct llog_ctxt *ctxt;
884 struct obd_uuid *mgs_uuid;
885 struct obd_export *exp;
886 struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
887 struct llog_handle *llh = NULL;
888 struct llog_ctxt *nctxt;
891 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
893 mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
895 CWARN("6a: re-open log %s using client API\n", name);
896 mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
897 if (mgc_obd == NULL) {
898 CERROR("6a: no MGC devices connected to %s found.\n",
900 GOTO(ctxt_release, rc = -ENOENT);
903 rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
904 NULL /* obd_connect_data */, NULL);
905 if (rc != -EALREADY) {
906 CERROR("6a: connect on connected MGC (%s) failed to return"
907 " -EALREADY\n", mgc_obd->obd_name);
910 GOTO(ctxt_release, rc = -EINVAL);
913 nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
914 rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
916 CERROR("6a: llog_open failed %d\n", rc);
920 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
922 CERROR("6a: llog_init_handle failed %d\n", rc);
926 plain_counter = 1; /* llog header is first record */
927 CWARN("6b: process log %s using client API\n", name);
928 rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
930 CERROR("6b: llog_process failed %d\n", rc);
931 CWARN("6b: processed %d records\n", plain_counter);
933 rc = verify_handle("6b", llh, plain_counter);
937 plain_counter = 1; /* llog header is first record */
938 CWARN("6c: process log %s reversely using client API\n", name);
939 rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
941 CERROR("6c: llog_reverse_process failed %d\n", rc);
942 CWARN("6c: processed %d records\n", plain_counter);
944 rc = verify_handle("6c", llh, plain_counter);
949 rc2 = llog_close(env, llh);
951 CERROR("6: llog_close failed: rc = %d\n", rc2);
956 llog_ctxt_put(nctxt);
963 struct llog_rec_hdr lrh; /* common header */
964 struct llog_logid_rec llr; /* LLOG_LOGID_MAGIC */
965 struct llog_unlink64_rec lur; /* MDS_UNLINK64_REC */
966 struct llog_setattr64_rec lsr64; /* MDS_SETATTR64_REC */
967 struct llog_setattr64_rec_v2 lsr64_v2; /* MDS_SETATTR64_REC */
968 struct llog_size_change_rec lscr; /* OST_SZ_REC */
969 struct llog_changelog_rec lcr; /* CHANGELOG_REC */
970 struct llog_changelog_user_rec lcur; /* CHANGELOG_USER_REC */
971 struct llog_gen_rec lgr; /* LLOG_GEN_REC */
974 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
975 struct llog_rec_hdr *rec, void *data)
977 struct lu_fid fid = {0};
979 logid_to_fid(&llh->lgh_id, &fid);
981 CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
982 rec->lrh_type, rec->lrh_index, PFID(&fid));
988 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
989 struct llog_rec_hdr *rec, void *data)
992 /* test LLOG_DEL_RECORD is working */
993 return LLOG_DEL_RECORD;
996 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
998 struct llog_handle *llh;
999 int rc = 0, i, process_count;
1004 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1006 CERROR("7_sub: create log failed\n");
1010 rc = llog_init_handle(env, llh,
1011 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1014 CERROR("7_sub: can't init llog handle: %d\n", rc);
1015 GOTO(out_close, rc);
1017 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
1018 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1019 if (rc == -ENOSPC) {
1021 } else if (rc < 0) {
1022 CERROR("7_sub: write recs failed at #%d: %d\n",
1024 GOTO(out_close, rc);
1028 if (rc != -ENOSPC) {
1029 CWARN("7_sub: write record more than BITMAP size!\n");
1030 GOTO(out_close, rc = -EINVAL);
1033 rc = verify_handle("7_sub", llh, num_recs + 1);
1035 CERROR("7_sub: verify handle failed: %d\n", rc);
1036 GOTO(out_close, rc);
1038 if (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1)
1039 CWARN("7_sub: records are not aligned, written %d from %u\n",
1040 num_recs, LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
1043 rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
1045 CERROR("7_sub: llog process failed: %d\n", rc);
1046 GOTO(out_close, rc);
1048 process_count = plain_counter;
1049 if (process_count != num_recs) {
1050 CERROR("7_sub: processed %d records from %d total\n",
1051 process_count, num_recs);
1052 GOTO(out_close, rc = -EINVAL);
1056 rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
1057 if (rc && rc != LLOG_DEL_PLAIN) {
1058 CERROR("7_sub: reverse llog process failed: %d\n", rc);
1059 GOTO(out_close, rc);
1061 if (process_count != plain_counter) {
1062 CERROR("7_sub: Reverse/direct processing found different"
1063 "number of records: %d/%d\n",
1064 plain_counter, process_count);
1065 GOTO(out_close, rc = -EINVAL);
1067 if (llog_exist(llh)) {
1068 CERROR("7_sub: llog exists but should be zapped\n");
1069 GOTO(out_close, rc = -EEXIST);
1072 rc = verify_handle("7_sub", llh, 1);
1075 llog_destroy(env, llh);
1076 llog_close(env, llh);
1080 /* Test all llog records writing and processing */
1081 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
1083 struct llog_ctxt *ctxt;
1088 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1090 CWARN("7a: test llog_logid_rec\n");
1091 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1092 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1093 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1095 rc = llog_test_7_sub(env, ctxt);
1097 CERROR("7a: llog_logid_rec test failed\n");
1101 CWARN("7b: test llog_unlink64_rec\n");
1102 llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
1103 llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
1104 llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
1106 rc = llog_test_7_sub(env, ctxt);
1108 CERROR("7b: llog_unlink_rec test failed\n");
1112 CWARN("7c: test llog_setattr64_rec\n");
1113 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
1114 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
1115 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1117 rc = llog_test_7_sub(env, ctxt);
1119 CERROR("7c: llog_setattr64_rec test failed\n");
1123 CWARN("7d: test llog_size_change_rec\n");
1124 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1125 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1126 llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
1128 rc = llog_test_7_sub(env, ctxt);
1130 CERROR("7d: llog_size_change_rec test failed\n");
1134 CWARN("7e: test llog_changelog_rec\n");
1135 /* Direct access to cr_do_not_use: peculiar case for this test */
1136 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1137 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1138 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1140 rc = llog_test_7_sub(env, ctxt);
1142 CERROR("7e: llog_changelog_rec test failed\n");
1146 CWARN("7f: test llog_changelog_user_rec\n");
1147 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1148 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1149 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1151 rc = llog_test_7_sub(env, ctxt);
1153 CERROR("7f: llog_changelog_user_rec test failed\n");
1157 CWARN("7g: test llog_gen_rec\n");
1158 llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
1159 llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
1160 llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
1162 rc = llog_test_7_sub(env, ctxt);
1164 CERROR("7g: llog_size_change_rec test failed\n");
1168 CWARN("7h: test llog_setattr64_rec_v2\n");
1169 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64_v2);
1170 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64_v2);
1171 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1173 rc = llog_test_7_sub(env, ctxt);
1175 CERROR("7h: llog_setattr64_rec_v2 test failed\n");
1179 llog_ctxt_put(ctxt);
1183 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
1184 struct llog_rec_hdr *rec, void *data)
1190 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
1192 struct llog_handle *llh = NULL;
1196 struct llog_mini_rec lmr;
1197 struct llog_ctxt *ctxt;
1198 struct dt_object *obj = NULL;
1202 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1205 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1206 lmr.lmr_hdr.lrh_type = 0xf00f00;
1208 CWARN("8a: fill the first plain llog\n");
1209 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1211 CERROR("8a: llog_create with logid failed: %d\n", rc);
1215 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1217 CERROR("8a: can't init llog handle: %d\n", rc);
1222 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1224 CERROR("5a: process with test_8_cb failed: %d\n", rc);
1227 orig_counter = plain_counter;
1229 for (i = 0; i < 100; i++) {
1230 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1232 CERROR("5a: add record failed\n");
1237 /* grab the current plain llog, we'll corrupt it later */
1238 obj = llh->u.chd.chd_current_log->lgh_obj;
1240 lu_object_get(&obj->do_lu);
1241 CWARN("8a: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1243 rc2 = llog_cat_close(env, llh);
1245 CERROR("8a: close log %s failed: %d\n", name, rc2);
1251 CWARN("8b: fill the second plain llog\n");
1252 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1254 CERROR("8b: llog_create with logid failed: %d\n", rc);
1258 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1260 CERROR("8b: can't init llog handle: %d\n", rc);
1264 for (i = 0; i < 100; i++) {
1265 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1267 CERROR("8b: add record failed\n");
1271 CWARN("8b: second llog "DFID"\n",
1272 PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1274 rc2 = llog_cat_close(env, llh);
1276 CERROR("8b: close log %s failed: %d\n", name, rc2);
1282 /* Here was 8c: drop two records from the first plain llog
1283 * llog_truncate was bad idea cause it creates a wrong state,
1284 * lgh_last_idx is wrong and two records belongs to zeroed buffer
1287 CWARN("8d: count survived records\n");
1288 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1290 CERROR("8d: llog_create with logid failed: %d\n", rc);
1294 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1296 CERROR("8d: can't init llog handle: %d\n", rc);
1301 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1303 CERROR("8d: process with test_8_cb failed: %d\n", rc);
1307 if (orig_counter + 200 != plain_counter) {
1308 CERROR("found %d records (expected %d)\n", plain_counter,
1309 orig_counter + 200);
1314 CWARN("8d: close re-opened catalog\n");
1315 rc2 = llog_cat_close(env, llh);
1317 CERROR("8d: close log %s failed: %d\n", name, rc2);
1322 llog_ctxt_put(ctxt);
1325 dt_object_put(env, obj);
1330 static int llog_test_9_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
1332 struct llog_handle *llh;
1338 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1340 CERROR("9_sub: create log failed\n");
1344 rc = llog_init_handle(env, llh,
1345 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1348 CERROR("9_sub: can't init llog handle: %d\n", rc);
1349 GOTO(out_close, rc);
1352 logid_to_fid(&llh->lgh_id, &fid);
1353 fid_to_logid(&fid, &llog_records.llr.lid_id);
1354 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1356 CERROR("9_sub: write recs failed at #1: %d\n", rc);
1357 GOTO(out_close, rc);
1359 /* The below message is checked in sanity.sh test_60a (run-llog.sh) */
1360 CWARN("9_sub: record type %x in log "DFID_NOBRACE"\n",
1361 llog_records.lrh.lrh_type, PFID(&fid));
1363 llog_close(env, llh);
1367 /* Prepare different types of llog records for llog_reader test*/
1368 static int llog_test_9(const struct lu_env *env, struct obd_device *obd)
1370 struct llog_ctxt *ctxt;
1375 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1377 CWARN("9a: test llog_logid_rec\n");
1378 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1379 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1380 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1382 rc = llog_test_9_sub(env, ctxt);
1384 CERROR("9a: llog_logid_rec test failed\n");
1388 CWARN("9b: test llog_obd_cfg_rec\n");
1389 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1390 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1391 llog_records.lscr.lsc_hdr.lrh_type = OBD_CFG_REC;
1393 rc = llog_test_9_sub(env, ctxt);
1395 CERROR("9b: llog_obd_cfg_rec test failed\n");
1399 CWARN("9c: test llog_changelog_rec\n");
1400 /* Direct access to cr_do_not_use: peculiar case for this test */
1401 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1402 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1403 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1405 rc = llog_test_9_sub(env, ctxt);
1407 CERROR("9c: llog_changelog_rec test failed\n");
1411 CWARN("9d: test llog_changelog_user_rec\n");
1412 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1413 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1414 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1416 rc = llog_test_9_sub(env, ctxt);
1418 CERROR("9d: llog_changelog_user_rec test failed\n");
1423 llog_ctxt_put(ctxt);
1427 struct llog_process_info {
1428 struct llog_handle *lpi_loghandle;
1433 struct completion lpi_completion;
1434 const struct lu_env *lpi_env;
1435 struct task_struct *lpi_reftask;
1439 static int llog_test_process_thread(void *arg)
1441 struct llog_process_info *lpi = arg;
1444 rc = llog_cat_process_or_fork(NULL, lpi->lpi_loghandle, lpi->lpi_cb,
1445 NULL, lpi->lpi_cbdata, 1, 0, true);
1447 complete(&lpi->lpi_completion);
1451 CWARN("10h: Error during catalog processing %d\n", rc);
1455 static int cat_check_old_cb(const struct lu_env *env, struct llog_handle *llh,
1456 struct llog_rec_hdr *rec, void *data)
1458 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
1459 struct lu_fid fid = {0};
1460 struct lu_fid *prev_fid = data;
1462 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
1463 CERROR("invalid record in catalog\n");
1467 logid_to_fid(&lir->lid_id, &fid);
1469 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
1470 rec->lrh_index, PFID(&fid),
1471 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
1473 if (prev_fid->f_oid > fid.f_oid) {
1474 CWARN("processing old record, fail\n");
1475 prev_fid->f_oid = 0xbad;
1476 RETURN(-LLOG_EEMPTY);
1479 if (prev_fid->f_oid == 0) {
1480 cfs_fail_loc = OBD_FAIL_ONCE | OBD_FAIL_LLOG_PROCESS_TIMEOUT;
1481 cfs_fail_val = (unsigned int) (llh->lgh_id.lgl_oi.oi.oi_id &
1483 msleep(1 * MSEC_PER_SEC);
1490 /* test catalog wrap around */
1491 static int llog_test_10(const struct lu_env *env, struct obd_device *obd)
1493 struct llog_handle *cath;
1495 int rc, rc2, i, enospc, eok;
1496 struct llog_mini_rec lmr;
1497 struct llog_ctxt *ctxt;
1500 struct dt_device *dt;
1504 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1507 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1508 lmr.lmr_hdr.lrh_type = 0xf00f00;
1510 snprintf(name, sizeof(name), "%x", llog_test_rand + 2);
1511 CWARN("10a: create a catalog log with name: %s\n", name);
1512 rc = llog_open_create(env, ctxt, &cath, NULL, name);
1514 CERROR("10a: llog_create with name %s failed: %d\n", name, rc);
1515 GOTO(ctxt_release, rc);
1517 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
1519 CERROR("10a: can't init llog handle: %d\n", rc);
1523 cat_logid = cath->lgh_id;
1524 dt = lu2dt_dev(cath->lgh_obj->do_lu.lo_dev);
1527 * sync device to commit all recent LLOG changes to disk and avoid
1528 * to consume a huge space with delayed journal commit callbacks
1529 * particularly on low memory nodes or VMs
1531 rc = dt_sync(env, dt);
1533 CERROR("10c: sync failed: %d\n", rc);
1537 /* force catalog wrap for 5th plain LLOG */
1538 cfs_fail_loc = CFS_FAIL_SKIP|OBD_FAIL_CAT_RECORDS;
1541 CWARN("10b: write %d log records\n", llog_test_recnum);
1542 for (i = 0; i < llog_test_recnum; i++) {
1543 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1545 CERROR("10b: write %d records failed at #%d: %d\n",
1546 llog_test_recnum, i + 1, rc);
1551 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1552 rc = verify_handle("10b", cath, 3);
1557 * sync device to commit all recent LLOG changes to disk and avoid
1558 * to consume a huge space with delayed journal commit callbacks
1559 * particularly on low memory nodes or VMs
1561 rc = dt_sync(env, dt);
1563 CERROR("10b: sync failed: %d\n", rc);
1567 CWARN("10c: write %d more log records\n", 2 * llog_test_recnum);
1568 for (i = 0; i < 2 * llog_test_recnum; i++) {
1569 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1571 CERROR("10c: write %d records failed at #%d: %d\n",
1572 2*llog_test_recnum, i + 1, rc);
1577 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1578 rc = verify_handle("10c", cath, 5);
1583 * sync device to commit all recent LLOG changes to disk and avoid
1584 * to consume a huge space with delayed journal commit callbacks
1585 * particularly on low memory nodes or VMs
1587 rc = dt_sync(env, dt);
1589 CERROR("10c: sync failed: %d\n", rc);
1594 * fill last allocated plain LLOG and reach -ENOSPC condition
1595 * because no slot available in Catalog
1599 CWARN("10c: write %d more log records\n", llog_test_recnum);
1600 for (i = 0; i < llog_test_recnum; i++) {
1601 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1602 if (rc && rc != -ENOSPC) {
1603 CERROR("10c: write %d records failed at #%d: %d\n",
1604 llog_test_recnum, i + 1, rc);
1608 * after last added plain LLOG has filled up, all new
1609 * records add should fail with -ENOSPC
1611 if (rc == -ENOSPC) {
1619 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1620 CERROR("10c: all last records adds should have failed with"
1622 GOTO(out, rc = -EINVAL);
1625 CWARN("10c: wrote %d records then %d failed with ENOSPC\n", eok,
1628 /* make sure no new record in Catalog */
1629 rc = verify_handle("10c", cath, 5);
1633 /* Catalog should have reached its max size for test */
1634 rc = dt_attr_get(env, cath->lgh_obj, &la);
1636 CERROR("10c: failed to get catalog attrs: %d\n", rc);
1639 cat_max_size = la.la_size;
1642 * cancel all 1st plain llog records to empty it, this will also cause
1643 * its catalog entry to be freed for next forced wrap in 10e
1645 CWARN("10d: Cancel %d records, see one log zapped\n", llog_test_recnum);
1647 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1648 if (rc != -LLOG_EEMPTY) {
1649 CERROR("10d: process with llog_cancel_rec_cb failed: %d\n", rc);
1651 * need to indicate error if for any reason llog_test_recnum is
1659 CWARN("10d: print the catalog entries.. we expect 3\n");
1661 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1663 CERROR("10d: process with cat_print_cb failed: %d\n", rc);
1666 if (cat_counter != 3) {
1667 CERROR("10d: %d entries in catalog\n", cat_counter);
1668 GOTO(out, rc = -EINVAL);
1671 /* verify one down in catalog (+1 with hdr) */
1672 rc = verify_handle("10d", cath, 4);
1677 * sync device to commit all recent LLOG changes to disk and avoid
1678 * to consume a huge space with delayed journal commit callbacks
1679 * particularly on low memory nodes or VMs
1681 rc = dt_sync(env, dt);
1683 CERROR("10d: sync failed: %d\n", rc);
1689 CWARN("10e: write %d more log records\n", llog_test_recnum);
1690 for (i = 0; i < llog_test_recnum; i++) {
1691 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1692 if (rc && rc != -ENOSPC) {
1693 CERROR("10e: write %d records failed at #%d: %d\n",
1694 llog_test_recnum, i + 1, rc);
1698 * after last added plain LLOG has filled up, all new
1699 * records add should fail with -ENOSPC
1701 if (rc == -ENOSPC) {
1709 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1710 CERROR("10e: all last records adds should have failed with"
1712 GOTO(out, rc = -EINVAL);
1715 CWARN("10e: wrote %d records then %d failed with ENOSPC\n", eok,
1718 CWARN("10e: print the catalog entries.. we expect 4\n");
1720 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1723 CERROR("10e: process with cat_print_cb failed: %d\n", rc);
1726 if (cat_counter != 4) {
1727 CERROR("10e: %d entries in catalog\n", cat_counter);
1728 GOTO(out, rc = -EINVAL);
1731 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1732 rc = verify_handle("10e", cath, 5);
1736 /* verify catalog has wrap around */
1737 if (cath->lgh_last_idx > cath->lgh_hdr->llh_cat_idx) {
1738 CERROR("10e: catalog failed to wrap around\n");
1739 GOTO(out, rc = -EINVAL);
1742 rc = dt_attr_get(env, cath->lgh_obj, &la);
1744 CERROR("10e: failed to get catalog attrs: %d\n", rc);
1748 if (la.la_size != cat_max_size) {
1749 CERROR("10e: catalog size has changed after it has wrap around,"
1750 " current size = %llu, expected size = %llu\n",
1751 la.la_size, cat_max_size);
1752 GOTO(out, rc = -EINVAL);
1754 CWARN("10e: catalog successfully wrap around, last_idx %d, first %d\n",
1755 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1758 * sync device to commit all recent LLOG changes to disk and avoid
1759 * to consume a huge space with delayed journal commit callbacks
1760 * particularly on low memory nodes or VMs
1762 rc = dt_sync(env, dt);
1764 CERROR("10e: sync failed: %d\n", rc);
1769 * cancel more records to free one more slot in Catalog
1770 * see if it is re-allocated when adding more records
1772 CWARN("10f: Cancel %d records, see one log zapped\n", llog_test_recnum);
1774 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1775 if (rc != -LLOG_EEMPTY) {
1776 CERROR("10f: process with llog_cancel_rec_cb failed: %d\n", rc);
1778 * need to indicate error if for any reason llog_test_recnum is
1786 CWARN("10f: print the catalog entries.. we expect 3\n");
1788 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1791 CERROR("10f: process with cat_print_cb failed: %d\n", rc);
1794 if (cat_counter != 3) {
1795 CERROR("10f: %d entries in catalog\n", cat_counter);
1796 GOTO(out, rc = -EINVAL);
1799 /* verify one down in catalog (+1 with hdr) */
1800 rc = verify_handle("10f", cath, 4);
1805 * sync device to commit all recent LLOG changes to disk and avoid
1806 * to consume a huge space with delayed journal commit callbacks
1807 * particularly on low memory nodes or VMs
1809 rc = dt_sync(env, dt);
1811 CERROR("10f: sync failed: %d\n", rc);
1817 CWARN("10f: write %d more log records\n", llog_test_recnum);
1818 for (i = 0; i < llog_test_recnum; i++) {
1819 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1820 if (rc && rc != -ENOSPC) {
1821 CERROR("10f: write %d records failed at #%d: %d\n",
1822 llog_test_recnum, i + 1, rc);
1826 * after last added plain LLOG has filled up, all new
1827 * records add should fail with -ENOSPC
1829 if (rc == -ENOSPC) {
1837 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1838 CERROR("10f: all last records adds should have failed with"
1840 GOTO(out, rc = -EINVAL);
1843 CWARN("10f: wrote %d records then %d failed with ENOSPC\n", eok,
1846 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1847 rc = verify_handle("10f", cath, 5);
1851 /* verify lgh_last_idx = llh_cat_idx = 2 now */
1852 if (cath->lgh_last_idx != cath->lgh_hdr->llh_cat_idx ||
1853 cath->lgh_last_idx != 2) {
1854 CERROR("10f: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 2\n",
1855 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1856 GOTO(out, rc = -EINVAL);
1859 rc = dt_attr_get(env, cath->lgh_obj, &la);
1861 CERROR("10f: failed to get catalog attrs: %d\n", rc);
1865 if (la.la_size != cat_max_size) {
1866 CERROR("10f: catalog size has changed after it has wrap around,"
1867 " current size = %llu, expected size = %llu\n",
1868 la.la_size, cat_max_size);
1869 GOTO(out, rc = -EINVAL);
1873 * sync device to commit all recent LLOG changes to disk and avoid
1874 * to consume a huge space with delayed journal commit callbacks
1875 * particularly on low memory nodes or VMs
1877 rc = dt_sync(env, dt);
1879 CERROR("10f: sync failed: %d\n", rc);
1883 /* will llh_cat_idx also successfully wrap ? */
1886 * cancel all records in the plain LLOGs referenced by 2 last indexes in
1890 /* cancel more records to free one more slot in Catalog */
1891 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1893 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1894 if (rc != -LLOG_EEMPTY) {
1895 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1896 /* need to indicate error if for any reason llog_test_recnum is
1903 CWARN("10g: print the catalog entries.. we expect 3\n");
1905 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1908 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1911 if (cat_counter != 3) {
1912 CERROR("10g: %d entries in catalog\n", cat_counter);
1913 GOTO(out, rc = -EINVAL);
1916 /* verify one down in catalog (+1 with hdr) */
1917 rc = verify_handle("10g", cath, 4);
1922 * sync device to commit all recent LLOG changes to disk and avoid
1923 * to consume a huge space with delayed journal commit callbacks
1924 * particularly on low memory nodes or VMs
1926 rc = dt_sync(env, dt);
1928 CERROR("10g: sync failed: %d\n", rc);
1932 /* cancel more records to free one more slot in Catalog */
1933 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1935 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1936 if (rc != -LLOG_EEMPTY) {
1937 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1939 * need to indicate error if for any reason llog_test_recnum is
1947 CWARN("10g: print the catalog entries.. we expect 2\n");
1949 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1952 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1955 if (cat_counter != 2) {
1956 CERROR("10g: %d entries in catalog\n", cat_counter);
1957 GOTO(out, rc = -EINVAL);
1960 /* verify one down in catalog (+1 with hdr) */
1961 rc = verify_handle("10g", cath, 3);
1965 /* verify lgh_last_idx = 2 and llh_cat_idx = 0 now */
1966 if (cath->lgh_hdr->llh_cat_idx != 0 ||
1967 cath->lgh_last_idx != 2) {
1968 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 0\n",
1969 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1970 GOTO(out, rc = -EINVAL);
1974 * sync device to commit all recent LLOG changes to disk and avoid
1975 * to consume a huge space with delayed journal commit callbacks
1976 * particularly on low memory nodes or VMs
1978 rc = dt_sync(env, dt);
1980 CERROR("10g: sync failed: %d\n", rc);
1984 /* cancel more records to free one more slot in Catalog */
1985 CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1987 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1988 if (rc != -LLOG_EEMPTY) {
1989 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1991 * need to indicate error if for any reason llog_test_recnum is
1999 CWARN("10g: print the catalog entries.. we expect 1\n");
2001 rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
2004 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
2007 if (cat_counter != 1) {
2008 CERROR("10g: %d entries in catalog\n", cat_counter);
2009 GOTO(out, rc = -EINVAL);
2012 /* verify one down in catalog (+1 with hdr) */
2013 rc = verify_handle("10g", cath, 2);
2017 /* verify lgh_last_idx = 2 and llh_cat_idx = 1 now */
2018 if (cath->lgh_hdr->llh_cat_idx != 1 ||
2019 cath->lgh_last_idx != 2) {
2020 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 1\n",
2021 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
2022 GOTO(out, rc = -EINVAL);
2025 CWARN("10g: llh_cat_idx has also successfully wrapped!\n");
2028 * catalog has only one valid entry other slots has outdated
2029 * records. Trying to race the llog_thread_process with llog_add
2030 * llog_thread_process read buffer and loop record on it.
2031 * llog_add adds a record and mark a record in bitmap.
2032 * llog_thread_process process record with old data.
2035 struct llog_process_info lpi;
2036 struct lu_fid test_fid = {0};
2038 lpi.lpi_loghandle = cath;
2039 lpi.lpi_cb = cat_check_old_cb;
2040 lpi.lpi_catdata = NULL;
2041 lpi.lpi_cbdata = &test_fid;
2042 init_completion(&lpi.lpi_completion);
2044 kthread_run(llog_test_process_thread, &lpi, "llog_test_process_thread");
2046 msleep(1 * MSEC_PER_SEC / 2);
2049 CWARN("10h: write %d more log records\n", llog_test_recnum);
2050 for (i = 0; i < llog_test_recnum; i++) {
2051 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
2052 if (rc && rc != -ENOSPC) {
2053 CERROR("10h: write %d records failed at #%d: %d\n",
2054 llog_test_recnum, i + 1, rc);
2058 * after last added plain LLOG has filled up, all new
2059 * records add should fail with -ENOSPC
2061 if (rc == -ENOSPC) {
2069 if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
2070 CERROR("10h: all last records adds should have failed with"
2072 GOTO(out, rc = -EINVAL);
2075 CWARN("10h: wrote %d records then %d failed with ENOSPC\n", eok,
2078 wait_for_completion(&lpi.lpi_completion);
2080 if (lpi.lpi_rc != 0) {
2081 CERROR("10h: race happened, old record was processed\n");
2082 GOTO(out, rc = -EINVAL);
2089 CWARN("10: put newly-created catalog\n");
2090 rc2 = llog_cat_close(env, cath);
2092 CERROR("10: close log %s failed: %d\n", name, rc2);
2097 llog_ctxt_put(ctxt);
2102 * -------------------------------------------------------------------------
2103 * Tests above, boring obd functions below
2104 * -------------------------------------------------------------------------
2106 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
2108 struct llog_handle *llh = NULL;
2109 struct llog_ctxt *ctxt;
2114 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
2117 sprintf(name, "%x", llog_test_rand);
2119 rc = llog_test_1(env, obd, name);
2121 GOTO(cleanup_ctxt, rc);
2123 rc = llog_test_2(env, obd, name, &llh);
2125 GOTO(cleanup_ctxt, rc);
2127 rc = llog_test_3(env, obd, llh);
2131 rc = llog_test_4(env, obd);
2135 rc = llog_test_5(env, obd);
2139 rc = llog_test_6(env, obd, name);
2143 rc = llog_test_7(env, obd);
2147 rc = llog_test_8(env, obd);
2151 rc = llog_test_9(env, obd);
2155 rc = llog_test_10(env, obd);
2160 err = llog_destroy(env, llh);
2162 CERROR("cleanup: llog_destroy failed: %d\n", err);
2163 llog_close(env, llh);
2167 llog_ctxt_put(ctxt);
2171 static int llog_test_cleanup(struct obd_device *obd)
2173 struct obd_device *tgt;
2179 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2183 tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
2184 rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
2186 CERROR("failed to llog_test_llog_finish: %d\n", rc);
2191 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
2193 struct obd_device *tgt;
2194 struct llog_ctxt *ctxt;
2195 struct dt_object *o;
2197 struct lu_context test_session;
2202 if (lcfg->lcfg_bufcount < 2) {
2203 CERROR("requires a TARGET OBD name\n");
2207 if (lcfg->lcfg_buflens[1] < 1) {
2208 CERROR("requires a TARGET OBD name\n");
2213 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2214 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2215 CERROR("target device not attached or not set up (%s)\n",
2216 lustre_cfg_string(lcfg, 1));
2220 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2224 rc = lu_context_init(&test_session, LCT_SERVER_SESSION);
2226 GOTO(cleanup_env, rc);
2227 test_session.lc_thread = (struct ptlrpc_thread *)current;
2228 lu_context_enter(&test_session);
2229 env.le_ses = &test_session;
2231 CWARN("Setup llog-test device over %s device\n",
2232 lustre_cfg_string(lcfg, 1));
2234 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2235 obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
2237 rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
2240 GOTO(cleanup_session, rc);
2242 /* use MGS llog dir for tests */
2243 ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
2246 llog_ctxt_put(ctxt);
2248 ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
2251 llog_ctxt_put(ctxt);
2253 llog_test_rand = prandom_u32();
2255 rc = llog_run_tests(&env, tgt);
2257 llog_test_cleanup(obd);
2259 lu_context_exit(&test_session);
2260 lu_context_fini(&test_session);
2266 static const struct obd_ops llog_obd_ops = {
2267 .o_owner = THIS_MODULE,
2268 .o_setup = llog_test_setup,
2269 .o_cleanup = llog_test_cleanup,
2272 static int __init llog_test_init(void)
2274 return class_register_type(&llog_obd_ops, NULL, false, NULL,
2278 static void __exit llog_test_exit(void)
2280 class_unregister_type("llog_test");
2283 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2284 MODULE_DESCRIPTION("Lustre Log test module");
2285 MODULE_VERSION(LUSTRE_VERSION_STRING);
2286 MODULE_LICENSE("GPL");
2288 module_init(llog_test_init);
2289 module_exit(llog_test_exit);