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.
30 * Copyright (c) 2012, 2013, Intel Corporation.
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>
44 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
45 * Author: Mikhail Pershin <tappro@whamcloud.com>
48 #define DEBUG_SUBSYSTEM S_LOG
51 #include <obd_class.h>
52 #include <lustre_log.h>
53 #include "llog_internal.h"
56 * Allocate a new log or catalog handle
57 * Used inside llog_open().
59 struct llog_handle *llog_alloc_handle(void)
61 struct llog_handle *loghandle;
63 OBD_ALLOC_PTR(loghandle);
64 if (loghandle == NULL)
67 init_rwsem(&loghandle->lgh_lock);
68 spin_lock_init(&loghandle->lgh_hdr_lock);
69 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
70 atomic_set(&loghandle->lgh_refcount, 1);
76 * Free llog handle and header data if exists. Used in llog_close() only
78 void llog_free_handle(struct llog_handle *loghandle)
80 LASSERT(loghandle != NULL);
82 /* failed llog_init_handle */
83 if (!loghandle->lgh_hdr)
86 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
87 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
88 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
89 LASSERT(list_empty(&loghandle->u.chd.chd_head));
90 LASSERT(sizeof(*(loghandle->lgh_hdr)) == LLOG_CHUNK_SIZE);
91 OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
93 OBD_FREE_PTR(loghandle);
96 void llog_handle_get(struct llog_handle *loghandle)
98 atomic_inc(&loghandle->lgh_refcount);
101 void llog_handle_put(struct llog_handle *loghandle)
103 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
104 if (atomic_dec_and_test(&loghandle->lgh_refcount))
105 llog_free_handle(loghandle);
108 /* returns negative on error; 0 if success; 1 if success & log destroyed */
109 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
112 struct llog_log_hdr *llh = loghandle->lgh_hdr;
116 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n",
117 index, POSTID(&loghandle->lgh_id.lgl_oi));
120 CERROR("Can't cancel index 0 which is header\n");
124 spin_lock(&loghandle->lgh_hdr_lock);
125 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
126 spin_unlock(&loghandle->lgh_hdr_lock);
127 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
133 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
134 (llh->llh_count == 1) &&
135 (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
136 spin_unlock(&loghandle->lgh_hdr_lock);
137 rc = llog_destroy(env, loghandle);
139 CERROR("%s: can't destroy empty llog #"DOSTID
141 loghandle->lgh_ctxt->loc_obd->obd_name,
142 POSTID(&loghandle->lgh_id.lgl_oi),
143 loghandle->lgh_id.lgl_ogen, rc);
146 RETURN(LLOG_DEL_PLAIN);
148 spin_unlock(&loghandle->lgh_hdr_lock);
150 rc = llog_write(env, loghandle, &llh->llh_hdr, LLOG_HEADER_IDX);
152 CERROR("%s: fail to write header for llog #"DOSTID
154 loghandle->lgh_ctxt->loc_obd->obd_name,
155 POSTID(&loghandle->lgh_id.lgl_oi),
156 loghandle->lgh_id.lgl_ogen, rc);
161 spin_lock(&loghandle->lgh_hdr_lock);
162 ext2_set_bit(index, llh->llh_bitmap);
164 spin_unlock(&loghandle->lgh_hdr_lock);
167 EXPORT_SYMBOL(llog_cancel_rec);
169 static int llog_read_header(const struct lu_env *env,
170 struct llog_handle *handle,
171 struct obd_uuid *uuid)
173 struct llog_operations *lop;
176 rc = llog_handle2ops(handle, &lop);
180 if (lop->lop_read_header == NULL)
183 rc = lop->lop_read_header(env, handle);
184 if (rc == LLOG_EEMPTY) {
185 struct llog_log_hdr *llh = handle->lgh_hdr;
187 handle->lgh_last_idx = 0; /* header is record with index 0 */
188 llh->llh_count = 1; /* for the header record */
189 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
190 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
191 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
192 llh->llh_timestamp = cfs_time_current_sec();
194 memcpy(&llh->llh_tgtuuid, uuid,
195 sizeof(llh->llh_tgtuuid));
196 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
197 ext2_set_bit(0, llh->llh_bitmap);
203 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
204 int flags, struct obd_uuid *uuid)
206 struct llog_log_hdr *llh;
210 LASSERT(handle->lgh_hdr == NULL);
215 handle->lgh_hdr = llh;
216 /* first assign flags to use llog_client_ops */
217 llh->llh_flags = flags;
218 rc = llog_read_header(env, handle, uuid);
220 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
221 flags & LLOG_F_IS_CAT) ||
222 (llh->llh_flags & LLOG_F_IS_CAT &&
223 flags & LLOG_F_IS_PLAIN))) {
224 CERROR("%s: llog type is %s but initializing %s\n",
225 handle->lgh_ctxt->loc_obd->obd_name,
226 llh->llh_flags & LLOG_F_IS_CAT ?
228 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
229 GOTO(out, rc = -EINVAL);
230 } else if (llh->llh_flags &
231 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
233 * it is possible to open llog without specifying llog
234 * type so it is taken from llh_flags
236 flags = llh->llh_flags;
238 /* for some reason the llh_flags has no type set */
239 CERROR("llog type is not specified!\n");
240 GOTO(out, rc = -EINVAL);
243 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
244 CERROR("%s: llog uuid mismatch: %s/%s\n",
245 handle->lgh_ctxt->loc_obd->obd_name,
247 (char *)llh->llh_tgtuuid.uuid);
248 GOTO(out, rc = -EEXIST);
251 if (flags & LLOG_F_IS_CAT) {
252 LASSERT(list_empty(&handle->u.chd.chd_head));
253 INIT_LIST_HEAD(&handle->u.chd.chd_head);
254 llh->llh_size = sizeof(struct llog_logid_rec);
255 } else if (!(flags & LLOG_F_IS_PLAIN)) {
256 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
257 handle->lgh_ctxt->loc_obd->obd_name,
258 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
264 handle->lgh_hdr = NULL;
268 EXPORT_SYMBOL(llog_init_handle);
270 static int llog_process_thread(void *arg)
272 struct llog_process_info *lpi = arg;
273 struct llog_handle *loghandle = lpi->lpi_loghandle;
274 struct llog_log_hdr *llh = loghandle->lgh_hdr;
275 struct llog_process_cat_data *cd = lpi->lpi_catdata;
277 __u64 cur_offset = LLOG_CHUNK_SIZE;
279 int rc = 0, index = 1, last_index;
281 int last_called_index = 0;
287 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
289 lpi->lpi_rc = -ENOMEM;
294 last_called_index = cd->lpcd_first_idx;
295 index = cd->lpcd_first_idx + 1;
297 if (cd != NULL && cd->lpcd_last_idx)
298 last_index = cd->lpcd_last_idx;
300 last_index = LLOG_BITMAP_BYTES * 8 - 1;
303 struct llog_rec_hdr *rec;
305 /* skip records not set in bitmap */
306 while (index <= last_index &&
307 !ext2_test_bit(index, llh->llh_bitmap))
310 LASSERT(index <= last_index + 1);
311 if (index == last_index + 1)
314 CDEBUG(D_OTHER, "index: %d last_index %d\n",
317 /* get the buf with our target record; avoid old garbage */
318 memset(buf, 0, LLOG_CHUNK_SIZE);
319 last_offset = cur_offset;
320 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
321 index, &cur_offset, buf, LLOG_CHUNK_SIZE);
325 /* NB: when rec->lrh_len is accessed it is already swabbed
326 * since it is used at the "end" of the loop and the rec
327 * swabbing is done at the beginning of the loop. */
328 for (rec = (struct llog_rec_hdr *)buf;
329 (char *)rec < buf + LLOG_CHUNK_SIZE;
330 rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
332 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
335 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
336 lustre_swab_llog_rec(rec);
338 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
339 rec->lrh_type, rec->lrh_index);
341 if (rec->lrh_index == 0) {
342 /* probably another rec just got added? */
343 if (index <= loghandle->lgh_last_idx)
344 GOTO(repeat, rc = 0);
345 GOTO(out, rc = 0); /* no more records */
347 if (rec->lrh_len == 0 ||
348 rec->lrh_len > LLOG_CHUNK_SIZE) {
349 CWARN("invalid length %d in llog record for "
350 "index %d/%d\n", rec->lrh_len,
351 rec->lrh_index, index);
352 GOTO(out, rc = -EINVAL);
355 if (rec->lrh_index < index) {
356 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
362 "lrh_index: %d lrh_len: %d (%d remains)\n",
363 rec->lrh_index, rec->lrh_len,
364 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
366 loghandle->lgh_cur_idx = rec->lrh_index;
367 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
370 /* if set, process the callback on this record */
371 if (ext2_test_bit(index, llh->llh_bitmap)) {
372 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
374 last_called_index = index;
375 if (rc == LLOG_PROC_BREAK) {
377 } else if (rc == LLOG_DEL_RECORD) {
378 rc = llog_cancel_rec(lpi->lpi_env,
385 CDEBUG(D_OTHER, "Skipped index %d\n", index);
388 /* next record, still in buffer? */
390 if (index > last_index)
397 cd->lpcd_last_idx = last_called_index;
399 if (unlikely(rc == -EIO)) {
400 /* something bad happened to the processing, probably I/O
401 * error or the log got corrupted..
402 * to be able to finally release the log we discard any
403 * remaining bits in the header */
404 CERROR("llog found corrupted\n");
405 while (index <= last_index) {
406 if (ext2_test_bit(index, llh->llh_bitmap) != 0)
407 llog_cancel_rec(lpi->lpi_env, loghandle, index);
413 OBD_FREE(buf, LLOG_CHUNK_SIZE);
418 static int llog_process_thread_daemonize(void *arg)
420 struct llog_process_info *lpi = arg;
426 /* client env has no keys, tags is just 0 */
427 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
432 rc = llog_process_thread(arg);
436 complete(&lpi->lpi_completion);
440 int llog_process_or_fork(const struct lu_env *env,
441 struct llog_handle *loghandle,
442 llog_cb_t cb, void *data, void *catdata, bool fork)
444 struct llog_process_info *lpi;
451 CERROR("cannot alloc pointer\n");
454 lpi->lpi_loghandle = loghandle;
456 lpi->lpi_cbdata = data;
457 lpi->lpi_catdata = catdata;
460 struct task_struct *task;
462 /* The new thread can't use parent env,
463 * init the new one in llog_process_thread_daemonize. */
465 init_completion(&lpi->lpi_completion);
466 task = kthread_run(llog_process_thread_daemonize, lpi,
467 "llog_process_thread");
470 CERROR("%s: cannot start thread: rc = %d\n",
471 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
474 wait_for_completion(&lpi->lpi_completion);
477 llog_process_thread(lpi);
485 EXPORT_SYMBOL(llog_process_or_fork);
487 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
488 llog_cb_t cb, void *data, void *catdata)
491 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
492 return rc == LLOG_DEL_PLAIN ? 0 : rc;
494 EXPORT_SYMBOL(llog_process);
496 int llog_reverse_process(const struct lu_env *env,
497 struct llog_handle *loghandle, llog_cb_t cb,
498 void *data, void *catdata)
500 struct llog_log_hdr *llh = loghandle->lgh_hdr;
501 struct llog_process_cat_data *cd = catdata;
503 int rc = 0, first_index = 1, index, idx;
506 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
511 first_index = cd->lpcd_first_idx + 1;
512 if (cd != NULL && cd->lpcd_last_idx)
513 index = cd->lpcd_last_idx;
515 index = LLOG_BITMAP_BYTES * 8 - 1;
518 struct llog_rec_hdr *rec;
519 struct llog_rec_tail *tail;
521 /* skip records not set in bitmap */
522 while (index >= first_index &&
523 !ext2_test_bit(index, llh->llh_bitmap))
526 LASSERT(index >= first_index - 1);
527 if (index == first_index - 1)
530 /* get the buf with our target record; avoid old garbage */
531 memset(buf, 0, LLOG_CHUNK_SIZE);
532 rc = llog_prev_block(env, loghandle, index, buf,
538 idx = rec->lrh_index;
539 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
540 while (idx < index) {
541 rec = (void *)rec + rec->lrh_len;
542 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
543 lustre_swab_llog_rec(rec);
546 LASSERT(idx == index);
547 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
549 /* process records in buffer, starting where we found one */
550 while ((void *)tail > buf) {
551 if (tail->lrt_index == 0)
552 GOTO(out, rc = 0); /* no more records */
554 /* if set, process the callback on this record */
555 if (ext2_test_bit(index, llh->llh_bitmap)) {
556 rec = (void *)tail - tail->lrt_len +
559 rc = cb(env, loghandle, rec, data);
560 if (rc == LLOG_PROC_BREAK) {
562 } else if (rc == LLOG_DEL_RECORD) {
563 rc = llog_cancel_rec(env, loghandle,
570 /* previous record, still in buffer? */
572 if (index < first_index)
574 tail = (void *)tail - tail->lrt_len;
580 OBD_FREE(buf, LLOG_CHUNK_SIZE);
583 EXPORT_SYMBOL(llog_reverse_process);
589 * llog_open - open llog, may not exist
590 * llog_exist - check if llog exists
591 * llog_close - close opened llog, pair for open, frees llog_handle
592 * llog_declare_create - declare llog creation
593 * llog_create - create new llog on disk, need transaction handle
594 * llog_declare_write_rec - declaration of llog write
595 * llog_write_rec - write llog record on disk, need transaction handle
596 * llog_declare_add - declare llog catalog record addition
597 * llog_add - add llog record in catalog, need transaction handle
599 int llog_exist(struct llog_handle *loghandle)
601 struct llog_operations *lop;
606 rc = llog_handle2ops(loghandle, &lop);
609 if (lop->lop_exist == NULL)
612 rc = lop->lop_exist(loghandle);
615 EXPORT_SYMBOL(llog_exist);
617 int llog_declare_create(const struct lu_env *env,
618 struct llog_handle *loghandle, struct thandle *th)
620 struct llog_operations *lop;
625 rc = llog_handle2ops(loghandle, &lop);
628 if (lop->lop_declare_create == NULL)
631 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
633 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
634 rc = lop->lop_declare_create(env, loghandle, th);
636 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
639 EXPORT_SYMBOL(llog_declare_create);
641 int llog_create(const struct lu_env *env, struct llog_handle *handle,
644 struct llog_operations *lop;
649 rc = llog_handle2ops(handle, &lop);
652 if (lop->lop_create == NULL)
655 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
657 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
658 rc = lop->lop_create(env, handle, th);
660 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
663 EXPORT_SYMBOL(llog_create);
665 int llog_declare_write_rec(const struct lu_env *env,
666 struct llog_handle *handle,
667 struct llog_rec_hdr *rec, int idx,
670 struct llog_operations *lop;
675 rc = llog_handle2ops(handle, &lop);
679 if (lop->lop_declare_write_rec == NULL)
682 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
684 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
685 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
687 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
690 EXPORT_SYMBOL(llog_declare_write_rec);
692 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
693 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
694 int idx, struct thandle *th)
696 struct llog_operations *lop;
697 int raised, rc, buflen;
701 rc = llog_handle2ops(handle, &lop);
706 if (lop->lop_write_rec == NULL)
709 buflen = rec->lrh_len;
710 LASSERT(cfs_size_round(buflen) == buflen);
712 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
714 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
715 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
717 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
720 EXPORT_SYMBOL(llog_write_rec);
722 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
723 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
730 if (lgh->lgh_logops->lop_add == NULL)
733 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
735 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
736 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
738 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
741 EXPORT_SYMBOL(llog_add);
743 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
744 struct llog_rec_hdr *rec, struct thandle *th)
750 if (lgh->lgh_logops->lop_declare_add == NULL)
753 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
755 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
756 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
758 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
761 EXPORT_SYMBOL(llog_declare_add);
764 * Helper function to open llog or create it if doesn't exist.
765 * It hides all transaction handling from caller.
767 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
768 struct llog_handle **res, struct llog_logid *logid,
777 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
781 if (llog_exist(*res))
784 LASSERT((*res)->lgh_obj != NULL);
786 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
788 th = dt_trans_create(env, d);
790 GOTO(out, rc = PTR_ERR(th));
792 rc = llog_declare_create(env, *res, th);
794 rc = dt_trans_start_local(env, d, th);
796 rc = llog_create(env, *res, th);
798 dt_trans_stop(env, d, th);
801 llog_close(env, *res);
804 EXPORT_SYMBOL(llog_open_create);
807 * Helper function to delete existent llog.
809 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
810 struct llog_logid *logid, char *name)
812 struct llog_handle *handle;
817 /* nothing to erase */
818 if (name == NULL && logid == NULL)
821 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
825 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
827 rc = llog_destroy(env, handle);
829 rc2 = llog_close(env, handle);
834 EXPORT_SYMBOL(llog_erase);
837 * Helper function for write record in llog.
838 * It hides all transaction handling from caller.
839 * Valid only with local llog.
841 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
842 struct llog_rec_hdr *rec, int idx)
844 struct dt_device *dt;
851 LASSERT(loghandle->lgh_ctxt);
852 LASSERT(loghandle->lgh_obj != NULL);
854 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
856 th = dt_trans_create(env, dt);
860 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
864 rc = dt_trans_start_local(env, dt, th);
868 down_write(&loghandle->lgh_lock);
869 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
870 up_write(&loghandle->lgh_lock);
872 dt_trans_stop(env, dt, th);
875 EXPORT_SYMBOL(llog_write);
877 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
878 struct llog_handle **lgh, struct llog_logid *logid,
879 char *name, enum llog_open_param open_param)
887 LASSERT(ctxt->loc_logops);
889 if (ctxt->loc_logops->lop_open == NULL) {
894 *lgh = llog_alloc_handle();
897 (*lgh)->lgh_ctxt = ctxt;
898 (*lgh)->lgh_logops = ctxt->loc_logops;
900 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
902 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
903 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
905 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
907 llog_free_handle(*lgh);
912 EXPORT_SYMBOL(llog_open);
914 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
916 struct llog_operations *lop;
921 rc = llog_handle2ops(loghandle, &lop);
924 if (lop->lop_close == NULL)
925 GOTO(out, rc = -EOPNOTSUPP);
926 rc = lop->lop_close(env, loghandle);
928 llog_handle_put(loghandle);
931 EXPORT_SYMBOL(llog_close);
934 * Helper function to get the llog size in records. It is used by MGS
935 * mostly to check that config llog exists and contains data.
937 * \param[in] env execution environment
938 * \param[in] ctxt llog context
939 * \param[in] name llog name
941 * \retval true if there are records in llog besides a header
942 * \retval false on error or llog without records
944 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
947 struct llog_handle *llh;
950 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
952 if (likely(rc == -ENOENT))
957 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
960 rc = llog_get_size(llh);
963 llog_close(env, llh);
965 /* The header is record 1, the llog is still considered as empty
966 * if there is only header */
969 EXPORT_SYMBOL(llog_is_empty);
971 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
972 struct llog_rec_hdr *rec, void *data)
974 struct llog_handle *copy_llh = data;
976 /* Append all records */
977 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
979 EXPORT_SYMBOL(llog_copy_handler);
981 /* backup plain llog */
982 int llog_backup(const struct lu_env *env, struct obd_device *obd,
983 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
984 char *name, char *backup)
986 struct llog_handle *llh, *bllh;
991 /* open original log */
992 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
994 /* the -ENOENT case is also reported to the caller
995 * but silently so it should handle that if needed.
998 CERROR("%s: failed to open log %s: rc = %d\n",
999 obd->obd_name, name, rc);
1003 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1005 GOTO(out_close, rc);
1007 /* Make sure there's no old backup log */
1008 rc = llog_erase(env, bctxt, NULL, backup);
1009 if (rc < 0 && rc != -ENOENT)
1010 GOTO(out_close, rc);
1012 /* open backup log */
1013 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1015 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1016 obd->obd_name, backup, rc);
1017 GOTO(out_close, rc);
1020 /* check that backup llog is not the same object as original one */
1021 if (llh->lgh_obj == bllh->lgh_obj) {
1022 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1023 obd->obd_name, name, backup, llh->lgh_obj,
1025 GOTO(out_backup, rc = -EEXIST);
1028 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1030 GOTO(out_backup, rc);
1032 /* Copy log record by record */
1033 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1036 CERROR("%s: failed to backup log %s: rc = %d\n",
1037 obd->obd_name, name, rc);
1039 llog_close(env, bllh);
1041 llog_close(env, llh);
1044 EXPORT_SYMBOL(llog_backup);