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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/obdclass/llog.c
33 * OST<->MDS recovery logging infrastructure.
34 * Invariants in implementation:
35 * - we do not share logs among different OST<->MDS connections, so that
36 * if an OST or MDS fails it need only look at log(s) relevant to itself
38 * Author: Andreas Dilger <adilger@clusterfs.com>
39 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40 * Author: Mikhail Pershin <tappro@whamcloud.com>
43 #define DEBUG_SUBSYSTEM S_LOG
45 #include <linux/fs_struct.h>
46 #include <linux/pid_namespace.h>
47 #include <linux/kthread.h>
48 #include <llog_swab.h>
49 #include <lustre_log.h>
50 #include <obd_support.h>
51 #include <obd_class.h>
52 #include "llog_internal.h"
55 * Allocate a new log or catalog handle
56 * Used inside llog_open().
58 static struct llog_handle *llog_alloc_handle(void)
60 struct llog_handle *loghandle;
62 OBD_ALLOC_PTR(loghandle);
63 if (loghandle == NULL)
66 init_rwsem(&loghandle->lgh_lock);
67 mutex_init(&loghandle->lgh_hdr_mutex);
68 init_rwsem(&loghandle->lgh_last_sem);
69 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
70 refcount_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 struct llog_handle *llog_handle_get(struct llog_handle *loghandle)
97 if (refcount_inc_not_zero(&loghandle->lgh_refcount))
102 int llog_handle_put(const struct lu_env *env, struct llog_handle *loghandle)
106 if (refcount_dec_and_test(&loghandle->lgh_refcount)) {
107 const struct llog_operations *lop;
109 rc = llog_handle2ops(loghandle, &lop);
112 rc = lop->lop_close(env, loghandle);
116 llog_free_handle(loghandle);
121 static int llog_declare_destroy(const struct lu_env *env,
122 struct llog_handle *handle,
125 const struct llog_operations *lop;
130 rc = llog_handle2ops(handle, &lop);
133 if (lop->lop_declare_destroy == NULL)
136 rc = lop->lop_declare_destroy(env, handle, th);
141 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
144 const struct llog_operations *lop;
148 rc = llog_handle2ops(handle, &lop);
151 if (lop->lop_destroy == NULL)
154 LASSERT(handle->lgh_obj != NULL);
155 if (!llog_exist(handle))
158 rc = lop->lop_destroy(env, handle, th);
163 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
165 const struct llog_operations *lop;
166 struct dt_device *dt;
172 rc = llog_handle2ops(handle, &lop);
175 if (lop->lop_destroy == NULL)
178 if (handle->lgh_obj == NULL) {
179 /* if lgh_obj == NULL, then it is from client side destroy */
180 rc = lop->lop_destroy(env, handle, NULL);
184 if (!llog_exist(handle))
187 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
189 if (unlikely(unlikely(dt->dd_rdonly)))
192 th = dt_trans_create(env, dt);
196 rc = llog_declare_destroy(env, handle, th);
200 rc = dt_trans_start_local(env, dt, th);
204 rc = lop->lop_destroy(env, handle, th);
207 dt_trans_stop(env, dt, th);
211 EXPORT_SYMBOL(llog_destroy);
213 /* returns negative on error; 0 if success; 1 if success & log destroyed */
214 int llog_cancel_arr_rec(const struct lu_env *env, struct llog_handle *loghandle,
217 struct llog_thread_info *lgi = llog_info(env);
218 struct dt_device *dt;
219 struct llog_log_hdr *llh;
224 bool subtract_count = false;
228 LASSERT(loghandle != NULL);
229 LASSERT(loghandle->lgh_ctxt != NULL);
230 LASSERT(loghandle->lgh_obj != NULL);
232 llh = loghandle->lgh_hdr;
234 CDEBUG(D_RPCTRACE, "Canceling %d records, first %d in log "DFID"\n",
235 num, index[0], PLOGID(&loghandle->lgh_id));
237 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
239 if (unlikely(unlikely(dt->dd_rdonly)))
242 th = dt_trans_create(env, dt);
246 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, 0, th);
250 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
251 rc = llog_declare_destroy(env, loghandle, th);
256 th->th_wait_submit = 1;
257 rc = dt_trans_start_local(env, dt, th);
261 down_write(&loghandle->lgh_lock);
263 mutex_lock(&loghandle->lgh_hdr_mutex);
264 for (i = 0; i < num; ++i) {
266 CERROR("Can't cancel index 0 which is header\n");
267 GOTO(out_unlock, rc = -EINVAL);
269 if (!__test_and_clear_bit_le(index[i], LLOG_HDR_BITMAP(llh))) {
270 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n",
272 GOTO(out_unlock, rc = -ENOENT);
275 loghandle->lgh_hdr->llh_count -= num;
276 subtract_count = true;
278 /* Since llog_process_thread use lgi_cookie, it`s better to save them
279 * and restore after using
281 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
282 /* Pass this index to llog_osd_write_rec(), which will use the index
283 * to only update the necesary bitmap. */
284 lgi->lgi_cookie.lgc_index = index[0];
286 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, (num != 1 ? NULL :
287 &lgi->lgi_cookie), LLOG_HEADER_IDX, th);
288 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
291 GOTO(out_unlock, rc);
293 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
294 (llh->llh_count == 1) &&
295 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
296 (loghandle->u.phd.phd_cat_handle != NULL &&
297 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
299 /* never try to destroy it again */
300 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
301 rc = llog_trans_destroy(env, loghandle, th);
303 /* Sigh, can not destroy the final plain llog, but
304 * the bitmap has been clearly, so the record can not
305 * be accessed anymore, let's return 0 for now, and
306 * the orphan will be handled by LFSCK. */
307 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
308 loghandle2name(loghandle),
309 PLOGID(&loghandle->lgh_id), rc);
310 GOTO(out_unlock, rc = 0);
317 /* restore bitmap while holding a mutex */
318 if (subtract_count) {
319 loghandle->lgh_hdr->llh_count += num;
320 subtract_count = false;
322 for (i = i - 1; i >= 0; i--)
323 set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
325 mutex_unlock(&loghandle->lgh_hdr_mutex);
326 up_write(&loghandle->lgh_lock);
328 rc1 = dt_trans_stop(env, dt, th);
332 mutex_lock(&loghandle->lgh_hdr_mutex);
334 loghandle->lgh_hdr->llh_count += num;
335 for (i = i - 1; i >= 0; i--)
336 set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
337 mutex_unlock(&loghandle->lgh_hdr_mutex);
342 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
345 return llog_cancel_arr_rec(env, loghandle, 1, &index);
348 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
349 const struct obd_uuid *uuid)
351 const struct llog_operations *lop;
355 rc = llog_handle2ops(handle, &lop);
359 if (lop->lop_read_header == NULL)
362 rc = lop->lop_read_header(env, handle);
363 if (rc == LLOG_EEMPTY) {
364 struct llog_log_hdr *llh = handle->lgh_hdr;
366 /* lrh_len should be initialized in llog_init_handle */
367 handle->lgh_last_idx = 0; /* header is record with index 0 */
368 llh->llh_count = 1; /* for the header record */
369 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
370 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
371 LLOG_MIN_CHUNK_SIZE);
372 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
373 llh->llh_hdr.lrh_index = 0;
374 llh->llh_timestamp = ktime_get_real_seconds();
376 memcpy(&llh->llh_tgtuuid, uuid,
377 sizeof(llh->llh_tgtuuid));
378 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
379 /* Since update llog header might also call this function,
380 * let's reset the bitmap to 0 here */
381 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
382 llh->llh_bitmap_offset -
383 sizeof(llh->llh_tail));
384 set_bit_le(0, LLOG_HDR_BITMAP(llh));
385 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
386 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
391 EXPORT_SYMBOL(llog_read_header);
393 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
394 int flags, struct obd_uuid *uuid)
396 struct llog_log_hdr *llh;
397 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
399 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
402 LASSERT(handle->lgh_hdr == NULL);
404 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
405 OBD_ALLOC_LARGE(llh, chunk_size);
409 handle->lgh_hdr = llh;
410 handle->lgh_hdr_size = chunk_size;
411 /* first assign flags to use llog_client_ops */
412 llh->llh_flags = flags;
413 rc = llog_read_header(env, handle, uuid);
415 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
416 flags & LLOG_F_IS_CAT) ||
417 (llh->llh_flags & LLOG_F_IS_CAT &&
418 flags & LLOG_F_IS_PLAIN))) {
419 CERROR("%s: llog type is %s but initializing %s\n",
420 loghandle2name(handle),
421 llh->llh_flags & LLOG_F_IS_CAT ?
423 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
424 GOTO(out, rc = -EINVAL);
425 } else if (llh->llh_flags &
426 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
428 * it is possible to open llog without specifying llog
429 * type so it is taken from llh_flags
431 flags = llh->llh_flags;
433 /* for some reason the llh_flags has no type set */
434 CERROR("llog type is not specified!\n");
435 GOTO(out, rc = -EINVAL);
438 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
439 CERROR("%s: llog uuid mismatch: %s/%s\n",
440 loghandle2name(handle),
442 (char *)llh->llh_tgtuuid.uuid);
443 GOTO(out, rc = -EEXIST);
446 if (flags & LLOG_F_IS_CAT) {
447 LASSERT(list_empty(&handle->u.chd.chd_head));
448 INIT_LIST_HEAD(&handle->u.chd.chd_head);
449 llh->llh_size = sizeof(struct llog_logid_rec);
450 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
451 } else if (!(flags & LLOG_F_IS_PLAIN)) {
452 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
453 loghandle2name(handle), flags, LLOG_F_IS_CAT,
457 llh->llh_flags |= fmt;
460 OBD_FREE_LARGE(llh, chunk_size);
461 handle->lgh_hdr = NULL;
465 EXPORT_SYMBOL(llog_init_handle);
467 #define LLOG_ERROR_REC(lgh, rec, format, a...) \
468 CERROR("%s: "DFID" rec type=%x idx=%u len=%u, " format "\n" , \
469 loghandle2name(lgh), PLOGID(&lgh->lgh_id), (rec)->lrh_type, \
470 (rec)->lrh_index, (rec)->lrh_len, ##a)
472 int llog_verify_record(const struct llog_handle *llh, struct llog_rec_hdr *rec)
474 int chunk_size = llh->lgh_hdr->llh_hdr.lrh_len;
476 if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC)
477 LLOG_ERROR_REC(llh, rec, "magic is bad");
478 else if (rec->lrh_len == 0 || rec->lrh_len > chunk_size)
479 LLOG_ERROR_REC(llh, rec, "bad record len, chunk size is %d",
481 else if (rec->lrh_index >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr))
482 LLOG_ERROR_REC(llh, rec, "index is too high");
488 EXPORT_SYMBOL(llog_verify_record);
490 static inline bool llog_is_index_skipable(int idx, struct llog_log_hdr *llh,
491 struct llog_process_cat_data *cd)
493 if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
496 return !test_bit_le(idx, LLOG_HDR_BITMAP(llh));
499 static int llog_process_thread(void *arg)
501 struct llog_process_info *lpi = arg;
502 struct llog_handle *loghandle = lpi->lpi_loghandle;
503 struct llog_log_hdr *llh = loghandle->lgh_hdr;
504 struct llog_process_cat_data *cd = lpi->lpi_catdata;
505 struct llog_thread_info *lti;
509 int rc = 0, index = 1, last_index;
511 int last_called_index = 0;
512 bool repeated = false;
519 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
521 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
522 /* expect chunk_size to be power of two */
523 LASSERT(is_power_of_2(chunk_size));
525 OBD_ALLOC_LARGE(buf, chunk_size);
527 lpi->lpi_rc = -ENOMEM;
532 last_called_index = cd->lpcd_first_idx;
533 index = cd->lpcd_first_idx + 1;
535 if (cd && cd->lpcd_last_idx)
536 last_index = cd->lpcd_last_idx;
537 else if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
538 last_index = loghandle->lgh_last_idx;
540 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
543 struct llog_rec_hdr *rec;
544 off_t chunk_offset = 0;
545 unsigned int buf_offset = 0;
549 /* skip records not set in bitmap */
550 while (index <= last_index &&
551 llog_is_index_skipable(index, llh, cd))
554 /* There are no indices prior the last_index */
555 if (index > last_index)
558 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
562 /* get the buf with our target record; avoid old garbage */
563 memset(buf, 0, chunk_size);
564 /* the record index for outdated chunk data */
565 /* it is safe to process buffer until saved lgh_last_idx */
566 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
567 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
568 index, &cur_offset, buf, chunk_size);
570 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
571 " buf_offset %u, rc = %d\n", cur_offset,
572 (__u64)chunk_offset, buf_offset, rc);
575 /* we`ve tried to reread the chunk, but there is no
577 if (repeated && (chunk_offset + buf_offset) == cur_offset &&
578 (rc == -EBADR || rc == -EIO))
580 /* EOF while trying to skip to the next chunk */
581 if (!index && rc == -EBADR)
586 /* NB: after llog_next_block() call the cur_offset is the
587 * offset of the next block after read one.
588 * The absolute offset of the current chunk is calculated
589 * from cur_offset value and stored in chunk_offset variable.
591 if ((cur_offset & (chunk_size - 1)) != 0)
592 chunk_offset = cur_offset & ~(chunk_size - 1);
594 chunk_offset = cur_offset - chunk_size;
596 /* NB: when rec->lrh_len is accessed it is already swabbed
597 * since it is used at the "end" of the loop and the rec
598 * swabbing is done at the beginning of the loop. */
599 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
600 (char *)rec < buf + chunk_size;
601 rec = llog_rec_hdr_next(rec)) {
603 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
606 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
607 lustre_swab_llog_rec(rec);
609 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
610 rec->lrh_type, rec->lrh_index);
612 /* start with first rec if block was skipped */
615 "%s: skipping to the index %u\n",
616 loghandle2name(loghandle),
618 index = rec->lrh_index;
621 if (index == (synced_idx + 1) &&
622 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
625 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
626 cfs_fail_val == (unsigned int)
627 (loghandle->lgh_id.lgl_oi.oi.oi_id &
629 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
632 /* the bitmap could be changed during processing
633 * records from the chunk. For wrapped catalog
634 * it means we can read deleted record and try to
635 * process it. Check this case and reread the chunk.
636 * It is safe to process to lh_last_idx, including
637 * lh_last_idx if it was synced. We can not do <=
638 * comparison, cause for wrapped catalog lgh_last_idx
639 * could be less than index. So we detect last index
640 * for processing as index == lh_last_idx+1. But when
641 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
642 * the first processing index is llh_cat_idx+1.The
643 * exception is !(lgh_last_idx == llh_cat_idx &&
644 * index == llh_cat_idx + 1), and after simplification
646 * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
647 * This exception is working for catalog only.
648 * The last check is for the partial chunk boundary,
649 * if it is reached then try to re-read for possible
652 if ((index == lh_last_idx && synced_idx != index) ||
653 (index == (lh_last_idx + 1) &&
654 lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
655 (((char *)rec - buf >= cur_offset - chunk_offset) &&
657 /* save offset inside buffer for the re-read */
658 buf_offset = (char *)rec - (char *)buf;
659 cur_offset = chunk_offset;
661 /* We need to be sure lgh_last_idx
662 * record was saved to disk
664 down_read(&loghandle->lgh_last_sem);
665 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
666 up_read(&loghandle->lgh_last_sem);
667 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
672 rc = llog_verify_record(loghandle, rec);
674 CDEBUG(D_OTHER, "invalid record at index %d\n",
677 * for fixed-sized llogs we can skip one record
678 * by using llh_size from llog header.
679 * Otherwise skip the next llog chunk.
682 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
683 rec->lrh_len = llh->llh_size;
686 /* make sure that is always next block */
687 cur_offset = chunk_offset + chunk_size;
688 /* no goal to find, just next block to read */
693 if (rec->lrh_index < index) {
694 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
699 if (rec->lrh_index > index) {
700 /* the record itself looks good, but we met a
701 * gap which can be result of old bugs, just
703 LLOG_ERROR_REC(loghandle, rec,
704 "gap in index, expected %u",
706 index = rec->lrh_index;
710 "lrh_index: %d lrh_len: %d (%d remains)\n",
711 rec->lrh_index, rec->lrh_len,
712 (int)(buf + chunk_size - (char *)rec));
714 /* lgh_cur_offset is used only at llog_test_3 */
715 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
718 /* if needed, process the callback on this record */
719 if (!llog_is_index_skipable(index, llh, cd)) {
720 struct llog_cookie *lgc;
724 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
726 "index: %d, lh_last_idx: %d "
727 "synced_idx: %d lgh_last_idx: %d\n",
728 index, lh_last_idx, synced_idx,
729 loghandle->lgh_last_idx);
732 lgc = <i->lgi_cookie;
733 /* store lu_env for recursive calls */
734 tmp_off = lgc->lgc_offset;
735 tmp_idx = lgc->lgc_index;
737 lgc->lgc_offset = (char *)rec -
738 (char *)buf + chunk_offset;
739 lgc->lgc_index = rec->lrh_index;
741 /* using lu_env for passing record offset to
742 * llog_write through various callbacks */
743 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
745 last_called_index = index;
748 lgc->lgc_offset = tmp_off;
749 lgc->lgc_index = tmp_idx;
752 if (rc == LLOG_PROC_BREAK ||
753 rc == LLOG_SKIP_PLAIN) {
755 } else if (rc == LLOG_DEL_RECORD) {
756 rc = llog_cancel_rec(lpi->lpi_env,
759 /* Allow parallel cancelling, ENOENT
760 * means record was canceled at another
761 * processing thread or callback
768 /* some stupid callbacks directly cancel records
769 * and delete llog. Check it and stop
771 if (loghandle->lgh_hdr == NULL ||
772 loghandle->lgh_hdr->llh_count == 1)
776 /* exit if the last index is reached */
777 if (index >= last_index)
784 CDEBUG(D_HA, "stop processing %s "DFID" index %d count %d\n",
785 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
786 PLOGID(&loghandle->lgh_id), index, llh->llh_count);
789 cd->lpcd_last_idx = last_called_index;
791 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
792 if (dt_object_remote(loghandle->lgh_obj)) {
793 /* If it is remote object, then -EIO might means
794 * disconnection or eviction, let's return -EAGAIN,
795 * so for update recovery log processing, it will
796 * retry until the umount or abort recovery, see
797 * lod_sub_recovery_thread() */
798 CERROR("%s retry remote llog process\n",
799 loghandle2name(loghandle));
802 /* something bad happened to the processing of a local
803 * llog file, probably I/O error or the log got
804 * corrupted to be able to finally release the log we
805 * discard any remaining bits in the header */
806 CERROR("%s: local llog is corrupted "DFID" %s index %d count %d\n",
807 loghandle2name(loghandle),
808 PLOGID(&loghandle->lgh_id),
809 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
810 "plain"), index, llh->llh_count);
812 while (index <= last_index) {
813 if (test_bit_le(index,
814 LLOG_HDR_BITMAP(llh)) != 0)
815 llog_cancel_rec(lpi->lpi_env, loghandle,
823 OBD_FREE_LARGE(buf, chunk_size);
828 static int llog_process_thread_daemonize(void *arg)
830 struct llog_process_info *lpi = arg;
833 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
835 task_lock(lpi->lpi_reftask);
836 new_ns = lpi->lpi_reftask->nsproxy;
837 if (curr_ns != new_ns) {
840 current->nsproxy = new_ns;
841 /* XXX: we should call put_nsproxy() instead of
842 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
843 * used outside of the kernel itself, because it calls
844 * free_nsproxy() which is not exported by the kernel
845 * (defined in kernel/nsproxy.c) */
847 atomic_dec(&curr_ns->count);
849 task_unlock(lpi->lpi_reftask);
852 /* client env has no keys, tags is just 0 */
853 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
858 rc = llog_process_thread(arg);
862 complete(&lpi->lpi_completion);
866 int llog_process_or_fork(const struct lu_env *env,
867 struct llog_handle *loghandle,
868 llog_cb_t cb, void *data, void *catdata, bool fork)
870 struct llog_process_info *lpi;
871 struct llog_process_data *d = data;
872 struct llog_process_cat_data *cd = catdata;
873 __u32 flags = loghandle->lgh_hdr->llh_flags;
880 CERROR("cannot alloc pointer\n");
883 lpi->lpi_loghandle = loghandle;
885 lpi->lpi_cbdata = data;
886 lpi->lpi_catdata = catdata;
888 CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d read_mode %d\n",
889 PLOGID(&loghandle->lgh_id), flags,
890 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
891 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
892 cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1,
893 cd ? cd->lpcd_read_mode : -1);
895 struct task_struct *task;
897 /* The new thread can't use parent env,
898 * init the new one in llog_process_thread_daemonize. */
900 init_completion(&lpi->lpi_completion);
901 /* take reference to current, so that
902 * llog_process_thread_daemonize() can use it to switch to
903 * namespace associated with current */
904 lpi->lpi_reftask = current;
905 task = kthread_run(llog_process_thread_daemonize, lpi,
906 "llog_process_thread");
909 CERROR("%s: cannot start thread: rc = %d\n",
910 loghandle2name(loghandle), rc);
913 wait_for_completion(&lpi->lpi_completion);
916 llog_process_thread(lpi);
924 EXPORT_SYMBOL(llog_process_or_fork);
926 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
927 llog_cb_t cb, void *data, void *catdata)
930 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
931 return rc == LLOG_DEL_PLAIN ? 0 : rc;
933 EXPORT_SYMBOL(llog_process);
935 static inline const struct cred *llog_raise_resource(void)
937 struct cred *cred = NULL;
939 if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
942 cred = prepare_creds();
946 cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
947 return override_creds(cred);
950 static inline void llog_restore_resource(const struct cred *old_cred)
953 revert_creds(old_cred);
956 int llog_reverse_process(const struct lu_env *env,
957 struct llog_handle *loghandle, llog_cb_t cb,
958 void *data, void *catdata)
960 struct llog_log_hdr *llh = loghandle->lgh_hdr;
961 struct llog_process_cat_data *cd = catdata;
963 int rc = 0, first_index = 1, index, idx;
964 __u32 chunk_size = llh->llh_hdr.lrh_len;
967 OBD_ALLOC_LARGE(buf, chunk_size);
972 first_index = cd->lpcd_first_idx + 1;
973 if (cd != NULL && cd->lpcd_last_idx)
974 index = cd->lpcd_last_idx;
976 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
979 struct llog_rec_hdr *rec;
980 struct llog_rec_tail *tail;
982 /* skip records not set in bitmap */
983 while (index >= first_index &&
984 llog_is_index_skipable(index, llh, cd))
987 LASSERT(index >= first_index - 1);
988 if (index == first_index - 1)
991 /* get the buf with our target record; avoid old garbage */
992 memset(buf, 0, chunk_size);
993 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
998 idx = rec->lrh_index;
999 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
1000 while (idx < index) {
1001 rec = (void *)rec + rec->lrh_len;
1002 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1003 lustre_swab_llog_rec(rec);
1006 LASSERT(idx == index);
1007 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
1009 /* process records in buffer, starting where we found one */
1010 while ((void *)tail > buf) {
1011 if (tail->lrt_index == 0)
1012 GOTO(out, rc = 0); /* no more records */
1014 /* if needed, process the callback on this record */
1015 if (!llog_is_index_skipable(index, llh, cd)) {
1016 rec = (void *)tail - tail->lrt_len +
1019 rc = cb(env, loghandle, rec, data);
1020 if (rc == LLOG_PROC_BREAK ||
1021 rc == LLOG_SKIP_PLAIN) {
1023 } else if (rc == LLOG_DEL_RECORD) {
1024 rc = llog_cancel_rec(env, loghandle,
1031 /* previous record, still in buffer? */
1033 if (index < first_index)
1035 tail = (void *)tail - tail->lrt_len;
1041 OBD_FREE_LARGE(buf, chunk_size);
1044 EXPORT_SYMBOL(llog_reverse_process);
1050 * llog_open - open llog, may not exist
1051 * llog_exist - check if llog exists
1052 * llog_close - close opened llog, pair for open, frees llog_handle
1053 * llog_declare_create - declare llog creation
1054 * llog_create - create new llog on disk, need transaction handle
1055 * llog_declare_write_rec - declaration of llog write
1056 * llog_write_rec - write llog record on disk, need transaction handle
1057 * llog_declare_add - declare llog catalog record addition
1058 * llog_add - add llog record in catalog, need transaction handle
1060 int llog_exist(struct llog_handle *loghandle)
1062 const struct llog_operations *lop;
1067 rc = llog_handle2ops(loghandle, &lop);
1070 if (lop->lop_exist == NULL)
1071 RETURN(-EOPNOTSUPP);
1073 rc = lop->lop_exist(loghandle);
1076 EXPORT_SYMBOL(llog_exist);
1078 int llog_declare_create(const struct lu_env *env,
1079 struct llog_handle *loghandle, struct thandle *th)
1081 const struct cred *old_cred;
1082 const struct llog_operations *lop;
1087 rc = llog_handle2ops(loghandle, &lop);
1090 if (lop->lop_declare_create == NULL)
1091 RETURN(-EOPNOTSUPP);
1093 old_cred = llog_raise_resource();
1094 rc = lop->lop_declare_create(env, loghandle, th);
1095 llog_restore_resource(old_cred);
1099 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1102 const struct cred *old_cred;
1103 const struct llog_operations *lop;
1108 rc = llog_handle2ops(handle, &lop);
1111 if (lop->lop_create == NULL)
1112 RETURN(-EOPNOTSUPP);
1114 old_cred = llog_raise_resource();
1115 rc = lop->lop_create(env, handle, th);
1116 llog_restore_resource(old_cred);
1120 int llog_declare_write_rec(const struct lu_env *env,
1121 struct llog_handle *handle,
1122 struct llog_rec_hdr *rec, int idx,
1125 const struct cred *old_cred;
1126 const struct llog_operations *lop;
1131 rc = llog_handle2ops(handle, &lop);
1135 if (lop->lop_declare_write_rec == NULL)
1136 RETURN(-EOPNOTSUPP);
1138 old_cred = llog_raise_resource();
1139 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1140 llog_restore_resource(old_cred);
1144 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1145 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1146 int idx, struct thandle *th)
1148 const struct cred *old_cred;
1149 const struct llog_operations *lop;
1154 /* API sanity checks */
1155 if (handle == NULL) {
1156 CERROR("loghandle is missed\n");
1158 } else if (handle->lgh_obj == NULL) {
1159 CERROR("loghandle %p with NULL object\n",
1162 } else if (th == NULL) {
1163 CERROR("%s: missed transaction handle\n",
1164 loghandle2name(handle));
1166 } else if (handle->lgh_hdr == NULL) {
1167 CERROR("%s: loghandle %p with no header\n",
1168 loghandle2name(handle), handle);
1172 rc = llog_handle2ops(handle, &lop);
1176 if (lop->lop_write_rec == NULL)
1177 RETURN(-EOPNOTSUPP);
1179 buflen = rec->lrh_len;
1180 LASSERT(cfs_size_round(buflen) == buflen);
1182 old_cred = llog_raise_resource();
1183 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1184 llog_restore_resource(old_cred);
1188 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1189 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1192 const struct cred *old_cred;
1197 if (lgh->lgh_logops->lop_add == NULL)
1198 RETURN(-EOPNOTSUPP);
1200 old_cred = llog_raise_resource();
1201 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1202 llog_restore_resource(old_cred);
1205 EXPORT_SYMBOL(llog_add);
1207 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1208 struct llog_rec_hdr *rec, struct thandle *th)
1210 const struct cred *old_cred;
1215 if (lgh->lgh_logops->lop_declare_add == NULL)
1216 RETURN(-EOPNOTSUPP);
1218 old_cred = llog_raise_resource();
1219 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1220 llog_restore_resource(old_cred);
1223 EXPORT_SYMBOL(llog_declare_add);
1226 * Helper function to open llog or create it if doesn't exist.
1227 * It hides all transaction handling from caller.
1229 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1230 struct llog_handle **res, struct llog_logid *logid,
1233 struct dt_device *d;
1239 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1243 if (llog_exist(*res))
1246 LASSERT((*res)->lgh_obj != NULL);
1248 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1250 if (unlikely(unlikely(d->dd_rdonly)))
1253 th = dt_trans_create(env, d);
1255 GOTO(out, rc = PTR_ERR(th));
1257 /* Create update llog object synchronously, which
1258 * happens during inialization process see
1259 * lod_sub_prep_llog(), to make sure the update
1260 * llog object is created before corss-MDT writing
1261 * updates into the llog object */
1262 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1265 th->th_wait_submit = 1;
1266 rc = llog_declare_create(env, *res, th);
1268 rc = dt_trans_start_local(env, d, th);
1270 rc = llog_create(env, *res, th);
1272 dt_trans_stop(env, d, th);
1275 llog_close(env, *res);
1278 EXPORT_SYMBOL(llog_open_create);
1281 * Helper function to delete existent llog.
1283 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1284 struct llog_logid *logid, char *name)
1286 struct llog_handle *handle;
1291 /* nothing to erase */
1292 if (name == NULL && logid == NULL)
1295 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1299 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1301 rc = llog_destroy(env, handle);
1303 rc2 = llog_close(env, handle);
1308 EXPORT_SYMBOL(llog_erase);
1311 * Helper function for write record in llog.
1312 * It hides all transaction handling from caller.
1313 * Valid only with local llog.
1315 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1316 struct llog_rec_hdr *rec, int idx)
1318 struct dt_device *dt;
1326 LASSERT(loghandle->lgh_ctxt);
1327 LASSERT(loghandle->lgh_obj != NULL);
1329 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1331 if (unlikely(unlikely(dt->dd_rdonly)))
1334 th = dt_trans_create(env, dt);
1336 RETURN(PTR_ERR(th));
1338 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1340 GOTO(out_trans, rc);
1342 th->th_wait_submit = 1;
1343 rc = dt_trans_start_local(env, dt, th);
1345 GOTO(out_trans, rc);
1347 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1349 down_write(&loghandle->lgh_lock);
1351 struct llog_thread_info *lti = llog_info(env);
1353 /* cookie comes from llog_process_thread */
1354 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1355 rec->lrh_index, th);
1356 /* upper layer didn`t pass cookie so change rc */
1357 rc = (rc == 1 ? 0 : rc);
1359 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1362 up_write(&loghandle->lgh_lock);
1364 dt_trans_stop(env, dt, th);
1367 EXPORT_SYMBOL(llog_write);
1369 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1370 struct llog_handle **lgh, struct llog_logid *logid,
1371 char *name, enum llog_open_param open_param)
1373 const struct cred *old_cred;
1379 LASSERT(ctxt->loc_logops);
1381 if (ctxt->loc_logops->lop_open == NULL) {
1383 RETURN(-EOPNOTSUPP);
1386 *lgh = llog_alloc_handle();
1389 (*lgh)->lgh_ctxt = ctxt;
1390 (*lgh)->lgh_logops = ctxt->loc_logops;
1392 old_cred = llog_raise_resource();
1393 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1394 llog_restore_resource(old_cred);
1396 llog_free_handle(*lgh);
1401 EXPORT_SYMBOL(llog_open);
1403 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1405 return llog_handle_put(env, loghandle);
1407 EXPORT_SYMBOL(llog_close);
1410 * Helper function to get the llog size in records. It is used by MGS
1411 * mostly to check that config llog exists and contains data.
1413 * \param[in] env execution environment
1414 * \param[in] ctxt llog context
1415 * \param[in] name llog name
1417 * \retval true if there are records in llog besides a header
1418 * \retval false on error or llog without records
1420 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1423 struct llog_handle *llh;
1426 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1428 if (likely(rc == -ENOENT))
1433 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1435 GOTO(out_close, rc);
1436 rc = llog_get_size(llh);
1439 llog_close(env, llh);
1441 /* The header is record 1, the llog is still considered as empty
1442 * if there is only header */
1445 EXPORT_SYMBOL(llog_is_empty);
1447 /* this callback run in raw read mode (canceled record are processed) */
1448 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1449 struct llog_rec_hdr *rec, void *data)
1451 struct llog_handle *copy_llh = data;
1452 int idx = rec->lrh_index;
1457 /* Append all records */
1458 rc = llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1460 /* Cancel the record if it is canceled on the source */
1461 if (!rc && !test_bit_le(idx, LLOG_HDR_BITMAP(llh->lgh_hdr)))
1462 rc = llog_cancel_rec(env, copy_llh, copy_llh->lgh_last_idx);
1467 /* backup plain llog */
1468 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1469 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1470 char *name, char *backup)
1472 struct llog_handle *llh, *bllh;
1473 struct llog_process_cat_data cd = {0};
1478 /* open original log */
1479 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1481 /* the -ENOENT case is also reported to the caller
1482 * but silently so it should handle that if needed.
1485 CERROR("%s: failed to open log %s: rc = %d\n",
1486 obd->obd_name, name, rc);
1490 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1492 GOTO(out_close, rc);
1494 /* Make sure there's no old backup log */
1495 rc = llog_erase(env, bctxt, NULL, backup);
1496 if (rc < 0 && rc != -ENOENT)
1497 GOTO(out_close, rc);
1499 /* open backup log */
1500 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1502 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1503 obd->obd_name, backup, rc);
1504 GOTO(out_close, rc);
1507 /* check that backup llog is not the same object as original one */
1508 if (llh->lgh_obj == bllh->lgh_obj) {
1509 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1510 obd->obd_name, name, backup, llh->lgh_obj,
1512 GOTO(out_backup, rc = -EEXIST);
1515 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1517 GOTO(out_backup, rc);
1519 /* Read canceled records to have an exact copy */
1520 cd.lpcd_read_mode = LLOG_READ_MODE_RAW;
1521 /* Copy log record by record */
1522 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1525 CERROR("%s: failed to backup log %s: rc = %d\n",
1526 obd->obd_name, name, rc);
1528 llog_close(env, bllh);
1530 llog_close(env, llh);
1533 EXPORT_SYMBOL(llog_backup);
1535 /* Get size of llog */
1536 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1541 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1543 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1544 loghandle2name(llh), PLOGID(&llh->lgh_id), rc);
1550 EXPORT_SYMBOL(llog_size);