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_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;
422 struct llog_thread_info *lti;
426 int rc = 0, index = 1, last_index;
428 int last_called_index = 0;
429 bool repeated = false;
436 lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
438 cur_offset = chunk_size = llh->llh_hdr.lrh_len;
439 /* expect chunk_size to be power of two */
440 LASSERT(is_power_of_2(chunk_size));
442 OBD_ALLOC_LARGE(buf, chunk_size);
444 lpi->lpi_rc = -ENOMEM;
449 last_called_index = cd->lpcd_first_idx;
450 index = cd->lpcd_first_idx + 1;
452 if (cd != NULL && cd->lpcd_last_idx)
453 last_index = cd->lpcd_last_idx;
455 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
458 struct llog_rec_hdr *rec;
459 off_t chunk_offset = 0;
460 unsigned int buf_offset = 0;
464 /* skip records not set in bitmap */
465 while (index <= last_index &&
466 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
469 /* There are no indices prior the last_index */
470 if (index > last_index)
473 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
477 /* get the buf with our target record; avoid old garbage */
478 memset(buf, 0, chunk_size);
479 /* the record index for outdated chunk data */
480 lh_last_idx = loghandle->lgh_last_idx + 1;
481 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
482 index, &cur_offset, buf, chunk_size);
484 CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
485 " buf_offset %u, rc = %d\n", cur_offset,
486 (__u64)chunk_offset, buf_offset, rc);
487 /* we`ve tried to reread the chunk, but there is no
489 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
495 /* NB: after llog_next_block() call the cur_offset is the
496 * offset of the next block after read one.
497 * The absolute offset of the current chunk is calculated
498 * from cur_offset value and stored in chunk_offset variable.
500 if ((cur_offset & (chunk_size - 1)) != 0) {
501 partial_chunk = true;
502 chunk_offset = cur_offset & ~(chunk_size - 1);
504 partial_chunk = false;
505 chunk_offset = cur_offset - chunk_size;
508 /* NB: when rec->lrh_len is accessed it is already swabbed
509 * since it is used at the "end" of the loop and the rec
510 * swabbing is done at the beginning of the loop. */
511 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
512 (char *)rec < buf + chunk_size;
513 rec = llog_rec_hdr_next(rec)) {
515 CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
518 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
519 lustre_swab_llog_rec(rec);
521 CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
522 rec->lrh_type, rec->lrh_index);
524 /* the bitmap could be changed during processing
525 * records from the chunk. For wrapped catalog
526 * it means we can read deleted record and try to
527 * process it. Check this case and reread the chunk. */
529 /* for partial chunk the end of it is zeroed, check
530 * for index 0 to distinguish it. */
531 if ((partial_chunk && rec->lrh_index == 0) ||
532 (index == lh_last_idx &&
533 lh_last_idx != (loghandle->lgh_last_idx + 1))) {
534 /* concurrent llog_add() might add new records
535 * while llog_processing, check this is not
536 * the case and re-read the current chunk
539 /* lgh_last_idx could be less then index
540 * for catalog, if catalog is wrapped */
541 if ((index > loghandle->lgh_last_idx &&
542 !(loghandle->lgh_hdr->llh_flags &
543 LLOG_F_IS_CAT)) || repeated ||
544 (loghandle->lgh_obj != NULL &&
545 dt_object_remote(loghandle->lgh_obj)))
547 /* <2 records means no more records
548 * if the last record we processed was
549 * the final one, then the underlying
550 * object might have been destroyed yet.
551 * we better don't access that.. */
552 mutex_lock(&loghandle->lgh_hdr_mutex);
553 records = loghandle->lgh_hdr->llh_count;
554 mutex_unlock(&loghandle->lgh_hdr_mutex);
557 CDEBUG(D_OTHER, "Re-read last llog buffer for "
558 "new records, index %u, last %u\n",
559 index, loghandle->lgh_last_idx);
560 /* save offset inside buffer for the re-read */
561 buf_offset = (char *)rec - (char *)buf;
562 cur_offset = chunk_offset;
569 if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
570 CWARN("%s: invalid length %d in llog "DFID
571 "record for index %d/%d\n",
572 loghandle->lgh_ctxt->loc_obd->obd_name,
574 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
575 rec->lrh_index, index);
577 GOTO(out, rc = -EINVAL);
580 if (rec->lrh_index < index) {
581 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
586 if (rec->lrh_index != index) {
587 CERROR("%s: "DFID" Invalid record: index %u"
588 " but expected %u\n",
589 loghandle->lgh_ctxt->loc_obd->obd_name,
590 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
591 rec->lrh_index, index);
592 GOTO(out, rc = -ERANGE);
596 "lrh_index: %d lrh_len: %d (%d remains)\n",
597 rec->lrh_index, rec->lrh_len,
598 (int)(buf + chunk_size - (char *)rec));
600 /* lgh_cur_offset is used only at llog_test_3 */
601 loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
604 /* if set, process the callback on this record */
605 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
606 struct llog_cookie *lgc;
611 lgc = <i->lgi_cookie;
612 /* store lu_env for recursive calls */
613 tmp_off = lgc->lgc_offset;
614 tmp_idx = lgc->lgc_index;
616 lgc->lgc_offset = (char *)rec -
617 (char *)buf + chunk_offset;
618 lgc->lgc_index = rec->lrh_index;
620 /* using lu_env for passing record offset to
621 * llog_write through various callbacks */
622 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
624 last_called_index = index;
627 lgc->lgc_offset = tmp_off;
628 lgc->lgc_index = tmp_idx;
631 if (rc == LLOG_PROC_BREAK) {
633 } else if (rc == LLOG_DEL_RECORD) {
634 rc = llog_cancel_rec(lpi->lpi_env,
640 /* some stupid callbacks directly cancel records
641 * and delete llog. Check it and stop
643 if (loghandle->lgh_hdr == NULL ||
644 loghandle->lgh_hdr->llh_count == 1)
647 /* exit if the last index is reached */
648 if (index >= last_index)
656 cd->lpcd_last_idx = last_called_index;
658 if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
659 if (dt_object_remote(loghandle->lgh_obj)) {
660 /* If it is remote object, then -EIO might means
661 * disconnection or eviction, let's return -EAGAIN,
662 * so for update recovery log processing, it will
663 * retry until the umount or abort recovery, see
664 * lod_sub_recovery_thread() */
665 CERROR("%s retry remote llog process\n",
666 loghandle->lgh_ctxt->loc_obd->obd_name);
669 /* something bad happened to the processing of a local
670 * llog file, probably I/O error or the log got
671 * corrupted to be able to finally release the log we
672 * discard any remaining bits in the header */
673 CERROR("%s: Local llog found corrupted #"DOSTID":%x"
674 " %s index %d count %d\n",
675 loghandle->lgh_ctxt->loc_obd->obd_name,
676 POSTID(&loghandle->lgh_id.lgl_oi),
677 loghandle->lgh_id.lgl_ogen,
678 ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
679 "plain"), index, llh->llh_count);
681 while (index <= last_index) {
682 if (ext2_test_bit(index,
683 LLOG_HDR_BITMAP(llh)) != 0)
684 llog_cancel_rec(lpi->lpi_env, loghandle,
692 OBD_FREE_LARGE(buf, chunk_size);
697 static int llog_process_thread_daemonize(void *arg)
699 struct llog_process_info *lpi = arg;
702 struct nsproxy *new_ns, *curr_ns = current->nsproxy;
704 task_lock(lpi->lpi_reftask);
705 new_ns = lpi->lpi_reftask->nsproxy;
706 if (curr_ns != new_ns) {
709 current->nsproxy = new_ns;
710 /* XXX: we should call put_nsproxy() instead of
711 * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
712 * used outside of the kernel itself, because it calls
713 * free_nsproxy() which is not exported by the kernel
714 * (defined in kernel/nsproxy.c) */
715 atomic_dec(&curr_ns->count);
717 task_unlock(lpi->lpi_reftask);
721 /* client env has no keys, tags is just 0 */
722 rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
727 rc = llog_process_thread(arg);
731 complete(&lpi->lpi_completion);
735 int llog_process_or_fork(const struct lu_env *env,
736 struct llog_handle *loghandle,
737 llog_cb_t cb, void *data, void *catdata, bool fork)
739 struct llog_process_info *lpi;
746 CERROR("cannot alloc pointer\n");
749 lpi->lpi_loghandle = loghandle;
751 lpi->lpi_cbdata = data;
752 lpi->lpi_catdata = catdata;
755 struct task_struct *task;
757 /* The new thread can't use parent env,
758 * init the new one in llog_process_thread_daemonize. */
760 init_completion(&lpi->lpi_completion);
761 /* take reference to current, so that
762 * llog_process_thread_daemonize() can use it to switch to
763 * namespace associated with current */
764 lpi->lpi_reftask = current;
765 task = kthread_run(llog_process_thread_daemonize, lpi,
766 "llog_process_thread");
769 CERROR("%s: cannot start thread: rc = %d\n",
770 loghandle->lgh_ctxt->loc_obd->obd_name, rc);
773 wait_for_completion(&lpi->lpi_completion);
776 llog_process_thread(lpi);
784 EXPORT_SYMBOL(llog_process_or_fork);
786 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
787 llog_cb_t cb, void *data, void *catdata)
790 rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
791 return rc == LLOG_DEL_PLAIN ? 0 : rc;
793 EXPORT_SYMBOL(llog_process);
795 int llog_reverse_process(const struct lu_env *env,
796 struct llog_handle *loghandle, llog_cb_t cb,
797 void *data, void *catdata)
799 struct llog_log_hdr *llh = loghandle->lgh_hdr;
800 struct llog_process_cat_data *cd = catdata;
802 int rc = 0, first_index = 1, index, idx;
803 __u32 chunk_size = llh->llh_hdr.lrh_len;
806 OBD_ALLOC_LARGE(buf, chunk_size);
811 first_index = cd->lpcd_first_idx + 1;
812 if (cd != NULL && cd->lpcd_last_idx)
813 index = cd->lpcd_last_idx;
815 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
818 struct llog_rec_hdr *rec;
819 struct llog_rec_tail *tail;
821 /* skip records not set in bitmap */
822 while (index >= first_index &&
823 !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
826 LASSERT(index >= first_index - 1);
827 if (index == first_index - 1)
830 /* get the buf with our target record; avoid old garbage */
831 memset(buf, 0, chunk_size);
832 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
837 idx = rec->lrh_index;
838 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
839 while (idx < index) {
840 rec = (void *)rec + rec->lrh_len;
841 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
842 lustre_swab_llog_rec(rec);
845 LASSERT(idx == index);
846 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
848 /* process records in buffer, starting where we found one */
849 while ((void *)tail > buf) {
850 if (tail->lrt_index == 0)
851 GOTO(out, rc = 0); /* no more records */
853 /* if set, process the callback on this record */
854 if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
855 rec = (void *)tail - tail->lrt_len +
858 rc = cb(env, loghandle, rec, data);
859 if (rc == LLOG_PROC_BREAK) {
861 } else if (rc == LLOG_DEL_RECORD) {
862 rc = llog_cancel_rec(env, loghandle,
869 /* previous record, still in buffer? */
871 if (index < first_index)
873 tail = (void *)tail - tail->lrt_len;
879 OBD_FREE_LARGE(buf, chunk_size);
882 EXPORT_SYMBOL(llog_reverse_process);
888 * llog_open - open llog, may not exist
889 * llog_exist - check if llog exists
890 * llog_close - close opened llog, pair for open, frees llog_handle
891 * llog_declare_create - declare llog creation
892 * llog_create - create new llog on disk, need transaction handle
893 * llog_declare_write_rec - declaration of llog write
894 * llog_write_rec - write llog record on disk, need transaction handle
895 * llog_declare_add - declare llog catalog record addition
896 * llog_add - add llog record in catalog, need transaction handle
898 int llog_exist(struct llog_handle *loghandle)
900 struct llog_operations *lop;
905 rc = llog_handle2ops(loghandle, &lop);
908 if (lop->lop_exist == NULL)
911 rc = lop->lop_exist(loghandle);
914 EXPORT_SYMBOL(llog_exist);
916 int llog_declare_create(const struct lu_env *env,
917 struct llog_handle *loghandle, struct thandle *th)
919 struct llog_operations *lop;
924 rc = llog_handle2ops(loghandle, &lop);
927 if (lop->lop_declare_create == NULL)
930 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
932 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
933 rc = lop->lop_declare_create(env, loghandle, th);
935 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
939 int llog_create(const struct lu_env *env, struct llog_handle *handle,
942 struct llog_operations *lop;
947 rc = llog_handle2ops(handle, &lop);
950 if (lop->lop_create == NULL)
953 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
955 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
956 rc = lop->lop_create(env, handle, th);
958 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
962 int llog_declare_write_rec(const struct lu_env *env,
963 struct llog_handle *handle,
964 struct llog_rec_hdr *rec, int idx,
967 struct llog_operations *lop;
972 rc = llog_handle2ops(handle, &lop);
976 if (lop->lop_declare_write_rec == NULL)
979 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
981 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
982 rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
984 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
988 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
989 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
990 int idx, struct thandle *th)
992 struct llog_operations *lop;
993 int raised, rc, buflen;
997 /* API sanity checks */
998 if (handle == NULL) {
999 CERROR("loghandle is missed\n");
1001 } else if (handle->lgh_obj == NULL) {
1002 CERROR("loghandle %p with NULL object\n",
1005 } else if (th == NULL) {
1006 CERROR("%s: missed transaction handle\n",
1007 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1009 } else if (handle->lgh_hdr == NULL) {
1010 CERROR("%s: loghandle %p with no header\n",
1011 handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1016 rc = llog_handle2ops(handle, &lop);
1020 if (lop->lop_write_rec == NULL)
1021 RETURN(-EOPNOTSUPP);
1023 buflen = rec->lrh_len;
1024 LASSERT(cfs_size_round(buflen) == buflen);
1026 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1028 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1029 rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1031 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1035 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1036 struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1043 if (lgh->lgh_logops->lop_add == NULL)
1044 RETURN(-EOPNOTSUPP);
1046 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1048 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1049 rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1051 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1054 EXPORT_SYMBOL(llog_add);
1056 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1057 struct llog_rec_hdr *rec, struct thandle *th)
1063 if (lgh->lgh_logops->lop_declare_add == NULL)
1064 RETURN(-EOPNOTSUPP);
1066 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1068 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1069 rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1071 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1074 EXPORT_SYMBOL(llog_declare_add);
1077 * Helper function to open llog or create it if doesn't exist.
1078 * It hides all transaction handling from caller.
1080 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1081 struct llog_handle **res, struct llog_logid *logid,
1084 struct dt_device *d;
1090 rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1094 if (llog_exist(*res))
1097 LASSERT((*res)->lgh_obj != NULL);
1099 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1101 th = dt_trans_create(env, d);
1103 GOTO(out, rc = PTR_ERR(th));
1105 /* Create update llog object synchronously, which
1106 * happens during inialization process see
1107 * lod_sub_prep_llog(), to make sure the update
1108 * llog object is created before corss-MDT writing
1109 * updates into the llog object */
1110 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1113 th->th_wait_submit = 1;
1114 rc = llog_declare_create(env, *res, th);
1116 rc = dt_trans_start_local(env, d, th);
1118 rc = llog_create(env, *res, th);
1120 dt_trans_stop(env, d, th);
1123 llog_close(env, *res);
1126 EXPORT_SYMBOL(llog_open_create);
1129 * Helper function to delete existent llog.
1131 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1132 struct llog_logid *logid, char *name)
1134 struct llog_handle *handle;
1139 /* nothing to erase */
1140 if (name == NULL && logid == NULL)
1143 rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1147 rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1149 rc = llog_destroy(env, handle);
1151 rc2 = llog_close(env, handle);
1156 EXPORT_SYMBOL(llog_erase);
1159 * Helper function for write record in llog.
1160 * It hides all transaction handling from caller.
1161 * Valid only with local llog.
1163 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1164 struct llog_rec_hdr *rec, int idx)
1166 struct dt_device *dt;
1174 LASSERT(loghandle->lgh_ctxt);
1175 LASSERT(loghandle->lgh_obj != NULL);
1177 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1179 th = dt_trans_create(env, dt);
1181 RETURN(PTR_ERR(th));
1183 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1185 GOTO(out_trans, rc);
1187 th->th_wait_submit = 1;
1188 rc = dt_trans_start_local(env, dt, th);
1190 GOTO(out_trans, rc);
1192 need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1194 down_write(&loghandle->lgh_lock);
1196 struct llog_thread_info *lti = llog_info(env);
1198 /* cookie comes from llog_process_thread */
1199 rc = llog_write_rec(env, loghandle, rec, <i->lgi_cookie,
1200 rec->lrh_index, th);
1201 /* upper layer didn`t pass cookie so change rc */
1202 rc = (rc == 1 ? 0 : rc);
1204 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1207 up_write(&loghandle->lgh_lock);
1209 dt_trans_stop(env, dt, th);
1212 EXPORT_SYMBOL(llog_write);
1214 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1215 struct llog_handle **lgh, struct llog_logid *logid,
1216 char *name, enum llog_open_param open_param)
1224 LASSERT(ctxt->loc_logops);
1226 if (ctxt->loc_logops->lop_open == NULL) {
1228 RETURN(-EOPNOTSUPP);
1231 *lgh = llog_alloc_handle();
1234 (*lgh)->lgh_ctxt = ctxt;
1235 (*lgh)->lgh_logops = ctxt->loc_logops;
1237 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1239 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1240 rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1242 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1244 llog_free_handle(*lgh);
1249 EXPORT_SYMBOL(llog_open);
1251 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1253 struct llog_operations *lop;
1258 rc = llog_handle2ops(loghandle, &lop);
1261 if (lop->lop_close == NULL)
1262 GOTO(out, rc = -EOPNOTSUPP);
1263 rc = lop->lop_close(env, loghandle);
1265 llog_handle_put(loghandle);
1268 EXPORT_SYMBOL(llog_close);
1271 * Helper function to get the llog size in records. It is used by MGS
1272 * mostly to check that config llog exists and contains data.
1274 * \param[in] env execution environment
1275 * \param[in] ctxt llog context
1276 * \param[in] name llog name
1278 * \retval true if there are records in llog besides a header
1279 * \retval false on error or llog without records
1281 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1284 struct llog_handle *llh;
1287 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1289 if (likely(rc == -ENOENT))
1294 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1296 GOTO(out_close, rc);
1297 rc = llog_get_size(llh);
1300 llog_close(env, llh);
1302 /* The header is record 1, the llog is still considered as empty
1303 * if there is only header */
1306 EXPORT_SYMBOL(llog_is_empty);
1308 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1309 struct llog_rec_hdr *rec, void *data)
1311 struct llog_handle *copy_llh = data;
1313 /* Append all records */
1314 return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1317 /* backup plain llog */
1318 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1319 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1320 char *name, char *backup)
1322 struct llog_handle *llh, *bllh;
1327 /* open original log */
1328 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1330 /* the -ENOENT case is also reported to the caller
1331 * but silently so it should handle that if needed.
1334 CERROR("%s: failed to open log %s: rc = %d\n",
1335 obd->obd_name, name, rc);
1339 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1341 GOTO(out_close, rc);
1343 /* Make sure there's no old backup log */
1344 rc = llog_erase(env, bctxt, NULL, backup);
1345 if (rc < 0 && rc != -ENOENT)
1346 GOTO(out_close, rc);
1348 /* open backup log */
1349 rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1351 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1352 obd->obd_name, backup, rc);
1353 GOTO(out_close, rc);
1356 /* check that backup llog is not the same object as original one */
1357 if (llh->lgh_obj == bllh->lgh_obj) {
1358 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1359 obd->obd_name, name, backup, llh->lgh_obj,
1361 GOTO(out_backup, rc = -EEXIST);
1364 rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1366 GOTO(out_backup, rc);
1368 /* Copy log record by record */
1369 rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1372 CERROR("%s: failed to backup log %s: rc = %d\n",
1373 obd->obd_name, name, rc);
1375 llog_close(env, bllh);
1377 llog_close(env, llh);
1380 EXPORT_SYMBOL(llog_backup);
1382 /* Get size of llog */
1383 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1388 rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1390 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1391 llh->lgh_ctxt->loc_obd->obd_name,
1392 PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1398 EXPORT_SYMBOL(llog_size);