1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2003 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * OST<->MDS recovery logging thread.
24 * Invariants in implementation:
25 * - we do not share logs among different OST<->MDS connections, so that
26 * if an OST or MDS fails it need only look at log(s) relevant to itself
29 #define DEBUG_SUBSYSTEM S_LOG
32 # define EXPORT_SYMTAB
38 # include <portals/list.h>
39 # include <liblustre.h>
42 #include <linux/kp30.h>
43 #include <linux/obd_class.h>
44 #include <linux/lustre_commit_confd.h>
45 #include <linux/obd_support.h>
46 #include <linux/obd_class.h>
47 #include <linux/lustre_net.h>
48 #include <portals/types.h>
49 #include <portals/list.h>
50 #include <linux/lustre_log.h>
51 #include "ptlrpc_internal.h"
53 #define LLCD_SIZE 4096
57 static struct llog_commit_master lustre_lcm;
58 static struct llog_commit_master *lcm = &lustre_lcm;
60 /* Allocate new commit structs in case we do not have enough */
61 static int llcd_alloc(void)
63 struct llog_canceld_ctxt *llcd;
64 int offset = offsetof(struct llog_canceld_ctxt, llcd_cookies);
66 OBD_ALLOC(llcd, LLCD_SIZE + offset);
72 spin_lock(&lcm->lcm_llcd_lock);
73 list_add(&llcd->llcd_list, &lcm->lcm_llcd_free);
74 atomic_inc(&lcm->lcm_llcd_numfree);
75 spin_unlock(&lcm->lcm_llcd_lock);
80 /* Get a free cookie struct from the list */
81 struct llog_canceld_ctxt *llcd_grab(void)
83 struct llog_canceld_ctxt *llcd;
85 spin_lock(&lcm->lcm_llcd_lock);
86 if (list_empty(&lcm->lcm_llcd_free)) {
87 spin_unlock(&lcm->lcm_llcd_lock);
88 if (llcd_alloc() < 0) {
89 CERROR("unable to allocate log commit data!\n");
92 spin_lock(&lcm->lcm_llcd_lock);
95 llcd = list_entry(lcm->lcm_llcd_free.next, typeof(*llcd), llcd_list);
96 list_del(&llcd->llcd_list);
97 atomic_dec(&lcm->lcm_llcd_numfree);
98 spin_unlock(&lcm->lcm_llcd_lock);
100 llcd->llcd_tries = 0;
101 llcd->llcd_cookiebytes = 0;
105 EXPORT_SYMBOL(llcd_grab);
107 static void llcd_put(struct llog_canceld_ctxt *llcd)
109 int offset = offsetof(struct llog_canceld_ctxt, llcd_cookies);
111 if (atomic_read(&lcm->lcm_llcd_numfree) >= lcm->lcm_llcd_maxfree) {
112 OBD_FREE(llcd, LLCD_SIZE + offset);
114 spin_lock(&lcm->lcm_llcd_lock);
115 list_add(&llcd->llcd_list, &lcm->lcm_llcd_free);
116 atomic_inc(&lcm->lcm_llcd_numfree);
117 spin_unlock(&lcm->lcm_llcd_lock);
121 /* Send some cookies to the appropriate target */
122 void llcd_send(struct llog_canceld_ctxt *llcd)
124 spin_lock(&llcd->llcd_lcm->lcm_llcd_lock);
125 list_add_tail(&llcd->llcd_list, &llcd->llcd_lcm->lcm_llcd_pending);
126 spin_unlock(&llcd->llcd_lcm->lcm_llcd_lock);
128 wake_up_nr(&llcd->llcd_lcm->lcm_waitq, 1);
130 EXPORT_SYMBOL(llcd_send);
132 /* deleted objects have a commit callback that cancels the MDS
133 * log record for the deletion. The commit callback calls this
136 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
137 struct llog_cookie *cookies, int flags, void *data)
139 struct llog_canceld_ctxt *llcd;
145 if (ctxt->loc_imp == NULL) {
146 CWARN("no import for ctxt %p\n", ctxt);
150 if (count == 0 || cookies == NULL) {
151 down(&ctxt->loc_sem);
152 if (ctxt->loc_llcd == NULL || !(flags & OBD_LLOG_FL_SENDNOW))
155 llcd = ctxt->loc_llcd;
159 down(&ctxt->loc_sem);
160 llcd = ctxt->loc_llcd;
164 CERROR("couldn't get an llcd - dropped "LPX64":%x+%u\n",
165 cookies->lgc_lgl.lgl_oid,
166 cookies->lgc_lgl.lgl_ogen, cookies->lgc_index);
167 GOTO(out, rc = -ENOMEM);
169 llcd->llcd_ctxt = ctxt;
170 ctxt->loc_llcd = llcd;
173 memcpy((char *)llcd->llcd_cookies + llcd->llcd_cookiebytes, cookies,
175 llcd->llcd_cookiebytes += sizeof(*cookies);
178 if ((LLCD_SIZE - llcd->llcd_cookiebytes < sizeof(*cookies) ||
179 flags & OBD_LLOG_FL_SENDNOW)) {
180 CDEBUG(D_HA, "send llcd %p:%p\n", llcd, llcd->llcd_ctxt);
181 ctxt->loc_llcd = NULL;
188 EXPORT_SYMBOL(llog_obd_repl_cancel);
190 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
195 if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
196 down(&ctxt->loc_sem);
197 if (ctxt->loc_llcd != NULL) {
198 CWARN("import will be destroyed, put "
199 "llcd %p:%p\n", ctxt->loc_llcd, ctxt);
200 llcd_put(ctxt->loc_llcd);
201 ctxt->loc_llcd = NULL;
202 ctxt->loc_imp = NULL;
206 rc = llog_cancel(ctxt, 0, NULL, OBD_LLOG_FL_SENDNOW, NULL);
211 EXPORT_SYMBOL(llog_obd_repl_sync);
213 static int log_commit_thread(void *arg)
215 struct llog_commit_master *lcm = arg;
216 struct llog_commit_daemon *lcd;
217 struct llog_canceld_ctxt *llcd, *n;
221 OBD_ALLOC(lcd, sizeof(*lcd));
226 ptlrpc_daemonize(); /* thread never needs to do IO */
228 SIGNAL_MASK_LOCK(current, flags);
229 sigfillset(¤t->blocked);
231 SIGNAL_MASK_UNLOCK(current, flags);
233 spin_lock(&lcm->lcm_thread_lock);
235 THREAD_NAME(current->comm, sizeof(current->comm) - 1,
236 "ll_log_comt_%02d", atomic_read(&lcm->lcm_thread_total));
237 atomic_inc(&lcm->lcm_thread_total);
238 spin_unlock(&lcm->lcm_thread_lock);
241 INIT_LIST_HEAD(&lcd->lcd_lcm_list);
242 INIT_LIST_HEAD(&lcd->lcd_llcd_list);
245 CDEBUG(D_HA, "%s started\n", current->comm);
247 struct ptlrpc_request *request;
248 struct obd_import *import = NULL;
249 struct list_head *sending_list;
252 /* If we do not have enough pages available, allocate some */
253 while (atomic_read(&lcm->lcm_llcd_numfree) <
254 lcm->lcm_llcd_minfree) {
255 if (llcd_alloc() < 0)
259 spin_lock(&lcm->lcm_thread_lock);
260 atomic_inc(&lcm->lcm_thread_numidle);
261 list_move(&lcd->lcd_lcm_list, &lcm->lcm_thread_idle);
262 spin_unlock(&lcm->lcm_thread_lock);
264 wait_event_interruptible(lcm->lcm_waitq,
265 !list_empty(&lcm->lcm_llcd_pending) ||
266 lcm->lcm_flags & LLOG_LCM_FL_EXIT);
268 /* If we are the last available thread, start a new one in case
269 * we get blocked on an RPC (nobody else will start a new one)*/
270 spin_lock(&lcm->lcm_thread_lock);
271 atomic_dec(&lcm->lcm_thread_numidle);
272 list_move(&lcd->lcd_lcm_list, &lcm->lcm_thread_busy);
273 spin_unlock(&lcm->lcm_thread_lock);
275 sending_list = &lcm->lcm_llcd_pending;
277 if (lcm->lcm_flags & LLOG_LCM_FL_EXIT) {
278 lcm->lcm_llcd_maxfree = 0;
279 lcm->lcm_llcd_minfree = 0;
280 lcm->lcm_thread_max = 0;
282 if (list_empty(&lcm->lcm_llcd_pending) ||
283 lcm->lcm_flags & LLOG_LCM_FL_EXIT_FORCE)
287 if (atomic_read(&lcm->lcm_thread_numidle) <= 1 &&
288 atomic_read(&lcm->lcm_thread_total) < lcm->lcm_thread_max) {
289 rc = llog_start_commit_thread();
291 CERROR("error starting thread: rc %d\n", rc);
294 /* Move all of the pending cancels from the same OST off of
295 * the list, so we don't get multiple threads blocked and/or
296 * doing upcalls on the same OST in case of failure. */
297 spin_lock(&lcm->lcm_llcd_lock);
298 if (!list_empty(sending_list)) {
299 list_move_tail(sending_list->next,
300 &lcd->lcd_llcd_list);
301 llcd = list_entry(lcd->lcd_llcd_list.next,
302 typeof(*llcd), llcd_list);
303 LASSERT(llcd->llcd_lcm == lcm);
304 import = llcd->llcd_ctxt->loc_imp;
306 list_for_each_entry_safe(llcd, n, sending_list, llcd_list) {
307 LASSERT(llcd->llcd_lcm == lcm);
308 if (import == llcd->llcd_ctxt->loc_imp)
309 list_move_tail(&llcd->llcd_list,
310 &lcd->lcd_llcd_list);
312 if (sending_list != &lcm->lcm_llcd_resend) {
313 list_for_each_entry_safe(llcd, n, &lcm->lcm_llcd_resend,
315 LASSERT(llcd->llcd_lcm == lcm);
316 if (import == llcd->llcd_ctxt->loc_imp)
317 list_move_tail(&llcd->llcd_list,
318 &lcd->lcd_llcd_list);
321 spin_unlock(&lcm->lcm_llcd_lock);
323 /* We are the only one manipulating our local list - no lock */
324 list_for_each_entry_safe(llcd,n, &lcd->lcd_llcd_list,llcd_list){
325 char *bufs[1] = {(char *)llcd->llcd_cookies};
327 list_del(&llcd->llcd_list);
328 if (llcd->llcd_cookiebytes == 0) {
329 CDEBUG(D_HA, "put empty llcd %p:%p\n",
330 llcd, llcd->llcd_ctxt);
335 down(&llcd->llcd_ctxt->loc_sem);
336 if (llcd->llcd_ctxt->loc_imp == NULL) {
337 up(&llcd->llcd_ctxt->loc_sem);
338 CWARN("import will be destroyed, put "
339 "llcd %p:%p\n", llcd, llcd->llcd_ctxt);
344 request = ptlrpc_prep_req(import, OBD_LOG_CANCEL, 1,
345 &llcd->llcd_cookiebytes,
347 up(&llcd->llcd_ctxt->loc_sem);
349 if (request == NULL) {
351 CERROR("error preparing commit: rc %d\n", rc);
353 spin_lock(&lcm->lcm_llcd_lock);
354 list_splice(&lcd->lcd_llcd_list,
355 &lcm->lcm_llcd_resend);
356 INIT_LIST_HEAD(&lcd->lcd_llcd_list);
357 spin_unlock(&lcm->lcm_llcd_lock);
361 request->rq_replen = lustre_msg_size(0, NULL);
362 down(&llcd->llcd_ctxt->loc_sem);
363 if (llcd->llcd_ctxt->loc_imp == NULL) {
364 up(&llcd->llcd_ctxt->loc_sem);
365 CWARN("import will be destroyed, put "
366 "llcd %p:%p\n", llcd, llcd->llcd_ctxt);
368 ptlrpc_req_finished(request);
371 rc = ptlrpc_queue_wait(request);
372 ptlrpc_req_finished(request);
373 up(&llcd->llcd_ctxt->loc_sem);
375 /* If the RPC failed, we put this and the remaining
376 * messages onto the resend list for another time. */
382 #if 0 /* FIXME just put llcd, not put it on resend list */
383 spin_lock(&lcm->lcm_llcd_lock);
384 list_splice(&lcd->lcd_llcd_list, &lcm->lcm_llcd_resend);
385 if (++llcd->llcd_tries < 5) {
386 CERROR("commit %p failed on attempt %d: rc %d\n",
387 llcd, llcd->llcd_tries, rc);
389 list_add_tail(&llcd->llcd_list,
390 &lcm->lcm_llcd_resend);
391 spin_unlock(&lcm->lcm_llcd_lock);
393 spin_unlock(&lcm->lcm_llcd_lock);
395 CERROR("commit %p:%p drop %d cookies: rc %d\n",
396 llcd, llcd->llcd_ctxt,
397 (int)(llcd->llcd_cookiebytes /
398 sizeof(*llcd->llcd_cookies)), rc);
407 sending_list = &lcm->lcm_llcd_resend;
408 if (!list_empty(sending_list))
413 /* If we are force exiting, just drop all of the cookies. */
414 if (lcm->lcm_flags & LLOG_LCM_FL_EXIT_FORCE) {
415 spin_lock(&lcm->lcm_llcd_lock);
416 list_splice(&lcm->lcm_llcd_pending, &lcd->lcd_llcd_list);
417 list_splice(&lcm->lcm_llcd_resend, &lcd->lcd_llcd_list);
418 list_splice(&lcm->lcm_llcd_free, &lcd->lcd_llcd_list);
419 spin_unlock(&lcm->lcm_llcd_lock);
421 list_for_each_entry_safe(llcd, n, &lcd->lcd_llcd_list,llcd_list)
425 spin_lock(&lcm->lcm_thread_lock);
426 list_del(&lcd->lcd_lcm_list);
427 spin_unlock(&lcm->lcm_thread_lock);
428 OBD_FREE(lcd, sizeof(*lcd));
430 spin_lock(&lcm->lcm_thread_lock);
431 atomic_dec(&lcm->lcm_thread_total);
432 spin_unlock(&lcm->lcm_thread_lock);
433 wake_up(&lcm->lcm_waitq);
435 CDEBUG(D_HA, "%s exiting\n", current->comm);
439 int llog_start_commit_thread(void)
444 if (atomic_read(&lcm->lcm_thread_total) >= lcm->lcm_thread_max)
447 rc = kernel_thread(log_commit_thread, lcm, CLONE_VM | CLONE_FILES);
449 CERROR("error starting thread #%d: %d\n",
450 atomic_read(&lcm->lcm_thread_total), rc);
456 EXPORT_SYMBOL(llog_start_commit_thread);
458 static struct llog_process_args {
459 struct semaphore llpa_sem;
460 struct llog_ctxt *llpa_ctxt;
465 int llog_init_commit_master(void)
467 INIT_LIST_HEAD(&lcm->lcm_thread_busy);
468 INIT_LIST_HEAD(&lcm->lcm_thread_idle);
469 spin_lock_init(&lcm->lcm_thread_lock);
470 atomic_set(&lcm->lcm_thread_numidle, 0);
471 init_waitqueue_head(&lcm->lcm_waitq);
472 INIT_LIST_HEAD(&lcm->lcm_llcd_pending);
473 INIT_LIST_HEAD(&lcm->lcm_llcd_resend);
474 INIT_LIST_HEAD(&lcm->lcm_llcd_free);
475 spin_lock_init(&lcm->lcm_llcd_lock);
476 atomic_set(&lcm->lcm_llcd_numfree, 0);
477 lcm->lcm_llcd_minfree = 0;
478 lcm->lcm_thread_max = 5;
479 /* FIXME initialize semaphore for llog_process_args */
480 sema_init(&llpa.llpa_sem, 1);
484 int llog_cleanup_commit_master(int force)
486 lcm->lcm_flags |= LLOG_LCM_FL_EXIT;
488 lcm->lcm_flags |= LLOG_LCM_FL_EXIT_FORCE;
489 wake_up(&lcm->lcm_waitq);
491 wait_event_interruptible(lcm->lcm_waitq,
492 atomic_read(&lcm->lcm_thread_total) == 0);
496 static int log_process_thread(void *args)
498 struct llog_process_args *data = args;
499 struct llog_ctxt *ctxt = data->llpa_ctxt;
500 void *cb = data->llpa_cb;
501 struct llog_logid logid = *(struct llog_logid *)(data->llpa_arg);
502 struct llog_handle *llh = NULL;
509 ptlrpc_daemonize(); /* thread never needs to do IO */
511 SIGNAL_MASK_LOCK(current, flags);
512 sigfillset(¤t->blocked);
514 SIGNAL_MASK_UNLOCK(current, flags);
517 rc = llog_open(ctxt, &llh, &logid, NULL, 0);
519 CERROR("llog_create failed %d\n", rc);
522 rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
524 CERROR("llog_init_handle failed %d\n", rc);
529 rc = llog_cat_process(llh, (llog_cb_t)cb, NULL);
530 if (rc != LLOG_PROC_BREAK)
531 CERROR("llog_cat_process failed %d\n", rc);
533 CWARN("no callback function for recovery\n");
536 CDEBUG(D_HA, "send llcd %p:%p forcibly after recovery\n",
537 ctxt->loc_llcd, ctxt);
538 llog_sync(ctxt, NULL);
540 rc = llog_cat_put(llh);
542 CERROR("llog_cat_put failed %d\n", rc);
547 static int llog_recovery_generic(struct llog_ctxt *ctxt, void *handle,void *arg)
552 down(&llpa.llpa_sem);
553 llpa.llpa_ctxt = ctxt;
554 llpa.llpa_cb = handle;
557 rc = kernel_thread(log_process_thread, &llpa, CLONE_VM | CLONE_FILES);
559 CERROR("error starting log_process_thread: %d\n", rc);
561 CDEBUG(D_HA, "log_process_thread: %d\n", rc);
568 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
569 struct llog_logid *logid, struct llog_gen *gen,
570 struct obd_uuid *uuid)
572 struct llog_canceld_ctxt *llcd;
576 /* send back llcd before recovery from llog */
577 if (ctxt->loc_llcd != NULL) {
578 CWARN("llcd %p:%p not empty\n", ctxt->loc_llcd, ctxt);
579 llog_sync(ctxt, NULL);
582 down(&ctxt->loc_sem);
583 ctxt->loc_gen = *gen;
586 CERROR("couldn't get an llcd\n");
590 llcd->llcd_ctxt = ctxt;
591 ctxt->loc_llcd = llcd;
594 rc = llog_recovery_generic(ctxt, ctxt->llog_proc_cb, logid);
596 CERROR("error recovery process: %d\n", rc);
600 EXPORT_SYMBOL(llog_repl_connect);
602 #else /* !__KERNEL__ */
604 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
605 struct llog_cookie *cookies, int flags, void *data)