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_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;
208 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n", index,
209 POSTID(&loghandle->lgh_id.lgl_oi));
212 CERROR("Can't cancel index 0 which is header\n");
216 LASSERT(loghandle != NULL);
217 LASSERT(loghandle->lgh_ctxt != NULL);
218 LASSERT(loghandle->lgh_obj != NULL);
220 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
222 th = dt_trans_create(env, dt);
226 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
230 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY))
231 rc = llog_declare_destroy(env, loghandle, th);
233 th->th_wait_submit = 1;
234 rc = dt_trans_start_local(env, dt, th);
238 down_write(&loghandle->lgh_lock);
240 mutex_lock(&loghandle->lgh_hdr_mutex);
241 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
242 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
243 GOTO(out_unlock, rc);
246 loghandle->lgh_hdr->llh_count--;
247 /* Pass this index to llog_osd_write_rec(), which will use the index
248 * to only update the necesary bitmap. */
249 lgi->lgi_cookie.lgc_index = index;
251 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
252 LLOG_HEADER_IDX, th);
254 loghandle->lgh_hdr->llh_count++;
255 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
256 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 = 0);
279 mutex_unlock(&loghandle->lgh_hdr_mutex);
280 up_write(&loghandle->lgh_lock);
282 dt_trans_stop(env, dt, th);
286 static int llog_read_header(const struct lu_env *env,
287 struct llog_handle *handle,
288 struct obd_uuid *uuid)
290 struct llog_operations *lop;
293 rc = llog_handle2ops(handle, &lop);
297 if (lop->lop_read_header == NULL)
300 rc = lop->lop_read_header(env, handle);
301 if (rc == LLOG_EEMPTY) {
302 struct llog_log_hdr *llh = handle->lgh_hdr;
304 /* lrh_len should be initialized in llog_init_handle */
305 handle->lgh_last_idx = 0; /* header is record with index 0 */
306 llh->llh_count = 1; /* for the header record */
307 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
308 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
309 LLOG_MIN_CHUNK_SIZE);
310 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
311 llh->llh_hdr.lrh_index = 0;
312 llh->llh_timestamp = cfs_time_current_sec();
314 memcpy(&llh->llh_tgtuuid, uuid,
315 sizeof(llh->llh_tgtuuid));
316 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
317 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
318 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
319 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
325 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
326 int flags, struct obd_uuid *uuid)
328 struct llog_log_hdr *llh;
329 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
331 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
334 LASSERT(handle->lgh_hdr == NULL);
336 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
337 OBD_ALLOC_LARGE(llh, chunk_size);
341 handle->lgh_hdr = llh;
342 handle->lgh_hdr_size = chunk_size;
343 /* first assign flags to use llog_client_ops */
344 llh->llh_flags = flags;
345 rc = llog_read_header(env, handle, uuid);
347 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
348 flags & LLOG_F_IS_CAT) ||
349 (llh->llh_flags & LLOG_F_IS_CAT &&
350 flags & LLOG_F_IS_PLAIN))) {
351 CERROR("%s: llog type is %s but initializing %s\n",
352 handle->lgh_ctxt->loc_obd->obd_name,
353 llh->llh_flags & LLOG_F_IS_CAT ?
355 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
356 GOTO(out, rc = -EINVAL);
357 } else if (llh->llh_flags &
358 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
360 * it is possible to open llog without specifying llog
361 * type so it is taken from llh_flags
363 flags = llh->llh_flags;
365 /* for some reason the llh_flags has no type set */
366 CERROR("llog type is not specified!\n");
367 GOTO(out, rc = -EINVAL);
370 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
371 CERROR("%s: llog uuid mismatch: %s/%s\n",
372 handle->lgh_ctxt->loc_obd->obd_name,
374 (char *)llh->llh_tgtuuid.uuid);
375 GOTO(out, rc = -EEXIST);
378 if (flags & LLOG_F_IS_CAT) {
379 LASSERT(list_empty(&handle->u.chd.chd_head));
380 INIT_LIST_HEAD(&handle->u.chd.chd_head);
381 llh->llh_size = sizeof(struct llog_logid_rec);
382 } else if (!(flags & LLOG_F_IS_PLAIN)) {
383 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
384 handle->lgh_ctxt->loc_obd->obd_name,
385 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
388 llh->llh_flags |= fmt;
391 OBD_FREE_LARGE(llh, chunk_size);
392 handle->lgh_hdr = NULL;
396 EXPORT_SYMBOL(llog_init_handle);
398 static int llog_process_thread(void *arg)
400 struct llog_process_info *lpi = arg;
401 struct llog_handle *loghandle = lpi->lpi_loghandle;
402 struct llog_log_hdr *llh = loghandle->lgh_hdr;
403 struct llog_process_cat_data *cd = lpi->lpi_catdata;
407 int rc = 0, index = 1, last_index;
409 int last_called_index = 0;
416 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
417 /* expect chunk_size to be power of two */
418 LASSERT(is_power_of_2(chunk_size));
420 OBD_ALLOC_LARGE(buf, chunk_size);
422 lpi->lpi_rc = -ENOMEM;
427 last_called_index = cd->lpcd_first_idx;
428 index = cd->lpcd_first_idx + 1;
430 if (cd != NULL && cd->lpcd_last_idx)
431 last_index = cd->lpcd_last_idx;
433 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
436 struct llog_rec_hdr *rec;
438 unsigned int buf_offset = 0;
441 /* skip records not set in bitmap */
442 while (index <= last_index &&
443 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
446 /* There are no indices prior the last_index */
447 if (index > last_index)
450 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
454 /* get the buf with our target record; avoid old garbage */
455 memset(buf, 0, chunk_size);
456 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
457 index, &cur_offset, buf, chunk_size);
461 /* NB: after llog_next_block() call the cur_offset is the
462 * offset of the next block after read one.
463 * The absolute offset of the current chunk is calculated
464 * from cur_offset value and stored in chunk_offset variable.
466 if (cur_offset % chunk_size != 0) {
467 partial_chunk = true;
468 chunk_offset = cur_offset & ~(chunk_size - 1);
470 partial_chunk = false;
471 chunk_offset = cur_offset - chunk_size;
474 /* NB: when rec->lrh_len is accessed it is already swabbed
475 * since it is used at the "end" of the loop and the rec
476 * swabbing is done at the beginning of the loop. */
477 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
478 (char *)rec < buf + chunk_size;
479 rec = llog_rec_hdr_next(rec)) {
481 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
484 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
485 lustre_swab_llog_rec(rec);
487 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
488 rec->lrh_type, rec->lrh_index);
490 /* for partial chunk the end of it is zeroed, check
491 * for index 0 to distinguish it. */
492 if (partial_chunk && rec->lrh_index == 0) {
493 /* concurrent llog_add() might add new records
494 * while llog_processing, check this is not
495 * the case and re-read the current chunk
497 if (index > loghandle->lgh_last_idx)
499 CDEBUG(D_OTHER, "Re-read last llog buffer for "
500 "new records, index %u, last %u\n",
501 index, loghandle->lgh_last_idx);
502 /* save offset inside buffer for the re-read */
503 buf_offset = (char *)rec - (char *)buf;
504 cur_offset = chunk_offset;
508 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
509 CWARN("invalid length %d in llog record for "
510 "index %d/%d\n", rec->lrh_len,
511 rec->lrh_index, index);
512 GOTO(out, rc = -EINVAL);
515 if (rec->lrh_index < index) {
516 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
521 if (rec->lrh_index != index) {
522 CERROR("%s: Invalid record: index %u but "
524 loghandle->lgh_ctxt->loc_obd->obd_name,
525 rec->lrh_index, index);
526 GOTO(out, rc = -ERANGE);
530 "lrh_index: %d lrh_len: %d (%d remains)\n",
531 rec->lrh_index, rec->lrh_len,
532 (int)(buf + chunk_size - (char *)rec));
534 loghandle->lgh_cur_idx = rec->lrh_index;
535 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
538 /* if set, process the callback on this record */
539 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
540 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
542 last_called_index = index;
543 if (rc == LLOG_PROC_BREAK) {
545 } else if (rc == LLOG_DEL_RECORD) {
546 rc = llog_cancel_rec(lpi->lpi_env,
553 /* exit if the last index is reached */
554 if (index >= last_index)
562 cd->lpcd_last_idx = last_called_index;
564 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
565 /* something bad happened to the processing of a local
566 * llog file, probably I/O error or the log got corrupted..
567 * to be able to finally release the log we discard any
568 * remaining bits in the header */
569 CERROR("Local llog found corrupted\n");
570 while (index <= last_index) {
571 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh)) != 0)
572 llog_cancel_rec(lpi->lpi_env, loghandle, index);
578 OBD_FREE_LARGE(buf, chunk_size);
583 static int llog_process_thread_daemonize(void *arg)
585 struct llog_process_info *lpi = arg;
591 /* client env has no keys, tags is just 0 */
592 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
597 rc = llog_process_thread(arg);
601 complete(&lpi->lpi_completion);
605 int llog_process_or_fork(const struct lu_env *env,
606 struct llog_handle *loghandle,
607 llog_cb_t cb, void *data, void *catdata, bool fork)
609 struct llog_process_info *lpi;
616 CERROR("cannot alloc pointer\n");
619 lpi->lpi_loghandle = loghandle;
621 lpi->lpi_cbdata = data;
622 lpi->lpi_catdata = catdata;
625 struct task_struct *task;
627 /* The new thread can't use parent env,
628 * init the new one in llog_process_thread_daemonize. */
630 init_completion(&lpi->lpi_completion);
631 task = kthread_run(llog_process_thread_daemonize, lpi,
632 "llog_process_thread");
635 CERROR("%s: cannot start thread: rc = %d\n",
636 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
639 wait_for_completion(&lpi->lpi_completion);
642 llog_process_thread(lpi);
650 EXPORT_SYMBOL(llog_process_or_fork);
652 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
653 llog_cb_t cb, void *data, void *catdata)
656 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
657 return rc == LLOG_DEL_PLAIN ? 0 : rc;
659 EXPORT_SYMBOL(llog_process);
661 int llog_reverse_process(const struct lu_env *env,
662 struct llog_handle *loghandle, llog_cb_t cb,
663 void *data, void *catdata)
665 struct llog_log_hdr *llh = loghandle->lgh_hdr;
666 struct llog_process_cat_data *cd = catdata;
668 int rc = 0, first_index = 1, index, idx;
669 __u32 chunk_size = llh->llh_hdr.lrh_len;
672 OBD_ALLOC_LARGE(buf, chunk_size);
677 first_index = cd->lpcd_first_idx + 1;
678 if (cd != NULL && cd->lpcd_last_idx)
679 index = cd->lpcd_last_idx;
681 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
684 struct llog_rec_hdr *rec;
685 struct llog_rec_tail *tail;
687 /* skip records not set in bitmap */
688 while (index >= first_index &&
689 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
692 LASSERT(index >= first_index - 1);
693 if (index == first_index - 1)
696 /* get the buf with our target record; avoid old garbage */
697 memset(buf, 0, chunk_size);
698 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
703 idx = rec->lrh_index;
704 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
705 while (idx < index) {
706 rec = (void *)rec + rec->lrh_len;
707 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
708 lustre_swab_llog_rec(rec);
711 LASSERT(idx == index);
712 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
714 /* process records in buffer, starting where we found one */
715 while ((void *)tail > buf) {
716 if (tail->lrt_index == 0)
717 GOTO(out, rc = 0); /* no more records */
719 /* if set, process the callback on this record */
720 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
721 rec = (void *)tail - tail->lrt_len +
724 rc = cb(env, loghandle, rec, data);
725 if (rc == LLOG_PROC_BREAK) {
727 } else if (rc == LLOG_DEL_RECORD) {
728 rc = llog_cancel_rec(env, loghandle,
735 /* previous record, still in buffer? */
737 if (index < first_index)
739 tail = (void *)tail - tail->lrt_len;
745 OBD_FREE_LARGE(buf, chunk_size);
748 EXPORT_SYMBOL(llog_reverse_process);
754 * llog_open - open llog, may not exist
755 * llog_exist - check if llog exists
756 * llog_close - close opened llog, pair for open, frees llog_handle
757 * llog_declare_create - declare llog creation
758 * llog_create - create new llog on disk, need transaction handle
759 * llog_declare_write_rec - declaration of llog write
760 * llog_write_rec - write llog record on disk, need transaction handle
761 * llog_declare_add - declare llog catalog record addition
762 * llog_add - add llog record in catalog, need transaction handle
764 int llog_exist(struct llog_handle *loghandle)
766 struct llog_operations *lop;
771 rc = llog_handle2ops(loghandle, &lop);
774 if (lop->lop_exist == NULL)
777 rc = lop->lop_exist(loghandle);
780 EXPORT_SYMBOL(llog_exist);
782 int llog_declare_create(const struct lu_env *env,
783 struct llog_handle *loghandle, struct thandle *th)
785 struct llog_operations *lop;
790 rc = llog_handle2ops(loghandle, &lop);
793 if (lop->lop_declare_create == NULL)
796 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
798 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
799 rc = lop->lop_declare_create(env, loghandle, th);
801 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
805 int llog_create(const struct lu_env *env, struct llog_handle *handle,
808 struct llog_operations *lop;
813 rc = llog_handle2ops(handle, &lop);
816 if (lop->lop_create == NULL)
819 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
821 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
822 rc = lop->lop_create(env, handle, th);
824 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
828 int llog_declare_write_rec(const struct lu_env *env,
829 struct llog_handle *handle,
830 struct llog_rec_hdr *rec, int idx,
833 struct llog_operations *lop;
838 rc = llog_handle2ops(handle, &lop);
842 if (lop->lop_declare_write_rec == NULL)
845 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
847 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
848 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
850 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
854 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
855 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
856 int idx, struct thandle *th)
858 struct llog_operations *lop;
859 int raised, rc, buflen;
863 rc = llog_handle2ops(handle, &lop);
868 if (lop->lop_write_rec == NULL)
871 buflen = rec->lrh_len;
872 LASSERT(cfs_size_round(buflen) == buflen);
874 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
876 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
877 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
879 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
883 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
884 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
891 if (lgh->lgh_logops->lop_add == NULL)
894 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
896 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
897 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
899 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
902 EXPORT_SYMBOL(llog_add);
904 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
905 struct llog_rec_hdr *rec, struct thandle *th)
911 if (lgh->lgh_logops->lop_declare_add == NULL)
914 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
916 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
917 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
919 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
922 EXPORT_SYMBOL(llog_declare_add);
925 * Helper function to open llog or create it if doesn't exist.
926 * It hides all transaction handling from caller.
928 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
929 struct llog_handle **res, struct llog_logid *logid,
938 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
942 if (llog_exist(*res))
945 LASSERT((*res)->lgh_obj != NULL);
947 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
949 th = dt_trans_create(env, d);
951 GOTO(out, rc = PTR_ERR(th));
953 /* Create update llog object synchronously, which
954 * happens during inialization process see
955 * lod_sub_prep_llog(), to make sure the update
956 * llog object is created before corss-MDT writing
957 * updates into the llog object */
958 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
961 th->th_wait_submit = 1;
962 rc = llog_declare_create(env, *res, th);
964 rc = dt_trans_start_local(env, d, th);
966 rc = llog_create(env, *res, th);
968 dt_trans_stop(env, d, th);
971 llog_close(env, *res);
974 EXPORT_SYMBOL(llog_open_create);
977 * Helper function to delete existent llog.
979 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
980 struct llog_logid *logid, char *name)
982 struct llog_handle *handle;
987 /* nothing to erase */
988 if (name == NULL && logid == NULL)
991 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
995 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
997 rc = llog_destroy(env, handle);
999 rc2 = llog_close(env, handle);
1004 EXPORT_SYMBOL(llog_erase);
1007 * Helper function for write record in llog.
1008 * It hides all transaction handling from caller.
1009 * Valid only with local llog.
1011 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1012 struct llog_rec_hdr *rec, int idx)
1014 struct dt_device *dt;
1021 LASSERT(loghandle->lgh_ctxt);
1022 LASSERT(loghandle->lgh_obj != NULL);
1024 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1026 th = dt_trans_create(env, dt);
1028 RETURN(PTR_ERR(th));
1030 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1032 GOTO(out_trans, rc);
1034 th->th_wait_submit = 1;
1035 rc = dt_trans_start_local(env, dt, th);
1037 GOTO(out_trans, rc);
1039 down_write(&loghandle->lgh_lock);
1040 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1041 up_write(&loghandle->lgh_lock);
1043 dt_trans_stop(env, dt, th);
1046 EXPORT_SYMBOL(llog_write);
1048 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1049 struct llog_handle **lgh, struct llog_logid *logid,
1050 char *name, enum llog_open_param open_param)
1058 LASSERT(ctxt->loc_logops);
1060 if (ctxt->loc_logops->lop_open == NULL) {
1062 RETURN(-EOPNOTSUPP);
1065 *lgh = llog_alloc_handle();
1068 (*lgh)->lgh_ctxt = ctxt;
1069 (*lgh)->lgh_logops = ctxt->loc_logops;
1071 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1073 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1074 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1076 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1078 llog_free_handle(*lgh);
1083 EXPORT_SYMBOL(llog_open);
1085 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1087 struct llog_operations *lop;
1092 rc = llog_handle2ops(loghandle, &lop);
1095 if (lop->lop_close == NULL)
1096 GOTO(out, rc = -EOPNOTSUPP);
1097 rc = lop->lop_close(env, loghandle);
1099 llog_handle_put(loghandle);
1102 EXPORT_SYMBOL(llog_close);
1105 * Helper function to get the llog size in records. It is used by MGS
1106 * mostly to check that config llog exists and contains data.
1108 * \param[in] env execution environment
1109 * \param[in] ctxt llog context
1110 * \param[in] name llog name
1112 * \retval true if there are records in llog besides a header
1113 * \retval false on error or llog without records
1115 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1118 struct llog_handle *llh;
1121 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1123 if (likely(rc == -ENOENT))
1128 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1130 GOTO(out_close, rc);
1131 rc = llog_get_size(llh);
1134 llog_close(env, llh);
1136 /* The header is record 1, the llog is still considered as empty
1137 * if there is only header */
1140 EXPORT_SYMBOL(llog_is_empty);
1142 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1143 struct llog_rec_hdr *rec, void *data)
1145 struct llog_handle *copy_llh = data;
1147 /* Append all records */
1148 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1151 /* backup plain llog */
1152 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1153 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1154 char *name, char *backup)
1156 struct llog_handle *llh, *bllh;
1161 /* open original log */
1162 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1164 /* the -ENOENT case is also reported to the caller
1165 * but silently so it should handle that if needed.
1168 CERROR("%s: failed to open log %s: rc = %d\n",
1169 obd->obd_name, name, rc);
1173 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1175 GOTO(out_close, rc);
1177 /* Make sure there's no old backup log */
1178 rc = llog_erase(env, bctxt, NULL, backup);
1179 if (rc < 0 && rc != -ENOENT)
1180 GOTO(out_close, rc);
1182 /* open backup log */
1183 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1185 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1186 obd->obd_name, backup, rc);
1187 GOTO(out_close, rc);
1190 /* check that backup llog is not the same object as original one */
1191 if (llh->lgh_obj == bllh->lgh_obj) {
1192 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1193 obd->obd_name, name, backup, llh->lgh_obj,
1195 GOTO(out_backup, rc = -EEXIST);
1198 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1200 GOTO(out_backup, rc);
1202 /* Copy log record by record */
1203 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1206 CERROR("%s: failed to backup log %s: rc = %d\n",
1207 obd->obd_name, name, rc);
1209 llog_close(env, bllh);
1211 llog_close(env, llh);
1214 EXPORT_SYMBOL(llog_backup);