4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdclass/llog.c
36 * OST<->MDS recovery logging infrastructure.
37 * Invariants in implementation:
38 * - we do not share logs among different OST<->MDS connections, so that
39 * if an OST or MDS fails it need only look at log(s) relevant to itself
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
43 * Author: Mikhail Pershin <tappro@whamcloud.com>
46 #define DEBUG_SUBSYSTEM S_LOG
49 #include <liblustre.h>
52 #include <obd_class.h>
53 #include <lustre_log.h>
54 #include "llog_internal.h"
56 /* Allocate a new log or catalog handle */
57 struct llog_handle *llog_alloc_handle(void)
59 struct llog_handle *loghandle;
62 OBD_ALLOC_PTR(loghandle);
63 if (loghandle == NULL)
64 RETURN(ERR_PTR(-ENOMEM));
66 cfs_init_rwsem(&loghandle->lgh_lock);
67 cfs_spin_lock_init(&loghandle->lgh_hdr_lock);
68 CFS_INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
72 EXPORT_SYMBOL(llog_alloc_handle);
75 void llog_free_handle(struct llog_handle *loghandle)
80 if (!loghandle->lgh_hdr)
82 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
83 cfs_list_del_init(&loghandle->u.phd.phd_entry);
84 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
85 LASSERT(cfs_list_empty(&loghandle->u.chd.chd_head));
86 LASSERT(sizeof(*(loghandle->lgh_hdr)) == LLOG_CHUNK_SIZE);
87 OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
90 OBD_FREE_PTR(loghandle);
92 EXPORT_SYMBOL(llog_free_handle);
94 /* returns negative on error; 0 if success; 1 if success & log destroyed */
95 int llog_cancel_rec(struct llog_handle *loghandle, int index)
97 struct llog_log_hdr *llh = loghandle->lgh_hdr;
101 CDEBUG(D_RPCTRACE, "Canceling %d in log "LPX64"\n",
102 index, loghandle->lgh_id.lgl_oid);
105 CERROR("Can't cancel index 0 which is header\n");
109 cfs_spin_lock(&loghandle->lgh_hdr_lock);
110 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
111 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
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 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
122 rc = llog_destroy(loghandle);
124 CERROR("%s: can't destroy empty llog #"LPX64"#"LPX64
126 loghandle->lgh_ctxt->loc_obd->obd_name,
127 loghandle->lgh_id.lgl_oid,
128 loghandle->lgh_id.lgl_oseq,
129 loghandle->lgh_id.lgl_ogen, rc);
134 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
136 rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
138 CERROR("%s: fail to write header for llog #"LPX64"#"LPX64
140 loghandle->lgh_ctxt->loc_obd->obd_name,
141 loghandle->lgh_id.lgl_oid,
142 loghandle->lgh_id.lgl_oseq,
143 loghandle->lgh_id.lgl_ogen, rc);
148 cfs_spin_lock(&loghandle->lgh_hdr_lock);
149 ext2_set_bit(index, llh->llh_bitmap);
151 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
154 EXPORT_SYMBOL(llog_cancel_rec);
156 int llog_init_handle(struct llog_handle *handle, int flags,
157 struct obd_uuid *uuid)
160 struct llog_log_hdr *llh;
162 LASSERT(handle->lgh_hdr == NULL);
167 handle->lgh_hdr = llh;
168 /* first assign flags to use llog_client_ops */
169 llh->llh_flags = flags;
170 rc = llog_read_header(handle);
172 flags = llh->llh_flags;
173 if (uuid && !obd_uuid_equals(uuid, &llh->llh_tgtuuid)) {
174 CERROR("uuid mismatch: %s/%s\n", (char *)uuid->uuid,
175 (char *)llh->llh_tgtuuid.uuid);
179 } else if (rc != LLOG_EEMPTY || !flags) {
180 /* set a pesudo flag for initialization */
181 flags = LLOG_F_IS_CAT;
186 handle->lgh_last_idx = 0; /* header is record with index 0 */
187 llh->llh_count = 1; /* for the header record */
188 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
189 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
190 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
191 llh->llh_timestamp = cfs_time_current_sec();
193 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
194 llh->llh_bitmap_offset = offsetof(typeof(*llh),llh_bitmap);
195 ext2_set_bit(0, llh->llh_bitmap);
198 if (flags & LLOG_F_IS_CAT) {
199 LASSERT(cfs_list_empty(&handle->u.chd.chd_head));
200 CFS_INIT_LIST_HEAD(&handle->u.chd.chd_head);
201 llh->llh_size = sizeof(struct llog_logid_rec);
202 } else if (!(flags & LLOG_F_IS_PLAIN)) {
203 CERROR("Unknown flags: %#x (Expected %#x or %#x\n",
204 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
210 handle->lgh_hdr = NULL;
214 EXPORT_SYMBOL(llog_init_handle);
216 int llog_close(struct llog_handle *loghandle)
218 struct llog_operations *lop;
222 rc = llog_handle2ops(loghandle, &lop);
225 if (lop->lop_close == NULL)
226 GOTO(out, -EOPNOTSUPP);
227 rc = lop->lop_close(loghandle);
229 llog_free_handle(loghandle);
232 EXPORT_SYMBOL(llog_close);
234 static int llog_process_thread(void *arg)
236 struct llog_process_info *lpi = (struct llog_process_info *)arg;
237 struct llog_handle *loghandle = lpi->lpi_loghandle;
238 struct llog_log_hdr *llh = loghandle->lgh_hdr;
239 struct llog_process_cat_data *cd = lpi->lpi_catdata;
241 __u64 cur_offset = LLOG_CHUNK_SIZE;
243 int rc = 0, index = 1, last_index;
244 int saved_index = 0, last_called_index = 0;
248 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
250 lpi->lpi_rc = -ENOMEM;
252 cfs_complete(&lpi->lpi_completion);
257 if (!(lpi->lpi_flags & LLOG_FLAG_NODEAMON))
258 cfs_daemonize_ctxt("llog_process_thread");
261 last_called_index = cd->lpcd_first_idx;
262 index = cd->lpcd_first_idx + 1;
264 if (cd != NULL && cd->lpcd_last_idx)
265 last_index = cd->lpcd_last_idx;
267 last_index = LLOG_BITMAP_BYTES * 8 - 1;
270 struct llog_rec_hdr *rec;
272 /* skip records not set in bitmap */
273 while (index <= last_index &&
274 !ext2_test_bit(index, llh->llh_bitmap))
277 LASSERT(index <= last_index + 1);
278 if (index == last_index + 1)
281 CDEBUG(D_OTHER, "index: %d last_index %d\n",
284 /* get the buf with our target record; avoid old garbage */
285 memset(buf, 0, LLOG_CHUNK_SIZE);
286 last_offset = cur_offset;
287 rc = llog_next_block(loghandle, &saved_index, index,
288 &cur_offset, buf, LLOG_CHUNK_SIZE);
292 /* NB: when rec->lrh_len is accessed it is already swabbed
293 * since it is used at the "end" of the loop and the rec
294 * swabbing is done at the beginning of the loop. */
295 for (rec = (struct llog_rec_hdr *)buf;
296 (char *)rec < buf + LLOG_CHUNK_SIZE;
297 rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
299 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
302 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
303 lustre_swab_llog_rec(rec, NULL);
305 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
306 rec->lrh_type, rec->lrh_index);
308 if (rec->lrh_index == 0)
309 GOTO(out, 0); /* no more records */
311 if (rec->lrh_len == 0 || rec->lrh_len >LLOG_CHUNK_SIZE){
312 CWARN("invalid length %d in llog record for "
313 "index %d/%d\n", rec->lrh_len,
314 rec->lrh_index, index);
315 GOTO(out, rc = -EINVAL);
318 if (rec->lrh_index < index) {
319 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
325 "lrh_index: %d lrh_len: %d (%d remains)\n",
326 rec->lrh_index, rec->lrh_len,
327 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
329 loghandle->lgh_cur_idx = rec->lrh_index;
330 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
333 /* if set, process the callback on this record */
334 if (ext2_test_bit(index, llh->llh_bitmap)) {
335 rc = lpi->lpi_cb(loghandle, rec,
337 last_called_index = index;
338 if (rc == LLOG_PROC_BREAK) {
340 } else if (rc == LLOG_DEL_RECORD) {
341 llog_cancel_rec(loghandle,
348 CDEBUG(D_OTHER, "Skipped index %d\n", index);
351 /* next record, still in buffer? */
353 if (index > last_index)
360 cd->lpcd_last_idx = last_called_index;
362 OBD_FREE(buf, LLOG_CHUNK_SIZE);
365 cfs_complete(&lpi->lpi_completion);
370 int llog_process_flags(struct llog_handle *loghandle, llog_cb_t cb,
371 void *data, void *catdata, int flags)
373 struct llog_process_info *lpi;
379 CERROR("cannot alloc pointer\n");
382 lpi->lpi_loghandle = loghandle;
384 lpi->lpi_cbdata = data;
385 lpi->lpi_catdata = catdata;
386 lpi->lpi_flags = flags;
389 cfs_init_completion(&lpi->lpi_completion);
390 rc = cfs_create_thread(llog_process_thread, lpi, CFS_DAEMON_FLAGS);
392 CERROR("cannot start thread: %d\n", rc);
396 cfs_wait_for_completion(&lpi->lpi_completion);
398 llog_process_thread(lpi);
404 EXPORT_SYMBOL(llog_process_flags);
406 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
407 void *data, void *catdata)
409 return llog_process_flags(loghandle, cb, data, catdata, 0);
411 EXPORT_SYMBOL(llog_process);
413 inline int llog_get_size(struct llog_handle *loghandle)
415 if (loghandle && loghandle->lgh_hdr)
416 return loghandle->lgh_hdr->llh_count;
419 EXPORT_SYMBOL(llog_get_size);
421 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
422 void *data, void *catdata)
424 struct llog_log_hdr *llh = loghandle->lgh_hdr;
425 struct llog_process_cat_data *cd = catdata;
427 int rc = 0, first_index = 1, index, idx;
430 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
435 first_index = cd->lpcd_first_idx + 1;
436 if (cd != NULL && cd->lpcd_last_idx)
437 index = cd->lpcd_last_idx;
439 index = LLOG_BITMAP_BYTES * 8 - 1;
442 struct llog_rec_hdr *rec;
443 struct llog_rec_tail *tail;
445 /* skip records not set in bitmap */
446 while (index >= first_index &&
447 !ext2_test_bit(index, llh->llh_bitmap))
450 LASSERT(index >= first_index - 1);
451 if (index == first_index - 1)
454 /* get the buf with our target record; avoid old garbage */
455 memset(buf, 0, LLOG_CHUNK_SIZE);
456 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
461 idx = le32_to_cpu(rec->lrh_index);
463 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
464 while (idx < index) {
465 rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
468 tail = (void *)rec + le32_to_cpu(rec->lrh_len) - sizeof(*tail);
470 /* process records in buffer, starting where we found one */
471 while ((void *)tail > buf) {
472 rec = (void *)tail - le32_to_cpu(tail->lrt_len) +
475 if (rec->lrh_index == 0)
476 GOTO(out, 0); /* no more records */
478 /* if set, process the callback on this record */
479 if (ext2_test_bit(index, llh->llh_bitmap)) {
480 rc = cb(loghandle, rec, data);
481 if (rc == LLOG_PROC_BREAK) {
488 /* previous record, still in buffer? */
490 if (index < first_index)
492 tail = (void *)rec - sizeof(*tail);
498 OBD_FREE(buf, LLOG_CHUNK_SIZE);
501 EXPORT_SYMBOL(llog_reverse_process);