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_class.h>
51 #include "llog_internal.h"
53 * Allocate a new log or catalog handle
54 * Used inside llog_open().
56 static struct llog_handle *llog_alloc_handle(void)
58 struct llog_handle *loghandle;
60 OBD_ALLOC_PTR(loghandle);
61 if (loghandle == NULL)
64 init_rwsem(&loghandle->lgh_lock);
65 mutex_init(&loghandle->lgh_hdr_mutex);
66 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
67 atomic_set(&loghandle->lgh_refcount, 1);
73 * Free llog handle and header data if exists. Used in llog_close() only
75 static void llog_free_handle(struct llog_handle *loghandle)
77 LASSERT(loghandle != NULL);
79 /* failed llog_init_handle */
80 if (loghandle->lgh_hdr == NULL)
83 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
84 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
85 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
86 LASSERT(list_empty(&loghandle->u.chd.chd_head));
87 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
89 OBD_FREE_PTR(loghandle);
92 void llog_handle_get(struct llog_handle *loghandle)
94 atomic_inc(&loghandle->lgh_refcount);
97 void llog_handle_put(struct llog_handle *loghandle)
99 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
100 if (atomic_dec_and_test(&loghandle->lgh_refcount))
101 llog_free_handle(loghandle);
104 static int llog_declare_destroy(const struct lu_env *env,
105 struct llog_handle *handle,
108 struct llog_operations *lop;
113 rc = llog_handle2ops(handle, &lop);
116 if (lop->lop_declare_destroy == NULL)
119 rc = lop->lop_declare_destroy(env, handle, th);
124 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
127 struct llog_operations *lop;
131 rc = llog_handle2ops(handle, &lop);
134 if (lop->lop_destroy == NULL)
137 LASSERT(handle->lgh_obj != NULL);
138 if (!dt_object_exists(handle->lgh_obj))
141 rc = lop->lop_destroy(env, handle, th);
146 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
148 struct llog_operations *lop;
149 struct dt_device *dt;
155 rc = llog_handle2ops(handle, &lop);
158 if (lop->lop_destroy == NULL)
161 if (handle->lgh_obj == NULL) {
162 /* if lgh_obj == NULL, then it is from client side destroy */
163 rc = lop->lop_destroy(env, handle, NULL);
167 if (!dt_object_exists(handle->lgh_obj))
170 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
172 th = dt_trans_create(env, dt);
176 rc = llog_declare_destroy(env, handle, th);
180 rc = dt_trans_start_local(env, dt, th);
184 rc = lop->lop_destroy(env, handle, th);
187 dt_trans_stop(env, dt, th);
191 EXPORT_SYMBOL(llog_destroy);
193 /* returns negative on error; 0 if success; 1 if success & log destroyed */
194 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
197 struct llog_thread_info *lgi = llog_info(env);
198 struct dt_device *dt;
199 struct llog_log_hdr *llh;
204 bool subtract_count = false;
208 LASSERT(loghandle != NULL);
209 LASSERT(loghandle->lgh_ctxt != NULL);
210 LASSERT(loghandle->lgh_obj != NULL);
212 llh = loghandle->lgh_hdr;
214 CDEBUG(D_RPCTRACE, "Canceling %d in log "DFID"\n", index,
215 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
218 CERROR("Can't cancel index 0 which is header\n");
222 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
224 th = dt_trans_create(env, dt);
228 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
232 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
233 rc = llog_declare_destroy(env, loghandle, th);
238 th->th_wait_submit = 1;
239 rc = dt_trans_start_local(env, dt, th);
243 down_write(&loghandle->lgh_lock);
245 mutex_lock(&loghandle->lgh_hdr_mutex);
246 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
247 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
248 GOTO(out_unlock, rc);
251 loghandle->lgh_hdr->llh_count--;
252 subtract_count = true;
254 /* Since llog_process_thread use lgi_cookie, it`s better to save them
255 * and restore after using
257 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
258 /* Pass this index to llog_osd_write_rec(), which will use the index
259 * to only update the necesary bitmap. */
260 lgi->lgi_cookie.lgc_index = index;
262 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
263 LLOG_HEADER_IDX, th);
264 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
267 GOTO(out_unlock, rc);
269 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
270 (llh->llh_count == 1) &&
271 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
272 (loghandle->u.phd.phd_cat_handle != NULL &&
273 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
275 /* never try to destroy it again */
276 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
277 rc = llog_trans_destroy(env, loghandle, th);
279 /* Sigh, can not destroy the final plain llog, but
280 * the bitmap has been clearly, so the record can not
281 * be accessed anymore, let's return 0 for now, and
282 * the orphan will be handled by LFSCK. */
283 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
284 loghandle->lgh_ctxt->loc_obd->obd_name,
285 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
286 GOTO(out_unlock, rc = 0);
292 mutex_unlock(&loghandle->lgh_hdr_mutex);
293 up_write(&loghandle->lgh_lock);
295 rc1 = dt_trans_stop(env, dt, th);
298 if (rc < 0 && subtract_count) {
299 mutex_lock(&loghandle->lgh_hdr_mutex);
300 loghandle->lgh_hdr->llh_count++;
301 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
302 mutex_unlock(&loghandle->lgh_hdr_mutex);
307 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
308 const struct obd_uuid *uuid)
310 struct llog_operations *lop;
314 rc = llog_handle2ops(handle, &lop);
318 if (lop->lop_read_header == NULL)
321 rc = lop->lop_read_header(env, handle);
322 if (rc == LLOG_EEMPTY) {
323 struct llog_log_hdr *llh = handle->lgh_hdr;
325 /* lrh_len should be initialized in llog_init_handle */
326 handle->lgh_last_idx = 0; /* header is record with index 0 */
327 llh->llh_count = 1; /* for the header record */
328 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
329 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
330 LLOG_MIN_CHUNK_SIZE);
331 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
332 llh->llh_hdr.lrh_index = 0;
333 llh->llh_timestamp = ktime_get_real_seconds();
335 memcpy(&llh->llh_tgtuuid, uuid,
336 sizeof(llh->llh_tgtuuid));
337 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
338 /* Since update llog header might also call this function,
339 * let's reset the bitmap to 0 here */
340 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
341 llh->llh_bitmap_offset -
342 sizeof(llh->llh_tail));
343 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
344 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
345 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
350 EXPORT_SYMBOL(llog_read_header);
352 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
353 int flags, struct obd_uuid *uuid)
355 struct llog_log_hdr *llh;
356 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
358 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
361 LASSERT(handle->lgh_hdr == NULL);
363 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
364 OBD_ALLOC_LARGE(llh, chunk_size);
368 handle->lgh_hdr = llh;
369 handle->lgh_hdr_size = chunk_size;
370 /* first assign flags to use llog_client_ops */
371 llh->llh_flags = flags;
372 rc = llog_read_header(env, handle, uuid);
374 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
375 flags & LLOG_F_IS_CAT) ||
376 (llh->llh_flags & LLOG_F_IS_CAT &&
377 flags & LLOG_F_IS_PLAIN))) {
378 CERROR("%s: llog type is %s but initializing %s\n",
379 handle->lgh_ctxt->loc_obd->obd_name,
380 llh->llh_flags & LLOG_F_IS_CAT ?
382 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
383 GOTO(out, rc = -EINVAL);
384 } else if (llh->llh_flags &
385 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
387 * it is possible to open llog without specifying llog
388 * type so it is taken from llh_flags
390 flags = llh->llh_flags;
392 /* for some reason the llh_flags has no type set */
393 CERROR("llog type is not specified!\n");
394 GOTO(out, rc = -EINVAL);
397 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
398 CERROR("%s: llog uuid mismatch: %s/%s\n",
399 handle->lgh_ctxt->loc_obd->obd_name,
401 (char *)llh->llh_tgtuuid.uuid);
402 GOTO(out, rc = -EEXIST);
405 if (flags & LLOG_F_IS_CAT) {
406 LASSERT(list_empty(&handle->u.chd.chd_head));
407 INIT_LIST_HEAD(&handle->u.chd.chd_head);
408 llh->llh_size = sizeof(struct llog_logid_rec);
409 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
410 } else if (!(flags & LLOG_F_IS_PLAIN)) {
411 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
412 handle->lgh_ctxt->loc_obd->obd_name,
413 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
416 llh->llh_flags |= fmt;
419 OBD_FREE_LARGE(llh, chunk_size);
420 handle->lgh_hdr = NULL;
424 EXPORT_SYMBOL(llog_init_handle);
426 static int llog_process_thread(void *arg)
428 struct llog_process_info *lpi = arg;
429 struct llog_handle *loghandle = lpi->lpi_loghandle;
430 struct llog_log_hdr *llh = loghandle->lgh_hdr;
431 struct llog_process_cat_data *cd = lpi->lpi_catdata;
432 struct llog_thread_info *lti;
436 int rc = 0, index = 1, last_index;
438 int last_called_index = 0;
439 bool repeated = false;
446 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
448 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
449 /* expect chunk_size to be power of two */
450 LASSERT(is_power_of_2(chunk_size));
452 OBD_ALLOC_LARGE(buf, chunk_size);
454 lpi->lpi_rc = -ENOMEM;
459 last_called_index = cd->lpcd_first_idx;
460 index = cd->lpcd_first_idx + 1;
462 if (cd != NULL && cd->lpcd_last_idx)
463 last_index = cd->lpcd_last_idx;
465 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
468 struct llog_rec_hdr *rec;
469 off_t chunk_offset = 0;
470 unsigned int buf_offset = 0;
474 /* skip records not set in bitmap */
475 while (index <= last_index &&
476 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
479 /* There are no indices prior the last_index */
480 if (index > last_index)
483 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
487 /* get the buf with our target record; avoid old garbage */
488 memset(buf, 0, chunk_size);
489 /* the record index for outdated chunk data */
490 lh_last_idx = loghandle->lgh_last_idx + 1;
491 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
492 index, &cur_offset, buf, chunk_size);
494 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
495 " buf_offset %u, rc = %d\n", cur_offset,
496 (__u64)chunk_offset, buf_offset, rc);
497 /* we`ve tried to reread the chunk, but there is no
499 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
505 /* NB: after llog_next_block() call the cur_offset is the
506 * offset of the next block after read one.
507 * The absolute offset of the current chunk is calculated
508 * from cur_offset value and stored in chunk_offset variable.
510 if ((cur_offset & (chunk_size - 1)) != 0) {
511 partial_chunk = true;
512 chunk_offset = cur_offset & ~(chunk_size - 1);
514 partial_chunk = false;
515 chunk_offset = cur_offset - chunk_size;
518 /* NB: when rec->lrh_len is accessed it is already swabbed
519 * since it is used at the "end" of the loop and the rec
520 * swabbing is done at the beginning of the loop. */
521 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
522 (char *)rec < buf + chunk_size;
523 rec = llog_rec_hdr_next(rec)) {
525 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
528 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
529 lustre_swab_llog_rec(rec);
531 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
532 rec->lrh_type, rec->lrh_index);
534 /* the bitmap could be changed during processing
535 * records from the chunk. For wrapped catalog
536 * it means we can read deleted record and try to
537 * process it. Check this case and reread the chunk. */
539 /* for partial chunk the end of it is zeroed, check
540 * for index 0 to distinguish it. */
541 if ((partial_chunk && rec->lrh_index == 0) ||
542 (index == lh_last_idx &&
543 lh_last_idx != (loghandle->lgh_last_idx + 1))) {
544 /* concurrent llog_add() might add new records
545 * while llog_processing, check this is not
546 * the case and re-read the current chunk
549 /* lgh_last_idx could be less then index
550 * for catalog, if catalog is wrapped */
551 if ((index > loghandle->lgh_last_idx &&
552 !(loghandle->lgh_hdr->llh_flags &
553 LLOG_F_IS_CAT)) || repeated ||
554 (loghandle->lgh_obj != NULL &&
555 dt_object_remote(loghandle->lgh_obj)))
557 /* <2 records means no more records
558 * if the last record we processed was
559 * the final one, then the underlying
560 * object might have been destroyed yet.
561 * we better don't access that.. */
562 mutex_lock(&loghandle->lgh_hdr_mutex);
563 records = loghandle->lgh_hdr->llh_count;
564 mutex_unlock(&loghandle->lgh_hdr_mutex);
567 CDEBUG(D_OTHER, "Re-read last llog buffer for "
568 "new records, index %u, last %u\n",
569 index, loghandle->lgh_last_idx);
570 /* save offset inside buffer for the re-read */
571 buf_offset = (char *)rec - (char *)buf;
572 cur_offset = chunk_offset;
579 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
580 CWARN("%s: invalid length %d in llog "DFID
581 "record for index %d/%d\n",
582 loghandle->lgh_ctxt->loc_obd->obd_name,
584 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
585 rec->lrh_index, index);
587 GOTO(out, rc = -EINVAL);
590 if (rec->lrh_index < index) {
591 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
596 if (rec->lrh_index != index) {
597 CERROR("%s: "DFID" Invalid record: index %u"
598 " but expected %u\n",
599 loghandle->lgh_ctxt->loc_obd->obd_name,
600 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
601 rec->lrh_index, index);
602 GOTO(out, rc = -ERANGE);
606 "lrh_index: %d lrh_len: %d (%d remains)\n",
607 rec->lrh_index, rec->lrh_len,
608 (int)(buf + chunk_size - (char *)rec));
610 /* lgh_cur_offset is used only at llog_test_3 */
611 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
614 /* if set, process the callback on this record */
615 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
616 struct llog_cookie *lgc;
621 lgc = <i->lgi_cookie;
622 /* store lu_env for recursive calls */
623 tmp_off = lgc->lgc_offset;
624 tmp_idx = lgc->lgc_index;
626 lgc->lgc_offset = (char *)rec -
627 (char *)buf + chunk_offset;
628 lgc->lgc_index = rec->lrh_index;
630 /* using lu_env for passing record offset to
631 * llog_write through various callbacks */
632 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
634 last_called_index = index;
637 lgc->lgc_offset = tmp_off;
638 lgc->lgc_index = tmp_idx;
641 if (rc == LLOG_PROC_BREAK) {
643 } else if (rc == LLOG_DEL_RECORD) {
644 rc = llog_cancel_rec(lpi->lpi_env,
650 /* some stupid callbacks directly cancel records
651 * and delete llog. Check it and stop
653 if (loghandle->lgh_hdr == NULL ||
654 loghandle->lgh_hdr->llh_count == 1)
657 /* exit if the last index is reached */
658 if (index >= last_index)
666 cd->lpcd_last_idx = last_called_index;
668 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
669 if (dt_object_remote(loghandle->lgh_obj)) {
670 /* If it is remote object, then -EIO might means
671 * disconnection or eviction, let's return -EAGAIN,
672 * so for update recovery log processing, it will
673 * retry until the umount or abort recovery, see
674 * lod_sub_recovery_thread() */
675 CERROR("%s retry remote llog process\n",
676 loghandle->lgh_ctxt->loc_obd->obd_name);
679 /* something bad happened to the processing of a local
680 * llog file, probably I/O error or the log got
681 * corrupted to be able to finally release the log we
682 * discard any remaining bits in the header */
683 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
684 " %s index %d count %d\n",
685 loghandle->lgh_ctxt->loc_obd->obd_name,
686 POSTID(&loghandle->lgh_id.lgl_oi),
687 loghandle->lgh_id.lgl_ogen,
688 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
689 "plain"), index, llh->llh_count);
691 while (index <= last_index) {
692 if (ext2_test_bit(index,
693 LLOG_HDR_BITMAP(llh)) != 0)
694 llog_cancel_rec(lpi->lpi_env, loghandle,
702 OBD_FREE_LARGE(buf, chunk_size);
707 static int llog_process_thread_daemonize(void *arg)
709 struct llog_process_info *lpi = arg;
712 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
714 task_lock(lpi->lpi_reftask);
715 new_ns = lpi->lpi_reftask->nsproxy;
716 if (curr_ns != new_ns) {
719 current->nsproxy = new_ns;
720 /* XXX: we should call put_nsproxy() instead of
721 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
722 * used outside of the kernel itself, because it calls
723 * free_nsproxy() which is not exported by the kernel
724 * (defined in kernel/nsproxy.c) */
725 atomic_dec(&curr_ns->count);
727 task_unlock(lpi->lpi_reftask);
731 /* client env has no keys, tags is just 0 */
732 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
737 rc = llog_process_thread(arg);
741 complete(&lpi->lpi_completion);
745 int llog_process_or_fork(const struct lu_env *env,
746 struct llog_handle *loghandle,
747 llog_cb_t cb, void *data, void *catdata, bool fork)
749 struct llog_process_info *lpi;
756 CERROR("cannot alloc pointer\n");
759 lpi->lpi_loghandle = loghandle;
761 lpi->lpi_cbdata = data;
762 lpi->lpi_catdata = catdata;
765 struct task_struct *task;
767 /* The new thread can't use parent env,
768 * init the new one in llog_process_thread_daemonize. */
770 init_completion(&lpi->lpi_completion);
771 /* take reference to current, so that
772 * llog_process_thread_daemonize() can use it to switch to
773 * namespace associated with current */
774 lpi->lpi_reftask = current;
775 task = kthread_run(llog_process_thread_daemonize, lpi,
776 "llog_process_thread");
779 CERROR("%s: cannot start thread: rc = %d\n",
780 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
783 wait_for_completion(&lpi->lpi_completion);
786 llog_process_thread(lpi);
794 EXPORT_SYMBOL(llog_process_or_fork);
796 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
797 llog_cb_t cb, void *data, void *catdata)
800 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
801 return rc == LLOG_DEL_PLAIN ? 0 : rc;
803 EXPORT_SYMBOL(llog_process);
805 int llog_reverse_process(const struct lu_env *env,
806 struct llog_handle *loghandle, llog_cb_t cb,
807 void *data, void *catdata)
809 struct llog_log_hdr *llh = loghandle->lgh_hdr;
810 struct llog_process_cat_data *cd = catdata;
812 int rc = 0, first_index = 1, index, idx;
813 __u32 chunk_size = llh->llh_hdr.lrh_len;
816 OBD_ALLOC_LARGE(buf, chunk_size);
821 first_index = cd->lpcd_first_idx + 1;
822 if (cd != NULL && cd->lpcd_last_idx)
823 index = cd->lpcd_last_idx;
825 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
828 struct llog_rec_hdr *rec;
829 struct llog_rec_tail *tail;
831 /* skip records not set in bitmap */
832 while (index >= first_index &&
833 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
836 LASSERT(index >= first_index - 1);
837 if (index == first_index - 1)
840 /* get the buf with our target record; avoid old garbage */
841 memset(buf, 0, chunk_size);
842 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
847 idx = rec->lrh_index;
848 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
849 while (idx < index) {
850 rec = (void *)rec + rec->lrh_len;
851 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
852 lustre_swab_llog_rec(rec);
855 LASSERT(idx == index);
856 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
858 /* process records in buffer, starting where we found one */
859 while ((void *)tail > buf) {
860 if (tail->lrt_index == 0)
861 GOTO(out, rc = 0); /* no more records */
863 /* if set, process the callback on this record */
864 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
865 rec = (void *)tail - tail->lrt_len +
868 rc = cb(env, loghandle, rec, data);
869 if (rc == LLOG_PROC_BREAK) {
871 } else if (rc == LLOG_DEL_RECORD) {
872 rc = llog_cancel_rec(env, loghandle,
879 /* previous record, still in buffer? */
881 if (index < first_index)
883 tail = (void *)tail - tail->lrt_len;
889 OBD_FREE_LARGE(buf, chunk_size);
892 EXPORT_SYMBOL(llog_reverse_process);
898 * llog_open - open llog, may not exist
899 * llog_exist - check if llog exists
900 * llog_close - close opened llog, pair for open, frees llog_handle
901 * llog_declare_create - declare llog creation
902 * llog_create - create new llog on disk, need transaction handle
903 * llog_declare_write_rec - declaration of llog write
904 * llog_write_rec - write llog record on disk, need transaction handle
905 * llog_declare_add - declare llog catalog record addition
906 * llog_add - add llog record in catalog, need transaction handle
908 int llog_exist(struct llog_handle *loghandle)
910 struct llog_operations *lop;
915 rc = llog_handle2ops(loghandle, &lop);
918 if (lop->lop_exist == NULL)
921 rc = lop->lop_exist(loghandle);
924 EXPORT_SYMBOL(llog_exist);
926 int llog_declare_create(const struct lu_env *env,
927 struct llog_handle *loghandle, struct thandle *th)
929 struct llog_operations *lop;
934 rc = llog_handle2ops(loghandle, &lop);
937 if (lop->lop_declare_create == NULL)
940 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
942 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
943 rc = lop->lop_declare_create(env, loghandle, th);
945 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
949 int llog_create(const struct lu_env *env, struct llog_handle *handle,
952 struct llog_operations *lop;
957 rc = llog_handle2ops(handle, &lop);
960 if (lop->lop_create == NULL)
963 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
965 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
966 rc = lop->lop_create(env, handle, th);
968 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
972 int llog_declare_write_rec(const struct lu_env *env,
973 struct llog_handle *handle,
974 struct llog_rec_hdr *rec, int idx,
977 struct llog_operations *lop;
982 rc = llog_handle2ops(handle, &lop);
986 if (lop->lop_declare_write_rec == NULL)
989 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
991 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
992 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
994 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
998 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
999 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1000 int idx, struct thandle *th)
1002 struct llog_operations *lop;
1003 int raised, rc, buflen;
1007 /* API sanity checks */
1008 if (handle == NULL) {
1009 CERROR("loghandle is missed\n");
1011 } else if (handle->lgh_obj == NULL) {
1012 CERROR("loghandle %p with NULL object\n",
1015 } else if (th == NULL) {
1016 CERROR("%s: missed transaction handle\n",
1017 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1019 } else if (handle->lgh_hdr == NULL) {
1020 CERROR("%s: loghandle %p with no header\n",
1021 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1026 rc = llog_handle2ops(handle, &lop);
1030 if (lop->lop_write_rec == NULL)
1031 RETURN(-EOPNOTSUPP);
1033 buflen = rec->lrh_len;
1034 LASSERT(cfs_size_round(buflen) == buflen);
1036 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1038 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1039 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1041 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1045 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1046 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1053 if (lgh->lgh_logops->lop_add == NULL)
1054 RETURN(-EOPNOTSUPP);
1056 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1058 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1059 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1061 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1064 EXPORT_SYMBOL(llog_add);
1066 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1067 struct llog_rec_hdr *rec, struct thandle *th)
1073 if (lgh->lgh_logops->lop_declare_add == NULL)
1074 RETURN(-EOPNOTSUPP);
1076 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1078 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1079 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1081 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1084 EXPORT_SYMBOL(llog_declare_add);
1087 * Helper function to open llog or create it if doesn't exist.
1088 * It hides all transaction handling from caller.
1090 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1091 struct llog_handle **res, struct llog_logid *logid,
1094 struct dt_device *d;
1100 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1104 if (llog_exist(*res))
1107 LASSERT((*res)->lgh_obj != NULL);
1109 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1111 th = dt_trans_create(env, d);
1113 GOTO(out, rc = PTR_ERR(th));
1115 /* Create update llog object synchronously, which
1116 * happens during inialization process see
1117 * lod_sub_prep_llog(), to make sure the update
1118 * llog object is created before corss-MDT writing
1119 * updates into the llog object */
1120 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1123 th->th_wait_submit = 1;
1124 rc = llog_declare_create(env, *res, th);
1126 rc = dt_trans_start_local(env, d, th);
1128 rc = llog_create(env, *res, th);
1130 dt_trans_stop(env, d, th);
1133 llog_close(env, *res);
1136 EXPORT_SYMBOL(llog_open_create);
1139 * Helper function to delete existent llog.
1141 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1142 struct llog_logid *logid, char *name)
1144 struct llog_handle *handle;
1149 /* nothing to erase */
1150 if (name == NULL && logid == NULL)
1153 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1157 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1159 rc = llog_destroy(env, handle);
1161 rc2 = llog_close(env, handle);
1166 EXPORT_SYMBOL(llog_erase);
1169 * Helper function for write record in llog.
1170 * It hides all transaction handling from caller.
1171 * Valid only with local llog.
1173 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1174 struct llog_rec_hdr *rec, int idx)
1176 struct dt_device *dt;
1184 LASSERT(loghandle->lgh_ctxt);
1185 LASSERT(loghandle->lgh_obj != NULL);
1187 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1189 th = dt_trans_create(env, dt);
1191 RETURN(PTR_ERR(th));
1193 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1195 GOTO(out_trans, rc);
1197 th->th_wait_submit = 1;
1198 rc = dt_trans_start_local(env, dt, th);
1200 GOTO(out_trans, rc);
1202 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1204 down_write(&loghandle->lgh_lock);
1206 struct llog_thread_info *lti = llog_info(env);
1208 /* cookie comes from llog_process_thread */
1209 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1210 rec->lrh_index, th);
1211 /* upper layer didn`t pass cookie so change rc */
1212 rc = (rc == 1 ? 0 : rc);
1214 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1217 up_write(&loghandle->lgh_lock);
1219 dt_trans_stop(env, dt, th);
1222 EXPORT_SYMBOL(llog_write);
1224 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1225 struct llog_handle **lgh, struct llog_logid *logid,
1226 char *name, enum llog_open_param open_param)
1234 LASSERT(ctxt->loc_logops);
1236 if (ctxt->loc_logops->lop_open == NULL) {
1238 RETURN(-EOPNOTSUPP);
1241 *lgh = llog_alloc_handle();
1244 (*lgh)->lgh_ctxt = ctxt;
1245 (*lgh)->lgh_logops = ctxt->loc_logops;
1247 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1249 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1250 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1252 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1254 llog_free_handle(*lgh);
1259 EXPORT_SYMBOL(llog_open);
1261 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1263 struct llog_operations *lop;
1268 rc = llog_handle2ops(loghandle, &lop);
1271 if (lop->lop_close == NULL)
1272 GOTO(out, rc = -EOPNOTSUPP);
1273 rc = lop->lop_close(env, loghandle);
1275 llog_handle_put(loghandle);
1278 EXPORT_SYMBOL(llog_close);
1281 * Helper function to get the llog size in records. It is used by MGS
1282 * mostly to check that config llog exists and contains data.
1284 * \param[in] env execution environment
1285 * \param[in] ctxt llog context
1286 * \param[in] name llog name
1288 * \retval true if there are records in llog besides a header
1289 * \retval false on error or llog without records
1291 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1294 struct llog_handle *llh;
1297 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1299 if (likely(rc == -ENOENT))
1304 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1306 GOTO(out_close, rc);
1307 rc = llog_get_size(llh);
1310 llog_close(env, llh);
1312 /* The header is record 1, the llog is still considered as empty
1313 * if there is only header */
1316 EXPORT_SYMBOL(llog_is_empty);
1318 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1319 struct llog_rec_hdr *rec, void *data)
1321 struct llog_handle *copy_llh = data;
1323 /* Append all records */
1324 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1327 /* backup plain llog */
1328 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1329 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1330 char *name, char *backup)
1332 struct llog_handle *llh, *bllh;
1337 /* open original log */
1338 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1340 /* the -ENOENT case is also reported to the caller
1341 * but silently so it should handle that if needed.
1344 CERROR("%s: failed to open log %s: rc = %d\n",
1345 obd->obd_name, name, rc);
1349 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1351 GOTO(out_close, rc);
1353 /* Make sure there's no old backup log */
1354 rc = llog_erase(env, bctxt, NULL, backup);
1355 if (rc < 0 && rc != -ENOENT)
1356 GOTO(out_close, rc);
1358 /* open backup log */
1359 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1361 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1362 obd->obd_name, backup, rc);
1363 GOTO(out_close, rc);
1366 /* check that backup llog is not the same object as original one */
1367 if (llh->lgh_obj == bllh->lgh_obj) {
1368 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1369 obd->obd_name, name, backup, llh->lgh_obj,
1371 GOTO(out_backup, rc = -EEXIST);
1374 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1376 GOTO(out_backup, rc);
1378 /* Copy log record by record */
1379 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1382 CERROR("%s: failed to backup log %s: rc = %d\n",
1383 obd->obd_name, name, rc);
1385 llog_close(env, bllh);
1387 llog_close(env, llh);
1390 EXPORT_SYMBOL(llog_backup);
1392 /* Get size of llog */
1393 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1398 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1400 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1401 llh->lgh_ctxt->loc_obd->obd_name,
1402 PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1408 EXPORT_SYMBOL(llog_size);