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, 2015, 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/kthread.h>
47 #include <llog_swab.h>
48 #include <lustre_log.h>
49 #include <obd_class.h>
50 #include "llog_internal.h"
52 * Allocate a new log or catalog handle
53 * Used inside llog_open().
55 static struct llog_handle *llog_alloc_handle(void)
57 struct llog_handle *loghandle;
59 OBD_ALLOC_PTR(loghandle);
60 if (loghandle == NULL)
63 init_rwsem(&loghandle->lgh_lock);
64 mutex_init(&loghandle->lgh_hdr_mutex);
65 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
66 atomic_set(&loghandle->lgh_refcount, 1);
72 * Free llog handle and header data if exists. Used in llog_close() only
74 static void llog_free_handle(struct llog_handle *loghandle)
76 LASSERT(loghandle != NULL);
78 /* failed llog_init_handle */
79 if (loghandle->lgh_hdr == NULL)
82 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
83 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
84 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
85 LASSERT(list_empty(&loghandle->u.chd.chd_head));
86 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
88 OBD_FREE_PTR(loghandle);
91 void llog_handle_get(struct llog_handle *loghandle)
93 atomic_inc(&loghandle->lgh_refcount);
96 void llog_handle_put(struct llog_handle *loghandle)
98 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
99 if (atomic_dec_and_test(&loghandle->lgh_refcount))
100 llog_free_handle(loghandle);
103 static int llog_declare_destroy(const struct lu_env *env,
104 struct llog_handle *handle,
107 struct llog_operations *lop;
112 rc = llog_handle2ops(handle, &lop);
115 if (lop->lop_declare_destroy == NULL)
118 rc = lop->lop_declare_destroy(env, handle, th);
123 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
126 struct llog_operations *lop;
130 rc = llog_handle2ops(handle, &lop);
133 if (lop->lop_destroy == NULL)
136 LASSERT(handle->lgh_obj != NULL);
137 if (!dt_object_exists(handle->lgh_obj))
140 rc = lop->lop_destroy(env, handle, th);
145 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
147 struct llog_operations *lop;
148 struct dt_device *dt;
154 rc = llog_handle2ops(handle, &lop);
157 if (lop->lop_destroy == NULL)
160 if (handle->lgh_obj == NULL) {
161 /* if lgh_obj == NULL, then it is from client side destroy */
162 rc = lop->lop_destroy(env, handle, NULL);
166 if (!dt_object_exists(handle->lgh_obj))
169 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
171 th = dt_trans_create(env, dt);
175 rc = llog_declare_destroy(env, handle, th);
179 rc = dt_trans_start_local(env, dt, th);
183 rc = lop->lop_destroy(env, handle, th);
186 dt_trans_stop(env, dt, th);
190 EXPORT_SYMBOL(llog_destroy);
192 /* returns negative on error; 0 if success; 1 if success & log destroyed */
193 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
196 struct llog_thread_info *lgi = llog_info(env);
197 struct dt_device *dt;
198 struct llog_log_hdr *llh = loghandle->lgh_hdr;
202 bool subtract_count = false;
206 CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n", index,
207 POSTID(&loghandle->lgh_id.lgl_oi));
210 CERROR("Can't cancel index 0 which is header\n");
214 LASSERT(loghandle != NULL);
215 LASSERT(loghandle->lgh_ctxt != NULL);
216 LASSERT(loghandle->lgh_obj != NULL);
218 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
220 th = dt_trans_create(env, dt);
224 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
228 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
229 rc = llog_declare_destroy(env, loghandle, th);
234 th->th_wait_submit = 1;
235 rc = dt_trans_start_local(env, dt, th);
239 down_write(&loghandle->lgh_lock);
241 mutex_lock(&loghandle->lgh_hdr_mutex);
242 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
243 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
244 GOTO(out_unlock, rc);
247 loghandle->lgh_hdr->llh_count--;
248 subtract_count = true;
249 /* Pass this index to llog_osd_write_rec(), which will use the index
250 * to only update the necesary bitmap. */
251 lgi->lgi_cookie.lgc_index = index;
253 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
254 LLOG_HEADER_IDX, th);
256 GOTO(out_unlock, rc);
258 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
259 (llh->llh_count == 1) &&
260 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
261 (loghandle->u.phd.phd_cat_handle != NULL &&
262 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
264 /* never try to destroy it again */
265 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
266 rc = llog_trans_destroy(env, loghandle, th);
268 /* Sigh, can not destroy the final plain llog, but
269 * the bitmap has been clearly, so the record can not
270 * be accessed anymore, let's return 0 for now, and
271 * the orphan will be handled by LFSCK. */
272 CERROR("%s: can't destroy empty llog #"DOSTID
274 loghandle->lgh_ctxt->loc_obd->obd_name,
275 POSTID(&loghandle->lgh_id.lgl_oi),
276 loghandle->lgh_id.lgl_ogen, rc);
277 GOTO(out_unlock, rc);
283 mutex_unlock(&loghandle->lgh_hdr_mutex);
284 up_write(&loghandle->lgh_lock);
286 rc1 = dt_trans_stop(env, dt, th);
289 if (rc < 0 && subtract_count) {
290 mutex_lock(&loghandle->lgh_hdr_mutex);
291 loghandle->lgh_hdr->llh_count++;
292 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
293 mutex_unlock(&loghandle->lgh_hdr_mutex);
298 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
299 const struct obd_uuid *uuid)
301 struct llog_operations *lop;
305 rc = llog_handle2ops(handle, &lop);
309 if (lop->lop_read_header == NULL)
312 rc = lop->lop_read_header(env, handle);
313 if (rc == LLOG_EEMPTY) {
314 struct llog_log_hdr *llh = handle->lgh_hdr;
316 /* lrh_len should be initialized in llog_init_handle */
317 handle->lgh_last_idx = 0; /* header is record with index 0 */
318 handle->lgh_write_offset = 0;
319 llh->llh_count = 1; /* for the header record */
320 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
321 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
322 LLOG_MIN_CHUNK_SIZE);
323 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
324 llh->llh_hdr.lrh_index = 0;
325 llh->llh_timestamp = cfs_time_current_sec();
327 memcpy(&llh->llh_tgtuuid, uuid,
328 sizeof(llh->llh_tgtuuid));
329 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
330 /* Since update llog header might also call this function,
331 * let's reset the bitmap to 0 here */
332 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
333 llh->llh_bitmap_offset -
334 sizeof(llh->llh_tail));
335 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
336 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
337 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
342 EXPORT_SYMBOL(llog_read_header);
344 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
345 int flags, struct obd_uuid *uuid)
347 struct llog_log_hdr *llh;
348 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
350 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
353 LASSERT(handle->lgh_hdr == NULL);
355 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
356 OBD_ALLOC_LARGE(llh, chunk_size);
360 handle->lgh_hdr = llh;
361 handle->lgh_hdr_size = chunk_size;
362 /* first assign flags to use llog_client_ops */
363 llh->llh_flags = flags;
364 rc = llog_read_header(env, handle, uuid);
366 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
367 flags & LLOG_F_IS_CAT) ||
368 (llh->llh_flags & LLOG_F_IS_CAT &&
369 flags & LLOG_F_IS_PLAIN))) {
370 CERROR("%s: llog type is %s but initializing %s\n",
371 handle->lgh_ctxt->loc_obd->obd_name,
372 llh->llh_flags & LLOG_F_IS_CAT ?
374 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
375 GOTO(out, rc = -EINVAL);
376 } else if (llh->llh_flags &
377 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
379 * it is possible to open llog without specifying llog
380 * type so it is taken from llh_flags
382 flags = llh->llh_flags;
384 /* for some reason the llh_flags has no type set */
385 CERROR("llog type is not specified!\n");
386 GOTO(out, rc = -EINVAL);
389 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
390 CERROR("%s: llog uuid mismatch: %s/%s\n",
391 handle->lgh_ctxt->loc_obd->obd_name,
393 (char *)llh->llh_tgtuuid.uuid);
394 GOTO(out, rc = -EEXIST);
397 if (flags & LLOG_F_IS_CAT) {
398 LASSERT(list_empty(&handle->u.chd.chd_head));
399 INIT_LIST_HEAD(&handle->u.chd.chd_head);
400 llh->llh_size = sizeof(struct llog_logid_rec);
401 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
402 } else if (!(flags & LLOG_F_IS_PLAIN)) {
403 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
404 handle->lgh_ctxt->loc_obd->obd_name,
405 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
408 llh->llh_flags |= fmt;
411 OBD_FREE_LARGE(llh, chunk_size);
412 handle->lgh_hdr = NULL;
416 EXPORT_SYMBOL(llog_init_handle);
418 static int llog_process_thread(void *arg)
420 struct llog_process_info *lpi = arg;
421 struct llog_handle *loghandle = lpi->lpi_loghandle;
422 struct llog_log_hdr *llh = loghandle->lgh_hdr;
423 struct llog_process_cat_data *cd = lpi->lpi_catdata;
426 __u64 cur_offset, tmp_offset;
427 int rc = 0, index = 1, last_index;
429 int last_called_index = 0;
436 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
437 /* expect chunk_size to be power of two */
438 LASSERT(is_power_of_2(chunk_size));
440 OBD_ALLOC_LARGE(buf, chunk_size);
442 lpi->lpi_rc = -ENOMEM;
447 last_called_index = cd->lpcd_first_idx;
448 index = cd->lpcd_first_idx + 1;
450 if (cd != NULL && cd->lpcd_last_idx)
451 last_index = cd->lpcd_last_idx;
453 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
456 struct llog_rec_hdr *rec;
458 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 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
477 index, &cur_offset, buf, chunk_size);
481 /* NB: after llog_next_block() call the cur_offset is the
482 * offset of the next block after read one.
483 * The absolute offset of the current chunk is calculated
484 * from cur_offset value and stored in chunk_offset variable.
486 tmp_offset = cur_offset;
487 if (do_div(tmp_offset, chunk_size) != 0) {
488 partial_chunk = true;
489 chunk_offset = cur_offset & ~(chunk_size - 1);
491 partial_chunk = false;
492 chunk_offset = cur_offset - chunk_size;
495 /* NB: when rec->lrh_len is accessed it is already swabbed
496 * since it is used at the "end" of the loop and the rec
497 * swabbing is done at the beginning of the loop. */
498 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
499 (char *)rec < buf + chunk_size;
500 rec = llog_rec_hdr_next(rec)) {
502 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
505 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
506 lustre_swab_llog_rec(rec);
508 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
509 rec->lrh_type, rec->lrh_index);
511 /* for partial chunk the end of it is zeroed, check
512 * for index 0 to distinguish it. */
513 if (partial_chunk && rec->lrh_index == 0) {
514 /* concurrent llog_add() might add new records
515 * while llog_processing, check this is not
516 * the case and re-read the current chunk
518 if (index > loghandle->lgh_last_idx)
520 CDEBUG(D_OTHER, "Re-read last llog buffer for "
521 "new records, index %u, last %u\n",
522 index, loghandle->lgh_last_idx);
523 /* save offset inside buffer for the re-read */
524 buf_offset = (char *)rec - (char *)buf;
525 cur_offset = chunk_offset;
529 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
530 CWARN("invalid length %d in llog record for "
531 "index %d/%d\n", rec->lrh_len,
532 rec->lrh_index, index);
533 GOTO(out, rc = -EINVAL);
536 if (rec->lrh_index < index) {
537 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
542 if (rec->lrh_index != index) {
543 CERROR("%s: Invalid record: index %u but "
545 loghandle->lgh_ctxt->loc_obd->obd_name,
546 rec->lrh_index, index);
547 GOTO(out, rc = -ERANGE);
551 "lrh_index: %d lrh_len: %d (%d remains)\n",
552 rec->lrh_index, rec->lrh_len,
553 (int)(buf + chunk_size - (char *)rec));
555 loghandle->lgh_cur_idx = rec->lrh_index;
556 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
559 /* if set, process the callback on this record */
560 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
561 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
563 last_called_index = index;
564 if (rc == LLOG_PROC_BREAK) {
566 } else if (rc == LLOG_DEL_RECORD) {
567 rc = llog_cancel_rec(lpi->lpi_env,
574 /* exit if the last index is reached */
575 if (index >= last_index)
583 cd->lpcd_last_idx = last_called_index;
585 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
586 if (dt_object_remote(loghandle->lgh_obj)) {
587 /* If it is remote object, then -EIO might means
588 * disconnection or eviction, let's return -EAGAIN,
589 * so for update recovery log processing, it will
590 * retry until the umount or abort recovery, see
591 * lod_sub_recovery_thread() */
592 CERROR("%s retry remote llog process\n",
593 loghandle->lgh_ctxt->loc_obd->obd_name);
596 /* something bad happened to the processing of a local
597 * llog file, probably I/O error or the log got
598 * corrupted to be able to finally release the log we
599 * discard any remaining bits in the header */
600 CERROR("Local llog found corrupted\n");
601 while (index <= last_index) {
602 if (ext2_test_bit(index,
603 LLOG_HDR_BITMAP(llh)) != 0)
604 llog_cancel_rec(lpi->lpi_env, loghandle,
612 OBD_FREE_LARGE(buf, chunk_size);
617 static int llog_process_thread_daemonize(void *arg)
619 struct llog_process_info *lpi = arg;
625 /* client env has no keys, tags is just 0 */
626 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
631 rc = llog_process_thread(arg);
635 complete(&lpi->lpi_completion);
639 int llog_process_or_fork(const struct lu_env *env,
640 struct llog_handle *loghandle,
641 llog_cb_t cb, void *data, void *catdata, bool fork)
643 struct llog_process_info *lpi;
650 CERROR("cannot alloc pointer\n");
653 lpi->lpi_loghandle = loghandle;
655 lpi->lpi_cbdata = data;
656 lpi->lpi_catdata = catdata;
659 struct task_struct *task;
661 /* The new thread can't use parent env,
662 * init the new one in llog_process_thread_daemonize. */
664 init_completion(&lpi->lpi_completion);
665 task = kthread_run(llog_process_thread_daemonize, lpi,
666 "llog_process_thread");
669 CERROR("%s: cannot start thread: rc = %d\n",
670 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
673 wait_for_completion(&lpi->lpi_completion);
676 llog_process_thread(lpi);
684 EXPORT_SYMBOL(llog_process_or_fork);
686 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
687 llog_cb_t cb, void *data, void *catdata)
690 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
691 return rc == LLOG_DEL_PLAIN ? 0 : rc;
693 EXPORT_SYMBOL(llog_process);
695 int llog_reverse_process(const struct lu_env *env,
696 struct llog_handle *loghandle, llog_cb_t cb,
697 void *data, void *catdata)
699 struct llog_log_hdr *llh = loghandle->lgh_hdr;
700 struct llog_process_cat_data *cd = catdata;
702 int rc = 0, first_index = 1, index, idx;
703 __u32 chunk_size = llh->llh_hdr.lrh_len;
706 OBD_ALLOC_LARGE(buf, chunk_size);
711 first_index = cd->lpcd_first_idx + 1;
712 if (cd != NULL && cd->lpcd_last_idx)
713 index = cd->lpcd_last_idx;
715 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
718 struct llog_rec_hdr *rec;
719 struct llog_rec_tail *tail;
721 /* skip records not set in bitmap */
722 while (index >= first_index &&
723 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
726 LASSERT(index >= first_index - 1);
727 if (index == first_index - 1)
730 /* get the buf with our target record; avoid old garbage */
731 memset(buf, 0, chunk_size);
732 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
737 idx = rec->lrh_index;
738 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
739 while (idx < index) {
740 rec = (void *)rec + rec->lrh_len;
741 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
742 lustre_swab_llog_rec(rec);
745 LASSERT(idx == index);
746 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
748 /* process records in buffer, starting where we found one */
749 while ((void *)tail > buf) {
750 if (tail->lrt_index == 0)
751 GOTO(out, rc = 0); /* no more records */
753 /* if set, process the callback on this record */
754 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
755 rec = (void *)tail - tail->lrt_len +
758 rc = cb(env, loghandle, rec, data);
759 if (rc == LLOG_PROC_BREAK) {
761 } else if (rc == LLOG_DEL_RECORD) {
762 rc = llog_cancel_rec(env, loghandle,
769 /* previous record, still in buffer? */
771 if (index < first_index)
773 tail = (void *)tail - tail->lrt_len;
779 OBD_FREE_LARGE(buf, chunk_size);
782 EXPORT_SYMBOL(llog_reverse_process);
788 * llog_open - open llog, may not exist
789 * llog_exist - check if llog exists
790 * llog_close - close opened llog, pair for open, frees llog_handle
791 * llog_declare_create - declare llog creation
792 * llog_create - create new llog on disk, need transaction handle
793 * llog_declare_write_rec - declaration of llog write
794 * llog_write_rec - write llog record on disk, need transaction handle
795 * llog_declare_add - declare llog catalog record addition
796 * llog_add - add llog record in catalog, need transaction handle
798 int llog_exist(struct llog_handle *loghandle)
800 struct llog_operations *lop;
805 rc = llog_handle2ops(loghandle, &lop);
808 if (lop->lop_exist == NULL)
811 rc = lop->lop_exist(loghandle);
814 EXPORT_SYMBOL(llog_exist);
816 int llog_declare_create(const struct lu_env *env,
817 struct llog_handle *loghandle, struct thandle *th)
819 struct llog_operations *lop;
824 rc = llog_handle2ops(loghandle, &lop);
827 if (lop->lop_declare_create == NULL)
830 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
832 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
833 rc = lop->lop_declare_create(env, loghandle, th);
835 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
839 int llog_create(const struct lu_env *env, struct llog_handle *handle,
842 struct llog_operations *lop;
847 rc = llog_handle2ops(handle, &lop);
850 if (lop->lop_create == NULL)
853 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
855 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
856 rc = lop->lop_create(env, handle, th);
858 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
862 int llog_declare_write_rec(const struct lu_env *env,
863 struct llog_handle *handle,
864 struct llog_rec_hdr *rec, int idx,
867 struct llog_operations *lop;
872 rc = llog_handle2ops(handle, &lop);
876 if (lop->lop_declare_write_rec == NULL)
879 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
881 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
882 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
884 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
888 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
889 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
890 int idx, struct thandle *th)
892 struct llog_operations *lop;
893 int raised, rc, buflen;
897 /* API sanity checks */
898 if (handle == NULL) {
899 CERROR("loghandle is missed\n");
901 } else if (handle->lgh_obj == NULL) {
902 CERROR("loghandle %p with NULL object\n",
905 } else if (th == NULL) {
906 CERROR("%s: missed transaction handle\n",
907 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
909 } else if (handle->lgh_hdr == NULL) {
910 CERROR("%s: loghandle %p with no header\n",
911 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
916 rc = llog_handle2ops(handle, &lop);
920 if (lop->lop_write_rec == NULL)
923 buflen = rec->lrh_len;
924 LASSERT(cfs_size_round(buflen) == buflen);
926 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
928 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
929 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
931 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
935 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
936 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
943 if (lgh->lgh_logops->lop_add == NULL)
946 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
948 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
949 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
951 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
954 EXPORT_SYMBOL(llog_add);
956 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
957 struct llog_rec_hdr *rec, struct thandle *th)
963 if (lgh->lgh_logops->lop_declare_add == NULL)
966 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
968 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
969 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
971 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
974 EXPORT_SYMBOL(llog_declare_add);
977 * Helper function to open llog or create it if doesn't exist.
978 * It hides all transaction handling from caller.
980 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
981 struct llog_handle **res, struct llog_logid *logid,
990 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
994 if (llog_exist(*res))
997 LASSERT((*res)->lgh_obj != NULL);
999 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1001 th = dt_trans_create(env, d);
1003 GOTO(out, rc = PTR_ERR(th));
1005 /* Create update llog object synchronously, which
1006 * happens during inialization process see
1007 * lod_sub_prep_llog(), to make sure the update
1008 * llog object is created before corss-MDT writing
1009 * updates into the llog object */
1010 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1013 th->th_wait_submit = 1;
1014 rc = llog_declare_create(env, *res, th);
1016 rc = dt_trans_start_local(env, d, th);
1018 rc = llog_create(env, *res, th);
1020 dt_trans_stop(env, d, th);
1023 llog_close(env, *res);
1026 EXPORT_SYMBOL(llog_open_create);
1029 * Helper function to delete existent llog.
1031 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1032 struct llog_logid *logid, char *name)
1034 struct llog_handle *handle;
1039 /* nothing to erase */
1040 if (name == NULL && logid == NULL)
1043 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1047 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1049 rc = llog_destroy(env, handle);
1051 rc2 = llog_close(env, handle);
1056 EXPORT_SYMBOL(llog_erase);
1059 * Helper function for write record in llog.
1060 * It hides all transaction handling from caller.
1061 * Valid only with local llog.
1063 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1064 struct llog_rec_hdr *rec, int idx)
1066 struct dt_device *dt;
1073 LASSERT(loghandle->lgh_ctxt);
1074 LASSERT(loghandle->lgh_obj != NULL);
1076 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1078 th = dt_trans_create(env, dt);
1080 RETURN(PTR_ERR(th));
1082 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1084 GOTO(out_trans, rc);
1086 th->th_wait_submit = 1;
1087 rc = dt_trans_start_local(env, dt, th);
1089 GOTO(out_trans, rc);
1091 down_write(&loghandle->lgh_lock);
1092 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1093 up_write(&loghandle->lgh_lock);
1095 dt_trans_stop(env, dt, th);
1098 EXPORT_SYMBOL(llog_write);
1100 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1101 struct llog_handle **lgh, struct llog_logid *logid,
1102 char *name, enum llog_open_param open_param)
1110 LASSERT(ctxt->loc_logops);
1112 if (ctxt->loc_logops->lop_open == NULL) {
1114 RETURN(-EOPNOTSUPP);
1117 *lgh = llog_alloc_handle();
1120 (*lgh)->lgh_ctxt = ctxt;
1121 (*lgh)->lgh_logops = ctxt->loc_logops;
1123 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1125 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1126 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1128 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1130 llog_free_handle(*lgh);
1135 EXPORT_SYMBOL(llog_open);
1137 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1139 struct llog_operations *lop;
1144 rc = llog_handle2ops(loghandle, &lop);
1147 if (lop->lop_close == NULL)
1148 GOTO(out, rc = -EOPNOTSUPP);
1149 rc = lop->lop_close(env, loghandle);
1151 llog_handle_put(loghandle);
1154 EXPORT_SYMBOL(llog_close);
1157 * Helper function to get the llog size in records. It is used by MGS
1158 * mostly to check that config llog exists and contains data.
1160 * \param[in] env execution environment
1161 * \param[in] ctxt llog context
1162 * \param[in] name llog name
1164 * \retval true if there are records in llog besides a header
1165 * \retval false on error or llog without records
1167 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1170 struct llog_handle *llh;
1173 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1175 if (likely(rc == -ENOENT))
1180 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1182 GOTO(out_close, rc);
1183 rc = llog_get_size(llh);
1186 llog_close(env, llh);
1188 /* The header is record 1, the llog is still considered as empty
1189 * if there is only header */
1192 EXPORT_SYMBOL(llog_is_empty);
1194 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1195 struct llog_rec_hdr *rec, void *data)
1197 struct llog_handle *copy_llh = data;
1199 /* Append all records */
1200 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1203 /* backup plain llog */
1204 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1205 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1206 char *name, char *backup)
1208 struct llog_handle *llh, *bllh;
1213 /* open original log */
1214 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1216 /* the -ENOENT case is also reported to the caller
1217 * but silently so it should handle that if needed.
1220 CERROR("%s: failed to open log %s: rc = %d\n",
1221 obd->obd_name, name, rc);
1225 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1227 GOTO(out_close, rc);
1229 /* Make sure there's no old backup log */
1230 rc = llog_erase(env, bctxt, NULL, backup);
1231 if (rc < 0 && rc != -ENOENT)
1232 GOTO(out_close, rc);
1234 /* open backup log */
1235 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1237 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1238 obd->obd_name, backup, rc);
1239 GOTO(out_close, rc);
1242 /* check that backup llog is not the same object as original one */
1243 if (llh->lgh_obj == bllh->lgh_obj) {
1244 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1245 obd->obd_name, name, backup, llh->lgh_obj,
1247 GOTO(out_backup, rc = -EEXIST);
1250 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1252 GOTO(out_backup, rc);
1254 /* Copy log record by record */
1255 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1258 CERROR("%s: failed to backup log %s: rc = %d\n",
1259 obd->obd_name, name, rc);
1261 llog_close(env, bllh);
1263 llog_close(env, llh);
1266 EXPORT_SYMBOL(llog_backup);
1268 /* Get size of llog */
1269 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1274 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1276 CERROR("%s: attr_get failed, rc = %d\n",
1277 llh->lgh_ctxt->loc_obd->obd_name, rc);
1283 EXPORT_SYMBOL(llog_size);