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, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog_cat.c
38 * OST<->MDS recovery logging infrastructure.
40 * Invariants in implementation:
41 * - we do not share logs among different OST<->MDS connections, so that
42 * if an OST or MDS fails it need only look at log(s) relevant to itself
44 * Author: Andreas Dilger <adilger@clusterfs.com>
45 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
46 * Author: Mikhail Pershin <mike.pershin@intel.com>
49 #define DEBUG_SUBSYSTEM S_LOG
52 #include <obd_class.h>
54 #include "llog_internal.h"
56 /* Create a new log handle and add it to the open list.
57 * This log handle will be closed when all of the records in it are removed.
59 * Assumes caller has already pushed us into the kernel context and is locking.
61 static int llog_cat_new_log(const struct lu_env *env,
62 struct llog_handle *cathandle,
63 struct llog_handle *loghandle,
66 struct llog_thread_info *lgi = llog_info(env);
67 struct llog_logid_rec *rec = &lgi->lgi_logid;
68 struct thandle *handle = NULL;
69 struct dt_device *dt = NULL;
70 struct llog_log_hdr *llh = cathandle->lgh_hdr;
75 index = (cathandle->lgh_last_idx + 1) %
76 (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ? (cfs_fail_val + 1) :
77 LLOG_HDR_BITMAP_SIZE(llh));
79 /* check that new llog index will not overlap with the first one.
80 * - llh_cat_idx is the index just before the first/oldest still in-use
82 * - lgh_last_idx is the last/newest used index in catalog
84 * When catalog is not wrapped yet then lgh_last_idx is always larger
85 * than llh_cat_idx. After the wrap around lgh_last_idx re-starts
86 * from 0 and llh_cat_idx becomes the upper limit for it
88 * Check if catalog has already wrapped around or not by comparing
89 * last_idx and cat_idx */
90 if ((index == llh->llh_cat_idx + 1 && llh->llh_count > 1) ||
91 (index == 0 && llh->llh_cat_idx == 0)) {
92 CWARN("%s: there are no more free slots in catalog\n",
93 loghandle->lgh_ctxt->loc_obd->obd_name);
97 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
100 if (loghandle->lgh_hdr != NULL) {
101 /* If llog object is remote and creation is failed, lgh_hdr
102 * might be left over here, free it first */
103 LASSERT(!llog_exist(loghandle));
104 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
105 loghandle->lgh_hdr = NULL;
109 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
111 handle = dt_trans_create(env, dt);
113 RETURN(PTR_ERR(handle));
115 /* Create update llog object synchronously, which
116 * happens during inialization process see
117 * lod_sub_prep_llog(), to make sure the update
118 * llog object is created before corss-MDT writing
119 * updates into the llog object */
120 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
123 handle->th_wait_submit = 1;
125 rc = llog_declare_create(env, loghandle, handle);
129 rec->lid_hdr.lrh_len = sizeof(*rec);
130 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
131 rec->lid_id = loghandle->lgh_id;
132 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
137 rc = dt_trans_start_local(env, dt, handle);
144 rc = llog_create(env, loghandle, th);
145 /* if llog is already created, no need to initialize it */
148 } else if (rc != 0) {
149 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
150 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
154 rc = llog_init_handle(env, loghandle,
155 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
156 &cathandle->lgh_hdr->llh_tgtuuid);
160 /* build the record for this log in the catalog */
161 rec->lid_hdr.lrh_len = sizeof(*rec);
162 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
163 rec->lid_id = loghandle->lgh_id;
165 /* append the new record into catalog. The new index will be
166 * assigned to the record and updated in rec header */
167 rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
168 &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
170 GOTO(out_destroy, rc);
172 CDEBUG(D_OTHER, "new plain log "DOSTID":%x for index %u of catalog"
173 DOSTID"\n", POSTID(&loghandle->lgh_id.lgl_oi),
174 loghandle->lgh_id.lgl_ogen, rec->lid_hdr.lrh_index,
175 POSTID(&cathandle->lgh_id.lgl_oi));
177 loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
179 if (handle != NULL) {
180 handle->th_result = rc >= 0 ? 0 : rc;
181 dt_trans_stop(env, dt, handle);
186 /* to signal llog_cat_close() it shouldn't try to destroy the llog,
187 * we want to destroy it in this transaction, otherwise the object
188 * becomes an orphan */
189 loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
190 /* this is to mimic full log, so another llog_cat_current_log()
191 * can skip it and ask for another onet */
192 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) + 1;
193 llog_trans_destroy(env, loghandle, th);
197 /* Open an existent log handle and add it to the open list.
198 * This log handle will be closed when all of the records in it are removed.
200 * Assumes caller has already pushed us into the kernel context and is locking.
201 * We return a lock on the handle to ensure nobody yanks it from us.
203 * This takes extra reference on llog_handle via llog_handle_get() and require
204 * this reference to be put by caller using llog_handle_put()
206 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
207 struct llog_handle **res, struct llog_logid *logid)
209 struct llog_handle *loghandle;
215 if (cathandle == NULL)
218 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
219 down_write(&cathandle->lgh_lock);
220 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
222 struct llog_logid *cgl = &loghandle->lgh_id;
224 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
225 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
226 if (cgl->lgl_ogen != logid->lgl_ogen) {
227 CERROR("%s: log "DOSTID" generation %x != %x\n",
228 loghandle->lgh_ctxt->loc_obd->obd_name,
229 POSTID(&logid->lgl_oi), cgl->lgl_ogen,
233 loghandle->u.phd.phd_cat_handle = cathandle;
234 up_write(&cathandle->lgh_lock);
238 up_write(&cathandle->lgh_lock);
240 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
243 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
244 cathandle->lgh_ctxt->loc_obd->obd_name,
245 POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
249 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
251 llog_close(env, loghandle);
256 down_write(&cathandle->lgh_lock);
257 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
258 up_write(&cathandle->lgh_lock);
260 loghandle->u.phd.phd_cat_handle = cathandle;
261 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
262 loghandle->u.phd.phd_cookie.lgc_index =
263 loghandle->lgh_hdr->llh_cat_idx;
266 llog_handle_get(loghandle);
271 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
273 struct llog_handle *loghandle, *n;
278 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
280 struct llog_log_hdr *llh = loghandle->lgh_hdr;
283 /* unlink open-not-created llogs */
284 list_del_init(&loghandle->u.phd.phd_entry);
285 llh = loghandle->lgh_hdr;
286 if (loghandle->lgh_obj != NULL && llh != NULL &&
287 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
288 (llh->llh_count == 1)) {
289 rc = llog_destroy(env, loghandle);
291 CERROR("%s: failure destroying log during "
292 "cleanup: rc = %d\n",
293 loghandle->lgh_ctxt->loc_obd->obd_name,
296 index = loghandle->u.phd.phd_cookie.lgc_index;
297 llog_cat_cleanup(env, cathandle, NULL, index);
299 llog_close(env, loghandle);
301 /* if handle was stored in ctxt, remove it too */
302 if (cathandle->lgh_ctxt->loc_handle == cathandle)
303 cathandle->lgh_ctxt->loc_handle = NULL;
304 rc = llog_close(env, cathandle);
307 EXPORT_SYMBOL(llog_cat_close);
310 * lockdep markers for nested struct llog_handle::lgh_lock locking.
317 /** Return the currently active log handle. If the current log handle doesn't
318 * have enough space left for the current record, start a new one.
320 * If reclen is 0, we only want to know what the currently active log is,
321 * otherwise we get a lock on this log so nobody can steal our space.
323 * Assumes caller has already pushed us into the kernel context and is locking.
325 * NOTE: loghandle is write-locked upon successful return
327 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
330 struct llog_handle *loghandle = NULL;
334 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
335 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
336 GOTO(next, loghandle);
339 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
340 loghandle = cathandle->u.chd.chd_current_log;
342 struct llog_log_hdr *llh;
344 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
345 llh = loghandle->lgh_hdr;
346 if (llh == NULL || !llog_is_full(loghandle)) {
347 up_read(&cathandle->lgh_lock);
350 up_write(&loghandle->lgh_lock);
353 up_read(&cathandle->lgh_lock);
355 /* time to use next log */
357 /* first, we have to make sure the state hasn't changed */
358 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
359 loghandle = cathandle->u.chd.chd_current_log;
361 struct llog_log_hdr *llh;
363 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
364 llh = loghandle->lgh_hdr;
366 if (!llog_is_full(loghandle)) {
367 up_write(&cathandle->lgh_lock);
370 up_write(&loghandle->lgh_lock);
375 CDEBUG(D_INODE, "use next log\n");
377 loghandle = cathandle->u.chd.chd_next_log;
378 cathandle->u.chd.chd_current_log = loghandle;
379 cathandle->u.chd.chd_next_log = NULL;
380 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
381 up_write(&cathandle->lgh_lock);
386 static int llog_cat_update_header(const struct lu_env *env,
387 struct llog_handle *cathandle)
389 struct llog_handle *loghandle;
394 down_write(&cathandle->lgh_lock);
395 if (!cathandle->lgh_stale) {
396 up_write(&cathandle->lgh_lock);
399 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
401 if (!llog_exist(loghandle))
404 rc = llog_read_header(env, loghandle, NULL);
406 up_write(&cathandle->lgh_lock);
410 rc = llog_read_header(env, cathandle, NULL);
412 cathandle->lgh_stale = 0;
413 up_write(&cathandle->lgh_lock);
420 /* Add a single record to the recovery log(s) using a catalog
421 * Returns as llog_write_record
423 * Assumes caller has already pushed us into the kernel context.
425 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
426 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
429 struct llog_handle *loghandle;
433 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
436 loghandle = llog_cat_current_log(cathandle, th);
437 LASSERT(!IS_ERR(loghandle));
439 /* loghandle is already locked by llog_cat_current_log() for us */
440 if (!llog_exist(loghandle)) {
441 rc = llog_cat_new_log(env, cathandle, loghandle, th);
443 up_write(&loghandle->lgh_lock);
444 /* nobody should be trying to use this llog */
445 down_write(&cathandle->lgh_lock);
446 if (cathandle->u.chd.chd_current_log == loghandle)
447 cathandle->u.chd.chd_current_log = NULL;
448 up_write(&cathandle->lgh_lock);
452 /* now let's try to add the record */
453 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
455 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
456 "llog_write_rec %d: lh=%p\n", rc, loghandle);
457 /* -ENOSPC is returned if no empty records left
458 * and when it's lack of space on the stogage.
459 * there is no point to try again if it's the second
460 * case. many callers (like llog test) expect ENOSPC,
461 * so we preserve this error code, but look for the
462 * actual cause here */
463 if (rc == -ENOSPC && llog_is_full(loghandle))
466 up_write(&loghandle->lgh_lock);
468 if (rc == -ENOBUFS) {
471 CERROR("%s: error on 2nd llog: rc = %d\n",
472 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
477 EXPORT_SYMBOL(llog_cat_add_rec);
479 int llog_cat_declare_add_rec(const struct lu_env *env,
480 struct llog_handle *cathandle,
481 struct llog_rec_hdr *rec, struct thandle *th)
483 struct llog_thread_info *lgi = llog_info(env);
484 struct llog_logid_rec *lirec = &lgi->lgi_logid;
485 struct llog_handle *loghandle, *next;
490 if (cathandle->u.chd.chd_current_log == NULL) {
491 /* declare new plain llog */
492 down_write(&cathandle->lgh_lock);
493 if (cathandle->u.chd.chd_current_log == NULL) {
494 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
495 NULL, NULL, LLOG_OPEN_NEW);
497 cathandle->u.chd.chd_current_log = loghandle;
498 list_add_tail(&loghandle->u.phd.phd_entry,
499 &cathandle->u.chd.chd_head);
502 up_write(&cathandle->lgh_lock);
503 } else if (cathandle->u.chd.chd_next_log == NULL) {
504 /* declare next plain llog */
505 down_write(&cathandle->lgh_lock);
506 if (cathandle->u.chd.chd_next_log == NULL) {
507 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
508 NULL, NULL, LLOG_OPEN_NEW);
510 cathandle->u.chd.chd_next_log = loghandle;
511 list_add_tail(&loghandle->u.phd.phd_entry,
512 &cathandle->u.chd.chd_head);
515 up_write(&cathandle->lgh_lock);
520 lirec->lid_hdr.lrh_len = sizeof(*lirec);
522 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
523 if (dt_object_remote(cathandle->lgh_obj)) {
524 /* For remote operation, if we put the llog object
525 * creation in the current transaction, then the
526 * llog object will not be created on the remote
527 * target until the transaction stop, if other
528 * operations start before the transaction stop,
529 * and use the same llog object, will be dependent
530 * on the success of this transaction. So let's
531 * create the llog object synchronously here to
532 * remove the dependency. */
534 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
535 loghandle = cathandle->u.chd.chd_current_log;
536 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
537 if (cathandle->lgh_stale) {
538 up_write(&loghandle->lgh_lock);
539 up_read(&cathandle->lgh_lock);
540 GOTO(out, rc = -EIO);
542 if (!llog_exist(loghandle)) {
543 rc = llog_cat_new_log(env, cathandle, loghandle,
546 cathandle->lgh_stale = 1;
548 up_write(&loghandle->lgh_lock);
549 up_read(&cathandle->lgh_lock);
551 rc = llog_cat_update_header(env, cathandle);
559 rc = llog_declare_create(env,
560 cathandle->u.chd.chd_current_log, th);
563 llog_declare_write_rec(env, cathandle,
564 &lirec->lid_hdr, -1, th);
569 /* declare records in the llogs */
570 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
573 down_write(&cathandle->lgh_lock);
574 if (cathandle->lgh_stale) {
575 up_write(&cathandle->lgh_lock);
576 GOTO(out, rc = -EIO);
579 cathandle->lgh_stale = 1;
580 up_write(&cathandle->lgh_lock);
581 rc = llog_cat_update_header(env, cathandle);
589 next = cathandle->u.chd.chd_next_log;
591 if (!llog_exist(next)) {
592 if (dt_object_remote(cathandle->lgh_obj)) {
593 /* For remote operation, if we put the llog
594 * object creation in the current transaction,
595 * then the llog object will not be created on
596 * the remote target until the transaction stop,
597 * if other operations start before the
598 * transaction stop, and use the same llog
599 * object, will be dependent on the success of
600 * this transaction. So let's create the llog
601 * object synchronously here to remove the
603 down_read_nested(&cathandle->lgh_lock,
605 next = cathandle->u.chd.chd_next_log;
606 down_write_nested(&next->lgh_lock, LLOGH_LOG);
607 if (!llog_exist(next))
608 rc = llog_cat_new_log(env, cathandle,
610 up_write(&next->lgh_lock);
611 up_read(&cathandle->lgh_lock);
615 rc = llog_declare_create(env, next, th);
616 llog_declare_write_rec(env, cathandle,
617 &lirec->lid_hdr, -1, th);
620 /* XXX: we hope for declarations made for existing llog
621 * this might be not correct with some backends
622 * where declarations are expected against specific
623 * object like ZFS with full debugging enabled */
624 /*llog_declare_write_rec(env, next, rec, -1, th);*/
629 EXPORT_SYMBOL(llog_cat_declare_add_rec);
631 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
632 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
634 struct llog_ctxt *ctxt;
635 struct dt_device *dt;
636 struct thandle *th = NULL;
639 ctxt = cathandle->lgh_ctxt;
641 LASSERT(ctxt->loc_exp);
643 LASSERT(cathandle->lgh_obj != NULL);
644 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
646 th = dt_trans_create(env, dt);
650 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
654 rc = dt_trans_start_local(env, dt, th);
657 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
659 dt_trans_stop(env, dt, th);
662 EXPORT_SYMBOL(llog_cat_add);
664 /* For each cookie in the cookie array, we clear the log in-use bit and either:
665 * - the log is empty, so mark it free in the catalog header and delete it
666 * - the log is not empty, just write out the log header
668 * The cookies may be in different log files, so we need to get new logs
671 * Assumes caller has already pushed us into the kernel context.
673 int llog_cat_cancel_records(const struct lu_env *env,
674 struct llog_handle *cathandle, int count,
675 struct llog_cookie *cookies)
677 int i, index, rc = 0, failed = 0;
681 for (i = 0; i < count; i++, cookies++) {
682 struct llog_handle *loghandle;
683 struct llog_logid *lgl = &cookies->lgc_lgl;
686 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
688 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
689 cathandle->lgh_ctxt->loc_obd->obd_name,
690 POSTID(&lgl->lgl_oi), rc);
695 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
696 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
697 index = loghandle->u.phd.phd_cookie.lgc_index;
698 rc = llog_cat_cleanup(env, cathandle, loghandle,
700 } else if (lrc == -ENOENT) {
701 if (rc == 0) /* ENOENT shouldn't rewrite any error */
703 } else if (lrc < 0) {
707 llog_handle_put(loghandle);
710 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
711 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
716 EXPORT_SYMBOL(llog_cat_cancel_records);
718 static int llog_cat_process_cb(const struct lu_env *env,
719 struct llog_handle *cat_llh,
720 struct llog_rec_hdr *rec, void *data)
722 struct llog_process_data *d = data;
723 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
724 struct llog_handle *llh;
725 struct llog_log_hdr *hdr;
729 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
730 CERROR("invalid record in catalog\n");
733 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
734 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
735 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
737 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
739 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
740 cat_llh->lgh_ctxt->loc_obd->obd_name,
741 POSTID(&lir->lid_id.lgl_oi), rc);
742 if (rc == -ENOENT || rc == -ESTALE) {
743 /* After a server crash, a stub of index
744 * record in catlog could be kept, because
745 * plain log destroy + catlog index record
746 * deletion are not atomic. So we end up with
747 * an index but no actual record. Destroy the
748 * index and move on. */
749 rc = llog_cat_cleanup(env, cat_llh, NULL,
756 /* clean old empty llogs, do not consider current llog in use */
757 /* ignore remote (lgh_obj=NULL) llogs */
759 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
760 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
761 llh != cat_llh->u.chd.chd_current_log) {
762 rc = llog_destroy(env, llh);
764 CERROR("%s: fail to destroy empty log: rc = %d\n",
765 llh->lgh_ctxt->loc_obd->obd_name, rc);
766 GOTO(out, rc = LLOG_DEL_PLAIN);
769 if (rec->lrh_index < d->lpd_startcat) {
770 /* Skip processing of the logs until startcat */
772 } else if (d->lpd_startidx > 0) {
773 struct llog_process_cat_data cd;
775 cd.lpcd_first_idx = d->lpd_startidx;
776 cd.lpcd_last_idx = 0;
777 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
779 /* Continue processing the next log from idx 0 */
782 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
787 /* The empty plain log was destroyed while processing */
788 if (rc == LLOG_DEL_PLAIN)
789 rc = llog_cat_cleanup(env, cat_llh, llh,
790 llh->u.phd.phd_cookie.lgc_index);
791 llog_handle_put(llh);
796 int llog_cat_process_or_fork(const struct lu_env *env,
797 struct llog_handle *cat_llh, llog_cb_t cat_cb,
798 llog_cb_t cb, void *data, int startcat,
799 int startidx, bool fork)
801 struct llog_process_data d;
802 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
806 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
809 d.lpd_startcat = startcat;
810 d.lpd_startidx = startidx;
812 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
813 llh->llh_count > 1) {
814 struct llog_process_cat_data cd;
816 CWARN("catlog "DOSTID" crosses index zero\n",
817 POSTID(&cat_llh->lgh_id.lgl_oi));
819 cd.lpcd_first_idx = llh->llh_cat_idx;
820 cd.lpcd_last_idx = 0;
821 rc = llog_process_or_fork(env, cat_llh, cat_cb,
826 cd.lpcd_first_idx = 0;
827 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
828 rc = llog_process_or_fork(env, cat_llh, cat_cb,
831 rc = llog_process_or_fork(env, cat_llh, cat_cb,
838 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
839 llog_cb_t cb, void *data, int startcat, int startidx)
841 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
842 cb, data, startcat, startidx, false);
844 EXPORT_SYMBOL(llog_cat_process);
846 static int llog_cat_size_cb(const struct lu_env *env,
847 struct llog_handle *cat_llh,
848 struct llog_rec_hdr *rec, void *data)
850 struct llog_process_data *d = data;
851 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
852 struct llog_handle *llh;
854 __u64 *cum_size = d->lpd_data;
858 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
859 CERROR("%s: invalid record in catalog, rc = %d\n",
860 cat_llh->lgh_ctxt->loc_obd->obd_name, -EINVAL);
863 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
864 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
865 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
867 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
869 CWARN("%s: cannot find handle for llog "DOSTID": rc = %d\n",
870 cat_llh->lgh_ctxt->loc_obd->obd_name,
871 POSTID(&lir->lid_id.lgl_oi), rc);
874 size = llog_size(env, llh);
877 CDEBUG(D_INFO, "Add llog entry "DOSTID" size "LPU64"\n",
878 POSTID(&llh->lgh_id.lgl_oi), size);
880 llog_handle_put(llh);
886 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
888 __u64 size = llog_size(env, cat_llh);
890 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
891 NULL, &size, 0, 0, false);
895 EXPORT_SYMBOL(llog_cat_size);
897 static int llog_cat_reverse_process_cb(const struct lu_env *env,
898 struct llog_handle *cat_llh,
899 struct llog_rec_hdr *rec, void *data)
901 struct llog_process_data *d = data;
902 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
903 struct llog_handle *llh;
904 struct llog_log_hdr *hdr;
907 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
908 CERROR("invalid record in catalog\n");
911 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
912 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
913 le32_to_cpu(rec->lrh_index), POSTID(&cat_llh->lgh_id.lgl_oi));
915 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
917 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
918 cat_llh->lgh_ctxt->loc_obd->obd_name,
919 POSTID(&lir->lid_id.lgl_oi), rc);
920 if (rc == -ENOENT || rc == -ESTALE) {
921 /* After a server crash, a stub of index
922 * record in catlog could be kept, because
923 * plain log destroy + catlog index record
924 * deletion are not atomic. So we end up with
925 * an index but no actual record. Destroy the
926 * index and move on. */
927 rc = llog_cat_cleanup(env, cat_llh, NULL,
934 /* clean old empty llogs, do not consider current llog in use */
936 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
937 hdr->llh_count == 1 &&
938 llh != cat_llh->u.chd.chd_current_log) {
939 rc = llog_destroy(env, llh);
941 CERROR("%s: fail to destroy empty log: rc = %d\n",
942 llh->lgh_ctxt->loc_obd->obd_name, rc);
943 GOTO(out, rc = LLOG_DEL_PLAIN);
946 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
949 /* The empty plain was destroyed while processing */
950 if (rc == LLOG_DEL_PLAIN)
951 rc = llog_cat_cleanup(env, cat_llh, llh,
952 llh->u.phd.phd_cookie.lgc_index);
954 llog_handle_put(llh);
958 int llog_cat_reverse_process(const struct lu_env *env,
959 struct llog_handle *cat_llh,
960 llog_cb_t cb, void *data)
962 struct llog_process_data d;
963 struct llog_process_cat_data cd;
964 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
968 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
972 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
973 llh->llh_count > 1) {
974 CWARN("catalog "DOSTID" crosses index zero\n",
975 POSTID(&cat_llh->lgh_id.lgl_oi));
977 cd.lpcd_first_idx = 0;
978 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
979 rc = llog_reverse_process(env, cat_llh,
980 llog_cat_reverse_process_cb,
985 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
986 cd.lpcd_last_idx = 0;
987 rc = llog_reverse_process(env, cat_llh,
988 llog_cat_reverse_process_cb,
991 rc = llog_reverse_process(env, cat_llh,
992 llog_cat_reverse_process_cb,
998 EXPORT_SYMBOL(llog_cat_reverse_process);
1000 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1002 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1007 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1009 * The llh_cat_idx equals to the first used index minus 1
1010 * so if we canceled the first index then llh_cat_idx
1013 if (llh->llh_cat_idx == (idx - 1)) {
1014 llh->llh_cat_idx = idx;
1016 while (idx != cathandle->lgh_last_idx) {
1017 idx = (idx + 1) % bitmap_size;
1018 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1019 /* update llh_cat_idx for each unset bit,
1020 * expecting the next one is set */
1021 llh->llh_cat_idx = idx;
1022 } else if (idx == 0) {
1023 /* skip header bit */
1024 llh->llh_cat_idx = 0;
1027 /* the first index is found */
1032 CDEBUG(D_RPCTRACE, "Set catlog "DOSTID" first idx %u,"
1033 " (last_idx %u)\n", POSTID(&cathandle->lgh_id.lgl_oi),
1034 llh->llh_cat_idx, cathandle->lgh_last_idx);
1040 /* Cleanup deleted plain llog traces from catalog */
1041 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1042 struct llog_handle *loghandle, int index)
1047 if (loghandle != NULL) {
1048 /* remove destroyed llog from catalog list and
1049 * chd_current_log variable */
1050 down_write(&cathandle->lgh_lock);
1051 if (cathandle->u.chd.chd_current_log == loghandle)
1052 cathandle->u.chd.chd_current_log = NULL;
1053 list_del_init(&loghandle->u.phd.phd_entry);
1054 up_write(&cathandle->lgh_lock);
1055 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1056 /* llog was opened and keep in a list, close it now */
1057 llog_close(env, loghandle);
1060 /* do not attempt to cleanup on-disk llog if on client side */
1061 if (cathandle->lgh_obj == NULL)
1064 /* remove plain llog entry from catalog by index */
1065 llog_cat_set_first_idx(cathandle, index);
1066 rc = llog_cancel_rec(env, cathandle, index);
1068 CDEBUG(D_HA, "cancel plain log at index"
1069 " %u of catalog "DOSTID"\n",
1070 index, POSTID(&cathandle->lgh_id.lgl_oi));