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, 2017, 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"
54 * lockdep markers for nested struct llog_handle::lgh_lock locking.
61 /* Create a new log handle and add it to the open list.
62 * This log handle will be closed when all of the records in it are removed.
64 * Assumes caller has already pushed us into the kernel context and is locking.
66 static int llog_cat_new_log(const struct lu_env *env,
67 struct llog_handle *cathandle,
68 struct llog_handle *loghandle,
71 struct llog_thread_info *lgi = llog_info(env);
72 struct llog_logid_rec *rec = &lgi->lgi_logid;
73 struct thandle *handle = NULL;
74 struct dt_device *dt = NULL;
75 struct llog_log_hdr *llh = cathandle->lgh_hdr;
80 index = (cathandle->lgh_last_idx + 1) %
81 (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ? (cfs_fail_val + 1) :
82 LLOG_HDR_BITMAP_SIZE(llh));
84 /* check that new llog index will not overlap with the first one.
85 * - llh_cat_idx is the index just before the first/oldest still in-use
87 * - lgh_last_idx is the last/newest used index in catalog
89 * When catalog is not wrapped yet then lgh_last_idx is always larger
90 * than llh_cat_idx. After the wrap around lgh_last_idx re-starts
91 * from 0 and llh_cat_idx becomes the upper limit for it
93 * Check if catalog has already wrapped around or not by comparing
94 * last_idx and cat_idx */
95 if ((index == llh->llh_cat_idx + 1 && llh->llh_count > 1) ||
96 (index == 0 && llh->llh_cat_idx == 0)) {
97 if (cathandle->lgh_name == NULL) {
98 CWARN("%s: there are no more free slots in catalog "
100 loghandle->lgh_ctxt->loc_obd->obd_name,
101 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
102 cathandle->lgh_id.lgl_ogen);
104 CWARN("%s: there are no more free slots in "
106 loghandle->lgh_ctxt->loc_obd->obd_name,
107 cathandle->lgh_name);
112 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
115 if (loghandle->lgh_hdr != NULL) {
116 /* If llog object is remote and creation is failed, lgh_hdr
117 * might be left over here, free it first */
118 LASSERT(!llog_exist(loghandle));
119 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
120 loghandle->lgh_hdr = NULL;
124 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
126 handle = dt_trans_create(env, dt);
128 RETURN(PTR_ERR(handle));
130 /* Create update llog object synchronously, which
131 * happens during inialization process see
132 * lod_sub_prep_llog(), to make sure the update
133 * llog object is created before corss-MDT writing
134 * updates into the llog object */
135 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
138 handle->th_wait_submit = 1;
140 rc = llog_declare_create(env, loghandle, handle);
144 rec->lid_hdr.lrh_len = sizeof(*rec);
145 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
146 rec->lid_id = loghandle->lgh_id;
147 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
152 rc = dt_trans_start_local(env, dt, handle);
159 rc = llog_create(env, loghandle, th);
160 /* if llog is already created, no need to initialize it */
163 } else if (rc != 0) {
164 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
165 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
169 rc = llog_init_handle(env, loghandle,
170 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
171 &cathandle->lgh_hdr->llh_tgtuuid);
175 /* build the record for this log in the catalog */
176 rec->lid_hdr.lrh_len = sizeof(*rec);
177 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
178 rec->lid_id = loghandle->lgh_id;
180 /* append the new record into catalog. The new index will be
181 * assigned to the record and updated in rec header */
182 rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
183 &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
185 GOTO(out_destroy, rc);
187 CDEBUG(D_OTHER, "new plain log "DFID".%u of catalog "DFID"\n",
188 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rec->lid_hdr.lrh_index,
189 PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
191 loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
193 /* limit max size of plain llog so that space can be
194 * released sooner, especially on small filesystems */
195 /* 2MB for the cases when free space hasn't been learned yet */
196 loghandle->lgh_max_size = 2 << 20;
197 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
198 rc = dt_statfs(env, dt, &lgi->lgi_statfs);
199 if (rc == 0 && lgi->lgi_statfs.os_bfree > 0) {
200 __u64 freespace = (lgi->lgi_statfs.os_bfree *
201 lgi->lgi_statfs.os_bsize) >> 6;
202 if (freespace < loghandle->lgh_max_size)
203 loghandle->lgh_max_size = freespace;
204 /* shouldn't be > 128MB in any case?
205 * it's 256K records of 512 bytes each */
206 if (freespace > (128 << 20))
207 loghandle->lgh_max_size = 128 << 20;
212 if (handle != NULL) {
213 handle->th_result = rc >= 0 ? 0 : rc;
214 dt_trans_stop(env, dt, handle);
219 /* to signal llog_cat_close() it shouldn't try to destroy the llog,
220 * we want to destroy it in this transaction, otherwise the object
221 * becomes an orphan */
222 loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
223 /* this is to mimic full log, so another llog_cat_current_log()
224 * can skip it and ask for another onet */
225 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(loghandle->lgh_hdr) + 1;
226 llog_trans_destroy(env, loghandle, th);
228 dt_trans_stop(env, dt, handle);
232 static int llog_cat_refresh(const struct lu_env *env,
233 struct llog_handle *cathandle)
235 struct llog_handle *loghandle;
238 down_write(&cathandle->lgh_lock);
239 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
241 if (!llog_exist(loghandle))
244 rc = llog_read_header(env, loghandle, NULL);
249 rc = llog_read_header(env, cathandle, NULL);
251 up_write(&loghandle->lgh_lock);
257 * prepare current/next log for catalog.
259 * if log is NULL, open it, and declare create, NB, if log is remote, create it
260 * synchronously here, see comments below.
262 static int llog_cat_prep_log(const struct lu_env *env,
263 struct llog_handle *cathandle,
264 struct llog_handle **ploghandle,
269 if (IS_ERR_OR_NULL(*ploghandle)) {
270 down_write(&cathandle->lgh_lock);
271 if (IS_ERR_OR_NULL(*ploghandle)) {
272 struct llog_handle *loghandle;
274 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
275 NULL, NULL, LLOG_OPEN_NEW);
277 *ploghandle = loghandle;
278 list_add_tail(&loghandle->u.phd.phd_entry,
279 &cathandle->u.chd.chd_head);
282 up_write(&cathandle->lgh_lock);
288 if (llog_exist(*ploghandle))
291 if (dt_object_remote(cathandle->lgh_obj)) {
293 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
294 down_write_nested(&(*ploghandle)->lgh_lock, LLOGH_LOG);
295 if (!llog_exist(*ploghandle)) {
296 /* For remote operation, if we put the llog object
297 * creation in the current transaction, then the
298 * llog object will not be created on the remote
299 * target until the transaction stop, if other
300 * operations start before the transaction stop,
301 * and use the same llog object, will be dependent
302 * on the success of this transaction. So let's
303 * create the llog object synchronously here to
304 * remove the dependency. */
305 rc = llog_cat_new_log(env, cathandle, *ploghandle,
308 up_write(&(*ploghandle)->lgh_lock);
309 up_read(&cathandle->lgh_lock);
311 rc = llog_cat_refresh(env, cathandle);
313 goto create_remote_log;
318 up_write(&(*ploghandle)->lgh_lock);
319 up_read(&cathandle->lgh_lock);
321 struct llog_thread_info *lgi = llog_info(env);
322 struct llog_logid_rec *lirec = &lgi->lgi_logid;
324 rc = llog_declare_create(env, *ploghandle, th);
328 lirec->lid_hdr.lrh_len = sizeof(*lirec);
329 rc = llog_declare_write_rec(env, cathandle, &lirec->lid_hdr, -1,
336 /* Open an existent log handle and add it to the open list.
337 * This log handle will be closed when all of the records in it are removed.
339 * Assumes caller has already pushed us into the kernel context and is locking.
340 * We return a lock on the handle to ensure nobody yanks it from us.
342 * This takes extra reference on llog_handle via llog_handle_get() and require
343 * this reference to be put by caller using llog_handle_put()
345 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
346 struct llog_handle **res, struct llog_logid *logid)
348 struct llog_handle *loghandle;
354 if (cathandle == NULL)
357 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
358 down_write(&cathandle->lgh_lock);
359 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
361 struct llog_logid *cgl = &loghandle->lgh_id;
363 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
364 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
365 if (cgl->lgl_ogen != logid->lgl_ogen) {
366 CWARN("%s: log "DFID" generation %x != %x\n",
367 loghandle->lgh_ctxt->loc_obd->obd_name,
368 PFID(&logid->lgl_oi.oi_fid),
369 cgl->lgl_ogen, logid->lgl_ogen);
372 loghandle->u.phd.phd_cat_handle = cathandle;
373 up_write(&cathandle->lgh_lock);
377 up_write(&cathandle->lgh_lock);
379 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
382 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
383 cathandle->lgh_ctxt->loc_obd->obd_name,
384 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen, rc);
388 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
390 llog_close(env, loghandle);
395 down_write(&cathandle->lgh_lock);
396 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
397 up_write(&cathandle->lgh_lock);
399 loghandle->u.phd.phd_cat_handle = cathandle;
400 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
401 loghandle->u.phd.phd_cookie.lgc_index =
402 loghandle->lgh_hdr->llh_cat_idx;
405 llog_handle_get(loghandle);
410 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
412 struct llog_handle *loghandle, *n;
417 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
419 struct llog_log_hdr *llh = loghandle->lgh_hdr;
422 /* unlink open-not-created llogs */
423 list_del_init(&loghandle->u.phd.phd_entry);
424 llh = loghandle->lgh_hdr;
425 if (loghandle->lgh_obj != NULL && llh != NULL &&
426 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
427 (llh->llh_count == 1)) {
428 rc = llog_destroy(env, loghandle);
430 CERROR("%s: failure destroying log during "
431 "cleanup: rc = %d\n",
432 loghandle->lgh_ctxt->loc_obd->obd_name,
435 index = loghandle->u.phd.phd_cookie.lgc_index;
436 llog_cat_cleanup(env, cathandle, NULL, index);
438 llog_close(env, loghandle);
440 /* if handle was stored in ctxt, remove it too */
441 if (cathandle->lgh_ctxt->loc_handle == cathandle)
442 cathandle->lgh_ctxt->loc_handle = NULL;
443 rc = llog_close(env, cathandle);
446 EXPORT_SYMBOL(llog_cat_close);
448 /** Return the currently active log handle. If the current log handle doesn't
449 * have enough space left for the current record, start a new one.
451 * If reclen is 0, we only want to know what the currently active log is,
452 * otherwise we get a lock on this log so nobody can steal our space.
454 * Assumes caller has already pushed us into the kernel context and is locking.
456 * NOTE: loghandle is write-locked upon successful return
458 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
461 struct llog_handle *loghandle = NULL;
465 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
466 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
467 GOTO(next, loghandle);
470 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
471 loghandle = cathandle->u.chd.chd_current_log;
473 struct llog_log_hdr *llh;
475 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
476 llh = loghandle->lgh_hdr;
477 if (llh == NULL || !llog_is_full(loghandle)) {
478 up_read(&cathandle->lgh_lock);
481 up_write(&loghandle->lgh_lock);
484 up_read(&cathandle->lgh_lock);
486 /* time to use next log */
488 /* first, we have to make sure the state hasn't changed */
489 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
490 loghandle = cathandle->u.chd.chd_current_log;
492 struct llog_log_hdr *llh;
494 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
495 llh = loghandle->lgh_hdr;
496 if (llh == NULL || !llog_is_full(loghandle))
497 GOTO(out_unlock, loghandle);
499 up_write(&loghandle->lgh_lock);
503 /* Sigh, the chd_next_log and chd_current_log is initialized
504 * in declare phase, and we do not serialize the catlog
505 * accessing, so it might be possible the llog creation
506 * thread (see llog_cat_declare_add_rec()) did not create
507 * llog successfully, then the following thread might
508 * meet this situation. */
509 if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
510 CERROR("%s: next log does not exist!\n",
511 cathandle->lgh_ctxt->loc_obd->obd_name);
512 loghandle = ERR_PTR(-EIO);
513 if (cathandle->u.chd.chd_next_log == NULL) {
514 /* Store the error in chd_next_log, so
515 * the following process can get correct
517 cathandle->u.chd.chd_next_log = loghandle;
519 GOTO(out_unlock, loghandle);
522 CDEBUG(D_INODE, "use next log\n");
524 loghandle = cathandle->u.chd.chd_next_log;
525 cathandle->u.chd.chd_current_log = loghandle;
526 cathandle->u.chd.chd_next_log = NULL;
527 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
530 up_write(&cathandle->lgh_lock);
535 /* Add a single record to the recovery log(s) using a catalog
536 * Returns as llog_write_record
538 * Assumes caller has already pushed us into the kernel context.
540 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
541 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
544 struct llog_handle *loghandle;
548 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
551 loghandle = llog_cat_current_log(cathandle, th);
552 if (IS_ERR(loghandle))
553 RETURN(PTR_ERR(loghandle));
555 /* loghandle is already locked by llog_cat_current_log() for us */
556 if (!llog_exist(loghandle)) {
557 rc = llog_cat_new_log(env, cathandle, loghandle, th);
559 up_write(&loghandle->lgh_lock);
560 /* nobody should be trying to use this llog */
561 down_write(&cathandle->lgh_lock);
562 /* only reset current log if still room in catalog, to
563 * avoid unnecessarily and racy creation of new and
564 * partially initialized llog_handle
566 if ((cathandle->u.chd.chd_current_log == loghandle) &&
568 cathandle->u.chd.chd_current_log = NULL;
569 up_write(&cathandle->lgh_lock);
573 /* now let's try to add the record */
574 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
576 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
577 "llog_write_rec %d: lh=%p\n", rc, loghandle);
578 /* -ENOSPC is returned if no empty records left
579 * and when it's lack of space on the stogage.
580 * there is no point to try again if it's the second
581 * case. many callers (like llog test) expect ENOSPC,
582 * so we preserve this error code, but look for the
583 * actual cause here */
584 if (rc == -ENOSPC && llog_is_full(loghandle))
587 up_write(&loghandle->lgh_lock);
589 if (rc == -ENOBUFS) {
592 CERROR("%s: error on 2nd llog: rc = %d\n",
593 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
598 EXPORT_SYMBOL(llog_cat_add_rec);
600 int llog_cat_declare_add_rec(const struct lu_env *env,
601 struct llog_handle *cathandle,
602 struct llog_rec_hdr *rec, struct thandle *th)
608 rc = llog_cat_prep_log(env, cathandle,
609 &cathandle->u.chd.chd_current_log, th);
613 rc = llog_cat_prep_log(env, cathandle, &cathandle->u.chd.chd_next_log,
619 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
621 if (rc == -ESTALE && dt_object_remote(cathandle->lgh_obj)) {
622 rc = llog_cat_refresh(env, cathandle);
629 * XXX: we hope for declarations made for existing llog this might be
630 * not correct with some backends where declarations are expected
631 * against specific object like ZFS with full debugging enabled.
633 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_next_log, rec, -1,
639 EXPORT_SYMBOL(llog_cat_declare_add_rec);
641 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
642 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
644 struct llog_ctxt *ctxt;
645 struct dt_device *dt;
646 struct thandle *th = NULL;
649 ctxt = cathandle->lgh_ctxt;
651 LASSERT(ctxt->loc_exp);
653 LASSERT(cathandle->lgh_obj != NULL);
654 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
656 th = dt_trans_create(env, dt);
660 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
664 rc = dt_trans_start_local(env, dt, th);
667 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
669 dt_trans_stop(env, dt, th);
672 EXPORT_SYMBOL(llog_cat_add);
674 /* For each cookie in the cookie array, we clear the log in-use bit and either:
675 * - the log is empty, so mark it free in the catalog header and delete it
676 * - the log is not empty, just write out the log header
678 * The cookies may be in different log files, so we need to get new logs
681 * Assumes caller has already pushed us into the kernel context.
683 int llog_cat_cancel_records(const struct lu_env *env,
684 struct llog_handle *cathandle, int count,
685 struct llog_cookie *cookies)
687 int i, index, rc = 0, failed = 0;
691 for (i = 0; i < count; i++, cookies++) {
692 struct llog_handle *loghandle;
693 struct llog_logid *lgl = &cookies->lgc_lgl;
696 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
698 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
700 cathandle->lgh_ctxt->loc_obd->obd_name,
701 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
706 if ((cathandle->lgh_ctxt->loc_flags &
707 LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
708 /* For update log, some of loghandles of cathandle
709 * might not exist because remote llog creation might
710 * be failed, so let's skip the record cancellation
711 * for these non-exist llogs.
714 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
716 cathandle->lgh_ctxt->loc_obd->obd_name,
717 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, lrc);
724 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
725 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
726 index = loghandle->u.phd.phd_cookie.lgc_index;
727 lrc = llog_cat_cleanup(env, cathandle, loghandle,
731 } else if (lrc == -ENOENT) {
732 if (rc == 0) /* ENOENT shouldn't rewrite any error */
734 } else if (lrc < 0) {
739 llog_handle_put(loghandle);
742 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
743 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
748 EXPORT_SYMBOL(llog_cat_cancel_records);
750 static int llog_cat_process_common(const struct lu_env *env,
751 struct llog_handle *cat_llh,
752 struct llog_rec_hdr *rec,
753 struct llog_handle **llhp)
755 struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
756 struct llog_log_hdr *hdr;
760 if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
762 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
763 cat_llh->lgh_ctxt->loc_obd->obd_name,
764 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
765 cat_llh->lgh_id.lgl_ogen, rc);
768 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
769 PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
770 le32_to_cpu(rec->lrh_index),
771 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
773 rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
775 /* After a server crash, a stub of index record in catlog could
776 * be kept, because plain log destroy + catlog index record
777 * deletion are not atomic. So we end up with an index but no
778 * actual record. Destroy the index and move on. */
779 if (rc == -ENOENT || rc == -ESTALE)
780 rc = LLOG_DEL_RECORD;
782 CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
783 cat_llh->lgh_ctxt->loc_obd->obd_name,
784 PFID(&lir->lid_id.lgl_oi.oi_fid),
785 lir->lid_id.lgl_ogen, rc);
790 /* clean old empty llogs, do not consider current llog in use */
791 /* ignore remote (lgh_obj == NULL) llogs */
792 hdr = (*llhp)->lgh_hdr;
793 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
794 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
795 *llhp != cat_llh->u.chd.chd_current_log) {
796 rc = llog_destroy(env, *llhp);
798 CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
799 (*llhp)->lgh_ctxt->loc_obd->obd_name,
800 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
807 static int llog_cat_process_cb(const struct lu_env *env,
808 struct llog_handle *cat_llh,
809 struct llog_rec_hdr *rec, void *data)
811 struct llog_process_data *d = data;
812 struct llog_handle *llh = NULL;
816 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
820 if (rec->lrh_index < d->lpd_startcat) {
821 /* Skip processing of the logs until startcat */
823 } else if (d->lpd_startidx > 0) {
824 struct llog_process_cat_data cd;
826 cd.lpcd_first_idx = d->lpd_startidx;
827 cd.lpcd_last_idx = 0;
828 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
830 /* Continue processing the next log from idx 0 */
833 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
838 /* The empty plain log was destroyed while processing */
839 if (rc == LLOG_DEL_PLAIN) {
840 rc = llog_cat_cleanup(env, cat_llh, llh,
841 llh->u.phd.phd_cookie.lgc_index);
842 } else if (rc == LLOG_DEL_RECORD) {
843 /* clear wrong catalog entry */
844 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
848 llog_handle_put(llh);
853 int llog_cat_process_or_fork(const struct lu_env *env,
854 struct llog_handle *cat_llh, llog_cb_t cat_cb,
855 llog_cb_t cb, void *data, int startcat,
856 int startidx, bool fork)
858 struct llog_process_data d;
859 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
863 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
866 d.lpd_startcat = startcat;
867 d.lpd_startidx = startidx;
869 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
870 llh->llh_count > 1) {
871 struct llog_process_cat_data cd;
873 CWARN("%s: catlog "DFID" crosses index zero\n",
874 cat_llh->lgh_ctxt->loc_obd->obd_name,
875 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
877 cd.lpcd_first_idx = llh->llh_cat_idx;
878 cd.lpcd_last_idx = 0;
879 rc = llog_process_or_fork(env, cat_llh, cat_cb,
884 cd.lpcd_first_idx = 0;
885 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
886 rc = llog_process_or_fork(env, cat_llh, cat_cb,
889 rc = llog_process_or_fork(env, cat_llh, cat_cb,
896 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
897 llog_cb_t cb, void *data, int startcat, int startidx)
899 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
900 cb, data, startcat, startidx, false);
902 EXPORT_SYMBOL(llog_cat_process);
904 static int llog_cat_size_cb(const struct lu_env *env,
905 struct llog_handle *cat_llh,
906 struct llog_rec_hdr *rec, void *data)
908 struct llog_process_data *d = data;
909 struct llog_handle *llh = NULL;
910 __u64 *cum_size = d->lpd_data;
915 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
917 if (rc == LLOG_DEL_PLAIN) {
918 /* empty log was deleted, don't count it */
919 rc = llog_cat_cleanup(env, cat_llh, llh,
920 llh->u.phd.phd_cookie.lgc_index);
921 } else if (rc == LLOG_DEL_RECORD) {
922 /* clear wrong catalog entry */
923 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
925 size = llog_size(env, llh);
928 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
929 PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
933 llog_handle_put(llh);
938 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
940 __u64 size = llog_size(env, cat_llh);
942 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
943 NULL, &size, 0, 0, false);
947 EXPORT_SYMBOL(llog_cat_size);
949 /* currently returns the number of "free" entries in catalog,
950 * ie the available entries for a new plain LLOG file creation,
951 * even if catalog has wrapped
953 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
955 /* simulate almost full Catalog */
956 if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
959 if (cat_llh->lgh_hdr->llh_count == 1)
960 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
962 if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
963 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
964 cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
966 /* catalog is presently wrapped */
967 return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
969 EXPORT_SYMBOL(llog_cat_free_space);
971 static int llog_cat_reverse_process_cb(const struct lu_env *env,
972 struct llog_handle *cat_llh,
973 struct llog_rec_hdr *rec, void *data)
975 struct llog_process_data *d = data;
976 struct llog_handle *llh;
980 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
982 /* The empty plain log 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);
986 } else if (rc == LLOG_DEL_RECORD) {
987 /* clear wrong catalog entry */
988 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
993 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
995 /* The empty plain was destroyed while processing */
996 if (rc == LLOG_DEL_PLAIN)
997 rc = llog_cat_cleanup(env, cat_llh, llh,
998 llh->u.phd.phd_cookie.lgc_index);
1000 llog_handle_put(llh);
1004 int llog_cat_reverse_process(const struct lu_env *env,
1005 struct llog_handle *cat_llh,
1006 llog_cb_t cb, void *data)
1008 struct llog_process_data d;
1009 struct llog_process_cat_data cd;
1010 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1014 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1018 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1019 llh->llh_count > 1) {
1020 CWARN("%s: catalog "DFID" crosses index zero\n",
1021 cat_llh->lgh_ctxt->loc_obd->obd_name,
1022 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1024 cd.lpcd_first_idx = 0;
1025 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1026 rc = llog_reverse_process(env, cat_llh,
1027 llog_cat_reverse_process_cb,
1032 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1033 cd.lpcd_last_idx = 0;
1034 rc = llog_reverse_process(env, cat_llh,
1035 llog_cat_reverse_process_cb,
1038 rc = llog_reverse_process(env, cat_llh,
1039 llog_cat_reverse_process_cb,
1045 EXPORT_SYMBOL(llog_cat_reverse_process);
1047 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1049 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1054 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1056 * The llh_cat_idx equals to the first used index minus 1
1057 * so if we canceled the first index then llh_cat_idx
1060 if (llh->llh_cat_idx == (idx - 1)) {
1061 llh->llh_cat_idx = idx;
1063 while (idx != cathandle->lgh_last_idx) {
1064 idx = (idx + 1) % bitmap_size;
1065 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1066 /* update llh_cat_idx for each unset bit,
1067 * expecting the next one is set */
1068 llh->llh_cat_idx = idx;
1069 } else if (idx == 0) {
1070 /* skip header bit */
1071 llh->llh_cat_idx = 0;
1074 /* the first index is found */
1079 CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
1080 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1081 llh->llh_cat_idx, cathandle->lgh_last_idx);
1087 /* Cleanup deleted plain llog traces from catalog */
1088 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1089 struct llog_handle *loghandle, int index)
1094 if (loghandle != NULL) {
1095 /* remove destroyed llog from catalog list and
1096 * chd_current_log variable */
1097 down_write(&cathandle->lgh_lock);
1098 if (cathandle->u.chd.chd_current_log == loghandle)
1099 cathandle->u.chd.chd_current_log = NULL;
1100 list_del_init(&loghandle->u.phd.phd_entry);
1101 up_write(&cathandle->lgh_lock);
1102 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1103 /* llog was opened and keep in a list, close it now */
1104 llog_close(env, loghandle);
1107 /* do not attempt to cleanup on-disk llog if on client side */
1108 if (cathandle->lgh_obj == NULL)
1111 /* remove plain llog entry from catalog by index */
1112 llog_cat_set_first_idx(cathandle, index);
1113 rc = llog_cancel_rec(env, cathandle, index);
1115 CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
1116 index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));