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_arr_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 records, first %d in log "DFID"\n",
220 num, index[0], PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
222 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
224 if (unlikely(unlikely(dt->dd_rdonly)))
227 th = dt_trans_create(env, dt);
231 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, 0, th);
235 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
236 rc = llog_declare_destroy(env, loghandle, th);
241 th->th_wait_submit = 1;
242 rc = dt_trans_start_local(env, dt, th);
246 down_write(&loghandle->lgh_lock);
248 mutex_lock(&loghandle->lgh_hdr_mutex);
249 for (i = 0; i < num; ++i) {
251 CERROR("Can't cancel index 0 which is header\n");
252 GOTO(out_unlock, rc = -EINVAL);
254 if (!ext2_clear_bit(index[i], LLOG_HDR_BITMAP(llh))) {
255 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n",
257 GOTO(out_unlock, rc = -ENOENT);
260 loghandle->lgh_hdr->llh_count -= num;
261 subtract_count = true;
263 /* Since llog_process_thread use lgi_cookie, it`s better to save them
264 * and restore after using
266 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
267 /* Pass this index to llog_osd_write_rec(), which will use the index
268 * to only update the necesary bitmap. */
269 lgi->lgi_cookie.lgc_index = index[0];
271 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, (num != 1 ? NULL :
272 &lgi->lgi_cookie), LLOG_HEADER_IDX, th);
273 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
276 GOTO(out_unlock, rc);
278 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
279 (llh->llh_count == 1) &&
280 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
281 (loghandle->u.phd.phd_cat_handle != NULL &&
282 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
284 /* never try to destroy it again */
285 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
286 rc = llog_trans_destroy(env, loghandle, th);
288 /* Sigh, can not destroy the final plain llog, but
289 * the bitmap has been clearly, so the record can not
290 * be accessed anymore, let's return 0 for now, and
291 * the orphan will be handled by LFSCK. */
292 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
293 loghandle->lgh_ctxt->loc_obd->obd_name,
294 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
295 GOTO(out_unlock, rc = 0);
301 mutex_unlock(&loghandle->lgh_hdr_mutex);
302 up_write(&loghandle->lgh_lock);
304 rc1 = dt_trans_stop(env, dt, th);
308 mutex_lock(&loghandle->lgh_hdr_mutex);
310 loghandle->lgh_hdr->llh_count += num;
311 for (i = i - 1; i >= 0; i--)
312 ext2_set_bit(index[i], LLOG_HDR_BITMAP(llh));
313 mutex_unlock(&loghandle->lgh_hdr_mutex);
318 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
321 return llog_cancel_arr_rec(env, loghandle, 1, &index);
324 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
325 const struct obd_uuid *uuid)
327 struct llog_operations *lop;
331 rc = llog_handle2ops(handle, &lop);
335 if (lop->lop_read_header == NULL)
338 rc = lop->lop_read_header(env, handle);
339 if (rc == LLOG_EEMPTY) {
340 struct llog_log_hdr *llh = handle->lgh_hdr;
342 /* lrh_len should be initialized in llog_init_handle */
343 handle->lgh_last_idx = 0; /* header is record with index 0 */
344 llh->llh_count = 1; /* for the header record */
345 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
346 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
347 LLOG_MIN_CHUNK_SIZE);
348 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
349 llh->llh_hdr.lrh_index = 0;
350 llh->llh_timestamp = ktime_get_real_seconds();
352 memcpy(&llh->llh_tgtuuid, uuid,
353 sizeof(llh->llh_tgtuuid));
354 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
355 /* Since update llog header might also call this function,
356 * let's reset the bitmap to 0 here */
357 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
358 llh->llh_bitmap_offset -
359 sizeof(llh->llh_tail));
360 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
361 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
362 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
367 EXPORT_SYMBOL(llog_read_header);
369 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
370 int flags, struct obd_uuid *uuid)
372 struct llog_log_hdr *llh;
373 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
375 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
378 LASSERT(handle->lgh_hdr == NULL);
380 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
381 OBD_ALLOC_LARGE(llh, chunk_size);
385 handle->lgh_hdr = llh;
386 handle->lgh_hdr_size = chunk_size;
387 /* first assign flags to use llog_client_ops */
388 llh->llh_flags = flags;
389 rc = llog_read_header(env, handle, uuid);
391 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
392 flags & LLOG_F_IS_CAT) ||
393 (llh->llh_flags & LLOG_F_IS_CAT &&
394 flags & LLOG_F_IS_PLAIN))) {
395 CERROR("%s: llog type is %s but initializing %s\n",
396 handle->lgh_ctxt->loc_obd->obd_name,
397 llh->llh_flags & LLOG_F_IS_CAT ?
399 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
400 GOTO(out, rc = -EINVAL);
401 } else if (llh->llh_flags &
402 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
404 * it is possible to open llog without specifying llog
405 * type so it is taken from llh_flags
407 flags = llh->llh_flags;
409 /* for some reason the llh_flags has no type set */
410 CERROR("llog type is not specified!\n");
411 GOTO(out, rc = -EINVAL);
414 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
415 CERROR("%s: llog uuid mismatch: %s/%s\n",
416 handle->lgh_ctxt->loc_obd->obd_name,
418 (char *)llh->llh_tgtuuid.uuid);
419 GOTO(out, rc = -EEXIST);
422 if (flags & LLOG_F_IS_CAT) {
423 LASSERT(list_empty(&handle->u.chd.chd_head));
424 INIT_LIST_HEAD(&handle->u.chd.chd_head);
425 llh->llh_size = sizeof(struct llog_logid_rec);
426 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
427 } else if (!(flags & LLOG_F_IS_PLAIN)) {
428 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
429 handle->lgh_ctxt->loc_obd->obd_name,
430 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
433 llh->llh_flags |= fmt;
436 OBD_FREE_LARGE(llh, chunk_size);
437 handle->lgh_hdr = NULL;
441 EXPORT_SYMBOL(llog_init_handle);
443 static int llog_process_thread(void *arg)
445 struct llog_process_info *lpi = arg;
446 struct llog_handle *loghandle = lpi->lpi_loghandle;
447 struct llog_log_hdr *llh = loghandle->lgh_hdr;
448 struct llog_process_cat_data *cd = lpi->lpi_catdata;
449 struct llog_thread_info *lti;
453 int rc = 0, index = 1, last_index;
455 int last_called_index = 0;
456 bool repeated = false;
463 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
465 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
466 /* expect chunk_size to be power of two */
467 LASSERT(is_power_of_2(chunk_size));
469 OBD_ALLOC_LARGE(buf, chunk_size);
471 lpi->lpi_rc = -ENOMEM;
476 last_called_index = cd->lpcd_first_idx;
477 index = cd->lpcd_first_idx + 1;
479 if (cd != NULL && cd->lpcd_last_idx)
480 last_index = cd->lpcd_last_idx;
482 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
485 struct llog_rec_hdr *rec;
486 off_t chunk_offset = 0;
487 unsigned int buf_offset = 0;
492 /* skip records not set in bitmap */
493 while (index <= last_index &&
494 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
497 /* There are no indices prior the last_index */
498 if (index > last_index)
501 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
505 /* get the buf with our target record; avoid old garbage */
506 memset(buf, 0, chunk_size);
507 /* the record index for outdated chunk data */
508 /* it is safe to process buffer until saved lgh_last_idx */
509 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
510 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
511 index, &cur_offset, buf, chunk_size);
513 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
514 " buf_offset %u, rc = %d\n", cur_offset,
515 (__u64)chunk_offset, buf_offset, rc);
516 /* we`ve tried to reread the chunk, but there is no
518 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
524 /* NB: after llog_next_block() call the cur_offset is the
525 * offset of the next block after read one.
526 * The absolute offset of the current chunk is calculated
527 * from cur_offset value and stored in chunk_offset variable.
529 if ((cur_offset & (chunk_size - 1)) != 0) {
530 partial_chunk = true;
531 chunk_offset = cur_offset & ~(chunk_size - 1);
533 partial_chunk = false;
534 chunk_offset = cur_offset - chunk_size;
537 /* NB: when rec->lrh_len is accessed it is already swabbed
538 * since it is used at the "end" of the loop and the rec
539 * swabbing is done at the beginning of the loop. */
540 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
541 (char *)rec < buf + chunk_size;
542 rec = llog_rec_hdr_next(rec)) {
544 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
547 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
548 lustre_swab_llog_rec(rec);
550 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
551 rec->lrh_type, rec->lrh_index);
553 if (index == (synced_idx + 1) &&
554 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
557 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
558 cfs_fail_val == (unsigned int)
559 (loghandle->lgh_id.lgl_oi.oi.oi_id &
561 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
564 /* the bitmap could be changed during processing
565 * records from the chunk. For wrapped catalog
566 * it means we can read deleted record and try to
567 * process it. Check this case and reread the chunk.
568 * It is safe to process to lh_last_idx, including
569 * lh_last_idx if it was synced. We can not do <=
570 * comparison, cause for wrapped catalog lgh_last_idx
571 * could be less than index. So we detect last index
572 * for processing as index == lh_last_idx+1. But when
573 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
574 * the first processing index is llh_cat_idx+1.
577 if ((index == lh_last_idx && synced_idx != index) ||
578 (index == (lh_last_idx + 1) &&
579 !(index == (llh->llh_cat_idx + 1) &&
580 (llh->llh_flags & LLOG_F_IS_CAT))) ||
581 (rec->lrh_index == 0 && !repeated)) {
583 /* save offset inside buffer for the re-read */
584 buf_offset = (char *)rec - (char *)buf;
585 cur_offset = chunk_offset;
587 /* We need to be sure lgh_last_idx
588 * record was saved to disk
590 down_read(&loghandle->lgh_last_sem);
591 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
592 up_read(&loghandle->lgh_last_sem);
593 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
600 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
601 CWARN("%s: invalid length %d in llog "DFID
602 "record for index %d/%d\n",
603 loghandle->lgh_ctxt->loc_obd->obd_name,
605 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
606 rec->lrh_index, index);
608 GOTO(out, rc = -EINVAL);
611 if (rec->lrh_index < index) {
612 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
617 if (rec->lrh_index != index) {
618 CERROR("%s: "DFID" Invalid record: index %u"
619 " but expected %u\n",
620 loghandle->lgh_ctxt->loc_obd->obd_name,
621 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
622 rec->lrh_index, index);
623 GOTO(out, rc = -ERANGE);
627 "lrh_index: %d lrh_len: %d (%d remains)\n",
628 rec->lrh_index, rec->lrh_len,
629 (int)(buf + chunk_size - (char *)rec));
631 /* lgh_cur_offset is used only at llog_test_3 */
632 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
635 /* if set, process the callback on this record */
636 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
637 struct llog_cookie *lgc;
641 CDEBUG(D_OTHER, "index: %d, lh_last_idx: %d "
642 "synced_idx: %d lgh_last_idx: %d\n",
643 index, lh_last_idx, synced_idx,
644 loghandle->lgh_last_idx);
647 lgc = <i->lgi_cookie;
648 /* store lu_env for recursive calls */
649 tmp_off = lgc->lgc_offset;
650 tmp_idx = lgc->lgc_index;
652 lgc->lgc_offset = (char *)rec -
653 (char *)buf + chunk_offset;
654 lgc->lgc_index = rec->lrh_index;
656 /* using lu_env for passing record offset to
657 * llog_write through various callbacks */
658 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
660 last_called_index = index;
663 lgc->lgc_offset = tmp_off;
664 lgc->lgc_index = tmp_idx;
667 if (rc == LLOG_PROC_BREAK) {
669 } else if (rc == LLOG_DEL_RECORD) {
670 rc = llog_cancel_rec(lpi->lpi_env,
676 /* some stupid callbacks directly cancel records
677 * and delete llog. Check it and stop
679 if (loghandle->lgh_hdr == NULL ||
680 loghandle->lgh_hdr->llh_count == 1)
683 /* exit if the last index is reached */
684 if (index >= last_index)
692 cd->lpcd_last_idx = last_called_index;
694 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
695 if (dt_object_remote(loghandle->lgh_obj)) {
696 /* If it is remote object, then -EIO might means
697 * disconnection or eviction, let's return -EAGAIN,
698 * so for update recovery log processing, it will
699 * retry until the umount or abort recovery, see
700 * lod_sub_recovery_thread() */
701 CERROR("%s retry remote llog process\n",
702 loghandle->lgh_ctxt->loc_obd->obd_name);
705 /* something bad happened to the processing of a local
706 * llog file, probably I/O error or the log got
707 * corrupted to be able to finally release the log we
708 * discard any remaining bits in the header */
709 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
710 " %s index %d count %d\n",
711 loghandle->lgh_ctxt->loc_obd->obd_name,
712 POSTID(&loghandle->lgh_id.lgl_oi),
713 loghandle->lgh_id.lgl_ogen,
714 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
715 "plain"), index, llh->llh_count);
717 while (index <= last_index) {
718 if (ext2_test_bit(index,
719 LLOG_HDR_BITMAP(llh)) != 0)
720 llog_cancel_rec(lpi->lpi_env, loghandle,
728 OBD_FREE_LARGE(buf, chunk_size);
733 static int llog_process_thread_daemonize(void *arg)
735 struct llog_process_info *lpi = arg;
738 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
740 task_lock(lpi->lpi_reftask);
741 new_ns = lpi->lpi_reftask->nsproxy;
742 if (curr_ns != new_ns) {
745 current->nsproxy = new_ns;
746 /* XXX: we should call put_nsproxy() instead of
747 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
748 * used outside of the kernel itself, because it calls
749 * free_nsproxy() which is not exported by the kernel
750 * (defined in kernel/nsproxy.c) */
751 atomic_dec(&curr_ns->count);
753 task_unlock(lpi->lpi_reftask);
757 /* client env has no keys, tags is just 0 */
758 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
763 rc = llog_process_thread(arg);
767 complete(&lpi->lpi_completion);
771 int llog_process_or_fork(const struct lu_env *env,
772 struct llog_handle *loghandle,
773 llog_cb_t cb, void *data, void *catdata, bool fork)
775 struct llog_process_info *lpi;
782 CERROR("cannot alloc pointer\n");
785 lpi->lpi_loghandle = loghandle;
787 lpi->lpi_cbdata = data;
788 lpi->lpi_catdata = catdata;
791 struct task_struct *task;
793 /* The new thread can't use parent env,
794 * init the new one in llog_process_thread_daemonize. */
796 init_completion(&lpi->lpi_completion);
797 /* take reference to current, so that
798 * llog_process_thread_daemonize() can use it to switch to
799 * namespace associated with current */
800 lpi->lpi_reftask = current;
801 task = kthread_run(llog_process_thread_daemonize, lpi,
802 "llog_process_thread");
805 CERROR("%s: cannot start thread: rc = %d\n",
806 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
809 wait_for_completion(&lpi->lpi_completion);
812 llog_process_thread(lpi);
820 EXPORT_SYMBOL(llog_process_or_fork);
822 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
823 llog_cb_t cb, void *data, void *catdata)
826 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
827 return rc == LLOG_DEL_PLAIN ? 0 : rc;
829 EXPORT_SYMBOL(llog_process);
831 int llog_reverse_process(const struct lu_env *env,
832 struct llog_handle *loghandle, llog_cb_t cb,
833 void *data, void *catdata)
835 struct llog_log_hdr *llh = loghandle->lgh_hdr;
836 struct llog_process_cat_data *cd = catdata;
838 int rc = 0, first_index = 1, index, idx;
839 __u32 chunk_size = llh->llh_hdr.lrh_len;
842 OBD_ALLOC_LARGE(buf, chunk_size);
847 first_index = cd->lpcd_first_idx + 1;
848 if (cd != NULL && cd->lpcd_last_idx)
849 index = cd->lpcd_last_idx;
851 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
854 struct llog_rec_hdr *rec;
855 struct llog_rec_tail *tail;
857 /* skip records not set in bitmap */
858 while (index >= first_index &&
859 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
862 LASSERT(index >= first_index - 1);
863 if (index == first_index - 1)
866 /* get the buf with our target record; avoid old garbage */
867 memset(buf, 0, chunk_size);
868 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
873 idx = rec->lrh_index;
874 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
875 while (idx < index) {
876 rec = (void *)rec + rec->lrh_len;
877 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
878 lustre_swab_llog_rec(rec);
881 LASSERT(idx == index);
882 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
884 /* process records in buffer, starting where we found one */
885 while ((void *)tail > buf) {
886 if (tail->lrt_index == 0)
887 GOTO(out, rc = 0); /* no more records */
889 /* if set, process the callback on this record */
890 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
891 rec = (void *)tail - tail->lrt_len +
894 rc = cb(env, loghandle, rec, data);
895 if (rc == LLOG_PROC_BREAK) {
897 } else if (rc == LLOG_DEL_RECORD) {
898 rc = llog_cancel_rec(env, loghandle,
905 /* previous record, still in buffer? */
907 if (index < first_index)
909 tail = (void *)tail - tail->lrt_len;
915 OBD_FREE_LARGE(buf, chunk_size);
918 EXPORT_SYMBOL(llog_reverse_process);
924 * llog_open - open llog, may not exist
925 * llog_exist - check if llog exists
926 * llog_close - close opened llog, pair for open, frees llog_handle
927 * llog_declare_create - declare llog creation
928 * llog_create - create new llog on disk, need transaction handle
929 * llog_declare_write_rec - declaration of llog write
930 * llog_write_rec - write llog record on disk, need transaction handle
931 * llog_declare_add - declare llog catalog record addition
932 * llog_add - add llog record in catalog, need transaction handle
934 int llog_exist(struct llog_handle *loghandle)
936 struct llog_operations *lop;
941 rc = llog_handle2ops(loghandle, &lop);
944 if (lop->lop_exist == NULL)
947 rc = lop->lop_exist(loghandle);
950 EXPORT_SYMBOL(llog_exist);
952 int llog_declare_create(const struct lu_env *env,
953 struct llog_handle *loghandle, struct thandle *th)
955 struct llog_operations *lop;
960 rc = llog_handle2ops(loghandle, &lop);
963 if (lop->lop_declare_create == NULL)
966 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
968 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
969 rc = lop->lop_declare_create(env, loghandle, th);
971 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
975 int llog_create(const struct lu_env *env, struct llog_handle *handle,
978 struct llog_operations *lop;
983 rc = llog_handle2ops(handle, &lop);
986 if (lop->lop_create == NULL)
989 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
991 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
992 rc = lop->lop_create(env, handle, th);
994 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
998 int llog_declare_write_rec(const struct lu_env *env,
999 struct llog_handle *handle,
1000 struct llog_rec_hdr *rec, int idx,
1003 struct llog_operations *lop;
1008 rc = llog_handle2ops(handle, &lop);
1012 if (lop->lop_declare_write_rec == NULL)
1013 RETURN(-EOPNOTSUPP);
1015 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1017 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1018 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1020 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1024 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1025 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1026 int idx, struct thandle *th)
1028 struct llog_operations *lop;
1029 int raised, rc, buflen;
1033 /* API sanity checks */
1034 if (handle == NULL) {
1035 CERROR("loghandle is missed\n");
1037 } else if (handle->lgh_obj == NULL) {
1038 CERROR("loghandle %p with NULL object\n",
1041 } else if (th == NULL) {
1042 CERROR("%s: missed transaction handle\n",
1043 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1045 } else if (handle->lgh_hdr == NULL) {
1046 CERROR("%s: loghandle %p with no header\n",
1047 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1052 rc = llog_handle2ops(handle, &lop);
1056 if (lop->lop_write_rec == NULL)
1057 RETURN(-EOPNOTSUPP);
1059 buflen = rec->lrh_len;
1060 LASSERT(cfs_size_round(buflen) == buflen);
1062 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1064 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1065 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1067 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1071 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1072 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1079 if (lgh->lgh_logops->lop_add == NULL)
1080 RETURN(-EOPNOTSUPP);
1082 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1084 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1085 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1087 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1090 EXPORT_SYMBOL(llog_add);
1092 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1093 struct llog_rec_hdr *rec, struct thandle *th)
1099 if (lgh->lgh_logops->lop_declare_add == NULL)
1100 RETURN(-EOPNOTSUPP);
1102 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1104 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1105 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1107 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1110 EXPORT_SYMBOL(llog_declare_add);
1113 * Helper function to open llog or create it if doesn't exist.
1114 * It hides all transaction handling from caller.
1116 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1117 struct llog_handle **res, struct llog_logid *logid,
1120 struct dt_device *d;
1126 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1130 if (llog_exist(*res))
1133 LASSERT((*res)->lgh_obj != NULL);
1135 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1137 if (unlikely(unlikely(d->dd_rdonly)))
1140 th = dt_trans_create(env, d);
1142 GOTO(out, rc = PTR_ERR(th));
1144 /* Create update llog object synchronously, which
1145 * happens during inialization process see
1146 * lod_sub_prep_llog(), to make sure the update
1147 * llog object is created before corss-MDT writing
1148 * updates into the llog object */
1149 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1152 th->th_wait_submit = 1;
1153 rc = llog_declare_create(env, *res, th);
1155 rc = dt_trans_start_local(env, d, th);
1157 rc = llog_create(env, *res, th);
1159 dt_trans_stop(env, d, th);
1162 llog_close(env, *res);
1165 EXPORT_SYMBOL(llog_open_create);
1168 * Helper function to delete existent llog.
1170 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1171 struct llog_logid *logid, char *name)
1173 struct llog_handle *handle;
1178 /* nothing to erase */
1179 if (name == NULL && logid == NULL)
1182 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1186 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1188 rc = llog_destroy(env, handle);
1190 rc2 = llog_close(env, handle);
1195 EXPORT_SYMBOL(llog_erase);
1198 * Helper function for write record in llog.
1199 * It hides all transaction handling from caller.
1200 * Valid only with local llog.
1202 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1203 struct llog_rec_hdr *rec, int idx)
1205 struct dt_device *dt;
1213 LASSERT(loghandle->lgh_ctxt);
1214 LASSERT(loghandle->lgh_obj != NULL);
1216 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1218 if (unlikely(unlikely(dt->dd_rdonly)))
1221 th = dt_trans_create(env, dt);
1223 RETURN(PTR_ERR(th));
1225 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1227 GOTO(out_trans, rc);
1229 th->th_wait_submit = 1;
1230 rc = dt_trans_start_local(env, dt, th);
1232 GOTO(out_trans, rc);
1234 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1236 down_write(&loghandle->lgh_lock);
1238 struct llog_thread_info *lti = llog_info(env);
1240 /* cookie comes from llog_process_thread */
1241 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1242 rec->lrh_index, th);
1243 /* upper layer didn`t pass cookie so change rc */
1244 rc = (rc == 1 ? 0 : rc);
1246 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1249 up_write(&loghandle->lgh_lock);
1251 dt_trans_stop(env, dt, th);
1254 EXPORT_SYMBOL(llog_write);
1256 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1257 struct llog_handle **lgh, struct llog_logid *logid,
1258 char *name, enum llog_open_param open_param)
1266 LASSERT(ctxt->loc_logops);
1268 if (ctxt->loc_logops->lop_open == NULL) {
1270 RETURN(-EOPNOTSUPP);
1273 *lgh = llog_alloc_handle();
1276 (*lgh)->lgh_ctxt = ctxt;
1277 (*lgh)->lgh_logops = ctxt->loc_logops;
1279 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1281 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1282 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1284 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1286 llog_free_handle(*lgh);
1291 EXPORT_SYMBOL(llog_open);
1293 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1295 struct llog_operations *lop;
1300 rc = llog_handle2ops(loghandle, &lop);
1303 if (lop->lop_close == NULL)
1304 GOTO(out, rc = -EOPNOTSUPP);
1305 rc = lop->lop_close(env, loghandle);
1307 llog_handle_put(loghandle);
1310 EXPORT_SYMBOL(llog_close);
1313 * Helper function to get the llog size in records. It is used by MGS
1314 * mostly to check that config llog exists and contains data.
1316 * \param[in] env execution environment
1317 * \param[in] ctxt llog context
1318 * \param[in] name llog name
1320 * \retval true if there are records in llog besides a header
1321 * \retval false on error or llog without records
1323 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1326 struct llog_handle *llh;
1329 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1331 if (likely(rc == -ENOENT))
1336 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1338 GOTO(out_close, rc);
1339 rc = llog_get_size(llh);
1342 llog_close(env, llh);
1344 /* The header is record 1, the llog is still considered as empty
1345 * if there is only header */
1348 EXPORT_SYMBOL(llog_is_empty);
1350 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1351 struct llog_rec_hdr *rec, void *data)
1353 struct llog_handle *copy_llh = data;
1355 /* Append all records */
1356 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1359 /* backup plain llog */
1360 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1361 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1362 char *name, char *backup)
1364 struct llog_handle *llh, *bllh;
1369 /* open original log */
1370 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1372 /* the -ENOENT case is also reported to the caller
1373 * but silently so it should handle that if needed.
1376 CERROR("%s: failed to open log %s: rc = %d\n",
1377 obd->obd_name, name, rc);
1381 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1383 GOTO(out_close, rc);
1385 /* Make sure there's no old backup log */
1386 rc = llog_erase(env, bctxt, NULL, backup);
1387 if (rc < 0 && rc != -ENOENT)
1388 GOTO(out_close, rc);
1390 /* open backup log */
1391 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1393 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1394 obd->obd_name, backup, rc);
1395 GOTO(out_close, rc);
1398 /* check that backup llog is not the same object as original one */
1399 if (llh->lgh_obj == bllh->lgh_obj) {
1400 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1401 obd->obd_name, name, backup, llh->lgh_obj,
1403 GOTO(out_backup, rc = -EEXIST);
1406 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1408 GOTO(out_backup, rc);
1410 /* Copy log record by record */
1411 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1414 CERROR("%s: failed to backup log %s: rc = %d\n",
1415 obd->obd_name, name, rc);
1417 llog_close(env, bllh);
1419 llog_close(env, llh);
1422 EXPORT_SYMBOL(llog_backup);
1424 /* Get size of llog */
1425 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1430 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1432 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1433 llh->lgh_ctxt->loc_obd->obd_name,
1434 PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1440 EXPORT_SYMBOL(llog_size);