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 (loghandle->u.phd.phd_cat_handle != NULL &&
263 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
265 rc = llog_trans_destroy(env, loghandle, th);
267 /* Sigh, can not destroy the final plain llog, but
268 * the bitmap has been clearly, so the record can not
269 * be accessed anymore, let's return 0 for now, and
270 * the orphan will be handled by LFSCK. */
271 CERROR("%s: can't destroy empty llog #"DOSTID
273 loghandle->lgh_ctxt->loc_obd->obd_name,
274 POSTID(&loghandle->lgh_id.lgl_oi),
275 loghandle->lgh_id.lgl_ogen, rc);
276 GOTO(out_unlock, rc);
282 mutex_unlock(&loghandle->lgh_hdr_mutex);
283 up_write(&loghandle->lgh_lock);
285 rc1 = dt_trans_stop(env, dt, th);
288 if (rc < 0 && subtract_count) {
289 mutex_lock(&loghandle->lgh_hdr_mutex);
290 loghandle->lgh_hdr->llh_count++;
291 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
292 mutex_unlock(&loghandle->lgh_hdr_mutex);
297 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
298 const struct obd_uuid *uuid)
300 struct llog_operations *lop;
304 rc = llog_handle2ops(handle, &lop);
308 if (lop->lop_read_header == NULL)
311 rc = lop->lop_read_header(env, handle);
312 if (rc == LLOG_EEMPTY) {
313 struct llog_log_hdr *llh = handle->lgh_hdr;
315 /* lrh_len should be initialized in llog_init_handle */
316 handle->lgh_last_idx = 0; /* header is record with index 0 */
317 handle->lgh_write_offset = 0;
318 llh->llh_count = 1; /* for the header record */
319 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
320 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
321 LLOG_MIN_CHUNK_SIZE);
322 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
323 llh->llh_hdr.lrh_index = 0;
324 llh->llh_timestamp = cfs_time_current_sec();
326 memcpy(&llh->llh_tgtuuid, uuid,
327 sizeof(llh->llh_tgtuuid));
328 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
329 /* Since update llog header might also call this function,
330 * let's reset the bitmap to 0 here */
331 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
332 llh->llh_bitmap_offset -
333 sizeof(llh->llh_tail));
334 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
335 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
336 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
341 EXPORT_SYMBOL(llog_read_header);
343 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
344 int flags, struct obd_uuid *uuid)
346 struct llog_log_hdr *llh;
347 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
349 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
352 LASSERT(handle->lgh_hdr == NULL);
354 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
355 OBD_ALLOC_LARGE(llh, chunk_size);
359 handle->lgh_hdr = llh;
360 handle->lgh_hdr_size = chunk_size;
361 /* first assign flags to use llog_client_ops */
362 llh->llh_flags = flags;
363 rc = llog_read_header(env, handle, uuid);
365 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
366 flags & LLOG_F_IS_CAT) ||
367 (llh->llh_flags & LLOG_F_IS_CAT &&
368 flags & LLOG_F_IS_PLAIN))) {
369 CERROR("%s: llog type is %s but initializing %s\n",
370 handle->lgh_ctxt->loc_obd->obd_name,
371 llh->llh_flags & LLOG_F_IS_CAT ?
373 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
374 GOTO(out, rc = -EINVAL);
375 } else if (llh->llh_flags &
376 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
378 * it is possible to open llog without specifying llog
379 * type so it is taken from llh_flags
381 flags = llh->llh_flags;
383 /* for some reason the llh_flags has no type set */
384 CERROR("llog type is not specified!\n");
385 GOTO(out, rc = -EINVAL);
388 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
389 CERROR("%s: llog uuid mismatch: %s/%s\n",
390 handle->lgh_ctxt->loc_obd->obd_name,
392 (char *)llh->llh_tgtuuid.uuid);
393 GOTO(out, rc = -EEXIST);
396 if (flags & LLOG_F_IS_CAT) {
397 LASSERT(list_empty(&handle->u.chd.chd_head));
398 INIT_LIST_HEAD(&handle->u.chd.chd_head);
399 llh->llh_size = sizeof(struct llog_logid_rec);
400 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
401 } else if (!(flags & LLOG_F_IS_PLAIN)) {
402 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
403 handle->lgh_ctxt->loc_obd->obd_name,
404 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
407 llh->llh_flags |= fmt;
410 OBD_FREE_LARGE(llh, chunk_size);
411 handle->lgh_hdr = NULL;
415 EXPORT_SYMBOL(llog_init_handle);
417 static int llog_process_thread(void *arg)
419 struct llog_process_info *lpi = arg;
420 struct llog_handle *loghandle = lpi->lpi_loghandle;
421 struct llog_log_hdr *llh = loghandle->lgh_hdr;
422 struct llog_process_cat_data *cd = lpi->lpi_catdata;
426 int rc = 0, index = 1, last_index;
428 int last_called_index = 0;
435 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
436 /* expect chunk_size to be power of two */
437 LASSERT(is_power_of_2(chunk_size));
439 OBD_ALLOC_LARGE(buf, chunk_size);
441 lpi->lpi_rc = -ENOMEM;
446 last_called_index = cd->lpcd_first_idx;
447 index = cd->lpcd_first_idx + 1;
449 if (cd != NULL && cd->lpcd_last_idx)
450 last_index = cd->lpcd_last_idx;
452 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
455 struct llog_rec_hdr *rec;
457 unsigned int buf_offset = 0;
460 /* skip records not set in bitmap */
461 while (index <= last_index &&
462 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
465 /* There are no indices prior the last_index */
466 if (index > last_index)
469 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
473 /* get the buf with our target record; avoid old garbage */
474 memset(buf, 0, chunk_size);
475 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
476 index, &cur_offset, buf, chunk_size);
480 /* NB: after llog_next_block() call the cur_offset is the
481 * offset of the next block after read one.
482 * The absolute offset of the current chunk is calculated
483 * from cur_offset value and stored in chunk_offset variable.
485 if (cur_offset % chunk_size != 0) {
486 partial_chunk = true;
487 chunk_offset = cur_offset & ~(chunk_size - 1);
489 partial_chunk = false;
490 chunk_offset = cur_offset - chunk_size;
493 /* NB: when rec->lrh_len is accessed it is already swabbed
494 * since it is used at the "end" of the loop and the rec
495 * swabbing is done at the beginning of the loop. */
496 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
497 (char *)rec < buf + chunk_size;
498 rec = llog_rec_hdr_next(rec)) {
500 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
503 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
504 lustre_swab_llog_rec(rec);
506 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
507 rec->lrh_type, rec->lrh_index);
509 /* for partial chunk the end of it is zeroed, check
510 * for index 0 to distinguish it. */
511 if (partial_chunk && rec->lrh_index == 0) {
512 /* concurrent llog_add() might add new records
513 * while llog_processing, check this is not
514 * the case and re-read the current chunk
516 if (index > loghandle->lgh_last_idx)
518 CDEBUG(D_OTHER, "Re-read last llog buffer for "
519 "new records, index %u, last %u\n",
520 index, loghandle->lgh_last_idx);
521 /* save offset inside buffer for the re-read */
522 buf_offset = (char *)rec - (char *)buf;
523 cur_offset = chunk_offset;
527 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
528 CWARN("invalid length %d in llog record for "
529 "index %d/%d\n", rec->lrh_len,
530 rec->lrh_index, index);
531 GOTO(out, rc = -EINVAL);
534 if (rec->lrh_index < index) {
535 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
540 if (rec->lrh_index != index) {
541 CERROR("%s: Invalid record: index %u but "
543 loghandle->lgh_ctxt->loc_obd->obd_name,
544 rec->lrh_index, index);
545 GOTO(out, rc = -ERANGE);
549 "lrh_index: %d lrh_len: %d (%d remains)\n",
550 rec->lrh_index, rec->lrh_len,
551 (int)(buf + chunk_size - (char *)rec));
553 loghandle->lgh_cur_idx = rec->lrh_index;
554 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
557 /* if set, process the callback on this record */
558 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
559 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
561 last_called_index = index;
562 if (rc == LLOG_PROC_BREAK) {
564 } else if (rc == LLOG_DEL_RECORD) {
565 rc = llog_cancel_rec(lpi->lpi_env,
572 /* exit if the last index is reached */
573 if (index >= last_index)
581 cd->lpcd_last_idx = last_called_index;
583 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
584 if (dt_object_remote(loghandle->lgh_obj)) {
585 /* If it is remote object, then -EIO might means
586 * disconnection or eviction, let's return -EAGAIN,
587 * so for update recovery log processing, it will
588 * retry until the umount or abort recovery, see
589 * lod_sub_recovery_thread() */
590 CERROR("%s retry remote llog process\n",
591 loghandle->lgh_ctxt->loc_obd->obd_name);
594 /* something bad happened to the processing of a local
595 * llog file, probably I/O error or the log got
596 * corrupted to be able to finally release the log we
597 * discard any remaining bits in the header */
598 CERROR("Local llog found corrupted\n");
599 while (index <= last_index) {
600 if (ext2_test_bit(index,
601 LLOG_HDR_BITMAP(llh)) != 0)
602 llog_cancel_rec(lpi->lpi_env, loghandle,
610 OBD_FREE_LARGE(buf, chunk_size);
615 static int llog_process_thread_daemonize(void *arg)
617 struct llog_process_info *lpi = arg;
623 /* client env has no keys, tags is just 0 */
624 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
629 rc = llog_process_thread(arg);
633 complete(&lpi->lpi_completion);
637 int llog_process_or_fork(const struct lu_env *env,
638 struct llog_handle *loghandle,
639 llog_cb_t cb, void *data, void *catdata, bool fork)
641 struct llog_process_info *lpi;
648 CERROR("cannot alloc pointer\n");
651 lpi->lpi_loghandle = loghandle;
653 lpi->lpi_cbdata = data;
654 lpi->lpi_catdata = catdata;
657 struct task_struct *task;
659 /* The new thread can't use parent env,
660 * init the new one in llog_process_thread_daemonize. */
662 init_completion(&lpi->lpi_completion);
663 task = kthread_run(llog_process_thread_daemonize, lpi,
664 "llog_process_thread");
667 CERROR("%s: cannot start thread: rc = %d\n",
668 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
671 wait_for_completion(&lpi->lpi_completion);
674 llog_process_thread(lpi);
682 EXPORT_SYMBOL(llog_process_or_fork);
684 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
685 llog_cb_t cb, void *data, void *catdata)
688 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
689 return rc == LLOG_DEL_PLAIN ? 0 : rc;
691 EXPORT_SYMBOL(llog_process);
693 int llog_reverse_process(const struct lu_env *env,
694 struct llog_handle *loghandle, llog_cb_t cb,
695 void *data, void *catdata)
697 struct llog_log_hdr *llh = loghandle->lgh_hdr;
698 struct llog_process_cat_data *cd = catdata;
700 int rc = 0, first_index = 1, index, idx;
701 __u32 chunk_size = llh->llh_hdr.lrh_len;
704 OBD_ALLOC_LARGE(buf, chunk_size);
709 first_index = cd->lpcd_first_idx + 1;
710 if (cd != NULL && cd->lpcd_last_idx)
711 index = cd->lpcd_last_idx;
713 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
716 struct llog_rec_hdr *rec;
717 struct llog_rec_tail *tail;
719 /* skip records not set in bitmap */
720 while (index >= first_index &&
721 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
724 LASSERT(index >= first_index - 1);
725 if (index == first_index - 1)
728 /* get the buf with our target record; avoid old garbage */
729 memset(buf, 0, chunk_size);
730 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
735 idx = rec->lrh_index;
736 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
737 while (idx < index) {
738 rec = (void *)rec + rec->lrh_len;
739 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
740 lustre_swab_llog_rec(rec);
743 LASSERT(idx == index);
744 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
746 /* process records in buffer, starting where we found one */
747 while ((void *)tail > buf) {
748 if (tail->lrt_index == 0)
749 GOTO(out, rc = 0); /* no more records */
751 /* if set, process the callback on this record */
752 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
753 rec = (void *)tail - tail->lrt_len +
756 rc = cb(env, loghandle, rec, data);
757 if (rc == LLOG_PROC_BREAK) {
759 } else if (rc == LLOG_DEL_RECORD) {
760 rc = llog_cancel_rec(env, loghandle,
767 /* previous record, still in buffer? */
769 if (index < first_index)
771 tail = (void *)tail - tail->lrt_len;
777 OBD_FREE_LARGE(buf, chunk_size);
780 EXPORT_SYMBOL(llog_reverse_process);
786 * llog_open - open llog, may not exist
787 * llog_exist - check if llog exists
788 * llog_close - close opened llog, pair for open, frees llog_handle
789 * llog_declare_create - declare llog creation
790 * llog_create - create new llog on disk, need transaction handle
791 * llog_declare_write_rec - declaration of llog write
792 * llog_write_rec - write llog record on disk, need transaction handle
793 * llog_declare_add - declare llog catalog record addition
794 * llog_add - add llog record in catalog, need transaction handle
796 int llog_exist(struct llog_handle *loghandle)
798 struct llog_operations *lop;
803 rc = llog_handle2ops(loghandle, &lop);
806 if (lop->lop_exist == NULL)
809 rc = lop->lop_exist(loghandle);
812 EXPORT_SYMBOL(llog_exist);
814 int llog_declare_create(const struct lu_env *env,
815 struct llog_handle *loghandle, struct thandle *th)
817 struct llog_operations *lop;
822 rc = llog_handle2ops(loghandle, &lop);
825 if (lop->lop_declare_create == NULL)
828 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
830 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
831 rc = lop->lop_declare_create(env, loghandle, th);
833 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
837 int llog_create(const struct lu_env *env, struct llog_handle *handle,
840 struct llog_operations *lop;
845 rc = llog_handle2ops(handle, &lop);
848 if (lop->lop_create == NULL)
851 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
853 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
854 rc = lop->lop_create(env, handle, th);
856 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
860 int llog_declare_write_rec(const struct lu_env *env,
861 struct llog_handle *handle,
862 struct llog_rec_hdr *rec, int idx,
865 struct llog_operations *lop;
870 rc = llog_handle2ops(handle, &lop);
874 if (lop->lop_declare_write_rec == NULL)
877 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
879 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
880 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
882 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
886 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
887 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
888 int idx, struct thandle *th)
890 struct llog_operations *lop;
891 int raised, rc, buflen;
895 rc = llog_handle2ops(handle, &lop);
900 if (lop->lop_write_rec == NULL)
903 buflen = rec->lrh_len;
904 LASSERT(cfs_size_round(buflen) == buflen);
906 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
908 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
909 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
911 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
915 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
916 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
923 if (lgh->lgh_logops->lop_add == NULL)
926 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
928 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
929 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
931 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
934 EXPORT_SYMBOL(llog_add);
936 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
937 struct llog_rec_hdr *rec, struct thandle *th)
943 if (lgh->lgh_logops->lop_declare_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_declare_add(env, lgh, rec, th);
951 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
954 EXPORT_SYMBOL(llog_declare_add);
957 * Helper function to open llog or create it if doesn't exist.
958 * It hides all transaction handling from caller.
960 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
961 struct llog_handle **res, struct llog_logid *logid,
970 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
974 if (llog_exist(*res))
977 LASSERT((*res)->lgh_obj != NULL);
979 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
981 th = dt_trans_create(env, d);
983 GOTO(out, rc = PTR_ERR(th));
985 /* Create update llog object synchronously, which
986 * happens during inialization process see
987 * lod_sub_prep_llog(), to make sure the update
988 * llog object is created before corss-MDT writing
989 * updates into the llog object */
990 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
993 th->th_wait_submit = 1;
994 rc = llog_declare_create(env, *res, th);
996 rc = dt_trans_start_local(env, d, th);
998 rc = llog_create(env, *res, th);
1000 dt_trans_stop(env, d, th);
1003 llog_close(env, *res);
1006 EXPORT_SYMBOL(llog_open_create);
1009 * Helper function to delete existent llog.
1011 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1012 struct llog_logid *logid, char *name)
1014 struct llog_handle *handle;
1019 /* nothing to erase */
1020 if (name == NULL && logid == NULL)
1023 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1027 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1029 rc = llog_destroy(env, handle);
1031 rc2 = llog_close(env, handle);
1036 EXPORT_SYMBOL(llog_erase);
1039 * Helper function for write record in llog.
1040 * It hides all transaction handling from caller.
1041 * Valid only with local llog.
1043 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1044 struct llog_rec_hdr *rec, int idx)
1046 struct dt_device *dt;
1053 LASSERT(loghandle->lgh_ctxt);
1054 LASSERT(loghandle->lgh_obj != NULL);
1056 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1058 th = dt_trans_create(env, dt);
1060 RETURN(PTR_ERR(th));
1062 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1064 GOTO(out_trans, rc);
1066 th->th_wait_submit = 1;
1067 rc = dt_trans_start_local(env, dt, th);
1069 GOTO(out_trans, rc);
1071 down_write(&loghandle->lgh_lock);
1072 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1073 up_write(&loghandle->lgh_lock);
1075 dt_trans_stop(env, dt, th);
1078 EXPORT_SYMBOL(llog_write);
1080 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1081 struct llog_handle **lgh, struct llog_logid *logid,
1082 char *name, enum llog_open_param open_param)
1090 LASSERT(ctxt->loc_logops);
1092 if (ctxt->loc_logops->lop_open == NULL) {
1094 RETURN(-EOPNOTSUPP);
1097 *lgh = llog_alloc_handle();
1100 (*lgh)->lgh_ctxt = ctxt;
1101 (*lgh)->lgh_logops = ctxt->loc_logops;
1103 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1105 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1106 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1108 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1110 llog_free_handle(*lgh);
1115 EXPORT_SYMBOL(llog_open);
1117 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1119 struct llog_operations *lop;
1124 rc = llog_handle2ops(loghandle, &lop);
1127 if (lop->lop_close == NULL)
1128 GOTO(out, rc = -EOPNOTSUPP);
1129 rc = lop->lop_close(env, loghandle);
1131 llog_handle_put(loghandle);
1134 EXPORT_SYMBOL(llog_close);
1137 * Helper function to get the llog size in records. It is used by MGS
1138 * mostly to check that config llog exists and contains data.
1140 * \param[in] env execution environment
1141 * \param[in] ctxt llog context
1142 * \param[in] name llog name
1144 * \retval true if there are records in llog besides a header
1145 * \retval false on error or llog without records
1147 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1150 struct llog_handle *llh;
1153 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1155 if (likely(rc == -ENOENT))
1160 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1162 GOTO(out_close, rc);
1163 rc = llog_get_size(llh);
1166 llog_close(env, llh);
1168 /* The header is record 1, the llog is still considered as empty
1169 * if there is only header */
1172 EXPORT_SYMBOL(llog_is_empty);
1174 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1175 struct llog_rec_hdr *rec, void *data)
1177 struct llog_handle *copy_llh = data;
1179 /* Append all records */
1180 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1183 /* backup plain llog */
1184 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1185 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1186 char *name, char *backup)
1188 struct llog_handle *llh, *bllh;
1193 /* open original log */
1194 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1196 /* the -ENOENT case is also reported to the caller
1197 * but silently so it should handle that if needed.
1200 CERROR("%s: failed to open log %s: rc = %d\n",
1201 obd->obd_name, name, rc);
1205 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1207 GOTO(out_close, rc);
1209 /* Make sure there's no old backup log */
1210 rc = llog_erase(env, bctxt, NULL, backup);
1211 if (rc < 0 && rc != -ENOENT)
1212 GOTO(out_close, rc);
1214 /* open backup log */
1215 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1217 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1218 obd->obd_name, backup, rc);
1219 GOTO(out_close, rc);
1222 /* check that backup llog is not the same object as original one */
1223 if (llh->lgh_obj == bllh->lgh_obj) {
1224 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1225 obd->obd_name, name, backup, llh->lgh_obj,
1227 GOTO(out_backup, rc = -EEXIST);
1230 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1232 GOTO(out_backup, rc);
1234 /* Copy log record by record */
1235 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1238 CERROR("%s: failed to backup log %s: rc = %d\n",
1239 obd->obd_name, name, rc);
1241 llog_close(env, bllh);
1243 llog_close(env, llh);
1246 EXPORT_SYMBOL(llog_backup);