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 atomic_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 void llog_handle_get(struct llog_handle *loghandle)
96 atomic_inc(&loghandle->lgh_refcount);
99 void llog_handle_put(struct llog_handle *loghandle)
101 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
102 if (atomic_dec_and_test(&loghandle->lgh_refcount))
103 llog_free_handle(loghandle);
106 static int llog_declare_destroy(const struct lu_env *env,
107 struct llog_handle *handle,
110 struct llog_operations *lop;
115 rc = llog_handle2ops(handle, &lop);
118 if (lop->lop_declare_destroy == NULL)
121 rc = lop->lop_declare_destroy(env, handle, th);
126 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
129 struct llog_operations *lop;
133 rc = llog_handle2ops(handle, &lop);
136 if (lop->lop_destroy == NULL)
139 LASSERT(handle->lgh_obj != NULL);
140 if (!dt_object_exists(handle->lgh_obj))
143 rc = lop->lop_destroy(env, handle, th);
148 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
150 struct llog_operations *lop;
151 struct dt_device *dt;
157 rc = llog_handle2ops(handle, &lop);
160 if (lop->lop_destroy == NULL)
163 if (handle->lgh_obj == NULL) {
164 /* if lgh_obj == NULL, then it is from client side destroy */
165 rc = lop->lop_destroy(env, handle, NULL);
169 if (!dt_object_exists(handle->lgh_obj))
172 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
174 if (unlikely(unlikely(dt->dd_rdonly)))
177 th = dt_trans_create(env, dt);
181 rc = llog_declare_destroy(env, handle, th);
185 rc = dt_trans_start_local(env, dt, th);
189 rc = lop->lop_destroy(env, handle, th);
192 dt_trans_stop(env, dt, th);
196 EXPORT_SYMBOL(llog_destroy);
198 /* returns negative on error; 0 if success; 1 if success & log destroyed */
199 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
202 struct llog_thread_info *lgi = llog_info(env);
203 struct dt_device *dt;
204 struct llog_log_hdr *llh;
209 bool subtract_count = false;
213 LASSERT(loghandle != NULL);
214 LASSERT(loghandle->lgh_ctxt != NULL);
215 LASSERT(loghandle->lgh_obj != NULL);
217 llh = loghandle->lgh_hdr;
219 CDEBUG(D_RPCTRACE, "Canceling %d in log "DFID"\n", index,
220 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
223 CERROR("Can't cancel index 0 which is header\n");
227 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
229 if (unlikely(unlikely(dt->dd_rdonly)))
232 th = dt_trans_create(env, dt);
236 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
240 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
241 rc = llog_declare_destroy(env, loghandle, th);
246 th->th_wait_submit = 1;
247 rc = dt_trans_start_local(env, dt, th);
251 down_write(&loghandle->lgh_lock);
253 mutex_lock(&loghandle->lgh_hdr_mutex);
254 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
255 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
256 GOTO(out_unlock, rc);
259 loghandle->lgh_hdr->llh_count--;
260 subtract_count = true;
262 /* Since llog_process_thread use lgi_cookie, it`s better to save them
263 * and restore after using
265 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
266 /* Pass this index to llog_osd_write_rec(), which will use the index
267 * to only update the necesary bitmap. */
268 lgi->lgi_cookie.lgc_index = index;
270 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
271 LLOG_HEADER_IDX, th);
272 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
275 GOTO(out_unlock, rc);
277 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
278 (llh->llh_count == 1) &&
279 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
280 (loghandle->u.phd.phd_cat_handle != NULL &&
281 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
283 /* never try to destroy it again */
284 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
285 rc = llog_trans_destroy(env, loghandle, th);
287 /* Sigh, can not destroy the final plain llog, but
288 * the bitmap has been clearly, so the record can not
289 * be accessed anymore, let's return 0 for now, and
290 * the orphan will be handled by LFSCK. */
291 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
292 loghandle->lgh_ctxt->loc_obd->obd_name,
293 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
294 GOTO(out_unlock, rc = 0);
300 mutex_unlock(&loghandle->lgh_hdr_mutex);
301 up_write(&loghandle->lgh_lock);
303 rc1 = dt_trans_stop(env, dt, th);
306 if (rc < 0 && subtract_count) {
307 mutex_lock(&loghandle->lgh_hdr_mutex);
308 loghandle->lgh_hdr->llh_count++;
309 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
310 mutex_unlock(&loghandle->lgh_hdr_mutex);
315 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
316 const struct obd_uuid *uuid)
318 struct llog_operations *lop;
322 rc = llog_handle2ops(handle, &lop);
326 if (lop->lop_read_header == NULL)
329 rc = lop->lop_read_header(env, handle);
330 if (rc == LLOG_EEMPTY) {
331 struct llog_log_hdr *llh = handle->lgh_hdr;
333 /* lrh_len should be initialized in llog_init_handle */
334 handle->lgh_last_idx = 0; /* header is record with index 0 */
335 llh->llh_count = 1; /* for the header record */
336 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
337 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
338 LLOG_MIN_CHUNK_SIZE);
339 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
340 llh->llh_hdr.lrh_index = 0;
341 llh->llh_timestamp = ktime_get_real_seconds();
343 memcpy(&llh->llh_tgtuuid, uuid,
344 sizeof(llh->llh_tgtuuid));
345 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
346 /* Since update llog header might also call this function,
347 * let's reset the bitmap to 0 here */
348 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
349 llh->llh_bitmap_offset -
350 sizeof(llh->llh_tail));
351 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
352 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
353 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
358 EXPORT_SYMBOL(llog_read_header);
360 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
361 int flags, struct obd_uuid *uuid)
363 struct llog_log_hdr *llh;
364 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
366 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
369 LASSERT(handle->lgh_hdr == NULL);
371 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
372 OBD_ALLOC_LARGE(llh, chunk_size);
376 handle->lgh_hdr = llh;
377 handle->lgh_hdr_size = chunk_size;
378 /* first assign flags to use llog_client_ops */
379 llh->llh_flags = flags;
380 rc = llog_read_header(env, handle, uuid);
382 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
383 flags & LLOG_F_IS_CAT) ||
384 (llh->llh_flags & LLOG_F_IS_CAT &&
385 flags & LLOG_F_IS_PLAIN))) {
386 CERROR("%s: llog type is %s but initializing %s\n",
387 handle->lgh_ctxt->loc_obd->obd_name,
388 llh->llh_flags & LLOG_F_IS_CAT ?
390 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
391 GOTO(out, rc = -EINVAL);
392 } else if (llh->llh_flags &
393 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
395 * it is possible to open llog without specifying llog
396 * type so it is taken from llh_flags
398 flags = llh->llh_flags;
400 /* for some reason the llh_flags has no type set */
401 CERROR("llog type is not specified!\n");
402 GOTO(out, rc = -EINVAL);
405 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
406 CERROR("%s: llog uuid mismatch: %s/%s\n",
407 handle->lgh_ctxt->loc_obd->obd_name,
409 (char *)llh->llh_tgtuuid.uuid);
410 GOTO(out, rc = -EEXIST);
413 if (flags & LLOG_F_IS_CAT) {
414 LASSERT(list_empty(&handle->u.chd.chd_head));
415 INIT_LIST_HEAD(&handle->u.chd.chd_head);
416 llh->llh_size = sizeof(struct llog_logid_rec);
417 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
418 } else if (!(flags & LLOG_F_IS_PLAIN)) {
419 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
420 handle->lgh_ctxt->loc_obd->obd_name,
421 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
424 llh->llh_flags |= fmt;
427 OBD_FREE_LARGE(llh, chunk_size);
428 handle->lgh_hdr = NULL;
432 EXPORT_SYMBOL(llog_init_handle);
434 static int llog_process_thread(void *arg)
436 struct llog_process_info *lpi = arg;
437 struct llog_handle *loghandle = lpi->lpi_loghandle;
438 struct llog_log_hdr *llh = loghandle->lgh_hdr;
439 struct llog_process_cat_data *cd = lpi->lpi_catdata;
440 struct llog_thread_info *lti;
444 int rc = 0, index = 1, last_index;
446 int last_called_index = 0;
447 bool repeated = false;
454 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
456 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
457 /* expect chunk_size to be power of two */
458 LASSERT(is_power_of_2(chunk_size));
460 OBD_ALLOC_LARGE(buf, chunk_size);
462 lpi->lpi_rc = -ENOMEM;
467 last_called_index = cd->lpcd_first_idx;
468 index = cd->lpcd_first_idx + 1;
470 if (cd != NULL && cd->lpcd_last_idx)
471 last_index = cd->lpcd_last_idx;
473 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
476 struct llog_rec_hdr *rec;
477 off_t chunk_offset = 0;
478 unsigned int buf_offset = 0;
483 /* skip records not set in bitmap */
484 while (index <= last_index &&
485 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
488 /* There are no indices prior the last_index */
489 if (index > last_index)
492 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
496 /* get the buf with our target record; avoid old garbage */
497 memset(buf, 0, chunk_size);
498 /* the record index for outdated chunk data */
499 /* it is safe to process buffer until saved lgh_last_idx */
500 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
501 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
502 index, &cur_offset, buf, chunk_size);
504 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
505 " buf_offset %u, rc = %d\n", cur_offset,
506 (__u64)chunk_offset, buf_offset, rc);
507 /* we`ve tried to reread the chunk, but there is no
509 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
515 /* NB: after llog_next_block() call the cur_offset is the
516 * offset of the next block after read one.
517 * The absolute offset of the current chunk is calculated
518 * from cur_offset value and stored in chunk_offset variable.
520 if ((cur_offset & (chunk_size - 1)) != 0) {
521 partial_chunk = true;
522 chunk_offset = cur_offset & ~(chunk_size - 1);
524 partial_chunk = false;
525 chunk_offset = cur_offset - chunk_size;
528 /* NB: when rec->lrh_len is accessed it is already swabbed
529 * since it is used at the "end" of the loop and the rec
530 * swabbing is done at the beginning of the loop. */
531 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
532 (char *)rec < buf + chunk_size;
533 rec = llog_rec_hdr_next(rec)) {
535 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
538 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
539 lustre_swab_llog_rec(rec);
541 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
542 rec->lrh_type, rec->lrh_index);
544 if (index == (synced_idx + 1) &&
545 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
548 /* the bitmap could be changed during processing
549 * records from the chunk. For wrapped catalog
550 * it means we can read deleted record and try to
551 * process it. Check this case and reread the chunk.
552 * It is safe to process to lh_last_idx, including
553 * lh_last_idx if it was synced. We can not do <=
554 * comparison, cause for wrapped catalog lgh_last_idx
555 * could be less than index. So we detect last index
556 * for processing as index == lh_last_idx+1. But when
557 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
558 * the first processing index is llh_cat_idx+1.
561 if ((index == lh_last_idx && synced_idx != index) ||
562 (index == (lh_last_idx + 1) &&
563 !(index == (llh->llh_cat_idx + 1) &&
564 (llh->llh_flags & LLOG_F_IS_CAT))) ||
565 (rec->lrh_index == 0 && !repeated)) {
567 /* save offset inside buffer for the re-read */
568 buf_offset = (char *)rec - (char *)buf;
569 cur_offset = chunk_offset;
571 /* We need to be sure lgh_last_idx
572 * record was saved to disk
574 down_read(&loghandle->lgh_last_sem);
575 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
576 up_read(&loghandle->lgh_last_sem);
577 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
584 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
585 CWARN("%s: invalid length %d in llog "DFID
586 "record for index %d/%d\n",
587 loghandle->lgh_ctxt->loc_obd->obd_name,
589 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
590 rec->lrh_index, index);
592 GOTO(out, rc = -EINVAL);
595 if (rec->lrh_index < index) {
596 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
601 if (rec->lrh_index != index) {
602 CERROR("%s: "DFID" Invalid record: index %u"
603 " but expected %u\n",
604 loghandle->lgh_ctxt->loc_obd->obd_name,
605 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
606 rec->lrh_index, index);
607 GOTO(out, rc = -ERANGE);
611 "lrh_index: %d lrh_len: %d (%d remains)\n",
612 rec->lrh_index, rec->lrh_len,
613 (int)(buf + chunk_size - (char *)rec));
615 /* lgh_cur_offset is used only at llog_test_3 */
616 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
619 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
620 index == lh_last_idx &&
621 cfs_fail_val == (unsigned int)
622 (loghandle->lgh_id.lgl_oi.oi.oi_id &
624 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
626 /* if set, process the callback on this record */
627 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
628 struct llog_cookie *lgc;
632 CDEBUG(D_OTHER, "index: %d, lh_last_idx: %d "
633 "synced_idx: %d lgh_last_idx: %d\n",
634 index, lh_last_idx, synced_idx,
635 loghandle->lgh_last_idx);
638 lgc = <i->lgi_cookie;
639 /* store lu_env for recursive calls */
640 tmp_off = lgc->lgc_offset;
641 tmp_idx = lgc->lgc_index;
643 lgc->lgc_offset = (char *)rec -
644 (char *)buf + chunk_offset;
645 lgc->lgc_index = rec->lrh_index;
647 /* using lu_env for passing record offset to
648 * llog_write through various callbacks */
649 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
651 last_called_index = index;
654 lgc->lgc_offset = tmp_off;
655 lgc->lgc_index = tmp_idx;
658 if (rc == LLOG_PROC_BREAK) {
660 } else if (rc == LLOG_DEL_RECORD) {
661 rc = llog_cancel_rec(lpi->lpi_env,
667 /* some stupid callbacks directly cancel records
668 * and delete llog. Check it and stop
670 if (loghandle->lgh_hdr == NULL ||
671 loghandle->lgh_hdr->llh_count == 1)
674 /* exit if the last index is reached */
675 if (index >= last_index)
683 cd->lpcd_last_idx = last_called_index;
685 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
686 if (dt_object_remote(loghandle->lgh_obj)) {
687 /* If it is remote object, then -EIO might means
688 * disconnection or eviction, let's return -EAGAIN,
689 * so for update recovery log processing, it will
690 * retry until the umount or abort recovery, see
691 * lod_sub_recovery_thread() */
692 CERROR("%s retry remote llog process\n",
693 loghandle->lgh_ctxt->loc_obd->obd_name);
696 /* something bad happened to the processing of a local
697 * llog file, probably I/O error or the log got
698 * corrupted to be able to finally release the log we
699 * discard any remaining bits in the header */
700 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
701 " %s index %d count %d\n",
702 loghandle->lgh_ctxt->loc_obd->obd_name,
703 POSTID(&loghandle->lgh_id.lgl_oi),
704 loghandle->lgh_id.lgl_ogen,
705 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
706 "plain"), index, llh->llh_count);
708 while (index <= last_index) {
709 if (ext2_test_bit(index,
710 LLOG_HDR_BITMAP(llh)) != 0)
711 llog_cancel_rec(lpi->lpi_env, loghandle,
719 OBD_FREE_LARGE(buf, chunk_size);
724 static int llog_process_thread_daemonize(void *arg)
726 struct llog_process_info *lpi = arg;
729 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
731 task_lock(lpi->lpi_reftask);
732 new_ns = lpi->lpi_reftask->nsproxy;
733 if (curr_ns != new_ns) {
736 current->nsproxy = new_ns;
737 /* XXX: we should call put_nsproxy() instead of
738 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
739 * used outside of the kernel itself, because it calls
740 * free_nsproxy() which is not exported by the kernel
741 * (defined in kernel/nsproxy.c) */
742 atomic_dec(&curr_ns->count);
744 task_unlock(lpi->lpi_reftask);
748 /* client env has no keys, tags is just 0 */
749 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
754 rc = llog_process_thread(arg);
758 complete(&lpi->lpi_completion);
762 int llog_process_or_fork(const struct lu_env *env,
763 struct llog_handle *loghandle,
764 llog_cb_t cb, void *data, void *catdata, bool fork)
766 struct llog_process_info *lpi;
773 CERROR("cannot alloc pointer\n");
776 lpi->lpi_loghandle = loghandle;
778 lpi->lpi_cbdata = data;
779 lpi->lpi_catdata = catdata;
782 struct task_struct *task;
784 /* The new thread can't use parent env,
785 * init the new one in llog_process_thread_daemonize. */
787 init_completion(&lpi->lpi_completion);
788 /* take reference to current, so that
789 * llog_process_thread_daemonize() can use it to switch to
790 * namespace associated with current */
791 lpi->lpi_reftask = current;
792 task = kthread_run(llog_process_thread_daemonize, lpi,
793 "llog_process_thread");
796 CERROR("%s: cannot start thread: rc = %d\n",
797 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
800 wait_for_completion(&lpi->lpi_completion);
803 llog_process_thread(lpi);
811 EXPORT_SYMBOL(llog_process_or_fork);
813 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
814 llog_cb_t cb, void *data, void *catdata)
817 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
818 return rc == LLOG_DEL_PLAIN ? 0 : rc;
820 EXPORT_SYMBOL(llog_process);
822 int llog_reverse_process(const struct lu_env *env,
823 struct llog_handle *loghandle, llog_cb_t cb,
824 void *data, void *catdata)
826 struct llog_log_hdr *llh = loghandle->lgh_hdr;
827 struct llog_process_cat_data *cd = catdata;
829 int rc = 0, first_index = 1, index, idx;
830 __u32 chunk_size = llh->llh_hdr.lrh_len;
833 OBD_ALLOC_LARGE(buf, chunk_size);
838 first_index = cd->lpcd_first_idx + 1;
839 if (cd != NULL && cd->lpcd_last_idx)
840 index = cd->lpcd_last_idx;
842 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
845 struct llog_rec_hdr *rec;
846 struct llog_rec_tail *tail;
848 /* skip records not set in bitmap */
849 while (index >= first_index &&
850 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
853 LASSERT(index >= first_index - 1);
854 if (index == first_index - 1)
857 /* get the buf with our target record; avoid old garbage */
858 memset(buf, 0, chunk_size);
859 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
864 idx = rec->lrh_index;
865 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
866 while (idx < index) {
867 rec = (void *)rec + rec->lrh_len;
868 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
869 lustre_swab_llog_rec(rec);
872 LASSERT(idx == index);
873 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
875 /* process records in buffer, starting where we found one */
876 while ((void *)tail > buf) {
877 if (tail->lrt_index == 0)
878 GOTO(out, rc = 0); /* no more records */
880 /* if set, process the callback on this record */
881 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
882 rec = (void *)tail - tail->lrt_len +
885 rc = cb(env, loghandle, rec, data);
886 if (rc == LLOG_PROC_BREAK) {
888 } else if (rc == LLOG_DEL_RECORD) {
889 rc = llog_cancel_rec(env, loghandle,
896 /* previous record, still in buffer? */
898 if (index < first_index)
900 tail = (void *)tail - tail->lrt_len;
906 OBD_FREE_LARGE(buf, chunk_size);
909 EXPORT_SYMBOL(llog_reverse_process);
915 * llog_open - open llog, may not exist
916 * llog_exist - check if llog exists
917 * llog_close - close opened llog, pair for open, frees llog_handle
918 * llog_declare_create - declare llog creation
919 * llog_create - create new llog on disk, need transaction handle
920 * llog_declare_write_rec - declaration of llog write
921 * llog_write_rec - write llog record on disk, need transaction handle
922 * llog_declare_add - declare llog catalog record addition
923 * llog_add - add llog record in catalog, need transaction handle
925 int llog_exist(struct llog_handle *loghandle)
927 struct llog_operations *lop;
932 rc = llog_handle2ops(loghandle, &lop);
935 if (lop->lop_exist == NULL)
938 rc = lop->lop_exist(loghandle);
941 EXPORT_SYMBOL(llog_exist);
943 int llog_declare_create(const struct lu_env *env,
944 struct llog_handle *loghandle, struct thandle *th)
946 struct llog_operations *lop;
951 rc = llog_handle2ops(loghandle, &lop);
954 if (lop->lop_declare_create == NULL)
957 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
959 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
960 rc = lop->lop_declare_create(env, loghandle, th);
962 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
966 int llog_create(const struct lu_env *env, struct llog_handle *handle,
969 struct llog_operations *lop;
974 rc = llog_handle2ops(handle, &lop);
977 if (lop->lop_create == NULL)
980 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
982 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
983 rc = lop->lop_create(env, handle, th);
985 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
989 int llog_declare_write_rec(const struct lu_env *env,
990 struct llog_handle *handle,
991 struct llog_rec_hdr *rec, int idx,
994 struct llog_operations *lop;
999 rc = llog_handle2ops(handle, &lop);
1003 if (lop->lop_declare_write_rec == NULL)
1004 RETURN(-EOPNOTSUPP);
1006 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1008 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1009 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1011 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1015 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1016 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1017 int idx, struct thandle *th)
1019 struct llog_operations *lop;
1020 int raised, rc, buflen;
1024 /* API sanity checks */
1025 if (handle == NULL) {
1026 CERROR("loghandle is missed\n");
1028 } else if (handle->lgh_obj == NULL) {
1029 CERROR("loghandle %p with NULL object\n",
1032 } else if (th == NULL) {
1033 CERROR("%s: missed transaction handle\n",
1034 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1036 } else if (handle->lgh_hdr == NULL) {
1037 CERROR("%s: loghandle %p with no header\n",
1038 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1043 rc = llog_handle2ops(handle, &lop);
1047 if (lop->lop_write_rec == NULL)
1048 RETURN(-EOPNOTSUPP);
1050 buflen = rec->lrh_len;
1051 LASSERT(cfs_size_round(buflen) == buflen);
1053 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1055 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1056 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1058 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1062 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1063 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1070 if (lgh->lgh_logops->lop_add == NULL)
1071 RETURN(-EOPNOTSUPP);
1073 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1075 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1076 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1078 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1081 EXPORT_SYMBOL(llog_add);
1083 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1084 struct llog_rec_hdr *rec, struct thandle *th)
1090 if (lgh->lgh_logops->lop_declare_add == NULL)
1091 RETURN(-EOPNOTSUPP);
1093 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1095 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1096 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1098 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1101 EXPORT_SYMBOL(llog_declare_add);
1104 * Helper function to open llog or create it if doesn't exist.
1105 * It hides all transaction handling from caller.
1107 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1108 struct llog_handle **res, struct llog_logid *logid,
1111 struct dt_device *d;
1117 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1121 if (llog_exist(*res))
1124 LASSERT((*res)->lgh_obj != NULL);
1126 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1128 if (unlikely(unlikely(d->dd_rdonly)))
1131 th = dt_trans_create(env, d);
1133 GOTO(out, rc = PTR_ERR(th));
1135 /* Create update llog object synchronously, which
1136 * happens during inialization process see
1137 * lod_sub_prep_llog(), to make sure the update
1138 * llog object is created before corss-MDT writing
1139 * updates into the llog object */
1140 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1143 th->th_wait_submit = 1;
1144 rc = llog_declare_create(env, *res, th);
1146 rc = dt_trans_start_local(env, d, th);
1148 rc = llog_create(env, *res, th);
1150 dt_trans_stop(env, d, th);
1153 llog_close(env, *res);
1156 EXPORT_SYMBOL(llog_open_create);
1159 * Helper function to delete existent llog.
1161 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1162 struct llog_logid *logid, char *name)
1164 struct llog_handle *handle;
1169 /* nothing to erase */
1170 if (name == NULL && logid == NULL)
1173 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1177 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1179 rc = llog_destroy(env, handle);
1181 rc2 = llog_close(env, handle);
1186 EXPORT_SYMBOL(llog_erase);
1189 * Helper function for write record in llog.
1190 * It hides all transaction handling from caller.
1191 * Valid only with local llog.
1193 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1194 struct llog_rec_hdr *rec, int idx)
1196 struct dt_device *dt;
1204 LASSERT(loghandle->lgh_ctxt);
1205 LASSERT(loghandle->lgh_obj != NULL);
1207 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1209 if (unlikely(unlikely(dt->dd_rdonly)))
1212 th = dt_trans_create(env, dt);
1214 RETURN(PTR_ERR(th));
1216 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1218 GOTO(out_trans, rc);
1220 th->th_wait_submit = 1;
1221 rc = dt_trans_start_local(env, dt, th);
1223 GOTO(out_trans, rc);
1225 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1227 down_write(&loghandle->lgh_lock);
1229 struct llog_thread_info *lti = llog_info(env);
1231 /* cookie comes from llog_process_thread */
1232 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1233 rec->lrh_index, th);
1234 /* upper layer didn`t pass cookie so change rc */
1235 rc = (rc == 1 ? 0 : rc);
1237 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1240 up_write(&loghandle->lgh_lock);
1242 dt_trans_stop(env, dt, th);
1245 EXPORT_SYMBOL(llog_write);
1247 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1248 struct llog_handle **lgh, struct llog_logid *logid,
1249 char *name, enum llog_open_param open_param)
1257 LASSERT(ctxt->loc_logops);
1259 if (ctxt->loc_logops->lop_open == NULL) {
1261 RETURN(-EOPNOTSUPP);
1264 *lgh = llog_alloc_handle();
1267 (*lgh)->lgh_ctxt = ctxt;
1268 (*lgh)->lgh_logops = ctxt->loc_logops;
1270 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1272 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1273 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1275 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1277 llog_free_handle(*lgh);
1282 EXPORT_SYMBOL(llog_open);
1284 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1286 struct llog_operations *lop;
1291 rc = llog_handle2ops(loghandle, &lop);
1294 if (lop->lop_close == NULL)
1295 GOTO(out, rc = -EOPNOTSUPP);
1296 rc = lop->lop_close(env, loghandle);
1298 llog_handle_put(loghandle);
1301 EXPORT_SYMBOL(llog_close);
1304 * Helper function to get the llog size in records. It is used by MGS
1305 * mostly to check that config llog exists and contains data.
1307 * \param[in] env execution environment
1308 * \param[in] ctxt llog context
1309 * \param[in] name llog name
1311 * \retval true if there are records in llog besides a header
1312 * \retval false on error or llog without records
1314 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1317 struct llog_handle *llh;
1320 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1322 if (likely(rc == -ENOENT))
1327 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1329 GOTO(out_close, rc);
1330 rc = llog_get_size(llh);
1333 llog_close(env, llh);
1335 /* The header is record 1, the llog is still considered as empty
1336 * if there is only header */
1339 EXPORT_SYMBOL(llog_is_empty);
1341 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1342 struct llog_rec_hdr *rec, void *data)
1344 struct llog_handle *copy_llh = data;
1346 /* Append all records */
1347 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1350 /* backup plain llog */
1351 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1352 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1353 char *name, char *backup)
1355 struct llog_handle *llh, *bllh;
1360 /* open original log */
1361 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1363 /* the -ENOENT case is also reported to the caller
1364 * but silently so it should handle that if needed.
1367 CERROR("%s: failed to open log %s: rc = %d\n",
1368 obd->obd_name, name, rc);
1372 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1374 GOTO(out_close, rc);
1376 /* Make sure there's no old backup log */
1377 rc = llog_erase(env, bctxt, NULL, backup);
1378 if (rc < 0 && rc != -ENOENT)
1379 GOTO(out_close, rc);
1381 /* open backup log */
1382 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1384 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1385 obd->obd_name, backup, rc);
1386 GOTO(out_close, rc);
1389 /* check that backup llog is not the same object as original one */
1390 if (llh->lgh_obj == bllh->lgh_obj) {
1391 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1392 obd->obd_name, name, backup, llh->lgh_obj,
1394 GOTO(out_backup, rc = -EEXIST);
1397 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1399 GOTO(out_backup, rc);
1401 /* Copy log record by record */
1402 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1405 CERROR("%s: failed to backup log %s: rc = %d\n",
1406 obd->obd_name, name, rc);
1408 llog_close(env, bllh);
1410 llog_close(env, llh);
1413 EXPORT_SYMBOL(llog_backup);
1415 /* Get size of llog */
1416 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1421 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1423 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1424 llh->lgh_ctxt->loc_obd->obd_name,
1425 PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1431 EXPORT_SYMBOL(llog_size);