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;
132 if (cathandle == NULL)
135 down_write(&cathandle->lgh_lock);
136 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
138 struct llog_logid *cgl = &loghandle->lgh_id;
140 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
141 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
142 if (cgl->lgl_ogen != logid->lgl_ogen) {
143 CERROR("%s: log "DOSTID" generation %x != %x\n",
144 loghandle->lgh_ctxt->loc_obd->obd_name,
145 POSTID(&logid->lgl_oi), cgl->lgl_ogen,
149 loghandle->u.phd.phd_cat_handle = cathandle;
150 up_write(&cathandle->lgh_lock);
154 up_write(&cathandle->lgh_lock);
156 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
159 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
160 cathandle->lgh_ctxt->loc_obd->obd_name,
161 POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
165 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
167 llog_close(env, loghandle);
172 down_write(&cathandle->lgh_lock);
173 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
174 up_write(&cathandle->lgh_lock);
176 loghandle->u.phd.phd_cat_handle = cathandle;
177 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
178 loghandle->u.phd.phd_cookie.lgc_index =
179 loghandle->lgh_hdr->llh_cat_idx;
182 llog_handle_get(loghandle);
187 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
189 struct llog_handle *loghandle, *n;
194 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
196 struct llog_log_hdr *llh = loghandle->lgh_hdr;
199 /* unlink open-not-created llogs */
200 list_del_init(&loghandle->u.phd.phd_entry);
201 llh = loghandle->lgh_hdr;
202 if (loghandle->lgh_obj != NULL && llh != NULL &&
203 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
204 (llh->llh_count == 1)) {
205 rc = llog_destroy(env, loghandle);
207 CERROR("%s: failure destroying log during "
208 "cleanup: rc = %d\n",
209 loghandle->lgh_ctxt->loc_obd->obd_name,
212 index = loghandle->u.phd.phd_cookie.lgc_index;
213 llog_cat_cleanup(env, cathandle, NULL, index);
215 llog_close(env, loghandle);
217 /* if handle was stored in ctxt, remove it too */
218 if (cathandle->lgh_ctxt->loc_handle == cathandle)
219 cathandle->lgh_ctxt->loc_handle = NULL;
220 rc = llog_close(env, cathandle);
223 EXPORT_SYMBOL(llog_cat_close);
226 * lockdep markers for nested struct llog_handle::lgh_lock locking.
233 /** Return the currently active log handle. If the current log handle doesn't
234 * have enough space left for the current record, start a new one.
236 * If reclen is 0, we only want to know what the currently active log is,
237 * otherwise we get a lock on this log so nobody can steal our space.
239 * Assumes caller has already pushed us into the kernel context and is locking.
241 * NOTE: loghandle is write-locked upon successful return
243 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
246 struct llog_handle *loghandle = NULL;
249 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
250 loghandle = cathandle->u.chd.chd_current_log;
252 struct llog_log_hdr *llh;
254 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
255 llh = loghandle->lgh_hdr;
257 loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
258 up_read(&cathandle->lgh_lock);
261 up_write(&loghandle->lgh_lock);
264 up_read(&cathandle->lgh_lock);
266 /* time to use next log */
268 /* first, we have to make sure the state hasn't changed */
269 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
270 loghandle = cathandle->u.chd.chd_current_log;
272 struct llog_log_hdr *llh;
274 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
275 llh = loghandle->lgh_hdr;
277 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
278 up_write(&cathandle->lgh_lock);
281 up_write(&loghandle->lgh_lock);
285 CDEBUG(D_INODE, "use next log\n");
287 loghandle = cathandle->u.chd.chd_next_log;
288 cathandle->u.chd.chd_current_log = loghandle;
289 cathandle->u.chd.chd_next_log = NULL;
290 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
291 up_write(&cathandle->lgh_lock);
296 /* Add a single record to the recovery log(s) using a catalog
297 * Returns as llog_write_record
299 * Assumes caller has already pushed us into the kernel context.
301 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
302 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
305 struct llog_handle *loghandle;
309 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
310 loghandle = llog_cat_current_log(cathandle, th);
311 LASSERT(!IS_ERR(loghandle));
313 /* loghandle is already locked by llog_cat_current_log() for us */
314 if (!llog_exist(loghandle)) {
315 rc = llog_cat_new_log(env, cathandle, loghandle, th);
317 up_write(&loghandle->lgh_lock);
321 /* now let's try to add the record */
322 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
324 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
325 "llog_write_rec %d: lh=%p\n", rc, loghandle);
326 up_write(&loghandle->lgh_lock);
328 /* try to use next log */
329 loghandle = llog_cat_current_log(cathandle, th);
330 LASSERT(!IS_ERR(loghandle));
331 /* new llog can be created concurrently */
332 if (!llog_exist(loghandle)) {
333 rc = llog_cat_new_log(env, cathandle, loghandle, th);
335 up_write(&loghandle->lgh_lock);
339 /* now let's try to add the record */
340 rc = llog_write_rec(env, loghandle, rec, reccookie,
343 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
344 up_write(&loghandle->lgh_lock);
349 EXPORT_SYMBOL(llog_cat_add_rec);
351 int llog_cat_declare_add_rec(const struct lu_env *env,
352 struct llog_handle *cathandle,
353 struct llog_rec_hdr *rec, struct thandle *th)
355 struct llog_thread_info *lgi = llog_info(env);
356 struct llog_logid_rec *lirec = &lgi->lgi_logid;
357 struct llog_handle *loghandle, *next;
362 if (cathandle->u.chd.chd_current_log == NULL) {
363 /* declare new plain llog */
364 down_write(&cathandle->lgh_lock);
365 if (cathandle->u.chd.chd_current_log == NULL) {
366 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
367 NULL, NULL, LLOG_OPEN_NEW);
369 cathandle->u.chd.chd_current_log = loghandle;
370 list_add_tail(&loghandle->u.phd.phd_entry,
371 &cathandle->u.chd.chd_head);
374 up_write(&cathandle->lgh_lock);
375 } else if (cathandle->u.chd.chd_next_log == NULL) {
376 /* declare next plain llog */
377 down_write(&cathandle->lgh_lock);
378 if (cathandle->u.chd.chd_next_log == NULL) {
379 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
380 NULL, NULL, LLOG_OPEN_NEW);
382 cathandle->u.chd.chd_next_log = loghandle;
383 list_add_tail(&loghandle->u.phd.phd_entry,
384 &cathandle->u.chd.chd_head);
387 up_write(&cathandle->lgh_lock);
392 lirec->lid_hdr.lrh_len = sizeof(*lirec);
394 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
395 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
399 llog_declare_write_rec(env, cathandle, &lirec->lid_hdr, -1, th);
401 /* declare records in the llogs */
402 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
407 next = cathandle->u.chd.chd_next_log;
409 if (!llog_exist(next)) {
410 rc = llog_declare_create(env, next, th);
411 llog_declare_write_rec(env, cathandle, &lirec->lid_hdr,
414 /* XXX: we hope for declarations made for existing llog
415 * this might be not correct with some backends
416 * where declarations are expected against specific
417 * object like ZFS with full debugging enabled */
418 /*llog_declare_write_rec(env, next, rec, -1, th);*/
423 EXPORT_SYMBOL(llog_cat_declare_add_rec);
425 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
426 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
428 struct llog_ctxt *ctxt;
429 struct dt_device *dt;
430 struct thandle *th = NULL;
433 ctxt = cathandle->lgh_ctxt;
435 LASSERT(ctxt->loc_exp);
437 LASSERT(cathandle->lgh_obj != NULL);
438 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
440 th = dt_trans_create(env, dt);
444 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
448 rc = dt_trans_start_local(env, dt, th);
451 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
453 dt_trans_stop(env, dt, th);
456 EXPORT_SYMBOL(llog_cat_add);
458 /* For each cookie in the cookie array, we clear the log in-use bit and either:
459 * - the log is empty, so mark it free in the catalog header and delete it
460 * - the log is not empty, just write out the log header
462 * The cookies may be in different log files, so we need to get new logs
465 * Assumes caller has already pushed us into the kernel context.
467 int llog_cat_cancel_records(const struct lu_env *env,
468 struct llog_handle *cathandle, int count,
469 struct llog_cookie *cookies)
471 int i, index, rc = 0, failed = 0;
475 for (i = 0; i < count; i++, cookies++) {
476 struct llog_handle *loghandle;
477 struct llog_logid *lgl = &cookies->lgc_lgl;
480 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
482 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
483 cathandle->lgh_ctxt->loc_obd->obd_name,
484 POSTID(&lgl->lgl_oi), rc);
489 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
490 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
491 index = loghandle->u.phd.phd_cookie.lgc_index;
492 rc = llog_cat_cleanup(env, cathandle, loghandle,
494 } else if (lrc == -ENOENT) {
495 if (rc == 0) /* ENOENT shouldn't rewrite any error */
497 } else if (lrc < 0) {
501 llog_handle_put(loghandle);
504 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
505 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
510 EXPORT_SYMBOL(llog_cat_cancel_records);
512 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
513 struct llog_rec_hdr *rec, void *data)
515 struct llog_process_data *d = data;
516 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
517 struct llog_handle *llh;
518 struct llog_log_hdr *hdr;
522 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
523 CERROR("invalid record in catalog\n");
526 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
527 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
528 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
530 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
532 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
533 cat_llh->lgh_ctxt->loc_obd->obd_name,
534 POSTID(&lir->lid_id.lgl_oi), rc);
535 if (rc == -ENOENT || rc == -ESTALE) {
536 /* After a server crash, a stub of index
537 * record in catlog could be kept, because
538 * plain log destroy + catlog index record
539 * deletion are not atomic. So we end up with
540 * an index but no actual record. Destroy the
541 * index and move on. */
542 rc = llog_cat_cleanup(env, cat_llh, NULL,
549 /* clean old empty llogs, do not consider current llog in use */
551 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
552 hdr->llh_count == 1 &&
553 llh != cat_llh->u.chd.chd_current_log) {
554 rc = llog_destroy(env, llh);
556 CERROR("%s: fail to destroy empty log: rc = %d\n",
557 llh->lgh_ctxt->loc_obd->obd_name, rc);
558 GOTO(out, rc = LLOG_DEL_PLAIN);
561 if (rec->lrh_index < d->lpd_startcat) {
562 /* Skip processing of the logs until startcat */
564 } else if (d->lpd_startidx > 0) {
565 struct llog_process_cat_data cd;
567 cd.lpcd_first_idx = d->lpd_startidx;
568 cd.lpcd_last_idx = 0;
569 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
571 /* Continue processing the next log from idx 0 */
574 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
579 /* The empty plain log was destroyed while processing */
580 if (rc == LLOG_DEL_PLAIN)
581 rc = llog_cat_cleanup(env, cat_llh, llh,
582 llh->u.phd.phd_cookie.lgc_index);
583 llog_handle_put(llh);
588 int llog_cat_process_or_fork(const struct lu_env *env,
589 struct llog_handle *cat_llh,
590 llog_cb_t cb, void *data, int startcat,
591 int startidx, bool fork)
593 struct llog_process_data d;
594 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
598 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
601 d.lpd_startcat = startcat;
602 d.lpd_startidx = startidx;
604 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
605 struct llog_process_cat_data cd;
607 CWARN("catlog "DOSTID" crosses index zero\n",
608 POSTID(&cat_llh->lgh_id.lgl_oi));
610 cd.lpcd_first_idx = llh->llh_cat_idx;
611 cd.lpcd_last_idx = 0;
612 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
617 cd.lpcd_first_idx = 0;
618 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
619 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
622 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
628 EXPORT_SYMBOL(llog_cat_process_or_fork);
630 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
631 llog_cb_t cb, void *data, int startcat, int startidx)
633 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
636 EXPORT_SYMBOL(llog_cat_process);
638 static int llog_cat_reverse_process_cb(const struct lu_env *env,
639 struct llog_handle *cat_llh,
640 struct llog_rec_hdr *rec, void *data)
642 struct llog_process_data *d = data;
643 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
644 struct llog_handle *llh;
645 struct llog_log_hdr *hdr;
648 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
649 CERROR("invalid record in catalog\n");
652 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
653 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
654 le32_to_cpu(rec->lrh_index), POSTID(&cat_llh->lgh_id.lgl_oi));
656 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
658 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
659 cat_llh->lgh_ctxt->loc_obd->obd_name,
660 POSTID(&lir->lid_id.lgl_oi), rc);
661 if (rc == -ENOENT || rc == -ESTALE) {
662 /* After a server crash, a stub of index
663 * record in catlog could be kept, because
664 * plain log destroy + catlog index record
665 * deletion are not atomic. So we end up with
666 * an index but no actual record. Destroy the
667 * index and move on. */
668 rc = llog_cat_cleanup(env, cat_llh, NULL,
675 /* clean old empty llogs, do not consider current llog in use */
677 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
678 hdr->llh_count == 1 &&
679 llh != cat_llh->u.chd.chd_current_log) {
680 rc = llog_destroy(env, llh);
682 CERROR("%s: fail to destroy empty log: rc = %d\n",
683 llh->lgh_ctxt->loc_obd->obd_name, rc);
684 GOTO(out, rc = LLOG_DEL_PLAIN);
687 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
690 /* The empty plain was destroyed while processing */
691 if (rc == LLOG_DEL_PLAIN)
692 rc = llog_cat_cleanup(env, cat_llh, llh,
693 llh->u.phd.phd_cookie.lgc_index);
695 llog_handle_put(llh);
699 int llog_cat_reverse_process(const struct lu_env *env,
700 struct llog_handle *cat_llh,
701 llog_cb_t cb, void *data)
703 struct llog_process_data d;
704 struct llog_process_cat_data cd;
705 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
709 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
713 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
714 CWARN("catalog "DOSTID" crosses index zero\n",
715 POSTID(&cat_llh->lgh_id.lgl_oi));
717 cd.lpcd_first_idx = 0;
718 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
719 rc = llog_reverse_process(env, cat_llh,
720 llog_cat_reverse_process_cb,
725 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
726 cd.lpcd_last_idx = 0;
727 rc = llog_reverse_process(env, cat_llh,
728 llog_cat_reverse_process_cb,
731 rc = llog_reverse_process(env, cat_llh,
732 llog_cat_reverse_process_cb,
738 EXPORT_SYMBOL(llog_cat_reverse_process);
740 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
742 struct llog_log_hdr *llh = cathandle->lgh_hdr;
743 int i, bitmap_size, idx;
746 bitmap_size = LLOG_BITMAP_SIZE(llh);
747 if (llh->llh_cat_idx == (index - 1)) {
748 idx = llh->llh_cat_idx + 1;
749 llh->llh_cat_idx = idx;
750 if (idx == cathandle->lgh_last_idx)
752 for (i = (index + 1) % bitmap_size;
753 i != cathandle->lgh_last_idx;
754 i = (i + 1) % bitmap_size) {
755 if (!ext2_test_bit(i, llh->llh_bitmap)) {
756 idx = llh->llh_cat_idx + 1;
757 llh->llh_cat_idx = idx;
759 llh->llh_cat_idx = 0;
765 CDEBUG(D_RPCTRACE, "set catlog "DOSTID" first idx %u\n",
766 POSTID(&cathandle->lgh_id.lgl_oi), llh->llh_cat_idx);
772 /* Cleanup deleted plain llog traces from catalog */
773 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
774 struct llog_handle *loghandle, int index)
779 if (loghandle != NULL) {
780 /* remove destroyed llog from catalog list and
781 * chd_current_log variable */
782 down_write(&cathandle->lgh_lock);
783 if (cathandle->u.chd.chd_current_log == loghandle)
784 cathandle->u.chd.chd_current_log = NULL;
785 list_del_init(&loghandle->u.phd.phd_entry);
786 up_write(&cathandle->lgh_lock);
787 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
788 /* llog was opened and keep in a list, close it now */
789 llog_close(env, loghandle);
791 /* remove plain llog entry from catalog by index */
792 llog_cat_set_first_idx(cathandle, index);
793 rc = llog_cancel_rec(env, cathandle, index);
795 CDEBUG(D_HA, "cancel plain log at index"
796 " %u of catalog "DOSTID"\n",
797 index, POSTID(&cathandle->lgh_id.lgl_oi));
801 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
802 struct llog_rec_hdr *rec, void *data)
804 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
805 struct llog_handle *loghandle;
806 struct llog_log_hdr *llh;
811 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
812 CERROR("invalid record in catalog\n");
816 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
817 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
818 rec->lrh_index, POSTID(&cathandle->lgh_id.lgl_oi));
820 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
822 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
823 cathandle->lgh_ctxt->loc_obd->obd_name,
824 POSTID(&lir->lid_id.lgl_oi), rc);
825 if (rc == -ENOENT || rc == -ESTALE) {
826 /* remove index from catalog */
827 llog_cat_cleanup(env, cathandle, NULL, rec->lrh_index);
832 llh = loghandle->lgh_hdr;
833 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
834 (llh->llh_count == 1)) {
835 rc = llog_destroy(env, loghandle);
837 CERROR("%s: fail to destroy empty log: rc = %d\n",
838 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
840 llog_cat_cleanup(env, cathandle, loghandle,
841 loghandle->u.phd.phd_cookie.lgc_index);
843 llog_handle_put(loghandle);
847 EXPORT_SYMBOL(cat_cancel_cb);
849 /* helper to initialize catalog llog and process it to cancel */
850 int llog_cat_init_and_process(const struct lu_env *env,
851 struct llog_handle *llh)
855 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
861 EXPORT_SYMBOL(llog_cat_init_and_process);