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/pid_namespace.h>
46 #include <linux/kthread.h>
47 #include <llog_swab.h>
48 #include <lustre_log.h>
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include "llog_internal.h"
54 * Allocate a new log or catalog handle
55 * Used inside llog_open().
57 static struct llog_handle *llog_alloc_handle(void)
59 struct llog_handle *loghandle;
61 OBD_ALLOC_PTR(loghandle);
62 if (loghandle == NULL)
65 init_rwsem(&loghandle->lgh_lock);
66 mutex_init(&loghandle->lgh_hdr_mutex);
67 init_rwsem(&loghandle->lgh_last_sem);
68 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
69 refcount_set(&loghandle->lgh_refcount, 1);
75 * Free llog handle and header data if exists. Used in llog_close() only
77 static void llog_free_handle(struct llog_handle *loghandle)
79 LASSERT(loghandle != NULL);
81 /* failed llog_init_handle */
82 if (loghandle->lgh_hdr == NULL)
85 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
86 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
87 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
88 LASSERT(list_empty(&loghandle->u.chd.chd_head));
89 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
91 OBD_FREE_PTR(loghandle);
94 struct llog_handle *llog_handle_get(struct llog_handle *loghandle)
96 if (refcount_inc_not_zero(&loghandle->lgh_refcount))
101 int llog_handle_put(const struct lu_env *env, struct llog_handle *loghandle)
105 if (refcount_dec_and_test(&loghandle->lgh_refcount)) {
106 const struct llog_operations *lop;
108 rc = llog_handle2ops(loghandle, &lop);
111 rc = lop->lop_close(env, loghandle);
115 llog_free_handle(loghandle);
120 static int llog_declare_destroy(const struct lu_env *env,
121 struct llog_handle *handle,
124 const struct llog_operations *lop;
129 rc = llog_handle2ops(handle, &lop);
132 if (lop->lop_declare_destroy == NULL)
135 rc = lop->lop_declare_destroy(env, handle, th);
140 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
143 const struct llog_operations *lop;
147 rc = llog_handle2ops(handle, &lop);
150 if (lop->lop_destroy == NULL)
153 LASSERT(handle->lgh_obj != NULL);
154 if (!llog_exist(handle))
157 rc = lop->lop_destroy(env, handle, th);
162 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
164 const struct llog_operations *lop;
165 struct dt_device *dt;
171 rc = llog_handle2ops(handle, &lop);
174 if (lop->lop_destroy == NULL)
177 if (handle->lgh_obj == NULL) {
178 /* if lgh_obj == NULL, then it is from client side destroy */
179 rc = lop->lop_destroy(env, handle, NULL);
183 if (!llog_exist(handle))
186 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
188 if (unlikely(unlikely(dt->dd_rdonly)))
191 th = dt_trans_create(env, dt);
195 rc = llog_declare_destroy(env, handle, th);
199 rc = dt_trans_start_local(env, dt, th);
203 rc = lop->lop_destroy(env, handle, th);
206 dt_trans_stop(env, dt, th);
210 EXPORT_SYMBOL(llog_destroy);
212 /* returns negative on error; 0 if success; 1 if success & log destroyed */
213 int llog_cancel_arr_rec(const struct lu_env *env, struct llog_handle *loghandle,
216 struct llog_thread_info *lgi = llog_info(env);
217 struct dt_device *dt;
218 struct llog_log_hdr *llh;
223 bool subtract_count = false;
227 LASSERT(loghandle != NULL);
228 LASSERT(loghandle->lgh_ctxt != NULL);
229 LASSERT(loghandle->lgh_obj != NULL);
231 llh = loghandle->lgh_hdr;
233 CDEBUG(D_RPCTRACE, "Canceling %d records, first %d in log "DFID"\n",
234 num, index[0], PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
236 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
238 if (unlikely(unlikely(dt->dd_rdonly)))
241 th = dt_trans_create(env, dt);
245 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, 0, th);
249 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
250 rc = llog_declare_destroy(env, loghandle, th);
255 th->th_wait_submit = 1;
256 rc = dt_trans_start_local(env, dt, th);
260 down_write(&loghandle->lgh_lock);
262 mutex_lock(&loghandle->lgh_hdr_mutex);
263 for (i = 0; i < num; ++i) {
265 CERROR("Can't cancel index 0 which is header\n");
266 GOTO(out_unlock, rc = -EINVAL);
268 if (!__test_and_clear_bit_le(index[i], LLOG_HDR_BITMAP(llh))) {
269 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n",
271 GOTO(out_unlock, rc = -ENOENT);
274 loghandle->lgh_hdr->llh_count -= num;
275 subtract_count = true;
277 /* Since llog_process_thread use lgi_cookie, it`s better to save them
278 * and restore after using
280 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
281 /* Pass this index to llog_osd_write_rec(), which will use the index
282 * to only update the necesary bitmap. */
283 lgi->lgi_cookie.lgc_index = index[0];
285 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, (num != 1 ? NULL :
286 &lgi->lgi_cookie), LLOG_HEADER_IDX, th);
287 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
290 GOTO(out_unlock, rc);
292 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
293 (llh->llh_count == 1) &&
294 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
295 (loghandle->u.phd.phd_cat_handle != NULL &&
296 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
298 /* never try to destroy it again */
299 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
300 rc = llog_trans_destroy(env, loghandle, th);
302 /* Sigh, can not destroy the final plain llog, but
303 * the bitmap has been clearly, so the record can not
304 * be accessed anymore, let's return 0 for now, and
305 * the orphan will be handled by LFSCK. */
306 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
307 loghandle2name(loghandle),
308 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
309 GOTO(out_unlock, rc = 0);
316 /* restore bitmap while holding a mutex */
317 if (subtract_count) {
318 loghandle->lgh_hdr->llh_count += num;
319 subtract_count = false;
321 for (i = i - 1; i >= 0; i--)
322 set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
324 mutex_unlock(&loghandle->lgh_hdr_mutex);
325 up_write(&loghandle->lgh_lock);
327 rc1 = dt_trans_stop(env, dt, th);
331 mutex_lock(&loghandle->lgh_hdr_mutex);
333 loghandle->lgh_hdr->llh_count += num;
334 for (i = i - 1; i >= 0; i--)
335 set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
336 mutex_unlock(&loghandle->lgh_hdr_mutex);
341 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
344 return llog_cancel_arr_rec(env, loghandle, 1, &index);
347 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
348 const struct obd_uuid *uuid)
350 const struct llog_operations *lop;
354 rc = llog_handle2ops(handle, &lop);
358 if (lop->lop_read_header == NULL)
361 rc = lop->lop_read_header(env, handle);
362 if (rc == LLOG_EEMPTY) {
363 struct llog_log_hdr *llh = handle->lgh_hdr;
365 /* lrh_len should be initialized in llog_init_handle */
366 handle->lgh_last_idx = 0; /* header is record with index 0 */
367 llh->llh_count = 1; /* for the header record */
368 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
369 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
370 LLOG_MIN_CHUNK_SIZE);
371 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
372 llh->llh_hdr.lrh_index = 0;
373 llh->llh_timestamp = ktime_get_real_seconds();
375 memcpy(&llh->llh_tgtuuid, uuid,
376 sizeof(llh->llh_tgtuuid));
377 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
378 /* Since update llog header might also call this function,
379 * let's reset the bitmap to 0 here */
380 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
381 llh->llh_bitmap_offset -
382 sizeof(llh->llh_tail));
383 set_bit_le(0, LLOG_HDR_BITMAP(llh));
384 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
385 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
390 EXPORT_SYMBOL(llog_read_header);
392 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
393 int flags, struct obd_uuid *uuid)
395 struct llog_log_hdr *llh;
396 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
398 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
401 LASSERT(handle->lgh_hdr == NULL);
403 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
404 OBD_ALLOC_LARGE(llh, chunk_size);
408 handle->lgh_hdr = llh;
409 handle->lgh_hdr_size = chunk_size;
410 /* first assign flags to use llog_client_ops */
411 llh->llh_flags = flags;
412 rc = llog_read_header(env, handle, uuid);
414 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
415 flags & LLOG_F_IS_CAT) ||
416 (llh->llh_flags & LLOG_F_IS_CAT &&
417 flags & LLOG_F_IS_PLAIN))) {
418 CERROR("%s: llog type is %s but initializing %s\n",
419 loghandle2name(handle),
420 llh->llh_flags & LLOG_F_IS_CAT ?
422 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
423 GOTO(out, rc = -EINVAL);
424 } else if (llh->llh_flags &
425 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
427 * it is possible to open llog without specifying llog
428 * type so it is taken from llh_flags
430 flags = llh->llh_flags;
432 /* for some reason the llh_flags has no type set */
433 CERROR("llog type is not specified!\n");
434 GOTO(out, rc = -EINVAL);
437 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
438 CERROR("%s: llog uuid mismatch: %s/%s\n",
439 loghandle2name(handle),
441 (char *)llh->llh_tgtuuid.uuid);
442 GOTO(out, rc = -EEXIST);
445 if (flags & LLOG_F_IS_CAT) {
446 LASSERT(list_empty(&handle->u.chd.chd_head));
447 INIT_LIST_HEAD(&handle->u.chd.chd_head);
448 llh->llh_size = sizeof(struct llog_logid_rec);
449 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
450 } else if (!(flags & LLOG_F_IS_PLAIN)) {
451 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
452 loghandle2name(handle), flags, LLOG_F_IS_CAT,
456 llh->llh_flags |= fmt;
459 OBD_FREE_LARGE(llh, chunk_size);
460 handle->lgh_hdr = NULL;
464 EXPORT_SYMBOL(llog_init_handle);
466 int llog_verify_record(const struct llog_handle *llh, struct llog_rec_hdr *rec)
468 int chunk_size = llh->lgh_hdr->llh_hdr.lrh_len;
470 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
471 CERROR("%s: record is too large: %d > %d\n",
472 loghandle2name(llh), rec->lrh_len, chunk_size);
475 if (rec->lrh_index >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
476 CERROR("%s: index is too high: %d\n",
477 loghandle2name(llh), rec->lrh_index);
480 if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC) {
481 CERROR("%s: magic %x is bad\n",
482 loghandle2name(llh), rec->lrh_type);
488 EXPORT_SYMBOL(llog_verify_record);
490 static inline bool llog_is_index_skipable(int idx, struct llog_log_hdr *llh,
491 struct llog_process_cat_data *cd)
493 if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
496 return !test_bit_le(idx, LLOG_HDR_BITMAP(llh));
499 static int llog_process_thread(void *arg)
501 struct llog_process_info *lpi = arg;
502 struct llog_handle *loghandle = lpi->lpi_loghandle;
503 struct llog_log_hdr *llh = loghandle->lgh_hdr;
504 struct llog_process_cat_data *cd = lpi->lpi_catdata;
505 struct llog_thread_info *lti;
509 int rc = 0, index = 1, last_index;
511 int last_called_index = 0;
512 bool repeated = false;
513 bool refresh_idx = 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;
533 last_called_index = cd->lpcd_first_idx;
534 index = cd->lpcd_first_idx + 1;
536 if (cd && cd->lpcd_last_idx)
537 last_index = cd->lpcd_last_idx;
538 else if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
539 last_index = loghandle->lgh_last_idx;
541 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
544 struct llog_rec_hdr *rec;
545 off_t chunk_offset = 0;
546 unsigned int buf_offset = 0;
550 /* skip records not set in bitmap */
551 while (index <= last_index &&
552 llog_is_index_skipable(index, llh, cd))
555 /* There are no indices prior the last_index */
556 if (index > last_index)
559 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
563 /* get the buf with our target record; avoid old garbage */
564 memset(buf, 0, chunk_size);
565 /* the record index for outdated chunk data */
566 /* it is safe to process buffer until saved lgh_last_idx */
567 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
568 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
569 index, &cur_offset, buf, chunk_size);
571 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
572 " buf_offset %u, rc = %d\n", cur_offset,
573 (__u64)chunk_offset, buf_offset, rc);
576 /* we`ve tried to reread the chunk, but there is no
578 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
584 /* NB: after llog_next_block() call the cur_offset is the
585 * offset of the next block after read one.
586 * The absolute offset of the current chunk is calculated
587 * from cur_offset value and stored in chunk_offset variable.
589 if ((cur_offset & (chunk_size - 1)) != 0)
590 chunk_offset = cur_offset & ~(chunk_size - 1);
592 chunk_offset = cur_offset - chunk_size;
594 /* NB: when rec->lrh_len is accessed it is already swabbed
595 * since it is used at the "end" of the loop and the rec
596 * swabbing is done at the beginning of the loop. */
597 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
598 (char *)rec < buf + chunk_size;
599 rec = llog_rec_hdr_next(rec)) {
601 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
604 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
605 lustre_swab_llog_rec(rec);
607 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
608 rec->lrh_type, rec->lrh_index);
610 if (index == (synced_idx + 1) &&
611 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
614 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
615 cfs_fail_val == (unsigned int)
616 (loghandle->lgh_id.lgl_oi.oi.oi_id &
618 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
621 /* the bitmap could be changed during processing
622 * records from the chunk. For wrapped catalog
623 * it means we can read deleted record and try to
624 * process it. Check this case and reread the chunk.
625 * It is safe to process to lh_last_idx, including
626 * lh_last_idx if it was synced. We can not do <=
627 * comparison, cause for wrapped catalog lgh_last_idx
628 * could be less than index. So we detect last index
629 * for processing as index == lh_last_idx+1. But when
630 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
631 * the first processing index is llh_cat_idx+1.The
632 * exception is !(lgh_last_idx == llh_cat_idx &&
633 * index == llh_cat_idx + 1), and after simplification
635 * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
636 * This exception is working for catalog only.
639 if ((index == lh_last_idx && synced_idx != index) ||
640 (index == (lh_last_idx + 1) &&
641 lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
642 (rec->lrh_index == 0 && !repeated)) {
644 /* save offset inside buffer for the re-read */
645 buf_offset = (char *)rec - (char *)buf;
646 cur_offset = chunk_offset;
648 /* We need to be sure lgh_last_idx
649 * record was saved to disk
651 down_read(&loghandle->lgh_last_sem);
652 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
653 up_read(&loghandle->lgh_last_sem);
654 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
661 rc = llog_verify_record(loghandle, rec);
663 CERROR("%s: invalid record in llog "DFID
664 " record for index %d/%d: rc = %d\n",
665 loghandle2name(loghandle),
666 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
667 rec->lrh_index, index, rc);
669 * the block seem to be corrupted, let's try
670 * with the next one. reset rc to go to the
675 GOTO(repeat, rc = 0);
678 if (rec->lrh_index < index) {
679 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
684 if (rec->lrh_index > index) {
685 /* the record itself looks good, but we met a
686 * gap which can be result of old bugs, just
688 CERROR("%s: "DFID" index %u, expected %u\n",
689 loghandle2name(loghandle),
690 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
691 rec->lrh_index, index);
692 index = rec->lrh_index;
696 "lrh_index: %d lrh_len: %d (%d remains)\n",
697 rec->lrh_index, rec->lrh_len,
698 (int)(buf + chunk_size - (char *)rec));
700 /* lgh_cur_offset is used only at llog_test_3 */
701 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
704 /* if needed, process the callback on this record */
705 if (!llog_is_index_skipable(index, llh, cd)) {
706 struct llog_cookie *lgc;
710 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
712 "index: %d, lh_last_idx: %d "
713 "synced_idx: %d lgh_last_idx: %d\n",
714 index, lh_last_idx, synced_idx,
715 loghandle->lgh_last_idx);
718 lgc = <i->lgi_cookie;
719 /* store lu_env for recursive calls */
720 tmp_off = lgc->lgc_offset;
721 tmp_idx = lgc->lgc_index;
723 lgc->lgc_offset = (char *)rec -
724 (char *)buf + chunk_offset;
725 lgc->lgc_index = rec->lrh_index;
727 /* using lu_env for passing record offset to
728 * llog_write through various callbacks */
729 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
731 last_called_index = index;
734 lgc->lgc_offset = tmp_off;
735 lgc->lgc_index = tmp_idx;
738 if (rc == LLOG_PROC_BREAK) {
740 } else if (rc == LLOG_DEL_RECORD) {
741 rc = llog_cancel_rec(lpi->lpi_env,
744 /* Allow parallel cancelling, ENOENT
745 * means record was canceled at another
746 * processing thread or callback
753 /* some stupid callbacks directly cancel records
754 * and delete llog. Check it and stop
756 if (loghandle->lgh_hdr == NULL ||
757 loghandle->lgh_hdr->llh_count == 1)
760 /* exit if the last index is reached */
761 if (index >= last_index)
768 CDEBUG(D_HA, "stop processing %s "DOSTID":%x index %d count %d\n",
769 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
770 POSTID(&loghandle->lgh_id.lgl_oi), loghandle->lgh_id.lgl_ogen,
771 index, llh->llh_count);
774 cd->lpcd_last_idx = last_called_index;
776 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
777 if (dt_object_remote(loghandle->lgh_obj)) {
778 /* If it is remote object, then -EIO might means
779 * disconnection or eviction, let's return -EAGAIN,
780 * so for update recovery log processing, it will
781 * retry until the umount or abort recovery, see
782 * lod_sub_recovery_thread() */
783 CERROR("%s retry remote llog process\n",
784 loghandle2name(loghandle));
787 /* something bad happened to the processing of a local
788 * llog file, probably I/O error or the log got
789 * corrupted to be able to finally release the log we
790 * discard any remaining bits in the header */
791 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
792 " %s index %d count %d\n",
793 loghandle2name(loghandle),
794 POSTID(&loghandle->lgh_id.lgl_oi),
795 loghandle->lgh_id.lgl_ogen,
796 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
797 "plain"), index, llh->llh_count);
799 while (index <= last_index) {
800 if (test_bit_le(index,
801 LLOG_HDR_BITMAP(llh)) != 0)
802 llog_cancel_rec(lpi->lpi_env, loghandle,
810 OBD_FREE_LARGE(buf, chunk_size);
815 static int llog_process_thread_daemonize(void *arg)
817 struct llog_process_info *lpi = arg;
820 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
822 task_lock(lpi->lpi_reftask);
823 new_ns = lpi->lpi_reftask->nsproxy;
824 if (curr_ns != new_ns) {
827 current->nsproxy = new_ns;
828 /* XXX: we should call put_nsproxy() instead of
829 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
830 * used outside of the kernel itself, because it calls
831 * free_nsproxy() which is not exported by the kernel
832 * (defined in kernel/nsproxy.c) */
834 atomic_dec(&curr_ns->count);
836 task_unlock(lpi->lpi_reftask);
838 /* client env has no keys, tags is just 0 */
839 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
844 rc = llog_process_thread(arg);
848 complete(&lpi->lpi_completion);
852 int llog_process_or_fork(const struct lu_env *env,
853 struct llog_handle *loghandle,
854 llog_cb_t cb, void *data, void *catdata, bool fork)
856 struct llog_process_info *lpi;
857 struct llog_process_data *d = data;
858 struct llog_process_cat_data *cd = catdata;
859 __u32 flags = loghandle->lgh_hdr->llh_flags;
866 CERROR("cannot alloc pointer\n");
869 lpi->lpi_loghandle = loghandle;
871 lpi->lpi_cbdata = data;
872 lpi->lpi_catdata = catdata;
874 CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d read_mode %d\n",
875 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), flags,
876 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
877 (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
878 cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1,
879 cd ? cd->lpcd_read_mode : -1);
881 struct task_struct *task;
883 /* The new thread can't use parent env,
884 * init the new one in llog_process_thread_daemonize. */
886 init_completion(&lpi->lpi_completion);
887 /* take reference to current, so that
888 * llog_process_thread_daemonize() can use it to switch to
889 * namespace associated with current */
890 lpi->lpi_reftask = current;
891 task = kthread_run(llog_process_thread_daemonize, lpi,
892 "llog_process_thread");
895 CERROR("%s: cannot start thread: rc = %d\n",
896 loghandle2name(loghandle), rc);
899 wait_for_completion(&lpi->lpi_completion);
902 llog_process_thread(lpi);
910 EXPORT_SYMBOL(llog_process_or_fork);
912 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
913 llog_cb_t cb, void *data, void *catdata)
916 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
917 return rc == LLOG_DEL_PLAIN ? 0 : rc;
919 EXPORT_SYMBOL(llog_process);
921 static inline const struct cred *llog_raise_resource(void)
923 struct cred *cred = NULL;
925 if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
928 cred = prepare_creds();
932 cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
933 return override_creds(cred);
936 static inline void llog_restore_resource(const struct cred *old_cred)
939 revert_creds(old_cred);
942 int llog_reverse_process(const struct lu_env *env,
943 struct llog_handle *loghandle, llog_cb_t cb,
944 void *data, void *catdata)
946 struct llog_log_hdr *llh = loghandle->lgh_hdr;
947 struct llog_process_cat_data *cd = catdata;
949 int rc = 0, first_index = 1, index, idx;
950 __u32 chunk_size = llh->llh_hdr.lrh_len;
953 OBD_ALLOC_LARGE(buf, chunk_size);
958 first_index = cd->lpcd_first_idx + 1;
959 if (cd != NULL && cd->lpcd_last_idx)
960 index = cd->lpcd_last_idx;
962 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
965 struct llog_rec_hdr *rec;
966 struct llog_rec_tail *tail;
968 /* skip records not set in bitmap */
969 while (index >= first_index &&
970 llog_is_index_skipable(index, llh, cd))
973 LASSERT(index >= first_index - 1);
974 if (index == first_index - 1)
977 /* get the buf with our target record; avoid old garbage */
978 memset(buf, 0, chunk_size);
979 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
984 idx = rec->lrh_index;
985 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
986 while (idx < index) {
987 rec = (void *)rec + rec->lrh_len;
988 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
989 lustre_swab_llog_rec(rec);
992 LASSERT(idx == index);
993 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
995 /* process records in buffer, starting where we found one */
996 while ((void *)tail > buf) {
997 if (tail->lrt_index == 0)
998 GOTO(out, rc = 0); /* no more records */
1000 /* if needed, process the callback on this record */
1001 if (!llog_is_index_skipable(index, llh, cd)) {
1002 rec = (void *)tail - tail->lrt_len +
1005 rc = cb(env, loghandle, rec, data);
1006 if (rc == LLOG_PROC_BREAK) {
1008 } else if (rc == LLOG_DEL_RECORD) {
1009 rc = llog_cancel_rec(env, loghandle,
1016 /* previous record, still in buffer? */
1018 if (index < first_index)
1020 tail = (void *)tail - tail->lrt_len;
1026 OBD_FREE_LARGE(buf, chunk_size);
1029 EXPORT_SYMBOL(llog_reverse_process);
1035 * llog_open - open llog, may not exist
1036 * llog_exist - check if llog exists
1037 * llog_close - close opened llog, pair for open, frees llog_handle
1038 * llog_declare_create - declare llog creation
1039 * llog_create - create new llog on disk, need transaction handle
1040 * llog_declare_write_rec - declaration of llog write
1041 * llog_write_rec - write llog record on disk, need transaction handle
1042 * llog_declare_add - declare llog catalog record addition
1043 * llog_add - add llog record in catalog, need transaction handle
1045 int llog_exist(struct llog_handle *loghandle)
1047 const struct llog_operations *lop;
1052 rc = llog_handle2ops(loghandle, &lop);
1055 if (lop->lop_exist == NULL)
1056 RETURN(-EOPNOTSUPP);
1058 rc = lop->lop_exist(loghandle);
1061 EXPORT_SYMBOL(llog_exist);
1063 int llog_declare_create(const struct lu_env *env,
1064 struct llog_handle *loghandle, struct thandle *th)
1066 const struct cred *old_cred;
1067 const struct llog_operations *lop;
1072 rc = llog_handle2ops(loghandle, &lop);
1075 if (lop->lop_declare_create == NULL)
1076 RETURN(-EOPNOTSUPP);
1078 old_cred = llog_raise_resource();
1079 rc = lop->lop_declare_create(env, loghandle, th);
1080 llog_restore_resource(old_cred);
1084 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1087 const struct cred *old_cred;
1088 const struct llog_operations *lop;
1093 rc = llog_handle2ops(handle, &lop);
1096 if (lop->lop_create == NULL)
1097 RETURN(-EOPNOTSUPP);
1099 old_cred = llog_raise_resource();
1100 rc = lop->lop_create(env, handle, th);
1101 llog_restore_resource(old_cred);
1105 int llog_declare_write_rec(const struct lu_env *env,
1106 struct llog_handle *handle,
1107 struct llog_rec_hdr *rec, int idx,
1110 const struct cred *old_cred;
1111 const struct llog_operations *lop;
1116 rc = llog_handle2ops(handle, &lop);
1120 if (lop->lop_declare_write_rec == NULL)
1121 RETURN(-EOPNOTSUPP);
1123 old_cred = llog_raise_resource();
1124 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1125 llog_restore_resource(old_cred);
1129 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1130 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1131 int idx, struct thandle *th)
1133 const struct cred *old_cred;
1134 const struct llog_operations *lop;
1139 /* API sanity checks */
1140 if (handle == NULL) {
1141 CERROR("loghandle is missed\n");
1143 } else if (handle->lgh_obj == NULL) {
1144 CERROR("loghandle %p with NULL object\n",
1147 } else if (th == NULL) {
1148 CERROR("%s: missed transaction handle\n",
1149 loghandle2name(handle));
1151 } else if (handle->lgh_hdr == NULL) {
1152 CERROR("%s: loghandle %p with no header\n",
1153 loghandle2name(handle), handle);
1157 rc = llog_handle2ops(handle, &lop);
1161 if (lop->lop_write_rec == NULL)
1162 RETURN(-EOPNOTSUPP);
1164 buflen = rec->lrh_len;
1165 LASSERT(cfs_size_round(buflen) == buflen);
1167 old_cred = llog_raise_resource();
1168 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1169 llog_restore_resource(old_cred);
1173 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1174 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1177 const struct cred *old_cred;
1182 if (lgh->lgh_logops->lop_add == NULL)
1183 RETURN(-EOPNOTSUPP);
1185 old_cred = llog_raise_resource();
1186 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1187 llog_restore_resource(old_cred);
1190 EXPORT_SYMBOL(llog_add);
1192 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1193 struct llog_rec_hdr *rec, struct thandle *th)
1195 const struct cred *old_cred;
1200 if (lgh->lgh_logops->lop_declare_add == NULL)
1201 RETURN(-EOPNOTSUPP);
1203 old_cred = llog_raise_resource();
1204 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1205 llog_restore_resource(old_cred);
1208 EXPORT_SYMBOL(llog_declare_add);
1211 * Helper function to open llog or create it if doesn't exist.
1212 * It hides all transaction handling from caller.
1214 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1215 struct llog_handle **res, struct llog_logid *logid,
1218 struct dt_device *d;
1224 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1228 if (llog_exist(*res))
1231 LASSERT((*res)->lgh_obj != NULL);
1233 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1235 if (unlikely(unlikely(d->dd_rdonly)))
1238 th = dt_trans_create(env, d);
1240 GOTO(out, rc = PTR_ERR(th));
1242 /* Create update llog object synchronously, which
1243 * happens during inialization process see
1244 * lod_sub_prep_llog(), to make sure the update
1245 * llog object is created before corss-MDT writing
1246 * updates into the llog object */
1247 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1250 th->th_wait_submit = 1;
1251 rc = llog_declare_create(env, *res, th);
1253 rc = dt_trans_start_local(env, d, th);
1255 rc = llog_create(env, *res, th);
1257 dt_trans_stop(env, d, th);
1260 llog_close(env, *res);
1263 EXPORT_SYMBOL(llog_open_create);
1266 * Helper function to delete existent llog.
1268 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1269 struct llog_logid *logid, char *name)
1271 struct llog_handle *handle;
1276 /* nothing to erase */
1277 if (name == NULL && logid == NULL)
1280 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1284 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1286 rc = llog_destroy(env, handle);
1288 rc2 = llog_close(env, handle);
1293 EXPORT_SYMBOL(llog_erase);
1296 * Helper function for write record in llog.
1297 * It hides all transaction handling from caller.
1298 * Valid only with local llog.
1300 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1301 struct llog_rec_hdr *rec, int idx)
1303 struct dt_device *dt;
1311 LASSERT(loghandle->lgh_ctxt);
1312 LASSERT(loghandle->lgh_obj != NULL);
1314 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1316 if (unlikely(unlikely(dt->dd_rdonly)))
1319 th = dt_trans_create(env, dt);
1321 RETURN(PTR_ERR(th));
1323 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1325 GOTO(out_trans, rc);
1327 th->th_wait_submit = 1;
1328 rc = dt_trans_start_local(env, dt, th);
1330 GOTO(out_trans, rc);
1332 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1334 down_write(&loghandle->lgh_lock);
1336 struct llog_thread_info *lti = llog_info(env);
1338 /* cookie comes from llog_process_thread */
1339 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1340 rec->lrh_index, th);
1341 /* upper layer didn`t pass cookie so change rc */
1342 rc = (rc == 1 ? 0 : rc);
1344 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1347 up_write(&loghandle->lgh_lock);
1349 dt_trans_stop(env, dt, th);
1352 EXPORT_SYMBOL(llog_write);
1354 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1355 struct llog_handle **lgh, struct llog_logid *logid,
1356 char *name, enum llog_open_param open_param)
1358 const struct cred *old_cred;
1364 LASSERT(ctxt->loc_logops);
1366 if (ctxt->loc_logops->lop_open == NULL) {
1368 RETURN(-EOPNOTSUPP);
1371 *lgh = llog_alloc_handle();
1374 (*lgh)->lgh_ctxt = ctxt;
1375 (*lgh)->lgh_logops = ctxt->loc_logops;
1377 old_cred = llog_raise_resource();
1378 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1379 llog_restore_resource(old_cred);
1381 llog_free_handle(*lgh);
1386 EXPORT_SYMBOL(llog_open);
1388 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1390 return llog_handle_put(env, loghandle);
1392 EXPORT_SYMBOL(llog_close);
1395 * Helper function to get the llog size in records. It is used by MGS
1396 * mostly to check that config llog exists and contains data.
1398 * \param[in] env execution environment
1399 * \param[in] ctxt llog context
1400 * \param[in] name llog name
1402 * \retval true if there are records in llog besides a header
1403 * \retval false on error or llog without records
1405 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1408 struct llog_handle *llh;
1411 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1413 if (likely(rc == -ENOENT))
1418 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1420 GOTO(out_close, rc);
1421 rc = llog_get_size(llh);
1424 llog_close(env, llh);
1426 /* The header is record 1, the llog is still considered as empty
1427 * if there is only header */
1430 EXPORT_SYMBOL(llog_is_empty);
1432 /* this callback run in raw read mode (canceled record are processed) */
1433 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1434 struct llog_rec_hdr *rec, void *data)
1436 struct llog_handle *copy_llh = data;
1437 int idx = rec->lrh_index;
1442 /* Append all records */
1443 rc = llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1445 /* Cancel the record if it is canceled on the source */
1446 if (!rc && !test_bit_le(idx, LLOG_HDR_BITMAP(llh->lgh_hdr)))
1447 rc = llog_cancel_rec(env, copy_llh, copy_llh->lgh_last_idx);
1452 /* backup plain llog */
1453 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1454 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1455 char *name, char *backup)
1457 struct llog_handle *llh, *bllh;
1458 struct llog_process_cat_data cd = {0};
1463 /* open original log */
1464 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1466 /* the -ENOENT case is also reported to the caller
1467 * but silently so it should handle that if needed.
1470 CERROR("%s: failed to open log %s: rc = %d\n",
1471 obd->obd_name, name, rc);
1475 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1477 GOTO(out_close, rc);
1479 /* Make sure there's no old backup log */
1480 rc = llog_erase(env, bctxt, NULL, backup);
1481 if (rc < 0 && rc != -ENOENT)
1482 GOTO(out_close, rc);
1484 /* open backup log */
1485 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1487 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1488 obd->obd_name, backup, rc);
1489 GOTO(out_close, rc);
1492 /* check that backup llog is not the same object as original one */
1493 if (llh->lgh_obj == bllh->lgh_obj) {
1494 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1495 obd->obd_name, name, backup, llh->lgh_obj,
1497 GOTO(out_backup, rc = -EEXIST);
1500 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1502 GOTO(out_backup, rc);
1504 /* Read canceled records to have an exact copy */
1505 cd.lpcd_read_mode = LLOG_READ_MODE_RAW;
1506 /* Copy log record by record */
1507 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1510 CERROR("%s: failed to backup log %s: rc = %d\n",
1511 obd->obd_name, name, rc);
1513 llog_close(env, bllh);
1515 llog_close(env, llh);
1518 EXPORT_SYMBOL(llog_backup);
1520 /* Get size of llog */
1521 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1526 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1528 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1529 loghandle2name(llh), PFID(&llh->lgh_id.lgl_oi.oi_fid),
1536 EXPORT_SYMBOL(llog_size);