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_size > 0) {
240 if (lgh->lgh_cur_offset != lgh->lgh_hdr->llh_hdr.lrh_len +
241 (start_idx + records - 1) *
242 lgh->lgh_hdr->llh_size)
243 CERROR("Wrong record offset in cur_off: "LPU64", should"
244 " be %u\n", lgh->lgh_cur_offset,
245 lgh->lgh_hdr->llh_hdr.lrh_len +
246 (start_idx + records - 1) *
247 lgh->lgh_hdr->llh_size);
249 size_t chunk_size = lgh->lgh_hdr->llh_hdr.lrh_len;
251 /* For variable size records the start offset is unknown, trust
252 * the first value and check others are consistent with it. */
254 rec_offset = lgh->lgh_cur_offset;
256 if (lgh->lgh_cur_offset != rec_offset) {
257 /* there can be padding record */
258 if ((lgh->lgh_cur_offset % chunk_size == 0) &&
259 (lgh->lgh_cur_offset - rec_offset <
260 rec->lrh_len + LLOG_MIN_REC_SIZE)) {
261 rec_offset = lgh->lgh_cur_offset;
264 CERROR("Wrong record offset in cur_off: "LPU64
265 ", should be %lld (rec len %u)\n",
267 (long long)rec_offset, rec->lrh_len);
270 rec_offset += rec->lrh_len;
273 if ((start_idx + records + paddings) != rec->lrh_index)
274 CERROR("Record with wrong index was read: %u, expected %u\n",
275 rec->lrh_index, start_idx + records + paddings);
277 /* modify all records in place */
278 lgr->lgr_gen.conn_cnt = rec->lrh_index;
279 rc = llog_write(env, lgh, rec, rec->lrh_index);
281 CERROR("cb_test_3: cannot modify record while processing\n");
283 /* Add new record to the llog at *last_rec position one by one to
284 * check that last block is re-read during processing */
285 if ((start_idx + records + paddings) == *last_rec ||
286 (start_idx + records + paddings) == (*last_rec + 1)) {
287 rc = llog_write(env, lgh, rec, LLOG_NEXT_IDX);
289 CERROR("cb_test_3: cannot add new record while "
297 /* Check in-place modifications were done for all records*/
298 static int test3_check_cb(const struct lu_env *env, struct llog_handle *lgh,
299 struct llog_rec_hdr *rec, void *data)
301 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
303 if (lgr->lgr_gen.conn_cnt != rec->lrh_index) {
304 CERROR("cb_test_3: record %u is not modified\n",
312 static int llog_test3_process(const struct lu_env *env,
313 struct llog_handle *lgh,
314 llog_cb_t cb, int start)
316 struct llog_process_cat_data cd;
317 int last_idx; /* new record will be injected here */
320 CWARN("test3: processing records from index %d to the end\n",
322 cd.lpcd_first_idx = start - 1;
323 cd.lpcd_last_idx = 0;
324 records = paddings = 0;
325 last_idx = lgh->lgh_last_idx;
326 rc = llog_process(env, lgh, cb, &last_idx, &cd);
329 CWARN("test3: total %u records processed with %u paddings\n",
334 /* Test plain llog functionality */
335 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
336 struct llog_handle *llh)
339 struct llog_rec_hdr *hdr = (void *)buf;
341 int num_recs = 1; /* 1 for the header */
346 hdr->lrh_len = sizeof(struct llog_gen_rec);
347 hdr->lrh_type = LLOG_GEN_REC;
348 llh->lgh_hdr->llh_size = sizeof(struct llog_gen_rec);
350 /* Fill the llog with 64-bytes records, use 1023 records,
351 * so last chunk will be partially full. Don't change this
352 * value until record size is changed.
354 CWARN("3a: write 1023 fixed-size llog records\n");
355 for (i = 0; i < 1023; i++) {
356 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
358 CERROR("3a: write 1023 records failed at #%d: %d\n",
365 rc = verify_handle("3a", llh, num_recs);
370 * Test fixed-size records processing:
371 * - search the needed index
372 * - go through all records from that index
373 * - check all indices are growing monotonically and exist
374 * - modify each record
376 * NB: test3_check_n_add adds two new records while processing
377 * after last record. There were 1023 records created so the last chunk
378 * misses exactly one record. Therefore one of new records will be
379 * the last in the current chunk and second causes the new chunk to be
385 rc = llog_test3_process(env, llh, test3_check_n_add_cb, start_idx);
389 /* extra record is created during llog_process() */
390 if (rc != expected) {
391 CERROR("3a: process total %d records but expect %d\n",
398 /* test modification in place */
399 rc = llog_test3_process(env, llh, test3_check_cb, start_idx);
403 if (rc != expected) {
404 CERROR("3a: process total %d records but expect %d\n",
409 CWARN("3b: write 566 variable size llog records\n");
411 /* Drop llh_size to 0 to mark llog as variable-size and write
412 * header to make this change permanent. */
413 llh->lgh_hdr->llh_size = 0;
414 llog_write(env, llh, &llh->lgh_hdr->llh_hdr, LLOG_HEADER_IDX);
416 hdr->lrh_type = OBD_CFG_REC;
418 /* there are 1025 64-bytes records in llog already,
419 * the last chunk contains single record, i.e. 64 bytes.
420 * Each pair of variable size records is 200 bytes, so
421 * we will have the following distribution per chunks:
422 * block 1: 64 + 80(80/120) + 80 + 48(pad) = 81 iterations
423 * block 2: 80(120/80) + 120 + 72(pad) = 81 itereations
424 * block 3: 80(80/120) + 80 + 112(pad) = 81 iterations
425 * -- the same as block 2 again and so on.
426 * block 7: 80(80/120) = 80 iterations and 192 bytes remain
427 * Total 6 * 81 + 80 = 566 itereations.
428 * Callback will add another 120 bytes in the end of the last chunk
429 * and another 120 bytes will cause padding (72 bytes) plus 120
430 * bytes in the new block.
432 for (i = 0; i < 566; i++) {
438 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
440 CERROR("3a: write 566 records failed at #%d: %d\n",
447 rc = verify_handle("3b", llh, num_recs);
453 rc = llog_test3_process(env, llh, test3_check_n_add_cb, start_idx);
457 if (rc != expected) {
458 CERROR("3b: process total %d records but expect %d\n",
465 /* test modification in place */
466 rc = llog_test3_process(env, llh, test3_check_cb, start_idx);
470 if (rc != expected) {
471 CERROR("3b: process total %d records but expect %d\n",
476 CWARN("3c: write records with variable size until BITMAP_SIZE, "
478 while (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
479 if ((num_recs % 2) == 0)
484 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
488 CERROR("3c: write recs failed at #%d: %d\n",
496 CWARN("3c: write record more than BITMAP size!\n");
499 CWARN("3c: wrote %d more records before end of llog is reached\n",
502 rc = verify_handle("3d", llh, num_recs);
507 /* Test catalogue additions */
508 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
510 struct llog_handle *cath;
512 int rc, rc2, i, buflen;
513 struct llog_mini_rec lmr;
514 struct llog_cookie cookie;
515 struct llog_ctxt *ctxt;
518 struct llog_rec_hdr *rec;
522 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
525 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
526 lmr.lmr_hdr.lrh_type = 0xf00f00;
528 sprintf(name, "%x", llog_test_rand + 1);
529 CWARN("4a: create a catalog log with name: %s\n", name);
530 rc = llog_open_create(env, ctxt, &cath, NULL, name);
532 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
533 GOTO(ctxt_release, rc);
535 rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
537 CERROR("4a: can't init llog handle: %d\n", rc);
542 cat_logid = cath->lgh_id;
544 CWARN("4b: write 1 record into the catalog\n");
545 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
547 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
551 rc = verify_handle("4b", cath, 2);
555 rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
559 CWARN("4c: cancel 1 log record\n");
560 rc = llog_cat_cancel_records(env, cath, 1, &cookie);
562 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
567 rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
571 CWARN("4d: write %d more log records\n", LLOG_TEST_RECNUM);
572 for (i = 0; i < LLOG_TEST_RECNUM; i++) {
573 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
575 CERROR("4d: write %d records failed at #%d: %d\n",
576 LLOG_TEST_RECNUM, i + 1, rc);
582 /* make sure new plain llog appears */
583 rc = verify_handle("4d", cath, 3);
587 CWARN("4e: add 5 large records, one record per block\n");
588 buflen = LLOG_MIN_CHUNK_SIZE;
589 OBD_ALLOC(buf, buflen);
591 GOTO(out, rc = -ENOMEM);
592 for (i = 0; i < 5; i++) {
594 rec->lrh_len = buflen;
595 rec->lrh_type = OBD_CFG_REC;
596 rc = llog_cat_add(env, cath, rec, NULL);
598 CERROR("4e: write 5 records failed at #%d: %d\n",
605 OBD_FREE(buf, buflen);
607 CWARN("4f: put newly-created catalog\n");
608 rc2 = llog_cat_close(env, cath);
610 CERROR("4: close log %s failed: %d\n", name, rc2);
619 static int cat_counter;
621 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
622 struct llog_rec_hdr *rec, void *data)
624 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
625 struct lu_fid fid = {0};
627 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
628 CERROR("invalid record in catalog\n");
632 logid_to_fid(&lir->lid_id, &fid);
634 CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
635 rec->lrh_index, PFID(&fid),
636 PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
643 static int plain_counter;
645 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
646 struct llog_rec_hdr *rec, void *data)
648 struct lu_fid fid = {0};
650 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
651 CERROR("log is not plain\n");
655 logid_to_fid(&llh->lgh_id, &fid);
657 CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
658 rec->lrh_index, PFID(&fid));
665 static int cancel_count;
667 static int llog_cancel_rec_cb(const struct lu_env *env,
668 struct llog_handle *llh,
669 struct llog_rec_hdr *rec, void *data)
671 struct llog_cookie cookie;
673 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
674 CERROR("log is not plain\n");
678 cookie.lgc_lgl = llh->lgh_id;
679 cookie.lgc_index = rec->lrh_index;
681 llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
683 if (cancel_count == LLOG_TEST_RECNUM)
684 RETURN(-LLOG_EEMPTY);
688 /* Test log and catalogue processing */
689 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
691 struct llog_handle *llh = NULL;
694 struct llog_mini_rec lmr;
695 struct llog_ctxt *ctxt;
699 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
702 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
703 lmr.lmr_hdr.lrh_type = 0xf00f00;
705 CWARN("5a: re-open catalog by id\n");
706 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
708 CERROR("5a: llog_create with logid failed: %d\n", rc);
712 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
714 CERROR("5a: can't init llog handle: %d\n", rc);
718 CWARN("5b: print the catalog entries.. we expect 2\n");
720 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
722 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
725 if (cat_counter != 2) {
726 CERROR("5b: %d entries in catalog\n", cat_counter);
727 GOTO(out, rc = -EINVAL);
730 CWARN("5c: Cancel %d records, see one log zapped\n", LLOG_TEST_RECNUM);
732 rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
733 if (rc != -LLOG_EEMPTY) {
734 CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
738 CWARN("5c: print the catalog entries.. we expect 1\n");
740 rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
742 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
745 if (cat_counter != 1) {
746 CERROR("5c: %d entries in catalog\n", cat_counter);
747 GOTO(out, rc = -EINVAL);
750 CWARN("5d: add 1 record to the log with many canceled empty pages\n");
751 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
753 CERROR("5d: add record to the log with many canceled empty "
758 CWARN("5e: print plain log entries.. expect 6\n");
760 rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
762 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
765 if (plain_counter != 6) {
766 CERROR("5e: found %d records\n", plain_counter);
767 GOTO(out, rc = -EINVAL);
770 CWARN("5f: print plain log entries reversely.. expect 6\n");
772 rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
774 CERROR("5f: reversely process with plain_print_cb failed: "
778 if (plain_counter != 6) {
779 CERROR("5f: found %d records\n", plain_counter);
780 GOTO(out, rc = -EINVAL);
784 CWARN("5g: close re-opened catalog\n");
785 rc2 = llog_cat_close(env, llh);
787 CERROR("5g: close log %s failed: %d\n", name, rc2);
797 /* Test client api; open log by name and process */
798 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
801 struct obd_device *mgc_obd;
802 struct llog_ctxt *ctxt;
803 struct obd_uuid *mgs_uuid;
804 struct obd_export *exp;
805 struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
806 struct llog_handle *llh = NULL;
807 struct llog_ctxt *nctxt;
810 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
812 mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
814 CWARN("6a: re-open log %s using client API\n", name);
815 mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
816 if (mgc_obd == NULL) {
817 CERROR("6a: no MGC devices connected to %s found.\n",
819 GOTO(ctxt_release, rc = -ENOENT);
822 rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
823 NULL /* obd_connect_data */, NULL);
824 if (rc != -EALREADY) {
825 CERROR("6a: connect on connected MGC (%s) failed to return"
826 " -EALREADY\n", mgc_obd->obd_name);
829 GOTO(ctxt_release, rc = -EINVAL);
832 nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
833 rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
835 CERROR("6a: llog_open failed %d\n", rc);
839 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
841 CERROR("6a: llog_init_handle failed %d\n", rc);
845 plain_counter = 1; /* llog header is first record */
846 CWARN("6b: process log %s using client API\n", name);
847 rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
849 CERROR("6b: llog_process failed %d\n", rc);
850 CWARN("6b: processed %d records\n", plain_counter);
852 rc = verify_handle("6b", llh, plain_counter);
856 plain_counter = 1; /* llog header is first record */
857 CWARN("6c: process log %s reversely using client API\n", name);
858 rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
860 CERROR("6c: llog_reverse_process failed %d\n", rc);
861 CWARN("6c: processed %d records\n", plain_counter);
863 rc = verify_handle("6c", llh, plain_counter);
868 rc2 = llog_close(env, llh);
870 CERROR("6: llog_close failed: rc = %d\n", rc2);
875 llog_ctxt_put(nctxt);
882 struct llog_rec_hdr lrh; /* common header */
883 struct llog_logid_rec llr; /* LLOG_LOGID_MAGIC */
884 struct llog_unlink64_rec lur; /* MDS_UNLINK64_REC */
885 struct llog_setattr64_rec lsr64; /* MDS_SETATTR64_REC */
886 struct llog_size_change_rec lscr; /* OST_SZ_REC */
887 struct llog_changelog_rec lcr; /* CHANGELOG_REC */
888 struct llog_changelog_user_rec lcur; /* CHANGELOG_USER_REC */
889 struct llog_gen_rec lgr; /* LLOG_GEN_REC */
892 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
893 struct llog_rec_hdr *rec, void *data)
895 struct lu_fid fid = {0};
897 logid_to_fid(&llh->lgh_id, &fid);
899 CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
900 rec->lrh_type, rec->lrh_index, PFID(&fid));
906 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
907 struct llog_rec_hdr *rec, void *data)
910 /* test LLOG_DEL_RECORD is working */
911 return LLOG_DEL_RECORD;
914 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
916 struct llog_handle *llh;
917 int rc = 0, i, process_count;
922 rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
924 CERROR("7_sub: create log failed\n");
928 rc = llog_init_handle(env, llh,
929 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
932 CERROR("7_sub: can't init llog handle: %d\n", rc);
935 for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
936 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
940 CERROR("7_sub: write recs failed at #%d: %d\n",
947 CWARN("7_sub: write record more than BITMAP size!\n");
948 GOTO(out_close, rc = -EINVAL);
951 rc = verify_handle("7_sub", llh, num_recs + 1);
953 CERROR("7_sub: verify handle failed: %d\n", rc);
956 if (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1)
957 CWARN("7_sub: records are not aligned, written %d from %u\n",
958 num_recs, LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
961 rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
963 CERROR("7_sub: llog process failed: %d\n", rc);
966 process_count = plain_counter;
967 if (process_count != num_recs) {
968 CERROR("7_sub: processed %d records from %d total\n",
969 process_count, num_recs);
970 GOTO(out_close, rc = -EINVAL);
974 rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
975 if (rc && rc != LLOG_DEL_PLAIN) {
976 CERROR("7_sub: reverse llog process failed: %d\n", rc);
979 if (process_count != plain_counter) {
980 CERROR("7_sub: Reverse/direct processing found different"
981 "number of records: %d/%d\n",
982 plain_counter, process_count);
983 GOTO(out_close, rc = -EINVAL);
985 if (llog_exist(llh)) {
986 CERROR("7_sub: llog exists but should be zapped\n");
987 GOTO(out_close, rc = -EEXIST);
990 rc = verify_handle("7_sub", llh, 1);
993 llog_destroy(env, llh);
994 llog_close(env, llh);
998 /* Test all llog records writing and processing */
999 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
1001 struct llog_ctxt *ctxt;
1006 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1008 CWARN("7a: test llog_logid_rec\n");
1009 llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1010 llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1011 llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1013 rc = llog_test_7_sub(env, ctxt);
1015 CERROR("7a: llog_logid_rec test failed\n");
1019 CWARN("7b: test llog_unlink64_rec\n");
1020 llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
1021 llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
1022 llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
1024 rc = llog_test_7_sub(env, ctxt);
1026 CERROR("7b: llog_unlink_rec test failed\n");
1030 CWARN("7c: test llog_setattr64_rec\n");
1031 llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
1032 llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
1033 llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1035 rc = llog_test_7_sub(env, ctxt);
1037 CERROR("7c: llog_setattr64_rec test failed\n");
1041 CWARN("7d: test llog_size_change_rec\n");
1042 llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1043 llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1044 llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
1046 rc = llog_test_7_sub(env, ctxt);
1048 CERROR("7d: llog_size_change_rec test failed\n");
1052 CWARN("7e: test llog_changelog_rec\n");
1053 /* Direct access to cr_do_not_use: peculiar case for this test */
1054 llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1055 llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1056 llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1058 rc = llog_test_7_sub(env, ctxt);
1060 CERROR("7e: llog_changelog_rec test failed\n");
1064 CWARN("7f: test llog_changelog_user_rec\n");
1065 llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1066 llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1067 llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1069 rc = llog_test_7_sub(env, ctxt);
1071 CERROR("7f: llog_changelog_user_rec test failed\n");
1075 CWARN("7g: test llog_gen_rec\n");
1076 llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
1077 llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
1078 llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
1080 rc = llog_test_7_sub(env, ctxt);
1082 CERROR("7g: llog_size_change_rec test failed\n");
1086 llog_ctxt_put(ctxt);
1090 static int llog_truncate(const struct lu_env *env, struct dt_object *o)
1094 struct dt_device *d;
1099 d = lu2dt_dev(o->do_lu.lo_dev);
1102 rc = dt_attr_get(env, o, &la);
1106 CDEBUG(D_OTHER, "original size "LPU64"\n", la.la_size);
1107 rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
1108 if (la.la_size < rc) {
1109 CERROR("too small llog: "LPU64"\n", la.la_size);
1113 /* drop 2 records */
1114 la.la_size = la.la_size - (sizeof(struct llog_mini_rec) * 2);
1115 la.la_valid = LA_SIZE;
1117 th = dt_trans_create(env, d);
1119 RETURN(PTR_ERR(th));
1121 rc = dt_declare_attr_set(env, o, &la, th);
1125 rc = dt_declare_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
1127 rc = dt_trans_start_local(env, d, th);
1131 rc = dt_punch(env, o, la.la_size, OBD_OBJECT_EOF, th);
1135 rc = dt_attr_set(env, o, &la, th);
1140 dt_trans_stop(env, d, th);
1145 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
1146 struct llog_rec_hdr *rec, void *data)
1152 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
1154 struct llog_handle *llh = NULL;
1158 struct llog_mini_rec lmr;
1159 struct llog_ctxt *ctxt;
1160 struct dt_object *obj = NULL;
1164 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1167 lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1168 lmr.lmr_hdr.lrh_type = 0xf00f00;
1170 CWARN("8a: fill the first plain llog\n");
1171 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1173 CERROR("8a: llog_create with logid failed: %d\n", rc);
1177 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1179 CERROR("8a: can't init llog handle: %d\n", rc);
1184 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1186 CERROR("5a: process with test_8_cb failed: %d\n", rc);
1189 orig_counter = plain_counter;
1191 for (i = 0; i < 100; i++) {
1192 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1194 CERROR("5a: add record failed\n");
1199 /* grab the current plain llog, we'll corrupt it later */
1200 obj = llh->u.chd.chd_current_log->lgh_obj;
1202 lu_object_get(&obj->do_lu);
1203 CWARN("8a: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1205 rc2 = llog_cat_close(env, llh);
1207 CERROR("8a: close log %s failed: %d\n", name, rc2);
1213 CWARN("8b: fill the second plain llog\n");
1214 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1216 CERROR("8b: llog_create with logid failed: %d\n", rc);
1220 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1222 CERROR("8b: can't init llog handle: %d\n", rc);
1226 for (i = 0; i < 100; i++) {
1227 rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
1229 CERROR("8b: add record failed\n");
1233 CWARN("8b: second llog "DFID"\n",
1234 PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1236 rc2 = llog_cat_close(env, llh);
1238 CERROR("8b: close log %s failed: %d\n", name, rc2);
1244 CWARN("8c: drop two records from the first plain llog\n");
1245 llog_truncate(env, obj);
1247 CWARN("8d: count survived records\n");
1248 rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1250 CERROR("8d: llog_create with logid failed: %d\n", rc);
1254 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1256 CERROR("8d: can't init llog handle: %d\n", rc);
1261 rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1263 CERROR("8d: process with test_8_cb failed: %d\n", rc);
1267 if (orig_counter + 200 - 2 != plain_counter) {
1268 CERROR("found %d records (expected %d)\n", plain_counter,
1269 orig_counter + 200 - 2);
1274 CWARN("8d: close re-opened catalog\n");
1275 rc2 = llog_cat_close(env, llh);
1277 CERROR("8d: close log %s failed: %d\n", name, rc2);
1282 llog_ctxt_put(ctxt);
1285 lu_object_put(env, &obj->do_lu);
1290 /* -------------------------------------------------------------------------
1291 * Tests above, boring obd functions below
1292 * ------------------------------------------------------------------------- */
1293 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
1295 struct llog_handle *llh = NULL;
1296 struct llog_ctxt *ctxt;
1301 ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1304 sprintf(name, "%x", llog_test_rand);
1306 rc = llog_test_1(env, obd, name);
1308 GOTO(cleanup_ctxt, rc);
1310 rc = llog_test_2(env, obd, name, &llh);
1312 GOTO(cleanup_ctxt, rc);
1314 rc = llog_test_3(env, obd, llh);
1318 rc = llog_test_4(env, obd);
1322 rc = llog_test_5(env, obd);
1326 rc = llog_test_6(env, obd, name);
1330 rc = llog_test_7(env, obd);
1334 rc = llog_test_8(env, obd);
1339 err = llog_destroy(env, llh);
1341 CERROR("cleanup: llog_destroy failed: %d\n", err);
1342 llog_close(env, llh);
1346 llog_ctxt_put(ctxt);
1350 static int llog_test_cleanup(struct obd_device *obd)
1352 struct obd_device *tgt;
1358 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
1362 tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
1363 rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
1365 CERROR("failed to llog_test_llog_finish: %d\n", rc);
1370 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1372 struct obd_device *tgt;
1373 struct llog_ctxt *ctxt;
1374 struct dt_object *o;
1376 struct lu_context test_session;
1381 if (lcfg->lcfg_bufcount < 2) {
1382 CERROR("requires a TARGET OBD name\n");
1386 if (lcfg->lcfg_buflens[1] < 1) {
1387 CERROR("requires a TARGET OBD name\n");
1392 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1393 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1394 CERROR("target device not attached or not set up (%s)\n",
1395 lustre_cfg_string(lcfg, 1));
1399 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
1403 rc = lu_context_init(&test_session, LCT_SERVER_SESSION);
1405 GOTO(cleanup_env, rc);
1406 test_session.lc_thread = (struct ptlrpc_thread *)current;
1407 lu_context_enter(&test_session);
1408 env.le_ses = &test_session;
1410 CWARN("Setup llog-test device over %s device\n",
1411 lustre_cfg_string(lcfg, 1));
1413 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1414 obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
1416 rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
1419 GOTO(cleanup_session, rc);
1421 /* use MGS llog dir for tests */
1422 ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
1425 llog_ctxt_put(ctxt);
1427 ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
1430 llog_ctxt_put(ctxt);
1432 llog_test_rand = cfs_rand();
1434 rc = llog_run_tests(&env, tgt);
1436 llog_test_cleanup(obd);
1438 lu_context_exit(&test_session);
1439 lu_context_fini(&test_session);
1445 static struct obd_ops llog_obd_ops = {
1446 .o_owner = THIS_MODULE,
1447 .o_setup = llog_test_setup,
1448 .o_cleanup = llog_test_cleanup,
1451 static int __init llog_test_init(void)
1453 return class_register_type(&llog_obd_ops, NULL, true, NULL,
1457 static void __exit llog_test_exit(void)
1459 class_unregister_type("llog_test");
1462 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1463 MODULE_DESCRIPTION("llog test module");
1464 MODULE_LICENSE("GPL");
1466 module_init(llog_test_init);
1467 module_exit(llog_test_exit);