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;
209 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_PLAIN_RECORDS) ||
210 OBD_FAIL_PRECHECK(OBD_FAIL_CATALOG_FULL_CHECK))) {
211 // limit the numer of plain records for test
212 loghandle->lgh_max_size = loghandle->lgh_hdr_size +
219 if (handle != NULL) {
220 handle->th_result = rc >= 0 ? 0 : rc;
221 dt_trans_stop(env, dt, handle);
226 /* to signal llog_cat_close() it shouldn't try to destroy the llog,
227 * we want to destroy it in this transaction, otherwise the object
228 * becomes an orphan */
229 loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
230 /* this is to mimic full log, so another llog_cat_current_log()
231 * can skip it and ask for another onet */
232 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(loghandle->lgh_hdr) + 1;
233 llog_trans_destroy(env, loghandle, th);
235 dt_trans_stop(env, dt, handle);
239 static int llog_cat_refresh(const struct lu_env *env,
240 struct llog_handle *cathandle)
242 struct llog_handle *loghandle;
245 down_write(&cathandle->lgh_lock);
246 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
248 if (!llog_exist(loghandle))
251 rc = llog_read_header(env, loghandle, NULL);
256 rc = llog_read_header(env, cathandle, NULL);
258 up_write(&loghandle->lgh_lock);
264 * prepare current/next log for catalog.
266 * if \a *ploghandle is NULL, open it, and declare create, NB, if \a
267 * *ploghandle is remote, create it synchronously here, see comments
270 * \a cathandle->lgh_lock is down_read-ed, it gets down_write-ed if \a
271 * *ploghandle has to be opened.
273 static int llog_cat_prep_log(const struct lu_env *env,
274 struct llog_handle *cathandle,
275 struct llog_handle **ploghandle,
284 if (IS_ERR_OR_NULL(*ploghandle)) {
285 up_read(&cathandle->lgh_lock);
286 down_write(&cathandle->lgh_lock);
288 if (IS_ERR_OR_NULL(*ploghandle)) {
289 struct llog_handle *loghandle;
291 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
292 NULL, NULL, LLOG_OPEN_NEW);
294 *ploghandle = loghandle;
295 list_add_tail(&loghandle->u.phd.phd_entry,
296 &cathandle->u.chd.chd_head);
303 rc = llog_exist(*ploghandle);
309 if (dt_object_remote(cathandle->lgh_obj)) {
310 down_write_nested(&(*ploghandle)->lgh_lock, LLOGH_LOG);
311 if (!llog_exist(*ploghandle)) {
312 /* For remote operation, if we put the llog object
313 * creation in the current transaction, then the
314 * llog object will not be created on the remote
315 * target until the transaction stop, if other
316 * operations start before the transaction stop,
317 * and use the same llog object, will be dependent
318 * on the success of this transaction. So let's
319 * create the llog object synchronously here to
320 * remove the dependency. */
321 rc = llog_cat_new_log(env, cathandle, *ploghandle,
324 up_write(&(*ploghandle)->lgh_lock);
326 up_write(&cathandle->lgh_lock);
328 up_read(&cathandle->lgh_lock);
330 rc = llog_cat_refresh(env, cathandle);
331 down_read_nested(&cathandle->lgh_lock,
335 /* *ploghandle might become NULL, restart */
339 up_write(&(*ploghandle)->lgh_lock);
341 struct llog_thread_info *lgi = llog_info(env);
342 struct llog_logid_rec *lirec = &lgi->lgi_logid;
344 rc = llog_declare_create(env, *ploghandle, th);
348 lirec->lid_hdr.lrh_len = sizeof(*lirec);
349 rc = llog_declare_write_rec(env, cathandle, &lirec->lid_hdr, -1,
355 up_write(&cathandle->lgh_lock);
356 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
363 /* Open an existent log handle and add it to the open list.
364 * This log handle will be closed when all of the records in it are removed.
366 * Assumes caller has already pushed us into the kernel context and is locking.
367 * We return a lock on the handle to ensure nobody yanks it from us.
369 * This takes extra reference on llog_handle via llog_handle_get() and require
370 * this reference to be put by caller using llog_handle_put()
372 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
373 struct llog_handle **res, struct llog_logid *logid)
375 struct llog_handle *loghandle;
381 if (cathandle == NULL)
384 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
385 down_write(&cathandle->lgh_lock);
386 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
388 struct llog_logid *cgl = &loghandle->lgh_id;
390 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
391 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
392 if (cgl->lgl_ogen != logid->lgl_ogen) {
393 CWARN("%s: log "DFID" generation %x != %x\n",
394 loghandle->lgh_ctxt->loc_obd->obd_name,
395 PFID(&logid->lgl_oi.oi_fid),
396 cgl->lgl_ogen, logid->lgl_ogen);
399 loghandle->u.phd.phd_cat_handle = cathandle;
400 up_write(&cathandle->lgh_lock);
404 up_write(&cathandle->lgh_lock);
406 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
409 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
410 cathandle->lgh_ctxt->loc_obd->obd_name,
411 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen, rc);
415 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
417 llog_close(env, loghandle);
422 down_write(&cathandle->lgh_lock);
423 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
424 up_write(&cathandle->lgh_lock);
426 loghandle->u.phd.phd_cat_handle = cathandle;
427 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
428 loghandle->u.phd.phd_cookie.lgc_index =
429 loghandle->lgh_hdr->llh_cat_idx;
432 llog_handle_get(loghandle);
437 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
439 struct llog_handle *loghandle, *n;
444 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
446 struct llog_log_hdr *llh = loghandle->lgh_hdr;
449 /* unlink open-not-created llogs */
450 list_del_init(&loghandle->u.phd.phd_entry);
451 llh = loghandle->lgh_hdr;
452 if (loghandle->lgh_obj != NULL && llh != NULL &&
453 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
454 (llh->llh_count == 1)) {
455 rc = llog_destroy(env, loghandle);
457 CERROR("%s: failure destroying log during "
458 "cleanup: rc = %d\n",
459 loghandle->lgh_ctxt->loc_obd->obd_name,
462 index = loghandle->u.phd.phd_cookie.lgc_index;
463 llog_cat_cleanup(env, cathandle, NULL, index);
465 llog_close(env, loghandle);
467 /* if handle was stored in ctxt, remove it too */
468 if (cathandle->lgh_ctxt->loc_handle == cathandle)
469 cathandle->lgh_ctxt->loc_handle = NULL;
470 rc = llog_close(env, cathandle);
473 EXPORT_SYMBOL(llog_cat_close);
475 /** Return the currently active log handle. If the current log handle doesn't
476 * have enough space left for the current record, start a new one.
478 * If reclen is 0, we only want to know what the currently active log is,
479 * otherwise we get a lock on this log so nobody can steal our space.
481 * Assumes caller has already pushed us into the kernel context and is locking.
483 * NOTE: loghandle is write-locked upon successful return
485 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
488 struct llog_handle *loghandle = NULL;
492 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
493 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
494 GOTO(next, loghandle);
497 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
498 loghandle = cathandle->u.chd.chd_current_log;
500 struct llog_log_hdr *llh;
502 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
503 llh = loghandle->lgh_hdr;
504 if (llh == NULL || !llog_is_full(loghandle)) {
505 up_read(&cathandle->lgh_lock);
508 up_write(&loghandle->lgh_lock);
511 up_read(&cathandle->lgh_lock);
513 /* time to use next log */
515 /* first, we have to make sure the state hasn't changed */
516 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
517 loghandle = cathandle->u.chd.chd_current_log;
519 struct llog_log_hdr *llh;
521 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
522 llh = loghandle->lgh_hdr;
523 if (llh == NULL || !llog_is_full(loghandle))
524 GOTO(out_unlock, loghandle);
526 up_write(&loghandle->lgh_lock);
530 /* Sigh, the chd_next_log and chd_current_log is initialized
531 * in declare phase, and we do not serialize the catlog
532 * accessing, so it might be possible the llog creation
533 * thread (see llog_cat_declare_add_rec()) did not create
534 * llog successfully, then the following thread might
535 * meet this situation. */
536 if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
537 CERROR("%s: next log does not exist!\n",
538 cathandle->lgh_ctxt->loc_obd->obd_name);
539 loghandle = ERR_PTR(-EIO);
540 if (cathandle->u.chd.chd_next_log == NULL) {
541 /* Store the error in chd_next_log, so
542 * the following process can get correct
544 cathandle->u.chd.chd_next_log = loghandle;
546 GOTO(out_unlock, loghandle);
549 CDEBUG(D_INODE, "use next log\n");
551 loghandle = cathandle->u.chd.chd_next_log;
552 cathandle->u.chd.chd_current_log = loghandle;
553 cathandle->u.chd.chd_next_log = NULL;
554 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
557 up_write(&cathandle->lgh_lock);
562 /* Add a single record to the recovery log(s) using a catalog
563 * Returns as llog_write_record
565 * Assumes caller has already pushed us into the kernel context.
567 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
568 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
571 struct llog_handle *loghandle;
575 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
578 loghandle = llog_cat_current_log(cathandle, th);
579 if (IS_ERR(loghandle))
580 RETURN(PTR_ERR(loghandle));
582 /* loghandle is already locked by llog_cat_current_log() for us */
583 if (!llog_exist(loghandle)) {
584 rc = llog_cat_new_log(env, cathandle, loghandle, th);
586 up_write(&loghandle->lgh_lock);
587 /* nobody should be trying to use this llog */
588 down_write(&cathandle->lgh_lock);
589 if (cathandle->u.chd.chd_current_log == loghandle)
590 cathandle->u.chd.chd_current_log = NULL;
591 up_write(&cathandle->lgh_lock);
595 /* now let's try to add the record */
596 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
598 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
599 "llog_write_rec %d: lh=%p\n", rc, loghandle);
600 /* -ENOSPC is returned if no empty records left
601 * and when it's lack of space on the stogage.
602 * there is no point to try again if it's the second
603 * case. many callers (like llog test) expect ENOSPC,
604 * so we preserve this error code, but look for the
605 * actual cause here */
606 if (rc == -ENOSPC && llog_is_full(loghandle))
609 up_write(&loghandle->lgh_lock);
611 if (rc == -ENOBUFS) {
614 CERROR("%s: error on 2nd llog: rc = %d\n",
615 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
620 EXPORT_SYMBOL(llog_cat_add_rec);
622 int llog_cat_declare_add_rec(const struct lu_env *env,
623 struct llog_handle *cathandle,
624 struct llog_rec_hdr *rec, struct thandle *th)
631 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
632 rc = llog_cat_prep_log(env, cathandle,
633 &cathandle->u.chd.chd_current_log, th);
637 rc = llog_cat_prep_log(env, cathandle, &cathandle->u.chd.chd_next_log,
642 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
644 if (rc == -ESTALE && dt_object_remote(cathandle->lgh_obj)) {
645 up_read(&cathandle->lgh_lock);
646 rc = llog_cat_refresh(env, cathandle);
654 * XXX: we hope for declarations made for existing llog this might be
655 * not correct with some backends where declarations are expected
656 * against specific object like ZFS with full debugging enabled.
658 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_next_log, rec, -1,
662 up_read(&cathandle->lgh_lock);
665 EXPORT_SYMBOL(llog_cat_declare_add_rec);
667 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
668 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
670 struct llog_ctxt *ctxt;
671 struct dt_device *dt;
672 struct thandle *th = NULL;
675 ctxt = cathandle->lgh_ctxt;
677 LASSERT(ctxt->loc_exp);
679 LASSERT(cathandle->lgh_obj != NULL);
680 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
682 th = dt_trans_create(env, dt);
686 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
690 rc = dt_trans_start_local(env, dt, th);
693 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
695 dt_trans_stop(env, dt, th);
698 EXPORT_SYMBOL(llog_cat_add);
700 int llog_cat_cancel_arr_rec(const struct lu_env *env,
701 struct llog_handle *cathandle,
702 struct llog_logid *lgl, int count, int *index)
704 struct llog_handle *loghandle;
708 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
710 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
712 cathandle->lgh_ctxt->loc_obd->obd_name,
713 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
717 if ((cathandle->lgh_ctxt->loc_flags &
718 LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
719 /* For update log, some of loghandles of cathandle
720 * might not exist because remote llog creation might
721 * be failed, so let's skip the record cancellation
722 * for these non-exist llogs.
725 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
727 cathandle->lgh_ctxt->loc_obd->obd_name,
728 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
730 llog_handle_put(loghandle);
734 rc = llog_cancel_arr_rec(env, loghandle, count, index);
735 if (rc == LLOG_DEL_PLAIN) { /* log has been destroyed */
738 cat_index = loghandle->u.phd.phd_cookie.lgc_index;
739 rc = llog_cat_cleanup(env, cathandle, loghandle, cat_index);
741 CERROR("%s: fail to cancel catalog record: rc = %d\n",
742 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
746 llog_handle_put(loghandle);
749 CERROR("%s: fail to cancel %d llog-records: rc = %d\n",
750 cathandle->lgh_ctxt->loc_obd->obd_name, count,
755 EXPORT_SYMBOL(llog_cat_cancel_arr_rec);
757 /* For each cookie in the cookie array, we clear the log in-use bit and either:
758 * - the log is empty, so mark it free in the catalog header and delete it
759 * - the log is not empty, just write out the log header
761 * The cookies may be in different log files, so we need to get new logs
764 * Assumes caller has already pushed us into the kernel context.
766 int llog_cat_cancel_records(const struct lu_env *env,
767 struct llog_handle *cathandle, int count,
768 struct llog_cookie *cookies)
770 int i, rc = 0, failed = 0;
774 for (i = 0; i < count; i++, cookies++) {
777 lrc = llog_cat_cancel_arr_rec(env, cathandle, &cookies->lgc_lgl,
778 1, &cookies->lgc_index);
786 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
787 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
791 EXPORT_SYMBOL(llog_cat_cancel_records);
793 static int llog_cat_process_common(const struct lu_env *env,
794 struct llog_handle *cat_llh,
795 struct llog_rec_hdr *rec,
796 struct llog_handle **llhp)
798 struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
799 struct llog_log_hdr *hdr;
803 if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
805 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
806 cat_llh->lgh_ctxt->loc_obd->obd_name,
807 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
808 cat_llh->lgh_id.lgl_ogen, rc);
811 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
812 PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
813 le32_to_cpu(rec->lrh_index),
814 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
816 rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
818 /* After a server crash, a stub of index record in catlog could
819 * be kept, because plain log destroy + catlog index record
820 * deletion are not atomic. So we end up with an index but no
821 * actual record. Destroy the index and move on. */
822 if (rc == -ENOENT || rc == -ESTALE)
823 rc = LLOG_DEL_RECORD;
825 CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
826 cat_llh->lgh_ctxt->loc_obd->obd_name,
827 PFID(&lir->lid_id.lgl_oi.oi_fid),
828 lir->lid_id.lgl_ogen, rc);
833 /* clean old empty llogs, do not consider current llog in use */
834 /* ignore remote (lgh_obj == NULL) llogs */
835 hdr = (*llhp)->lgh_hdr;
836 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
837 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
838 *llhp != cat_llh->u.chd.chd_current_log) {
839 rc = llog_destroy(env, *llhp);
841 CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
842 (*llhp)->lgh_ctxt->loc_obd->obd_name,
843 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
850 static int llog_cat_process_cb(const struct lu_env *env,
851 struct llog_handle *cat_llh,
852 struct llog_rec_hdr *rec, void *data)
854 struct llog_process_data *d = data;
855 struct llog_handle *llh = NULL;
859 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
863 if (rec->lrh_index < d->lpd_startcat) {
864 /* Skip processing of the logs until startcat */
866 } else if (d->lpd_startidx > 0) {
867 struct llog_process_cat_data cd;
869 cd.lpcd_first_idx = d->lpd_startidx;
870 cd.lpcd_last_idx = 0;
871 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
873 /* Continue processing the next log from idx 0 */
876 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
881 /* The empty plain log was destroyed while processing */
882 if (rc == LLOG_DEL_PLAIN) {
883 rc = llog_cat_cleanup(env, cat_llh, llh,
884 llh->u.phd.phd_cookie.lgc_index);
885 } else if (rc == LLOG_DEL_RECORD) {
886 /* clear wrong catalog entry */
887 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
891 llog_handle_put(llh);
896 int llog_cat_process_or_fork(const struct lu_env *env,
897 struct llog_handle *cat_llh, llog_cb_t cat_cb,
898 llog_cb_t cb, void *data, int startcat,
899 int startidx, bool fork)
901 struct llog_process_data d;
902 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
907 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
910 d.lpd_startcat = (startcat == LLOG_CAT_FIRST ? 0 : startcat);
911 d.lpd_startidx = startidx;
913 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
914 llh->llh_count > 1) {
915 struct llog_process_cat_data cd;
917 CWARN("%s: catlog "DFID" crosses index zero\n",
918 cat_llh->lgh_ctxt->loc_obd->obd_name,
919 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
920 /*startcat = 0 is default value for general processing */
921 if ((startcat != LLOG_CAT_FIRST &&
922 startcat >= llh->llh_cat_idx) || !startcat) {
923 /* processing the catalog part at the end */
924 cd.lpcd_first_idx = (startcat ? startcat :
926 if (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS))
927 cd.lpcd_last_idx = cfs_fail_val;
929 cd.lpcd_last_idx = 0;
930 rc = llog_process_or_fork(env, cat_llh, cat_cb,
932 /* Reset the startcat becasue it has already reached
940 /* processing the catalog part at the begining */
941 cd.lpcd_first_idx = (startcat == LLOG_CAT_FIRST) ? 0 : startcat;
942 /* Note, the processing will stop at the lgh_last_idx value,
943 * and it could be increased during processing. So records
944 * between current lgh_last_idx and lgh_last_idx in future
945 * would left unprocessed.
947 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
948 rc = llog_process_or_fork(env, cat_llh, cat_cb,
951 rc = llog_process_or_fork(env, cat_llh, cat_cb,
957 EXPORT_SYMBOL(llog_cat_process_or_fork);
959 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
960 llog_cb_t cb, void *data, int startcat, int startidx)
962 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
963 cb, data, startcat, startidx, false);
965 EXPORT_SYMBOL(llog_cat_process);
967 static int llog_cat_size_cb(const struct lu_env *env,
968 struct llog_handle *cat_llh,
969 struct llog_rec_hdr *rec, void *data)
971 struct llog_process_data *d = data;
972 struct llog_handle *llh = NULL;
973 __u64 *cum_size = d->lpd_data;
978 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
980 if (rc == LLOG_DEL_PLAIN) {
981 /* empty log was deleted, don't count it */
982 rc = llog_cat_cleanup(env, cat_llh, llh,
983 llh->u.phd.phd_cookie.lgc_index);
984 } else if (rc == LLOG_DEL_RECORD) {
985 /* clear wrong catalog entry */
986 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
988 size = llog_size(env, llh);
991 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
992 PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
996 llog_handle_put(llh);
1001 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
1003 __u64 size = llog_size(env, cat_llh);
1005 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
1006 NULL, &size, 0, 0, false);
1010 EXPORT_SYMBOL(llog_cat_size);
1012 /* currently returns the number of "free" entries in catalog,
1013 * ie the available entries for a new plain LLOG file creation,
1014 * even if catalog has wrapped
1016 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
1018 /* simulate almost full Catalog */
1019 if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
1020 return cfs_fail_val;
1022 if (cat_llh->lgh_hdr->llh_count == 1)
1023 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
1025 if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
1026 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
1027 cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1029 /* catalog is presently wrapped */
1030 return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1032 EXPORT_SYMBOL(llog_cat_free_space);
1034 static int llog_cat_reverse_process_cb(const struct lu_env *env,
1035 struct llog_handle *cat_llh,
1036 struct llog_rec_hdr *rec, void *data)
1038 struct llog_process_data *d = data;
1039 struct llog_handle *llh;
1043 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
1045 /* The empty plain log was destroyed while processing */
1046 if (rc == LLOG_DEL_PLAIN) {
1047 rc = llog_cat_cleanup(env, cat_llh, llh,
1048 llh->u.phd.phd_cookie.lgc_index);
1049 } else if (rc == LLOG_DEL_RECORD) {
1050 /* clear wrong catalog entry */
1051 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
1056 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1058 /* The empty plain was destroyed while processing */
1059 if (rc == LLOG_DEL_PLAIN)
1060 rc = llog_cat_cleanup(env, cat_llh, llh,
1061 llh->u.phd.phd_cookie.lgc_index);
1063 llog_handle_put(llh);
1067 int llog_cat_reverse_process(const struct lu_env *env,
1068 struct llog_handle *cat_llh,
1069 llog_cb_t cb, void *data)
1071 struct llog_process_data d;
1072 struct llog_process_cat_data cd;
1073 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1077 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1081 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1082 llh->llh_count > 1) {
1083 CWARN("%s: catalog "DFID" crosses index zero\n",
1084 cat_llh->lgh_ctxt->loc_obd->obd_name,
1085 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1087 cd.lpcd_first_idx = 0;
1088 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1089 rc = llog_reverse_process(env, cat_llh,
1090 llog_cat_reverse_process_cb,
1095 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1096 cd.lpcd_last_idx = 0;
1097 rc = llog_reverse_process(env, cat_llh,
1098 llog_cat_reverse_process_cb,
1101 rc = llog_reverse_process(env, cat_llh,
1102 llog_cat_reverse_process_cb,
1108 EXPORT_SYMBOL(llog_cat_reverse_process);
1110 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1112 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1117 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1119 * The llh_cat_idx equals to the first used index minus 1
1120 * so if we canceled the first index then llh_cat_idx
1123 if (llh->llh_cat_idx == (idx - 1)) {
1124 llh->llh_cat_idx = idx;
1126 while (idx != cathandle->lgh_last_idx) {
1127 idx = (idx + 1) % bitmap_size;
1128 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1129 /* update llh_cat_idx for each unset bit,
1130 * expecting the next one is set */
1131 llh->llh_cat_idx = idx;
1132 } else if (idx == 0) {
1133 /* skip header bit */
1134 llh->llh_cat_idx = 0;
1137 /* the first index is found */
1142 CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
1143 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1144 llh->llh_cat_idx, cathandle->lgh_last_idx);
1150 /* Cleanup deleted plain llog traces from catalog */
1151 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1152 struct llog_handle *loghandle, int index)
1157 if (loghandle != NULL) {
1158 /* remove destroyed llog from catalog list and
1159 * chd_current_log variable */
1160 down_write(&cathandle->lgh_lock);
1161 if (cathandle->u.chd.chd_current_log == loghandle)
1162 cathandle->u.chd.chd_current_log = NULL;
1163 list_del_init(&loghandle->u.phd.phd_entry);
1164 up_write(&cathandle->lgh_lock);
1165 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1166 /* llog was opened and keep in a list, close it now */
1167 llog_close(env, loghandle);
1170 /* do not attempt to cleanup on-disk llog if on client side */
1171 if (cathandle->lgh_obj == NULL)
1174 /* remove plain llog entry from catalog by index */
1175 llog_cat_set_first_idx(cathandle, index);
1176 rc = llog_cancel_rec(env, cathandle, index);
1178 CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
1179 index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));