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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/llog.c
34 * OST<->MDS recovery logging infrastructure.
35 * Invariants in implementation:
36 * - we do not share logs among different OST<->MDS connections, so that
37 * if an OST or MDS fails it need only look at log(s) relevant to itself
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41 * Author: Mikhail Pershin <tappro@whamcloud.com>
44 #define DEBUG_SUBSYSTEM S_LOG
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"
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 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 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 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 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 (!ext2_clear_bit(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 loghandle->lgh_ctxt->loc_obd->obd_name,
308 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
309 GOTO(out_unlock, rc = 0);
315 mutex_unlock(&loghandle->lgh_hdr_mutex);
316 up_write(&loghandle->lgh_lock);
318 rc1 = dt_trans_stop(env, dt, th);
322 mutex_lock(&loghandle->lgh_hdr_mutex);
324 loghandle->lgh_hdr->llh_count += num;
325 for (i = i - 1; i >= 0; i--)
326 ext2_set_bit(index[i], LLOG_HDR_BITMAP(llh));
327 mutex_unlock(&loghandle->lgh_hdr_mutex);
332 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
335 return llog_cancel_arr_rec(env, loghandle, 1, &index);
338 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
339 const struct obd_uuid *uuid)
341 struct llog_operations *lop;
345 rc = llog_handle2ops(handle, &lop);
349 if (lop->lop_read_header == NULL)
352 rc = lop->lop_read_header(env, handle);
353 if (rc == LLOG_EEMPTY) {
354 struct llog_log_hdr *llh = handle->lgh_hdr;
356 /* lrh_len should be initialized in llog_init_handle */
357 handle->lgh_last_idx = 0; /* header is record with index 0 */
358 llh->llh_count = 1; /* for the header record */
359 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
360 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
361 LLOG_MIN_CHUNK_SIZE);
362 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
363 llh->llh_hdr.lrh_index = 0;
364 llh->llh_timestamp = ktime_get_real_seconds();
366 memcpy(&llh->llh_tgtuuid, uuid,
367 sizeof(llh->llh_tgtuuid));
368 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
369 /* Since update llog header might also call this function,
370 * let's reset the bitmap to 0 here */
371 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
372 llh->llh_bitmap_offset -
373 sizeof(llh->llh_tail));
374 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
375 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
376 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
381 EXPORT_SYMBOL(llog_read_header);
383 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
384 int flags, struct obd_uuid *uuid)
386 struct llog_log_hdr *llh;
387 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
389 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
392 LASSERT(handle->lgh_hdr == NULL);
394 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
395 OBD_ALLOC_LARGE(llh, chunk_size);
399 handle->lgh_hdr = llh;
400 handle->lgh_hdr_size = chunk_size;
401 /* first assign flags to use llog_client_ops */
402 llh->llh_flags = flags;
403 rc = llog_read_header(env, handle, uuid);
405 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
406 flags & LLOG_F_IS_CAT) ||
407 (llh->llh_flags & LLOG_F_IS_CAT &&
408 flags & LLOG_F_IS_PLAIN))) {
409 CERROR("%s: llog type is %s but initializing %s\n",
410 handle->lgh_ctxt->loc_obd->obd_name,
411 llh->llh_flags & LLOG_F_IS_CAT ?
413 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
414 GOTO(out, rc = -EINVAL);
415 } else if (llh->llh_flags &
416 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
418 * it is possible to open llog without specifying llog
419 * type so it is taken from llh_flags
421 flags = llh->llh_flags;
423 /* for some reason the llh_flags has no type set */
424 CERROR("llog type is not specified!\n");
425 GOTO(out, rc = -EINVAL);
428 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
429 CERROR("%s: llog uuid mismatch: %s/%s\n",
430 handle->lgh_ctxt->loc_obd->obd_name,
432 (char *)llh->llh_tgtuuid.uuid);
433 GOTO(out, rc = -EEXIST);
436 if (flags & LLOG_F_IS_CAT) {
437 LASSERT(list_empty(&handle->u.chd.chd_head));
438 INIT_LIST_HEAD(&handle->u.chd.chd_head);
439 llh->llh_size = sizeof(struct llog_logid_rec);
440 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
441 } else if (!(flags & LLOG_F_IS_PLAIN)) {
442 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
443 handle->lgh_ctxt->loc_obd->obd_name,
444 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
447 llh->llh_flags |= fmt;
450 OBD_FREE_LARGE(llh, chunk_size);
451 handle->lgh_hdr = NULL;
455 EXPORT_SYMBOL(llog_init_handle);
457 static int llog_process_thread(void *arg)
459 struct llog_process_info *lpi = arg;
460 struct llog_handle *loghandle = lpi->lpi_loghandle;
461 struct llog_log_hdr *llh = loghandle->lgh_hdr;
462 struct llog_process_cat_data *cd = lpi->lpi_catdata;
463 struct llog_thread_info *lti;
467 int rc = 0, index = 1, last_index;
469 int last_called_index = 0;
470 bool repeated = false;
477 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
479 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
480 /* expect chunk_size to be power of two */
481 LASSERT(is_power_of_2(chunk_size));
483 OBD_ALLOC_LARGE(buf, chunk_size);
485 lpi->lpi_rc = -ENOMEM;
490 last_called_index = cd->lpcd_first_idx;
491 index = cd->lpcd_first_idx + 1;
493 if (cd != NULL && cd->lpcd_last_idx)
494 last_index = cd->lpcd_last_idx;
496 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
499 struct llog_rec_hdr *rec;
500 off_t chunk_offset = 0;
501 unsigned int buf_offset = 0;
506 /* skip records not set in bitmap */
507 while (index <= last_index &&
508 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
511 /* There are no indices prior the last_index */
512 if (index > last_index)
515 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
519 /* get the buf with our target record; avoid old garbage */
520 memset(buf, 0, chunk_size);
521 /* the record index for outdated chunk data */
522 /* it is safe to process buffer until saved lgh_last_idx */
523 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
524 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
525 index, &cur_offset, buf, chunk_size);
527 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
528 " buf_offset %u, rc = %d\n", cur_offset,
529 (__u64)chunk_offset, buf_offset, rc);
530 /* we`ve tried to reread the chunk, but there is no
532 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
538 /* NB: after llog_next_block() call the cur_offset is the
539 * offset of the next block after read one.
540 * The absolute offset of the current chunk is calculated
541 * from cur_offset value and stored in chunk_offset variable.
543 if ((cur_offset & (chunk_size - 1)) != 0) {
544 partial_chunk = true;
545 chunk_offset = cur_offset & ~(chunk_size - 1);
547 partial_chunk = false;
548 chunk_offset = cur_offset - chunk_size;
551 /* NB: when rec->lrh_len is accessed it is already swabbed
552 * since it is used at the "end" of the loop and the rec
553 * swabbing is done at the beginning of the loop. */
554 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
555 (char *)rec < buf + chunk_size;
556 rec = llog_rec_hdr_next(rec)) {
558 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
561 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
562 lustre_swab_llog_rec(rec);
564 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
565 rec->lrh_type, rec->lrh_index);
567 if (index == (synced_idx + 1) &&
568 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
571 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
572 cfs_fail_val == (unsigned int)
573 (loghandle->lgh_id.lgl_oi.oi.oi_id &
575 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
578 /* the bitmap could be changed during processing
579 * records from the chunk. For wrapped catalog
580 * it means we can read deleted record and try to
581 * process it. Check this case and reread the chunk.
582 * It is safe to process to lh_last_idx, including
583 * lh_last_idx if it was synced. We can not do <=
584 * comparison, cause for wrapped catalog lgh_last_idx
585 * could be less than index. So we detect last index
586 * for processing as index == lh_last_idx+1. But when
587 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
588 * the first processing index is llh_cat_idx+1.
591 if ((index == lh_last_idx && synced_idx != index) ||
592 (index == (lh_last_idx + 1) &&
593 !(index == (llh->llh_cat_idx + 1) &&
594 (llh->llh_flags & LLOG_F_IS_CAT))) ||
595 (rec->lrh_index == 0 && !repeated)) {
597 /* save offset inside buffer for the re-read */
598 buf_offset = (char *)rec - (char *)buf;
599 cur_offset = chunk_offset;
601 /* We need to be sure lgh_last_idx
602 * record was saved to disk
604 down_read(&loghandle->lgh_last_sem);
605 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
606 up_read(&loghandle->lgh_last_sem);
607 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
614 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
615 CWARN("%s: invalid length %d in llog "DFID
616 "record for index %d/%d\n",
617 loghandle->lgh_ctxt->loc_obd->obd_name,
619 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
620 rec->lrh_index, index);
622 GOTO(out, rc = -EINVAL);
625 if (rec->lrh_index < index) {
626 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
631 if (rec->lrh_index != index) {
632 CERROR("%s: "DFID" Invalid record: index %u"
633 " but expected %u\n",
634 loghandle->lgh_ctxt->loc_obd->obd_name,
635 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
636 rec->lrh_index, index);
637 GOTO(out, rc = -ERANGE);
641 "lrh_index: %d lrh_len: %d (%d remains)\n",
642 rec->lrh_index, rec->lrh_len,
643 (int)(buf + chunk_size - (char *)rec));
645 /* lgh_cur_offset is used only at llog_test_3 */
646 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
649 /* if set, process the callback on this record */
650 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
651 struct llog_cookie *lgc;
655 CDEBUG(D_OTHER, "index: %d, lh_last_idx: %d "
656 "synced_idx: %d lgh_last_idx: %d\n",
657 index, lh_last_idx, synced_idx,
658 loghandle->lgh_last_idx);
661 lgc = <i->lgi_cookie;
662 /* store lu_env for recursive calls */
663 tmp_off = lgc->lgc_offset;
664 tmp_idx = lgc->lgc_index;
666 lgc->lgc_offset = (char *)rec -
667 (char *)buf + chunk_offset;
668 lgc->lgc_index = rec->lrh_index;
670 /* using lu_env for passing record offset to
671 * llog_write through various callbacks */
672 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
674 last_called_index = index;
677 lgc->lgc_offset = tmp_off;
678 lgc->lgc_index = tmp_idx;
681 if (rc == LLOG_PROC_BREAK) {
683 } else if (rc == LLOG_DEL_RECORD) {
684 rc = llog_cancel_rec(lpi->lpi_env,
690 /* some stupid callbacks directly cancel records
691 * and delete llog. Check it and stop
693 if (loghandle->lgh_hdr == NULL ||
694 loghandle->lgh_hdr->llh_count == 1)
697 /* exit if the last index is reached */
698 if (index >= last_index)
706 cd->lpcd_last_idx = last_called_index;
708 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
709 if (dt_object_remote(loghandle->lgh_obj)) {
710 /* If it is remote object, then -EIO might means
711 * disconnection or eviction, let's return -EAGAIN,
712 * so for update recovery log processing, it will
713 * retry until the umount or abort recovery, see
714 * lod_sub_recovery_thread() */
715 CERROR("%s retry remote llog process\n",
716 loghandle->lgh_ctxt->loc_obd->obd_name);
719 /* something bad happened to the processing of a local
720 * llog file, probably I/O error or the log got
721 * corrupted to be able to finally release the log we
722 * discard any remaining bits in the header */
723 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
724 " %s index %d count %d\n",
725 loghandle->lgh_ctxt->loc_obd->obd_name,
726 POSTID(&loghandle->lgh_id.lgl_oi),
727 loghandle->lgh_id.lgl_ogen,
728 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
729 "plain"), index, llh->llh_count);
731 while (index <= last_index) {
732 if (ext2_test_bit(index,
733 LLOG_HDR_BITMAP(llh)) != 0)
734 llog_cancel_rec(lpi->lpi_env, loghandle,
742 OBD_FREE_LARGE(buf, chunk_size);
747 static int llog_process_thread_daemonize(void *arg)
749 struct llog_process_info *lpi = arg;
752 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
754 task_lock(lpi->lpi_reftask);
755 new_ns = lpi->lpi_reftask->nsproxy;
756 if (curr_ns != new_ns) {
759 current->nsproxy = new_ns;
760 /* XXX: we should call put_nsproxy() instead of
761 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
762 * used outside of the kernel itself, because it calls
763 * free_nsproxy() which is not exported by the kernel
764 * (defined in kernel/nsproxy.c) */
766 atomic_dec(&curr_ns->count);
768 task_unlock(lpi->lpi_reftask);
772 /* client env has no keys, tags is just 0 */
773 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
778 rc = llog_process_thread(arg);
782 complete(&lpi->lpi_completion);
786 int llog_process_or_fork(const struct lu_env *env,
787 struct llog_handle *loghandle,
788 llog_cb_t cb, void *data, void *catdata, bool fork)
790 struct llog_process_info *lpi;
791 struct llog_process_data *d = data;
792 struct llog_process_cat_data *cd = catdata;
799 CERROR("cannot alloc pointer\n");
802 lpi->lpi_loghandle = loghandle;
804 lpi->lpi_cbdata = data;
805 lpi->lpi_catdata = catdata;
807 CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d\n",
808 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
809 loghandle->lgh_hdr->llh_flags, d ? d->lpd_startcat : -1,
810 d ? d->lpd_startidx : -1, cd ? cd->lpcd_first_idx : -1,
811 cd ? cd->lpcd_last_idx : -1);
813 struct task_struct *task;
815 /* The new thread can't use parent env,
816 * init the new one in llog_process_thread_daemonize. */
818 init_completion(&lpi->lpi_completion);
819 /* take reference to current, so that
820 * llog_process_thread_daemonize() can use it to switch to
821 * namespace associated with current */
822 lpi->lpi_reftask = current;
823 task = kthread_run(llog_process_thread_daemonize, lpi,
824 "llog_process_thread");
827 CERROR("%s: cannot start thread: rc = %d\n",
828 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
831 wait_for_completion(&lpi->lpi_completion);
834 llog_process_thread(lpi);
842 EXPORT_SYMBOL(llog_process_or_fork);
844 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
845 llog_cb_t cb, void *data, void *catdata)
848 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
849 return rc == LLOG_DEL_PLAIN ? 0 : rc;
851 EXPORT_SYMBOL(llog_process);
853 static inline const struct cred *llog_raise_resource(void)
855 struct cred *cred = NULL;
857 if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
860 cred = prepare_creds();
864 cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
865 return override_creds(cred);
868 static inline void llog_restore_resource(const struct cred *old_cred)
871 revert_creds(old_cred);
874 int llog_reverse_process(const struct lu_env *env,
875 struct llog_handle *loghandle, llog_cb_t cb,
876 void *data, void *catdata)
878 struct llog_log_hdr *llh = loghandle->lgh_hdr;
879 struct llog_process_cat_data *cd = catdata;
881 int rc = 0, first_index = 1, index, idx;
882 __u32 chunk_size = llh->llh_hdr.lrh_len;
885 OBD_ALLOC_LARGE(buf, chunk_size);
890 first_index = cd->lpcd_first_idx + 1;
891 if (cd != NULL && cd->lpcd_last_idx)
892 index = cd->lpcd_last_idx;
894 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
897 struct llog_rec_hdr *rec;
898 struct llog_rec_tail *tail;
900 /* skip records not set in bitmap */
901 while (index >= first_index &&
902 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
905 LASSERT(index >= first_index - 1);
906 if (index == first_index - 1)
909 /* get the buf with our target record; avoid old garbage */
910 memset(buf, 0, chunk_size);
911 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
916 idx = rec->lrh_index;
917 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
918 while (idx < index) {
919 rec = (void *)rec + rec->lrh_len;
920 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
921 lustre_swab_llog_rec(rec);
924 LASSERT(idx == index);
925 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
927 /* process records in buffer, starting where we found one */
928 while ((void *)tail > buf) {
929 if (tail->lrt_index == 0)
930 GOTO(out, rc = 0); /* no more records */
932 /* if set, process the callback on this record */
933 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
934 rec = (void *)tail - tail->lrt_len +
937 rc = cb(env, loghandle, rec, data);
938 if (rc == LLOG_PROC_BREAK) {
940 } else if (rc == LLOG_DEL_RECORD) {
941 rc = llog_cancel_rec(env, loghandle,
948 /* previous record, still in buffer? */
950 if (index < first_index)
952 tail = (void *)tail - tail->lrt_len;
958 OBD_FREE_LARGE(buf, chunk_size);
961 EXPORT_SYMBOL(llog_reverse_process);
967 * llog_open - open llog, may not exist
968 * llog_exist - check if llog exists
969 * llog_close - close opened llog, pair for open, frees llog_handle
970 * llog_declare_create - declare llog creation
971 * llog_create - create new llog on disk, need transaction handle
972 * llog_declare_write_rec - declaration of llog write
973 * llog_write_rec - write llog record on disk, need transaction handle
974 * llog_declare_add - declare llog catalog record addition
975 * llog_add - add llog record in catalog, need transaction handle
977 int llog_exist(struct llog_handle *loghandle)
979 struct llog_operations *lop;
984 rc = llog_handle2ops(loghandle, &lop);
987 if (lop->lop_exist == NULL)
990 rc = lop->lop_exist(loghandle);
993 EXPORT_SYMBOL(llog_exist);
995 int llog_declare_create(const struct lu_env *env,
996 struct llog_handle *loghandle, struct thandle *th)
998 const struct cred *old_cred;
999 struct llog_operations *lop;
1004 rc = llog_handle2ops(loghandle, &lop);
1007 if (lop->lop_declare_create == NULL)
1008 RETURN(-EOPNOTSUPP);
1010 old_cred = llog_raise_resource();
1011 rc = lop->lop_declare_create(env, loghandle, th);
1012 llog_restore_resource(old_cred);
1016 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1019 const struct cred *old_cred;
1020 struct llog_operations *lop;
1025 rc = llog_handle2ops(handle, &lop);
1028 if (lop->lop_create == NULL)
1029 RETURN(-EOPNOTSUPP);
1031 old_cred = llog_raise_resource();
1032 rc = lop->lop_create(env, handle, th);
1033 llog_restore_resource(old_cred);
1037 int llog_declare_write_rec(const struct lu_env *env,
1038 struct llog_handle *handle,
1039 struct llog_rec_hdr *rec, int idx,
1042 const struct cred *old_cred;
1043 struct llog_operations *lop;
1048 rc = llog_handle2ops(handle, &lop);
1052 if (lop->lop_declare_write_rec == NULL)
1053 RETURN(-EOPNOTSUPP);
1055 old_cred = llog_raise_resource();
1056 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1057 llog_restore_resource(old_cred);
1061 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1062 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1063 int idx, struct thandle *th)
1065 const struct cred *old_cred;
1066 struct llog_operations *lop;
1071 /* API sanity checks */
1072 if (handle == NULL) {
1073 CERROR("loghandle is missed\n");
1075 } else if (handle->lgh_obj == NULL) {
1076 CERROR("loghandle %p with NULL object\n",
1079 } else if (th == NULL) {
1080 CERROR("%s: missed transaction handle\n",
1081 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1083 } else if (handle->lgh_hdr == NULL) {
1084 CERROR("%s: loghandle %p with no header\n",
1085 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1090 rc = llog_handle2ops(handle, &lop);
1094 if (lop->lop_write_rec == NULL)
1095 RETURN(-EOPNOTSUPP);
1097 buflen = rec->lrh_len;
1098 LASSERT(cfs_size_round(buflen) == buflen);
1100 old_cred = llog_raise_resource();
1101 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1102 llog_restore_resource(old_cred);
1106 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1107 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1110 const struct cred *old_cred;
1115 if (lgh->lgh_logops->lop_add == NULL)
1116 RETURN(-EOPNOTSUPP);
1118 old_cred = llog_raise_resource();
1119 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1120 llog_restore_resource(old_cred);
1123 EXPORT_SYMBOL(llog_add);
1125 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1126 struct llog_rec_hdr *rec, struct thandle *th)
1128 const struct cred *old_cred;
1133 if (lgh->lgh_logops->lop_declare_add == NULL)
1134 RETURN(-EOPNOTSUPP);
1136 old_cred = llog_raise_resource();
1137 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1138 llog_restore_resource(old_cred);
1141 EXPORT_SYMBOL(llog_declare_add);
1144 * Helper function to open llog or create it if doesn't exist.
1145 * It hides all transaction handling from caller.
1147 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1148 struct llog_handle **res, struct llog_logid *logid,
1151 struct dt_device *d;
1157 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1161 if (llog_exist(*res))
1164 LASSERT((*res)->lgh_obj != NULL);
1166 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1168 if (unlikely(unlikely(d->dd_rdonly)))
1171 th = dt_trans_create(env, d);
1173 GOTO(out, rc = PTR_ERR(th));
1175 /* Create update llog object synchronously, which
1176 * happens during inialization process see
1177 * lod_sub_prep_llog(), to make sure the update
1178 * llog object is created before corss-MDT writing
1179 * updates into the llog object */
1180 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1183 th->th_wait_submit = 1;
1184 rc = llog_declare_create(env, *res, th);
1186 rc = dt_trans_start_local(env, d, th);
1188 rc = llog_create(env, *res, th);
1190 dt_trans_stop(env, d, th);
1193 llog_close(env, *res);
1196 EXPORT_SYMBOL(llog_open_create);
1199 * Helper function to delete existent llog.
1201 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1202 struct llog_logid *logid, char *name)
1204 struct llog_handle *handle;
1209 /* nothing to erase */
1210 if (name == NULL && logid == NULL)
1213 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1217 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1219 rc = llog_destroy(env, handle);
1221 rc2 = llog_close(env, handle);
1226 EXPORT_SYMBOL(llog_erase);
1229 * Helper function for write record in llog.
1230 * It hides all transaction handling from caller.
1231 * Valid only with local llog.
1233 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1234 struct llog_rec_hdr *rec, int idx)
1236 struct dt_device *dt;
1244 LASSERT(loghandle->lgh_ctxt);
1245 LASSERT(loghandle->lgh_obj != NULL);
1247 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1249 if (unlikely(unlikely(dt->dd_rdonly)))
1252 th = dt_trans_create(env, dt);
1254 RETURN(PTR_ERR(th));
1256 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1258 GOTO(out_trans, rc);
1260 th->th_wait_submit = 1;
1261 rc = dt_trans_start_local(env, dt, th);
1263 GOTO(out_trans, rc);
1265 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1267 down_write(&loghandle->lgh_lock);
1269 struct llog_thread_info *lti = llog_info(env);
1271 /* cookie comes from llog_process_thread */
1272 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1273 rec->lrh_index, th);
1274 /* upper layer didn`t pass cookie so change rc */
1275 rc = (rc == 1 ? 0 : rc);
1277 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1280 up_write(&loghandle->lgh_lock);
1282 dt_trans_stop(env, dt, th);
1285 EXPORT_SYMBOL(llog_write);
1287 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1288 struct llog_handle **lgh, struct llog_logid *logid,
1289 char *name, enum llog_open_param open_param)
1291 const struct cred *old_cred;
1297 LASSERT(ctxt->loc_logops);
1299 if (ctxt->loc_logops->lop_open == NULL) {
1301 RETURN(-EOPNOTSUPP);
1304 *lgh = llog_alloc_handle();
1307 (*lgh)->lgh_ctxt = ctxt;
1308 (*lgh)->lgh_logops = ctxt->loc_logops;
1310 old_cred = llog_raise_resource();
1311 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1312 llog_restore_resource(old_cred);
1314 llog_free_handle(*lgh);
1319 EXPORT_SYMBOL(llog_open);
1321 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1323 return llog_handle_put(env, loghandle);
1325 EXPORT_SYMBOL(llog_close);
1328 * Helper function to get the llog size in records. It is used by MGS
1329 * mostly to check that config llog exists and contains data.
1331 * \param[in] env execution environment
1332 * \param[in] ctxt llog context
1333 * \param[in] name llog name
1335 * \retval true if there are records in llog besides a header
1336 * \retval false on error or llog without records
1338 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1341 struct llog_handle *llh;
1344 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1346 if (likely(rc == -ENOENT))
1351 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1353 GOTO(out_close, rc);
1354 rc = llog_get_size(llh);
1357 llog_close(env, llh);
1359 /* The header is record 1, the llog is still considered as empty
1360 * if there is only header */
1363 EXPORT_SYMBOL(llog_is_empty);
1365 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1366 struct llog_rec_hdr *rec, void *data)
1368 struct llog_handle *copy_llh = data;
1370 /* Append all records */
1371 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1374 /* backup plain llog */
1375 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1376 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1377 char *name, char *backup)
1379 struct llog_handle *llh, *bllh;
1384 /* open original log */
1385 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1387 /* the -ENOENT case is also reported to the caller
1388 * but silently so it should handle that if needed.
1391 CERROR("%s: failed to open log %s: rc = %d\n",
1392 obd->obd_name, name, rc);
1396 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1398 GOTO(out_close, rc);
1400 /* Make sure there's no old backup log */
1401 rc = llog_erase(env, bctxt, NULL, backup);
1402 if (rc < 0 && rc != -ENOENT)
1403 GOTO(out_close, rc);
1405 /* open backup log */
1406 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1408 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1409 obd->obd_name, backup, rc);
1410 GOTO(out_close, rc);
1413 /* check that backup llog is not the same object as original one */
1414 if (llh->lgh_obj == bllh->lgh_obj) {
1415 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1416 obd->obd_name, name, backup, llh->lgh_obj,
1418 GOTO(out_backup, rc = -EEXIST);
1421 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1423 GOTO(out_backup, rc);
1425 /* Copy log record by record */
1426 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1429 CERROR("%s: failed to backup log %s: rc = %d\n",
1430 obd->obd_name, name, rc);
1432 llog_close(env, bllh);
1434 llog_close(env, llh);
1437 EXPORT_SYMBOL(llog_backup);
1439 /* Get size of llog */
1440 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1445 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1447 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1448 llh->lgh_ctxt->loc_obd->obd_name,
1449 PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1455 EXPORT_SYMBOL(llog_size);