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, 2014, 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 static 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 static 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);
168 static int llog_read_header(const struct lu_env *env,
169 struct llog_handle *handle,
170 struct obd_uuid *uuid)
172 struct llog_operations *lop;
175 rc = llog_handle2ops(handle, &lop);
179 if (lop->lop_read_header == NULL)
182 rc = lop->lop_read_header(env, handle);
183 if (rc == LLOG_EEMPTY) {
184 struct llog_log_hdr *llh = handle->lgh_hdr;
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,
194 sizeof(llh->llh_tgtuuid));
195 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
196 ext2_set_bit(0, llh->llh_bitmap);
202 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
203 int flags, struct obd_uuid *uuid)
205 struct llog_log_hdr *llh;
206 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
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);
261 llh->llh_flags |= fmt;
265 handle->lgh_hdr = NULL;
269 EXPORT_SYMBOL(llog_init_handle);
271 static int llog_process_thread(void *arg)
273 struct llog_process_info *lpi = arg;
274 struct llog_handle *loghandle = lpi->lpi_loghandle;
275 struct llog_log_hdr *llh = loghandle->lgh_hdr;
276 struct llog_process_cat_data *cd = lpi->lpi_catdata;
278 __u64 cur_offset = LLOG_CHUNK_SIZE;
280 int rc = 0, index = 1, last_index;
282 int last_called_index = 0;
288 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
290 lpi->lpi_rc = -ENOMEM;
295 last_called_index = cd->lpcd_first_idx;
296 index = cd->lpcd_first_idx + 1;
298 if (cd != NULL && cd->lpcd_last_idx)
299 last_index = cd->lpcd_last_idx;
301 last_index = LLOG_BITMAP_BYTES * 8 - 1;
303 if (index > last_index) {
304 /* Record is not in this buffer. */
309 struct llog_rec_hdr *rec;
311 /* skip records not set in bitmap */
312 while (index <= last_index &&
313 !ext2_test_bit(index, llh->llh_bitmap))
316 LASSERT(index <= last_index + 1);
317 if (index == last_index + 1)
320 CDEBUG(D_OTHER, "index: %d last_index %d\n",
323 /* get the buf with our target record; avoid old garbage */
324 memset(buf, 0, LLOG_CHUNK_SIZE);
325 last_offset = cur_offset;
326 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
327 index, &cur_offset, buf, LLOG_CHUNK_SIZE);
331 /* NB: when rec->lrh_len is accessed it is already swabbed
332 * since it is used at the "end" of the loop and the rec
333 * swabbing is done at the beginning of the loop. */
334 for (rec = (struct llog_rec_hdr *)buf;
335 (char *)rec < buf + LLOG_CHUNK_SIZE;
336 rec = llog_rec_hdr_next(rec)) {
338 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
341 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
342 lustre_swab_llog_rec(rec);
344 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
345 rec->lrh_type, rec->lrh_index);
347 if (rec->lrh_index == 0) {
348 /* probably another rec just got added? */
349 if (index <= loghandle->lgh_last_idx)
350 GOTO(repeat, rc = 0);
351 GOTO(out, rc = 0); /* no more records */
353 if (rec->lrh_len == 0 ||
354 rec->lrh_len > LLOG_CHUNK_SIZE) {
355 CWARN("invalid length %d in llog record for "
356 "index %d/%d\n", rec->lrh_len,
357 rec->lrh_index, index);
358 GOTO(out, rc = -EINVAL);
361 if (rec->lrh_index < index) {
362 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
368 "lrh_index: %d lrh_len: %d (%d remains)\n",
369 rec->lrh_index, rec->lrh_len,
370 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
372 loghandle->lgh_cur_idx = rec->lrh_index;
373 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
376 /* if set, process the callback on this record */
377 if (ext2_test_bit(index, llh->llh_bitmap)) {
378 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
380 last_called_index = index;
381 if (rc == LLOG_PROC_BREAK) {
383 } else if (rc == LLOG_DEL_RECORD) {
384 rc = llog_cancel_rec(lpi->lpi_env,
391 CDEBUG(D_OTHER, "Skipped index %d\n", index);
394 /* next record, still in buffer? */
396 if (index > last_index)
403 cd->lpcd_last_idx = last_called_index;
405 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
406 /* something bad happened to the processing of a local
407 * llog file, probably I/O error or the log got corrupted..
408 * to be able to finally release the log we discard any
409 * remaining bits in the header */
410 CERROR("Local llog found corrupted\n");
411 while (index <= last_index) {
412 if (ext2_test_bit(index, llh->llh_bitmap) != 0)
413 llog_cancel_rec(lpi->lpi_env, loghandle, index);
419 OBD_FREE(buf, LLOG_CHUNK_SIZE);
424 static int llog_process_thread_daemonize(void *arg)
426 struct llog_process_info *lpi = arg;
432 /* client env has no keys, tags is just 0 */
433 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
438 rc = llog_process_thread(arg);
442 complete(&lpi->lpi_completion);
446 int llog_process_or_fork(const struct lu_env *env,
447 struct llog_handle *loghandle,
448 llog_cb_t cb, void *data, void *catdata, bool fork)
450 struct llog_process_info *lpi;
457 CERROR("cannot alloc pointer\n");
460 lpi->lpi_loghandle = loghandle;
462 lpi->lpi_cbdata = data;
463 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);
491 EXPORT_SYMBOL(llog_process_or_fork);
493 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
494 llog_cb_t cb, void *data, void *catdata)
497 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
498 return rc == LLOG_DEL_PLAIN ? 0 : rc;
500 EXPORT_SYMBOL(llog_process);
502 int llog_reverse_process(const struct lu_env *env,
503 struct llog_handle *loghandle, llog_cb_t cb,
504 void *data, void *catdata)
506 struct llog_log_hdr *llh = loghandle->lgh_hdr;
507 struct llog_process_cat_data *cd = catdata;
509 int rc = 0, first_index = 1, index, idx;
512 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
517 first_index = cd->lpcd_first_idx + 1;
518 if (cd != NULL && cd->lpcd_last_idx)
519 index = cd->lpcd_last_idx;
521 index = LLOG_BITMAP_BYTES * 8 - 1;
524 struct llog_rec_hdr *rec;
525 struct llog_rec_tail *tail;
527 /* skip records not set in bitmap */
528 while (index >= first_index &&
529 !ext2_test_bit(index, llh->llh_bitmap))
532 LASSERT(index >= first_index - 1);
533 if (index == first_index - 1)
536 /* get the buf with our target record; avoid old garbage */
537 memset(buf, 0, LLOG_CHUNK_SIZE);
538 rc = llog_prev_block(env, loghandle, index, buf,
544 idx = rec->lrh_index;
545 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
546 while (idx < index) {
547 rec = (void *)rec + rec->lrh_len;
548 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
549 lustre_swab_llog_rec(rec);
552 LASSERT(idx == index);
553 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
555 /* process records in buffer, starting where we found one */
556 while ((void *)tail > buf) {
557 if (tail->lrt_index == 0)
558 GOTO(out, rc = 0); /* no more records */
560 /* if set, process the callback on this record */
561 if (ext2_test_bit(index, llh->llh_bitmap)) {
562 rec = (void *)tail - tail->lrt_len +
565 rc = cb(env, loghandle, rec, data);
566 if (rc == LLOG_PROC_BREAK) {
568 } else if (rc == LLOG_DEL_RECORD) {
569 rc = llog_cancel_rec(env, loghandle,
576 /* previous record, still in buffer? */
578 if (index < first_index)
580 tail = (void *)tail - tail->lrt_len;
586 OBD_FREE(buf, LLOG_CHUNK_SIZE);
589 EXPORT_SYMBOL(llog_reverse_process);
595 * llog_open - open llog, may not exist
596 * llog_exist - check if llog exists
597 * llog_close - close opened llog, pair for open, frees llog_handle
598 * llog_declare_create - declare llog creation
599 * llog_create - create new llog on disk, need transaction handle
600 * llog_declare_write_rec - declaration of llog write
601 * llog_write_rec - write llog record on disk, need transaction handle
602 * llog_declare_add - declare llog catalog record addition
603 * llog_add - add llog record in catalog, need transaction handle
605 int llog_exist(struct llog_handle *loghandle)
607 struct llog_operations *lop;
612 rc = llog_handle2ops(loghandle, &lop);
615 if (lop->lop_exist == NULL)
618 rc = lop->lop_exist(loghandle);
621 EXPORT_SYMBOL(llog_exist);
623 int llog_declare_create(const struct lu_env *env,
624 struct llog_handle *loghandle, struct thandle *th)
626 struct llog_operations *lop;
631 rc = llog_handle2ops(loghandle, &lop);
634 if (lop->lop_declare_create == NULL)
637 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
639 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
640 rc = lop->lop_declare_create(env, loghandle, th);
642 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
646 int llog_create(const struct lu_env *env, struct llog_handle *handle,
649 struct llog_operations *lop;
654 rc = llog_handle2ops(handle, &lop);
657 if (lop->lop_create == NULL)
660 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
662 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
663 rc = lop->lop_create(env, handle, th);
665 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
669 int llog_declare_write_rec(const struct lu_env *env,
670 struct llog_handle *handle,
671 struct llog_rec_hdr *rec, int idx,
674 struct llog_operations *lop;
679 rc = llog_handle2ops(handle, &lop);
683 if (lop->lop_declare_write_rec == NULL)
686 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
688 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
689 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
691 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
695 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
696 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
697 int idx, struct thandle *th)
699 struct llog_operations *lop;
700 int raised, rc, buflen;
704 rc = llog_handle2ops(handle, &lop);
709 if (lop->lop_write_rec == NULL)
712 buflen = rec->lrh_len;
713 LASSERT(cfs_size_round(buflen) == buflen);
715 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
717 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
718 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
720 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
724 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
725 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
732 if (lgh->lgh_logops->lop_add == NULL)
735 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
737 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
738 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
740 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
743 EXPORT_SYMBOL(llog_add);
745 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
746 struct llog_rec_hdr *rec, struct thandle *th)
752 if (lgh->lgh_logops->lop_declare_add == NULL)
755 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
757 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
758 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
760 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
763 EXPORT_SYMBOL(llog_declare_add);
766 * Helper function to open llog or create it if doesn't exist.
767 * It hides all transaction handling from caller.
769 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
770 struct llog_handle **res, struct llog_logid *logid,
779 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
783 if (llog_exist(*res))
786 LASSERT((*res)->lgh_obj != NULL);
788 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
790 th = dt_trans_create(env, d);
792 GOTO(out, rc = PTR_ERR(th));
794 rc = llog_declare_create(env, *res, th);
796 rc = dt_trans_start_local(env, d, th);
798 rc = llog_create(env, *res, th);
800 dt_trans_stop(env, d, th);
803 llog_close(env, *res);
806 EXPORT_SYMBOL(llog_open_create);
809 * Helper function to delete existent llog.
811 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
812 struct llog_logid *logid, char *name)
814 struct llog_handle *handle;
819 /* nothing to erase */
820 if (name == NULL && logid == NULL)
823 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
827 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
829 rc = llog_destroy(env, handle);
831 rc2 = llog_close(env, handle);
836 EXPORT_SYMBOL(llog_erase);
839 * Helper function for write record in llog.
840 * It hides all transaction handling from caller.
841 * Valid only with local llog.
843 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
844 struct llog_rec_hdr *rec, int idx)
846 struct dt_device *dt;
853 LASSERT(loghandle->lgh_ctxt);
854 LASSERT(loghandle->lgh_obj != NULL);
856 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
858 th = dt_trans_create(env, dt);
862 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
866 rc = dt_trans_start_local(env, dt, th);
870 down_write(&loghandle->lgh_lock);
871 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
872 up_write(&loghandle->lgh_lock);
874 dt_trans_stop(env, dt, th);
877 EXPORT_SYMBOL(llog_write);
879 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
880 struct llog_handle **lgh, struct llog_logid *logid,
881 char *name, enum llog_open_param open_param)
889 LASSERT(ctxt->loc_logops);
891 if (ctxt->loc_logops->lop_open == NULL) {
896 *lgh = llog_alloc_handle();
899 (*lgh)->lgh_ctxt = ctxt;
900 (*lgh)->lgh_logops = ctxt->loc_logops;
902 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
904 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
905 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
907 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
909 llog_free_handle(*lgh);
914 EXPORT_SYMBOL(llog_open);
916 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
918 struct llog_operations *lop;
923 rc = llog_handle2ops(loghandle, &lop);
926 if (lop->lop_close == NULL)
927 GOTO(out, rc = -EOPNOTSUPP);
928 rc = lop->lop_close(env, loghandle);
930 llog_handle_put(loghandle);
933 EXPORT_SYMBOL(llog_close);
936 * Helper function to get the llog size in records. It is used by MGS
937 * mostly to check that config llog exists and contains data.
939 * \param[in] env execution environment
940 * \param[in] ctxt llog context
941 * \param[in] name llog name
943 * \retval true if there are records in llog besides a header
944 * \retval false on error or llog without records
946 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
949 struct llog_handle *llh;
952 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
954 if (likely(rc == -ENOENT))
959 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
962 rc = llog_get_size(llh);
965 llog_close(env, llh);
967 /* The header is record 1, the llog is still considered as empty
968 * if there is only header */
971 EXPORT_SYMBOL(llog_is_empty);
973 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
974 struct llog_rec_hdr *rec, void *data)
976 struct llog_handle *copy_llh = data;
978 /* Append all records */
979 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
982 /* backup plain llog */
983 int llog_backup(const struct lu_env *env, struct obd_device *obd,
984 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
985 char *name, char *backup)
987 struct llog_handle *llh, *bllh;
992 /* open original log */
993 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
995 /* the -ENOENT case is also reported to the caller
996 * but silently so it should handle that if needed.
999 CERROR("%s: failed to open log %s: rc = %d\n",
1000 obd->obd_name, name, rc);
1004 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1006 GOTO(out_close, rc);
1008 /* Make sure there's no old backup log */
1009 rc = llog_erase(env, bctxt, NULL, backup);
1010 if (rc < 0 && rc != -ENOENT)
1011 GOTO(out_close, rc);
1013 /* open backup log */
1014 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1016 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1017 obd->obd_name, backup, rc);
1018 GOTO(out_close, rc);
1021 /* check that backup llog is not the same object as original one */
1022 if (llh->lgh_obj == bllh->lgh_obj) {
1023 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1024 obd->obd_name, name, backup, llh->lgh_obj,
1026 GOTO(out_backup, rc = -EEXIST);
1029 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1031 GOTO(out_backup, rc);
1033 /* Copy log record by record */
1034 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1037 CERROR("%s: failed to backup log %s: rc = %d\n",
1038 obd->obd_name, name, rc);
1040 llog_close(env, bllh);
1042 llog_close(env, llh);
1045 EXPORT_SYMBOL(llog_backup);