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 /* Allocate a new log or catalog handle */
49 struct llog_handle *llog_alloc_handle(void)
51 struct llog_handle *loghandle;
54 OBD_ALLOC(loghandle, sizeof(*loghandle));
55 if (loghandle == NULL)
56 RETURN(ERR_PTR(-ENOMEM));
58 init_rwsem(&loghandle->lgh_lock);
62 EXPORT_SYMBOL(llog_alloc_handle);
65 void llog_free_handle(struct llog_handle *loghandle)
70 if (!loghandle->lgh_hdr)
72 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
73 list_del_init(&loghandle->u.phd.phd_entry);
74 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
75 LASSERT(list_empty(&loghandle->u.chd.chd_head));
76 OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
79 OBD_FREE(loghandle, sizeof(*loghandle));
81 EXPORT_SYMBOL(llog_free_handle);
83 /* returns negative on error; 0 if success; 1 if success & log destroyed */
84 int llog_cancel_rec(struct llog_handle *loghandle, int index)
86 struct llog_log_hdr *llh = loghandle->lgh_hdr;
90 CDEBUG(D_HA, "canceling %d in log "LPX64"\n",
91 index, loghandle->lgh_id.lgl_oid);
94 CERROR("cannot cancel index 0 (which is header)\n");
98 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
99 CDEBUG(D_HA, "catalog index %u already clear?\n", index);
105 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
106 (llh->llh_count == 1) &&
107 (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
108 rc = llog_destroy(loghandle);
110 CERROR("failure destroying log after last cancel: %d\n",
112 ext2_set_bit(index, llh->llh_bitmap);
120 rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
122 CERROR("failure re-writing header %d\n", rc);
123 ext2_set_bit(index, llh->llh_bitmap);
128 EXPORT_SYMBOL(llog_cancel_rec);
130 int llog_init_handle(struct llog_handle *handle, int flags,
131 struct obd_uuid *uuid)
134 struct llog_log_hdr *llh;
136 LASSERT(handle->lgh_hdr == NULL);
138 OBD_ALLOC(llh, sizeof(*llh));
141 handle->lgh_hdr = llh;
142 /* first assign flags to use llog_client_ops */
143 llh->llh_flags = flags;
144 rc = llog_read_header(handle);
146 flags = llh->llh_flags;
147 if (uuid && !obd_uuid_equals(uuid, &llh->llh_tgtuuid)) {
148 CERROR("uuid mismatch: %s/%s\n", (char *)uuid->uuid,
149 (char *)llh->llh_tgtuuid.uuid);
153 } else if (rc != LLOG_EEMPTY || !flags) {
154 /* set a pesudo flag for initialization */
155 flags = LLOG_F_IS_CAT;
160 handle->lgh_last_idx = 0; /* header is record with index 0 */
161 llh->llh_count = 1; /* for the header record */
162 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
163 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
164 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
165 llh->llh_timestamp = CURRENT_SECONDS;
167 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
168 llh->llh_bitmap_offset = offsetof(typeof(*llh),llh_bitmap);
169 ext2_set_bit(0, llh->llh_bitmap);
172 if (flags & LLOG_F_IS_CAT) {
173 INIT_LIST_HEAD(&handle->u.chd.chd_head);
174 llh->llh_size = sizeof(struct llog_logid_rec);
175 } else if (flags & LLOG_F_IS_PLAIN) {
176 INIT_LIST_HEAD(&handle->u.phd.phd_entry);
178 CERROR("Unknown flags: %#x (Expected %#x or %#x\n",
179 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
184 OBD_FREE(llh, sizeof(*llh));
185 handle->lgh_hdr = NULL;
189 EXPORT_SYMBOL(llog_init_handle);
191 int llog_close(struct llog_handle *loghandle)
193 struct llog_operations *lop;
197 rc = llog_handle2ops(loghandle, &lop);
200 if (lop->lop_close == NULL)
201 GOTO(out, -EOPNOTSUPP);
202 rc = lop->lop_close(loghandle);
204 llog_free_handle(loghandle);
207 EXPORT_SYMBOL(llog_close);
209 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
210 void *data, void *catdata)
212 struct llog_log_hdr *llh = loghandle->lgh_hdr;
213 struct llog_process_cat_data *cd = catdata;
215 __u64 cur_offset = LLOG_CHUNK_SIZE;
216 int rc = 0, index = 1, last_index;
217 int saved_index = 0, last_called_index = 0;
222 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
227 last_called_index = cd->first_idx;
228 index = cd->first_idx + 1;
230 if (cd != NULL && cd->last_idx)
231 last_index = cd->last_idx;
233 last_index = LLOG_BITMAP_BYTES * 8 - 1;
236 struct llog_rec_hdr *rec;
238 /* skip records not set in bitmap */
239 while (index <= last_index &&
240 !ext2_test_bit(index, llh->llh_bitmap))
243 LASSERT(index <= last_index + 1);
244 if (index == last_index + 1)
247 CDEBUG(D_OTHER, "index: %d last_index %d\n",
250 /* get the buf with our target record; avoid old garbage */
251 memset(buf, 0, LLOG_CHUNK_SIZE);
252 rc = llog_next_block(loghandle, &saved_index, index,
253 &cur_offset, buf, LLOG_CHUNK_SIZE);
257 /* NB: when rec->lrh_len is accessed it is already swabbed
258 * since it is used at the "end" of the loop and the rec
259 * swabbing is done at the beginning of the loop. */
260 for (rec = (struct llog_rec_hdr *)buf;
261 (char *)rec < buf + LLOG_CHUNK_SIZE;
262 rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
264 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
267 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
268 lustre_swab_llog_rec(rec, NULL);
270 CDEBUG(D_OTHER, "after swabbing, type: %#x\n",
273 if (rec->lrh_index == 0)
274 GOTO(out, 0); /* no more records */
276 if (rec->lrh_len == 0 || rec->lrh_len >LLOG_CHUNK_SIZE){
277 CWARN("invalid length %d in llog record for "
278 "index %d\n", rec->lrh_len,
283 if (rec->lrh_index < index) {
284 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
290 "lrh_index: %d lrh_len: %d (%d remains)\n",
291 rec->lrh_index, rec->lrh_len,
292 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
294 /* if set, process the callback on this record */
295 if (ext2_test_bit(index, llh->llh_bitmap)) {
296 rc = cb(loghandle, rec, data);
297 last_called_index = index;
298 if (rc == LLOG_PROC_BREAK) {
299 CWARN("recovery from log: "LPX64":%x"
301 loghandle->lgh_id.lgl_oid,
302 loghandle->lgh_id.lgl_ogen);
304 } else if (rc == LLOG_DEL_RECORD) {
305 llog_cancel_rec(loghandle, rec->lrh_index);
311 CDEBUG(D_OTHER, "Skipped index %d\n", index);
314 /* next record, still in buffer? */
316 if (index > last_index)
323 cd->last_idx = last_called_index;
325 OBD_FREE(buf, LLOG_CHUNK_SIZE);
328 EXPORT_SYMBOL(llog_process);
330 inline int llog_get_size(struct llog_handle *loghandle)
332 if (loghandle && loghandle->lgh_hdr)
333 return loghandle->lgh_hdr->llh_count;
336 EXPORT_SYMBOL(llog_get_size);
338 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
339 void *data, void *catdata)
341 struct llog_log_hdr *llh = loghandle->lgh_hdr;
342 struct llog_process_cat_data *cd = catdata;
344 int rc = 0, first_index = 1, index, idx;
347 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
352 first_index = cd->first_idx + 1;
353 if (cd != NULL && cd->last_idx)
354 index = cd->last_idx;
356 index = LLOG_BITMAP_BYTES * 8 - 1;
359 struct llog_rec_hdr *rec;
360 struct llog_rec_tail *tail;
362 /* skip records not set in bitmap */
363 while (index >= first_index &&
364 !ext2_test_bit(index, llh->llh_bitmap))
367 LASSERT(index >= first_index - 1);
368 if (index == first_index - 1)
371 /* get the buf with our target record; avoid old garbage */
372 memset(buf, 0, LLOG_CHUNK_SIZE);
373 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
378 idx = le32_to_cpu(rec->lrh_index);
380 CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
381 while (idx < index) {
382 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
385 tail = (void *)rec + le32_to_cpu(rec->lrh_len) - sizeof(*tail);
387 /* process records in buffer, starting where we found one */
388 while ((void *)tail > buf) {
389 rec = (void *)tail - le32_to_cpu(tail->lrt_len) +
392 if (rec->lrh_index == 0)
393 GOTO(out, 0); /* no more records */
395 /* if set, process the callback on this record */
396 if (ext2_test_bit(index, llh->llh_bitmap)) {
397 rc = cb(loghandle, rec, data);
398 if (rc == LLOG_PROC_BREAK) {
399 CWARN("recovery from log: "LPX64":%x"
401 loghandle->lgh_id.lgl_oid,
402 loghandle->lgh_id.lgl_ogen);
409 /* previous record, still in buffer? */
411 if (index < first_index)
413 tail = (void *)rec - sizeof(*tail);
419 OBD_FREE(buf, LLOG_CHUNK_SIZE);
422 EXPORT_SYMBOL(llog_reverse_process);