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, 2016, 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 if (cathandle->lgh_name == NULL) {
89 CWARN("%s: there are no more free slots in catalog "
91 loghandle->lgh_ctxt->loc_obd->obd_name,
92 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
93 cathandle->lgh_id.lgl_ogen);
95 CWARN("%s: there are no more free slots in "
97 loghandle->lgh_ctxt->loc_obd->obd_name,
103 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
106 if (loghandle->lgh_hdr != NULL) {
107 /* If llog object is remote and creation is failed, lgh_hdr
108 * might be left over here, free it first */
109 LASSERT(!llog_exist(loghandle));
110 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
111 loghandle->lgh_hdr = NULL;
115 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
117 handle = dt_trans_create(env, dt);
119 RETURN(PTR_ERR(handle));
121 /* Create update llog object synchronously, which
122 * happens during inialization process see
123 * lod_sub_prep_llog(), to make sure the update
124 * llog object is created before corss-MDT writing
125 * updates into the llog object */
126 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
129 handle->th_wait_submit = 1;
131 rc = llog_declare_create(env, loghandle, handle);
135 rec->lid_hdr.lrh_len = sizeof(*rec);
136 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
137 rec->lid_id = loghandle->lgh_id;
138 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
143 rc = dt_trans_start_local(env, dt, handle);
150 rc = llog_create(env, loghandle, th);
151 /* if llog is already created, no need to initialize it */
154 } else if (rc != 0) {
155 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
156 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
160 rc = llog_init_handle(env, loghandle,
161 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
162 &cathandle->lgh_hdr->llh_tgtuuid);
166 /* build the record for this log in the catalog */
167 rec->lid_hdr.lrh_len = sizeof(*rec);
168 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
169 rec->lid_id = loghandle->lgh_id;
171 /* append the new record into catalog. The new index will be
172 * assigned to the record and updated in rec header */
173 rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
174 &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
176 GOTO(out_destroy, rc);
178 CDEBUG(D_OTHER, "new plain log "DFID".%u of catalog "DFID"\n",
179 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rec->lid_hdr.lrh_index,
180 PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
182 loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
184 /* limit max size of plain llog so that space can be
185 * released sooner, especially on small filesystems */
186 /* 2MB for the cases when free space hasn't been learned yet */
187 loghandle->lgh_max_size = 2 << 20;
188 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
189 rc = dt_statfs(env, dt, &lgi->lgi_statfs);
190 if (rc == 0 && lgi->lgi_statfs.os_bfree > 0) {
191 __u64 freespace = (lgi->lgi_statfs.os_bfree *
192 lgi->lgi_statfs.os_bsize) >> 6;
193 if (freespace < loghandle->lgh_max_size)
194 loghandle->lgh_max_size = freespace;
195 /* shouldn't be > 128MB in any case?
196 * it's 256K records of 512 bytes each */
197 if (freespace > (128 << 20))
198 loghandle->lgh_max_size = 128 << 20;
203 if (handle != NULL) {
204 handle->th_result = rc >= 0 ? 0 : rc;
205 dt_trans_stop(env, dt, handle);
210 /* to signal llog_cat_close() it shouldn't try to destroy the llog,
211 * we want to destroy it in this transaction, otherwise the object
212 * becomes an orphan */
213 loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
214 /* this is to mimic full log, so another llog_cat_current_log()
215 * can skip it and ask for another onet */
216 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) + 1;
217 llog_trans_destroy(env, loghandle, th);
221 /* Open an existent log handle and add it to the open list.
222 * This log handle will be closed when all of the records in it are removed.
224 * Assumes caller has already pushed us into the kernel context and is locking.
225 * We return a lock on the handle to ensure nobody yanks it from us.
227 * This takes extra reference on llog_handle via llog_handle_get() and require
228 * this reference to be put by caller using llog_handle_put()
230 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
231 struct llog_handle **res, struct llog_logid *logid)
233 struct llog_handle *loghandle;
239 if (cathandle == NULL)
242 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
243 down_write(&cathandle->lgh_lock);
244 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
246 struct llog_logid *cgl = &loghandle->lgh_id;
248 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
249 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
250 if (cgl->lgl_ogen != logid->lgl_ogen) {
251 CWARN("%s: log "DFID" generation %x != %x\n",
252 loghandle->lgh_ctxt->loc_obd->obd_name,
253 PFID(&logid->lgl_oi.oi_fid),
254 cgl->lgl_ogen, logid->lgl_ogen);
257 loghandle->u.phd.phd_cat_handle = cathandle;
258 up_write(&cathandle->lgh_lock);
262 up_write(&cathandle->lgh_lock);
264 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
267 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
268 cathandle->lgh_ctxt->loc_obd->obd_name,
269 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen, rc);
273 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
275 llog_close(env, loghandle);
280 down_write(&cathandle->lgh_lock);
281 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
282 up_write(&cathandle->lgh_lock);
284 loghandle->u.phd.phd_cat_handle = cathandle;
285 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
286 loghandle->u.phd.phd_cookie.lgc_index =
287 loghandle->lgh_hdr->llh_cat_idx;
290 llog_handle_get(loghandle);
295 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
297 struct llog_handle *loghandle, *n;
302 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
304 struct llog_log_hdr *llh = loghandle->lgh_hdr;
307 /* unlink open-not-created llogs */
308 list_del_init(&loghandle->u.phd.phd_entry);
309 llh = loghandle->lgh_hdr;
310 if (loghandle->lgh_obj != NULL && llh != NULL &&
311 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
312 (llh->llh_count == 1)) {
313 rc = llog_destroy(env, loghandle);
315 CERROR("%s: failure destroying log during "
316 "cleanup: rc = %d\n",
317 loghandle->lgh_ctxt->loc_obd->obd_name,
320 index = loghandle->u.phd.phd_cookie.lgc_index;
321 llog_cat_cleanup(env, cathandle, NULL, index);
323 llog_close(env, loghandle);
325 /* if handle was stored in ctxt, remove it too */
326 if (cathandle->lgh_ctxt->loc_handle == cathandle)
327 cathandle->lgh_ctxt->loc_handle = NULL;
328 rc = llog_close(env, cathandle);
331 EXPORT_SYMBOL(llog_cat_close);
334 * lockdep markers for nested struct llog_handle::lgh_lock locking.
341 /** Return the currently active log handle. If the current log handle doesn't
342 * have enough space left for the current record, start a new one.
344 * If reclen is 0, we only want to know what the currently active log is,
345 * otherwise we get a lock on this log so nobody can steal our space.
347 * Assumes caller has already pushed us into the kernel context and is locking.
349 * NOTE: loghandle is write-locked upon successful return
351 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
354 struct llog_handle *loghandle = NULL;
358 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
359 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
360 GOTO(next, loghandle);
363 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
364 loghandle = cathandle->u.chd.chd_current_log;
366 struct llog_log_hdr *llh;
368 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
369 llh = loghandle->lgh_hdr;
370 if (llh == NULL || !llog_is_full(loghandle)) {
371 up_read(&cathandle->lgh_lock);
374 up_write(&loghandle->lgh_lock);
377 up_read(&cathandle->lgh_lock);
379 /* time to use next log */
381 /* first, we have to make sure the state hasn't changed */
382 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
383 loghandle = cathandle->u.chd.chd_current_log;
385 struct llog_log_hdr *llh;
387 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
388 llh = loghandle->lgh_hdr;
389 if (llh == NULL || !llog_is_full(loghandle))
390 GOTO(out_unlock, loghandle);
392 up_write(&loghandle->lgh_lock);
396 /* Sigh, the chd_next_log and chd_current_log is initialized
397 * in declare phase, and we do not serialize the catlog
398 * accessing, so it might be possible the llog creation
399 * thread (see llog_cat_declare_add_rec()) did not create
400 * llog successfully, then the following thread might
401 * meet this situation. */
402 if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
403 CERROR("%s: next log does not exist!\n",
404 cathandle->lgh_ctxt->loc_obd->obd_name);
405 loghandle = ERR_PTR(-EIO);
406 if (cathandle->u.chd.chd_next_log == NULL) {
407 /* Store the error in chd_next_log, so
408 * the following process can get correct
410 cathandle->u.chd.chd_next_log = loghandle;
412 GOTO(out_unlock, loghandle);
415 CDEBUG(D_INODE, "use next log\n");
417 loghandle = cathandle->u.chd.chd_next_log;
418 cathandle->u.chd.chd_current_log = loghandle;
419 cathandle->u.chd.chd_next_log = NULL;
420 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
423 up_write(&cathandle->lgh_lock);
428 static int llog_cat_update_header(const struct lu_env *env,
429 struct llog_handle *cathandle)
431 struct llog_handle *loghandle;
436 down_write(&cathandle->lgh_lock);
437 if (!cathandle->lgh_stale) {
438 up_write(&cathandle->lgh_lock);
441 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
443 if (!llog_exist(loghandle))
446 rc = llog_read_header(env, loghandle, NULL);
448 up_write(&cathandle->lgh_lock);
452 rc = llog_read_header(env, cathandle, NULL);
454 cathandle->lgh_stale = 0;
455 up_write(&cathandle->lgh_lock);
462 /* Add a single record to the recovery log(s) using a catalog
463 * Returns as llog_write_record
465 * Assumes caller has already pushed us into the kernel context.
467 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
468 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
471 struct llog_handle *loghandle;
475 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
478 loghandle = llog_cat_current_log(cathandle, th);
479 if (IS_ERR(loghandle))
480 RETURN(PTR_ERR(loghandle));
482 /* loghandle is already locked by llog_cat_current_log() for us */
483 if (!llog_exist(loghandle)) {
484 rc = llog_cat_new_log(env, cathandle, loghandle, th);
486 up_write(&loghandle->lgh_lock);
487 /* nobody should be trying to use this llog */
488 down_write(&cathandle->lgh_lock);
489 /* only reset current log if still room in catalog, to
490 * avoid unnecessarily and racy creation of new and
491 * partially initialized llog_handle
493 if ((cathandle->u.chd.chd_current_log == loghandle) &&
495 cathandle->u.chd.chd_current_log = NULL;
496 up_write(&cathandle->lgh_lock);
500 /* now let's try to add the record */
501 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
503 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
504 "llog_write_rec %d: lh=%p\n", rc, loghandle);
505 /* -ENOSPC is returned if no empty records left
506 * and when it's lack of space on the stogage.
507 * there is no point to try again if it's the second
508 * case. many callers (like llog test) expect ENOSPC,
509 * so we preserve this error code, but look for the
510 * actual cause here */
511 if (rc == -ENOSPC && llog_is_full(loghandle))
514 up_write(&loghandle->lgh_lock);
516 if (rc == -ENOBUFS) {
519 CERROR("%s: error on 2nd llog: rc = %d\n",
520 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
525 EXPORT_SYMBOL(llog_cat_add_rec);
527 int llog_cat_declare_add_rec(const struct lu_env *env,
528 struct llog_handle *cathandle,
529 struct llog_rec_hdr *rec, struct thandle *th)
531 struct llog_thread_info *lgi = llog_info(env);
532 struct llog_logid_rec *lirec = &lgi->lgi_logid;
533 struct llog_handle *loghandle, *next;
538 if (cathandle->u.chd.chd_current_log == NULL) {
539 /* declare new plain llog */
540 down_write(&cathandle->lgh_lock);
541 if (cathandle->u.chd.chd_current_log == NULL) {
542 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
543 NULL, NULL, LLOG_OPEN_NEW);
545 cathandle->u.chd.chd_current_log = loghandle;
546 list_add_tail(&loghandle->u.phd.phd_entry,
547 &cathandle->u.chd.chd_head);
550 up_write(&cathandle->lgh_lock);
551 } else if (cathandle->u.chd.chd_next_log == NULL ||
552 IS_ERR(cathandle->u.chd.chd_next_log)) {
553 /* declare next plain llog */
554 down_write(&cathandle->lgh_lock);
555 if (cathandle->u.chd.chd_next_log == NULL ||
556 IS_ERR(cathandle->u.chd.chd_next_log)) {
557 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
558 NULL, NULL, LLOG_OPEN_NEW);
560 cathandle->u.chd.chd_next_log = loghandle;
561 list_add_tail(&loghandle->u.phd.phd_entry,
562 &cathandle->u.chd.chd_head);
565 up_write(&cathandle->lgh_lock);
570 lirec->lid_hdr.lrh_len = sizeof(*lirec);
572 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
573 if (dt_object_remote(cathandle->lgh_obj)) {
574 /* For remote operation, if we put the llog object
575 * creation in the current transaction, then the
576 * llog object will not be created on the remote
577 * target until the transaction stop, if other
578 * operations start before the transaction stop,
579 * and use the same llog object, will be dependent
580 * on the success of this transaction. So let's
581 * create the llog object synchronously here to
582 * remove the dependency. */
584 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
585 loghandle = cathandle->u.chd.chd_current_log;
586 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
587 if (cathandle->lgh_stale) {
588 up_write(&loghandle->lgh_lock);
589 up_read(&cathandle->lgh_lock);
590 GOTO(out, rc = -EIO);
592 if (!llog_exist(loghandle)) {
593 rc = llog_cat_new_log(env, cathandle, loghandle,
596 cathandle->lgh_stale = 1;
598 up_write(&loghandle->lgh_lock);
599 up_read(&cathandle->lgh_lock);
601 rc = llog_cat_update_header(env, cathandle);
609 rc = llog_declare_create(env,
610 cathandle->u.chd.chd_current_log, th);
613 llog_declare_write_rec(env, cathandle,
614 &lirec->lid_hdr, -1, th);
619 /* declare records in the llogs */
620 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
623 down_write(&cathandle->lgh_lock);
624 if (cathandle->lgh_stale) {
625 up_write(&cathandle->lgh_lock);
626 GOTO(out, rc = -EIO);
629 cathandle->lgh_stale = 1;
630 up_write(&cathandle->lgh_lock);
631 rc = llog_cat_update_header(env, cathandle);
639 next = cathandle->u.chd.chd_next_log;
640 if (!IS_ERR_OR_NULL(next)) {
641 if (!llog_exist(next)) {
642 if (dt_object_remote(cathandle->lgh_obj)) {
643 /* For remote operation, if we put the llog
644 * object creation in the current transaction,
645 * then the llog object will not be created on
646 * the remote target until the transaction stop,
647 * if other operations start before the
648 * transaction stop, and use the same llog
649 * object, will be dependent on the success of
650 * this transaction. So let's create the llog
651 * object synchronously here to remove the
653 down_write_nested(&cathandle->lgh_lock,
655 next = cathandle->u.chd.chd_next_log;
656 if (IS_ERR_OR_NULL(next)) {
657 /* Sigh, another thread just tried,
658 * let's fail as well */
659 up_write(&cathandle->lgh_lock);
667 down_write_nested(&next->lgh_lock, LLOGH_LOG);
668 if (!llog_exist(next)) {
669 rc = llog_cat_new_log(env, cathandle,
672 cathandle->u.chd.chd_next_log =
675 up_write(&next->lgh_lock);
676 up_write(&cathandle->lgh_lock);
680 rc = llog_declare_create(env, next, th);
681 llog_declare_write_rec(env, cathandle,
682 &lirec->lid_hdr, -1, th);
685 /* XXX: we hope for declarations made for existing llog
686 * this might be not correct with some backends
687 * where declarations are expected against specific
688 * object like ZFS with full debugging enabled */
689 /*llog_declare_write_rec(env, next, rec, -1, th);*/
694 EXPORT_SYMBOL(llog_cat_declare_add_rec);
696 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
697 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
699 struct llog_ctxt *ctxt;
700 struct dt_device *dt;
701 struct thandle *th = NULL;
704 ctxt = cathandle->lgh_ctxt;
706 LASSERT(ctxt->loc_exp);
708 LASSERT(cathandle->lgh_obj != NULL);
709 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
711 th = dt_trans_create(env, dt);
715 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
719 rc = dt_trans_start_local(env, dt, th);
722 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
724 dt_trans_stop(env, dt, th);
727 EXPORT_SYMBOL(llog_cat_add);
729 /* For each cookie in the cookie array, we clear the log in-use bit and either:
730 * - the log is empty, so mark it free in the catalog header and delete it
731 * - the log is not empty, just write out the log header
733 * The cookies may be in different log files, so we need to get new logs
736 * Assumes caller has already pushed us into the kernel context.
738 int llog_cat_cancel_records(const struct lu_env *env,
739 struct llog_handle *cathandle, int count,
740 struct llog_cookie *cookies)
742 int i, index, rc = 0, failed = 0;
746 for (i = 0; i < count; i++, cookies++) {
747 struct llog_handle *loghandle;
748 struct llog_logid *lgl = &cookies->lgc_lgl;
751 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
753 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
755 cathandle->lgh_ctxt->loc_obd->obd_name,
756 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
761 if ((cathandle->lgh_ctxt->loc_flags &
762 LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
763 /* For update log, some of loghandles of cathandle
764 * might not exist because remote llog creation might
765 * be failed, so let's skip the record cancellation
766 * for these non-exist llogs.
769 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
771 cathandle->lgh_ctxt->loc_obd->obd_name,
772 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, lrc);
779 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
780 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
781 index = loghandle->u.phd.phd_cookie.lgc_index;
782 lrc = llog_cat_cleanup(env, cathandle, loghandle,
786 } else if (lrc == -ENOENT) {
787 if (rc == 0) /* ENOENT shouldn't rewrite any error */
789 } else if (lrc < 0) {
794 llog_handle_put(loghandle);
797 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
798 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
803 EXPORT_SYMBOL(llog_cat_cancel_records);
805 static int llog_cat_process_cb(const struct lu_env *env,
806 struct llog_handle *cat_llh,
807 struct llog_rec_hdr *rec, void *data)
809 struct llog_process_data *d = data;
810 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
811 struct llog_handle *llh;
812 struct llog_log_hdr *hdr;
816 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
817 CERROR("invalid record in catalog\n");
820 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
821 DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
822 rec->lrh_index, PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
824 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
826 CERROR("%s: cannot find handle for llog "DFID": rc = %d\n",
827 cat_llh->lgh_ctxt->loc_obd->obd_name,
828 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
829 if (rc == -ENOENT || rc == -ESTALE) {
830 /* After a server crash, a stub of index
831 * record in catlog could be kept, because
832 * plain log destroy + catlog index record
833 * deletion are not atomic. So we end up with
834 * an index but no actual record. Destroy the
835 * index and move on. */
836 rc = llog_cat_cleanup(env, cat_llh, NULL,
843 /* clean old empty llogs, do not consider current llog in use */
844 /* ignore remote (lgh_obj=NULL) llogs */
846 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
847 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
848 llh != cat_llh->u.chd.chd_current_log) {
849 rc = llog_destroy(env, llh);
851 CERROR("%s: fail to destroy empty log: rc = %d\n",
852 llh->lgh_ctxt->loc_obd->obd_name, rc);
853 GOTO(out, rc = LLOG_DEL_PLAIN);
856 if (rec->lrh_index < d->lpd_startcat) {
857 /* Skip processing of the logs until startcat */
859 } else if (d->lpd_startidx > 0) {
860 struct llog_process_cat_data cd;
862 cd.lpcd_first_idx = d->lpd_startidx;
863 cd.lpcd_last_idx = 0;
864 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
866 /* Continue processing the next log from idx 0 */
869 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
874 /* The empty plain log was destroyed while processing */
875 if (rc == LLOG_DEL_PLAIN)
876 rc = llog_cat_cleanup(env, cat_llh, llh,
877 llh->u.phd.phd_cookie.lgc_index);
878 llog_handle_put(llh);
883 int llog_cat_process_or_fork(const struct lu_env *env,
884 struct llog_handle *cat_llh, llog_cb_t cat_cb,
885 llog_cb_t cb, void *data, int startcat,
886 int startidx, bool fork)
888 struct llog_process_data d;
889 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
893 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
896 d.lpd_startcat = startcat;
897 d.lpd_startidx = startidx;
899 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
900 llh->llh_count > 1) {
901 struct llog_process_cat_data cd;
903 CWARN("%s: catlog "DFID" crosses index zero\n",
904 cat_llh->lgh_ctxt->loc_obd->obd_name,
905 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
907 cd.lpcd_first_idx = llh->llh_cat_idx;
908 cd.lpcd_last_idx = 0;
909 rc = llog_process_or_fork(env, cat_llh, cat_cb,
914 cd.lpcd_first_idx = 0;
915 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
916 rc = llog_process_or_fork(env, cat_llh, cat_cb,
919 rc = llog_process_or_fork(env, cat_llh, cat_cb,
926 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
927 llog_cb_t cb, void *data, int startcat, int startidx)
929 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
930 cb, data, startcat, startidx, false);
932 EXPORT_SYMBOL(llog_cat_process);
934 static int llog_cat_size_cb(const struct lu_env *env,
935 struct llog_handle *cat_llh,
936 struct llog_rec_hdr *rec, void *data)
938 struct llog_process_data *d = data;
939 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
940 struct llog_handle *llh;
942 __u64 *cum_size = d->lpd_data;
946 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
947 CERROR("%s: invalid record in catalog, rc = %d\n",
948 cat_llh->lgh_ctxt->loc_obd->obd_name, -EINVAL);
951 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
952 DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
953 rec->lrh_index, PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
955 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
957 CWARN("%s: cannot find handle for llog "DFID": rc = %d\n",
958 cat_llh->lgh_ctxt->loc_obd->obd_name,
959 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
962 size = llog_size(env, llh);
965 CDEBUG(D_INFO, "Add llog entry "DFID" size %llu\n",
966 PFID(&llh->lgh_id.lgl_oi.oi_fid), size);
968 llog_handle_put(llh);
974 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
976 __u64 size = llog_size(env, cat_llh);
978 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
979 NULL, &size, 0, 0, false);
983 EXPORT_SYMBOL(llog_cat_size);
985 static int llog_cat_reverse_process_cb(const struct lu_env *env,
986 struct llog_handle *cat_llh,
987 struct llog_rec_hdr *rec, void *data)
989 struct llog_process_data *d = data;
990 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
991 struct llog_handle *llh;
992 struct llog_log_hdr *hdr;
995 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
996 CERROR("invalid record in catalog\n");
999 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
1000 DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
1001 le32_to_cpu(rec->lrh_index),
1002 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1004 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
1006 CERROR("%s: cannot find handle for llog "DFID": rc = %d\n",
1007 cat_llh->lgh_ctxt->loc_obd->obd_name,
1008 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
1009 if (rc == -ENOENT || rc == -ESTALE) {
1010 /* After a server crash, a stub of index
1011 * record in catlog could be kept, because
1012 * plain log destroy + catlog index record
1013 * deletion are not atomic. So we end up with
1014 * an index but no actual record. Destroy the
1015 * index and move on. */
1016 rc = llog_cat_cleanup(env, cat_llh, NULL,
1023 /* clean old empty llogs, do not consider current llog in use */
1025 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
1026 hdr->llh_count == 1 &&
1027 llh != cat_llh->u.chd.chd_current_log) {
1028 rc = llog_destroy(env, llh);
1030 CERROR("%s: fail to destroy empty log: rc = %d\n",
1031 llh->lgh_ctxt->loc_obd->obd_name, rc);
1032 GOTO(out, rc = LLOG_DEL_PLAIN);
1035 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1038 /* The empty plain was destroyed while processing */
1039 if (rc == LLOG_DEL_PLAIN)
1040 rc = llog_cat_cleanup(env, cat_llh, llh,
1041 llh->u.phd.phd_cookie.lgc_index);
1043 llog_handle_put(llh);
1047 int llog_cat_reverse_process(const struct lu_env *env,
1048 struct llog_handle *cat_llh,
1049 llog_cb_t cb, void *data)
1051 struct llog_process_data d;
1052 struct llog_process_cat_data cd;
1053 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1057 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1061 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1062 llh->llh_count > 1) {
1063 CWARN("%s: catalog "DFID" crosses index zero\n",
1064 cat_llh->lgh_ctxt->loc_obd->obd_name,
1065 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1067 cd.lpcd_first_idx = 0;
1068 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1069 rc = llog_reverse_process(env, cat_llh,
1070 llog_cat_reverse_process_cb,
1075 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1076 cd.lpcd_last_idx = 0;
1077 rc = llog_reverse_process(env, cat_llh,
1078 llog_cat_reverse_process_cb,
1081 rc = llog_reverse_process(env, cat_llh,
1082 llog_cat_reverse_process_cb,
1088 EXPORT_SYMBOL(llog_cat_reverse_process);
1090 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1092 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1097 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1099 * The llh_cat_idx equals to the first used index minus 1
1100 * so if we canceled the first index then llh_cat_idx
1103 if (llh->llh_cat_idx == (idx - 1)) {
1104 llh->llh_cat_idx = idx;
1106 while (idx != cathandle->lgh_last_idx) {
1107 idx = (idx + 1) % bitmap_size;
1108 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1109 /* update llh_cat_idx for each unset bit,
1110 * expecting the next one is set */
1111 llh->llh_cat_idx = idx;
1112 } else if (idx == 0) {
1113 /* skip header bit */
1114 llh->llh_cat_idx = 0;
1117 /* the first index is found */
1122 CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
1123 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1124 llh->llh_cat_idx, cathandle->lgh_last_idx);
1130 /* Cleanup deleted plain llog traces from catalog */
1131 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1132 struct llog_handle *loghandle, int index)
1137 if (loghandle != NULL) {
1138 /* remove destroyed llog from catalog list and
1139 * chd_current_log variable */
1140 down_write(&cathandle->lgh_lock);
1141 if (cathandle->u.chd.chd_current_log == loghandle)
1142 cathandle->u.chd.chd_current_log = NULL;
1143 list_del_init(&loghandle->u.phd.phd_entry);
1144 up_write(&cathandle->lgh_lock);
1145 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1146 /* llog was opened and keep in a list, close it now */
1147 llog_close(env, loghandle);
1150 /* do not attempt to cleanup on-disk llog if on client side */
1151 if (cathandle->lgh_obj == NULL)
1154 /* remove plain llog entry from catalog by index */
1155 llog_cat_set_first_idx(cathandle, index);
1156 rc = llog_cancel_rec(env, cathandle, index);
1158 CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
1159 index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));