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>
52 #include <lustre_log.h>
54 #include "llog_internal.h"
56 /* Create a new log handle and add it to the open list.
57 * This log handle will be closed when all of the records in it are removed.
59 * Assumes caller has already pushed us into the kernel context and is locking.
61 static struct llog_handle *llog_cat_new_log(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_create(cathandle->lgh_ctxt, &loghandle, NULL, NULL);
87 rc = llog_init_handle(loghandle,
88 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
89 &cathandle->lgh_hdr->llh_tgtuuid);
91 GOTO(out_destroy, rc);
96 cfs_spin_lock(&loghandle->lgh_hdr_lock);
98 if (ext2_set_bit(index, llh->llh_bitmap)) {
99 CERROR("argh, index %u already set in log bitmap?\n",
101 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
102 LBUG(); /* should never happen */
104 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
106 cathandle->lgh_last_idx = index;
107 llh->llh_tail.lrt_index = index;
109 CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
110 LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
111 index, cathandle->lgh_id.lgl_oid);
112 /* build the record for this log in the catalog */
113 rec.lid_hdr.lrh_len = sizeof(rec);
114 rec.lid_hdr.lrh_index = index;
115 rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
116 rec.lid_id = loghandle->lgh_id;
117 rec.lid_tail.lrt_len = sizeof(rec);
118 rec.lid_tail.lrt_index = index;
120 /* update the catalog: header and record */
121 rc = llog_write_rec(cathandle, &rec.lid_hdr,
122 &loghandle->u.phd.phd_cookie, 1, NULL, index);
124 GOTO(out_destroy, rc);
127 loghandle->lgh_hdr->llh_cat_idx = index;
128 cathandle->u.chd.chd_current_log = loghandle;
129 LASSERT(cfs_list_empty(&loghandle->u.phd.phd_entry));
130 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
131 &cathandle->u.chd.chd_head);
135 llog_destroy(loghandle);
140 /* Open an existent log handle and add it to the open list.
141 * This log handle will be closed when all of the records in it are removed.
143 * Assumes caller has already pushed us into the kernel context and is locking.
144 * We return a lock on the handle to ensure nobody yanks it from us.
146 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
147 struct llog_logid *logid)
149 struct llog_handle *loghandle;
153 if (cathandle == NULL)
156 cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
158 struct llog_logid *cgl = &loghandle->lgh_id;
159 if (cgl->lgl_oid == logid->lgl_oid) {
160 if (cgl->lgl_ogen != logid->lgl_ogen) {
161 CERROR("log "LPX64" generation %x != %x\n",
162 logid->lgl_oid, cgl->lgl_ogen,
166 loghandle->u.phd.phd_cat_handle = cathandle;
171 rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL);
173 CERROR("error opening log id "LPX64":%x: rc %d\n",
174 logid->lgl_oid, logid->lgl_ogen, rc);
176 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
178 cfs_list_add(&loghandle->u.phd.phd_entry,
179 &cathandle->u.chd.chd_head);
183 loghandle->u.phd.phd_cat_handle = cathandle;
184 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
185 loghandle->u.phd.phd_cookie.lgc_index =
186 loghandle->lgh_hdr->llh_cat_idx;
194 int llog_cat_put(struct llog_handle *cathandle)
196 struct llog_handle *loghandle, *n;
200 cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
202 int err = llog_close(loghandle);
204 CERROR("error closing loghandle\n");
206 rc = llog_close(cathandle);
209 EXPORT_SYMBOL(llog_cat_put);
212 * lockdep markers for nested struct llog_handle::lgh_lock locking.
219 /** Return the currently active log handle. If the current log handle doesn't
220 * have enough space left for the current record, start a new one.
222 * If reclen is 0, we only want to know what the currently active log is,
223 * otherwise we get a lock on this log so nobody can steal our space.
225 * Assumes caller has already pushed us into the kernel context and is locking.
227 * NOTE: loghandle is write-locked upon successful return
229 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
232 struct llog_handle *loghandle = NULL;
235 cfs_down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
236 loghandle = cathandle->u.chd.chd_current_log;
238 struct llog_log_hdr *llh = loghandle->lgh_hdr;
239 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
240 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
241 cfs_up_read(&cathandle->lgh_lock);
244 cfs_up_write(&loghandle->lgh_lock);
249 cfs_down_write(&loghandle->lgh_lock);
250 cfs_up_read(&cathandle->lgh_lock);
253 cfs_up_read(&cathandle->lgh_lock);
255 /* time to create new log */
257 /* first, we have to make sure the state hasn't changed */
258 cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
259 loghandle = cathandle->u.chd.chd_current_log;
261 struct llog_log_hdr *llh = loghandle->lgh_hdr;
262 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
263 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
264 cfs_up_write(&cathandle->lgh_lock);
267 cfs_up_write(&loghandle->lgh_lock);
271 CDEBUG(D_INODE, "creating new log\n");
272 loghandle = llog_cat_new_log(cathandle);
273 if (!IS_ERR(loghandle))
274 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
275 cfs_up_write(&cathandle->lgh_lock);
279 /* Add a single record to the recovery log(s) using a catalog
280 * Returns as llog_write_record
282 * Assumes caller has already pushed us into the kernel context.
284 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
285 struct llog_cookie *reccookie, void *buf)
287 struct llog_handle *loghandle;
291 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
292 loghandle = llog_cat_current_log(cathandle, 1);
293 if (IS_ERR(loghandle))
294 RETURN(PTR_ERR(loghandle));
295 /* loghandle is already locked by llog_cat_current_log() for us */
296 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
298 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
299 cfs_up_write(&loghandle->lgh_lock);
301 /* to create a new plain log */
302 loghandle = llog_cat_current_log(cathandle, 1);
303 if (IS_ERR(loghandle))
304 RETURN(PTR_ERR(loghandle));
305 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
306 cfs_up_write(&loghandle->lgh_lock);
311 EXPORT_SYMBOL(llog_cat_add_rec);
313 /* For each cookie in the cookie array, we clear the log in-use bit and either:
314 * - the log is empty, so mark it free in the catalog header and delete it
315 * - the log is not empty, just write out the log header
317 * The cookies may be in different log files, so we need to get new logs
320 * Assumes caller has already pushed us into the kernel context.
322 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
323 struct llog_cookie *cookies)
325 int i, index, rc = 0;
328 cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
329 for (i = 0; i < count; i++, cookies++) {
330 struct llog_handle *loghandle;
331 struct llog_logid *lgl = &cookies->lgc_lgl;
333 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
335 CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
339 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
340 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
341 cfs_up_write(&loghandle->lgh_lock);
343 if (rc == 1) { /* log has been destroyed */
344 index = loghandle->u.phd.phd_cookie.lgc_index;
345 if (cathandle->u.chd.chd_current_log == loghandle)
346 cathandle->u.chd.chd_current_log = NULL;
347 llog_free_handle(loghandle);
350 llog_cat_set_first_idx(cathandle, index);
351 rc = llog_cancel_rec(cathandle, index);
353 CDEBUG(D_RPCTRACE,"cancel plain log at index %u"
354 " of catalog "LPX64"\n",
355 index, cathandle->lgh_id.lgl_oid);
358 cfs_up_write(&cathandle->lgh_lock);
362 EXPORT_SYMBOL(llog_cat_cancel_records);
364 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
365 struct llog_rec_hdr *rec, void *data)
367 struct llog_process_data *d = data;
368 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
369 struct llog_handle *llh;
373 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
374 CERROR("invalid record in catalog\n");
377 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
378 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
379 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
381 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
383 CERROR("Cannot find handle for log "LPX64"\n",
384 lir->lid_id.lgl_oid);
388 if (rec->lrh_index < d->lpd_startcat)
389 /* Skip processing of the logs until startcat */
392 if (d->lpd_startidx > 0) {
393 struct llog_process_cat_data cd;
395 cd.lpcd_first_idx = d->lpd_startidx;
396 cd.lpcd_last_idx = 0;
397 rc = llog_process(env, llh, d->lpd_cb, d->lpd_data, &cd);
398 /* Continue processing the next log from idx 0 */
401 rc = llog_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
407 int llog_cat_process_or_fork(const struct lu_env *env,
408 struct llog_handle *cat_llh,
409 llog_cb_t cb, void *data, int startcat,
410 int startidx, bool fork)
412 struct llog_process_data d;
413 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
417 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
420 d.lpd_startcat = startcat;
421 d.lpd_startidx = startidx;
423 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
424 struct llog_process_cat_data cd;
426 CWARN("catlog "LPX64" crosses index zero\n",
427 cat_llh->lgh_id.lgl_oid);
429 cd.lpcd_first_idx = llh->llh_cat_idx;
430 cd.lpcd_last_idx = 0;
431 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
436 cd.lpcd_first_idx = 0;
437 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
438 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
441 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
447 EXPORT_SYMBOL(llog_cat_process_or_fork);
449 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
450 llog_cb_t cb, void *data, int startcat, int startidx)
452 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
455 EXPORT_SYMBOL(llog_cat_process);
458 int llog_cat_process_thread(void *data)
460 struct llog_process_cat_args *args = data;
461 struct llog_ctxt *ctxt = args->lpca_ctxt;
462 struct llog_handle *llh = NULL;
463 llog_cb_t cb = args->lpca_cb;
464 struct llog_thread_info *lgi;
469 cfs_daemonize_ctxt("ll_log_process");
471 rc = lu_env_init(&env, LCT_LOCAL);
474 lgi = llog_info(&env);
477 lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
478 rc = llog_create(ctxt, &llh, &lgi->lgi_logid, NULL);
480 CERROR("llog_create() failed %d\n", rc);
483 rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
485 CERROR("llog_init_handle failed %d\n", rc);
486 GOTO(release_llh, rc);
490 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
491 if (rc != LLOG_PROC_BREAK && rc != 0)
492 CERROR("llog_cat_process() failed %d\n", rc);
493 cb(&env, llh, NULL, NULL);
495 CWARN("No callback function for recovery\n");
499 * Make sure that all cached data is sent.
501 llog_sync(ctxt, NULL, 0);
502 GOTO(release_llh, rc);
504 rc = llog_cat_put(llh);
506 CERROR("llog_cat_put() failed %d\n", rc);
514 EXPORT_SYMBOL(llog_cat_process_thread);
517 static int llog_cat_reverse_process_cb(const struct lu_env *env,
518 struct llog_handle *cat_llh,
519 struct llog_rec_hdr *rec, void *data)
521 struct llog_process_data *d = data;
522 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
523 struct llog_handle *llh;
526 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
527 CERROR("invalid record in catalog\n");
530 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
531 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
532 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
534 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
536 CERROR("Cannot find handle for log "LPX64"\n",
537 lir->lid_id.lgl_oid);
541 rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
545 int llog_cat_reverse_process(const struct lu_env *env,
546 struct llog_handle *cat_llh,
547 llog_cb_t cb, void *data)
549 struct llog_process_data d;
550 struct llog_process_cat_data cd;
551 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
555 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
559 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
560 CWARN("catalog "LPX64" crosses index zero\n",
561 cat_llh->lgh_id.lgl_oid);
563 cd.lpcd_first_idx = 0;
564 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
565 rc = llog_reverse_process(env, cat_llh,
566 llog_cat_reverse_process_cb,
571 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
572 cd.lpcd_last_idx = 0;
573 rc = llog_reverse_process(env, cat_llh,
574 llog_cat_reverse_process_cb,
577 rc = llog_reverse_process(env, cat_llh,
578 llog_cat_reverse_process_cb,
584 EXPORT_SYMBOL(llog_cat_reverse_process);
586 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
588 struct llog_log_hdr *llh = cathandle->lgh_hdr;
589 int i, bitmap_size, idx;
592 bitmap_size = LLOG_BITMAP_SIZE(llh);
593 if (llh->llh_cat_idx == (index - 1)) {
594 idx = llh->llh_cat_idx + 1;
595 llh->llh_cat_idx = idx;
596 if (idx == cathandle->lgh_last_idx)
598 for (i = (index + 1) % bitmap_size;
599 i != cathandle->lgh_last_idx;
600 i = (i + 1) % bitmap_size) {
601 if (!ext2_test_bit(i, llh->llh_bitmap)) {
602 idx = llh->llh_cat_idx + 1;
603 llh->llh_cat_idx = idx;
605 llh->llh_cat_idx = 0;
611 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
612 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
618 /* callback func for llog_process in llog_obd_origin_setup */
619 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
620 struct llog_rec_hdr *rec, void *data)
622 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
623 struct llog_handle *loghandle;
624 struct llog_log_hdr *llh;
629 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
630 CERROR("%s: invalid record in catalog\n",
631 loghandle->lgh_ctxt->loc_obd->obd_name);
634 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
635 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
636 rec->lrh_index, cathandle->lgh_id.lgl_oid);
638 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
640 CERROR("%s: cannot find handle for llog "LPX64"\n",
641 loghandle->lgh_ctxt->loc_obd->obd_name,
642 lir->lid_id.lgl_oid);
644 index = rec->lrh_index;
650 llh = loghandle->lgh_hdr;
651 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
652 (llh->llh_count == 1)) {
653 rc = llog_destroy(loghandle);
655 CERROR("%s: fail to destroy empty log: rc = %d\n",
656 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
658 index = loghandle->u.phd.phd_cookie.lgc_index;
659 llog_free_handle(loghandle);
663 llog_cat_set_first_idx(cathandle, index);
664 rc = llog_cancel_rec(cathandle, index);
667 "cancel log "LPX64":%x at index %u of catalog "
668 LPX64"\n", lir->lid_id.lgl_oid,
669 lir->lid_id.lgl_ogen, rec->lrh_index,
670 cathandle->lgh_id.lgl_oid);