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