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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog.c
38 * OST<->MDS recovery logging infrastructure.
39 * Invariants in implementation:
40 * - we do not share logs among different OST<->MDS connections, so that
41 * if an OST or MDS fails it need only look at log(s) relevant to itself
43 * Author: Andreas Dilger <adilger@clusterfs.com>
44 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
45 * Author: Mikhail Pershin <tappro@whamcloud.com>
48 #define DEBUG_SUBSYSTEM S_LOG
50 #include <linux/kthread.h>
51 #include <llog_swab.h>
52 #include <lustre_log.h>
53 #include <obd_class.h>
54 #include "llog_internal.h"
56 * Allocate a new log or catalog handle
57 * Used inside llog_open().
59 static struct llog_handle *llog_alloc_handle(void)
61 struct llog_handle *loghandle;
63 OBD_ALLOC_PTR(loghandle);
64 if (loghandle == NULL)
67 init_rwsem(&loghandle->lgh_lock);
68 mutex_init(&loghandle->lgh_hdr_mutex);
69 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
70 atomic_set(&loghandle->lgh_refcount, 1);
76 * Free llog handle and header data if exists. Used in llog_close() only
78 static void llog_free_handle(struct llog_handle *loghandle)
80 LASSERT(loghandle != NULL);
82 /* failed llog_init_handle */
83 if (loghandle->lgh_hdr == NULL)
86 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
87 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
88 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
89 LASSERT(list_empty(&loghandle->u.chd.chd_head));
90 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
92 OBD_FREE_PTR(loghandle);
95 void llog_handle_get(struct llog_handle *loghandle)
97 atomic_inc(&loghandle->lgh_refcount);
100 void llog_handle_put(struct llog_handle *loghandle)
102 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
103 if (atomic_dec_and_test(&loghandle->lgh_refcount))
104 llog_free_handle(loghandle);
107 static int llog_declare_destroy(const struct lu_env *env,
108 struct llog_handle *handle,
111 struct llog_operations *lop;
116 rc = llog_handle2ops(handle, &lop);
119 if (lop->lop_declare_destroy == NULL)
122 rc = lop->lop_declare_destroy(env, handle, th);
127 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
130 struct llog_operations *lop;
134 rc = llog_handle2ops(handle, &lop);
137 if (lop->lop_destroy == NULL)
140 LASSERT(handle->lgh_obj != NULL);
141 if (!dt_object_exists(handle->lgh_obj))
144 rc = lop->lop_destroy(env, handle, th);
149 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
151 struct llog_operations *lop;
152 struct dt_device *dt;
158 rc = llog_handle2ops(handle, &lop);
161 if (lop->lop_destroy == NULL)
164 if (handle->lgh_obj == NULL) {
165 /* if lgh_obj == NULL, then it is from client side destroy */
166 rc = lop->lop_destroy(env, handle, NULL);
170 if (!dt_object_exists(handle->lgh_obj))
173 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
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 = loghandle->lgh_hdr;
206 bool subtract_count = false;
210 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n", index,
211 POSTID(&loghandle->lgh_id.lgl_oi));
214 CERROR("Can't cancel index 0 which is header\n");
218 LASSERT(loghandle != NULL);
219 LASSERT(loghandle->lgh_ctxt != NULL);
220 LASSERT(loghandle->lgh_obj != NULL);
222 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
224 th = dt_trans_create(env, dt);
228 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
232 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
233 rc = llog_declare_destroy(env, loghandle, th);
238 th->th_wait_submit = 1;
239 rc = dt_trans_start_local(env, dt, th);
243 down_write(&loghandle->lgh_lock);
245 mutex_lock(&loghandle->lgh_hdr_mutex);
246 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
247 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
248 GOTO(out_unlock, rc);
251 loghandle->lgh_hdr->llh_count--;
252 subtract_count = true;
253 /* Pass this index to llog_osd_write_rec(), which will use the index
254 * to only update the necesary bitmap. */
255 lgi->lgi_cookie.lgc_index = index;
257 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
258 LLOG_HEADER_IDX, th);
260 GOTO(out_unlock, rc);
262 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
263 (llh->llh_count == 1) &&
264 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
265 (loghandle->u.phd.phd_cat_handle != NULL &&
266 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
268 /* never try to destroy it again */
269 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
270 rc = llog_trans_destroy(env, loghandle, th);
272 /* Sigh, can not destroy the final plain llog, but
273 * the bitmap has been clearly, so the record can not
274 * be accessed anymore, let's return 0 for now, and
275 * the orphan will be handled by LFSCK. */
276 CERROR("%s: can't destroy empty llog #"DOSTID
278 loghandle->lgh_ctxt->loc_obd->obd_name,
279 POSTID(&loghandle->lgh_id.lgl_oi),
280 loghandle->lgh_id.lgl_ogen, rc);
281 GOTO(out_unlock, rc);
287 mutex_unlock(&loghandle->lgh_hdr_mutex);
288 up_write(&loghandle->lgh_lock);
290 rc1 = dt_trans_stop(env, dt, th);
293 if (rc < 0 && subtract_count) {
294 mutex_lock(&loghandle->lgh_hdr_mutex);
295 loghandle->lgh_hdr->llh_count++;
296 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
297 mutex_unlock(&loghandle->lgh_hdr_mutex);
302 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
303 const struct obd_uuid *uuid)
305 struct llog_operations *lop;
309 rc = llog_handle2ops(handle, &lop);
313 if (lop->lop_read_header == NULL)
316 rc = lop->lop_read_header(env, handle);
317 if (rc == LLOG_EEMPTY) {
318 struct llog_log_hdr *llh = handle->lgh_hdr;
320 /* lrh_len should be initialized in llog_init_handle */
321 handle->lgh_last_idx = 0; /* header is record with index 0 */
322 handle->lgh_write_offset = 0;
323 llh->llh_count = 1; /* for the header record */
324 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
325 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
326 LLOG_MIN_CHUNK_SIZE);
327 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
328 llh->llh_hdr.lrh_index = 0;
329 llh->llh_timestamp = cfs_time_current_sec();
331 memcpy(&llh->llh_tgtuuid, uuid,
332 sizeof(llh->llh_tgtuuid));
333 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
334 /* Since update llog header might also call this function,
335 * let's reset the bitmap to 0 here */
336 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
337 llh->llh_bitmap_offset -
338 sizeof(llh->llh_tail));
339 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
340 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
341 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
346 EXPORT_SYMBOL(llog_read_header);
348 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
349 int flags, struct obd_uuid *uuid)
351 struct llog_log_hdr *llh;
352 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
354 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
357 LASSERT(handle->lgh_hdr == NULL);
359 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
360 OBD_ALLOC_LARGE(llh, chunk_size);
364 handle->lgh_hdr = llh;
365 handle->lgh_hdr_size = chunk_size;
366 /* first assign flags to use llog_client_ops */
367 llh->llh_flags = flags;
368 rc = llog_read_header(env, handle, uuid);
370 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
371 flags & LLOG_F_IS_CAT) ||
372 (llh->llh_flags & LLOG_F_IS_CAT &&
373 flags & LLOG_F_IS_PLAIN))) {
374 CERROR("%s: llog type is %s but initializing %s\n",
375 handle->lgh_ctxt->loc_obd->obd_name,
376 llh->llh_flags & LLOG_F_IS_CAT ?
378 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
379 GOTO(out, rc = -EINVAL);
380 } else if (llh->llh_flags &
381 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
383 * it is possible to open llog without specifying llog
384 * type so it is taken from llh_flags
386 flags = llh->llh_flags;
388 /* for some reason the llh_flags has no type set */
389 CERROR("llog type is not specified!\n");
390 GOTO(out, rc = -EINVAL);
393 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
394 CERROR("%s: llog uuid mismatch: %s/%s\n",
395 handle->lgh_ctxt->loc_obd->obd_name,
397 (char *)llh->llh_tgtuuid.uuid);
398 GOTO(out, rc = -EEXIST);
401 if (flags & LLOG_F_IS_CAT) {
402 LASSERT(list_empty(&handle->u.chd.chd_head));
403 INIT_LIST_HEAD(&handle->u.chd.chd_head);
404 llh->llh_size = sizeof(struct llog_logid_rec);
405 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
406 } else if (!(flags & LLOG_F_IS_PLAIN)) {
407 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
408 handle->lgh_ctxt->loc_obd->obd_name,
409 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
412 llh->llh_flags |= fmt;
415 OBD_FREE_LARGE(llh, chunk_size);
416 handle->lgh_hdr = NULL;
420 EXPORT_SYMBOL(llog_init_handle);
422 static int llog_process_thread(void *arg)
424 struct llog_process_info *lpi = arg;
425 struct llog_handle *loghandle = lpi->lpi_loghandle;
426 struct llog_log_hdr *llh = loghandle->lgh_hdr;
427 struct llog_process_cat_data *cd = lpi->lpi_catdata;
430 __u64 cur_offset, tmp_offset;
431 int rc = 0, index = 1, last_index;
433 int last_called_index = 0;
440 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
441 /* expect chunk_size to be power of two */
442 LASSERT(is_power_of_2(chunk_size));
444 OBD_ALLOC_LARGE(buf, chunk_size);
446 lpi->lpi_rc = -ENOMEM;
451 last_called_index = cd->lpcd_first_idx;
452 index = cd->lpcd_first_idx + 1;
454 if (cd != NULL && cd->lpcd_last_idx)
455 last_index = cd->lpcd_last_idx;
457 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
460 struct llog_rec_hdr *rec;
462 unsigned int buf_offset = 0;
465 /* skip records not set in bitmap */
466 while (index <= last_index &&
467 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
470 /* There are no indices prior the last_index */
471 if (index > last_index)
474 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
478 /* get the buf with our target record; avoid old garbage */
479 memset(buf, 0, chunk_size);
480 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
481 index, &cur_offset, buf, chunk_size);
485 /* NB: after llog_next_block() call the cur_offset is the
486 * offset of the next block after read one.
487 * The absolute offset of the current chunk is calculated
488 * from cur_offset value and stored in chunk_offset variable.
490 tmp_offset = cur_offset;
491 if (do_div(tmp_offset, chunk_size) != 0) {
492 partial_chunk = true;
493 chunk_offset = cur_offset & ~(chunk_size - 1);
495 partial_chunk = false;
496 chunk_offset = cur_offset - chunk_size;
499 /* NB: when rec->lrh_len is accessed it is already swabbed
500 * since it is used at the "end" of the loop and the rec
501 * swabbing is done at the beginning of the loop. */
502 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
503 (char *)rec < buf + chunk_size;
504 rec = llog_rec_hdr_next(rec)) {
506 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
509 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
510 lustre_swab_llog_rec(rec);
512 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
513 rec->lrh_type, rec->lrh_index);
515 /* for partial chunk the end of it is zeroed, check
516 * for index 0 to distinguish it. */
517 if (partial_chunk && rec->lrh_index == 0) {
518 /* concurrent llog_add() might add new records
519 * while llog_processing, check this is not
520 * the case and re-read the current chunk
522 if (index > loghandle->lgh_last_idx)
524 CDEBUG(D_OTHER, "Re-read last llog buffer for "
525 "new records, index %u, last %u\n",
526 index, loghandle->lgh_last_idx);
527 /* save offset inside buffer for the re-read */
528 buf_offset = (char *)rec - (char *)buf;
529 cur_offset = chunk_offset;
533 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
534 CWARN("invalid length %d in llog record for "
535 "index %d/%d\n", rec->lrh_len,
536 rec->lrh_index, index);
537 GOTO(out, rc = -EINVAL);
540 if (rec->lrh_index < index) {
541 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
546 if (rec->lrh_index != index) {
547 CERROR("%s: Invalid record: index %u but "
549 loghandle->lgh_ctxt->loc_obd->obd_name,
550 rec->lrh_index, index);
551 GOTO(out, rc = -ERANGE);
555 "lrh_index: %d lrh_len: %d (%d remains)\n",
556 rec->lrh_index, rec->lrh_len,
557 (int)(buf + chunk_size - (char *)rec));
559 loghandle->lgh_cur_idx = rec->lrh_index;
560 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
563 /* if set, process the callback on this record */
564 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
565 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
567 last_called_index = index;
568 if (rc == LLOG_PROC_BREAK) {
570 } else if (rc == LLOG_DEL_RECORD) {
571 rc = llog_cancel_rec(lpi->lpi_env,
578 /* exit if the last index is reached */
579 if (index >= last_index)
587 cd->lpcd_last_idx = last_called_index;
589 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
590 if (dt_object_remote(loghandle->lgh_obj)) {
591 /* If it is remote object, then -EIO might means
592 * disconnection or eviction, let's return -EAGAIN,
593 * so for update recovery log processing, it will
594 * retry until the umount or abort recovery, see
595 * lod_sub_recovery_thread() */
596 CERROR("%s retry remote llog process\n",
597 loghandle->lgh_ctxt->loc_obd->obd_name);
600 /* something bad happened to the processing of a local
601 * llog file, probably I/O error or the log got
602 * corrupted to be able to finally release the log we
603 * discard any remaining bits in the header */
604 CERROR("Local llog found corrupted\n");
605 while (index <= last_index) {
606 if (ext2_test_bit(index,
607 LLOG_HDR_BITMAP(llh)) != 0)
608 llog_cancel_rec(lpi->lpi_env, loghandle,
616 OBD_FREE_LARGE(buf, chunk_size);
621 static int llog_process_thread_daemonize(void *arg)
623 struct llog_process_info *lpi = arg;
629 /* client env has no keys, tags is just 0 */
630 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
635 rc = llog_process_thread(arg);
639 complete(&lpi->lpi_completion);
643 int llog_process_or_fork(const struct lu_env *env,
644 struct llog_handle *loghandle,
645 llog_cb_t cb, void *data, void *catdata, bool fork)
647 struct llog_process_info *lpi;
654 CERROR("cannot alloc pointer\n");
657 lpi->lpi_loghandle = loghandle;
659 lpi->lpi_cbdata = data;
660 lpi->lpi_catdata = catdata;
663 struct task_struct *task;
665 /* The new thread can't use parent env,
666 * init the new one in llog_process_thread_daemonize. */
668 init_completion(&lpi->lpi_completion);
669 task = kthread_run(llog_process_thread_daemonize, lpi,
670 "llog_process_thread");
673 CERROR("%s: cannot start thread: rc = %d\n",
674 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
677 wait_for_completion(&lpi->lpi_completion);
680 llog_process_thread(lpi);
688 EXPORT_SYMBOL(llog_process_or_fork);
690 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
691 llog_cb_t cb, void *data, void *catdata)
694 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
695 return rc == LLOG_DEL_PLAIN ? 0 : rc;
697 EXPORT_SYMBOL(llog_process);
699 int llog_reverse_process(const struct lu_env *env,
700 struct llog_handle *loghandle, llog_cb_t cb,
701 void *data, void *catdata)
703 struct llog_log_hdr *llh = loghandle->lgh_hdr;
704 struct llog_process_cat_data *cd = catdata;
706 int rc = 0, first_index = 1, index, idx;
707 __u32 chunk_size = llh->llh_hdr.lrh_len;
710 OBD_ALLOC_LARGE(buf, chunk_size);
715 first_index = cd->lpcd_first_idx + 1;
716 if (cd != NULL && cd->lpcd_last_idx)
717 index = cd->lpcd_last_idx;
719 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
722 struct llog_rec_hdr *rec;
723 struct llog_rec_tail *tail;
725 /* skip records not set in bitmap */
726 while (index >= first_index &&
727 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
730 LASSERT(index >= first_index - 1);
731 if (index == first_index - 1)
734 /* get the buf with our target record; avoid old garbage */
735 memset(buf, 0, chunk_size);
736 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
741 idx = rec->lrh_index;
742 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
743 while (idx < index) {
744 rec = (void *)rec + rec->lrh_len;
745 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
746 lustre_swab_llog_rec(rec);
749 LASSERT(idx == index);
750 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
752 /* process records in buffer, starting where we found one */
753 while ((void *)tail > buf) {
754 if (tail->lrt_index == 0)
755 GOTO(out, rc = 0); /* no more records */
757 /* if set, process the callback on this record */
758 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
759 rec = (void *)tail - tail->lrt_len +
762 rc = cb(env, loghandle, rec, data);
763 if (rc == LLOG_PROC_BREAK) {
765 } else if (rc == LLOG_DEL_RECORD) {
766 rc = llog_cancel_rec(env, loghandle,
773 /* previous record, still in buffer? */
775 if (index < first_index)
777 tail = (void *)tail - tail->lrt_len;
783 OBD_FREE_LARGE(buf, chunk_size);
786 EXPORT_SYMBOL(llog_reverse_process);
792 * llog_open - open llog, may not exist
793 * llog_exist - check if llog exists
794 * llog_close - close opened llog, pair for open, frees llog_handle
795 * llog_declare_create - declare llog creation
796 * llog_create - create new llog on disk, need transaction handle
797 * llog_declare_write_rec - declaration of llog write
798 * llog_write_rec - write llog record on disk, need transaction handle
799 * llog_declare_add - declare llog catalog record addition
800 * llog_add - add llog record in catalog, need transaction handle
802 int llog_exist(struct llog_handle *loghandle)
804 struct llog_operations *lop;
809 rc = llog_handle2ops(loghandle, &lop);
812 if (lop->lop_exist == NULL)
815 rc = lop->lop_exist(loghandle);
818 EXPORT_SYMBOL(llog_exist);
820 int llog_declare_create(const struct lu_env *env,
821 struct llog_handle *loghandle, struct thandle *th)
823 struct llog_operations *lop;
828 rc = llog_handle2ops(loghandle, &lop);
831 if (lop->lop_declare_create == NULL)
834 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
836 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
837 rc = lop->lop_declare_create(env, loghandle, th);
839 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
843 int llog_create(const struct lu_env *env, struct llog_handle *handle,
846 struct llog_operations *lop;
851 rc = llog_handle2ops(handle, &lop);
854 if (lop->lop_create == NULL)
857 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
859 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
860 rc = lop->lop_create(env, handle, th);
862 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
866 int llog_declare_write_rec(const struct lu_env *env,
867 struct llog_handle *handle,
868 struct llog_rec_hdr *rec, int idx,
871 struct llog_operations *lop;
876 rc = llog_handle2ops(handle, &lop);
880 if (lop->lop_declare_write_rec == NULL)
883 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
885 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
886 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
888 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
892 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
893 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
894 int idx, struct thandle *th)
896 struct llog_operations *lop;
897 int raised, rc, buflen;
901 /* API sanity checks */
902 if (handle == NULL) {
903 CERROR("loghandle is missed\n");
905 } else if (handle->lgh_obj == NULL) {
906 CERROR("loghandle %p with NULL object\n",
909 } else if (th == NULL) {
910 CERROR("%s: missed transaction handle\n",
911 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
913 } else if (handle->lgh_hdr == NULL) {
914 CERROR("%s: loghandle %p with no header\n",
915 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
920 rc = llog_handle2ops(handle, &lop);
924 if (lop->lop_write_rec == NULL)
927 buflen = rec->lrh_len;
928 LASSERT(cfs_size_round(buflen) == buflen);
930 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
932 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
933 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
935 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
939 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
940 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
947 if (lgh->lgh_logops->lop_add == NULL)
950 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
952 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
953 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
955 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
958 EXPORT_SYMBOL(llog_add);
960 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
961 struct llog_rec_hdr *rec, struct thandle *th)
967 if (lgh->lgh_logops->lop_declare_add == NULL)
970 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
972 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
973 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
975 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
978 EXPORT_SYMBOL(llog_declare_add);
981 * Helper function to open llog or create it if doesn't exist.
982 * It hides all transaction handling from caller.
984 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
985 struct llog_handle **res, struct llog_logid *logid,
994 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
998 if (llog_exist(*res))
1001 LASSERT((*res)->lgh_obj != NULL);
1003 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1005 th = dt_trans_create(env, d);
1007 GOTO(out, rc = PTR_ERR(th));
1009 /* Create update llog object synchronously, which
1010 * happens during inialization process see
1011 * lod_sub_prep_llog(), to make sure the update
1012 * llog object is created before corss-MDT writing
1013 * updates into the llog object */
1014 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1017 th->th_wait_submit = 1;
1018 rc = llog_declare_create(env, *res, th);
1020 rc = dt_trans_start_local(env, d, th);
1022 rc = llog_create(env, *res, th);
1024 dt_trans_stop(env, d, th);
1027 llog_close(env, *res);
1030 EXPORT_SYMBOL(llog_open_create);
1033 * Helper function to delete existent llog.
1035 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1036 struct llog_logid *logid, char *name)
1038 struct llog_handle *handle;
1043 /* nothing to erase */
1044 if (name == NULL && logid == NULL)
1047 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1051 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1053 rc = llog_destroy(env, handle);
1055 rc2 = llog_close(env, handle);
1060 EXPORT_SYMBOL(llog_erase);
1063 * Helper function for write record in llog.
1064 * It hides all transaction handling from caller.
1065 * Valid only with local llog.
1067 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1068 struct llog_rec_hdr *rec, int idx)
1070 struct dt_device *dt;
1077 LASSERT(loghandle->lgh_ctxt);
1078 LASSERT(loghandle->lgh_obj != NULL);
1080 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1082 th = dt_trans_create(env, dt);
1084 RETURN(PTR_ERR(th));
1086 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1088 GOTO(out_trans, rc);
1090 th->th_wait_submit = 1;
1091 rc = dt_trans_start_local(env, dt, th);
1093 GOTO(out_trans, rc);
1095 down_write(&loghandle->lgh_lock);
1096 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1097 up_write(&loghandle->lgh_lock);
1099 dt_trans_stop(env, dt, th);
1102 EXPORT_SYMBOL(llog_write);
1104 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1105 struct llog_handle **lgh, struct llog_logid *logid,
1106 char *name, enum llog_open_param open_param)
1114 LASSERT(ctxt->loc_logops);
1116 if (ctxt->loc_logops->lop_open == NULL) {
1118 RETURN(-EOPNOTSUPP);
1121 *lgh = llog_alloc_handle();
1124 (*lgh)->lgh_ctxt = ctxt;
1125 (*lgh)->lgh_logops = ctxt->loc_logops;
1127 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1129 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1130 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1132 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1134 llog_free_handle(*lgh);
1139 EXPORT_SYMBOL(llog_open);
1141 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1143 struct llog_operations *lop;
1148 rc = llog_handle2ops(loghandle, &lop);
1151 if (lop->lop_close == NULL)
1152 GOTO(out, rc = -EOPNOTSUPP);
1153 rc = lop->lop_close(env, loghandle);
1155 llog_handle_put(loghandle);
1158 EXPORT_SYMBOL(llog_close);
1161 * Helper function to get the llog size in records. It is used by MGS
1162 * mostly to check that config llog exists and contains data.
1164 * \param[in] env execution environment
1165 * \param[in] ctxt llog context
1166 * \param[in] name llog name
1168 * \retval true if there are records in llog besides a header
1169 * \retval false on error or llog without records
1171 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1174 struct llog_handle *llh;
1177 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1179 if (likely(rc == -ENOENT))
1184 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1186 GOTO(out_close, rc);
1187 rc = llog_get_size(llh);
1190 llog_close(env, llh);
1192 /* The header is record 1, the llog is still considered as empty
1193 * if there is only header */
1196 EXPORT_SYMBOL(llog_is_empty);
1198 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1199 struct llog_rec_hdr *rec, void *data)
1201 struct llog_handle *copy_llh = data;
1203 /* Append all records */
1204 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1207 /* backup plain llog */
1208 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1209 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1210 char *name, char *backup)
1212 struct llog_handle *llh, *bllh;
1217 /* open original log */
1218 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1220 /* the -ENOENT case is also reported to the caller
1221 * but silently so it should handle that if needed.
1224 CERROR("%s: failed to open log %s: rc = %d\n",
1225 obd->obd_name, name, rc);
1229 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1231 GOTO(out_close, rc);
1233 /* Make sure there's no old backup log */
1234 rc = llog_erase(env, bctxt, NULL, backup);
1235 if (rc < 0 && rc != -ENOENT)
1236 GOTO(out_close, rc);
1238 /* open backup log */
1239 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1241 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1242 obd->obd_name, backup, rc);
1243 GOTO(out_close, rc);
1246 /* check that backup llog is not the same object as original one */
1247 if (llh->lgh_obj == bllh->lgh_obj) {
1248 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1249 obd->obd_name, name, backup, llh->lgh_obj,
1251 GOTO(out_backup, rc = -EEXIST);
1254 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1256 GOTO(out_backup, rc);
1258 /* Copy log record by record */
1259 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1262 CERROR("%s: failed to backup log %s: rc = %d\n",
1263 obd->obd_name, name, rc);
1265 llog_close(env, bllh);
1267 llog_close(env, llh);
1270 EXPORT_SYMBOL(llog_backup);
1272 /* Get size of llog */
1273 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1278 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1280 CERROR("%s: attr_get failed, rc = %d\n",
1281 llh->lgh_ctxt->loc_obd->obd_name, rc);
1287 EXPORT_SYMBOL(llog_size);