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
50 #include <linux/kthread.h>
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 init_rwsem(&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 == NULL)
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 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
92 OBD_FREE_PTR(loghandle);
95 void llog_handle_get(struct llog_handle *loghandle)
97 atomic_inc(&loghandle->lgh_refcount);
100 void llog_handle_put(struct llog_handle *loghandle)
102 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
103 if (atomic_dec_and_test(&loghandle->lgh_refcount))
104 llog_free_handle(loghandle);
107 /* returns negative on error; 0 if success; 1 if success & log destroyed */
108 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
111 struct llog_log_hdr *llh = loghandle->lgh_hdr;
115 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n",
116 index, POSTID(&loghandle->lgh_id.lgl_oi));
119 CERROR("Can't cancel index 0 which is header\n");
123 down_write(&loghandle->lgh_hdr_lock);
124 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
125 up_write(&loghandle->lgh_hdr_lock);
126 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
132 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
133 (llh->llh_count == 1) &&
134 (loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1)) {
135 up_write(&loghandle->lgh_hdr_lock);
136 rc = llog_destroy(env, loghandle);
138 CERROR("%s: can't destroy empty llog #"DOSTID
140 loghandle->lgh_ctxt->loc_obd->obd_name,
141 POSTID(&loghandle->lgh_id.lgl_oi),
142 loghandle->lgh_id.lgl_ogen, rc);
145 RETURN(LLOG_DEL_PLAIN);
147 up_write(&loghandle->lgh_hdr_lock);
149 rc = llog_write(env, loghandle, &llh->llh_hdr, LLOG_HEADER_IDX);
151 CERROR("%s: fail to write header for llog #"DOSTID
153 loghandle->lgh_ctxt->loc_obd->obd_name,
154 POSTID(&loghandle->lgh_id.lgl_oi),
155 loghandle->lgh_id.lgl_ogen, rc);
160 down_write(&loghandle->lgh_hdr_lock);
161 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
163 up_write(&loghandle->lgh_hdr_lock);
167 static int llog_read_header(const struct lu_env *env,
168 struct llog_handle *handle,
169 struct obd_uuid *uuid)
171 struct llog_operations *lop;
174 rc = llog_handle2ops(handle, &lop);
178 if (lop->lop_read_header == NULL)
181 rc = lop->lop_read_header(env, handle);
182 if (rc == LLOG_EEMPTY) {
183 struct llog_log_hdr *llh = handle->lgh_hdr;
185 /* lrh_len should be initialized in llog_init_handle */
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 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
190 LLOG_MIN_CHUNK_SIZE);
191 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
192 llh->llh_hdr.lrh_index = 0;
193 llh->llh_timestamp = cfs_time_current_sec();
195 memcpy(&llh->llh_tgtuuid, uuid,
196 sizeof(llh->llh_tgtuuid));
197 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
198 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
199 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
200 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
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;
210 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
212 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
215 LASSERT(handle->lgh_hdr == NULL);
217 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
218 OBD_ALLOC_LARGE(llh, chunk_size);
222 handle->lgh_hdr = llh;
223 handle->lgh_hdr_size = chunk_size;
224 /* first assign flags to use llog_client_ops */
225 llh->llh_flags = flags;
226 rc = llog_read_header(env, handle, uuid);
228 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
229 flags & LLOG_F_IS_CAT) ||
230 (llh->llh_flags & LLOG_F_IS_CAT &&
231 flags & LLOG_F_IS_PLAIN))) {
232 CERROR("%s: llog type is %s but initializing %s\n",
233 handle->lgh_ctxt->loc_obd->obd_name,
234 llh->llh_flags & LLOG_F_IS_CAT ?
236 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
237 GOTO(out, rc = -EINVAL);
238 } else if (llh->llh_flags &
239 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
241 * it is possible to open llog without specifying llog
242 * type so it is taken from llh_flags
244 flags = llh->llh_flags;
246 /* for some reason the llh_flags has no type set */
247 CERROR("llog type is not specified!\n");
248 GOTO(out, rc = -EINVAL);
251 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
252 CERROR("%s: llog uuid mismatch: %s/%s\n",
253 handle->lgh_ctxt->loc_obd->obd_name,
255 (char *)llh->llh_tgtuuid.uuid);
256 GOTO(out, rc = -EEXIST);
259 if (flags & LLOG_F_IS_CAT) {
260 LASSERT(list_empty(&handle->u.chd.chd_head));
261 INIT_LIST_HEAD(&handle->u.chd.chd_head);
262 llh->llh_size = sizeof(struct llog_logid_rec);
263 } else if (!(flags & LLOG_F_IS_PLAIN)) {
264 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
265 handle->lgh_ctxt->loc_obd->obd_name,
266 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
269 llh->llh_flags |= fmt;
272 OBD_FREE_LARGE(llh, chunk_size);
273 handle->lgh_hdr = NULL;
277 EXPORT_SYMBOL(llog_init_handle);
279 static int llog_process_thread(void *arg)
281 struct llog_process_info *lpi = arg;
282 struct llog_handle *loghandle = lpi->lpi_loghandle;
283 struct llog_log_hdr *llh = loghandle->lgh_hdr;
284 struct llog_process_cat_data *cd = lpi->lpi_catdata;
289 int rc = 0, index = 1, last_index;
291 int last_called_index = 0;
298 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
300 OBD_ALLOC_LARGE(buf, chunk_size);
302 lpi->lpi_rc = -ENOMEM;
307 last_called_index = cd->lpcd_first_idx;
308 index = cd->lpcd_first_idx + 1;
310 if (cd != NULL && cd->lpcd_last_idx)
311 last_index = cd->lpcd_last_idx;
313 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
315 if (index > last_index) /* Record is not in this buffer. */
319 struct llog_rec_hdr *rec;
321 /* skip records not set in bitmap */
322 while (index <= last_index &&
323 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
326 LASSERT(index <= last_index + 1);
327 if (index == last_index + 1)
330 CDEBUG(D_OTHER, "index: %d last_index %d\n",
333 /* get the buf with our target record; avoid old garbage */
334 memset(buf, 0, chunk_size);
335 last_offset = cur_offset;
336 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
337 index, &cur_offset, buf, chunk_size);
341 /* NB: when rec->lrh_len is accessed it is already swabbed
342 * since it is used at the "end" of the loop and the rec
343 * swabbing is done at the beginning of the loop. */
344 for (rec = (struct llog_rec_hdr *)buf;
345 (char *)rec < buf + chunk_size;
346 rec = llog_rec_hdr_next(rec)) {
348 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
351 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
352 lustre_swab_llog_rec(rec);
354 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
355 rec->lrh_type, rec->lrh_index);
357 if (rec->lrh_index == 0) {
358 /* probably another rec just got added? */
359 if (index <= loghandle->lgh_last_idx)
360 GOTO(repeat, rc = 0);
361 GOTO(out, rc = 0); /* no more records */
363 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
364 CWARN("invalid length %d in llog record for "
365 "index %d/%d\n", rec->lrh_len,
366 rec->lrh_index, index);
367 GOTO(out, rc = -EINVAL);
370 if (rec->lrh_index < index) {
371 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
377 "lrh_index: %d lrh_len: %d (%d remains)\n",
378 rec->lrh_index, rec->lrh_len,
379 (int)(buf + chunk_size - (char *)rec));
381 loghandle->lgh_cur_idx = rec->lrh_index;
382 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
385 /* if set, process the callback on this record */
386 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
387 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
389 last_called_index = index;
390 if (rc == LLOG_PROC_BREAK) {
392 } else if (rc == LLOG_DEL_RECORD) {
393 rc = llog_cancel_rec(lpi->lpi_env,
400 CDEBUG(D_OTHER, "Skipped index %d\n", index);
403 /* next record, still in buffer? */
405 if (index > last_index)
412 cd->lpcd_last_idx = last_called_index;
414 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
415 /* something bad happened to the processing of a local
416 * llog file, probably I/O error or the log got corrupted..
417 * to be able to finally release the log we discard any
418 * remaining bits in the header */
419 CERROR("Local llog found corrupted\n");
420 while (index <= last_index) {
421 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh)) != 0)
422 llog_cancel_rec(lpi->lpi_env, loghandle, index);
428 OBD_FREE_LARGE(buf, chunk_size);
433 static int llog_process_thread_daemonize(void *arg)
435 struct llog_process_info *lpi = arg;
441 /* client env has no keys, tags is just 0 */
442 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
447 rc = llog_process_thread(arg);
451 complete(&lpi->lpi_completion);
455 int llog_process_or_fork(const struct lu_env *env,
456 struct llog_handle *loghandle,
457 llog_cb_t cb, void *data, void *catdata, bool fork)
459 struct llog_process_info *lpi;
466 CERROR("cannot alloc pointer\n");
469 lpi->lpi_loghandle = loghandle;
471 lpi->lpi_cbdata = data;
472 lpi->lpi_catdata = catdata;
475 struct task_struct *task;
477 /* The new thread can't use parent env,
478 * init the new one in llog_process_thread_daemonize. */
480 init_completion(&lpi->lpi_completion);
481 task = kthread_run(llog_process_thread_daemonize, lpi,
482 "llog_process_thread");
485 CERROR("%s: cannot start thread: rc = %d\n",
486 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
489 wait_for_completion(&lpi->lpi_completion);
492 llog_process_thread(lpi);
500 EXPORT_SYMBOL(llog_process_or_fork);
502 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
503 llog_cb_t cb, void *data, void *catdata)
506 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
507 return rc == LLOG_DEL_PLAIN ? 0 : rc;
509 EXPORT_SYMBOL(llog_process);
511 int llog_reverse_process(const struct lu_env *env,
512 struct llog_handle *loghandle, llog_cb_t cb,
513 void *data, void *catdata)
515 struct llog_log_hdr *llh = loghandle->lgh_hdr;
516 struct llog_process_cat_data *cd = catdata;
518 int rc = 0, first_index = 1, index, idx;
519 __u32 chunk_size = llh->llh_hdr.lrh_len;
522 OBD_ALLOC_LARGE(buf, chunk_size);
527 first_index = cd->lpcd_first_idx + 1;
528 if (cd != NULL && cd->lpcd_last_idx)
529 index = cd->lpcd_last_idx;
531 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
534 struct llog_rec_hdr *rec;
535 struct llog_rec_tail *tail;
537 /* skip records not set in bitmap */
538 while (index >= first_index &&
539 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
542 LASSERT(index >= first_index - 1);
543 if (index == first_index - 1)
546 /* get the buf with our target record; avoid old garbage */
547 memset(buf, 0, chunk_size);
548 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
553 idx = rec->lrh_index;
554 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
555 while (idx < index) {
556 rec = (void *)rec + rec->lrh_len;
557 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
558 lustre_swab_llog_rec(rec);
561 LASSERT(idx == index);
562 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
564 /* process records in buffer, starting where we found one */
565 while ((void *)tail > buf) {
566 if (tail->lrt_index == 0)
567 GOTO(out, rc = 0); /* no more records */
569 /* if set, process the callback on this record */
570 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
571 rec = (void *)tail - tail->lrt_len +
574 rc = cb(env, loghandle, rec, data);
575 if (rc == LLOG_PROC_BREAK) {
577 } else if (rc == LLOG_DEL_RECORD) {
578 rc = llog_cancel_rec(env, loghandle,
585 /* previous record, still in buffer? */
587 if (index < first_index)
589 tail = (void *)tail - tail->lrt_len;
595 OBD_FREE_LARGE(buf, chunk_size);
598 EXPORT_SYMBOL(llog_reverse_process);
604 * llog_open - open llog, may not exist
605 * llog_exist - check if llog exists
606 * llog_close - close opened llog, pair for open, frees llog_handle
607 * llog_declare_create - declare llog creation
608 * llog_create - create new llog on disk, need transaction handle
609 * llog_declare_write_rec - declaration of llog write
610 * llog_write_rec - write llog record on disk, need transaction handle
611 * llog_declare_add - declare llog catalog record addition
612 * llog_add - add llog record in catalog, need transaction handle
614 int llog_exist(struct llog_handle *loghandle)
616 struct llog_operations *lop;
621 rc = llog_handle2ops(loghandle, &lop);
624 if (lop->lop_exist == NULL)
627 rc = lop->lop_exist(loghandle);
630 EXPORT_SYMBOL(llog_exist);
632 int llog_declare_create(const struct lu_env *env,
633 struct llog_handle *loghandle, struct thandle *th)
635 struct llog_operations *lop;
640 rc = llog_handle2ops(loghandle, &lop);
643 if (lop->lop_declare_create == NULL)
646 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
648 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
649 rc = lop->lop_declare_create(env, loghandle, th);
651 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
655 int llog_create(const struct lu_env *env, struct llog_handle *handle,
658 struct llog_operations *lop;
663 rc = llog_handle2ops(handle, &lop);
666 if (lop->lop_create == NULL)
669 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
671 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
672 rc = lop->lop_create(env, handle, th);
674 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
678 int llog_declare_write_rec(const struct lu_env *env,
679 struct llog_handle *handle,
680 struct llog_rec_hdr *rec, int idx,
683 struct llog_operations *lop;
688 rc = llog_handle2ops(handle, &lop);
692 if (lop->lop_declare_write_rec == NULL)
695 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
697 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
698 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
700 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
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);
733 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
734 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
741 if (lgh->lgh_logops->lop_add == NULL)
744 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
746 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
747 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
749 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
752 EXPORT_SYMBOL(llog_add);
754 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
755 struct llog_rec_hdr *rec, struct thandle *th)
761 if (lgh->lgh_logops->lop_declare_add == NULL)
764 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
766 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
767 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
769 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
772 EXPORT_SYMBOL(llog_declare_add);
775 * Helper function to open llog or create it if doesn't exist.
776 * It hides all transaction handling from caller.
778 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
779 struct llog_handle **res, struct llog_logid *logid,
788 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
792 if (llog_exist(*res))
795 LASSERT((*res)->lgh_obj != NULL);
797 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
799 th = dt_trans_create(env, d);
801 GOTO(out, rc = PTR_ERR(th));
803 th->th_wait_submit = 1;
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 th->th_wait_submit = 1;
877 rc = dt_trans_start_local(env, dt, th);
881 down_write(&loghandle->lgh_lock);
882 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
883 up_write(&loghandle->lgh_lock);
885 dt_trans_stop(env, dt, th);
888 EXPORT_SYMBOL(llog_write);
890 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
891 struct llog_handle **lgh, struct llog_logid *logid,
892 char *name, enum llog_open_param open_param)
900 LASSERT(ctxt->loc_logops);
902 if (ctxt->loc_logops->lop_open == NULL) {
907 *lgh = llog_alloc_handle();
910 (*lgh)->lgh_ctxt = ctxt;
911 (*lgh)->lgh_logops = ctxt->loc_logops;
913 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
915 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
916 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
918 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
920 llog_free_handle(*lgh);
925 EXPORT_SYMBOL(llog_open);
927 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
929 struct llog_operations *lop;
934 rc = llog_handle2ops(loghandle, &lop);
937 if (lop->lop_close == NULL)
938 GOTO(out, rc = -EOPNOTSUPP);
939 rc = lop->lop_close(env, loghandle);
941 llog_handle_put(loghandle);
944 EXPORT_SYMBOL(llog_close);
947 * Helper function to get the llog size in records. It is used by MGS
948 * mostly to check that config llog exists and contains data.
950 * \param[in] env execution environment
951 * \param[in] ctxt llog context
952 * \param[in] name llog name
954 * \retval true if there are records in llog besides a header
955 * \retval false on error or llog without records
957 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
960 struct llog_handle *llh;
963 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
965 if (likely(rc == -ENOENT))
970 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
973 rc = llog_get_size(llh);
976 llog_close(env, llh);
978 /* The header is record 1, the llog is still considered as empty
979 * if there is only header */
982 EXPORT_SYMBOL(llog_is_empty);
984 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
985 struct llog_rec_hdr *rec, void *data)
987 struct llog_handle *copy_llh = data;
989 /* Append all records */
990 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
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);