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);
195 th->th_wait_submit = 1;
197 rc = llog_declare_destroy(env, handle, th);
201 rc = dt_trans_start_local(env, dt, th);
205 rc = lop->lop_destroy(env, handle, th);
208 dt_trans_stop(env, dt, th);
212 EXPORT_SYMBOL(llog_destroy);
214 /* returns negative on error; 0 if success; 1 if success & log destroyed */
215 int llog_cancel_arr_rec(const struct lu_env *env, struct llog_handle *loghandle,
218 struct llog_thread_info *lgi = llog_info(env);
219 struct dt_device *dt;
220 struct llog_log_hdr *llh;
225 bool subtract_count = false;
229 LASSERT(loghandle != NULL);
230 LASSERT(loghandle->lgh_ctxt != NULL);
231 LASSERT(loghandle->lgh_obj != NULL);
233 llh = loghandle->lgh_hdr;
235 CDEBUG(D_OTHER, "Canceling %d records, first %d in log "DFID"\n",
236 num, index[0], PLOGID(&loghandle->lgh_id));
238 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
240 if (unlikely(unlikely(dt->dd_rdonly)))
243 th = dt_trans_create(env, dt);
247 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, 0, th);
251 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
252 rc = llog_declare_destroy(env, loghandle, th);
257 th->th_wait_submit = 1;
258 rc = dt_trans_start_local(env, dt, th);
262 down_write(&loghandle->lgh_lock);
264 mutex_lock(&loghandle->lgh_hdr_mutex);
265 for (i = 0; i < num; ++i) {
267 CERROR("Can't cancel index 0 which is header\n");
268 GOTO(out_unlock, rc = -EINVAL);
270 if (!__test_and_clear_bit_le(index[i], LLOG_HDR_BITMAP(llh))) {
271 CDEBUG(D_OTHER, "Catalog index %u already clear?\n",
273 GOTO(out_unlock, rc = -ENOENT);
276 loghandle->lgh_hdr->llh_count -= num;
277 subtract_count = true;
279 /* Since llog_process_thread use lgi_cookie, it`s better to save them
280 * and restore after using
282 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
283 /* Pass this index to llog_osd_write_rec(), which will use the index
284 * to only update the necesary bitmap. */
285 lgi->lgi_cookie.lgc_index = index[0];
287 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, (num != 1 ? NULL :
288 &lgi->lgi_cookie), LLOG_HEADER_IDX, th);
289 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
292 GOTO(out_unlock, rc);
294 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
295 (llh->llh_count == 1) &&
296 ((loghandle->lgh_last_idx == llog_max_idx(llh)) ||
297 (loghandle->u.phd.phd_cat_handle != NULL &&
298 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
300 /* never try to destroy it again */
301 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
302 rc = llog_trans_destroy(env, loghandle, th);
304 /* Sigh, can not destroy the final plain llog, but
305 * the bitmap has been clearly, so the record can not
306 * be accessed anymore, let's return 0 for now, and
307 * the orphan will be handled by LFSCK. */
308 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
309 loghandle2name(loghandle),
310 PLOGID(&loghandle->lgh_id), rc);
311 GOTO(out_unlock, rc = 0);
318 /* restore bitmap while holding a mutex */
319 if (subtract_count) {
320 loghandle->lgh_hdr->llh_count += num;
321 subtract_count = false;
323 for (i = i - 1; i >= 0; i--)
324 set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
326 mutex_unlock(&loghandle->lgh_hdr_mutex);
327 up_write(&loghandle->lgh_lock);
329 rc1 = dt_trans_stop(env, dt, th);
333 mutex_lock(&loghandle->lgh_hdr_mutex);
335 loghandle->lgh_hdr->llh_count += num;
336 for (i = i - 1; i >= 0; i--)
337 set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
338 mutex_unlock(&loghandle->lgh_hdr_mutex);
343 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
346 return llog_cancel_arr_rec(env, loghandle, 1, &index);
349 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
350 const struct obd_uuid *uuid)
352 const struct llog_operations *lop;
356 rc = llog_handle2ops(handle, &lop);
360 if (lop->lop_read_header == NULL)
363 rc = lop->lop_read_header(env, handle);
364 if (rc == LLOG_EEMPTY) {
365 struct llog_log_hdr *llh = handle->lgh_hdr;
367 /* lrh_len should be initialized in llog_init_handle */
368 handle->lgh_last_idx = 0; /* header is record with index 0 */
369 llh->llh_count = 1; /* for the header record */
370 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
371 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
372 LLOG_MIN_CHUNK_SIZE);
373 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
374 llh->llh_hdr.lrh_index = 0;
375 llh->llh_timestamp = ktime_get_real_seconds();
377 memcpy(&llh->llh_tgtuuid, uuid,
378 sizeof(llh->llh_tgtuuid));
379 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
380 /* Since update llog header might also call this function,
381 * let's reset the bitmap to 0 here */
382 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
383 llh->llh_bitmap_offset -
384 sizeof(llh->llh_tail));
385 set_bit_le(0, LLOG_HDR_BITMAP(llh));
386 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
387 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
392 EXPORT_SYMBOL(llog_read_header);
394 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
395 int flags, struct obd_uuid *uuid)
397 struct llog_log_hdr *llh;
398 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
400 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
404 LASSERT(handle->lgh_hdr == NULL);
406 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
407 OBD_ALLOC_LARGE(llh, chunk_size);
411 handle->lgh_hdr = llh;
412 handle->lgh_hdr_size = chunk_size;
413 /* first assign flags to use llog_client_ops */
414 llh->llh_flags = flags;
415 rc = llog_read_header(env, handle, uuid);
417 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
418 flags & LLOG_F_IS_CAT) ||
419 (llh->llh_flags & LLOG_F_IS_CAT &&
420 flags & LLOG_F_IS_PLAIN))) {
421 CERROR("%s: llog type is %s but initializing %s\n",
422 loghandle2name(handle),
423 llh->llh_flags & LLOG_F_IS_CAT ?
425 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
426 GOTO(out, rc = -EINVAL);
427 } else if (llh->llh_flags &
428 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
430 * it is possible to open llog without specifying llog
431 * type so it is taken from llh_flags
433 flags = llh->llh_flags;
435 /* for some reason the llh_flags has no type set */
436 CERROR("llog type is not specified!\n");
437 GOTO(out, rc = -EINVAL);
440 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
441 CERROR("%s: llog uuid mismatch: %s/%s\n",
442 loghandle2name(handle),
444 (char *)llh->llh_tgtuuid.uuid);
445 GOTO(out, rc = -EINVAL);
448 if (flags & LLOG_F_IS_CAT) {
449 LASSERT(list_empty(&handle->u.chd.chd_head));
450 INIT_LIST_HEAD(&handle->u.chd.chd_head);
451 llh->llh_size = sizeof(struct llog_logid_rec);
452 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
453 } else if (!(flags & LLOG_F_IS_PLAIN)) {
454 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
455 loghandle2name(handle), flags, LLOG_F_IS_CAT,
459 llh->llh_flags |= fmt;
462 OBD_FREE_LARGE(llh, chunk_size);
463 handle->lgh_hdr = NULL;
467 EXPORT_SYMBOL(llog_init_handle);
469 #define LLOG_ERROR_REC(lgh, rec, format, a...) \
470 CERROR("%s: "DFID" rec type=%x idx=%u len=%u, " format "\n" , \
471 loghandle2name(lgh), PLOGID(&lgh->lgh_id), (rec)->lrh_type, \
472 (rec)->lrh_index, (rec)->lrh_len, ##a)
474 int llog_verify_record(const struct llog_handle *llh, struct llog_rec_hdr *rec)
476 int chunk_size = llh->lgh_hdr->llh_hdr.lrh_len;
478 if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC)
479 LLOG_ERROR_REC(llh, rec, "magic is bad");
480 else if (rec->lrh_len == 0 || rec->lrh_len > chunk_size)
481 LLOG_ERROR_REC(llh, rec, "bad record len, chunk size is %d",
483 else if (rec->lrh_index > llog_max_idx(llh->lgh_hdr))
484 LLOG_ERROR_REC(llh, rec, "index is too high");
490 EXPORT_SYMBOL(llog_verify_record);
492 static inline bool llog_is_index_skipable(int idx, struct llog_log_hdr *llh,
493 struct llog_process_cat_data *cd)
495 if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
498 return !test_bit_le(idx, LLOG_HDR_BITMAP(llh));
501 static int llog_process_thread(void *arg)
503 struct llog_process_info *lpi = arg;
504 struct llog_handle *loghandle = lpi->lpi_loghandle;
505 struct llog_log_hdr *llh = loghandle->lgh_hdr;
506 struct llog_process_cat_data *cd = lpi->lpi_catdata;
507 struct llog_thread_info *lti;
511 int rc = 0, index = 1, last_index;
513 int last_called_index = 0;
514 bool repeated = false;
521 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
523 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
524 /* expect chunk_size to be power of two */
525 LASSERT(is_power_of_2(chunk_size));
527 OBD_ALLOC_LARGE(buf, chunk_size);
529 lpi->lpi_rc = -ENOMEM;
533 last_index = llog_max_idx(llh);
535 if (cd->lpcd_first_idx >= llog_max_idx(llh))
536 /* End of the indexes -> Nothing to do */
539 index = cd->lpcd_first_idx + 1;
540 last_called_index = cd->lpcd_first_idx;
541 if (cd->lpcd_last_idx > 0 &&
542 cd->lpcd_last_idx <= llog_max_idx(llh))
543 last_index = cd->lpcd_last_idx;
544 else if (cd->lpcd_read_mode & LLOG_READ_MODE_RAW)
545 last_index = loghandle->lgh_last_idx;
549 struct llog_rec_hdr *rec;
550 off_t chunk_offset = 0;
551 unsigned int buf_offset = 0;
555 /* skip records not set in bitmap */
556 while (index <= last_index &&
557 llog_is_index_skipable(index, llh, cd))
560 /* There are no indices prior the last_index */
561 if (index > last_index)
564 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
568 /* get the buf with our target record; avoid old garbage */
569 memset(buf, 0, chunk_size);
570 /* the record index for outdated chunk data */
571 /* it is safe to process buffer until saved lgh_last_idx */
572 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
573 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
574 index, &cur_offset, buf, chunk_size);
576 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
577 " buf_offset %u, rc = %d\n", cur_offset,
578 (__u64)chunk_offset, buf_offset, rc);
581 /* we`ve tried to reread the chunk, but there is no
583 if (repeated && (chunk_offset + buf_offset) == cur_offset &&
584 (rc == -EBADR || rc == -EIO))
586 /* EOF while trying to skip to the next chunk */
587 if (!index && rc == -EBADR)
592 /* NB: after llog_next_block() call the cur_offset is the
593 * offset of the next block after read one.
594 * The absolute offset of the current chunk is calculated
595 * from cur_offset value and stored in chunk_offset variable.
597 if ((cur_offset & (chunk_size - 1)) != 0)
598 chunk_offset = cur_offset & ~(chunk_size - 1);
600 chunk_offset = cur_offset - chunk_size;
602 /* NB: when rec->lrh_len is accessed it is already swabbed
603 * since it is used at the "end" of the loop and the rec
604 * swabbing is done at the beginning of the loop. */
605 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
606 (char *)rec < buf + chunk_size;
607 rec = llog_rec_hdr_next(rec)) {
609 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
612 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
613 lustre_swab_llog_rec(rec);
615 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
616 rec->lrh_type, rec->lrh_index);
618 /* start with first rec if block was skipped */
621 "%s: skipping to the index %u\n",
622 loghandle2name(loghandle),
624 index = rec->lrh_index;
627 if (index == (synced_idx + 1) &&
628 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
631 if (CFS_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
632 cfs_fail_val == (unsigned int)
633 (loghandle->lgh_id.lgl_oi.oi.oi_id &
635 CFS_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
638 /* the bitmap could be changed during processing
639 * records from the chunk. For wrapped catalog
640 * it means we can read deleted record and try to
641 * process it. Check this case and reread the chunk.
642 * It is safe to process to lh_last_idx, including
643 * lh_last_idx if it was synced. We can not do <=
644 * comparison, cause for wrapped catalog lgh_last_idx
645 * could be less than index. So we detect last index
646 * for processing as index == lh_last_idx+1. But when
647 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
648 * the first processing index is llh_cat_idx+1.The
649 * exception is !(lgh_last_idx == llh_cat_idx &&
650 * index == llh_cat_idx + 1), and after simplification
652 * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
653 * This exception is working for catalog only.
654 * The last check is for the partial chunk boundary,
655 * if it is reached then try to re-read for possible
658 if ((index == lh_last_idx && synced_idx != index) ||
659 (index == (lh_last_idx + 1) &&
660 lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
661 (((char *)rec - buf >= cur_offset - chunk_offset) &&
663 /* save offset inside buffer for the re-read */
664 buf_offset = (char *)rec - (char *)buf;
665 cur_offset = chunk_offset;
667 /* We need to be sure lgh_last_idx
668 * record was saved to disk
670 down_read(&loghandle->lgh_last_sem);
671 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
672 up_read(&loghandle->lgh_last_sem);
673 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
678 rc = llog_verify_record(loghandle, rec);
680 CDEBUG(D_OTHER, "invalid record at index %d\n",
683 * for fixed-sized llogs we can skip one record
684 * by using llh_size from llog header.
685 * Otherwise skip the next llog chunk.
688 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
689 rec->lrh_len = llh->llh_size;
692 /* make sure that is always next block */
693 cur_offset = chunk_offset + chunk_size;
694 /* no goal to find, just next block to read */
699 if (rec->lrh_index < index) {
700 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
705 if (rec->lrh_index > index) {
706 /* the record itself looks good, but we met a
707 * gap which can be result of old bugs, just
709 LLOG_ERROR_REC(loghandle, rec,
710 "gap in index, expected %u",
712 index = rec->lrh_index;
716 "lrh_index: %d lrh_len: %d (%d remains)\n",
717 rec->lrh_index, rec->lrh_len,
718 (int)(buf + chunk_size - (char *)rec));
720 /* lgh_cur_offset is used only at llog_test_3 */
721 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
724 /* if needed, process the callback on this record */
725 if (!llog_is_index_skipable(index, llh, cd)) {
726 struct llog_cookie *lgc;
730 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
732 "index: %d, lh_last_idx: %d "
733 "synced_idx: %d lgh_last_idx: %d\n",
734 index, lh_last_idx, synced_idx,
735 loghandle->lgh_last_idx);
738 lgc = <i->lgi_cookie;
739 /* store lu_env for recursive calls */
740 tmp_off = lgc->lgc_offset;
741 tmp_idx = lgc->lgc_index;
743 lgc->lgc_offset = (char *)rec -
744 (char *)buf + chunk_offset;
745 lgc->lgc_index = rec->lrh_index;
747 /* using lu_env for passing record offset to
748 * llog_write through various callbacks */
749 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
751 last_called_index = index;
754 lgc->lgc_offset = tmp_off;
755 lgc->lgc_index = tmp_idx;
758 if (rc == LLOG_PROC_BREAK ||
759 rc == LLOG_SKIP_PLAIN) {
761 } else if (rc == LLOG_DEL_RECORD) {
762 rc = llog_cancel_rec(lpi->lpi_env,
765 /* Allow parallel cancelling, ENOENT
766 * means record was canceled at another
767 * processing thread or callback
774 /* some stupid callbacks directly cancel records
775 * and delete llog. Check it and stop
777 if (loghandle->lgh_hdr == NULL ||
778 loghandle->lgh_hdr->llh_count == 1)
782 /* exit if the last index is reached */
783 if (index >= last_index)
790 CDEBUG(D_HA, "stop processing %s "DFID" index %d count %d\n",
791 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
792 PLOGID(&loghandle->lgh_id), index, llh->llh_count);
795 cd->lpcd_last_idx = last_called_index;
797 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
798 if (dt_object_remote(loghandle->lgh_obj)) {
799 /* If it is remote object, then -EIO might means
800 * disconnection or eviction, let's return -EAGAIN,
801 * so for update recovery log processing, it will
802 * retry until the umount or abort recovery, see
803 * lod_sub_recovery_thread() */
804 CERROR("%s retry remote llog process\n",
805 loghandle2name(loghandle));
808 /* something bad happened to the processing of a local
809 * llog file, probably I/O error or the log got
810 * corrupted to be able to finally release the log we
811 * discard any remaining bits in the header */
812 CERROR("%s: local llog is corrupted "DFID" %s index %d count %d\n",
813 loghandle2name(loghandle),
814 PLOGID(&loghandle->lgh_id),
815 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
816 "plain"), index, llh->llh_count);
818 while (index <= last_index) {
819 if (test_bit_le(index,
820 LLOG_HDR_BITMAP(llh)) != 0)
821 llog_cancel_rec(lpi->lpi_env, loghandle,
829 OBD_FREE_LARGE(buf, chunk_size);
834 static int llog_process_thread_daemonize(void *arg)
836 struct llog_process_info *lpi = arg;
839 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
841 task_lock(lpi->lpi_reftask);
842 new_ns = lpi->lpi_reftask->nsproxy;
843 if (curr_ns != new_ns) {
846 current->nsproxy = new_ns;
847 /* XXX: we should call put_nsproxy() instead of
848 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
849 * used outside of the kernel itself, because it calls
850 * free_nsproxy() which is not exported by the kernel
851 * (defined in kernel/nsproxy.c) */
853 nsproxy_dec(curr_ns);
855 task_unlock(lpi->lpi_reftask);
858 /* client env has no keys, tags is just 0 */
859 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
864 rc = llog_process_thread(arg);
868 complete(&lpi->lpi_completion);
872 int llog_process_or_fork(const struct lu_env *env,
873 struct llog_handle *loghandle,
874 llog_cb_t cb, void *data, void *catdata, bool fork)
876 struct llog_process_info *lpi;
877 struct llog_process_data *d = data;
878 struct llog_process_cat_data *cd = catdata;
879 __u32 flags = loghandle->lgh_hdr->llh_flags;
886 CERROR("cannot alloc pointer\n");
889 lpi->lpi_loghandle = loghandle;
891 lpi->lpi_cbdata = data;
892 lpi->lpi_catdata = catdata;
894 CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d read_mode %d\n",
895 PLOGID(&loghandle->lgh_id), flags,
896 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
897 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
898 cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1,
899 cd ? cd->lpcd_read_mode : -1);
901 struct task_struct *task;
903 /* The new thread can't use parent env,
904 * init the new one in llog_process_thread_daemonize. */
906 init_completion(&lpi->lpi_completion);
907 /* take reference to current, so that
908 * llog_process_thread_daemonize() can use it to switch to
909 * namespace associated with current */
910 lpi->lpi_reftask = current;
911 task = kthread_run(llog_process_thread_daemonize, lpi,
912 "llog_process_thread");
915 CERROR("%s: cannot start thread: rc = %d\n",
916 loghandle2name(loghandle), rc);
919 wait_for_completion(&lpi->lpi_completion);
922 llog_process_thread(lpi);
930 EXPORT_SYMBOL(llog_process_or_fork);
932 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
933 llog_cb_t cb, void *data, void *catdata)
936 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
937 return rc == LLOG_DEL_PLAIN ? 0 : rc;
939 EXPORT_SYMBOL(llog_process);
941 static inline const struct cred *llog_raise_resource(void)
943 struct cred *cred = NULL;
945 if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
948 cred = prepare_creds();
952 cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
953 return override_creds(cred);
956 static inline void llog_restore_resource(const struct cred *old_cred)
959 revert_creds(old_cred);
962 int llog_reverse_process(const struct lu_env *env,
963 struct llog_handle *loghandle, llog_cb_t cb,
964 void *data, void *catdata)
966 struct llog_log_hdr *llh = loghandle->lgh_hdr;
967 struct llog_process_cat_data *cd = catdata;
969 int rc = 0, first_index = 1, index, idx;
970 __u32 chunk_size = llh->llh_hdr.lrh_len;
973 OBD_ALLOC_LARGE(buf, chunk_size);
978 first_index = cd->lpcd_first_idx + 1;
979 if (cd != NULL && cd->lpcd_last_idx)
980 index = cd->lpcd_last_idx;
982 index = llog_max_idx(llh);
985 struct llog_rec_hdr *rec;
986 struct llog_rec_tail *tail;
988 /* skip records not set in bitmap */
989 while (index >= first_index &&
990 llog_is_index_skipable(index, llh, cd))
993 LASSERT(index >= first_index - 1);
994 if (index == first_index - 1)
997 /* get the buf with our target record; avoid old garbage */
998 memset(buf, 0, chunk_size);
999 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
1004 idx = rec->lrh_index;
1005 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
1006 while (idx < index) {
1007 rec = (void *)rec + rec->lrh_len;
1008 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1009 lustre_swab_llog_rec(rec);
1012 LASSERT(idx == index);
1013 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
1015 /* process records in buffer, starting where we found one */
1016 while ((void *)tail > buf) {
1017 if (tail->lrt_index == 0)
1018 GOTO(out, rc = 0); /* no more records */
1020 /* if needed, process the callback on this record */
1021 if (!llog_is_index_skipable(index, llh, cd)) {
1022 rec = (void *)tail - tail->lrt_len +
1025 rc = cb(env, loghandle, rec, data);
1026 if (rc == LLOG_PROC_BREAK ||
1027 rc == LLOG_SKIP_PLAIN) {
1029 } else if (rc == LLOG_DEL_RECORD) {
1030 rc = llog_cancel_rec(env, loghandle,
1037 /* previous record, still in buffer? */
1039 if (index < first_index)
1041 tail = (void *)tail - tail->lrt_len;
1047 OBD_FREE_LARGE(buf, chunk_size);
1050 EXPORT_SYMBOL(llog_reverse_process);
1056 * llog_open - open llog, may not exist
1057 * llog_exist - check if llog exists
1058 * llog_close - close opened llog, pair for open, frees llog_handle
1059 * llog_declare_create - declare llog creation
1060 * llog_create - create new llog on disk, need transaction handle
1061 * llog_declare_write_rec - declaration of llog write
1062 * llog_write_rec - write llog record on disk, need transaction handle
1063 * llog_declare_add - declare llog catalog record addition
1064 * llog_add - add llog record in catalog, need transaction handle
1066 int llog_exist(struct llog_handle *loghandle)
1068 const struct llog_operations *lop;
1073 rc = llog_handle2ops(loghandle, &lop);
1076 if (lop->lop_exist == NULL)
1077 RETURN(-EOPNOTSUPP);
1079 rc = lop->lop_exist(loghandle);
1082 EXPORT_SYMBOL(llog_exist);
1084 int llog_declare_create(const struct lu_env *env,
1085 struct llog_handle *loghandle, struct thandle *th)
1087 const struct cred *old_cred;
1088 const struct llog_operations *lop;
1093 rc = llog_handle2ops(loghandle, &lop);
1096 if (lop->lop_declare_create == NULL)
1097 RETURN(-EOPNOTSUPP);
1099 old_cred = llog_raise_resource();
1100 rc = lop->lop_declare_create(env, loghandle, th);
1101 llog_restore_resource(old_cred);
1105 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1108 const struct cred *old_cred;
1109 const struct llog_operations *lop;
1114 rc = llog_handle2ops(handle, &lop);
1117 if (lop->lop_create == NULL)
1118 RETURN(-EOPNOTSUPP);
1120 old_cred = llog_raise_resource();
1121 rc = lop->lop_create(env, handle, th);
1122 llog_restore_resource(old_cred);
1126 int llog_declare_write_rec(const struct lu_env *env,
1127 struct llog_handle *handle,
1128 struct llog_rec_hdr *rec, int idx,
1131 const struct cred *old_cred;
1132 const struct llog_operations *lop;
1137 rc = llog_handle2ops(handle, &lop);
1141 if (lop->lop_declare_write_rec == NULL)
1142 RETURN(-EOPNOTSUPP);
1144 old_cred = llog_raise_resource();
1145 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1146 llog_restore_resource(old_cred);
1150 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1151 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1152 int idx, struct thandle *th)
1154 const struct cred *old_cred;
1155 const struct llog_operations *lop;
1160 /* API sanity checks */
1161 if (handle == NULL) {
1162 CERROR("loghandle is missed\n");
1164 } else if (handle->lgh_obj == NULL) {
1165 CERROR("loghandle %p with NULL object\n",
1168 } else if (th == NULL) {
1169 CERROR("%s: missed transaction handle\n",
1170 loghandle2name(handle));
1172 } else if (handle->lgh_hdr == NULL) {
1173 CERROR("%s: loghandle %p with no header\n",
1174 loghandle2name(handle), handle);
1178 rc = llog_handle2ops(handle, &lop);
1182 if (lop->lop_write_rec == NULL)
1183 RETURN(-EOPNOTSUPP);
1185 buflen = rec->lrh_len;
1186 LASSERT(round_up(buflen, 8) == buflen);
1188 old_cred = llog_raise_resource();
1189 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1190 llog_restore_resource(old_cred);
1194 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1195 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1198 const struct cred *old_cred;
1203 if (lgh->lgh_logops->lop_add == NULL)
1204 RETURN(-EOPNOTSUPP);
1206 old_cred = llog_raise_resource();
1207 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1208 llog_restore_resource(old_cred);
1211 EXPORT_SYMBOL(llog_add);
1213 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1214 struct llog_rec_hdr *rec, struct thandle *th)
1216 const struct cred *old_cred;
1221 if (lgh->lgh_logops->lop_declare_add == NULL)
1222 RETURN(-EOPNOTSUPP);
1224 old_cred = llog_raise_resource();
1225 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1226 llog_restore_resource(old_cred);
1229 EXPORT_SYMBOL(llog_declare_add);
1232 * Helper function to open llog or create it if doesn't exist.
1233 * It hides all transaction handling from caller.
1235 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1236 struct llog_handle **res, struct llog_logid *logid,
1239 struct dt_device *d;
1245 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1249 if (llog_exist(*res))
1252 LASSERT((*res)->lgh_obj != NULL);
1254 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1256 if (unlikely(unlikely(d->dd_rdonly)))
1259 th = dt_trans_create(env, d);
1261 GOTO(out, rc = PTR_ERR(th));
1263 /* Create update llog object synchronously, which
1264 * happens during inialization process see
1265 * lod_sub_prep_llog(), to make sure the update
1266 * llog object is created before corss-MDT writing
1267 * updates into the llog object */
1268 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1271 th->th_wait_submit = 1;
1272 rc = llog_declare_create(env, *res, th);
1274 rc = dt_trans_start_local(env, d, th);
1276 rc = llog_create(env, *res, th);
1278 dt_trans_stop(env, d, th);
1281 llog_close(env, *res);
1284 EXPORT_SYMBOL(llog_open_create);
1287 * Helper function to delete existent llog.
1289 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1290 struct llog_logid *logid, char *name)
1292 struct llog_handle *handle;
1297 /* nothing to erase */
1298 if (name == NULL && logid == NULL)
1301 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1305 rc = llog_destroy(env, handle);
1306 llog_close(env, handle);
1310 EXPORT_SYMBOL(llog_erase);
1313 * Helper function for write record in llog.
1314 * It hides all transaction handling from caller.
1315 * Valid only with local llog.
1317 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1318 struct llog_rec_hdr *rec, int idx)
1320 struct dt_device *dt;
1328 LASSERT(loghandle->lgh_ctxt);
1329 LASSERT(loghandle->lgh_obj != NULL);
1331 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1333 if (unlikely(unlikely(dt->dd_rdonly)))
1336 th = dt_trans_create(env, dt);
1338 RETURN(PTR_ERR(th));
1340 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1342 GOTO(out_trans, rc);
1344 th->th_wait_submit = 1;
1345 rc = dt_trans_start_local(env, dt, th);
1347 GOTO(out_trans, rc);
1349 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1351 down_write(&loghandle->lgh_lock);
1353 struct llog_thread_info *lti = llog_info(env);
1355 /* cookie comes from llog_process_thread */
1356 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1357 rec->lrh_index, th);
1358 /* upper layer didn`t pass cookie so change rc */
1359 rc = (rc == 1 ? 0 : rc);
1361 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1364 up_write(&loghandle->lgh_lock);
1366 dt_trans_stop(env, dt, th);
1369 EXPORT_SYMBOL(llog_write);
1371 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1372 struct llog_handle **lgh, struct llog_logid *logid,
1373 char *name, enum llog_open_param open_param)
1375 const struct cred *old_cred;
1381 LASSERT(ctxt->loc_logops);
1383 if (ctxt->loc_logops->lop_open == NULL) {
1385 RETURN(-EOPNOTSUPP);
1388 *lgh = llog_alloc_handle();
1391 (*lgh)->lgh_ctxt = ctxt;
1392 (*lgh)->lgh_logops = ctxt->loc_logops;
1394 old_cred = llog_raise_resource();
1395 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1396 llog_restore_resource(old_cred);
1398 llog_free_handle(*lgh);
1403 EXPORT_SYMBOL(llog_open);
1405 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1407 return llog_handle_put(env, loghandle);
1409 EXPORT_SYMBOL(llog_close);
1412 * Helper function to get the llog size in records. It is used by MGS
1413 * mostly to check that config llog exists and contains data.
1415 * \param[in] env execution environment
1416 * \param[in] ctxt llog context
1417 * \param[in] name llog name
1419 * \retval true if there are records in llog besides a header
1420 * \retval false on error or llog without records
1422 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1425 struct llog_handle *llh;
1428 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1430 if (likely(rc == -ENOENT))
1435 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1437 GOTO(out_close, rc);
1438 rc = llog_get_size(llh);
1441 llog_close(env, llh);
1443 /* The header is record 1, the llog is still considered as empty
1444 * if there is only header */
1447 EXPORT_SYMBOL(llog_is_empty);
1449 /* this callback run in raw read mode (canceled record are processed) */
1450 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1451 struct llog_rec_hdr *rec, void *data)
1453 struct llog_handle *copy_llh = data;
1454 int idx = rec->lrh_index;
1459 /* Append all records */
1460 rc = llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1462 /* Cancel the record if it is canceled on the source */
1463 if (!rc && !test_bit_le(idx, LLOG_HDR_BITMAP(llh->lgh_hdr)))
1464 rc = llog_cancel_rec(env, copy_llh, copy_llh->lgh_last_idx);
1469 /* backup plain llog */
1470 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1471 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1472 char *name, char *backup)
1474 struct llog_handle *llh, *bllh;
1475 struct llog_process_cat_data cd = {0};
1480 /* open original log */
1481 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1483 /* the -ENOENT case is also reported to the caller
1484 * but silently so it should handle that if needed.
1487 CERROR("%s: failed to open log %s: rc = %d\n",
1488 obd->obd_name, name, rc);
1492 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1494 GOTO(out_close, rc);
1496 /* Make sure there's no old backup log */
1497 rc = llog_erase(env, bctxt, NULL, backup);
1498 if (rc < 0 && rc != -ENOENT)
1499 GOTO(out_close, rc);
1501 /* open backup log */
1502 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1504 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1505 obd->obd_name, backup, rc);
1506 GOTO(out_close, rc);
1509 /* check that backup llog is not the same object as original one */
1510 if (llh->lgh_obj == bllh->lgh_obj) {
1511 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1512 obd->obd_name, name, backup, llh->lgh_obj,
1514 GOTO(out_backup, rc = -EEXIST);
1517 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1519 GOTO(out_backup, rc);
1521 /* Read canceled records to have an exact copy */
1522 cd.lpcd_read_mode = LLOG_READ_MODE_RAW;
1523 /* Copy log record by record */
1524 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1527 CERROR("%s: failed to backup log %s: rc = %d\n",
1528 obd->obd_name, name, rc);
1530 llog_close(env, bllh);
1532 llog_close(env, llh);
1535 EXPORT_SYMBOL(llog_backup);
1537 /* Get size of llog */
1538 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1543 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1545 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1546 loghandle2name(llh), PLOGID(&llh->lgh_id), rc);
1552 EXPORT_SYMBOL(llog_size);
1554 /* set llog ctime to current, and set LLOG_F_RM_ON_ERR|LLOG_F_MAX_AGE flag in
1555 * log header. It will be reclaimed when expired (UPDATE_LOG_MAX_AGE old).
1557 int llog_retain(const struct lu_env *env, struct llog_handle *log)
1559 struct dt_object *dto = log->lgh_obj;
1560 struct dt_device *dt = lu2dt_dev(log->lgh_obj->do_lu.lo_dev);
1561 struct lu_attr la = { 0 };
1565 la.la_ctime = ktime_get_real_seconds();
1566 la.la_valid = LA_CTIME;
1568 th = dt_trans_create(env, dt);
1572 th->th_wait_submit = 1;
1573 log->lgh_hdr->llh_flags |= LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR;
1574 rc = llog_declare_write_rec(env, log, &log->lgh_hdr->llh_hdr, -1, th);
1578 rc = dt_declare_attr_set(env, dto, &la, th);
1582 rc = dt_trans_start_local(env, dt, th);
1586 rc = llog_write_rec(env, log, &log->lgh_hdr->llh_hdr, NULL,
1587 LLOG_HEADER_IDX, th);
1591 rc = dt_attr_set(env, dto, &la, th);
1593 dt_trans_stop(env, dt, th);
1595 CDEBUG(D_OTHER, "retain log "DFID" rc = %d\n",
1596 PLOGID(&log->lgh_id), rc);
1599 EXPORT_SYMBOL(llog_retain);