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 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;
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");
227 GOTO(out, rc = -EINVAL);
228 } else if (llh->llh_flags &
229 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
231 * it is possible to open llog without specifying llog
232 * type so it is taken from llh_flags
234 flags = llh->llh_flags;
236 /* for some reason the llh_flags has no type set */
237 CERROR("llog type is not specified!\n");
238 GOTO(out, rc = -EINVAL);
241 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
242 CERROR("%s: llog uuid mismatch: %s/%s\n",
243 handle->lgh_ctxt->loc_obd->obd_name,
245 (char *)llh->llh_tgtuuid.uuid);
246 GOTO(out, rc = -EEXIST);
249 if (flags & LLOG_F_IS_CAT) {
250 LASSERT(list_empty(&handle->u.chd.chd_head));
251 INIT_LIST_HEAD(&handle->u.chd.chd_head);
252 llh->llh_size = sizeof(struct llog_logid_rec);
253 } else if (!(flags & LLOG_F_IS_PLAIN)) {
254 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
255 handle->lgh_ctxt->loc_obd->obd_name,
256 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
262 handle->lgh_hdr = NULL;
266 EXPORT_SYMBOL(llog_init_handle);
268 static int llog_process_thread(void *arg)
270 struct llog_process_info *lpi = arg;
271 struct llog_handle *loghandle = lpi->lpi_loghandle;
272 struct llog_log_hdr *llh = loghandle->lgh_hdr;
273 struct llog_process_cat_data *cd = lpi->lpi_catdata;
275 __u64 cur_offset = LLOG_CHUNK_SIZE;
277 int rc = 0, index = 1, last_index;
279 int last_called_index = 0;
283 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
285 lpi->lpi_rc = -ENOMEM;
290 last_called_index = cd->lpcd_first_idx;
291 index = cd->lpcd_first_idx + 1;
293 if (cd != NULL && cd->lpcd_last_idx)
294 last_index = cd->lpcd_last_idx;
296 last_index = LLOG_BITMAP_BYTES * 8 - 1;
299 struct llog_rec_hdr *rec;
301 /* skip records not set in bitmap */
302 while (index <= last_index &&
303 !ext2_test_bit(index, llh->llh_bitmap))
306 LASSERT(index <= last_index + 1);
307 if (index == last_index + 1)
310 CDEBUG(D_OTHER, "index: %d last_index %d\n",
313 /* get the buf with our target record; avoid old garbage */
314 memset(buf, 0, LLOG_CHUNK_SIZE);
315 last_offset = cur_offset;
316 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
317 index, &cur_offset, buf, LLOG_CHUNK_SIZE);
321 /* NB: when rec->lrh_len is accessed it is already swabbed
322 * since it is used at the "end" of the loop and the rec
323 * swabbing is done at the beginning of the loop. */
324 for (rec = (struct llog_rec_hdr *)buf;
325 (char *)rec < buf + LLOG_CHUNK_SIZE;
326 rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
328 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
331 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
332 lustre_swab_llog_rec(rec);
334 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
335 rec->lrh_type, rec->lrh_index);
337 if (rec->lrh_index == 0) {
338 /* probably another rec just got added? */
339 if (index <= loghandle->lgh_last_idx)
340 GOTO(repeat, rc = 0);
341 GOTO(out, rc = 0); /* no more records */
343 if (rec->lrh_len == 0 ||
344 rec->lrh_len > LLOG_CHUNK_SIZE) {
345 CWARN("invalid length %d in llog record for "
346 "index %d/%d\n", rec->lrh_len,
347 rec->lrh_index, index);
348 GOTO(out, rc = -EINVAL);
351 if (rec->lrh_index < index) {
352 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
358 "lrh_index: %d lrh_len: %d (%d remains)\n",
359 rec->lrh_index, rec->lrh_len,
360 (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
362 loghandle->lgh_cur_idx = rec->lrh_index;
363 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
366 /* if set, process the callback on this record */
367 if (ext2_test_bit(index, llh->llh_bitmap)) {
368 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
370 last_called_index = index;
371 if (rc == LLOG_PROC_BREAK) {
373 } else if (rc == LLOG_DEL_RECORD) {
374 llog_cancel_rec(lpi->lpi_env,
382 CDEBUG(D_OTHER, "Skipped index %d\n", index);
385 /* next record, still in buffer? */
387 if (index > last_index)
394 cd->lpcd_last_idx = last_called_index;
396 OBD_FREE(buf, LLOG_CHUNK_SIZE);
401 static int llog_process_thread_daemonize(void *arg)
403 struct llog_process_info *lpi = arg;
409 /* client env has no keys, tags is just 0 */
410 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
415 rc = llog_process_thread(arg);
419 complete(&lpi->lpi_completion);
423 int llog_process_or_fork(const struct lu_env *env,
424 struct llog_handle *loghandle,
425 llog_cb_t cb, void *data, void *catdata, bool fork)
427 struct llog_process_info *lpi;
432 CERROR("cannot alloc pointer\n");
435 lpi->lpi_loghandle = loghandle;
437 lpi->lpi_cbdata = data;
438 lpi->lpi_catdata = catdata;
441 /* The new thread can't use parent env,
442 * init the new one in llog_process_thread_daemonize. */
444 init_completion(&lpi->lpi_completion);
445 rc = PTR_ERR(kthread_run(llog_process_thread_daemonize, lpi,
446 "llog_process_thread"));
447 if (IS_ERR_VALUE(rc)) {
448 CERROR("%s: cannot start thread: rc = %d\n",
449 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
453 wait_for_completion(&lpi->lpi_completion);
456 llog_process_thread(lpi);
462 EXPORT_SYMBOL(llog_process_or_fork);
464 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
465 llog_cb_t cb, void *data, void *catdata)
467 return llog_process_or_fork(env, loghandle, cb, data, catdata, true);
469 EXPORT_SYMBOL(llog_process);
471 int llog_reverse_process(const struct lu_env *env,
472 struct llog_handle *loghandle, llog_cb_t cb,
473 void *data, void *catdata)
475 struct llog_log_hdr *llh = loghandle->lgh_hdr;
476 struct llog_process_cat_data *cd = catdata;
478 int rc = 0, first_index = 1, index, idx;
480 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
485 first_index = cd->lpcd_first_idx + 1;
486 if (cd != NULL && cd->lpcd_last_idx)
487 index = cd->lpcd_last_idx;
489 index = LLOG_BITMAP_BYTES * 8 - 1;
492 struct llog_rec_hdr *rec;
493 struct llog_rec_tail *tail;
495 /* skip records not set in bitmap */
496 while (index >= first_index &&
497 !ext2_test_bit(index, llh->llh_bitmap))
500 LASSERT(index >= first_index - 1);
501 if (index == first_index - 1)
504 /* get the buf with our target record; avoid old garbage */
505 memset(buf, 0, LLOG_CHUNK_SIZE);
506 rc = llog_prev_block(env, loghandle, index, buf,
512 idx = rec->lrh_index;
513 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
514 while (idx < index) {
515 rec = (void *)rec + rec->lrh_len;
516 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
517 lustre_swab_llog_rec(rec);
520 LASSERT(idx == index);
521 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
523 /* process records in buffer, starting where we found one */
524 while ((void *)tail > buf) {
525 if (tail->lrt_index == 0)
526 GOTO(out, rc = 0); /* no more records */
528 /* if set, process the callback on this record */
529 if (ext2_test_bit(index, llh->llh_bitmap)) {
530 rec = (void *)tail - tail->lrt_len +
533 rc = cb(env, loghandle, rec, data);
534 if (rc == LLOG_PROC_BREAK) {
536 } else if (rc == LLOG_DEL_RECORD) {
537 llog_cancel_rec(env, loghandle,
545 /* previous record, still in buffer? */
547 if (index < first_index)
549 tail = (void *)tail - tail->lrt_len;
555 OBD_FREE(buf, LLOG_CHUNK_SIZE);
558 EXPORT_SYMBOL(llog_reverse_process);
564 * llog_open - open llog, may not exist
565 * llog_exist - check if llog exists
566 * llog_close - close opened llog, pair for open, frees llog_handle
567 * llog_declare_create - declare llog creation
568 * llog_create - create new llog on disk, need transaction handle
569 * llog_declare_write_rec - declaration of llog write
570 * llog_write_rec - write llog record on disk, need transaction handle
571 * llog_declare_add - declare llog catalog record addition
572 * llog_add - add llog record in catalog, need transaction handle
574 int llog_exist(struct llog_handle *loghandle)
576 struct llog_operations *lop;
579 rc = llog_handle2ops(loghandle, &lop);
582 if (lop->lop_exist == NULL)
585 rc = lop->lop_exist(loghandle);
588 EXPORT_SYMBOL(llog_exist);
590 int llog_declare_create(const struct lu_env *env,
591 struct llog_handle *loghandle, struct thandle *th)
593 struct llog_operations *lop;
596 rc = llog_handle2ops(loghandle, &lop);
599 if (lop->lop_declare_create == NULL)
602 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
604 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
605 rc = lop->lop_declare_create(env, loghandle, th);
607 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
610 EXPORT_SYMBOL(llog_declare_create);
612 int llog_create(const struct lu_env *env, struct llog_handle *handle,
615 struct llog_operations *lop;
618 rc = llog_handle2ops(handle, &lop);
621 if (lop->lop_create == NULL)
624 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
626 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
627 rc = lop->lop_create(env, handle, th);
629 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
632 EXPORT_SYMBOL(llog_create);
634 int llog_declare_write_rec(const struct lu_env *env,
635 struct llog_handle *handle,
636 struct llog_rec_hdr *rec, int idx,
639 struct llog_operations *lop;
642 rc = llog_handle2ops(handle, &lop);
646 if (lop->lop_declare_write_rec == NULL)
649 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
651 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
652 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
654 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
657 EXPORT_SYMBOL(llog_declare_write_rec);
659 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
660 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
661 int numcookies, void *buf, int idx, struct thandle *th)
663 struct llog_operations *lop;
664 int raised, rc, buflen;
666 rc = llog_handle2ops(handle, &lop);
671 if (lop->lop_write_rec == NULL)
675 buflen = rec->lrh_len + sizeof(struct llog_rec_hdr) +
676 sizeof(struct llog_rec_tail);
678 buflen = rec->lrh_len;
679 LASSERT(cfs_size_round(buflen) == buflen);
681 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
683 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
684 rc = lop->lop_write_rec(env, handle, rec, logcookies, numcookies,
687 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
690 EXPORT_SYMBOL(llog_write_rec);
692 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
693 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
694 void *buf, struct thandle *th)
698 if (lgh->lgh_logops->lop_add == NULL)
701 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
703 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
704 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, buf, th);
706 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
709 EXPORT_SYMBOL(llog_add);
711 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
712 struct llog_rec_hdr *rec, struct thandle *th)
716 if (lgh->lgh_logops->lop_declare_add == NULL)
719 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
721 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
722 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
724 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
727 EXPORT_SYMBOL(llog_declare_add);
730 * Helper function to open llog or create it if doesn't exist.
731 * It hides all transaction handling from caller.
733 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
734 struct llog_handle **res, struct llog_logid *logid,
741 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
745 if (llog_exist(*res))
748 LASSERT((*res)->lgh_obj != NULL);
750 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
752 th = dt_trans_create(env, d);
754 GOTO(out, rc = PTR_ERR(th));
756 rc = llog_declare_create(env, *res, th);
758 rc = dt_trans_start_local(env, d, th);
760 rc = llog_create(env, *res, th);
762 dt_trans_stop(env, d, th);
765 llog_close(env, *res);
768 EXPORT_SYMBOL(llog_open_create);
771 * Helper function to delete existent llog.
773 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
774 struct llog_logid *logid, char *name)
776 struct llog_handle *handle;
779 /* nothing to erase */
780 if (name == NULL && logid == NULL)
783 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
787 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
789 rc = llog_destroy(env, handle);
791 rc2 = llog_close(env, handle);
796 EXPORT_SYMBOL(llog_erase);
799 * Helper function for write record in llog.
800 * It hides all transaction handling from caller.
801 * Valid only with local llog.
803 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
804 struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
805 int cookiecount, void *buf, int idx)
807 struct dt_device *dt;
812 LASSERT(loghandle->lgh_ctxt);
813 LASSERT(loghandle->lgh_obj != NULL);
815 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
817 th = dt_trans_create(env, dt);
821 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
825 rc = dt_trans_start_local(env, dt, th);
829 down_write(&loghandle->lgh_lock);
830 rc = llog_write_rec(env, loghandle, rec, reccookie,
831 cookiecount, buf, idx, th);
832 up_write(&loghandle->lgh_lock);
834 dt_trans_stop(env, dt, th);
837 EXPORT_SYMBOL(llog_write);
839 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
840 struct llog_handle **lgh, struct llog_logid *logid,
841 char *name, enum llog_open_param open_param)
847 LASSERT(ctxt->loc_logops);
849 if (ctxt->loc_logops->lop_open == NULL) {
854 *lgh = llog_alloc_handle();
857 (*lgh)->lgh_ctxt = ctxt;
858 (*lgh)->lgh_logops = ctxt->loc_logops;
860 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
862 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
863 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
865 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
867 llog_free_handle(*lgh);
872 EXPORT_SYMBOL(llog_open);
874 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
876 struct llog_operations *lop;
879 rc = llog_handle2ops(loghandle, &lop);
882 if (lop->lop_close == NULL)
883 GOTO(out, rc = -EOPNOTSUPP);
884 rc = lop->lop_close(env, loghandle);
886 llog_handle_put(loghandle);
889 EXPORT_SYMBOL(llog_close);
891 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
894 struct llog_handle *llh;
897 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
899 if (likely(rc == -ENOENT))
904 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
907 rc = llog_get_size(llh);
910 llog_close(env, llh);
912 /* header is record 1 */
915 EXPORT_SYMBOL(llog_is_empty);
917 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
918 struct llog_rec_hdr *rec, void *data)
920 struct llog_handle *copy_llh = data;
922 /* Append all records */
923 return llog_write(env, copy_llh, rec, NULL, 0, NULL, -1);
925 EXPORT_SYMBOL(llog_copy_handler);
927 /* backup plain llog */
928 int llog_backup(const struct lu_env *env, struct obd_device *obd,
929 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
930 char *name, char *backup)
932 struct llog_handle *llh, *bllh;
937 /* open original log */
938 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
940 /* the -ENOENT case is also reported to the caller
941 * but silently so it should handle that if needed.
944 CERROR("%s: failed to open log %s: rc = %d\n",
945 obd->obd_name, name, rc);
949 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
953 /* Make sure there's no old backup log */
954 rc = llog_erase(env, bctxt, NULL, backup);
955 if (rc < 0 && rc != -ENOENT)
958 /* open backup log */
959 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
961 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
962 obd->obd_name, backup, rc);
966 /* check that backup llog is not the same object as original one */
967 if (llh->lgh_obj == bllh->lgh_obj) {
968 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
969 obd->obd_name, name, backup, llh->lgh_obj,
971 GOTO(out_backup, rc = -EEXIST);
974 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
976 GOTO(out_backup, rc);
978 /* Copy log record by record */
979 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
982 CERROR("%s: failed to backup log %s: rc = %d\n",
983 obd->obd_name, name, rc);
985 llog_close(env, bllh);
987 llog_close(env, llh);
990 EXPORT_SYMBOL(llog_backup);