1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/recov_thread.c
38 * OST<->MDS recovery logging thread.
39 * Invariants in implementation:
40 * - we do not share logs among different OST<->MDS connections, so that
41 * if an OST or MDS fails it need only look at log(s) relevant to itself
43 * Author: Andreas Dilger <adilger@clusterfs.com>
44 * Yury Umanets <yury.umanets@sun.com>
45 * Alexey Lyashkov <alexey.lyashkov@sun.com>
48 #define DEBUG_SUBSYSTEM S_LOG
51 # define EXPORT_SYMTAB
55 # include <libcfs/libcfs.h>
57 # include <libcfs/list.h>
58 # include <liblustre.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <obd_class.h>
64 #include <lustre_net.h>
65 #include <lnet/types.h>
66 #include <libcfs/list.h>
67 #include <lustre_log.h>
68 #include "ptlrpc_internal.h"
70 static atomic_t llcd_count = ATOMIC_INIT(0);
71 static cfs_mem_cache_t *llcd_cache = NULL;
75 LLOG_LCM_FL_START = 1 << 0,
76 LLOG_LCM_FL_EXIT = 1 << 1
80 * Allocate new llcd from cache, init it and return to caller.
81 * Bumps number of objects allocated.
83 static struct llog_canceld_ctxt *llcd_alloc(void)
85 struct llog_canceld_ctxt *llcd;
89 * Payload of lustre_msg V2 is bigger.
91 llcd_size = CFS_PAGE_SIZE -
92 lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
93 llcd_size += offsetof(struct llog_canceld_ctxt, llcd_cookies);
94 OBD_SLAB_ALLOC(llcd, llcd_cache, CFS_ALLOC_STD, llcd_size);
98 llcd->llcd_size = llcd_size;
99 llcd->llcd_cookiebytes = 0;
100 atomic_inc(&llcd_count);
105 * Returns passed llcd to cache.
107 static void llcd_free(struct llog_canceld_ctxt *llcd)
109 LASSERT(atomic_read(&llcd_count) > 0);
110 OBD_SLAB_FREE(llcd, llcd_cache, llcd->llcd_size);
111 atomic_dec(&llcd_count);
115 * Copy passed @cookies to @llcd.
117 static void llcd_copy(struct llog_canceld_ctxt *llcd,
118 struct llog_cookie *cookies)
120 memcpy((char *)llcd->llcd_cookies + llcd->llcd_cookiebytes,
121 cookies, sizeof(*cookies));
122 llcd->llcd_cookiebytes += sizeof(*cookies);
126 * Checks if passed cookie fits into llcd free space buffer. Returns
127 * 1 if yes and 0 otherwise.
129 static int llcd_fit(struct llog_canceld_ctxt *llcd,
130 struct llog_cookie *cookies)
132 return (llcd->llcd_size -
133 llcd->llcd_cookiebytes) >= sizeof(*cookies);
136 static void llcd_print(struct llog_canceld_ctxt *llcd,
137 const char *func, int line)
139 CDEBUG(D_RPCTRACE, "Llcd (%p) at %s:%d:\n", llcd, func, line);
140 CDEBUG(D_RPCTRACE, " size: %d\n", llcd->llcd_size);
141 CDEBUG(D_RPCTRACE, " ctxt: %p\n", llcd->llcd_ctxt);
142 CDEBUG(D_RPCTRACE, " lcm : %p\n", llcd->llcd_lcm);
143 CDEBUG(D_RPCTRACE, " cookiebytes : %d\n", llcd->llcd_cookiebytes);
147 * Llcd completion function. Called uppon llcd send finish regardless
148 * sending result. Error is passed in @rc. Note, that this will be called
149 * in cleanup time when all inflight rpcs aborted.
152 llcd_interpret(const struct lu_env *env,
153 struct ptlrpc_request *req, void *noused, int rc)
155 struct llog_canceld_ctxt *llcd = req->rq_async_args.pointer_arg[0];
156 CDEBUG(D_RPCTRACE, "Sent llcd %p (%d)\n", llcd, rc);
162 * Send @llcd to remote node. Free llcd uppon completion or error. Sending
163 * is performed in async style so this function will return asap without
166 static int llcd_send(struct llog_canceld_ctxt *llcd)
168 char *bufs[2] = { NULL, (char *)llcd->llcd_cookies };
169 struct obd_import *import = NULL;
170 struct llog_commit_master *lcm;
171 struct ptlrpc_request *req;
172 struct llog_ctxt *ctxt;
176 ctxt = llcd->llcd_ctxt;
178 CERROR("Invalid llcd with NULL ctxt found (%p)\n",
180 llcd_print(llcd, __FUNCTION__, __LINE__);
183 LASSERT_SEM_LOCKED(&ctxt->loc_sem);
185 if (llcd->llcd_cookiebytes == 0)
188 lcm = llcd->llcd_lcm;
191 * Check if we're in exit stage. Do not send llcd in
194 if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
195 GOTO(exit, rc = -ENODEV);
197 CDEBUG(D_RPCTRACE, "Sending llcd %p\n", llcd);
199 import = llcd->llcd_ctxt->loc_imp;
200 if (!import || (import == LP_POISON) ||
201 (import->imp_client == LP_POISON)) {
202 CERROR("Invalid import %p for llcd %p\n",
204 GOTO(exit, rc = -ENODEV);
207 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_RECOV, 10);
210 * No need to get import here as it is already done in
211 * llog_receptor_accept().
213 req = ptlrpc_request_alloc(import, &RQF_LOG_CANCEL);
215 CERROR("Can't allocate request for sending llcd %p\n",
217 GOTO(exit, rc = -ENOMEM);
219 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES,
220 RCL_CLIENT, llcd->llcd_cookiebytes);
222 rc = ptlrpc_request_bufs_pack(req, LUSTRE_LOG_VERSION,
223 OBD_LOG_CANCEL, bufs, NULL);
225 ptlrpc_request_free(req);
229 ptlrpc_at_set_req_timeout(req);
230 ptlrpc_request_set_replen(req);
233 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
234 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
235 req->rq_interpret_reply = (ptlrpc_interpterer_t)llcd_interpret;
236 req->rq_async_args.pointer_arg[0] = llcd;
237 rc = ptlrpc_set_add_new_req(&lcm->lcm_pc, req);
239 ptlrpc_request_free(req);
244 CDEBUG(D_RPCTRACE, "Refused llcd %p\n", llcd);
250 * Attach @llcd to @ctxt. Establish llcd vs. ctxt reserve connection
251 * so hat they can refer each other.
254 llcd_attach(struct llog_ctxt *ctxt, struct llog_canceld_ctxt *llcd)
256 struct llog_commit_master *lcm;
258 LASSERT(ctxt != NULL && llcd != NULL);
259 LASSERT_SEM_LOCKED(&ctxt->loc_sem);
260 LASSERT(ctxt->loc_llcd == NULL);
262 atomic_inc(&lcm->lcm_count);
263 CDEBUG(D_RPCTRACE, "Attach llcd %p to ctxt %p (%d)\n",
264 llcd, ctxt, atomic_read(&lcm->lcm_count));
265 llcd->llcd_ctxt = llog_ctxt_get(ctxt);
266 llcd->llcd_lcm = ctxt->loc_lcm;
267 ctxt->loc_llcd = llcd;
272 * Opposite to llcd_attach(). Detaches llcd from its @ctxt. This makes
273 * sure that this llcd will not be found another time we try to cancel.
275 static struct llog_canceld_ctxt *llcd_detach(struct llog_ctxt *ctxt)
277 struct llog_commit_master *lcm;
278 struct llog_canceld_ctxt *llcd;
280 LASSERT(ctxt != NULL);
281 LASSERT_SEM_LOCKED(&ctxt->loc_sem);
283 llcd = ctxt->loc_llcd;
288 if (atomic_read(&lcm->lcm_count) == 0) {
289 CERROR("Invalid detach occured %p:%p\n", ctxt, llcd);
290 llcd_print(llcd, __FUNCTION__, __LINE__);
293 atomic_dec(&lcm->lcm_count);
294 ctxt->loc_llcd = NULL;
296 CDEBUG(D_RPCTRACE, "Detach llcd %p from ctxt %p (%d)\n",
297 llcd, ctxt, atomic_read(&lcm->lcm_count));
304 * Return @llcd cached in @ctxt. Allocate new one if required. Attach it
305 * to ctxt so that it may be used for gathering cookies and sending.
307 static struct llog_canceld_ctxt *llcd_get(struct llog_ctxt *ctxt)
309 struct llog_canceld_ctxt *llcd;
313 CERROR("Couldn't alloc an llcd for ctxt %p\n", ctxt);
316 llcd_attach(ctxt, llcd);
321 * Deatch llcd from its @ctxt. Free llcd.
323 static void llcd_put(struct llog_ctxt *ctxt)
325 struct llog_commit_master *lcm;
326 struct llog_canceld_ctxt *llcd;
329 llcd = llcd_detach(ctxt);
333 if (atomic_read(&lcm->lcm_count) == 0)
334 cfs_waitq_signal(&lcm->lcm_waitq);
338 * Detach llcd from its @ctxt so that nobody will find it with try to
339 * re-use. Send llcd to remote node.
341 static int llcd_push(struct llog_ctxt *ctxt)
343 struct llog_canceld_ctxt *llcd;
347 * Make sure that this llcd will not be sent again as we detach
350 llcd = llcd_detach(ctxt);
352 CERROR("Invalid detached llcd found %p\n", llcd);
353 llcd_print(llcd, __FUNCTION__, __LINE__);
357 rc = llcd_send(llcd);
359 CERROR("Couldn't send llcd %p (%d)\n", llcd, rc);
364 * Start recovery thread which actually deals llcd sending. This
365 * is all ptlrpc standard thread based so there is not much of work
368 int llog_recov_thread_start(struct llog_commit_master *lcm)
373 rc = ptlrpcd_start(lcm->lcm_name, &lcm->lcm_pc);
375 CERROR("Error %d while starting recovery thread %s\n",
379 lcm->lcm_set = lcm->lcm_pc.pc_set;
382 EXPORT_SYMBOL(llog_recov_thread_start);
385 * Stop recovery thread. Complement to llog_recov_thread_start().
387 void llog_recov_thread_stop(struct llog_commit_master *lcm, int force)
389 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
393 * Let all know that we're stopping. This will also make
394 * llcd_send() refuse any new llcds.
396 set_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags);
399 * Stop processing thread. No new rpcs will be accepted for
400 * for processing now.
402 ptlrpcd_stop(&lcm->lcm_pc, force);
405 * Wait for llcd number == 0. Note, this is infinite wait.
406 * All other parts should make sure that no lost llcd is left.
408 l_wait_event(lcm->lcm_waitq,
409 atomic_read(&lcm->lcm_count) == 0, &lwi);
412 EXPORT_SYMBOL(llog_recov_thread_stop);
415 * Initialize commit master structure and start recovery thread on it.
417 struct llog_commit_master *llog_recov_thread_init(char *name)
419 struct llog_commit_master *lcm;
428 * Try to create threads with unique names.
430 snprintf(lcm->lcm_name, sizeof(lcm->lcm_name),
431 "ll_log_commit_%s", name);
433 strncpy(lcm->lcm_name, name, sizeof(lcm->lcm_name));
434 cfs_waitq_init(&lcm->lcm_waitq);
435 atomic_set(&lcm->lcm_count, 0);
436 rc = llog_recov_thread_start(lcm);
438 CERROR("Can't start commit thread, rc %d\n", rc);
446 EXPORT_SYMBOL(llog_recov_thread_init);
449 * Finalize commit master and its recovery thread.
451 void llog_recov_thread_fini(struct llog_commit_master *lcm, int force)
454 llog_recov_thread_stop(lcm, force);
458 EXPORT_SYMBOL(llog_recov_thread_fini);
460 static int llog_recov_thread_replay(struct llog_ctxt *ctxt,
463 struct obd_device *obd = ctxt->loc_obd;
464 struct llog_process_cat_args *lpca;
468 if (obd->obd_stopping)
472 * This will be balanced in llog_cat_process_thread()
479 lpca->lpca_arg = arg;
482 * This will be balanced in llog_cat_process_thread()
484 lpca->lpca_ctxt = llog_ctxt_get(ctxt);
485 if (!lpca->lpca_ctxt) {
489 rc = cfs_kernel_thread(llog_cat_process_thread, lpca,
490 CLONE_VM | CLONE_FILES);
492 CERROR("Error starting llog_cat_process_thread(): %d\n", rc);
496 CDEBUG(D_HA, "Started llog_cat_process_thread(): %d\n", rc);
503 int llog_obd_repl_connect(struct llog_ctxt *ctxt,
504 struct llog_logid *logid, struct llog_gen *gen,
505 struct obd_uuid *uuid)
511 * Send back cached llcd from llog before recovery if we have any.
512 * This is void is nothing cached is found there.
514 llog_sync(ctxt, NULL);
517 * Start recovery in separate thread.
519 mutex_down(&ctxt->loc_sem);
520 ctxt->loc_gen = *gen;
521 rc = llog_recov_thread_replay(ctxt, ctxt->llog_proc_cb, logid);
522 mutex_up(&ctxt->loc_sem);
526 EXPORT_SYMBOL(llog_obd_repl_connect);
529 * Deleted objects have a commit callback that cancels the MDS
530 * log record for the deletion. The commit callback calls this
533 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
534 struct lov_stripe_md *lsm, int count,
535 struct llog_cookie *cookies, int flags)
537 struct llog_commit_master *lcm;
538 struct llog_canceld_ctxt *llcd;
542 LASSERT(ctxt != NULL);
544 mutex_down(&ctxt->loc_sem);
548 * Let's check if we have all structures alive. We also check for
549 * possible shutdown. Do nothing if we're stopping.
551 if (ctxt->loc_imp == NULL) {
552 CDEBUG(D_RPCTRACE, "No import for ctxt %p\n", ctxt);
553 GOTO(out, rc = -ENODEV);
556 if (ctxt->loc_obd->obd_stopping) {
557 CDEBUG(D_RPCTRACE, "Obd is stopping for ctxt %p\n", ctxt);
558 GOTO(out, rc = -ENODEV);
561 if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
562 CDEBUG(D_RPCTRACE, "Commit thread is stopping for ctxt %p\n",
564 GOTO(out, rc = -ENODEV);
567 llcd = ctxt->loc_llcd;
569 if (count > 0 && cookies != NULL) {
571 * Get new llcd from ctxt if required.
574 llcd = llcd_get(ctxt);
576 GOTO(out, rc = -ENOMEM);
578 * Allocation is successful, let's check for stop
579 * flag again to fall back as soon as possible.
581 if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
582 GOTO(out, rc = -ENODEV);
586 * Llcd does not have enough room for @cookies. Let's push
587 * it out and allocate new one.
589 if (!llcd_fit(llcd, cookies)) {
590 rc = llcd_push(ctxt);
593 llcd = llcd_get(ctxt);
595 GOTO(out, rc = -ENOMEM);
597 * Allocation is successful, let's check for stop
598 * flag again to fall back as soon as possible.
600 if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
601 GOTO(out, rc = -ENODEV);
605 * Copy cookies to @llcd, no matter old or new allocated one.
607 llcd_copy(llcd, cookies);
611 * Let's check if we need to send copied @cookies asap. If yes - do it.
613 if (llcd && (flags & OBD_LLOG_FL_SENDNOW)) {
614 rc = llcd_push(ctxt);
622 mutex_up(&ctxt->loc_sem);
625 EXPORT_SYMBOL(llog_obd_repl_cancel);
627 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
632 mutex_down(&ctxt->loc_sem);
633 if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
634 CDEBUG(D_RPCTRACE, "Reverse import disconnect\n");
636 * Check for llcd which might be left attached to @ctxt.
640 mutex_up(&ctxt->loc_sem);
642 mutex_up(&ctxt->loc_sem);
643 rc = llog_cancel(ctxt, NULL, 0, NULL, OBD_LLOG_FL_SENDNOW);
647 EXPORT_SYMBOL(llog_obd_repl_sync);
649 #else /* !__KERNEL__ */
651 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
652 struct lov_stripe_md *lsm, int count,
653 struct llog_cookie *cookies, int flags)
660 * Module init time fucntion. Initializes slab for llcd objects.
662 int llog_recov_init(void)
666 llcd_size = CFS_PAGE_SIZE -
667 lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
668 llcd_size += offsetof(struct llog_canceld_ctxt, llcd_cookies);
669 llcd_cache = cfs_mem_cache_create("llcd_cache", llcd_size, 0, 0);
671 CERROR("Error allocating llcd cache\n");
678 * Module fini time fucntion. Releases slab for llcd objects.
680 void llog_recov_fini(void)
683 * Kill llcd cache when thread is stopped and we're sure no
688 * In 2.6.22 cfs_mem_cache_destroy() will not return error
689 * for busy resources. Let's check it another way.
691 LASSERTF(atomic_read(&llcd_count) == 0,
692 "Can't destroy llcd cache! Number of "
693 "busy llcds: %d\n", atomic_read(&llcd_count));
694 cfs_mem_cache_destroy(llcd_cache);