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 the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
25 * OST<->MDS recovery logging infrastructure.
27 * Invariants in implementation:
28 * - we do not share logs among different OST<->MDS connections, so that
29 * if an OST or MDS fails it need only look at log(s) relevant to itself
32 #define DEBUG_SUBSYSTEM S_LOG
39 #include <liblustre.h>
42 #include <obd_class.h>
43 #include <lustre_log.h>
44 #include <libcfs/list.h>
46 /* Create a new log handle and add it to the open list.
47 * This log handle will be closed when all of the records in it are removed.
49 * Assumes caller has already pushed us into the kernel context and is locking.
51 static struct llog_handle *llog_cat_new_log(struct llog_handle *cathandle)
53 struct llog_handle *loghandle;
54 struct llog_log_hdr *llh;
55 struct llog_logid_rec rec = { { 0 }, };
56 int rc, index, bitmap_size;
59 llh = cathandle->lgh_hdr;
60 bitmap_size = LLOG_BITMAP_SIZE(llh);
62 index = (cathandle->lgh_last_idx + 1) % bitmap_size;
64 /* maximum number of available slots in catlog is bitmap_size - 2 */
65 if (llh->llh_cat_idx == index) {
66 CERROR("no free catalog slots for log...\n");
67 RETURN(ERR_PTR(-ENOSPC));
71 if (ext2_set_bit(index, llh->llh_bitmap)) {
72 CERROR("argh, index %u already set in log bitmap?\n",
74 LBUG(); /* should never happen */
76 cathandle->lgh_last_idx = index;
78 llh->llh_tail.lrt_index = index;
81 rc = llog_create(cathandle->lgh_ctxt, &loghandle, NULL, NULL);
85 rc = llog_init_handle(loghandle,
86 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
87 &cathandle->lgh_hdr->llh_tgtuuid);
89 GOTO(out_destroy, rc);
91 CDEBUG(D_HA, "new recovery log "LPX64":%x for index %u of catalog "
92 LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
93 index, cathandle->lgh_id.lgl_oid);
94 /* build the record for this log in the catalog */
95 rec.lid_hdr.lrh_len = sizeof(rec);
96 rec.lid_hdr.lrh_index = index;
97 rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
98 rec.lid_id = loghandle->lgh_id;
99 rec.lid_tail.lrt_len = sizeof(rec);
100 rec.lid_tail.lrt_index = index;
102 /* update the catalog: header and record */
103 rc = llog_write_rec(cathandle, &rec.lid_hdr,
104 &loghandle->u.phd.phd_cookie, 1, NULL, index);
106 GOTO(out_destroy, rc);
109 loghandle->lgh_hdr->llh_cat_idx = index;
110 cathandle->u.chd.chd_current_log = loghandle;
111 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
112 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
116 llog_destroy(loghandle);
120 EXPORT_SYMBOL(llog_cat_new_log);
122 /* Open an existent log handle and add it to the open list.
123 * This log handle will be closed when all of the records in it are removed.
125 * Assumes caller has already pushed us into the kernel context and is locking.
126 * We return a lock on the handle to ensure nobody yanks it from us.
128 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
129 struct llog_logid *logid)
131 struct llog_handle *loghandle;
135 if (cathandle == NULL)
138 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
140 struct llog_logid *cgl = &loghandle->lgh_id;
141 if (cgl->lgl_oid == logid->lgl_oid) {
142 if (cgl->lgl_ogen != logid->lgl_ogen) {
143 CERROR("log "LPX64" generation %x != %x\n",
144 logid->lgl_oid, cgl->lgl_ogen,
148 loghandle->u.phd.phd_cat_handle = cathandle;
153 rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL);
155 CERROR("error opening log id "LPX64":%x: rc %d\n",
156 logid->lgl_oid, logid->lgl_ogen, rc);
158 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
160 list_add(&loghandle->u.phd.phd_entry,
161 &cathandle->u.chd.chd_head);
165 loghandle->u.phd.phd_cat_handle = cathandle;
166 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
167 loghandle->u.phd.phd_cookie.lgc_index =
168 loghandle->lgh_hdr->llh_cat_idx;
176 int llog_cat_put(struct llog_handle *cathandle)
178 struct llog_handle *loghandle, *n;
182 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
184 int err = llog_close(loghandle);
186 CERROR("error closing loghandle\n");
188 rc = llog_close(cathandle);
191 EXPORT_SYMBOL(llog_cat_put);
193 /* Return the currently active log handle. If the current log handle doesn't
194 * have enough space left for the current record, start a new one.
196 * If reclen is 0, we only want to know what the currently active log is,
197 * otherwise we get a lock on this log so nobody can steal our space.
199 * Assumes caller has already pushed us into the kernel context and is locking.
201 * NOTE: loghandle is write-locked upon successful return
203 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
206 struct llog_handle *loghandle = NULL;
209 down_read(&cathandle->lgh_lock);
210 loghandle = cathandle->u.chd.chd_current_log;
212 struct llog_log_hdr *llh = loghandle->lgh_hdr;
213 down_write(&loghandle->lgh_lock);
214 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
215 up_read(&cathandle->lgh_lock);
218 up_write(&loghandle->lgh_lock);
223 down_write(&loghandle->lgh_lock);
224 up_read(&cathandle->lgh_lock);
227 up_read(&cathandle->lgh_lock);
229 /* time to create new log */
231 /* first, we have to make sure the state hasn't changed */
232 down_write(&cathandle->lgh_lock);
233 loghandle = cathandle->u.chd.chd_current_log;
235 struct llog_log_hdr *llh = loghandle->lgh_hdr;
236 down_write(&loghandle->lgh_lock);
237 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
238 up_write(&cathandle->lgh_lock);
241 up_write(&loghandle->lgh_lock);
245 CDEBUG(D_INODE, "creating new log\n");
246 loghandle = llog_cat_new_log(cathandle);
247 if (!IS_ERR(loghandle))
248 down_write(&loghandle->lgh_lock);
249 up_write(&cathandle->lgh_lock);
253 /* Add a single record to the recovery log(s) using a catalog
254 * Returns as llog_write_record
256 * Assumes caller has already pushed us into the kernel context.
258 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
259 struct llog_cookie *reccookie, void *buf)
261 struct llog_handle *loghandle;
265 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
266 loghandle = llog_cat_current_log(cathandle, 1);
267 if (IS_ERR(loghandle))
268 RETURN(PTR_ERR(loghandle));
269 /* loghandle is already locked by llog_cat_current_log() for us */
270 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
271 up_write(&loghandle->lgh_lock);
273 /* to create a new plain log */
274 loghandle = llog_cat_current_log(cathandle, 1);
275 if (IS_ERR(loghandle))
276 RETURN(PTR_ERR(loghandle));
277 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
278 up_write(&loghandle->lgh_lock);
283 EXPORT_SYMBOL(llog_cat_add_rec);
285 /* For each cookie in the cookie array, we clear the log in-use bit and either:
286 * - the log is empty, so mark it free in the catalog header and delete it
287 * - the log is not empty, just write out the log header
289 * The cookies may be in different log files, so we need to get new logs
292 * Assumes caller has already pushed us into the kernel context.
294 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
295 struct llog_cookie *cookies)
297 int i, index, rc = 0;
300 down_write(&cathandle->lgh_lock);
301 for (i = 0; i < count; i++, cookies++) {
302 struct llog_handle *loghandle;
303 struct llog_logid *lgl = &cookies->lgc_lgl;
305 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
307 CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
311 down_write(&loghandle->lgh_lock);
312 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
313 up_write(&loghandle->lgh_lock);
315 if (rc == 1) { /* log has been destroyed */
316 index = loghandle->u.phd.phd_cookie.lgc_index;
317 if (cathandle->u.chd.chd_current_log == loghandle)
318 cathandle->u.chd.chd_current_log = NULL;
319 llog_free_handle(loghandle);
322 llog_cat_set_first_idx(cathandle, index);
323 rc = llog_cancel_rec(cathandle, index);
325 CDEBUG(D_HA, "cancel plain log at index %u "
326 "of catalog "LPX64"\n",
327 index, cathandle->lgh_id.lgl_oid);
330 up_write(&cathandle->lgh_lock);
334 EXPORT_SYMBOL(llog_cat_cancel_records);
336 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec,
339 struct llog_process_data *d = data;
340 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
341 struct llog_handle *llh;
345 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
346 CERROR("invalid record in catalog\n");
349 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
350 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
351 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
353 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
355 CERROR("Cannot find handle for log "LPX64"\n",
356 lir->lid_id.lgl_oid);
360 rc = llog_process(llh, d->lpd_cb, d->lpd_data, NULL);
364 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data)
366 struct llog_process_data d;
367 struct llog_process_cat_data cd;
368 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
372 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
376 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
377 CWARN("catlog "LPX64" crosses index zero\n",
378 cat_llh->lgh_id.lgl_oid);
380 cd.first_idx = llh->llh_cat_idx;
382 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
387 cd.last_idx = cat_llh->lgh_last_idx;
388 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
390 rc = llog_process(cat_llh, llog_cat_process_cb, &d, NULL);
395 EXPORT_SYMBOL(llog_cat_process);
397 static int llog_cat_reverse_process_cb(struct llog_handle *cat_llh,
398 struct llog_rec_hdr *rec, void *data)
400 struct llog_process_data *d = data;
401 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
402 struct llog_handle *llh;
405 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
406 CERROR("invalid record in catalog\n");
409 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
410 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
411 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
413 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
415 CERROR("Cannot find handle for log "LPX64"\n",
416 lir->lid_id.lgl_oid);
420 rc = llog_reverse_process(llh, d->lpd_cb, d->lpd_data, NULL);
424 int llog_cat_reverse_process(struct llog_handle *cat_llh,
425 llog_cb_t cb, void *data)
427 struct llog_process_data d;
428 struct llog_process_cat_data cd;
429 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
433 LASSERT(llh->llh_flags &cpu_to_le32(LLOG_F_IS_CAT));
437 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
438 CWARN("catalog "LPX64" crosses index zero\n",
439 cat_llh->lgh_id.lgl_oid);
442 cd.last_idx = cat_llh->lgh_last_idx;
443 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
448 cd.first_idx = le32_to_cpu(llh->llh_cat_idx);
450 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
453 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
459 EXPORT_SYMBOL(llog_cat_reverse_process);
461 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
463 struct llog_log_hdr *llh = cathandle->lgh_hdr;
464 int i, bitmap_size, idx;
467 bitmap_size = LLOG_BITMAP_SIZE(llh);
468 if (llh->llh_cat_idx == (index - 1)) {
469 idx = llh->llh_cat_idx + 1;
470 llh->llh_cat_idx = idx;
471 if (idx == cathandle->lgh_last_idx)
473 for (i = (index + 1) % bitmap_size;
474 i != cathandle->lgh_last_idx;
475 i = (i + 1) % bitmap_size) {
476 if (!ext2_test_bit(i, llh->llh_bitmap)) {
477 idx = llh->llh_cat_idx + 1;
478 llh->llh_cat_idx = idx;
480 llh->llh_cat_idx = 0;
486 CDEBUG(D_HA, "set catlog "LPX64" first idx %u\n",
487 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
494 /* Assumes caller has already pushed us into the kernel context. */
495 int llog_cat_init(struct llog_handle *cathandle, struct obd_uuid *tgtuuid)
497 struct llog_log_hdr *llh;
502 LASSERT(sizeof(*llh) == LLOG_CHUNK_SIZE);
504 down(&cathandle->lgh_lock);
505 llh = cathandle->lgh_hdr;
507 if (cathandle->lgh_file->f_dentry->d_inode->i_size == 0) {
508 llog_write_rec(cathandle, &llh->llh_hdr, NULL, 0, NULL, 0);
511 rc = lustre_fwrite(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
513 if (rc != LLOG_CHUNK_SIZE) {
514 CERROR("error writing catalog header: rc %d\n", rc);
515 OBD_FREE(llh, sizeof(*llh));
521 rc = lustre_fread(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
523 if (rc != LLOG_CHUNK_SIZE) {
524 CERROR("error reading catalog header: rc %d\n", rc);
525 /* Can we do much else if the header is bad? */
531 cathandle->lgh_tgtuuid = &llh->llh_tgtuuid;
532 up(&cathandle->lgh_lock);
535 EXPORT_SYMBOL(llog_cat_init);