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, 2016, 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 = loghandle->lgh_hdr;
203 bool subtract_count = false;
207 CDEBUG(D_RPCTRACE, "Canceling %d in log "DFID"\n", index,
208 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
211 CERROR("Can't cancel index 0 which is header\n");
215 LASSERT(loghandle != NULL);
216 LASSERT(loghandle->lgh_ctxt != NULL);
217 LASSERT(loghandle->lgh_obj != NULL);
219 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
221 th = dt_trans_create(env, dt);
225 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
229 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
230 rc = llog_declare_destroy(env, loghandle, th);
235 th->th_wait_submit = 1;
236 rc = dt_trans_start_local(env, dt, th);
240 down_write(&loghandle->lgh_lock);
242 mutex_lock(&loghandle->lgh_hdr_mutex);
243 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
244 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
245 GOTO(out_unlock, rc);
248 loghandle->lgh_hdr->llh_count--;
249 subtract_count = true;
250 /* Pass this index to llog_osd_write_rec(), which will use the index
251 * to only update the necesary bitmap. */
252 lgi->lgi_cookie.lgc_index = index;
254 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
255 LLOG_HEADER_IDX, th);
257 GOTO(out_unlock, rc);
259 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
260 (llh->llh_count == 1) &&
261 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
262 (loghandle->u.phd.phd_cat_handle != NULL &&
263 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
265 /* never try to destroy it again */
266 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
267 rc = llog_trans_destroy(env, loghandle, th);
269 /* Sigh, can not destroy the final plain llog, but
270 * the bitmap has been clearly, so the record can not
271 * be accessed anymore, let's return 0 for now, and
272 * the orphan will be handled by LFSCK. */
273 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
274 loghandle->lgh_ctxt->loc_obd->obd_name,
275 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
276 GOTO(out_unlock, rc = 0);
282 mutex_unlock(&loghandle->lgh_hdr_mutex);
283 up_write(&loghandle->lgh_lock);
285 rc1 = dt_trans_stop(env, dt, th);
288 if (rc < 0 && subtract_count) {
289 mutex_lock(&loghandle->lgh_hdr_mutex);
290 loghandle->lgh_hdr->llh_count++;
291 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
292 mutex_unlock(&loghandle->lgh_hdr_mutex);
297 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
298 const struct obd_uuid *uuid)
300 struct llog_operations *lop;
304 rc = llog_handle2ops(handle, &lop);
308 if (lop->lop_read_header == NULL)
311 rc = lop->lop_read_header(env, handle);
312 if (rc == LLOG_EEMPTY) {
313 struct llog_log_hdr *llh = handle->lgh_hdr;
315 /* lrh_len should be initialized in llog_init_handle */
316 handle->lgh_last_idx = 0; /* header is record with index 0 */
317 llh->llh_count = 1; /* for the header record */
318 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
319 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
320 LLOG_MIN_CHUNK_SIZE);
321 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
322 llh->llh_hdr.lrh_index = 0;
323 llh->llh_timestamp = ktime_get_real_seconds();
325 memcpy(&llh->llh_tgtuuid, uuid,
326 sizeof(llh->llh_tgtuuid));
327 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
328 /* Since update llog header might also call this function,
329 * let's reset the bitmap to 0 here */
330 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
331 llh->llh_bitmap_offset -
332 sizeof(llh->llh_tail));
333 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
334 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
335 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
340 EXPORT_SYMBOL(llog_read_header);
342 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
343 int flags, struct obd_uuid *uuid)
345 struct llog_log_hdr *llh;
346 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
348 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
351 LASSERT(handle->lgh_hdr == NULL);
353 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
354 OBD_ALLOC_LARGE(llh, chunk_size);
358 handle->lgh_hdr = llh;
359 handle->lgh_hdr_size = chunk_size;
360 /* first assign flags to use llog_client_ops */
361 llh->llh_flags = flags;
362 rc = llog_read_header(env, handle, uuid);
364 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
365 flags & LLOG_F_IS_CAT) ||
366 (llh->llh_flags & LLOG_F_IS_CAT &&
367 flags & LLOG_F_IS_PLAIN))) {
368 CERROR("%s: llog type is %s but initializing %s\n",
369 handle->lgh_ctxt->loc_obd->obd_name,
370 llh->llh_flags & LLOG_F_IS_CAT ?
372 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
373 GOTO(out, rc = -EINVAL);
374 } else if (llh->llh_flags &
375 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
377 * it is possible to open llog without specifying llog
378 * type so it is taken from llh_flags
380 flags = llh->llh_flags;
382 /* for some reason the llh_flags has no type set */
383 CERROR("llog type is not specified!\n");
384 GOTO(out, rc = -EINVAL);
387 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
388 CERROR("%s: llog uuid mismatch: %s/%s\n",
389 handle->lgh_ctxt->loc_obd->obd_name,
391 (char *)llh->llh_tgtuuid.uuid);
392 GOTO(out, rc = -EEXIST);
395 if (flags & LLOG_F_IS_CAT) {
396 LASSERT(list_empty(&handle->u.chd.chd_head));
397 INIT_LIST_HEAD(&handle->u.chd.chd_head);
398 llh->llh_size = sizeof(struct llog_logid_rec);
399 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
400 } else if (!(flags & LLOG_F_IS_PLAIN)) {
401 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
402 handle->lgh_ctxt->loc_obd->obd_name,
403 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
406 llh->llh_flags |= fmt;
409 OBD_FREE_LARGE(llh, chunk_size);
410 handle->lgh_hdr = NULL;
414 EXPORT_SYMBOL(llog_init_handle);
416 static int llog_process_thread(void *arg)
418 struct llog_process_info *lpi = arg;
419 struct llog_handle *loghandle = lpi->lpi_loghandle;
420 struct llog_log_hdr *llh = loghandle->lgh_hdr;
421 struct llog_process_cat_data *cd = lpi->lpi_catdata;
425 int rc = 0, index = 1, last_index;
427 int last_called_index = 0;
428 bool repeated = false;
435 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
436 /* expect chunk_size to be power of two */
437 LASSERT(is_power_of_2(chunk_size));
439 OBD_ALLOC_LARGE(buf, chunk_size);
441 lpi->lpi_rc = -ENOMEM;
446 last_called_index = cd->lpcd_first_idx;
447 index = cd->lpcd_first_idx + 1;
449 if (cd != NULL && cd->lpcd_last_idx)
450 last_index = cd->lpcd_last_idx;
452 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
455 struct llog_rec_hdr *rec;
456 off_t chunk_offset = 0;
457 unsigned int buf_offset = 0;
461 /* skip records not set in bitmap */
462 while (index <= last_index &&
463 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
466 /* There are no indices prior the last_index */
467 if (index > last_index)
470 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
474 /* get the buf with our target record; avoid old garbage */
475 memset(buf, 0, chunk_size);
476 /* the record index for outdated chunk data */
477 lh_last_idx = loghandle->lgh_last_idx + 1;
478 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
479 index, &cur_offset, buf, chunk_size);
481 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
482 " buf_offset %u, rc = %d\n", cur_offset,
483 (__u64)chunk_offset, buf_offset, rc);
484 /* we`ve tried to reread the chunk, but there is no
486 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
492 /* NB: after llog_next_block() call the cur_offset is the
493 * offset of the next block after read one.
494 * The absolute offset of the current chunk is calculated
495 * from cur_offset value and stored in chunk_offset variable.
497 if ((cur_offset & (chunk_size - 1)) != 0) {
498 partial_chunk = true;
499 chunk_offset = cur_offset & ~(chunk_size - 1);
501 partial_chunk = false;
502 chunk_offset = cur_offset - chunk_size;
505 /* NB: when rec->lrh_len is accessed it is already swabbed
506 * since it is used at the "end" of the loop and the rec
507 * swabbing is done at the beginning of the loop. */
508 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
509 (char *)rec < buf + chunk_size;
510 rec = llog_rec_hdr_next(rec)) {
512 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
515 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
516 lustre_swab_llog_rec(rec);
518 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
519 rec->lrh_type, rec->lrh_index);
521 /* the bitmap could be changed during processing
522 * records from the chunk. For wrapped catalog
523 * it means we can read deleted record and try to
524 * process it. Check this case and reread the chunk. */
526 /* for partial chunk the end of it is zeroed, check
527 * for index 0 to distinguish it. */
528 if ((partial_chunk && rec->lrh_index == 0) ||
529 (index == lh_last_idx &&
530 lh_last_idx != (loghandle->lgh_last_idx + 1))) {
531 /* concurrent llog_add() might add new records
532 * while llog_processing, check this is not
533 * the case and re-read the current chunk
536 /* lgh_last_idx could be less then index
537 * for catalog, if catalog is wrapped */
538 if ((index > loghandle->lgh_last_idx &&
539 !(loghandle->lgh_hdr->llh_flags &
540 LLOG_F_IS_CAT)) || repeated ||
541 (loghandle->lgh_obj != NULL &&
542 dt_object_remote(loghandle->lgh_obj)))
544 /* <2 records means no more records
545 * if the last record we processed was
546 * the final one, then the underlying
547 * object might have been destroyed yet.
548 * we better don't access that.. */
549 mutex_lock(&loghandle->lgh_hdr_mutex);
550 records = loghandle->lgh_hdr->llh_count;
551 mutex_unlock(&loghandle->lgh_hdr_mutex);
554 CDEBUG(D_OTHER, "Re-read last llog buffer for "
555 "new records, index %u, last %u\n",
556 index, loghandle->lgh_last_idx);
557 /* save offset inside buffer for the re-read */
558 buf_offset = (char *)rec - (char *)buf;
559 cur_offset = chunk_offset;
566 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
567 CWARN("%s: invalid length %d in llog "DFID
568 "record for index %d/%d\n",
569 loghandle->lgh_ctxt->loc_obd->obd_name,
571 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
572 rec->lrh_index, index);
574 GOTO(out, rc = -EINVAL);
577 if (rec->lrh_index < index) {
578 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
583 if (rec->lrh_index != index) {
584 CERROR("%s: "DFID" Invalid record: index %u"
585 " but expected %u\n",
586 loghandle->lgh_ctxt->loc_obd->obd_name,
587 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
588 rec->lrh_index, index);
589 GOTO(out, rc = -ERANGE);
593 "lrh_index: %d lrh_len: %d (%d remains)\n",
594 rec->lrh_index, rec->lrh_len,
595 (int)(buf + chunk_size - (char *)rec));
597 loghandle->lgh_cur_idx = rec->lrh_index;
598 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
601 /* if set, process the callback on this record */
602 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
603 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
605 last_called_index = index;
606 if (rc == LLOG_PROC_BREAK) {
608 } else if (rc == LLOG_DEL_RECORD) {
609 rc = llog_cancel_rec(lpi->lpi_env,
615 /* some stupid callbacks directly cancel records
616 * and delete llog. Check it and stop
618 if (loghandle->lgh_hdr == NULL ||
619 loghandle->lgh_hdr->llh_count == 1)
622 /* exit if the last index is reached */
623 if (index >= last_index)
631 cd->lpcd_last_idx = last_called_index;
633 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
634 if (dt_object_remote(loghandle->lgh_obj)) {
635 /* If it is remote object, then -EIO might means
636 * disconnection or eviction, let's return -EAGAIN,
637 * so for update recovery log processing, it will
638 * retry until the umount or abort recovery, see
639 * lod_sub_recovery_thread() */
640 CERROR("%s retry remote llog process\n",
641 loghandle->lgh_ctxt->loc_obd->obd_name);
644 /* something bad happened to the processing of a local
645 * llog file, probably I/O error or the log got
646 * corrupted to be able to finally release the log we
647 * discard any remaining bits in the header */
648 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
649 " %s index %d count %d\n",
650 loghandle->lgh_ctxt->loc_obd->obd_name,
651 POSTID(&loghandle->lgh_id.lgl_oi),
652 loghandle->lgh_id.lgl_ogen,
653 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
654 "plain"), index, llh->llh_count);
656 while (index <= last_index) {
657 if (ext2_test_bit(index,
658 LLOG_HDR_BITMAP(llh)) != 0)
659 llog_cancel_rec(lpi->lpi_env, loghandle,
667 OBD_FREE_LARGE(buf, chunk_size);
672 static int llog_process_thread_daemonize(void *arg)
674 struct llog_process_info *lpi = arg;
677 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
679 task_lock(lpi->lpi_reftask);
680 new_ns = lpi->lpi_reftask->nsproxy;
681 if (curr_ns != new_ns) {
684 current->nsproxy = new_ns;
685 /* XXX: we should call put_nsproxy() instead of
686 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
687 * used outside of the kernel itself, because it calls
688 * free_nsproxy() which is not exported by the kernel
689 * (defined in kernel/nsproxy.c) */
690 atomic_dec(&curr_ns->count);
692 task_unlock(lpi->lpi_reftask);
696 /* client env has no keys, tags is just 0 */
697 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
702 rc = llog_process_thread(arg);
706 complete(&lpi->lpi_completion);
710 int llog_process_or_fork(const struct lu_env *env,
711 struct llog_handle *loghandle,
712 llog_cb_t cb, void *data, void *catdata, bool fork)
714 struct llog_process_info *lpi;
721 CERROR("cannot alloc pointer\n");
724 lpi->lpi_loghandle = loghandle;
726 lpi->lpi_cbdata = data;
727 lpi->lpi_catdata = catdata;
730 struct task_struct *task;
732 /* The new thread can't use parent env,
733 * init the new one in llog_process_thread_daemonize. */
735 init_completion(&lpi->lpi_completion);
736 /* take reference to current, so that
737 * llog_process_thread_daemonize() can use it to switch to
738 * namespace associated with current */
739 lpi->lpi_reftask = current;
740 task = kthread_run(llog_process_thread_daemonize, lpi,
741 "llog_process_thread");
744 CERROR("%s: cannot start thread: rc = %d\n",
745 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
748 wait_for_completion(&lpi->lpi_completion);
751 llog_process_thread(lpi);
759 EXPORT_SYMBOL(llog_process_or_fork);
761 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
762 llog_cb_t cb, void *data, void *catdata)
765 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
766 return rc == LLOG_DEL_PLAIN ? 0 : rc;
768 EXPORT_SYMBOL(llog_process);
770 int llog_reverse_process(const struct lu_env *env,
771 struct llog_handle *loghandle, llog_cb_t cb,
772 void *data, void *catdata)
774 struct llog_log_hdr *llh = loghandle->lgh_hdr;
775 struct llog_process_cat_data *cd = catdata;
777 int rc = 0, first_index = 1, index, idx;
778 __u32 chunk_size = llh->llh_hdr.lrh_len;
781 OBD_ALLOC_LARGE(buf, chunk_size);
786 first_index = cd->lpcd_first_idx + 1;
787 if (cd != NULL && cd->lpcd_last_idx)
788 index = cd->lpcd_last_idx;
790 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
793 struct llog_rec_hdr *rec;
794 struct llog_rec_tail *tail;
796 /* skip records not set in bitmap */
797 while (index >= first_index &&
798 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
801 LASSERT(index >= first_index - 1);
802 if (index == first_index - 1)
805 /* get the buf with our target record; avoid old garbage */
806 memset(buf, 0, chunk_size);
807 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
812 idx = rec->lrh_index;
813 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
814 while (idx < index) {
815 rec = (void *)rec + rec->lrh_len;
816 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
817 lustre_swab_llog_rec(rec);
820 LASSERT(idx == index);
821 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
823 /* process records in buffer, starting where we found one */
824 while ((void *)tail > buf) {
825 if (tail->lrt_index == 0)
826 GOTO(out, rc = 0); /* no more records */
828 /* if set, process the callback on this record */
829 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
830 rec = (void *)tail - tail->lrt_len +
833 rc = cb(env, loghandle, rec, data);
834 if (rc == LLOG_PROC_BREAK) {
836 } else if (rc == LLOG_DEL_RECORD) {
837 rc = llog_cancel_rec(env, loghandle,
844 /* previous record, still in buffer? */
846 if (index < first_index)
848 tail = (void *)tail - tail->lrt_len;
854 OBD_FREE_LARGE(buf, chunk_size);
857 EXPORT_SYMBOL(llog_reverse_process);
863 * llog_open - open llog, may not exist
864 * llog_exist - check if llog exists
865 * llog_close - close opened llog, pair for open, frees llog_handle
866 * llog_declare_create - declare llog creation
867 * llog_create - create new llog on disk, need transaction handle
868 * llog_declare_write_rec - declaration of llog write
869 * llog_write_rec - write llog record on disk, need transaction handle
870 * llog_declare_add - declare llog catalog record addition
871 * llog_add - add llog record in catalog, need transaction handle
873 int llog_exist(struct llog_handle *loghandle)
875 struct llog_operations *lop;
880 rc = llog_handle2ops(loghandle, &lop);
883 if (lop->lop_exist == NULL)
886 rc = lop->lop_exist(loghandle);
889 EXPORT_SYMBOL(llog_exist);
891 int llog_declare_create(const struct lu_env *env,
892 struct llog_handle *loghandle, struct thandle *th)
894 struct llog_operations *lop;
899 rc = llog_handle2ops(loghandle, &lop);
902 if (lop->lop_declare_create == NULL)
905 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
907 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
908 rc = lop->lop_declare_create(env, loghandle, th);
910 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
914 int llog_create(const struct lu_env *env, struct llog_handle *handle,
917 struct llog_operations *lop;
922 rc = llog_handle2ops(handle, &lop);
925 if (lop->lop_create == NULL)
928 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
930 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
931 rc = lop->lop_create(env, handle, th);
933 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
937 int llog_declare_write_rec(const struct lu_env *env,
938 struct llog_handle *handle,
939 struct llog_rec_hdr *rec, int idx,
942 struct llog_operations *lop;
947 rc = llog_handle2ops(handle, &lop);
951 if (lop->lop_declare_write_rec == NULL)
954 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
956 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
957 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
959 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
963 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
964 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
965 int idx, struct thandle *th)
967 struct llog_operations *lop;
968 int raised, rc, buflen;
972 /* API sanity checks */
973 if (handle == NULL) {
974 CERROR("loghandle is missed\n");
976 } else if (handle->lgh_obj == NULL) {
977 CERROR("loghandle %p with NULL object\n",
980 } else if (th == NULL) {
981 CERROR("%s: missed transaction handle\n",
982 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
984 } else if (handle->lgh_hdr == NULL) {
985 CERROR("%s: loghandle %p with no header\n",
986 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
991 rc = llog_handle2ops(handle, &lop);
995 if (lop->lop_write_rec == NULL)
998 buflen = rec->lrh_len;
999 LASSERT(cfs_size_round(buflen) == buflen);
1001 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1003 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1004 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1006 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1010 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1011 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1018 if (lgh->lgh_logops->lop_add == NULL)
1019 RETURN(-EOPNOTSUPP);
1021 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1023 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1024 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1026 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1029 EXPORT_SYMBOL(llog_add);
1031 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1032 struct llog_rec_hdr *rec, struct thandle *th)
1038 if (lgh->lgh_logops->lop_declare_add == NULL)
1039 RETURN(-EOPNOTSUPP);
1041 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1043 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1044 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1046 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1049 EXPORT_SYMBOL(llog_declare_add);
1052 * Helper function to open llog or create it if doesn't exist.
1053 * It hides all transaction handling from caller.
1055 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1056 struct llog_handle **res, struct llog_logid *logid,
1059 struct dt_device *d;
1065 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1069 if (llog_exist(*res))
1072 LASSERT((*res)->lgh_obj != NULL);
1074 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1076 th = dt_trans_create(env, d);
1078 GOTO(out, rc = PTR_ERR(th));
1080 /* Create update llog object synchronously, which
1081 * happens during inialization process see
1082 * lod_sub_prep_llog(), to make sure the update
1083 * llog object is created before corss-MDT writing
1084 * updates into the llog object */
1085 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1088 th->th_wait_submit = 1;
1089 rc = llog_declare_create(env, *res, th);
1091 rc = dt_trans_start_local(env, d, th);
1093 rc = llog_create(env, *res, th);
1095 dt_trans_stop(env, d, th);
1098 llog_close(env, *res);
1101 EXPORT_SYMBOL(llog_open_create);
1104 * Helper function to delete existent llog.
1106 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1107 struct llog_logid *logid, char *name)
1109 struct llog_handle *handle;
1114 /* nothing to erase */
1115 if (name == NULL && logid == NULL)
1118 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1122 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1124 rc = llog_destroy(env, handle);
1126 rc2 = llog_close(env, handle);
1131 EXPORT_SYMBOL(llog_erase);
1134 * Helper function for write record in llog.
1135 * It hides all transaction handling from caller.
1136 * Valid only with local llog.
1138 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1139 struct llog_rec_hdr *rec, int idx)
1141 struct dt_device *dt;
1148 LASSERT(loghandle->lgh_ctxt);
1149 LASSERT(loghandle->lgh_obj != NULL);
1151 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1153 th = dt_trans_create(env, dt);
1155 RETURN(PTR_ERR(th));
1157 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1159 GOTO(out_trans, rc);
1161 th->th_wait_submit = 1;
1162 rc = dt_trans_start_local(env, dt, th);
1164 GOTO(out_trans, rc);
1166 down_write(&loghandle->lgh_lock);
1167 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1168 up_write(&loghandle->lgh_lock);
1170 dt_trans_stop(env, dt, th);
1173 EXPORT_SYMBOL(llog_write);
1175 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1176 struct llog_handle **lgh, struct llog_logid *logid,
1177 char *name, enum llog_open_param open_param)
1185 LASSERT(ctxt->loc_logops);
1187 if (ctxt->loc_logops->lop_open == NULL) {
1189 RETURN(-EOPNOTSUPP);
1192 *lgh = llog_alloc_handle();
1195 (*lgh)->lgh_ctxt = ctxt;
1196 (*lgh)->lgh_logops = ctxt->loc_logops;
1198 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1200 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1201 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1203 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1205 llog_free_handle(*lgh);
1210 EXPORT_SYMBOL(llog_open);
1212 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1214 struct llog_operations *lop;
1219 rc = llog_handle2ops(loghandle, &lop);
1222 if (lop->lop_close == NULL)
1223 GOTO(out, rc = -EOPNOTSUPP);
1224 rc = lop->lop_close(env, loghandle);
1226 llog_handle_put(loghandle);
1229 EXPORT_SYMBOL(llog_close);
1232 * Helper function to get the llog size in records. It is used by MGS
1233 * mostly to check that config llog exists and contains data.
1235 * \param[in] env execution environment
1236 * \param[in] ctxt llog context
1237 * \param[in] name llog name
1239 * \retval true if there are records in llog besides a header
1240 * \retval false on error or llog without records
1242 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1245 struct llog_handle *llh;
1248 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1250 if (likely(rc == -ENOENT))
1255 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1257 GOTO(out_close, rc);
1258 rc = llog_get_size(llh);
1261 llog_close(env, llh);
1263 /* The header is record 1, the llog is still considered as empty
1264 * if there is only header */
1267 EXPORT_SYMBOL(llog_is_empty);
1269 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1270 struct llog_rec_hdr *rec, void *data)
1272 struct llog_handle *copy_llh = data;
1274 /* Append all records */
1275 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1278 /* backup plain llog */
1279 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1280 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1281 char *name, char *backup)
1283 struct llog_handle *llh, *bllh;
1288 /* open original log */
1289 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1291 /* the -ENOENT case is also reported to the caller
1292 * but silently so it should handle that if needed.
1295 CERROR("%s: failed to open log %s: rc = %d\n",
1296 obd->obd_name, name, rc);
1300 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1302 GOTO(out_close, rc);
1304 /* Make sure there's no old backup log */
1305 rc = llog_erase(env, bctxt, NULL, backup);
1306 if (rc < 0 && rc != -ENOENT)
1307 GOTO(out_close, rc);
1309 /* open backup log */
1310 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1312 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1313 obd->obd_name, backup, rc);
1314 GOTO(out_close, rc);
1317 /* check that backup llog is not the same object as original one */
1318 if (llh->lgh_obj == bllh->lgh_obj) {
1319 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1320 obd->obd_name, name, backup, llh->lgh_obj,
1322 GOTO(out_backup, rc = -EEXIST);
1325 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1327 GOTO(out_backup, rc);
1329 /* Copy log record by record */
1330 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1333 CERROR("%s: failed to backup log %s: rc = %d\n",
1334 obd->obd_name, name, rc);
1336 llog_close(env, bllh);
1338 llog_close(env, llh);
1341 EXPORT_SYMBOL(llog_backup);
1343 /* Get size of llog */
1344 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1349 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1351 CERROR("%s: attr_get failed, rc = %d\n",
1352 llh->lgh_ctxt->loc_obd->obd_name, rc);
1358 EXPORT_SYMBOL(llog_size);