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