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);
201 down_write(&cathandle->lgh_lock);
202 cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
203 up_write(&cathandle->lgh_lock);
205 loghandle->u.phd.phd_cat_handle = cathandle;
206 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
207 loghandle->u.phd.phd_cookie.lgc_index =
208 loghandle->lgh_hdr->llh_cat_idx;
211 llog_handle_get(loghandle);
216 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
218 struct llog_handle *loghandle, *n;
223 cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
225 struct llog_log_hdr *llh = loghandle->lgh_hdr;
228 /* unlink open-not-created llogs */
229 cfs_list_del_init(&loghandle->u.phd.phd_entry);
230 llh = loghandle->lgh_hdr;
231 if (loghandle->lgh_obj != NULL && llh != NULL &&
232 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
233 (llh->llh_count == 1)) {
234 rc = llog_destroy(env, loghandle);
236 CERROR("%s: failure destroying log during "
237 "cleanup: rc = %d\n",
238 loghandle->lgh_ctxt->loc_obd->obd_name,
241 index = loghandle->u.phd.phd_cookie.lgc_index;
242 llog_cat_cleanup(env, cathandle, NULL, index);
244 llog_close(env, loghandle);
246 /* if handle was stored in ctxt, remove it too */
247 if (cathandle->lgh_ctxt->loc_handle == cathandle)
248 cathandle->lgh_ctxt->loc_handle = NULL;
249 rc = llog_close(env, cathandle);
252 EXPORT_SYMBOL(llog_cat_close);
255 * lockdep markers for nested struct llog_handle::lgh_lock locking.
262 /** Return the currently active log handle. If the current log handle doesn't
263 * have enough space left for the current record, start a new one.
265 * If reclen is 0, we only want to know what the currently active log is,
266 * otherwise we get a lock on this log so nobody can steal our space.
268 * Assumes caller has already pushed us into the kernel context and is locking.
270 * NOTE: loghandle is write-locked upon successful return
272 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
275 struct llog_handle *loghandle = NULL;
278 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
279 loghandle = cathandle->u.chd.chd_current_log;
281 struct llog_log_hdr *llh;
283 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
284 llh = loghandle->lgh_hdr;
286 loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
287 up_read(&cathandle->lgh_lock);
290 up_write(&loghandle->lgh_lock);
293 up_read(&cathandle->lgh_lock);
295 /* time to use next log */
297 /* first, we have to make sure the state hasn't changed */
298 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
299 loghandle = cathandle->u.chd.chd_current_log;
301 struct llog_log_hdr *llh;
303 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
304 llh = loghandle->lgh_hdr;
306 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
307 up_write(&cathandle->lgh_lock);
310 up_write(&loghandle->lgh_lock);
314 CDEBUG(D_INODE, "use next log\n");
316 loghandle = cathandle->u.chd.chd_next_log;
317 cathandle->u.chd.chd_current_log = loghandle;
318 cathandle->u.chd.chd_next_log = NULL;
319 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
320 up_write(&cathandle->lgh_lock);
325 /* Add a single record to the recovery log(s) using a catalog
326 * Returns as llog_write_record
328 * Assumes caller has already pushed us into the kernel context.
330 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
331 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
332 void *buf, struct thandle *th)
334 struct llog_handle *loghandle;
338 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
339 loghandle = llog_cat_current_log(cathandle, th);
340 LASSERT(!IS_ERR(loghandle));
342 /* loghandle is already locked by llog_cat_current_log() for us */
343 if (!llog_exist(loghandle)) {
344 rc = llog_cat_new_log(env, cathandle, loghandle, th);
346 up_write(&loghandle->lgh_lock);
350 /* now let's try to add the record */
351 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1, th);
353 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
354 "llog_write_rec %d: lh=%p\n", rc, loghandle);
355 up_write(&loghandle->lgh_lock);
357 /* try to use next log */
358 loghandle = llog_cat_current_log(cathandle, th);
359 LASSERT(!IS_ERR(loghandle));
360 /* new llog can be created concurrently */
361 if (!llog_exist(loghandle)) {
362 rc = llog_cat_new_log(env, cathandle, loghandle, th);
364 up_write(&loghandle->lgh_lock);
368 /* now let's try to add the record */
369 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
372 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
373 up_write(&loghandle->lgh_lock);
378 EXPORT_SYMBOL(llog_cat_add_rec);
380 int llog_cat_declare_add_rec(const struct lu_env *env,
381 struct llog_handle *cathandle,
382 struct llog_rec_hdr *rec, struct thandle *th)
384 struct llog_handle *loghandle, *next;
389 if (cathandle->u.chd.chd_current_log == NULL) {
390 /* declare new plain llog */
391 down_write(&cathandle->lgh_lock);
392 if (cathandle->u.chd.chd_current_log == NULL) {
393 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
394 NULL, NULL, LLOG_OPEN_NEW);
396 cathandle->u.chd.chd_current_log = loghandle;
397 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
398 &cathandle->u.chd.chd_head);
401 up_write(&cathandle->lgh_lock);
402 } else if (cathandle->u.chd.chd_next_log == NULL) {
403 /* declare next plain llog */
404 down_write(&cathandle->lgh_lock);
405 if (cathandle->u.chd.chd_next_log == NULL) {
406 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
407 NULL, NULL, LLOG_OPEN_NEW);
409 cathandle->u.chd.chd_next_log = loghandle;
410 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
411 &cathandle->u.chd.chd_head);
414 up_write(&cathandle->lgh_lock);
419 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
420 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
424 llog_declare_write_rec(env, cathandle, NULL, -1, th);
426 /* declare records in the llogs */
427 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
432 next = cathandle->u.chd.chd_next_log;
434 if (!llog_exist(next)) {
435 rc = llog_declare_create(env, next, th);
436 llog_declare_write_rec(env, cathandle, NULL, -1, th);
438 llog_declare_write_rec(env, next, rec, -1, th);
443 EXPORT_SYMBOL(llog_cat_declare_add_rec);
445 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
446 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
449 struct llog_ctxt *ctxt;
450 struct dt_device *dt;
451 struct thandle *th = NULL;
454 ctxt = cathandle->lgh_ctxt;
456 LASSERT(ctxt->loc_exp);
458 if (cathandle->lgh_obj != NULL) {
459 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
462 th = dt_trans_create(env, dt);
466 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
470 rc = dt_trans_start_local(env, dt, th);
473 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
475 dt_trans_stop(env, dt, th);
476 } else { /* lvfs compat code */
477 LASSERT(cathandle->lgh_file != NULL);
478 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
480 rc = llog_cat_add_rec(env, cathandle, rec, reccookie,
485 EXPORT_SYMBOL(llog_cat_add);
487 /* For each cookie in the cookie array, we clear the log in-use bit and either:
488 * - the log is empty, so mark it free in the catalog header and delete it
489 * - the log is not empty, just write out the log header
491 * The cookies may be in different log files, so we need to get new logs
494 * Assumes caller has already pushed us into the kernel context.
496 int llog_cat_cancel_records(const struct lu_env *env,
497 struct llog_handle *cathandle, int count,
498 struct llog_cookie *cookies)
500 int i, index, rc = 0, failed = 0;
504 for (i = 0; i < count; i++, cookies++) {
505 struct llog_handle *loghandle;
506 struct llog_logid *lgl = &cookies->lgc_lgl;
509 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
511 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
512 cathandle->lgh_ctxt->loc_obd->obd_name,
518 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
519 if (lrc == 1) { /* log has been destroyed */
520 index = loghandle->u.phd.phd_cookie.lgc_index;
521 rc = llog_cat_cleanup(env, cathandle, loghandle,
523 } else if (lrc == -ENOENT) {
524 if (rc == 0) /* ENOENT shouldn't rewrite any error */
526 } else if (lrc < 0) {
530 llog_handle_put(loghandle);
533 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
534 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
539 EXPORT_SYMBOL(llog_cat_cancel_records);
541 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
542 struct llog_rec_hdr *rec, void *data)
544 struct llog_process_data *d = data;
545 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
546 struct llog_handle *llh;
550 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
551 CERROR("invalid record in catalog\n");
554 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
555 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
556 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
558 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
560 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
561 cat_llh->lgh_ctxt->loc_obd->obd_name,
562 lir->lid_id.lgl_oid, rc);
566 if (rec->lrh_index < d->lpd_startcat)
567 /* Skip processing of the logs until startcat */
570 if (d->lpd_startidx > 0) {
571 struct llog_process_cat_data cd;
573 cd.lpcd_first_idx = d->lpd_startidx;
574 cd.lpcd_last_idx = 0;
575 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
577 /* Continue processing the next log from idx 0 */
580 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
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 "LPX64" crosses index zero\n",
608 cat_llh->lgh_id.lgl_oid);
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);
639 int llog_cat_process_thread(void *data)
641 struct llog_process_cat_args *args = data;
642 struct llog_ctxt *ctxt = args->lpca_ctxt;
643 struct llog_handle *llh = NULL;
644 llog_cb_t cb = args->lpca_cb;
645 struct llog_thread_info *lgi;
650 cfs_daemonize_ctxt("ll_log_process");
652 rc = lu_env_init(&env, LCT_LOCAL);
655 lgi = llog_info(&env);
658 lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
659 rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
662 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
663 ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
664 lgi->lgi_logid.lgl_ogen, rc);
667 rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
669 CERROR("%s: llog_init_handle failed: rc = %d\n",
670 llh->lgh_ctxt->loc_obd->obd_name, rc);
671 GOTO(release_llh, rc);
675 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
676 if (rc != LLOG_PROC_BREAK && rc != 0)
677 CERROR("%s: llog_cat_process() failed: rc = %d\n",
678 llh->lgh_ctxt->loc_obd->obd_name, rc);
679 cb(&env, llh, NULL, NULL);
681 CWARN("No callback function for recovery\n");
685 * Make sure that all cached data is sent.
687 llog_sync(ctxt, NULL, 0);
688 GOTO(release_llh, rc);
690 rc = llog_cat_close(&env, llh);
692 CERROR("%s: llog_cat_close() failed: rc = %d\n",
693 llh->lgh_ctxt->loc_obd->obd_name, rc);
701 EXPORT_SYMBOL(llog_cat_process_thread);
704 static int llog_cat_reverse_process_cb(const struct lu_env *env,
705 struct llog_handle *cat_llh,
706 struct llog_rec_hdr *rec, void *data)
708 struct llog_process_data *d = data;
709 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
710 struct llog_handle *llh;
713 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
714 CERROR("invalid record in catalog\n");
717 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
718 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
719 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
721 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
723 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
724 cat_llh->lgh_ctxt->loc_obd->obd_name,
725 lir->lid_id.lgl_oid, rc);
729 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
730 llog_handle_put(llh);
734 int llog_cat_reverse_process(const struct lu_env *env,
735 struct llog_handle *cat_llh,
736 llog_cb_t cb, void *data)
738 struct llog_process_data d;
739 struct llog_process_cat_data cd;
740 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
744 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
748 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
749 CWARN("catalog "LPX64" crosses index zero\n",
750 cat_llh->lgh_id.lgl_oid);
752 cd.lpcd_first_idx = 0;
753 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
754 rc = llog_reverse_process(env, cat_llh,
755 llog_cat_reverse_process_cb,
760 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
761 cd.lpcd_last_idx = 0;
762 rc = llog_reverse_process(env, cat_llh,
763 llog_cat_reverse_process_cb,
766 rc = llog_reverse_process(env, cat_llh,
767 llog_cat_reverse_process_cb,
773 EXPORT_SYMBOL(llog_cat_reverse_process);
775 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
777 struct llog_log_hdr *llh = cathandle->lgh_hdr;
778 int i, bitmap_size, idx;
781 bitmap_size = LLOG_BITMAP_SIZE(llh);
782 if (llh->llh_cat_idx == (index - 1)) {
783 idx = llh->llh_cat_idx + 1;
784 llh->llh_cat_idx = idx;
785 if (idx == cathandle->lgh_last_idx)
787 for (i = (index + 1) % bitmap_size;
788 i != cathandle->lgh_last_idx;
789 i = (i + 1) % bitmap_size) {
790 if (!ext2_test_bit(i, llh->llh_bitmap)) {
791 idx = llh->llh_cat_idx + 1;
792 llh->llh_cat_idx = idx;
794 llh->llh_cat_idx = 0;
800 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
801 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
807 /* Cleanup deleted plain llog traces from catalog */
808 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
809 struct llog_handle *loghandle, int index)
814 if (loghandle != NULL) {
815 /* remove destroyed llog from catalog list and
816 * chd_current_log variable */
817 down_write(&cathandle->lgh_lock);
818 if (cathandle->u.chd.chd_current_log == loghandle)
819 cathandle->u.chd.chd_current_log = NULL;
820 cfs_list_del_init(&loghandle->u.phd.phd_entry);
821 up_write(&cathandle->lgh_lock);
822 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
823 /* llog was opened and keep in a list, close it now */
824 llog_close(env, loghandle);
826 /* remove plain llog entry from catalog by index */
827 llog_cat_set_first_idx(cathandle, index);
828 rc = llog_cancel_rec(env, cathandle, index);
830 CDEBUG(D_HA, "cancel plain log at index"
831 " %u of catalog "LPX64"\n",
832 index, cathandle->lgh_id.lgl_oid);
836 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
837 struct llog_rec_hdr *rec, void *data)
839 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
840 struct llog_handle *loghandle;
841 struct llog_log_hdr *llh;
846 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
847 CERROR("invalid record in catalog\n");
850 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
851 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
852 rec->lrh_index, cathandle->lgh_id.lgl_oid);
854 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
856 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
857 cathandle->lgh_ctxt->loc_obd->obd_name,
858 lir->lid_id.lgl_oid, rc);
859 if (rc == -ENOENT || rc == -ESTALE) {
860 /* remove index from catalog */
861 llog_cat_cleanup(env, cathandle, NULL, rec->lrh_index);
866 llh = loghandle->lgh_hdr;
867 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
868 (llh->llh_count == 1)) {
869 rc = llog_destroy(env, loghandle);
871 CERROR("%s: fail to destroy empty log: rc = %d\n",
872 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
874 llog_cat_cleanup(env, cathandle, loghandle,
875 loghandle->u.phd.phd_cookie.lgc_index);
877 llog_handle_put(loghandle);
881 EXPORT_SYMBOL(cat_cancel_cb);
883 /* helper to initialize catalog llog and process it to cancel */
884 int llog_cat_init_and_process(const struct lu_env *env,
885 struct llog_handle *llh)
889 rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
893 rc = llog_process_or_fork(env, llh, cat_cancel_cb, NULL, NULL, false);
895 CERROR("%s: llog_process() with cat_cancel_cb failed: rc = "
896 "%d\n", llh->lgh_ctxt->loc_obd->obd_name, rc);
899 EXPORT_SYMBOL(llog_cat_init_and_process);