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