4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog_test.c
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mikhail Pershin <mike.pershin@intel.com>
42 #define DEBUG_SUBSYSTEM S_CLASS
44 #include <linux/module.h>
45 #include <linux/init.h>
47 #include <obd_class.h>
48 #include <lustre_fid.h>
49 #include <lustre_log.h>
51 /* This is slightly more than the number of records that can fit into a
52 * single llog file, because the llog_log_header takes up some of the
53 * space in the first block that cannot be used for the bitmap. */
54 #define LLOG_TEST_RECNUM (LLOG_MIN_CHUNK_SIZE * 8)
56 static int llog_test_rand;
57 static struct obd_uuid uuid = { .uuid = "test_uuid" };
58 static struct llog_logid cat_logid;
60 struct llog_mini_rec {
61 struct llog_rec_hdr lmr_hdr;
62 struct llog_rec_tail lmr_tail;
63 } __attribute__((packed));
65 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
71 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
72 if (ext2_test_bit(i, LLOG_HDR_BITMAP(llh->lgh_hdr))) {
78 if (active_recs != num_recs) {
79 CERROR("%s: expected %d active recs after write, found %d\n",
80 test, num_recs, active_recs);
84 if (llh->lgh_hdr->llh_count != num_recs) {
85 CERROR("%s: handle->count is %d, expected %d after write\n",
86 test, llh->lgh_hdr->llh_count, num_recs);
90 /* a catalog may wrap */
91 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_CAT) &&
92 (llh->lgh_last_idx < last_idx)) {
93 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
94 test, llh->lgh_last_idx, last_idx);
101 /* Test named-log create/open, close */
102 static int llog_test_1(const struct lu_env *env,
103 struct obd_device *obd, char *name)
105 struct llog_handle *llh;
106 struct llog_ctxt *ctxt;
112 CWARN("1a: create a log with name: %s\n", name);
113 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
116 rc = llog_open_create(env, ctxt, &llh, NULL, name);
118 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
121 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, &uuid);
123 CERROR("1a: can't init llog handle: %d\n", rc);
127 rc = verify_handle("1", llh, 1);
129 CWARN("1b: close newly-created log\n");
131 rc2 = llog_close(env, llh);
133 CERROR("1b: close log %s failed: %d\n", name, rc2);
142 /* Test named-log reopen; returns opened log on success */
143 static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
144 char *name, struct llog_handle **llh)
146 struct llog_ctxt *ctxt;
147 struct llog_handle *loghandle;
148 struct llog_logid logid;
153 CWARN("2a: re-open a log with name: %s\n", name);
154 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
157 rc = llog_open(env, ctxt, llh, NULL, name, LLOG_OPEN_EXISTS);
159 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
163 rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
165 CERROR("2a: can't init llog handle: %d\n", rc);
166 GOTO(out_close_llh, rc);
169 rc = verify_handle("2", *llh, 1);
171 GOTO(out_close_llh, rc);
173 /* XXX: there is known issue with tests 2b, MGS is not able to create
174 * anonymous llog, exit now to allow following tests run.
175 * It is fixed in upcoming llog over OSD code */
178 CWARN("2b: create a log without specified NAME & LOGID\n");
179 rc = llog_open_create(env, ctxt, &loghandle, NULL, NULL);
181 CERROR("2b: create log failed\n");
182 GOTO(out_close_llh, rc);
184 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, &uuid);
186 CERROR("2b: can't init llog handle: %d\n", rc);
190 logid = loghandle->lgh_id;
191 llog_close(env, loghandle);
193 CWARN("2c: re-open the log by LOGID\n");
194 rc = llog_open(env, ctxt, &loghandle, &logid, NULL, LLOG_OPEN_EXISTS);
196 CERROR("2c: re-open log by LOGID failed\n");
197 GOTO(out_close_llh, rc);
200 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, &uuid);
202 CERROR("2c: can't init llog handle: %d\n", rc);
206 CWARN("2b: destroy this log\n");
207 rc = llog_destroy(env, loghandle);
209 CERROR("2d: destroy log failed\n");
211 llog_close(env, loghandle);
214 llog_close(env, *llh);
222 static off_t rec_offset;
224 static int start_idx;
228 * - check lgh_cur_offset correctness
229 * - check record index consistency
230 * - modify each record in-place
231 * - add new record during *last_idx processing
233 static int test3_check_n_add_cb(const struct lu_env *env,
234 struct llog_handle *lgh,
235 struct llog_rec_hdr *rec, void *data)
237 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
238 int *last_rec = data;
241 if (lgh->lgh_hdr->llh_flags & LLOG_F_IS_FIXSIZE) {
242 LASSERT(lgh->lgh_hdr->llh_size > 0);
243 if (lgh->lgh_cur_offset != lgh->lgh_hdr->llh_hdr.lrh_len +
244 (start_idx + records - 1) *
245 lgh->lgh_hdr->llh_size)
246 CERROR("Wrong record offset in cur_off: "LPU64", should"
247 " be %u\n", lgh->lgh_cur_offset,
248 lgh->lgh_hdr->llh_hdr.lrh_len +
249 (start_idx + records - 1) *
250 lgh->lgh_hdr->llh_size);
252 size_t chunk_size = lgh->lgh_hdr->llh_hdr.lrh_len;
254 /* For variable size records the start offset is unknown, trust
255 * the first value and check others are consistent with it. */
257 rec_offset = lgh->lgh_cur_offset;
259 if (lgh->lgh_cur_offset != rec_offset) {
260 /* there can be padding record */
261 if ((lgh->lgh_cur_offset % chunk_size == 0) &&
262 (lgh->lgh_cur_offset - rec_offset <
263 rec->lrh_len + LLOG_MIN_REC_SIZE)) {
264 rec_offset = lgh->lgh_cur_offset;
267 CERROR("Wrong record offset in cur_off: "LPU64
268 ", should be %lld (rec len %u)\n",
270 (long long)rec_offset, rec->lrh_len);
273 rec_offset += rec->lrh_len;
276 if ((start_idx + records + paddings) != rec->lrh_index)
277 CERROR("Record with wrong index was read: %u, expected %u\n",
278 rec->lrh_index, start_idx + records + paddings);
280 /* modify all records in place */
281 lgr->lgr_gen.conn_cnt = rec->lrh_index;
282 rc = llog_write(env, lgh, rec, rec->lrh_index);
284 CERROR("cb_test_3: cannot modify record while processing\n");
286 /* Add new record to the llog at *last_rec position one by one to
287 * check that last block is re-read during processing */
288 if ((start_idx + records + paddings) == *last_rec ||
289 (start_idx + records + paddings) == (*last_rec + 1)) {
290 rc = llog_write(env, lgh, rec, LLOG_NEXT_IDX);
292 CERROR("cb_test_3: cannot add new record while "
300 /* Check in-place modifications were done for all records*/
301 static int test3_check_cb(const struct lu_env *env, struct llog_handle *lgh,
302 struct llog_rec_hdr *rec, void *data)
304 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
306 if (lgr->lgr_gen.conn_cnt != rec->lrh_index) {
307 CERROR("cb_test_3: record %u is not modified\n",
315 static int llog_test3_process(const struct lu_env *env,
316 struct llog_handle *lgh,
317 llog_cb_t cb, int start)
319 struct llog_process_cat_data cd;
320 int last_idx; /* new record will be injected here */
323 CWARN("test3: processing records from index %d to the end\n",
325 cd.lpcd_first_idx = start - 1;
326 cd.lpcd_last_idx = 0;
327 records = paddings = 0;
328 last_idx = lgh->lgh_last_idx;
329 rc = llog_process(env, lgh, cb, &last_idx, &cd);
332 CWARN("test3: total %u records processed with %u paddings\n",
337 /* Test plain llog functionality */
338 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
339 struct llog_handle *llh)
342 struct llog_rec_hdr *hdr = (void *)buf;
344 int num_recs = 1; /* 1 for the header */
349 hdr->lrh_len = sizeof(struct llog_gen_rec);
350 hdr->lrh_type = LLOG_GEN_REC;
351 llh->lgh_hdr->llh_size = sizeof(struct llog_gen_rec);
352 llh->lgh_hdr->llh_flags |= LLOG_F_IS_FIXSIZE;
354 /* Fill the llog with 64-bytes records, use 1023 records,
355 * so last chunk will be partially full. Don't change this
356 * value until record size is changed.
358 CWARN("3a: write 1023 fixed-size llog records\n");
359 for (i = 0; i < 1023; i++) {
360 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
362 CERROR("3a: write 1023 records failed at #%d: %d\n",
369 rc = verify_handle("3a", llh, num_recs);
374 * Test fixed-size records processing:
375 * - search the needed index
376 * - go through all records from that index
377 * - check all indices are growing monotonically and exist
378 * - modify each record
380 * NB: test3_check_n_add adds two new records while processing
381 * after last record. There were 1023 records created so the last chunk
382 * misses exactly one record. Therefore one of new records will be
383 * the last in the current chunk and second causes the new chunk to be
389 rc = llog_test3_process(env, llh, test3_check_n_add_cb, start_idx);
393 /* extra record is created during llog_process() */
394 if (rc != expected) {
395 CERROR("3a: process total %d records but expect %d\n",
402 /* test modification in place */
403 rc = llog_test3_process(env, llh, test3_check_cb, start_idx);
407 if (rc != expected) {
408 CERROR("3a: process total %d records but expect %d\n",
413 CWARN("3b: write 566 variable size llog records\n");
415 /* Drop llh_size to 0 to mark llog as variable-size and write
416 * header to make this change permanent. */
417 llh->lgh_hdr->llh_flags &= ~LLOG_F_IS_FIXSIZE;
418 llog_write(env, llh, &llh->lgh_hdr->llh_hdr, LLOG_HEADER_IDX);
420 hdr->lrh_type = OBD_CFG_REC;
422 /* there are 1025 64-bytes records in llog already,
423 * the last chunk contains single record, i.e. 64 bytes.
424 * Each pair of variable size records is 200 bytes, so
425 * we will have the following distribution per chunks:
426 * block 1: 64 + 80(80/120) + 80 + 48(pad) = 81 iterations
427 * block 2: 80(120/80) + 120 + 72(pad) = 81 itereations
428 * block 3: 80(80/120) + 80 + 112(pad) = 81 iterations
429 * -- the same as block 2 again and so on.
430 * block 7: 80(80/120) = 80 iterations and 192 bytes remain
431 * Total 6 * 81 + 80 = 566 itereations.
432 * Callback will add another 120 bytes in the end of the last chunk
433 * and another 120 bytes will cause padding (72 bytes) plus 120
434 * bytes in the new block.
436 for (i = 0; i < 566; i++) {
442 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
444 CERROR("3a: write 566 records failed at #%d: %d\n",
451 rc = verify_handle("3b", llh, num_recs);
457 rc = llog_test3_process(env, llh, test3_check_n_add_cb, start_idx);
461 if (rc != expected) {
462 CERROR("3b: process total %d records but expect %d\n",
469 /* test modification in place */
470 rc = llog_test3_process(env, llh, test3_check_cb, start_idx);
474 if (rc != expected) {
475 CERROR("3b: process total %d records but expect %d\n",
480 CWARN("3c: write records with variable size until BITMAP_SIZE, "
482 while (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
483 if ((num_recs % 2) == 0)
488 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
492 CERROR("3c: write recs failed at #%d: %d\n",
500 CWARN("3c: write record more than BITMAP size!\n");
503 CWARN("3c: wrote %d more records before end of llog is reached\n",
506 rc = verify_handle("3d", llh, num_recs);
511 /* Test catalogue additions */
512 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
514 struct llog_handle *cath;
516 int rc, rc2, i, buflen;
517 struct llog_mini_rec lmr;
518 struct llog_cookie cookie;
519 struct llog_ctxt *ctxt;
522 struct llog_rec_hdr *rec;
526 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
529 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
530 lmr.lmr_hdr.lrh_type = 0xf00f00;
532 sprintf(name, "%x", llog_test_rand + 1);
533 CWARN("4a: create a catalog log with name: %s\n", name);
534 rc = llog_open_create(env, ctxt, &cath, NULL, name);
536 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
537 GOTO(ctxt_release, rc);
539 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
541 CERROR("4a: can't init llog handle: %d\n", rc);
546 cat_logid = cath->lgh_id;
548 CWARN("4b: write 1 record into the catalog\n");
549 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
551 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
555 rc = verify_handle("4b", cath, 2);
559 rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
563 CWARN("4c: cancel 1 log record\n");
564 rc = llog_cat_cancel_records(env, cath, 1, &cookie);
566 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
571 rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
575 CWARN("4d: write %d more log records\n", LLOG_TEST_RECNUM);
576 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
577 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
579 CERROR("4d: write %d records failed at #%d: %d\n",
580 LLOG_TEST_RECNUM, i + 1, rc);
586 /* make sure new plain llog appears */
587 rc = verify_handle("4d", cath, 3);
591 CWARN("4e: add 5 large records, one record per block\n");
592 buflen = LLOG_MIN_CHUNK_SIZE;
593 OBD_ALLOC(buf, buflen);
595 GOTO(out, rc = -ENOMEM);
596 for (i = 0; i < 5; i++) {
598 rec->lrh_len = buflen;
599 rec->lrh_type = OBD_CFG_REC;
600 rc = llog_cat_add(env, cath, rec, NULL);
602 CERROR("4e: write 5 records failed at #%d: %d\n",
609 OBD_FREE(buf, buflen);
611 CWARN("4f: put newly-created catalog\n");
612 rc2 = llog_cat_close(env, cath);
614 CERROR("4: close log %s failed: %d\n", name, rc2);
623 static int cat_counter;
625 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
626 struct llog_rec_hdr *rec, void *data)
628 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
629 struct lu_fid fid = {0};
631 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
632 CERROR("invalid record in catalog\n");
636 logid_to_fid(&lir->lid_id, &fid);
638 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
639 rec->lrh_index, PFID(&fid),
640 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
647 static int plain_counter;
649 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
650 struct llog_rec_hdr *rec, void *data)
652 struct lu_fid fid = {0};
654 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
655 CERROR("log is not plain\n");
659 logid_to_fid(&llh->lgh_id, &fid);
661 CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
662 rec->lrh_index, PFID(&fid));
669 static int cancel_count;
671 static int llog_cancel_rec_cb(const struct lu_env *env,
672 struct llog_handle *llh,
673 struct llog_rec_hdr *rec, void *data)
675 struct llog_cookie cookie;
677 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
678 CERROR("log is not plain\n");
682 cookie.lgc_lgl = llh->lgh_id;
683 cookie.lgc_index = rec->lrh_index;
685 llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
687 if (cancel_count == LLOG_TEST_RECNUM)
688 RETURN(-LLOG_EEMPTY);
692 /* Test log and catalogue processing */
693 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
695 struct llog_handle *llh = NULL;
698 struct llog_mini_rec lmr;
699 struct llog_ctxt *ctxt;
703 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
706 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
707 lmr.lmr_hdr.lrh_type = 0xf00f00;
709 CWARN("5a: re-open catalog by id\n");
710 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
712 CERROR("5a: llog_create with logid failed: %d\n", rc);
716 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
718 CERROR("5a: can't init llog handle: %d\n", rc);
722 CWARN("5b: print the catalog entries.. we expect 2\n");
724 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
726 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
729 if (cat_counter != 2) {
730 CERROR("5b: %d entries in catalog\n", cat_counter);
731 GOTO(out, rc = -EINVAL);
734 CWARN("5c: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
736 rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
737 if (rc != -LLOG_EEMPTY) {
738 CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
742 CWARN("5c: print the catalog entries.. we expect 1\n");
744 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
746 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
749 if (cat_counter != 1) {
750 CERROR("5c: %d entries in catalog\n", cat_counter);
751 GOTO(out, rc = -EINVAL);
754 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
755 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
757 CERROR("5d: add record to the log with many canceled empty "
762 CWARN("5e: print plain log entries.. expect 6\n");
764 rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
766 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
769 if (plain_counter != 6) {
770 CERROR("5e: found %d records\n", plain_counter);
771 GOTO(out, rc = -EINVAL);
774 CWARN("5f: print plain log entries reversely.. expect 6\n");
776 rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
778 CERROR("5f: reversely process with plain_print_cb failed: "
782 if (plain_counter != 6) {
783 CERROR("5f: found %d records\n", plain_counter);
784 GOTO(out, rc = -EINVAL);
788 CWARN("5g: close re-opened catalog\n");
789 rc2 = llog_cat_close(env, llh);
791 CERROR("5g: close log %s failed: %d\n", name, rc2);
801 /* Test client api; open log by name and process */
802 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
805 struct obd_device *mgc_obd;
806 struct llog_ctxt *ctxt;
807 struct obd_uuid *mgs_uuid;
808 struct obd_export *exp;
809 struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
810 struct llog_handle *llh = NULL;
811 struct llog_ctxt *nctxt;
814 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
816 mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
818 CWARN("6a: re-open log %s using client API\n", name);
819 mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
820 if (mgc_obd == NULL) {
821 CERROR("6a: no MGC devices connected to %s found.\n",
823 GOTO(ctxt_release, rc = -ENOENT);
826 rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
827 NULL /* obd_connect_data */, NULL);
828 if (rc != -EALREADY) {
829 CERROR("6a: connect on connected MGC (%s) failed to return"
830 " -EALREADY\n", mgc_obd->obd_name);
833 GOTO(ctxt_release, rc = -EINVAL);
836 nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
837 rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
839 CERROR("6a: llog_open failed %d\n", rc);
843 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
845 CERROR("6a: llog_init_handle failed %d\n", rc);
849 plain_counter = 1; /* llog header is first record */
850 CWARN("6b: process log %s using client API\n", name);
851 rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
853 CERROR("6b: llog_process failed %d\n", rc);
854 CWARN("6b: processed %d records\n", plain_counter);
856 rc = verify_handle("6b", llh, plain_counter);
860 plain_counter = 1; /* llog header is first record */
861 CWARN("6c: process log %s reversely using client API\n", name);
862 rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
864 CERROR("6c: llog_reverse_process failed %d\n", rc);
865 CWARN("6c: processed %d records\n", plain_counter);
867 rc = verify_handle("6c", llh, plain_counter);
872 rc2 = llog_close(env, llh);
874 CERROR("6: llog_close failed: rc = %d\n", rc2);
879 llog_ctxt_put(nctxt);
886 struct llog_rec_hdr lrh; /* common header */
887 struct llog_logid_rec llr; /* LLOG_LOGID_MAGIC */
888 struct llog_unlink64_rec lur; /* MDS_UNLINK64_REC */
889 struct llog_setattr64_rec lsr64; /* MDS_SETATTR64_REC */
890 struct llog_size_change_rec lscr; /* OST_SZ_REC */
891 struct llog_changelog_rec lcr; /* CHANGELOG_REC */
892 struct llog_changelog_user_rec lcur; /* CHANGELOG_USER_REC */
893 struct llog_gen_rec lgr; /* LLOG_GEN_REC */
896 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
897 struct llog_rec_hdr *rec, void *data)
899 struct lu_fid fid = {0};
901 logid_to_fid(&llh->lgh_id, &fid);
903 CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
904 rec->lrh_type, rec->lrh_index, PFID(&fid));
910 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
911 struct llog_rec_hdr *rec, void *data)
914 /* test LLOG_DEL_RECORD is working */
915 return LLOG_DEL_RECORD;
918 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
920 struct llog_handle *llh;
921 int rc = 0, i, process_count;
926 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
928 CERROR("7_sub: create log failed\n");
932 rc = llog_init_handle(env, llh,
933 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
936 CERROR("7_sub: can't init llog handle: %d\n", rc);
939 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
940 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
944 CERROR("7_sub: write recs failed at #%d: %d\n",
951 CWARN("7_sub: write record more than BITMAP size!\n");
952 GOTO(out_close, rc = -EINVAL);
955 rc = verify_handle("7_sub", llh, num_recs + 1);
957 CERROR("7_sub: verify handle failed: %d\n", rc);
960 if (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1)
961 CWARN("7_sub: records are not aligned, written %d from %u\n",
962 num_recs, LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
965 rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
967 CERROR("7_sub: llog process failed: %d\n", rc);
970 process_count = plain_counter;
971 if (process_count != num_recs) {
972 CERROR("7_sub: processed %d records from %d total\n",
973 process_count, num_recs);
974 GOTO(out_close, rc = -EINVAL);
978 rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
979 if (rc && rc != LLOG_DEL_PLAIN) {
980 CERROR("7_sub: reverse llog process failed: %d\n", rc);
983 if (process_count != plain_counter) {
984 CERROR("7_sub: Reverse/direct processing found different"
985 "number of records: %d/%d\n",
986 plain_counter, process_count);
987 GOTO(out_close, rc = -EINVAL);
989 if (llog_exist(llh)) {
990 CERROR("7_sub: llog exists but should be zapped\n");
991 GOTO(out_close, rc = -EEXIST);
994 rc = verify_handle("7_sub", llh, 1);
997 llog_destroy(env, llh);
998 llog_close(env, llh);
1002 /* Test all llog records writing and processing */
1003 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
1005 struct llog_ctxt *ctxt;
1010 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1012 CWARN("7a: test llog_logid_rec\n");
1013 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1014 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1015 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1017 rc = llog_test_7_sub(env, ctxt);
1019 CERROR("7a: llog_logid_rec test failed\n");
1023 CWARN("7b: test llog_unlink64_rec\n");
1024 llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
1025 llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
1026 llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
1028 rc = llog_test_7_sub(env, ctxt);
1030 CERROR("7b: llog_unlink_rec test failed\n");
1034 CWARN("7c: test llog_setattr64_rec\n");
1035 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
1036 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
1037 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1039 rc = llog_test_7_sub(env, ctxt);
1041 CERROR("7c: llog_setattr64_rec test failed\n");
1045 CWARN("7d: test llog_size_change_rec\n");
1046 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1047 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1048 llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
1050 rc = llog_test_7_sub(env, ctxt);
1052 CERROR("7d: llog_size_change_rec test failed\n");
1056 CWARN("7e: test llog_changelog_rec\n");
1057 /* Direct access to cr_do_not_use: peculiar case for this test */
1058 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1059 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1060 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1062 rc = llog_test_7_sub(env, ctxt);
1064 CERROR("7e: llog_changelog_rec test failed\n");
1068 CWARN("7f: test llog_changelog_user_rec\n");
1069 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1070 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1071 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1073 rc = llog_test_7_sub(env, ctxt);
1075 CERROR("7f: llog_changelog_user_rec test failed\n");
1079 CWARN("7g: test llog_gen_rec\n");
1080 llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
1081 llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
1082 llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
1084 rc = llog_test_7_sub(env, ctxt);
1086 CERROR("7g: llog_size_change_rec test failed\n");
1090 llog_ctxt_put(ctxt);
1094 static int llog_truncate(const struct lu_env *env, struct dt_object *o)
1098 struct dt_device *d;
1103 d = lu2dt_dev(o->do_lu.lo_dev);
1106 rc = dt_attr_get(env, o, &la);
1110 CDEBUG(D_OTHER, "original size "LPU64"\n", la.la_size);
1111 rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
1112 if (la.la_size < rc) {
1113 CERROR("too small llog: "LPU64"\n", la.la_size);
1117 /* drop 2 records */
1118 la.la_size = la.la_size - (sizeof(struct llog_mini_rec) * 2);
1119 la.la_valid = LA_SIZE;
1121 th = dt_trans_create(env, d);
1123 RETURN(PTR_ERR(th));
1125 rc = dt_declare_attr_set(env, o, &la, th);
1129 rc = dt_declare_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
1131 rc = dt_trans_start_local(env, d, th);
1135 rc = dt_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
1139 rc = dt_attr_set(env, o, &la, th);
1144 dt_trans_stop(env, d, th);
1149 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
1150 struct llog_rec_hdr *rec, void *data)
1156 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
1158 struct llog_handle *llh = NULL;
1162 struct llog_mini_rec lmr;
1163 struct llog_ctxt *ctxt;
1164 struct dt_object *obj = NULL;
1168 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1171 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1172 lmr.lmr_hdr.lrh_type = 0xf00f00;
1174 CWARN("8a: fill the first plain llog\n");
1175 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1177 CERROR("8a: llog_create with logid failed: %d\n", rc);
1181 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1183 CERROR("8a: can't init llog handle: %d\n", rc);
1188 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1190 CERROR("5a: process with test_8_cb failed: %d\n", rc);
1193 orig_counter = plain_counter;
1195 for (i = 0; i < 100; i++) {
1196 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1198 CERROR("5a: add record failed\n");
1203 /* grab the current plain llog, we'll corrupt it later */
1204 obj = llh->u.chd.chd_current_log->lgh_obj;
1206 lu_object_get(&obj->do_lu);
1207 CWARN("8a: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1209 rc2 = llog_cat_close(env, llh);
1211 CERROR("8a: close log %s failed: %d\n", name, rc2);
1217 CWARN("8b: fill the second plain llog\n");
1218 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1220 CERROR("8b: llog_create with logid failed: %d\n", rc);
1224 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1226 CERROR("8b: can't init llog handle: %d\n", rc);
1230 for (i = 0; i < 100; i++) {
1231 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1233 CERROR("8b: add record failed\n");
1237 CWARN("8b: second llog "DFID"\n",
1238 PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1240 rc2 = llog_cat_close(env, llh);
1242 CERROR("8b: close log %s failed: %d\n", name, rc2);
1248 CWARN("8c: drop two records from the first plain llog\n");
1249 llog_truncate(env, obj);
1251 CWARN("8d: count survived records\n");
1252 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1254 CERROR("8d: llog_create with logid failed: %d\n", rc);
1258 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1260 CERROR("8d: can't init llog handle: %d\n", rc);
1265 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1267 CERROR("8d: process with test_8_cb failed: %d\n", rc);
1271 if (orig_counter + 200 - 2 != plain_counter) {
1272 CERROR("found %d records (expected %d)\n", plain_counter,
1273 orig_counter + 200 - 2);
1278 CWARN("8d: close re-opened catalog\n");
1279 rc2 = llog_cat_close(env, llh);
1281 CERROR("8d: close log %s failed: %d\n", name, rc2);
1286 llog_ctxt_put(ctxt);
1289 lu_object_put(env, &obj->do_lu);
1294 static int llog_test_9_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
1296 struct llog_handle *llh;
1302 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1304 CERROR("9_sub: create log failed\n");
1308 rc = llog_init_handle(env, llh,
1309 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1312 CERROR("9_sub: can't init llog handle: %d\n", rc);
1313 GOTO(out_close, rc);
1316 logid_to_fid(&llh->lgh_id, &fid);
1317 fid_to_logid(&fid, &llog_records.llr.lid_id);
1318 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1320 CERROR("9_sub: write recs failed at #1: %d\n", rc);
1321 GOTO(out_close, rc);
1323 CWARN("9_sub: record type %x in log "DFID_NOBRACE"\n",
1324 llog_records.lrh.lrh_type, PFID(&fid));
1326 llog_close(env, llh);
1330 /* Prepare different types of llog records for llog_reader test*/
1331 static int llog_test_9(const struct lu_env *env, struct obd_device *obd)
1333 struct llog_ctxt *ctxt;
1338 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1340 CWARN("9a: test llog_logid_rec\n");
1341 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1342 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1343 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1345 rc = llog_test_9_sub(env, ctxt);
1347 CERROR("9a: llog_logid_rec test failed\n");
1351 CWARN("9b: test llog_obd_cfg_rec\n");
1352 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1353 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1354 llog_records.lscr.lsc_hdr.lrh_type = OBD_CFG_REC;
1356 rc = llog_test_9_sub(env, ctxt);
1358 CERROR("9b: llog_obd_cfg_rec test failed\n");
1362 CWARN("9c: test llog_changelog_rec\n");
1363 /* Direct access to cr_do_not_use: peculiar case for this test */
1364 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1365 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1366 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1368 rc = llog_test_9_sub(env, ctxt);
1370 CERROR("9c: llog_changelog_rec test failed\n");
1374 CWARN("9d: test llog_changelog_user_rec\n");
1375 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1376 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1377 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1379 rc = llog_test_9_sub(env, ctxt);
1381 CERROR("9d: llog_changelog_user_rec test failed\n");
1386 llog_ctxt_put(ctxt);
1390 /* test catalog wrap around */
1391 static int llog_test_10(const struct lu_env *env, struct obd_device *obd)
1393 struct llog_handle *cath;
1395 int rc, rc2, i, enospc, eok;
1396 struct llog_mini_rec lmr;
1397 struct llog_ctxt *ctxt;
1400 struct dt_device *dt;
1404 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1407 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1408 lmr.lmr_hdr.lrh_type = 0xf00f00;
1410 snprintf(name, sizeof(name), "%x", llog_test_rand + 2);
1411 CWARN("10a: create a catalog log with name: %s\n", name);
1412 rc = llog_open_create(env, ctxt, &cath, NULL, name);
1414 CERROR("10a: llog_create with name %s failed: %d\n", name, rc);
1415 GOTO(ctxt_release, rc);
1417 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
1419 CERROR("10a: can't init llog handle: %d\n", rc);
1423 cat_logid = cath->lgh_id;
1424 dt = lu2dt_dev(cath->lgh_obj->do_lu.lo_dev);
1426 /* force catalog wrap for 5th plain LLOG */
1427 cfs_fail_loc = CFS_FAIL_SKIP|OBD_FAIL_CAT_RECORDS;
1430 CWARN("10b: write %d log records\n", LLOG_TEST_RECNUM);
1431 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
1432 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1434 CERROR("10b: write %d records failed at #%d: %d\n",
1435 LLOG_TEST_RECNUM, i + 1, rc);
1440 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1441 rc = verify_handle("10b", cath, 3);
1445 /* sync device to commit all recent LLOG changes to disk and avoid
1446 * to consume a huge space with delayed journal commit callbacks
1447 * particularly on low memory nodes or VMs */
1448 rc = dt_sync(env, dt);
1450 CERROR("10b: sync failed: %d\n", rc);
1454 CWARN("10c: write %d more log records\n", 2 * LLOG_TEST_RECNUM);
1455 for (i = 0; i < 2 * LLOG_TEST_RECNUM; i++) {
1456 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1458 CERROR("10c: write %d records failed at #%d: %d\n",
1459 2*LLOG_TEST_RECNUM, i + 1, rc);
1464 /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1465 rc = verify_handle("10c", cath, 5);
1469 /* sync device to commit all recent LLOG changes to disk and avoid
1470 * to consume a huge space with delayed journal commit callbacks
1471 * particularly on low memory nodes or VMs */
1472 rc = dt_sync(env, dt);
1474 CERROR("10c: sync failed: %d\n", rc);
1478 /* fill last allocated plain LLOG and reach -ENOSPC condition
1479 * because no slot available in Catalog */
1482 CWARN("10c: write %d more log records\n", LLOG_TEST_RECNUM);
1483 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
1484 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1485 if (rc && rc != -ENOSPC) {
1486 CERROR("10c: write %d records failed at #%d: %d\n",
1487 LLOG_TEST_RECNUM, i + 1, rc);
1490 /* after last added plain LLOG has filled up, all new
1491 * records add should fail with -ENOSPC */
1492 if (rc == -ENOSPC) {
1500 if ((enospc == 0) && (enospc+eok != LLOG_TEST_RECNUM)) {
1501 CERROR("10c: all last records adds should have failed with"
1503 GOTO(out, rc = -EINVAL);
1506 CWARN("10c: wrote %d records then %d failed with ENOSPC\n", eok,
1509 /* make sure no new record in Catalog */
1510 rc = verify_handle("10c", cath, 5);
1514 /* Catalog should have reached its max size for test */
1515 rc = dt_attr_get(env, cath->lgh_obj, &la);
1517 CERROR("10c: failed to get catalog attrs: %d\n", rc);
1520 cat_max_size = la.la_size;
1522 /* cancel all 1st plain llog records to empty it, this will also cause
1523 * its catalog entry to be freed for next forced wrap in 10e */
1524 CWARN("10d: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
1526 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1527 if (rc != -LLOG_EEMPTY) {
1528 CERROR("10d: process with llog_cancel_rec_cb failed: %d\n", rc);
1529 /* need to indicate error if for any reason LLOG_TEST_RECNUM is
1536 CWARN("10d: print the catalog entries.. we expect 3\n");
1538 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1540 CERROR("10d: process with cat_print_cb failed: %d\n", rc);
1543 if (cat_counter != 3) {
1544 CERROR("10d: %d entries in catalog\n", cat_counter);
1545 GOTO(out, rc = -EINVAL);
1548 /* verify one down in catalog (+1 with hdr) */
1549 rc = verify_handle("10d", cath, 4);
1553 /* sync device to commit all recent LLOG changes to disk and avoid
1554 * to consume a huge space with delayed journal commit callbacks
1555 * particularly on low memory nodes or VMs */
1556 rc = dt_sync(env, dt);
1558 CERROR("10d: sync failed: %d\n", rc);
1564 CWARN("10e: write %d more log records\n", LLOG_TEST_RECNUM);
1565 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
1566 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1567 if (rc && rc != -ENOSPC) {
1568 CERROR("10e: write %d records failed at #%d: %d\n",
1569 LLOG_TEST_RECNUM, i + 1, rc);
1572 /* after last added plain LLOG has filled up, all new
1573 * records add should fail with -ENOSPC */
1574 if (rc == -ENOSPC) {
1582 if ((enospc == 0) && (enospc+eok != LLOG_TEST_RECNUM)) {
1583 CERROR("10e: all last records adds should have failed with"
1585 GOTO(out, rc = -EINVAL);
1588 CWARN("10e: wrote %d records then %d failed with ENOSPC\n", eok,
1591 CWARN("10e: print the catalog entries.. we expect 4\n");
1593 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1595 CERROR("10d: process with cat_print_cb failed: %d\n", rc);
1598 if (cat_counter != 4) {
1599 CERROR("10d: %d entries in catalog\n", cat_counter);
1600 GOTO(out, rc = -EINVAL);
1603 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1604 rc = verify_handle("10e", cath, 5);
1608 /* verify catalog has wrap around */
1609 if (cath->lgh_last_idx > cath->lgh_hdr->llh_cat_idx) {
1610 CERROR("10e: catalog failed to wrap around\n");
1611 GOTO(out, rc = -EINVAL);
1614 rc = dt_attr_get(env, cath->lgh_obj, &la);
1616 CERROR("10e: failed to get catalog attrs: %d\n", rc);
1620 if (la.la_size != cat_max_size) {
1621 CERROR("10e: catalog size has changed after it has wrap around,"
1622 " current size = "LPU64", expected size = "LPU64"\n",
1623 la.la_size, cat_max_size);
1624 GOTO(out, rc = -EINVAL);
1626 CWARN("10e: catalog successfully wrap around, last_idx %d, first %d\n",
1627 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1629 /* sync device to commit all recent LLOG changes to disk and avoid
1630 * to consume a huge space with delayed journal commit callbacks
1631 * particularly on low memory nodes or VMs */
1632 rc = dt_sync(env, dt);
1634 CERROR("10e: sync failed: %d\n", rc);
1638 /* cancel more records to free one more slot in Catalog
1639 * see if it is re-allocated when adding more records */
1640 CWARN("10f: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
1642 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1643 if (rc != -LLOG_EEMPTY) {
1644 CERROR("10f: process with llog_cancel_rec_cb failed: %d\n", rc);
1645 /* need to indicate error if for any reason LLOG_TEST_RECNUM is
1652 CWARN("10f: print the catalog entries.. we expect 3\n");
1654 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1656 CERROR("10f: process with cat_print_cb failed: %d\n", rc);
1659 if (cat_counter != 3) {
1660 CERROR("10f: %d entries in catalog\n", cat_counter);
1661 GOTO(out, rc = -EINVAL);
1664 /* verify one down in catalog (+1 with hdr) */
1665 rc = verify_handle("10f", cath, 4);
1669 /* sync device to commit all recent LLOG changes to disk and avoid
1670 * to consume a huge space with delayed journal commit callbacks
1671 * particularly on low memory nodes or VMs */
1672 rc = dt_sync(env, dt);
1674 CERROR("10f: sync failed: %d\n", rc);
1680 CWARN("10f: write %d more log records\n", LLOG_TEST_RECNUM);
1681 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
1682 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1683 if (rc && rc != -ENOSPC) {
1684 CERROR("10f: write %d records failed at #%d: %d\n",
1685 LLOG_TEST_RECNUM, i + 1, rc);
1688 /* after last added plain LLOG has filled up, all new
1689 * records add should fail with -ENOSPC */
1690 if (rc == -ENOSPC) {
1698 if ((enospc == 0) && (enospc+eok != LLOG_TEST_RECNUM)) {
1699 CERROR("10f: all last records adds should have failed with"
1701 GOTO(out, rc = -EINVAL);
1704 CWARN("10f: wrote %d records then %d failed with ENOSPC\n", eok,
1707 /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1708 rc = verify_handle("10f", cath, 5);
1712 /* verify lgh_last_idx = llh_cat_idx = 2 now */
1713 if (cath->lgh_last_idx != cath->lgh_hdr->llh_cat_idx ||
1714 cath->lgh_last_idx != 2) {
1715 CERROR("10f: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 2\n",
1716 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1717 GOTO(out, rc = -EINVAL);
1720 rc = dt_attr_get(env, cath->lgh_obj, &la);
1722 CERROR("10f: failed to get catalog attrs: %d\n", rc);
1726 if (la.la_size != cat_max_size) {
1727 CERROR("10f: catalog size has changed after it has wrap around,"
1728 " current size = "LPU64", expected size = "LPU64"\n",
1729 la.la_size, cat_max_size);
1730 GOTO(out, rc = -EINVAL);
1733 /* sync device to commit all recent LLOG changes to disk and avoid
1734 * to consume a huge space with delayed journal commit callbacks
1735 * particularly on low memory nodes or VMs */
1736 rc = dt_sync(env, dt);
1738 CERROR("10f: sync failed: %d\n", rc);
1742 /* will llh_cat_idx also successfully wrap ? */
1744 /* cancel all records in the plain LLOGs referenced by 2 last indexes in
1747 /* cancel more records to free one more slot in Catalog */
1748 CWARN("10g: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
1750 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1751 if (rc != -LLOG_EEMPTY) {
1752 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1753 /* need to indicate error if for any reason LLOG_TEST_RECNUM is
1760 CWARN("10g: print the catalog entries.. we expect 3\n");
1762 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1764 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1767 if (cat_counter != 3) {
1768 CERROR("10g: %d entries in catalog\n", cat_counter);
1769 GOTO(out, rc = -EINVAL);
1772 /* verify one down in catalog (+1 with hdr) */
1773 rc = verify_handle("10g", cath, 4);
1777 /* sync device to commit all recent LLOG changes to disk and avoid
1778 * to consume a huge space with delayed journal commit callbacks
1779 * particularly on low memory nodes or VMs */
1780 rc = dt_sync(env, dt);
1782 CERROR("10g: sync failed: %d\n", rc);
1786 /* cancel more records to free one more slot in Catalog */
1787 CWARN("10g: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
1789 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1790 if (rc != -LLOG_EEMPTY) {
1791 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1792 /* need to indicate error if for any reason LLOG_TEST_RECNUM is
1799 CWARN("10g: print the catalog entries.. we expect 2\n");
1801 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1803 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1806 if (cat_counter != 2) {
1807 CERROR("10g: %d entries in catalog\n", cat_counter);
1808 GOTO(out, rc = -EINVAL);
1811 /* verify one down in catalog (+1 with hdr) */
1812 rc = verify_handle("10g", cath, 3);
1816 /* verify lgh_last_idx = 2 and llh_cat_idx = 0 now */
1817 if (cath->lgh_hdr->llh_cat_idx != 0 ||
1818 cath->lgh_last_idx != 2) {
1819 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 0\n",
1820 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1821 GOTO(out, rc = -EINVAL);
1824 /* sync device to commit all recent LLOG changes to disk and avoid
1825 * to consume a huge space with delayed journal commit callbacks
1826 * particularly on low memory nodes or VMs */
1827 rc = dt_sync(env, dt);
1829 CERROR("10g: sync failed: %d\n", rc);
1833 /* cancel more records to free one more slot in Catalog */
1834 CWARN("10g: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
1836 rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1837 if (rc != -LLOG_EEMPTY) {
1838 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1839 /* need to indicate error if for any reason LLOG_TEST_RECNUM is
1846 CWARN("10g: print the catalog entries.. we expect 1\n");
1848 rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1850 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1853 if (cat_counter != 1) {
1854 CERROR("10g: %d entries in catalog\n", cat_counter);
1855 GOTO(out, rc = -EINVAL);
1858 /* verify one down in catalog (+1 with hdr) */
1859 rc = verify_handle("10g", cath, 2);
1863 /* verify lgh_last_idx = 2 and llh_cat_idx = 1 now */
1864 if (cath->lgh_hdr->llh_cat_idx != 1 ||
1865 cath->lgh_last_idx != 2) {
1866 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 1\n",
1867 cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1868 GOTO(out, rc = -EINVAL);
1871 CWARN("10g: llh_cat_idx has also successfully wrapped!\n");
1877 CWARN("10: put newly-created catalog\n");
1878 rc2 = llog_cat_close(env, cath);
1880 CERROR("10: close log %s failed: %d\n", name, rc2);
1885 llog_ctxt_put(ctxt);
1889 /* -------------------------------------------------------------------------
1890 * Tests above, boring obd functions below
1891 * ------------------------------------------------------------------------- */
1892 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
1894 struct llog_handle *llh = NULL;
1895 struct llog_ctxt *ctxt;
1900 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1903 sprintf(name, "%x", llog_test_rand);
1905 rc = llog_test_1(env, obd, name);
1907 GOTO(cleanup_ctxt, rc);
1909 rc = llog_test_2(env, obd, name, &llh);
1911 GOTO(cleanup_ctxt, rc);
1913 rc = llog_test_3(env, obd, llh);
1917 rc = llog_test_4(env, obd);
1921 rc = llog_test_5(env, obd);
1925 rc = llog_test_6(env, obd, name);
1929 rc = llog_test_7(env, obd);
1933 rc = llog_test_8(env, obd);
1937 rc = llog_test_9(env, obd);
1941 rc = llog_test_10(env, obd);
1946 err = llog_destroy(env, llh);
1948 CERROR("cleanup: llog_destroy failed: %d\n", err);
1949 llog_close(env, llh);
1953 llog_ctxt_put(ctxt);
1957 static int llog_test_cleanup(struct obd_device *obd)
1959 struct obd_device *tgt;
1965 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
1969 tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
1970 rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
1972 CERROR("failed to llog_test_llog_finish: %d\n", rc);
1977 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1979 struct obd_device *tgt;
1980 struct llog_ctxt *ctxt;
1981 struct dt_object *o;
1983 struct lu_context test_session;
1988 if (lcfg->lcfg_bufcount < 2) {
1989 CERROR("requires a TARGET OBD name\n");
1993 if (lcfg->lcfg_buflens[1] < 1) {
1994 CERROR("requires a TARGET OBD name\n");
1999 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2000 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2001 CERROR("target device not attached or not set up (%s)\n",
2002 lustre_cfg_string(lcfg, 1));
2006 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2010 rc = lu_context_init(&test_session, LCT_SERVER_SESSION);
2012 GOTO(cleanup_env, rc);
2013 test_session.lc_thread = (struct ptlrpc_thread *)current;
2014 lu_context_enter(&test_session);
2015 env.le_ses = &test_session;
2017 CWARN("Setup llog-test device over %s device\n",
2018 lustre_cfg_string(lcfg, 1));
2020 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2021 obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
2023 rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
2026 GOTO(cleanup_session, rc);
2028 /* use MGS llog dir for tests */
2029 ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
2032 llog_ctxt_put(ctxt);
2034 ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
2037 llog_ctxt_put(ctxt);
2039 llog_test_rand = cfs_rand();
2041 rc = llog_run_tests(&env, tgt);
2043 llog_test_cleanup(obd);
2045 lu_context_exit(&test_session);
2046 lu_context_fini(&test_session);
2052 static struct obd_ops llog_obd_ops = {
2053 .o_owner = THIS_MODULE,
2054 .o_setup = llog_test_setup,
2055 .o_cleanup = llog_test_cleanup,
2058 static int __init llog_test_init(void)
2060 return class_register_type(&llog_obd_ops, NULL, true, NULL,
2064 static void __exit llog_test_exit(void)
2066 class_unregister_type("llog_test");
2069 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2070 MODULE_DESCRIPTION("Lustre Log test module");
2071 MODULE_VERSION(LUSTRE_VERSION_STRING);
2072 MODULE_LICENSE("GPL");
2074 module_init(llog_test_init);
2075 module_exit(llog_test_exit);