Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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  *  Copyright (C) 2003 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org/
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * A kernel module which tests the llog API from the OBD setup function.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_CLASS
29
30 #include <linux/module.h>
31 #include <linux/init.h>
32
33 #include <linux/obd_class.h>
34 #include <linux/lustre_log.h>
35 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
36
37 static int llog_test_rand;
38 static struct obd_uuid uuid = { .uuid = "test_uuid" };
39 static struct llog_logid cat_logid;
40
41 struct llog_mini_rec {
42         struct llog_rec_hdr     lmr_hdr;
43         __u32                   padding[2];
44         struct llog_rec_tail    lmr_tail;
45 } __attribute__((packed));
46
47 static int verify_handle(char *test, struct llog_handle *llh, int num_recs)
48 {
49         int i;
50         int last_idx = 0;
51         int active_recs = 0;
52
53         for (i = 0; i < LLOG_BITMAP_BYTES * 8; i++) {
54                 if (ext2_test_bit(i, llh->lgh_hdr->llh_bitmap)) {
55                         last_idx = i;
56                         active_recs++;
57                 }
58         }
59
60         if (active_recs != num_recs) {
61                 CERROR("%s: expected %d active recs after write, found %d\n",
62                        test, num_recs, active_recs);
63                 RETURN(-ERANGE);
64         }
65
66         if (le32_to_cpu(llh->lgh_hdr->llh_count) != num_recs) {
67                 CERROR("%s: handle->count is %d, expected %d after write\n",
68                        test, le32_to_cpu(llh->lgh_hdr->llh_count), num_recs);
69                 RETURN(-ERANGE);
70         }
71
72         if (llh->lgh_last_idx < last_idx) {
73                 CERROR("%s: handle->last_idx is %d, expected %d after write\n",
74                        test, llh->lgh_last_idx, last_idx);
75                 RETURN(-ERANGE);
76         }
77
78         RETURN(0);
79 }
80
81 /* Test named-log create/open, close */
82 static int llog_test_1(struct obd_device *obd, char *name)
83 {
84         struct llog_handle *llh;
85         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
86         int rc;
87         int rc2;
88         ENTRY;
89
90         CERROR("1a: create a log with name: %s\n", name);
91         LASSERT(ctxt);
92
93         rc = llog_create(ctxt, &llh, NULL, name);
94         if (rc) {
95                 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
96                 RETURN(rc);
97         }
98         llog_init_handle(llh, LLOG_F_IS_PLAIN, &uuid);
99
100         if ((rc = verify_handle("1", llh, 1)))
101                 GOTO(out, rc);
102
103  out:
104         CERROR("1b: close newly-created log\n");
105         rc2 = llog_close(llh);
106         if (rc2) {
107                 CERROR("1b: close log %s failed: %d\n", name, rc2);
108                 if (rc == 0)
109                         rc = rc2;
110         }
111         RETURN(rc);
112 }
113
114 /* Test named-log reopen; returns opened log on success */
115 static int llog_test_2(struct obd_device *obd, char *name,
116                        struct llog_handle **llh)
117 {
118         struct llog_handle *loghandle;
119         struct llog_logid logid;
120         int rc;
121         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
122         ENTRY;
123
124         CERROR("2a: re-open a log with name: %s\n", name);
125         rc = llog_create(ctxt, llh, NULL, name);
126         if (rc) {
127                 CERROR("2a: re-open log with name %s failed: %d\n", name, rc);
128                 RETURN(rc);
129         }
130         llog_init_handle(*llh, LLOG_F_IS_PLAIN, &uuid);
131
132         if ((rc = verify_handle("2", *llh, 1)))
133                 RETURN(rc);
134
135         CERROR("2b: create a log without specified NAME & LOGID\n");
136         rc = llog_create(ctxt, &loghandle, NULL, NULL);
137         if (rc) {
138                 CERROR("2b: create log failed\n");
139                 RETURN(rc);
140         }
141         llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
142         logid = loghandle->lgh_id;
143         llog_close(loghandle);
144
145         CERROR("2b: re-open the log by LOGID\n");
146         rc = llog_create(ctxt, &loghandle, &logid, NULL);
147         if (rc) {
148                 CERROR("2b: re-open log by LOGID failed\n");
149                 RETURN(rc);
150         }
151         llog_init_handle(loghandle, LLOG_F_IS_PLAIN, &uuid);
152
153         CERROR("2b: destroy this log\n");
154         rc = llog_destroy(loghandle);
155         if (rc) {
156                 CERROR("2b: destroy log failed\n");
157                 RETURN(rc);
158         }
159         llog_free_handle(loghandle);
160
161         RETURN(rc);
162 }
163
164 /* Test record writing, single and in bulk */
165 static int llog_test_3(struct obd_device *obd, struct llog_handle *llh)
166 {
167         struct llog_create_rec lcr;
168         int rc, i;
169         int num_recs = 1;       /* 1 for the header */
170         ENTRY;
171
172         lcr.lcr_hdr.lrh_len = lcr.lcr_tail.lrt_len = cpu_to_le32(sizeof(lcr));
173         lcr.lcr_hdr.lrh_type = cpu_to_le32(OST_SZ_REC);
174
175         CERROR("3a: write one create_rec\n");
176         rc = llog_write_rec(llh,  &lcr.lcr_hdr, NULL, 0, NULL, -1);
177         num_recs++;
178         if (rc) {
179                 CERROR("3a: write one log record failed: %d\n", rc);
180                 RETURN(rc);
181         }
182
183         if ((rc = verify_handle("3a", llh, num_recs)))
184                 RETURN(rc);
185
186         CERROR("3b: write 10 cfg log records with 8 bytes bufs\n");
187         for (i = 0; i < 10; i++) {
188                 struct llog_rec_hdr hdr;
189                 char buf[8];
190                 hdr.lrh_len = cpu_to_le32(8);
191                 hdr.lrh_type = cpu_to_le32(OBD_CFG_REC);
192                 memset(buf, 0, sizeof buf);
193                 rc = llog_write_rec(llh, &hdr, NULL, 0, buf, -1);
194                 if (rc) {
195                         CERROR("3b: write 10 records failed at #%d: %d\n",
196                                i + 1, rc);
197                         RETURN(rc);
198                 }
199                 num_recs++;
200                 if ((rc = verify_handle("3c", llh, num_recs)))
201                         RETURN(rc);
202         }
203
204         if ((rc = verify_handle("3b", llh, num_recs)))
205                 RETURN(rc);
206
207         CERROR("3c: write 1000 more log records\n");
208         for (i = 0; i < 1000; i++) {
209                 rc = llog_write_rec(llh, &lcr.lcr_hdr, NULL, 0, NULL, -1);
210                 if (rc) {
211                         CERROR("3c: write 1000 records failed at #%d: %d\n",
212                                i + 1, rc);
213                         RETURN(rc);
214                 }
215                 num_recs++;
216                 if ((rc = verify_handle("3b", llh, num_recs)))
217                         RETURN(rc);
218         }
219
220         if ((rc = verify_handle("3c", llh, num_recs)))
221                 RETURN(rc);
222
223         RETURN(rc);
224 }
225
226 /* Test catalogue additions */
227 static int llog_test_4(struct obd_device *obd)
228 {
229         struct llog_handle *cath;
230         char name[10];
231         int rc, i, buflen;
232         struct llog_mini_rec lmr;
233         struct llog_cookie cookie;
234         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
235         int num_recs = 0;
236         char *buf;
237         struct llog_rec_hdr rec;
238
239         ENTRY;
240
241         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len =
242                 cpu_to_le32(LLOG_MIN_REC_SIZE);
243         lmr.lmr_hdr.lrh_type = cpu_to_le32(0xf00f00);
244
245         sprintf(name, "%x", llog_test_rand+1);
246         CERROR("4a: create a catalog log with name: %s\n", name);
247         rc = llog_create(ctxt, &cath, NULL, name);
248         if (rc) {
249                 CERROR("1a: llog_create with name %s failed: %d\n", name, rc);
250                 GOTO(out, rc);
251         }
252         llog_init_handle(cath, LLOG_F_IS_CAT, &uuid);
253         num_recs++;
254         cat_logid = cath->lgh_id;
255
256         CERROR("4b: write 1 record into the catalog\n");
257         rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, &cookie, NULL);
258         if (rc != 1) {
259                 CERROR("4b: write 1 catalog record failed at: %d\n", rc);
260                 GOTO(out, rc);
261         }
262         num_recs++;
263         if ((rc = verify_handle("4b", cath, 2)))
264                 RETURN(rc);
265
266         if ((rc = verify_handle("4b", cath->u.chd.chd_current_log, num_recs)))
267                 RETURN(rc);
268
269         CERROR("4c: cancel 1 log record\n");
270         rc = llog_cat_cancel_records(cath, 1, &cookie);
271         if (rc) {
272                 CERROR("4c: cancel 1 catalog based record failed: %d\n", rc);
273                 GOTO(out, rc);
274         }
275         num_recs--;
276
277         if ((rc = verify_handle("4c", cath->u.chd.chd_current_log, num_recs)))
278                 RETURN(rc);
279
280         CERROR("4d: write 40,000 more log records\n");
281         for (i = 0; i < 40000; i++) {
282                 rc = llog_cat_add_rec(cath, &lmr.lmr_hdr, NULL, NULL);
283                 if (rc) {
284                         CERROR("4d: write 40000 records failed at #%d: %d\n",
285                                i + 1, rc);
286                         GOTO(out, rc);
287                 }
288                 num_recs++;
289         }
290
291         CERROR("4e: add 5 large records, one record per block\n");
292         buflen = LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr)
293                         - sizeof(struct llog_rec_tail);
294         OBD_ALLOC(buf, buflen);
295         if (buf == NULL)
296                 GOTO(out, rc = -ENOMEM);
297         for (i = 0; i < 5; i++) {
298                 rec.lrh_len = cpu_to_le32(buflen);
299                 rec.lrh_type = cpu_to_le32(OBD_CFG_REC);
300                 rc = llog_cat_add_rec(cath, &rec, NULL, buf);
301                 if (rc) {
302                         CERROR("4e: write 5 records failed at #%d: %d\n",
303                                i + 1, rc);
304                         OBD_FREE(buf, buflen);
305                         GOTO(out, rc);
306                 }
307                 num_recs++;
308         }
309         OBD_FREE(buf, buflen);
310
311  out:
312         CERROR("4f: put newly-created catalog\n");
313         rc = llog_cat_put(cath);
314         if (rc)
315                 CERROR("1b: close log %s failed: %d\n", name, rc);
316         RETURN(rc);
317 }
318
319 static int cat_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
320                         void *data)
321 {
322         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
323
324         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
325                 CERROR("invalid record in catalog\n");
326                 RETURN(-EINVAL);
327         }
328
329         CERROR("seeing record at index %d - "LPX64":%x in log "LPX64"\n",
330                le32_to_cpu(rec->lrh_index), lir->lid_id.lgl_oid,
331                lir->lid_id.lgl_ogen, llh->lgh_id.lgl_oid);
332         RETURN(0);
333 }
334
335 static int plain_print_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
336                           void *data)
337 {
338         if (!le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN) {
339                 CERROR("log is not plain\n");
340                 RETURN(-EINVAL);
341         }
342
343         CERROR("seeing record at index %d in log "LPX64"\n",
344                le32_to_cpu(rec->lrh_index), llh->lgh_id.lgl_oid);
345         RETURN(0);
346 }
347
348 static int llog_cancel_rec_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
349                               void *data)
350 {
351         struct llog_cookie cookie;
352         static int i = 0;
353
354         if (!le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN) {
355                 CERROR("log is not plain\n");
356                 RETURN(-EINVAL);
357         }
358
359         cookie.lgc_lgl = llh->lgh_id;
360         cookie.lgc_index = le32_to_cpu(rec->lrh_index);
361
362         llog_cat_cancel_records(llh->u.phd.phd_cat_handle, 1, &cookie);
363         i++;
364         if (i == 40000)
365                 RETURN(-4711);
366         RETURN(0);
367 }
368
369
370
371 /* Test log and catalogue processing */
372 static int llog_test_5(struct obd_device *obd)
373 {
374         struct llog_handle *llh = NULL;
375         char name[10];
376         int rc;
377         struct llog_mini_rec lmr;
378         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
379
380         ENTRY;
381
382         lmr.lmr_hdr.lrh_len = lmr.lmr_tail.lrt_len =
383                 cpu_to_le32(LLOG_MIN_REC_SIZE);
384         lmr.lmr_hdr.lrh_type = cpu_to_le32(0xf00f00);
385
386         CERROR("5a: re-open catalog by id\n");
387         rc = llog_create(ctxt, &llh, &cat_logid, NULL);
388         if (rc) {
389                 CERROR("5a: llog_create with logid failed: %d\n", rc);
390                 GOTO(out, rc);
391         }
392         llog_init_handle(llh, LLOG_F_IS_CAT, &uuid);
393
394         CERROR("5b: print the catalog entries.. we expect 2\n");
395         rc = llog_process(llh, (llog_cb_t)cat_print_cb, "test 5");
396         if (rc) {
397                 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
398                 GOTO(out, rc);
399         }
400
401         CERROR("5c: Cancel 40000 records, see one log zapped\n");
402         rc = llog_cat_process(llh, llog_cancel_rec_cb, "foobar");
403         if (rc != -4711) {
404                 CERROR("5c: process with cat_cancel_cb failed: %d\n", rc);
405                 GOTO(out, rc);
406         }
407
408         CERROR("5d: add 1 record to the log with many canceled empty pages\n");
409         rc = llog_cat_add_rec(llh, &lmr.lmr_hdr, NULL, NULL);
410         if (rc) {
411                 CERROR("5d: add record to the log with many canceled empty\
412                        pages failed\n");
413                 GOTO(out, rc);
414         }
415
416         CERROR("5b: print the catalog entries.. we expect 1\n");
417         rc = llog_process(llh, (llog_cb_t)cat_print_cb, "test 5");
418         if (rc) {
419                 CERROR("5b: process with cat_print_cb failed: %d\n", rc);
420                 GOTO(out, rc);
421         }
422
423         CERROR("5e: print plain log entries.. expect 6\n");
424         rc = llog_cat_process(llh, plain_print_cb, "foobar");
425         if (rc) {
426                 CERROR("5e: process with plain_print_cb failed: %d\n", rc);
427                 GOTO(out, rc);
428         }
429
430  out:
431         CERROR("5: close re-opened catalog\n");
432         if (llh)
433                 rc = llog_cat_put(llh);
434         if (rc)
435                 CERROR("1b: close log %s failed: %d\n", name, rc);
436         RETURN(rc);
437 }
438
439 /* Test client api; open log by name and process */
440 static int llog_test_6(struct obd_device *obd, char *name)
441 {
442         struct obd_device *mdc_obd;
443         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
444         struct obd_uuid *mds_uuid = &ctxt->loc_exp->exp_obd->obd_uuid;
445         struct lustre_handle exph = {0, };
446         struct obd_export *exp;
447         struct obd_uuid uuid = {"LLOG_TEST6_UUID"};
448         struct llog_handle *llh = NULL;
449         struct llog_ctxt *nctxt;
450         int rc;
451
452         CERROR("6a: re-open log %s using client API\n", name);
453         mdc_obd = class_find_client_obd(mds_uuid, LUSTRE_MDC_NAME, NULL);
454         if (mdc_obd == NULL) {
455                 CERROR("6: no MDC devices connected to %s found.\n",
456                        mds_uuid->uuid);
457                 RETURN(-ENOENT);
458         }
459
460         rc = obd_connect(&exph, mdc_obd, &uuid);
461         if (rc) {
462                 CERROR("6: failed to connect to MDC: %s\n", mdc_obd->obd_name);
463                 RETURN(rc);
464         }
465         exp = class_conn2export(&exph);
466
467         nctxt = llog_get_context(mdc_obd, LLOG_CONFIG_REPL_CTXT);
468         rc = llog_create(nctxt, &llh, NULL, name);
469         if (rc) {
470                 CERROR("6: llog_create failed %d\n", rc);
471                 RETURN(rc);
472         }
473
474         rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
475         if (rc) {
476                 CERROR("6: llog_init_handle failed %d\n", rc);
477                 GOTO(parse_out, rc);
478         }
479
480         rc = llog_process(llh, (llog_cb_t)plain_print_cb, NULL);
481         if (rc)
482                 CERROR("6: llog_process failed %d\n", rc);
483
484 parse_out:
485         rc = llog_close(llh);
486         if (rc) {
487                 CERROR("6: llog_close failed: rc = %d\n", rc);
488         }
489
490         rc = obd_disconnect(exp, 0);
491
492         RETURN(rc);
493 }
494
495 /* -------------------------------------------------------------------------
496  * Tests above, boring obd functions below
497  * ------------------------------------------------------------------------- */
498 static int llog_run_tests(struct obd_device *obd)
499 {
500         struct llog_handle *llh;
501         struct obd_run_ctxt saved;
502         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_TEST_ORIG_CTXT);
503         int rc, err, cleanup_phase = 0;
504         char name[10];
505         ENTRY;
506
507         sprintf(name, "%x", llog_test_rand);
508         push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
509
510         rc = llog_test_1(obd, name);
511         if (rc)
512                 GOTO(cleanup, rc);
513
514         rc = llog_test_2(obd, name, &llh);
515         if (rc)
516                 GOTO(cleanup, rc);
517         cleanup_phase = 1; /* close llh */
518
519         rc = llog_test_3(obd, llh);
520         if (rc)
521                 GOTO(cleanup, rc);
522
523         rc = llog_test_4(obd);
524         if (rc)
525                 GOTO(cleanup, rc);
526
527         rc = llog_test_5(obd);
528         if (rc)
529                 GOTO(cleanup, rc);
530
531         rc = llog_test_6(obd, name);
532         if (rc)
533                 GOTO(cleanup, rc);
534
535  cleanup:
536         switch (cleanup_phase) {
537         case 1:
538                 err = llog_close(llh);
539                 if (err)
540                         CERROR("cleanup: llog_close failed: %d\n", err);
541                 if (!rc)
542                         rc = err;
543         case 0:
544                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
545         }
546
547         return rc;
548 }
549
550
551 static int llog_test_llog_init(struct obd_device *obd, struct obd_device *tgt,
552                                int count, struct llog_logid *logid)
553 {
554         int rc;
555         ENTRY;
556
557         rc = llog_setup(obd, LLOG_TEST_ORIG_CTXT, tgt, 0, NULL, &llog_lvfs_ops);
558         RETURN(rc);
559 }
560
561 static int llog_test_llog_finish(struct obd_device *obd, int count)
562 {
563         int rc;
564         ENTRY;
565
566         rc = llog_cleanup(llog_get_context(obd, LLOG_TEST_ORIG_CTXT));
567         RETURN(rc);
568 }
569
570 static int llog_test_cleanup(struct obd_device *obd, int flags)
571 {
572         int rc = obd_llog_finish(obd, 0);
573         if (rc)
574                 CERROR("failed to llog_test_llog_finish: %d\n", rc);
575
576         return rc;
577 }
578
579 static int llog_test_setup(struct obd_device *obd, obd_count len, void *buf)
580 {
581         struct lustre_cfg *lcfg = buf;
582         struct obd_device *tgt;
583         int rc;
584         ENTRY;
585
586         if (lcfg->lcfg_inllen1 < 1) {
587                 CERROR("requires a TARGET OBD name\n");
588                 RETURN(-EINVAL);
589         }
590
591         tgt = class_name2obd(lcfg->lcfg_inlbuf1);
592         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
593                 CERROR("target device not attached or not set up (%s)\n",
594                        lcfg->lcfg_inlbuf1);
595                 RETURN(-EINVAL);
596         }
597
598         rc = obd_llog_init(obd, tgt, 0, NULL);
599         if (rc)
600                 RETURN(rc);
601
602         llog_test_rand = ll_insecure_random_int();
603
604         rc = llog_run_tests(obd);
605         if (rc)
606                 llog_test_cleanup(obd, 0);
607         RETURN(rc);
608 }
609
610 static int llog_test_attach(struct obd_device *dev, obd_count len, void *data)
611 {
612         struct lprocfs_static_vars lvars;
613
614         lprocfs_init_vars(ost, &lvars);
615         return lprocfs_obd_attach(dev, lvars.obd_vars);
616 }
617
618 static int llog_test_detach(struct obd_device *dev)
619 {
620         return lprocfs_obd_detach(dev);
621 }
622
623 static struct obd_ops llog_obd_ops = {
624         o_owner:       THIS_MODULE,
625         o_attach:      llog_test_attach,
626         o_detach:      llog_test_detach,
627         o_setup:       llog_test_setup,
628         o_cleanup:     llog_test_cleanup,
629         o_llog_init:   llog_test_llog_init,
630         o_llog_finish: llog_test_llog_finish,
631
632 };
633
634 static int __init llog_test_init(void)
635 {
636         struct lprocfs_static_vars lvars;
637
638         lprocfs_init_multi_vars(0, &lvars);
639         return class_register_type(&llog_obd_ops,lvars.module_vars,"llog_test");
640 }
641
642 static void __exit llog_test_exit(void)
643 {
644         class_unregister_type("llog_test");
645 }
646
647 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
648 MODULE_DESCRIPTION("llog test module");
649 MODULE_LICENSE("GPL");
650
651 module_init(llog_test_init);
652 module_exit(llog_test_exit);