Whamcloud - gitweb
LU-16763 kunit: consolidate kernel unit testing
[fs/lustre-release.git] / lustre / kunit / llog_test.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2012, 2017, Intel Corporation.
8  *
9  */
10
11 /*
12  * This file is part of Lustre, http://www.lustre.org/
13  *
14  * lustre/kunit/llog_test.c
15  *
16  * Author: Phil Schwan <phil@clusterfs.com>
17  * Author: Mikhail Pershin <mike.pershin@intel.com>
18  *
19  */
20
21 #define DEBUG_SUBSYSTEM S_CLASS
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/kthread.h>
26 #include <linux/delay.h>
27 #include <linux/random.h>
28
29 #include <obd_class.h>
30 #include <lustre_fid.h>
31 #include <lustre_log.h>
32
33 /* This is slightly more than the number of records that can fit into a
34  * single llog file, because the llog_log_header takes up some of the
35  * space in the first block that cannot be used for the bitmap. */
36 static int llog_test_recnum = (LLOG_MIN_CHUNK_SIZE * 8);
37 static int llog_test_rand;
38 static struct obd_uuid uuid = { .uuid = "test_uuid" };
39 static struct llog_logid cat_logid;
40
41 struct llog_mini_rec {
42         struct llog_rec_hdr lmr_hdr;
43         struct llog_rec_tail lmr_tail;
44 } __attribute__((packed));
45
46 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
47 {
48         int i;
49         int last_idx = 0;
50         int active_recs = 0;
51
52         for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
53                 if (test_bit_le(i, LLOG_HDR_BITMAP(llh->lgh_hdr))) {
54                         last_idx = i;
55                         active_recs++;
56                 }
57         }
58
59         /* check the llog is sane at first, llh_count and lgh_last_idx*/
60         if (llh->lgh_hdr->llh_count != active_recs) {
61                 CERROR("%s: handle->count is %d, but there are %d recs found\n",
62                        test, llh->lgh_hdr->llh_count, active_recs);
63                 RETURN(-ERANGE);
64         }
65
66         if (llh->lgh_last_idx != LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index ||
67             (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_CAT) &&
68              llh->lgh_last_idx < last_idx)) {
69                 CERROR("%s: lgh_last_idx is %d (%d in the header), last found %d\n",
70                        test, llh->lgh_last_idx,
71                        LLOG_HDR_TAIL(llh->lgh_hdr)->lrt_index, last_idx);
72                 RETURN(-ERANGE);
73         }
74
75         /* finally checks against expected value from the caller */
76         if (active_recs != num_recs) {
77                 CERROR("%s: expected %d active recs after write, found %d\n",
78                        test, num_recs, active_recs);
79                 RETURN(-ERANGE);
80         }
81
82         RETURN(0);
83 }
84
85 /* Test named-log create/open, close */
86 static int llog_test_1(const struct lu_env *env,
87                        struct obd_device *obd, char *name)
88 {
89         struct llog_handle *llh;
90         struct llog_ctxt *ctxt;
91         int rc;
92         int rc2;
93
94         ENTRY;
95
96         CWARN("1a: create a log with name: %s\n", name);
97         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
98         LASSERT(ctxt);
99
100         rc = llog_open_create(env, ctxt, &llh, NULL, name);
101         if (rc) {
102                 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
103                 GOTO(out, rc);
104         }
105         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, &uuid);
106         if (rc) {
107                 CERROR("1a: can't init llog handle: %d\n", rc);
108                 GOTO(out_close, rc);
109         }
110
111         rc = verify_handle("1", llh, 1);
112
113         CWARN("1b: close newly-created log\n");
114 out_close:
115         rc2 = llog_close(env, llh);
116         if (rc2) {
117                 CERROR("1b: close log %s failed: %d\n", name, rc2);
118                 if (rc == 0)
119                         rc = rc2;
120         }
121 out:
122         llog_ctxt_put(ctxt);
123         RETURN(rc);
124 }
125
126 static int test_2_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
127                             struct llog_rec_hdr *rec, void *data)
128 {
129         return LLOG_DEL_RECORD;
130 }
131
132 /* Test named-log reopen; returns opened log on success */
133 static int llog_test_2(const struct lu_env *env, struct obd_device *obd,
134                        char *name, struct llog_handle **llh)
135 {
136         struct llog_ctxt *ctxt;
137         struct llog_handle *lgh;
138         struct llog_logid  logid;
139         int rc;
140         struct llog_mini_rec lmr;
141
142         ENTRY;
143
144         CWARN("2a: re-open a log with name: %s\n", name);
145         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
146         LASSERT(ctxt);
147
148         rc = llog_open(env, ctxt, llh, NULL, name, LLOG_OPEN_EXISTS);
149         if (rc) {
150                 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
151                 GOTO(out_put, rc);
152         }
153
154         rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &uuid);
155         if (rc) {
156                 CERROR("2a: can't init llog handle: %d\n", rc);
157                 GOTO(out_close_llh, rc);
158         }
159
160         rc = verify_handle("2", *llh, 1);
161         if (rc)
162                 GOTO(out_close_llh, rc);
163
164         CWARN("2b: create a log without specified NAME & LOGID\n");
165         rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
166         if (rc) {
167                 CERROR("2b: create log failed\n");
168                 GOTO(out_close_llh, rc);
169         }
170         rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
171         if (rc) {
172                 CERROR("2b: can't init llog handle: %d\n", rc);
173                 GOTO(out_close, rc);
174         }
175
176         logid = lgh->lgh_id;
177
178         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
179         lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
180
181         /* Check llog header values are correct after record add/cancel */
182         CWARN("2b: write 1 llog records, check llh_count\n");
183         rc = llog_write(env, lgh, &lmr.lmr_hdr, LLOG_NEXT_IDX);
184         if (rc < 0)
185                 GOTO(out_close, rc);
186
187         /* in-memory values after record addition */
188         rc = verify_handle("2b", lgh, 2);
189         if (rc < 0)
190                 GOTO(out_close, rc);
191
192         /* re-open llog to read on-disk values */
193         llog_close(env, lgh);
194
195         CWARN("2c: re-open the log by LOGID and verify llh_count\n");
196         rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
197         if (rc < 0) {
198                 CERROR("2c: re-open log by LOGID failed\n");
199                 GOTO(out_close_llh, rc);
200         }
201
202         rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
203         if (rc < 0) {
204                 CERROR("2c: can't init llog handle: %d\n", rc);
205                 GOTO(out_close, rc);
206         }
207
208         /* check values just read from disk */
209         rc = verify_handle("2c", lgh, 2);
210         if (rc < 0)
211                 GOTO(out_close, rc);
212
213         rc = llog_process(env, lgh, test_2_cancel_cb, NULL, NULL);
214         if (rc < 0)
215                 GOTO(out_close, rc);
216
217         /* in-memory values */
218         rc = verify_handle("2c", lgh, 1);
219         if (rc < 0)
220                 GOTO(out_close, rc);
221
222         /* re-open llog to get on-disk values */
223         llog_close(env, lgh);
224
225         rc = llog_open(env, ctxt, &lgh, &logid, NULL, LLOG_OPEN_EXISTS);
226         if (rc) {
227                 CERROR("2c: re-open log by LOGID failed\n");
228                 GOTO(out_close_llh, rc);
229         }
230
231         rc = llog_init_handle(env, lgh, LLOG_F_IS_PLAIN, &uuid);
232         if (rc) {
233                 CERROR("2c: can't init llog handle: %d\n", rc);
234                 GOTO(out_close, rc);
235         }
236
237         /* on-disk values after llog re-open */
238         rc = verify_handle("2c", lgh, 1);
239         if (rc < 0)
240                 GOTO(out_close, rc);
241
242         CWARN("2d: destroy this log\n");
243         rc = llog_destroy(env, lgh);
244         if (rc)
245                 CERROR("2d: destroy log failed\n");
246 out_close:
247         llog_close(env, lgh);
248 out_close_llh:
249         if (rc)
250                 llog_close(env, *llh);
251 out_put:
252         llog_ctxt_put(ctxt);
253
254         RETURN(rc);
255 }
256
257 static int test_3_rec_num;
258 static off_t test_3_rec_off;
259 static int test_3_paddings;
260 static int test_3_start_idx;
261
262 /*
263  * Test 3 callback.
264  * - check lgh_cur_offset correctness
265  * - check record index consistency
266  * - modify each record in-place
267  * - add new record during *last_idx processing
268  */
269 static int test3_check_n_add_cb(const struct lu_env *env,
270                                 struct llog_handle *lgh,
271                                 struct llog_rec_hdr *rec, void *data)
272 {
273         struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
274         int *last_rec = data;
275         unsigned cur_idx = test_3_start_idx + test_3_rec_num;
276         int rc;
277
278         if (lgh->lgh_hdr->llh_flags & LLOG_F_IS_FIXSIZE) {
279                 LASSERT(lgh->lgh_hdr->llh_size > 0);
280                 if (lgh->lgh_cur_offset != lgh->lgh_hdr->llh_hdr.lrh_len +
281                                         (cur_idx - 1) * lgh->lgh_hdr->llh_size)
282                         CERROR("Wrong record offset in cur_off: %llu, should be %u\n",
283                                lgh->lgh_cur_offset,
284                                lgh->lgh_hdr->llh_hdr.lrh_len +
285                                (cur_idx - 1) * lgh->lgh_hdr->llh_size);
286         } else {
287                 size_t chunk_size = lgh->lgh_hdr->llh_hdr.lrh_len;
288
289                 /*
290                  * For variable size records the start offset is unknown, trust
291                  * the first value and check others are consistent with it.
292                  */
293                 if (test_3_rec_off == 0)
294                         test_3_rec_off = lgh->lgh_cur_offset;
295
296                 if (lgh->lgh_cur_offset != test_3_rec_off) {
297                         __u64 tmp = lgh->lgh_cur_offset;
298
299                         /* there can be padding record */
300                         if ((do_div(tmp, chunk_size) == 0) &&
301                             (lgh->lgh_cur_offset - test_3_rec_off <
302                              rec->lrh_len + LLOG_MIN_REC_SIZE)) {
303                                 test_3_rec_off = lgh->lgh_cur_offset;
304                                 test_3_paddings++;
305                         } else {
306                                 CERROR("Wrong record offset in cur_off: %llu"
307                                        ", should be %lld (rec len %u)\n",
308                                        lgh->lgh_cur_offset,
309                                        (long long)test_3_rec_off,
310                                        rec->lrh_len);
311                         }
312                 }
313                 test_3_rec_off += rec->lrh_len;
314         }
315
316         cur_idx += test_3_paddings;
317         if (cur_idx != rec->lrh_index)
318                 CERROR("Record with wrong index was read: %u, expected %u\n",
319                        rec->lrh_index, cur_idx);
320
321         /* modify all records in place */
322         lgr->lgr_gen.conn_cnt = rec->lrh_index;
323         rc = llog_write(env, lgh, rec, rec->lrh_index);
324         if (rc < 0)
325                 CERROR("cb_test_3: cannot modify record while processing\n");
326
327         /*
328          * Add new record to the llog at *last_rec position one by one to
329          * check that last block is re-read during processing
330          */
331         if (cur_idx == *last_rec || cur_idx == (*last_rec + 1)) {
332                 rc = llog_write(env, lgh, rec, LLOG_NEXT_IDX);
333                 if (rc < 0)
334                         CERROR("cb_test_3: cannot add new record while "
335                                "processing\n");
336         }
337         test_3_rec_num++;
338
339         return rc;
340 }
341
342 /* Check in-place modifications were done for all records*/
343 static int test3_check_cb(const struct lu_env *env, struct llog_handle *lgh,
344                           struct llog_rec_hdr *rec, void *data)
345 {
346         struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
347
348         if (lgr->lgr_gen.conn_cnt != rec->lrh_index) {
349                 CERROR("cb_test_3: record %u is not modified\n",
350                        rec->lrh_index);
351                 return -EINVAL;
352         }
353         test_3_rec_num++;
354         return 0;
355 }
356
357 static int llog_test3_process(const struct lu_env *env,
358                               struct llog_handle *lgh,
359                               llog_cb_t cb, int start)
360 {
361         struct llog_process_cat_data cd;
362         int last_idx; /* new record will be injected here */
363         int rc = 0;
364
365         CWARN("test3: processing records from index %d to the end\n",
366               start);
367         cd.lpcd_read_mode = LLOG_READ_MODE_NORMAL;
368         cd.lpcd_first_idx = start - 1;
369         cd.lpcd_last_idx = 0;
370         test_3_rec_num = test_3_paddings = 0;
371         last_idx = lgh->lgh_last_idx;
372         rc = llog_process(env, lgh, cb, &last_idx, &cd);
373         if (rc < 0)
374                 return rc;
375         CWARN("test3: total %u records processed with %u paddings\n",
376               test_3_rec_num, test_3_paddings);
377         return test_3_rec_num;
378 }
379
380 /* Test plain llog functionality */
381 static int llog_test_3(const struct lu_env *env, struct obd_device *obd,
382                        struct llog_handle *llh)
383 {
384         char buf[128];
385         struct llog_rec_hdr *hdr = (void *)buf;
386         int rc, i;
387         int num_recs = 1; /* 1 for the header */
388         int expected;
389
390         ENTRY;
391
392         hdr->lrh_len = sizeof(struct llog_gen_rec);
393         hdr->lrh_type = LLOG_GEN_REC;
394         llh->lgh_hdr->llh_size = sizeof(struct llog_gen_rec);
395         llh->lgh_hdr->llh_flags |= LLOG_F_IS_FIXSIZE;
396
397         /*
398          * Fill the llog with 64-bytes records, use 1023 records,
399          * so last chunk will be partially full. Don't change this
400          * value until record size is changed.
401          */
402         CWARN("3a: write 1023 fixed-size llog records\n");
403         for (i = 0; i < 1023; i++) {
404                 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
405                 if (rc < 0) {
406                         CERROR("3a: write 1023 records failed at #%d: %d\n",
407                                i + 1, rc);
408                         RETURN(rc);
409                 }
410                 num_recs++;
411         }
412
413         rc = verify_handle("3a", llh, num_recs);
414         if (rc)
415                 RETURN(rc);
416
417         /*
418          * Test fixed-size records processing:
419          * - search the needed index
420          * - go through all records from that index
421          * - check all indices are growing monotonically and exist
422          * - modify each record
423          *
424          * NB: test3_check_n_add adds two new records while processing
425          * after last record. There were 1023 records created so the last chunk
426          * misses exactly one record. Therefore one of new records will be
427          * the last in the current chunk and second causes the new chunk to be
428          * created.
429          */
430         test_3_rec_off = 0;
431         test_3_start_idx = 501;
432         expected = 525;
433         rc = llog_test3_process(env, llh, test3_check_n_add_cb,
434                                 test_3_start_idx);
435         if (rc < 0)
436                 RETURN(rc);
437
438         /* extra record is created during llog_process() */
439         if (rc != expected) {
440                 CERROR("3a: process total %d records but expect %d\n",
441                        rc, expected);
442                 RETURN(-ERANGE);
443         }
444
445         num_recs += 2;
446
447         /* test modification in place */
448         rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
449         if (rc < 0)
450                 RETURN(rc);
451
452         if (rc != expected) {
453                 CERROR("3a: process total %d records but expect %d\n",
454                        rc, expected);
455                 RETURN(-ERANGE);
456         }
457
458         CWARN("3b: write 566 variable size llog records\n");
459
460         /*
461          * Drop llh_size to 0 to mark llog as variable-size and write
462          * header to make this change permanent.
463          */
464         llh->lgh_hdr->llh_flags &= ~LLOG_F_IS_FIXSIZE;
465         llog_write(env, llh, &llh->lgh_hdr->llh_hdr, LLOG_HEADER_IDX);
466
467         hdr->lrh_type = OBD_CFG_REC;
468
469         /*
470          * there are 1025 64-bytes records in llog already,
471          * the last chunk contains single record, i.e. 64 bytes.
472          * Each pair of variable size records is 200 bytes, so
473          * we will have the following distribution per chunks:
474          * block 1: 64 + 80(80/120) + 80 + 48(pad) = 81 iterations
475          * block 2: 80(120/80) + 120 + 72(pad) = 81 itereations
476          * block 3: 80(80/120) + 80 + 112(pad) = 81 iterations
477          * -- the same as block 2 again and so on.
478          * block 7: 80(80/120) = 80 iterations and 192 bytes remain
479          * Total 6 * 81 + 80 = 566 itereations.
480          * Callback will add another 120 bytes in the end of the last chunk
481          * and another 120 bytes will cause padding (72 bytes) plus 120
482          * bytes in the new block.
483          */
484         for (i = 0; i < 566; i++) {
485                 if ((i % 2) == 0)
486                         hdr->lrh_len = 80;
487                 else
488                         hdr->lrh_len = 120;
489
490                 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
491                 if (rc < 0) {
492                         CERROR("3b: write 566 records failed at #%d: %d\n",
493                                i + 1, rc);
494                         RETURN(rc);
495                 }
496                 num_recs++;
497         }
498
499         rc = verify_handle("3b", llh, num_recs);
500         if (rc)
501                 RETURN(rc);
502
503         test_3_start_idx = 1026;
504         expected = 568;
505         rc = llog_test3_process(env, llh, test3_check_n_add_cb,
506                                 test_3_start_idx);
507         if (rc < 0)
508                 RETURN(rc);
509
510         if (rc != expected) {
511                 CERROR("3b: process total %d records but expect %d\n",
512                        rc, expected);
513                 RETURN(-ERANGE);
514         }
515
516         num_recs += 2;
517
518         /* test modification in place */
519         rc = llog_test3_process(env, llh, test3_check_cb, test_3_start_idx);
520         if (rc < 0)
521                 RETURN(rc);
522
523         if (rc != expected) {
524                 CERROR("3b: process total %d records but expect %d\n",
525                        rc, expected);
526                 RETURN(-ERANGE);
527         }
528
529         CWARN("3c: write records with variable size until BITMAP_SIZE, "
530               "return -ENOSPC\n");
531         while (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
532                 if ((num_recs % 2) == 0)
533                         hdr->lrh_len = 80;
534                 else
535                         hdr->lrh_len = 128;
536
537                 rc = llog_write(env, llh, hdr, LLOG_NEXT_IDX);
538                 if (rc == -ENOSPC) {
539                         break;
540                 } else if (rc < 0) {
541                         CERROR("3c: write recs failed at #%d: %d\n",
542                                num_recs, rc);
543                         RETURN(rc);
544                 }
545                 num_recs++;
546         }
547
548         if (rc != -ENOSPC) {
549                 CWARN("3c: write record more than BITMAP size!\n");
550                 RETURN(-EINVAL);
551         }
552         CWARN("3c: wrote %d more records before end of llog is reached\n",
553               num_recs);
554
555         rc = verify_handle("3c", llh, num_recs);
556
557         RETURN(rc);
558 }
559
560 /* Test catalogue additions */
561 static int llog_test_4(const struct lu_env *env, struct obd_device *obd)
562 {
563         struct llog_handle *cath, *llh;
564         char name[10];
565         int rc, rc2, i, buflen;
566         struct llog_mini_rec lmr;
567         struct llog_cookie cookie;
568         struct llog_ctxt *ctxt;
569         int num_recs = 0;
570         char *buf;
571         struct llog_rec_hdr *rec;
572
573         ENTRY;
574
575         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
576         LASSERT(ctxt);
577
578         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
579         lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
580
581         sprintf(name, "%x", llog_test_rand + 1);
582         CWARN("4a: create a catalog log with name: %s\n", name);
583         rc = llog_open_create(env, ctxt, &cath, NULL, name);
584         if (rc) {
585                 CERROR("4a: llog_create with name %s failed: %d\n", name, rc);
586                 GOTO(ctxt_release, rc);
587         }
588         rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
589         if (rc) {
590                 CERROR("4a: can't init llog handle: %d\n", rc);
591                 GOTO(out, rc);
592         }
593
594         num_recs++;
595         cat_logid = cath->lgh_id;
596
597         CWARN("4b: write 1 record into the catalog\n");
598         rc = llog_cat_add(env, cath, &lmr.lmr_hdr, &cookie);
599         if (rc != 1) {
600                 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
601                 GOTO(out, rc);
602         }
603         num_recs++;
604         rc = verify_handle("4b", cath, 2);
605         if (rc)
606                 GOTO(out, rc);
607
608         rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs);
609         if (rc)
610                 GOTO(out, rc);
611
612         /* estimate the max number of record for the plain llog
613          * cause it depends on disk size
614          */
615         llh = cath->u.chd.chd_current_log;
616         if (llh->lgh_max_size != 0) {
617                 llog_test_recnum = (llh->lgh_max_size -
618                         sizeof(struct llog_log_hdr)) / LLOG_MIN_REC_SIZE;
619         }
620
621         if (llog_test_recnum >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr))
622                 llog_test_recnum = LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1;
623
624         CWARN("4c: cancel 1 log record\n");
625         rc = llog_cat_cancel_records(env, cath, 1, &cookie);
626         if (rc) {
627                 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
628                 GOTO(out, rc);
629         }
630         num_recs--;
631
632         rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs);
633         if (rc)
634                 GOTO(out, rc);
635
636         CWARN("4d: write %d more log records\n", llog_test_recnum);
637         for (i = 0; i < llog_test_recnum; i++) {
638                 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
639                 if (rc) {
640                         CERROR("4d: write %d records failed at #%d: %d\n",
641                                llog_test_recnum, i + 1, rc);
642                         GOTO(out, rc);
643                 }
644                 num_recs++;
645         }
646
647         /* make sure new plain llog appears */
648         rc = verify_handle("4d", cath, 3);
649         if (rc)
650                 GOTO(out, rc);
651
652         CWARN("4e: add 5 large records, one record per block\n");
653         buflen = LLOG_MIN_CHUNK_SIZE;
654         OBD_ALLOC(buf, buflen);
655         if (buf == NULL)
656                 GOTO(out, rc = -ENOMEM);
657         for (i = 0; i < 5; i++) {
658                 rec = (void *)buf;
659                 rec->lrh_len = buflen;
660                 rec->lrh_type = OBD_CFG_REC;
661                 rc = llog_cat_add(env, cath, rec, NULL);
662                 if (rc) {
663                         CERROR("4e: write 5 records failed at #%d: %d\n",
664                                i + 1, rc);
665                         GOTO(out_free, rc);
666                 }
667                 num_recs++;
668         }
669 out_free:
670         OBD_FREE(buf, buflen);
671 out:
672         CWARN("4f: put newly-created catalog\n");
673         rc2 = llog_cat_close(env, cath);
674         if (rc2) {
675                 CERROR("4: close log %s failed: %d\n", name, rc2);
676                 if (rc == 0)
677                         rc = rc2;
678         }
679 ctxt_release:
680         llog_ctxt_put(ctxt);
681         RETURN(rc);
682 }
683
684 static int cat_counter;
685
686 static int cat_print_cb(const struct lu_env *env, struct llog_handle *llh,
687                         struct llog_rec_hdr *rec, void *data)
688 {
689         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
690         struct lu_fid fid = {0};
691
692         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
693                 CERROR("invalid record in catalog\n");
694                 RETURN(-EINVAL);
695         }
696
697         logid_to_fid(&lir->lid_id, &fid);
698
699         CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
700               rec->lrh_index, PFID(&fid),
701               PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
702
703         cat_counter++;
704
705         RETURN(0);
706 }
707
708 static int plain_counter;
709
710 static int plain_print_cb(const struct lu_env *env, struct llog_handle *llh,
711                           struct llog_rec_hdr *rec, void *data)
712 {
713         struct lu_fid fid = {0};
714
715         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
716                 CERROR("log is not plain\n");
717                 RETURN(-EINVAL);
718         }
719
720         logid_to_fid(&llh->lgh_id, &fid);
721
722         CDEBUG(D_INFO, "seeing record at index %d in log "DFID"\n",
723                rec->lrh_index, PFID(&fid));
724
725         plain_counter++;
726
727         RETURN(0);
728 }
729
730 static int cancel_count;
731
732 static int llog_cancel_rec_cb(const struct lu_env *env,
733                               struct llog_handle *llh,
734                               struct llog_rec_hdr *rec, void *data)
735 {
736         struct llog_cookie cookie;
737
738         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
739                 CERROR("log is not plain\n");
740                 RETURN(-EINVAL);
741         }
742
743         cookie.lgc_lgl = llh->lgh_id;
744         cookie.lgc_index = rec->lrh_index;
745
746         llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1, &cookie);
747         cancel_count++;
748         if (cancel_count == llog_test_recnum)
749                 RETURN(-LLOG_EEMPTY);
750         RETURN(0);
751 }
752
753 /* Test log and catalogue processing */
754 static int llog_test_5(const struct lu_env *env, struct obd_device *obd)
755 {
756         struct llog_handle *llh = NULL;
757         char name[10];
758         int rc, rc2;
759         struct llog_mini_rec lmr;
760         struct llog_ctxt *ctxt;
761
762         ENTRY;
763
764         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
765         LASSERT(ctxt);
766
767         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
768         lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
769
770         CWARN("5a: re-open catalog by id\n");
771         rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
772         if (rc) {
773                 CERROR("5a: llog_create with logid failed: %d\n", rc);
774                 GOTO(out_put, rc);
775         }
776
777         rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
778         if (rc) {
779                 CERROR("5a: can't init llog handle: %d\n", rc);
780                 GOTO(out, rc);
781         }
782
783         CWARN("5b: print the catalog entries.. we expect 2\n");
784         cat_counter = 0;
785         rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
786         if (rc) {
787                 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
788                 GOTO(out, rc);
789         }
790         if (cat_counter != 2) {
791                 CERROR("5b: %d entries in catalog\n", cat_counter);
792                 GOTO(out, rc = -EINVAL);
793         }
794
795         CWARN("5c: Cancel %d records, see one log zapped\n", llog_test_recnum);
796         cancel_count = 0;
797         rc = llog_cat_process(env, llh, llog_cancel_rec_cb, "foobar", 0, 0);
798         if (rc != -LLOG_EEMPTY) {
799                 CERROR("5c: process with llog_cancel_rec_cb failed: %d\n", rc);
800                 GOTO(out, rc);
801         }
802
803         CWARN("5c: print the catalog entries.. we expect 1\n");
804         cat_counter = 0;
805         rc = llog_process(env, llh, cat_print_cb, "test 5", NULL);
806         if (rc) {
807                 CERROR("5c: process with cat_print_cb failed: %d\n", rc);
808                 GOTO(out, rc);
809         }
810         if (cat_counter != 1) {
811                 CERROR("5c: %d entries in catalog\n", cat_counter);
812                 GOTO(out, rc = -EINVAL);
813         }
814
815         CWARN("5d: add 1 record to the log with many canceled empty pages\n");
816         rc = llog_cat_add(env, llh, &lmr.lmr_hdr, NULL);
817         if (rc) {
818                 CERROR("5d: add record to the log with many canceled empty "
819                        "pages failed\n");
820                 GOTO(out, rc);
821         }
822
823         CWARN("5e: print plain log entries.. expect 6\n");
824         plain_counter = 0;
825         rc = llog_cat_process(env, llh, plain_print_cb, "foobar", 0, 0);
826         if (rc) {
827                 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
828                 GOTO(out, rc);
829         }
830         if (plain_counter != 6) {
831                 CERROR("5e: found %d records\n", plain_counter);
832                 GOTO(out, rc = -EINVAL);
833         }
834
835         CWARN("5f: print plain log entries reversely.. expect 6\n");
836         plain_counter = 0;
837         rc = llog_cat_reverse_process(env, llh, plain_print_cb, "foobar");
838         if (rc) {
839                 CERROR("5f: reversely process with plain_print_cb failed: "
840                        "%d\n", rc);
841                 GOTO(out, rc);
842         }
843         if (plain_counter != 6) {
844                 CERROR("5f: found %d records\n", plain_counter);
845                 GOTO(out, rc = -EINVAL);
846         }
847
848 out:
849         CWARN("5g: close re-opened catalog\n");
850         rc2 = llog_cat_close(env, llh);
851         if (rc2) {
852                 CERROR("5g: close log %s failed: %d\n", name, rc2);
853                 if (rc == 0)
854                         rc = rc2;
855         }
856 out_put:
857         llog_ctxt_put(ctxt);
858
859         RETURN(rc);
860 }
861
862 /* Test client api; open log by name and process */
863 static int llog_test_6(const struct lu_env *env, struct obd_device *obd,
864                        char *name)
865 {
866         struct obd_device *mgc_obd;
867         struct llog_ctxt *ctxt;
868         struct obd_uuid *mgs_uuid;
869         struct obd_export *exp;
870         struct obd_uuid uuid = { "LLOG_TEST6_UUID" };
871         struct llog_handle *llh = NULL;
872         struct llog_ctxt *nctxt;
873         int rc, rc2;
874
875         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
876         LASSERT(ctxt);
877         mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
878
879         CWARN("6a: re-open log %s using client API\n", name);
880         mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
881         if (mgc_obd == NULL) {
882                 CERROR("6a: no MGC devices connected to %s found.\n",
883                        mgs_uuid->uuid);
884                 GOTO(ctxt_release, rc = -ENOENT);
885         }
886
887         rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
888                          NULL /* obd_connect_data */, NULL);
889         if (rc != -EALREADY) {
890                 CERROR("6a: connect on connected MGC (%s) failed to return"
891                        " -EALREADY\n", mgc_obd->obd_name);
892                 if (rc == 0)
893                         obd_disconnect(exp);
894                 GOTO(ctxt_release, rc = -EINVAL);
895         }
896
897         nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
898         rc = llog_open(env, nctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
899         if (rc) {
900                 CERROR("6a: llog_open failed %d\n", rc);
901                 GOTO(nctxt_put, rc);
902         }
903
904         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
905         if (rc) {
906                 CERROR("6a: llog_init_handle failed %d\n", rc);
907                 GOTO(parse_out, rc);
908         }
909
910         plain_counter = 1; /* llog header is first record */
911         CWARN("6b: process log %s using client API\n", name);
912         rc = llog_process(env, llh, plain_print_cb, NULL, NULL);
913         if (rc)
914                 CERROR("6b: llog_process failed %d\n", rc);
915         CWARN("6b: processed %d records\n", plain_counter);
916
917         rc = verify_handle("6b", llh, plain_counter);
918         if (rc)
919                 GOTO(parse_out, rc);
920
921         plain_counter = 1; /* llog header is first record */
922         CWARN("6c: process log %s reversely using client API\n", name);
923         rc = llog_reverse_process(env, llh, plain_print_cb, NULL, NULL);
924         if (rc)
925                 CERROR("6c: llog_reverse_process failed %d\n", rc);
926         CWARN("6c: processed %d records\n", plain_counter);
927
928         rc = verify_handle("6c", llh, plain_counter);
929         if (rc)
930                 GOTO(parse_out, rc);
931
932 parse_out:
933         rc2 = llog_close(env, llh);
934         if (rc2) {
935                 CERROR("6: llog_close failed: rc = %d\n", rc2);
936                 if (rc == 0)
937                         rc = rc2;
938         }
939 nctxt_put:
940         llog_ctxt_put(nctxt);
941 ctxt_release:
942         llog_ctxt_put(ctxt);
943         RETURN(rc);
944 }
945
946 static union {
947         struct llog_rec_hdr             lrh;   /* common header */
948         struct llog_logid_rec           llr;   /* LLOG_LOGID_MAGIC */
949         struct llog_unlink64_rec        lur;   /* MDS_UNLINK64_REC */
950         struct llog_setattr64_rec       lsr64; /* MDS_SETATTR64_REC */
951         struct llog_setattr64_rec_v2    lsr64_v2; /* MDS_SETATTR64_REC */
952         struct llog_size_change_rec     lscr;  /* OST_SZ_REC */
953         struct llog_changelog_rec       lcr;   /* CHANGELOG_REC */
954         struct llog_changelog_user_rec2 lcur;  /* CHANGELOG_USER_REC2 */
955         struct llog_gen_rec             lgr;   /* LLOG_GEN_REC */
956 } llog_records;
957
958 static int test_7_print_cb(const struct lu_env *env, struct llog_handle *llh,
959                            struct llog_rec_hdr *rec, void *data)
960 {
961         struct lu_fid fid = {0};
962
963         logid_to_fid(&llh->lgh_id, &fid);
964
965         CDEBUG(D_OTHER, "record type %#x at index %d in log "DFID"\n",
966                rec->lrh_type, rec->lrh_index, PFID(&fid));
967
968         plain_counter++;
969         return 0;
970 }
971
972 static int test_7_cancel_cb(const struct lu_env *env, struct llog_handle *llh,
973                             struct llog_rec_hdr *rec, void *data)
974 {
975         plain_counter++;
976         /* test LLOG_DEL_RECORD is working */
977         return LLOG_DEL_RECORD;
978 }
979
980 static int llog_test_7_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
981 {
982         struct llog_handle *llh;
983         int rc = 0, i, process_count;
984         int num_recs = 0;
985
986         ENTRY;
987
988         rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
989         if (rc) {
990                 CERROR("7_sub: create log failed\n");
991                 RETURN(rc);
992         }
993
994         rc = llog_init_handle(env, llh,
995                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
996                               &uuid);
997         if (rc) {
998                 CERROR("7_sub: can't init llog handle: %d\n", rc);
999                 GOTO(out_close, rc);
1000         }
1001         for (i = 0; i < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr); i++) {
1002                 rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1003                 if (rc == -ENOSPC) {
1004                         break;
1005                 } else if (rc < 0) {
1006                         CERROR("7_sub: write recs failed at #%d: %d\n",
1007                                i + 1, rc);
1008                         GOTO(out_close, rc);
1009                 }
1010                 num_recs++;
1011         }
1012         if (rc != -ENOSPC) {
1013                 CWARN("7_sub: write record more than BITMAP size!\n");
1014                 GOTO(out_close, rc = -EINVAL);
1015         }
1016
1017         rc = verify_handle("7_sub", llh, num_recs + 1);
1018         if (rc) {
1019                 CERROR("7_sub: verify handle failed: %d\n", rc);
1020                 GOTO(out_close, rc);
1021         }
1022         if (num_recs < LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1)
1023                 CWARN("7_sub: records are not aligned, written %d from %u\n",
1024                       num_recs, LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
1025
1026         plain_counter = 0;
1027         rc = llog_process(env, llh, test_7_print_cb, "test 7", NULL);
1028         if (rc) {
1029                 CERROR("7_sub: llog process failed: %d\n", rc);
1030                 GOTO(out_close, rc);
1031         }
1032         process_count = plain_counter;
1033         if (process_count != num_recs) {
1034                 CERROR("7_sub: processed %d records from %d total\n",
1035                        process_count, num_recs);
1036                 GOTO(out_close, rc = -EINVAL);
1037         }
1038
1039         plain_counter = 0;
1040         rc = llog_reverse_process(env, llh, test_7_cancel_cb, "test 7", NULL);
1041         if (rc && rc != LLOG_DEL_PLAIN) {
1042                 CERROR("7_sub: reverse llog process failed: %d\n", rc);
1043                 GOTO(out_close, rc);
1044         }
1045         if (process_count != plain_counter) {
1046                 CERROR("7_sub: Reverse/direct processing found different number of records: %d/%d\n",
1047                        plain_counter, process_count);
1048                 GOTO(out_close, rc = -EINVAL);
1049         }
1050         if (llog_exist(llh)) {
1051                 CERROR("7_sub: llog exists but should be zapped\n");
1052                 GOTO(out_close, rc = -EEXIST);
1053         }
1054
1055         rc = verify_handle("7_sub", llh, 1);
1056 out_close:
1057         if (rc)
1058                 llog_destroy(env, llh);
1059         llog_close(env, llh);
1060         RETURN(rc);
1061 }
1062
1063 /* Test all llog records writing and processing */
1064 static int llog_test_7(const struct lu_env *env, struct obd_device *obd)
1065 {
1066         struct llog_ctxt *ctxt;
1067         int rc;
1068
1069         ENTRY;
1070
1071         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1072
1073         CWARN("7a: test llog_logid_rec\n");
1074         llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1075         llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1076         llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1077
1078         rc = llog_test_7_sub(env, ctxt);
1079         if (rc) {
1080                 CERROR("7a: llog_logid_rec test failed\n");
1081                 GOTO(out, rc);
1082         }
1083
1084         CWARN("7b: test llog_unlink64_rec\n");
1085         llog_records.lur.lur_hdr.lrh_len = sizeof(llog_records.lur);
1086         llog_records.lur.lur_tail.lrt_len = sizeof(llog_records.lur);
1087         llog_records.lur.lur_hdr.lrh_type = MDS_UNLINK64_REC;
1088
1089         rc = llog_test_7_sub(env, ctxt);
1090         if (rc) {
1091                 CERROR("7b: llog_unlink_rec test failed\n");
1092                 GOTO(out, rc);
1093         }
1094
1095         CWARN("7c: test llog_setattr64_rec\n");
1096         llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64);
1097         llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64);
1098         llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1099
1100         rc = llog_test_7_sub(env, ctxt);
1101         if (rc) {
1102                 CERROR("7c: llog_setattr64_rec test failed\n");
1103                 GOTO(out, rc);
1104         }
1105
1106         CWARN("7d: test llog_size_change_rec\n");
1107         llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1108         llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1109         llog_records.lscr.lsc_hdr.lrh_type = OST_SZ_REC;
1110
1111         rc = llog_test_7_sub(env, ctxt);
1112         if (rc) {
1113                 CERROR("7d: llog_size_change_rec test failed\n");
1114                 GOTO(out, rc);
1115         }
1116
1117         CWARN("7e: test llog_changelog_rec\n");
1118         /* Direct access to cr_do_not_use: peculiar case for this test */
1119         llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1120         llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1121         llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1122
1123         rc = llog_test_7_sub(env, ctxt);
1124         if (rc) {
1125                 CERROR("7e: llog_changelog_rec test failed\n");
1126                 GOTO(out, rc);
1127         }
1128
1129         CWARN("7f: test llog_changelog_user_rec2\n");
1130         llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1131         llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1132         llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC2;
1133
1134         rc = llog_test_7_sub(env, ctxt);
1135         if (rc) {
1136                 CERROR("7f: llog_changelog_user_rec test failed\n");
1137                 GOTO(out, rc);
1138         }
1139
1140         CWARN("7g: test llog_gen_rec\n");
1141         llog_records.lgr.lgr_hdr.lrh_len = sizeof(llog_records.lgr);
1142         llog_records.lgr.lgr_tail.lrt_len = sizeof(llog_records.lgr);
1143         llog_records.lgr.lgr_hdr.lrh_type = LLOG_GEN_REC;
1144
1145         rc = llog_test_7_sub(env, ctxt);
1146         if (rc) {
1147                 CERROR("7g: llog_size_change_rec test failed\n");
1148                 GOTO(out, rc);
1149         }
1150
1151         CWARN("7h: test llog_setattr64_rec_v2\n");
1152         llog_records.lsr64.lsr_hdr.lrh_len = sizeof(llog_records.lsr64_v2);
1153         llog_records.lsr64.lsr_tail.lrt_len = sizeof(llog_records.lsr64_v2);
1154         llog_records.lsr64.lsr_hdr.lrh_type = MDS_SETATTR64_REC;
1155
1156         rc = llog_test_7_sub(env, ctxt);
1157         if (rc) {
1158                 CERROR("7h: llog_setattr64_rec_v2 test failed\n");
1159                 GOTO(out, rc);
1160         }
1161 out:
1162         llog_ctxt_put(ctxt);
1163         RETURN(rc);
1164 }
1165
1166 static int test_8_cb(const struct lu_env *env, struct llog_handle *llh,
1167                           struct llog_rec_hdr *rec, void *data)
1168 {
1169         plain_counter++;
1170         return 0;
1171 }
1172
1173 static int llog_zeroes(const struct lu_env *env, struct dt_object *o,
1174                       __u64 start, __u64 end)
1175 {
1176         struct lu_attr la;
1177         struct thandle *th;
1178         struct dt_device *d;
1179         char *buf;
1180         loff_t pos = start;
1181         struct lu_buf lb;
1182         int rc;
1183
1184         ENTRY;
1185
1186         OBD_ALLOC(buf, end - start);
1187         if (!buf)
1188                 RETURN(-ENOMEM);
1189
1190         LASSERT(o);
1191         d = lu2dt_dev(o->do_lu.lo_dev);
1192         LASSERT(d);
1193
1194         rc = dt_attr_get(env, o, &la);
1195         if (rc)
1196                 GOTO(free, rc);
1197
1198         CDEBUG(D_OTHER, "llog size %llu\n", la.la_size);
1199         rc = sizeof(struct llog_log_hdr) + sizeof(struct llog_mini_rec);
1200         if (la.la_size < end) {
1201                 CERROR("llog size %llu is small for punch at [%llu;%llu]\n",
1202                        la.la_size, start, end);
1203                 GOTO(free, rc = 0);
1204         }
1205
1206         th = dt_trans_create(env, d);
1207         if (IS_ERR(th))
1208                 GOTO(free, rc = PTR_ERR(th));
1209
1210         lb.lb_buf = buf;
1211         lb.lb_len = end - start;
1212         rc = dt_declare_write(env, o, &lb, pos, th);
1213         if (rc)
1214                 GOTO(stop, rc);
1215
1216         rc = dt_trans_start_local(env, d, th);
1217         if (rc)
1218                 GOTO(stop, rc);
1219
1220         rc = dt_write(env, o, &lb, &pos, th);
1221         if (rc)
1222                 GOTO(stop, rc);
1223 stop:
1224         dt_trans_stop(env, d, th);
1225 free:
1226         OBD_FREE(buf, end - start);
1227
1228         RETURN(rc);
1229 }
1230
1231 struct llog_test8_rec {
1232         struct llog_rec_hdr ltr_hdr;
1233         __u64 padding[29];
1234         struct llog_rec_tail ltr_tail;
1235 } __attribute__((packed));
1236
1237 static int llog_test_8(const struct lu_env *env, struct obd_device *obd)
1238 {
1239         struct llog_handle *llh = NULL;
1240         char name[10];
1241         int rc, rc2, i;
1242         int orig_counter;
1243         struct llog_test8_rec ltr;
1244         struct llog_ctxt *ctxt;
1245         struct dt_object *obj = NULL;
1246         int plain_pos;
1247         int reclen = sizeof(ltr);
1248
1249         ENTRY;
1250
1251         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1252         LASSERT(ctxt);
1253
1254         /* simulate generic llog records with 256-bytes size */
1255         ltr.ltr_hdr.lrh_len = ltr.ltr_tail.lrt_len = reclen;
1256         ltr.ltr_hdr.lrh_type = LLOG_OP_MAGIC;
1257
1258         CWARN("8a: fill the first plain llog\n");
1259         rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1260         if (rc) {
1261                 CERROR("8a: llog_create with logid failed: %d\n", rc);
1262                 GOTO(out_put, rc);
1263         }
1264
1265         rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1266         if (rc) {
1267                 CERROR("8a: can't init llog handle: %d\n", rc);
1268                 GOTO(out, rc);
1269         }
1270
1271         plain_counter = 0;
1272         rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1273         if (rc != 0) {
1274                 CERROR("8a: process with test_8_cb failed: %d\n", rc);
1275                 GOTO(out, rc);
1276         }
1277         orig_counter = plain_counter;
1278
1279         for (i = 0; i < 20; i++) {
1280                 rc = llog_cat_add(env, llh, &ltr.ltr_hdr, NULL);
1281                 if (rc) {
1282                         CERROR("5a: add record failed\n");
1283                         GOTO(out, rc);
1284                 }
1285         }
1286         CWARN("8b: first llog "DFID"\n",
1287               PFID(lu_object_fid(&llh->u.chd.chd_current_log->lgh_obj->do_lu)));
1288
1289         /* get llog index in catalog to clear it later */
1290         plain_pos = (llh->lgh_last_idx - 1) * sizeof(struct llog_logid_rec);
1291         /* destroy plain llog to don't leave it orphaned */
1292         list_del_init(&llh->u.chd.chd_current_log->u.phd.phd_entry);
1293         llog_destroy(env, llh->u.chd.chd_current_log);
1294         llog_close(env, llh->u.chd.chd_current_log);
1295         llh->u.chd.chd_current_log = NULL;
1296
1297         rc2 = llog_cat_close(env, llh);
1298         if (rc2) {
1299                 CERROR("8a: close log %s failed: %d\n", name, rc2);
1300                 if (rc == 0)
1301                         rc = rc2;
1302                 GOTO(out_put, rc);
1303         }
1304
1305         CWARN("8b: fill the second plain llog\n");
1306         rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1307         if (rc) {
1308                 CERROR("8b: llog_create with logid failed: %d\n", rc);
1309                 GOTO(out_put, rc);
1310         }
1311
1312         rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1313         if (rc) {
1314                 CERROR("8b: can't init llog handle: %d\n", rc);
1315                 GOTO(out, rc);
1316         }
1317
1318         for (i = 0; i < 100; i++) {
1319                 rc = llog_cat_add(env, llh, &ltr.ltr_hdr, NULL);
1320                 if (rc) {
1321                         CERROR("8b: add record failed\n");
1322                         GOTO(out, rc);
1323                 }
1324         }
1325         /* grab the current plain llog, we'll corrupt it later */
1326         obj = llh->u.chd.chd_current_log->lgh_obj;
1327         LASSERT(obj);
1328         lu_object_get(&obj->do_lu);
1329         CWARN("8b: pin llog "DFID"\n", PFID(lu_object_fid(&obj->do_lu)));
1330
1331         /* must lost all 20 records */
1332         CWARN("8b: clean first llog record in catalog\n");
1333         llog_zeroes(env, llh->lgh_obj, 8192 + plain_pos,
1334                     8192 + plain_pos + sizeof(struct llog_logid_rec));
1335
1336         rc2 = llog_cat_close(env, llh);
1337         if (rc2) {
1338                 CERROR("8b: close log %s failed: %d\n", name, rc2);
1339                 if (rc == 0)
1340                         rc = rc2;
1341                 GOTO(out_put, rc);
1342         }
1343
1344         /* lost 28 records, from 5 to 32 in block */
1345         CWARN("8c: corrupt first chunk in the middle\n");
1346         llog_zeroes(env, obj, 8192 + reclen * 4, 8192 + reclen * 10);
1347         /* lost whole chunk - 32 records */
1348         CWARN("8c: corrupt second chunk at start\n");
1349         llog_zeroes(env, obj, 16384, 16384 + reclen);
1350
1351         CWARN("8d: count survived records\n");
1352         rc = llog_open(env, ctxt, &llh, &cat_logid, NULL, LLOG_OPEN_EXISTS);
1353         if (rc) {
1354                 CERROR("8d: llog_create with logid failed: %d\n", rc);
1355                 GOTO(out_put, rc);
1356         }
1357
1358         rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, &uuid);
1359         if (rc) {
1360                 CERROR("8d: can't init llog handle: %d\n", rc);
1361                 GOTO(out, rc);
1362         }
1363
1364         plain_counter = 0;
1365         rc = llog_cat_process(env, llh, test_8_cb, "foobar", 0, 0);
1366         if (rc != 0) {
1367                 CERROR("8d: process with test_8_cb failed: %d\n", rc);
1368                 GOTO(out, rc);
1369         }
1370
1371         /* if llog processing skips bad data as expected then 80
1372          * records from 120 should be lost
1373          */
1374         if (orig_counter + 120 - 80 != plain_counter) {
1375                 CERROR("found %d records (expected %d)\n", plain_counter,
1376                        orig_counter + 120 - 80);
1377                 rc = -EIO;
1378         }
1379
1380 out:
1381         CWARN("8d: close re-opened catalog\n");
1382         rc2 = llog_cat_close(env, llh);
1383         if (rc2) {
1384                 CERROR("8d: close log %s failed: %d\n", name, rc2);
1385                 if (rc == 0)
1386                         rc = rc2;
1387         }
1388 out_put:
1389         llog_ctxt_put(ctxt);
1390
1391         if (obj != NULL)
1392                 dt_object_put(env, obj);
1393
1394         RETURN(rc);
1395 }
1396
1397 static int llog_test_9_sub(const struct lu_env *env, struct llog_ctxt *ctxt)
1398 {
1399         struct llog_handle *llh;
1400         struct lu_fid fid;
1401         int rc = 0;
1402
1403         ENTRY;
1404
1405         rc = llog_open_create(env, ctxt, &llh, NULL, NULL);
1406         if (rc != 0) {
1407                 CERROR("9_sub: create log failed\n");
1408                 RETURN(rc);
1409         }
1410
1411         rc = llog_init_handle(env, llh,
1412                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
1413                               &uuid);
1414         if (rc != 0) {
1415                 CERROR("9_sub: can't init llog handle: %d\n", rc);
1416                 GOTO(out_close, rc);
1417         }
1418
1419         logid_to_fid(&llh->lgh_id, &fid);
1420         fid_to_logid(&fid, &llog_records.llr.lid_id);
1421         rc = llog_write(env, llh, &llog_records.lrh, LLOG_NEXT_IDX);
1422         if (rc < 0) {
1423                 CERROR("9_sub: write recs failed at #1: %d\n", rc);
1424                 GOTO(out_close, rc);
1425         }
1426         /* The below message is checked in sanity.sh test_60a (run-llog.sh) */
1427         CWARN("9_sub: record type %x in log "DFID_NOBRACE"\n",
1428               llog_records.lrh.lrh_type, PFID(&fid));
1429 out_close:
1430         llog_close(env, llh);
1431         RETURN(rc);
1432 }
1433
1434 /* Prepare different types of llog records for llog_reader test*/
1435 static int llog_test_9(const struct lu_env *env, struct obd_device *obd)
1436 {
1437         struct llog_ctxt *ctxt;
1438         int rc;
1439
1440         ENTRY;
1441
1442         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1443
1444         CWARN("9a: test llog_logid_rec\n");
1445         llog_records.llr.lid_hdr.lrh_len = sizeof(llog_records.llr);
1446         llog_records.llr.lid_tail.lrt_len = sizeof(llog_records.llr);
1447         llog_records.llr.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
1448
1449         rc = llog_test_9_sub(env, ctxt);
1450         if (rc != 0) {
1451                 CERROR("9a: llog_logid_rec test failed\n");
1452                 GOTO(out, rc);
1453         }
1454
1455         CWARN("9b: test llog_obd_cfg_rec\n");
1456         llog_records.lscr.lsc_hdr.lrh_len = sizeof(llog_records.lscr);
1457         llog_records.lscr.lsc_tail.lrt_len = sizeof(llog_records.lscr);
1458         llog_records.lscr.lsc_hdr.lrh_type = OBD_CFG_REC;
1459
1460         rc = llog_test_9_sub(env, ctxt);
1461         if (rc != 0) {
1462                 CERROR("9b: llog_obd_cfg_rec test failed\n");
1463                 GOTO(out, rc);
1464         }
1465
1466         CWARN("9c: test llog_changelog_rec\n");
1467         /* Direct access to cr_do_not_use: peculiar case for this test */
1468         llog_records.lcr.cr_hdr.lrh_len = sizeof(llog_records.lcr);
1469         llog_records.lcr.cr_do_not_use.lrt_len = sizeof(llog_records.lcr);
1470         llog_records.lcr.cr_hdr.lrh_type = CHANGELOG_REC;
1471
1472         rc = llog_test_9_sub(env, ctxt);
1473         if (rc != 0) {
1474                 CERROR("9c: llog_changelog_rec test failed\n");
1475                 GOTO(out, rc);
1476         }
1477
1478         CWARN("9d: test llog_changelog_user_rec2\n");
1479         llog_records.lcur.cur_hdr.lrh_len = sizeof(llog_records.lcur);
1480         llog_records.lcur.cur_tail.lrt_len = sizeof(llog_records.lcur);
1481         llog_records.lcur.cur_hdr.lrh_type = CHANGELOG_USER_REC;
1482
1483         rc = llog_test_9_sub(env, ctxt);
1484         if (rc != 0) {
1485                 CERROR("9d: llog_changelog_user_rec test failed\n");
1486                 GOTO(out, rc);
1487         }
1488
1489 out:
1490         llog_ctxt_put(ctxt);
1491         RETURN(rc);
1492 }
1493
1494 struct llog_process_info {
1495         struct llog_handle *lpi_loghandle;
1496         llog_cb_t lpi_cb;
1497         void *lpi_cbdata;
1498         void *lpi_catdata;
1499         int lpi_rc;
1500         struct completion lpi_completion;
1501         const struct lu_env *lpi_env;
1502         struct task_struct *lpi_reftask;
1503 };
1504
1505
1506 static int llog_test_process_thread(void *arg)
1507 {
1508         struct llog_process_info *lpi = arg;
1509         int rc;
1510
1511         rc = llog_cat_process_or_fork(NULL, lpi->lpi_loghandle, lpi->lpi_cb,
1512                                       NULL, lpi->lpi_cbdata, 1, 0, true);
1513
1514         complete(&lpi->lpi_completion);
1515         msleep(MSEC_PER_SEC / 2);
1516         lpi->lpi_rc = rc;
1517         if (rc)
1518                 CWARN("10h: Error during catalog processing %d\n", rc);
1519         return rc;
1520 }
1521
1522 static int cat_check_old_cb(const struct lu_env *env, struct llog_handle *llh,
1523                         struct llog_rec_hdr *rec, void *data)
1524 {
1525         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
1526         struct lu_fid fid = {0};
1527         struct lu_fid *prev_fid = data;
1528
1529         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
1530                 CERROR("invalid record in catalog\n");
1531                 RETURN(-EINVAL);
1532         }
1533
1534         logid_to_fid(&lir->lid_id, &fid);
1535
1536         CWARN("seeing record at index %d - "DFID" in log "DFID"\n",
1537               rec->lrh_index, PFID(&fid),
1538               PFID(lu_object_fid(&llh->lgh_obj->do_lu)));
1539
1540         if (prev_fid->f_oid > fid.f_oid) {
1541                 CWARN("processing old record, fail\n");
1542                 prev_fid->f_oid = 0xbad;
1543                 RETURN(-LLOG_EEMPTY);
1544         }
1545
1546         if (prev_fid->f_oid == 0) {
1547                 cfs_fail_loc = CFS_FAIL_ONCE | OBD_FAIL_LLOG_PROCESS_TIMEOUT;
1548                 cfs_fail_val = (unsigned int) (llh->lgh_id.lgl_oi.oi.oi_id &
1549                                                0xFFFFFFFF);
1550                 msleep(1 * MSEC_PER_SEC);
1551         }
1552         *prev_fid = fid;
1553
1554         RETURN(0);
1555 }
1556
1557 /* test catalog wrap around */
1558 static int llog_test_10(const struct lu_env *env, struct obd_device *obd)
1559 {
1560         struct llog_handle *cath;
1561         char name[10];
1562         int rc, rc2, i, enospc, eok;
1563         struct llog_mini_rec lmr;
1564         struct llog_ctxt *ctxt;
1565         struct lu_attr la;
1566         __u64 cat_max_size;
1567         struct dt_device *dt;
1568
1569         ENTRY;
1570
1571         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
1572         LASSERT(ctxt);
1573
1574         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
1575         lmr.lmr_hdr.lrh_type = LLOG_OP_MAGIC;
1576
1577         snprintf(name, sizeof(name), "%x", llog_test_rand + 2);
1578         CWARN("10a: create a catalog log with name: %s\n", name);
1579         rc = llog_open_create(env, ctxt, &cath, NULL, name);
1580         if (rc) {
1581                 CERROR("10a: llog_create with name %s failed: %d\n", name, rc);
1582                 GOTO(ctxt_release, rc);
1583         }
1584         rc = llog_init_handle(env, cath, LLOG_F_IS_CAT, &uuid);
1585         if (rc) {
1586                 CERROR("10a: can't init llog handle: %d\n", rc);
1587                 GOTO(out, rc);
1588         }
1589
1590         cat_logid = cath->lgh_id;
1591         dt = lu2dt_dev(cath->lgh_obj->do_lu.lo_dev);
1592
1593         /*
1594          * sync device to commit all recent LLOG changes to disk and avoid
1595          * to consume a huge space with delayed journal commit callbacks
1596          * particularly on low memory nodes or VMs
1597          */
1598         rc = dt_sync(env, dt);
1599         if (rc) {
1600                 CERROR("10c: sync failed: %d\n", rc);
1601                 GOTO(out, rc);
1602         }
1603
1604         /* force catalog wrap for 5th plain LLOG */
1605         cfs_fail_loc = CFS_FAIL_SKIP|OBD_FAIL_CAT_RECORDS;
1606         cfs_fail_val = 4;
1607
1608         CWARN("10b: write %d log records\n", llog_test_recnum);
1609         for (i = 0; i < llog_test_recnum; i++) {
1610                 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1611                 if (rc) {
1612                         CERROR("10b: write %d records failed at #%d: %d\n",
1613                                llog_test_recnum, i + 1, rc);
1614                         GOTO(out, rc);
1615                 }
1616         }
1617
1618         /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1619         rc = verify_handle("10b", cath, 3);
1620         if (rc)
1621                 GOTO(out, rc);
1622
1623         /*
1624          * sync device to commit all recent LLOG changes to disk and avoid
1625          * to consume a huge space with delayed journal commit callbacks
1626          * particularly on low memory nodes or VMs
1627          */
1628         rc = dt_sync(env, dt);
1629         if (rc) {
1630                 CERROR("10b: sync failed: %d\n", rc);
1631                 GOTO(out, rc);
1632         }
1633
1634         CWARN("10c: write %d more log records\n", 2 * llog_test_recnum);
1635         for (i = 0; i < 2 * llog_test_recnum; i++) {
1636                 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1637                 if (rc) {
1638                         CERROR("10c: write %d records failed at #%d: %d\n",
1639                                2*llog_test_recnum, i + 1, rc);
1640                         GOTO(out, rc);
1641                 }
1642         }
1643
1644         /* make sure 2 new plain llog appears in catalog (+1 with hdr) */
1645         rc = verify_handle("10c", cath, 5);
1646         if (rc)
1647                 GOTO(out, rc);
1648
1649         /*
1650          * sync device to commit all recent LLOG changes to disk and avoid
1651          * to consume a huge space with delayed journal commit callbacks
1652          * particularly on low memory nodes or VMs
1653          */
1654         rc = dt_sync(env, dt);
1655         if (rc) {
1656                 CERROR("10c: sync failed: %d\n", rc);
1657                 GOTO(out, rc);
1658         }
1659
1660         /*
1661          * fill last allocated plain LLOG and reach -ENOSPC condition
1662          * because no slot available in Catalog
1663          */
1664         enospc = 0;
1665         eok = 0;
1666         CWARN("10c: write %d more log records\n", llog_test_recnum);
1667         for (i = 0; i < llog_test_recnum; i++) {
1668                 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1669                 if (rc && rc != -ENOSPC) {
1670                         CERROR("10c: write %d records failed at #%d: %d\n",
1671                                llog_test_recnum, i + 1, rc);
1672                         GOTO(out, rc);
1673                 }
1674                 /*
1675                  * after last added plain LLOG has filled up, all new
1676                  * records add should fail with -ENOSPC
1677                  */
1678                 if (rc == -ENOSPC) {
1679                         enospc++;
1680                 } else {
1681                         enospc = 0;
1682                         eok++;
1683                 }
1684         }
1685
1686         if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1687                 CERROR("10c: all last records adds should have failed with"
1688                        " -ENOSPC\n");
1689                 GOTO(out, rc = -EINVAL);
1690         }
1691
1692         CWARN("10c: wrote %d records then %d failed with ENOSPC\n", eok,
1693               enospc);
1694
1695         /* make sure no new record in Catalog */
1696         rc = verify_handle("10c", cath, 5);
1697         if (rc)
1698                 GOTO(out, rc);
1699
1700         /* Catalog should have reached its max size for test */
1701         rc = dt_attr_get(env, cath->lgh_obj, &la);
1702         if (rc) {
1703                 CERROR("10c: failed to get catalog attrs: %d\n", rc);
1704                 GOTO(out, rc);
1705         }
1706         cat_max_size = la.la_size;
1707
1708         /*
1709          * cancel all 1st plain llog records to empty it, this will also cause
1710          * its catalog entry to be freed for next forced wrap in 10e
1711          */
1712         CWARN("10d: Cancel %d records, see one log zapped\n", llog_test_recnum);
1713         cancel_count = 0;
1714         rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1715         if (rc != -LLOG_EEMPTY) {
1716                 CERROR("10d: process with llog_cancel_rec_cb failed: %d\n", rc);
1717                 /*
1718                  * need to indicate error if for any reason llog_test_recnum is
1719                  * not reached
1720                  */
1721                 if (rc == 0)
1722                         rc = -ERANGE;
1723                 GOTO(out, rc);
1724         }
1725
1726         CWARN("10d: print the catalog entries.. we expect 3\n");
1727         cat_counter = 0;
1728         rc = llog_process(env, cath, cat_print_cb, "test 10", NULL);
1729         if (rc) {
1730                 CERROR("10d: process with cat_print_cb failed: %d\n", rc);
1731                 GOTO(out, rc);
1732         }
1733         if (cat_counter != 3) {
1734                 CERROR("10d: %d entries in catalog\n", cat_counter);
1735                 GOTO(out, rc = -EINVAL);
1736         }
1737
1738         /* verify one down in catalog (+1 with hdr) */
1739         rc = verify_handle("10d", cath, 4);
1740         if (rc)
1741                 GOTO(out, rc);
1742
1743         /*
1744          * sync device to commit all recent LLOG changes to disk and avoid
1745          * to consume a huge space with delayed journal commit callbacks
1746          * particularly on low memory nodes or VMs
1747          */
1748         rc = dt_sync(env, dt);
1749         if (rc) {
1750                 CERROR("10d: sync failed: %d\n", rc);
1751                 GOTO(out, rc);
1752         }
1753
1754         enospc = 0;
1755         eok = 0;
1756         CWARN("10e: write %d more log records\n", llog_test_recnum);
1757         for (i = 0; i < llog_test_recnum; i++) {
1758                 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1759                 if (rc && rc != -ENOSPC) {
1760                         CERROR("10e: write %d records failed at #%d: %d\n",
1761                                llog_test_recnum, i + 1, rc);
1762                         GOTO(out, rc);
1763                 }
1764                 /*
1765                  * after last added plain LLOG has filled up, all new
1766                  * records add should fail with -ENOSPC
1767                  */
1768                 if (rc == -ENOSPC) {
1769                         enospc++;
1770                 } else {
1771                         enospc = 0;
1772                         eok++;
1773                 }
1774         }
1775
1776         if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1777                 CERROR("10e: all last records adds should have failed with"
1778                        " -ENOSPC\n");
1779                 GOTO(out, rc = -EINVAL);
1780         }
1781
1782         CWARN("10e: wrote %d records then %d failed with ENOSPC\n", eok,
1783               enospc);
1784
1785         CWARN("10e: print the catalog entries.. we expect 4\n");
1786         cat_counter = 0;
1787         rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1788                                       0, 0, false);
1789         if (rc) {
1790                 CERROR("10e: process with cat_print_cb failed: %d\n", rc);
1791                 GOTO(out, rc);
1792         }
1793         if (cat_counter != 4) {
1794                 CERROR("10e: %d entries in catalog\n", cat_counter);
1795                 GOTO(out, rc = -EINVAL);
1796         }
1797
1798         /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1799         rc = verify_handle("10e", cath, 5);
1800         if (rc)
1801                 GOTO(out, rc);
1802
1803         /* verify catalog has wrap around */
1804         if (cath->lgh_last_idx > cath->lgh_hdr->llh_cat_idx) {
1805                 CERROR("10e: catalog failed to wrap around\n");
1806                 GOTO(out, rc = -EINVAL);
1807         }
1808
1809         rc = dt_attr_get(env, cath->lgh_obj, &la);
1810         if (rc) {
1811                 CERROR("10e: failed to get catalog attrs: %d\n", rc);
1812                 GOTO(out, rc);
1813         }
1814
1815         if (la.la_size != cat_max_size) {
1816                 CERROR("10e: catalog size has changed after it has wrap around,"
1817                        " current size = %llu, expected size = %llu\n",
1818                        la.la_size, cat_max_size);
1819                 GOTO(out, rc = -EINVAL);
1820         }
1821         CWARN("10e: catalog successfully wrap around, last_idx %d, first %d\n",
1822               cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1823
1824         /*
1825          * sync device to commit all recent LLOG changes to disk and avoid
1826          * to consume a huge space with delayed journal commit callbacks
1827          * particularly on low memory nodes or VMs
1828          */
1829         rc = dt_sync(env, dt);
1830         if (rc) {
1831                 CERROR("10e: sync failed: %d\n", rc);
1832                 GOTO(out, rc);
1833         }
1834
1835         /*
1836          * cancel more records to free one more slot in Catalog
1837          * see if it is re-allocated when adding more records
1838          */
1839         CWARN("10f: Cancel %d records, see one log zapped\n", llog_test_recnum);
1840         cancel_count = 0;
1841         rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1842         if (rc != -LLOG_EEMPTY) {
1843                 CERROR("10f: process with llog_cancel_rec_cb failed: %d\n", rc);
1844                 /*
1845                  * need to indicate error if for any reason llog_test_recnum is
1846                  * not reached
1847                  */
1848                 if (rc == 0)
1849                         rc = -ERANGE;
1850                 GOTO(out, rc);
1851         }
1852
1853         CWARN("10f: print the catalog entries.. we expect 3\n");
1854         cat_counter = 0;
1855         rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1856                                       0, 0, false);
1857         if (rc) {
1858                 CERROR("10f: process with cat_print_cb failed: %d\n", rc);
1859                 GOTO(out, rc);
1860         }
1861         if (cat_counter != 3) {
1862                 CERROR("10f: %d entries in catalog\n", cat_counter);
1863                 GOTO(out, rc = -EINVAL);
1864         }
1865
1866         /* verify one down in catalog (+1 with hdr) */
1867         rc = verify_handle("10f", cath, 4);
1868         if (rc)
1869                 GOTO(out, rc);
1870
1871         /*
1872          * sync device to commit all recent LLOG changes to disk and avoid
1873          * to consume a huge space with delayed journal commit callbacks
1874          * particularly on low memory nodes or VMs
1875          */
1876         rc = dt_sync(env, dt);
1877         if (rc) {
1878                 CERROR("10f: sync failed: %d\n", rc);
1879                 GOTO(out, rc);
1880         }
1881
1882         enospc = 0;
1883         eok = 0;
1884         CWARN("10f: write %d more log records\n", llog_test_recnum);
1885         for (i = 0; i < llog_test_recnum; i++) {
1886                 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
1887                 if (rc && rc != -ENOSPC) {
1888                         CERROR("10f: write %d records failed at #%d: %d\n",
1889                                llog_test_recnum, i + 1, rc);
1890                         GOTO(out, rc);
1891                 }
1892                 /*
1893                  * after last added plain LLOG has filled up, all new
1894                  * records add should fail with -ENOSPC
1895                  */
1896                 if (rc == -ENOSPC) {
1897                         enospc++;
1898                 } else {
1899                         enospc = 0;
1900                         eok++;
1901                 }
1902         }
1903
1904         if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
1905                 CERROR("10f: all last records adds should have failed with"
1906                        " -ENOSPC\n");
1907                 GOTO(out, rc = -EINVAL);
1908         }
1909
1910         CWARN("10f: wrote %d records then %d failed with ENOSPC\n", eok,
1911               enospc);
1912
1913         /* make sure 1 new plain llog appears in catalog (+1 with hdr) */
1914         rc = verify_handle("10f", cath, 5);
1915         if (rc)
1916                 GOTO(out, rc);
1917
1918         /* verify lgh_last_idx = llh_cat_idx = 2 now */
1919         if (cath->lgh_last_idx != cath->lgh_hdr->llh_cat_idx ||
1920             cath->lgh_last_idx != 2) {
1921                 CERROR("10f: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 2\n",
1922                        cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
1923                 GOTO(out, rc = -EINVAL);
1924         }
1925
1926         rc = dt_attr_get(env, cath->lgh_obj, &la);
1927         if (rc) {
1928                 CERROR("10f: failed to get catalog attrs: %d\n", rc);
1929                 GOTO(out, rc);
1930         }
1931
1932         if (la.la_size != cat_max_size) {
1933                 CERROR("10f: catalog size has changed after it has wrap around,"
1934                        " current size = %llu, expected size = %llu\n",
1935                        la.la_size, cat_max_size);
1936                 GOTO(out, rc = -EINVAL);
1937         }
1938
1939         /*
1940          * sync device to commit all recent LLOG changes to disk and avoid
1941          * to consume a huge space with delayed journal commit callbacks
1942          * particularly on low memory nodes or VMs
1943          */
1944         rc = dt_sync(env, dt);
1945         if (rc) {
1946                 CERROR("10f: sync failed: %d\n", rc);
1947                 GOTO(out, rc);
1948         }
1949
1950         /* will llh_cat_idx also successfully wrap ? */
1951
1952         /*
1953          * cancel all records in the plain LLOGs referenced by 2 last indexes in
1954          * Catalog
1955          */
1956
1957         /* cancel more records to free one more slot in Catalog */
1958         CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
1959         cancel_count = 0;
1960         rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
1961         if (rc != -LLOG_EEMPTY) {
1962                 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
1963                 /* need to indicate error if for any reason llog_test_recnum is
1964                  * not reached */
1965                 if (rc == 0)
1966                         rc = -ERANGE;
1967                 GOTO(out, rc);
1968         }
1969
1970         CWARN("10g: print the catalog entries.. we expect 3\n");
1971         cat_counter = 0;
1972         rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
1973                                       0, 0, false);
1974         if (rc) {
1975                 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
1976                 GOTO(out, rc);
1977         }
1978         if (cat_counter != 3) {
1979                 CERROR("10g: %d entries in catalog\n", cat_counter);
1980                 GOTO(out, rc = -EINVAL);
1981         }
1982
1983         /* verify one down in catalog (+1 with hdr) */
1984         rc = verify_handle("10g", cath, 4);
1985         if (rc)
1986                 GOTO(out, rc);
1987
1988         /*
1989          * sync device to commit all recent LLOG changes to disk and avoid
1990          * to consume a huge space with delayed journal commit callbacks
1991          * particularly on low memory nodes or VMs
1992          */
1993         rc = dt_sync(env, dt);
1994         if (rc) {
1995                 CERROR("10g: sync failed: %d\n", rc);
1996                 GOTO(out, rc);
1997         }
1998
1999         /* cancel more records to free one more slot in Catalog */
2000         CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
2001         cancel_count = 0;
2002         rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
2003         if (rc != -LLOG_EEMPTY) {
2004                 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
2005                 /*
2006                  * need to indicate error if for any reason llog_test_recnum is
2007                  * not reached
2008                  */
2009                 if (rc == 0)
2010                         rc = -ERANGE;
2011                 GOTO(out, rc);
2012         }
2013
2014         CWARN("10g: print the catalog entries.. we expect 2\n");
2015         cat_counter = 0;
2016         rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
2017                                       0, 0, false);
2018         if (rc) {
2019                 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
2020                 GOTO(out, rc);
2021         }
2022         if (cat_counter != 2) {
2023                 CERROR("10g: %d entries in catalog\n", cat_counter);
2024                 GOTO(out, rc = -EINVAL);
2025         }
2026
2027         /* verify one down in catalog (+1 with hdr) */
2028         rc = verify_handle("10g", cath, 3);
2029         if (rc)
2030                 GOTO(out, rc);
2031
2032         /* verify lgh_last_idx = 2 and llh_cat_idx = 0 now */
2033         if (cath->lgh_hdr->llh_cat_idx != 0 ||
2034             cath->lgh_last_idx != 2) {
2035                 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 0\n",
2036                        cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
2037                 GOTO(out, rc = -EINVAL);
2038         }
2039
2040         /*
2041          * sync device to commit all recent LLOG changes to disk and avoid
2042          * to consume a huge space with delayed journal commit callbacks
2043          * particularly on low memory nodes or VMs
2044          */
2045         rc = dt_sync(env, dt);
2046         if (rc) {
2047                 CERROR("10g: sync failed: %d\n", rc);
2048                 GOTO(out, rc);
2049         }
2050
2051         /* cancel more records to free one more slot in Catalog */
2052         CWARN("10g: Cancel %d records, see one log zapped\n", llog_test_recnum);
2053         cancel_count = 0;
2054         rc = llog_cat_process(env, cath, llog_cancel_rec_cb, "foobar", 0, 0);
2055         if (rc != -LLOG_EEMPTY) {
2056                 CERROR("10g: process with llog_cancel_rec_cb failed: %d\n", rc);
2057                 /*
2058                  * need to indicate error if for any reason llog_test_recnum is
2059                  * not reached
2060                  */
2061                 if (rc == 0)
2062                         rc = -ERANGE;
2063                 GOTO(out, rc);
2064         }
2065
2066         CWARN("10g: print the catalog entries.. we expect 1\n");
2067         cat_counter = 0;
2068         rc = llog_cat_process_or_fork(env, cath, cat_print_cb, NULL, "test 10",
2069                                       0, 0, false);
2070         if (rc) {
2071                 CERROR("10g: process with cat_print_cb failed: %d\n", rc);
2072                 GOTO(out, rc);
2073         }
2074         if (cat_counter != 1) {
2075                 CERROR("10g: %d entries in catalog\n", cat_counter);
2076                 GOTO(out, rc = -EINVAL);
2077         }
2078
2079         /* verify one down in catalog (+1 with hdr) */
2080         rc = verify_handle("10g", cath, 2);
2081         if (rc)
2082                 GOTO(out, rc);
2083
2084         /* verify lgh_last_idx = 2 and llh_cat_idx = 1 now */
2085         if (cath->lgh_hdr->llh_cat_idx != 1 ||
2086             cath->lgh_last_idx != 2) {
2087                 CERROR("10g: lgh_last_idx = %d vs 2, llh_cat_idx = %d vs 1\n",
2088                        cath->lgh_last_idx, cath->lgh_hdr->llh_cat_idx);
2089                 GOTO(out, rc = -EINVAL);
2090         }
2091
2092         CWARN("10g: llh_cat_idx has also successfully wrapped!\n");
2093
2094         /*
2095          * catalog has only one valid entry other slots has outdated
2096          * records. Trying to race the llog_thread_process with llog_add
2097          * llog_thread_process read buffer and loop record on it.
2098          * llog_add adds a record and mark a record in bitmap.
2099          * llog_thread_process process record with old data.
2100          */
2101         {
2102         struct llog_process_info lpi;
2103         struct lu_fid test_fid = {0};
2104
2105         lpi.lpi_loghandle = cath;
2106         lpi.lpi_cb = cat_check_old_cb;
2107         lpi.lpi_catdata = NULL;
2108         lpi.lpi_cbdata = &test_fid;
2109         init_completion(&lpi.lpi_completion);
2110
2111         kthread_run(llog_test_process_thread, &lpi, "llog_test_process_thread");
2112
2113         msleep(1 * MSEC_PER_SEC / 2);
2114         enospc = 0;
2115         eok = 0;
2116         CWARN("10h: write %d more log records\n", llog_test_recnum);
2117         for (i = 0; i < llog_test_recnum; i++) {
2118                 rc = llog_cat_add(env, cath, &lmr.lmr_hdr, NULL);
2119                 if (rc && rc != -ENOSPC) {
2120                         CERROR("10h: write %d records failed at #%d: %d\n",
2121                                llog_test_recnum, i + 1, rc);
2122                         GOTO(out, rc);
2123                 }
2124                 /*
2125                  * after last added plain LLOG has filled up, all new
2126                  * records add should fail with -ENOSPC
2127                  */
2128                 if (rc == -ENOSPC) {
2129                         enospc++;
2130                 } else {
2131                         enospc = 0;
2132                         eok++;
2133                 }
2134         }
2135
2136         if ((enospc == 0) && (enospc+eok != llog_test_recnum)) {
2137                 CERROR("10h: all last records adds should have failed with"
2138                        " -ENOSPC\n");
2139                 GOTO(out, rc = -EINVAL);
2140         }
2141
2142         CWARN("10h: wrote %d records then %d failed with ENOSPC\n", eok,
2143               enospc);
2144
2145         wait_for_completion(&lpi.lpi_completion);
2146
2147         if (lpi.lpi_rc != 0) {
2148                 CERROR("10h: race happened, old record was processed\n");
2149                 GOTO(out, rc = -EINVAL);
2150         }
2151         }
2152 out:
2153         cfs_fail_loc = 0;
2154         cfs_fail_val = 0;
2155
2156         CWARN("10: put newly-created catalog\n");
2157         rc2 = llog_cat_close(env, cath);
2158         if (rc2) {
2159                 CERROR("10: close log %s failed: %d\n", name, rc2);
2160                 if (rc == 0)
2161                         rc = rc2;
2162         }
2163 ctxt_release:
2164         llog_ctxt_put(ctxt);
2165         RETURN(rc);
2166 }
2167
2168 /*
2169  * -------------------------------------------------------------------------
2170  * Tests above, boring obd functions below
2171  * -------------------------------------------------------------------------
2172  */
2173 static int llog_run_tests(const struct lu_env *env, struct obd_device *obd)
2174 {
2175         struct llog_handle *llh = NULL;
2176         struct llog_ctxt *ctxt;
2177         int rc, err;
2178         char name[10];
2179
2180         ENTRY;
2181         ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
2182         LASSERT(ctxt);
2183
2184         sprintf(name, "%x", llog_test_rand);
2185
2186         rc = llog_test_1(env, obd, name);
2187         if (rc)
2188                 GOTO(cleanup_ctxt, rc);
2189
2190         rc = llog_test_2(env, obd, name, &llh);
2191         if (rc)
2192                 GOTO(cleanup_ctxt, rc);
2193
2194         rc = llog_test_3(env, obd, llh);
2195         if (rc)
2196                 GOTO(cleanup, rc);
2197
2198         rc = llog_test_4(env, obd);
2199         if (rc)
2200                 GOTO(cleanup, rc);
2201
2202         rc = llog_test_5(env, obd);
2203         if (rc)
2204                 GOTO(cleanup, rc);
2205
2206         rc = llog_test_6(env, obd, name);
2207         if (rc)
2208                 GOTO(cleanup, rc);
2209
2210         rc = llog_test_7(env, obd);
2211         if (rc)
2212                 GOTO(cleanup, rc);
2213
2214         rc = llog_test_8(env, obd);
2215         if (rc)
2216                 GOTO(cleanup, rc);
2217
2218         rc = llog_test_9(env, obd);
2219         if (rc != 0)
2220                 GOTO(cleanup, rc);
2221
2222         rc = llog_test_10(env, obd);
2223         if (rc)
2224                 GOTO(cleanup, rc);
2225
2226 cleanup:
2227         err = llog_destroy(env, llh);
2228         if (err)
2229                 CERROR("cleanup: llog_destroy failed: %d\n", err);
2230         llog_close(env, llh);
2231         if (rc == 0)
2232                 rc = err;
2233 cleanup_ctxt:
2234         llog_ctxt_put(ctxt);
2235         return rc;
2236 }
2237
2238 static int llog_test_cleanup(struct obd_device *obd)
2239 {
2240         struct obd_device *tgt;
2241         struct lu_env env;
2242         int rc;
2243
2244         ENTRY;
2245
2246         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2247         if (rc)
2248                 RETURN(rc);
2249
2250         tgt = obd->obd_lvfs_ctxt.dt->dd_lu_dev.ld_obd;
2251         rc = llog_cleanup(&env, llog_get_context(tgt, LLOG_TEST_ORIG_CTXT));
2252         if (rc)
2253                 CERROR("failed to llog_test_llog_finish: %d\n", rc);
2254         lu_env_fini(&env);
2255         RETURN(rc);
2256 }
2257
2258 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
2259 {
2260         struct obd_device *tgt;
2261         struct llog_ctxt *ctxt;
2262         struct dt_object *o;
2263         struct lu_env env;
2264         int rc;
2265
2266         ENTRY;
2267
2268         if (lcfg->lcfg_bufcount < 2) {
2269                 CERROR("requires a TARGET OBD name\n");
2270                 RETURN(-EINVAL);
2271         }
2272
2273         if (lcfg->lcfg_buflens[1] < 1) {
2274                 CERROR("requires a TARGET OBD name\n");
2275                 RETURN(-EINVAL);
2276         }
2277
2278         /* disk obd */
2279         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2280         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2281                 CERROR("target device not attached or not set up (%s)\n",
2282                         lustre_cfg_string(lcfg, 1));
2283                 RETURN(-EINVAL);
2284         }
2285
2286         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
2287         if (rc)
2288                 RETURN(rc);
2289
2290         CWARN("Setup llog-test device over %s device\n",
2291               lustre_cfg_string(lcfg, 1));
2292
2293         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2294         obd->obd_lvfs_ctxt.dt = lu2dt_dev(tgt->obd_lu_dev);
2295
2296         rc = llog_setup(&env, tgt, &tgt->obd_olg, LLOG_TEST_ORIG_CTXT, tgt,
2297                         &llog_osd_ops);
2298         if (rc)
2299                 GOTO(cleanup_env, rc);
2300
2301         /* use MGS llog dir for tests */
2302         ctxt = llog_get_context(tgt, LLOG_CONFIG_ORIG_CTXT);
2303         LASSERT(ctxt);
2304         o = ctxt->loc_dir;
2305         llog_ctxt_put(ctxt);
2306
2307         ctxt = llog_get_context(tgt, LLOG_TEST_ORIG_CTXT);
2308         LASSERT(ctxt);
2309         ctxt->loc_dir = o;
2310         llog_ctxt_put(ctxt);
2311
2312         llog_test_rand = get_random_u32();
2313
2314         rc = llog_run_tests(&env, tgt);
2315         if (rc)
2316                 llog_test_cleanup(obd);
2317
2318 cleanup_env:
2319         lu_env_fini(&env);
2320         RETURN(rc);
2321 }
2322
2323 static const struct obd_ops llog_obd_ops = {
2324         .o_owner       = THIS_MODULE,
2325         .o_setup       = llog_test_setup,
2326         .o_cleanup     = llog_test_cleanup,
2327 };
2328
2329 static int __init llog_test_init(void)
2330 {
2331         return class_register_type(&llog_obd_ops, NULL, false,
2332                                    "llog_test", NULL);
2333 }
2334
2335 static void __exit llog_test_exit(void)
2336 {
2337         class_unregister_type("llog_test");
2338 }
2339
2340 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2341 MODULE_DESCRIPTION("Lustre Log test module");
2342 MODULE_VERSION(LUSTRE_VERSION_STRING);
2343 MODULE_LICENSE("GPL");
2344
2345 module_init(llog_test_init);
2346 module_exit(llog_test_exit);