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 "DOSTID"\n", index,
208 POSTID(&loghandle->lgh_id.lgl_oi));
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 #"DOSTID
275 loghandle->lgh_ctxt->loc_obd->obd_name,
276 POSTID(&loghandle->lgh_id.lgl_oi),
277 loghandle->lgh_id.lgl_ogen, rc);
278 GOTO(out_unlock, rc);
284 mutex_unlock(&loghandle->lgh_hdr_mutex);
285 up_write(&loghandle->lgh_lock);
287 rc1 = dt_trans_stop(env, dt, th);
290 if (rc < 0 && subtract_count) {
291 mutex_lock(&loghandle->lgh_hdr_mutex);
292 loghandle->lgh_hdr->llh_count++;
293 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
294 mutex_unlock(&loghandle->lgh_hdr_mutex);
299 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
300 const struct obd_uuid *uuid)
302 struct llog_operations *lop;
306 rc = llog_handle2ops(handle, &lop);
310 if (lop->lop_read_header == NULL)
313 rc = lop->lop_read_header(env, handle);
314 if (rc == LLOG_EEMPTY) {
315 struct llog_log_hdr *llh = handle->lgh_hdr;
317 /* lrh_len should be initialized in llog_init_handle */
318 handle->lgh_last_idx = 0; /* header is record with index 0 */
319 handle->lgh_write_offset = 0;
320 llh->llh_count = 1; /* for the header record */
321 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
322 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
323 LLOG_MIN_CHUNK_SIZE);
324 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
325 llh->llh_hdr.lrh_index = 0;
326 llh->llh_timestamp = cfs_time_current_sec();
328 memcpy(&llh->llh_tgtuuid, uuid,
329 sizeof(llh->llh_tgtuuid));
330 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
331 /* Since update llog header might also call this function,
332 * let's reset the bitmap to 0 here */
333 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
334 llh->llh_bitmap_offset -
335 sizeof(llh->llh_tail));
336 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
337 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
338 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
343 EXPORT_SYMBOL(llog_read_header);
345 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
346 int flags, struct obd_uuid *uuid)
348 struct llog_log_hdr *llh;
349 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
351 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
354 LASSERT(handle->lgh_hdr == NULL);
356 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
357 OBD_ALLOC_LARGE(llh, chunk_size);
361 handle->lgh_hdr = llh;
362 handle->lgh_hdr_size = chunk_size;
363 /* first assign flags to use llog_client_ops */
364 llh->llh_flags = flags;
365 rc = llog_read_header(env, handle, uuid);
367 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
368 flags & LLOG_F_IS_CAT) ||
369 (llh->llh_flags & LLOG_F_IS_CAT &&
370 flags & LLOG_F_IS_PLAIN))) {
371 CERROR("%s: llog type is %s but initializing %s\n",
372 handle->lgh_ctxt->loc_obd->obd_name,
373 llh->llh_flags & LLOG_F_IS_CAT ?
375 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
376 GOTO(out, rc = -EINVAL);
377 } else if (llh->llh_flags &
378 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
380 * it is possible to open llog without specifying llog
381 * type so it is taken from llh_flags
383 flags = llh->llh_flags;
385 /* for some reason the llh_flags has no type set */
386 CERROR("llog type is not specified!\n");
387 GOTO(out, rc = -EINVAL);
390 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
391 CERROR("%s: llog uuid mismatch: %s/%s\n",
392 handle->lgh_ctxt->loc_obd->obd_name,
394 (char *)llh->llh_tgtuuid.uuid);
395 GOTO(out, rc = -EEXIST);
398 if (flags & LLOG_F_IS_CAT) {
399 LASSERT(list_empty(&handle->u.chd.chd_head));
400 INIT_LIST_HEAD(&handle->u.chd.chd_head);
401 llh->llh_size = sizeof(struct llog_logid_rec);
402 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
403 } else if (!(flags & LLOG_F_IS_PLAIN)) {
404 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
405 handle->lgh_ctxt->loc_obd->obd_name,
406 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
409 llh->llh_flags |= fmt;
412 OBD_FREE_LARGE(llh, chunk_size);
413 handle->lgh_hdr = NULL;
417 EXPORT_SYMBOL(llog_init_handle);
419 static int llog_process_thread(void *arg)
421 struct llog_process_info *lpi = arg;
422 struct llog_handle *loghandle = lpi->lpi_loghandle;
423 struct llog_log_hdr *llh = loghandle->lgh_hdr;
424 struct llog_process_cat_data *cd = lpi->lpi_catdata;
427 __u64 cur_offset, tmp_offset;
428 int rc = 0, index = 1, last_index;
430 int last_called_index = 0;
437 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
438 /* expect chunk_size to be power of two */
439 LASSERT(is_power_of_2(chunk_size));
441 OBD_ALLOC_LARGE(buf, chunk_size);
443 lpi->lpi_rc = -ENOMEM;
448 last_called_index = cd->lpcd_first_idx;
449 index = cd->lpcd_first_idx + 1;
451 if (cd != NULL && cd->lpcd_last_idx)
452 last_index = cd->lpcd_last_idx;
454 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
457 struct llog_rec_hdr *rec;
459 unsigned int buf_offset = 0;
462 /* skip records not set in bitmap */
463 while (index <= last_index &&
464 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
467 /* There are no indices prior the last_index */
468 if (index > last_index)
471 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
475 /* get the buf with our target record; avoid old garbage */
476 memset(buf, 0, chunk_size);
477 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
478 index, &cur_offset, buf, chunk_size);
482 /* NB: after llog_next_block() call the cur_offset is the
483 * offset of the next block after read one.
484 * The absolute offset of the current chunk is calculated
485 * from cur_offset value and stored in chunk_offset variable.
487 tmp_offset = cur_offset;
488 if (do_div(tmp_offset, chunk_size) != 0) {
489 partial_chunk = true;
490 chunk_offset = cur_offset & ~(chunk_size - 1);
492 partial_chunk = false;
493 chunk_offset = cur_offset - chunk_size;
496 /* NB: when rec->lrh_len is accessed it is already swabbed
497 * since it is used at the "end" of the loop and the rec
498 * swabbing is done at the beginning of the loop. */
499 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
500 (char *)rec < buf + chunk_size;
501 rec = llog_rec_hdr_next(rec)) {
503 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
506 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
507 lustre_swab_llog_rec(rec);
509 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
510 rec->lrh_type, rec->lrh_index);
512 /* for partial chunk the end of it is zeroed, check
513 * for index 0 to distinguish it. */
514 if (partial_chunk && rec->lrh_index == 0) {
515 /* concurrent llog_add() might add new records
516 * while llog_processing, check this is not
517 * the case and re-read the current chunk
520 if (index > loghandle->lgh_last_idx)
522 /* <2 records means no more records
523 * if the last record we processed was
524 * the final one, then the underlying
525 * object might have been destroyed yet.
526 * we better don't access that.. */
527 mutex_lock(&loghandle->lgh_hdr_mutex);
528 records = loghandle->lgh_hdr->llh_count;
529 mutex_unlock(&loghandle->lgh_hdr_mutex);
532 CDEBUG(D_OTHER, "Re-read last llog buffer for "
533 "new records, index %u, last %u\n",
534 index, loghandle->lgh_last_idx);
535 /* save offset inside buffer for the re-read */
536 buf_offset = (char *)rec - (char *)buf;
537 cur_offset = chunk_offset;
541 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
542 CWARN("invalid length %d in llog record for "
543 "index %d/%d\n", rec->lrh_len,
544 rec->lrh_index, index);
545 GOTO(out, rc = -EINVAL);
548 if (rec->lrh_index < index) {
549 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
554 if (rec->lrh_index != index) {
555 CERROR("%s: Invalid record: index %u but "
557 loghandle->lgh_ctxt->loc_obd->obd_name,
558 rec->lrh_index, index);
559 GOTO(out, rc = -ERANGE);
563 "lrh_index: %d lrh_len: %d (%d remains)\n",
564 rec->lrh_index, rec->lrh_len,
565 (int)(buf + chunk_size - (char *)rec));
567 loghandle->lgh_cur_idx = rec->lrh_index;
568 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
571 /* if set, process the callback on this record */
572 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
573 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
575 last_called_index = index;
576 if (rc == LLOG_PROC_BREAK) {
578 } else if (rc == LLOG_DEL_RECORD) {
579 rc = llog_cancel_rec(lpi->lpi_env,
586 /* exit if the last index is reached */
587 if (index >= last_index)
595 cd->lpcd_last_idx = last_called_index;
597 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
598 if (dt_object_remote(loghandle->lgh_obj)) {
599 /* If it is remote object, then -EIO might means
600 * disconnection or eviction, let's return -EAGAIN,
601 * so for update recovery log processing, it will
602 * retry until the umount or abort recovery, see
603 * lod_sub_recovery_thread() */
604 CERROR("%s retry remote llog process\n",
605 loghandle->lgh_ctxt->loc_obd->obd_name);
608 /* something bad happened to the processing of a local
609 * llog file, probably I/O error or the log got
610 * corrupted to be able to finally release the log we
611 * discard any remaining bits in the header */
612 CERROR("Local llog found corrupted\n");
613 while (index <= last_index) {
614 if (ext2_test_bit(index,
615 LLOG_HDR_BITMAP(llh)) != 0)
616 llog_cancel_rec(lpi->lpi_env, loghandle,
624 OBD_FREE_LARGE(buf, chunk_size);
629 static int llog_process_thread_daemonize(void *arg)
631 struct llog_process_info *lpi = arg;
634 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
636 task_lock(lpi->lpi_reftask);
637 new_ns = lpi->lpi_reftask->nsproxy;
638 if (curr_ns != new_ns) {
641 current->nsproxy = new_ns;
642 /* XXX: we should call put_nsproxy() instead of
643 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
644 * used outside of the kernel itself, because it calls
645 * free_nsproxy() which is not exported by the kernel
646 * (defined in kernel/nsproxy.c) */
647 atomic_dec(&curr_ns->count);
649 task_unlock(lpi->lpi_reftask);
653 /* client env has no keys, tags is just 0 */
654 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
659 rc = llog_process_thread(arg);
663 complete(&lpi->lpi_completion);
667 int llog_process_or_fork(const struct lu_env *env,
668 struct llog_handle *loghandle,
669 llog_cb_t cb, void *data, void *catdata, bool fork)
671 struct llog_process_info *lpi;
678 CERROR("cannot alloc pointer\n");
681 lpi->lpi_loghandle = loghandle;
683 lpi->lpi_cbdata = data;
684 lpi->lpi_catdata = catdata;
687 struct task_struct *task;
689 /* The new thread can't use parent env,
690 * init the new one in llog_process_thread_daemonize. */
692 init_completion(&lpi->lpi_completion);
693 /* take reference to current, so that
694 * llog_process_thread_daemonize() can use it to switch to
695 * namespace associated with current */
696 lpi->lpi_reftask = current;
697 task = kthread_run(llog_process_thread_daemonize, lpi,
698 "llog_process_thread");
701 CERROR("%s: cannot start thread: rc = %d\n",
702 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
705 wait_for_completion(&lpi->lpi_completion);
708 llog_process_thread(lpi);
716 EXPORT_SYMBOL(llog_process_or_fork);
718 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
719 llog_cb_t cb, void *data, void *catdata)
722 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
723 return rc == LLOG_DEL_PLAIN ? 0 : rc;
725 EXPORT_SYMBOL(llog_process);
727 int llog_reverse_process(const struct lu_env *env,
728 struct llog_handle *loghandle, llog_cb_t cb,
729 void *data, void *catdata)
731 struct llog_log_hdr *llh = loghandle->lgh_hdr;
732 struct llog_process_cat_data *cd = catdata;
734 int rc = 0, first_index = 1, index, idx;
735 __u32 chunk_size = llh->llh_hdr.lrh_len;
738 OBD_ALLOC_LARGE(buf, chunk_size);
743 first_index = cd->lpcd_first_idx + 1;
744 if (cd != NULL && cd->lpcd_last_idx)
745 index = cd->lpcd_last_idx;
747 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
750 struct llog_rec_hdr *rec;
751 struct llog_rec_tail *tail;
753 /* skip records not set in bitmap */
754 while (index >= first_index &&
755 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
758 LASSERT(index >= first_index - 1);
759 if (index == first_index - 1)
762 /* get the buf with our target record; avoid old garbage */
763 memset(buf, 0, chunk_size);
764 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
769 idx = rec->lrh_index;
770 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
771 while (idx < index) {
772 rec = (void *)rec + rec->lrh_len;
773 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
774 lustre_swab_llog_rec(rec);
777 LASSERT(idx == index);
778 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
780 /* process records in buffer, starting where we found one */
781 while ((void *)tail > buf) {
782 if (tail->lrt_index == 0)
783 GOTO(out, rc = 0); /* no more records */
785 /* if set, process the callback on this record */
786 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
787 rec = (void *)tail - tail->lrt_len +
790 rc = cb(env, loghandle, rec, data);
791 if (rc == LLOG_PROC_BREAK) {
793 } else if (rc == LLOG_DEL_RECORD) {
794 rc = llog_cancel_rec(env, loghandle,
801 /* previous record, still in buffer? */
803 if (index < first_index)
805 tail = (void *)tail - tail->lrt_len;
811 OBD_FREE_LARGE(buf, chunk_size);
814 EXPORT_SYMBOL(llog_reverse_process);
820 * llog_open - open llog, may not exist
821 * llog_exist - check if llog exists
822 * llog_close - close opened llog, pair for open, frees llog_handle
823 * llog_declare_create - declare llog creation
824 * llog_create - create new llog on disk, need transaction handle
825 * llog_declare_write_rec - declaration of llog write
826 * llog_write_rec - write llog record on disk, need transaction handle
827 * llog_declare_add - declare llog catalog record addition
828 * llog_add - add llog record in catalog, need transaction handle
830 int llog_exist(struct llog_handle *loghandle)
832 struct llog_operations *lop;
837 rc = llog_handle2ops(loghandle, &lop);
840 if (lop->lop_exist == NULL)
843 rc = lop->lop_exist(loghandle);
846 EXPORT_SYMBOL(llog_exist);
848 int llog_declare_create(const struct lu_env *env,
849 struct llog_handle *loghandle, struct thandle *th)
851 struct llog_operations *lop;
856 rc = llog_handle2ops(loghandle, &lop);
859 if (lop->lop_declare_create == NULL)
862 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
864 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
865 rc = lop->lop_declare_create(env, loghandle, th);
867 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
871 int llog_create(const struct lu_env *env, struct llog_handle *handle,
874 struct llog_operations *lop;
879 rc = llog_handle2ops(handle, &lop);
882 if (lop->lop_create == NULL)
885 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
887 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
888 rc = lop->lop_create(env, handle, th);
890 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
894 int llog_declare_write_rec(const struct lu_env *env,
895 struct llog_handle *handle,
896 struct llog_rec_hdr *rec, int idx,
899 struct llog_operations *lop;
904 rc = llog_handle2ops(handle, &lop);
908 if (lop->lop_declare_write_rec == NULL)
911 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
913 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
914 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
916 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
920 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
921 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
922 int idx, struct thandle *th)
924 struct llog_operations *lop;
925 int raised, rc, buflen;
929 /* API sanity checks */
930 if (handle == NULL) {
931 CERROR("loghandle is missed\n");
933 } else if (handle->lgh_obj == NULL) {
934 CERROR("loghandle %p with NULL object\n",
937 } else if (th == NULL) {
938 CERROR("%s: missed transaction handle\n",
939 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
941 } else if (handle->lgh_hdr == NULL) {
942 CERROR("%s: loghandle %p with no header\n",
943 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
948 rc = llog_handle2ops(handle, &lop);
952 if (lop->lop_write_rec == NULL)
955 buflen = rec->lrh_len;
956 LASSERT(cfs_size_round(buflen) == buflen);
958 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
960 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
961 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
963 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
967 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
968 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
975 if (lgh->lgh_logops->lop_add == NULL)
978 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
980 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
981 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
983 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
986 EXPORT_SYMBOL(llog_add);
988 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
989 struct llog_rec_hdr *rec, struct thandle *th)
995 if (lgh->lgh_logops->lop_declare_add == NULL)
998 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1000 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1001 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1003 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1006 EXPORT_SYMBOL(llog_declare_add);
1009 * Helper function to open llog or create it if doesn't exist.
1010 * It hides all transaction handling from caller.
1012 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1013 struct llog_handle **res, struct llog_logid *logid,
1016 struct dt_device *d;
1022 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1026 if (llog_exist(*res))
1029 LASSERT((*res)->lgh_obj != NULL);
1031 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1033 th = dt_trans_create(env, d);
1035 GOTO(out, rc = PTR_ERR(th));
1037 /* Create update llog object synchronously, which
1038 * happens during inialization process see
1039 * lod_sub_prep_llog(), to make sure the update
1040 * llog object is created before corss-MDT writing
1041 * updates into the llog object */
1042 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1045 th->th_wait_submit = 1;
1046 rc = llog_declare_create(env, *res, th);
1048 rc = dt_trans_start_local(env, d, th);
1050 rc = llog_create(env, *res, th);
1052 dt_trans_stop(env, d, th);
1055 llog_close(env, *res);
1058 EXPORT_SYMBOL(llog_open_create);
1061 * Helper function to delete existent llog.
1063 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1064 struct llog_logid *logid, char *name)
1066 struct llog_handle *handle;
1071 /* nothing to erase */
1072 if (name == NULL && logid == NULL)
1075 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1079 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1081 rc = llog_destroy(env, handle);
1083 rc2 = llog_close(env, handle);
1088 EXPORT_SYMBOL(llog_erase);
1091 * Helper function for write record in llog.
1092 * It hides all transaction handling from caller.
1093 * Valid only with local llog.
1095 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1096 struct llog_rec_hdr *rec, int idx)
1098 struct dt_device *dt;
1105 LASSERT(loghandle->lgh_ctxt);
1106 LASSERT(loghandle->lgh_obj != NULL);
1108 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1110 th = dt_trans_create(env, dt);
1112 RETURN(PTR_ERR(th));
1114 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1116 GOTO(out_trans, rc);
1118 th->th_wait_submit = 1;
1119 rc = dt_trans_start_local(env, dt, th);
1121 GOTO(out_trans, rc);
1123 down_write(&loghandle->lgh_lock);
1124 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1125 up_write(&loghandle->lgh_lock);
1127 dt_trans_stop(env, dt, th);
1130 EXPORT_SYMBOL(llog_write);
1132 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1133 struct llog_handle **lgh, struct llog_logid *logid,
1134 char *name, enum llog_open_param open_param)
1142 LASSERT(ctxt->loc_logops);
1144 if (ctxt->loc_logops->lop_open == NULL) {
1146 RETURN(-EOPNOTSUPP);
1149 *lgh = llog_alloc_handle();
1152 (*lgh)->lgh_ctxt = ctxt;
1153 (*lgh)->lgh_logops = ctxt->loc_logops;
1155 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1157 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1158 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1160 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1162 llog_free_handle(*lgh);
1167 EXPORT_SYMBOL(llog_open);
1169 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1171 struct llog_operations *lop;
1176 rc = llog_handle2ops(loghandle, &lop);
1179 if (lop->lop_close == NULL)
1180 GOTO(out, rc = -EOPNOTSUPP);
1181 rc = lop->lop_close(env, loghandle);
1183 llog_handle_put(loghandle);
1186 EXPORT_SYMBOL(llog_close);
1189 * Helper function to get the llog size in records. It is used by MGS
1190 * mostly to check that config llog exists and contains data.
1192 * \param[in] env execution environment
1193 * \param[in] ctxt llog context
1194 * \param[in] name llog name
1196 * \retval true if there are records in llog besides a header
1197 * \retval false on error or llog without records
1199 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1202 struct llog_handle *llh;
1205 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1207 if (likely(rc == -ENOENT))
1212 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1214 GOTO(out_close, rc);
1215 rc = llog_get_size(llh);
1218 llog_close(env, llh);
1220 /* The header is record 1, the llog is still considered as empty
1221 * if there is only header */
1224 EXPORT_SYMBOL(llog_is_empty);
1226 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1227 struct llog_rec_hdr *rec, void *data)
1229 struct llog_handle *copy_llh = data;
1231 /* Append all records */
1232 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1235 /* backup plain llog */
1236 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1237 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1238 char *name, char *backup)
1240 struct llog_handle *llh, *bllh;
1245 /* open original log */
1246 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1248 /* the -ENOENT case is also reported to the caller
1249 * but silently so it should handle that if needed.
1252 CERROR("%s: failed to open log %s: rc = %d\n",
1253 obd->obd_name, name, rc);
1257 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1259 GOTO(out_close, rc);
1261 /* Make sure there's no old backup log */
1262 rc = llog_erase(env, bctxt, NULL, backup);
1263 if (rc < 0 && rc != -ENOENT)
1264 GOTO(out_close, rc);
1266 /* open backup log */
1267 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1269 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1270 obd->obd_name, backup, rc);
1271 GOTO(out_close, rc);
1274 /* check that backup llog is not the same object as original one */
1275 if (llh->lgh_obj == bllh->lgh_obj) {
1276 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1277 obd->obd_name, name, backup, llh->lgh_obj,
1279 GOTO(out_backup, rc = -EEXIST);
1282 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1284 GOTO(out_backup, rc);
1286 /* Copy log record by record */
1287 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1290 CERROR("%s: failed to backup log %s: rc = %d\n",
1291 obd->obd_name, name, rc);
1293 llog_close(env, bllh);
1295 llog_close(env, llh);
1298 EXPORT_SYMBOL(llog_backup);
1300 /* Get size of llog */
1301 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1306 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1308 CERROR("%s: attr_get failed, rc = %d\n",
1309 llh->lgh_ctxt->loc_obd->obd_name, rc);
1315 EXPORT_SYMBOL(llog_size);