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