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);
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 rc = llog_trans_destroy(env, loghandle, th);
264 /* Sigh, can not destroy the final plain llog, but
265 * the bitmap has been clearly, so the record can not
266 * be accessed anymore, let's return 0 for now, and
267 * the orphan will be handled by LFSCK. */
268 CERROR("%s: can't destroy empty llog #"DOSTID
270 loghandle->lgh_ctxt->loc_obd->obd_name,
271 POSTID(&loghandle->lgh_id.lgl_oi),
272 loghandle->lgh_id.lgl_ogen, rc);
273 GOTO(out_unlock, rc);
279 mutex_unlock(&loghandle->lgh_hdr_mutex);
280 up_write(&loghandle->lgh_lock);
282 rc1 = dt_trans_stop(env, dt, th);
285 if (rc < 0 && subtract_count) {
286 mutex_lock(&loghandle->lgh_hdr_mutex);
287 loghandle->lgh_hdr->llh_count++;
288 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
289 mutex_unlock(&loghandle->lgh_hdr_mutex);
294 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
295 const struct obd_uuid *uuid)
297 struct llog_operations *lop;
301 rc = llog_handle2ops(handle, &lop);
305 if (lop->lop_read_header == NULL)
308 rc = lop->lop_read_header(env, handle);
309 if (rc == LLOG_EEMPTY) {
310 struct llog_log_hdr *llh = handle->lgh_hdr;
312 /* lrh_len should be initialized in llog_init_handle */
313 handle->lgh_last_idx = 0; /* header is record with index 0 */
314 handle->lgh_write_offset = 0;
315 llh->llh_count = 1; /* for the header record */
316 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
317 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
318 LLOG_MIN_CHUNK_SIZE);
319 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
320 llh->llh_hdr.lrh_index = 0;
321 llh->llh_timestamp = cfs_time_current_sec();
323 memcpy(&llh->llh_tgtuuid, uuid,
324 sizeof(llh->llh_tgtuuid));
325 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
326 /* Since update llog header might also call this function,
327 * let's reset the bitmap to 0 here */
328 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
329 llh->llh_bitmap_offset -
330 sizeof(llh->llh_tail));
331 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
332 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
333 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
338 EXPORT_SYMBOL(llog_read_header);
340 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
341 int flags, struct obd_uuid *uuid)
343 struct llog_log_hdr *llh;
344 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
346 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
349 LASSERT(handle->lgh_hdr == NULL);
351 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
352 OBD_ALLOC_LARGE(llh, chunk_size);
356 handle->lgh_hdr = llh;
357 handle->lgh_hdr_size = chunk_size;
358 /* first assign flags to use llog_client_ops */
359 llh->llh_flags = flags;
360 rc = llog_read_header(env, handle, uuid);
362 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
363 flags & LLOG_F_IS_CAT) ||
364 (llh->llh_flags & LLOG_F_IS_CAT &&
365 flags & LLOG_F_IS_PLAIN))) {
366 CERROR("%s: llog type is %s but initializing %s\n",
367 handle->lgh_ctxt->loc_obd->obd_name,
368 llh->llh_flags & LLOG_F_IS_CAT ?
370 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
371 GOTO(out, rc = -EINVAL);
372 } else if (llh->llh_flags &
373 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
375 * it is possible to open llog without specifying llog
376 * type so it is taken from llh_flags
378 flags = llh->llh_flags;
380 /* for some reason the llh_flags has no type set */
381 CERROR("llog type is not specified!\n");
382 GOTO(out, rc = -EINVAL);
385 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
386 CERROR("%s: llog uuid mismatch: %s/%s\n",
387 handle->lgh_ctxt->loc_obd->obd_name,
389 (char *)llh->llh_tgtuuid.uuid);
390 GOTO(out, rc = -EEXIST);
393 if (flags & LLOG_F_IS_CAT) {
394 LASSERT(list_empty(&handle->u.chd.chd_head));
395 INIT_LIST_HEAD(&handle->u.chd.chd_head);
396 llh->llh_size = sizeof(struct llog_logid_rec);
397 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
398 } else if (!(flags & LLOG_F_IS_PLAIN)) {
399 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
400 handle->lgh_ctxt->loc_obd->obd_name,
401 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
404 llh->llh_flags |= fmt;
407 OBD_FREE_LARGE(llh, chunk_size);
408 handle->lgh_hdr = NULL;
412 EXPORT_SYMBOL(llog_init_handle);
414 static int llog_process_thread(void *arg)
416 struct llog_process_info *lpi = arg;
417 struct llog_handle *loghandle = lpi->lpi_loghandle;
418 struct llog_log_hdr *llh = loghandle->lgh_hdr;
419 struct llog_process_cat_data *cd = lpi->lpi_catdata;
423 int rc = 0, index = 1, last_index;
425 int last_called_index = 0;
432 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
433 /* expect chunk_size to be power of two */
434 LASSERT(is_power_of_2(chunk_size));
436 OBD_ALLOC_LARGE(buf, chunk_size);
438 lpi->lpi_rc = -ENOMEM;
443 last_called_index = cd->lpcd_first_idx;
444 index = cd->lpcd_first_idx + 1;
446 if (cd != NULL && cd->lpcd_last_idx)
447 last_index = cd->lpcd_last_idx;
449 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
452 struct llog_rec_hdr *rec;
454 unsigned int buf_offset = 0;
457 /* skip records not set in bitmap */
458 while (index <= last_index &&
459 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
462 /* There are no indices prior the last_index */
463 if (index > last_index)
466 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
470 /* get the buf with our target record; avoid old garbage */
471 memset(buf, 0, chunk_size);
472 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
473 index, &cur_offset, buf, chunk_size);
477 /* NB: after llog_next_block() call the cur_offset is the
478 * offset of the next block after read one.
479 * The absolute offset of the current chunk is calculated
480 * from cur_offset value and stored in chunk_offset variable.
482 if (cur_offset % chunk_size != 0) {
483 partial_chunk = true;
484 chunk_offset = cur_offset & ~(chunk_size - 1);
486 partial_chunk = false;
487 chunk_offset = cur_offset - chunk_size;
490 /* NB: when rec->lrh_len is accessed it is already swabbed
491 * since it is used at the "end" of the loop and the rec
492 * swabbing is done at the beginning of the loop. */
493 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
494 (char *)rec < buf + chunk_size;
495 rec = llog_rec_hdr_next(rec)) {
497 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
500 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
501 lustre_swab_llog_rec(rec);
503 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
504 rec->lrh_type, rec->lrh_index);
506 /* for partial chunk the end of it is zeroed, check
507 * for index 0 to distinguish it. */
508 if (partial_chunk && rec->lrh_index == 0) {
509 /* concurrent llog_add() might add new records
510 * while llog_processing, check this is not
511 * the case and re-read the current chunk
513 if (index > loghandle->lgh_last_idx)
515 CDEBUG(D_OTHER, "Re-read last llog buffer for "
516 "new records, index %u, last %u\n",
517 index, loghandle->lgh_last_idx);
518 /* save offset inside buffer for the re-read */
519 buf_offset = (char *)rec - (char *)buf;
520 cur_offset = chunk_offset;
524 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
525 CWARN("invalid length %d in llog record for "
526 "index %d/%d\n", rec->lrh_len,
527 rec->lrh_index, index);
528 GOTO(out, rc = -EINVAL);
531 if (rec->lrh_index < index) {
532 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
537 if (rec->lrh_index != index) {
538 CERROR("%s: Invalid record: index %u but "
540 loghandle->lgh_ctxt->loc_obd->obd_name,
541 rec->lrh_index, index);
542 GOTO(out, rc = -ERANGE);
546 "lrh_index: %d lrh_len: %d (%d remains)\n",
547 rec->lrh_index, rec->lrh_len,
548 (int)(buf + chunk_size - (char *)rec));
550 loghandle->lgh_cur_idx = rec->lrh_index;
551 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
554 /* if set, process the callback on this record */
555 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
556 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
558 last_called_index = index;
559 if (rc == LLOG_PROC_BREAK) {
561 } else if (rc == LLOG_DEL_RECORD) {
562 rc = llog_cancel_rec(lpi->lpi_env,
569 /* exit if the last index is reached */
570 if (index >= last_index)
578 cd->lpcd_last_idx = last_called_index;
580 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
581 if (dt_object_remote(loghandle->lgh_obj)) {
582 /* If it is remote object, then -EIO might means
583 * disconnection or eviction, let's return -EAGAIN,
584 * so for update recovery log processing, it will
585 * retry until the umount or abort recovery, see
586 * lod_sub_recovery_thread() */
587 CERROR("%s retry remote llog process\n",
588 loghandle->lgh_ctxt->loc_obd->obd_name);
591 /* something bad happened to the processing of a local
592 * llog file, probably I/O error or the log got
593 * corrupted to be able to finally release the log we
594 * discard any remaining bits in the header */
595 CERROR("Local llog found corrupted\n");
596 while (index <= last_index) {
597 if (ext2_test_bit(index,
598 LLOG_HDR_BITMAP(llh)) != 0)
599 llog_cancel_rec(lpi->lpi_env, loghandle,
607 OBD_FREE_LARGE(buf, chunk_size);
612 static int llog_process_thread_daemonize(void *arg)
614 struct llog_process_info *lpi = arg;
620 /* client env has no keys, tags is just 0 */
621 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
626 rc = llog_process_thread(arg);
630 complete(&lpi->lpi_completion);
634 int llog_process_or_fork(const struct lu_env *env,
635 struct llog_handle *loghandle,
636 llog_cb_t cb, void *data, void *catdata, bool fork)
638 struct llog_process_info *lpi;
645 CERROR("cannot alloc pointer\n");
648 lpi->lpi_loghandle = loghandle;
650 lpi->lpi_cbdata = data;
651 lpi->lpi_catdata = catdata;
654 struct task_struct *task;
656 /* The new thread can't use parent env,
657 * init the new one in llog_process_thread_daemonize. */
659 init_completion(&lpi->lpi_completion);
660 task = kthread_run(llog_process_thread_daemonize, lpi,
661 "llog_process_thread");
664 CERROR("%s: cannot start thread: rc = %d\n",
665 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
668 wait_for_completion(&lpi->lpi_completion);
671 llog_process_thread(lpi);
679 EXPORT_SYMBOL(llog_process_or_fork);
681 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
682 llog_cb_t cb, void *data, void *catdata)
685 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
686 return rc == LLOG_DEL_PLAIN ? 0 : rc;
688 EXPORT_SYMBOL(llog_process);
690 int llog_reverse_process(const struct lu_env *env,
691 struct llog_handle *loghandle, llog_cb_t cb,
692 void *data, void *catdata)
694 struct llog_log_hdr *llh = loghandle->lgh_hdr;
695 struct llog_process_cat_data *cd = catdata;
697 int rc = 0, first_index = 1, index, idx;
698 __u32 chunk_size = llh->llh_hdr.lrh_len;
701 OBD_ALLOC_LARGE(buf, chunk_size);
706 first_index = cd->lpcd_first_idx + 1;
707 if (cd != NULL && cd->lpcd_last_idx)
708 index = cd->lpcd_last_idx;
710 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
713 struct llog_rec_hdr *rec;
714 struct llog_rec_tail *tail;
716 /* skip records not set in bitmap */
717 while (index >= first_index &&
718 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
721 LASSERT(index >= first_index - 1);
722 if (index == first_index - 1)
725 /* get the buf with our target record; avoid old garbage */
726 memset(buf, 0, chunk_size);
727 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
732 idx = rec->lrh_index;
733 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
734 while (idx < index) {
735 rec = (void *)rec + rec->lrh_len;
736 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
737 lustre_swab_llog_rec(rec);
740 LASSERT(idx == index);
741 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
743 /* process records in buffer, starting where we found one */
744 while ((void *)tail > buf) {
745 if (tail->lrt_index == 0)
746 GOTO(out, rc = 0); /* no more records */
748 /* if set, process the callback on this record */
749 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
750 rec = (void *)tail - tail->lrt_len +
753 rc = cb(env, loghandle, rec, data);
754 if (rc == LLOG_PROC_BREAK) {
756 } else if (rc == LLOG_DEL_RECORD) {
757 rc = llog_cancel_rec(env, loghandle,
764 /* previous record, still in buffer? */
766 if (index < first_index)
768 tail = (void *)tail - tail->lrt_len;
774 OBD_FREE_LARGE(buf, chunk_size);
777 EXPORT_SYMBOL(llog_reverse_process);
783 * llog_open - open llog, may not exist
784 * llog_exist - check if llog exists
785 * llog_close - close opened llog, pair for open, frees llog_handle
786 * llog_declare_create - declare llog creation
787 * llog_create - create new llog on disk, need transaction handle
788 * llog_declare_write_rec - declaration of llog write
789 * llog_write_rec - write llog record on disk, need transaction handle
790 * llog_declare_add - declare llog catalog record addition
791 * llog_add - add llog record in catalog, need transaction handle
793 int llog_exist(struct llog_handle *loghandle)
795 struct llog_operations *lop;
800 rc = llog_handle2ops(loghandle, &lop);
803 if (lop->lop_exist == NULL)
806 rc = lop->lop_exist(loghandle);
809 EXPORT_SYMBOL(llog_exist);
811 int llog_declare_create(const struct lu_env *env,
812 struct llog_handle *loghandle, struct thandle *th)
814 struct llog_operations *lop;
819 rc = llog_handle2ops(loghandle, &lop);
822 if (lop->lop_declare_create == NULL)
825 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
827 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
828 rc = lop->lop_declare_create(env, loghandle, th);
830 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
834 int llog_create(const struct lu_env *env, struct llog_handle *handle,
837 struct llog_operations *lop;
842 rc = llog_handle2ops(handle, &lop);
845 if (lop->lop_create == NULL)
848 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
850 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
851 rc = lop->lop_create(env, handle, th);
853 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
857 int llog_declare_write_rec(const struct lu_env *env,
858 struct llog_handle *handle,
859 struct llog_rec_hdr *rec, int idx,
862 struct llog_operations *lop;
867 rc = llog_handle2ops(handle, &lop);
871 if (lop->lop_declare_write_rec == NULL)
874 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
876 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
877 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
879 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
883 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
884 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
885 int idx, struct thandle *th)
887 struct llog_operations *lop;
888 int raised, rc, buflen;
892 rc = llog_handle2ops(handle, &lop);
897 if (lop->lop_write_rec == NULL)
900 buflen = rec->lrh_len;
901 LASSERT(cfs_size_round(buflen) == buflen);
903 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
905 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
906 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
908 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
912 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
913 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
920 if (lgh->lgh_logops->lop_add == NULL)
923 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
925 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
926 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
928 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
931 EXPORT_SYMBOL(llog_add);
933 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
934 struct llog_rec_hdr *rec, struct thandle *th)
940 if (lgh->lgh_logops->lop_declare_add == NULL)
943 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
945 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
946 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
948 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
951 EXPORT_SYMBOL(llog_declare_add);
954 * Helper function to open llog or create it if doesn't exist.
955 * It hides all transaction handling from caller.
957 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
958 struct llog_handle **res, struct llog_logid *logid,
967 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
971 if (llog_exist(*res))
974 LASSERT((*res)->lgh_obj != NULL);
976 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
978 th = dt_trans_create(env, d);
980 GOTO(out, rc = PTR_ERR(th));
982 /* Create update llog object synchronously, which
983 * happens during inialization process see
984 * lod_sub_prep_llog(), to make sure the update
985 * llog object is created before corss-MDT writing
986 * updates into the llog object */
987 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
990 th->th_wait_submit = 1;
991 rc = llog_declare_create(env, *res, th);
993 rc = dt_trans_start_local(env, d, th);
995 rc = llog_create(env, *res, th);
997 dt_trans_stop(env, d, th);
1000 llog_close(env, *res);
1003 EXPORT_SYMBOL(llog_open_create);
1006 * Helper function to delete existent llog.
1008 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1009 struct llog_logid *logid, char *name)
1011 struct llog_handle *handle;
1016 /* nothing to erase */
1017 if (name == NULL && logid == NULL)
1020 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1024 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1026 rc = llog_destroy(env, handle);
1028 rc2 = llog_close(env, handle);
1033 EXPORT_SYMBOL(llog_erase);
1036 * Helper function for write record in llog.
1037 * It hides all transaction handling from caller.
1038 * Valid only with local llog.
1040 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1041 struct llog_rec_hdr *rec, int idx)
1043 struct dt_device *dt;
1050 LASSERT(loghandle->lgh_ctxt);
1051 LASSERT(loghandle->lgh_obj != NULL);
1053 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1055 th = dt_trans_create(env, dt);
1057 RETURN(PTR_ERR(th));
1059 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1061 GOTO(out_trans, rc);
1063 th->th_wait_submit = 1;
1064 rc = dt_trans_start_local(env, dt, th);
1066 GOTO(out_trans, rc);
1068 down_write(&loghandle->lgh_lock);
1069 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1070 up_write(&loghandle->lgh_lock);
1072 dt_trans_stop(env, dt, th);
1075 EXPORT_SYMBOL(llog_write);
1077 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1078 struct llog_handle **lgh, struct llog_logid *logid,
1079 char *name, enum llog_open_param open_param)
1087 LASSERT(ctxt->loc_logops);
1089 if (ctxt->loc_logops->lop_open == NULL) {
1091 RETURN(-EOPNOTSUPP);
1094 *lgh = llog_alloc_handle();
1097 (*lgh)->lgh_ctxt = ctxt;
1098 (*lgh)->lgh_logops = ctxt->loc_logops;
1100 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1102 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1103 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1105 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1107 llog_free_handle(*lgh);
1112 EXPORT_SYMBOL(llog_open);
1114 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1116 struct llog_operations *lop;
1121 rc = llog_handle2ops(loghandle, &lop);
1124 if (lop->lop_close == NULL)
1125 GOTO(out, rc = -EOPNOTSUPP);
1126 rc = lop->lop_close(env, loghandle);
1128 llog_handle_put(loghandle);
1131 EXPORT_SYMBOL(llog_close);
1134 * Helper function to get the llog size in records. It is used by MGS
1135 * mostly to check that config llog exists and contains data.
1137 * \param[in] env execution environment
1138 * \param[in] ctxt llog context
1139 * \param[in] name llog name
1141 * \retval true if there are records in llog besides a header
1142 * \retval false on error or llog without records
1144 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1147 struct llog_handle *llh;
1150 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1152 if (likely(rc == -ENOENT))
1157 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1159 GOTO(out_close, rc);
1160 rc = llog_get_size(llh);
1163 llog_close(env, llh);
1165 /* The header is record 1, the llog is still considered as empty
1166 * if there is only header */
1169 EXPORT_SYMBOL(llog_is_empty);
1171 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1172 struct llog_rec_hdr *rec, void *data)
1174 struct llog_handle *copy_llh = data;
1176 /* Append all records */
1177 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1180 /* backup plain llog */
1181 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1182 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1183 char *name, char *backup)
1185 struct llog_handle *llh, *bllh;
1190 /* open original log */
1191 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1193 /* the -ENOENT case is also reported to the caller
1194 * but silently so it should handle that if needed.
1197 CERROR("%s: failed to open log %s: rc = %d\n",
1198 obd->obd_name, name, rc);
1202 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1204 GOTO(out_close, rc);
1206 /* Make sure there's no old backup log */
1207 rc = llog_erase(env, bctxt, NULL, backup);
1208 if (rc < 0 && rc != -ENOENT)
1209 GOTO(out_close, rc);
1211 /* open backup log */
1212 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1214 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1215 obd->obd_name, backup, rc);
1216 GOTO(out_close, rc);
1219 /* check that backup llog is not the same object as original one */
1220 if (llh->lgh_obj == bllh->lgh_obj) {
1221 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1222 obd->obd_name, name, backup, llh->lgh_obj,
1224 GOTO(out_backup, rc = -EEXIST);
1227 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1229 GOTO(out_backup, rc);
1231 /* Copy log record by record */
1232 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1235 CERROR("%s: failed to backup log %s: rc = %d\n",
1236 obd->obd_name, name, rc);
1238 llog_close(env, bllh);
1240 llog_close(env, llh);
1243 EXPORT_SYMBOL(llog_backup);