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], PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
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 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), 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 int llog_verify_record(const struct llog_handle *llh, struct llog_rec_hdr *rec)
469 int chunk_size = llh->lgh_hdr->llh_hdr.lrh_len;
471 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
472 CERROR("%s: record is too large: %d > %d\n",
473 loghandle2name(llh), rec->lrh_len, chunk_size);
476 if (rec->lrh_index >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
477 CERROR("%s: index is too high: %d\n",
478 loghandle2name(llh), rec->lrh_index);
481 if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC) {
482 CERROR("%s: magic %x is bad\n",
483 loghandle2name(llh), rec->lrh_type);
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;
514 bool refresh_idx = 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;
534 last_called_index = cd->lpcd_first_idx;
535 index = cd->lpcd_first_idx + 1;
537 if (cd && cd->lpcd_last_idx)
538 last_index = cd->lpcd_last_idx;
539 else if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
540 last_index = loghandle->lgh_last_idx;
542 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
545 struct llog_rec_hdr *rec;
546 off_t chunk_offset = 0;
547 unsigned int buf_offset = 0;
551 /* skip records not set in bitmap */
552 while (index <= last_index &&
553 llog_is_index_skipable(index, llh, cd))
556 /* There are no indices prior the last_index */
557 if (index > last_index)
560 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
564 /* get the buf with our target record; avoid old garbage */
565 memset(buf, 0, chunk_size);
566 /* the record index for outdated chunk data */
567 /* it is safe to process buffer until saved lgh_last_idx */
568 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
569 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
570 index, &cur_offset, buf, chunk_size);
572 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
573 " buf_offset %u, rc = %d\n", cur_offset,
574 (__u64)chunk_offset, buf_offset, rc);
577 /* we`ve tried to reread the chunk, but there is no
579 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
585 /* NB: after llog_next_block() call the cur_offset is the
586 * offset of the next block after read one.
587 * The absolute offset of the current chunk is calculated
588 * from cur_offset value and stored in chunk_offset variable.
590 if ((cur_offset & (chunk_size - 1)) != 0)
591 chunk_offset = cur_offset & ~(chunk_size - 1);
593 chunk_offset = cur_offset - chunk_size;
595 /* NB: when rec->lrh_len is accessed it is already swabbed
596 * since it is used at the "end" of the loop and the rec
597 * swabbing is done at the beginning of the loop. */
598 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
599 (char *)rec < buf + chunk_size;
600 rec = llog_rec_hdr_next(rec)) {
602 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
605 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
606 lustre_swab_llog_rec(rec);
608 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
609 rec->lrh_type, rec->lrh_index);
611 if (index == (synced_idx + 1) &&
612 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
615 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
616 cfs_fail_val == (unsigned int)
617 (loghandle->lgh_id.lgl_oi.oi.oi_id &
619 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
622 /* the bitmap could be changed during processing
623 * records from the chunk. For wrapped catalog
624 * it means we can read deleted record and try to
625 * process it. Check this case and reread the chunk.
626 * It is safe to process to lh_last_idx, including
627 * lh_last_idx if it was synced. We can not do <=
628 * comparison, cause for wrapped catalog lgh_last_idx
629 * could be less than index. So we detect last index
630 * for processing as index == lh_last_idx+1. But when
631 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
632 * the first processing index is llh_cat_idx+1.The
633 * exception is !(lgh_last_idx == llh_cat_idx &&
634 * index == llh_cat_idx + 1), and after simplification
636 * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
637 * This exception is working for catalog only.
640 if ((index == lh_last_idx && synced_idx != index) ||
641 (index == (lh_last_idx + 1) &&
642 lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
643 (rec->lrh_index == 0 && !repeated)) {
645 /* save offset inside buffer for the re-read */
646 buf_offset = (char *)rec - (char *)buf;
647 cur_offset = chunk_offset;
649 /* We need to be sure lgh_last_idx
650 * record was saved to disk
652 down_read(&loghandle->lgh_last_sem);
653 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
654 up_read(&loghandle->lgh_last_sem);
655 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
662 rc = llog_verify_record(loghandle, rec);
664 CERROR("%s: invalid record in llog "DFID
665 " record for index %d/%d: rc = %d\n",
666 loghandle2name(loghandle),
667 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
668 rec->lrh_index, index, rc);
670 * the block seem to be corrupted, let's try
671 * with the next one. reset rc to go to the
676 GOTO(repeat, rc = 0);
679 if (rec->lrh_index < index) {
680 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
685 if (rec->lrh_index > index) {
686 /* the record itself looks good, but we met a
687 * gap which can be result of old bugs, just
689 CERROR("%s: "DFID" index %u, expected %u\n",
690 loghandle2name(loghandle),
691 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
692 rec->lrh_index, index);
693 index = rec->lrh_index;
697 "lrh_index: %d lrh_len: %d (%d remains)\n",
698 rec->lrh_index, rec->lrh_len,
699 (int)(buf + chunk_size - (char *)rec));
701 /* lgh_cur_offset is used only at llog_test_3 */
702 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
705 /* if needed, process the callback on this record */
706 if (!llog_is_index_skipable(index, llh, cd)) {
707 struct llog_cookie *lgc;
711 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
713 "index: %d, lh_last_idx: %d "
714 "synced_idx: %d lgh_last_idx: %d\n",
715 index, lh_last_idx, synced_idx,
716 loghandle->lgh_last_idx);
719 lgc = <i->lgi_cookie;
720 /* store lu_env for recursive calls */
721 tmp_off = lgc->lgc_offset;
722 tmp_idx = lgc->lgc_index;
724 lgc->lgc_offset = (char *)rec -
725 (char *)buf + chunk_offset;
726 lgc->lgc_index = rec->lrh_index;
728 /* using lu_env for passing record offset to
729 * llog_write through various callbacks */
730 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
732 last_called_index = index;
735 lgc->lgc_offset = tmp_off;
736 lgc->lgc_index = tmp_idx;
739 if (rc == LLOG_PROC_BREAK ||
740 rc == LLOG_SKIP_PLAIN) {
742 } else if (rc == LLOG_DEL_RECORD) {
743 rc = llog_cancel_rec(lpi->lpi_env,
746 /* Allow parallel cancelling, ENOENT
747 * means record was canceled at another
748 * processing thread or callback
755 /* some stupid callbacks directly cancel records
756 * and delete llog. Check it and stop
758 if (loghandle->lgh_hdr == NULL ||
759 loghandle->lgh_hdr->llh_count == 1)
762 /* exit if the last index is reached */
763 if (index >= last_index)
770 CDEBUG(D_HA, "stop processing %s "DOSTID":%x index %d count %d\n",
771 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
772 POSTID(&loghandle->lgh_id.lgl_oi), loghandle->lgh_id.lgl_ogen,
773 index, llh->llh_count);
776 cd->lpcd_last_idx = last_called_index;
778 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
779 if (dt_object_remote(loghandle->lgh_obj)) {
780 /* If it is remote object, then -EIO might means
781 * disconnection or eviction, let's return -EAGAIN,
782 * so for update recovery log processing, it will
783 * retry until the umount or abort recovery, see
784 * lod_sub_recovery_thread() */
785 CERROR("%s retry remote llog process\n",
786 loghandle2name(loghandle));
789 /* something bad happened to the processing of a local
790 * llog file, probably I/O error or the log got
791 * corrupted to be able to finally release the log we
792 * discard any remaining bits in the header */
793 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
794 " %s index %d count %d\n",
795 loghandle2name(loghandle),
796 POSTID(&loghandle->lgh_id.lgl_oi),
797 loghandle->lgh_id.lgl_ogen,
798 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
799 "plain"), index, llh->llh_count);
801 while (index <= last_index) {
802 if (test_bit_le(index,
803 LLOG_HDR_BITMAP(llh)) != 0)
804 llog_cancel_rec(lpi->lpi_env, loghandle,
812 OBD_FREE_LARGE(buf, chunk_size);
817 static int llog_process_thread_daemonize(void *arg)
819 struct llog_process_info *lpi = arg;
822 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
824 task_lock(lpi->lpi_reftask);
825 new_ns = lpi->lpi_reftask->nsproxy;
826 if (curr_ns != new_ns) {
829 current->nsproxy = new_ns;
830 /* XXX: we should call put_nsproxy() instead of
831 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
832 * used outside of the kernel itself, because it calls
833 * free_nsproxy() which is not exported by the kernel
834 * (defined in kernel/nsproxy.c) */
836 atomic_dec(&curr_ns->count);
838 task_unlock(lpi->lpi_reftask);
841 /* client env has no keys, tags is just 0 */
842 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
847 rc = llog_process_thread(arg);
851 complete(&lpi->lpi_completion);
855 int llog_process_or_fork(const struct lu_env *env,
856 struct llog_handle *loghandle,
857 llog_cb_t cb, void *data, void *catdata, bool fork)
859 struct llog_process_info *lpi;
860 struct llog_process_data *d = data;
861 struct llog_process_cat_data *cd = catdata;
862 __u32 flags = loghandle->lgh_hdr->llh_flags;
869 CERROR("cannot alloc pointer\n");
872 lpi->lpi_loghandle = loghandle;
874 lpi->lpi_cbdata = data;
875 lpi->lpi_catdata = catdata;
877 CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d read_mode %d\n",
878 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), flags,
879 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
880 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
881 cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1,
882 cd ? cd->lpcd_read_mode : -1);
884 struct task_struct *task;
886 /* The new thread can't use parent env,
887 * init the new one in llog_process_thread_daemonize. */
889 init_completion(&lpi->lpi_completion);
890 /* take reference to current, so that
891 * llog_process_thread_daemonize() can use it to switch to
892 * namespace associated with current */
893 lpi->lpi_reftask = current;
894 task = kthread_run(llog_process_thread_daemonize, lpi,
895 "llog_process_thread");
898 CERROR("%s: cannot start thread: rc = %d\n",
899 loghandle2name(loghandle), rc);
902 wait_for_completion(&lpi->lpi_completion);
905 llog_process_thread(lpi);
913 EXPORT_SYMBOL(llog_process_or_fork);
915 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
916 llog_cb_t cb, void *data, void *catdata)
919 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
920 return rc == LLOG_DEL_PLAIN ? 0 : rc;
922 EXPORT_SYMBOL(llog_process);
924 static inline const struct cred *llog_raise_resource(void)
926 struct cred *cred = NULL;
928 if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
931 cred = prepare_creds();
935 cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
936 return override_creds(cred);
939 static inline void llog_restore_resource(const struct cred *old_cred)
942 revert_creds(old_cred);
945 int llog_reverse_process(const struct lu_env *env,
946 struct llog_handle *loghandle, llog_cb_t cb,
947 void *data, void *catdata)
949 struct llog_log_hdr *llh = loghandle->lgh_hdr;
950 struct llog_process_cat_data *cd = catdata;
952 int rc = 0, first_index = 1, index, idx;
953 __u32 chunk_size = llh->llh_hdr.lrh_len;
956 OBD_ALLOC_LARGE(buf, chunk_size);
961 first_index = cd->lpcd_first_idx + 1;
962 if (cd != NULL && cd->lpcd_last_idx)
963 index = cd->lpcd_last_idx;
965 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
968 struct llog_rec_hdr *rec;
969 struct llog_rec_tail *tail;
971 /* skip records not set in bitmap */
972 while (index >= first_index &&
973 llog_is_index_skipable(index, llh, cd))
976 LASSERT(index >= first_index - 1);
977 if (index == first_index - 1)
980 /* get the buf with our target record; avoid old garbage */
981 memset(buf, 0, chunk_size);
982 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
987 idx = rec->lrh_index;
988 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
989 while (idx < index) {
990 rec = (void *)rec + rec->lrh_len;
991 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
992 lustre_swab_llog_rec(rec);
995 LASSERT(idx == index);
996 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
998 /* process records in buffer, starting where we found one */
999 while ((void *)tail > buf) {
1000 if (tail->lrt_index == 0)
1001 GOTO(out, rc = 0); /* no more records */
1003 /* if needed, process the callback on this record */
1004 if (!llog_is_index_skipable(index, llh, cd)) {
1005 rec = (void *)tail - tail->lrt_len +
1008 rc = cb(env, loghandle, rec, data);
1009 if (rc == LLOG_PROC_BREAK ||
1010 rc == LLOG_SKIP_PLAIN) {
1012 } else if (rc == LLOG_DEL_RECORD) {
1013 rc = llog_cancel_rec(env, loghandle,
1020 /* previous record, still in buffer? */
1022 if (index < first_index)
1024 tail = (void *)tail - tail->lrt_len;
1030 OBD_FREE_LARGE(buf, chunk_size);
1033 EXPORT_SYMBOL(llog_reverse_process);
1039 * llog_open - open llog, may not exist
1040 * llog_exist - check if llog exists
1041 * llog_close - close opened llog, pair for open, frees llog_handle
1042 * llog_declare_create - declare llog creation
1043 * llog_create - create new llog on disk, need transaction handle
1044 * llog_declare_write_rec - declaration of llog write
1045 * llog_write_rec - write llog record on disk, need transaction handle
1046 * llog_declare_add - declare llog catalog record addition
1047 * llog_add - add llog record in catalog, need transaction handle
1049 int llog_exist(struct llog_handle *loghandle)
1051 const struct llog_operations *lop;
1056 rc = llog_handle2ops(loghandle, &lop);
1059 if (lop->lop_exist == NULL)
1060 RETURN(-EOPNOTSUPP);
1062 rc = lop->lop_exist(loghandle);
1065 EXPORT_SYMBOL(llog_exist);
1067 int llog_declare_create(const struct lu_env *env,
1068 struct llog_handle *loghandle, struct thandle *th)
1070 const struct cred *old_cred;
1071 const struct llog_operations *lop;
1076 rc = llog_handle2ops(loghandle, &lop);
1079 if (lop->lop_declare_create == NULL)
1080 RETURN(-EOPNOTSUPP);
1082 old_cred = llog_raise_resource();
1083 rc = lop->lop_declare_create(env, loghandle, th);
1084 llog_restore_resource(old_cred);
1088 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1091 const struct cred *old_cred;
1092 const struct llog_operations *lop;
1097 rc = llog_handle2ops(handle, &lop);
1100 if (lop->lop_create == NULL)
1101 RETURN(-EOPNOTSUPP);
1103 old_cred = llog_raise_resource();
1104 rc = lop->lop_create(env, handle, th);
1105 llog_restore_resource(old_cred);
1109 int llog_declare_write_rec(const struct lu_env *env,
1110 struct llog_handle *handle,
1111 struct llog_rec_hdr *rec, int idx,
1114 const struct cred *old_cred;
1115 const struct llog_operations *lop;
1120 rc = llog_handle2ops(handle, &lop);
1124 if (lop->lop_declare_write_rec == NULL)
1125 RETURN(-EOPNOTSUPP);
1127 old_cred = llog_raise_resource();
1128 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1129 llog_restore_resource(old_cred);
1133 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1134 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1135 int idx, struct thandle *th)
1137 const struct cred *old_cred;
1138 const struct llog_operations *lop;
1143 /* API sanity checks */
1144 if (handle == NULL) {
1145 CERROR("loghandle is missed\n");
1147 } else if (handle->lgh_obj == NULL) {
1148 CERROR("loghandle %p with NULL object\n",
1151 } else if (th == NULL) {
1152 CERROR("%s: missed transaction handle\n",
1153 loghandle2name(handle));
1155 } else if (handle->lgh_hdr == NULL) {
1156 CERROR("%s: loghandle %p with no header\n",
1157 loghandle2name(handle), handle);
1161 rc = llog_handle2ops(handle, &lop);
1165 if (lop->lop_write_rec == NULL)
1166 RETURN(-EOPNOTSUPP);
1168 buflen = rec->lrh_len;
1169 LASSERT(cfs_size_round(buflen) == buflen);
1171 old_cred = llog_raise_resource();
1172 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1173 llog_restore_resource(old_cred);
1177 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1178 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1181 const struct cred *old_cred;
1186 if (lgh->lgh_logops->lop_add == NULL)
1187 RETURN(-EOPNOTSUPP);
1189 old_cred = llog_raise_resource();
1190 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1191 llog_restore_resource(old_cred);
1194 EXPORT_SYMBOL(llog_add);
1196 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1197 struct llog_rec_hdr *rec, struct thandle *th)
1199 const struct cred *old_cred;
1204 if (lgh->lgh_logops->lop_declare_add == NULL)
1205 RETURN(-EOPNOTSUPP);
1207 old_cred = llog_raise_resource();
1208 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1209 llog_restore_resource(old_cred);
1212 EXPORT_SYMBOL(llog_declare_add);
1215 * Helper function to open llog or create it if doesn't exist.
1216 * It hides all transaction handling from caller.
1218 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1219 struct llog_handle **res, struct llog_logid *logid,
1222 struct dt_device *d;
1228 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1232 if (llog_exist(*res))
1235 LASSERT((*res)->lgh_obj != NULL);
1237 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1239 if (unlikely(unlikely(d->dd_rdonly)))
1242 th = dt_trans_create(env, d);
1244 GOTO(out, rc = PTR_ERR(th));
1246 /* Create update llog object synchronously, which
1247 * happens during inialization process see
1248 * lod_sub_prep_llog(), to make sure the update
1249 * llog object is created before corss-MDT writing
1250 * updates into the llog object */
1251 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1254 th->th_wait_submit = 1;
1255 rc = llog_declare_create(env, *res, th);
1257 rc = dt_trans_start_local(env, d, th);
1259 rc = llog_create(env, *res, th);
1261 dt_trans_stop(env, d, th);
1264 llog_close(env, *res);
1267 EXPORT_SYMBOL(llog_open_create);
1270 * Helper function to delete existent llog.
1272 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1273 struct llog_logid *logid, char *name)
1275 struct llog_handle *handle;
1280 /* nothing to erase */
1281 if (name == NULL && logid == NULL)
1284 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1288 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1290 rc = llog_destroy(env, handle);
1292 rc2 = llog_close(env, handle);
1297 EXPORT_SYMBOL(llog_erase);
1300 * Helper function for write record in llog.
1301 * It hides all transaction handling from caller.
1302 * Valid only with local llog.
1304 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1305 struct llog_rec_hdr *rec, int idx)
1307 struct dt_device *dt;
1315 LASSERT(loghandle->lgh_ctxt);
1316 LASSERT(loghandle->lgh_obj != NULL);
1318 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1320 if (unlikely(unlikely(dt->dd_rdonly)))
1323 th = dt_trans_create(env, dt);
1325 RETURN(PTR_ERR(th));
1327 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1329 GOTO(out_trans, rc);
1331 th->th_wait_submit = 1;
1332 rc = dt_trans_start_local(env, dt, th);
1334 GOTO(out_trans, rc);
1336 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1338 down_write(&loghandle->lgh_lock);
1340 struct llog_thread_info *lti = llog_info(env);
1342 /* cookie comes from llog_process_thread */
1343 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1344 rec->lrh_index, th);
1345 /* upper layer didn`t pass cookie so change rc */
1346 rc = (rc == 1 ? 0 : rc);
1348 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1351 up_write(&loghandle->lgh_lock);
1353 dt_trans_stop(env, dt, th);
1356 EXPORT_SYMBOL(llog_write);
1358 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1359 struct llog_handle **lgh, struct llog_logid *logid,
1360 char *name, enum llog_open_param open_param)
1362 const struct cred *old_cred;
1368 LASSERT(ctxt->loc_logops);
1370 if (ctxt->loc_logops->lop_open == NULL) {
1372 RETURN(-EOPNOTSUPP);
1375 *lgh = llog_alloc_handle();
1378 (*lgh)->lgh_ctxt = ctxt;
1379 (*lgh)->lgh_logops = ctxt->loc_logops;
1381 old_cred = llog_raise_resource();
1382 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1383 llog_restore_resource(old_cred);
1385 llog_free_handle(*lgh);
1390 EXPORT_SYMBOL(llog_open);
1392 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1394 return llog_handle_put(env, loghandle);
1396 EXPORT_SYMBOL(llog_close);
1399 * Helper function to get the llog size in records. It is used by MGS
1400 * mostly to check that config llog exists and contains data.
1402 * \param[in] env execution environment
1403 * \param[in] ctxt llog context
1404 * \param[in] name llog name
1406 * \retval true if there are records in llog besides a header
1407 * \retval false on error or llog without records
1409 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1412 struct llog_handle *llh;
1415 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1417 if (likely(rc == -ENOENT))
1422 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1424 GOTO(out_close, rc);
1425 rc = llog_get_size(llh);
1428 llog_close(env, llh);
1430 /* The header is record 1, the llog is still considered as empty
1431 * if there is only header */
1434 EXPORT_SYMBOL(llog_is_empty);
1436 /* this callback run in raw read mode (canceled record are processed) */
1437 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1438 struct llog_rec_hdr *rec, void *data)
1440 struct llog_handle *copy_llh = data;
1441 int idx = rec->lrh_index;
1446 /* Append all records */
1447 rc = llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1449 /* Cancel the record if it is canceled on the source */
1450 if (!rc && !test_bit_le(idx, LLOG_HDR_BITMAP(llh->lgh_hdr)))
1451 rc = llog_cancel_rec(env, copy_llh, copy_llh->lgh_last_idx);
1456 /* backup plain llog */
1457 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1458 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1459 char *name, char *backup)
1461 struct llog_handle *llh, *bllh;
1462 struct llog_process_cat_data cd = {0};
1467 /* open original log */
1468 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1470 /* the -ENOENT case is also reported to the caller
1471 * but silently so it should handle that if needed.
1474 CERROR("%s: failed to open log %s: rc = %d\n",
1475 obd->obd_name, name, rc);
1479 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1481 GOTO(out_close, rc);
1483 /* Make sure there's no old backup log */
1484 rc = llog_erase(env, bctxt, NULL, backup);
1485 if (rc < 0 && rc != -ENOENT)
1486 GOTO(out_close, rc);
1488 /* open backup log */
1489 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1491 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1492 obd->obd_name, backup, rc);
1493 GOTO(out_close, rc);
1496 /* check that backup llog is not the same object as original one */
1497 if (llh->lgh_obj == bllh->lgh_obj) {
1498 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1499 obd->obd_name, name, backup, llh->lgh_obj,
1501 GOTO(out_backup, rc = -EEXIST);
1504 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1506 GOTO(out_backup, rc);
1508 /* Read canceled records to have an exact copy */
1509 cd.lpcd_read_mode = LLOG_READ_MODE_RAW;
1510 /* Copy log record by record */
1511 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1514 CERROR("%s: failed to backup log %s: rc = %d\n",
1515 obd->obd_name, name, rc);
1517 llog_close(env, bllh);
1519 llog_close(env, llh);
1522 EXPORT_SYMBOL(llog_backup);
1524 /* Get size of llog */
1525 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1530 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1532 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1533 loghandle2name(llh), PFID(&llh->lgh_id.lgl_oi.oi_fid),
1540 EXPORT_SYMBOL(llog_size);