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;
288 int rc = 0, index = 1, last_index;
290 int last_called_index = 0;
297 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
298 /* expect chunk_size to be power of two */
299 LASSERT(is_power_of_2(chunk_size));
301 OBD_ALLOC_LARGE(buf, chunk_size);
303 lpi->lpi_rc = -ENOMEM;
308 last_called_index = cd->lpcd_first_idx;
309 index = cd->lpcd_first_idx + 1;
311 if (cd != NULL && cd->lpcd_last_idx)
312 last_index = cd->lpcd_last_idx;
314 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
317 struct llog_rec_hdr *rec;
319 unsigned int buf_offset = 0;
322 /* skip records not set in bitmap */
323 while (index <= last_index &&
324 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
327 /* There are no indices prior the last_index */
328 if (index > last_index)
331 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
335 /* get the buf with our target record; avoid old garbage */
336 memset(buf, 0, chunk_size);
337 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
338 index, &cur_offset, buf, chunk_size);
342 /* NB: after llog_next_block() call the cur_offset is the
343 * offset of the next block after read one.
344 * The absolute offset of the current chunk is calculated
345 * from cur_offset value and stored in chunk_offset variable.
347 if (cur_offset % chunk_size != 0) {
348 partial_chunk = true;
349 chunk_offset = cur_offset & ~(chunk_size - 1);
351 partial_chunk = false;
352 chunk_offset = cur_offset - chunk_size;
355 /* NB: when rec->lrh_len is accessed it is already swabbed
356 * since it is used at the "end" of the loop and the rec
357 * swabbing is done at the beginning of the loop. */
358 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
359 (char *)rec < buf + chunk_size;
360 rec = llog_rec_hdr_next(rec)) {
362 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
365 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
366 lustre_swab_llog_rec(rec);
368 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
369 rec->lrh_type, rec->lrh_index);
371 /* for partial chunk the end of it is zeroed, check
372 * for index 0 to distinguish it. */
373 if (partial_chunk && rec->lrh_index == 0) {
374 /* concurrent llog_add() might add new records
375 * while llog_processing, check this is not
376 * the case and re-read the current chunk
378 if (index > loghandle->lgh_last_idx)
380 CDEBUG(D_OTHER, "Re-read last llog buffer for "
381 "new records, index %u, last %u\n",
382 index, loghandle->lgh_last_idx);
383 /* save offset inside buffer for the re-read */
384 buf_offset = (char *)rec - (char *)buf;
385 cur_offset = chunk_offset;
389 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
390 CWARN("invalid length %d in llog record for "
391 "index %d/%d\n", rec->lrh_len,
392 rec->lrh_index, index);
393 GOTO(out, rc = -EINVAL);
396 if (rec->lrh_index < index) {
397 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
402 if (rec->lrh_index != index) {
403 CERROR("%s: Invalid record: index %u but "
405 loghandle->lgh_ctxt->loc_obd->obd_name,
406 rec->lrh_index, index);
407 GOTO(out, rc = -ERANGE);
411 "lrh_index: %d lrh_len: %d (%d remains)\n",
412 rec->lrh_index, rec->lrh_len,
413 (int)(buf + chunk_size - (char *)rec));
415 loghandle->lgh_cur_idx = rec->lrh_index;
416 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
419 /* if set, process the callback on this record */
420 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
421 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
423 last_called_index = index;
424 if (rc == LLOG_PROC_BREAK) {
426 } else if (rc == LLOG_DEL_RECORD) {
427 rc = llog_cancel_rec(lpi->lpi_env,
434 /* exit if the last index is reached */
435 if (index >= last_index)
443 cd->lpcd_last_idx = last_called_index;
445 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
446 /* something bad happened to the processing of a local
447 * llog file, probably I/O error or the log got corrupted..
448 * to be able to finally release the log we discard any
449 * remaining bits in the header */
450 CERROR("Local llog found corrupted\n");
451 while (index <= last_index) {
452 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh)) != 0)
453 llog_cancel_rec(lpi->lpi_env, loghandle, index);
459 OBD_FREE_LARGE(buf, chunk_size);
464 static int llog_process_thread_daemonize(void *arg)
466 struct llog_process_info *lpi = arg;
472 /* client env has no keys, tags is just 0 */
473 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
478 rc = llog_process_thread(arg);
482 complete(&lpi->lpi_completion);
486 int llog_process_or_fork(const struct lu_env *env,
487 struct llog_handle *loghandle,
488 llog_cb_t cb, void *data, void *catdata, bool fork)
490 struct llog_process_info *lpi;
497 CERROR("cannot alloc pointer\n");
500 lpi->lpi_loghandle = loghandle;
502 lpi->lpi_cbdata = data;
503 lpi->lpi_catdata = catdata;
506 struct task_struct *task;
508 /* The new thread can't use parent env,
509 * init the new one in llog_process_thread_daemonize. */
511 init_completion(&lpi->lpi_completion);
512 task = kthread_run(llog_process_thread_daemonize, lpi,
513 "llog_process_thread");
516 CERROR("%s: cannot start thread: rc = %d\n",
517 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
520 wait_for_completion(&lpi->lpi_completion);
523 llog_process_thread(lpi);
531 EXPORT_SYMBOL(llog_process_or_fork);
533 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
534 llog_cb_t cb, void *data, void *catdata)
537 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
538 return rc == LLOG_DEL_PLAIN ? 0 : rc;
540 EXPORT_SYMBOL(llog_process);
542 int llog_reverse_process(const struct lu_env *env,
543 struct llog_handle *loghandle, llog_cb_t cb,
544 void *data, void *catdata)
546 struct llog_log_hdr *llh = loghandle->lgh_hdr;
547 struct llog_process_cat_data *cd = catdata;
549 int rc = 0, first_index = 1, index, idx;
550 __u32 chunk_size = llh->llh_hdr.lrh_len;
553 OBD_ALLOC_LARGE(buf, chunk_size);
558 first_index = cd->lpcd_first_idx + 1;
559 if (cd != NULL && cd->lpcd_last_idx)
560 index = cd->lpcd_last_idx;
562 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
565 struct llog_rec_hdr *rec;
566 struct llog_rec_tail *tail;
568 /* skip records not set in bitmap */
569 while (index >= first_index &&
570 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
573 LASSERT(index >= first_index - 1);
574 if (index == first_index - 1)
577 /* get the buf with our target record; avoid old garbage */
578 memset(buf, 0, chunk_size);
579 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
584 idx = rec->lrh_index;
585 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
586 while (idx < index) {
587 rec = (void *)rec + rec->lrh_len;
588 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
589 lustre_swab_llog_rec(rec);
592 LASSERT(idx == index);
593 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
595 /* process records in buffer, starting where we found one */
596 while ((void *)tail > buf) {
597 if (tail->lrt_index == 0)
598 GOTO(out, rc = 0); /* no more records */
600 /* if set, process the callback on this record */
601 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
602 rec = (void *)tail - tail->lrt_len +
605 rc = cb(env, loghandle, rec, data);
606 if (rc == LLOG_PROC_BREAK) {
608 } else if (rc == LLOG_DEL_RECORD) {
609 rc = llog_cancel_rec(env, loghandle,
616 /* previous record, still in buffer? */
618 if (index < first_index)
620 tail = (void *)tail - tail->lrt_len;
626 OBD_FREE_LARGE(buf, chunk_size);
629 EXPORT_SYMBOL(llog_reverse_process);
635 * llog_open - open llog, may not exist
636 * llog_exist - check if llog exists
637 * llog_close - close opened llog, pair for open, frees llog_handle
638 * llog_declare_create - declare llog creation
639 * llog_create - create new llog on disk, need transaction handle
640 * llog_declare_write_rec - declaration of llog write
641 * llog_write_rec - write llog record on disk, need transaction handle
642 * llog_declare_add - declare llog catalog record addition
643 * llog_add - add llog record in catalog, need transaction handle
645 int llog_exist(struct llog_handle *loghandle)
647 struct llog_operations *lop;
652 rc = llog_handle2ops(loghandle, &lop);
655 if (lop->lop_exist == NULL)
658 rc = lop->lop_exist(loghandle);
661 EXPORT_SYMBOL(llog_exist);
663 int llog_declare_create(const struct lu_env *env,
664 struct llog_handle *loghandle, struct thandle *th)
666 struct llog_operations *lop;
671 rc = llog_handle2ops(loghandle, &lop);
674 if (lop->lop_declare_create == NULL)
677 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
679 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
680 rc = lop->lop_declare_create(env, loghandle, th);
682 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
686 int llog_create(const struct lu_env *env, struct llog_handle *handle,
689 struct llog_operations *lop;
694 rc = llog_handle2ops(handle, &lop);
697 if (lop->lop_create == NULL)
700 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
702 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
703 rc = lop->lop_create(env, handle, th);
705 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
709 int llog_declare_write_rec(const struct lu_env *env,
710 struct llog_handle *handle,
711 struct llog_rec_hdr *rec, int idx,
714 struct llog_operations *lop;
719 rc = llog_handle2ops(handle, &lop);
723 if (lop->lop_declare_write_rec == NULL)
726 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
728 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
729 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
731 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
735 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
736 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
737 int idx, struct thandle *th)
739 struct llog_operations *lop;
740 int raised, rc, buflen;
744 rc = llog_handle2ops(handle, &lop);
749 if (lop->lop_write_rec == NULL)
752 buflen = rec->lrh_len;
753 LASSERT(cfs_size_round(buflen) == buflen);
755 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
757 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
758 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
760 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
764 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
765 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
772 if (lgh->lgh_logops->lop_add == NULL)
775 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
777 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
778 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
780 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
783 EXPORT_SYMBOL(llog_add);
785 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
786 struct llog_rec_hdr *rec, struct thandle *th)
792 if (lgh->lgh_logops->lop_declare_add == NULL)
795 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
797 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
798 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
800 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
803 EXPORT_SYMBOL(llog_declare_add);
806 * Helper function to open llog or create it if doesn't exist.
807 * It hides all transaction handling from caller.
809 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
810 struct llog_handle **res, struct llog_logid *logid,
819 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
823 if (llog_exist(*res))
826 LASSERT((*res)->lgh_obj != NULL);
828 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
830 th = dt_trans_create(env, d);
832 GOTO(out, rc = PTR_ERR(th));
834 /* Create the remote update llog object synchronously, which
835 * happens during inialization process see lod_sub_prep_llog(),
836 * to make sure the update llog object is created before
837 * corss-MDT writing updates into the llog object */
838 if (dt_object_remote((*res)->lgh_obj))
841 th->th_wait_submit = 1;
842 rc = llog_declare_create(env, *res, th);
844 rc = dt_trans_start_local(env, d, th);
846 rc = llog_create(env, *res, th);
848 dt_trans_stop(env, d, th);
851 llog_close(env, *res);
854 EXPORT_SYMBOL(llog_open_create);
857 * Helper function to delete existent llog.
859 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
860 struct llog_logid *logid, char *name)
862 struct llog_handle *handle;
867 /* nothing to erase */
868 if (name == NULL && logid == NULL)
871 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
875 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
877 rc = llog_destroy(env, handle);
879 rc2 = llog_close(env, handle);
884 EXPORT_SYMBOL(llog_erase);
887 * Helper function for write record in llog.
888 * It hides all transaction handling from caller.
889 * Valid only with local llog.
891 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
892 struct llog_rec_hdr *rec, int idx)
894 struct dt_device *dt;
901 LASSERT(loghandle->lgh_ctxt);
902 LASSERT(loghandle->lgh_obj != NULL);
904 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
906 th = dt_trans_create(env, dt);
910 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
914 th->th_wait_submit = 1;
915 rc = dt_trans_start_local(env, dt, th);
919 down_write(&loghandle->lgh_lock);
920 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
921 up_write(&loghandle->lgh_lock);
923 dt_trans_stop(env, dt, th);
926 EXPORT_SYMBOL(llog_write);
928 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
929 struct llog_handle **lgh, struct llog_logid *logid,
930 char *name, enum llog_open_param open_param)
938 LASSERT(ctxt->loc_logops);
940 if (ctxt->loc_logops->lop_open == NULL) {
945 *lgh = llog_alloc_handle();
948 (*lgh)->lgh_ctxt = ctxt;
949 (*lgh)->lgh_logops = ctxt->loc_logops;
951 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
953 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
954 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
956 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
958 llog_free_handle(*lgh);
963 EXPORT_SYMBOL(llog_open);
965 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
967 struct llog_operations *lop;
972 rc = llog_handle2ops(loghandle, &lop);
975 if (lop->lop_close == NULL)
976 GOTO(out, rc = -EOPNOTSUPP);
977 rc = lop->lop_close(env, loghandle);
979 llog_handle_put(loghandle);
982 EXPORT_SYMBOL(llog_close);
985 * Helper function to get the llog size in records. It is used by MGS
986 * mostly to check that config llog exists and contains data.
988 * \param[in] env execution environment
989 * \param[in] ctxt llog context
990 * \param[in] name llog name
992 * \retval true if there are records in llog besides a header
993 * \retval false on error or llog without records
995 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
998 struct llog_handle *llh;
1001 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1003 if (likely(rc == -ENOENT))
1008 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1010 GOTO(out_close, rc);
1011 rc = llog_get_size(llh);
1014 llog_close(env, llh);
1016 /* The header is record 1, the llog is still considered as empty
1017 * if there is only header */
1020 EXPORT_SYMBOL(llog_is_empty);
1022 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1023 struct llog_rec_hdr *rec, void *data)
1025 struct llog_handle *copy_llh = data;
1027 /* Append all records */
1028 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1031 /* backup plain llog */
1032 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1033 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1034 char *name, char *backup)
1036 struct llog_handle *llh, *bllh;
1041 /* open original log */
1042 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1044 /* the -ENOENT case is also reported to the caller
1045 * but silently so it should handle that if needed.
1048 CERROR("%s: failed to open log %s: rc = %d\n",
1049 obd->obd_name, name, rc);
1053 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1055 GOTO(out_close, rc);
1057 /* Make sure there's no old backup log */
1058 rc = llog_erase(env, bctxt, NULL, backup);
1059 if (rc < 0 && rc != -ENOENT)
1060 GOTO(out_close, rc);
1062 /* open backup log */
1063 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1065 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1066 obd->obd_name, backup, rc);
1067 GOTO(out_close, rc);
1070 /* check that backup llog is not the same object as original one */
1071 if (llh->lgh_obj == bllh->lgh_obj) {
1072 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1073 obd->obd_name, name, backup, llh->lgh_obj,
1075 GOTO(out_backup, rc = -EEXIST);
1078 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1080 GOTO(out_backup, rc);
1082 /* Copy log record by record */
1083 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1086 CERROR("%s: failed to backup log %s: rc = %d\n",
1087 obd->obd_name, name, rc);
1089 llog_close(env, bllh);
1091 llog_close(env, llh);
1094 EXPORT_SYMBOL(llog_backup);