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