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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/llog_cat.c
34 * OST<->MDS recovery logging infrastructure.
36 * Invariants in implementation:
37 * - we do not share logs among different OST<->MDS connections, so that
38 * if an OST or MDS fails it need only look at log(s) relevant to itself
40 * Author: Andreas Dilger <adilger@clusterfs.com>
41 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
42 * Author: Mikhail Pershin <mike.pershin@intel.com>
45 #define DEBUG_SUBSYSTEM S_LOG
48 #include <obd_class.h>
50 #include "llog_internal.h"
52 /* Create a new log handle and add it to the open list.
53 * This log handle will be closed when all of the records in it are removed.
55 * Assumes caller has already pushed us into the kernel context and is locking.
57 static int llog_cat_new_log(const struct lu_env *env,
58 struct llog_handle *cathandle,
59 struct llog_handle *loghandle,
62 struct llog_thread_info *lgi = llog_info(env);
63 struct llog_logid_rec *rec = &lgi->lgi_logid;
64 struct thandle *handle = NULL;
65 struct dt_device *dt = NULL;
66 struct llog_log_hdr *llh = cathandle->lgh_hdr;
71 index = (cathandle->lgh_last_idx + 1) %
72 (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ? (cfs_fail_val + 1) :
73 LLOG_HDR_BITMAP_SIZE(llh));
75 /* check that new llog index will not overlap with the first one.
76 * - llh_cat_idx is the index just before the first/oldest still in-use
78 * - lgh_last_idx is the last/newest used index in catalog
80 * When catalog is not wrapped yet then lgh_last_idx is always larger
81 * than llh_cat_idx. After the wrap around lgh_last_idx re-starts
82 * from 0 and llh_cat_idx becomes the upper limit for it
84 * Check if catalog has already wrapped around or not by comparing
85 * last_idx and cat_idx */
86 if ((index == llh->llh_cat_idx + 1 && llh->llh_count > 1) ||
87 (index == 0 && llh->llh_cat_idx == 0)) {
88 CWARN("%s: there are no more free slots in catalog\n",
89 loghandle->lgh_ctxt->loc_obd->obd_name);
93 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
96 if (loghandle->lgh_hdr != NULL) {
97 /* If llog object is remote and creation is failed, lgh_hdr
98 * might be left over here, free it first */
99 LASSERT(!llog_exist(loghandle));
100 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
101 loghandle->lgh_hdr = NULL;
105 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
107 handle = dt_trans_create(env, dt);
109 RETURN(PTR_ERR(handle));
111 /* Create update llog object synchronously, which
112 * happens during inialization process see
113 * lod_sub_prep_llog(), to make sure the update
114 * llog object is created before corss-MDT writing
115 * updates into the llog object */
116 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
119 handle->th_wait_submit = 1;
121 rc = llog_declare_create(env, loghandle, handle);
125 rec->lid_hdr.lrh_len = sizeof(*rec);
126 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
127 rec->lid_id = loghandle->lgh_id;
128 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
133 rc = dt_trans_start_local(env, dt, handle);
140 rc = llog_create(env, loghandle, th);
141 /* if llog is already created, no need to initialize it */
144 } else if (rc != 0) {
145 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
146 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
150 rc = llog_init_handle(env, loghandle,
151 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
152 &cathandle->lgh_hdr->llh_tgtuuid);
156 /* build the record for this log in the catalog */
157 rec->lid_hdr.lrh_len = sizeof(*rec);
158 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
159 rec->lid_id = loghandle->lgh_id;
161 /* append the new record into catalog. The new index will be
162 * assigned to the record and updated in rec header */
163 rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
164 &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
166 GOTO(out_destroy, rc);
168 CDEBUG(D_OTHER, "new plain log "DOSTID":%x for index %u of catalog"
169 DOSTID"\n", POSTID(&loghandle->lgh_id.lgl_oi),
170 loghandle->lgh_id.lgl_ogen, rec->lid_hdr.lrh_index,
171 POSTID(&cathandle->lgh_id.lgl_oi));
173 loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
175 if (handle != NULL) {
176 handle->th_result = rc >= 0 ? 0 : rc;
177 dt_trans_stop(env, dt, handle);
182 /* to signal llog_cat_close() it shouldn't try to destroy the llog,
183 * we want to destroy it in this transaction, otherwise the object
184 * becomes an orphan */
185 loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
186 /* this is to mimic full log, so another llog_cat_current_log()
187 * can skip it and ask for another onet */
188 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) + 1;
189 llog_trans_destroy(env, loghandle, th);
193 /* Open an existent log handle and add it to the open list.
194 * This log handle will be closed when all of the records in it are removed.
196 * Assumes caller has already pushed us into the kernel context and is locking.
197 * We return a lock on the handle to ensure nobody yanks it from us.
199 * This takes extra reference on llog_handle via llog_handle_get() and require
200 * this reference to be put by caller using llog_handle_put()
202 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
203 struct llog_handle **res, struct llog_logid *logid)
205 struct llog_handle *loghandle;
211 if (cathandle == NULL)
214 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
215 down_write(&cathandle->lgh_lock);
216 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
218 struct llog_logid *cgl = &loghandle->lgh_id;
220 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
221 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
222 if (cgl->lgl_ogen != logid->lgl_ogen) {
223 CERROR("%s: log "DOSTID" generation %x != %x\n",
224 loghandle->lgh_ctxt->loc_obd->obd_name,
225 POSTID(&logid->lgl_oi), cgl->lgl_ogen,
229 loghandle->u.phd.phd_cat_handle = cathandle;
230 up_write(&cathandle->lgh_lock);
234 up_write(&cathandle->lgh_lock);
236 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
239 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
240 cathandle->lgh_ctxt->loc_obd->obd_name,
241 POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
245 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
247 llog_close(env, loghandle);
252 down_write(&cathandle->lgh_lock);
253 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
254 up_write(&cathandle->lgh_lock);
256 loghandle->u.phd.phd_cat_handle = cathandle;
257 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
258 loghandle->u.phd.phd_cookie.lgc_index =
259 loghandle->lgh_hdr->llh_cat_idx;
262 llog_handle_get(loghandle);
267 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
269 struct llog_handle *loghandle, *n;
274 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
276 struct llog_log_hdr *llh = loghandle->lgh_hdr;
279 /* unlink open-not-created llogs */
280 list_del_init(&loghandle->u.phd.phd_entry);
281 llh = loghandle->lgh_hdr;
282 if (loghandle->lgh_obj != NULL && llh != NULL &&
283 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
284 (llh->llh_count == 1)) {
285 rc = llog_destroy(env, loghandle);
287 CERROR("%s: failure destroying log during "
288 "cleanup: rc = %d\n",
289 loghandle->lgh_ctxt->loc_obd->obd_name,
292 index = loghandle->u.phd.phd_cookie.lgc_index;
293 llog_cat_cleanup(env, cathandle, NULL, index);
295 llog_close(env, loghandle);
297 /* if handle was stored in ctxt, remove it too */
298 if (cathandle->lgh_ctxt->loc_handle == cathandle)
299 cathandle->lgh_ctxt->loc_handle = NULL;
300 rc = llog_close(env, cathandle);
303 EXPORT_SYMBOL(llog_cat_close);
306 * lockdep markers for nested struct llog_handle::lgh_lock locking.
313 /** Return the currently active log handle. If the current log handle doesn't
314 * have enough space left for the current record, start a new one.
316 * If reclen is 0, we only want to know what the currently active log is,
317 * otherwise we get a lock on this log so nobody can steal our space.
319 * Assumes caller has already pushed us into the kernel context and is locking.
321 * NOTE: loghandle is write-locked upon successful return
323 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
326 struct llog_handle *loghandle = NULL;
330 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
331 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
332 GOTO(next, loghandle);
335 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
336 loghandle = cathandle->u.chd.chd_current_log;
338 struct llog_log_hdr *llh;
340 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
341 llh = loghandle->lgh_hdr;
342 if (llh == NULL || !llog_is_full(loghandle)) {
343 up_read(&cathandle->lgh_lock);
346 up_write(&loghandle->lgh_lock);
349 up_read(&cathandle->lgh_lock);
351 /* time to use next log */
353 /* first, we have to make sure the state hasn't changed */
354 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
355 loghandle = cathandle->u.chd.chd_current_log;
357 struct llog_log_hdr *llh;
359 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
360 llh = loghandle->lgh_hdr;
362 if (!llog_is_full(loghandle))
363 GOTO(out_unlock, loghandle);
365 up_write(&loghandle->lgh_lock);
369 /* Sigh, the chd_next_log and chd_current_log is initialized
370 * in declare phase, and we do not serialize the catlog
371 * accessing, so it might be possible the llog creation
372 * thread (see llog_cat_declare_add_rec()) did not create
373 * llog successfully, then the following thread might
374 * meet this situation. */
375 if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
376 CERROR("%s: next log does not exist!\n",
377 cathandle->lgh_ctxt->loc_obd->obd_name);
378 loghandle = ERR_PTR(-EIO);
379 if (cathandle->u.chd.chd_next_log == NULL) {
380 /* Store the error in chd_next_log, so
381 * the following process can get correct
383 cathandle->u.chd.chd_next_log = loghandle;
385 GOTO(out_unlock, loghandle);
388 CDEBUG(D_INODE, "use next log\n");
390 loghandle = cathandle->u.chd.chd_next_log;
391 cathandle->u.chd.chd_current_log = loghandle;
392 cathandle->u.chd.chd_next_log = NULL;
393 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
396 up_write(&cathandle->lgh_lock);
401 static int llog_cat_update_header(const struct lu_env *env,
402 struct llog_handle *cathandle)
404 struct llog_handle *loghandle;
409 down_write(&cathandle->lgh_lock);
410 if (!cathandle->lgh_stale) {
411 up_write(&cathandle->lgh_lock);
414 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
416 if (!llog_exist(loghandle))
419 rc = llog_read_header(env, loghandle, NULL);
421 up_write(&cathandle->lgh_lock);
425 rc = llog_read_header(env, cathandle, NULL);
427 cathandle->lgh_stale = 0;
428 up_write(&cathandle->lgh_lock);
435 /* Add a single record to the recovery log(s) using a catalog
436 * Returns as llog_write_record
438 * Assumes caller has already pushed us into the kernel context.
440 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
441 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
444 struct llog_handle *loghandle;
448 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
451 loghandle = llog_cat_current_log(cathandle, th);
452 if (IS_ERR(loghandle))
453 RETURN(PTR_ERR(loghandle));
455 /* loghandle is already locked by llog_cat_current_log() for us */
456 if (!llog_exist(loghandle)) {
457 rc = llog_cat_new_log(env, cathandle, loghandle, th);
459 up_write(&loghandle->lgh_lock);
460 /* nobody should be trying to use this llog */
461 down_write(&cathandle->lgh_lock);
462 if (cathandle->u.chd.chd_current_log == loghandle)
463 cathandle->u.chd.chd_current_log = NULL;
464 up_write(&cathandle->lgh_lock);
468 /* now let's try to add the record */
469 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
471 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
472 "llog_write_rec %d: lh=%p\n", rc, loghandle);
473 /* -ENOSPC is returned if no empty records left
474 * and when it's lack of space on the stogage.
475 * there is no point to try again if it's the second
476 * case. many callers (like llog test) expect ENOSPC,
477 * so we preserve this error code, but look for the
478 * actual cause here */
479 if (rc == -ENOSPC && llog_is_full(loghandle))
482 up_write(&loghandle->lgh_lock);
484 if (rc == -ENOBUFS) {
487 CERROR("%s: error on 2nd llog: rc = %d\n",
488 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
493 EXPORT_SYMBOL(llog_cat_add_rec);
495 int llog_cat_declare_add_rec(const struct lu_env *env,
496 struct llog_handle *cathandle,
497 struct llog_rec_hdr *rec, struct thandle *th)
499 struct llog_thread_info *lgi = llog_info(env);
500 struct llog_logid_rec *lirec = &lgi->lgi_logid;
501 struct llog_handle *loghandle, *next;
506 if (cathandle->u.chd.chd_current_log == NULL) {
507 /* declare new plain llog */
508 down_write(&cathandle->lgh_lock);
509 if (cathandle->u.chd.chd_current_log == NULL) {
510 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
511 NULL, NULL, LLOG_OPEN_NEW);
513 cathandle->u.chd.chd_current_log = loghandle;
514 list_add_tail(&loghandle->u.phd.phd_entry,
515 &cathandle->u.chd.chd_head);
518 up_write(&cathandle->lgh_lock);
519 } else if (cathandle->u.chd.chd_next_log == NULL ||
520 IS_ERR(cathandle->u.chd.chd_next_log)) {
521 /* declare next plain llog */
522 down_write(&cathandle->lgh_lock);
523 if (cathandle->u.chd.chd_next_log == NULL ||
524 IS_ERR(cathandle->u.chd.chd_next_log)) {
525 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
526 NULL, NULL, LLOG_OPEN_NEW);
528 cathandle->u.chd.chd_next_log = loghandle;
529 list_add_tail(&loghandle->u.phd.phd_entry,
530 &cathandle->u.chd.chd_head);
533 up_write(&cathandle->lgh_lock);
538 lirec->lid_hdr.lrh_len = sizeof(*lirec);
540 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
541 if (dt_object_remote(cathandle->lgh_obj)) {
542 /* For remote operation, if we put the llog object
543 * creation in the current transaction, then the
544 * llog object will not be created on the remote
545 * target until the transaction stop, if other
546 * operations start before the transaction stop,
547 * and use the same llog object, will be dependent
548 * on the success of this transaction. So let's
549 * create the llog object synchronously here to
550 * remove the dependency. */
552 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
553 loghandle = cathandle->u.chd.chd_current_log;
554 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
555 if (cathandle->lgh_stale) {
556 up_write(&loghandle->lgh_lock);
557 up_read(&cathandle->lgh_lock);
558 GOTO(out, rc = -EIO);
560 if (!llog_exist(loghandle)) {
561 rc = llog_cat_new_log(env, cathandle, loghandle,
564 cathandle->lgh_stale = 1;
566 up_write(&loghandle->lgh_lock);
567 up_read(&cathandle->lgh_lock);
569 rc = llog_cat_update_header(env, cathandle);
577 rc = llog_declare_create(env,
578 cathandle->u.chd.chd_current_log, th);
581 llog_declare_write_rec(env, cathandle,
582 &lirec->lid_hdr, -1, th);
587 /* declare records in the llogs */
588 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
591 down_write(&cathandle->lgh_lock);
592 if (cathandle->lgh_stale) {
593 up_write(&cathandle->lgh_lock);
594 GOTO(out, rc = -EIO);
597 cathandle->lgh_stale = 1;
598 up_write(&cathandle->lgh_lock);
599 rc = llog_cat_update_header(env, cathandle);
607 next = cathandle->u.chd.chd_next_log;
608 if (!IS_ERR_OR_NULL(next)) {
609 if (!llog_exist(next)) {
610 if (dt_object_remote(cathandle->lgh_obj)) {
611 /* For remote operation, if we put the llog
612 * object creation in the current transaction,
613 * then the llog object will not be created on
614 * the remote target until the transaction stop,
615 * if other operations start before the
616 * transaction stop, and use the same llog
617 * object, will be dependent on the success of
618 * this transaction. So let's create the llog
619 * object synchronously here to remove the
621 down_write_nested(&cathandle->lgh_lock,
623 next = cathandle->u.chd.chd_next_log;
624 if (IS_ERR_OR_NULL(next)) {
625 /* Sigh, another thread just tried,
626 * let's fail as well */
627 up_write(&cathandle->lgh_lock);
635 down_write_nested(&next->lgh_lock, LLOGH_LOG);
636 if (!llog_exist(next)) {
637 rc = llog_cat_new_log(env, cathandle,
640 cathandle->u.chd.chd_next_log =
643 up_write(&next->lgh_lock);
644 up_write(&cathandle->lgh_lock);
648 rc = llog_declare_create(env, next, th);
649 llog_declare_write_rec(env, cathandle,
650 &lirec->lid_hdr, -1, th);
653 /* XXX: we hope for declarations made for existing llog
654 * this might be not correct with some backends
655 * where declarations are expected against specific
656 * object like ZFS with full debugging enabled */
657 /*llog_declare_write_rec(env, next, rec, -1, th);*/
662 EXPORT_SYMBOL(llog_cat_declare_add_rec);
664 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
665 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
667 struct llog_ctxt *ctxt;
668 struct dt_device *dt;
669 struct thandle *th = NULL;
672 ctxt = cathandle->lgh_ctxt;
674 LASSERT(ctxt->loc_exp);
676 LASSERT(cathandle->lgh_obj != NULL);
677 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
679 th = dt_trans_create(env, dt);
683 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
687 rc = dt_trans_start_local(env, dt, th);
690 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
692 dt_trans_stop(env, dt, th);
695 EXPORT_SYMBOL(llog_cat_add);
697 /* For each cookie in the cookie array, we clear the log in-use bit and either:
698 * - the log is empty, so mark it free in the catalog header and delete it
699 * - the log is not empty, just write out the log header
701 * The cookies may be in different log files, so we need to get new logs
704 * Assumes caller has already pushed us into the kernel context.
706 int llog_cat_cancel_records(const struct lu_env *env,
707 struct llog_handle *cathandle, int count,
708 struct llog_cookie *cookies)
710 int i, index, rc = 0, failed = 0;
714 for (i = 0; i < count; i++, cookies++) {
715 struct llog_handle *loghandle;
716 struct llog_logid *lgl = &cookies->lgc_lgl;
719 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
721 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
722 cathandle->lgh_ctxt->loc_obd->obd_name,
723 POSTID(&lgl->lgl_oi), rc);
728 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
729 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
730 index = loghandle->u.phd.phd_cookie.lgc_index;
731 rc = llog_cat_cleanup(env, cathandle, loghandle,
733 } else if (lrc == -ENOENT) {
734 if (rc == 0) /* ENOENT shouldn't rewrite any error */
736 } else if (lrc < 0) {
740 llog_handle_put(loghandle);
743 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
744 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
749 EXPORT_SYMBOL(llog_cat_cancel_records);
751 static int llog_cat_process_cb(const struct lu_env *env,
752 struct llog_handle *cat_llh,
753 struct llog_rec_hdr *rec, void *data)
755 struct llog_process_data *d = data;
756 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
757 struct llog_handle *llh;
758 struct llog_log_hdr *hdr;
762 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
763 CERROR("invalid record in catalog\n");
766 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
767 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
768 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
770 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
772 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
773 cat_llh->lgh_ctxt->loc_obd->obd_name,
774 POSTID(&lir->lid_id.lgl_oi), rc);
775 if (rc == -ENOENT || rc == -ESTALE) {
776 /* After a server crash, a stub of index
777 * record in catlog could be kept, because
778 * plain log destroy + catlog index record
779 * deletion are not atomic. So we end up with
780 * an index but no actual record. Destroy the
781 * index and move on. */
782 rc = llog_cat_cleanup(env, cat_llh, NULL,
789 /* clean old empty llogs, do not consider current llog in use */
790 /* ignore remote (lgh_obj=NULL) llogs */
792 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
793 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
794 llh != cat_llh->u.chd.chd_current_log) {
795 rc = llog_destroy(env, llh);
797 CERROR("%s: fail to destroy empty log: rc = %d\n",
798 llh->lgh_ctxt->loc_obd->obd_name, rc);
799 GOTO(out, rc = LLOG_DEL_PLAIN);
802 if (rec->lrh_index < d->lpd_startcat) {
803 /* Skip processing of the logs until startcat */
805 } else if (d->lpd_startidx > 0) {
806 struct llog_process_cat_data cd;
808 cd.lpcd_first_idx = d->lpd_startidx;
809 cd.lpcd_last_idx = 0;
810 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
812 /* Continue processing the next log from idx 0 */
815 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
820 /* The empty plain log was destroyed while processing */
821 if (rc == LLOG_DEL_PLAIN)
822 rc = llog_cat_cleanup(env, cat_llh, llh,
823 llh->u.phd.phd_cookie.lgc_index);
824 llog_handle_put(llh);
829 int llog_cat_process_or_fork(const struct lu_env *env,
830 struct llog_handle *cat_llh, llog_cb_t cat_cb,
831 llog_cb_t cb, void *data, int startcat,
832 int startidx, bool fork)
834 struct llog_process_data d;
835 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
839 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
842 d.lpd_startcat = startcat;
843 d.lpd_startidx = startidx;
845 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
846 llh->llh_count > 1) {
847 struct llog_process_cat_data cd;
849 CWARN("catlog "DOSTID" crosses index zero\n",
850 POSTID(&cat_llh->lgh_id.lgl_oi));
852 cd.lpcd_first_idx = llh->llh_cat_idx;
853 cd.lpcd_last_idx = 0;
854 rc = llog_process_or_fork(env, cat_llh, cat_cb,
859 cd.lpcd_first_idx = 0;
860 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
861 rc = llog_process_or_fork(env, cat_llh, cat_cb,
864 rc = llog_process_or_fork(env, cat_llh, cat_cb,
871 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
872 llog_cb_t cb, void *data, int startcat, int startidx)
874 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
875 cb, data, startcat, startidx, false);
877 EXPORT_SYMBOL(llog_cat_process);
879 static int llog_cat_size_cb(const struct lu_env *env,
880 struct llog_handle *cat_llh,
881 struct llog_rec_hdr *rec, void *data)
883 struct llog_process_data *d = data;
884 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
885 struct llog_handle *llh;
887 __u64 *cum_size = d->lpd_data;
891 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
892 CERROR("%s: invalid record in catalog, rc = %d\n",
893 cat_llh->lgh_ctxt->loc_obd->obd_name, -EINVAL);
896 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
897 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
898 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
900 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
902 CWARN("%s: cannot find handle for llog "DOSTID": rc = %d\n",
903 cat_llh->lgh_ctxt->loc_obd->obd_name,
904 POSTID(&lir->lid_id.lgl_oi), rc);
907 size = llog_size(env, llh);
910 CDEBUG(D_INFO, "Add llog entry "DOSTID" size %llu\n",
911 POSTID(&llh->lgh_id.lgl_oi), size);
913 llog_handle_put(llh);
919 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
921 __u64 size = llog_size(env, cat_llh);
923 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
924 NULL, &size, 0, 0, false);
928 EXPORT_SYMBOL(llog_cat_size);
930 static int llog_cat_reverse_process_cb(const struct lu_env *env,
931 struct llog_handle *cat_llh,
932 struct llog_rec_hdr *rec, void *data)
934 struct llog_process_data *d = data;
935 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
936 struct llog_handle *llh;
937 struct llog_log_hdr *hdr;
940 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
941 CERROR("invalid record in catalog\n");
944 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
945 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
946 le32_to_cpu(rec->lrh_index), POSTID(&cat_llh->lgh_id.lgl_oi));
948 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
950 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
951 cat_llh->lgh_ctxt->loc_obd->obd_name,
952 POSTID(&lir->lid_id.lgl_oi), rc);
953 if (rc == -ENOENT || rc == -ESTALE) {
954 /* After a server crash, a stub of index
955 * record in catlog could be kept, because
956 * plain log destroy + catlog index record
957 * deletion are not atomic. So we end up with
958 * an index but no actual record. Destroy the
959 * index and move on. */
960 rc = llog_cat_cleanup(env, cat_llh, NULL,
967 /* clean old empty llogs, do not consider current llog in use */
969 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
970 hdr->llh_count == 1 &&
971 llh != cat_llh->u.chd.chd_current_log) {
972 rc = llog_destroy(env, llh);
974 CERROR("%s: fail to destroy empty log: rc = %d\n",
975 llh->lgh_ctxt->loc_obd->obd_name, rc);
976 GOTO(out, rc = LLOG_DEL_PLAIN);
979 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
982 /* The empty plain was destroyed while processing */
983 if (rc == LLOG_DEL_PLAIN)
984 rc = llog_cat_cleanup(env, cat_llh, llh,
985 llh->u.phd.phd_cookie.lgc_index);
987 llog_handle_put(llh);
991 int llog_cat_reverse_process(const struct lu_env *env,
992 struct llog_handle *cat_llh,
993 llog_cb_t cb, void *data)
995 struct llog_process_data d;
996 struct llog_process_cat_data cd;
997 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1001 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1005 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1006 llh->llh_count > 1) {
1007 CWARN("catalog "DOSTID" crosses index zero\n",
1008 POSTID(&cat_llh->lgh_id.lgl_oi));
1010 cd.lpcd_first_idx = 0;
1011 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1012 rc = llog_reverse_process(env, cat_llh,
1013 llog_cat_reverse_process_cb,
1018 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1019 cd.lpcd_last_idx = 0;
1020 rc = llog_reverse_process(env, cat_llh,
1021 llog_cat_reverse_process_cb,
1024 rc = llog_reverse_process(env, cat_llh,
1025 llog_cat_reverse_process_cb,
1031 EXPORT_SYMBOL(llog_cat_reverse_process);
1033 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1035 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1040 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1042 * The llh_cat_idx equals to the first used index minus 1
1043 * so if we canceled the first index then llh_cat_idx
1046 if (llh->llh_cat_idx == (idx - 1)) {
1047 llh->llh_cat_idx = idx;
1049 while (idx != cathandle->lgh_last_idx) {
1050 idx = (idx + 1) % bitmap_size;
1051 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1052 /* update llh_cat_idx for each unset bit,
1053 * expecting the next one is set */
1054 llh->llh_cat_idx = idx;
1055 } else if (idx == 0) {
1056 /* skip header bit */
1057 llh->llh_cat_idx = 0;
1060 /* the first index is found */
1065 CDEBUG(D_RPCTRACE, "Set catlog "DOSTID" first idx %u,"
1066 " (last_idx %u)\n", POSTID(&cathandle->lgh_id.lgl_oi),
1067 llh->llh_cat_idx, cathandle->lgh_last_idx);
1073 /* Cleanup deleted plain llog traces from catalog */
1074 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1075 struct llog_handle *loghandle, int index)
1080 if (loghandle != NULL) {
1081 /* remove destroyed llog from catalog list and
1082 * chd_current_log variable */
1083 down_write(&cathandle->lgh_lock);
1084 if (cathandle->u.chd.chd_current_log == loghandle)
1085 cathandle->u.chd.chd_current_log = NULL;
1086 list_del_init(&loghandle->u.phd.phd_entry);
1087 up_write(&cathandle->lgh_lock);
1088 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1089 /* llog was opened and keep in a list, close it now */
1090 llog_close(env, loghandle);
1093 /* do not attempt to cleanup on-disk llog if on client side */
1094 if (cathandle->lgh_obj == NULL)
1097 /* remove plain llog entry from catalog by index */
1098 llog_cat_set_first_idx(cathandle, index);
1099 rc = llog_cancel_rec(env, cathandle, index);
1101 CDEBUG(D_HA, "cancel plain log at index"
1102 " %u of catalog "DOSTID"\n",
1103 index, POSTID(&cathandle->lgh_id.lgl_oi));