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 the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
25 * OST<->MDS recovery logging thread.
27 * Invariants in implementation:
28 * - we do not share logs among different OST<->MDS connections, so that
29 * if an OST or MDS fails it need only look at log(s) relevant to itself
32 #define DEBUG_SUBSYSTEM S_LOG
35 # define EXPORT_SYMTAB
39 # include <libcfs/libcfs.h>
41 # include <libcfs/list.h>
42 # include <liblustre.h>
45 #include <libcfs/kp30.h>
46 #include <obd_class.h>
47 #include <lustre_commit_confd.h>
48 #include <obd_support.h>
49 #include <obd_class.h>
50 #include <lustre_net.h>
51 #include <lnet/types.h>
52 #include <libcfs/list.h>
53 #include <lustre_log.h>
54 #include "ptlrpc_internal.h"
58 /* Allocate new commit structs in case we do not have enough.
59 * Make the llcd size small enough that it fits into a single page when we
60 * are sending/receiving it. */
61 static int llcd_alloc(struct llog_commit_master *lcm)
63 struct llog_canceld_ctxt *llcd;
66 /* payload of lustre_msg V2 is bigger */
67 llcd_size = 4096 - lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
69 llcd_size + offsetof(struct llog_canceld_ctxt, llcd_cookies));
73 llcd->llcd_size = llcd_size;
76 spin_lock(&lcm->lcm_llcd_lock);
77 list_add(&llcd->llcd_list, &lcm->lcm_llcd_free);
78 atomic_inc(&lcm->lcm_llcd_numfree);
79 spin_unlock(&lcm->lcm_llcd_lock);
84 /* Get a free cookie struct from the list */
85 static struct llog_canceld_ctxt *llcd_grab(struct llog_commit_master *lcm)
87 struct llog_canceld_ctxt *llcd;
90 spin_lock(&lcm->lcm_llcd_lock);
91 if (list_empty(&lcm->lcm_llcd_free)) {
92 spin_unlock(&lcm->lcm_llcd_lock);
93 if (llcd_alloc(lcm) < 0) {
94 CERROR("unable to allocate log commit data!\n");
97 /* check new llcd wasn't grabbed while lock dropped, b=7407 */
101 llcd = list_entry(lcm->lcm_llcd_free.next, typeof(*llcd), llcd_list);
102 list_del(&llcd->llcd_list);
103 atomic_dec(&lcm->lcm_llcd_numfree);
104 spin_unlock(&lcm->lcm_llcd_lock);
106 llcd->llcd_cookiebytes = 0;
111 static void llcd_put(struct llog_canceld_ctxt *llcd)
113 struct llog_commit_master *lcm = llcd->llcd_lcm;
115 llog_ctxt_put(llcd->llcd_ctxt);
116 if (atomic_read(&lcm->lcm_llcd_numfree) >= lcm->lcm_llcd_maxfree) {
117 int llcd_size = llcd->llcd_size +
118 offsetof(struct llog_canceld_ctxt, llcd_cookies);
119 OBD_FREE(llcd, llcd_size);
121 spin_lock(&lcm->lcm_llcd_lock);
122 list_add(&llcd->llcd_list, &lcm->lcm_llcd_free);
123 atomic_inc(&lcm->lcm_llcd_numfree);
124 spin_unlock(&lcm->lcm_llcd_lock);
128 /* Send some cookies to the appropriate target */
129 static void llcd_send(struct llog_canceld_ctxt *llcd)
131 if (!(llcd->llcd_lcm->lcm_flags & LLOG_LCM_FL_EXIT)) {
132 spin_lock(&llcd->llcd_lcm->lcm_llcd_lock);
133 list_add_tail(&llcd->llcd_list,
134 &llcd->llcd_lcm->lcm_llcd_pending);
135 spin_unlock(&llcd->llcd_lcm->lcm_llcd_lock);
137 cfs_waitq_signal_nr(&llcd->llcd_lcm->lcm_waitq, 1);
140 /* deleted objects have a commit callback that cancels the MDS
141 * log record for the deletion. The commit callback calls this
144 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
145 struct lov_stripe_md *lsm, int count,
146 struct llog_cookie *cookies, int flags)
148 struct llog_canceld_ctxt *llcd;
154 mutex_down(&ctxt->loc_sem);
155 if (ctxt->loc_imp == NULL) {
156 CDEBUG(D_RPCTRACE, "no import for ctxt %p\n", ctxt);
160 llcd = ctxt->loc_llcd;
162 if (count > 0 && cookies != NULL) {
164 llcd = llcd_grab(ctxt->loc_lcm);
166 CERROR("couldn't get an llcd - dropped "LPX64
168 cookies->lgc_lgl.lgl_oid,
169 cookies->lgc_lgl.lgl_ogen,
171 GOTO(out, rc = -ENOMEM);
173 llcd->llcd_ctxt = llog_ctxt_get(ctxt);
174 ctxt->loc_llcd = llcd;
177 memcpy((char *)llcd->llcd_cookies + llcd->llcd_cookiebytes,
178 cookies, sizeof(*cookies));
179 llcd->llcd_cookiebytes += sizeof(*cookies);
181 if (llcd == NULL || !(flags & OBD_LLOG_FL_SENDNOW))
185 if ((llcd->llcd_size - llcd->llcd_cookiebytes) < sizeof(*cookies) ||
186 (flags & OBD_LLOG_FL_SENDNOW)) {
187 CDEBUG(D_RPCTRACE, "send llcd %p:%p\n", llcd, llcd->llcd_ctxt);
188 ctxt->loc_llcd = NULL;
192 mutex_up(&ctxt->loc_sem);
195 EXPORT_SYMBOL(llog_obd_repl_cancel);
197 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
202 if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
203 CDEBUG(D_RPCTRACE,"reverse import disconnect, put llcd %p:%p\n",
204 ctxt->loc_llcd, ctxt);
205 mutex_down(&ctxt->loc_sem);
206 if (ctxt->loc_llcd != NULL) {
207 llcd_put(ctxt->loc_llcd);
208 ctxt->loc_llcd = NULL;
210 ctxt->loc_imp = NULL;
211 mutex_up(&ctxt->loc_sem);
213 rc = llog_cancel(ctxt, NULL, 0, NULL, OBD_LLOG_FL_SENDNOW);
218 EXPORT_SYMBOL(llog_obd_repl_sync);
220 static int log_commit_thread(void *arg)
222 struct llog_commit_master *lcm = arg;
223 struct llog_commit_daemon *lcd;
224 struct llog_canceld_ctxt *llcd, *n;
225 struct obd_import *import = NULL;
228 OBD_ALLOC(lcd, sizeof(*lcd));
232 spin_lock(&lcm->lcm_thread_lock);
233 THREAD_NAME(cfs_curproc_comm(), CFS_CURPROC_COMM_MAX - 1,
234 "ll_log_comt_%02d", atomic_read(&lcm->lcm_thread_total));
235 atomic_inc(&lcm->lcm_thread_total);
236 spin_unlock(&lcm->lcm_thread_lock);
238 ptlrpc_daemonize(cfs_curproc_comm()); /* thread never needs to do IO */
240 CFS_INIT_LIST_HEAD(&lcd->lcd_lcm_list);
241 CFS_INIT_LIST_HEAD(&lcd->lcd_llcd_list);
244 CDEBUG(D_HA, "%s started\n", cfs_curproc_comm());
246 struct ptlrpc_request *request;
247 struct list_head *sending_list;
251 class_import_put(import);
254 /* If we do not have enough pages available, allocate some */
255 while (atomic_read(&lcm->lcm_llcd_numfree) <
256 lcm->lcm_llcd_minfree) {
257 if (llcd_alloc(lcm) < 0)
261 spin_lock(&lcm->lcm_thread_lock);
262 atomic_inc(&lcm->lcm_thread_numidle);
263 list_move(&lcd->lcd_lcm_list, &lcm->lcm_thread_idle);
264 spin_unlock(&lcm->lcm_thread_lock);
266 wait_event_interruptible(lcm->lcm_waitq,
267 !list_empty(&lcm->lcm_llcd_pending) ||
268 lcm->lcm_flags & LLOG_LCM_FL_EXIT);
270 /* If we are the last available thread, start a new one in case
271 * we get blocked on an RPC (nobody else will start a new one)*/
272 spin_lock(&lcm->lcm_thread_lock);
273 atomic_dec(&lcm->lcm_thread_numidle);
274 list_move(&lcd->lcd_lcm_list, &lcm->lcm_thread_busy);
275 spin_unlock(&lcm->lcm_thread_lock);
277 sending_list = &lcm->lcm_llcd_pending;
280 class_import_put(import);
282 if (lcm->lcm_flags & LLOG_LCM_FL_EXIT) {
283 lcm->lcm_llcd_maxfree = 0;
284 lcm->lcm_llcd_minfree = 0;
285 lcm->lcm_thread_max = 0;
287 if (list_empty(&lcm->lcm_llcd_pending) ||
288 lcm->lcm_flags & LLOG_LCM_FL_EXIT_FORCE)
292 if (atomic_read(&lcm->lcm_thread_numidle) <= 1 &&
293 atomic_read(&lcm->lcm_thread_total) < lcm->lcm_thread_max) {
294 rc = llog_start_commit_thread(lcm);
296 CERROR("error starting thread: rc %d\n", rc);
299 /* Move all of the pending cancels from the same OST off of
300 * the list, so we don't get multiple threads blocked and/or
301 * doing upcalls on the same OST in case of failure. */
302 spin_lock(&lcm->lcm_llcd_lock);
303 if (!list_empty(sending_list)) {
304 list_move_tail(sending_list->next,
305 &lcd->lcd_llcd_list);
306 llcd = list_entry(lcd->lcd_llcd_list.next,
307 typeof(*llcd), llcd_list);
308 LASSERT(llcd->llcd_lcm == lcm);
309 import = llcd->llcd_ctxt->loc_imp;
311 class_import_get(import);
313 list_for_each_entry_safe(llcd, n, sending_list, llcd_list) {
314 LASSERT(llcd->llcd_lcm == lcm);
315 if (import == llcd->llcd_ctxt->loc_imp)
316 list_move_tail(&llcd->llcd_list,
317 &lcd->lcd_llcd_list);
319 if (sending_list != &lcm->lcm_llcd_resend) {
320 list_for_each_entry_safe(llcd, n, &lcm->lcm_llcd_resend,
322 LASSERT(llcd->llcd_lcm == lcm);
323 if (import == llcd->llcd_ctxt->loc_imp)
324 list_move_tail(&llcd->llcd_list,
325 &lcd->lcd_llcd_list);
328 spin_unlock(&lcm->lcm_llcd_lock);
330 /* We are the only one manipulating our local list - no lock */
331 list_for_each_entry_safe(llcd,n, &lcd->lcd_llcd_list,llcd_list){
332 int size[2] = { sizeof(struct ptlrpc_body),
333 llcd->llcd_cookiebytes };
334 char *bufs[2] = { NULL, (char *)llcd->llcd_cookies };
336 list_del(&llcd->llcd_list);
337 if (llcd->llcd_cookiebytes == 0) {
338 CDEBUG(D_RPCTRACE, "put empty llcd %p:%p\n",
339 llcd, llcd->llcd_ctxt);
344 mutex_down(&llcd->llcd_ctxt->loc_sem);
345 if (llcd->llcd_ctxt->loc_imp == NULL) {
346 mutex_up(&llcd->llcd_ctxt->loc_sem);
347 CWARN("import will be destroyed, put "
348 "llcd %p:%p\n", llcd, llcd->llcd_ctxt);
352 mutex_up(&llcd->llcd_ctxt->loc_sem);
354 if (!import || (import == LP_POISON) ||
355 (import->imp_client == LP_POISON)) {
356 CERROR("No import %p (llcd=%p, ctxt=%p)\n",
357 import, llcd, llcd->llcd_ctxt);
362 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_RECOV, 10);
364 request = ptlrpc_prep_req(import, LUSTRE_LOG_VERSION,
365 OBD_LOG_CANCEL, 2, size,bufs);
366 if (request == NULL) {
368 CERROR("error preparing commit: rc %d\n", rc);
370 spin_lock(&lcm->lcm_llcd_lock);
371 list_splice(&lcd->lcd_llcd_list,
372 &lcm->lcm_llcd_resend);
373 CFS_INIT_LIST_HEAD(&lcd->lcd_llcd_list);
374 spin_unlock(&lcm->lcm_llcd_lock);
378 /* XXX FIXME bug 249, 5515 */
379 request->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
380 request->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
382 ptlrpc_req_set_repsize(request, 1, NULL);
383 mutex_down(&llcd->llcd_ctxt->loc_sem);
384 if (llcd->llcd_ctxt->loc_imp == NULL) {
385 mutex_up(&llcd->llcd_ctxt->loc_sem);
386 CWARN("import will be destroyed, put "
387 "llcd %p:%p\n", llcd, llcd->llcd_ctxt);
389 ptlrpc_req_finished(request);
392 mutex_up(&llcd->llcd_ctxt->loc_sem);
393 rc = ptlrpc_queue_wait(request);
394 ptlrpc_req_finished(request);
396 /* If the RPC failed, we put this and the remaining
397 * messages onto the resend list for another time. */
403 CERROR("commit %p:%p drop %d cookies: rc %d\n",
404 llcd, llcd->llcd_ctxt,
405 (int)(llcd->llcd_cookiebytes /
406 sizeof(*llcd->llcd_cookies)), rc);
411 sending_list = &lcm->lcm_llcd_resend;
412 if (!list_empty(sending_list))
418 class_import_put(import);
420 /* If we are force exiting, just drop all of the cookies. */
421 if (lcm->lcm_flags & LLOG_LCM_FL_EXIT_FORCE) {
422 spin_lock(&lcm->lcm_llcd_lock);
423 list_splice(&lcm->lcm_llcd_pending, &lcd->lcd_llcd_list);
424 list_splice(&lcm->lcm_llcd_resend, &lcd->lcd_llcd_list);
425 list_splice(&lcm->lcm_llcd_free, &lcd->lcd_llcd_list);
426 spin_unlock(&lcm->lcm_llcd_lock);
428 list_for_each_entry_safe(llcd, n, &lcd->lcd_llcd_list,llcd_list)
432 spin_lock(&lcm->lcm_thread_lock);
433 list_del(&lcd->lcd_lcm_list);
434 spin_unlock(&lcm->lcm_thread_lock);
435 OBD_FREE(lcd, sizeof(*lcd));
437 CDEBUG(D_HA, "%s exiting\n", cfs_curproc_comm());
439 spin_lock(&lcm->lcm_thread_lock);
440 atomic_dec(&lcm->lcm_thread_total);
441 spin_unlock(&lcm->lcm_thread_lock);
442 cfs_waitq_signal(&lcm->lcm_waitq);
447 int llog_start_commit_thread(struct llog_commit_master *lcm)
452 if (atomic_read(&lcm->lcm_thread_total) >= lcm->lcm_thread_max)
455 rc = cfs_kernel_thread(log_commit_thread, lcm, CLONE_VM | CLONE_FILES);
457 CERROR("error starting thread #%d: %d\n",
458 atomic_read(&lcm->lcm_thread_total), rc);
464 EXPORT_SYMBOL(llog_start_commit_thread);
466 static struct llog_process_args {
467 struct semaphore llpa_sem;
468 struct llog_ctxt *llpa_ctxt;
473 int llog_init_commit_master(struct llog_commit_master *lcm)
475 CFS_INIT_LIST_HEAD(&lcm->lcm_thread_busy);
476 CFS_INIT_LIST_HEAD(&lcm->lcm_thread_idle);
477 spin_lock_init(&lcm->lcm_thread_lock);
478 atomic_set(&lcm->lcm_thread_numidle, 0);
479 cfs_waitq_init(&lcm->lcm_waitq);
480 CFS_INIT_LIST_HEAD(&lcm->lcm_llcd_pending);
481 CFS_INIT_LIST_HEAD(&lcm->lcm_llcd_resend);
482 CFS_INIT_LIST_HEAD(&lcm->lcm_llcd_free);
483 spin_lock_init(&lcm->lcm_llcd_lock);
484 atomic_set(&lcm->lcm_llcd_numfree, 0);
485 lcm->lcm_llcd_minfree = 0;
486 lcm->lcm_thread_max = 5;
487 /* FIXME initialize semaphore for llog_process_args */
488 sema_init(&llpa.llpa_sem, 1);
491 EXPORT_SYMBOL(llog_init_commit_master);
493 int llog_cleanup_commit_master(struct llog_commit_master *lcm,
496 lcm->lcm_flags |= LLOG_LCM_FL_EXIT;
498 lcm->lcm_flags |= LLOG_LCM_FL_EXIT_FORCE;
499 cfs_waitq_signal(&lcm->lcm_waitq);
501 wait_event_interruptible(lcm->lcm_waitq,
502 atomic_read(&lcm->lcm_thread_total) == 0);
505 EXPORT_SYMBOL(llog_cleanup_commit_master);
507 static int log_process_thread(void *args)
509 struct llog_process_args *data = args;
510 struct llog_ctxt *ctxt = data->llpa_ctxt;
511 void *cb = data->llpa_cb;
512 struct llog_logid logid = *(struct llog_logid *)(data->llpa_arg);
513 struct llog_handle *llh = NULL;
517 mutex_up(&data->llpa_sem);
518 ptlrpc_daemonize("llog_process"); /* thread does IO to log files */
520 rc = llog_create(ctxt, &llh, &logid, NULL);
522 CERROR("llog_create failed %d\n", rc);
525 rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
527 CERROR("llog_init_handle failed %d\n", rc);
528 GOTO(release_llh, rc);
532 rc = llog_cat_process(llh, (llog_cb_t)cb, NULL);
533 if (rc != LLOG_PROC_BREAK)
534 CERROR("llog_cat_process failed %d\n", rc);
536 CWARN("no callback function for recovery\n");
539 CDEBUG(D_HA, "send llcd %p:%p forcibly after recovery\n",
540 ctxt->loc_llcd, ctxt);
541 llog_sync(ctxt, NULL);
544 rc = llog_cat_put(llh);
546 CERROR("llog_cat_put failed %d\n", rc);
552 static int llog_recovery_generic(struct llog_ctxt *ctxt, void *handle,void *arg)
554 struct obd_device *obd = ctxt->loc_obd;
558 if (obd->obd_stopping)
561 mutex_down(&llpa.llpa_sem);
562 llpa.llpa_cb = handle;
564 llpa.llpa_ctxt = llog_get_context(ctxt->loc_obd, ctxt->loc_idx);
565 if (!llpa.llpa_ctxt) {
569 rc = cfs_kernel_thread(log_process_thread, &llpa, CLONE_VM | CLONE_FILES);
571 CERROR("error starting log_process_thread: %d\n", rc);
573 CDEBUG(D_HA, "log_process_thread: %d\n", rc);
580 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
581 struct llog_logid *logid, struct llog_gen *gen,
582 struct obd_uuid *uuid)
584 struct llog_canceld_ctxt *llcd;
588 /* send back llcd before recovery from llog */
589 if (ctxt->loc_llcd != NULL) {
590 CWARN("llcd %p:%p not empty\n", ctxt->loc_llcd, ctxt);
591 llog_sync(ctxt, NULL);
594 mutex_down(&ctxt->loc_sem);
595 ctxt->loc_gen = *gen;
596 llcd = llcd_grab(ctxt->loc_lcm);
598 CERROR("couldn't get an llcd\n");
599 mutex_up(&ctxt->loc_sem);
602 llcd->llcd_ctxt = llog_ctxt_get(ctxt);
603 ctxt->loc_llcd = llcd;
604 mutex_up(&ctxt->loc_sem);
606 rc = llog_recovery_generic(ctxt, ctxt->llog_proc_cb, logid);
608 CERROR("error recovery process: %d\n", rc);
612 EXPORT_SYMBOL(llog_repl_connect);
614 #else /* !__KERNEL__ */
616 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
617 struct lov_stripe_md *lsm, int count,
618 struct llog_cookie *cookies, int flags)