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) 2012, Intel Corporation.
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 * This takes extra reference on llog_handle via llog_handle_get() and require
152 * this reference to be put by caller using llog_handle_put()
154 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
155 struct llog_handle **res, struct llog_logid *logid)
157 struct llog_handle *loghandle;
162 if (cathandle == NULL)
165 down_write(&cathandle->lgh_lock);
166 cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
168 struct llog_logid *cgl = &loghandle->lgh_id;
170 if (cgl->lgl_oid == logid->lgl_oid) {
171 if (cgl->lgl_ogen != logid->lgl_ogen) {
172 CERROR("%s: log "LPX64" generation %x != %x\n",
173 loghandle->lgh_ctxt->loc_obd->obd_name,
174 logid->lgl_oid, cgl->lgl_ogen,
178 loghandle->u.phd.phd_cat_handle = cathandle;
179 up_write(&cathandle->lgh_lock);
183 up_write(&cathandle->lgh_lock);
185 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
188 CERROR("%s: error opening log id "LPX64":%x: rc = %d\n",
189 cathandle->lgh_ctxt->loc_obd->obd_name,
190 logid->lgl_oid, logid->lgl_ogen, rc);
194 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
196 llog_close(env, loghandle);
200 down_write(&cathandle->lgh_lock);
201 cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
202 up_write(&cathandle->lgh_lock);
204 loghandle->u.phd.phd_cat_handle = cathandle;
205 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
206 loghandle->u.phd.phd_cookie.lgc_index =
207 loghandle->lgh_hdr->llh_cat_idx;
210 llog_handle_get(loghandle);
215 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
217 struct llog_handle *loghandle, *n;
222 cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
224 struct llog_log_hdr *llh = loghandle->lgh_hdr;
227 /* unlink open-not-created llogs */
228 cfs_list_del_init(&loghandle->u.phd.phd_entry);
229 llh = loghandle->lgh_hdr;
230 if (loghandle->lgh_obj != NULL && llh != NULL &&
231 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
232 (llh->llh_count == 1)) {
233 rc = llog_destroy(env, loghandle);
235 CERROR("%s: failure destroying log during "
236 "cleanup: rc = %d\n",
237 loghandle->lgh_ctxt->loc_obd->obd_name,
240 index = loghandle->u.phd.phd_cookie.lgc_index;
241 llog_cat_cleanup(env, cathandle, NULL, index);
243 llog_close(env, loghandle);
245 /* if handle was stored in ctxt, remove it too */
246 if (cathandle->lgh_ctxt->loc_handle == cathandle)
247 cathandle->lgh_ctxt->loc_handle = NULL;
248 rc = llog_close(env, cathandle);
251 EXPORT_SYMBOL(llog_cat_close);
254 * lockdep markers for nested struct llog_handle::lgh_lock locking.
261 /** Return the currently active log handle. If the current log handle doesn't
262 * have enough space left for the current record, start a new one.
264 * If reclen is 0, we only want to know what the currently active log is,
265 * otherwise we get a lock on this log so nobody can steal our space.
267 * Assumes caller has already pushed us into the kernel context and is locking.
269 * NOTE: loghandle is write-locked upon successful return
271 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
274 struct llog_handle *loghandle = NULL;
277 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
278 loghandle = cathandle->u.chd.chd_current_log;
280 struct llog_log_hdr *llh;
282 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
283 llh = loghandle->lgh_hdr;
285 loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
286 up_read(&cathandle->lgh_lock);
289 up_write(&loghandle->lgh_lock);
292 up_read(&cathandle->lgh_lock);
294 /* time to use next log */
296 /* first, we have to make sure the state hasn't changed */
297 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
298 loghandle = cathandle->u.chd.chd_current_log;
300 struct llog_log_hdr *llh;
302 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
303 llh = loghandle->lgh_hdr;
305 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
306 up_write(&cathandle->lgh_lock);
309 up_write(&loghandle->lgh_lock);
313 CDEBUG(D_INODE, "use next log\n");
315 loghandle = cathandle->u.chd.chd_next_log;
316 cathandle->u.chd.chd_current_log = loghandle;
317 cathandle->u.chd.chd_next_log = NULL;
318 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
319 up_write(&cathandle->lgh_lock);
324 /* Add a single record to the recovery log(s) using a catalog
325 * Returns as llog_write_record
327 * Assumes caller has already pushed us into the kernel context.
329 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
330 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
331 void *buf, struct thandle *th)
333 struct llog_handle *loghandle;
337 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
338 loghandle = llog_cat_current_log(cathandle, th);
339 LASSERT(!IS_ERR(loghandle));
341 /* loghandle is already locked by llog_cat_current_log() for us */
342 if (!llog_exist(loghandle)) {
343 rc = llog_cat_new_log(env, cathandle, loghandle, th);
345 up_write(&loghandle->lgh_lock);
349 /* now let's try to add the record */
350 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1, th);
352 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
353 up_write(&loghandle->lgh_lock);
355 /* try to use next log */
356 loghandle = llog_cat_current_log(cathandle, th);
357 LASSERT(!IS_ERR(loghandle));
358 /* new llog can be created concurrently */
359 if (!llog_exist(loghandle)) {
360 rc = llog_cat_new_log(env, cathandle, loghandle, th);
362 up_write(&loghandle->lgh_lock);
366 /* now let's try to add the record */
367 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
370 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
371 up_write(&loghandle->lgh_lock);
376 EXPORT_SYMBOL(llog_cat_add_rec);
378 int llog_cat_declare_add_rec(const struct lu_env *env,
379 struct llog_handle *cathandle,
380 struct llog_rec_hdr *rec, struct thandle *th)
382 struct llog_handle *loghandle, *next;
387 if (cathandle->u.chd.chd_current_log == NULL) {
388 /* declare new plain llog */
389 down_write(&cathandle->lgh_lock);
390 if (cathandle->u.chd.chd_current_log == NULL) {
391 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
392 NULL, NULL, LLOG_OPEN_NEW);
394 cathandle->u.chd.chd_current_log = loghandle;
395 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
396 &cathandle->u.chd.chd_head);
399 up_write(&cathandle->lgh_lock);
400 } else if (cathandle->u.chd.chd_next_log == NULL) {
401 /* declare next plain llog */
402 down_write(&cathandle->lgh_lock);
403 if (cathandle->u.chd.chd_next_log == NULL) {
404 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
405 NULL, NULL, LLOG_OPEN_NEW);
407 cathandle->u.chd.chd_next_log = loghandle;
408 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
409 &cathandle->u.chd.chd_head);
412 up_write(&cathandle->lgh_lock);
417 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
418 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
422 llog_declare_write_rec(env, cathandle, NULL, -1, th);
424 /* declare records in the llogs */
425 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
430 next = cathandle->u.chd.chd_next_log;
432 if (!llog_exist(next)) {
433 rc = llog_declare_create(env, next, th);
434 llog_declare_write_rec(env, cathandle, NULL, -1, th);
436 llog_declare_write_rec(env, next, rec, -1, th);
441 EXPORT_SYMBOL(llog_cat_declare_add_rec);
443 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
444 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
447 struct llog_ctxt *ctxt;
448 struct dt_device *dt;
449 struct thandle *th = NULL;
452 ctxt = cathandle->lgh_ctxt;
454 LASSERT(ctxt->loc_exp);
456 if (cathandle->lgh_obj != NULL) {
457 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
460 th = dt_trans_create(env, dt);
464 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
468 rc = dt_trans_start_local(env, dt, th);
471 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
473 dt_trans_stop(env, dt, th);
474 } else { /* lvfs compat code */
475 LASSERT(cathandle->lgh_file != NULL);
476 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
478 rc = llog_cat_add_rec(env, cathandle, rec, reccookie,
483 EXPORT_SYMBOL(llog_cat_add);
485 /* For each cookie in the cookie array, we clear the log in-use bit and either:
486 * - the log is empty, so mark it free in the catalog header and delete it
487 * - the log is not empty, just write out the log header
489 * The cookies may be in different log files, so we need to get new logs
492 * Assumes caller has already pushed us into the kernel context.
494 int llog_cat_cancel_records(const struct lu_env *env,
495 struct llog_handle *cathandle, int count,
496 struct llog_cookie *cookies)
498 int i, index, rc = 0, failed = 0;
502 for (i = 0; i < count; i++, cookies++) {
503 struct llog_handle *loghandle;
504 struct llog_logid *lgl = &cookies->lgc_lgl;
507 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
509 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
510 cathandle->lgh_ctxt->loc_obd->obd_name,
516 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
517 if (lrc == 1) { /* log has been destroyed */
518 index = loghandle->u.phd.phd_cookie.lgc_index;
519 rc = llog_cat_cleanup(env, cathandle, loghandle,
521 } else if (lrc == -ENOENT) {
522 if (rc == 0) /* ENOENT shouldn't rewrite any error */
524 } else if (lrc < 0) {
528 llog_handle_put(loghandle);
531 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
532 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
537 EXPORT_SYMBOL(llog_cat_cancel_records);
539 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
540 struct llog_rec_hdr *rec, void *data)
542 struct llog_process_data *d = data;
543 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
544 struct llog_handle *llh;
548 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
549 CERROR("invalid record in catalog\n");
552 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
553 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
554 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
556 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
558 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
559 cat_llh->lgh_ctxt->loc_obd->obd_name,
560 lir->lid_id.lgl_oid, rc);
564 if (rec->lrh_index < d->lpd_startcat)
565 /* Skip processing of the logs until startcat */
568 if (d->lpd_startidx > 0) {
569 struct llog_process_cat_data cd;
571 cd.lpcd_first_idx = d->lpd_startidx;
572 cd.lpcd_last_idx = 0;
573 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
575 /* Continue processing the next log from idx 0 */
578 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
581 llog_handle_put(llh);
586 int llog_cat_process_or_fork(const struct lu_env *env,
587 struct llog_handle *cat_llh,
588 llog_cb_t cb, void *data, int startcat,
589 int startidx, bool fork)
591 struct llog_process_data d;
592 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
596 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
599 d.lpd_startcat = startcat;
600 d.lpd_startidx = startidx;
602 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
603 struct llog_process_cat_data cd;
605 CWARN("catlog "LPX64" crosses index zero\n",
606 cat_llh->lgh_id.lgl_oid);
608 cd.lpcd_first_idx = llh->llh_cat_idx;
609 cd.lpcd_last_idx = 0;
610 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
615 cd.lpcd_first_idx = 0;
616 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
617 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
620 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
626 EXPORT_SYMBOL(llog_cat_process_or_fork);
628 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
629 llog_cb_t cb, void *data, int startcat, int startidx)
631 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
634 EXPORT_SYMBOL(llog_cat_process);
637 int llog_cat_process_thread(void *data)
639 struct llog_process_cat_args *args = data;
640 struct llog_ctxt *ctxt = args->lpca_ctxt;
641 struct llog_handle *llh = NULL;
642 llog_cb_t cb = args->lpca_cb;
643 struct llog_thread_info *lgi;
648 cfs_daemonize_ctxt("ll_log_process");
650 rc = lu_env_init(&env, LCT_LOCAL);
653 lgi = llog_info(&env);
656 lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
657 rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
660 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
661 ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
662 lgi->lgi_logid.lgl_ogen, rc);
665 rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
667 CERROR("%s: llog_init_handle failed: rc = %d\n",
668 llh->lgh_ctxt->loc_obd->obd_name, rc);
669 GOTO(release_llh, rc);
673 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
674 if (rc != LLOG_PROC_BREAK && rc != 0)
675 CERROR("%s: llog_cat_process() failed: rc = %d\n",
676 llh->lgh_ctxt->loc_obd->obd_name, rc);
677 cb(&env, llh, NULL, NULL);
679 CWARN("No callback function for recovery\n");
683 * Make sure that all cached data is sent.
685 llog_sync(ctxt, NULL, 0);
686 GOTO(release_llh, rc);
688 rc = llog_cat_close(&env, llh);
690 CERROR("%s: llog_cat_close() failed: rc = %d\n",
691 llh->lgh_ctxt->loc_obd->obd_name, rc);
699 EXPORT_SYMBOL(llog_cat_process_thread);
702 static int llog_cat_reverse_process_cb(const struct lu_env *env,
703 struct llog_handle *cat_llh,
704 struct llog_rec_hdr *rec, void *data)
706 struct llog_process_data *d = data;
707 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
708 struct llog_handle *llh;
711 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
712 CERROR("invalid record in catalog\n");
715 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
716 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
717 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
719 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
721 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
722 cat_llh->lgh_ctxt->loc_obd->obd_name,
723 lir->lid_id.lgl_oid, rc);
727 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
728 llog_handle_put(llh);
732 int llog_cat_reverse_process(const struct lu_env *env,
733 struct llog_handle *cat_llh,
734 llog_cb_t cb, void *data)
736 struct llog_process_data d;
737 struct llog_process_cat_data cd;
738 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
742 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
746 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
747 CWARN("catalog "LPX64" crosses index zero\n",
748 cat_llh->lgh_id.lgl_oid);
750 cd.lpcd_first_idx = 0;
751 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
752 rc = llog_reverse_process(env, cat_llh,
753 llog_cat_reverse_process_cb,
758 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
759 cd.lpcd_last_idx = 0;
760 rc = llog_reverse_process(env, cat_llh,
761 llog_cat_reverse_process_cb,
764 rc = llog_reverse_process(env, cat_llh,
765 llog_cat_reverse_process_cb,
771 EXPORT_SYMBOL(llog_cat_reverse_process);
773 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
775 struct llog_log_hdr *llh = cathandle->lgh_hdr;
776 int i, bitmap_size, idx;
779 bitmap_size = LLOG_BITMAP_SIZE(llh);
780 if (llh->llh_cat_idx == (index - 1)) {
781 idx = llh->llh_cat_idx + 1;
782 llh->llh_cat_idx = idx;
783 if (idx == cathandle->lgh_last_idx)
785 for (i = (index + 1) % bitmap_size;
786 i != cathandle->lgh_last_idx;
787 i = (i + 1) % bitmap_size) {
788 if (!ext2_test_bit(i, llh->llh_bitmap)) {
789 idx = llh->llh_cat_idx + 1;
790 llh->llh_cat_idx = idx;
792 llh->llh_cat_idx = 0;
798 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
799 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
805 /* Cleanup deleted plain llog traces from catalog */
806 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
807 struct llog_handle *loghandle, int index)
812 if (loghandle != NULL) {
813 /* remove destroyed llog from catalog list and
814 * chd_current_log variable */
815 down_write(&cathandle->lgh_lock);
816 if (cathandle->u.chd.chd_current_log == loghandle)
817 cathandle->u.chd.chd_current_log = NULL;
818 cfs_list_del_init(&loghandle->u.phd.phd_entry);
819 up_write(&cathandle->lgh_lock);
820 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
821 /* llog was opened and keep in a list, close it now */
822 llog_close(env, loghandle);
824 /* remove plain llog entry from catalog by index */
825 llog_cat_set_first_idx(cathandle, index);
826 rc = llog_cancel_rec(env, cathandle, index);
828 CDEBUG(D_HA, "cancel plain log at index"
829 " %u of catalog "LPX64"\n",
830 index, cathandle->lgh_id.lgl_oid);
834 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
835 struct llog_rec_hdr *rec, void *data)
837 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
838 struct llog_handle *loghandle;
839 struct llog_log_hdr *llh;
844 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
845 CERROR("%s: invalid record in catalog\n",
846 loghandle->lgh_ctxt->loc_obd->obd_name);
849 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
850 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
851 rec->lrh_index, cathandle->lgh_id.lgl_oid);
853 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
855 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
856 cathandle->lgh_ctxt->loc_obd->obd_name,
857 lir->lid_id.lgl_oid, rc);
858 if (rc == -ENOENT || rc == -ESTALE) {
859 /* remove index from catalog */
860 llog_cat_cleanup(env, cathandle, NULL, rec->lrh_index);
865 llh = loghandle->lgh_hdr;
866 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
867 (llh->llh_count == 1)) {
868 rc = llog_destroy(env, loghandle);
870 CERROR("%s: fail to destroy empty log: rc = %d\n",
871 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
873 llog_cat_cleanup(env, cathandle, loghandle,
874 loghandle->u.phd.phd_cookie.lgc_index);
876 llog_handle_put(loghandle);
880 EXPORT_SYMBOL(cat_cancel_cb);
882 /* helper to initialize catalog llog and process it to cancel */
883 int llog_cat_init_and_process(const struct lu_env *env,
884 struct llog_handle *llh)
888 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
892 rc = llog_process_or_fork(env, llh, cat_cancel_cb, NULL, NULL, false);
894 CERROR("%s: llog_process() with cat_cancel_cb failed: rc = "
895 "%d\n", llh->lgh_ctxt->loc_obd->obd_name, rc);
898 EXPORT_SYMBOL(llog_cat_init_and_process);