4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog_cat.c
38 * OST<->MDS recovery logging infrastructure.
40 * Invariants in implementation:
41 * - we do not share logs among different OST<->MDS connections, so that
42 * if an OST or MDS fails it need only look at log(s) relevant to itself
44 * Author: Andreas Dilger <adilger@clusterfs.com>
45 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
46 * Author: Mikhail Pershin <mike.pershin@intel.com>
49 #define DEBUG_SUBSYSTEM S_LOG
52 #include <liblustre.h>
55 #include <obd_class.h>
57 #include "llog_internal.h"
59 /* Create a new log handle and add it to the open list.
60 * This log handle will be closed when all of the records in it are removed.
62 * Assumes caller has already pushed us into the kernel context and is locking.
64 static int llog_cat_new_log(const struct lu_env *env,
65 struct llog_handle *cathandle,
66 struct llog_handle *loghandle,
70 struct llog_log_hdr *llh;
71 struct llog_logid_rec rec = { { 0 }, };
72 int rc, index, bitmap_size;
75 llh = cathandle->lgh_hdr;
76 bitmap_size = LLOG_BITMAP_SIZE(llh);
78 index = (cathandle->lgh_last_idx + 1) % bitmap_size;
80 /* maximum number of available slots in catlog is bitmap_size - 2 */
81 if (llh->llh_cat_idx == index) {
82 CERROR("no free catalog slots for log...\n");
86 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
89 rc = llog_create(env, loghandle, th);
90 /* if llog is already created, no need to initialize it */
94 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
95 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
99 rc = llog_init_handle(env, loghandle,
100 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
101 &cathandle->lgh_hdr->llh_tgtuuid);
103 GOTO(out_destroy, rc);
108 spin_lock(&loghandle->lgh_hdr_lock);
110 if (ext2_set_bit(index, llh->llh_bitmap)) {
111 CERROR("argh, index %u already set in log bitmap?\n",
113 spin_unlock(&loghandle->lgh_hdr_lock);
114 LBUG(); /* should never happen */
116 spin_unlock(&loghandle->lgh_hdr_lock);
118 cathandle->lgh_last_idx = index;
119 llh->llh_tail.lrt_index = index;
121 CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
122 LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
123 index, cathandle->lgh_id.lgl_oid);
124 /* build the record for this log in the catalog */
125 rec.lid_hdr.lrh_len = sizeof(rec);
126 rec.lid_hdr.lrh_index = index;
127 rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
128 rec.lid_id = loghandle->lgh_id;
129 rec.lid_tail.lrt_len = sizeof(rec);
130 rec.lid_tail.lrt_index = index;
132 /* update the catalog: header and record */
133 rc = llog_write_rec(env, cathandle, &rec.lid_hdr,
134 &loghandle->u.phd.phd_cookie, 1, NULL, index, th);
136 GOTO(out_destroy, rc);
138 loghandle->lgh_hdr->llh_cat_idx = index;
141 llog_destroy(env, loghandle);
145 /* Open an existent log handle and add it to the open list.
146 * This log handle will be closed when all of the records in it are removed.
148 * Assumes caller has already pushed us into the kernel context and is locking.
149 * We return a lock on the handle to ensure nobody yanks it from us.
151 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
152 struct llog_handle **res, struct llog_logid *logid)
154 struct llog_handle *loghandle;
159 if (cathandle == NULL)
162 down_write(&cathandle->lgh_lock);
163 cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
165 struct llog_logid *cgl = &loghandle->lgh_id;
167 if (cgl->lgl_oid == logid->lgl_oid) {
168 if (cgl->lgl_ogen != logid->lgl_ogen) {
169 CERROR("%s: log "LPX64" generation %x != %x\n",
170 loghandle->lgh_ctxt->loc_obd->obd_name,
171 logid->lgl_oid, cgl->lgl_ogen,
175 loghandle->u.phd.phd_cat_handle = cathandle;
176 up_write(&cathandle->lgh_lock);
180 up_write(&cathandle->lgh_lock);
182 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
185 CERROR("%s: error opening log id "LPX64":%x: rc = %d\n",
186 cathandle->lgh_ctxt->loc_obd->obd_name,
187 logid->lgl_oid, logid->lgl_ogen, rc);
191 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
193 llog_close(env, loghandle);
197 down_write(&cathandle->lgh_lock);
198 cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
199 up_write(&cathandle->lgh_lock);
201 loghandle->u.phd.phd_cat_handle = cathandle;
202 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
203 loghandle->u.phd.phd_cookie.lgc_index =
204 loghandle->lgh_hdr->llh_cat_idx;
211 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
213 struct llog_handle *loghandle, *n;
218 cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
220 struct llog_log_hdr *llh = loghandle->lgh_hdr;
223 /* unlink open-not-created llogs */
224 cfs_list_del_init(&loghandle->u.phd.phd_entry);
225 llh = loghandle->lgh_hdr;
226 if (loghandle->lgh_obj != NULL && llh != NULL &&
227 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
228 (llh->llh_count == 1)) {
229 rc = llog_destroy(env, loghandle);
231 CERROR("%s: failure destroying log during "
232 "cleanup: rc = %d\n",
233 loghandle->lgh_ctxt->loc_obd->obd_name,
236 index = loghandle->u.phd.phd_cookie.lgc_index;
239 llog_cat_set_first_idx(cathandle, index);
240 rc = llog_cancel_rec(env, cathandle, index);
243 "cancel plain log at index %u of "
245 index, cathandle->lgh_id.lgl_oid);
247 llog_close(env, loghandle);
249 /* if handle was stored in ctxt, remove it too */
250 if (cathandle->lgh_ctxt->loc_handle == cathandle)
251 cathandle->lgh_ctxt->loc_handle = NULL;
252 rc = llog_close(env, cathandle);
255 EXPORT_SYMBOL(llog_cat_close);
258 * lockdep markers for nested struct llog_handle::lgh_lock locking.
265 /** Return the currently active log handle. If the current log handle doesn't
266 * have enough space left for the current record, start a new one.
268 * If reclen is 0, we only want to know what the currently active log is,
269 * otherwise we get a lock on this log so nobody can steal our space.
271 * Assumes caller has already pushed us into the kernel context and is locking.
273 * NOTE: loghandle is write-locked upon successful return
275 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
278 struct llog_handle *loghandle = NULL;
281 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
282 loghandle = cathandle->u.chd.chd_current_log;
284 struct llog_log_hdr *llh;
286 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
287 llh = loghandle->lgh_hdr;
289 loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
290 up_read(&cathandle->lgh_lock);
293 up_write(&loghandle->lgh_lock);
296 up_read(&cathandle->lgh_lock);
298 /* time to use next log */
300 /* first, we have to make sure the state hasn't changed */
301 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
302 loghandle = cathandle->u.chd.chd_current_log;
304 struct llog_log_hdr *llh;
306 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
307 llh = loghandle->lgh_hdr;
309 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
310 up_write(&cathandle->lgh_lock);
313 up_write(&loghandle->lgh_lock);
317 CDEBUG(D_INODE, "use next log\n");
319 loghandle = cathandle->u.chd.chd_next_log;
320 cathandle->u.chd.chd_current_log = loghandle;
321 cathandle->u.chd.chd_next_log = NULL;
322 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
323 up_write(&cathandle->lgh_lock);
328 /* Add a single record to the recovery log(s) using a catalog
329 * Returns as llog_write_record
331 * Assumes caller has already pushed us into the kernel context.
333 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
334 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
335 void *buf, struct thandle *th)
337 struct llog_handle *loghandle;
341 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
342 loghandle = llog_cat_current_log(cathandle, th);
343 LASSERT(!IS_ERR(loghandle));
345 /* loghandle is already locked by llog_cat_current_log() for us */
346 if (!llog_exist(loghandle)) {
347 rc = llog_cat_new_log(env, cathandle, loghandle, th);
349 up_write(&loghandle->lgh_lock);
353 /* now let's try to add the record */
354 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1, th);
356 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
357 up_write(&loghandle->lgh_lock);
359 /* try to use next log */
360 loghandle = llog_cat_current_log(cathandle, th);
361 LASSERT(!IS_ERR(loghandle));
362 /* new llog can be created concurrently */
363 if (!llog_exist(loghandle)) {
364 rc = llog_cat_new_log(env, cathandle, loghandle, th);
366 up_write(&loghandle->lgh_lock);
370 /* now let's try to add the record */
371 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
374 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
375 up_write(&loghandle->lgh_lock);
380 EXPORT_SYMBOL(llog_cat_add_rec);
382 int llog_cat_declare_add_rec(const struct lu_env *env,
383 struct llog_handle *cathandle,
384 struct llog_rec_hdr *rec, struct thandle *th)
386 struct llog_handle *loghandle, *next;
391 if (cathandle->u.chd.chd_current_log == NULL) {
392 /* declare new plain llog */
393 down_write(&cathandle->lgh_lock);
394 if (cathandle->u.chd.chd_current_log == NULL) {
395 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
396 NULL, NULL, LLOG_OPEN_NEW);
398 cathandle->u.chd.chd_current_log = loghandle;
399 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
400 &cathandle->u.chd.chd_head);
403 up_write(&cathandle->lgh_lock);
404 } else if (cathandle->u.chd.chd_next_log == NULL) {
405 /* declare next plain llog */
406 down_write(&cathandle->lgh_lock);
407 if (cathandle->u.chd.chd_next_log == NULL) {
408 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
409 NULL, NULL, LLOG_OPEN_NEW);
411 cathandle->u.chd.chd_next_log = loghandle;
412 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
413 &cathandle->u.chd.chd_head);
416 up_write(&cathandle->lgh_lock);
421 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
422 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
426 llog_declare_write_rec(env, cathandle, NULL, -1, th);
428 /* declare records in the llogs */
429 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
434 next = cathandle->u.chd.chd_next_log;
436 if (!llog_exist(next)) {
437 rc = llog_declare_create(env, next, th);
438 llog_declare_write_rec(env, cathandle, NULL, -1, th);
440 llog_declare_write_rec(env, next, rec, -1, th);
445 EXPORT_SYMBOL(llog_cat_declare_add_rec);
447 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
448 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
451 struct llog_ctxt *ctxt;
452 struct dt_device *dt;
453 struct thandle *th = NULL;
456 ctxt = cathandle->lgh_ctxt;
458 LASSERT(ctxt->loc_exp);
460 if (cathandle->lgh_obj != NULL) {
461 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
464 th = dt_trans_create(env, dt);
468 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
472 rc = dt_trans_start_local(env, dt, th);
475 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
477 dt_trans_stop(env, dt, th);
478 } else { /* lvfs compat code */
479 LASSERT(cathandle->lgh_file != NULL);
480 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
482 rc = llog_cat_add_rec(env, cathandle, rec, reccookie,
487 EXPORT_SYMBOL(llog_cat_add);
489 /* For each cookie in the cookie array, we clear the log in-use bit and either:
490 * - the log is empty, so mark it free in the catalog header and delete it
491 * - the log is not empty, just write out the log header
493 * The cookies may be in different log files, so we need to get new logs
496 * Assumes caller has already pushed us into the kernel context.
498 int llog_cat_cancel_records(const struct lu_env *env,
499 struct llog_handle *cathandle, int count,
500 struct llog_cookie *cookies)
502 int i, index, rc = 0, failed = 0;
506 for (i = 0; i < count; i++, cookies++) {
507 struct llog_handle *loghandle;
508 struct llog_logid *lgl = &cookies->lgc_lgl;
511 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
513 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
514 cathandle->lgh_ctxt->loc_obd->obd_name,
519 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
520 if (lrc == 1) { /* log has been destroyed */
521 index = loghandle->u.phd.phd_cookie.lgc_index;
522 down_write(&cathandle->lgh_lock);
523 if (cathandle->u.chd.chd_current_log == loghandle)
524 cathandle->u.chd.chd_current_log = NULL;
525 up_write(&cathandle->lgh_lock);
526 llog_close(env, loghandle);
529 llog_cat_set_first_idx(cathandle, index);
530 lrc = llog_cancel_rec(env, cathandle, index);
532 CDEBUG(D_RPCTRACE, "cancel plain log at index"
533 " %u of catalog "LPX64"\n",
534 index, cathandle->lgh_id.lgl_oid);
535 } else if (lrc == -ENOENT) {
536 if (rc == 0) /* ENOENT shouldn't rewrite any error */
538 } else if (lrc < 0) {
544 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
545 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
550 EXPORT_SYMBOL(llog_cat_cancel_records);
552 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
553 struct llog_rec_hdr *rec, void *data)
555 struct llog_process_data *d = data;
556 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
557 struct llog_handle *llh;
561 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
562 CERROR("invalid record in catalog\n");
565 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
566 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
567 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
569 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
571 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
572 cat_llh->lgh_ctxt->loc_obd->obd_name,
573 lir->lid_id.lgl_oid, rc);
577 if (rec->lrh_index < d->lpd_startcat)
578 /* Skip processing of the logs until startcat */
581 if (d->lpd_startidx > 0) {
582 struct llog_process_cat_data cd;
584 cd.lpcd_first_idx = d->lpd_startidx;
585 cd.lpcd_last_idx = 0;
586 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
588 /* Continue processing the next log from idx 0 */
591 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
598 int llog_cat_process_or_fork(const struct lu_env *env,
599 struct llog_handle *cat_llh,
600 llog_cb_t cb, void *data, int startcat,
601 int startidx, bool fork)
603 struct llog_process_data d;
604 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
608 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
611 d.lpd_startcat = startcat;
612 d.lpd_startidx = startidx;
614 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
615 struct llog_process_cat_data cd;
617 CWARN("catlog "LPX64" crosses index zero\n",
618 cat_llh->lgh_id.lgl_oid);
620 cd.lpcd_first_idx = llh->llh_cat_idx;
621 cd.lpcd_last_idx = 0;
622 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
627 cd.lpcd_first_idx = 0;
628 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
629 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
632 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
638 EXPORT_SYMBOL(llog_cat_process_or_fork);
640 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
641 llog_cb_t cb, void *data, int startcat, int startidx)
643 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
646 EXPORT_SYMBOL(llog_cat_process);
649 int llog_cat_process_thread(void *data)
651 struct llog_process_cat_args *args = data;
652 struct llog_ctxt *ctxt = args->lpca_ctxt;
653 struct llog_handle *llh = NULL;
654 llog_cb_t cb = args->lpca_cb;
655 struct llog_thread_info *lgi;
660 cfs_daemonize_ctxt("ll_log_process");
662 rc = lu_env_init(&env, LCT_LOCAL);
665 lgi = llog_info(&env);
668 lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
669 rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
672 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
673 ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
674 lgi->lgi_logid.lgl_ogen, rc);
677 rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
679 CERROR("%s: llog_init_handle failed: rc = %d\n",
680 llh->lgh_ctxt->loc_obd->obd_name, rc);
681 GOTO(release_llh, rc);
685 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
686 if (rc != LLOG_PROC_BREAK && rc != 0)
687 CERROR("%s: llog_cat_process() failed: rc = %d\n",
688 llh->lgh_ctxt->loc_obd->obd_name, rc);
689 cb(&env, llh, NULL, NULL);
691 CWARN("No callback function for recovery\n");
695 * Make sure that all cached data is sent.
697 llog_sync(ctxt, NULL, 0);
698 GOTO(release_llh, rc);
700 rc = llog_cat_close(&env, llh);
702 CERROR("%s: llog_cat_close() failed: rc = %d\n",
703 llh->lgh_ctxt->loc_obd->obd_name, rc);
711 EXPORT_SYMBOL(llog_cat_process_thread);
714 static int llog_cat_reverse_process_cb(const struct lu_env *env,
715 struct llog_handle *cat_llh,
716 struct llog_rec_hdr *rec, void *data)
718 struct llog_process_data *d = data;
719 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
720 struct llog_handle *llh;
723 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
724 CERROR("invalid record in catalog\n");
727 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
728 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
729 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
731 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
733 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
734 cat_llh->lgh_ctxt->loc_obd->obd_name,
735 lir->lid_id.lgl_oid, rc);
739 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
743 int llog_cat_reverse_process(const struct lu_env *env,
744 struct llog_handle *cat_llh,
745 llog_cb_t cb, void *data)
747 struct llog_process_data d;
748 struct llog_process_cat_data cd;
749 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
753 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
757 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
758 CWARN("catalog "LPX64" crosses index zero\n",
759 cat_llh->lgh_id.lgl_oid);
761 cd.lpcd_first_idx = 0;
762 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
763 rc = llog_reverse_process(env, cat_llh,
764 llog_cat_reverse_process_cb,
769 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
770 cd.lpcd_last_idx = 0;
771 rc = llog_reverse_process(env, cat_llh,
772 llog_cat_reverse_process_cb,
775 rc = llog_reverse_process(env, cat_llh,
776 llog_cat_reverse_process_cb,
782 EXPORT_SYMBOL(llog_cat_reverse_process);
784 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
786 struct llog_log_hdr *llh = cathandle->lgh_hdr;
787 int i, bitmap_size, idx;
790 bitmap_size = LLOG_BITMAP_SIZE(llh);
791 if (llh->llh_cat_idx == (index - 1)) {
792 idx = llh->llh_cat_idx + 1;
793 llh->llh_cat_idx = idx;
794 if (idx == cathandle->lgh_last_idx)
796 for (i = (index + 1) % bitmap_size;
797 i != cathandle->lgh_last_idx;
798 i = (i + 1) % bitmap_size) {
799 if (!ext2_test_bit(i, llh->llh_bitmap)) {
800 idx = llh->llh_cat_idx + 1;
801 llh->llh_cat_idx = idx;
803 llh->llh_cat_idx = 0;
809 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
810 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
816 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
817 struct llog_rec_hdr *rec, void *data)
819 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
820 struct llog_handle *loghandle;
821 struct llog_log_hdr *llh;
826 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
827 CERROR("%s: invalid record in catalog\n",
828 loghandle->lgh_ctxt->loc_obd->obd_name);
831 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
832 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
833 rec->lrh_index, cathandle->lgh_id.lgl_oid);
835 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
837 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
838 cathandle->lgh_ctxt->loc_obd->obd_name,
839 lir->lid_id.lgl_oid, rc);
840 if (rc == -ENOENT || rc == -ESTALE) {
841 index = rec->lrh_index;
847 llh = loghandle->lgh_hdr;
848 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
849 (llh->llh_count == 1)) {
850 rc = llog_destroy(env, loghandle);
852 CERROR("%s: fail to destroy empty log: rc = %d\n",
853 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
855 index = loghandle->u.phd.phd_cookie.lgc_index;
856 llog_close(env, loghandle);
860 llog_cat_set_first_idx(cathandle, index);
861 rc = llog_cancel_rec(env, cathandle, index);
864 "cancel log "LPX64":%x at index %u of catalog "
865 LPX64"\n", lir->lid_id.lgl_oid,
866 lir->lid_id.lgl_ogen, rec->lrh_index,
867 cathandle->lgh_id.lgl_oid);
872 EXPORT_SYMBOL(cat_cancel_cb);
874 /* helper to initialize catalog llog and process it to cancel */
875 int llog_cat_init_and_process(const struct lu_env *env,
876 struct llog_handle *llh)
880 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
884 rc = llog_process(env, llh, cat_cancel_cb, NULL, NULL);
886 CERROR("%s: llog_process() with cat_cancel_cb failed: rc = "
887 "%d\n", llh->lgh_ctxt->loc_obd->obd_name, rc);
890 EXPORT_SYMBOL(llog_cat_init_and_process);