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/
31 * lustre/obdclass/llog_cat.c
33 * OST<->MDS recovery logging infrastructure.
35 * Invariants in implementation:
36 * - we do not share logs among different OST<->MDS connections, so that
37 * if an OST or MDS fails it need only look at log(s) relevant to itself
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.pershin@intel.com>
44 #define DEBUG_SUBSYSTEM S_LOG
47 #include <obd_class.h>
49 #include "llog_internal.h"
53 * lockdep markers for nested struct llog_handle::lgh_lock locking.
60 /* Create a new log handle and add it to the open list.
61 * This log handle will be closed when all of the records in it are removed.
63 * Assumes caller has already pushed us into the kernel context and is locking.
65 static int llog_cat_new_log(const struct lu_env *env,
66 struct llog_handle *cathandle,
67 struct llog_handle *loghandle,
70 struct llog_thread_info *lgi = llog_info(env);
71 struct llog_logid_rec *rec = &lgi->lgi_logid;
72 struct thandle *handle = NULL;
73 struct dt_device *dt = NULL;
74 struct llog_log_hdr *llh = cathandle->lgh_hdr;
79 index = (cathandle->lgh_last_idx + 1) %
80 (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ? (cfs_fail_val + 1) :
81 LLOG_HDR_BITMAP_SIZE(llh));
83 /* check that new llog index will not overlap with the first one.
84 * - llh_cat_idx is the index just before the first/oldest still in-use
86 * - lgh_last_idx is the last/newest used index in catalog
88 * When catalog is not wrapped yet then lgh_last_idx is always larger
89 * than llh_cat_idx. After the wrap around lgh_last_idx re-starts
90 * from 0 and llh_cat_idx becomes the upper limit for it
92 * Check if catalog has already wrapped around or not by comparing
93 * last_idx and cat_idx */
94 if ((index == llh->llh_cat_idx + 1 && llh->llh_count > 1) ||
95 (index == 0 && llh->llh_cat_idx == 0)) {
96 if (cathandle->lgh_name == NULL) {
97 CWARN("%s: there are no more free slots in catalog "
99 loghandle2name(loghandle),
100 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
101 cathandle->lgh_id.lgl_ogen);
103 CWARN("%s: there are no more free slots in "
104 "catalog %s\n", loghandle2name(loghandle),
105 cathandle->lgh_name);
110 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
113 if (loghandle->lgh_hdr != NULL) {
114 /* If llog object is remote and creation is failed, lgh_hdr
115 * might be left over here, free it first */
116 LASSERT(!llog_exist(loghandle));
117 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
118 loghandle->lgh_hdr = NULL;
122 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
124 handle = dt_trans_create(env, dt);
126 RETURN(PTR_ERR(handle));
128 /* Create update llog object synchronously, which
129 * happens during inialization process see
130 * lod_sub_prep_llog(), to make sure the update
131 * llog object is created before corss-MDT writing
132 * updates into the llog object */
133 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
136 handle->th_wait_submit = 1;
138 rc = llog_declare_create(env, loghandle, handle);
142 rec->lid_hdr.lrh_len = sizeof(*rec);
143 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
144 rec->lid_id = loghandle->lgh_id;
145 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
150 rc = dt_trans_start_local(env, dt, handle);
157 rc = llog_create(env, loghandle, th);
158 /* if llog is already created, no need to initialize it */
161 } else if (rc != 0) {
162 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
163 loghandle2name(loghandle), rc);
167 rc = llog_init_handle(env, loghandle,
168 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
169 &cathandle->lgh_hdr->llh_tgtuuid);
173 /* build the record for this log in the catalog */
174 rec->lid_hdr.lrh_len = sizeof(*rec);
175 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
176 rec->lid_id = loghandle->lgh_id;
178 /* append the new record into catalog. The new index will be
179 * assigned to the record and updated in rec header */
180 rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
181 &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
183 GOTO(out_destroy, rc);
185 CDEBUG(D_OTHER, "new plain log "DFID".%u of catalog "DFID"\n",
186 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rec->lid_hdr.lrh_index,
187 PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
189 loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
191 /* limit max size of plain llog so that space can be
192 * released sooner, especially on small filesystems */
193 /* 2MB for the cases when free space hasn't been learned yet */
194 loghandle->lgh_max_size = 2 << 20;
195 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
196 rc = dt_statfs(env, dt, &lgi->lgi_statfs);
197 if (rc == 0 && lgi->lgi_statfs.os_bfree > 0) {
198 __u64 freespace = (lgi->lgi_statfs.os_bfree *
199 lgi->lgi_statfs.os_bsize) >> 6;
200 if (freespace < loghandle->lgh_max_size)
201 loghandle->lgh_max_size = freespace;
202 /* shouldn't be > 128MB in any case?
203 * it's 256K records of 512 bytes each */
204 if (freespace > (128 << 20))
205 loghandle->lgh_max_size = 128 << 20;
207 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_PLAIN_RECORDS) ||
208 OBD_FAIL_PRECHECK(OBD_FAIL_CATALOG_FULL_CHECK))) {
209 // limit the numer of plain records for test
210 loghandle->lgh_max_size = loghandle->lgh_hdr_size +
217 if (handle != NULL) {
218 handle->th_result = rc >= 0 ? 0 : rc;
219 dt_trans_stop(env, dt, handle);
224 /* to signal llog_cat_close() it shouldn't try to destroy the llog,
225 * we want to destroy it in this transaction, otherwise the object
226 * becomes an orphan */
227 loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
228 /* this is to mimic full log, so another llog_cat_current_log()
229 * can skip it and ask for another onet */
230 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(loghandle->lgh_hdr) + 1;
231 llog_trans_destroy(env, loghandle, th);
233 dt_trans_stop(env, dt, handle);
237 static int llog_cat_refresh(const struct lu_env *env,
238 struct llog_handle *cathandle)
240 struct llog_handle *loghandle;
243 down_write(&cathandle->lgh_lock);
244 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
246 if (!llog_exist(loghandle))
249 down_write(&loghandle->lgh_lock);
250 rc = llog_read_header(env, loghandle, NULL);
251 up_write(&loghandle->lgh_lock);
256 rc = llog_read_header(env, cathandle, NULL);
258 up_write(&cathandle->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 loghandle2name(loghandle),
395 PFID(&logid->lgl_oi.oi_fid),
396 cgl->lgl_ogen, logid->lgl_ogen);
399 *res = llog_handle_get(loghandle);
401 CERROR("%s: log "DFID" refcount is zero!\n",
402 loghandle2name(loghandle),
403 PFID(&logid->lgl_oi.oi_fid));
406 loghandle->u.phd.phd_cat_handle = cathandle;
407 up_write(&cathandle->lgh_lock);
411 up_write(&cathandle->lgh_lock);
413 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
416 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
417 loghandle2name(cathandle), PFID(&logid->lgl_oi.oi_fid),
418 logid->lgl_ogen, rc);
422 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN |
423 LLOG_F_ZAP_WHEN_EMPTY | fmt, NULL);
425 llog_close(env, loghandle);
430 *res = llog_handle_get(loghandle);
432 down_write(&cathandle->lgh_lock);
433 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
434 up_write(&cathandle->lgh_lock);
436 loghandle->u.phd.phd_cat_handle = cathandle;
437 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
438 loghandle->u.phd.phd_cookie.lgc_index =
439 loghandle->lgh_hdr->llh_cat_idx;
443 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
445 struct llog_handle *loghandle, *n;
450 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
452 struct llog_log_hdr *llh = loghandle->lgh_hdr;
455 /* unlink open-not-created llogs */
456 list_del_init(&loghandle->u.phd.phd_entry);
457 llh = loghandle->lgh_hdr;
458 if (loghandle->lgh_obj != NULL && llh != NULL &&
459 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
460 (llh->llh_count == 1)) {
461 rc = llog_destroy(env, loghandle);
463 CERROR("%s: failure destroying log during "
464 "cleanup: rc = %d\n",
465 loghandle2name(loghandle), rc);
467 index = loghandle->u.phd.phd_cookie.lgc_index;
468 llog_cat_cleanup(env, cathandle, NULL, index);
470 llog_close(env, loghandle);
472 /* if handle was stored in ctxt, remove it too */
473 if (cathandle->lgh_ctxt->loc_handle == cathandle)
474 cathandle->lgh_ctxt->loc_handle = NULL;
475 rc = llog_close(env, cathandle);
478 EXPORT_SYMBOL(llog_cat_close);
480 /** Return the currently active log handle. If the current log handle doesn't
481 * have enough space left for the current record, start a new one.
483 * If reclen is 0, we only want to know what the currently active log is,
484 * otherwise we get a lock on this log so nobody can steal our space.
486 * Assumes caller has already pushed us into the kernel context and is locking.
488 * NOTE: loghandle is write-locked upon successful return
490 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
493 struct llog_handle *loghandle = NULL;
497 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
498 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
499 GOTO(next, loghandle);
502 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
503 loghandle = cathandle->u.chd.chd_current_log;
505 struct llog_log_hdr *llh;
507 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
508 llh = loghandle->lgh_hdr;
509 if (llh == NULL || !llog_is_full(loghandle)) {
510 up_read(&cathandle->lgh_lock);
513 up_write(&loghandle->lgh_lock);
516 up_read(&cathandle->lgh_lock);
518 /* time to use next log */
520 /* first, we have to make sure the state hasn't changed */
521 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
522 loghandle = cathandle->u.chd.chd_current_log;
524 struct llog_log_hdr *llh;
526 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
527 llh = loghandle->lgh_hdr;
528 if (llh == NULL || !llog_is_full(loghandle))
529 GOTO(out_unlock, loghandle);
531 up_write(&loghandle->lgh_lock);
535 /* Sigh, the chd_next_log and chd_current_log is initialized
536 * in declare phase, and we do not serialize the catlog
537 * accessing, so it might be possible the llog creation
538 * thread (see llog_cat_declare_add_rec()) did not create
539 * llog successfully, then the following thread might
540 * meet this situation. */
541 if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
542 CERROR("%s: next log does not exist!\n",
543 loghandle2name(cathandle));
544 loghandle = ERR_PTR(-EIO);
545 if (cathandle->u.chd.chd_next_log == NULL) {
546 /* Store the error in chd_next_log, so
547 * the following process can get correct
549 cathandle->u.chd.chd_next_log = loghandle;
551 GOTO(out_unlock, loghandle);
554 CDEBUG(D_INODE, "use next log\n");
556 loghandle = cathandle->u.chd.chd_next_log;
557 cathandle->u.chd.chd_current_log = loghandle;
558 cathandle->u.chd.chd_next_log = NULL;
559 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
562 up_write(&cathandle->lgh_lock);
567 /* Add a single record to the recovery log(s) using a catalog
568 * Returns as llog_write_record
570 * Assumes caller has already pushed us into the kernel context.
572 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
573 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
576 struct llog_handle *loghandle;
580 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
583 loghandle = llog_cat_current_log(cathandle, th);
584 if (IS_ERR(loghandle))
585 RETURN(PTR_ERR(loghandle));
587 /* loghandle is already locked by llog_cat_current_log() for us */
588 if (!llog_exist(loghandle)) {
589 rc = llog_cat_new_log(env, cathandle, loghandle, th);
591 up_write(&loghandle->lgh_lock);
592 /* nobody should be trying to use this llog */
593 down_write(&cathandle->lgh_lock);
594 if (cathandle->u.chd.chd_current_log == loghandle)
595 cathandle->u.chd.chd_current_log = NULL;
596 up_write(&cathandle->lgh_lock);
600 /* now let's try to add the record */
601 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
603 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
604 "llog_write_rec %d: lh=%p\n", rc, loghandle);
605 /* -ENOSPC is returned if no empty records left
606 * and when it's lack of space on the stogage.
607 * there is no point to try again if it's the second
608 * case. many callers (like llog test) expect ENOSPC,
609 * so we preserve this error code, but look for the
610 * actual cause here */
611 if (rc == -ENOSPC && llog_is_full(loghandle))
614 up_write(&loghandle->lgh_lock);
616 if (rc == -ENOBUFS) {
619 CERROR("%s: error on 2nd llog: rc = %d\n",
620 loghandle2name(cathandle), rc);
625 EXPORT_SYMBOL(llog_cat_add_rec);
627 int llog_cat_declare_add_rec(const struct lu_env *env,
628 struct llog_handle *cathandle,
629 struct llog_rec_hdr *rec, struct thandle *th)
636 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
637 rc = llog_cat_prep_log(env, cathandle,
638 &cathandle->u.chd.chd_current_log, th);
642 rc = llog_cat_prep_log(env, cathandle, &cathandle->u.chd.chd_next_log,
647 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
649 if (rc == -ESTALE && dt_object_remote(cathandle->lgh_obj)) {
650 up_read(&cathandle->lgh_lock);
651 rc = llog_cat_refresh(env, cathandle);
659 * XXX: we hope for declarations made for existing llog this might be
660 * not correct with some backends where declarations are expected
661 * against specific object like ZFS with full debugging enabled.
663 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_next_log, rec, -1,
667 up_read(&cathandle->lgh_lock);
670 EXPORT_SYMBOL(llog_cat_declare_add_rec);
672 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
673 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
675 struct llog_ctxt *ctxt;
676 struct dt_device *dt;
677 struct thandle *th = NULL;
680 ctxt = cathandle->lgh_ctxt;
682 LASSERT(ctxt->loc_exp);
684 LASSERT(cathandle->lgh_obj != NULL);
685 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
687 th = dt_trans_create(env, dt);
691 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
695 rc = dt_trans_start_local(env, dt, th);
698 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
700 dt_trans_stop(env, dt, th);
703 EXPORT_SYMBOL(llog_cat_add);
705 int llog_cat_cancel_arr_rec(const struct lu_env *env,
706 struct llog_handle *cathandle,
707 struct llog_logid *lgl, int count, int *index)
709 struct llog_handle *loghandle;
713 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
715 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
716 ": rc = %d\n", loghandle2name(cathandle),
717 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
721 if ((cathandle->lgh_ctxt->loc_flags &
722 LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
723 /* For update log, some of loghandles of cathandle
724 * might not exist because remote llog creation might
725 * be failed, so let's skip the record cancellation
726 * for these non-exist llogs.
729 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
730 ": rc = %d\n", loghandle2name(cathandle),
731 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
733 llog_handle_put(env, loghandle);
737 rc = llog_cancel_arr_rec(env, loghandle, count, index);
738 if (rc == LLOG_DEL_PLAIN) { /* log has been destroyed */
741 cat_index = loghandle->u.phd.phd_cookie.lgc_index;
742 rc = llog_cat_cleanup(env, cathandle, loghandle, cat_index);
744 CERROR("%s: fail to cancel catalog record: rc = %d\n",
745 loghandle2name(cathandle), rc);
749 llog_handle_put(env, loghandle);
752 CERROR("%s: fail to cancel %d llog-records: rc = %d\n",
753 loghandle2name(cathandle), count, rc);
757 EXPORT_SYMBOL(llog_cat_cancel_arr_rec);
759 /* For each cookie in the cookie array, we clear the log in-use bit and either:
760 * - the log is empty, so mark it free in the catalog header and delete it
761 * - the log is not empty, just write out the log header
763 * The cookies may be in different log files, so we need to get new logs
766 * Assumes caller has already pushed us into the kernel context.
768 int llog_cat_cancel_records(const struct lu_env *env,
769 struct llog_handle *cathandle, int count,
770 struct llog_cookie *cookies)
772 int i, rc = 0, failed = 0;
776 for (i = 0; i < count; i++, cookies++) {
779 lrc = llog_cat_cancel_arr_rec(env, cathandle, &cookies->lgc_lgl,
780 1, &cookies->lgc_index);
788 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
789 loghandle2name(cathandle), failed, count, rc);
792 EXPORT_SYMBOL(llog_cat_cancel_records);
794 static int llog_cat_process_common(const struct lu_env *env,
795 struct llog_handle *cat_llh,
796 struct llog_rec_hdr *rec,
797 struct llog_handle **llhp)
799 struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
800 struct llog_log_hdr *hdr;
804 if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
806 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
807 loghandle2name(cat_llh),
808 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
809 cat_llh->lgh_id.lgl_ogen, rc);
812 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
813 PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
814 le32_to_cpu(rec->lrh_index),
815 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
817 rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
819 /* After a server crash, a stub of index record in catlog could
820 * be kept, because plain log destroy + catlog index record
821 * deletion are not atomic. So we end up with an index but no
822 * actual record. Destroy the index and move on. */
823 if (rc == -ENOENT || rc == -ESTALE)
824 rc = LLOG_DEL_RECORD;
826 CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
827 loghandle2name(cat_llh),
828 PFID(&lir->lid_id.lgl_oi.oi_fid),
829 lir->lid_id.lgl_ogen, rc);
834 /* clean old empty llogs, do not consider current llog in use */
835 /* ignore remote (lgh_obj == NULL) llogs */
836 hdr = (*llhp)->lgh_hdr;
837 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
838 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
839 *llhp != cat_llh->u.chd.chd_current_log &&
840 *llhp != cat_llh->u.chd.chd_next_log) {
841 rc = llog_destroy(env, *llhp);
843 CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
844 loghandle2name((*llhp)),
845 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
852 static int llog_cat_process_cb(const struct lu_env *env,
853 struct llog_handle *cat_llh,
854 struct llog_rec_hdr *rec, void *data)
856 struct llog_process_data *d = data;
857 struct llog_handle *llh = NULL;
861 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
865 if (rec->lrh_index < d->lpd_startcat) {
866 /* Skip processing of the logs until startcat */
868 } else if (d->lpd_startidx > 0) {
869 struct llog_process_cat_data cd;
871 cd.lpcd_first_idx = d->lpd_startidx;
872 cd.lpcd_last_idx = 0;
873 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
875 /* Continue processing the next log from idx 0 */
878 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
881 if (rc == -ENOENT && (cat_llh->lgh_hdr->llh_flags & LLOG_F_RM_ON_ERR)) {
883 * plain llog is reported corrupted, so better to just remove
884 * it if the caller is fine with that.
886 CERROR("%s: remove corrupted/missing llog "DFID"\n",
887 loghandle2name(cat_llh),
888 PFID(&llh->lgh_id.lgl_oi.oi_fid));
893 /* The empty plain log was destroyed while processing */
894 if (rc == LLOG_DEL_PLAIN || rc == LLOG_DEL_RECORD)
895 /* clear wrong catalog entry */
896 rc = llog_cat_cleanup(env, cat_llh, llh, rec->lrh_index);
899 llog_handle_put(env, llh);
904 int llog_cat_process_or_fork(const struct lu_env *env,
905 struct llog_handle *cat_llh, llog_cb_t cat_cb,
906 llog_cb_t cb, void *data, int startcat,
907 int startidx, bool fork)
909 struct llog_process_data d;
910 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
915 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
918 d.lpd_startcat = (startcat == LLOG_CAT_FIRST ? 0 : startcat);
919 d.lpd_startidx = startidx;
921 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
922 llh->llh_count > 1) {
923 struct llog_process_cat_data cd;
925 CWARN("%s: catlog "DFID" crosses index zero\n",
926 loghandle2name(cat_llh),
927 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
928 /*startcat = 0 is default value for general processing */
929 if ((startcat != LLOG_CAT_FIRST &&
930 startcat >= llh->llh_cat_idx) || !startcat) {
931 /* processing the catalog part at the end */
932 cd.lpcd_first_idx = (startcat ? startcat :
934 if (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS))
935 cd.lpcd_last_idx = cfs_fail_val;
937 cd.lpcd_last_idx = 0;
938 rc = llog_process_or_fork(env, cat_llh, cat_cb,
940 /* Reset the startcat becasue it has already reached
948 /* processing the catalog part at the begining */
949 cd.lpcd_first_idx = (startcat == LLOG_CAT_FIRST) ? 0 : startcat;
950 /* Note, the processing will stop at the lgh_last_idx value,
951 * and it could be increased during processing. So records
952 * between current lgh_last_idx and lgh_last_idx in future
953 * would left unprocessed.
955 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
956 rc = llog_process_or_fork(env, cat_llh, cat_cb,
959 rc = llog_process_or_fork(env, cat_llh, cat_cb,
965 EXPORT_SYMBOL(llog_cat_process_or_fork);
967 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
968 llog_cb_t cb, void *data, int startcat, int startidx)
970 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
971 cb, data, startcat, startidx, false);
973 EXPORT_SYMBOL(llog_cat_process);
975 static int llog_cat_size_cb(const struct lu_env *env,
976 struct llog_handle *cat_llh,
977 struct llog_rec_hdr *rec, void *data)
979 struct llog_process_data *d = data;
980 struct llog_handle *llh = NULL;
981 __u64 *cum_size = d->lpd_data;
986 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
988 if (rc == LLOG_DEL_PLAIN) {
989 /* empty log was deleted, don't count it */
990 rc = llog_cat_cleanup(env, cat_llh, llh,
991 llh->u.phd.phd_cookie.lgc_index);
992 } else if (rc == LLOG_DEL_RECORD) {
993 /* clear wrong catalog entry */
994 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
996 size = llog_size(env, llh);
999 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
1000 PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
1004 llog_handle_put(env, llh);
1009 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
1011 __u64 size = llog_size(env, cat_llh);
1013 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
1014 NULL, &size, 0, 0, false);
1018 EXPORT_SYMBOL(llog_cat_size);
1020 /* currently returns the number of "free" entries in catalog,
1021 * ie the available entries for a new plain LLOG file creation,
1022 * even if catalog has wrapped
1024 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
1026 /* simulate almost full Catalog */
1027 if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
1028 return cfs_fail_val;
1030 if (cat_llh->lgh_hdr->llh_count == 1)
1031 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
1033 if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
1034 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
1035 cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1037 /* catalog is presently wrapped */
1038 return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1040 EXPORT_SYMBOL(llog_cat_free_space);
1042 static int llog_cat_reverse_process_cb(const struct lu_env *env,
1043 struct llog_handle *cat_llh,
1044 struct llog_rec_hdr *rec, void *data)
1046 struct llog_process_data *d = data;
1047 struct llog_handle *llh;
1051 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
1053 /* The empty plain log was destroyed while processing */
1054 if (rc == LLOG_DEL_PLAIN) {
1055 rc = llog_cat_cleanup(env, cat_llh, llh,
1056 llh->u.phd.phd_cookie.lgc_index);
1057 } else if (rc == LLOG_DEL_RECORD) {
1058 /* clear wrong catalog entry */
1059 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
1064 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1066 /* The empty plain was destroyed while processing */
1067 if (rc == LLOG_DEL_PLAIN)
1068 rc = llog_cat_cleanup(env, cat_llh, llh,
1069 llh->u.phd.phd_cookie.lgc_index);
1071 llog_handle_put(env, llh);
1075 int llog_cat_reverse_process(const struct lu_env *env,
1076 struct llog_handle *cat_llh,
1077 llog_cb_t cb, void *data)
1079 struct llog_process_data d;
1080 struct llog_process_cat_data cd;
1081 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1085 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1089 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1090 llh->llh_count > 1) {
1091 CWARN("%s: catalog "DFID" crosses index zero\n",
1092 loghandle2name(cat_llh),
1093 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1095 cd.lpcd_first_idx = 0;
1096 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1097 rc = llog_reverse_process(env, cat_llh,
1098 llog_cat_reverse_process_cb,
1103 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1104 cd.lpcd_last_idx = 0;
1105 rc = llog_reverse_process(env, cat_llh,
1106 llog_cat_reverse_process_cb,
1109 rc = llog_reverse_process(env, cat_llh,
1110 llog_cat_reverse_process_cb,
1116 EXPORT_SYMBOL(llog_cat_reverse_process);
1118 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1120 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1125 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1127 * The llh_cat_idx equals to the first used index minus 1
1128 * so if we canceled the first index then llh_cat_idx
1131 if (llh->llh_cat_idx == (idx - 1)) {
1132 llh->llh_cat_idx = idx;
1134 while (idx != cathandle->lgh_last_idx) {
1135 idx = (idx + 1) % bitmap_size;
1136 if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
1137 /* update llh_cat_idx for each unset bit,
1138 * expecting the next one is set */
1139 llh->llh_cat_idx = idx;
1140 } else if (idx == 0) {
1141 /* skip header bit */
1142 llh->llh_cat_idx = 0;
1145 /* the first index is found */
1150 CDEBUG(D_HA, "catlog "DFID" first idx %u, last_idx %u\n",
1151 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1152 llh->llh_cat_idx, cathandle->lgh_last_idx);
1158 /* Cleanup deleted plain llog traces from catalog */
1159 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1160 struct llog_handle *loghandle, int index)
1163 struct lu_fid fid = {.f_seq = 0, .f_oid = 0, .f_ver = 0};
1166 if (loghandle != NULL) {
1167 /* remove destroyed llog from catalog list and
1168 * chd_current_log variable */
1169 fid = loghandle->lgh_id.lgl_oi.oi_fid;
1170 down_write(&cathandle->lgh_lock);
1171 if (cathandle->u.chd.chd_current_log == loghandle)
1172 cathandle->u.chd.chd_current_log = NULL;
1173 list_del_init(&loghandle->u.phd.phd_entry);
1174 up_write(&cathandle->lgh_lock);
1175 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index ||
1176 loghandle->u.phd.phd_cookie.lgc_index == 0);
1177 /* llog was opened and keep in a list, close it now */
1178 llog_close(env, loghandle);
1181 /* do not attempt to cleanup on-disk llog if on client side */
1182 if (cathandle->lgh_obj == NULL)
1185 /* remove plain llog entry from catalog by index */
1186 llog_cat_set_first_idx(cathandle, index);
1187 rc = llog_cancel_rec(env, cathandle, index);
1190 "cancel plain log "DFID" at index %u of catalog "DFID"\n",
1192 PFID(&cathandle->lgh_id.lgl_oi.oi_fid));