4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/llog.c
34 * OST<->MDS recovery logging infrastructure.
35 * Invariants in implementation:
36 * - we do not share logs among different OST<->MDS connections, so that
37 * if an OST or MDS fails it need only look at log(s) relevant to itself
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41 * Author: Mikhail Pershin <tappro@whamcloud.com>
44 #define DEBUG_SUBSYSTEM S_LOG
46 #include <linux/pid_namespace.h>
47 #include <linux/kthread.h>
48 #include <llog_swab.h>
49 #include <lustre_log.h>
50 #include <obd_class.h>
51 #include "llog_internal.h"
53 * Allocate a new log or catalog handle
54 * Used inside llog_open().
56 static struct llog_handle *llog_alloc_handle(void)
58 struct llog_handle *loghandle;
60 OBD_ALLOC_PTR(loghandle);
61 if (loghandle == NULL)
64 init_rwsem(&loghandle->lgh_lock);
65 mutex_init(&loghandle->lgh_hdr_mutex);
66 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
67 atomic_set(&loghandle->lgh_refcount, 1);
73 * Free llog handle and header data if exists. Used in llog_close() only
75 static void llog_free_handle(struct llog_handle *loghandle)
77 LASSERT(loghandle != NULL);
79 /* failed llog_init_handle */
80 if (loghandle->lgh_hdr == NULL)
83 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
84 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
85 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
86 LASSERT(list_empty(&loghandle->u.chd.chd_head));
87 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
89 OBD_FREE_PTR(loghandle);
92 void llog_handle_get(struct llog_handle *loghandle)
94 atomic_inc(&loghandle->lgh_refcount);
97 void llog_handle_put(struct llog_handle *loghandle)
99 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
100 if (atomic_dec_and_test(&loghandle->lgh_refcount))
101 llog_free_handle(loghandle);
104 static int llog_declare_destroy(const struct lu_env *env,
105 struct llog_handle *handle,
108 struct llog_operations *lop;
113 rc = llog_handle2ops(handle, &lop);
116 if (lop->lop_declare_destroy == NULL)
119 rc = lop->lop_declare_destroy(env, handle, th);
124 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
127 struct llog_operations *lop;
131 rc = llog_handle2ops(handle, &lop);
134 if (lop->lop_destroy == NULL)
137 LASSERT(handle->lgh_obj != NULL);
138 if (!dt_object_exists(handle->lgh_obj))
141 rc = lop->lop_destroy(env, handle, th);
146 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
148 struct llog_operations *lop;
149 struct dt_device *dt;
155 rc = llog_handle2ops(handle, &lop);
158 if (lop->lop_destroy == NULL)
161 if (handle->lgh_obj == NULL) {
162 /* if lgh_obj == NULL, then it is from client side destroy */
163 rc = lop->lop_destroy(env, handle, NULL);
167 if (!dt_object_exists(handle->lgh_obj))
170 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
172 th = dt_trans_create(env, dt);
176 rc = llog_declare_destroy(env, handle, th);
180 rc = dt_trans_start_local(env, dt, th);
184 rc = lop->lop_destroy(env, handle, th);
187 dt_trans_stop(env, dt, th);
191 EXPORT_SYMBOL(llog_destroy);
193 /* returns negative on error; 0 if success; 1 if success & log destroyed */
194 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
197 struct llog_thread_info *lgi = llog_info(env);
198 struct dt_device *dt;
199 struct llog_log_hdr *llh = loghandle->lgh_hdr;
203 bool subtract_count = false;
207 CDEBUG(D_RPCTRACE, "Canceling %d in log "DFID"\n", index,
208 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
211 CERROR("Can't cancel index 0 which is header\n");
215 LASSERT(loghandle != NULL);
216 LASSERT(loghandle->lgh_ctxt != NULL);
217 LASSERT(loghandle->lgh_obj != NULL);
219 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
221 th = dt_trans_create(env, dt);
225 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
229 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
230 rc = llog_declare_destroy(env, loghandle, th);
235 th->th_wait_submit = 1;
236 rc = dt_trans_start_local(env, dt, th);
240 down_write(&loghandle->lgh_lock);
242 mutex_lock(&loghandle->lgh_hdr_mutex);
243 if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
244 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
245 GOTO(out_unlock, rc);
248 loghandle->lgh_hdr->llh_count--;
249 subtract_count = true;
250 /* Pass this index to llog_osd_write_rec(), which will use the index
251 * to only update the necesary bitmap. */
252 lgi->lgi_cookie.lgc_index = index;
254 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, &lgi->lgi_cookie,
255 LLOG_HEADER_IDX, th);
257 GOTO(out_unlock, rc);
259 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
260 (llh->llh_count == 1) &&
261 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
262 (loghandle->u.phd.phd_cat_handle != NULL &&
263 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
265 /* never try to destroy it again */
266 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
267 rc = llog_trans_destroy(env, loghandle, th);
269 /* Sigh, can not destroy the final plain llog, but
270 * the bitmap has been clearly, so the record can not
271 * be accessed anymore, let's return 0 for now, and
272 * the orphan will be handled by LFSCK. */
273 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
274 loghandle->lgh_ctxt->loc_obd->obd_name,
275 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
276 GOTO(out_unlock, rc = 0);
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 llh->llh_count = 1; /* for the header record */
318 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
319 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
320 LLOG_MIN_CHUNK_SIZE);
321 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
322 llh->llh_hdr.lrh_index = 0;
323 llh->llh_timestamp = ktime_get_real_seconds();
325 memcpy(&llh->llh_tgtuuid, uuid,
326 sizeof(llh->llh_tgtuuid));
327 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
328 /* Since update llog header might also call this function,
329 * let's reset the bitmap to 0 here */
330 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
331 llh->llh_bitmap_offset -
332 sizeof(llh->llh_tail));
333 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
334 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
335 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
340 EXPORT_SYMBOL(llog_read_header);
342 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
343 int flags, struct obd_uuid *uuid)
345 struct llog_log_hdr *llh;
346 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
348 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
351 LASSERT(handle->lgh_hdr == NULL);
353 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
354 OBD_ALLOC_LARGE(llh, chunk_size);
358 handle->lgh_hdr = llh;
359 handle->lgh_hdr_size = chunk_size;
360 /* first assign flags to use llog_client_ops */
361 llh->llh_flags = flags;
362 rc = llog_read_header(env, handle, uuid);
364 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
365 flags & LLOG_F_IS_CAT) ||
366 (llh->llh_flags & LLOG_F_IS_CAT &&
367 flags & LLOG_F_IS_PLAIN))) {
368 CERROR("%s: llog type is %s but initializing %s\n",
369 handle->lgh_ctxt->loc_obd->obd_name,
370 llh->llh_flags & LLOG_F_IS_CAT ?
372 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
373 GOTO(out, rc = -EINVAL);
374 } else if (llh->llh_flags &
375 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
377 * it is possible to open llog without specifying llog
378 * type so it is taken from llh_flags
380 flags = llh->llh_flags;
382 /* for some reason the llh_flags has no type set */
383 CERROR("llog type is not specified!\n");
384 GOTO(out, rc = -EINVAL);
387 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
388 CERROR("%s: llog uuid mismatch: %s/%s\n",
389 handle->lgh_ctxt->loc_obd->obd_name,
391 (char *)llh->llh_tgtuuid.uuid);
392 GOTO(out, rc = -EEXIST);
395 if (flags & LLOG_F_IS_CAT) {
396 LASSERT(list_empty(&handle->u.chd.chd_head));
397 INIT_LIST_HEAD(&handle->u.chd.chd_head);
398 llh->llh_size = sizeof(struct llog_logid_rec);
399 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
400 } else if (!(flags & LLOG_F_IS_PLAIN)) {
401 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
402 handle->lgh_ctxt->loc_obd->obd_name,
403 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
406 llh->llh_flags |= fmt;
409 OBD_FREE_LARGE(llh, chunk_size);
410 handle->lgh_hdr = NULL;
414 EXPORT_SYMBOL(llog_init_handle);
416 static int llog_process_thread(void *arg)
418 struct llog_process_info *lpi = arg;
419 struct llog_handle *loghandle = lpi->lpi_loghandle;
420 struct llog_log_hdr *llh = loghandle->lgh_hdr;
421 struct llog_process_cat_data *cd = lpi->lpi_catdata;
424 __u64 cur_offset, tmp_offset;
425 int rc = 0, index = 1, last_index;
427 int last_called_index = 0;
434 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
435 /* expect chunk_size to be power of two */
436 LASSERT(is_power_of_2(chunk_size));
438 OBD_ALLOC_LARGE(buf, chunk_size);
440 lpi->lpi_rc = -ENOMEM;
445 last_called_index = cd->lpcd_first_idx;
446 index = cd->lpcd_first_idx + 1;
448 if (cd != NULL && cd->lpcd_last_idx)
449 last_index = cd->lpcd_last_idx;
451 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
454 struct llog_rec_hdr *rec;
456 unsigned int buf_offset = 0;
459 /* skip records not set in bitmap */
460 while (index <= last_index &&
461 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
464 /* There are no indices prior the last_index */
465 if (index > last_index)
468 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
472 /* get the buf with our target record; avoid old garbage */
473 memset(buf, 0, chunk_size);
474 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
475 index, &cur_offset, buf, chunk_size);
479 /* NB: after llog_next_block() call the cur_offset is the
480 * offset of the next block after read one.
481 * The absolute offset of the current chunk is calculated
482 * from cur_offset value and stored in chunk_offset variable.
484 tmp_offset = cur_offset;
485 if (do_div(tmp_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
517 if (index > loghandle->lgh_last_idx)
519 /* <2 records means no more records
520 * if the last record we processed was
521 * the final one, then the underlying
522 * object might have been destroyed yet.
523 * we better don't access that.. */
524 mutex_lock(&loghandle->lgh_hdr_mutex);
525 records = loghandle->lgh_hdr->llh_count;
526 mutex_unlock(&loghandle->lgh_hdr_mutex);
529 CDEBUG(D_OTHER, "Re-read last llog buffer for "
530 "new records, index %u, last %u\n",
531 index, loghandle->lgh_last_idx);
532 /* save offset inside buffer for the re-read */
533 buf_offset = (char *)rec - (char *)buf;
534 cur_offset = chunk_offset;
538 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
539 CWARN("%s: invalid length %d in llog "DFID
540 "record for index %d/%d\n",
541 loghandle->lgh_ctxt->loc_obd->obd_name,
543 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
544 rec->lrh_index, index);
546 GOTO(out, rc = -EINVAL);
549 if (rec->lrh_index < index) {
550 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
555 if (rec->lrh_index != index) {
556 CERROR("%s: "DFID" Invalid record: index %u"
557 " but expected %u\n",
558 loghandle->lgh_ctxt->loc_obd->obd_name,
559 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
560 rec->lrh_index, index);
561 GOTO(out, rc = -ERANGE);
565 "lrh_index: %d lrh_len: %d (%d remains)\n",
566 rec->lrh_index, rec->lrh_len,
567 (int)(buf + chunk_size - (char *)rec));
569 loghandle->lgh_cur_idx = rec->lrh_index;
570 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
573 /* if set, process the callback on this record */
574 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
575 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
577 last_called_index = index;
578 if (rc == LLOG_PROC_BREAK) {
580 } else if (rc == LLOG_DEL_RECORD) {
581 rc = llog_cancel_rec(lpi->lpi_env,
588 /* exit if the last index is reached */
589 if (index >= last_index)
597 cd->lpcd_last_idx = last_called_index;
599 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
600 if (dt_object_remote(loghandle->lgh_obj)) {
601 /* If it is remote object, then -EIO might means
602 * disconnection or eviction, let's return -EAGAIN,
603 * so for update recovery log processing, it will
604 * retry until the umount or abort recovery, see
605 * lod_sub_recovery_thread() */
606 CERROR("%s retry remote llog process\n",
607 loghandle->lgh_ctxt->loc_obd->obd_name);
610 /* something bad happened to the processing of a local
611 * llog file, probably I/O error or the log got
612 * corrupted to be able to finally release the log we
613 * discard any remaining bits in the header */
614 CERROR("Local llog found corrupted\n");
615 while (index <= last_index) {
616 if (ext2_test_bit(index,
617 LLOG_HDR_BITMAP(llh)) != 0)
618 llog_cancel_rec(lpi->lpi_env, loghandle,
626 OBD_FREE_LARGE(buf, chunk_size);
631 static int llog_process_thread_daemonize(void *arg)
633 struct llog_process_info *lpi = arg;
636 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
638 task_lock(lpi->lpi_reftask);
639 new_ns = lpi->lpi_reftask->nsproxy;
640 if (curr_ns != new_ns) {
643 current->nsproxy = new_ns;
644 /* XXX: we should call put_nsproxy() instead of
645 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
646 * used outside of the kernel itself, because it calls
647 * free_nsproxy() which is not exported by the kernel
648 * (defined in kernel/nsproxy.c) */
649 atomic_dec(&curr_ns->count);
651 task_unlock(lpi->lpi_reftask);
655 /* client env has no keys, tags is just 0 */
656 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
661 rc = llog_process_thread(arg);
665 complete(&lpi->lpi_completion);
669 int llog_process_or_fork(const struct lu_env *env,
670 struct llog_handle *loghandle,
671 llog_cb_t cb, void *data, void *catdata, bool fork)
673 struct llog_process_info *lpi;
680 CERROR("cannot alloc pointer\n");
683 lpi->lpi_loghandle = loghandle;
685 lpi->lpi_cbdata = data;
686 lpi->lpi_catdata = catdata;
689 struct task_struct *task;
691 /* The new thread can't use parent env,
692 * init the new one in llog_process_thread_daemonize. */
694 init_completion(&lpi->lpi_completion);
695 /* take reference to current, so that
696 * llog_process_thread_daemonize() can use it to switch to
697 * namespace associated with current */
698 lpi->lpi_reftask = current;
699 task = kthread_run(llog_process_thread_daemonize, lpi,
700 "llog_process_thread");
703 CERROR("%s: cannot start thread: rc = %d\n",
704 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
707 wait_for_completion(&lpi->lpi_completion);
710 llog_process_thread(lpi);
718 EXPORT_SYMBOL(llog_process_or_fork);
720 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
721 llog_cb_t cb, void *data, void *catdata)
724 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
725 return rc == LLOG_DEL_PLAIN ? 0 : rc;
727 EXPORT_SYMBOL(llog_process);
729 int llog_reverse_process(const struct lu_env *env,
730 struct llog_handle *loghandle, llog_cb_t cb,
731 void *data, void *catdata)
733 struct llog_log_hdr *llh = loghandle->lgh_hdr;
734 struct llog_process_cat_data *cd = catdata;
736 int rc = 0, first_index = 1, index, idx;
737 __u32 chunk_size = llh->llh_hdr.lrh_len;
740 OBD_ALLOC_LARGE(buf, chunk_size);
745 first_index = cd->lpcd_first_idx + 1;
746 if (cd != NULL && cd->lpcd_last_idx)
747 index = cd->lpcd_last_idx;
749 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
752 struct llog_rec_hdr *rec;
753 struct llog_rec_tail *tail;
755 /* skip records not set in bitmap */
756 while (index >= first_index &&
757 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
760 LASSERT(index >= first_index - 1);
761 if (index == first_index - 1)
764 /* get the buf with our target record; avoid old garbage */
765 memset(buf, 0, chunk_size);
766 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
771 idx = rec->lrh_index;
772 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
773 while (idx < index) {
774 rec = (void *)rec + rec->lrh_len;
775 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
776 lustre_swab_llog_rec(rec);
779 LASSERT(idx == index);
780 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
782 /* process records in buffer, starting where we found one */
783 while ((void *)tail > buf) {
784 if (tail->lrt_index == 0)
785 GOTO(out, rc = 0); /* no more records */
787 /* if set, process the callback on this record */
788 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
789 rec = (void *)tail - tail->lrt_len +
792 rc = cb(env, loghandle, rec, data);
793 if (rc == LLOG_PROC_BREAK) {
795 } else if (rc == LLOG_DEL_RECORD) {
796 rc = llog_cancel_rec(env, loghandle,
803 /* previous record, still in buffer? */
805 if (index < first_index)
807 tail = (void *)tail - tail->lrt_len;
813 OBD_FREE_LARGE(buf, chunk_size);
816 EXPORT_SYMBOL(llog_reverse_process);
822 * llog_open - open llog, may not exist
823 * llog_exist - check if llog exists
824 * llog_close - close opened llog, pair for open, frees llog_handle
825 * llog_declare_create - declare llog creation
826 * llog_create - create new llog on disk, need transaction handle
827 * llog_declare_write_rec - declaration of llog write
828 * llog_write_rec - write llog record on disk, need transaction handle
829 * llog_declare_add - declare llog catalog record addition
830 * llog_add - add llog record in catalog, need transaction handle
832 int llog_exist(struct llog_handle *loghandle)
834 struct llog_operations *lop;
839 rc = llog_handle2ops(loghandle, &lop);
842 if (lop->lop_exist == NULL)
845 rc = lop->lop_exist(loghandle);
848 EXPORT_SYMBOL(llog_exist);
850 int llog_declare_create(const struct lu_env *env,
851 struct llog_handle *loghandle, struct thandle *th)
853 struct llog_operations *lop;
858 rc = llog_handle2ops(loghandle, &lop);
861 if (lop->lop_declare_create == NULL)
864 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
866 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
867 rc = lop->lop_declare_create(env, loghandle, th);
869 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
873 int llog_create(const struct lu_env *env, struct llog_handle *handle,
876 struct llog_operations *lop;
881 rc = llog_handle2ops(handle, &lop);
884 if (lop->lop_create == NULL)
887 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
889 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
890 rc = lop->lop_create(env, handle, th);
892 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
896 int llog_declare_write_rec(const struct lu_env *env,
897 struct llog_handle *handle,
898 struct llog_rec_hdr *rec, int idx,
901 struct llog_operations *lop;
906 rc = llog_handle2ops(handle, &lop);
910 if (lop->lop_declare_write_rec == NULL)
913 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
915 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
916 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
918 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
922 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
923 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
924 int idx, struct thandle *th)
926 struct llog_operations *lop;
927 int raised, rc, buflen;
931 /* API sanity checks */
932 if (handle == NULL) {
933 CERROR("loghandle is missed\n");
935 } else if (handle->lgh_obj == NULL) {
936 CERROR("loghandle %p with NULL object\n",
939 } else if (th == NULL) {
940 CERROR("%s: missed transaction handle\n",
941 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
943 } else if (handle->lgh_hdr == NULL) {
944 CERROR("%s: loghandle %p with no header\n",
945 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
950 rc = llog_handle2ops(handle, &lop);
954 if (lop->lop_write_rec == NULL)
957 buflen = rec->lrh_len;
958 LASSERT(cfs_size_round(buflen) == buflen);
960 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
962 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
963 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
965 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
969 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
970 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
977 if (lgh->lgh_logops->lop_add == NULL)
980 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
982 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
983 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
985 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
988 EXPORT_SYMBOL(llog_add);
990 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
991 struct llog_rec_hdr *rec, struct thandle *th)
997 if (lgh->lgh_logops->lop_declare_add == NULL)
1000 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1002 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1003 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1005 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1008 EXPORT_SYMBOL(llog_declare_add);
1011 * Helper function to open llog or create it if doesn't exist.
1012 * It hides all transaction handling from caller.
1014 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1015 struct llog_handle **res, struct llog_logid *logid,
1018 struct dt_device *d;
1024 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1028 if (llog_exist(*res))
1031 LASSERT((*res)->lgh_obj != NULL);
1033 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1035 th = dt_trans_create(env, d);
1037 GOTO(out, rc = PTR_ERR(th));
1039 /* Create update llog object synchronously, which
1040 * happens during inialization process see
1041 * lod_sub_prep_llog(), to make sure the update
1042 * llog object is created before corss-MDT writing
1043 * updates into the llog object */
1044 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1047 th->th_wait_submit = 1;
1048 rc = llog_declare_create(env, *res, th);
1050 rc = dt_trans_start_local(env, d, th);
1052 rc = llog_create(env, *res, th);
1054 dt_trans_stop(env, d, th);
1057 llog_close(env, *res);
1060 EXPORT_SYMBOL(llog_open_create);
1063 * Helper function to delete existent llog.
1065 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1066 struct llog_logid *logid, char *name)
1068 struct llog_handle *handle;
1073 /* nothing to erase */
1074 if (name == NULL && logid == NULL)
1077 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1081 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1083 rc = llog_destroy(env, handle);
1085 rc2 = llog_close(env, handle);
1090 EXPORT_SYMBOL(llog_erase);
1093 * Helper function for write record in llog.
1094 * It hides all transaction handling from caller.
1095 * Valid only with local llog.
1097 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1098 struct llog_rec_hdr *rec, int idx)
1100 struct dt_device *dt;
1107 LASSERT(loghandle->lgh_ctxt);
1108 LASSERT(loghandle->lgh_obj != NULL);
1110 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1112 th = dt_trans_create(env, dt);
1114 RETURN(PTR_ERR(th));
1116 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1118 GOTO(out_trans, rc);
1120 th->th_wait_submit = 1;
1121 rc = dt_trans_start_local(env, dt, th);
1123 GOTO(out_trans, rc);
1125 down_write(&loghandle->lgh_lock);
1126 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1127 up_write(&loghandle->lgh_lock);
1129 dt_trans_stop(env, dt, th);
1132 EXPORT_SYMBOL(llog_write);
1134 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1135 struct llog_handle **lgh, struct llog_logid *logid,
1136 char *name, enum llog_open_param open_param)
1144 LASSERT(ctxt->loc_logops);
1146 if (ctxt->loc_logops->lop_open == NULL) {
1148 RETURN(-EOPNOTSUPP);
1151 *lgh = llog_alloc_handle();
1154 (*lgh)->lgh_ctxt = ctxt;
1155 (*lgh)->lgh_logops = ctxt->loc_logops;
1157 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1159 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1160 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1162 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1164 llog_free_handle(*lgh);
1169 EXPORT_SYMBOL(llog_open);
1171 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1173 struct llog_operations *lop;
1178 rc = llog_handle2ops(loghandle, &lop);
1181 if (lop->lop_close == NULL)
1182 GOTO(out, rc = -EOPNOTSUPP);
1183 rc = lop->lop_close(env, loghandle);
1185 llog_handle_put(loghandle);
1188 EXPORT_SYMBOL(llog_close);
1191 * Helper function to get the llog size in records. It is used by MGS
1192 * mostly to check that config llog exists and contains data.
1194 * \param[in] env execution environment
1195 * \param[in] ctxt llog context
1196 * \param[in] name llog name
1198 * \retval true if there are records in llog besides a header
1199 * \retval false on error or llog without records
1201 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1204 struct llog_handle *llh;
1207 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1209 if (likely(rc == -ENOENT))
1214 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1216 GOTO(out_close, rc);
1217 rc = llog_get_size(llh);
1220 llog_close(env, llh);
1222 /* The header is record 1, the llog is still considered as empty
1223 * if there is only header */
1226 EXPORT_SYMBOL(llog_is_empty);
1228 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1229 struct llog_rec_hdr *rec, void *data)
1231 struct llog_handle *copy_llh = data;
1233 /* Append all records */
1234 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1237 /* backup plain llog */
1238 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1239 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1240 char *name, char *backup)
1242 struct llog_handle *llh, *bllh;
1247 /* open original log */
1248 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1250 /* the -ENOENT case is also reported to the caller
1251 * but silently so it should handle that if needed.
1254 CERROR("%s: failed to open log %s: rc = %d\n",
1255 obd->obd_name, name, rc);
1259 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1261 GOTO(out_close, rc);
1263 /* Make sure there's no old backup log */
1264 rc = llog_erase(env, bctxt, NULL, backup);
1265 if (rc < 0 && rc != -ENOENT)
1266 GOTO(out_close, rc);
1268 /* open backup log */
1269 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1271 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1272 obd->obd_name, backup, rc);
1273 GOTO(out_close, rc);
1276 /* check that backup llog is not the same object as original one */
1277 if (llh->lgh_obj == bllh->lgh_obj) {
1278 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1279 obd->obd_name, name, backup, llh->lgh_obj,
1281 GOTO(out_backup, rc = -EEXIST);
1284 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1286 GOTO(out_backup, rc);
1288 /* Copy log record by record */
1289 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1292 CERROR("%s: failed to backup log %s: rc = %d\n",
1293 obd->obd_name, name, rc);
1295 llog_close(env, bllh);
1297 llog_close(env, llh);
1300 EXPORT_SYMBOL(llog_backup);
1302 /* Get size of llog */
1303 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1308 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1310 CERROR("%s: attr_get failed, rc = %d\n",
1311 llh->lgh_ctxt->loc_obd->obd_name, rc);
1317 EXPORT_SYMBOL(llog_size);