1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog.c
38 * OST<->MDS recovery logging infrastructure.
39 * Invariants in implementation:
40 * - we do not share logs among different OST<->MDS connections, so that
41 * if an OST or MDS fails it need only look at log(s) relevant to itself
43 * Author: Andreas Dilger <adilger@clusterfs.com>
46 #define DEBUG_SUBSYSTEM S_LOG
53 #include <liblustre.h>
56 #include <obd_class.h>
57 #include <lustre_log.h>
58 #include <libcfs/list.h>
59 #include "llog_internal.h"
61 /* Allocate a new log or catalog handle */
62 struct llog_handle *llog_alloc_handle(void)
64 struct llog_handle *loghandle;
67 OBD_ALLOC(loghandle, sizeof(*loghandle));
68 if (loghandle == NULL)
69 RETURN(ERR_PTR(-ENOMEM));
71 init_rwsem(&loghandle->lgh_lock);
75 EXPORT_SYMBOL(llog_alloc_handle);
78 void llog_free_handle(struct llog_handle *loghandle)
83 if (!loghandle->lgh_hdr)
85 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
86 list_del_init(&loghandle->u.phd.phd_entry);
87 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
88 LASSERT(list_empty(&loghandle->u.chd.chd_head));
89 OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
92 OBD_FREE(loghandle, sizeof(*loghandle));
94 EXPORT_SYMBOL(llog_free_handle);
96 /* returns negative on error; 0 if success; 1 if success & log destroyed */
97 int llog_cancel_rec(struct llog_handle *loghandle, int index)
99 struct llog_log_hdr *llh = loghandle->lgh_hdr;
103 CDEBUG(D_RPCTRACE, "Canceling %d in log "LPX64"\n",
104 index, loghandle->lgh_id.lgl_oid);
107 CERROR("Can't cancel index 0 which is header\n");
111 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
112 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
118 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
119 (llh->llh_count == 1) &&
120 (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
121 rc = llog_destroy(loghandle);
123 CERROR("Failure destroying log after last cancel: %d\n",
125 ext2_set_bit(index, llh->llh_bitmap);
133 rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
135 CERROR("Failure re-writing header %d\n", rc);
136 ext2_set_bit(index, llh->llh_bitmap);
141 EXPORT_SYMBOL(llog_cancel_rec);
143 int llog_init_handle(struct llog_handle *handle, int flags,
144 struct obd_uuid *uuid)
147 struct llog_log_hdr *llh;
149 LASSERT(handle->lgh_hdr == NULL);
151 OBD_ALLOC(llh, sizeof(*llh));
154 handle->lgh_hdr = llh;
155 /* first assign flags to use llog_client_ops */
156 llh->llh_flags = flags;
157 rc = llog_read_header(handle);
159 flags = llh->llh_flags;
160 if (uuid && !obd_uuid_equals(uuid, &llh->llh_tgtuuid)) {
161 CERROR("uuid mismatch: %s/%s\n", (char *)uuid->uuid,
162 (char *)llh->llh_tgtuuid.uuid);
166 } else if (rc != LLOG_EEMPTY || !flags) {
167 /* set a pesudo flag for initialization */
168 flags = LLOG_F_IS_CAT;
173 handle->lgh_last_idx = 0; /* header is record with index 0 */
174 llh->llh_count = 1; /* for the header record */
175 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
176 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
177 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
178 llh->llh_timestamp = cfs_time_current_sec();
180 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
181 llh->llh_bitmap_offset = offsetof(typeof(*llh),llh_bitmap);
182 ext2_set_bit(0, llh->llh_bitmap);
185 if (flags & LLOG_F_IS_CAT) {
186 CFS_INIT_LIST_HEAD(&handle->u.chd.chd_head);
187 llh->llh_size = sizeof(struct llog_logid_rec);
188 } else if (flags & LLOG_F_IS_PLAIN) {
189 CFS_INIT_LIST_HEAD(&handle->u.phd.phd_entry);
191 CERROR("Unknown flags: %#x (Expected %#x or %#x\n",
192 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
197 OBD_FREE(llh, sizeof(*llh));
198 handle->lgh_hdr = NULL;
202 EXPORT_SYMBOL(llog_init_handle);
204 int llog_close(struct llog_handle *loghandle)
206 struct llog_operations *lop;
210 rc = llog_handle2ops(loghandle, &lop);
213 if (lop->lop_close == NULL)
214 GOTO(out, -EOPNOTSUPP);
215 rc = lop->lop_close(loghandle);
217 llog_free_handle(loghandle);
220 EXPORT_SYMBOL(llog_close);
222 static int llog_process_thread(void *arg)
224 struct llog_process_info *lpi = (struct llog_process_info *)arg;
225 struct llog_handle *loghandle = lpi->lpi_loghandle;
226 struct llog_log_hdr *llh = loghandle->lgh_hdr;
227 struct llog_process_cat_data *cd = lpi->lpi_catdata;
229 __u64 cur_offset = LLOG_CHUNK_SIZE;
231 int rc = 0, index = 1, last_index;
232 int saved_index = 0, last_called_index = 0;
236 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
238 lpi->lpi_rc = -ENOMEM;
240 complete(&lpi->lpi_completion);
245 cfs_daemonize_ctxt("llog_process_thread");
248 last_called_index = cd->lpcd_first_idx;
249 index = cd->lpcd_first_idx + 1;
251 if (cd != NULL && cd->lpcd_last_idx)
252 last_index = cd->lpcd_last_idx;
254 last_index = LLOG_BITMAP_BYTES * 8 - 1;
257 struct llog_rec_hdr *rec;
259 /* skip records not set in bitmap */
260 while (index <= last_index &&
261 !ext2_test_bit(index, llh->llh_bitmap))
264 LASSERT(index <= last_index + 1);
265 if (index == last_index + 1)
268 CDEBUG(D_OTHER, "index: %d last_index %d\n",
271 /* get the buf with our target record; avoid old garbage */
272 memset(buf, 0, LLOG_CHUNK_SIZE);
273 last_offset = cur_offset;
274 rc = llog_next_block(loghandle, &saved_index, index,
275 &cur_offset, buf, LLOG_CHUNK_SIZE);
279 /* NB: when rec->lrh_len is accessed it is already swabbed
280 * since it is used at the "end" of the loop and the rec
281 * swabbing is done at the beginning of the loop. */
282 for (rec = (struct llog_rec_hdr *)buf;
283 (char *)rec < buf + LLOG_CHUNK_SIZE;
284 rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
286 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
289 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
290 lustre_swab_llog_rec(rec, NULL);
292 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
293 rec->lrh_type, rec->lrh_index);
295 if (rec->lrh_index == 0)
296 GOTO(out, 0); /* no more records */
298 if (rec->lrh_len == 0 || rec->lrh_len >LLOG_CHUNK_SIZE){
299 CWARN("invalid length %d in llog record for "
300 "index %d/%d\n", rec->lrh_len,
301 rec->lrh_index, index);
302 GOTO(out, rc = -EINVAL);
305 if (rec->lrh_index < index) {
306 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
312 "lrh_index: %d lrh_len: %d (%d remains)\n",
313 rec->lrh_index, rec->lrh_len,
314 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
316 loghandle->lgh_cur_idx = rec->lrh_index;
317 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
320 /* if set, process the callback on this record */
321 if (ext2_test_bit(index, llh->llh_bitmap)) {
322 rc = lpi->lpi_cb(loghandle, rec,
324 last_called_index = index;
325 if (rc == LLOG_PROC_BREAK) {
327 } else if (rc == LLOG_DEL_RECORD) {
328 llog_cancel_rec(loghandle,
335 CDEBUG(D_OTHER, "Skipped index %d\n", index);
338 /* next record, still in buffer? */
340 if (index > last_index)
347 cd->lpcd_last_idx = last_called_index;
349 OBD_FREE(buf, LLOG_CHUNK_SIZE);
352 complete(&lpi->lpi_completion);
357 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
358 void *data, void *catdata)
360 struct llog_process_info *lpi;
366 CERROR("cannot alloc pointer\n");
369 lpi->lpi_loghandle = loghandle;
371 lpi->lpi_cbdata = data;
372 lpi->lpi_catdata = catdata;
375 init_completion(&lpi->lpi_completion);
376 rc = cfs_kernel_thread(llog_process_thread, lpi, CLONE_VM | CLONE_FILES);
378 CERROR("cannot start thread: %d\n", rc);
382 wait_for_completion(&lpi->lpi_completion);
384 llog_process_thread(lpi);
390 EXPORT_SYMBOL(llog_process);
392 inline int llog_get_size(struct llog_handle *loghandle)
394 if (loghandle && loghandle->lgh_hdr)
395 return loghandle->lgh_hdr->llh_count;
398 EXPORT_SYMBOL(llog_get_size);
400 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
401 void *data, void *catdata)
403 struct llog_log_hdr *llh = loghandle->lgh_hdr;
404 struct llog_process_cat_data *cd = catdata;
406 int rc = 0, first_index = 1, index, idx;
409 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
414 first_index = cd->lpcd_first_idx + 1;
415 if (cd != NULL && cd->lpcd_last_idx)
416 index = cd->lpcd_last_idx;
418 index = LLOG_BITMAP_BYTES * 8 - 1;
421 struct llog_rec_hdr *rec;
422 struct llog_rec_tail *tail;
424 /* skip records not set in bitmap */
425 while (index >= first_index &&
426 !ext2_test_bit(index, llh->llh_bitmap))
429 LASSERT(index >= first_index - 1);
430 if (index == first_index - 1)
433 /* get the buf with our target record; avoid old garbage */
434 memset(buf, 0, LLOG_CHUNK_SIZE);
435 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
440 idx = le32_to_cpu(rec->lrh_index);
442 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
443 while (idx < index) {
444 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
447 tail = (void *)rec + le32_to_cpu(rec->lrh_len) - sizeof(*tail);
449 /* process records in buffer, starting where we found one */
450 while ((void *)tail > buf) {
451 rec = (void *)tail - le32_to_cpu(tail->lrt_len) +
454 if (rec->lrh_index == 0)
455 GOTO(out, 0); /* no more records */
457 /* if set, process the callback on this record */
458 if (ext2_test_bit(index, llh->llh_bitmap)) {
459 rc = cb(loghandle, rec, data);
460 if (rc == LLOG_PROC_BREAK) {
467 /* previous record, still in buffer? */
469 if (index < first_index)
471 tail = (void *)rec - sizeof(*tail);
477 OBD_FREE(buf, LLOG_CHUNK_SIZE);
480 EXPORT_SYMBOL(llog_reverse_process);