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, 2014, 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 if (llh->lgh_last_idx < last_idx) {
91 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
92 test, llh->lgh_last_idx, last_idx);
99 /* Test named-log create/open, close */
100 static int llog_test_1(const struct lu_env *env,
101 struct obd_device *obd, char *name)
103 struct llog_handle *llh;
104 struct llog_ctxt *ctxt;
110 CWARN("1a: create a log with name: %s\n", name);
111 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
114 rc = llog_open_create(env, ctxt, &llh, NULL, name);
116 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
119 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, &uuid);
121 CERROR("1a: can't init llog handle: %d\n", rc);
125 rc = verify_handle("1", llh, 1);
127 CWARN("1b: close newly-created log\n");
129 rc2 = llog_close(env, llh);
131 CERROR("1b: close log %s failed: %d\n", name, rc2);
140 /* Test named-log reopen; returns opened log on success */
141 static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
142 char *name, struct llog_handle **llh)
144 struct llog_ctxt *ctxt;
145 struct llog_handle *loghandle;
146 struct llog_logid logid;
151 CWARN("2a: re-open a log with name: %s\n", name);
152 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
155 rc = llog_open(env, ctxt, llh, NULL, name, LLOG_OPEN_EXISTS);
157 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
161 rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
163 CERROR("2a: can't init llog handle: %d\n", rc);
164 GOTO(out_close_llh, rc);
167 rc = verify_handle("2", *llh, 1);
169 GOTO(out_close_llh, rc);
171 /* XXX: there is known issue with tests 2b, MGS is not able to create
172 * anonymous llog, exit now to allow following tests run.
173 * It is fixed in upcoming llog over OSD code */
176 CWARN("2b: create a log without specified NAME & LOGID\n");
177 rc = llog_open_create(env, ctxt, &loghandle, NULL, NULL);
179 CERROR("2b: create log failed\n");
180 GOTO(out_close_llh, rc);
182 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, &uuid);
184 CERROR("2b: can't init llog handle: %d\n", rc);
188 logid = loghandle->lgh_id;
189 llog_close(env, loghandle);
191 CWARN("2c: re-open the log by LOGID\n");
192 rc = llog_open(env, ctxt, &loghandle, &logid, NULL, LLOG_OPEN_EXISTS);
194 CERROR("2c: re-open log by LOGID failed\n");
195 GOTO(out_close_llh, rc);
198 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, &uuid);
200 CERROR("2c: can't init llog handle: %d\n", rc);
204 CWARN("2b: destroy this log\n");
205 rc = llog_destroy(env, loghandle);
207 CERROR("2d: destroy log failed\n");
209 llog_close(env, loghandle);
212 llog_close(env, *llh);
220 static off_t rec_offset;
222 static int start_idx;
226 * - check lgh_cur_offset correctness
227 * - check record index consistency
228 * - modify each record in-place
229 * - add new record during *last_idx processing
231 static int test3_check_n_add_cb(const struct lu_env *env,
232 struct llog_handle *lgh,
233 struct llog_rec_hdr *rec, void *data)
235 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
236 int *last_rec = data;
239 if (lgh->lgh_hdr->llh_flags & LLOG_F_IS_FIXSIZE) {
240 LASSERT(lgh->lgh_hdr->llh_size > 0);
241 if (lgh->lgh_cur_offset != lgh->lgh_hdr->llh_hdr.lrh_len +
242 (start_idx + records - 1) *
243 lgh->lgh_hdr->llh_size)
244 CERROR("Wrong record offset in cur_off: "LPU64", should"
245 " be %u\n", lgh->lgh_cur_offset,
246 lgh->lgh_hdr->llh_hdr.lrh_len +
247 (start_idx + records - 1) *
248 lgh->lgh_hdr->llh_size);
250 size_t chunk_size = lgh->lgh_hdr->llh_hdr.lrh_len;
252 /* For variable size records the start offset is unknown, trust
253 * the first value and check others are consistent with it. */
255 rec_offset = lgh->lgh_cur_offset;
257 if (lgh->lgh_cur_offset != rec_offset) {
258 /* there can be padding record */
259 if ((lgh->lgh_cur_offset % chunk_size == 0) &&
260 (lgh->lgh_cur_offset - rec_offset <
261 rec->lrh_len + LLOG_MIN_REC_SIZE)) {
262 rec_offset = lgh->lgh_cur_offset;
265 CERROR("Wrong record offset in cur_off: "LPU64
266 ", should be %lld (rec len %u)\n",
268 (long long)rec_offset, rec->lrh_len);
271 rec_offset += rec->lrh_len;
274 if ((start_idx + records + paddings) != rec->lrh_index)
275 CERROR("Record with wrong index was read: %u, expected %u\n",
276 rec->lrh_index, start_idx + records + paddings);
278 /* modify all records in place */
279 lgr->lgr_gen.conn_cnt = rec->lrh_index;
280 rc = llog_write(env, lgh, rec, rec->lrh_index);
282 CERROR("cb_test_3: cannot modify record while processing\n");
284 /* Add new record to the llog at *last_rec position one by one to
285 * check that last block is re-read during processing */
286 if ((start_idx + records + paddings) == *last_rec ||
287 (start_idx + records + paddings) == (*last_rec + 1)) {
288 rc = llog_write(env, lgh, rec, LLOG_NEXT_IDX);
290 CERROR("cb_test_3: cannot add new record while "
298 /* Check in-place modifications were done for all records*/
299 static int test3_check_cb(const struct lu_env *env, struct llog_handle *lgh,
300 struct llog_rec_hdr *rec, void *data)
302 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
304 if (lgr->lgr_gen.conn_cnt != rec->lrh_index) {
305 CERROR("cb_test_3: record %u is not modified\n",
313 static int llog_test3_process(const struct lu_env *env,
314 struct llog_handle *lgh,
315 llog_cb_t cb, int start)
317 struct llog_process_cat_data cd;
318 int last_idx; /* new record will be injected here */
321 CWARN("test3: processing records from index %d to the end\n",
323 cd.lpcd_first_idx = start - 1;
324 cd.lpcd_last_idx = 0;
325 records = paddings = 0;
326 last_idx = lgh->lgh_last_idx;
327 rc = llog_process(env, lgh, cb, &last_idx, &cd);
330 CWARN("test3: total %u records processed with %u paddings\n",
335 /* Test plain llog functionality */
336 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
337 struct llog_handle *llh)
340 struct llog_rec_hdr *hdr = (void *)buf;
342 int num_recs = 1; /* 1 for the header */
347 hdr->lrh_len = sizeof(struct llog_gen_rec);
348 hdr->lrh_type = LLOG_GEN_REC;
349 llh->lgh_hdr->llh_size = sizeof(struct llog_gen_rec);
350 llh->lgh_hdr->llh_flags |= LLOG_F_IS_FIXSIZE;
352 /* Fill the llog with 64-bytes records, use 1023 records,
353 * so last chunk will be partially full. Don't change this
354 * value until record size is changed.
356 CWARN("3a: write 1023 fixed-size llog records\n");
357 for (i = 0; i < 1023; i++) {
358 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
360 CERROR("3a: write 1023 records failed at #%d: %d\n",
367 rc = verify_handle("3a", llh, num_recs);
372 * Test fixed-size records processing:
373 * - search the needed index
374 * - go through all records from that index
375 * - check all indices are growing monotonically and exist
376 * - modify each record
378 * NB: test3_check_n_add adds two new records while processing
379 * after last record. There were 1023 records created so the last chunk
380 * misses exactly one record. Therefore one of new records will be
381 * the last in the current chunk and second causes the new chunk to be
387 rc = llog_test3_process(env, llh, test3_check_n_add_cb, start_idx);
391 /* extra record is created during llog_process() */
392 if (rc != expected) {
393 CERROR("3a: process total %d records but expect %d\n",
400 /* test modification in place */
401 rc = llog_test3_process(env, llh, test3_check_cb, start_idx);
405 if (rc != expected) {
406 CERROR("3a: process total %d records but expect %d\n",
411 CWARN("3b: write 566 variable size llog records\n");
413 /* Drop llh_size to 0 to mark llog as variable-size and write
414 * header to make this change permanent. */
415 llh->lgh_hdr->llh_flags &= ~LLOG_F_IS_FIXSIZE;
416 llog_write(env, llh, &llh->lgh_hdr->llh_hdr, LLOG_HEADER_IDX);
418 hdr->lrh_type = OBD_CFG_REC;
420 /* there are 1025 64-bytes records in llog already,
421 * the last chunk contains single record, i.e. 64 bytes.
422 * Each pair of variable size records is 200 bytes, so
423 * we will have the following distribution per chunks:
424 * block 1: 64 + 80(80/120) + 80 + 48(pad) = 81 iterations
425 * block 2: 80(120/80) + 120 + 72(pad) = 81 itereations
426 * block 3: 80(80/120) + 80 + 112(pad) = 81 iterations
427 * -- the same as block 2 again and so on.
428 * block 7: 80(80/120) = 80 iterations and 192 bytes remain
429 * Total 6 * 81 + 80 = 566 itereations.
430 * Callback will add another 120 bytes in the end of the last chunk
431 * and another 120 bytes will cause padding (72 bytes) plus 120
432 * bytes in the new block.
434 for (i = 0; i < 566; i++) {
440 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
442 CERROR("3a: write 566 records failed at #%d: %d\n",
449 rc = verify_handle("3b", llh, num_recs);
455 rc = llog_test3_process(env, llh, test3_check_n_add_cb, start_idx);
459 if (rc != expected) {
460 CERROR("3b: process total %d records but expect %d\n",
467 /* test modification in place */
468 rc = llog_test3_process(env, llh, test3_check_cb, start_idx);
472 if (rc != expected) {
473 CERROR("3b: process total %d records but expect %d\n",
478 CWARN("3c: write records with variable size until BITMAP_SIZE, "
480 while (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
481 if ((num_recs % 2) == 0)
486 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
490 CERROR("3c: write recs failed at #%d: %d\n",
498 CWARN("3c: write record more than BITMAP size!\n");
501 CWARN("3c: wrote %d more records before end of llog is reached\n",
504 rc = verify_handle("3d", llh, num_recs);
509 /* Test catalogue additions */
510 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
512 struct llog_handle *cath;
514 int rc, rc2, i, buflen;
515 struct llog_mini_rec lmr;
516 struct llog_cookie cookie;
517 struct llog_ctxt *ctxt;
520 struct llog_rec_hdr *rec;
524 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
527 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
528 lmr.lmr_hdr.lrh_type = 0xf00f00;
530 sprintf(name, "%x", llog_test_rand + 1);
531 CWARN("4a: create a catalog log with name: %s\n", name);
532 rc = llog_open_create(env, ctxt, &cath, NULL, name);
534 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
535 GOTO(ctxt_release, rc);
537 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
539 CERROR("4a: can't init llog handle: %d\n", rc);
544 cat_logid = cath->lgh_id;
546 CWARN("4b: write 1 record into the catalog\n");
547 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
549 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
553 rc = verify_handle("4b", cath, 2);
557 rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
561 CWARN("4c: cancel 1 log record\n");
562 rc = llog_cat_cancel_records(env, cath, 1, &cookie);
564 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
569 rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
573 CWARN("4d: write %d more log records\n", LLOG_TEST_RECNUM);
574 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
575 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
577 CERROR("4d: write %d records failed at #%d: %d\n",
578 LLOG_TEST_RECNUM, i + 1, rc);
584 /* make sure new plain llog appears */
585 rc = verify_handle("4d", cath, 3);
589 CWARN("4e: add 5 large records, one record per block\n");
590 buflen = LLOG_MIN_CHUNK_SIZE;
591 OBD_ALLOC(buf, buflen);
593 GOTO(out, rc = -ENOMEM);
594 for (i = 0; i < 5; i++) {
596 rec->lrh_len = buflen;
597 rec->lrh_type = OBD_CFG_REC;
598 rc = llog_cat_add(env, cath, rec, NULL);
600 CERROR("4e: write 5 records failed at #%d: %d\n",
607 OBD_FREE(buf, buflen);
609 CWARN("4f: put newly-created catalog\n");
610 rc2 = llog_cat_close(env, cath);
612 CERROR("4: close log %s failed: %d\n", name, rc2);
621 static int cat_counter;
623 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
624 struct llog_rec_hdr *rec, void *data)
626 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
627 struct lu_fid fid = {0};
629 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
630 CERROR("invalid record in catalog\n");
634 logid_to_fid(&lir->lid_id, &fid);
636 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
637 rec->lrh_index, PFID(&fid),
638 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
645 static int plain_counter;
647 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
648 struct llog_rec_hdr *rec, void *data)
650 struct lu_fid fid = {0};
652 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
653 CERROR("log is not plain\n");
657 logid_to_fid(&llh->lgh_id, &fid);
659 CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
660 rec->lrh_index, PFID(&fid));
667 static int cancel_count;
669 static int llog_cancel_rec_cb(const struct lu_env *env,
670 struct llog_handle *llh,
671 struct llog_rec_hdr *rec, void *data)
673 struct llog_cookie cookie;
675 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
676 CERROR("log is not plain\n");
680 cookie.lgc_lgl = llh->lgh_id;
681 cookie.lgc_index = rec->lrh_index;
683 llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
685 if (cancel_count == LLOG_TEST_RECNUM)
686 RETURN(-LLOG_EEMPTY);
690 /* Test log and catalogue processing */
691 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
693 struct llog_handle *llh = NULL;
696 struct llog_mini_rec lmr;
697 struct llog_ctxt *ctxt;
701 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
704 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
705 lmr.lmr_hdr.lrh_type = 0xf00f00;
707 CWARN("5a: re-open catalog by id\n");
708 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
710 CERROR("5a: llog_create with logid failed: %d\n", rc);
714 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
716 CERROR("5a: can't init llog handle: %d\n", rc);
720 CWARN("5b: print the catalog entries.. we expect 2\n");
722 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
724 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
727 if (cat_counter != 2) {
728 CERROR("5b: %d entries in catalog\n", cat_counter);
729 GOTO(out, rc = -EINVAL);
732 CWARN("5c: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
734 rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
735 if (rc != -LLOG_EEMPTY) {
736 CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
740 CWARN("5c: print the catalog entries.. we expect 1\n");
742 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
744 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
747 if (cat_counter != 1) {
748 CERROR("5c: %d entries in catalog\n", cat_counter);
749 GOTO(out, rc = -EINVAL);
752 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
753 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
755 CERROR("5d: add record to the log with many canceled empty "
760 CWARN("5e: print plain log entries.. expect 6\n");
762 rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
764 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
767 if (plain_counter != 6) {
768 CERROR("5e: found %d records\n", plain_counter);
769 GOTO(out, rc = -EINVAL);
772 CWARN("5f: print plain log entries reversely.. expect 6\n");
774 rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
776 CERROR("5f: reversely process with plain_print_cb failed: "
780 if (plain_counter != 6) {
781 CERROR("5f: found %d records\n", plain_counter);
782 GOTO(out, rc = -EINVAL);
786 CWARN("5g: close re-opened catalog\n");
787 rc2 = llog_cat_close(env, llh);
789 CERROR("5g: close log %s failed: %d\n", name, rc2);
799 /* Test client api; open log by name and process */
800 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
803 struct obd_device *mgc_obd;
804 struct llog_ctxt *ctxt;
805 struct obd_uuid *mgs_uuid;
806 struct obd_export *exp;
807 struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
808 struct llog_handle *llh = NULL;
809 struct llog_ctxt *nctxt;
812 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
814 mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
816 CWARN("6a: re-open log %s using client API\n", name);
817 mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
818 if (mgc_obd == NULL) {
819 CERROR("6a: no MGC devices connected to %s found.\n",
821 GOTO(ctxt_release, rc = -ENOENT);
824 rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
825 NULL /* obd_connect_data */, NULL);
826 if (rc != -EALREADY) {
827 CERROR("6a: connect on connected MGC (%s) failed to return"
828 " -EALREADY\n", mgc_obd->obd_name);
831 GOTO(ctxt_release, rc = -EINVAL);
834 nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
835 rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
837 CERROR("6a: llog_open failed %d\n", rc);
841 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
843 CERROR("6a: llog_init_handle failed %d\n", rc);
847 plain_counter = 1; /* llog header is first record */
848 CWARN("6b: process log %s using client API\n", name);
849 rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
851 CERROR("6b: llog_process failed %d\n", rc);
852 CWARN("6b: processed %d records\n", plain_counter);
854 rc = verify_handle("6b", llh, plain_counter);
858 plain_counter = 1; /* llog header is first record */
859 CWARN("6c: process log %s reversely using client API\n", name);
860 rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
862 CERROR("6c: llog_reverse_process failed %d\n", rc);
863 CWARN("6c: processed %d records\n", plain_counter);
865 rc = verify_handle("6c", llh, plain_counter);
870 rc2 = llog_close(env, llh);
872 CERROR("6: llog_close failed: rc = %d\n", rc2);
877 llog_ctxt_put(nctxt);
884 struct llog_rec_hdr lrh; /* common header */
885 struct llog_logid_rec llr; /* LLOG_LOGID_MAGIC */
886 struct llog_unlink64_rec lur; /* MDS_UNLINK64_REC */
887 struct llog_setattr64_rec lsr64; /* MDS_SETATTR64_REC */
888 struct llog_size_change_rec lscr; /* OST_SZ_REC */
889 struct llog_changelog_rec lcr; /* CHANGELOG_REC */
890 struct llog_changelog_user_rec lcur; /* CHANGELOG_USER_REC */
891 struct llog_gen_rec lgr; /* LLOG_GEN_REC */
894 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
895 struct llog_rec_hdr *rec, void *data)
897 struct lu_fid fid = {0};
899 logid_to_fid(&llh->lgh_id, &fid);
901 CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
902 rec->lrh_type, rec->lrh_index, PFID(&fid));
908 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
909 struct llog_rec_hdr *rec, void *data)
912 /* test LLOG_DEL_RECORD is working */
913 return LLOG_DEL_RECORD;
916 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
918 struct llog_handle *llh;
919 int rc = 0, i, process_count;
924 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
926 CERROR("7_sub: create log failed\n");
930 rc = llog_init_handle(env, llh,
931 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
934 CERROR("7_sub: can't init llog handle: %d\n", rc);
937 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
938 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
942 CERROR("7_sub: write recs failed at #%d: %d\n",
949 CWARN("7_sub: write record more than BITMAP size!\n");
950 GOTO(out_close, rc = -EINVAL);
953 rc = verify_handle("7_sub", llh, num_recs + 1);
955 CERROR("7_sub: verify handle failed: %d\n", rc);
958 if (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1)
959 CWARN("7_sub: records are not aligned, written %d from %u\n",
960 num_recs, LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
963 rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
965 CERROR("7_sub: llog process failed: %d\n", rc);
968 process_count = plain_counter;
969 if (process_count != num_recs) {
970 CERROR("7_sub: processed %d records from %d total\n",
971 process_count, num_recs);
972 GOTO(out_close, rc = -EINVAL);
976 rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
977 if (rc && rc != LLOG_DEL_PLAIN) {
978 CERROR("7_sub: reverse llog process failed: %d\n", rc);
981 if (process_count != plain_counter) {
982 CERROR("7_sub: Reverse/direct processing found different"
983 "number of records: %d/%d\n",
984 plain_counter, process_count);
985 GOTO(out_close, rc = -EINVAL);
987 if (llog_exist(llh)) {
988 CERROR("7_sub: llog exists but should be zapped\n");
989 GOTO(out_close, rc = -EEXIST);
992 rc = verify_handle("7_sub", llh, 1);
995 llog_destroy(env, llh);
996 llog_close(env, llh);
1000 /* Test all llog records writing and processing */
1001 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
1003 struct llog_ctxt *ctxt;
1008 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1010 CWARN("7a: test llog_logid_rec\n");
1011 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1012 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1013 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1015 rc = llog_test_7_sub(env, ctxt);
1017 CERROR("7a: llog_logid_rec test failed\n");
1021 CWARN("7b: test llog_unlink64_rec\n");
1022 llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
1023 llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
1024 llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
1026 rc = llog_test_7_sub(env, ctxt);
1028 CERROR("7b: llog_unlink_rec test failed\n");
1032 CWARN("7c: test llog_setattr64_rec\n");
1033 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
1034 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
1035 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1037 rc = llog_test_7_sub(env, ctxt);
1039 CERROR("7c: llog_setattr64_rec test failed\n");
1043 CWARN("7d: test llog_size_change_rec\n");
1044 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1045 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1046 llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
1048 rc = llog_test_7_sub(env, ctxt);
1050 CERROR("7d: llog_size_change_rec test failed\n");
1054 CWARN("7e: test llog_changelog_rec\n");
1055 /* Direct access to cr_do_not_use: peculiar case for this test */
1056 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1057 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1058 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1060 rc = llog_test_7_sub(env, ctxt);
1062 CERROR("7e: llog_changelog_rec test failed\n");
1066 CWARN("7f: test llog_changelog_user_rec\n");
1067 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1068 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1069 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1071 rc = llog_test_7_sub(env, ctxt);
1073 CERROR("7f: llog_changelog_user_rec test failed\n");
1077 CWARN("7g: test llog_gen_rec\n");
1078 llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
1079 llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
1080 llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
1082 rc = llog_test_7_sub(env, ctxt);
1084 CERROR("7g: llog_size_change_rec test failed\n");
1088 llog_ctxt_put(ctxt);
1092 static int llog_truncate(const struct lu_env *env, struct dt_object *o)
1096 struct dt_device *d;
1101 d = lu2dt_dev(o->do_lu.lo_dev);
1104 rc = dt_attr_get(env, o, &la);
1108 CDEBUG(D_OTHER, "original size "LPU64"\n", la.la_size);
1109 rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
1110 if (la.la_size < rc) {
1111 CERROR("too small llog: "LPU64"\n", la.la_size);
1115 /* drop 2 records */
1116 la.la_size = la.la_size - (sizeof(struct llog_mini_rec) * 2);
1117 la.la_valid = LA_SIZE;
1119 th = dt_trans_create(env, d);
1121 RETURN(PTR_ERR(th));
1123 rc = dt_declare_attr_set(env, o, &la, th);
1127 rc = dt_declare_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
1129 rc = dt_trans_start_local(env, d, th);
1133 rc = dt_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
1137 rc = dt_attr_set(env, o, &la, th);
1142 dt_trans_stop(env, d, th);
1147 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
1148 struct llog_rec_hdr *rec, void *data)
1154 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
1156 struct llog_handle *llh = NULL;
1160 struct llog_mini_rec lmr;
1161 struct llog_ctxt *ctxt;
1162 struct dt_object *obj = NULL;
1166 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1169 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1170 lmr.lmr_hdr.lrh_type = 0xf00f00;
1172 CWARN("8a: fill the first plain llog\n");
1173 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1175 CERROR("8a: llog_create with logid failed: %d\n", rc);
1179 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1181 CERROR("8a: can't init llog handle: %d\n", rc);
1186 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1188 CERROR("5a: process with test_8_cb failed: %d\n", rc);
1191 orig_counter = plain_counter;
1193 for (i = 0; i < 100; i++) {
1194 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1196 CERROR("5a: add record failed\n");
1201 /* grab the current plain llog, we'll corrupt it later */
1202 obj = llh->u.chd.chd_current_log->lgh_obj;
1204 lu_object_get(&obj->do_lu);
1205 CWARN("8a: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1207 rc2 = llog_cat_close(env, llh);
1209 CERROR("8a: close log %s failed: %d\n", name, rc2);
1215 CWARN("8b: fill the second plain llog\n");
1216 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1218 CERROR("8b: llog_create with logid failed: %d\n", rc);
1222 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1224 CERROR("8b: can't init llog handle: %d\n", rc);
1228 for (i = 0; i < 100; i++) {
1229 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1231 CERROR("8b: add record failed\n");
1235 CWARN("8b: second llog "DFID"\n",
1236 PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1238 rc2 = llog_cat_close(env, llh);
1240 CERROR("8b: close log %s failed: %d\n", name, rc2);
1246 CWARN("8c: drop two records from the first plain llog\n");
1247 llog_truncate(env, obj);
1249 CWARN("8d: count survived records\n");
1250 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1252 CERROR("8d: llog_create with logid failed: %d\n", rc);
1256 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1258 CERROR("8d: can't init llog handle: %d\n", rc);
1263 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1265 CERROR("8d: process with test_8_cb failed: %d\n", rc);
1269 if (orig_counter + 200 - 2 != plain_counter) {
1270 CERROR("found %d records (expected %d)\n", plain_counter,
1271 orig_counter + 200 - 2);
1276 CWARN("8d: close re-opened catalog\n");
1277 rc2 = llog_cat_close(env, llh);
1279 CERROR("8d: close log %s failed: %d\n", name, rc2);
1284 llog_ctxt_put(ctxt);
1287 lu_object_put(env, &obj->do_lu);
1292 static int llog_test_9_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
1294 struct llog_handle *llh;
1300 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1302 CERROR("9_sub: create log failed\n");
1306 rc = llog_init_handle(env, llh,
1307 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1310 CERROR("9_sub: can't init llog handle: %d\n", rc);
1311 GOTO(out_close, rc);
1314 logid_to_fid(&llh->lgh_id, &fid);
1315 fid_to_logid(&fid, &llog_records.llr.lid_id);
1316 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1318 CERROR("9_sub: write recs failed at #1: %d\n", rc);
1319 GOTO(out_close, rc);
1321 CWARN("9_sub: record type %x in log "DFID_NOBRACE"\n",
1322 llog_records.lrh.lrh_type, PFID(&fid));
1324 llog_close(env, llh);
1328 /* Prepare different types of llog records for llog_reader test*/
1329 static int llog_test_9(const struct lu_env *env, struct obd_device *obd)
1331 struct llog_ctxt *ctxt;
1336 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1338 CWARN("9a: test llog_logid_rec\n");
1339 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1340 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1341 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1343 rc = llog_test_9_sub(env, ctxt);
1345 CERROR("9a: llog_logid_rec test failed\n");
1349 CWARN("9b: test llog_obd_cfg_rec\n");
1350 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1351 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1352 llog_records.lscr.lsc_hdr.lrh_type = OBD_CFG_REC;
1354 rc = llog_test_9_sub(env, ctxt);
1356 CERROR("9b: llog_obd_cfg_rec test failed\n");
1360 CWARN("9c: test llog_changelog_rec\n");
1361 /* Direct access to cr_do_not_use: peculiar case for this test */
1362 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1363 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1364 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1366 rc = llog_test_9_sub(env, ctxt);
1368 CERROR("9c: llog_changelog_rec test failed\n");
1372 CWARN("9d: test llog_changelog_user_rec\n");
1373 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1374 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1375 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1377 rc = llog_test_9_sub(env, ctxt);
1379 CERROR("9d: llog_changelog_user_rec test failed\n");
1384 llog_ctxt_put(ctxt);
1387 /* -------------------------------------------------------------------------
1388 * Tests above, boring obd functions below
1389 * ------------------------------------------------------------------------- */
1390 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
1392 struct llog_handle *llh = NULL;
1393 struct llog_ctxt *ctxt;
1398 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1401 sprintf(name, "%x", llog_test_rand);
1403 rc = llog_test_1(env, obd, name);
1405 GOTO(cleanup_ctxt, rc);
1407 rc = llog_test_2(env, obd, name, &llh);
1409 GOTO(cleanup_ctxt, rc);
1411 rc = llog_test_3(env, obd, llh);
1415 rc = llog_test_4(env, obd);
1419 rc = llog_test_5(env, obd);
1423 rc = llog_test_6(env, obd, name);
1427 rc = llog_test_7(env, obd);
1431 rc = llog_test_8(env, obd);
1435 rc = llog_test_9(env, obd);
1439 err = llog_destroy(env, llh);
1441 CERROR("cleanup: llog_destroy failed: %d\n", err);
1442 llog_close(env, llh);
1446 llog_ctxt_put(ctxt);
1450 static int llog_test_cleanup(struct obd_device *obd)
1452 struct obd_device *tgt;
1458 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
1462 tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
1463 rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
1465 CERROR("failed to llog_test_llog_finish: %d\n", rc);
1470 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1472 struct obd_device *tgt;
1473 struct llog_ctxt *ctxt;
1474 struct dt_object *o;
1476 struct lu_context test_session;
1481 if (lcfg->lcfg_bufcount < 2) {
1482 CERROR("requires a TARGET OBD name\n");
1486 if (lcfg->lcfg_buflens[1] < 1) {
1487 CERROR("requires a TARGET OBD name\n");
1492 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1493 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1494 CERROR("target device not attached or not set up (%s)\n",
1495 lustre_cfg_string(lcfg, 1));
1499 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
1503 rc = lu_context_init(&test_session, LCT_SERVER_SESSION);
1505 GOTO(cleanup_env, rc);
1506 test_session.lc_thread = (struct ptlrpc_thread *)current;
1507 lu_context_enter(&test_session);
1508 env.le_ses = &test_session;
1510 CWARN("Setup llog-test device over %s device\n",
1511 lustre_cfg_string(lcfg, 1));
1513 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1514 obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
1516 rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
1519 GOTO(cleanup_session, rc);
1521 /* use MGS llog dir for tests */
1522 ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
1525 llog_ctxt_put(ctxt);
1527 ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
1530 llog_ctxt_put(ctxt);
1532 llog_test_rand = cfs_rand();
1534 rc = llog_run_tests(&env, tgt);
1536 llog_test_cleanup(obd);
1538 lu_context_exit(&test_session);
1539 lu_context_fini(&test_session);
1545 static struct obd_ops llog_obd_ops = {
1546 .o_owner = THIS_MODULE,
1547 .o_setup = llog_test_setup,
1548 .o_cleanup = llog_test_cleanup,
1551 static int __init llog_test_init(void)
1553 return class_register_type(&llog_obd_ops, NULL, true, NULL,
1557 static void __exit llog_test_exit(void)
1559 class_unregister_type("llog_test");
1562 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1563 MODULE_DESCRIPTION("llog test module");
1564 MODULE_LICENSE("GPL");
1566 module_init(llog_test_init);
1567 module_exit(llog_test_exit);