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>
43 /* Allocate a new log or catalog handle */
44 struct llog_handle *llog_alloc_handle(void)
46 struct llog_handle *loghandle;
49 OBD_ALLOC(loghandle, sizeof(*loghandle));
50 if (loghandle == NULL)
51 RETURN(ERR_PTR(-ENOMEM));
53 init_rwsem(&loghandle->lgh_lock);
57 EXPORT_SYMBOL(llog_alloc_handle);
59 void llog_free_handle(struct llog_handle *loghandle)
64 if (!loghandle->lgh_hdr)
66 if (le32_to_cpu(loghandle->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)
67 list_del_init(&loghandle->u.phd.phd_entry);
68 if (le32_to_cpu(loghandle->lgh_hdr->llh_flags) & LLOG_F_IS_CAT)
69 LASSERT(list_empty(&loghandle->u.chd.chd_head));
70 OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
73 OBD_FREE(loghandle, sizeof(*loghandle));
75 EXPORT_SYMBOL(llog_free_handle);
77 /* returns negative on error; 0 if success; 1 if success & log destroyed */
78 int llog_cancel_rec(struct llog_handle *loghandle, int index)
80 struct llog_log_hdr *llh = loghandle->lgh_hdr;
84 CDEBUG(D_HA, "canceling %d in log "LPX64"\n",
85 index, loghandle->lgh_id.lgl_oid);
88 CERROR("cannot cancel index 0 (which is header)\n");
92 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
93 CERROR("catalog index %u already clear?\n", index);
97 llh->llh_count = cpu_to_le32(le32_to_cpu(llh->llh_count) - 1);
99 if ((le32_to_cpu(llh->llh_flags) & LLOG_F_ZAP_WHEN_EMPTY) &&
100 (le32_to_cpu(llh->llh_count) == 1) &&
101 (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
102 rc = llog_destroy(loghandle);
104 CERROR("failure destroying log after last cancel: %d\n",
106 ext2_set_bit(index, llh->llh_bitmap);
114 rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
116 CERROR("failure re-writing header %d\n", rc);
117 ext2_set_bit(index, llh->llh_bitmap);
122 EXPORT_SYMBOL(llog_cancel_rec);
124 int llog_init_handle(struct llog_handle *handle, int flags,
125 struct obd_uuid *uuid)
128 struct llog_log_hdr *llh;
130 LASSERT(handle->lgh_hdr == NULL);
132 OBD_ALLOC(llh, sizeof(*llh));
135 handle->lgh_hdr = llh;
136 /* first assign flags to use llog_client_ops */
137 llh->llh_flags = cpu_to_le32(flags);
138 rc = llog_read_header(handle);
140 flags = le32_to_cpu(llh->llh_flags);
142 LASSERT(obd_uuid_equals(uuid, &llh->llh_tgtuuid));
144 } else if (rc != LLOG_EEMPTY || !flags) {
145 /* set a pesudo flag for initialization */
146 flags = LLOG_F_IS_CAT;
151 handle->lgh_last_idx = 0; /* header is record with index 0 */
152 llh->llh_count = cpu_to_le32(1); /* for the header record */
153 llh->llh_hdr.lrh_type = cpu_to_le32(LLOG_HDR_MAGIC);
154 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len =
155 cpu_to_le32(LLOG_CHUNK_SIZE);
156 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
157 llh->llh_timestamp = cpu_to_le64(LTIME_S(CURRENT_TIME));
159 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
160 llh->llh_bitmap_offset = cpu_to_le32(offsetof(typeof(*llh),llh_bitmap));
161 ext2_set_bit(0, llh->llh_bitmap);
164 if (flags & LLOG_F_IS_CAT) {
165 INIT_LIST_HEAD(&handle->u.chd.chd_head);
166 llh->llh_size = cpu_to_le32(sizeof(struct llog_logid_rec));
168 else if (flags & LLOG_F_IS_PLAIN)
169 INIT_LIST_HEAD(&handle->u.phd.phd_entry);
174 OBD_FREE(llh, sizeof(*llh));
175 handle->lgh_hdr = NULL;
179 EXPORT_SYMBOL(llog_init_handle);
181 int llog_close(struct llog_handle *loghandle)
183 struct llog_operations *lop;
187 rc = llog_handle2ops(loghandle, &lop);
190 if (lop->lop_close == NULL)
191 GOTO(out, rc = -EOPNOTSUPP);
192 rc = lop->lop_close(loghandle);
194 llog_free_handle(loghandle);
197 EXPORT_SYMBOL(llog_close);
199 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
200 void *data, void *catdata)
202 struct llog_log_hdr *llh = loghandle->lgh_hdr;
203 struct llog_process_cat_data *cd = catdata;
205 __u64 cur_offset = LLOG_CHUNK_SIZE;
206 int rc = 0, index = 1, last_index, idx;
210 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
215 index = cd->first_idx + 1;
216 if (cd != NULL && cd->last_idx)
217 last_index = cd->last_idx;
219 last_index = LLOG_BITMAP_BYTES * 8 - 1;
222 struct llog_rec_hdr *rec;
224 /* skip records not set in bitmap */
225 while (index <= last_index &&
226 !ext2_test_bit(index, llh->llh_bitmap))
229 LASSERT(index <= last_index + 1);
230 if (index == last_index + 1)
233 /* get the buf with our target record; avoid old garbage */
234 memset(buf, 0, LLOG_CHUNK_SIZE);
235 rc = llog_next_block(loghandle, &saved_index, index,
236 &cur_offset, buf, LLOG_CHUNK_SIZE);
241 idx = le32_to_cpu(rec->lrh_index);
243 CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
244 while (idx < index) {
245 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
249 /* process records in buffer, starting where we found one */
250 while ((void *)rec < buf + LLOG_CHUNK_SIZE) {
251 if (rec->lrh_index == 0)
252 GOTO(out, rc = 0); /* no more records */
254 /* if set, process the callback on this record */
255 if (ext2_test_bit(index, llh->llh_bitmap)) {
256 rc = cb(loghandle, rec, data);
257 if (rc == LLOG_PROC_BREAK) {
258 CDEBUG(D_HA, "recovery from log: "LPX64":%x"
260 loghandle->lgh_id.lgl_oid,
261 loghandle->lgh_id.lgl_ogen);
263 } else if (rc == LLOG_DEL_RECORD) {
264 llog_cancel_rec(loghandle, rec->lrh_index);
270 /* next record, still in buffer? */
272 if (index > last_index)
274 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
280 OBD_FREE(buf, LLOG_CHUNK_SIZE);
283 EXPORT_SYMBOL(llog_process);
285 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
286 void *data, void *catdata)
288 struct llog_log_hdr *llh = loghandle->lgh_hdr;
289 struct llog_process_cat_data *cd = catdata;
291 int rc = 0, first_index = 1, index, idx;
292 struct llog_rec_tail *tail;
295 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
300 first_index = cd->first_idx + 1;
301 if (cd != NULL && cd->last_idx)
302 index = cd->last_idx;
304 index = LLOG_BITMAP_BYTES * 8 - 1;
307 struct llog_rec_hdr *rec;
309 /* skip records not set in bitmap */
310 while (index >= first_index &&
311 !ext2_test_bit(index, llh->llh_bitmap))
314 LASSERT(index >= first_index - 1);
315 if (index == first_index - 1)
318 /* get the buf with our target record; avoid old garbage */
319 memset(buf, 0, LLOG_CHUNK_SIZE);
320 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
325 idx = le32_to_cpu(rec->lrh_index);
327 CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
328 while (idx < index) {
329 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
333 /* process records in buffer, starting where we found one */
334 while ((void *)rec >= buf) {
335 if (rec->lrh_index == 0)
336 GOTO(out, 0); /* no more records */
338 /* if set, process the callback on this record */
339 if (ext2_test_bit(index, llh->llh_bitmap)) {
340 rc = cb(loghandle, rec, data);
341 if (rc == LLOG_PROC_BREAK) {
342 CWARN("recovery from log: "LPX64":%x"
344 loghandle->lgh_id.lgl_oid,
345 loghandle->lgh_id.lgl_ogen);
352 /* previous record, still in buffer? */
354 if (index < first_index)
357 if ((void *)rec == buf)
360 tail = (void *)rec - sizeof(struct llog_rec_tail);
361 rec = ((void *)rec - le32_to_cpu(tail->lrt_len));
367 OBD_FREE(buf, LLOG_CHUNK_SIZE);
370 EXPORT_SYMBOL(llog_reverse_process);