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 cfs_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 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
114 LBUG(); /* should never happen */
116 cfs_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 cfs_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 cfs_up_write(&cathandle->lgh_lock);
180 cfs_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 cfs_down_write(&cathandle->lgh_lock);
198 cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
199 cfs_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 cfs_down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
282 loghandle = cathandle->u.chd.chd_current_log;
284 struct llog_log_hdr *llh;
286 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
287 llh = loghandle->lgh_hdr;
289 loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
290 cfs_up_read(&cathandle->lgh_lock);
293 cfs_up_write(&loghandle->lgh_lock);
296 cfs_up_read(&cathandle->lgh_lock);
298 /* time to use next log */
300 /* first, we have to make sure the state hasn't changed */
301 cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
302 loghandle = cathandle->u.chd.chd_current_log;
304 struct llog_log_hdr *llh;
306 cfs_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 cfs_up_write(&cathandle->lgh_lock);
313 cfs_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 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
323 cfs_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 cfs_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 cfs_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 cfs_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 cfs_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 cfs_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 cfs_up_write(&cathandle->lgh_lock);
404 } else if (cathandle->u.chd.chd_next_log == NULL) {
405 /* declare next plain llog */
406 cfs_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 cfs_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("Cannot find log "LPX64"\n", lgl->lgl_oid);
517 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
518 if (lrc == 1) { /* log has been destroyed */
519 index = loghandle->u.phd.phd_cookie.lgc_index;
520 cfs_down_write(&cathandle->lgh_lock);
521 if (cathandle->u.chd.chd_current_log == loghandle)
522 cathandle->u.chd.chd_current_log = NULL;
523 cfs_up_write(&cathandle->lgh_lock);
524 llog_close(env, loghandle);
527 llog_cat_set_first_idx(cathandle, index);
528 lrc = llog_cancel_rec(env, cathandle, index);
530 CDEBUG(D_RPCTRACE, "cancel plain log at index"
531 " %u of catalog "LPX64"\n",
532 index, cathandle->lgh_id.lgl_oid);
533 } else if (lrc == -ENOENT) {
534 if (rc == 0) /* ENOENT shouldn't rewrite any error */
536 } else if (lrc < 0) {
542 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
543 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
548 EXPORT_SYMBOL(llog_cat_cancel_records);
550 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
551 struct llog_rec_hdr *rec, void *data)
553 struct llog_process_data *d = data;
554 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
555 struct llog_handle *llh;
559 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
560 CERROR("invalid record in catalog\n");
563 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
564 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
565 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
567 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
569 CERROR("Cannot find handle for log "LPX64"\n",
570 lir->lid_id.lgl_oid);
574 if (rec->lrh_index < d->lpd_startcat)
575 /* Skip processing of the logs until startcat */
578 if (d->lpd_startidx > 0) {
579 struct llog_process_cat_data cd;
581 cd.lpcd_first_idx = d->lpd_startidx;
582 cd.lpcd_last_idx = 0;
583 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
585 /* Continue processing the next log from idx 0 */
588 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
595 int llog_cat_process_or_fork(const struct lu_env *env,
596 struct llog_handle *cat_llh,
597 llog_cb_t cb, void *data, int startcat,
598 int startidx, bool fork)
600 struct llog_process_data d;
601 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
605 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
608 d.lpd_startcat = startcat;
609 d.lpd_startidx = startidx;
611 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
612 struct llog_process_cat_data cd;
614 CWARN("catlog "LPX64" crosses index zero\n",
615 cat_llh->lgh_id.lgl_oid);
617 cd.lpcd_first_idx = llh->llh_cat_idx;
618 cd.lpcd_last_idx = 0;
619 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
624 cd.lpcd_first_idx = 0;
625 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
626 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
629 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
635 EXPORT_SYMBOL(llog_cat_process_or_fork);
637 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
638 llog_cb_t cb, void *data, int startcat, int startidx)
640 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
643 EXPORT_SYMBOL(llog_cat_process);
646 int llog_cat_process_thread(void *data)
648 struct llog_process_cat_args *args = data;
649 struct llog_ctxt *ctxt = args->lpca_ctxt;
650 struct llog_handle *llh = NULL;
651 llog_cb_t cb = args->lpca_cb;
652 struct llog_thread_info *lgi;
657 cfs_daemonize_ctxt("ll_log_process");
659 rc = lu_env_init(&env, LCT_LOCAL);
662 lgi = llog_info(&env);
665 lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
666 rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
669 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
670 ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
671 lgi->lgi_logid.lgl_ogen, rc);
674 rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
676 CERROR("%s: llog_init_handle failed: rc = %d\n",
677 llh->lgh_ctxt->loc_obd->obd_name, rc);
678 GOTO(release_llh, rc);
682 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
683 if (rc != LLOG_PROC_BREAK && rc != 0)
684 CERROR("%s: llog_cat_process() failed: rc = %d\n",
685 llh->lgh_ctxt->loc_obd->obd_name, rc);
686 cb(&env, llh, NULL, NULL);
688 CWARN("No callback function for recovery\n");
692 * Make sure that all cached data is sent.
694 llog_sync(ctxt, NULL, 0);
695 GOTO(release_llh, rc);
697 rc = llog_cat_close(&env, llh);
699 CERROR("%s: llog_cat_close() failed: rc = %d\n",
700 llh->lgh_ctxt->loc_obd->obd_name, rc);
708 EXPORT_SYMBOL(llog_cat_process_thread);
711 static int llog_cat_reverse_process_cb(const struct lu_env *env,
712 struct llog_handle *cat_llh,
713 struct llog_rec_hdr *rec, void *data)
715 struct llog_process_data *d = data;
716 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
717 struct llog_handle *llh;
720 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
721 CERROR("invalid record in catalog\n");
724 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
725 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
726 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
728 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
730 CERROR("Cannot find handle for log "LPX64"\n",
731 lir->lid_id.lgl_oid);
735 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
739 int llog_cat_reverse_process(const struct lu_env *env,
740 struct llog_handle *cat_llh,
741 llog_cb_t cb, void *data)
743 struct llog_process_data d;
744 struct llog_process_cat_data cd;
745 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
749 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
753 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
754 CWARN("catalog "LPX64" crosses index zero\n",
755 cat_llh->lgh_id.lgl_oid);
757 cd.lpcd_first_idx = 0;
758 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
759 rc = llog_reverse_process(env, cat_llh,
760 llog_cat_reverse_process_cb,
765 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
766 cd.lpcd_last_idx = 0;
767 rc = llog_reverse_process(env, cat_llh,
768 llog_cat_reverse_process_cb,
771 rc = llog_reverse_process(env, cat_llh,
772 llog_cat_reverse_process_cb,
778 EXPORT_SYMBOL(llog_cat_reverse_process);
780 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
782 struct llog_log_hdr *llh = cathandle->lgh_hdr;
783 int i, bitmap_size, idx;
786 bitmap_size = LLOG_BITMAP_SIZE(llh);
787 if (llh->llh_cat_idx == (index - 1)) {
788 idx = llh->llh_cat_idx + 1;
789 llh->llh_cat_idx = idx;
790 if (idx == cathandle->lgh_last_idx)
792 for (i = (index + 1) % bitmap_size;
793 i != cathandle->lgh_last_idx;
794 i = (i + 1) % bitmap_size) {
795 if (!ext2_test_bit(i, llh->llh_bitmap)) {
796 idx = llh->llh_cat_idx + 1;
797 llh->llh_cat_idx = idx;
799 llh->llh_cat_idx = 0;
805 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
806 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
812 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
813 struct llog_rec_hdr *rec, void *data)
815 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
816 struct llog_handle *loghandle;
817 struct llog_log_hdr *llh;
822 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
823 CERROR("%s: invalid record in catalog\n",
824 loghandle->lgh_ctxt->loc_obd->obd_name);
827 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
828 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
829 rec->lrh_index, cathandle->lgh_id.lgl_oid);
831 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
833 CERROR("%s: cannot find handle for llog "LPX64"\n: %d",
834 loghandle->lgh_ctxt->loc_obd->obd_name,
835 lir->lid_id.lgl_oid, rc);
836 if (rc == -ENOENT || rc == -ESTALE) {
837 index = rec->lrh_index;
843 llh = loghandle->lgh_hdr;
844 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
845 (llh->llh_count == 1)) {
846 rc = llog_destroy(env, loghandle);
848 CERROR("%s: fail to destroy empty log: rc = %d\n",
849 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
851 index = loghandle->u.phd.phd_cookie.lgc_index;
852 llog_close(env, loghandle);
856 llog_cat_set_first_idx(cathandle, index);
857 rc = llog_cancel_rec(env, cathandle, index);
860 "cancel log "LPX64":%x at index %u of catalog "
861 LPX64"\n", lir->lid_id.lgl_oid,
862 lir->lid_id.lgl_ogen, rec->lrh_index,
863 cathandle->lgh_id.lgl_oid);
868 EXPORT_SYMBOL(cat_cancel_cb);
870 /* helper to initialize catalog llog and process it to cancel */
871 int llog_cat_init_and_process(const struct lu_env *env,
872 struct llog_handle *llh)
876 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
880 rc = llog_process(env, llh, cat_cancel_cb, NULL, NULL);
882 CERROR("%s: llog_process() with cat_cancel_cb failed: rc = "
883 "%d\n", llh->lgh_ctxt->loc_obd->obd_name, rc);
886 EXPORT_SYMBOL(llog_cat_init_and_process);