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, 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 "../include/obd_class.h"
52 #include "../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;
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 spin_lock(&loghandle->lgh_hdr_lock);
124 if (!ext2_clear_bit(index, llh->llh_bitmap)) {
125 spin_unlock(&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_BITMAP_BYTES * 8) - 1)) {
135 spin_unlock(&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);
147 spin_unlock(&loghandle->lgh_hdr_lock);
149 rc = llog_write(env, loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
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 spin_lock(&loghandle->lgh_hdr_lock);
161 ext2_set_bit(index, llh->llh_bitmap);
163 spin_unlock(&loghandle->lgh_hdr_lock);
166 EXPORT_SYMBOL(llog_cancel_rec);
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 = get_seconds();
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;
208 LASSERT(handle->lgh_hdr == NULL);
213 handle->lgh_hdr = llh;
214 /* first assign flags to use llog_client_ops */
215 llh->llh_flags = flags;
216 rc = llog_read_header(env, handle, uuid);
218 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
219 flags & LLOG_F_IS_CAT) ||
220 (llh->llh_flags & LLOG_F_IS_CAT &&
221 flags & LLOG_F_IS_PLAIN))) {
222 CERROR("%s: llog type is %s but initializing %s\n",
223 handle->lgh_ctxt->loc_obd->obd_name,
224 llh->llh_flags & LLOG_F_IS_CAT ?
226 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
229 } else if (llh->llh_flags &
230 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
232 * it is possible to open llog without specifying llog
233 * type so it is taken from llh_flags
235 flags = llh->llh_flags;
237 /* for some reason the llh_flags has no type set */
238 CERROR("llog type is not specified!\n");
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);
252 if (flags & LLOG_F_IS_CAT) {
253 LASSERT(list_empty(&handle->u.chd.chd_head));
254 INIT_LIST_HEAD(&handle->u.chd.chd_head);
255 llh->llh_size = sizeof(struct llog_logid_rec);
256 } else if (!(flags & LLOG_F_IS_PLAIN)) {
257 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
258 handle->lgh_ctxt->loc_obd->obd_name,
259 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
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;
286 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
288 lpi->lpi_rc = -ENOMEM;
293 last_called_index = cd->lpcd_first_idx;
294 index = cd->lpcd_first_idx + 1;
296 if (cd != NULL && cd->lpcd_last_idx)
297 last_index = cd->lpcd_last_idx;
299 last_index = LLOG_BITMAP_BYTES * 8 - 1;
302 struct llog_rec_hdr *rec;
304 /* skip records not set in bitmap */
305 while (index <= last_index &&
306 !ext2_test_bit(index, llh->llh_bitmap))
309 LASSERT(index <= last_index + 1);
310 if (index == last_index + 1)
313 CDEBUG(D_OTHER, "index: %d last_index %d\n",
316 /* get the buf with our target record; avoid old garbage */
317 memset(buf, 0, LLOG_CHUNK_SIZE);
318 last_offset = cur_offset;
319 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
320 index, &cur_offset, buf, LLOG_CHUNK_SIZE);
324 /* NB: when rec->lrh_len is accessed it is already swabbed
325 * since it is used at the "end" of the loop and the rec
326 * swabbing is done at the beginning of the loop. */
327 for (rec = (struct llog_rec_hdr *)buf;
328 (char *)rec < buf + LLOG_CHUNK_SIZE;
329 rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
331 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
334 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
335 lustre_swab_llog_rec(rec);
337 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
338 rec->lrh_type, rec->lrh_index);
340 if (rec->lrh_index == 0) {
341 /* probably another rec just got added? */
343 if (index <= loghandle->lgh_last_idx)
345 goto out; /* 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 index %d/%d\n",
351 rec->lrh_index, index);
356 if (rec->lrh_index < index) {
357 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
363 "lrh_index: %d lrh_len: %d (%d remains)\n",
364 rec->lrh_index, rec->lrh_len,
365 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
367 loghandle->lgh_cur_idx = rec->lrh_index;
368 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
371 /* if set, process the callback on this record */
372 if (ext2_test_bit(index, llh->llh_bitmap)) {
373 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
375 last_called_index = index;
376 if (rc == LLOG_PROC_BREAK) {
378 } else if (rc == LLOG_DEL_RECORD) {
379 llog_cancel_rec(lpi->lpi_env,
387 CDEBUG(D_OTHER, "Skipped index %d\n", index);
390 /* next record, still in buffer? */
392 if (index > last_index) {
401 cd->lpcd_last_idx = last_called_index;
403 OBD_FREE(buf, LLOG_CHUNK_SIZE);
408 static int llog_process_thread_daemonize(void *arg)
410 struct llog_process_info *lpi = arg;
416 /* client env has no keys, tags is just 0 */
417 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
422 rc = llog_process_thread(arg);
426 complete(&lpi->lpi_completion);
430 int llog_process_or_fork(const struct lu_env *env,
431 struct llog_handle *loghandle,
432 llog_cb_t cb, void *data, void *catdata, bool fork)
434 struct llog_process_info *lpi;
439 CERROR("cannot alloc pointer\n");
442 lpi->lpi_loghandle = loghandle;
444 lpi->lpi_cbdata = data;
445 lpi->lpi_catdata = catdata;
448 /* The new thread can't use parent env,
449 * init the new one in llog_process_thread_daemonize. */
451 init_completion(&lpi->lpi_completion);
452 rc = PTR_ERR(kthread_run(llog_process_thread_daemonize, lpi,
453 "llog_process_thread"));
454 if (IS_ERR_VALUE(rc)) {
455 CERROR("%s: cannot start thread: rc = %d\n",
456 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
460 wait_for_completion(&lpi->lpi_completion);
463 llog_process_thread(lpi);
469 EXPORT_SYMBOL(llog_process_or_fork);
471 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
472 llog_cb_t cb, void *data, void *catdata)
474 return llog_process_or_fork(env, loghandle, cb, data, catdata, true);
476 EXPORT_SYMBOL(llog_process);
478 int llog_reverse_process(const struct lu_env *env,
479 struct llog_handle *loghandle, llog_cb_t cb,
480 void *data, void *catdata)
482 struct llog_log_hdr *llh = loghandle->lgh_hdr;
483 struct llog_process_cat_data *cd = catdata;
485 int rc = 0, first_index = 1, index, idx;
487 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
492 first_index = cd->lpcd_first_idx + 1;
493 if (cd != NULL && cd->lpcd_last_idx)
494 index = cd->lpcd_last_idx;
496 index = LLOG_BITMAP_BYTES * 8 - 1;
499 struct llog_rec_hdr *rec;
500 struct llog_rec_tail *tail;
502 /* skip records not set in bitmap */
503 while (index >= first_index &&
504 !ext2_test_bit(index, llh->llh_bitmap))
507 LASSERT(index >= first_index - 1);
508 if (index == first_index - 1)
511 /* get the buf with our target record; avoid old garbage */
512 memset(buf, 0, LLOG_CHUNK_SIZE);
513 rc = llog_prev_block(env, loghandle, index, buf,
519 idx = rec->lrh_index;
520 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
521 while (idx < index) {
522 rec = (void *)rec + rec->lrh_len;
523 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
524 lustre_swab_llog_rec(rec);
527 LASSERT(idx == index);
528 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
530 /* process records in buffer, starting where we found one */
531 while ((void *)tail > buf) {
532 if (tail->lrt_index == 0) {
533 /* no more records */
538 /* if set, process the callback on this record */
539 if (ext2_test_bit(index, llh->llh_bitmap)) {
540 rec = (void *)tail - tail->lrt_len +
543 rc = cb(env, loghandle, rec, data);
544 if (rc == LLOG_PROC_BREAK) {
546 } else if (rc == LLOG_DEL_RECORD) {
547 llog_cancel_rec(env, loghandle,
555 /* previous record, still in buffer? */
557 if (index < first_index) {
561 tail = (void *)tail - tail->lrt_len;
567 OBD_FREE(buf, LLOG_CHUNK_SIZE);
570 EXPORT_SYMBOL(llog_reverse_process);
576 * llog_open - open llog, may not exist
577 * llog_exist - check if llog exists
578 * llog_close - close opened llog, pair for open, frees llog_handle
579 * llog_declare_create - declare llog creation
580 * llog_create - create new llog on disk, need transaction handle
581 * llog_declare_write_rec - declaration of llog write
582 * llog_write_rec - write llog record on disk, need transaction handle
583 * llog_declare_add - declare llog catalog record addition
584 * llog_add - add llog record in catalog, need transaction handle
586 int llog_exist(struct llog_handle *loghandle)
588 struct llog_operations *lop;
591 rc = llog_handle2ops(loghandle, &lop);
594 if (lop->lop_exist == NULL)
597 rc = lop->lop_exist(loghandle);
600 EXPORT_SYMBOL(llog_exist);
602 int llog_declare_create(const struct lu_env *env,
603 struct llog_handle *loghandle, struct thandle *th)
605 struct llog_operations *lop;
608 rc = llog_handle2ops(loghandle, &lop);
611 if (lop->lop_declare_create == NULL)
614 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
616 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
617 rc = lop->lop_declare_create(env, loghandle, th);
619 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
622 EXPORT_SYMBOL(llog_declare_create);
624 int llog_create(const struct lu_env *env, struct llog_handle *handle,
627 struct llog_operations *lop;
630 rc = llog_handle2ops(handle, &lop);
633 if (lop->lop_create == NULL)
636 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
638 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
639 rc = lop->lop_create(env, handle, th);
641 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
644 EXPORT_SYMBOL(llog_create);
646 int llog_declare_write_rec(const struct lu_env *env,
647 struct llog_handle *handle,
648 struct llog_rec_hdr *rec, int idx,
651 struct llog_operations *lop;
654 rc = llog_handle2ops(handle, &lop);
658 if (lop->lop_declare_write_rec == NULL)
661 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
663 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
664 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
666 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
669 EXPORT_SYMBOL(llog_declare_write_rec);
671 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
672 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
673 int numcookies, void *buf, int idx, struct thandle *th)
675 struct llog_operations *lop;
676 int raised, rc, buflen;
678 rc = llog_handle2ops(handle, &lop);
683 if (lop->lop_write_rec == NULL)
687 buflen = rec->lrh_len + sizeof(struct llog_rec_hdr) +
688 sizeof(struct llog_rec_tail);
690 buflen = rec->lrh_len;
691 LASSERT(cfs_size_round(buflen) == buflen);
693 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
695 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
696 rc = lop->lop_write_rec(env, handle, rec, logcookies, numcookies,
699 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
702 EXPORT_SYMBOL(llog_write_rec);
704 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
705 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
706 void *buf, struct thandle *th)
710 if (lgh->lgh_logops->lop_add == NULL)
713 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
715 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
716 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, buf, th);
718 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
721 EXPORT_SYMBOL(llog_add);
723 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
724 struct llog_rec_hdr *rec, struct thandle *th)
728 if (lgh->lgh_logops->lop_declare_add == NULL)
731 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
733 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
734 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
736 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
739 EXPORT_SYMBOL(llog_declare_add);
742 * Helper function to open llog or create it if doesn't exist.
743 * It hides all transaction handling from caller.
745 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
746 struct llog_handle **res, struct llog_logid *logid,
753 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
757 if (llog_exist(*res))
760 LASSERT((*res)->lgh_obj != NULL);
762 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
764 th = dt_trans_create(env, d);
770 rc = llog_declare_create(env, *res, th);
772 rc = dt_trans_start_local(env, d, th);
774 rc = llog_create(env, *res, th);
776 dt_trans_stop(env, d, th);
779 llog_close(env, *res);
782 EXPORT_SYMBOL(llog_open_create);
785 * Helper function to delete existent llog.
787 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
788 struct llog_logid *logid, char *name)
790 struct llog_handle *handle;
793 /* nothing to erase */
794 if (name == NULL && logid == NULL)
797 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
801 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
803 rc = llog_destroy(env, handle);
805 rc2 = llog_close(env, handle);
810 EXPORT_SYMBOL(llog_erase);
813 * Helper function for write record in llog.
814 * It hides all transaction handling from caller.
815 * Valid only with local llog.
817 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
818 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
819 int cookiecount, void *buf, int idx)
821 struct dt_device *dt;
826 LASSERT(loghandle->lgh_ctxt);
827 LASSERT(loghandle->lgh_obj != NULL);
829 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
831 th = dt_trans_create(env, dt);
835 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
839 rc = dt_trans_start_local(env, dt, th);
843 down_write(&loghandle->lgh_lock);
844 rc = llog_write_rec(env, loghandle, rec, reccookie,
845 cookiecount, buf, idx, th);
846 up_write(&loghandle->lgh_lock);
848 dt_trans_stop(env, dt, th);
851 EXPORT_SYMBOL(llog_write);
853 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
854 struct llog_handle **lgh, struct llog_logid *logid,
855 char *name, enum llog_open_param open_param)
861 LASSERT(ctxt->loc_logops);
863 if (ctxt->loc_logops->lop_open == NULL) {
868 *lgh = llog_alloc_handle();
871 (*lgh)->lgh_ctxt = ctxt;
872 (*lgh)->lgh_logops = ctxt->loc_logops;
874 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
876 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
877 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
879 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
881 llog_free_handle(*lgh);
886 EXPORT_SYMBOL(llog_open);
888 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
890 struct llog_operations *lop;
893 rc = llog_handle2ops(loghandle, &lop);
896 if (lop->lop_close == NULL) {
900 rc = lop->lop_close(env, loghandle);
902 llog_handle_put(loghandle);
905 EXPORT_SYMBOL(llog_close);
907 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
910 struct llog_handle *llh;
913 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
915 if (likely(rc == -ENOENT))
920 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
923 rc = llog_get_size(llh);
926 llog_close(env, llh);
928 /* header is record 1 */
931 EXPORT_SYMBOL(llog_is_empty);
933 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
934 struct llog_rec_hdr *rec, void *data)
936 struct llog_handle *copy_llh = data;
938 /* Append all records */
939 return llog_write(env, copy_llh, rec, NULL, 0, NULL, -1);
941 EXPORT_SYMBOL(llog_copy_handler);
943 /* backup plain llog */
944 int llog_backup(const struct lu_env *env, struct obd_device *obd,
945 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
946 char *name, char *backup)
948 struct llog_handle *llh, *bllh;
953 /* open original log */
954 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
956 /* the -ENOENT case is also reported to the caller
957 * but silently so it should handle that if needed.
960 CERROR("%s: failed to open log %s: rc = %d\n",
961 obd->obd_name, name, rc);
965 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
969 /* Make sure there's no old backup log */
970 rc = llog_erase(env, bctxt, NULL, backup);
971 if (rc < 0 && rc != -ENOENT)
974 /* open backup log */
975 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
977 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
978 obd->obd_name, backup, rc);
982 /* check that backup llog is not the same object as original one */
983 if (llh->lgh_obj == bllh->lgh_obj) {
984 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
985 obd->obd_name, name, backup, llh->lgh_obj,
991 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
995 /* Copy log record by record */
996 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
999 CERROR("%s: failed to backup log %s: rc = %d\n",
1000 obd->obd_name, name, rc);
1002 llog_close(env, bllh);
1004 llog_close(env, llh);
1007 EXPORT_SYMBOL(llog_backup);