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/obd_class.h>
42 #include <linux/lustre_log.h>
43 #include <libcfs/list.h>
45 /* Allocate a new log or catalog handle */
46 struct llog_handle *llog_alloc_handle(void)
48 struct llog_handle *loghandle;
51 OBD_ALLOC(loghandle, sizeof(*loghandle));
52 if (loghandle == NULL)
53 RETURN(ERR_PTR(-ENOMEM));
55 init_rwsem(&loghandle->lgh_lock);
59 EXPORT_SYMBOL(llog_alloc_handle);
62 void llog_free_handle(struct llog_handle *loghandle)
67 if (!loghandle->lgh_hdr)
69 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
70 list_del_init(&loghandle->u.phd.phd_entry);
71 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
72 LASSERT(list_empty(&loghandle->u.chd.chd_head));
73 OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
76 OBD_FREE(loghandle, sizeof(*loghandle));
78 EXPORT_SYMBOL(llog_free_handle);
80 /* returns negative on error; 0 if success; 1 if success & log destroyed */
81 int llog_cancel_rec(struct llog_handle *loghandle, int index)
83 struct llog_log_hdr *llh = loghandle->lgh_hdr;
87 CDEBUG(D_HA, "canceling %d in log "LPX64"\n",
88 index, loghandle->lgh_id.lgl_oid);
91 CERROR("cannot cancel index 0 (which is header)\n");
95 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
96 CERROR("catalog index %u already clear?\n", index);
102 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
103 (llh->llh_count == 1) &&
104 (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
105 rc = llog_destroy(loghandle);
107 CERROR("failure destroying log after last cancel: %d\n",
109 ext2_set_bit(index, llh->llh_bitmap);
117 rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
119 CERROR("failure re-writing header %d\n", rc);
120 ext2_set_bit(index, llh->llh_bitmap);
125 EXPORT_SYMBOL(llog_cancel_rec);
127 int llog_init_handle(struct llog_handle *handle, int flags,
128 struct obd_uuid *uuid)
131 struct llog_log_hdr *llh;
133 LASSERT(handle->lgh_hdr == NULL);
135 OBD_ALLOC(llh, sizeof(*llh));
138 handle->lgh_hdr = llh;
139 /* first assign flags to use llog_client_ops */
140 llh->llh_flags = flags;
141 rc = llog_read_header(handle);
143 flags = llh->llh_flags;
145 LASSERT(obd_uuid_equals(uuid, &llh->llh_tgtuuid));
147 } else if (rc != LLOG_EEMPTY || !flags) {
148 /* set a pesudo flag for initialization */
149 flags = LLOG_F_IS_CAT;
154 handle->lgh_last_idx = 0; /* header is record with index 0 */
155 llh->llh_count = 1; /* for the header record */
156 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
157 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
158 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
159 llh->llh_timestamp = CURRENT_SECONDS;
161 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
162 llh->llh_bitmap_offset = offsetof(typeof(*llh),llh_bitmap);
163 ext2_set_bit(0, llh->llh_bitmap);
166 if (flags & LLOG_F_IS_CAT) {
167 INIT_LIST_HEAD(&handle->u.chd.chd_head);
168 llh->llh_size = sizeof(struct llog_logid_rec);
170 else if (flags & LLOG_F_IS_PLAIN)
171 INIT_LIST_HEAD(&handle->u.phd.phd_entry);
176 OBD_FREE(llh, sizeof(*llh));
177 handle->lgh_hdr = NULL;
181 EXPORT_SYMBOL(llog_init_handle);
183 int llog_close(struct llog_handle *loghandle)
185 struct llog_operations *lop;
189 rc = llog_handle2ops(loghandle, &lop);
192 if (lop->lop_close == NULL)
193 GOTO(out, -EOPNOTSUPP);
194 rc = lop->lop_close(loghandle);
196 llog_free_handle(loghandle);
199 EXPORT_SYMBOL(llog_close);
201 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
202 void *data, void *catdata)
204 struct llog_log_hdr *llh = loghandle->lgh_hdr;
205 struct llog_process_cat_data *cd = catdata;
207 __u64 cur_offset = LLOG_CHUNK_SIZE;
208 int rc = 0, index = 1, last_index, idx;
212 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
217 index = cd->first_idx + 1;
218 if (cd != NULL && cd->last_idx)
219 last_index = cd->last_idx;
221 last_index = LLOG_BITMAP_BYTES * 8 - 1;
224 struct llog_rec_hdr *rec;
226 /* skip records not set in bitmap */
227 while (index <= last_index &&
228 !ext2_test_bit(index, llh->llh_bitmap))
231 LASSERT(index <= last_index + 1);
232 if (index == last_index + 1)
235 /* get the buf with our target record; avoid old garbage */
236 memset(buf, 0, LLOG_CHUNK_SIZE);
237 rc = llog_next_block(loghandle, &saved_index, index,
238 &cur_offset, buf, LLOG_CHUNK_SIZE);
243 idx = rec->lrh_index;
245 CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
246 while (idx < index) {
247 rec = (struct llog_rec_hdr *)
248 ((char *)rec + rec->lrh_len);
252 /* process records in buffer, starting where we found one */
253 while ((void *)rec < buf + LLOG_CHUNK_SIZE) {
254 if (rec->lrh_index == 0)
255 GOTO(out, 0); /* no more records */
257 /* if set, process the callback on this record */
258 if (ext2_test_bit(index, llh->llh_bitmap)) {
259 rc = cb(loghandle, rec, data);
260 if (rc == LLOG_PROC_BREAK) {
261 CWARN("recovery from log: "LPX64":%x"
263 loghandle->lgh_id.lgl_oid,
264 loghandle->lgh_id.lgl_ogen);
271 /* next record, still in buffer? */
273 if (index > last_index)
275 rec = (struct llog_rec_hdr *)
276 ((char *)rec + rec->lrh_len);
282 OBD_FREE(buf, LLOG_CHUNK_SIZE);
285 EXPORT_SYMBOL(llog_process);