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