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