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, 2013, 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 <obd_class.h>
54 #include "llog_internal.h"
56 /* Create a new log handle and add it to the open list.
57 * This log handle will be closed when all of the records in it are removed.
59 * Assumes caller has already pushed us into the kernel context and is locking.
61 static int llog_cat_new_log(const struct lu_env *env,
62 struct llog_handle *cathandle,
63 struct llog_handle *loghandle,
66 struct llog_thread_info *lgi = llog_info(env);
67 struct llog_logid_rec *rec = &lgi->lgi_logid;
72 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
75 rc = llog_create(env, loghandle, th);
76 /* if llog is already created, no need to initialize it */
80 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
81 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
85 rc = llog_init_handle(env, loghandle,
86 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
87 &cathandle->lgh_hdr->llh_tgtuuid);
89 GOTO(out_destroy, rc);
91 /* build the record for this log in the catalog */
92 rec->lid_hdr.lrh_len = sizeof(*rec);
93 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
94 rec->lid_id = loghandle->lgh_id;
96 /* append the new record into catalog. The new index will be
97 * assigned to the record and updated in rec header */
98 rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
99 &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
101 GOTO(out_destroy, rc);
103 CDEBUG(D_OTHER, "new recovery log "DOSTID":%x for index %u of catalog"
104 DOSTID"\n", POSTID(&loghandle->lgh_id.lgl_oi),
105 loghandle->lgh_id.lgl_ogen, rec->lid_hdr.lrh_index,
106 POSTID(&cathandle->lgh_id.lgl_oi));
108 loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
111 llog_destroy(env, loghandle);
115 /* Open an existent log handle and add it to the open list.
116 * This log handle will be closed when all of the records in it are removed.
118 * Assumes caller has already pushed us into the kernel context and is locking.
119 * We return a lock on the handle to ensure nobody yanks it from us.
121 * This takes extra reference on llog_handle via llog_handle_get() and require
122 * this reference to be put by caller using llog_handle_put()
124 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
125 struct llog_handle **res, struct llog_logid *logid)
127 struct llog_handle *loghandle;
133 if (cathandle == NULL)
136 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
137 down_write(&cathandle->lgh_lock);
138 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
140 struct llog_logid *cgl = &loghandle->lgh_id;
142 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
143 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
144 if (cgl->lgl_ogen != logid->lgl_ogen) {
145 CERROR("%s: log "DOSTID" generation %x != %x\n",
146 loghandle->lgh_ctxt->loc_obd->obd_name,
147 POSTID(&logid->lgl_oi), cgl->lgl_ogen,
151 loghandle->u.phd.phd_cat_handle = cathandle;
152 up_write(&cathandle->lgh_lock);
156 up_write(&cathandle->lgh_lock);
158 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
161 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
162 cathandle->lgh_ctxt->loc_obd->obd_name,
163 POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
167 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
169 llog_close(env, loghandle);
174 down_write(&cathandle->lgh_lock);
175 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
176 up_write(&cathandle->lgh_lock);
178 loghandle->u.phd.phd_cat_handle = cathandle;
179 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
180 loghandle->u.phd.phd_cookie.lgc_index =
181 loghandle->lgh_hdr->llh_cat_idx;
184 llog_handle_get(loghandle);
189 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
191 struct llog_handle *loghandle, *n;
196 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
198 struct llog_log_hdr *llh = loghandle->lgh_hdr;
201 /* unlink open-not-created llogs */
202 list_del_init(&loghandle->u.phd.phd_entry);
203 llh = loghandle->lgh_hdr;
204 if (loghandle->lgh_obj != NULL && llh != NULL &&
205 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
206 (llh->llh_count == 1)) {
207 rc = llog_destroy(env, loghandle);
209 CERROR("%s: failure destroying log during "
210 "cleanup: rc = %d\n",
211 loghandle->lgh_ctxt->loc_obd->obd_name,
214 index = loghandle->u.phd.phd_cookie.lgc_index;
215 llog_cat_cleanup(env, cathandle, NULL, index);
217 llog_close(env, loghandle);
219 /* if handle was stored in ctxt, remove it too */
220 if (cathandle->lgh_ctxt->loc_handle == cathandle)
221 cathandle->lgh_ctxt->loc_handle = NULL;
222 rc = llog_close(env, cathandle);
225 EXPORT_SYMBOL(llog_cat_close);
228 * lockdep markers for nested struct llog_handle::lgh_lock locking.
235 /** Return the currently active log handle. If the current log handle doesn't
236 * have enough space left for the current record, start a new one.
238 * If reclen is 0, we only want to know what the currently active log is,
239 * otherwise we get a lock on this log so nobody can steal our space.
241 * Assumes caller has already pushed us into the kernel context and is locking.
243 * NOTE: loghandle is write-locked upon successful return
245 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
248 struct llog_handle *loghandle = NULL;
251 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
252 loghandle = cathandle->u.chd.chd_current_log;
254 struct llog_log_hdr *llh;
256 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
257 llh = loghandle->lgh_hdr;
259 loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
260 up_read(&cathandle->lgh_lock);
263 up_write(&loghandle->lgh_lock);
266 up_read(&cathandle->lgh_lock);
268 /* time to use next log */
270 /* first, we have to make sure the state hasn't changed */
271 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
272 loghandle = cathandle->u.chd.chd_current_log;
274 struct llog_log_hdr *llh;
276 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
277 llh = loghandle->lgh_hdr;
279 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
280 up_write(&cathandle->lgh_lock);
283 up_write(&loghandle->lgh_lock);
287 CDEBUG(D_INODE, "use next log\n");
289 loghandle = cathandle->u.chd.chd_next_log;
290 cathandle->u.chd.chd_current_log = loghandle;
291 cathandle->u.chd.chd_next_log = NULL;
292 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
293 up_write(&cathandle->lgh_lock);
298 /* Add a single record to the recovery log(s) using a catalog
299 * Returns as llog_write_record
301 * Assumes caller has already pushed us into the kernel context.
303 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
304 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
307 struct llog_handle *loghandle;
311 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
312 loghandle = llog_cat_current_log(cathandle, th);
313 LASSERT(!IS_ERR(loghandle));
315 /* loghandle is already locked by llog_cat_current_log() for us */
316 if (!llog_exist(loghandle)) {
317 rc = llog_cat_new_log(env, cathandle, loghandle, th);
319 up_write(&loghandle->lgh_lock);
323 /* now let's try to add the record */
324 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
326 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
327 "llog_write_rec %d: lh=%p\n", rc, loghandle);
328 up_write(&loghandle->lgh_lock);
330 /* try to use next log */
331 loghandle = llog_cat_current_log(cathandle, th);
332 LASSERT(!IS_ERR(loghandle));
333 /* new llog can be created concurrently */
334 if (!llog_exist(loghandle)) {
335 rc = llog_cat_new_log(env, cathandle, loghandle, th);
337 up_write(&loghandle->lgh_lock);
341 /* now let's try to add the record */
342 rc = llog_write_rec(env, loghandle, rec, reccookie,
345 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
346 up_write(&loghandle->lgh_lock);
351 EXPORT_SYMBOL(llog_cat_add_rec);
353 int llog_cat_declare_add_rec(const struct lu_env *env,
354 struct llog_handle *cathandle,
355 struct llog_rec_hdr *rec, struct thandle *th)
357 struct llog_thread_info *lgi = llog_info(env);
358 struct llog_logid_rec *lirec = &lgi->lgi_logid;
359 struct llog_handle *loghandle, *next;
364 if (cathandle->u.chd.chd_current_log == NULL) {
365 /* declare new plain llog */
366 down_write(&cathandle->lgh_lock);
367 if (cathandle->u.chd.chd_current_log == NULL) {
368 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
369 NULL, NULL, LLOG_OPEN_NEW);
371 cathandle->u.chd.chd_current_log = loghandle;
372 list_add_tail(&loghandle->u.phd.phd_entry,
373 &cathandle->u.chd.chd_head);
376 up_write(&cathandle->lgh_lock);
377 } else if (cathandle->u.chd.chd_next_log == NULL) {
378 /* declare next plain llog */
379 down_write(&cathandle->lgh_lock);
380 if (cathandle->u.chd.chd_next_log == NULL) {
381 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
382 NULL, NULL, LLOG_OPEN_NEW);
384 cathandle->u.chd.chd_next_log = loghandle;
385 list_add_tail(&loghandle->u.phd.phd_entry,
386 &cathandle->u.chd.chd_head);
389 up_write(&cathandle->lgh_lock);
394 lirec->lid_hdr.lrh_len = sizeof(*lirec);
396 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
397 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
401 llog_declare_write_rec(env, cathandle, &lirec->lid_hdr, -1, th);
403 /* declare records in the llogs */
404 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
409 next = cathandle->u.chd.chd_next_log;
411 if (!llog_exist(next)) {
412 rc = llog_declare_create(env, next, th);
413 llog_declare_write_rec(env, cathandle, &lirec->lid_hdr,
416 /* XXX: we hope for declarations made for existing llog
417 * this might be not correct with some backends
418 * where declarations are expected against specific
419 * object like ZFS with full debugging enabled */
420 /*llog_declare_write_rec(env, next, rec, -1, th);*/
425 EXPORT_SYMBOL(llog_cat_declare_add_rec);
427 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
428 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
430 struct llog_ctxt *ctxt;
431 struct dt_device *dt;
432 struct thandle *th = NULL;
435 ctxt = cathandle->lgh_ctxt;
437 LASSERT(ctxt->loc_exp);
439 LASSERT(cathandle->lgh_obj != NULL);
440 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
442 th = dt_trans_create(env, dt);
446 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
450 rc = dt_trans_start_local(env, dt, th);
453 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
455 dt_trans_stop(env, dt, th);
458 EXPORT_SYMBOL(llog_cat_add);
460 /* For each cookie in the cookie array, we clear the log in-use bit and either:
461 * - the log is empty, so mark it free in the catalog header and delete it
462 * - the log is not empty, just write out the log header
464 * The cookies may be in different log files, so we need to get new logs
467 * Assumes caller has already pushed us into the kernel context.
469 int llog_cat_cancel_records(const struct lu_env *env,
470 struct llog_handle *cathandle, int count,
471 struct llog_cookie *cookies)
473 int i, index, rc = 0, failed = 0;
477 for (i = 0; i < count; i++, cookies++) {
478 struct llog_handle *loghandle;
479 struct llog_logid *lgl = &cookies->lgc_lgl;
482 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
484 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
485 cathandle->lgh_ctxt->loc_obd->obd_name,
486 POSTID(&lgl->lgl_oi), rc);
491 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
492 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
493 index = loghandle->u.phd.phd_cookie.lgc_index;
494 rc = llog_cat_cleanup(env, cathandle, loghandle,
496 } else if (lrc == -ENOENT) {
497 if (rc == 0) /* ENOENT shouldn't rewrite any error */
499 } else if (lrc < 0) {
503 llog_handle_put(loghandle);
506 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
507 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
512 EXPORT_SYMBOL(llog_cat_cancel_records);
514 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
515 struct llog_rec_hdr *rec, void *data)
517 struct llog_process_data *d = data;
518 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
519 struct llog_handle *llh;
520 struct llog_log_hdr *hdr;
524 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
525 CERROR("invalid record in catalog\n");
528 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
529 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
530 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
532 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
534 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
535 cat_llh->lgh_ctxt->loc_obd->obd_name,
536 POSTID(&lir->lid_id.lgl_oi), rc);
537 if (rc == -ENOENT || rc == -ESTALE) {
538 /* After a server crash, a stub of index
539 * record in catlog could be kept, because
540 * plain log destroy + catlog index record
541 * deletion are not atomic. So we end up with
542 * an index but no actual record. Destroy the
543 * index and move on. */
544 rc = llog_cat_cleanup(env, cat_llh, NULL,
551 /* clean old empty llogs, do not consider current llog in use */
553 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
554 hdr->llh_count == 1 &&
555 llh != cat_llh->u.chd.chd_current_log) {
556 rc = llog_destroy(env, llh);
558 CERROR("%s: fail to destroy empty log: rc = %d\n",
559 llh->lgh_ctxt->loc_obd->obd_name, rc);
560 GOTO(out, rc = LLOG_DEL_PLAIN);
563 if (rec->lrh_index < d->lpd_startcat) {
564 /* Skip processing of the logs until startcat */
566 } else if (d->lpd_startidx > 0) {
567 struct llog_process_cat_data cd;
569 cd.lpcd_first_idx = d->lpd_startidx;
570 cd.lpcd_last_idx = 0;
571 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
573 /* Continue processing the next log from idx 0 */
576 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
581 /* The empty plain log was destroyed while processing */
582 if (rc == LLOG_DEL_PLAIN)
583 rc = llog_cat_cleanup(env, cat_llh, llh,
584 llh->u.phd.phd_cookie.lgc_index);
585 llog_handle_put(llh);
590 int llog_cat_process_or_fork(const struct lu_env *env,
591 struct llog_handle *cat_llh,
592 llog_cb_t cb, void *data, int startcat,
593 int startidx, bool fork)
595 struct llog_process_data d;
596 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
600 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
603 d.lpd_startcat = startcat;
604 d.lpd_startidx = startidx;
606 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
607 struct llog_process_cat_data cd;
609 CWARN("catlog "DOSTID" crosses index zero\n",
610 POSTID(&cat_llh->lgh_id.lgl_oi));
612 cd.lpcd_first_idx = llh->llh_cat_idx;
613 cd.lpcd_last_idx = 0;
614 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
619 cd.lpcd_first_idx = 0;
620 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
621 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
624 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
630 EXPORT_SYMBOL(llog_cat_process_or_fork);
632 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
633 llog_cb_t cb, void *data, int startcat, int startidx)
635 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
638 EXPORT_SYMBOL(llog_cat_process);
640 static int llog_cat_reverse_process_cb(const struct lu_env *env,
641 struct llog_handle *cat_llh,
642 struct llog_rec_hdr *rec, void *data)
644 struct llog_process_data *d = data;
645 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
646 struct llog_handle *llh;
647 struct llog_log_hdr *hdr;
650 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
651 CERROR("invalid record in catalog\n");
654 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
655 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
656 le32_to_cpu(rec->lrh_index), POSTID(&cat_llh->lgh_id.lgl_oi));
658 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
660 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
661 cat_llh->lgh_ctxt->loc_obd->obd_name,
662 POSTID(&lir->lid_id.lgl_oi), rc);
663 if (rc == -ENOENT || rc == -ESTALE) {
664 /* After a server crash, a stub of index
665 * record in catlog could be kept, because
666 * plain log destroy + catlog index record
667 * deletion are not atomic. So we end up with
668 * an index but no actual record. Destroy the
669 * index and move on. */
670 rc = llog_cat_cleanup(env, cat_llh, NULL,
677 /* clean old empty llogs, do not consider current llog in use */
679 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
680 hdr->llh_count == 1 &&
681 llh != cat_llh->u.chd.chd_current_log) {
682 rc = llog_destroy(env, llh);
684 CERROR("%s: fail to destroy empty log: rc = %d\n",
685 llh->lgh_ctxt->loc_obd->obd_name, rc);
686 GOTO(out, rc = LLOG_DEL_PLAIN);
689 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
692 /* The empty plain was destroyed while processing */
693 if (rc == LLOG_DEL_PLAIN)
694 rc = llog_cat_cleanup(env, cat_llh, llh,
695 llh->u.phd.phd_cookie.lgc_index);
697 llog_handle_put(llh);
701 int llog_cat_reverse_process(const struct lu_env *env,
702 struct llog_handle *cat_llh,
703 llog_cb_t cb, void *data)
705 struct llog_process_data d;
706 struct llog_process_cat_data cd;
707 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
711 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
715 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
716 CWARN("catalog "DOSTID" crosses index zero\n",
717 POSTID(&cat_llh->lgh_id.lgl_oi));
719 cd.lpcd_first_idx = 0;
720 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
721 rc = llog_reverse_process(env, cat_llh,
722 llog_cat_reverse_process_cb,
727 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
728 cd.lpcd_last_idx = 0;
729 rc = llog_reverse_process(env, cat_llh,
730 llog_cat_reverse_process_cb,
733 rc = llog_reverse_process(env, cat_llh,
734 llog_cat_reverse_process_cb,
740 EXPORT_SYMBOL(llog_cat_reverse_process);
742 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
744 struct llog_log_hdr *llh = cathandle->lgh_hdr;
745 int i, bitmap_size, idx;
748 bitmap_size = LLOG_BITMAP_SIZE(llh);
749 if (llh->llh_cat_idx == (index - 1)) {
750 idx = llh->llh_cat_idx + 1;
751 llh->llh_cat_idx = idx;
752 if (idx == cathandle->lgh_last_idx)
754 for (i = (index + 1) % bitmap_size;
755 i != cathandle->lgh_last_idx;
756 i = (i + 1) % bitmap_size) {
757 if (!ext2_test_bit(i, llh->llh_bitmap)) {
758 idx = llh->llh_cat_idx + 1;
759 llh->llh_cat_idx = idx;
761 llh->llh_cat_idx = 0;
767 CDEBUG(D_RPCTRACE, "set catlog "DOSTID" first idx %u\n",
768 POSTID(&cathandle->lgh_id.lgl_oi), llh->llh_cat_idx);
774 /* Cleanup deleted plain llog traces from catalog */
775 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
776 struct llog_handle *loghandle, int index)
781 if (loghandle != NULL) {
782 /* remove destroyed llog from catalog list and
783 * chd_current_log variable */
784 down_write(&cathandle->lgh_lock);
785 if (cathandle->u.chd.chd_current_log == loghandle)
786 cathandle->u.chd.chd_current_log = NULL;
787 list_del_init(&loghandle->u.phd.phd_entry);
788 up_write(&cathandle->lgh_lock);
789 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
790 /* llog was opened and keep in a list, close it now */
791 llog_close(env, loghandle);
793 /* remove plain llog entry from catalog by index */
794 llog_cat_set_first_idx(cathandle, index);
795 rc = llog_cancel_rec(env, cathandle, index);
797 CDEBUG(D_HA, "cancel plain log at index"
798 " %u of catalog "DOSTID"\n",
799 index, POSTID(&cathandle->lgh_id.lgl_oi));
803 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
804 struct llog_rec_hdr *rec, void *data)
806 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
807 struct llog_handle *loghandle;
808 struct llog_log_hdr *llh;
813 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
814 CERROR("invalid record in catalog\n");
818 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
819 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
820 rec->lrh_index, POSTID(&cathandle->lgh_id.lgl_oi));
822 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
824 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
825 cathandle->lgh_ctxt->loc_obd->obd_name,
826 POSTID(&lir->lid_id.lgl_oi), rc);
827 if (rc == -ENOENT || rc == -ESTALE) {
828 /* remove index from catalog */
829 llog_cat_cleanup(env, cathandle, NULL, rec->lrh_index);
834 llh = loghandle->lgh_hdr;
835 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
836 (llh->llh_count == 1)) {
837 rc = llog_destroy(env, loghandle);
839 CERROR("%s: fail to destroy empty log: rc = %d\n",
840 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
842 llog_cat_cleanup(env, cathandle, loghandle,
843 loghandle->u.phd.phd_cookie.lgc_index);
845 llog_handle_put(loghandle);
849 EXPORT_SYMBOL(cat_cancel_cb);
851 /* helper to initialize catalog llog and process it to cancel */
852 int llog_cat_init_and_process(const struct lu_env *env,
853 struct llog_handle *llh)
857 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
863 EXPORT_SYMBOL(llog_cat_init_and_process);