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 <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 "DOSTID":%x for index %u of catalog"
122 DOSTID"\n", POSTID(&loghandle->lgh_id.lgl_oi),
123 loghandle->lgh_id.lgl_ogen, index,
124 POSTID(&cathandle->lgh_id.lgl_oi));
125 /* build the record for this log in the catalog */
126 rec.lid_hdr.lrh_len = sizeof(rec);
127 rec.lid_hdr.lrh_index = index;
128 rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
129 rec.lid_id = loghandle->lgh_id;
130 rec.lid_tail.lrt_len = sizeof(rec);
131 rec.lid_tail.lrt_index = index;
133 /* update the catalog: header and record */
134 rc = llog_write_rec(env, cathandle, &rec.lid_hdr,
135 &loghandle->u.phd.phd_cookie, 1, NULL, index, th);
137 GOTO(out_destroy, rc);
139 loghandle->lgh_hdr->llh_cat_idx = index;
142 llog_destroy(env, loghandle);
146 /* Open an existent log handle and add it to the open list.
147 * This log handle will be closed when all of the records in it are removed.
149 * Assumes caller has already pushed us into the kernel context and is locking.
150 * We return a lock on the handle to ensure nobody yanks it from us.
152 * This takes extra reference on llog_handle via llog_handle_get() and require
153 * this reference to be put by caller using llog_handle_put()
155 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
156 struct llog_handle **res, struct llog_logid *logid)
158 struct llog_handle *loghandle;
163 if (cathandle == NULL)
166 down_write(&cathandle->lgh_lock);
167 cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
169 struct llog_logid *cgl = &loghandle->lgh_id;
171 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
172 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
173 if (cgl->lgl_ogen != logid->lgl_ogen) {
174 CERROR("%s: log "DOSTID" generation %x != %x\n",
175 loghandle->lgh_ctxt->loc_obd->obd_name,
176 POSTID(&logid->lgl_oi), cgl->lgl_ogen,
180 loghandle->u.phd.phd_cat_handle = cathandle;
181 up_write(&cathandle->lgh_lock);
185 up_write(&cathandle->lgh_lock);
187 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
190 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
191 cathandle->lgh_ctxt->loc_obd->obd_name,
192 POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
196 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
198 llog_close(env, loghandle);
203 down_write(&cathandle->lgh_lock);
204 cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
205 up_write(&cathandle->lgh_lock);
207 loghandle->u.phd.phd_cat_handle = cathandle;
208 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
209 loghandle->u.phd.phd_cookie.lgc_index =
210 loghandle->lgh_hdr->llh_cat_idx;
213 llog_handle_get(loghandle);
218 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
220 struct llog_handle *loghandle, *n;
225 cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
227 struct llog_log_hdr *llh = loghandle->lgh_hdr;
230 /* unlink open-not-created llogs */
231 cfs_list_del_init(&loghandle->u.phd.phd_entry);
232 llh = loghandle->lgh_hdr;
233 if (loghandle->lgh_obj != NULL && llh != NULL &&
234 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
235 (llh->llh_count == 1)) {
236 rc = llog_destroy(env, loghandle);
238 CERROR("%s: failure destroying log during "
239 "cleanup: rc = %d\n",
240 loghandle->lgh_ctxt->loc_obd->obd_name,
243 index = loghandle->u.phd.phd_cookie.lgc_index;
244 llog_cat_cleanup(env, cathandle, NULL, index);
246 llog_close(env, loghandle);
248 /* if handle was stored in ctxt, remove it too */
249 if (cathandle->lgh_ctxt->loc_handle == cathandle)
250 cathandle->lgh_ctxt->loc_handle = NULL;
251 rc = llog_close(env, cathandle);
254 EXPORT_SYMBOL(llog_cat_close);
257 * lockdep markers for nested struct llog_handle::lgh_lock locking.
264 /** Return the currently active log handle. If the current log handle doesn't
265 * have enough space left for the current record, start a new one.
267 * If reclen is 0, we only want to know what the currently active log is,
268 * otherwise we get a lock on this log so nobody can steal our space.
270 * Assumes caller has already pushed us into the kernel context and is locking.
272 * NOTE: loghandle is write-locked upon successful return
274 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
277 struct llog_handle *loghandle = NULL;
280 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
281 loghandle = cathandle->u.chd.chd_current_log;
283 struct llog_log_hdr *llh;
285 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
286 llh = loghandle->lgh_hdr;
288 loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
289 up_read(&cathandle->lgh_lock);
292 up_write(&loghandle->lgh_lock);
295 up_read(&cathandle->lgh_lock);
297 /* time to use next log */
299 /* first, we have to make sure the state hasn't changed */
300 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
301 loghandle = cathandle->u.chd.chd_current_log;
303 struct llog_log_hdr *llh;
305 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
306 llh = loghandle->lgh_hdr;
308 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
309 up_write(&cathandle->lgh_lock);
312 up_write(&loghandle->lgh_lock);
316 CDEBUG(D_INODE, "use next log\n");
318 loghandle = cathandle->u.chd.chd_next_log;
319 cathandle->u.chd.chd_current_log = loghandle;
320 cathandle->u.chd.chd_next_log = NULL;
321 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
322 up_write(&cathandle->lgh_lock);
327 /* Add a single record to the recovery log(s) using a catalog
328 * Returns as llog_write_record
330 * Assumes caller has already pushed us into the kernel context.
332 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
333 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
334 void *buf, struct thandle *th)
336 struct llog_handle *loghandle;
340 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
341 loghandle = llog_cat_current_log(cathandle, th);
342 LASSERT(!IS_ERR(loghandle));
344 /* loghandle is already locked by llog_cat_current_log() for us */
345 if (!llog_exist(loghandle)) {
346 rc = llog_cat_new_log(env, cathandle, loghandle, th);
348 up_write(&loghandle->lgh_lock);
352 /* now let's try to add the record */
353 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1, th);
355 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
356 "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 LASSERT(cathandle->lgh_obj != NULL);
461 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
463 th = dt_trans_create(env, dt);
467 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
471 rc = dt_trans_start_local(env, dt, th);
474 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
476 dt_trans_stop(env, dt, th);
479 EXPORT_SYMBOL(llog_cat_add);
481 /* For each cookie in the cookie array, we clear the log in-use bit and either:
482 * - the log is empty, so mark it free in the catalog header and delete it
483 * - the log is not empty, just write out the log header
485 * The cookies may be in different log files, so we need to get new logs
488 * Assumes caller has already pushed us into the kernel context.
490 int llog_cat_cancel_records(const struct lu_env *env,
491 struct llog_handle *cathandle, int count,
492 struct llog_cookie *cookies)
494 int i, index, rc = 0, failed = 0;
498 for (i = 0; i < count; i++, cookies++) {
499 struct llog_handle *loghandle;
500 struct llog_logid *lgl = &cookies->lgc_lgl;
503 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
505 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
506 cathandle->lgh_ctxt->loc_obd->obd_name,
507 POSTID(&lgl->lgl_oi), rc);
512 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
513 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
514 index = loghandle->u.phd.phd_cookie.lgc_index;
515 rc = llog_cat_cleanup(env, cathandle, loghandle,
517 } else if (lrc == -ENOENT) {
518 if (rc == 0) /* ENOENT shouldn't rewrite any error */
520 } else if (lrc < 0) {
524 llog_handle_put(loghandle);
527 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
528 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
533 EXPORT_SYMBOL(llog_cat_cancel_records);
535 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
536 struct llog_rec_hdr *rec, void *data)
538 struct llog_process_data *d = data;
539 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
540 struct llog_handle *llh;
544 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
545 CERROR("invalid record in catalog\n");
548 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
549 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
550 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
552 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
554 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
555 cat_llh->lgh_ctxt->loc_obd->obd_name,
556 POSTID(&lir->lid_id.lgl_oi), rc);
560 if (rec->lrh_index < d->lpd_startcat) {
561 /* Skip processing of the logs until startcat */
563 } else if (d->lpd_startidx > 0) {
564 struct llog_process_cat_data cd;
566 cd.lpcd_first_idx = d->lpd_startidx;
567 cd.lpcd_last_idx = 0;
568 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
570 /* Continue processing the next log from idx 0 */
573 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
577 /* The empty plain log was destroyed while processing */
578 if (rc == LLOG_DEL_PLAIN)
579 rc = llog_cat_cleanup(env, cat_llh, llh,
580 llh->u.phd.phd_cookie.lgc_index);
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 "DOSTID" crosses index zero\n",
606 POSTID(&cat_llh->lgh_id.lgl_oi));
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);
636 static int llog_cat_reverse_process_cb(const struct lu_env *env,
637 struct llog_handle *cat_llh,
638 struct llog_rec_hdr *rec, void *data)
640 struct llog_process_data *d = data;
641 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
642 struct llog_handle *llh;
645 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
646 CERROR("invalid record in catalog\n");
649 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
650 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
651 le32_to_cpu(rec->lrh_index), POSTID(&cat_llh->lgh_id.lgl_oi));
653 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
655 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
656 cat_llh->lgh_ctxt->loc_obd->obd_name,
657 POSTID(&lir->lid_id.lgl_oi), rc);
661 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
663 /* The empty plain was destroyed while processing */
664 if (rc == LLOG_DEL_PLAIN)
665 rc = llog_cat_cleanup(env, cat_llh, llh,
666 llh->u.phd.phd_cookie.lgc_index);
668 llog_handle_put(llh);
672 int llog_cat_reverse_process(const struct lu_env *env,
673 struct llog_handle *cat_llh,
674 llog_cb_t cb, void *data)
676 struct llog_process_data d;
677 struct llog_process_cat_data cd;
678 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
682 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
686 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
687 CWARN("catalog "DOSTID" crosses index zero\n",
688 POSTID(&cat_llh->lgh_id.lgl_oi));
690 cd.lpcd_first_idx = 0;
691 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
692 rc = llog_reverse_process(env, cat_llh,
693 llog_cat_reverse_process_cb,
698 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
699 cd.lpcd_last_idx = 0;
700 rc = llog_reverse_process(env, cat_llh,
701 llog_cat_reverse_process_cb,
704 rc = llog_reverse_process(env, cat_llh,
705 llog_cat_reverse_process_cb,
711 EXPORT_SYMBOL(llog_cat_reverse_process);
713 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
715 struct llog_log_hdr *llh = cathandle->lgh_hdr;
716 int i, bitmap_size, idx;
719 bitmap_size = LLOG_BITMAP_SIZE(llh);
720 if (llh->llh_cat_idx == (index - 1)) {
721 idx = llh->llh_cat_idx + 1;
722 llh->llh_cat_idx = idx;
723 if (idx == cathandle->lgh_last_idx)
725 for (i = (index + 1) % bitmap_size;
726 i != cathandle->lgh_last_idx;
727 i = (i + 1) % bitmap_size) {
728 if (!ext2_test_bit(i, llh->llh_bitmap)) {
729 idx = llh->llh_cat_idx + 1;
730 llh->llh_cat_idx = idx;
732 llh->llh_cat_idx = 0;
738 CDEBUG(D_RPCTRACE, "set catlog "DOSTID" first idx %u\n",
739 POSTID(&cathandle->lgh_id.lgl_oi), llh->llh_cat_idx);
745 /* Cleanup deleted plain llog traces from catalog */
746 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
747 struct llog_handle *loghandle, int index)
752 if (loghandle != NULL) {
753 /* remove destroyed llog from catalog list and
754 * chd_current_log variable */
755 down_write(&cathandle->lgh_lock);
756 if (cathandle->u.chd.chd_current_log == loghandle)
757 cathandle->u.chd.chd_current_log = NULL;
758 cfs_list_del_init(&loghandle->u.phd.phd_entry);
759 up_write(&cathandle->lgh_lock);
760 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
761 /* llog was opened and keep in a list, close it now */
762 llog_close(env, loghandle);
764 /* remove plain llog entry from catalog by index */
765 llog_cat_set_first_idx(cathandle, index);
766 rc = llog_cancel_rec(env, cathandle, index);
768 CDEBUG(D_HA, "cancel plain log at index"
769 " %u of catalog "DOSTID"\n",
770 index, POSTID(&cathandle->lgh_id.lgl_oi));
774 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
775 struct llog_rec_hdr *rec, void *data)
777 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
778 struct llog_handle *loghandle;
779 struct llog_log_hdr *llh;
784 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
785 CERROR("invalid record in catalog\n");
789 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
790 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
791 rec->lrh_index, POSTID(&cathandle->lgh_id.lgl_oi));
793 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
795 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
796 cathandle->lgh_ctxt->loc_obd->obd_name,
797 POSTID(&lir->lid_id.lgl_oi), rc);
798 if (rc == -ENOENT || rc == -ESTALE) {
799 /* remove index from catalog */
800 llog_cat_cleanup(env, cathandle, NULL, rec->lrh_index);
805 llh = loghandle->lgh_hdr;
806 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
807 (llh->llh_count == 1)) {
808 rc = llog_destroy(env, loghandle);
810 CERROR("%s: fail to destroy empty log: rc = %d\n",
811 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
813 llog_cat_cleanup(env, cathandle, loghandle,
814 loghandle->u.phd.phd_cookie.lgc_index);
816 llog_handle_put(loghandle);
820 EXPORT_SYMBOL(cat_cancel_cb);
822 /* helper to initialize catalog llog and process it to cancel */
823 int llog_cat_init_and_process(const struct lu_env *env,
824 struct llog_handle *llh)
828 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
832 rc = llog_process_or_fork(env, llh, cat_cancel_cb, NULL, NULL, false);
834 CERROR("%s: llog_process() with cat_cancel_cb failed: rc = "
835 "%d\n", llh->lgh_ctxt->loc_obd->obd_name, rc);
838 EXPORT_SYMBOL(llog_cat_init_and_process);