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 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
353 "llog_write_rec %d: lh=%p\n", rc, loghandle);
354 up_write(&loghandle->lgh_lock);
356 /* try to use next log */
357 loghandle = llog_cat_current_log(cathandle, th);
358 LASSERT(!IS_ERR(loghandle));
359 /* new llog can be created concurrently */
360 if (!llog_exist(loghandle)) {
361 rc = llog_cat_new_log(env, cathandle, loghandle, th);
363 up_write(&loghandle->lgh_lock);
367 /* now let's try to add the record */
368 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
371 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
372 up_write(&loghandle->lgh_lock);
377 EXPORT_SYMBOL(llog_cat_add_rec);
379 int llog_cat_declare_add_rec(const struct lu_env *env,
380 struct llog_handle *cathandle,
381 struct llog_rec_hdr *rec, struct thandle *th)
383 struct llog_handle *loghandle, *next;
388 if (cathandle->u.chd.chd_current_log == NULL) {
389 /* declare new plain llog */
390 down_write(&cathandle->lgh_lock);
391 if (cathandle->u.chd.chd_current_log == NULL) {
392 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
393 NULL, NULL, LLOG_OPEN_NEW);
395 cathandle->u.chd.chd_current_log = loghandle;
396 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
397 &cathandle->u.chd.chd_head);
400 up_write(&cathandle->lgh_lock);
401 } else if (cathandle->u.chd.chd_next_log == NULL) {
402 /* declare next plain llog */
403 down_write(&cathandle->lgh_lock);
404 if (cathandle->u.chd.chd_next_log == NULL) {
405 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
406 NULL, NULL, LLOG_OPEN_NEW);
408 cathandle->u.chd.chd_next_log = loghandle;
409 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
410 &cathandle->u.chd.chd_head);
413 up_write(&cathandle->lgh_lock);
418 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
419 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
423 llog_declare_write_rec(env, cathandle, NULL, -1, th);
425 /* declare records in the llogs */
426 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
431 next = cathandle->u.chd.chd_next_log;
433 if (!llog_exist(next)) {
434 rc = llog_declare_create(env, next, th);
435 llog_declare_write_rec(env, cathandle, NULL, -1, th);
437 llog_declare_write_rec(env, next, rec, -1, th);
442 EXPORT_SYMBOL(llog_cat_declare_add_rec);
444 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
445 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
448 struct llog_ctxt *ctxt;
449 struct dt_device *dt;
450 struct thandle *th = NULL;
453 ctxt = cathandle->lgh_ctxt;
455 LASSERT(ctxt->loc_exp);
457 if (cathandle->lgh_obj != NULL) {
458 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
461 th = dt_trans_create(env, dt);
465 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
469 rc = dt_trans_start_local(env, dt, th);
472 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
474 dt_trans_stop(env, dt, th);
475 } else { /* lvfs compat code */
476 LASSERT(cathandle->lgh_file != NULL);
477 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
479 rc = llog_cat_add_rec(env, cathandle, rec, reccookie,
484 EXPORT_SYMBOL(llog_cat_add);
486 /* For each cookie in the cookie array, we clear the log in-use bit and either:
487 * - the log is empty, so mark it free in the catalog header and delete it
488 * - the log is not empty, just write out the log header
490 * The cookies may be in different log files, so we need to get new logs
493 * Assumes caller has already pushed us into the kernel context.
495 int llog_cat_cancel_records(const struct lu_env *env,
496 struct llog_handle *cathandle, int count,
497 struct llog_cookie *cookies)
499 int i, index, rc = 0, failed = 0;
503 for (i = 0; i < count; i++, cookies++) {
504 struct llog_handle *loghandle;
505 struct llog_logid *lgl = &cookies->lgc_lgl;
508 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
510 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
511 cathandle->lgh_ctxt->loc_obd->obd_name,
517 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
518 if (lrc == 1) { /* log has been destroyed */
519 index = loghandle->u.phd.phd_cookie.lgc_index;
520 rc = llog_cat_cleanup(env, cathandle, loghandle,
522 } else if (lrc == -ENOENT) {
523 if (rc == 0) /* ENOENT shouldn't rewrite any error */
525 } else if (lrc < 0) {
529 llog_handle_put(loghandle);
532 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
533 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
538 EXPORT_SYMBOL(llog_cat_cancel_records);
540 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
541 struct llog_rec_hdr *rec, void *data)
543 struct llog_process_data *d = data;
544 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
545 struct llog_handle *llh;
549 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
550 CERROR("invalid record in catalog\n");
553 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
554 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
555 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
557 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
559 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
560 cat_llh->lgh_ctxt->loc_obd->obd_name,
561 lir->lid_id.lgl_oid, rc);
565 if (rec->lrh_index < d->lpd_startcat)
566 /* Skip processing of the logs until startcat */
569 if (d->lpd_startidx > 0) {
570 struct llog_process_cat_data cd;
572 cd.lpcd_first_idx = d->lpd_startidx;
573 cd.lpcd_last_idx = 0;
574 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
576 /* Continue processing the next log from idx 0 */
579 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
582 llog_handle_put(llh);
587 int llog_cat_process_or_fork(const struct lu_env *env,
588 struct llog_handle *cat_llh,
589 llog_cb_t cb, void *data, int startcat,
590 int startidx, bool fork)
592 struct llog_process_data d;
593 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
597 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
600 d.lpd_startcat = startcat;
601 d.lpd_startidx = startidx;
603 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
604 struct llog_process_cat_data cd;
606 CWARN("catlog "LPX64" crosses index zero\n",
607 cat_llh->lgh_id.lgl_oid);
609 cd.lpcd_first_idx = llh->llh_cat_idx;
610 cd.lpcd_last_idx = 0;
611 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
616 cd.lpcd_first_idx = 0;
617 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
618 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
621 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
627 EXPORT_SYMBOL(llog_cat_process_or_fork);
629 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
630 llog_cb_t cb, void *data, int startcat, int startidx)
632 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
635 EXPORT_SYMBOL(llog_cat_process);
638 int llog_cat_process_thread(void *data)
640 struct llog_process_cat_args *args = data;
641 struct llog_ctxt *ctxt = args->lpca_ctxt;
642 struct llog_handle *llh = NULL;
643 llog_cb_t cb = args->lpca_cb;
644 struct llog_thread_info *lgi;
649 cfs_daemonize_ctxt("ll_log_process");
651 rc = lu_env_init(&env, LCT_LOCAL);
654 lgi = llog_info(&env);
657 lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
658 rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
661 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
662 ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
663 lgi->lgi_logid.lgl_ogen, rc);
666 rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
668 CERROR("%s: llog_init_handle failed: rc = %d\n",
669 llh->lgh_ctxt->loc_obd->obd_name, rc);
670 GOTO(release_llh, rc);
674 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
675 if (rc != LLOG_PROC_BREAK && rc != 0)
676 CERROR("%s: llog_cat_process() failed: rc = %d\n",
677 llh->lgh_ctxt->loc_obd->obd_name, rc);
678 cb(&env, llh, NULL, NULL);
680 CWARN("No callback function for recovery\n");
684 * Make sure that all cached data is sent.
686 llog_sync(ctxt, NULL, 0);
687 GOTO(release_llh, rc);
689 rc = llog_cat_close(&env, llh);
691 CERROR("%s: llog_cat_close() failed: rc = %d\n",
692 llh->lgh_ctxt->loc_obd->obd_name, rc);
700 EXPORT_SYMBOL(llog_cat_process_thread);
703 static int llog_cat_reverse_process_cb(const struct lu_env *env,
704 struct llog_handle *cat_llh,
705 struct llog_rec_hdr *rec, void *data)
707 struct llog_process_data *d = data;
708 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
709 struct llog_handle *llh;
712 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
713 CERROR("invalid record in catalog\n");
716 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
717 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
718 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
720 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
722 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
723 cat_llh->lgh_ctxt->loc_obd->obd_name,
724 lir->lid_id.lgl_oid, rc);
728 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
729 llog_handle_put(llh);
733 int llog_cat_reverse_process(const struct lu_env *env,
734 struct llog_handle *cat_llh,
735 llog_cb_t cb, void *data)
737 struct llog_process_data d;
738 struct llog_process_cat_data cd;
739 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
743 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
747 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
748 CWARN("catalog "LPX64" crosses index zero\n",
749 cat_llh->lgh_id.lgl_oid);
751 cd.lpcd_first_idx = 0;
752 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
753 rc = llog_reverse_process(env, cat_llh,
754 llog_cat_reverse_process_cb,
759 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
760 cd.lpcd_last_idx = 0;
761 rc = llog_reverse_process(env, cat_llh,
762 llog_cat_reverse_process_cb,
765 rc = llog_reverse_process(env, cat_llh,
766 llog_cat_reverse_process_cb,
772 EXPORT_SYMBOL(llog_cat_reverse_process);
774 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
776 struct llog_log_hdr *llh = cathandle->lgh_hdr;
777 int i, bitmap_size, idx;
780 bitmap_size = LLOG_BITMAP_SIZE(llh);
781 if (llh->llh_cat_idx == (index - 1)) {
782 idx = llh->llh_cat_idx + 1;
783 llh->llh_cat_idx = idx;
784 if (idx == cathandle->lgh_last_idx)
786 for (i = (index + 1) % bitmap_size;
787 i != cathandle->lgh_last_idx;
788 i = (i + 1) % bitmap_size) {
789 if (!ext2_test_bit(i, llh->llh_bitmap)) {
790 idx = llh->llh_cat_idx + 1;
791 llh->llh_cat_idx = idx;
793 llh->llh_cat_idx = 0;
799 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
800 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
806 /* Cleanup deleted plain llog traces from catalog */
807 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
808 struct llog_handle *loghandle, int index)
813 if (loghandle != NULL) {
814 /* remove destroyed llog from catalog list and
815 * chd_current_log variable */
816 down_write(&cathandle->lgh_lock);
817 if (cathandle->u.chd.chd_current_log == loghandle)
818 cathandle->u.chd.chd_current_log = NULL;
819 cfs_list_del_init(&loghandle->u.phd.phd_entry);
820 up_write(&cathandle->lgh_lock);
821 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
822 /* llog was opened and keep in a list, close it now */
823 llog_close(env, loghandle);
825 /* remove plain llog entry from catalog by index */
826 llog_cat_set_first_idx(cathandle, index);
827 rc = llog_cancel_rec(env, cathandle, index);
829 CDEBUG(D_HA, "cancel plain log at index"
830 " %u of catalog "LPX64"\n",
831 index, cathandle->lgh_id.lgl_oid);
835 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
836 struct llog_rec_hdr *rec, void *data)
838 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
839 struct llog_handle *loghandle;
840 struct llog_log_hdr *llh;
845 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
846 CERROR("%s: invalid record in catalog\n",
847 loghandle->lgh_ctxt->loc_obd->obd_name);
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);