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, 2016, 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"
52 /* Create a new log handle and add it to the open list.
53 * This log handle will be closed when all of the records in it are removed.
55 * Assumes caller has already pushed us into the kernel context and is locking.
57 static int llog_cat_new_log(const struct lu_env *env,
58 struct llog_handle *cathandle,
59 struct llog_handle *loghandle,
62 struct llog_thread_info *lgi = llog_info(env);
63 struct llog_logid_rec *rec = &lgi->lgi_logid;
64 struct thandle *handle = NULL;
65 struct dt_device *dt = NULL;
66 struct llog_log_hdr *llh = cathandle->lgh_hdr;
71 index = (cathandle->lgh_last_idx + 1) %
72 (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ? (cfs_fail_val + 1) :
73 LLOG_HDR_BITMAP_SIZE(llh));
75 /* check that new llog index will not overlap with the first one.
76 * - llh_cat_idx is the index just before the first/oldest still in-use
78 * - lgh_last_idx is the last/newest used index in catalog
80 * When catalog is not wrapped yet then lgh_last_idx is always larger
81 * than llh_cat_idx. After the wrap around lgh_last_idx re-starts
82 * from 0 and llh_cat_idx becomes the upper limit for it
84 * Check if catalog has already wrapped around or not by comparing
85 * last_idx and cat_idx */
86 if ((index == llh->llh_cat_idx + 1 && llh->llh_count > 1) ||
87 (index == 0 && llh->llh_cat_idx == 0)) {
88 CWARN("%s: there are no more free slots in catalog\n",
89 loghandle->lgh_ctxt->loc_obd->obd_name);
93 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
96 if (loghandle->lgh_hdr != NULL) {
97 /* If llog object is remote and creation is failed, lgh_hdr
98 * might be left over here, free it first */
99 LASSERT(!llog_exist(loghandle));
100 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
101 loghandle->lgh_hdr = NULL;
105 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
107 handle = dt_trans_create(env, dt);
109 RETURN(PTR_ERR(handle));
111 /* Create update llog object synchronously, which
112 * happens during inialization process see
113 * lod_sub_prep_llog(), to make sure the update
114 * llog object is created before corss-MDT writing
115 * updates into the llog object */
116 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
119 handle->th_wait_submit = 1;
121 rc = llog_declare_create(env, loghandle, handle);
125 rec->lid_hdr.lrh_len = sizeof(*rec);
126 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
127 rec->lid_id = loghandle->lgh_id;
128 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
133 rc = dt_trans_start_local(env, dt, handle);
140 rc = llog_create(env, loghandle, th);
141 /* if llog is already created, no need to initialize it */
144 } else if (rc != 0) {
145 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
146 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
150 rc = llog_init_handle(env, loghandle,
151 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
152 &cathandle->lgh_hdr->llh_tgtuuid);
156 /* build the record for this log in the catalog */
157 rec->lid_hdr.lrh_len = sizeof(*rec);
158 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
159 rec->lid_id = loghandle->lgh_id;
161 /* append the new record into catalog. The new index will be
162 * assigned to the record and updated in rec header */
163 rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
164 &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
166 GOTO(out_destroy, rc);
168 CDEBUG(D_OTHER, "new plain log "DFID".%u of catalog "DFID"\n",
169 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rec->lid_hdr.lrh_index,
170 PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
172 loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
174 /* limit max size of plain llog so that space can be
175 * released sooner, especially on small filesystems */
176 /* 2MB for the cases when free space hasn't been learned yet */
177 loghandle->lgh_max_size = 2 << 20;
178 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
179 rc = dt_statfs(env, dt, &lgi->lgi_statfs);
180 if (rc == 0 && lgi->lgi_statfs.os_bfree > 0) {
181 __u64 freespace = (lgi->lgi_statfs.os_bfree *
182 lgi->lgi_statfs.os_bsize) >> 6;
183 if (freespace < loghandle->lgh_max_size)
184 loghandle->lgh_max_size = freespace;
185 /* shouldn't be > 128MB in any case?
186 * it's 256K records of 512 bytes each */
187 if (freespace > (128 << 20))
188 loghandle->lgh_max_size = 128 << 20;
193 if (handle != NULL) {
194 handle->th_result = rc >= 0 ? 0 : rc;
195 dt_trans_stop(env, dt, handle);
200 /* to signal llog_cat_close() it shouldn't try to destroy the llog,
201 * we want to destroy it in this transaction, otherwise the object
202 * becomes an orphan */
203 loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
204 /* this is to mimic full log, so another llog_cat_current_log()
205 * can skip it and ask for another onet */
206 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) + 1;
207 llog_trans_destroy(env, loghandle, th);
211 /* Open an existent log handle and add it to the open list.
212 * This log handle will be closed when all of the records in it are removed.
214 * Assumes caller has already pushed us into the kernel context and is locking.
215 * We return a lock on the handle to ensure nobody yanks it from us.
217 * This takes extra reference on llog_handle via llog_handle_get() and require
218 * this reference to be put by caller using llog_handle_put()
220 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
221 struct llog_handle **res, struct llog_logid *logid)
223 struct llog_handle *loghandle;
229 if (cathandle == NULL)
232 fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
233 down_write(&cathandle->lgh_lock);
234 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
236 struct llog_logid *cgl = &loghandle->lgh_id;
238 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
239 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
240 if (cgl->lgl_ogen != logid->lgl_ogen) {
241 CWARN("%s: log "DFID" generation %x != %x\n",
242 loghandle->lgh_ctxt->loc_obd->obd_name,
243 PFID(&logid->lgl_oi.oi_fid),
244 cgl->lgl_ogen, logid->lgl_ogen);
247 loghandle->u.phd.phd_cat_handle = cathandle;
248 up_write(&cathandle->lgh_lock);
252 up_write(&cathandle->lgh_lock);
254 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
257 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
258 cathandle->lgh_ctxt->loc_obd->obd_name,
259 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen, rc);
263 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
265 llog_close(env, loghandle);
270 down_write(&cathandle->lgh_lock);
271 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
272 up_write(&cathandle->lgh_lock);
274 loghandle->u.phd.phd_cat_handle = cathandle;
275 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
276 loghandle->u.phd.phd_cookie.lgc_index =
277 loghandle->lgh_hdr->llh_cat_idx;
280 llog_handle_get(loghandle);
285 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
287 struct llog_handle *loghandle, *n;
292 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
294 struct llog_log_hdr *llh = loghandle->lgh_hdr;
297 /* unlink open-not-created llogs */
298 list_del_init(&loghandle->u.phd.phd_entry);
299 llh = loghandle->lgh_hdr;
300 if (loghandle->lgh_obj != NULL && llh != NULL &&
301 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
302 (llh->llh_count == 1)) {
303 rc = llog_destroy(env, loghandle);
305 CERROR("%s: failure destroying log during "
306 "cleanup: rc = %d\n",
307 loghandle->lgh_ctxt->loc_obd->obd_name,
310 index = loghandle->u.phd.phd_cookie.lgc_index;
311 llog_cat_cleanup(env, cathandle, NULL, index);
313 llog_close(env, loghandle);
315 /* if handle was stored in ctxt, remove it too */
316 if (cathandle->lgh_ctxt->loc_handle == cathandle)
317 cathandle->lgh_ctxt->loc_handle = NULL;
318 rc = llog_close(env, cathandle);
321 EXPORT_SYMBOL(llog_cat_close);
324 * lockdep markers for nested struct llog_handle::lgh_lock locking.
331 /** Return the currently active log handle. If the current log handle doesn't
332 * have enough space left for the current record, start a new one.
334 * If reclen is 0, we only want to know what the currently active log is,
335 * otherwise we get a lock on this log so nobody can steal our space.
337 * Assumes caller has already pushed us into the kernel context and is locking.
339 * NOTE: loghandle is write-locked upon successful return
341 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
344 struct llog_handle *loghandle = NULL;
348 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
349 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
350 GOTO(next, loghandle);
353 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
354 loghandle = cathandle->u.chd.chd_current_log;
356 struct llog_log_hdr *llh;
358 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
359 llh = loghandle->lgh_hdr;
360 if (llh == NULL || !llog_is_full(loghandle)) {
361 up_read(&cathandle->lgh_lock);
364 up_write(&loghandle->lgh_lock);
367 up_read(&cathandle->lgh_lock);
369 /* time to use next log */
371 /* first, we have to make sure the state hasn't changed */
372 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
373 loghandle = cathandle->u.chd.chd_current_log;
375 struct llog_log_hdr *llh;
377 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
378 llh = loghandle->lgh_hdr;
380 if (!llog_is_full(loghandle))
381 GOTO(out_unlock, loghandle);
383 up_write(&loghandle->lgh_lock);
387 /* Sigh, the chd_next_log and chd_current_log is initialized
388 * in declare phase, and we do not serialize the catlog
389 * accessing, so it might be possible the llog creation
390 * thread (see llog_cat_declare_add_rec()) did not create
391 * llog successfully, then the following thread might
392 * meet this situation. */
393 if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
394 CERROR("%s: next log does not exist!\n",
395 cathandle->lgh_ctxt->loc_obd->obd_name);
396 loghandle = ERR_PTR(-EIO);
397 if (cathandle->u.chd.chd_next_log == NULL) {
398 /* Store the error in chd_next_log, so
399 * the following process can get correct
401 cathandle->u.chd.chd_next_log = loghandle;
403 GOTO(out_unlock, loghandle);
406 CDEBUG(D_INODE, "use next log\n");
408 loghandle = cathandle->u.chd.chd_next_log;
409 cathandle->u.chd.chd_current_log = loghandle;
410 cathandle->u.chd.chd_next_log = NULL;
411 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
414 up_write(&cathandle->lgh_lock);
419 static int llog_cat_update_header(const struct lu_env *env,
420 struct llog_handle *cathandle)
422 struct llog_handle *loghandle;
427 down_write(&cathandle->lgh_lock);
428 if (!cathandle->lgh_stale) {
429 up_write(&cathandle->lgh_lock);
432 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
434 if (!llog_exist(loghandle))
437 rc = llog_read_header(env, loghandle, NULL);
439 up_write(&cathandle->lgh_lock);
443 rc = llog_read_header(env, cathandle, NULL);
445 cathandle->lgh_stale = 0;
446 up_write(&cathandle->lgh_lock);
453 /* Add a single record to the recovery log(s) using a catalog
454 * Returns as llog_write_record
456 * Assumes caller has already pushed us into the kernel context.
458 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
459 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
462 struct llog_handle *loghandle;
466 LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
469 loghandle = llog_cat_current_log(cathandle, th);
470 if (IS_ERR(loghandle))
471 RETURN(PTR_ERR(loghandle));
473 /* loghandle is already locked by llog_cat_current_log() for us */
474 if (!llog_exist(loghandle)) {
475 rc = llog_cat_new_log(env, cathandle, loghandle, th);
477 up_write(&loghandle->lgh_lock);
478 /* nobody should be trying to use this llog */
479 down_write(&cathandle->lgh_lock);
480 if (cathandle->u.chd.chd_current_log == loghandle)
481 cathandle->u.chd.chd_current_log = NULL;
482 up_write(&cathandle->lgh_lock);
486 /* now let's try to add the record */
487 rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
489 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
490 "llog_write_rec %d: lh=%p\n", rc, loghandle);
491 /* -ENOSPC is returned if no empty records left
492 * and when it's lack of space on the stogage.
493 * there is no point to try again if it's the second
494 * case. many callers (like llog test) expect ENOSPC,
495 * so we preserve this error code, but look for the
496 * actual cause here */
497 if (rc == -ENOSPC && llog_is_full(loghandle))
500 up_write(&loghandle->lgh_lock);
502 if (rc == -ENOBUFS) {
505 CERROR("%s: error on 2nd llog: rc = %d\n",
506 cathandle->lgh_ctxt->loc_obd->obd_name, rc);
511 EXPORT_SYMBOL(llog_cat_add_rec);
513 int llog_cat_declare_add_rec(const struct lu_env *env,
514 struct llog_handle *cathandle,
515 struct llog_rec_hdr *rec, struct thandle *th)
517 struct llog_thread_info *lgi = llog_info(env);
518 struct llog_logid_rec *lirec = &lgi->lgi_logid;
519 struct llog_handle *loghandle, *next;
524 if (cathandle->u.chd.chd_current_log == NULL) {
525 /* declare new plain llog */
526 down_write(&cathandle->lgh_lock);
527 if (cathandle->u.chd.chd_current_log == NULL) {
528 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
529 NULL, NULL, LLOG_OPEN_NEW);
531 cathandle->u.chd.chd_current_log = loghandle;
532 list_add_tail(&loghandle->u.phd.phd_entry,
533 &cathandle->u.chd.chd_head);
536 up_write(&cathandle->lgh_lock);
537 } else if (cathandle->u.chd.chd_next_log == NULL ||
538 IS_ERR(cathandle->u.chd.chd_next_log)) {
539 /* declare next plain llog */
540 down_write(&cathandle->lgh_lock);
541 if (cathandle->u.chd.chd_next_log == NULL ||
542 IS_ERR(cathandle->u.chd.chd_next_log)) {
543 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
544 NULL, NULL, LLOG_OPEN_NEW);
546 cathandle->u.chd.chd_next_log = loghandle;
547 list_add_tail(&loghandle->u.phd.phd_entry,
548 &cathandle->u.chd.chd_head);
551 up_write(&cathandle->lgh_lock);
556 lirec->lid_hdr.lrh_len = sizeof(*lirec);
558 if (!llog_exist(cathandle->u.chd.chd_current_log)) {
559 if (dt_object_remote(cathandle->lgh_obj)) {
560 /* For remote operation, if we put the llog object
561 * creation in the current transaction, then the
562 * llog object will not be created on the remote
563 * target until the transaction stop, if other
564 * operations start before the transaction stop,
565 * and use the same llog object, will be dependent
566 * on the success of this transaction. So let's
567 * create the llog object synchronously here to
568 * remove the dependency. */
570 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
571 loghandle = cathandle->u.chd.chd_current_log;
572 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
573 if (cathandle->lgh_stale) {
574 up_write(&loghandle->lgh_lock);
575 up_read(&cathandle->lgh_lock);
576 GOTO(out, rc = -EIO);
578 if (!llog_exist(loghandle)) {
579 rc = llog_cat_new_log(env, cathandle, loghandle,
582 cathandle->lgh_stale = 1;
584 up_write(&loghandle->lgh_lock);
585 up_read(&cathandle->lgh_lock);
587 rc = llog_cat_update_header(env, cathandle);
595 rc = llog_declare_create(env,
596 cathandle->u.chd.chd_current_log, th);
599 llog_declare_write_rec(env, cathandle,
600 &lirec->lid_hdr, -1, th);
605 /* declare records in the llogs */
606 rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
609 down_write(&cathandle->lgh_lock);
610 if (cathandle->lgh_stale) {
611 up_write(&cathandle->lgh_lock);
612 GOTO(out, rc = -EIO);
615 cathandle->lgh_stale = 1;
616 up_write(&cathandle->lgh_lock);
617 rc = llog_cat_update_header(env, cathandle);
625 next = cathandle->u.chd.chd_next_log;
626 if (!IS_ERR_OR_NULL(next)) {
627 if (!llog_exist(next)) {
628 if (dt_object_remote(cathandle->lgh_obj)) {
629 /* For remote operation, if we put the llog
630 * object creation in the current transaction,
631 * then the llog object will not be created on
632 * the remote target until the transaction stop,
633 * if other operations start before the
634 * transaction stop, and use the same llog
635 * object, will be dependent on the success of
636 * this transaction. So let's create the llog
637 * object synchronously here to remove the
639 down_write_nested(&cathandle->lgh_lock,
641 next = cathandle->u.chd.chd_next_log;
642 if (IS_ERR_OR_NULL(next)) {
643 /* Sigh, another thread just tried,
644 * let's fail as well */
645 up_write(&cathandle->lgh_lock);
653 down_write_nested(&next->lgh_lock, LLOGH_LOG);
654 if (!llog_exist(next)) {
655 rc = llog_cat_new_log(env, cathandle,
658 cathandle->u.chd.chd_next_log =
661 up_write(&next->lgh_lock);
662 up_write(&cathandle->lgh_lock);
666 rc = llog_declare_create(env, next, th);
667 llog_declare_write_rec(env, cathandle,
668 &lirec->lid_hdr, -1, th);
671 /* XXX: we hope for declarations made for existing llog
672 * this might be not correct with some backends
673 * where declarations are expected against specific
674 * object like ZFS with full debugging enabled */
675 /*llog_declare_write_rec(env, next, rec, -1, th);*/
680 EXPORT_SYMBOL(llog_cat_declare_add_rec);
682 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
683 struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
685 struct llog_ctxt *ctxt;
686 struct dt_device *dt;
687 struct thandle *th = NULL;
690 ctxt = cathandle->lgh_ctxt;
692 LASSERT(ctxt->loc_exp);
694 LASSERT(cathandle->lgh_obj != NULL);
695 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
697 th = dt_trans_create(env, dt);
701 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
705 rc = dt_trans_start_local(env, dt, th);
708 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
710 dt_trans_stop(env, dt, th);
713 EXPORT_SYMBOL(llog_cat_add);
715 /* For each cookie in the cookie array, we clear the log in-use bit and either:
716 * - the log is empty, so mark it free in the catalog header and delete it
717 * - the log is not empty, just write out the log header
719 * The cookies may be in different log files, so we need to get new logs
722 * Assumes caller has already pushed us into the kernel context.
724 int llog_cat_cancel_records(const struct lu_env *env,
725 struct llog_handle *cathandle, int count,
726 struct llog_cookie *cookies)
728 int i, index, rc = 0, failed = 0;
732 for (i = 0; i < count; i++, cookies++) {
733 struct llog_handle *loghandle;
734 struct llog_logid *lgl = &cookies->lgc_lgl;
737 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
739 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
741 cathandle->lgh_ctxt->loc_obd->obd_name,
742 PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
747 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
748 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
749 index = loghandle->u.phd.phd_cookie.lgc_index;
750 rc = llog_cat_cleanup(env, cathandle, loghandle, index);
751 } else if (lrc == -ENOENT) {
752 if (rc == 0) /* ENOENT shouldn't rewrite any error */
754 } else if (lrc < 0) {
758 llog_handle_put(loghandle);
761 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
762 cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
767 EXPORT_SYMBOL(llog_cat_cancel_records);
769 static int llog_cat_process_cb(const struct lu_env *env,
770 struct llog_handle *cat_llh,
771 struct llog_rec_hdr *rec, void *data)
773 struct llog_process_data *d = data;
774 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
775 struct llog_handle *llh;
776 struct llog_log_hdr *hdr;
780 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
781 CERROR("invalid record in catalog\n");
784 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
785 DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
786 rec->lrh_index, PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
788 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
790 CERROR("%s: cannot find handle for llog "DFID": rc = %d\n",
791 cat_llh->lgh_ctxt->loc_obd->obd_name,
792 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
793 if (rc == -ENOENT || rc == -ESTALE) {
794 /* After a server crash, a stub of index
795 * record in catlog could be kept, because
796 * plain log destroy + catlog index record
797 * deletion are not atomic. So we end up with
798 * an index but no actual record. Destroy the
799 * index and move on. */
800 rc = llog_cat_cleanup(env, cat_llh, NULL,
807 /* clean old empty llogs, do not consider current llog in use */
808 /* ignore remote (lgh_obj=NULL) llogs */
810 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
811 hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
812 llh != cat_llh->u.chd.chd_current_log) {
813 rc = llog_destroy(env, llh);
815 CERROR("%s: fail to destroy empty log: rc = %d\n",
816 llh->lgh_ctxt->loc_obd->obd_name, rc);
817 GOTO(out, rc = LLOG_DEL_PLAIN);
820 if (rec->lrh_index < d->lpd_startcat) {
821 /* Skip processing of the logs until startcat */
823 } else if (d->lpd_startidx > 0) {
824 struct llog_process_cat_data cd;
826 cd.lpcd_first_idx = d->lpd_startidx;
827 cd.lpcd_last_idx = 0;
828 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
830 /* Continue processing the next log from idx 0 */
833 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
838 /* The empty plain log was destroyed while processing */
839 if (rc == LLOG_DEL_PLAIN)
840 rc = llog_cat_cleanup(env, cat_llh, llh,
841 llh->u.phd.phd_cookie.lgc_index);
842 llog_handle_put(llh);
847 int llog_cat_process_or_fork(const struct lu_env *env,
848 struct llog_handle *cat_llh, llog_cb_t cat_cb,
849 llog_cb_t cb, void *data, int startcat,
850 int startidx, bool fork)
852 struct llog_process_data d;
853 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
857 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
860 d.lpd_startcat = startcat;
861 d.lpd_startidx = startidx;
863 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
864 llh->llh_count > 1) {
865 struct llog_process_cat_data cd;
867 CWARN("%s: catlog "DFID" crosses index zero\n",
868 cat_llh->lgh_ctxt->loc_obd->obd_name,
869 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
871 cd.lpcd_first_idx = llh->llh_cat_idx;
872 cd.lpcd_last_idx = 0;
873 rc = llog_process_or_fork(env, cat_llh, cat_cb,
878 cd.lpcd_first_idx = 0;
879 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
880 rc = llog_process_or_fork(env, cat_llh, cat_cb,
883 rc = llog_process_or_fork(env, cat_llh, cat_cb,
890 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
891 llog_cb_t cb, void *data, int startcat, int startidx)
893 return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
894 cb, data, startcat, startidx, false);
896 EXPORT_SYMBOL(llog_cat_process);
898 static int llog_cat_size_cb(const struct lu_env *env,
899 struct llog_handle *cat_llh,
900 struct llog_rec_hdr *rec, void *data)
902 struct llog_process_data *d = data;
903 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
904 struct llog_handle *llh;
906 __u64 *cum_size = d->lpd_data;
910 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
911 CERROR("%s: invalid record in catalog, rc = %d\n",
912 cat_llh->lgh_ctxt->loc_obd->obd_name, -EINVAL);
915 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
916 DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
917 rec->lrh_index, PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
919 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
921 CWARN("%s: cannot find handle for llog "DFID": rc = %d\n",
922 cat_llh->lgh_ctxt->loc_obd->obd_name,
923 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
926 size = llog_size(env, llh);
929 CDEBUG(D_INFO, "Add llog entry "DFID" size %llu\n",
930 PFID(&llh->lgh_id.lgl_oi.oi_fid), size);
932 llog_handle_put(llh);
938 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
940 __u64 size = llog_size(env, cat_llh);
942 llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
943 NULL, &size, 0, 0, false);
947 EXPORT_SYMBOL(llog_cat_size);
949 static int llog_cat_reverse_process_cb(const struct lu_env *env,
950 struct llog_handle *cat_llh,
951 struct llog_rec_hdr *rec, void *data)
953 struct llog_process_data *d = data;
954 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
955 struct llog_handle *llh;
956 struct llog_log_hdr *hdr;
959 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
960 CERROR("invalid record in catalog\n");
963 CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
964 DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
965 le32_to_cpu(rec->lrh_index),
966 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
968 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
970 CERROR("%s: cannot find handle for llog "DFID": rc = %d\n",
971 cat_llh->lgh_ctxt->loc_obd->obd_name,
972 PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
973 if (rc == -ENOENT || rc == -ESTALE) {
974 /* After a server crash, a stub of index
975 * record in catlog could be kept, because
976 * plain log destroy + catlog index record
977 * deletion are not atomic. So we end up with
978 * an index but no actual record. Destroy the
979 * index and move on. */
980 rc = llog_cat_cleanup(env, cat_llh, NULL,
987 /* clean old empty llogs, do not consider current llog in use */
989 if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
990 hdr->llh_count == 1 &&
991 llh != cat_llh->u.chd.chd_current_log) {
992 rc = llog_destroy(env, llh);
994 CERROR("%s: fail to destroy empty log: rc = %d\n",
995 llh->lgh_ctxt->loc_obd->obd_name, rc);
996 GOTO(out, rc = LLOG_DEL_PLAIN);
999 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1002 /* The empty plain was destroyed while processing */
1003 if (rc == LLOG_DEL_PLAIN)
1004 rc = llog_cat_cleanup(env, cat_llh, llh,
1005 llh->u.phd.phd_cookie.lgc_index);
1007 llog_handle_put(llh);
1011 int llog_cat_reverse_process(const struct lu_env *env,
1012 struct llog_handle *cat_llh,
1013 llog_cb_t cb, void *data)
1015 struct llog_process_data d;
1016 struct llog_process_cat_data cd;
1017 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1021 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1025 if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1026 llh->llh_count > 1) {
1027 CWARN("%s: catalog "DFID" crosses index zero\n",
1028 cat_llh->lgh_ctxt->loc_obd->obd_name,
1029 PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1031 cd.lpcd_first_idx = 0;
1032 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1033 rc = llog_reverse_process(env, cat_llh,
1034 llog_cat_reverse_process_cb,
1039 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1040 cd.lpcd_last_idx = 0;
1041 rc = llog_reverse_process(env, cat_llh,
1042 llog_cat_reverse_process_cb,
1045 rc = llog_reverse_process(env, cat_llh,
1046 llog_cat_reverse_process_cb,
1052 EXPORT_SYMBOL(llog_cat_reverse_process);
1054 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1056 struct llog_log_hdr *llh = cathandle->lgh_hdr;
1061 bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1063 * The llh_cat_idx equals to the first used index minus 1
1064 * so if we canceled the first index then llh_cat_idx
1067 if (llh->llh_cat_idx == (idx - 1)) {
1068 llh->llh_cat_idx = idx;
1070 while (idx != cathandle->lgh_last_idx) {
1071 idx = (idx + 1) % bitmap_size;
1072 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1073 /* update llh_cat_idx for each unset bit,
1074 * expecting the next one is set */
1075 llh->llh_cat_idx = idx;
1076 } else if (idx == 0) {
1077 /* skip header bit */
1078 llh->llh_cat_idx = 0;
1081 /* the first index is found */
1086 CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
1087 PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1088 llh->llh_cat_idx, cathandle->lgh_last_idx);
1094 /* Cleanup deleted plain llog traces from catalog */
1095 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1096 struct llog_handle *loghandle, int index)
1101 if (loghandle != NULL) {
1102 /* remove destroyed llog from catalog list and
1103 * chd_current_log variable */
1104 down_write(&cathandle->lgh_lock);
1105 if (cathandle->u.chd.chd_current_log == loghandle)
1106 cathandle->u.chd.chd_current_log = NULL;
1107 list_del_init(&loghandle->u.phd.phd_entry);
1108 up_write(&cathandle->lgh_lock);
1109 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1110 /* llog was opened and keep in a list, close it now */
1111 llog_close(env, loghandle);
1114 /* do not attempt to cleanup on-disk llog if on client side */
1115 if (cathandle->lgh_obj == NULL)
1118 /* remove plain llog entry from catalog by index */
1119 llog_cat_set_first_idx(cathandle, index);
1120 rc = llog_cancel_rec(env, cathandle, index);
1122 CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
1123 index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));