Whamcloud - gitweb
1cb65fd97c38b711a16a7a414a845d8d1942873d
[fs/lustre-release.git] / lustre / ptlrpc / recov_thread.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/recov_thread.c
37  *
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
42  *
43  * Author: Andreas Dilger   <adilger@clusterfs.com>
44  *         Yury Umanets     <yury.umanets@sun.com>
45  *         Alexey Lyashkov  <alexey.lyashkov@sun.com>
46  */
47
48 #define DEBUG_SUBSYSTEM S_LOG
49
50 #ifndef EXPORT_SYMTAB
51 # define EXPORT_SYMTAB
52 #endif
53
54 #ifdef __KERNEL__
55 # include <libcfs/libcfs.h>
56 #else
57 # include <libcfs/list.h>
58 # include <liblustre.h>
59 #endif
60
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"
69
70 static atomic_t                   llcd_count = ATOMIC_INIT(0);
71 static cfs_mem_cache_t           *llcd_cache = NULL;
72
73 #ifdef __KERNEL__
74 enum {
75         LLOG_LCM_FL_START       = 1 << 0,
76         LLOG_LCM_FL_EXIT        = 1 << 1
77 };
78
79 static void llcd_print(struct llog_canceld_ctxt *llcd, 
80                        const char *func, int line) 
81 {
82         CDEBUG(D_RPCTRACE, "Llcd (%p) at %s:%d:\n", llcd, func, line);
83         CDEBUG(D_RPCTRACE, "  size: %d\n", llcd->llcd_size);
84         CDEBUG(D_RPCTRACE, "  ctxt: %p\n", llcd->llcd_ctxt);
85         CDEBUG(D_RPCTRACE, "  lcm : %p\n", llcd->llcd_lcm);
86         CDEBUG(D_RPCTRACE, "  cookiebytes : %d\n", llcd->llcd_cookiebytes);
87 }
88
89 /**
90  * Allocate new llcd from cache, init it and return to caller.
91  * Bumps number of objects allocated.
92  */
93 static struct llog_canceld_ctxt *llcd_alloc(struct llog_commit_master *lcm)
94 {
95         struct llog_canceld_ctxt *llcd;
96         int llcd_size;
97
98         LASSERT(lcm != NULL);
99
100         /*
101          * Payload of lustre_msg V2 is bigger.
102          */
103         llcd_size = CFS_PAGE_SIZE -
104                 lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
105         llcd_size += offsetof(struct llog_canceld_ctxt, llcd_cookies);
106         OBD_SLAB_ALLOC(llcd, llcd_cache, CFS_ALLOC_STD, llcd_size);
107         if (!llcd)
108                 return NULL;
109
110         CFS_INIT_LIST_HEAD(&llcd->llcd_list);
111         llcd->llcd_size = llcd_size;
112         llcd->llcd_cookiebytes = 0;
113
114         spin_lock(&lcm->lcm_lock);
115         llcd->llcd_lcm = lcm;
116         atomic_inc(&lcm->lcm_count);
117         list_add_tail(&llcd->llcd_list, &lcm->lcm_llcds);
118         spin_unlock(&lcm->lcm_lock);
119         atomic_inc(&llcd_count);
120
121         CDEBUG(D_RPCTRACE, "Alloc llcd %p on lcm %p (%d)\n",
122                llcd, lcm, atomic_read(&lcm->lcm_count));
123
124         return llcd;
125 }
126
127 /**
128  * Returns passed llcd to cache.
129  */
130 static void llcd_free(struct llog_canceld_ctxt *llcd)
131 {
132         struct llog_commit_master *lcm = llcd->llcd_lcm;
133
134         if (lcm) {
135                 if (atomic_read(&lcm->lcm_count) == 0) {
136                         CERROR("Invalid llcd free %p\n", llcd);
137                         llcd_print(llcd, __FUNCTION__, __LINE__);
138                         LBUG();
139                 }
140                 spin_lock(&lcm->lcm_lock);
141                 LASSERT(!list_empty(&llcd->llcd_list));
142                 list_del_init(&llcd->llcd_list);
143                 atomic_dec(&lcm->lcm_count);
144                 spin_unlock(&lcm->lcm_lock);
145         }
146
147         CDEBUG(D_RPCTRACE, "Free llcd %p on lcm %p (%d)\n", 
148                llcd, lcm, atomic_read(&lcm->lcm_count));
149
150         LASSERT(atomic_read(&llcd_count) > 0);
151         atomic_dec(&llcd_count);
152         OBD_SLAB_FREE(llcd, llcd_cache, llcd->llcd_size);
153 }
154
155 /**
156  * Copy passed @cookies to @llcd.
157  */
158 static void llcd_copy(struct llog_canceld_ctxt *llcd,
159                       struct llog_cookie *cookies)
160 {
161         memcpy((char *)llcd->llcd_cookies + llcd->llcd_cookiebytes,
162               cookies, sizeof(*cookies));
163         llcd->llcd_cookiebytes += sizeof(*cookies);
164 }
165
166 /**
167  * Checks if passed cookie fits into llcd free space buffer. Returns
168  * 1 if yes and 0 otherwise.
169  */
170 static int llcd_fit(struct llog_canceld_ctxt *llcd,
171                     struct llog_cookie *cookies)
172 {
173         return (llcd->llcd_size -
174                 llcd->llcd_cookiebytes) >= sizeof(*cookies);
175 }
176
177 /**
178  * Llcd completion function. Called uppon llcd send finish regardless
179  * sending result. Error is passed in @rc. Note, that this will be called
180  * in cleanup time when all inflight rpcs aborted.
181  */
182 static int
183 llcd_interpret(const struct lu_env *env,
184                struct ptlrpc_request *req, void *noused, int rc)
185 {
186         struct llog_canceld_ctxt *llcd = req->rq_async_args.pointer_arg[0];
187         CDEBUG(D_RPCTRACE, "Sent llcd %p (%d) - killing it\n", llcd, rc);
188         llcd_free(llcd);
189         return 0;
190 }
191
192 /**
193  * Send @llcd to remote node. Free llcd uppon completion or error. Sending
194  * is performed in async style so this function will return asap without
195  * blocking.
196  */
197 static int llcd_send(struct llog_canceld_ctxt *llcd)
198 {
199         char *bufs[2] = { NULL, (char *)llcd->llcd_cookies };
200         struct obd_import *import = NULL;
201         struct llog_commit_master *lcm;
202         struct ptlrpc_request *req;
203         struct llog_ctxt *ctxt;
204         int rc;
205         ENTRY;
206
207         ctxt = llcd->llcd_ctxt;
208         if (!ctxt) {
209                 CERROR("Invalid llcd with NULL ctxt found (%p)\n",
210                        llcd);
211                 llcd_print(llcd, __FUNCTION__, __LINE__);
212                 LBUG();
213         }
214         LASSERT_SEM_LOCKED(&ctxt->loc_sem);
215
216         if (llcd->llcd_cookiebytes == 0)
217                 GOTO(exit, rc = 0);
218
219         lcm = llcd->llcd_lcm;
220
221         /*
222          * Check if we're in exit stage. Do not send llcd in
223          * this case.
224          */
225         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
226                 GOTO(exit, rc = -ENODEV);
227
228         CDEBUG(D_RPCTRACE, "Sending llcd %p\n", llcd);
229
230         import = llcd->llcd_ctxt->loc_imp;
231         if (!import || (import == LP_POISON) ||
232             (import->imp_client == LP_POISON)) {
233                 CERROR("Invalid import %p for llcd %p\n",
234                        import, llcd);
235                 GOTO(exit, rc = -ENODEV);
236         }
237
238         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_RECOV, 10);
239
240         /*
241          * No need to get import here as it is already done in
242          * llog_receptor_accept().
243          */
244         req = ptlrpc_request_alloc(import, &RQF_LOG_CANCEL);
245         if (req == NULL) {
246                 CERROR("Can't allocate request for sending llcd %p\n",
247                        llcd);
248                 GOTO(exit, rc = -ENOMEM);
249         }
250         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES,
251                              RCL_CLIENT, llcd->llcd_cookiebytes);
252
253         rc = ptlrpc_request_bufs_pack(req, LUSTRE_LOG_VERSION,
254                                       OBD_LOG_CANCEL, bufs, NULL);
255         if (rc) {
256                 ptlrpc_request_free(req);
257                 GOTO(exit, rc);
258         }
259
260         ptlrpc_at_set_req_timeout(req);
261         ptlrpc_request_set_replen(req);
262
263         /* bug 5515 */
264         req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
265         req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
266         req->rq_interpret_reply = (ptlrpc_interpterer_t)llcd_interpret;
267         req->rq_async_args.pointer_arg[0] = llcd;
268         rc = ptlrpc_set_add_new_req(&lcm->lcm_pc, req);
269         if (rc) {
270                 ptlrpc_request_free(req);
271                 GOTO(exit, rc);
272         }
273         RETURN(0);
274 exit:
275         CDEBUG(D_RPCTRACE, "Refused llcd %p\n", llcd);
276         llcd_free(llcd);
277         return rc;
278 }
279
280 /**
281  * Attach @llcd to @ctxt. Establish llcd vs. ctxt reserve connection
282  * so hat they can refer each other.
283  */
284 static int
285 llcd_attach(struct llog_ctxt *ctxt, struct llog_canceld_ctxt *llcd)
286 {
287         LASSERT(ctxt != NULL && llcd != NULL);
288         LASSERT_SEM_LOCKED(&ctxt->loc_sem);
289         LASSERT(ctxt->loc_llcd == NULL);
290         llcd->llcd_ctxt = llog_ctxt_get(ctxt);
291         ctxt->loc_llcd = llcd;
292
293         CDEBUG(D_RPCTRACE, "Attach llcd %p to ctxt %p\n",
294                llcd, ctxt);
295
296         return 0;
297 }
298
299 /**
300  * Opposite to llcd_attach(). Detaches llcd from its @ctxt. This makes
301  * sure that this llcd will not be found another time we try to cancel.
302  */
303 static struct llog_canceld_ctxt *llcd_detach(struct llog_ctxt *ctxt)
304 {
305         struct llog_canceld_ctxt *llcd;
306
307         LASSERT(ctxt != NULL);
308         LASSERT_SEM_LOCKED(&ctxt->loc_sem);
309
310         llcd = ctxt->loc_llcd;
311         if (!llcd)
312                 return NULL;
313
314         CDEBUG(D_RPCTRACE, "Detach llcd %p from ctxt %p\n", 
315                llcd, ctxt);
316
317         ctxt->loc_llcd = NULL;
318         llog_ctxt_put(ctxt);
319         return llcd;
320 }
321
322 /**
323  * Return @llcd cached in @ctxt. Allocate new one if required. Attach it
324  * to ctxt so that it may be used for gathering cookies and sending.
325  */
326 static struct llog_canceld_ctxt *llcd_get(struct llog_ctxt *ctxt)
327 {
328         struct llog_canceld_ctxt *llcd;
329
330         llcd = llcd_alloc(ctxt->loc_lcm);
331         if (!llcd) {
332                 CERROR("Can't alloc an llcd for ctxt %p\n", ctxt);
333                 return NULL;
334         }
335         llcd_attach(ctxt, llcd);
336         return llcd;
337 }
338
339 /**
340  * Deatch llcd from its @ctxt. Free llcd.
341  */
342 static void llcd_put(struct llog_ctxt *ctxt)
343 {
344         struct llog_canceld_ctxt *llcd;
345
346         llcd = llcd_detach(ctxt);
347         if (llcd)
348                 llcd_free(llcd);
349 }
350
351 /**
352  * Detach llcd from its @ctxt so that nobody will find it with try to
353  * re-use. Send llcd to remote node.
354  */
355 static int llcd_push(struct llog_ctxt *ctxt)
356 {
357         struct llog_canceld_ctxt *llcd;
358         int rc;
359
360         /*
361          * Make sure that this llcd will not be sent again as we detach
362          * it from ctxt.
363          */
364         llcd = llcd_detach(ctxt);
365         if (!llcd) {
366                 CERROR("Invalid detached llcd found %p\n", llcd);
367                 llcd_print(llcd, __FUNCTION__, __LINE__);
368                 LBUG();
369         }
370
371         rc = llcd_send(llcd);
372         if (rc)
373                 CERROR("Couldn't send llcd %p (%d)\n", llcd, rc);
374         return rc;
375 }
376
377 /**
378  * Start recovery thread which actually deals llcd sending. This
379  * is all ptlrpc standard thread based so there is not much of work
380  * to do.
381  */
382 int llog_recov_thread_start(struct llog_commit_master *lcm)
383 {
384         int rc;
385         ENTRY;
386
387         rc = ptlrpcd_start(lcm->lcm_name, &lcm->lcm_pc);
388         if (rc) {
389                 CERROR("Error %d while starting recovery thread %s\n",
390                        rc, lcm->lcm_name);
391                 RETURN(rc);
392         }
393         RETURN(rc);
394 }
395 EXPORT_SYMBOL(llog_recov_thread_start);
396
397 /**
398  * Stop recovery thread. Complement to llog_recov_thread_start().
399  */
400 void llog_recov_thread_stop(struct llog_commit_master *lcm, int force)
401 {
402         ENTRY;
403
404         /*
405          * Let all know that we're stopping. This will also make
406          * llcd_send() refuse any new llcds.
407          */
408         set_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags);
409
410         /*
411          * Stop processing thread. No new rpcs will be accepted for
412          * for processing now.
413          */
414         ptlrpcd_stop(&lcm->lcm_pc, force);
415
416         /*
417          * By this point no alive inflight llcds should be left. Only
418          * those forgotten in sync may still be attached to ctxt. Let's
419          * print them.
420          */
421         if (atomic_read(&lcm->lcm_count) != 0) {
422                 struct llog_canceld_ctxt *llcd;
423                 struct list_head         *tmp;
424
425                 CERROR("Busy llcds found (%d) on lcm %p\n", 
426                        atomic_read(&lcm->lcm_count) == 0, lcm);
427
428                 spin_lock(&lcm->lcm_lock);
429                 list_for_each(tmp, &lcm->lcm_llcds) {
430                         llcd = list_entry(tmp, struct llog_canceld_ctxt,
431                                           llcd_list);
432                         llcd_print(llcd, __FUNCTION__, __LINE__);
433                 }
434                 spin_unlock(&lcm->lcm_lock);
435                 
436                 /*
437                  * No point to go further with busy llcds at this point
438                  * as this is clear bug. It might mean we got hanging
439                  * rpc which holds import ref and this means we will not
440                  * be able to cleanup anyways.
441                  *
442                  * Or we just missed to kill them when they were not
443                  * attached to ctxt. In this case our slab will remind
444                  * us about this a bit later.
445                  */
446                 LBUG();
447         }
448         EXIT;
449 }
450 EXPORT_SYMBOL(llog_recov_thread_stop);
451
452 /**
453  * Initialize commit master structure and start recovery thread on it.
454  */
455 struct llog_commit_master *llog_recov_thread_init(char *name)
456 {
457         struct llog_commit_master *lcm;
458         int rc;
459         ENTRY;
460
461         OBD_ALLOC_PTR(lcm);
462         if (!lcm)
463                 RETURN(NULL);
464
465         /*
466          * Try to create threads with unique names.
467          */
468         snprintf(lcm->lcm_name, sizeof(lcm->lcm_name),
469                  "ll_log_commit_%s", name);
470
471         strncpy(lcm->lcm_name, name, sizeof(lcm->lcm_name));
472         atomic_set(&lcm->lcm_count, 0);
473         spin_lock_init(&lcm->lcm_lock);
474         CFS_INIT_LIST_HEAD(&lcm->lcm_llcds);
475         rc = llog_recov_thread_start(lcm);
476         if (rc) {
477                 CERROR("Can't start commit thread, rc %d\n", rc);
478                 GOTO(out, rc);
479         }
480         RETURN(lcm);
481 out:
482         OBD_FREE_PTR(lcm);
483         return NULL;
484 }
485 EXPORT_SYMBOL(llog_recov_thread_init);
486
487 /**
488  * Finalize commit master and its recovery thread.
489  */
490 void llog_recov_thread_fini(struct llog_commit_master *lcm, int force)
491 {
492         ENTRY;
493         llog_recov_thread_stop(lcm, force);
494         OBD_FREE_PTR(lcm);
495         EXIT;
496 }
497 EXPORT_SYMBOL(llog_recov_thread_fini);
498
499 static int llog_recov_thread_replay(struct llog_ctxt *ctxt,
500                                     void *cb, void *arg)
501 {
502         struct obd_device *obd = ctxt->loc_obd;
503         struct llog_process_cat_args *lpca;
504         int rc;
505         ENTRY;
506
507         if (obd->obd_stopping)
508                 RETURN(-ENODEV);
509
510         /*
511          * This will be balanced in llog_cat_process_thread()
512          */
513         OBD_ALLOC_PTR(lpca);
514         if (!lpca)
515                 RETURN(-ENOMEM);
516
517         lpca->lpca_cb = cb;
518         lpca->lpca_arg = arg;
519
520         /*
521          * This will be balanced in llog_cat_process_thread()
522          */
523         lpca->lpca_ctxt = llog_ctxt_get(ctxt);
524         if (!lpca->lpca_ctxt) {
525                 OBD_FREE_PTR(lpca);
526                 RETURN(-ENODEV);
527         }
528         rc = cfs_kernel_thread(llog_cat_process_thread, lpca,
529                                CLONE_VM | CLONE_FILES);
530         if (rc < 0) {
531                 CERROR("Error starting llog_cat_process_thread(): %d\n", rc);
532                 OBD_FREE_PTR(lpca);
533                 llog_ctxt_put(ctxt);
534         } else {
535                 CDEBUG(D_HA, "Started llog_cat_process_thread(): %d\n", rc);
536                 rc = 0;
537         }
538
539         RETURN(rc);
540 }
541
542 int llog_obd_repl_connect(struct llog_ctxt *ctxt,
543                           struct llog_logid *logid, struct llog_gen *gen,
544                           struct obd_uuid *uuid)
545 {
546         int rc;
547         ENTRY;
548
549         /*
550          * Send back cached llcd from llog before recovery if we have any.
551          * This is void is nothing cached is found there.
552          */
553         llog_sync(ctxt, NULL);
554
555         /*
556          * Start recovery in separate thread.
557          */
558         mutex_down(&ctxt->loc_sem);
559         ctxt->loc_gen = *gen;
560         rc = llog_recov_thread_replay(ctxt, ctxt->llog_proc_cb, logid);
561         mutex_up(&ctxt->loc_sem);
562
563         RETURN(rc);
564 }
565 EXPORT_SYMBOL(llog_obd_repl_connect);
566
567 /**
568  * Deleted objects have a commit callback that cancels the MDS
569  * log record for the deletion. The commit callback calls this
570  * function.
571  */
572 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
573                          struct lov_stripe_md *lsm, int count,
574                          struct llog_cookie *cookies, int flags)
575 {
576         struct llog_commit_master *lcm;
577         struct llog_canceld_ctxt *llcd;
578         int rc = 0;
579         ENTRY;
580
581         LASSERT(ctxt != NULL);
582
583         mutex_down(&ctxt->loc_sem);
584         lcm = ctxt->loc_lcm;
585
586         /*
587          * Let's check if we have all structures alive. We also check for
588          * possible shutdown. Do nothing if we're stopping.
589          */
590         if (ctxt->loc_imp == NULL) {
591                 CDEBUG(D_RPCTRACE, "No import for ctxt %p\n", ctxt);
592                 GOTO(out, rc = -ENODEV);
593         }
594
595         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
596                 CDEBUG(D_RPCTRACE, "Commit thread is stopping for ctxt %p\n",
597                        ctxt);
598                 GOTO(out, rc = -ENODEV);
599         }
600
601         llcd = ctxt->loc_llcd;
602
603         if (count > 0 && cookies != NULL) {
604                 /*
605                  * Get new llcd from ctxt if required.
606                  */
607                 if (!llcd) {
608                         llcd = llcd_get(ctxt);
609                         if (!llcd)
610                                 GOTO(out, rc = -ENOMEM);
611                         /*
612                          * Allocation is successful, let's check for stop
613                          * flag again to fall back as soon as possible.
614                          */
615                         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
616                                 GOTO(out, rc = -ENODEV);
617                 }
618
619                 /*
620                  * Llcd does not have enough room for @cookies. Let's push
621                  * it out and allocate new one.
622                  */
623                 if (!llcd_fit(llcd, cookies)) {
624                         rc = llcd_push(ctxt);
625                         if (rc)
626                                 GOTO(out, rc);
627                         llcd = llcd_get(ctxt);
628                         if (!llcd)
629                                 GOTO(out, rc = -ENOMEM);
630                         /*
631                          * Allocation is successful, let's check for stop
632                          * flag again to fall back as soon as possible.
633                          */
634                         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
635                                 GOTO(out, rc = -ENODEV);
636                 }
637
638                 /*
639                  * Copy cookies to @llcd, no matter old or new allocated
640                  * one.
641                  */
642                 llcd_copy(llcd, cookies);
643         }
644
645         /*
646          * Let's check if we need to send copied @cookies asap. If yes
647          * then do it.
648          */
649         if (llcd && (flags & OBD_LLOG_FL_SENDNOW)) {
650                 CDEBUG(D_RPCTRACE, "Sync llcd %p\n", llcd);
651                 rc = llcd_push(ctxt);
652                 if (rc)
653                         GOTO(out, rc);
654         }
655         EXIT;
656 out:
657         if (rc)
658                 llcd_put(ctxt);
659         mutex_up(&ctxt->loc_sem);
660         return rc;
661 }
662 EXPORT_SYMBOL(llog_obd_repl_cancel);
663
664 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
665 {
666         int rc = 0;
667         ENTRY;
668
669         /* 
670          * Flush any remaining llcd. 
671          */
672         mutex_down(&ctxt->loc_sem);
673         if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
674                 /*
675                  * This is ost->mds connection, we can't be sure that mds
676                  * can still receive cookies, let's killed the cached llcd.
677                  */
678                 CDEBUG(D_RPCTRACE, "Kill cached llcd\n");
679                 llcd_put(ctxt);
680                 mutex_up(&ctxt->loc_sem);
681         } else {
682                 /* 
683                  * This is either llog_sync() from generic llog code or sync
684                  * on client disconnect. In either way let's do it and send
685                  * llcds to the target with waiting for completion. 
686                  */
687                 CDEBUG(D_RPCTRACE, "Sync cached llcd\n");
688                 mutex_up(&ctxt->loc_sem);
689                 rc = llog_cancel(ctxt, NULL, 0, NULL, OBD_LLOG_FL_SENDNOW);
690         }
691         RETURN(rc);
692 }
693 EXPORT_SYMBOL(llog_obd_repl_sync);
694
695 #else /* !__KERNEL__ */
696
697 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
698                          struct lov_stripe_md *lsm, int count,
699                          struct llog_cookie *cookies, int flags)
700 {
701         return 0;
702 }
703 #endif
704
705 /**
706  * Module init time fucntion. Initializes slab for llcd objects.
707  */
708 int llog_recov_init(void)
709 {
710         int llcd_size;
711
712         llcd_size = CFS_PAGE_SIZE -
713                 lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
714         llcd_size += offsetof(struct llog_canceld_ctxt, llcd_cookies);
715         llcd_cache = cfs_mem_cache_create("llcd_cache", llcd_size, 0, 0);
716         if (!llcd_cache) {
717                 CERROR("Error allocating llcd cache\n");
718                 return -ENOMEM;
719         }
720         return 0;
721 }
722
723 /**
724  * Module fini time fucntion. Releases slab for llcd objects.
725  */
726 void llog_recov_fini(void)
727 {
728         /*
729          * Kill llcd cache when thread is stopped and we're sure no
730          * llcd in use left.
731          */
732         if (llcd_cache) {
733                 /*
734                  * In 2.6.22 cfs_mem_cache_destroy() will not return error
735                  * for busy resources. Let's check it another way.
736                  */
737                 LASSERTF(atomic_read(&llcd_count) == 0,
738                          "Can't destroy llcd cache! Number of "
739                          "busy llcds: %d\n", atomic_read(&llcd_count));
740                 cfs_mem_cache_destroy(llcd_cache);
741                 llcd_cache = NULL;
742         }
743 }