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 int llog_cat_cancel_arr_rec(const struct lu_env *env,
675 struct llog_handle *cathandle,
676 struct llog_logid *lgl, int count, int *index)
678 struct llog_handle *loghandle;
682 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
684 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
686 cathandle->lgh_ctxt->loc_obd->obd_name,
687 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
691 if ((cathandle->lgh_ctxt->loc_flags &
692 LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
693 /* For update log, some of loghandles of cathandle
694 * might not exist because remote llog creation might
695 * be failed, so let's skip the record cancellation
696 * for these non-exist llogs.
699 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
701 cathandle->lgh_ctxt->loc_obd->obd_name,
702 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
704 llog_handle_put(loghandle);
708 rc = llog_cancel_arr_rec(env, loghandle, count, index);
709 if (rc == LLOG_DEL_PLAIN) { /* log has been destroyed */
712 cat_index = loghandle->u.phd.phd_cookie.lgc_index;
713 rc = llog_cat_cleanup(env, cathandle, loghandle, cat_index);
715 CERROR("%s: fail to cancel catalog record: rc = %d\n",
716 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
720 llog_handle_put(loghandle);
723 CERROR("%s: fail to cancel %d llog-records: rc = %d\n",
724 cathandle->lgh_ctxt->loc_obd->obd_name, count,
729 EXPORT_SYMBOL(llog_cat_cancel_arr_rec);
731 /* For each cookie in the cookie array, we clear the log in-use bit and either:
732 * - the log is empty, so mark it free in the catalog header and delete it
733 * - the log is not empty, just write out the log header
735 * The cookies may be in different log files, so we need to get new logs
738 * Assumes caller has already pushed us into the kernel context.
740 int llog_cat_cancel_records(const struct lu_env *env,
741 struct llog_handle *cathandle, int count,
742 struct llog_cookie *cookies)
744 int i, rc = 0, failed = 0;
748 for (i = 0; i < count; i++, cookies++) {
751 lrc = llog_cat_cancel_arr_rec(env, cathandle, &cookies->lgc_lgl,
752 1, &cookies->lgc_index);
760 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
761 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
765 EXPORT_SYMBOL(llog_cat_cancel_records);
767 static int llog_cat_process_common(const struct lu_env *env,
768 struct llog_handle *cat_llh,
769 struct llog_rec_hdr *rec,
770 struct llog_handle **llhp)
772 struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
773 struct llog_log_hdr *hdr;
777 if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
779 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
780 cat_llh->lgh_ctxt->loc_obd->obd_name,
781 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
782 cat_llh->lgh_id.lgl_ogen, rc);
785 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
786 PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
787 le32_to_cpu(rec->lrh_index),
788 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
790 rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
792 /* After a server crash, a stub of index record in catlog could
793 * be kept, because plain log destroy + catlog index record
794 * deletion are not atomic. So we end up with an index but no
795 * actual record. Destroy the index and move on. */
796 if (rc == -ENOENT || rc == -ESTALE)
797 rc = LLOG_DEL_RECORD;
799 CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
800 cat_llh->lgh_ctxt->loc_obd->obd_name,
801 PFID(&lir->lid_id.lgl_oi.oi_fid),
802 lir->lid_id.lgl_ogen, rc);
807 /* clean old empty llogs, do not consider current llog in use */
808 /* ignore remote (lgh_obj == NULL) llogs */
809 hdr = (*llhp)->lgh_hdr;
810 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
811 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
812 *llhp != cat_llh->u.chd.chd_current_log) {
813 rc = llog_destroy(env, *llhp);
815 CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
816 (*llhp)->lgh_ctxt->loc_obd->obd_name,
817 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
824 static int llog_cat_process_cb(const struct lu_env *env,
825 struct llog_handle *cat_llh,
826 struct llog_rec_hdr *rec, void *data)
828 struct llog_process_data *d = data;
829 struct llog_handle *llh = NULL;
833 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
837 if (rec->lrh_index < d->lpd_startcat) {
838 /* Skip processing of the logs until startcat */
840 } else if (d->lpd_startidx > 0) {
841 struct llog_process_cat_data cd;
843 cd.lpcd_first_idx = d->lpd_startidx;
844 cd.lpcd_last_idx = 0;
845 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
847 /* Continue processing the next log from idx 0 */
850 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
855 /* The empty plain log was destroyed while processing */
856 if (rc == LLOG_DEL_PLAIN) {
857 rc = llog_cat_cleanup(env, cat_llh, llh,
858 llh->u.phd.phd_cookie.lgc_index);
859 } else if (rc == LLOG_DEL_RECORD) {
860 /* clear wrong catalog entry */
861 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
865 llog_handle_put(llh);
870 int llog_cat_process_or_fork(const struct lu_env *env,
871 struct llog_handle *cat_llh, llog_cb_t cat_cb,
872 llog_cb_t cb, void *data, int startcat,
873 int startidx, bool fork)
875 struct llog_process_data d;
876 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
881 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
884 d.lpd_startcat = (startcat == LLOG_CAT_FIRST ? 0 : startcat);
885 d.lpd_startidx = startidx;
887 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
888 llh->llh_count > 1) {
889 struct llog_process_cat_data cd;
891 CWARN("%s: catlog "DFID" crosses index zero\n",
892 cat_llh->lgh_ctxt->loc_obd->obd_name,
893 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
894 /*startcat = 0 is default value for general processing */
895 if ((startcat != LLOG_CAT_FIRST &&
896 startcat >= llh->llh_cat_idx) || !startcat) {
897 /* processing the catalog part at the end */
898 cd.lpcd_first_idx = (startcat ? startcat :
900 if (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS))
901 cd.lpcd_last_idx = cfs_fail_val;
903 cd.lpcd_last_idx = 0;
904 rc = llog_process_or_fork(env, cat_llh, cat_cb,
906 /* Reset the startcat becasue it has already reached
913 /* processing the catalog part at the begining */
914 cd.lpcd_first_idx = (startcat == LLOG_CAT_FIRST) ? 0 : startcat;
915 /* Note, the processing will stop at the lgh_last_idx value,
916 * and it could be increased during processing. So records
917 * between current lgh_last_idx and lgh_last_idx in future
918 * would left unprocessed.
920 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
921 rc = llog_process_or_fork(env, cat_llh, cat_cb,
924 rc = llog_process_or_fork(env, cat_llh, cat_cb,
930 EXPORT_SYMBOL(llog_cat_process_or_fork);
932 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
933 llog_cb_t cb, void *data, int startcat, int startidx)
935 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
936 cb, data, startcat, startidx, false);
938 EXPORT_SYMBOL(llog_cat_process);
940 static int llog_cat_size_cb(const struct lu_env *env,
941 struct llog_handle *cat_llh,
942 struct llog_rec_hdr *rec, void *data)
944 struct llog_process_data *d = data;
945 struct llog_handle *llh = NULL;
946 __u64 *cum_size = d->lpd_data;
951 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
953 if (rc == LLOG_DEL_PLAIN) {
954 /* empty log was deleted, don't count it */
955 rc = llog_cat_cleanup(env, cat_llh, llh,
956 llh->u.phd.phd_cookie.lgc_index);
957 } else if (rc == LLOG_DEL_RECORD) {
958 /* clear wrong catalog entry */
959 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
961 size = llog_size(env, llh);
964 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
965 PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
969 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 /* currently returns the number of "free" entries in catalog,
986 * ie the available entries for a new plain LLOG file creation,
987 * even if catalog has wrapped
989 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
991 /* simulate almost full Catalog */
992 if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
995 if (cat_llh->lgh_hdr->llh_count == 1)
996 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
998 if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
999 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
1000 cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1002 /* catalog is presently wrapped */
1003 return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1005 EXPORT_SYMBOL(llog_cat_free_space);
1007 static int llog_cat_reverse_process_cb(const struct lu_env *env,
1008 struct llog_handle *cat_llh,
1009 struct llog_rec_hdr *rec, void *data)
1011 struct llog_process_data *d = data;
1012 struct llog_handle *llh;
1016 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
1018 /* The empty plain log was destroyed while processing */
1019 if (rc == LLOG_DEL_PLAIN) {
1020 rc = llog_cat_cleanup(env, cat_llh, llh,
1021 llh->u.phd.phd_cookie.lgc_index);
1022 } else if (rc == LLOG_DEL_RECORD) {
1023 /* clear wrong catalog entry */
1024 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
1029 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1031 /* The empty plain was destroyed while processing */
1032 if (rc == LLOG_DEL_PLAIN)
1033 rc = llog_cat_cleanup(env, cat_llh, llh,
1034 llh->u.phd.phd_cookie.lgc_index);
1036 llog_handle_put(llh);
1040 int llog_cat_reverse_process(const struct lu_env *env,
1041 struct llog_handle *cat_llh,
1042 llog_cb_t cb, void *data)
1044 struct llog_process_data d;
1045 struct llog_process_cat_data cd;
1046 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1050 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1054 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1055 llh->llh_count > 1) {
1056 CWARN("%s: catalog "DFID" crosses index zero\n",
1057 cat_llh->lgh_ctxt->loc_obd->obd_name,
1058 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1060 cd.lpcd_first_idx = 0;
1061 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1062 rc = llog_reverse_process(env, cat_llh,
1063 llog_cat_reverse_process_cb,
1068 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1069 cd.lpcd_last_idx = 0;
1070 rc = llog_reverse_process(env, cat_llh,
1071 llog_cat_reverse_process_cb,
1074 rc = llog_reverse_process(env, cat_llh,
1075 llog_cat_reverse_process_cb,
1081 EXPORT_SYMBOL(llog_cat_reverse_process);
1083 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1085 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1090 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1092 * The llh_cat_idx equals to the first used index minus 1
1093 * so if we canceled the first index then llh_cat_idx
1096 if (llh->llh_cat_idx == (idx - 1)) {
1097 llh->llh_cat_idx = idx;
1099 while (idx != cathandle->lgh_last_idx) {
1100 idx = (idx + 1) % bitmap_size;
1101 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1102 /* update llh_cat_idx for each unset bit,
1103 * expecting the next one is set */
1104 llh->llh_cat_idx = idx;
1105 } else if (idx == 0) {
1106 /* skip header bit */
1107 llh->llh_cat_idx = 0;
1110 /* the first index is found */
1115 CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
1116 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1117 llh->llh_cat_idx, cathandle->lgh_last_idx);
1123 /* Cleanup deleted plain llog traces from catalog */
1124 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1125 struct llog_handle *loghandle, int index)
1130 if (loghandle != NULL) {
1131 /* remove destroyed llog from catalog list and
1132 * chd_current_log variable */
1133 down_write(&cathandle->lgh_lock);
1134 if (cathandle->u.chd.chd_current_log == loghandle)
1135 cathandle->u.chd.chd_current_log = NULL;
1136 list_del_init(&loghandle->u.phd.phd_entry);
1137 up_write(&cathandle->lgh_lock);
1138 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1139 /* llog was opened and keep in a list, close it now */
1140 llog_close(env, loghandle);
1143 /* do not attempt to cleanup on-disk llog if on client side */
1144 if (cathandle->lgh_obj == NULL)
1147 /* remove plain llog entry from catalog by index */
1148 llog_cat_set_first_idx(cathandle, index);
1149 rc = llog_cancel_rec(env, cathandle, index);
1151 CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
1152 index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));