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 if (unlikely(unlikely(dt->dd_rdonly)))
175 th = dt_trans_create(env, dt);
179 rc = llog_declare_destroy(env, handle, th);
183 rc = dt_trans_start_local(env, dt, th);
187 rc = lop->lop_destroy(env, handle, th);
190 dt_trans_stop(env, dt, th);
194 EXPORT_SYMBOL(llog_destroy);
196 /* returns negative on error; 0 if success; 1 if success & log destroyed */
197 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
200 struct llog_thread_info *lgi = llog_info(env);
201 struct dt_device *dt;
202 struct llog_log_hdr *llh;
207 bool subtract_count = false;
211 LASSERT(loghandle != NULL);
212 LASSERT(loghandle->lgh_ctxt != NULL);
213 LASSERT(loghandle->lgh_obj != NULL);
215 llh = loghandle->lgh_hdr;
217 CDEBUG(D_RPCTRACE, "Canceling %d in log "DFID"\n", index,
218 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
221 CERROR("Can't cancel index 0 which is header\n");
225 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
227 if (unlikely(unlikely(dt->dd_rdonly)))
230 th = dt_trans_create(env, dt);
234 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
238 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
239 rc = llog_declare_destroy(env, loghandle, th);
244 th->th_wait_submit = 1;
245 rc = dt_trans_start_local(env, dt, th);
249 down_write(&loghandle->lgh_lock);
251 mutex_lock(&loghandle->lgh_hdr_mutex);
252 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
253 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
254 GOTO(out_unlock, rc);
257 loghandle->lgh_hdr->llh_count--;
258 subtract_count = true;
260 /* Since llog_process_thread use lgi_cookie, it`s better to save them
261 * and restore after using
263 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
264 /* Pass this index to llog_osd_write_rec(), which will use the index
265 * to only update the necesary bitmap. */
266 lgi->lgi_cookie.lgc_index = index;
268 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
269 LLOG_HEADER_IDX, th);
270 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
273 GOTO(out_unlock, rc);
275 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
276 (llh->llh_count == 1) &&
277 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
278 (loghandle->u.phd.phd_cat_handle != NULL &&
279 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
281 /* never try to destroy it again */
282 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
283 rc = llog_trans_destroy(env, loghandle, th);
285 /* Sigh, can not destroy the final plain llog, but
286 * the bitmap has been clearly, so the record can not
287 * be accessed anymore, let's return 0 for now, and
288 * the orphan will be handled by LFSCK. */
289 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
290 loghandle->lgh_ctxt->loc_obd->obd_name,
291 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
292 GOTO(out_unlock, rc = 0);
298 mutex_unlock(&loghandle->lgh_hdr_mutex);
299 up_write(&loghandle->lgh_lock);
301 rc1 = dt_trans_stop(env, dt, th);
304 if (rc < 0 && subtract_count) {
305 mutex_lock(&loghandle->lgh_hdr_mutex);
306 loghandle->lgh_hdr->llh_count++;
307 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
308 mutex_unlock(&loghandle->lgh_hdr_mutex);
313 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
314 const struct obd_uuid *uuid)
316 struct llog_operations *lop;
320 rc = llog_handle2ops(handle, &lop);
324 if (lop->lop_read_header == NULL)
327 rc = lop->lop_read_header(env, handle);
328 if (rc == LLOG_EEMPTY) {
329 struct llog_log_hdr *llh = handle->lgh_hdr;
331 /* lrh_len should be initialized in llog_init_handle */
332 handle->lgh_last_idx = 0; /* header is record with index 0 */
333 llh->llh_count = 1; /* for the header record */
334 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
335 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
336 LLOG_MIN_CHUNK_SIZE);
337 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
338 llh->llh_hdr.lrh_index = 0;
339 llh->llh_timestamp = ktime_get_real_seconds();
341 memcpy(&llh->llh_tgtuuid, uuid,
342 sizeof(llh->llh_tgtuuid));
343 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
344 /* Since update llog header might also call this function,
345 * let's reset the bitmap to 0 here */
346 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
347 llh->llh_bitmap_offset -
348 sizeof(llh->llh_tail));
349 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
350 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
351 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
356 EXPORT_SYMBOL(llog_read_header);
358 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
359 int flags, struct obd_uuid *uuid)
361 struct llog_log_hdr *llh;
362 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
364 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
367 LASSERT(handle->lgh_hdr == NULL);
369 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
370 OBD_ALLOC_LARGE(llh, chunk_size);
374 handle->lgh_hdr = llh;
375 handle->lgh_hdr_size = chunk_size;
376 /* first assign flags to use llog_client_ops */
377 llh->llh_flags = flags;
378 rc = llog_read_header(env, handle, uuid);
380 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
381 flags & LLOG_F_IS_CAT) ||
382 (llh->llh_flags & LLOG_F_IS_CAT &&
383 flags & LLOG_F_IS_PLAIN))) {
384 CERROR("%s: llog type is %s but initializing %s\n",
385 handle->lgh_ctxt->loc_obd->obd_name,
386 llh->llh_flags & LLOG_F_IS_CAT ?
388 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
389 GOTO(out, rc = -EINVAL);
390 } else if (llh->llh_flags &
391 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
393 * it is possible to open llog without specifying llog
394 * type so it is taken from llh_flags
396 flags = llh->llh_flags;
398 /* for some reason the llh_flags has no type set */
399 CERROR("llog type is not specified!\n");
400 GOTO(out, rc = -EINVAL);
403 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
404 CERROR("%s: llog uuid mismatch: %s/%s\n",
405 handle->lgh_ctxt->loc_obd->obd_name,
407 (char *)llh->llh_tgtuuid.uuid);
408 GOTO(out, rc = -EEXIST);
411 if (flags & LLOG_F_IS_CAT) {
412 LASSERT(list_empty(&handle->u.chd.chd_head));
413 INIT_LIST_HEAD(&handle->u.chd.chd_head);
414 llh->llh_size = sizeof(struct llog_logid_rec);
415 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
416 } else if (!(flags & LLOG_F_IS_PLAIN)) {
417 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
418 handle->lgh_ctxt->loc_obd->obd_name,
419 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
422 llh->llh_flags |= fmt;
425 OBD_FREE_LARGE(llh, chunk_size);
426 handle->lgh_hdr = NULL;
430 EXPORT_SYMBOL(llog_init_handle);
432 static int llog_process_thread(void *arg)
434 struct llog_process_info *lpi = arg;
435 struct llog_handle *loghandle = lpi->lpi_loghandle;
436 struct llog_log_hdr *llh = loghandle->lgh_hdr;
437 struct llog_process_cat_data *cd = lpi->lpi_catdata;
438 struct llog_thread_info *lti;
442 int rc = 0, index = 1, last_index;
444 int last_called_index = 0;
445 bool repeated = false;
452 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
454 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
455 /* expect chunk_size to be power of two */
456 LASSERT(is_power_of_2(chunk_size));
458 OBD_ALLOC_LARGE(buf, chunk_size);
460 lpi->lpi_rc = -ENOMEM;
465 last_called_index = cd->lpcd_first_idx;
466 index = cd->lpcd_first_idx + 1;
468 if (cd != NULL && cd->lpcd_last_idx)
469 last_index = cd->lpcd_last_idx;
471 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
474 struct llog_rec_hdr *rec;
475 off_t chunk_offset = 0;
476 unsigned int buf_offset = 0;
480 /* skip records not set in bitmap */
481 while (index <= last_index &&
482 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
485 /* There are no indices prior the last_index */
486 if (index > last_index)
489 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
493 /* get the buf with our target record; avoid old garbage */
494 memset(buf, 0, chunk_size);
495 /* the record index for outdated chunk data */
496 lh_last_idx = loghandle->lgh_last_idx + 1;
497 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
498 index, &cur_offset, buf, chunk_size);
500 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
501 " buf_offset %u, rc = %d\n", cur_offset,
502 (__u64)chunk_offset, buf_offset, rc);
503 /* we`ve tried to reread the chunk, but there is no
505 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
511 /* NB: after llog_next_block() call the cur_offset is the
512 * offset of the next block after read one.
513 * The absolute offset of the current chunk is calculated
514 * from cur_offset value and stored in chunk_offset variable.
516 if ((cur_offset & (chunk_size - 1)) != 0) {
517 partial_chunk = true;
518 chunk_offset = cur_offset & ~(chunk_size - 1);
520 partial_chunk = false;
521 chunk_offset = cur_offset - chunk_size;
524 /* NB: when rec->lrh_len is accessed it is already swabbed
525 * since it is used at the "end" of the loop and the rec
526 * swabbing is done at the beginning of the loop. */
527 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
528 (char *)rec < buf + chunk_size;
529 rec = llog_rec_hdr_next(rec)) {
531 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
534 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
535 lustre_swab_llog_rec(rec);
537 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
538 rec->lrh_type, rec->lrh_index);
540 /* for partial chunk the end of it is zeroed, check
541 * for index 0 to distinguish it. */
542 if (partial_chunk && rec->lrh_index == 0) {
543 /* concurrent llog_add() might add new records
544 * while llog_processing, check this is not
545 * the case and re-read the current chunk
548 /* lgh_last_idx could be less then index
549 * for catalog, if catalog is wrapped */
550 if ((index > loghandle->lgh_last_idx &&
551 !(loghandle->lgh_hdr->llh_flags &
552 LLOG_F_IS_CAT)) || repeated ||
553 (loghandle->lgh_obj != NULL &&
554 dt_object_remote(loghandle->lgh_obj)))
556 /* <2 records means no more records
557 * if the last record we processed was
558 * the final one, then the underlying
559 * object might have been destroyed yet.
560 * we better don't access that.. */
561 mutex_lock(&loghandle->lgh_hdr_mutex);
562 records = loghandle->lgh_hdr->llh_count;
563 mutex_unlock(&loghandle->lgh_hdr_mutex);
566 CDEBUG(D_OTHER, "Re-read last llog buffer for "
567 "new records, index %u, last %u\n",
568 index, loghandle->lgh_last_idx);
569 /* save offset inside buffer for the re-read */
570 buf_offset = (char *)rec - (char *)buf;
571 cur_offset = chunk_offset;
578 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
579 CWARN("%s: invalid length %d in llog "DFID
580 "record for index %d/%d\n",
581 loghandle->lgh_ctxt->loc_obd->obd_name,
583 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
584 rec->lrh_index, index);
586 GOTO(out, rc = -EINVAL);
589 if (rec->lrh_index < index) {
590 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
595 if (rec->lrh_index != index) {
596 CERROR("%s: "DFID" Invalid record: index %u"
597 " but expected %u\n",
598 loghandle->lgh_ctxt->loc_obd->obd_name,
599 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
600 rec->lrh_index, index);
601 GOTO(out, rc = -ERANGE);
605 "lrh_index: %d lrh_len: %d (%d remains)\n",
606 rec->lrh_index, rec->lrh_len,
607 (int)(buf + chunk_size - (char *)rec));
609 /* lgh_cur_offset is used only at llog_test_3 */
610 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
613 /* if set, process the callback on this record */
614 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
615 struct llog_cookie *lgc;
618 /* the bitmap could be changed during processing
619 * records from the chunk. For wrapped catalog
620 * it means we can read deleted record and try to
621 * process it. Check this case and reread the chunk.
622 * Checking the race with llog_add the bit is set
623 * after incrementation of lgh_last_idx */
624 if (index == lh_last_idx &&
626 (loghandle->lgh_last_idx + 1)) {
627 /* save offset inside buffer for
629 buf_offset = (char *)rec - (char *)buf;
630 cur_offset = chunk_offset;
636 lgc = <i->lgi_cookie;
637 /* store lu_env for recursive calls */
638 tmp_off = lgc->lgc_offset;
639 tmp_idx = lgc->lgc_index;
641 lgc->lgc_offset = (char *)rec -
642 (char *)buf + chunk_offset;
643 lgc->lgc_index = rec->lrh_index;
645 /* using lu_env for passing record offset to
646 * llog_write through various callbacks */
647 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
649 last_called_index = index;
652 lgc->lgc_offset = tmp_off;
653 lgc->lgc_index = tmp_idx;
656 if (rc == LLOG_PROC_BREAK) {
658 } else if (rc == LLOG_DEL_RECORD) {
659 rc = llog_cancel_rec(lpi->lpi_env,
665 /* some stupid callbacks directly cancel records
666 * and delete llog. Check it and stop
668 if (loghandle->lgh_hdr == NULL ||
669 loghandle->lgh_hdr->llh_count == 1)
672 /* exit if the last index is reached */
673 if (index >= last_index)
681 cd->lpcd_last_idx = last_called_index;
683 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
684 if (dt_object_remote(loghandle->lgh_obj)) {
685 /* If it is remote object, then -EIO might means
686 * disconnection or eviction, let's return -EAGAIN,
687 * so for update recovery log processing, it will
688 * retry until the umount or abort recovery, see
689 * lod_sub_recovery_thread() */
690 CERROR("%s retry remote llog process\n",
691 loghandle->lgh_ctxt->loc_obd->obd_name);
694 /* something bad happened to the processing of a local
695 * llog file, probably I/O error or the log got
696 * corrupted to be able to finally release the log we
697 * discard any remaining bits in the header */
698 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
699 " %s index %d count %d\n",
700 loghandle->lgh_ctxt->loc_obd->obd_name,
701 POSTID(&loghandle->lgh_id.lgl_oi),
702 loghandle->lgh_id.lgl_ogen,
703 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
704 "plain"), index, llh->llh_count);
706 while (index <= last_index) {
707 if (ext2_test_bit(index,
708 LLOG_HDR_BITMAP(llh)) != 0)
709 llog_cancel_rec(lpi->lpi_env, loghandle,
717 OBD_FREE_LARGE(buf, chunk_size);
722 static int llog_process_thread_daemonize(void *arg)
724 struct llog_process_info *lpi = arg;
727 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
729 task_lock(lpi->lpi_reftask);
730 new_ns = lpi->lpi_reftask->nsproxy;
731 if (curr_ns != new_ns) {
734 current->nsproxy = new_ns;
735 /* XXX: we should call put_nsproxy() instead of
736 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
737 * used outside of the kernel itself, because it calls
738 * free_nsproxy() which is not exported by the kernel
739 * (defined in kernel/nsproxy.c) */
740 atomic_dec(&curr_ns->count);
742 task_unlock(lpi->lpi_reftask);
746 /* client env has no keys, tags is just 0 */
747 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
752 rc = llog_process_thread(arg);
756 complete(&lpi->lpi_completion);
760 int llog_process_or_fork(const struct lu_env *env,
761 struct llog_handle *loghandle,
762 llog_cb_t cb, void *data, void *catdata, bool fork)
764 struct llog_process_info *lpi;
771 CERROR("cannot alloc pointer\n");
774 lpi->lpi_loghandle = loghandle;
776 lpi->lpi_cbdata = data;
777 lpi->lpi_catdata = catdata;
780 struct task_struct *task;
782 /* The new thread can't use parent env,
783 * init the new one in llog_process_thread_daemonize. */
785 init_completion(&lpi->lpi_completion);
786 /* take reference to current, so that
787 * llog_process_thread_daemonize() can use it to switch to
788 * namespace associated with current */
789 lpi->lpi_reftask = current;
790 task = kthread_run(llog_process_thread_daemonize, lpi,
791 "llog_process_thread");
794 CERROR("%s: cannot start thread: rc = %d\n",
795 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
798 wait_for_completion(&lpi->lpi_completion);
801 llog_process_thread(lpi);
809 EXPORT_SYMBOL(llog_process_or_fork);
811 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
812 llog_cb_t cb, void *data, void *catdata)
815 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
816 return rc == LLOG_DEL_PLAIN ? 0 : rc;
818 EXPORT_SYMBOL(llog_process);
820 int llog_reverse_process(const struct lu_env *env,
821 struct llog_handle *loghandle, llog_cb_t cb,
822 void *data, void *catdata)
824 struct llog_log_hdr *llh = loghandle->lgh_hdr;
825 struct llog_process_cat_data *cd = catdata;
827 int rc = 0, first_index = 1, index, idx;
828 __u32 chunk_size = llh->llh_hdr.lrh_len;
831 OBD_ALLOC_LARGE(buf, chunk_size);
836 first_index = cd->lpcd_first_idx + 1;
837 if (cd != NULL && cd->lpcd_last_idx)
838 index = cd->lpcd_last_idx;
840 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
843 struct llog_rec_hdr *rec;
844 struct llog_rec_tail *tail;
846 /* skip records not set in bitmap */
847 while (index >= first_index &&
848 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
851 LASSERT(index >= first_index - 1);
852 if (index == first_index - 1)
855 /* get the buf with our target record; avoid old garbage */
856 memset(buf, 0, chunk_size);
857 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
862 idx = rec->lrh_index;
863 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
864 while (idx < index) {
865 rec = (void *)rec + rec->lrh_len;
866 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
867 lustre_swab_llog_rec(rec);
870 LASSERT(idx == index);
871 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
873 /* process records in buffer, starting where we found one */
874 while ((void *)tail > buf) {
875 if (tail->lrt_index == 0)
876 GOTO(out, rc = 0); /* no more records */
878 /* if set, process the callback on this record */
879 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
880 rec = (void *)tail - tail->lrt_len +
883 rc = cb(env, loghandle, rec, data);
884 if (rc == LLOG_PROC_BREAK) {
886 } else if (rc == LLOG_DEL_RECORD) {
887 rc = llog_cancel_rec(env, loghandle,
894 /* previous record, still in buffer? */
896 if (index < first_index)
898 tail = (void *)tail - tail->lrt_len;
904 OBD_FREE_LARGE(buf, chunk_size);
907 EXPORT_SYMBOL(llog_reverse_process);
913 * llog_open - open llog, may not exist
914 * llog_exist - check if llog exists
915 * llog_close - close opened llog, pair for open, frees llog_handle
916 * llog_declare_create - declare llog creation
917 * llog_create - create new llog on disk, need transaction handle
918 * llog_declare_write_rec - declaration of llog write
919 * llog_write_rec - write llog record on disk, need transaction handle
920 * llog_declare_add - declare llog catalog record addition
921 * llog_add - add llog record in catalog, need transaction handle
923 int llog_exist(struct llog_handle *loghandle)
925 struct llog_operations *lop;
930 rc = llog_handle2ops(loghandle, &lop);
933 if (lop->lop_exist == NULL)
936 rc = lop->lop_exist(loghandle);
939 EXPORT_SYMBOL(llog_exist);
941 int llog_declare_create(const struct lu_env *env,
942 struct llog_handle *loghandle, struct thandle *th)
944 struct llog_operations *lop;
949 rc = llog_handle2ops(loghandle, &lop);
952 if (lop->lop_declare_create == NULL)
955 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
957 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
958 rc = lop->lop_declare_create(env, loghandle, th);
960 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
964 int llog_create(const struct lu_env *env, struct llog_handle *handle,
967 struct llog_operations *lop;
972 rc = llog_handle2ops(handle, &lop);
975 if (lop->lop_create == NULL)
978 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
980 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
981 rc = lop->lop_create(env, handle, th);
983 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
987 int llog_declare_write_rec(const struct lu_env *env,
988 struct llog_handle *handle,
989 struct llog_rec_hdr *rec, int idx,
992 struct llog_operations *lop;
997 rc = llog_handle2ops(handle, &lop);
1001 if (lop->lop_declare_write_rec == NULL)
1002 RETURN(-EOPNOTSUPP);
1004 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1006 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1007 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1009 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1013 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1014 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1015 int idx, struct thandle *th)
1017 struct llog_operations *lop;
1018 int raised, rc, buflen;
1022 /* API sanity checks */
1023 if (handle == NULL) {
1024 CERROR("loghandle is missed\n");
1026 } else if (handle->lgh_obj == NULL) {
1027 CERROR("loghandle %p with NULL object\n",
1030 } else if (th == NULL) {
1031 CERROR("%s: missed transaction handle\n",
1032 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1034 } else if (handle->lgh_hdr == NULL) {
1035 CERROR("%s: loghandle %p with no header\n",
1036 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1041 rc = llog_handle2ops(handle, &lop);
1045 if (lop->lop_write_rec == NULL)
1046 RETURN(-EOPNOTSUPP);
1048 buflen = rec->lrh_len;
1049 LASSERT(cfs_size_round(buflen) == buflen);
1051 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1053 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1054 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1056 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1060 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1061 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1068 if (lgh->lgh_logops->lop_add == NULL)
1069 RETURN(-EOPNOTSUPP);
1071 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1073 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1074 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1076 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1079 EXPORT_SYMBOL(llog_add);
1081 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1082 struct llog_rec_hdr *rec, struct thandle *th)
1088 if (lgh->lgh_logops->lop_declare_add == NULL)
1089 RETURN(-EOPNOTSUPP);
1091 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1093 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1094 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1096 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1099 EXPORT_SYMBOL(llog_declare_add);
1102 * Helper function to open llog or create it if doesn't exist.
1103 * It hides all transaction handling from caller.
1105 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1106 struct llog_handle **res, struct llog_logid *logid,
1109 struct dt_device *d;
1115 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1119 if (llog_exist(*res))
1122 LASSERT((*res)->lgh_obj != NULL);
1124 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1126 if (unlikely(unlikely(d->dd_rdonly)))
1129 th = dt_trans_create(env, d);
1131 GOTO(out, rc = PTR_ERR(th));
1133 /* Create update llog object synchronously, which
1134 * happens during inialization process see
1135 * lod_sub_prep_llog(), to make sure the update
1136 * llog object is created before corss-MDT writing
1137 * updates into the llog object */
1138 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1141 th->th_wait_submit = 1;
1142 rc = llog_declare_create(env, *res, th);
1144 rc = dt_trans_start_local(env, d, th);
1146 rc = llog_create(env, *res, th);
1148 dt_trans_stop(env, d, th);
1151 llog_close(env, *res);
1154 EXPORT_SYMBOL(llog_open_create);
1157 * Helper function to delete existent llog.
1159 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1160 struct llog_logid *logid, char *name)
1162 struct llog_handle *handle;
1167 /* nothing to erase */
1168 if (name == NULL && logid == NULL)
1171 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1175 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1177 rc = llog_destroy(env, handle);
1179 rc2 = llog_close(env, handle);
1184 EXPORT_SYMBOL(llog_erase);
1187 * Helper function for write record in llog.
1188 * It hides all transaction handling from caller.
1189 * Valid only with local llog.
1191 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1192 struct llog_rec_hdr *rec, int idx)
1194 struct dt_device *dt;
1202 LASSERT(loghandle->lgh_ctxt);
1203 LASSERT(loghandle->lgh_obj != NULL);
1205 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1207 if (unlikely(unlikely(dt->dd_rdonly)))
1210 th = dt_trans_create(env, dt);
1212 RETURN(PTR_ERR(th));
1214 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1216 GOTO(out_trans, rc);
1218 th->th_wait_submit = 1;
1219 rc = dt_trans_start_local(env, dt, th);
1221 GOTO(out_trans, rc);
1223 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1225 down_write(&loghandle->lgh_lock);
1227 struct llog_thread_info *lti = llog_info(env);
1229 /* cookie comes from llog_process_thread */
1230 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1231 rec->lrh_index, th);
1232 /* upper layer didn`t pass cookie so change rc */
1233 rc = (rc == 1 ? 0 : rc);
1235 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1238 up_write(&loghandle->lgh_lock);
1240 dt_trans_stop(env, dt, th);
1243 EXPORT_SYMBOL(llog_write);
1245 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1246 struct llog_handle **lgh, struct llog_logid *logid,
1247 char *name, enum llog_open_param open_param)
1255 LASSERT(ctxt->loc_logops);
1257 if (ctxt->loc_logops->lop_open == NULL) {
1259 RETURN(-EOPNOTSUPP);
1262 *lgh = llog_alloc_handle();
1265 (*lgh)->lgh_ctxt = ctxt;
1266 (*lgh)->lgh_logops = ctxt->loc_logops;
1268 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1270 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1271 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1273 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1275 llog_free_handle(*lgh);
1280 EXPORT_SYMBOL(llog_open);
1282 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1284 struct llog_operations *lop;
1289 rc = llog_handle2ops(loghandle, &lop);
1292 if (lop->lop_close == NULL)
1293 GOTO(out, rc = -EOPNOTSUPP);
1294 rc = lop->lop_close(env, loghandle);
1296 llog_handle_put(loghandle);
1299 EXPORT_SYMBOL(llog_close);
1302 * Helper function to get the llog size in records. It is used by MGS
1303 * mostly to check that config llog exists and contains data.
1305 * \param[in] env execution environment
1306 * \param[in] ctxt llog context
1307 * \param[in] name llog name
1309 * \retval true if there are records in llog besides a header
1310 * \retval false on error or llog without records
1312 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1315 struct llog_handle *llh;
1318 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1320 if (likely(rc == -ENOENT))
1325 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1327 GOTO(out_close, rc);
1328 rc = llog_get_size(llh);
1331 llog_close(env, llh);
1333 /* The header is record 1, the llog is still considered as empty
1334 * if there is only header */
1337 EXPORT_SYMBOL(llog_is_empty);
1339 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1340 struct llog_rec_hdr *rec, void *data)
1342 struct llog_handle *copy_llh = data;
1344 /* Append all records */
1345 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1348 /* backup plain llog */
1349 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1350 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1351 char *name, char *backup)
1353 struct llog_handle *llh, *bllh;
1358 /* open original log */
1359 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1361 /* the -ENOENT case is also reported to the caller
1362 * but silently so it should handle that if needed.
1365 CERROR("%s: failed to open log %s: rc = %d\n",
1366 obd->obd_name, name, rc);
1370 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1372 GOTO(out_close, rc);
1374 /* Make sure there's no old backup log */
1375 rc = llog_erase(env, bctxt, NULL, backup);
1376 if (rc < 0 && rc != -ENOENT)
1377 GOTO(out_close, rc);
1379 /* open backup log */
1380 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1382 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1383 obd->obd_name, backup, rc);
1384 GOTO(out_close, rc);
1387 /* check that backup llog is not the same object as original one */
1388 if (llh->lgh_obj == bllh->lgh_obj) {
1389 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1390 obd->obd_name, name, backup, llh->lgh_obj,
1392 GOTO(out_backup, rc = -EEXIST);
1395 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1397 GOTO(out_backup, rc);
1399 /* Copy log record by record */
1400 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1403 CERROR("%s: failed to backup log %s: rc = %d\n",
1404 obd->obd_name, name, rc);
1406 llog_close(env, bllh);
1408 llog_close(env, llh);
1411 EXPORT_SYMBOL(llog_backup);
1413 /* Get size of llog */
1414 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1419 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1421 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1422 llh->lgh_ctxt->loc_obd->obd_name,
1423 PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1429 EXPORT_SYMBOL(llog_size);