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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdclass/llog_cat.c
36 * OST<->MDS recovery logging infrastructure.
38 * Invariants in implementation:
39 * - we do not share logs among different OST<->MDS connections, so that
40 * if an OST or MDS fails it need only look at log(s) relevant to itself
42 * Author: Andreas Dilger <adilger@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_LOG
48 #include <liblustre.h>
51 #include <obd_class.h>
53 #include "llog_internal.h"
55 /* Create a new log handle and add it to the open list.
56 * This log handle will be closed when all of the records in it are removed.
58 * Assumes caller has already pushed us into the kernel context and is locking.
60 static struct llog_handle *llog_cat_new_log(const struct lu_env *env,
61 struct llog_handle *cathandle)
63 struct llog_handle *loghandle;
64 struct llog_log_hdr *llh;
65 struct llog_logid_rec rec = { { 0 }, };
66 int rc, index, bitmap_size;
69 llh = cathandle->lgh_hdr;
70 bitmap_size = LLOG_BITMAP_SIZE(llh);
72 index = (cathandle->lgh_last_idx + 1) % bitmap_size;
74 /* maximum number of available slots in catlog is bitmap_size - 2 */
75 if (llh->llh_cat_idx == index) {
76 CERROR("no free catalog slots for log...\n");
77 RETURN(ERR_PTR(-ENOSPC));
80 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
81 RETURN(ERR_PTR(-ENOSPC));
83 rc = llog_open_create(env, cathandle->lgh_ctxt, &loghandle, NULL,
88 rc = llog_init_handle(env, loghandle,
89 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
90 &cathandle->lgh_hdr->llh_tgtuuid);
92 GOTO(out_destroy, rc);
97 cfs_spin_lock(&loghandle->lgh_hdr_lock);
99 if (ext2_set_bit(index, llh->llh_bitmap)) {
100 CERROR("argh, index %u already set in log bitmap?\n",
102 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
103 LBUG(); /* should never happen */
105 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
107 cathandle->lgh_last_idx = index;
108 llh->llh_tail.lrt_index = index;
110 CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
111 LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
112 index, cathandle->lgh_id.lgl_oid);
113 /* build the record for this log in the catalog */
114 rec.lid_hdr.lrh_len = sizeof(rec);
115 rec.lid_hdr.lrh_index = index;
116 rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
117 rec.lid_id = loghandle->lgh_id;
118 rec.lid_tail.lrt_len = sizeof(rec);
119 rec.lid_tail.lrt_index = index;
121 /* update the catalog: header and record */
122 rc = llog_write_rec(env, cathandle, &rec.lid_hdr,
123 &loghandle->u.phd.phd_cookie, 1, NULL, index);
125 GOTO(out_destroy, rc);
128 loghandle->lgh_hdr->llh_cat_idx = index;
129 cathandle->u.chd.chd_current_log = loghandle;
130 LASSERT(cfs_list_empty(&loghandle->u.phd.phd_entry));
131 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
132 &cathandle->u.chd.chd_head);
136 llog_destroy(env, loghandle);
141 /* Open an existent log handle and add it to the open list.
142 * This log handle will be closed when all of the records in it are removed.
144 * Assumes caller has already pushed us into the kernel context and is locking.
145 * We return a lock on the handle to ensure nobody yanks it from us.
147 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
148 struct llog_handle **res, struct llog_logid *logid)
150 struct llog_handle *loghandle;
155 if (cathandle == NULL)
158 cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
160 struct llog_logid *cgl = &loghandle->lgh_id;
162 if (cgl->lgl_oid == logid->lgl_oid) {
163 if (cgl->lgl_ogen != logid->lgl_ogen) {
164 CERROR("%s: log "LPX64" generation %x != %x\n",
165 loghandle->lgh_ctxt->loc_obd->obd_name,
166 logid->lgl_oid, cgl->lgl_ogen,
170 loghandle->u.phd.phd_cat_handle = cathandle;
175 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
178 CERROR("%s: error opening log id "LPX64":%x: rc = %d\n",
179 cathandle->lgh_ctxt->loc_obd->obd_name,
180 logid->lgl_oid, logid->lgl_ogen, rc);
184 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
186 llog_close(env, loghandle);
190 cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
192 loghandle->u.phd.phd_cat_handle = cathandle;
193 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
194 loghandle->u.phd.phd_cookie.lgc_index = loghandle->lgh_hdr->llh_cat_idx;
201 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
203 struct llog_handle *loghandle, *n;
208 cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
210 struct llog_log_hdr *llh = loghandle->lgh_hdr;
213 /* unlink open-not-created llogs */
214 cfs_list_del_init(&loghandle->u.phd.phd_entry);
215 llh = loghandle->lgh_hdr;
216 if (loghandle->lgh_obj != NULL && llh != NULL &&
217 (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
218 (llh->llh_count == 1)) {
219 rc = llog_destroy(env, loghandle);
221 CERROR("%s: failure destroying log during "
222 "cleanup: rc = %d\n",
223 loghandle->lgh_ctxt->loc_obd->obd_name,
226 index = loghandle->u.phd.phd_cookie.lgc_index;
229 llog_cat_set_first_idx(cathandle, index);
230 rc = llog_cancel_rec(env, cathandle, index);
233 "cancel plain log at index %u of "
235 index, cathandle->lgh_id.lgl_oid);
237 llog_close(env, loghandle);
239 /* if handle was stored in ctxt, remove it too */
240 if (cathandle->lgh_ctxt->loc_handle == cathandle)
241 cathandle->lgh_ctxt->loc_handle = NULL;
242 rc = llog_close(env, cathandle);
245 EXPORT_SYMBOL(llog_cat_close);
248 * lockdep markers for nested struct llog_handle::lgh_lock locking.
255 /** Return the currently active log handle. If the current log handle doesn't
256 * have enough space left for the current record, start a new one.
258 * If reclen is 0, we only want to know what the currently active log is,
259 * otherwise we get a lock on this log so nobody can steal our space.
261 * Assumes caller has already pushed us into the kernel context and is locking.
263 * NOTE: loghandle is write-locked upon successful return
265 static struct llog_handle *llog_cat_current_log(const struct lu_env *env,
266 struct llog_handle *cathandle,
269 struct llog_handle *loghandle = NULL;
272 cfs_down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
273 loghandle = cathandle->u.chd.chd_current_log;
275 struct llog_log_hdr *llh = loghandle->lgh_hdr;
277 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
278 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
279 cfs_up_read(&cathandle->lgh_lock);
282 cfs_up_write(&loghandle->lgh_lock);
287 cfs_down_write(&loghandle->lgh_lock);
288 cfs_up_read(&cathandle->lgh_lock);
291 cfs_up_read(&cathandle->lgh_lock);
293 /* time to create new log */
295 /* first, we have to make sure the state hasn't changed */
296 cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
297 loghandle = cathandle->u.chd.chd_current_log;
299 struct llog_log_hdr *llh = loghandle->lgh_hdr;
301 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
302 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
303 cfs_up_write(&cathandle->lgh_lock);
306 cfs_up_write(&loghandle->lgh_lock);
310 CDEBUG(D_INODE, "creating new log\n");
311 loghandle = llog_cat_new_log(env, cathandle);
312 if (!IS_ERR(loghandle))
313 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
314 cfs_up_write(&cathandle->lgh_lock);
318 /* Add a single record to the recovery log(s) using a catalog
319 * Returns as llog_write_record
321 * Assumes caller has already pushed us into the kernel context.
323 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
324 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
327 struct llog_handle *loghandle;
331 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
332 loghandle = llog_cat_current_log(env, cathandle, 1);
333 if (IS_ERR(loghandle))
334 RETURN(PTR_ERR(loghandle));
335 /* loghandle is already locked by llog_cat_current_log() for us */
336 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1);
338 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
339 cfs_up_write(&loghandle->lgh_lock);
341 /* to create a new plain log */
342 loghandle = llog_cat_current_log(env, cathandle, 1);
343 if (IS_ERR(loghandle))
344 RETURN(PTR_ERR(loghandle));
345 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
347 cfs_up_write(&loghandle->lgh_lock);
352 EXPORT_SYMBOL(llog_cat_add_rec);
354 /* For each cookie in the cookie array, we clear the log in-use bit and either:
355 * - the log is empty, so mark it free in the catalog header and delete it
356 * - the log is not empty, just write out the log header
358 * The cookies may be in different log files, so we need to get new logs
361 * Assumes caller has already pushed us into the kernel context.
363 int llog_cat_cancel_records(const struct lu_env *env,
364 struct llog_handle *cathandle, int count,
365 struct llog_cookie *cookies)
367 int i, index, rc = 0;
371 cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
372 for (i = 0; i < count; i++, cookies++) {
373 struct llog_handle *loghandle;
374 struct llog_logid *lgl = &cookies->lgc_lgl;
376 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
378 CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
382 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
383 rc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
384 cfs_up_write(&loghandle->lgh_lock);
386 if (rc == 1) { /* log has been destroyed */
387 index = loghandle->u.phd.phd_cookie.lgc_index;
388 if (cathandle->u.chd.chd_current_log == loghandle)
389 cathandle->u.chd.chd_current_log = NULL;
390 llog_close(env, loghandle);
393 llog_cat_set_first_idx(cathandle, index);
394 rc = llog_cancel_rec(env, cathandle, index);
396 CDEBUG(D_RPCTRACE,"cancel plain log at index %u"
397 " of catalog "LPX64"\n",
398 index, cathandle->lgh_id.lgl_oid);
401 cfs_up_write(&cathandle->lgh_lock);
405 EXPORT_SYMBOL(llog_cat_cancel_records);
407 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
408 struct llog_rec_hdr *rec, void *data)
410 struct llog_process_data *d = data;
411 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
412 struct llog_handle *llh;
416 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
417 CERROR("invalid record in catalog\n");
420 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
421 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
422 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
424 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
426 CERROR("Cannot find handle for log "LPX64"\n",
427 lir->lid_id.lgl_oid);
431 if (rec->lrh_index < d->lpd_startcat)
432 /* Skip processing of the logs until startcat */
435 if (d->lpd_startidx > 0) {
436 struct llog_process_cat_data cd;
438 cd.lpcd_first_idx = d->lpd_startidx;
439 cd.lpcd_last_idx = 0;
440 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
442 /* Continue processing the next log from idx 0 */
445 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
452 int llog_cat_process_or_fork(const struct lu_env *env,
453 struct llog_handle *cat_llh,
454 llog_cb_t cb, void *data, int startcat,
455 int startidx, bool fork)
457 struct llog_process_data d;
458 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
462 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
465 d.lpd_startcat = startcat;
466 d.lpd_startidx = startidx;
468 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
469 struct llog_process_cat_data cd;
471 CWARN("catlog "LPX64" crosses index zero\n",
472 cat_llh->lgh_id.lgl_oid);
474 cd.lpcd_first_idx = llh->llh_cat_idx;
475 cd.lpcd_last_idx = 0;
476 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
481 cd.lpcd_first_idx = 0;
482 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
483 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
486 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
492 EXPORT_SYMBOL(llog_cat_process_or_fork);
494 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
495 llog_cb_t cb, void *data, int startcat, int startidx)
497 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
500 EXPORT_SYMBOL(llog_cat_process);
503 int llog_cat_process_thread(void *data)
505 struct llog_process_cat_args *args = data;
506 struct llog_ctxt *ctxt = args->lpca_ctxt;
507 struct llog_handle *llh = NULL;
508 llog_cb_t cb = args->lpca_cb;
509 struct llog_thread_info *lgi;
514 cfs_daemonize_ctxt("ll_log_process");
516 rc = lu_env_init(&env, LCT_LOCAL);
519 lgi = llog_info(&env);
522 lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
523 rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
526 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
527 ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
528 lgi->lgi_logid.lgl_ogen, rc);
531 rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
533 CERROR("%s: llog_init_handle failed: rc = %d\n",
534 llh->lgh_ctxt->loc_obd->obd_name, rc);
535 GOTO(release_llh, rc);
539 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
540 if (rc != LLOG_PROC_BREAK && rc != 0)
541 CERROR("%s: llog_cat_process() failed: rc = %d\n",
542 llh->lgh_ctxt->loc_obd->obd_name, rc);
543 cb(&env, llh, NULL, NULL);
545 CWARN("No callback function for recovery\n");
549 * Make sure that all cached data is sent.
551 llog_sync(ctxt, NULL, 0);
552 GOTO(release_llh, rc);
554 rc = llog_cat_close(&env, llh);
556 CERROR("%s: llog_cat_close() failed: rc = %d\n",
557 llh->lgh_ctxt->loc_obd->obd_name, rc);
565 EXPORT_SYMBOL(llog_cat_process_thread);
568 static int llog_cat_reverse_process_cb(const struct lu_env *env,
569 struct llog_handle *cat_llh,
570 struct llog_rec_hdr *rec, void *data)
572 struct llog_process_data *d = data;
573 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
574 struct llog_handle *llh;
577 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
578 CERROR("invalid record in catalog\n");
581 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
582 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
583 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
585 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
587 CERROR("Cannot find handle for log "LPX64"\n",
588 lir->lid_id.lgl_oid);
592 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
596 int llog_cat_reverse_process(const struct lu_env *env,
597 struct llog_handle *cat_llh,
598 llog_cb_t cb, void *data)
600 struct llog_process_data d;
601 struct llog_process_cat_data cd;
602 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
606 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
610 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
611 CWARN("catalog "LPX64" crosses index zero\n",
612 cat_llh->lgh_id.lgl_oid);
614 cd.lpcd_first_idx = 0;
615 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
616 rc = llog_reverse_process(env, cat_llh,
617 llog_cat_reverse_process_cb,
622 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
623 cd.lpcd_last_idx = 0;
624 rc = llog_reverse_process(env, cat_llh,
625 llog_cat_reverse_process_cb,
628 rc = llog_reverse_process(env, cat_llh,
629 llog_cat_reverse_process_cb,
635 EXPORT_SYMBOL(llog_cat_reverse_process);
637 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
639 struct llog_log_hdr *llh = cathandle->lgh_hdr;
640 int i, bitmap_size, idx;
643 bitmap_size = LLOG_BITMAP_SIZE(llh);
644 if (llh->llh_cat_idx == (index - 1)) {
645 idx = llh->llh_cat_idx + 1;
646 llh->llh_cat_idx = idx;
647 if (idx == cathandle->lgh_last_idx)
649 for (i = (index + 1) % bitmap_size;
650 i != cathandle->lgh_last_idx;
651 i = (i + 1) % bitmap_size) {
652 if (!ext2_test_bit(i, llh->llh_bitmap)) {
653 idx = llh->llh_cat_idx + 1;
654 llh->llh_cat_idx = idx;
656 llh->llh_cat_idx = 0;
662 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
663 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
669 /* callback func for llog_process in llog_obd_origin_setup */
670 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
671 struct llog_rec_hdr *rec, void *data)
673 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
674 struct llog_handle *loghandle;
675 struct llog_log_hdr *llh;
680 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
681 CERROR("%s: invalid record in catalog\n",
682 loghandle->lgh_ctxt->loc_obd->obd_name);
685 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
686 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
687 rec->lrh_index, cathandle->lgh_id.lgl_oid);
689 rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
691 CERROR("%s: cannot find handle for llog "LPX64"\n",
692 loghandle->lgh_ctxt->loc_obd->obd_name,
693 lir->lid_id.lgl_oid);
695 index = rec->lrh_index;
701 llh = loghandle->lgh_hdr;
702 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
703 (llh->llh_count == 1)) {
704 rc = llog_destroy(env, loghandle);
706 CERROR("%s: fail to destroy empty log: rc = %d\n",
707 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
709 index = loghandle->u.phd.phd_cookie.lgc_index;
710 llog_close(env, loghandle);
714 llog_cat_set_first_idx(cathandle, index);
715 rc = llog_cancel_rec(env, cathandle, index);
718 "cancel log "LPX64":%x at index %u of catalog "
719 LPX64"\n", lir->lid_id.lgl_oid,
720 lir->lid_id.lgl_ogen, rec->lrh_index,
721 cathandle->lgh_id.lgl_oid);