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 rc = llog_read_header(env, loghandle, NULL);
254 rc = llog_read_header(env, cathandle, NULL);
256 up_write(&loghandle->lgh_lock);
262 * prepare current/next log for catalog.
264 * if \a *ploghandle is NULL, open it, and declare create, NB, if \a
265 * *ploghandle is remote, create it synchronously here, see comments
268 * \a cathandle->lgh_lock is down_read-ed, it gets down_write-ed if \a
269 * *ploghandle has to be opened.
271 static int llog_cat_prep_log(const struct lu_env *env,
272 struct llog_handle *cathandle,
273 struct llog_handle **ploghandle,
282 if (IS_ERR_OR_NULL(*ploghandle)) {
283 up_read(&cathandle->lgh_lock);
284 down_write(&cathandle->lgh_lock);
286 if (IS_ERR_OR_NULL(*ploghandle)) {
287 struct llog_handle *loghandle;
289 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
290 NULL, NULL, LLOG_OPEN_NEW);
292 *ploghandle = loghandle;
293 list_add_tail(&loghandle->u.phd.phd_entry,
294 &cathandle->u.chd.chd_head);
301 rc = llog_exist(*ploghandle);
307 if (dt_object_remote(cathandle->lgh_obj)) {
308 down_write_nested(&(*ploghandle)->lgh_lock, LLOGH_LOG);
309 if (!llog_exist(*ploghandle)) {
310 /* For remote operation, if we put the llog object
311 * creation in the current transaction, then the
312 * llog object will not be created on the remote
313 * target until the transaction stop, if other
314 * operations start before the transaction stop,
315 * and use the same llog object, will be dependent
316 * on the success of this transaction. So let's
317 * create the llog object synchronously here to
318 * remove the dependency. */
319 rc = llog_cat_new_log(env, cathandle, *ploghandle,
322 up_write(&(*ploghandle)->lgh_lock);
324 up_write(&cathandle->lgh_lock);
326 up_read(&cathandle->lgh_lock);
328 rc = llog_cat_refresh(env, cathandle);
329 down_read_nested(&cathandle->lgh_lock,
333 /* *ploghandle might become NULL, restart */
337 up_write(&(*ploghandle)->lgh_lock);
339 struct llog_thread_info *lgi = llog_info(env);
340 struct llog_logid_rec *lirec = &lgi->lgi_logid;
342 rc = llog_declare_create(env, *ploghandle, th);
346 lirec->lid_hdr.lrh_len = sizeof(*lirec);
347 rc = llog_declare_write_rec(env, cathandle, &lirec->lid_hdr, -1,
353 up_write(&cathandle->lgh_lock);
354 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
361 /* Open an existent log handle and add it to the open list.
362 * This log handle will be closed when all of the records in it are removed.
364 * Assumes caller has already pushed us into the kernel context and is locking.
365 * We return a lock on the handle to ensure nobody yanks it from us.
367 * This takes extra reference on llog_handle via llog_handle_get() and require
368 * this reference to be put by caller using llog_handle_put()
370 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
371 struct llog_handle **res, struct llog_logid *logid)
373 struct llog_handle *loghandle;
379 if (cathandle == NULL)
382 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
383 down_write(&cathandle->lgh_lock);
384 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
386 struct llog_logid *cgl = &loghandle->lgh_id;
388 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
389 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
390 if (cgl->lgl_ogen != logid->lgl_ogen) {
391 CWARN("%s: log "DFID" generation %x != %x\n",
392 loghandle2name(loghandle),
393 PFID(&logid->lgl_oi.oi_fid),
394 cgl->lgl_ogen, logid->lgl_ogen);
397 *res = llog_handle_get(loghandle);
399 CERROR("%s: log "DFID" refcount is zero!\n",
400 loghandle2name(loghandle),
401 PFID(&logid->lgl_oi.oi_fid));
404 loghandle->u.phd.phd_cat_handle = cathandle;
405 up_write(&cathandle->lgh_lock);
409 up_write(&cathandle->lgh_lock);
411 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
414 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
415 loghandle2name(cathandle), PFID(&logid->lgl_oi.oi_fid),
416 logid->lgl_ogen, rc);
420 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN |
421 LLOG_F_ZAP_WHEN_EMPTY | fmt, NULL);
423 llog_close(env, loghandle);
428 *res = llog_handle_get(loghandle);
430 down_write(&cathandle->lgh_lock);
431 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
432 up_write(&cathandle->lgh_lock);
434 loghandle->u.phd.phd_cat_handle = cathandle;
435 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
436 loghandle->u.phd.phd_cookie.lgc_index =
437 loghandle->lgh_hdr->llh_cat_idx;
441 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
443 struct llog_handle *loghandle, *n;
448 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
450 struct llog_log_hdr *llh = loghandle->lgh_hdr;
453 /* unlink open-not-created llogs */
454 list_del_init(&loghandle->u.phd.phd_entry);
455 llh = loghandle->lgh_hdr;
456 if (loghandle->lgh_obj != NULL && llh != NULL &&
457 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
458 (llh->llh_count == 1)) {
459 rc = llog_destroy(env, loghandle);
461 CERROR("%s: failure destroying log during "
462 "cleanup: rc = %d\n",
463 loghandle2name(loghandle), rc);
465 index = loghandle->u.phd.phd_cookie.lgc_index;
466 llog_cat_cleanup(env, cathandle, NULL, index);
468 llog_close(env, loghandle);
470 /* if handle was stored in ctxt, remove it too */
471 if (cathandle->lgh_ctxt->loc_handle == cathandle)
472 cathandle->lgh_ctxt->loc_handle = NULL;
473 rc = llog_close(env, cathandle);
476 EXPORT_SYMBOL(llog_cat_close);
478 /** Return the currently active log handle. If the current log handle doesn't
479 * have enough space left for the current record, start a new one.
481 * If reclen is 0, we only want to know what the currently active log is,
482 * otherwise we get a lock on this log so nobody can steal our space.
484 * Assumes caller has already pushed us into the kernel context and is locking.
486 * NOTE: loghandle is write-locked upon successful return
488 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
491 struct llog_handle *loghandle = NULL;
495 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
496 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
497 GOTO(next, loghandle);
500 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
501 loghandle = cathandle->u.chd.chd_current_log;
503 struct llog_log_hdr *llh;
505 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
506 llh = loghandle->lgh_hdr;
507 if (llh == NULL || !llog_is_full(loghandle)) {
508 up_read(&cathandle->lgh_lock);
511 up_write(&loghandle->lgh_lock);
514 up_read(&cathandle->lgh_lock);
516 /* time to use next log */
518 /* first, we have to make sure the state hasn't changed */
519 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
520 loghandle = cathandle->u.chd.chd_current_log;
522 struct llog_log_hdr *llh;
524 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
525 llh = loghandle->lgh_hdr;
526 if (llh == NULL || !llog_is_full(loghandle))
527 GOTO(out_unlock, loghandle);
529 up_write(&loghandle->lgh_lock);
533 /* Sigh, the chd_next_log and chd_current_log is initialized
534 * in declare phase, and we do not serialize the catlog
535 * accessing, so it might be possible the llog creation
536 * thread (see llog_cat_declare_add_rec()) did not create
537 * llog successfully, then the following thread might
538 * meet this situation. */
539 if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
540 CERROR("%s: next log does not exist!\n",
541 loghandle2name(cathandle));
542 loghandle = ERR_PTR(-EIO);
543 if (cathandle->u.chd.chd_next_log == NULL) {
544 /* Store the error in chd_next_log, so
545 * the following process can get correct
547 cathandle->u.chd.chd_next_log = loghandle;
549 GOTO(out_unlock, loghandle);
552 CDEBUG(D_INODE, "use next log\n");
554 loghandle = cathandle->u.chd.chd_next_log;
555 cathandle->u.chd.chd_current_log = loghandle;
556 cathandle->u.chd.chd_next_log = NULL;
557 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
560 up_write(&cathandle->lgh_lock);
565 /* Add a single record to the recovery log(s) using a catalog
566 * Returns as llog_write_record
568 * Assumes caller has already pushed us into the kernel context.
570 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
571 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
574 struct llog_handle *loghandle;
578 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
581 loghandle = llog_cat_current_log(cathandle, th);
582 if (IS_ERR(loghandle))
583 RETURN(PTR_ERR(loghandle));
585 /* loghandle is already locked by llog_cat_current_log() for us */
586 if (!llog_exist(loghandle)) {
587 rc = llog_cat_new_log(env, cathandle, loghandle, th);
589 up_write(&loghandle->lgh_lock);
590 /* nobody should be trying to use this llog */
591 down_write(&cathandle->lgh_lock);
592 if (cathandle->u.chd.chd_current_log == loghandle)
593 cathandle->u.chd.chd_current_log = NULL;
594 up_write(&cathandle->lgh_lock);
598 /* now let's try to add the record */
599 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
601 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
602 "llog_write_rec %d: lh=%p\n", rc, loghandle);
603 /* -ENOSPC is returned if no empty records left
604 * and when it's lack of space on the stogage.
605 * there is no point to try again if it's the second
606 * case. many callers (like llog test) expect ENOSPC,
607 * so we preserve this error code, but look for the
608 * actual cause here */
609 if (rc == -ENOSPC && llog_is_full(loghandle))
612 up_write(&loghandle->lgh_lock);
614 if (rc == -ENOBUFS) {
617 CERROR("%s: error on 2nd llog: rc = %d\n",
618 loghandle2name(cathandle), rc);
623 EXPORT_SYMBOL(llog_cat_add_rec);
625 int llog_cat_declare_add_rec(const struct lu_env *env,
626 struct llog_handle *cathandle,
627 struct llog_rec_hdr *rec, struct thandle *th)
634 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
635 rc = llog_cat_prep_log(env, cathandle,
636 &cathandle->u.chd.chd_current_log, th);
640 rc = llog_cat_prep_log(env, cathandle, &cathandle->u.chd.chd_next_log,
645 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
647 if (rc == -ESTALE && dt_object_remote(cathandle->lgh_obj)) {
648 up_read(&cathandle->lgh_lock);
649 rc = llog_cat_refresh(env, cathandle);
657 * XXX: we hope for declarations made for existing llog this might be
658 * not correct with some backends where declarations are expected
659 * against specific object like ZFS with full debugging enabled.
661 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_next_log, rec, -1,
665 up_read(&cathandle->lgh_lock);
668 EXPORT_SYMBOL(llog_cat_declare_add_rec);
670 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
671 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
673 struct llog_ctxt *ctxt;
674 struct dt_device *dt;
675 struct thandle *th = NULL;
678 ctxt = cathandle->lgh_ctxt;
680 LASSERT(ctxt->loc_exp);
682 LASSERT(cathandle->lgh_obj != NULL);
683 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
685 th = dt_trans_create(env, dt);
689 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
693 rc = dt_trans_start_local(env, dt, th);
696 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
698 dt_trans_stop(env, dt, th);
701 EXPORT_SYMBOL(llog_cat_add);
703 int llog_cat_cancel_arr_rec(const struct lu_env *env,
704 struct llog_handle *cathandle,
705 struct llog_logid *lgl, int count, int *index)
707 struct llog_handle *loghandle;
711 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
713 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
714 ": rc = %d\n", loghandle2name(cathandle),
715 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
719 if ((cathandle->lgh_ctxt->loc_flags &
720 LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
721 /* For update log, some of loghandles of cathandle
722 * might not exist because remote llog creation might
723 * be failed, so let's skip the record cancellation
724 * for these non-exist llogs.
727 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
728 ": rc = %d\n", loghandle2name(cathandle),
729 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
731 llog_handle_put(env, loghandle);
735 rc = llog_cancel_arr_rec(env, loghandle, count, index);
736 if (rc == LLOG_DEL_PLAIN) { /* log has been destroyed */
739 cat_index = loghandle->u.phd.phd_cookie.lgc_index;
740 rc = llog_cat_cleanup(env, cathandle, loghandle, cat_index);
742 CERROR("%s: fail to cancel catalog record: rc = %d\n",
743 loghandle2name(cathandle), rc);
747 llog_handle_put(env, loghandle);
750 CERROR("%s: fail to cancel %d llog-records: rc = %d\n",
751 loghandle2name(cathandle), count, rc);
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 loghandle2name(cathandle), failed, count, rc);
790 EXPORT_SYMBOL(llog_cat_cancel_records);
792 static int llog_cat_process_common(const struct lu_env *env,
793 struct llog_handle *cat_llh,
794 struct llog_rec_hdr *rec,
795 struct llog_handle **llhp)
797 struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
798 struct llog_log_hdr *hdr;
802 if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
804 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
805 loghandle2name(cat_llh),
806 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
807 cat_llh->lgh_id.lgl_ogen, rc);
810 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
811 PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
812 le32_to_cpu(rec->lrh_index),
813 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
815 rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
817 /* After a server crash, a stub of index record in catlog could
818 * be kept, because plain log destroy + catlog index record
819 * deletion are not atomic. So we end up with an index but no
820 * actual record. Destroy the index and move on. */
821 if (rc == -ENOENT || rc == -ESTALE)
822 rc = LLOG_DEL_RECORD;
824 CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
825 loghandle2name(cat_llh),
826 PFID(&lir->lid_id.lgl_oi.oi_fid),
827 lir->lid_id.lgl_ogen, rc);
832 /* clean old empty llogs, do not consider current llog in use */
833 /* ignore remote (lgh_obj == NULL) llogs */
834 hdr = (*llhp)->lgh_hdr;
835 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
836 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
837 *llhp != cat_llh->u.chd.chd_current_log &&
838 *llhp != cat_llh->u.chd.chd_next_log) {
839 rc = llog_destroy(env, *llhp);
841 CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
842 loghandle2name((*llhp)),
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,
879 if (rc == -ENOENT && (cat_llh->lgh_hdr->llh_flags & LLOG_F_RM_ON_ERR)) {
881 * plain llog is reported corrupted, so better to just remove
882 * it if the caller is fine with that.
884 CERROR("%s: remove corrupted/missing llog "DFID"\n",
885 loghandle2name(cat_llh),
886 PFID(&llh->lgh_id.lgl_oi.oi_fid));
891 /* The empty plain log was destroyed while processing */
892 if (rc == LLOG_DEL_PLAIN || rc == LLOG_DEL_RECORD)
893 /* clear wrong catalog entry */
894 rc = llog_cat_cleanup(env, cat_llh, llh, rec->lrh_index);
897 llog_handle_put(env, llh);
902 int llog_cat_process_or_fork(const struct lu_env *env,
903 struct llog_handle *cat_llh, llog_cb_t cat_cb,
904 llog_cb_t cb, void *data, int startcat,
905 int startidx, bool fork)
907 struct llog_process_data d;
908 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
913 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
916 d.lpd_startcat = (startcat == LLOG_CAT_FIRST ? 0 : startcat);
917 d.lpd_startidx = startidx;
919 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
920 llh->llh_count > 1) {
921 struct llog_process_cat_data cd;
923 CWARN("%s: catlog "DFID" crosses index zero\n",
924 loghandle2name(cat_llh),
925 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
926 /*startcat = 0 is default value for general processing */
927 if ((startcat != LLOG_CAT_FIRST &&
928 startcat >= llh->llh_cat_idx) || !startcat) {
929 /* processing the catalog part at the end */
930 cd.lpcd_first_idx = (startcat ? startcat :
932 if (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS))
933 cd.lpcd_last_idx = cfs_fail_val;
935 cd.lpcd_last_idx = 0;
936 rc = llog_process_or_fork(env, cat_llh, cat_cb,
938 /* Reset the startcat becasue it has already reached
946 /* processing the catalog part at the begining */
947 cd.lpcd_first_idx = (startcat == LLOG_CAT_FIRST) ? 0 : startcat;
948 /* Note, the processing will stop at the lgh_last_idx value,
949 * and it could be increased during processing. So records
950 * between current lgh_last_idx and lgh_last_idx in future
951 * would left unprocessed.
953 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
954 rc = llog_process_or_fork(env, cat_llh, cat_cb,
957 rc = llog_process_or_fork(env, cat_llh, cat_cb,
963 EXPORT_SYMBOL(llog_cat_process_or_fork);
965 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
966 llog_cb_t cb, void *data, int startcat, int startidx)
968 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
969 cb, data, startcat, startidx, false);
971 EXPORT_SYMBOL(llog_cat_process);
973 static int llog_cat_size_cb(const struct lu_env *env,
974 struct llog_handle *cat_llh,
975 struct llog_rec_hdr *rec, void *data)
977 struct llog_process_data *d = data;
978 struct llog_handle *llh = NULL;
979 __u64 *cum_size = d->lpd_data;
984 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
986 if (rc == LLOG_DEL_PLAIN) {
987 /* empty log was deleted, don't count it */
988 rc = llog_cat_cleanup(env, cat_llh, llh,
989 llh->u.phd.phd_cookie.lgc_index);
990 } else if (rc == LLOG_DEL_RECORD) {
991 /* clear wrong catalog entry */
992 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
994 size = llog_size(env, llh);
997 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
998 PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
1002 llog_handle_put(env, llh);
1007 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
1009 __u64 size = llog_size(env, cat_llh);
1011 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
1012 NULL, &size, 0, 0, false);
1016 EXPORT_SYMBOL(llog_cat_size);
1018 /* currently returns the number of "free" entries in catalog,
1019 * ie the available entries for a new plain LLOG file creation,
1020 * even if catalog has wrapped
1022 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
1024 /* simulate almost full Catalog */
1025 if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
1026 return cfs_fail_val;
1028 if (cat_llh->lgh_hdr->llh_count == 1)
1029 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
1031 if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
1032 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
1033 cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1035 /* catalog is presently wrapped */
1036 return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1038 EXPORT_SYMBOL(llog_cat_free_space);
1040 static int llog_cat_reverse_process_cb(const struct lu_env *env,
1041 struct llog_handle *cat_llh,
1042 struct llog_rec_hdr *rec, void *data)
1044 struct llog_process_data *d = data;
1045 struct llog_handle *llh;
1049 rc = llog_cat_process_common(env, cat_llh, rec, &llh);
1051 /* The empty plain log was destroyed while processing */
1052 if (rc == LLOG_DEL_PLAIN) {
1053 rc = llog_cat_cleanup(env, cat_llh, llh,
1054 llh->u.phd.phd_cookie.lgc_index);
1055 } else if (rc == LLOG_DEL_RECORD) {
1056 /* clear wrong catalog entry */
1057 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
1062 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1064 /* The empty plain was destroyed while processing */
1065 if (rc == LLOG_DEL_PLAIN)
1066 rc = llog_cat_cleanup(env, cat_llh, llh,
1067 llh->u.phd.phd_cookie.lgc_index);
1069 llog_handle_put(env, llh);
1073 int llog_cat_reverse_process(const struct lu_env *env,
1074 struct llog_handle *cat_llh,
1075 llog_cb_t cb, void *data)
1077 struct llog_process_data d;
1078 struct llog_process_cat_data cd;
1079 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1083 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1087 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1088 llh->llh_count > 1) {
1089 CWARN("%s: catalog "DFID" crosses index zero\n",
1090 loghandle2name(cat_llh),
1091 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1093 cd.lpcd_first_idx = 0;
1094 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1095 rc = llog_reverse_process(env, cat_llh,
1096 llog_cat_reverse_process_cb,
1101 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1102 cd.lpcd_last_idx = 0;
1103 rc = llog_reverse_process(env, cat_llh,
1104 llog_cat_reverse_process_cb,
1107 rc = llog_reverse_process(env, cat_llh,
1108 llog_cat_reverse_process_cb,
1114 EXPORT_SYMBOL(llog_cat_reverse_process);
1116 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1118 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1123 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1125 * The llh_cat_idx equals to the first used index minus 1
1126 * so if we canceled the first index then llh_cat_idx
1129 if (llh->llh_cat_idx == (idx - 1)) {
1130 llh->llh_cat_idx = idx;
1132 while (idx != cathandle->lgh_last_idx) {
1133 idx = (idx + 1) % bitmap_size;
1134 if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
1135 /* update llh_cat_idx for each unset bit,
1136 * expecting the next one is set */
1137 llh->llh_cat_idx = idx;
1138 } else if (idx == 0) {
1139 /* skip header bit */
1140 llh->llh_cat_idx = 0;
1143 /* the first index is found */
1148 CDEBUG(D_HA, "catlog "DFID" first idx %u, last_idx %u\n",
1149 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1150 llh->llh_cat_idx, cathandle->lgh_last_idx);
1156 /* Cleanup deleted plain llog traces from catalog */
1157 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1158 struct llog_handle *loghandle, int index)
1161 struct lu_fid fid = {.f_seq = 0, .f_oid = 0, .f_ver = 0};
1164 if (loghandle != NULL) {
1165 /* remove destroyed llog from catalog list and
1166 * chd_current_log variable */
1167 fid = loghandle->lgh_id.lgl_oi.oi_fid;
1168 down_write(&cathandle->lgh_lock);
1169 if (cathandle->u.chd.chd_current_log == loghandle)
1170 cathandle->u.chd.chd_current_log = NULL;
1171 list_del_init(&loghandle->u.phd.phd_entry);
1172 up_write(&cathandle->lgh_lock);
1173 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index ||
1174 loghandle->u.phd.phd_cookie.lgc_index == 0);
1175 /* llog was opened and keep in a list, close it now */
1176 llog_close(env, loghandle);
1179 /* do not attempt to cleanup on-disk llog if on client side */
1180 if (cathandle->lgh_obj == NULL)
1183 /* remove plain llog entry from catalog by index */
1184 llog_cat_set_first_idx(cathandle, index);
1185 rc = llog_cancel_rec(env, cathandle, index);
1188 "cancel plain log "DFID" at index %u of catalog "DFID"\n",
1190 PFID(&cathandle->lgh_id.lgl_oi.oi_fid));