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",
110 rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
112 CERROR("failure re-writing header %d\n", rc);
116 EXPORT_SYMBOL(llog_cancel_rec);
118 int llog_init_handle(struct llog_handle *handle, int flags,
119 struct obd_uuid *uuid)
122 struct llog_log_hdr *llh;
124 LASSERT(handle->lgh_hdr == NULL);
126 OBD_ALLOC(llh, sizeof(*llh));
129 handle->lgh_hdr = llh;
130 /* first assign flags to use llog_client_ops */
131 llh->llh_flags = cpu_to_le32(flags);
132 rc = llog_read_header(handle);
134 flags = le32_to_cpu(llh->llh_flags);
136 LASSERT(obd_uuid_equals(uuid, &llh->llh_tgtuuid));
138 } else if (rc != LLOG_EEMPTY || !flags) {
139 /* set a pesudo flag for initialization */
140 flags = LLOG_F_IS_CAT;
145 handle->lgh_last_idx = 0; /* header is record with index 0 */
146 llh->llh_count = cpu_to_le32(1); /* for the header record */
147 llh->llh_hdr.lrh_type = cpu_to_le32(LLOG_HDR_MAGIC);
148 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len =
149 cpu_to_le32(LLOG_CHUNK_SIZE);
150 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
151 llh->llh_timestamp = cpu_to_le64(LTIME_S(CURRENT_TIME));
153 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
154 llh->llh_bitmap_offset = cpu_to_le32(offsetof(typeof(*llh),llh_bitmap));
155 ext2_set_bit(0, llh->llh_bitmap);
158 if (flags & LLOG_F_IS_CAT) {
159 INIT_LIST_HEAD(&handle->u.chd.chd_head);
160 llh->llh_size = cpu_to_le32(sizeof(struct llog_logid_rec));
162 else if (flags & LLOG_F_IS_PLAIN)
163 INIT_LIST_HEAD(&handle->u.phd.phd_entry);
168 OBD_FREE(llh, sizeof(*llh));
169 handle->lgh_hdr = NULL;
173 EXPORT_SYMBOL(llog_init_handle);
175 int llog_close(struct llog_handle *loghandle)
177 struct llog_operations *lop;
181 rc = llog_handle2ops(loghandle, &lop);
184 if (lop->lop_close == NULL)
185 GOTO(out, -EOPNOTSUPP);
186 rc = lop->lop_close(loghandle);
188 llog_free_handle(loghandle);
191 EXPORT_SYMBOL(llog_close);
193 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
194 void *data, void *catdata)
196 struct llog_log_hdr *llh = loghandle->lgh_hdr;
197 struct llog_process_cat_data *cd = catdata;
199 __u64 cur_offset = LLOG_CHUNK_SIZE;
200 int rc = 0, index = 1, last_index, idx;
204 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
209 index = cd->first_idx + 1;
210 if (cd != NULL && cd->last_idx)
211 last_index = cd->last_idx;
213 last_index = LLOG_BITMAP_BYTES * 8 - 1;
216 struct llog_rec_hdr *rec;
218 /* skip records not set in bitmap */
219 while (index <= last_index &&
220 !ext2_test_bit(index, llh->llh_bitmap))
223 LASSERT(index <= last_index + 1);
224 if (index == last_index + 1)
227 /* get the buf with our target record; avoid old garbage */
228 memset(buf, 0, LLOG_CHUNK_SIZE);
229 rc = llog_next_block(loghandle, &saved_index, index,
230 &cur_offset, buf, LLOG_CHUNK_SIZE);
235 idx = le32_to_cpu(rec->lrh_index);
237 CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
238 while (idx < index) {
239 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
243 /* process records in buffer, starting where we found one */
244 while ((void *)rec < buf + LLOG_CHUNK_SIZE) {
245 if (rec->lrh_index == 0)
246 GOTO(out, rc = 0); /* no more records */
248 /* if set, process the callback on this record */
249 if (ext2_test_bit(index, llh->llh_bitmap)) {
250 rc = cb(loghandle, rec, data);
251 if (rc == LLOG_PROC_BREAK) {
252 CWARN("recovery from log: "LPX64":%x"
254 loghandle->lgh_id.lgl_oid,
255 loghandle->lgh_id.lgl_ogen);
257 } else if (rc == LLOG_DEL_RECORD) {
258 llog_cancel_rec(loghandle, rec->lrh_index);
264 /* next record, still in buffer? */
266 if (index > last_index)
268 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
274 OBD_FREE(buf, LLOG_CHUNK_SIZE);
277 EXPORT_SYMBOL(llog_process);
279 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
280 void *data, void *catdata)
282 struct llog_log_hdr *llh = loghandle->lgh_hdr;
283 struct llog_process_cat_data *cd = catdata;
285 int rc = 0, first_index = 1, index, idx;
286 struct llog_rec_tail *tail;
289 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
294 first_index = cd->first_idx + 1;
295 if (cd != NULL && cd->last_idx)
296 index = cd->last_idx;
298 index = LLOG_BITMAP_BYTES * 8 - 1;
301 struct llog_rec_hdr *rec;
303 /* skip records not set in bitmap */
304 while (index >= first_index &&
305 !ext2_test_bit(index, llh->llh_bitmap))
308 LASSERT(index >= first_index - 1);
309 if (index == first_index - 1)
312 /* get the buf with our target record; avoid old garbage */
313 memset(buf, 0, LLOG_CHUNK_SIZE);
314 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
319 idx = le32_to_cpu(rec->lrh_index);
321 CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
322 while (idx < index) {
323 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
327 /* process records in buffer, starting where we found one */
328 while ((void *)rec >= buf) {
329 if (rec->lrh_index == 0)
330 GOTO(out, 0); /* no more records */
332 /* if set, process the callback on this record */
333 if (ext2_test_bit(index, llh->llh_bitmap)) {
334 rc = cb(loghandle, rec, data);
335 if (rc == LLOG_PROC_BREAK) {
336 CWARN("recovery from log: "LPX64":%x"
338 loghandle->lgh_id.lgl_oid,
339 loghandle->lgh_id.lgl_ogen);
346 /* previous record, still in buffer? */
348 if (index < first_index)
351 if ((void *)rec == buf)
354 tail = (void *)rec - sizeof(struct llog_rec_tail);
355 rec = ((void *)rec - le32_to_cpu(tail->lrt_len));
361 OBD_FREE(buf, LLOG_CHUNK_SIZE);
364 EXPORT_SYMBOL(llog_reverse_process);