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