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