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 <liblustre.h>
54 #include <obd_class.h>
55 #include <lustre_log.h>
56 #include "llog_internal.h"
59 * Allocate a new log or catalog handle
60 * Used inside llog_open().
62 struct llog_handle *llog_alloc_handle(void)
64 struct llog_handle *loghandle;
66 OBD_ALLOC_PTR(loghandle);
67 if (loghandle == NULL)
70 init_rwsem(&loghandle->lgh_lock);
71 spin_lock_init(&loghandle->lgh_hdr_lock);
72 CFS_INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
73 atomic_set(&loghandle->lgh_refcount, 1);
79 * Free llog handle and header data if exists. Used in llog_close() only
81 void llog_free_handle(struct llog_handle *loghandle)
83 LASSERT(loghandle != NULL);
85 /* failed llog_init_handle */
86 if (!loghandle->lgh_hdr)
89 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
90 LASSERT(cfs_list_empty(&loghandle->u.phd.phd_entry));
91 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
92 LASSERT(cfs_list_empty(&loghandle->u.chd.chd_head));
93 LASSERT(sizeof(*(loghandle->lgh_hdr)) == LLOG_CHUNK_SIZE);
94 OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
96 OBD_FREE_PTR(loghandle);
99 void llog_handle_get(struct llog_handle *loghandle)
101 atomic_inc(&loghandle->lgh_refcount);
104 void llog_handle_put(struct llog_handle *loghandle)
106 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
107 if (atomic_dec_and_test(&loghandle->lgh_refcount))
108 llog_free_handle(loghandle);
111 /* returns negative on error; 0 if success; 1 if success & log destroyed */
112 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
115 struct llog_log_hdr *llh = loghandle->lgh_hdr;
119 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n",
120 index, POSTID(&loghandle->lgh_id.lgl_oi));
123 CERROR("Can't cancel index 0 which is header\n");
127 spin_lock(&loghandle->lgh_hdr_lock);
128 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
129 spin_unlock(&loghandle->lgh_hdr_lock);
130 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
136 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
137 (llh->llh_count == 1) &&
138 (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
139 spin_unlock(&loghandle->lgh_hdr_lock);
140 rc = llog_destroy(env, loghandle);
142 CERROR("%s: can't destroy empty llog #"DOSTID
144 loghandle->lgh_ctxt->loc_obd->obd_name,
145 POSTID(&loghandle->lgh_id.lgl_oi),
146 loghandle->lgh_id.lgl_ogen, rc);
149 RETURN(LLOG_DEL_PLAIN);
151 spin_unlock(&loghandle->lgh_hdr_lock);
153 rc = llog_write(env, loghandle, &llh->llh_hdr, LLOG_HEADER_IDX);
155 CERROR("%s: fail to write header for llog #"DOSTID
157 loghandle->lgh_ctxt->loc_obd->obd_name,
158 POSTID(&loghandle->lgh_id.lgl_oi),
159 loghandle->lgh_id.lgl_ogen, rc);
164 spin_lock(&loghandle->lgh_hdr_lock);
165 ext2_set_bit(index, llh->llh_bitmap);
167 spin_unlock(&loghandle->lgh_hdr_lock);
170 EXPORT_SYMBOL(llog_cancel_rec);
172 static int llog_read_header(const struct lu_env *env,
173 struct llog_handle *handle,
174 struct obd_uuid *uuid)
176 struct llog_operations *lop;
179 rc = llog_handle2ops(handle, &lop);
183 if (lop->lop_read_header == NULL)
186 rc = lop->lop_read_header(env, handle);
187 if (rc == LLOG_EEMPTY) {
188 struct llog_log_hdr *llh = handle->lgh_hdr;
190 handle->lgh_last_idx = 0; /* header is record with index 0 */
191 llh->llh_count = 1; /* for the header record */
192 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
193 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
194 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
195 llh->llh_timestamp = cfs_time_current_sec();
197 memcpy(&llh->llh_tgtuuid, uuid,
198 sizeof(llh->llh_tgtuuid));
199 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
200 ext2_set_bit(0, llh->llh_bitmap);
206 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
207 int flags, struct obd_uuid *uuid)
209 struct llog_log_hdr *llh;
213 LASSERT(handle->lgh_hdr == NULL);
218 handle->lgh_hdr = llh;
219 /* first assign flags to use llog_client_ops */
220 llh->llh_flags = flags;
221 rc = llog_read_header(env, handle, uuid);
223 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
224 flags & LLOG_F_IS_CAT) ||
225 (llh->llh_flags & LLOG_F_IS_CAT &&
226 flags & LLOG_F_IS_PLAIN))) {
227 CERROR("%s: llog type is %s but initializing %s\n",
228 handle->lgh_ctxt->loc_obd->obd_name,
229 llh->llh_flags & LLOG_F_IS_CAT ?
231 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
232 GOTO(out, rc = -EINVAL);
233 } else if (llh->llh_flags &
234 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
236 * it is possible to open llog without specifying llog
237 * type so it is taken from llh_flags
239 flags = llh->llh_flags;
241 /* for some reason the llh_flags has no type set */
242 CERROR("llog type is not specified!\n");
243 GOTO(out, rc = -EINVAL);
246 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
247 CERROR("%s: llog uuid mismatch: %s/%s\n",
248 handle->lgh_ctxt->loc_obd->obd_name,
250 (char *)llh->llh_tgtuuid.uuid);
251 GOTO(out, rc = -EEXIST);
254 if (flags & LLOG_F_IS_CAT) {
255 LASSERT(cfs_list_empty(&handle->u.chd.chd_head));
256 CFS_INIT_LIST_HEAD(&handle->u.chd.chd_head);
257 llh->llh_size = sizeof(struct llog_logid_rec);
258 } else if (!(flags & LLOG_F_IS_PLAIN)) {
259 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
260 handle->lgh_ctxt->loc_obd->obd_name,
261 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
267 handle->lgh_hdr = NULL;
271 EXPORT_SYMBOL(llog_init_handle);
273 static int llog_process_thread(void *arg)
275 struct llog_process_info *lpi = arg;
276 struct llog_handle *loghandle = lpi->lpi_loghandle;
277 struct llog_log_hdr *llh = loghandle->lgh_hdr;
278 struct llog_process_cat_data *cd = lpi->lpi_catdata;
280 __u64 cur_offset = LLOG_CHUNK_SIZE;
282 int rc = 0, index = 1, last_index;
284 int last_called_index = 0;
290 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
292 lpi->lpi_rc = -ENOMEM;
297 last_called_index = cd->lpcd_first_idx;
298 index = cd->lpcd_first_idx + 1;
300 if (cd != NULL && cd->lpcd_last_idx)
301 last_index = cd->lpcd_last_idx;
303 last_index = LLOG_BITMAP_BYTES * 8 - 1;
306 struct llog_rec_hdr *rec;
308 /* skip records not set in bitmap */
309 while (index <= last_index &&
310 !ext2_test_bit(index, llh->llh_bitmap))
313 LASSERT(index <= last_index + 1);
314 if (index == last_index + 1)
317 CDEBUG(D_OTHER, "index: %d last_index %d\n",
320 /* get the buf with our target record; avoid old garbage */
321 memset(buf, 0, LLOG_CHUNK_SIZE);
322 last_offset = cur_offset;
323 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
324 index, &cur_offset, buf, LLOG_CHUNK_SIZE);
328 /* NB: when rec->lrh_len is accessed it is already swabbed
329 * since it is used at the "end" of the loop and the rec
330 * swabbing is done at the beginning of the loop. */
331 for (rec = (struct llog_rec_hdr *)buf;
332 (char *)rec < buf + LLOG_CHUNK_SIZE;
333 rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
335 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
338 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
339 lustre_swab_llog_rec(rec);
341 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
342 rec->lrh_type, rec->lrh_index);
344 if (rec->lrh_index == 0) {
345 /* probably another rec just got added? */
346 if (index <= loghandle->lgh_last_idx)
347 GOTO(repeat, rc = 0);
348 GOTO(out, rc = 0); /* no more records */
350 if (rec->lrh_len == 0 ||
351 rec->lrh_len > LLOG_CHUNK_SIZE) {
352 CWARN("invalid length %d in llog record for "
353 "index %d/%d\n", rec->lrh_len,
354 rec->lrh_index, index);
355 GOTO(out, rc = -EINVAL);
358 if (rec->lrh_index < index) {
359 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
365 "lrh_index: %d lrh_len: %d (%d remains)\n",
366 rec->lrh_index, rec->lrh_len,
367 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
369 loghandle->lgh_cur_idx = rec->lrh_index;
370 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
373 /* if set, process the callback on this record */
374 if (ext2_test_bit(index, llh->llh_bitmap)) {
375 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
377 last_called_index = index;
378 if (rc == LLOG_PROC_BREAK) {
380 } else if (rc == LLOG_DEL_RECORD) {
381 rc = llog_cancel_rec(lpi->lpi_env,
388 CDEBUG(D_OTHER, "Skipped index %d\n", index);
391 /* next record, still in buffer? */
393 if (index > last_index)
400 cd->lpcd_last_idx = last_called_index;
402 if (unlikely(rc == -EIO)) {
403 /* something bad happened to the processing, probably I/O
404 * error or the log got corrupted..
405 * to be able to finally release the log we discard any
406 * remaining bits in the header */
407 CERROR("llog found corrupted\n");
408 while (index <= last_index) {
409 if (ext2_test_bit(index, llh->llh_bitmap) != 0)
410 llog_cancel_rec(lpi->lpi_env, loghandle, index);
416 OBD_FREE(buf, LLOG_CHUNK_SIZE);
422 static int llog_process_thread_daemonize(void *arg)
424 struct llog_process_info *lpi = arg;
430 /* client env has no keys, tags is just 0 */
431 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
436 rc = llog_process_thread(arg);
440 complete(&lpi->lpi_completion);
445 int llog_process_or_fork(const struct lu_env *env,
446 struct llog_handle *loghandle,
447 llog_cb_t cb, void *data, void *catdata, bool fork)
449 struct llog_process_info *lpi;
456 CERROR("cannot alloc pointer\n");
459 lpi->lpi_loghandle = loghandle;
461 lpi->lpi_cbdata = data;
462 lpi->lpi_catdata = catdata;
466 struct task_struct *task;
468 /* The new thread can't use parent env,
469 * init the new one in llog_process_thread_daemonize. */
471 init_completion(&lpi->lpi_completion);
472 task = kthread_run(llog_process_thread_daemonize, lpi,
473 "llog_process_thread");
476 CERROR("%s: cannot start thread: rc = %d\n",
477 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
480 wait_for_completion(&lpi->lpi_completion);
483 llog_process_thread(lpi);
487 llog_process_thread(lpi);
497 EXPORT_SYMBOL(llog_process_or_fork);
499 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
500 llog_cb_t cb, void *data, void *catdata)
503 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
504 return rc == LLOG_DEL_PLAIN ? 0 : rc;
506 EXPORT_SYMBOL(llog_process);
508 int llog_reverse_process(const struct lu_env *env,
509 struct llog_handle *loghandle, llog_cb_t cb,
510 void *data, void *catdata)
512 struct llog_log_hdr *llh = loghandle->lgh_hdr;
513 struct llog_process_cat_data *cd = catdata;
515 int rc = 0, first_index = 1, index, idx;
518 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
523 first_index = cd->lpcd_first_idx + 1;
524 if (cd != NULL && cd->lpcd_last_idx)
525 index = cd->lpcd_last_idx;
527 index = LLOG_BITMAP_BYTES * 8 - 1;
530 struct llog_rec_hdr *rec;
531 struct llog_rec_tail *tail;
533 /* skip records not set in bitmap */
534 while (index >= first_index &&
535 !ext2_test_bit(index, llh->llh_bitmap))
538 LASSERT(index >= first_index - 1);
539 if (index == first_index - 1)
542 /* get the buf with our target record; avoid old garbage */
543 memset(buf, 0, LLOG_CHUNK_SIZE);
544 rc = llog_prev_block(env, loghandle, index, buf,
550 idx = rec->lrh_index;
551 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
552 while (idx < index) {
553 rec = (void *)rec + rec->lrh_len;
554 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
555 lustre_swab_llog_rec(rec);
558 LASSERT(idx == index);
559 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
561 /* process records in buffer, starting where we found one */
562 while ((void *)tail > buf) {
563 if (tail->lrt_index == 0)
564 GOTO(out, rc = 0); /* no more records */
566 /* if set, process the callback on this record */
567 if (ext2_test_bit(index, llh->llh_bitmap)) {
568 rec = (void *)tail - tail->lrt_len +
571 rc = cb(env, loghandle, rec, data);
572 if (rc == LLOG_PROC_BREAK) {
574 } else if (rc == LLOG_DEL_RECORD) {
575 rc = llog_cancel_rec(env, loghandle,
582 /* previous record, still in buffer? */
584 if (index < first_index)
586 tail = (void *)tail - tail->lrt_len;
592 OBD_FREE(buf, LLOG_CHUNK_SIZE);
595 EXPORT_SYMBOL(llog_reverse_process);
601 * llog_open - open llog, may not exist
602 * llog_exist - check if llog exists
603 * llog_close - close opened llog, pair for open, frees llog_handle
604 * llog_declare_create - declare llog creation
605 * llog_create - create new llog on disk, need transaction handle
606 * llog_declare_write_rec - declaration of llog write
607 * llog_write_rec - write llog record on disk, need transaction handle
608 * llog_declare_add - declare llog catalog record addition
609 * llog_add - add llog record in catalog, need transaction handle
611 int llog_exist(struct llog_handle *loghandle)
613 struct llog_operations *lop;
618 rc = llog_handle2ops(loghandle, &lop);
621 if (lop->lop_exist == NULL)
624 rc = lop->lop_exist(loghandle);
627 EXPORT_SYMBOL(llog_exist);
629 int llog_declare_create(const struct lu_env *env,
630 struct llog_handle *loghandle, struct thandle *th)
632 struct llog_operations *lop;
637 rc = llog_handle2ops(loghandle, &lop);
640 if (lop->lop_declare_create == NULL)
643 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
645 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
646 rc = lop->lop_declare_create(env, loghandle, th);
648 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
651 EXPORT_SYMBOL(llog_declare_create);
653 int llog_create(const struct lu_env *env, struct llog_handle *handle,
656 struct llog_operations *lop;
661 rc = llog_handle2ops(handle, &lop);
664 if (lop->lop_create == NULL)
667 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
669 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
670 rc = lop->lop_create(env, handle, th);
672 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
675 EXPORT_SYMBOL(llog_create);
677 int llog_declare_write_rec(const struct lu_env *env,
678 struct llog_handle *handle,
679 struct llog_rec_hdr *rec, int idx,
682 struct llog_operations *lop;
687 rc = llog_handle2ops(handle, &lop);
691 if (lop->lop_declare_write_rec == NULL)
694 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
696 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
697 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
699 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
702 EXPORT_SYMBOL(llog_declare_write_rec);
704 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
705 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
706 int idx, struct thandle *th)
708 struct llog_operations *lop;
709 int raised, rc, buflen;
713 rc = llog_handle2ops(handle, &lop);
718 if (lop->lop_write_rec == NULL)
721 buflen = rec->lrh_len;
722 LASSERT(cfs_size_round(buflen) == buflen);
724 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
726 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
727 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
729 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
732 EXPORT_SYMBOL(llog_write_rec);
734 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
735 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
742 if (lgh->lgh_logops->lop_add == NULL)
745 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
747 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
748 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
750 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
753 EXPORT_SYMBOL(llog_add);
755 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
756 struct llog_rec_hdr *rec, struct thandle *th)
762 if (lgh->lgh_logops->lop_declare_add == NULL)
765 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
767 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
768 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
770 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
773 EXPORT_SYMBOL(llog_declare_add);
776 * Helper function to open llog or create it if doesn't exist.
777 * It hides all transaction handling from caller.
779 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
780 struct llog_handle **res, struct llog_logid *logid,
789 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
793 if (llog_exist(*res))
796 LASSERT((*res)->lgh_obj != NULL);
798 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
800 th = dt_trans_create(env, d);
802 GOTO(out, rc = PTR_ERR(th));
804 rc = llog_declare_create(env, *res, th);
806 rc = dt_trans_start_local(env, d, th);
808 rc = llog_create(env, *res, th);
810 dt_trans_stop(env, d, th);
813 llog_close(env, *res);
816 EXPORT_SYMBOL(llog_open_create);
819 * Helper function to delete existent llog.
821 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
822 struct llog_logid *logid, char *name)
824 struct llog_handle *handle;
829 /* nothing to erase */
830 if (name == NULL && logid == NULL)
833 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
837 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
839 rc = llog_destroy(env, handle);
841 rc2 = llog_close(env, handle);
846 EXPORT_SYMBOL(llog_erase);
849 * Helper function for write record in llog.
850 * It hides all transaction handling from caller.
851 * Valid only with local llog.
853 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
854 struct llog_rec_hdr *rec, int idx)
856 struct dt_device *dt;
863 LASSERT(loghandle->lgh_ctxt);
864 LASSERT(loghandle->lgh_obj != NULL);
866 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
868 th = dt_trans_create(env, dt);
872 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
876 rc = dt_trans_start_local(env, dt, th);
880 down_write(&loghandle->lgh_lock);
881 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
882 up_write(&loghandle->lgh_lock);
884 dt_trans_stop(env, dt, th);
887 EXPORT_SYMBOL(llog_write);
889 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
890 struct llog_handle **lgh, struct llog_logid *logid,
891 char *name, enum llog_open_param open_param)
899 LASSERT(ctxt->loc_logops);
901 if (ctxt->loc_logops->lop_open == NULL) {
906 *lgh = llog_alloc_handle();
909 (*lgh)->lgh_ctxt = ctxt;
910 (*lgh)->lgh_logops = ctxt->loc_logops;
912 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
914 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
915 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
917 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
919 llog_free_handle(*lgh);
924 EXPORT_SYMBOL(llog_open);
926 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
928 struct llog_operations *lop;
933 rc = llog_handle2ops(loghandle, &lop);
936 if (lop->lop_close == NULL)
937 GOTO(out, rc = -EOPNOTSUPP);
938 rc = lop->lop_close(env, loghandle);
940 llog_handle_put(loghandle);
943 EXPORT_SYMBOL(llog_close);
946 * Helper function to get the llog size in records. It is used by MGS
947 * mostly to check that config llog exists and contains data.
949 * \param[in] env execution environment
950 * \param[in] ctxt llog context
951 * \param[in] name llog name
953 * \retval true if there are records in llog besides a header
954 * \retval false on error or llog without records
956 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
959 struct llog_handle *llh;
962 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
964 if (likely(rc == -ENOENT))
969 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
972 rc = llog_get_size(llh);
975 llog_close(env, llh);
977 /* The header is record 1, the llog is still considered as empty
978 * if there is only header */
981 EXPORT_SYMBOL(llog_is_empty);
983 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
984 struct llog_rec_hdr *rec, void *data)
986 struct llog_handle *copy_llh = data;
988 /* Append all records */
989 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
991 EXPORT_SYMBOL(llog_copy_handler);
993 /* backup plain llog */
994 int llog_backup(const struct lu_env *env, struct obd_device *obd,
995 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
996 char *name, char *backup)
998 struct llog_handle *llh, *bllh;
1003 /* open original log */
1004 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1006 /* the -ENOENT case is also reported to the caller
1007 * but silently so it should handle that if needed.
1010 CERROR("%s: failed to open log %s: rc = %d\n",
1011 obd->obd_name, name, rc);
1015 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1017 GOTO(out_close, rc);
1019 /* Make sure there's no old backup log */
1020 rc = llog_erase(env, bctxt, NULL, backup);
1021 if (rc < 0 && rc != -ENOENT)
1022 GOTO(out_close, rc);
1024 /* open backup log */
1025 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1027 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1028 obd->obd_name, backup, rc);
1029 GOTO(out_close, rc);
1032 /* check that backup llog is not the same object as original one */
1033 if (llh->lgh_obj == bllh->lgh_obj) {
1034 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1035 obd->obd_name, name, backup, llh->lgh_obj,
1037 GOTO(out_backup, rc = -EEXIST);
1040 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1042 GOTO(out_backup, rc);
1044 /* Copy log record by record */
1045 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1048 CERROR("%s: failed to backup log %s: rc = %d\n",
1049 obd->obd_name, name, rc);
1051 llog_close(env, bllh);
1053 llog_close(env, llh);
1056 EXPORT_SYMBOL(llog_backup);