1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * OST<->MDS recovery logging infrastructure.
24 * Invariants in implementation:
25 * - we do not share logs among different OST<->MDS connections, so that
26 * if an OST or MDS fails it need only look at log(s) relevant to itself
29 #define DEBUG_SUBSYSTEM S_LOG
38 #include <liblustre.h>
41 #include <linux/lustre_log.h>
42 #include <portals/list.h>
44 /* Create a new log handle and add it to the open list.
45 * This log handle will be closed when all of the records in it are removed.
47 * Assumes caller has already pushed us into the kernel context and is locking.
49 static struct llog_handle *llog_cat_new_log(struct llog_handle *cathandle,
50 struct llog_cookie *logcookie)
52 struct llog_handle *loghandle;
53 struct llog_log_hdr *llh;
54 struct llog_logid_rec rec;
55 int rc, index, bitmap_size;
58 llh = cathandle->lgh_hdr;
59 bitmap_size = sizeof(llh->llh_bitmap) * 8;
61 index = (cathandle->lgh_last_idx + 1) % bitmap_size;
63 /* maximum number of available slots in catalog is bitmap_size - 2 */
64 if (llh->llh_cat_idx == cpu_to_le32(index)) {
65 CERROR("no free catalog slots for log...\n");
66 RETURN(ERR_PTR(-ENOSPC));
70 if (ext2_set_bit(index, llh->llh_bitmap)) {
71 CERROR("argh, index %u already set in log bitmap?\n",
73 LBUG(); /* should never happen */
75 cathandle->lgh_last_idx = index;
76 llh->llh_count = cpu_to_le32(le32_to_cpu(llh->llh_count) + 1);
77 llh->llh_tail.lrt_index = cpu_to_le32(index);
80 if (logcookie && llog_cookie_get_flags(logcookie) & LLOG_COOKIE_REPLAY_NEW)
81 rc = llog_open(cathandle->lgh_ctxt, &loghandle,
82 &logcookie->lgc_lgl, NULL, OBD_LLOG_FL_CREATE);
84 rc = llog_open(cathandle->lgh_ctxt, &loghandle, NULL, NULL,
87 CERROR("cannot create new log, error = %d\n", rc);
91 rc = llog_init_handle(loghandle,
92 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
93 &cathandle->lgh_hdr->llh_tgtuuid);
95 GOTO(out_destroy, rc);
97 CDEBUG(D_HA, "new recovery log "LPX64":%x for index %u of catalog "
98 LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
99 index, cathandle->lgh_id.lgl_oid);
100 /* build the record for this log in the catalog */
101 rec.lid_hdr.lrh_len = cpu_to_le32(sizeof(rec));
102 rec.lid_hdr.lrh_index = cpu_to_le32(index);
103 rec.lid_hdr.lrh_type = cpu_to_le32(LLOG_LOGID_MAGIC);
104 rec.lid_id = loghandle->lgh_id;
105 rec.lid_tail.lrt_len = cpu_to_le32(sizeof(rec));
106 rec.lid_tail.lrt_index = cpu_to_le32(index);
108 /* update the catalog: header and record */
109 rc = llog_write_rec(cathandle, &rec.lid_hdr,
110 &loghandle->u.phd.phd_cookie, 1, NULL, index);
112 GOTO(out_destroy, rc);
114 loghandle->lgh_hdr->llh_cat_idx = cpu_to_le32(index);
115 cathandle->u.chd.chd_current_log = loghandle;
116 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
117 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
121 llog_destroy(loghandle);
122 else if (logcookie) {
123 if (llog_cookie_get_flags(logcookie) & LLOG_COOKIE_REPLAY_NEW)
124 LASSERT(EQ_LOGID(loghandle->lgh_id, logcookie->lgc_lgl));
126 llog_cookie_set_flags(logcookie, LLOG_COOKIE_REPLAY_NEW);
132 /* Open an existent log handle and add it to the open list.
133 * This log handle will be closed when all of the records in it are removed.
135 * Assumes caller has already pushed us into the kernel context and is locking.
136 * We return a lock on the handle to ensure nobody yanks it from us.
138 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
139 struct llog_logid *logid)
141 struct llog_handle *loghandle;
145 if (cathandle == NULL)
148 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
150 struct llog_logid *cgl = &loghandle->lgh_id;
151 if (cgl->lgl_oid == logid->lgl_oid) {
152 if (cgl->lgl_ogen != logid->lgl_ogen) {
153 CERROR("log "LPX64" generation %x != %x\n",
154 logid->lgl_oid, cgl->lgl_ogen,
158 loghandle->u.phd.phd_cat_handle = cathandle;
163 rc = llog_open(cathandle->lgh_ctxt, &loghandle, logid, NULL, 0);
165 CERROR("error opening log id "LPX64":%x: rc %d\n",
166 logid->lgl_oid, logid->lgl_ogen, rc);
168 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
170 list_add(&loghandle->u.phd.phd_entry,
171 &cathandle->u.chd.chd_head);
175 loghandle->u.phd.phd_cat_handle = cathandle;
176 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
177 loghandle->u.phd.phd_cookie.lgc_index =
178 le32_to_cpu(loghandle->lgh_hdr->llh_cat_idx);
185 EXPORT_SYMBOL(llog_cat_id2handle);
187 int llog_cat_put(struct llog_handle *cathandle)
189 struct llog_handle *loghandle, *n;
193 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
195 int err = llog_close(loghandle);
197 CERROR("error closing loghandle\n");
199 rc = llog_close(cathandle);
202 EXPORT_SYMBOL(llog_cat_put);
204 /* Return the currently active log handle. If the current log handle doesn't
205 * have enough space left for the current record, start a new one.
207 * If reclen is 0, we only want to know what the currently active log is,
208 * otherwise we get a lock on this log so nobody can steal our space.
210 * Assumes caller has already pushed us into the kernel context and is locking.
212 * NOTE: loghandle is write-locked upon successful return
214 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
216 struct llog_cookie *logcookie,
217 struct rw_semaphore **lock)
219 struct llog_handle *loghandle = NULL;
222 down_read(&cathandle->lgh_lock);
223 loghandle = cathandle->u.chd.chd_current_log;
225 struct llog_log_hdr *llh = loghandle->lgh_hdr;
226 if (loghandle->lgh_last_idx < (sizeof(llh->llh_bitmap)*8) - 1 &&
228 !(llog_cookie_get_flags(logcookie) & LLOG_COOKIE_REPLAY) ||
229 EQ_LOGID(loghandle->lgh_id, logcookie->lgc_lgl))) {
230 down_write(&loghandle->lgh_lock);
231 up_read(&cathandle->lgh_lock);
236 LASSERT(!logcookie ||
237 !(llog_cookie_get_flags(logcookie) & LLOG_COOKIE_REPLAY) ||
238 llog_cookie_get_flags(logcookie) & LLOG_COOKIE_REPLAY_NEW);
242 down_write(&loghandle->lgh_lock);
243 up_read(&cathandle->lgh_lock);
246 up_read(&cathandle->lgh_lock);
248 /* time to create new log */
250 /* first, we have to make sure the state hasn't changed */
251 down_write(&cathandle->lgh_lock);
252 loghandle = cathandle->u.chd.chd_current_log;
254 struct llog_log_hdr *llh = loghandle->lgh_hdr;
255 if (loghandle->lgh_last_idx < (sizeof(llh->llh_bitmap)*8) - 1 &&
257 !(llog_cookie_get_flags(logcookie) & LLOG_COOKIE_REPLAY) ||
258 EQ_LOGID(loghandle->lgh_id, logcookie->lgc_lgl))) {
259 down_write(&loghandle->lgh_lock);
260 up_write(&cathandle->lgh_lock);
265 CDEBUG(D_INODE, "creating new log\n");
266 loghandle = llog_cat_new_log(cathandle, logcookie);
267 if (!IS_ERR(loghandle)) {
268 down_write(&loghandle->lgh_lock);
270 *lock = &loghandle->lgh_lock;
273 up_write(&cathandle->lgh_lock);
277 /* Add a single record to the recovery log(s) using a catalog
278 * Returns as llog_write_record
280 * Assumes caller has already pushed us into the kernel context.
282 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
283 struct llog_cookie *reccookie, void *buf,
284 struct rw_semaphore **lock, int *lock_count)
286 struct llog_handle *loghandle;
290 LASSERT(le32_to_cpu(rec->lrh_len) <= LLOG_CHUNK_SIZE);
291 loghandle = llog_cat_current_log(cathandle, 1, reccookie, lock);
292 if (IS_ERR(loghandle))
293 RETURN(PTR_ERR(loghandle));
294 /* loghandle is already locked by llog_cat_current_log() for us */
295 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
296 if (!lock || *lock == NULL) {
297 up_write(&loghandle->lgh_lock);
299 LASSERT(lock_count != NULL);
305 EXPORT_SYMBOL(llog_cat_add_rec);
307 /* For each cookie in the cookie array, we clear the log in-use bit and either:
308 * - the log is empty, so mark it free in the catalog header and delete it
309 * - the log is not empty, just write out the log header
311 * The cookies may be in different log files, so we need to get new logs
314 * Assumes caller has already pushed us into the kernel context.
316 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
317 struct llog_cookie *cookies)
319 int i, index, rc = 0;
322 down_write(&cathandle->lgh_lock);
323 for (i = 0; i < count; i++, cookies++) {
324 struct llog_handle *loghandle;
325 struct llog_logid *lgl = &cookies->lgc_lgl;
327 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
329 CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
333 down_write(&loghandle->lgh_lock);
334 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
335 up_write(&loghandle->lgh_lock);
337 if (rc == 1) { /* log has been destroyed */
338 index = loghandle->u.phd.phd_cookie.lgc_index;
339 if (cathandle->u.chd.chd_current_log == loghandle)
340 cathandle->u.chd.chd_current_log = NULL;
341 llog_free_handle(loghandle);
344 llog_cat_set_first_idx(cathandle, index);
345 rc = llog_cancel_rec(cathandle, index);
347 CDEBUG(D_HA, "cancel plain log at index %u "
348 "of catalog "LPX64"\n",
349 index, cathandle->lgh_id.lgl_oid);
352 up_write(&cathandle->lgh_lock);
356 EXPORT_SYMBOL(llog_cat_cancel_records);
358 static int llog_cat_process_cb(struct llog_handle *cat_llh,
359 struct llog_rec_hdr *rec, void *data)
361 struct llog_process_data *d = data;
362 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
363 struct llog_handle *llh;
366 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
367 CERROR("invalid record in catalog\n");
370 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
371 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
372 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
374 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
376 CERROR("Cannot find handle for log "LPX64"\n",
377 lir->lid_id.lgl_oid);
381 rc = llog_process(llh, d->lpd_cb, d->lpd_data, NULL);
385 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data)
387 struct llog_process_data d;
388 struct llog_process_cat_data cd;
389 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
393 LASSERT(llh->llh_flags &cpu_to_le32(LLOG_F_IS_CAT));
397 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
398 CWARN("catalog "LPX64" crosses index zero\n",
399 cat_llh->lgh_id.lgl_oid);
401 cd.first_idx = le32_to_cpu(llh->llh_cat_idx);
403 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
408 cd.last_idx = cat_llh->lgh_last_idx;
409 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
411 rc = llog_process(cat_llh, llog_cat_process_cb, &d, NULL);
416 EXPORT_SYMBOL(llog_cat_process);
418 static int llog_cat_reverse_process_cb(struct llog_handle *cat_llh,
419 struct llog_rec_hdr *rec, void *data)
421 struct llog_process_data *d = data;
422 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
423 struct llog_handle *llh;
426 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
427 CERROR("invalid record in catalog\n");
430 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
431 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
432 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
434 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
436 CERROR("Cannot find handle for log "LPX64"\n",
437 lir->lid_id.lgl_oid);
441 rc = llog_reverse_process(llh, d->lpd_cb, d->lpd_data, NULL);
445 int llog_cat_reverse_process(struct llog_handle *cat_llh,
446 llog_cb_t cb, void *data)
448 struct llog_process_data d;
449 struct llog_process_cat_data cd;
450 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
454 LASSERT(llh->llh_flags &cpu_to_le32(LLOG_F_IS_CAT));
458 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
459 CWARN("catalog "LPX64" crosses index zero\n",
460 cat_llh->lgh_id.lgl_oid);
463 cd.last_idx = cat_llh->lgh_last_idx;
464 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
469 cd.first_idx = le32_to_cpu(llh->llh_cat_idx);
471 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
474 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
480 EXPORT_SYMBOL(llog_cat_reverse_process);
482 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
484 struct llog_log_hdr *llh = cathandle->lgh_hdr;
485 int i, bitmap_size, idx;
488 bitmap_size = sizeof(llh->llh_bitmap) * 8;
489 if (llh->llh_cat_idx == cpu_to_le32(index - 1)) {
490 idx = le32_to_cpu(llh->llh_cat_idx) + 1;
491 llh->llh_cat_idx = cpu_to_le32(idx);
492 if (idx == cathandle->lgh_last_idx)
494 for (i = (index + 1) % bitmap_size;
495 i != cathandle->lgh_last_idx;
496 i = (i + 1) % bitmap_size) {
497 if (!ext2_test_bit(i, llh->llh_bitmap)) {
498 idx = le32_to_cpu(llh->llh_cat_idx) + 1;
499 llh->llh_cat_idx = cpu_to_le32(idx);
501 llh->llh_cat_idx = 0;
507 CDEBUG(D_HA, "set catalog "LPX64" first idx %u\n",
508 cathandle->lgh_id.lgl_oid,le32_to_cpu(llh->llh_cat_idx));
513 EXPORT_SYMBOL(llog_cat_set_first_idx);
515 int llog_catalog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
516 void *buf, struct llog_cookie *logcookies,
517 int numcookies, void *data,
518 struct rw_semaphore **lock, int *lock_count)
520 struct llog_handle *cathandle;
524 cathandle = ctxt->loc_handle;
525 LASSERT(cathandle != NULL);
527 rc = llog_cat_add_rec(cathandle, rec, logcookies, buf, lock, lock_count);
529 CERROR("write one catalog record failed: %d\n", rc);
532 EXPORT_SYMBOL(llog_catalog_add);
534 int llog_catalog_cancel(struct llog_ctxt *ctxt, int count,
535 struct llog_cookie *cookies, int flags, void *data)
537 struct llog_handle *cathandle;
541 if (cookies == NULL || count == 0)
543 cathandle = ctxt->loc_handle;
544 LASSERT(cathandle != NULL);
545 rc = llog_cat_cancel_records(cathandle, count, cookies);
548 EXPORT_SYMBOL(llog_catalog_cancel);
550 int llog_catalog_setup(struct llog_ctxt **res, char *name,
551 struct obd_export *exp,
552 struct lvfs_run_ctxt *lvfs_ctxt,
553 struct fsfilt_operations *fsops,
554 struct dentry *logs_de,
555 struct dentry *objects_de)
557 struct llog_ctxt *ctxt;
558 struct llog_catid catid;
559 struct llog_handle *handle;
564 OBD_ALLOC(ctxt, sizeof(*ctxt));
570 /* marking this ctxt alone. */
572 ctxt->loc_fsops = fsops;
573 ctxt->loc_lvfs_ctxt = lvfs_ctxt;
575 ctxt->loc_logs_dir = logs_de;
576 ctxt->loc_objects_dir = objects_de;
577 ctxt->loc_logops = &llog_lvfs_ops;
578 ctxt->loc_logops->lop_add = llog_catalog_add;
579 ctxt->loc_logops->lop_cancel = llog_catalog_cancel;
581 memset(&catid, 0, sizeof(struct llog_catid));
582 rc = llog_get_cat_list(lvfs_ctxt, fsops, name, 1, &catid);
584 CERROR("error llog_get_cat_list rc: %d\n", rc);
587 if (catid.lci_logid.lgl_oid)
588 rc = llog_open(ctxt, &handle, &catid.lci_logid, NULL,
591 rc = llog_open(ctxt, &handle, NULL, NULL, OBD_LLOG_FL_CREATE);
593 catid.lci_logid = handle->lgh_id;
598 ctxt->loc_handle = handle;
599 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
603 rc = llog_put_cat_list(lvfs_ctxt, fsops, name, 1, &catid);
605 CERROR("error llog_get_cat_list rc: %d\n", rc);
608 OBD_FREE(ctxt, sizeof(*ctxt));
611 EXPORT_SYMBOL(llog_catalog_setup);
613 int llog_catalog_cleanup(struct llog_ctxt *ctxt)
615 struct llog_handle *cathandle;
621 cathandle = ctxt->loc_handle;
623 llog_cat_put(ctxt->loc_handle);
625 // OBD_FREE(ctxt, sizeof(*ctxt));
628 EXPORT_SYMBOL(llog_catalog_cleanup);
630 int llog_cat_half_bottom(struct llog_cookie *cookie, struct llog_handle *handle)
632 struct llog_handle *loghandle;
633 struct llog_logid *lgl = &cookie->lgc_lgl;
636 down_read(&handle->lgh_lock);
637 rc = llog_cat_id2handle(handle, &loghandle, lgl);
640 if (2 * loghandle->lgh_hdr->llh_cat_idx <=
641 handle->lgh_last_idx + handle->lgh_hdr->llh_cat_idx + 1)
646 up_read(&handle->lgh_lock);
649 EXPORT_SYMBOL(llog_cat_half_bottom);