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, 2017, 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_support.h>
51 #include <obd_class.h>
52 #include "llog_internal.h"
54 * Allocate a new log or catalog handle
55 * Used inside llog_open().
57 static struct llog_handle *llog_alloc_handle(void)
59 struct llog_handle *loghandle;
61 OBD_ALLOC_PTR(loghandle);
62 if (loghandle == NULL)
65 init_rwsem(&loghandle->lgh_lock);
66 mutex_init(&loghandle->lgh_hdr_mutex);
67 init_rwsem(&loghandle->lgh_last_sem);
68 INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
69 atomic_set(&loghandle->lgh_refcount, 1);
75 * Free llog handle and header data if exists. Used in llog_close() only
77 static void llog_free_handle(struct llog_handle *loghandle)
79 LASSERT(loghandle != NULL);
81 /* failed llog_init_handle */
82 if (loghandle->lgh_hdr == NULL)
85 if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
86 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
87 else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
88 LASSERT(list_empty(&loghandle->u.chd.chd_head));
89 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
91 OBD_FREE_PTR(loghandle);
94 void llog_handle_get(struct llog_handle *loghandle)
96 atomic_inc(&loghandle->lgh_refcount);
99 void llog_handle_put(struct llog_handle *loghandle)
101 LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
102 if (atomic_dec_and_test(&loghandle->lgh_refcount))
103 llog_free_handle(loghandle);
106 static int llog_declare_destroy(const struct lu_env *env,
107 struct llog_handle *handle,
110 struct llog_operations *lop;
115 rc = llog_handle2ops(handle, &lop);
118 if (lop->lop_declare_destroy == NULL)
121 rc = lop->lop_declare_destroy(env, handle, th);
126 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
129 struct llog_operations *lop;
133 rc = llog_handle2ops(handle, &lop);
136 if (lop->lop_destroy == NULL)
139 LASSERT(handle->lgh_obj != NULL);
140 if (!dt_object_exists(handle->lgh_obj))
143 rc = lop->lop_destroy(env, handle, th);
148 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
150 struct llog_operations *lop;
151 struct dt_device *dt;
157 rc = llog_handle2ops(handle, &lop);
160 if (lop->lop_destroy == NULL)
163 if (handle->lgh_obj == NULL) {
164 /* if lgh_obj == NULL, then it is from client side destroy */
165 rc = lop->lop_destroy(env, handle, NULL);
169 if (!dt_object_exists(handle->lgh_obj))
172 dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
174 if (unlikely(unlikely(dt->dd_rdonly)))
177 th = dt_trans_create(env, dt);
181 rc = llog_declare_destroy(env, handle, th);
185 rc = dt_trans_start_local(env, dt, th);
189 rc = lop->lop_destroy(env, handle, th);
192 dt_trans_stop(env, dt, th);
196 EXPORT_SYMBOL(llog_destroy);
198 /* returns negative on error; 0 if success; 1 if success & log destroyed */
199 int llog_cancel_arr_rec(const struct lu_env *env, struct llog_handle *loghandle,
202 struct llog_thread_info *lgi = llog_info(env);
203 struct dt_device *dt;
204 struct llog_log_hdr *llh;
209 bool subtract_count = false;
213 LASSERT(loghandle != NULL);
214 LASSERT(loghandle->lgh_ctxt != NULL);
215 LASSERT(loghandle->lgh_obj != NULL);
217 llh = loghandle->lgh_hdr;
219 CDEBUG(D_RPCTRACE, "Canceling %d records, first %d in log "DFID"\n",
220 num, index[0], PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
222 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
224 if (unlikely(unlikely(dt->dd_rdonly)))
227 th = dt_trans_create(env, dt);
231 rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, 0, th);
235 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
236 rc = llog_declare_destroy(env, loghandle, th);
241 th->th_wait_submit = 1;
242 rc = dt_trans_start_local(env, dt, th);
246 down_write(&loghandle->lgh_lock);
248 mutex_lock(&loghandle->lgh_hdr_mutex);
249 for (i = 0; i < num; ++i) {
251 CERROR("Can't cancel index 0 which is header\n");
252 GOTO(out_unlock, rc = -EINVAL);
254 if (!ext2_clear_bit(index[i], LLOG_HDR_BITMAP(llh))) {
255 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n",
257 GOTO(out_unlock, rc = -ENOENT);
260 loghandle->lgh_hdr->llh_count -= num;
261 subtract_count = true;
263 /* Since llog_process_thread use lgi_cookie, it`s better to save them
264 * and restore after using
266 tmp_lgc_index = lgi->lgi_cookie.lgc_index;
267 /* Pass this index to llog_osd_write_rec(), which will use the index
268 * to only update the necesary bitmap. */
269 lgi->lgi_cookie.lgc_index = index[0];
271 rc = llog_write_rec(env, loghandle, &llh->llh_hdr, (num != 1 ? NULL :
272 &lgi->lgi_cookie), LLOG_HEADER_IDX, th);
273 lgi->lgi_cookie.lgc_index = tmp_lgc_index;
276 GOTO(out_unlock, rc);
278 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
279 (llh->llh_count == 1) &&
280 ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
281 (loghandle->u.phd.phd_cat_handle != NULL &&
282 loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
284 /* never try to destroy it again */
285 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
286 rc = llog_trans_destroy(env, loghandle, th);
288 /* Sigh, can not destroy the final plain llog, but
289 * the bitmap has been clearly, so the record can not
290 * be accessed anymore, let's return 0 for now, and
291 * the orphan will be handled by LFSCK. */
292 CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
293 loghandle->lgh_ctxt->loc_obd->obd_name,
294 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
295 GOTO(out_unlock, rc = 0);
301 mutex_unlock(&loghandle->lgh_hdr_mutex);
302 up_write(&loghandle->lgh_lock);
304 rc1 = dt_trans_stop(env, dt, th);
308 mutex_lock(&loghandle->lgh_hdr_mutex);
310 loghandle->lgh_hdr->llh_count += num;
311 for (i = i - 1; i >= 0; i--)
312 ext2_set_bit(index[i], LLOG_HDR_BITMAP(llh));
313 mutex_unlock(&loghandle->lgh_hdr_mutex);
318 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
321 return llog_cancel_arr_rec(env, loghandle, 1, &index);
324 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
325 const struct obd_uuid *uuid)
327 struct llog_operations *lop;
331 rc = llog_handle2ops(handle, &lop);
335 if (lop->lop_read_header == NULL)
338 rc = lop->lop_read_header(env, handle);
339 if (rc == LLOG_EEMPTY) {
340 struct llog_log_hdr *llh = handle->lgh_hdr;
342 /* lrh_len should be initialized in llog_init_handle */
343 handle->lgh_last_idx = 0; /* header is record with index 0 */
344 llh->llh_count = 1; /* for the header record */
345 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
346 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
347 LLOG_MIN_CHUNK_SIZE);
348 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
349 llh->llh_hdr.lrh_index = 0;
350 llh->llh_timestamp = ktime_get_real_seconds();
352 memcpy(&llh->llh_tgtuuid, uuid,
353 sizeof(llh->llh_tgtuuid));
354 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
355 /* Since update llog header might also call this function,
356 * let's reset the bitmap to 0 here */
357 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
358 llh->llh_bitmap_offset -
359 sizeof(llh->llh_tail));
360 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
361 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
362 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
367 EXPORT_SYMBOL(llog_read_header);
369 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
370 int flags, struct obd_uuid *uuid)
372 struct llog_log_hdr *llh;
373 enum llog_flag fmt = flags & LLOG_F_EXT_MASK;
375 int chunk_size = handle->lgh_ctxt->loc_chunk_size;
378 LASSERT(handle->lgh_hdr == NULL);
380 LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
381 OBD_ALLOC_LARGE(llh, chunk_size);
385 handle->lgh_hdr = llh;
386 handle->lgh_hdr_size = chunk_size;
387 /* first assign flags to use llog_client_ops */
388 llh->llh_flags = flags;
389 rc = llog_read_header(env, handle, uuid);
391 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
392 flags & LLOG_F_IS_CAT) ||
393 (llh->llh_flags & LLOG_F_IS_CAT &&
394 flags & LLOG_F_IS_PLAIN))) {
395 CERROR("%s: llog type is %s but initializing %s\n",
396 handle->lgh_ctxt->loc_obd->obd_name,
397 llh->llh_flags & LLOG_F_IS_CAT ?
399 flags & LLOG_F_IS_CAT ? "catalog" : "plain");
400 GOTO(out, rc = -EINVAL);
401 } else if (llh->llh_flags &
402 (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
404 * it is possible to open llog without specifying llog
405 * type so it is taken from llh_flags
407 flags = llh->llh_flags;
409 /* for some reason the llh_flags has no type set */
410 CERROR("llog type is not specified!\n");
411 GOTO(out, rc = -EINVAL);
414 !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
415 CERROR("%s: llog uuid mismatch: %s/%s\n",
416 handle->lgh_ctxt->loc_obd->obd_name,
418 (char *)llh->llh_tgtuuid.uuid);
419 GOTO(out, rc = -EEXIST);
422 if (flags & LLOG_F_IS_CAT) {
423 LASSERT(list_empty(&handle->u.chd.chd_head));
424 INIT_LIST_HEAD(&handle->u.chd.chd_head);
425 llh->llh_size = sizeof(struct llog_logid_rec);
426 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
427 } else if (!(flags & LLOG_F_IS_PLAIN)) {
428 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
429 handle->lgh_ctxt->loc_obd->obd_name,
430 flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
433 llh->llh_flags |= fmt;
436 OBD_FREE_LARGE(llh, chunk_size);
437 handle->lgh_hdr = NULL;
441 EXPORT_SYMBOL(llog_init_handle);
443 static int llog_process_thread(void *arg)
445 struct llog_process_info *lpi = arg;
446 struct llog_handle *loghandle = lpi->lpi_loghandle;
447 struct llog_log_hdr *llh = loghandle->lgh_hdr;
448 struct llog_process_cat_data *cd = lpi->lpi_catdata;
449 struct llog_thread_info *lti;
453 int rc = 0, index = 1, last_index;
455 int last_called_index = 0;
456 bool repeated = false;
463 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
465 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
466 /* expect chunk_size to be power of two */
467 LASSERT(is_power_of_2(chunk_size));
469 OBD_ALLOC_LARGE(buf, chunk_size);
471 lpi->lpi_rc = -ENOMEM;
476 last_called_index = cd->lpcd_first_idx;
477 index = cd->lpcd_first_idx + 1;
479 if (cd != NULL && cd->lpcd_last_idx)
480 last_index = cd->lpcd_last_idx;
482 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
485 struct llog_rec_hdr *rec;
486 off_t chunk_offset = 0;
487 unsigned int buf_offset = 0;
492 /* skip records not set in bitmap */
493 while (index <= last_index &&
494 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
497 /* There are no indices prior the last_index */
498 if (index > last_index)
501 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
505 /* get the buf with our target record; avoid old garbage */
506 memset(buf, 0, chunk_size);
507 /* the record index for outdated chunk data */
508 /* it is safe to process buffer until saved lgh_last_idx */
509 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
510 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
511 index, &cur_offset, buf, chunk_size);
513 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
514 " buf_offset %u, rc = %d\n", cur_offset,
515 (__u64)chunk_offset, buf_offset, rc);
516 /* we`ve tried to reread the chunk, but there is no
518 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
524 /* NB: after llog_next_block() call the cur_offset is the
525 * offset of the next block after read one.
526 * The absolute offset of the current chunk is calculated
527 * from cur_offset value and stored in chunk_offset variable.
529 if ((cur_offset & (chunk_size - 1)) != 0) {
530 partial_chunk = true;
531 chunk_offset = cur_offset & ~(chunk_size - 1);
533 partial_chunk = false;
534 chunk_offset = cur_offset - chunk_size;
537 /* NB: when rec->lrh_len is accessed it is already swabbed
538 * since it is used at the "end" of the loop and the rec
539 * swabbing is done at the beginning of the loop. */
540 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
541 (char *)rec < buf + chunk_size;
542 rec = llog_rec_hdr_next(rec)) {
544 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
547 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
548 lustre_swab_llog_rec(rec);
550 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
551 rec->lrh_type, rec->lrh_index);
553 if (index == (synced_idx + 1) &&
554 synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
557 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
558 cfs_fail_val == (unsigned int)
559 (loghandle->lgh_id.lgl_oi.oi.oi_id &
561 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
564 /* the bitmap could be changed during processing
565 * records from the chunk. For wrapped catalog
566 * it means we can read deleted record and try to
567 * process it. Check this case and reread the chunk.
568 * It is safe to process to lh_last_idx, including
569 * lh_last_idx if it was synced. We can not do <=
570 * comparison, cause for wrapped catalog lgh_last_idx
571 * could be less than index. So we detect last index
572 * for processing as index == lh_last_idx+1. But when
573 * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
574 * the first processing index is llh_cat_idx+1.
577 if ((index == lh_last_idx && synced_idx != index) ||
578 (index == (lh_last_idx + 1) &&
579 !(index == (llh->llh_cat_idx + 1) &&
580 (llh->llh_flags & LLOG_F_IS_CAT))) ||
581 (rec->lrh_index == 0 && !repeated)) {
583 /* save offset inside buffer for the re-read */
584 buf_offset = (char *)rec - (char *)buf;
585 cur_offset = chunk_offset;
587 /* We need to be sure lgh_last_idx
588 * record was saved to disk
590 down_read(&loghandle->lgh_last_sem);
591 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
592 up_read(&loghandle->lgh_last_sem);
593 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
600 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
601 CWARN("%s: invalid length %d in llog "DFID
602 "record for index %d/%d\n",
603 loghandle->lgh_ctxt->loc_obd->obd_name,
605 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
606 rec->lrh_index, index);
608 GOTO(out, rc = -EINVAL);
611 if (rec->lrh_index < index) {
612 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
617 if (rec->lrh_index != index) {
618 CERROR("%s: "DFID" Invalid record: index %u"
619 " but expected %u\n",
620 loghandle->lgh_ctxt->loc_obd->obd_name,
621 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
622 rec->lrh_index, index);
623 GOTO(out, rc = -ERANGE);
627 "lrh_index: %d lrh_len: %d (%d remains)\n",
628 rec->lrh_index, rec->lrh_len,
629 (int)(buf + chunk_size - (char *)rec));
631 /* lgh_cur_offset is used only at llog_test_3 */
632 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
635 /* if set, process the callback on this record */
636 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
637 struct llog_cookie *lgc;
641 CDEBUG(D_OTHER, "index: %d, lh_last_idx: %d "
642 "synced_idx: %d lgh_last_idx: %d\n",
643 index, lh_last_idx, synced_idx,
644 loghandle->lgh_last_idx);
647 lgc = <i->lgi_cookie;
648 /* store lu_env for recursive calls */
649 tmp_off = lgc->lgc_offset;
650 tmp_idx = lgc->lgc_index;
652 lgc->lgc_offset = (char *)rec -
653 (char *)buf + chunk_offset;
654 lgc->lgc_index = rec->lrh_index;
656 /* using lu_env for passing record offset to
657 * llog_write through various callbacks */
658 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
660 last_called_index = index;
663 lgc->lgc_offset = tmp_off;
664 lgc->lgc_index = tmp_idx;
667 if (rc == LLOG_PROC_BREAK) {
669 } else if (rc == LLOG_DEL_RECORD) {
670 rc = llog_cancel_rec(lpi->lpi_env,
676 /* some stupid callbacks directly cancel records
677 * and delete llog. Check it and stop
679 if (loghandle->lgh_hdr == NULL ||
680 loghandle->lgh_hdr->llh_count == 1)
683 /* exit if the last index is reached */
684 if (index >= last_index)
692 cd->lpcd_last_idx = last_called_index;
694 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
695 if (dt_object_remote(loghandle->lgh_obj)) {
696 /* If it is remote object, then -EIO might means
697 * disconnection or eviction, let's return -EAGAIN,
698 * so for update recovery log processing, it will
699 * retry until the umount or abort recovery, see
700 * lod_sub_recovery_thread() */
701 CERROR("%s retry remote llog process\n",
702 loghandle->lgh_ctxt->loc_obd->obd_name);
705 /* something bad happened to the processing of a local
706 * llog file, probably I/O error or the log got
707 * corrupted to be able to finally release the log we
708 * discard any remaining bits in the header */
709 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
710 " %s index %d count %d\n",
711 loghandle->lgh_ctxt->loc_obd->obd_name,
712 POSTID(&loghandle->lgh_id.lgl_oi),
713 loghandle->lgh_id.lgl_ogen,
714 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
715 "plain"), index, llh->llh_count);
717 while (index <= last_index) {
718 if (ext2_test_bit(index,
719 LLOG_HDR_BITMAP(llh)) != 0)
720 llog_cancel_rec(lpi->lpi_env, loghandle,
728 OBD_FREE_LARGE(buf, chunk_size);
733 static int llog_process_thread_daemonize(void *arg)
735 struct llog_process_info *lpi = arg;
738 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
740 task_lock(lpi->lpi_reftask);
741 new_ns = lpi->lpi_reftask->nsproxy;
742 if (curr_ns != new_ns) {
745 current->nsproxy = new_ns;
746 /* XXX: we should call put_nsproxy() instead of
747 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
748 * used outside of the kernel itself, because it calls
749 * free_nsproxy() which is not exported by the kernel
750 * (defined in kernel/nsproxy.c) */
751 atomic_dec(&curr_ns->count);
753 task_unlock(lpi->lpi_reftask);
757 /* client env has no keys, tags is just 0 */
758 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
763 rc = llog_process_thread(arg);
767 complete(&lpi->lpi_completion);
771 int llog_process_or_fork(const struct lu_env *env,
772 struct llog_handle *loghandle,
773 llog_cb_t cb, void *data, void *catdata, bool fork)
775 struct llog_process_info *lpi;
782 CERROR("cannot alloc pointer\n");
785 lpi->lpi_loghandle = loghandle;
787 lpi->lpi_cbdata = data;
788 lpi->lpi_catdata = catdata;
791 struct task_struct *task;
793 /* The new thread can't use parent env,
794 * init the new one in llog_process_thread_daemonize. */
796 init_completion(&lpi->lpi_completion);
797 /* take reference to current, so that
798 * llog_process_thread_daemonize() can use it to switch to
799 * namespace associated with current */
800 lpi->lpi_reftask = current;
801 task = kthread_run(llog_process_thread_daemonize, lpi,
802 "llog_process_thread");
805 CERROR("%s: cannot start thread: rc = %d\n",
806 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
809 wait_for_completion(&lpi->lpi_completion);
812 llog_process_thread(lpi);
820 EXPORT_SYMBOL(llog_process_or_fork);
822 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
823 llog_cb_t cb, void *data, void *catdata)
826 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
827 return rc == LLOG_DEL_PLAIN ? 0 : rc;
829 EXPORT_SYMBOL(llog_process);
831 static inline const struct cred *llog_raise_resource(void)
833 struct cred *cred = NULL;
835 if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
838 cred = prepare_creds();
842 cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
843 return override_creds(cred);
846 static inline void llog_restore_resource(const struct cred *old_cred)
849 revert_creds(old_cred);
852 int llog_reverse_process(const struct lu_env *env,
853 struct llog_handle *loghandle, llog_cb_t cb,
854 void *data, void *catdata)
856 struct llog_log_hdr *llh = loghandle->lgh_hdr;
857 struct llog_process_cat_data *cd = catdata;
859 int rc = 0, first_index = 1, index, idx;
860 __u32 chunk_size = llh->llh_hdr.lrh_len;
863 OBD_ALLOC_LARGE(buf, chunk_size);
868 first_index = cd->lpcd_first_idx + 1;
869 if (cd != NULL && cd->lpcd_last_idx)
870 index = cd->lpcd_last_idx;
872 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
875 struct llog_rec_hdr *rec;
876 struct llog_rec_tail *tail;
878 /* skip records not set in bitmap */
879 while (index >= first_index &&
880 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
883 LASSERT(index >= first_index - 1);
884 if (index == first_index - 1)
887 /* get the buf with our target record; avoid old garbage */
888 memset(buf, 0, chunk_size);
889 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
894 idx = rec->lrh_index;
895 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
896 while (idx < index) {
897 rec = (void *)rec + rec->lrh_len;
898 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
899 lustre_swab_llog_rec(rec);
902 LASSERT(idx == index);
903 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
905 /* process records in buffer, starting where we found one */
906 while ((void *)tail > buf) {
907 if (tail->lrt_index == 0)
908 GOTO(out, rc = 0); /* no more records */
910 /* if set, process the callback on this record */
911 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
912 rec = (void *)tail - tail->lrt_len +
915 rc = cb(env, loghandle, rec, data);
916 if (rc == LLOG_PROC_BREAK) {
918 } else if (rc == LLOG_DEL_RECORD) {
919 rc = llog_cancel_rec(env, loghandle,
926 /* previous record, still in buffer? */
928 if (index < first_index)
930 tail = (void *)tail - tail->lrt_len;
936 OBD_FREE_LARGE(buf, chunk_size);
939 EXPORT_SYMBOL(llog_reverse_process);
945 * llog_open - open llog, may not exist
946 * llog_exist - check if llog exists
947 * llog_close - close opened llog, pair for open, frees llog_handle
948 * llog_declare_create - declare llog creation
949 * llog_create - create new llog on disk, need transaction handle
950 * llog_declare_write_rec - declaration of llog write
951 * llog_write_rec - write llog record on disk, need transaction handle
952 * llog_declare_add - declare llog catalog record addition
953 * llog_add - add llog record in catalog, need transaction handle
955 int llog_exist(struct llog_handle *loghandle)
957 struct llog_operations *lop;
962 rc = llog_handle2ops(loghandle, &lop);
965 if (lop->lop_exist == NULL)
968 rc = lop->lop_exist(loghandle);
971 EXPORT_SYMBOL(llog_exist);
973 int llog_declare_create(const struct lu_env *env,
974 struct llog_handle *loghandle, struct thandle *th)
976 const struct cred *old_cred;
977 struct llog_operations *lop;
982 rc = llog_handle2ops(loghandle, &lop);
985 if (lop->lop_declare_create == NULL)
988 old_cred = llog_raise_resource();
989 rc = lop->lop_declare_create(env, loghandle, th);
990 llog_restore_resource(old_cred);
994 int llog_create(const struct lu_env *env, struct llog_handle *handle,
997 const struct cred *old_cred;
998 struct llog_operations *lop;
1003 rc = llog_handle2ops(handle, &lop);
1006 if (lop->lop_create == NULL)
1007 RETURN(-EOPNOTSUPP);
1009 old_cred = llog_raise_resource();
1010 rc = lop->lop_create(env, handle, th);
1011 llog_restore_resource(old_cred);
1015 int llog_declare_write_rec(const struct lu_env *env,
1016 struct llog_handle *handle,
1017 struct llog_rec_hdr *rec, int idx,
1020 const struct cred *old_cred;
1021 struct llog_operations *lop;
1026 rc = llog_handle2ops(handle, &lop);
1030 if (lop->lop_declare_write_rec == NULL)
1031 RETURN(-EOPNOTSUPP);
1033 old_cred = llog_raise_resource();
1034 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1035 llog_restore_resource(old_cred);
1039 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1040 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1041 int idx, struct thandle *th)
1043 const struct cred *old_cred;
1044 struct llog_operations *lop;
1049 /* API sanity checks */
1050 if (handle == NULL) {
1051 CERROR("loghandle is missed\n");
1053 } else if (handle->lgh_obj == NULL) {
1054 CERROR("loghandle %p with NULL object\n",
1057 } else if (th == NULL) {
1058 CERROR("%s: missed transaction handle\n",
1059 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1061 } else if (handle->lgh_hdr == NULL) {
1062 CERROR("%s: loghandle %p with no header\n",
1063 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1068 rc = llog_handle2ops(handle, &lop);
1072 if (lop->lop_write_rec == NULL)
1073 RETURN(-EOPNOTSUPP);
1075 buflen = rec->lrh_len;
1076 LASSERT(cfs_size_round(buflen) == buflen);
1078 old_cred = llog_raise_resource();
1079 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1080 llog_restore_resource(old_cred);
1084 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1085 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1088 const struct cred *old_cred;
1093 if (lgh->lgh_logops->lop_add == NULL)
1094 RETURN(-EOPNOTSUPP);
1096 old_cred = llog_raise_resource();
1097 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1098 llog_restore_resource(old_cred);
1101 EXPORT_SYMBOL(llog_add);
1103 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1104 struct llog_rec_hdr *rec, struct thandle *th)
1106 const struct cred *old_cred;
1111 if (lgh->lgh_logops->lop_declare_add == NULL)
1112 RETURN(-EOPNOTSUPP);
1114 old_cred = llog_raise_resource();
1115 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1116 llog_restore_resource(old_cred);
1119 EXPORT_SYMBOL(llog_declare_add);
1122 * Helper function to open llog or create it if doesn't exist.
1123 * It hides all transaction handling from caller.
1125 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1126 struct llog_handle **res, struct llog_logid *logid,
1129 struct dt_device *d;
1135 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1139 if (llog_exist(*res))
1142 LASSERT((*res)->lgh_obj != NULL);
1144 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1146 if (unlikely(unlikely(d->dd_rdonly)))
1149 th = dt_trans_create(env, d);
1151 GOTO(out, rc = PTR_ERR(th));
1153 /* Create update llog object synchronously, which
1154 * happens during inialization process see
1155 * lod_sub_prep_llog(), to make sure the update
1156 * llog object is created before corss-MDT writing
1157 * updates into the llog object */
1158 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1161 th->th_wait_submit = 1;
1162 rc = llog_declare_create(env, *res, th);
1164 rc = dt_trans_start_local(env, d, th);
1166 rc = llog_create(env, *res, th);
1168 dt_trans_stop(env, d, th);
1171 llog_close(env, *res);
1174 EXPORT_SYMBOL(llog_open_create);
1177 * Helper function to delete existent llog.
1179 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1180 struct llog_logid *logid, char *name)
1182 struct llog_handle *handle;
1187 /* nothing to erase */
1188 if (name == NULL && logid == NULL)
1191 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1195 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1197 rc = llog_destroy(env, handle);
1199 rc2 = llog_close(env, handle);
1204 EXPORT_SYMBOL(llog_erase);
1207 * Helper function for write record in llog.
1208 * It hides all transaction handling from caller.
1209 * Valid only with local llog.
1211 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1212 struct llog_rec_hdr *rec, int idx)
1214 struct dt_device *dt;
1222 LASSERT(loghandle->lgh_ctxt);
1223 LASSERT(loghandle->lgh_obj != NULL);
1225 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1227 if (unlikely(unlikely(dt->dd_rdonly)))
1230 th = dt_trans_create(env, dt);
1232 RETURN(PTR_ERR(th));
1234 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1236 GOTO(out_trans, rc);
1238 th->th_wait_submit = 1;
1239 rc = dt_trans_start_local(env, dt, th);
1241 GOTO(out_trans, rc);
1243 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1245 down_write(&loghandle->lgh_lock);
1247 struct llog_thread_info *lti = llog_info(env);
1249 /* cookie comes from llog_process_thread */
1250 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1251 rec->lrh_index, th);
1252 /* upper layer didn`t pass cookie so change rc */
1253 rc = (rc == 1 ? 0 : rc);
1255 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1258 up_write(&loghandle->lgh_lock);
1260 dt_trans_stop(env, dt, th);
1263 EXPORT_SYMBOL(llog_write);
1265 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1266 struct llog_handle **lgh, struct llog_logid *logid,
1267 char *name, enum llog_open_param open_param)
1269 const struct cred *old_cred;
1275 LASSERT(ctxt->loc_logops);
1277 if (ctxt->loc_logops->lop_open == NULL) {
1279 RETURN(-EOPNOTSUPP);
1282 *lgh = llog_alloc_handle();
1285 (*lgh)->lgh_ctxt = ctxt;
1286 (*lgh)->lgh_logops = ctxt->loc_logops;
1288 old_cred = llog_raise_resource();
1289 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1290 llog_restore_resource(old_cred);
1292 llog_free_handle(*lgh);
1297 EXPORT_SYMBOL(llog_open);
1299 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1301 struct llog_operations *lop;
1306 rc = llog_handle2ops(loghandle, &lop);
1309 if (lop->lop_close == NULL)
1310 GOTO(out, rc = -EOPNOTSUPP);
1311 rc = lop->lop_close(env, loghandle);
1313 llog_handle_put(loghandle);
1316 EXPORT_SYMBOL(llog_close);
1319 * Helper function to get the llog size in records. It is used by MGS
1320 * mostly to check that config llog exists and contains data.
1322 * \param[in] env execution environment
1323 * \param[in] ctxt llog context
1324 * \param[in] name llog name
1326 * \retval true if there are records in llog besides a header
1327 * \retval false on error or llog without records
1329 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1332 struct llog_handle *llh;
1335 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1337 if (likely(rc == -ENOENT))
1342 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1344 GOTO(out_close, rc);
1345 rc = llog_get_size(llh);
1348 llog_close(env, llh);
1350 /* The header is record 1, the llog is still considered as empty
1351 * if there is only header */
1354 EXPORT_SYMBOL(llog_is_empty);
1356 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1357 struct llog_rec_hdr *rec, void *data)
1359 struct llog_handle *copy_llh = data;
1361 /* Append all records */
1362 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1365 /* backup plain llog */
1366 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1367 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1368 char *name, char *backup)
1370 struct llog_handle *llh, *bllh;
1375 /* open original log */
1376 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1378 /* the -ENOENT case is also reported to the caller
1379 * but silently so it should handle that if needed.
1382 CERROR("%s: failed to open log %s: rc = %d\n",
1383 obd->obd_name, name, rc);
1387 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1389 GOTO(out_close, rc);
1391 /* Make sure there's no old backup log */
1392 rc = llog_erase(env, bctxt, NULL, backup);
1393 if (rc < 0 && rc != -ENOENT)
1394 GOTO(out_close, rc);
1396 /* open backup log */
1397 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1399 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1400 obd->obd_name, backup, rc);
1401 GOTO(out_close, rc);
1404 /* check that backup llog is not the same object as original one */
1405 if (llh->lgh_obj == bllh->lgh_obj) {
1406 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1407 obd->obd_name, name, backup, llh->lgh_obj,
1409 GOTO(out_backup, rc = -EEXIST);
1412 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1414 GOTO(out_backup, rc);
1416 /* Copy log record by record */
1417 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1420 CERROR("%s: failed to backup log %s: rc = %d\n",
1421 obd->obd_name, name, rc);
1423 llog_close(env, bllh);
1425 llog_close(env, llh);
1428 EXPORT_SYMBOL(llog_backup);
1430 /* Get size of llog */
1431 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1436 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1438 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1439 llh->lgh_ctxt->loc_obd->obd_name,
1440 PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1446 EXPORT_SYMBOL(llog_size);