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, 2015, 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_declare_destroy(const struct lu_env *env,
108 struct llog_handle *handle,
111 struct llog_operations *lop;
116 rc = llog_handle2ops(handle, &lop);
119 if (lop->lop_declare_destroy == NULL)
122 rc = lop->lop_declare_destroy(env, handle, th);
127 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
130 struct llog_operations *lop;
134 rc = llog_handle2ops(handle, &lop);
137 if (lop->lop_destroy == NULL)
140 LASSERT(handle->lgh_obj != NULL);
141 if (!dt_object_exists(handle->lgh_obj))
144 rc = lop->lop_destroy(env, handle, th);
149 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
151 struct llog_operations *lop;
152 struct dt_device *dt;
158 rc = llog_handle2ops(handle, &lop);
161 if (lop->lop_destroy == NULL)
164 if (handle->lgh_obj == NULL) {
165 /* if lgh_obj == NULL, then it is from client side destroy */
166 rc = lop->lop_destroy(env, handle, NULL);
170 if (!dt_object_exists(handle->lgh_obj))
173 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
175 th = dt_trans_create(env, dt);
179 rc = llog_declare_destroy(env, handle, th);
183 rc = dt_trans_start_local(env, dt, th);
187 rc = lop->lop_destroy(env, handle, th);
190 dt_trans_stop(env, dt, th);
194 EXPORT_SYMBOL(llog_destroy);
196 /* returns negative on error; 0 if success; 1 if success & log destroyed */
197 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
200 struct llog_thread_info *lgi = llog_info(env);
201 struct dt_device *dt;
202 struct llog_log_hdr *llh = loghandle->lgh_hdr;
206 bool subtract_count = false;
210 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n", index,
211 POSTID(&loghandle->lgh_id.lgl_oi));
214 CERROR("Can't cancel index 0 which is header\n");
218 LASSERT(loghandle != NULL);
219 LASSERT(loghandle->lgh_ctxt != NULL);
220 LASSERT(loghandle->lgh_obj != NULL);
222 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
224 th = dt_trans_create(env, dt);
228 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
232 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY))
233 rc = llog_declare_destroy(env, loghandle, th);
235 th->th_wait_submit = 1;
236 rc = dt_trans_start_local(env, dt, th);
240 down_write(&loghandle->lgh_lock);
242 mutex_lock(&loghandle->lgh_hdr_mutex);
243 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
244 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
245 GOTO(out_unlock, rc);
248 loghandle->lgh_hdr->llh_count--;
249 subtract_count = true;
250 /* Pass this index to llog_osd_write_rec(), which will use the index
251 * to only update the necesary bitmap. */
252 lgi->lgi_cookie.lgc_index = index;
254 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
255 LLOG_HEADER_IDX, th);
257 GOTO(out_unlock, rc);
259 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
260 (llh->llh_count == 1) &&
261 (loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1)) {
262 rc = llog_trans_destroy(env, loghandle, th);
264 /* Sigh, can not destroy the final plain llog, but
265 * the bitmap has been clearly, so the record can not
266 * be accessed anymore, let's return 0 for now, and
267 * the orphan will be handled by LFSCK. */
268 CERROR("%s: can't destroy empty llog #"DOSTID
270 loghandle->lgh_ctxt->loc_obd->obd_name,
271 POSTID(&loghandle->lgh_id.lgl_oi),
272 loghandle->lgh_id.lgl_ogen, rc);
273 GOTO(out_unlock, rc);
279 mutex_unlock(&loghandle->lgh_hdr_mutex);
280 up_write(&loghandle->lgh_lock);
282 rc1 = dt_trans_stop(env, dt, th);
285 if (rc < 0 && subtract_count) {
286 mutex_lock(&loghandle->lgh_hdr_mutex);
287 loghandle->lgh_hdr->llh_count++;
288 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
289 mutex_unlock(&loghandle->lgh_hdr_mutex);
294 static int llog_read_header(const struct lu_env *env,
295 struct llog_handle *handle,
296 struct obd_uuid *uuid)
298 struct llog_operations *lop;
301 rc = llog_handle2ops(handle, &lop);
305 if (lop->lop_read_header == NULL)
308 rc = lop->lop_read_header(env, handle);
309 if (rc == LLOG_EEMPTY) {
310 struct llog_log_hdr *llh = handle->lgh_hdr;
312 /* lrh_len should be initialized in llog_init_handle */
313 handle->lgh_last_idx = 0; /* header is record with index 0 */
314 llh->llh_count = 1; /* for the header record */
315 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
316 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
317 LLOG_MIN_CHUNK_SIZE);
318 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
319 llh->llh_hdr.lrh_index = 0;
320 llh->llh_timestamp = cfs_time_current_sec();
322 memcpy(&llh->llh_tgtuuid, uuid,
323 sizeof(llh->llh_tgtuuid));
324 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
325 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
326 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
327 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
333 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
334 int flags, struct obd_uuid *uuid)
336 struct llog_log_hdr *llh;
337 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
339 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
342 LASSERT(handle->lgh_hdr == NULL);
344 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
345 OBD_ALLOC_LARGE(llh, chunk_size);
349 handle->lgh_hdr = llh;
350 handle->lgh_hdr_size = chunk_size;
351 /* first assign flags to use llog_client_ops */
352 llh->llh_flags = flags;
353 rc = llog_read_header(env, handle, uuid);
355 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
356 flags & LLOG_F_IS_CAT) ||
357 (llh->llh_flags & LLOG_F_IS_CAT &&
358 flags & LLOG_F_IS_PLAIN))) {
359 CERROR("%s: llog type is %s but initializing %s\n",
360 handle->lgh_ctxt->loc_obd->obd_name,
361 llh->llh_flags & LLOG_F_IS_CAT ?
363 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
364 GOTO(out, rc = -EINVAL);
365 } else if (llh->llh_flags &
366 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
368 * it is possible to open llog without specifying llog
369 * type so it is taken from llh_flags
371 flags = llh->llh_flags;
373 /* for some reason the llh_flags has no type set */
374 CERROR("llog type is not specified!\n");
375 GOTO(out, rc = -EINVAL);
378 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
379 CERROR("%s: llog uuid mismatch: %s/%s\n",
380 handle->lgh_ctxt->loc_obd->obd_name,
382 (char *)llh->llh_tgtuuid.uuid);
383 GOTO(out, rc = -EEXIST);
386 if (flags & LLOG_F_IS_CAT) {
387 LASSERT(list_empty(&handle->u.chd.chd_head));
388 INIT_LIST_HEAD(&handle->u.chd.chd_head);
389 llh->llh_size = sizeof(struct llog_logid_rec);
390 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
391 } else if (!(flags & LLOG_F_IS_PLAIN)) {
392 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
393 handle->lgh_ctxt->loc_obd->obd_name,
394 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
397 llh->llh_flags |= fmt;
400 OBD_FREE_LARGE(llh, chunk_size);
401 handle->lgh_hdr = NULL;
405 EXPORT_SYMBOL(llog_init_handle);
407 static int llog_process_thread(void *arg)
409 struct llog_process_info *lpi = arg;
410 struct llog_handle *loghandle = lpi->lpi_loghandle;
411 struct llog_log_hdr *llh = loghandle->lgh_hdr;
412 struct llog_process_cat_data *cd = lpi->lpi_catdata;
416 int rc = 0, index = 1, last_index;
418 int last_called_index = 0;
425 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
426 /* expect chunk_size to be power of two */
427 LASSERT(is_power_of_2(chunk_size));
429 OBD_ALLOC_LARGE(buf, chunk_size);
431 lpi->lpi_rc = -ENOMEM;
436 last_called_index = cd->lpcd_first_idx;
437 index = cd->lpcd_first_idx + 1;
439 if (cd != NULL && cd->lpcd_last_idx)
440 last_index = cd->lpcd_last_idx;
442 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
445 struct llog_rec_hdr *rec;
447 unsigned int buf_offset = 0;
450 /* skip records not set in bitmap */
451 while (index <= last_index &&
452 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
455 /* There are no indices prior the last_index */
456 if (index > last_index)
459 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
463 /* get the buf with our target record; avoid old garbage */
464 memset(buf, 0, chunk_size);
465 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
466 index, &cur_offset, buf, chunk_size);
470 /* NB: after llog_next_block() call the cur_offset is the
471 * offset of the next block after read one.
472 * The absolute offset of the current chunk is calculated
473 * from cur_offset value and stored in chunk_offset variable.
475 if (cur_offset % chunk_size != 0) {
476 partial_chunk = true;
477 chunk_offset = cur_offset & ~(chunk_size - 1);
479 partial_chunk = false;
480 chunk_offset = cur_offset - chunk_size;
483 /* NB: when rec->lrh_len is accessed it is already swabbed
484 * since it is used at the "end" of the loop and the rec
485 * swabbing is done at the beginning of the loop. */
486 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
487 (char *)rec < buf + chunk_size;
488 rec = llog_rec_hdr_next(rec)) {
490 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
493 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
494 lustre_swab_llog_rec(rec);
496 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
497 rec->lrh_type, rec->lrh_index);
499 /* for partial chunk the end of it is zeroed, check
500 * for index 0 to distinguish it. */
501 if (partial_chunk && rec->lrh_index == 0) {
502 /* concurrent llog_add() might add new records
503 * while llog_processing, check this is not
504 * the case and re-read the current chunk
506 if (index > loghandle->lgh_last_idx)
508 CDEBUG(D_OTHER, "Re-read last llog buffer for "
509 "new records, index %u, last %u\n",
510 index, loghandle->lgh_last_idx);
511 /* save offset inside buffer for the re-read */
512 buf_offset = (char *)rec - (char *)buf;
513 cur_offset = chunk_offset;
517 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
518 CWARN("invalid length %d in llog record for "
519 "index %d/%d\n", rec->lrh_len,
520 rec->lrh_index, index);
521 GOTO(out, rc = -EINVAL);
524 if (rec->lrh_index < index) {
525 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
530 if (rec->lrh_index != index) {
531 CERROR("%s: Invalid record: index %u but "
533 loghandle->lgh_ctxt->loc_obd->obd_name,
534 rec->lrh_index, index);
535 GOTO(out, rc = -ERANGE);
539 "lrh_index: %d lrh_len: %d (%d remains)\n",
540 rec->lrh_index, rec->lrh_len,
541 (int)(buf + chunk_size - (char *)rec));
543 loghandle->lgh_cur_idx = rec->lrh_index;
544 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
547 /* if set, process the callback on this record */
548 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
549 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
551 last_called_index = index;
552 if (rc == LLOG_PROC_BREAK) {
554 } else if (rc == LLOG_DEL_RECORD) {
555 rc = llog_cancel_rec(lpi->lpi_env,
562 /* exit if the last index is reached */
563 if (index >= last_index)
571 cd->lpcd_last_idx = last_called_index;
573 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
574 /* something bad happened to the processing of a local
575 * llog file, probably I/O error or the log got corrupted..
576 * to be able to finally release the log we discard any
577 * remaining bits in the header */
578 CERROR("Local llog found corrupted\n");
579 while (index <= last_index) {
580 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh)) != 0)
581 llog_cancel_rec(lpi->lpi_env, loghandle, index);
587 OBD_FREE_LARGE(buf, chunk_size);
592 static int llog_process_thread_daemonize(void *arg)
594 struct llog_process_info *lpi = arg;
600 /* client env has no keys, tags is just 0 */
601 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
606 rc = llog_process_thread(arg);
610 complete(&lpi->lpi_completion);
614 int llog_process_or_fork(const struct lu_env *env,
615 struct llog_handle *loghandle,
616 llog_cb_t cb, void *data, void *catdata, bool fork)
618 struct llog_process_info *lpi;
625 CERROR("cannot alloc pointer\n");
628 lpi->lpi_loghandle = loghandle;
630 lpi->lpi_cbdata = data;
631 lpi->lpi_catdata = catdata;
634 struct task_struct *task;
636 /* The new thread can't use parent env,
637 * init the new one in llog_process_thread_daemonize. */
639 init_completion(&lpi->lpi_completion);
640 task = kthread_run(llog_process_thread_daemonize, lpi,
641 "llog_process_thread");
644 CERROR("%s: cannot start thread: rc = %d\n",
645 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
648 wait_for_completion(&lpi->lpi_completion);
651 llog_process_thread(lpi);
659 EXPORT_SYMBOL(llog_process_or_fork);
661 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
662 llog_cb_t cb, void *data, void *catdata)
665 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
666 return rc == LLOG_DEL_PLAIN ? 0 : rc;
668 EXPORT_SYMBOL(llog_process);
670 int llog_reverse_process(const struct lu_env *env,
671 struct llog_handle *loghandle, llog_cb_t cb,
672 void *data, void *catdata)
674 struct llog_log_hdr *llh = loghandle->lgh_hdr;
675 struct llog_process_cat_data *cd = catdata;
677 int rc = 0, first_index = 1, index, idx;
678 __u32 chunk_size = llh->llh_hdr.lrh_len;
681 OBD_ALLOC_LARGE(buf, chunk_size);
686 first_index = cd->lpcd_first_idx + 1;
687 if (cd != NULL && cd->lpcd_last_idx)
688 index = cd->lpcd_last_idx;
690 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
693 struct llog_rec_hdr *rec;
694 struct llog_rec_tail *tail;
696 /* skip records not set in bitmap */
697 while (index >= first_index &&
698 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
701 LASSERT(index >= first_index - 1);
702 if (index == first_index - 1)
705 /* get the buf with our target record; avoid old garbage */
706 memset(buf, 0, chunk_size);
707 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
712 idx = rec->lrh_index;
713 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
714 while (idx < index) {
715 rec = (void *)rec + rec->lrh_len;
716 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
717 lustre_swab_llog_rec(rec);
720 LASSERT(idx == index);
721 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
723 /* process records in buffer, starting where we found one */
724 while ((void *)tail > buf) {
725 if (tail->lrt_index == 0)
726 GOTO(out, rc = 0); /* no more records */
728 /* if set, process the callback on this record */
729 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
730 rec = (void *)tail - tail->lrt_len +
733 rc = cb(env, loghandle, rec, data);
734 if (rc == LLOG_PROC_BREAK) {
736 } else if (rc == LLOG_DEL_RECORD) {
737 rc = llog_cancel_rec(env, loghandle,
744 /* previous record, still in buffer? */
746 if (index < first_index)
748 tail = (void *)tail - tail->lrt_len;
754 OBD_FREE_LARGE(buf, chunk_size);
757 EXPORT_SYMBOL(llog_reverse_process);
763 * llog_open - open llog, may not exist
764 * llog_exist - check if llog exists
765 * llog_close - close opened llog, pair for open, frees llog_handle
766 * llog_declare_create - declare llog creation
767 * llog_create - create new llog on disk, need transaction handle
768 * llog_declare_write_rec - declaration of llog write
769 * llog_write_rec - write llog record on disk, need transaction handle
770 * llog_declare_add - declare llog catalog record addition
771 * llog_add - add llog record in catalog, need transaction handle
773 int llog_exist(struct llog_handle *loghandle)
775 struct llog_operations *lop;
780 rc = llog_handle2ops(loghandle, &lop);
783 if (lop->lop_exist == NULL)
786 rc = lop->lop_exist(loghandle);
789 EXPORT_SYMBOL(llog_exist);
791 int llog_declare_create(const struct lu_env *env,
792 struct llog_handle *loghandle, struct thandle *th)
794 struct llog_operations *lop;
799 rc = llog_handle2ops(loghandle, &lop);
802 if (lop->lop_declare_create == NULL)
805 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
807 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
808 rc = lop->lop_declare_create(env, loghandle, th);
810 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
814 int llog_create(const struct lu_env *env, struct llog_handle *handle,
817 struct llog_operations *lop;
822 rc = llog_handle2ops(handle, &lop);
825 if (lop->lop_create == NULL)
828 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
830 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
831 rc = lop->lop_create(env, handle, th);
833 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
837 int llog_declare_write_rec(const struct lu_env *env,
838 struct llog_handle *handle,
839 struct llog_rec_hdr *rec, int idx,
842 struct llog_operations *lop;
847 rc = llog_handle2ops(handle, &lop);
851 if (lop->lop_declare_write_rec == NULL)
854 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
856 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
857 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
859 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
863 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
864 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
865 int idx, struct thandle *th)
867 struct llog_operations *lop;
868 int raised, rc, buflen;
872 rc = llog_handle2ops(handle, &lop);
877 if (lop->lop_write_rec == NULL)
880 buflen = rec->lrh_len;
881 LASSERT(cfs_size_round(buflen) == buflen);
883 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
885 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
886 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
888 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
892 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
893 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
900 if (lgh->lgh_logops->lop_add == NULL)
903 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
905 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
906 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
908 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
911 EXPORT_SYMBOL(llog_add);
913 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
914 struct llog_rec_hdr *rec, struct thandle *th)
920 if (lgh->lgh_logops->lop_declare_add == NULL)
923 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
925 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
926 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
928 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
931 EXPORT_SYMBOL(llog_declare_add);
934 * Helper function to open llog or create it if doesn't exist.
935 * It hides all transaction handling from caller.
937 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
938 struct llog_handle **res, struct llog_logid *logid,
947 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
951 if (llog_exist(*res))
954 LASSERT((*res)->lgh_obj != NULL);
956 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
958 th = dt_trans_create(env, d);
960 GOTO(out, rc = PTR_ERR(th));
962 /* Create update llog object synchronously, which
963 * happens during inialization process see
964 * lod_sub_prep_llog(), to make sure the update
965 * llog object is created before corss-MDT writing
966 * updates into the llog object */
967 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
970 th->th_wait_submit = 1;
971 rc = llog_declare_create(env, *res, th);
973 rc = dt_trans_start_local(env, d, th);
975 rc = llog_create(env, *res, th);
977 dt_trans_stop(env, d, th);
980 llog_close(env, *res);
983 EXPORT_SYMBOL(llog_open_create);
986 * Helper function to delete existent llog.
988 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
989 struct llog_logid *logid, char *name)
991 struct llog_handle *handle;
996 /* nothing to erase */
997 if (name == NULL && logid == NULL)
1000 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1004 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1006 rc = llog_destroy(env, handle);
1008 rc2 = llog_close(env, handle);
1013 EXPORT_SYMBOL(llog_erase);
1016 * Helper function for write record in llog.
1017 * It hides all transaction handling from caller.
1018 * Valid only with local llog.
1020 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1021 struct llog_rec_hdr *rec, int idx)
1023 struct dt_device *dt;
1030 LASSERT(loghandle->lgh_ctxt);
1031 LASSERT(loghandle->lgh_obj != NULL);
1033 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1035 th = dt_trans_create(env, dt);
1037 RETURN(PTR_ERR(th));
1039 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1041 GOTO(out_trans, rc);
1043 th->th_wait_submit = 1;
1044 rc = dt_trans_start_local(env, dt, th);
1046 GOTO(out_trans, rc);
1048 down_write(&loghandle->lgh_lock);
1049 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1050 up_write(&loghandle->lgh_lock);
1052 dt_trans_stop(env, dt, th);
1055 EXPORT_SYMBOL(llog_write);
1057 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1058 struct llog_handle **lgh, struct llog_logid *logid,
1059 char *name, enum llog_open_param open_param)
1067 LASSERT(ctxt->loc_logops);
1069 if (ctxt->loc_logops->lop_open == NULL) {
1071 RETURN(-EOPNOTSUPP);
1074 *lgh = llog_alloc_handle();
1077 (*lgh)->lgh_ctxt = ctxt;
1078 (*lgh)->lgh_logops = ctxt->loc_logops;
1080 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1082 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1083 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1085 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1087 llog_free_handle(*lgh);
1092 EXPORT_SYMBOL(llog_open);
1094 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1096 struct llog_operations *lop;
1101 rc = llog_handle2ops(loghandle, &lop);
1104 if (lop->lop_close == NULL)
1105 GOTO(out, rc = -EOPNOTSUPP);
1106 rc = lop->lop_close(env, loghandle);
1108 llog_handle_put(loghandle);
1111 EXPORT_SYMBOL(llog_close);
1114 * Helper function to get the llog size in records. It is used by MGS
1115 * mostly to check that config llog exists and contains data.
1117 * \param[in] env execution environment
1118 * \param[in] ctxt llog context
1119 * \param[in] name llog name
1121 * \retval true if there are records in llog besides a header
1122 * \retval false on error or llog without records
1124 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1127 struct llog_handle *llh;
1130 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1132 if (likely(rc == -ENOENT))
1137 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1139 GOTO(out_close, rc);
1140 rc = llog_get_size(llh);
1143 llog_close(env, llh);
1145 /* The header is record 1, the llog is still considered as empty
1146 * if there is only header */
1149 EXPORT_SYMBOL(llog_is_empty);
1151 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1152 struct llog_rec_hdr *rec, void *data)
1154 struct llog_handle *copy_llh = data;
1156 /* Append all records */
1157 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1160 /* backup plain llog */
1161 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1162 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1163 char *name, char *backup)
1165 struct llog_handle *llh, *bllh;
1170 /* open original log */
1171 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1173 /* the -ENOENT case is also reported to the caller
1174 * but silently so it should handle that if needed.
1177 CERROR("%s: failed to open log %s: rc = %d\n",
1178 obd->obd_name, name, rc);
1182 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1184 GOTO(out_close, rc);
1186 /* Make sure there's no old backup log */
1187 rc = llog_erase(env, bctxt, NULL, backup);
1188 if (rc < 0 && rc != -ENOENT)
1189 GOTO(out_close, rc);
1191 /* open backup log */
1192 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1194 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1195 obd->obd_name, backup, rc);
1196 GOTO(out_close, rc);
1199 /* check that backup llog is not the same object as original one */
1200 if (llh->lgh_obj == bllh->lgh_obj) {
1201 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1202 obd->obd_name, name, backup, llh->lgh_obj,
1204 GOTO(out_backup, rc = -EEXIST);
1207 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1209 GOTO(out_backup, rc);
1211 /* Copy log record by record */
1212 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1215 CERROR("%s: failed to backup log %s: rc = %d\n",
1216 obd->obd_name, name, rc);
1218 llog_close(env, bllh);
1220 llog_close(env, llh);
1223 EXPORT_SYMBOL(llog_backup);