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 mutex_init(&loghandle->lgh_hdr_mutex);
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 static int llog_cancel_rec_internal(const struct lu_env *env,
108 struct llog_handle *loghandle, int index)
110 struct dt_device *dt;
111 struct llog_log_hdr *llh = loghandle->lgh_hdr;
118 LASSERT(loghandle->lgh_ctxt);
119 LASSERT(loghandle->lgh_obj != NULL);
121 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
123 th = dt_trans_create(env, dt);
127 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
131 th->th_wait_submit = 1;
132 rc = dt_trans_start_local(env, dt, th);
136 down_write(&loghandle->lgh_lock);
138 mutex_lock(&loghandle->lgh_hdr_mutex);
139 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
140 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
141 GOTO(out_unlock, rc);
144 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, NULL,
145 LLOG_HEADER_IDX, th);
147 loghandle->lgh_hdr->llh_count--;
149 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
151 mutex_unlock(&loghandle->lgh_hdr_mutex);
152 up_write(&loghandle->lgh_lock);
154 dt_trans_stop(env, dt, th);
158 /* returns negative on error; 0 if success; 1 if success & log destroyed */
159 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
162 struct llog_log_hdr *llh = loghandle->lgh_hdr;
166 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n",
167 index, POSTID(&loghandle->lgh_id.lgl_oi));
170 CERROR("Can't cancel index 0 which is header\n");
174 rc = llog_cancel_rec_internal(env, loghandle, index);
176 CERROR("%s: fail to write header for llog #"DOSTID
178 loghandle->lgh_ctxt->loc_obd->obd_name,
179 POSTID(&loghandle->lgh_id.lgl_oi),
180 loghandle->lgh_id.lgl_ogen, rc);
184 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
185 (llh->llh_count == 1) &&
186 (loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1)) {
187 rc = llog_destroy(env, loghandle);
189 /* Sigh, can not destroy the final plain llog, but
190 * the bitmap has been clearly, so the record can not
191 * be accessed anymore, let's return 0 for now, and
192 * the orphan will be handled by LFSCK. */
193 CERROR("%s: can't destroy empty llog #"DOSTID
195 loghandle->lgh_ctxt->loc_obd->obd_name,
196 POSTID(&loghandle->lgh_id.lgl_oi),
197 loghandle->lgh_id.lgl_ogen, rc);
200 RETURN(LLOG_DEL_PLAIN);
206 static int llog_read_header(const struct lu_env *env,
207 struct llog_handle *handle,
208 struct obd_uuid *uuid)
210 struct llog_operations *lop;
213 rc = llog_handle2ops(handle, &lop);
217 if (lop->lop_read_header == NULL)
220 rc = lop->lop_read_header(env, handle);
221 if (rc == LLOG_EEMPTY) {
222 struct llog_log_hdr *llh = handle->lgh_hdr;
224 /* lrh_len should be initialized in llog_init_handle */
225 handle->lgh_last_idx = 0; /* header is record with index 0 */
226 llh->llh_count = 1; /* for the header record */
227 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
228 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
229 LLOG_MIN_CHUNK_SIZE);
230 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
231 llh->llh_hdr.lrh_index = 0;
232 llh->llh_timestamp = cfs_time_current_sec();
234 memcpy(&llh->llh_tgtuuid, uuid,
235 sizeof(llh->llh_tgtuuid));
236 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
237 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
238 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
239 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
245 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
246 int flags, struct obd_uuid *uuid)
248 struct llog_log_hdr *llh;
249 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
251 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
254 LASSERT(handle->lgh_hdr == NULL);
256 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
257 OBD_ALLOC_LARGE(llh, chunk_size);
261 handle->lgh_hdr = llh;
262 handle->lgh_hdr_size = chunk_size;
263 /* first assign flags to use llog_client_ops */
264 llh->llh_flags = flags;
265 rc = llog_read_header(env, handle, uuid);
267 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
268 flags & LLOG_F_IS_CAT) ||
269 (llh->llh_flags & LLOG_F_IS_CAT &&
270 flags & LLOG_F_IS_PLAIN))) {
271 CERROR("%s: llog type is %s but initializing %s\n",
272 handle->lgh_ctxt->loc_obd->obd_name,
273 llh->llh_flags & LLOG_F_IS_CAT ?
275 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
276 GOTO(out, rc = -EINVAL);
277 } else if (llh->llh_flags &
278 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
280 * it is possible to open llog without specifying llog
281 * type so it is taken from llh_flags
283 flags = llh->llh_flags;
285 /* for some reason the llh_flags has no type set */
286 CERROR("llog type is not specified!\n");
287 GOTO(out, rc = -EINVAL);
290 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
291 CERROR("%s: llog uuid mismatch: %s/%s\n",
292 handle->lgh_ctxt->loc_obd->obd_name,
294 (char *)llh->llh_tgtuuid.uuid);
295 GOTO(out, rc = -EEXIST);
298 if (flags & LLOG_F_IS_CAT) {
299 LASSERT(list_empty(&handle->u.chd.chd_head));
300 INIT_LIST_HEAD(&handle->u.chd.chd_head);
301 llh->llh_size = sizeof(struct llog_logid_rec);
302 } else if (!(flags & LLOG_F_IS_PLAIN)) {
303 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
304 handle->lgh_ctxt->loc_obd->obd_name,
305 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
308 llh->llh_flags |= fmt;
311 OBD_FREE_LARGE(llh, chunk_size);
312 handle->lgh_hdr = NULL;
316 EXPORT_SYMBOL(llog_init_handle);
318 static int llog_process_thread(void *arg)
320 struct llog_process_info *lpi = arg;
321 struct llog_handle *loghandle = lpi->lpi_loghandle;
322 struct llog_log_hdr *llh = loghandle->lgh_hdr;
323 struct llog_process_cat_data *cd = lpi->lpi_catdata;
327 int rc = 0, index = 1, last_index;
329 int last_called_index = 0;
336 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
337 /* expect chunk_size to be power of two */
338 LASSERT(is_power_of_2(chunk_size));
340 OBD_ALLOC_LARGE(buf, chunk_size);
342 lpi->lpi_rc = -ENOMEM;
347 last_called_index = cd->lpcd_first_idx;
348 index = cd->lpcd_first_idx + 1;
350 if (cd != NULL && cd->lpcd_last_idx)
351 last_index = cd->lpcd_last_idx;
353 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
356 struct llog_rec_hdr *rec;
358 unsigned int buf_offset = 0;
361 /* skip records not set in bitmap */
362 while (index <= last_index &&
363 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
366 /* There are no indices prior the last_index */
367 if (index > last_index)
370 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
374 /* get the buf with our target record; avoid old garbage */
375 memset(buf, 0, chunk_size);
376 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
377 index, &cur_offset, buf, chunk_size);
381 /* NB: after llog_next_block() call the cur_offset is the
382 * offset of the next block after read one.
383 * The absolute offset of the current chunk is calculated
384 * from cur_offset value and stored in chunk_offset variable.
386 if (cur_offset % chunk_size != 0) {
387 partial_chunk = true;
388 chunk_offset = cur_offset & ~(chunk_size - 1);
390 partial_chunk = false;
391 chunk_offset = cur_offset - chunk_size;
394 /* NB: when rec->lrh_len is accessed it is already swabbed
395 * since it is used at the "end" of the loop and the rec
396 * swabbing is done at the beginning of the loop. */
397 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
398 (char *)rec < buf + chunk_size;
399 rec = llog_rec_hdr_next(rec)) {
401 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
404 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
405 lustre_swab_llog_rec(rec);
407 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
408 rec->lrh_type, rec->lrh_index);
410 /* for partial chunk the end of it is zeroed, check
411 * for index 0 to distinguish it. */
412 if (partial_chunk && rec->lrh_index == 0) {
413 /* concurrent llog_add() might add new records
414 * while llog_processing, check this is not
415 * the case and re-read the current chunk
417 if (index > loghandle->lgh_last_idx)
419 CDEBUG(D_OTHER, "Re-read last llog buffer for "
420 "new records, index %u, last %u\n",
421 index, loghandle->lgh_last_idx);
422 /* save offset inside buffer for the re-read */
423 buf_offset = (char *)rec - (char *)buf;
424 cur_offset = chunk_offset;
428 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
429 CWARN("invalid length %d in llog record for "
430 "index %d/%d\n", rec->lrh_len,
431 rec->lrh_index, index);
432 GOTO(out, rc = -EINVAL);
435 if (rec->lrh_index < index) {
436 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
441 if (rec->lrh_index != index) {
442 CERROR("%s: Invalid record: index %u but "
444 loghandle->lgh_ctxt->loc_obd->obd_name,
445 rec->lrh_index, index);
446 GOTO(out, rc = -ERANGE);
450 "lrh_index: %d lrh_len: %d (%d remains)\n",
451 rec->lrh_index, rec->lrh_len,
452 (int)(buf + chunk_size - (char *)rec));
454 loghandle->lgh_cur_idx = rec->lrh_index;
455 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
458 /* if set, process the callback on this record */
459 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
460 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
462 last_called_index = index;
463 if (rc == LLOG_PROC_BREAK) {
465 } else if (rc == LLOG_DEL_RECORD) {
466 rc = llog_cancel_rec(lpi->lpi_env,
473 /* exit if the last index is reached */
474 if (index >= last_index)
482 cd->lpcd_last_idx = last_called_index;
484 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
485 /* something bad happened to the processing of a local
486 * llog file, probably I/O error or the log got corrupted..
487 * to be able to finally release the log we discard any
488 * remaining bits in the header */
489 CERROR("Local llog found corrupted\n");
490 while (index <= last_index) {
491 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh)) != 0)
492 llog_cancel_rec(lpi->lpi_env, loghandle, index);
498 OBD_FREE_LARGE(buf, chunk_size);
503 static int llog_process_thread_daemonize(void *arg)
505 struct llog_process_info *lpi = arg;
511 /* client env has no keys, tags is just 0 */
512 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
517 rc = llog_process_thread(arg);
521 complete(&lpi->lpi_completion);
525 int llog_process_or_fork(const struct lu_env *env,
526 struct llog_handle *loghandle,
527 llog_cb_t cb, void *data, void *catdata, bool fork)
529 struct llog_process_info *lpi;
536 CERROR("cannot alloc pointer\n");
539 lpi->lpi_loghandle = loghandle;
541 lpi->lpi_cbdata = data;
542 lpi->lpi_catdata = catdata;
545 struct task_struct *task;
547 /* The new thread can't use parent env,
548 * init the new one in llog_process_thread_daemonize. */
550 init_completion(&lpi->lpi_completion);
551 task = kthread_run(llog_process_thread_daemonize, lpi,
552 "llog_process_thread");
555 CERROR("%s: cannot start thread: rc = %d\n",
556 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
559 wait_for_completion(&lpi->lpi_completion);
562 llog_process_thread(lpi);
570 EXPORT_SYMBOL(llog_process_or_fork);
572 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
573 llog_cb_t cb, void *data, void *catdata)
576 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
577 return rc == LLOG_DEL_PLAIN ? 0 : rc;
579 EXPORT_SYMBOL(llog_process);
581 int llog_reverse_process(const struct lu_env *env,
582 struct llog_handle *loghandle, llog_cb_t cb,
583 void *data, void *catdata)
585 struct llog_log_hdr *llh = loghandle->lgh_hdr;
586 struct llog_process_cat_data *cd = catdata;
588 int rc = 0, first_index = 1, index, idx;
589 __u32 chunk_size = llh->llh_hdr.lrh_len;
592 OBD_ALLOC_LARGE(buf, chunk_size);
597 first_index = cd->lpcd_first_idx + 1;
598 if (cd != NULL && cd->lpcd_last_idx)
599 index = cd->lpcd_last_idx;
601 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
604 struct llog_rec_hdr *rec;
605 struct llog_rec_tail *tail;
607 /* skip records not set in bitmap */
608 while (index >= first_index &&
609 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
612 LASSERT(index >= first_index - 1);
613 if (index == first_index - 1)
616 /* get the buf with our target record; avoid old garbage */
617 memset(buf, 0, chunk_size);
618 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
623 idx = rec->lrh_index;
624 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
625 while (idx < index) {
626 rec = (void *)rec + rec->lrh_len;
627 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
628 lustre_swab_llog_rec(rec);
631 LASSERT(idx == index);
632 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
634 /* process records in buffer, starting where we found one */
635 while ((void *)tail > buf) {
636 if (tail->lrt_index == 0)
637 GOTO(out, rc = 0); /* no more records */
639 /* if set, process the callback on this record */
640 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
641 rec = (void *)tail - tail->lrt_len +
644 rc = cb(env, loghandle, rec, data);
645 if (rc == LLOG_PROC_BREAK) {
647 } else if (rc == LLOG_DEL_RECORD) {
648 rc = llog_cancel_rec(env, loghandle,
655 /* previous record, still in buffer? */
657 if (index < first_index)
659 tail = (void *)tail - tail->lrt_len;
665 OBD_FREE_LARGE(buf, chunk_size);
668 EXPORT_SYMBOL(llog_reverse_process);
674 * llog_open - open llog, may not exist
675 * llog_exist - check if llog exists
676 * llog_close - close opened llog, pair for open, frees llog_handle
677 * llog_declare_create - declare llog creation
678 * llog_create - create new llog on disk, need transaction handle
679 * llog_declare_write_rec - declaration of llog write
680 * llog_write_rec - write llog record on disk, need transaction handle
681 * llog_declare_add - declare llog catalog record addition
682 * llog_add - add llog record in catalog, need transaction handle
684 int llog_exist(struct llog_handle *loghandle)
686 struct llog_operations *lop;
691 rc = llog_handle2ops(loghandle, &lop);
694 if (lop->lop_exist == NULL)
697 rc = lop->lop_exist(loghandle);
700 EXPORT_SYMBOL(llog_exist);
702 int llog_declare_create(const struct lu_env *env,
703 struct llog_handle *loghandle, struct thandle *th)
705 struct llog_operations *lop;
710 rc = llog_handle2ops(loghandle, &lop);
713 if (lop->lop_declare_create == NULL)
716 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
718 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
719 rc = lop->lop_declare_create(env, loghandle, th);
721 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
725 int llog_create(const struct lu_env *env, struct llog_handle *handle,
728 struct llog_operations *lop;
733 rc = llog_handle2ops(handle, &lop);
736 if (lop->lop_create == NULL)
739 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
741 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
742 rc = lop->lop_create(env, handle, th);
744 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
748 int llog_declare_write_rec(const struct lu_env *env,
749 struct llog_handle *handle,
750 struct llog_rec_hdr *rec, int idx,
753 struct llog_operations *lop;
758 rc = llog_handle2ops(handle, &lop);
762 if (lop->lop_declare_write_rec == NULL)
765 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
767 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
768 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
770 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
774 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
775 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
776 int idx, struct thandle *th)
778 struct llog_operations *lop;
779 int raised, rc, buflen;
783 rc = llog_handle2ops(handle, &lop);
788 if (lop->lop_write_rec == NULL)
791 buflen = rec->lrh_len;
792 LASSERT(cfs_size_round(buflen) == buflen);
794 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
796 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
797 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
799 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
803 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
804 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
811 if (lgh->lgh_logops->lop_add == NULL)
814 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
816 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
817 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
819 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
822 EXPORT_SYMBOL(llog_add);
824 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
825 struct llog_rec_hdr *rec, struct thandle *th)
831 if (lgh->lgh_logops->lop_declare_add == NULL)
834 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
836 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
837 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
839 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
842 EXPORT_SYMBOL(llog_declare_add);
845 * Helper function to open llog or create it if doesn't exist.
846 * It hides all transaction handling from caller.
848 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
849 struct llog_handle **res, struct llog_logid *logid,
858 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
862 if (llog_exist(*res))
865 LASSERT((*res)->lgh_obj != NULL);
867 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
869 th = dt_trans_create(env, d);
871 GOTO(out, rc = PTR_ERR(th));
873 /* Create update llog object synchronously, which
874 * happens during inialization process see
875 * lod_sub_prep_llog(), to make sure the update
876 * llog object is created before corss-MDT writing
877 * updates into the llog object */
878 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
881 th->th_wait_submit = 1;
882 rc = llog_declare_create(env, *res, th);
884 rc = dt_trans_start_local(env, d, th);
886 rc = llog_create(env, *res, th);
888 dt_trans_stop(env, d, th);
891 llog_close(env, *res);
894 EXPORT_SYMBOL(llog_open_create);
897 * Helper function to delete existent llog.
899 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
900 struct llog_logid *logid, char *name)
902 struct llog_handle *handle;
907 /* nothing to erase */
908 if (name == NULL && logid == NULL)
911 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
915 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
917 rc = llog_destroy(env, handle);
919 rc2 = llog_close(env, handle);
924 EXPORT_SYMBOL(llog_erase);
927 * Helper function for write record in llog.
928 * It hides all transaction handling from caller.
929 * Valid only with local llog.
931 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
932 struct llog_rec_hdr *rec, int idx)
934 struct dt_device *dt;
941 LASSERT(loghandle->lgh_ctxt);
942 LASSERT(loghandle->lgh_obj != NULL);
944 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
946 th = dt_trans_create(env, dt);
950 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
954 th->th_wait_submit = 1;
955 rc = dt_trans_start_local(env, dt, th);
959 down_write(&loghandle->lgh_lock);
960 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
961 up_write(&loghandle->lgh_lock);
963 dt_trans_stop(env, dt, th);
966 EXPORT_SYMBOL(llog_write);
968 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
969 struct llog_handle **lgh, struct llog_logid *logid,
970 char *name, enum llog_open_param open_param)
978 LASSERT(ctxt->loc_logops);
980 if (ctxt->loc_logops->lop_open == NULL) {
985 *lgh = llog_alloc_handle();
988 (*lgh)->lgh_ctxt = ctxt;
989 (*lgh)->lgh_logops = ctxt->loc_logops;
991 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
993 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
994 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
996 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
998 llog_free_handle(*lgh);
1003 EXPORT_SYMBOL(llog_open);
1005 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1007 struct llog_operations *lop;
1012 rc = llog_handle2ops(loghandle, &lop);
1015 if (lop->lop_close == NULL)
1016 GOTO(out, rc = -EOPNOTSUPP);
1017 rc = lop->lop_close(env, loghandle);
1019 llog_handle_put(loghandle);
1022 EXPORT_SYMBOL(llog_close);
1025 * Helper function to get the llog size in records. It is used by MGS
1026 * mostly to check that config llog exists and contains data.
1028 * \param[in] env execution environment
1029 * \param[in] ctxt llog context
1030 * \param[in] name llog name
1032 * \retval true if there are records in llog besides a header
1033 * \retval false on error or llog without records
1035 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1038 struct llog_handle *llh;
1041 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1043 if (likely(rc == -ENOENT))
1048 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1050 GOTO(out_close, rc);
1051 rc = llog_get_size(llh);
1054 llog_close(env, llh);
1056 /* The header is record 1, the llog is still considered as empty
1057 * if there is only header */
1060 EXPORT_SYMBOL(llog_is_empty);
1062 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1063 struct llog_rec_hdr *rec, void *data)
1065 struct llog_handle *copy_llh = data;
1067 /* Append all records */
1068 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1071 /* backup plain llog */
1072 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1073 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1074 char *name, char *backup)
1076 struct llog_handle *llh, *bllh;
1081 /* open original log */
1082 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1084 /* the -ENOENT case is also reported to the caller
1085 * but silently so it should handle that if needed.
1088 CERROR("%s: failed to open log %s: rc = %d\n",
1089 obd->obd_name, name, rc);
1093 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1095 GOTO(out_close, rc);
1097 /* Make sure there's no old backup log */
1098 rc = llog_erase(env, bctxt, NULL, backup);
1099 if (rc < 0 && rc != -ENOENT)
1100 GOTO(out_close, rc);
1102 /* open backup log */
1103 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1105 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1106 obd->obd_name, backup, rc);
1107 GOTO(out_close, rc);
1110 /* check that backup llog is not the same object as original one */
1111 if (llh->lgh_obj == bllh->lgh_obj) {
1112 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1113 obd->obd_name, name, backup, llh->lgh_obj,
1115 GOTO(out_backup, rc = -EEXIST);
1118 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1120 GOTO(out_backup, rc);
1122 /* Copy log record by record */
1123 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1126 CERROR("%s: failed to backup log %s: rc = %d\n",
1127 obd->obd_name, name, rc);
1129 llog_close(env, bllh);
1131 llog_close(env, llh);
1134 EXPORT_SYMBOL(llog_backup);