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
41 #include <liblustre.h>
44 #include <linux/obd_class.h>
45 #include <linux/lustre_log.h>
46 #include <libcfs/list.h>
48 /* Create a new log handle and add it to the open list.
49 * This log handle will be closed when all of the records in it are removed.
51 * Assumes caller has already pushed us into the kernel context and is locking.
53 static struct llog_handle *llog_cat_new_log(struct llog_handle *cathandle)
55 struct llog_handle *loghandle;
56 struct llog_log_hdr *llh;
57 struct llog_logid_rec rec = { { 0 }, };
58 int rc, index, bitmap_size;
61 llh = cathandle->lgh_hdr;
62 bitmap_size = LLOG_BITMAP_SIZE(llh);
64 index = (cathandle->lgh_last_idx + 1) % bitmap_size;
66 /* maximum number of available slots in catlog is bitmap_size - 2 */
67 if (llh->llh_cat_idx == index) {
68 CERROR("no free catalog slots for log...\n");
69 RETURN(ERR_PTR(-ENOSPC));
73 if (ext2_set_bit(index, llh->llh_bitmap)) {
74 CERROR("argh, index %u already set in log bitmap?\n",
76 LBUG(); /* should never happen */
78 cathandle->lgh_last_idx = index;
80 llh->llh_tail.lrt_index = index;
83 rc = llog_create(cathandle->lgh_ctxt, &loghandle, NULL, 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);
93 CDEBUG(D_HA, "new recovery log "LPX64":%x for index %u of catalog "
94 LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
95 index, cathandle->lgh_id.lgl_oid);
96 /* build the record for this log in the catalog */
97 rec.lid_hdr.lrh_len = sizeof(rec);
98 rec.lid_hdr.lrh_index = index;
99 rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
100 rec.lid_id = loghandle->lgh_id;
101 rec.lid_tail.lrt_len = sizeof(rec);
102 rec.lid_tail.lrt_index = index;
104 /* update the catalog: header and record */
105 rc = llog_write_rec(cathandle, &rec.lid_hdr,
106 &loghandle->u.phd.phd_cookie, 1, NULL, index);
108 GOTO(out_destroy, rc);
111 loghandle->lgh_hdr->llh_cat_idx = index;
112 cathandle->u.chd.chd_current_log = loghandle;
113 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
114 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
118 llog_destroy(loghandle);
122 EXPORT_SYMBOL(llog_cat_new_log);
124 /* Open an existent log handle and add it to the open list.
125 * This log handle will be closed when all of the records in it are removed.
127 * Assumes caller has already pushed us into the kernel context and is locking.
128 * We return a lock on the handle to ensure nobody yanks it from us.
130 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
131 struct llog_logid *logid)
133 struct llog_handle *loghandle;
137 if (cathandle == NULL)
140 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
142 struct llog_logid *cgl = &loghandle->lgh_id;
143 if (cgl->lgl_oid == logid->lgl_oid) {
144 if (cgl->lgl_ogen != logid->lgl_ogen) {
145 CERROR("log "LPX64" generation %x != %x\n",
146 logid->lgl_oid, cgl->lgl_ogen,
150 loghandle->u.phd.phd_cat_handle = cathandle;
155 rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL, NULL);
157 CERROR("error opening log id "LPX64":%x: rc %d\n",
158 logid->lgl_oid, logid->lgl_ogen, rc);
160 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
162 list_add(&loghandle->u.phd.phd_entry,
163 &cathandle->u.chd.chd_head);
167 loghandle->u.phd.phd_cat_handle = cathandle;
168 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
169 loghandle->u.phd.phd_cookie.lgc_index =
170 loghandle->lgh_hdr->llh_cat_idx;
178 int llog_cat_put(struct llog_handle *cathandle)
180 struct llog_handle *loghandle, *n;
184 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
186 int err = llog_close(loghandle);
188 CERROR("error closing loghandle\n");
190 rc = llog_close(cathandle);
193 EXPORT_SYMBOL(llog_cat_put);
195 /* Return the currently active log handle. If the current log handle doesn't
196 * have enough space left for the current record, start a new one.
198 * If reclen is 0, we only want to know what the currently active log is,
199 * otherwise we get a lock on this log so nobody can steal our space.
201 * Assumes caller has already pushed us into the kernel context and is locking.
203 * NOTE: loghandle is write-locked upon successful return
205 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
208 struct llog_handle *loghandle = NULL;
211 down_read(&cathandle->lgh_lock);
212 loghandle = cathandle->u.chd.chd_current_log;
214 struct llog_log_hdr *llh = loghandle->lgh_hdr;
215 down_write(&loghandle->lgh_lock);
216 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
217 up_read(&cathandle->lgh_lock);
220 up_write(&loghandle->lgh_lock);
225 down_write(&loghandle->lgh_lock);
226 up_read(&cathandle->lgh_lock);
229 up_read(&cathandle->lgh_lock);
231 /* time to create new log */
233 /* first, we have to make sure the state hasn't changed */
234 down_write(&cathandle->lgh_lock);
235 loghandle = cathandle->u.chd.chd_current_log;
237 struct llog_log_hdr *llh = loghandle->lgh_hdr;
238 down_write(&loghandle->lgh_lock);
239 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
240 up_write(&cathandle->lgh_lock);
243 up_write(&loghandle->lgh_lock);
247 CDEBUG(D_INODE, "creating new log\n");
248 loghandle = llog_cat_new_log(cathandle);
249 if (!IS_ERR(loghandle))
250 down_write(&loghandle->lgh_lock);
251 up_write(&cathandle->lgh_lock);
255 /* Add a single record to the recovery log(s) using a catalog
256 * Returns as llog_write_record
258 * Assumes caller has already pushed us into the kernel context.
260 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
261 struct llog_cookie *reccookie, void *buf)
263 struct llog_handle *loghandle;
267 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
268 loghandle = llog_cat_current_log(cathandle, 1);
269 if (IS_ERR(loghandle))
270 RETURN(PTR_ERR(loghandle));
271 /* loghandle is already locked by llog_cat_current_log() for us */
272 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
273 up_write(&loghandle->lgh_lock);
275 /* to create a new plain log */
276 loghandle = llog_cat_current_log(cathandle, 1);
277 if (IS_ERR(loghandle))
278 RETURN(PTR_ERR(loghandle));
279 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
280 up_write(&loghandle->lgh_lock);
285 EXPORT_SYMBOL(llog_cat_add_rec);
287 /* For each cookie in the cookie array, we clear the log in-use bit and either:
288 * - the log is empty, so mark it free in the catalog header and delete it
289 * - the log is not empty, just write out the log header
291 * The cookies may be in different log files, so we need to get new logs
294 * Assumes caller has already pushed us into the kernel context.
296 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
297 struct llog_cookie *cookies)
299 int i, index, rc = 0;
302 down_write(&cathandle->lgh_lock);
303 for (i = 0; i < count; i++, cookies++) {
304 struct llog_handle *loghandle;
305 struct llog_logid *lgl = &cookies->lgc_lgl;
307 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
309 CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
313 down_write(&loghandle->lgh_lock);
314 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
315 up_write(&loghandle->lgh_lock);
317 if (rc == 1) { /* log has been destroyed */
318 index = loghandle->u.phd.phd_cookie.lgc_index;
319 if (cathandle->u.chd.chd_current_log == loghandle)
320 cathandle->u.chd.chd_current_log = NULL;
321 llog_free_handle(loghandle);
324 llog_cat_set_first_idx(cathandle, index);
325 rc = llog_cancel_rec(cathandle, index);
327 CDEBUG(D_HA, "cancel plain log at index %u "
328 "of catalog "LPX64"\n",
329 index, cathandle->lgh_id.lgl_oid);
332 up_write(&cathandle->lgh_lock);
336 EXPORT_SYMBOL(llog_cat_cancel_records);
338 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec,
341 struct llog_process_data *d = data;
342 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
343 struct llog_handle *llh;
346 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
347 CERROR("invalid record in catalog\n");
350 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
351 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
352 rec->lrh_index, cat_llh->lgh_id.lgl_oid);
354 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
356 CERROR("Cannot find handle for log "LPX64"\n",
357 lir->lid_id.lgl_oid);
361 rc = llog_process(llh, d->lpd_cb, d->lpd_data, NULL);
365 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data)
367 struct llog_process_data d;
368 struct llog_process_cat_data cd;
369 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
373 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
377 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
378 CWARN("catlog "LPX64" crosses index zero\n",
379 cat_llh->lgh_id.lgl_oid);
381 cd.first_idx = llh->llh_cat_idx;
383 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
388 cd.last_idx = cat_llh->lgh_last_idx;
389 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
391 rc = llog_process(cat_llh, llog_cat_process_cb, &d, NULL);
396 EXPORT_SYMBOL(llog_cat_process);
398 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
400 struct llog_log_hdr *llh = cathandle->lgh_hdr;
401 int i, bitmap_size, idx;
404 bitmap_size = LLOG_BITMAP_SIZE(llh);
405 if (llh->llh_cat_idx == (index - 1)) {
406 idx = llh->llh_cat_idx + 1;
407 llh->llh_cat_idx = idx;
408 if (idx == cathandle->lgh_last_idx)
410 for (i = (index + 1) % bitmap_size;
411 i != cathandle->lgh_last_idx;
412 i = (i + 1) % bitmap_size) {
413 if (!ext2_test_bit(i, llh->llh_bitmap)) {
414 idx = llh->llh_cat_idx + 1;
415 llh->llh_cat_idx = idx;
417 llh->llh_cat_idx = 0;
423 CDEBUG(D_HA, "set catlog "LPX64" first idx %u\n",
424 cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
431 /* Assumes caller has already pushed us into the kernel context. */
432 int llog_cat_init(struct llog_handle *cathandle, struct obd_uuid *tgtuuid)
434 struct llog_log_hdr *llh;
439 LASSERT(sizeof(*llh) == LLOG_CHUNK_SIZE);
441 down(&cathandle->lgh_lock);
442 llh = cathandle->lgh_hdr;
444 if (cathandle->lgh_file->f_dentry->d_inode->i_size == 0) {
445 llog_write_rec(cathandle, &llh->llh_hdr, NULL, 0, NULL, 0);
448 rc = lustre_fwrite(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
450 if (rc != LLOG_CHUNK_SIZE) {
451 CERROR("error writing catalog header: rc %d\n", rc);
452 OBD_FREE(llh, sizeof(*llh));
458 rc = lustre_fread(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
460 if (rc != LLOG_CHUNK_SIZE) {
461 CERROR("error reading catalog header: rc %d\n", rc);
462 /* Can we do much else if the header is bad? */
468 cathandle->lgh_tgtuuid = &llh->llh_tgtuuid;
469 up(&cathandle->lgh_lock);
472 EXPORT_SYMBOL(llog_cat_init);