Whamcloud - gitweb
LU-812 kernel: AUTOCONF_INCLUDED removed
[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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdclass/llog_test.c
35  *
36  * Author: Phil Schwan <phil@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include <linux/module.h>
42 #include <linux/init.h>
43
44 #include <obd_class.h>
45 #include <lustre_log.h>
46
47 static int llog_test_rand;
48 static struct obd_uuid uuid = { .uuid = "test_uuid" };
49 static struct llog_logid cat_logid;
50
51 struct llog_mini_rec {
52         struct llog_rec_hdr     lmr_hdr;
53         struct llog_rec_tail    lmr_tail;
54 } __attribute__((packed));
55
56 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
57 {
58         int i;
59         int last_idx = 0;
60         int active_recs = 0;
61
62         for (i = 0; i < LLOG_BITMAP_BYTES * 8; i++) {
63                 if (ext2_test_bit(i, llh->lgh_hdr->llh_bitmap)) {
64                         last_idx = i;
65                         active_recs++;
66                 }
67         }
68
69         if (active_recs != num_recs) {
70                 CERROR("%s: expected %d active recs after write, found %d\n",
71                        test, num_recs, active_recs);
72                 RETURN(-ERANGE);
73         }
74
75         if (llh->lgh_hdr->llh_count != num_recs) {
76                 CERROR("%s: handle->count is %d, expected %d after write\n",
77                        test, llh->lgh_hdr->llh_count, num_recs);
78                 RETURN(-ERANGE);
79         }
80
81         if (llh->lgh_last_idx < last_idx) {
82                 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
83                        test, llh->lgh_last_idx, last_idx);
84                 RETURN(-ERANGE);
85         }
86
87         RETURN(0);
88 }
89
90 /* Test named-log create/open, close */
91 static int llog_test_1(struct obd_device *obd, char *name)
92 {
93         struct llog_handle *llh;
94         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
95         int rc;
96         int rc2;
97         ENTRY;
98
99         CWARN("1a: create a log with name: %s\n", name);
100         LASSERT(ctxt);
101
102         rc = llog_create(ctxt, &llh, NULL, name);
103         if (rc) {
104                 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
105                 llog_ctxt_put(ctxt);
106                 RETURN(rc);
107         }
108         llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
109
110         if ((rc = verify_handle("1", llh, 1)))
111                 GOTO(out, rc);
112
113  out:
114         CWARN("1b: close newly-created log\n");
115         rc2 = llog_close(llh);
116         llog_ctxt_put(ctxt);
117         if (rc2) {
118                 CERROR("1b: close log %s failed: %d\n", name, rc2);
119                 if (rc == 0)
120                         rc = rc2;
121         }
122         RETURN(rc);
123 }
124
125 /* Test named-log reopen; returns opened log on success */
126 static int llog_test_2(struct obd_device *obd, char *name,
127                        struct llog_handle **llh)
128 {
129         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
130         int rc;
131         ENTRY;
132
133         CWARN("2a: re-open a log with name: %s\n", name);
134         rc = llog_create(ctxt, llh, NULL, name);
135         if (rc) {
136                 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
137                 GOTO(out, rc);
138         }
139         llog_init_handle(*llh, LLOG_F_IS_PLAIN, &uuid);
140
141         if ((rc = verify_handle("2", *llh, 1)))
142                 GOTO(out, rc);
143 #if 0
144         CWARN("2b: create a log without specified NAME & LOGID\n");
145         rc = llog_create(ctxt, &loghandle, NULL, NULL);
146         if (rc) {
147                 CERROR("2b: create log failed\n");
148                 GOTO(out, rc);
149         }
150         llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
151         logid = loghandle->lgh_id;
152         llog_close(loghandle);
153
154         CWARN("2b: re-open the log by LOGID\n");
155         rc = llog_create(ctxt, &loghandle, &logid, NULL);
156         if (rc) {
157                 CERROR("2b: re-open log by LOGID failed\n");
158                 GOTO(out, rc);
159         }
160         llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
161
162         CWARN("2b: destroy this log\n");
163         rc = llog_destroy(loghandle);
164         if (rc) {
165                 CERROR("2b: destroy log failed\n");
166                 GOTO(out, rc);
167         }
168         llog_free_handle(loghandle);
169 #endif
170 out:
171         llog_ctxt_put(ctxt);
172
173         RETURN(rc);
174 }
175
176 /* Test record writing, single and in bulk */
177 static int llog_test_3(struct obd_device *obd, struct llog_handle *llh)
178 {
179         struct llog_create_rec lcr;
180         int rc, i;
181         int num_recs = 1;       /* 1 for the header */
182         ENTRY;
183
184         lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = sizeof(lcr);
185         lcr.lcr_hdr.lrh_type = OST_SZ_REC;
186
187         CWARN("3a: write one create_rec\n");
188         rc = llog_write_rec(llh,  &lcr.lcr_hdr, NULL, 0, NULL, -1);
189         num_recs++;
190         if (rc) {
191                 CERROR("3a: write one log record failed: %d\n", rc);
192                 RETURN(rc);
193         }
194
195         if ((rc = verify_handle("3a", llh, num_recs)))
196                 RETURN(rc);
197
198         CWARN("3b: write 10 cfg log records with 8 bytes bufs\n");
199         for (i = 0; i < 10; i++) {
200                 struct llog_rec_hdr hdr;
201                 char buf[8];
202                 hdr.lrh_len = 8;
203                 hdr.lrh_type = OBD_CFG_REC;
204                 memset(buf, 0, sizeof buf);
205                 rc = llog_write_rec(llh, &hdr, NULL, 0, buf, -1);
206                 if (rc) {
207                         CERROR("3b: write 10 records failed at #%d: %d\n",
208                                i + 1, rc);
209                         RETURN(rc);
210                 }
211                 num_recs++;
212                 if ((rc = verify_handle("3c", llh, num_recs)))
213                         RETURN(rc);
214         }
215
216         if ((rc = verify_handle("3b", llh, num_recs)))
217                 RETURN(rc);
218
219         CWARN("3c: write 1000 more log records\n");
220         for (i = 0; i < 1000; i++) {
221                 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
222                 if (rc) {
223                         CERROR("3c: write 1000 records failed at #%d: %d\n",
224                                i + 1, rc);
225                         RETURN(rc);
226                 }
227                 num_recs++;
228                 if ((rc = verify_handle("3b", llh, num_recs)))
229                         RETURN(rc);
230         }
231
232         if ((rc = verify_handle("3c", llh, num_recs)))
233                 RETURN(rc);
234
235         CWARN("3d: write log more than BITMAP_SIZE, return -ENOSPC\n");
236         for (i = 0; i < LLOG_BITMAP_SIZE(llh->lgh_hdr) + 1; i++) {
237                 struct llog_rec_hdr hdr;
238                 char buf_even[24];
239                 char buf_odd[32];
240
241                 memset(buf_odd, 0, sizeof buf_odd);
242                 memset(buf_even, 0, sizeof buf_even);
243                 if ((i % 2) == 0) {
244                         hdr.lrh_len = 24;
245                         hdr.lrh_type = OBD_CFG_REC;
246                         rc = llog_write_rec(llh, &hdr, NULL, 0, buf_even, -1);
247                 } else {
248                         hdr.lrh_len = 32;
249                         hdr.lrh_type = OBD_CFG_REC;
250                         rc = llog_write_rec(llh, &hdr, NULL, 0, buf_odd, -1);
251                 }
252                 if (rc) {
253                         if (rc == -ENOSPC) {
254                                 break;
255                         } else {
256                                 CERROR("3c: write recs failed at #%d: %d\n",
257                                         i + 1, rc);
258                                 RETURN(rc);
259                         }
260                 }
261                 num_recs++;
262         }
263         if (rc != -ENOSPC) {
264                 CWARN("3d: write record more than BITMAP size!\n");
265                 RETURN(-EINVAL);
266         }
267         if ((rc = verify_handle("3d", llh, num_recs)))
268                 RETURN(rc);
269
270         RETURN(rc);
271 }
272
273 /* Test catalogue additions */
274 static int llog_test_4(struct obd_device *obd)
275 {
276         struct llog_handle *cath;
277         char name[10];
278         int rc, i, buflen;
279         struct llog_mini_rec lmr;
280         struct llog_cookie cookie;
281         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
282         int num_recs = 0;
283         char *buf;
284         struct llog_rec_hdr rec;
285
286         ENTRY;
287
288         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
289         lmr.lmr_hdr.lrh_type = 0xf00f00;
290
291         sprintf(name, "%x", llog_test_rand+1);
292         CWARN("4a: create a catalog log with name: %s\n", name);
293         rc = llog_create(ctxt, &cath, NULL, name);
294         if (rc) {
295                 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
296                 GOTO(out, rc);
297         }
298         llog_init_handle(cath, LLOG_F_IS_CAT, &uuid);
299         num_recs++;
300         cat_logid = cath->lgh_id;
301
302         CWARN("4b: write 1 record into the catalog\n");
303         rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, &cookie, NULL);
304         if (rc != 1) {
305                 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
306                 GOTO(out, rc);
307         }
308         num_recs++;
309         if ((rc = verify_handle("4b", cath, 2)))
310                 GOTO(ctxt_release, rc);
311
312         if ((rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs)))
313                 GOTO(ctxt_release, rc);
314
315         CWARN("4c: cancel 1 log record\n");
316         rc = llog_cat_cancel_records(cath, 1, &cookie);
317         if (rc) {
318                 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
319                 GOTO(out, rc);
320         }
321         num_recs--;
322
323         if ((rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs)))
324                 GOTO(ctxt_release, rc);
325
326         CWARN("4d: write 40,000 more log records\n");
327         for (i = 0; i < 40000; i++) {
328                 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, NULL, NULL);
329                 if (rc) {
330                         CERROR("4d: write 40000 records failed at #%d: %d\n",
331                                i + 1, rc);
332                         GOTO(out, rc);
333                 }
334                 num_recs++;
335         }
336
337         CWARN("4e: add 5 large records, one record per block\n");
338         buflen = LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr)
339                         - sizeof(struct llog_rec_tail);
340         OBD_ALLOC(buf, buflen);
341         if (buf == NULL)
342                 GOTO(out, rc = -ENOMEM);
343         for (i = 0; i < 5; i++) {
344                 rec.lrh_len = buflen;
345                 rec.lrh_type = OBD_CFG_REC;
346                 rc = llog_cat_add_rec(cath, &rec, NULL, buf);
347                 if (rc) {
348                         CERROR("4e: write 5 records failed at #%d: %d\n",
349                                i + 1, rc);
350                         OBD_FREE(buf, buflen);
351                         GOTO(out, rc);
352                 }
353                 num_recs++;
354         }
355         OBD_FREE(buf, buflen);
356
357  out:
358         CWARN("4f: put newly-created catalog\n");
359         rc = llog_cat_put(cath);
360 ctxt_release:
361         llog_ctxt_put(ctxt);
362         if (rc)
363                 CERROR("1b: close log %s failed: %d\n", name, rc);
364         RETURN(rc);
365 }
366
367 static int cat_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
368                         void *data)
369 {
370         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
371
372         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
373                 CERROR("invalid record in catalog\n");
374                 RETURN(-EINVAL);
375         }
376
377         CWARN("seeing record at index %d - "LPX64":%x in log "LPX64"\n",
378                rec->lrh_index, lir->lid_id.lgl_oid,
379                lir->lid_id.lgl_ogen, llh->lgh_id.lgl_oid);
380         RETURN(0);
381 }
382
383 static int plain_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
384                           void *data)
385 {
386         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
387                 CERROR("log is not plain\n");
388                 RETURN(-EINVAL);
389         }
390
391         CWARN("seeing record at index %d in log "LPX64"\n",
392                rec->lrh_index, llh->lgh_id.lgl_oid);
393         RETURN(0);
394 }
395
396 static int llog_cancel_rec_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
397                               void *data)
398 {
399         struct llog_cookie cookie;
400         static int i = 0;
401
402         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
403                 CERROR("log is not plain\n");
404                 RETURN(-EINVAL);
405         }
406
407         cookie.lgc_lgl = llh->lgh_id;
408         cookie.lgc_index = rec->lrh_index;
409
410         llog_cat_cancel_records(llh->u.phd.phd_cat_handle, 1, &cookie);
411         i++;
412         if (i == 40000)
413                 RETURN(-4711);
414         RETURN(0);
415 }
416
417 /* Test log and catalogue processing */
418 static int llog_test_5(struct obd_device *obd)
419 {
420         struct llog_handle *llh = NULL;
421         char name[10];
422         int rc;
423         struct llog_mini_rec lmr;
424         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
425
426         ENTRY;
427
428         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len = LLOG_MIN_REC_SIZE;
429         lmr.lmr_hdr.lrh_type = 0xf00f00;
430
431         CWARN("5a: re-open catalog by id\n");
432         rc = llog_create(ctxt, &llh, &cat_logid, NULL);
433         if (rc) {
434                 CERROR("5a: llog_create with logid failed: %d\n", rc);
435                 GOTO(out, rc);
436         }
437         llog_init_handle(llh, LLOG_F_IS_CAT, &uuid);
438
439         CWARN("5b: print the catalog entries.. we expect 2\n");
440         rc = llog_process(llh, cat_print_cb, "test 5", NULL);
441         if (rc) {
442                 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
443                 GOTO(out, rc);
444         }
445
446         CWARN("5c: Cancel 40000 records, see one log zapped\n");
447         rc = llog_cat_process(llh, llog_cancel_rec_cb, "foobar", 0, 0);
448         if (rc != -4711) {
449                 CERROR("5c: process with cat_cancel_cb failed: %d\n", rc);
450                 GOTO(out, rc);
451         }
452
453         CWARN("5d: add 1 record to the log with many canceled empty pages\n");
454         rc = llog_cat_add_rec(llh, &lmr.lmr_hdr, NULL, NULL);
455         if (rc) {
456                 CERROR("5d: add record to the log with many canceled empty\
457                        pages failed\n");
458                 GOTO(out, rc);
459         }
460
461         CWARN("5b: print the catalog entries.. we expect 1\n");
462         rc = llog_process(llh, cat_print_cb, "test 5", NULL);
463         if (rc) {
464                 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
465                 GOTO(out, rc);
466         }
467
468         CWARN("5e: print plain log entries.. expect 6\n");
469         rc = llog_cat_process(llh, plain_print_cb, "foobar", 0, 0);
470         if (rc) {
471                 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
472                 GOTO(out, rc);
473         }
474
475         CWARN("5f: print plain log entries reversely.. expect 6\n");
476         rc = llog_cat_reverse_process(llh, plain_print_cb, "foobar");
477         if (rc) {
478                 CERROR("5f: reversely process with plain_print_cb failed: %d\n", rc);
479                 GOTO(out, rc);
480         }
481
482  out:
483         CWARN("5: close re-opened catalog\n");
484         if (llh)
485                 rc = llog_cat_put(llh);
486         if (rc)
487                 CERROR("1b: close log %s failed: %d\n", name, rc);
488         llog_ctxt_put(ctxt);
489
490         RETURN(rc);
491 }
492
493 /* Test client api; open log by name and process */
494 static int llog_test_6(struct obd_device *obd, char *name)
495 {
496         struct obd_device *mgc_obd;
497         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
498         struct obd_uuid *mgs_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
499         struct obd_export *exp;
500         struct obd_uuid uuid = {"LLOG_TEST6_UUID"};
501         struct llog_handle *llh = NULL;
502         struct llog_ctxt *nctxt;
503         int rc;
504
505         CWARN("6a: re-open log %s using client API\n", name);
506         mgc_obd = class_find_client_obd(mgs_uuid, LUSTRE_MGC_NAME, NULL);
507         if (mgc_obd == NULL) {
508                 CERROR("6: no MGC devices connected to %s found.\n",
509                        mgs_uuid->uuid);
510                 GOTO(ctxt_release, rc = -ENOENT);
511         }
512
513         rc = obd_connect(NULL, &exp, mgc_obd, &uuid,
514                          NULL /* obd_connect_data */, NULL);
515         if (rc != -EALREADY) {
516                 CERROR("6: connect on connected MDC (%s) failed to return"
517                        " -EALREADY", mgc_obd->obd_name);
518                 if (rc == 0)
519                         obd_disconnect(exp);
520                 GOTO(ctxt_release, rc = -EINVAL);
521         }
522
523         nctxt = llog_get_context(mgc_obd, LLOG_CONFIG_REPL_CTXT);
524         rc = llog_create(nctxt, &llh, NULL, name);
525         if (rc) {
526                 CERROR("6: llog_create failed %d\n", rc);
527                 llog_ctxt_put(nctxt);
528                 GOTO(ctxt_release, rc);
529         }
530
531         rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
532         if (rc) {
533                 CERROR("6: llog_init_handle failed %d\n", rc);
534                 GOTO(parse_out, rc);
535         }
536
537         rc = llog_process(llh, plain_print_cb, NULL, NULL);
538         if (rc)
539                 CERROR("6: llog_process failed %d\n", rc);
540
541         rc = llog_reverse_process(llh, plain_print_cb, NULL, NULL);
542         if (rc)
543                 CERROR("6: llog_reverse_process failed %d\n", rc);
544
545 parse_out:
546         rc = llog_close(llh);
547         llog_ctxt_put(nctxt);
548         if (rc) {
549                 CERROR("6: llog_close failed: rc = %d\n", rc);
550         }
551 ctxt_release:
552         llog_ctxt_put(ctxt);
553         RETURN(rc);
554 }
555
556 static int llog_test_7(struct obd_device *obd)
557 {
558         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
559         struct llog_handle *llh;
560         struct llog_create_rec lcr;
561         char name[10];
562         int rc;
563         ENTRY;
564
565         sprintf(name, "%x", llog_test_rand+2);
566         CWARN("7: create a log with name: %s\n", name);
567         LASSERT(ctxt);
568
569         rc = llog_create(ctxt, &llh, NULL, name);
570         if (rc) {
571                 CERROR("7: llog_create with name %s failed: %d\n", name, rc);
572                 GOTO(ctxt_release, rc);
573         }
574         llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
575
576         lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = sizeof(lcr);
577         lcr.lcr_hdr.lrh_type = OST_SZ_REC;
578         rc = llog_write_rec(llh,  &lcr.lcr_hdr, NULL, 0, NULL, -1);
579         if (rc) {
580                 CERROR("7: write one log record failed: %d\n", rc);
581                 GOTO(ctxt_release, rc);
582         }
583
584         rc = llog_destroy(llh);
585         if (rc)
586                 CERROR("7: llog_destroy failed: %d\n", rc);
587         else
588                 llog_free_handle(llh);
589 ctxt_release:
590         llog_ctxt_put(ctxt);
591         RETURN(rc);
592 }
593
594 /* -------------------------------------------------------------------------
595  * Tests above, boring obd functions below
596  * ------------------------------------------------------------------------- */
597 static int llog_run_tests(struct obd_device *obd)
598 {
599         struct llog_handle *llh;
600         struct lvfs_run_ctxt saved;
601         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
602         int rc, err, cleanup_phase = 0;
603         char name[10];
604         ENTRY;
605
606         sprintf(name, "%x", llog_test_rand);
607         push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
608
609         rc = llog_test_1(obd, name);
610         if (rc)
611                 GOTO(cleanup, rc);
612
613         rc = llog_test_2(obd, name, &llh);
614         if (rc)
615                 GOTO(cleanup, rc);
616         cleanup_phase = 1; /* close llh */
617
618         rc = llog_test_3(obd, llh);
619         if (rc)
620                 GOTO(cleanup, rc);
621
622         rc = llog_test_4(obd);
623         if (rc)
624                 GOTO(cleanup, rc);
625
626         rc = llog_test_5(obd);
627         if (rc)
628                 GOTO(cleanup, rc);
629
630         rc = llog_test_6(obd, name);
631         if (rc)
632                 GOTO(cleanup, rc);
633
634         rc = llog_test_7(obd);
635         if (rc)
636                 GOTO(cleanup, rc);
637
638  cleanup:
639         switch (cleanup_phase) {
640         case 1:
641                 err = llog_close(llh);
642                 if (err)
643                         CERROR("cleanup: llog_close failed: %d\n", err);
644                 if (!rc)
645                         rc = err;
646         case 0:
647                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
648         }
649         llog_ctxt_put(ctxt);
650         return rc;
651 }
652
653
654 static int llog_test_llog_init(struct obd_device *obd,
655                                struct obd_llog_group *olg,
656                                struct obd_device *tgt, int *index)
657 {
658         int rc;
659         ENTRY;
660
661         rc = llog_setup(obd, &obd->obd_olg, LLOG_TEST_ORIG_CTXT, tgt, 0, NULL,
662                         &llog_lvfs_ops);
663         RETURN(rc);
664 }
665
666 static int llog_test_llog_finish(struct obd_device *obd, int count)
667 {
668         int rc;
669         ENTRY;
670
671         rc = llog_cleanup(llog_get_context(obd, LLOG_TEST_ORIG_CTXT));
672         RETURN(rc);
673 }
674 #ifdef LPROCFS
675 static struct lprocfs_vars lprocfs_llog_test_obd_vars[] = { {0} };
676 static struct lprocfs_vars lprocfs_llog_test_module_vars[] = { {0} };
677 static void lprocfs_llog_test_init_vars(struct lprocfs_static_vars *lvars)
678 {
679     lvars->module_vars  = lprocfs_llog_test_module_vars;
680     lvars->obd_vars     = lprocfs_llog_test_obd_vars;
681 }
682 #endif
683
684 static int llog_test_cleanup(struct obd_device *obd)
685 {
686         int rc = obd_llog_finish(obd, 0);
687         if (rc)
688                 CERROR("failed to llog_test_llog_finish: %d\n", rc);
689
690         lprocfs_obd_cleanup(obd);
691
692         return rc;
693 }
694
695 static int llog_test_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
696 {
697         struct lprocfs_static_vars lvars;
698         struct obd_device *tgt;
699         int rc;
700         ENTRY;
701
702         if (lcfg->lcfg_bufcount < 2) {
703                 CERROR("requires a TARGET OBD name\n");
704                 RETURN(-EINVAL);
705         }
706
707         if (lcfg->lcfg_buflens[1] < 1) {
708                 CERROR("requires a TARGET OBD name\n");
709                 RETURN(-EINVAL);
710         }
711
712         /* disk obd */
713         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
714         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
715                 CERROR("target device not attached or not set up (%s)\n",
716                        lustre_cfg_string(lcfg, 1));
717                 RETURN(-EINVAL);
718         }
719
720         rc = obd_llog_init(obd, NULL, tgt, NULL);
721         if (rc)
722                 RETURN(rc);
723
724         llog_test_rand = cfs_rand();
725
726         rc = llog_run_tests(obd);
727         if (rc)
728                 llog_test_cleanup(obd);
729
730         lprocfs_llog_test_init_vars(&lvars);
731         lprocfs_obd_setup(obd, lvars.obd_vars);
732
733         RETURN(rc);
734 }
735
736 static struct obd_ops llog_obd_ops = {
737         .o_owner       = THIS_MODULE,
738         .o_setup       = llog_test_setup,
739         .o_cleanup     = llog_test_cleanup,
740         .o_llog_init   = llog_test_llog_init,
741         .o_llog_finish = llog_test_llog_finish,
742 };
743
744 static int __init llog_test_init(void)
745 {
746         struct lprocfs_static_vars lvars;
747
748         lprocfs_llog_test_init_vars(&lvars);
749         return class_register_type(&llog_obd_ops, NULL,
750                                    lvars.module_vars,"llog_test", NULL);
751 }
752
753 static void __exit llog_test_exit(void)
754 {
755         class_unregister_type("llog_test");
756 }
757
758 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
759 MODULE_DESCRIPTION("llog test module");
760 MODULE_LICENSE("GPL");
761
762 module_init(llog_test_init);
763 module_exit(llog_test_exit);