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;
403 LASSERT(handle->lgh_hdr == NULL);
405 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
406 OBD_ALLOC_LARGE(llh, chunk_size);
410 handle->lgh_hdr = llh;
411 handle->lgh_hdr_size = chunk_size;
412 /* first assign flags to use llog_client_ops */
413 llh->llh_flags = flags;
414 rc = llog_read_header(env, handle, uuid);
416 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
417 flags & LLOG_F_IS_CAT) ||
418 (llh->llh_flags & LLOG_F_IS_CAT &&
419 flags & LLOG_F_IS_PLAIN))) {
420 CERROR("%s: llog type is %s but initializing %s\n",
421 loghandle2name(handle),
422 llh->llh_flags & LLOG_F_IS_CAT ?
424 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
425 GOTO(out, rc = -EINVAL);
426 } else if (llh->llh_flags &
427 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
429 * it is possible to open llog without specifying llog
430 * type so it is taken from llh_flags
432 flags = llh->llh_flags;
434 /* for some reason the llh_flags has no type set */
435 CERROR("llog type is not specified!\n");
436 GOTO(out, rc = -EINVAL);
439 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
440 CERROR("%s: llog uuid mismatch: %s/%s\n",
441 loghandle2name(handle),
443 (char *)llh->llh_tgtuuid.uuid);
444 GOTO(out, rc = -EEXIST);
447 if (flags & LLOG_F_IS_CAT) {
448 LASSERT(list_empty(&handle->u.chd.chd_head));
449 INIT_LIST_HEAD(&handle->u.chd.chd_head);
450 llh->llh_size = sizeof(struct llog_logid_rec);
451 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
452 } else if (!(flags & LLOG_F_IS_PLAIN)) {
453 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
454 loghandle2name(handle), flags, LLOG_F_IS_CAT,
458 llh->llh_flags |= fmt;
461 OBD_FREE_LARGE(llh, chunk_size);
462 handle->lgh_hdr = NULL;
466 EXPORT_SYMBOL(llog_init_handle);
468 #define LLOG_ERROR_REC(lgh, rec, format, a...) \
469 CERROR("%s: "DFID" rec type=%x idx=%u len=%u, " format "\n" , \
470 loghandle2name(lgh), PLOGID(&lgh->lgh_id), (rec)->lrh_type, \
471 (rec)->lrh_index, (rec)->lrh_len, ##a)
473 int llog_verify_record(const struct llog_handle *llh, struct llog_rec_hdr *rec)
475 int chunk_size = llh->lgh_hdr->llh_hdr.lrh_len;
477 if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC)
478 LLOG_ERROR_REC(llh, rec, "magic is bad");
479 else if (rec->lrh_len == 0 || rec->lrh_len > chunk_size)
480 LLOG_ERROR_REC(llh, rec, "bad record len, chunk size is %d",
482 else if (rec->lrh_index > llog_max_idx(llh->lgh_hdr))
483 LLOG_ERROR_REC(llh, rec, "index is too high");
489 EXPORT_SYMBOL(llog_verify_record);
491 static inline bool llog_is_index_skipable(int idx, struct llog_log_hdr *llh,
492 struct llog_process_cat_data *cd)
494 if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
497 return !test_bit_le(idx, LLOG_HDR_BITMAP(llh));
500 static int llog_process_thread(void *arg)
502 struct llog_process_info *lpi = arg;
503 struct llog_handle *loghandle = lpi->lpi_loghandle;
504 struct llog_log_hdr *llh = loghandle->lgh_hdr;
505 struct llog_process_cat_data *cd = lpi->lpi_catdata;
506 struct llog_thread_info *lti;
510 int rc = 0, index = 1, last_index;
512 int last_called_index = 0;
513 bool repeated = false;
520 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
522 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
523 /* expect chunk_size to be power of two */
524 LASSERT(is_power_of_2(chunk_size));
526 OBD_ALLOC_LARGE(buf, chunk_size);
528 lpi->lpi_rc = -ENOMEM;
532 last_index = llog_max_idx(llh);
534 if (cd->lpcd_first_idx >= llog_max_idx(llh))
535 /* End of the indexes -> Nothing to do */
538 index = cd->lpcd_first_idx + 1;
539 last_called_index = cd->lpcd_first_idx;
540 if (cd->lpcd_last_idx > 0 &&
541 cd->lpcd_last_idx <= llog_max_idx(llh))
542 last_index = cd->lpcd_last_idx;
543 else if (cd->lpcd_read_mode & LLOG_READ_MODE_RAW)
544 last_index = loghandle->lgh_last_idx;
548 struct llog_rec_hdr *rec;
549 off_t chunk_offset = 0;
550 unsigned int buf_offset = 0;
554 /* skip records not set in bitmap */
555 while (index <= last_index &&
556 llog_is_index_skipable(index, llh, cd))
559 /* There are no indices prior the last_index */
560 if (index > last_index)
563 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
567 /* get the buf with our target record; avoid old garbage */
568 memset(buf, 0, chunk_size);
569 /* the record index for outdated chunk data */
570 /* it is safe to process buffer until saved lgh_last_idx */
571 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
572 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
573 index, &cur_offset, buf, chunk_size);
575 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
576 " buf_offset %u, rc = %d\n", cur_offset,
577 (__u64)chunk_offset, buf_offset, rc);
580 /* we`ve tried to reread the chunk, but there is no
582 if (repeated && (chunk_offset + buf_offset) == cur_offset &&
583 (rc == -EBADR || rc == -EIO))
585 /* EOF while trying to skip to the next chunk */
586 if (!index && rc == -EBADR)
591 /* NB: after llog_next_block() call the cur_offset is the
592 * offset of the next block after read one.
593 * The absolute offset of the current chunk is calculated
594 * from cur_offset value and stored in chunk_offset variable.
596 if ((cur_offset & (chunk_size - 1)) != 0)
597 chunk_offset = cur_offset & ~(chunk_size - 1);
599 chunk_offset = cur_offset - chunk_size;
601 /* NB: when rec->lrh_len is accessed it is already swabbed
602 * since it is used at the "end" of the loop and the rec
603 * swabbing is done at the beginning of the loop. */
604 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
605 (char *)rec < buf + chunk_size;
606 rec = llog_rec_hdr_next(rec)) {
608 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
611 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
612 lustre_swab_llog_rec(rec);
614 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
615 rec->lrh_type, rec->lrh_index);
617 /* start with first rec if block was skipped */
620 "%s: skipping to the index %u\n",
621 loghandle2name(loghandle),
623 index = rec->lrh_index;
626 if (index == (synced_idx + 1) &&
627 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
630 if (CFS_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
631 cfs_fail_val == (unsigned int)
632 (loghandle->lgh_id.lgl_oi.oi.oi_id &
634 CFS_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
637 /* the bitmap could be changed during processing
638 * records from the chunk. For wrapped catalog
639 * it means we can read deleted record and try to
640 * process it. Check this case and reread the chunk.
641 * It is safe to process to lh_last_idx, including
642 * lh_last_idx if it was synced. We can not do <=
643 * comparison, cause for wrapped catalog lgh_last_idx
644 * could be less than index. So we detect last index
645 * for processing as index == lh_last_idx+1. But when
646 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
647 * the first processing index is llh_cat_idx+1.The
648 * exception is !(lgh_last_idx == llh_cat_idx &&
649 * index == llh_cat_idx + 1), and after simplification
651 * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
652 * This exception is working for catalog only.
653 * The last check is for the partial chunk boundary,
654 * if it is reached then try to re-read for possible
657 if ((index == lh_last_idx && synced_idx != index) ||
658 (index == (lh_last_idx + 1) &&
659 lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
660 (((char *)rec - buf >= cur_offset - chunk_offset) &&
662 /* save offset inside buffer for the re-read */
663 buf_offset = (char *)rec - (char *)buf;
664 cur_offset = chunk_offset;
666 /* We need to be sure lgh_last_idx
667 * record was saved to disk
669 down_read(&loghandle->lgh_last_sem);
670 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
671 up_read(&loghandle->lgh_last_sem);
672 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
677 rc = llog_verify_record(loghandle, rec);
679 CDEBUG(D_OTHER, "invalid record at index %d\n",
682 * for fixed-sized llogs we can skip one record
683 * by using llh_size from llog header.
684 * Otherwise skip the next llog chunk.
687 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
688 rec->lrh_len = llh->llh_size;
691 /* make sure that is always next block */
692 cur_offset = chunk_offset + chunk_size;
693 /* no goal to find, just next block to read */
698 if (rec->lrh_index < index) {
699 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
704 if (rec->lrh_index > index) {
705 /* the record itself looks good, but we met a
706 * gap which can be result of old bugs, just
708 LLOG_ERROR_REC(loghandle, rec,
709 "gap in index, expected %u",
711 index = rec->lrh_index;
715 "lrh_index: %d lrh_len: %d (%d remains)\n",
716 rec->lrh_index, rec->lrh_len,
717 (int)(buf + chunk_size - (char *)rec));
719 /* lgh_cur_offset is used only at llog_test_3 */
720 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
723 /* if needed, process the callback on this record */
724 if (!llog_is_index_skipable(index, llh, cd)) {
725 struct llog_cookie *lgc;
729 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
731 "index: %d, lh_last_idx: %d "
732 "synced_idx: %d lgh_last_idx: %d\n",
733 index, lh_last_idx, synced_idx,
734 loghandle->lgh_last_idx);
737 lgc = <i->lgi_cookie;
738 /* store lu_env for recursive calls */
739 tmp_off = lgc->lgc_offset;
740 tmp_idx = lgc->lgc_index;
742 lgc->lgc_offset = (char *)rec -
743 (char *)buf + chunk_offset;
744 lgc->lgc_index = rec->lrh_index;
746 /* using lu_env for passing record offset to
747 * llog_write through various callbacks */
748 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
750 last_called_index = index;
753 lgc->lgc_offset = tmp_off;
754 lgc->lgc_index = tmp_idx;
757 if (rc == LLOG_PROC_BREAK ||
758 rc == LLOG_SKIP_PLAIN) {
760 } else if (rc == LLOG_DEL_RECORD) {
761 rc = llog_cancel_rec(lpi->lpi_env,
764 /* Allow parallel cancelling, ENOENT
765 * means record was canceled at another
766 * processing thread or callback
773 /* some stupid callbacks directly cancel records
774 * and delete llog. Check it and stop
776 if (loghandle->lgh_hdr == NULL ||
777 loghandle->lgh_hdr->llh_count == 1)
781 /* exit if the last index is reached */
782 if (index >= last_index)
789 CDEBUG(D_HA, "stop processing %s "DFID" index %d count %d\n",
790 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
791 PLOGID(&loghandle->lgh_id), index, llh->llh_count);
794 cd->lpcd_last_idx = last_called_index;
796 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
797 if (dt_object_remote(loghandle->lgh_obj)) {
798 /* If it is remote object, then -EIO might means
799 * disconnection or eviction, let's return -EAGAIN,
800 * so for update recovery log processing, it will
801 * retry until the umount or abort recovery, see
802 * lod_sub_recovery_thread() */
803 CERROR("%s retry remote llog process\n",
804 loghandle2name(loghandle));
807 /* something bad happened to the processing of a local
808 * llog file, probably I/O error or the log got
809 * corrupted to be able to finally release the log we
810 * discard any remaining bits in the header */
811 CERROR("%s: local llog is corrupted "DFID" %s index %d count %d\n",
812 loghandle2name(loghandle),
813 PLOGID(&loghandle->lgh_id),
814 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
815 "plain"), index, llh->llh_count);
817 while (index <= last_index) {
818 if (test_bit_le(index,
819 LLOG_HDR_BITMAP(llh)) != 0)
820 llog_cancel_rec(lpi->lpi_env, loghandle,
828 OBD_FREE_LARGE(buf, chunk_size);
833 static int llog_process_thread_daemonize(void *arg)
835 struct llog_process_info *lpi = arg;
838 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
840 task_lock(lpi->lpi_reftask);
841 new_ns = lpi->lpi_reftask->nsproxy;
842 if (curr_ns != new_ns) {
845 current->nsproxy = new_ns;
846 /* XXX: we should call put_nsproxy() instead of
847 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
848 * used outside of the kernel itself, because it calls
849 * free_nsproxy() which is not exported by the kernel
850 * (defined in kernel/nsproxy.c) */
852 atomic_dec(&curr_ns->count);
854 task_unlock(lpi->lpi_reftask);
857 /* client env has no keys, tags is just 0 */
858 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
863 rc = llog_process_thread(arg);
867 complete(&lpi->lpi_completion);
871 int llog_process_or_fork(const struct lu_env *env,
872 struct llog_handle *loghandle,
873 llog_cb_t cb, void *data, void *catdata, bool fork)
875 struct llog_process_info *lpi;
876 struct llog_process_data *d = data;
877 struct llog_process_cat_data *cd = catdata;
878 __u32 flags = loghandle->lgh_hdr->llh_flags;
885 CERROR("cannot alloc pointer\n");
888 lpi->lpi_loghandle = loghandle;
890 lpi->lpi_cbdata = data;
891 lpi->lpi_catdata = catdata;
893 CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d read_mode %d\n",
894 PLOGID(&loghandle->lgh_id), flags,
895 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
896 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
897 cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1,
898 cd ? cd->lpcd_read_mode : -1);
900 struct task_struct *task;
902 /* The new thread can't use parent env,
903 * init the new one in llog_process_thread_daemonize. */
905 init_completion(&lpi->lpi_completion);
906 /* take reference to current, so that
907 * llog_process_thread_daemonize() can use it to switch to
908 * namespace associated with current */
909 lpi->lpi_reftask = current;
910 task = kthread_run(llog_process_thread_daemonize, lpi,
911 "llog_process_thread");
914 CERROR("%s: cannot start thread: rc = %d\n",
915 loghandle2name(loghandle), rc);
918 wait_for_completion(&lpi->lpi_completion);
921 llog_process_thread(lpi);
929 EXPORT_SYMBOL(llog_process_or_fork);
931 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
932 llog_cb_t cb, void *data, void *catdata)
935 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
936 return rc == LLOG_DEL_PLAIN ? 0 : rc;
938 EXPORT_SYMBOL(llog_process);
940 static inline const struct cred *llog_raise_resource(void)
942 struct cred *cred = NULL;
944 if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
947 cred = prepare_creds();
951 cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
952 return override_creds(cred);
955 static inline void llog_restore_resource(const struct cred *old_cred)
958 revert_creds(old_cred);
961 int llog_reverse_process(const struct lu_env *env,
962 struct llog_handle *loghandle, llog_cb_t cb,
963 void *data, void *catdata)
965 struct llog_log_hdr *llh = loghandle->lgh_hdr;
966 struct llog_process_cat_data *cd = catdata;
968 int rc = 0, first_index = 1, index, idx;
969 __u32 chunk_size = llh->llh_hdr.lrh_len;
972 OBD_ALLOC_LARGE(buf, chunk_size);
977 first_index = cd->lpcd_first_idx + 1;
978 if (cd != NULL && cd->lpcd_last_idx)
979 index = cd->lpcd_last_idx;
981 index = llog_max_idx(llh);
984 struct llog_rec_hdr *rec;
985 struct llog_rec_tail *tail;
987 /* skip records not set in bitmap */
988 while (index >= first_index &&
989 llog_is_index_skipable(index, llh, cd))
992 LASSERT(index >= first_index - 1);
993 if (index == first_index - 1)
996 /* get the buf with our target record; avoid old garbage */
997 memset(buf, 0, chunk_size);
998 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
1003 idx = rec->lrh_index;
1004 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
1005 while (idx < index) {
1006 rec = (void *)rec + rec->lrh_len;
1007 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1008 lustre_swab_llog_rec(rec);
1011 LASSERT(idx == index);
1012 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
1014 /* process records in buffer, starting where we found one */
1015 while ((void *)tail > buf) {
1016 if (tail->lrt_index == 0)
1017 GOTO(out, rc = 0); /* no more records */
1019 /* if needed, process the callback on this record */
1020 if (!llog_is_index_skipable(index, llh, cd)) {
1021 rec = (void *)tail - tail->lrt_len +
1024 rc = cb(env, loghandle, rec, data);
1025 if (rc == LLOG_PROC_BREAK ||
1026 rc == LLOG_SKIP_PLAIN) {
1028 } else if (rc == LLOG_DEL_RECORD) {
1029 rc = llog_cancel_rec(env, loghandle,
1036 /* previous record, still in buffer? */
1038 if (index < first_index)
1040 tail = (void *)tail - tail->lrt_len;
1046 OBD_FREE_LARGE(buf, chunk_size);
1049 EXPORT_SYMBOL(llog_reverse_process);
1055 * llog_open - open llog, may not exist
1056 * llog_exist - check if llog exists
1057 * llog_close - close opened llog, pair for open, frees llog_handle
1058 * llog_declare_create - declare llog creation
1059 * llog_create - create new llog on disk, need transaction handle
1060 * llog_declare_write_rec - declaration of llog write
1061 * llog_write_rec - write llog record on disk, need transaction handle
1062 * llog_declare_add - declare llog catalog record addition
1063 * llog_add - add llog record in catalog, need transaction handle
1065 int llog_exist(struct llog_handle *loghandle)
1067 const struct llog_operations *lop;
1072 rc = llog_handle2ops(loghandle, &lop);
1075 if (lop->lop_exist == NULL)
1076 RETURN(-EOPNOTSUPP);
1078 rc = lop->lop_exist(loghandle);
1081 EXPORT_SYMBOL(llog_exist);
1083 int llog_declare_create(const struct lu_env *env,
1084 struct llog_handle *loghandle, struct thandle *th)
1086 const struct cred *old_cred;
1087 const struct llog_operations *lop;
1092 rc = llog_handle2ops(loghandle, &lop);
1095 if (lop->lop_declare_create == NULL)
1096 RETURN(-EOPNOTSUPP);
1098 old_cred = llog_raise_resource();
1099 rc = lop->lop_declare_create(env, loghandle, th);
1100 llog_restore_resource(old_cred);
1104 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1107 const struct cred *old_cred;
1108 const struct llog_operations *lop;
1113 rc = llog_handle2ops(handle, &lop);
1116 if (lop->lop_create == NULL)
1117 RETURN(-EOPNOTSUPP);
1119 old_cred = llog_raise_resource();
1120 rc = lop->lop_create(env, handle, th);
1121 llog_restore_resource(old_cred);
1125 int llog_declare_write_rec(const struct lu_env *env,
1126 struct llog_handle *handle,
1127 struct llog_rec_hdr *rec, int idx,
1130 const struct cred *old_cred;
1131 const struct llog_operations *lop;
1136 rc = llog_handle2ops(handle, &lop);
1140 if (lop->lop_declare_write_rec == NULL)
1141 RETURN(-EOPNOTSUPP);
1143 old_cred = llog_raise_resource();
1144 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1145 llog_restore_resource(old_cred);
1149 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1150 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1151 int idx, struct thandle *th)
1153 const struct cred *old_cred;
1154 const struct llog_operations *lop;
1159 /* API sanity checks */
1160 if (handle == NULL) {
1161 CERROR("loghandle is missed\n");
1163 } else if (handle->lgh_obj == NULL) {
1164 CERROR("loghandle %p with NULL object\n",
1167 } else if (th == NULL) {
1168 CERROR("%s: missed transaction handle\n",
1169 loghandle2name(handle));
1171 } else if (handle->lgh_hdr == NULL) {
1172 CERROR("%s: loghandle %p with no header\n",
1173 loghandle2name(handle), handle);
1177 rc = llog_handle2ops(handle, &lop);
1181 if (lop->lop_write_rec == NULL)
1182 RETURN(-EOPNOTSUPP);
1184 buflen = rec->lrh_len;
1185 LASSERT(round_up(buflen, 8) == buflen);
1187 old_cred = llog_raise_resource();
1188 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1189 llog_restore_resource(old_cred);
1193 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1194 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1197 const struct cred *old_cred;
1202 if (lgh->lgh_logops->lop_add == NULL)
1203 RETURN(-EOPNOTSUPP);
1205 old_cred = llog_raise_resource();
1206 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1207 llog_restore_resource(old_cred);
1210 EXPORT_SYMBOL(llog_add);
1212 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1213 struct llog_rec_hdr *rec, struct thandle *th)
1215 const struct cred *old_cred;
1220 if (lgh->lgh_logops->lop_declare_add == NULL)
1221 RETURN(-EOPNOTSUPP);
1223 old_cred = llog_raise_resource();
1224 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1225 llog_restore_resource(old_cred);
1228 EXPORT_SYMBOL(llog_declare_add);
1231 * Helper function to open llog or create it if doesn't exist.
1232 * It hides all transaction handling from caller.
1234 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1235 struct llog_handle **res, struct llog_logid *logid,
1238 struct dt_device *d;
1244 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1248 if (llog_exist(*res))
1251 LASSERT((*res)->lgh_obj != NULL);
1253 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1255 if (unlikely(unlikely(d->dd_rdonly)))
1258 th = dt_trans_create(env, d);
1260 GOTO(out, rc = PTR_ERR(th));
1262 /* Create update llog object synchronously, which
1263 * happens during inialization process see
1264 * lod_sub_prep_llog(), to make sure the update
1265 * llog object is created before corss-MDT writing
1266 * updates into the llog object */
1267 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1270 th->th_wait_submit = 1;
1271 rc = llog_declare_create(env, *res, th);
1273 rc = dt_trans_start_local(env, d, th);
1275 rc = llog_create(env, *res, th);
1277 dt_trans_stop(env, d, th);
1280 llog_close(env, *res);
1283 EXPORT_SYMBOL(llog_open_create);
1286 * Helper function to delete existent llog.
1288 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1289 struct llog_logid *logid, char *name)
1291 struct llog_handle *handle;
1296 /* nothing to erase */
1297 if (name == NULL && logid == NULL)
1300 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1304 rc = llog_destroy(env, handle);
1305 llog_close(env, handle);
1309 EXPORT_SYMBOL(llog_erase);
1312 * Helper function for write record in llog.
1313 * It hides all transaction handling from caller.
1314 * Valid only with local llog.
1316 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1317 struct llog_rec_hdr *rec, int idx)
1319 struct dt_device *dt;
1327 LASSERT(loghandle->lgh_ctxt);
1328 LASSERT(loghandle->lgh_obj != NULL);
1330 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1332 if (unlikely(unlikely(dt->dd_rdonly)))
1335 th = dt_trans_create(env, dt);
1337 RETURN(PTR_ERR(th));
1339 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1341 GOTO(out_trans, rc);
1343 th->th_wait_submit = 1;
1344 rc = dt_trans_start_local(env, dt, th);
1346 GOTO(out_trans, rc);
1348 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1350 down_write(&loghandle->lgh_lock);
1352 struct llog_thread_info *lti = llog_info(env);
1354 /* cookie comes from llog_process_thread */
1355 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1356 rec->lrh_index, th);
1357 /* upper layer didn`t pass cookie so change rc */
1358 rc = (rc == 1 ? 0 : rc);
1360 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1363 up_write(&loghandle->lgh_lock);
1365 dt_trans_stop(env, dt, th);
1368 EXPORT_SYMBOL(llog_write);
1370 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1371 struct llog_handle **lgh, struct llog_logid *logid,
1372 char *name, enum llog_open_param open_param)
1374 const struct cred *old_cred;
1380 LASSERT(ctxt->loc_logops);
1382 if (ctxt->loc_logops->lop_open == NULL) {
1384 RETURN(-EOPNOTSUPP);
1387 *lgh = llog_alloc_handle();
1390 (*lgh)->lgh_ctxt = ctxt;
1391 (*lgh)->lgh_logops = ctxt->loc_logops;
1393 old_cred = llog_raise_resource();
1394 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1395 llog_restore_resource(old_cred);
1397 llog_free_handle(*lgh);
1402 EXPORT_SYMBOL(llog_open);
1404 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1406 return llog_handle_put(env, loghandle);
1408 EXPORT_SYMBOL(llog_close);
1411 * Helper function to get the llog size in records. It is used by MGS
1412 * mostly to check that config llog exists and contains data.
1414 * \param[in] env execution environment
1415 * \param[in] ctxt llog context
1416 * \param[in] name llog name
1418 * \retval true if there are records in llog besides a header
1419 * \retval false on error or llog without records
1421 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1424 struct llog_handle *llh;
1427 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1429 if (likely(rc == -ENOENT))
1434 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1436 GOTO(out_close, rc);
1437 rc = llog_get_size(llh);
1440 llog_close(env, llh);
1442 /* The header is record 1, the llog is still considered as empty
1443 * if there is only header */
1446 EXPORT_SYMBOL(llog_is_empty);
1448 /* this callback run in raw read mode (canceled record are processed) */
1449 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1450 struct llog_rec_hdr *rec, void *data)
1452 struct llog_handle *copy_llh = data;
1453 int idx = rec->lrh_index;
1458 /* Append all records */
1459 rc = llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1461 /* Cancel the record if it is canceled on the source */
1462 if (!rc && !test_bit_le(idx, LLOG_HDR_BITMAP(llh->lgh_hdr)))
1463 rc = llog_cancel_rec(env, copy_llh, copy_llh->lgh_last_idx);
1468 /* backup plain llog */
1469 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1470 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1471 char *name, char *backup)
1473 struct llog_handle *llh, *bllh;
1474 struct llog_process_cat_data cd = {0};
1479 /* open original log */
1480 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1482 /* the -ENOENT case is also reported to the caller
1483 * but silently so it should handle that if needed.
1486 CERROR("%s: failed to open log %s: rc = %d\n",
1487 obd->obd_name, name, rc);
1491 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1493 GOTO(out_close, rc);
1495 /* Make sure there's no old backup log */
1496 rc = llog_erase(env, bctxt, NULL, backup);
1497 if (rc < 0 && rc != -ENOENT)
1498 GOTO(out_close, rc);
1500 /* open backup log */
1501 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1503 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1504 obd->obd_name, backup, rc);
1505 GOTO(out_close, rc);
1508 /* check that backup llog is not the same object as original one */
1509 if (llh->lgh_obj == bllh->lgh_obj) {
1510 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1511 obd->obd_name, name, backup, llh->lgh_obj,
1513 GOTO(out_backup, rc = -EEXIST);
1516 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1518 GOTO(out_backup, rc);
1520 /* Read canceled records to have an exact copy */
1521 cd.lpcd_read_mode = LLOG_READ_MODE_RAW;
1522 /* Copy log record by record */
1523 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1526 CERROR("%s: failed to backup log %s: rc = %d\n",
1527 obd->obd_name, name, rc);
1529 llog_close(env, bllh);
1531 llog_close(env, llh);
1534 EXPORT_SYMBOL(llog_backup);
1536 /* Get size of llog */
1537 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1542 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1544 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1545 loghandle2name(llh), PLOGID(&llh->lgh_id), rc);
1551 EXPORT_SYMBOL(llog_size);
1553 /* set llog ctime to current, and set LLOG_F_RM_ON_ERR|LLOG_F_MAX_AGE flag in
1554 * log header. It will be reclaimed when expired (UPDATE_LOG_MAX_AGE old).
1556 int llog_retain(const struct lu_env *env, struct llog_handle *log)
1558 struct dt_object *dto = log->lgh_obj;
1559 struct dt_device *dt = lu2dt_dev(log->lgh_obj->do_lu.lo_dev);
1560 struct lu_attr la = { 0 };
1564 la.la_ctime = ktime_get_real_seconds();
1565 la.la_valid = LA_CTIME;
1567 th = dt_trans_create(env, dt);
1571 th->th_wait_submit = 1;
1572 log->lgh_hdr->llh_flags |= LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR;
1573 rc = llog_declare_write_rec(env, log, &log->lgh_hdr->llh_hdr, -1, th);
1577 rc = dt_declare_attr_set(env, dto, &la, th);
1581 rc = dt_trans_start_local(env, dt, th);
1585 rc = llog_write_rec(env, log, &log->lgh_hdr->llh_hdr, NULL,
1586 LLOG_HEADER_IDX, th);
1590 rc = dt_attr_set(env, dto, &la, th);
1592 dt_trans_stop(env, dt, th);
1594 CDEBUG(D_OTHER, "retain log "DFID" rc = %d\n",
1595 PLOGID(&log->lgh_id), rc);
1598 EXPORT_SYMBOL(llog_retain);