Whamcloud - gitweb
LU-2624 ptlrpc: improve stop of ptlrpcd threads
[fs/lustre-release.git] / lustre / ptlrpc / recov_thread.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 #ifdef __KERNEL__
51 # include <libcfs/libcfs.h>
52 #else
53 # include <libcfs/list.h>
54 # include <liblustre.h>
55 #endif
56
57 #include <obd_class.h>
58 #include <obd_support.h>
59 #include <obd_class.h>
60 #include <lustre_net.h>
61 #include <lnet/types.h>
62 #include <libcfs/list.h>
63 #include <lustre_log.h>
64 #include "ptlrpc_internal.h"
65
66 static cfs_atomic_t               llcd_count = CFS_ATOMIC_INIT(0);
67 static cfs_mem_cache_t           *llcd_cache = NULL;
68
69 #ifdef __KERNEL__
70 enum {
71         LLOG_LCM_FL_START       = 1 << 0,
72         LLOG_LCM_FL_EXIT        = 1 << 1
73 };
74
75 struct llcd_async_args {
76         struct llog_canceld_ctxt *la_ctxt;
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_GFP(llcd, llcd_cache, size + overhead, CFS_ALLOC_STD);
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         cfs_atomic_inc(&lcm->lcm_count);
118         cfs_list_add_tail(&llcd->llcd_list, &lcm->lcm_llcds);
119         spin_unlock(&lcm->lcm_lock);
120         cfs_atomic_inc(&llcd_count);
121
122         CDEBUG(D_RPCTRACE, "Alloc llcd %p on lcm %p (%d)\n",
123                llcd, lcm, cfs_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 (cfs_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(!cfs_list_empty(&llcd->llcd_list));
144                 cfs_list_del_init(&llcd->llcd_list);
145                 cfs_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, cfs_atomic_read(&lcm->lcm_count));
150         }
151
152         LASSERT(cfs_atomic_read(&llcd_count) > 0);
153         cfs_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 *args, int rc)
190 {
191         struct llcd_async_args *la = args;
192         struct llog_canceld_ctxt *llcd = la->la_ctxt;
193
194         CDEBUG(D_RPCTRACE, "Sent llcd %p (%d) - killing it\n", llcd, rc);
195         llcd_free(llcd);
196         return 0;
197 }
198
199 /**
200  * Send @llcd to remote node. Free llcd uppon completion or error. Sending
201  * is performed in async style so this function will return asap without
202  * blocking.
203  */
204 static int llcd_send(struct llog_canceld_ctxt *llcd)
205 {
206         char *bufs[2] = { NULL, (char *)llcd->llcd_cookies };
207         struct obd_import *import = NULL;
208         struct llog_commit_master *lcm;
209         struct llcd_async_args *la;
210         struct ptlrpc_request *req;
211         struct llog_ctxt *ctxt;
212         int rc;
213         ENTRY;
214
215         ctxt = llcd->llcd_ctxt;
216         if (!ctxt) {
217                 CERROR("Invalid llcd with NULL ctxt found (%p)\n",
218                        llcd);
219                 llcd_print(llcd, __FUNCTION__, __LINE__);
220                 LBUG();
221         }
222         LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
223
224         if (llcd->llcd_cookiebytes == 0)
225                 GOTO(exit, rc = 0);
226
227         lcm = llcd->llcd_lcm;
228
229         /*
230          * Check if we're in exit stage. Do not send llcd in
231          * this case.
232          */
233         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
234                 GOTO(exit, rc = -ENODEV);
235
236         CDEBUG(D_RPCTRACE, "Sending llcd %p\n", llcd);
237
238         import = llcd->llcd_ctxt->loc_imp;
239         if (!import || (import == LP_POISON) ||
240             (import->imp_client == LP_POISON)) {
241                 CERROR("Invalid import %p for llcd %p\n",
242                        import, llcd);
243                 GOTO(exit, rc = -ENODEV);
244         }
245
246         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_RECOV, 10);
247
248         /*
249          * No need to get import here as it is already done in
250          * llog_receptor_accept().
251          */
252         req = ptlrpc_request_alloc(import, &RQF_LOG_CANCEL);
253         if (req == NULL) {
254                 CERROR("Can't allocate request for sending llcd %p\n",
255                        llcd);
256                 GOTO(exit, rc = -ENOMEM);
257         }
258         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES,
259                              RCL_CLIENT, llcd->llcd_cookiebytes);
260
261         rc = ptlrpc_request_bufs_pack(req, LUSTRE_LOG_VERSION,
262                                       OBD_LOG_CANCEL, bufs, NULL);
263         if (rc) {
264                 ptlrpc_request_free(req);
265                 GOTO(exit, rc);
266         }
267
268         ptlrpc_at_set_req_timeout(req);
269         ptlrpc_request_set_replen(req);
270
271         /* bug 5515 */
272         req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
273         req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
274
275         req->rq_interpret_reply = (ptlrpc_interpterer_t)llcd_interpret;
276
277         CLASSERT(sizeof(*la) <= sizeof(req->rq_async_args));
278         la = ptlrpc_req_async_args(req);
279         la->la_ctxt = llcd;
280
281         /* llog cancels will be replayed after reconnect so this will do twice
282          * first from replay llog, second for resended rpc */
283         req->rq_no_delay = req->rq_no_resend = 1;
284
285         ptlrpc_set_add_new_req(&lcm->lcm_pc, req);
286         RETURN(0);
287 exit:
288         CDEBUG(D_RPCTRACE, "Refused llcd %p\n", llcd);
289         llcd_free(llcd);
290         return rc;
291 }
292
293 /**
294  * Attach @llcd to @ctxt. Establish llcd vs. ctxt reserve connection
295  * so hat they can refer each other.
296  */
297 static int
298 llcd_attach(struct llog_ctxt *ctxt, struct llog_canceld_ctxt *llcd)
299 {
300         LASSERT(ctxt != NULL && llcd != NULL);
301         LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
302         LASSERT(ctxt->loc_llcd == NULL);
303         llcd->llcd_ctxt = llog_ctxt_get(ctxt);
304         ctxt->loc_llcd = llcd;
305
306         CDEBUG(D_RPCTRACE, "Attach llcd %p to ctxt %p\n",
307                llcd, ctxt);
308
309         return 0;
310 }
311
312 /**
313  * Opposite to llcd_attach(). Detaches llcd from its @ctxt. This makes
314  * sure that this llcd will not be found another time we try to cancel.
315  */
316 static struct llog_canceld_ctxt *llcd_detach(struct llog_ctxt *ctxt)
317 {
318         struct llog_canceld_ctxt *llcd;
319
320         LASSERT(ctxt != NULL);
321         LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
322
323         llcd = ctxt->loc_llcd;
324         if (!llcd)
325                 return NULL;
326
327         CDEBUG(D_RPCTRACE, "Detach llcd %p from ctxt %p\n",
328                llcd, ctxt);
329
330         ctxt->loc_llcd = NULL;
331         llog_ctxt_put(ctxt);
332         return llcd;
333 }
334
335 /**
336  * Return @llcd cached in @ctxt. Allocate new one if required. Attach it
337  * to ctxt so that it may be used for gathering cookies and sending.
338  */
339 static struct llog_canceld_ctxt *llcd_get(struct llog_ctxt *ctxt)
340 {
341         struct llog_canceld_ctxt *llcd;
342         LASSERT(ctxt);
343         llcd = llcd_alloc(ctxt->loc_lcm);
344         if (!llcd) {
345                 CERROR("Can't alloc an llcd for ctxt %p\n", ctxt);
346                 return NULL;
347         }
348         llcd_attach(ctxt, llcd);
349         return llcd;
350 }
351
352 /**
353  * Deatch llcd from its @ctxt. Free llcd.
354  */
355 static void llcd_put(struct llog_ctxt *ctxt)
356 {
357         struct llog_canceld_ctxt *llcd;
358
359         llcd = llcd_detach(ctxt);
360         if (llcd)
361                 llcd_free(llcd);
362 }
363
364 /**
365  * Detach llcd from its @ctxt so that nobody will find it with try to
366  * re-use. Send llcd to remote node.
367  */
368 static int llcd_push(struct llog_ctxt *ctxt)
369 {
370         struct llog_canceld_ctxt *llcd;
371         int rc;
372
373         /*
374          * Make sure that this llcd will not be sent again as we detach
375          * it from ctxt.
376          */
377         llcd = llcd_detach(ctxt);
378         if (!llcd) {
379                 CERROR("Invalid detached llcd found %p\n", llcd);
380                 llcd_print(llcd, __FUNCTION__, __LINE__);
381                 LBUG();
382         }
383
384         rc = llcd_send(llcd);
385         if (rc)
386                 CERROR("Couldn't send llcd %p (%d)\n", llcd, rc);
387         return rc;
388 }
389
390 /**
391  * Start recovery thread which actually deals llcd sending. This
392  * is all ptlrpc standard thread based so there is not much of work
393  * to do.
394  */
395 int llog_recov_thread_start(struct llog_commit_master *lcm)
396 {
397         int rc;
398         ENTRY;
399
400         rc = ptlrpcd_start(-1, 1, lcm->lcm_name, &lcm->lcm_pc);
401         if (rc) {
402                 CERROR("Error %d while starting recovery thread %s\n",
403                        rc, lcm->lcm_name);
404                 RETURN(rc);
405         }
406         RETURN(rc);
407 }
408 EXPORT_SYMBOL(llog_recov_thread_start);
409
410 /**
411  * Stop recovery thread. Complement to llog_recov_thread_start().
412  */
413 void llog_recov_thread_stop(struct llog_commit_master *lcm, int force)
414 {
415         ENTRY;
416
417         /*
418          * Let all know that we're stopping. This will also make
419          * llcd_send() refuse any new llcds.
420          */
421         set_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags);
422
423         /*
424          * Stop processing thread. No new rpcs will be accepted for
425          * for processing now.
426          */
427         ptlrpcd_stop(&lcm->lcm_pc, force);
428         ptlrpcd_free(&lcm->lcm_pc);
429
430         /*
431          * By this point no alive inflight llcds should be left. Only
432          * those forgotten in sync may still be attached to ctxt. Let's
433          * print them.
434          */
435         if (cfs_atomic_read(&lcm->lcm_count) != 0) {
436                 struct llog_canceld_ctxt *llcd;
437                 cfs_list_t               *tmp;
438
439                 CERROR("Busy llcds found (%d) on lcm %p\n",
440                        cfs_atomic_read(&lcm->lcm_count), lcm);
441
442                 spin_lock(&lcm->lcm_lock);
443                 cfs_list_for_each(tmp, &lcm->lcm_llcds) {
444                         llcd = cfs_list_entry(tmp, struct llog_canceld_ctxt,
445                                               llcd_list);
446                         llcd_print(llcd, __func__, __LINE__);
447                 }
448                 spin_unlock(&lcm->lcm_lock);
449
450                 /*
451                  * No point to go further with busy llcds at this point
452                  * as this is clear bug. It might mean we got hanging
453                  * rpc which holds import ref and this means we will not
454                  * be able to cleanup anyways.
455                  *
456                  * Or we just missed to kill them when they were not
457                  * attached to ctxt. In this case our slab will remind
458                  * us about this a bit later.
459                  */
460                 LBUG();
461         }
462         EXIT;
463 }
464 EXPORT_SYMBOL(llog_recov_thread_stop);
465
466 /**
467  * Initialize commit master structure and start recovery thread on it.
468  */
469 struct llog_commit_master *llog_recov_thread_init(char *name)
470 {
471         struct llog_commit_master *lcm;
472         int rc;
473         ENTRY;
474
475         OBD_ALLOC_PTR(lcm);
476         if (!lcm)
477                 RETURN(NULL);
478
479         /*
480          * Try to create threads with unique names.
481          */
482         snprintf(lcm->lcm_name, sizeof(lcm->lcm_name),
483                  "lcm_%s", name);
484
485         cfs_atomic_set(&lcm->lcm_count, 0);
486         cfs_atomic_set(&lcm->lcm_refcount, 1);
487         spin_lock_init(&lcm->lcm_lock);
488         CFS_INIT_LIST_HEAD(&lcm->lcm_llcds);
489         rc = llog_recov_thread_start(lcm);
490         if (rc) {
491                 CERROR("Can't start commit thread, rc %d\n", rc);
492                 GOTO(out, rc);
493         }
494         RETURN(lcm);
495 out:
496         OBD_FREE_PTR(lcm);
497         return NULL;
498 }
499 EXPORT_SYMBOL(llog_recov_thread_init);
500
501 /**
502  * Finalize commit master and its recovery thread.
503  */
504 void llog_recov_thread_fini(struct llog_commit_master *lcm, int force)
505 {
506         ENTRY;
507         llog_recov_thread_stop(lcm, force);
508         lcm_put(lcm);
509         EXIT;
510 }
511 EXPORT_SYMBOL(llog_recov_thread_fini);
512
513 static int llog_recov_thread_replay(struct llog_ctxt *ctxt,
514                                     void *cb, void *arg)
515 {
516         struct obd_device *obd = ctxt->loc_obd;
517         struct llog_process_cat_args *lpca;
518         int rc;
519         ENTRY;
520
521         if (obd->obd_stopping)
522                 RETURN(-ENODEV);
523
524         /*
525          * This will be balanced in llog_cat_process_thread()
526          */
527         OBD_ALLOC_PTR(lpca);
528         if (!lpca)
529                 RETURN(-ENOMEM);
530
531         lpca->lpca_cb = cb;
532         lpca->lpca_arg = arg;
533
534         /*
535          * This will be balanced in llog_cat_process_thread()
536          */
537         lpca->lpca_ctxt = llog_ctxt_get(ctxt);
538         if (!lpca->lpca_ctxt) {
539                 OBD_FREE_PTR(lpca);
540                 RETURN(-ENODEV);
541         }
542         rc = cfs_create_thread(llog_cat_process_thread, lpca, CFS_DAEMON_FLAGS);
543         if (rc < 0) {
544                 CERROR("Error starting llog_cat_process_thread(): %d\n", rc);
545                 OBD_FREE_PTR(lpca);
546                 llog_ctxt_put(ctxt);
547         } else {
548                 CDEBUG(D_HA, "Started llog_cat_process_thread(): %d\n", rc);
549                 rc = 0;
550         }
551
552         RETURN(rc);
553 }
554
555 int llog_obd_repl_connect(struct llog_ctxt *ctxt,
556                           struct llog_logid *logid, struct llog_gen *gen,
557                           struct obd_uuid *uuid)
558 {
559         int rc;
560         ENTRY;
561
562         /*
563          * Send back cached llcd from llog before recovery if we have any.
564          * This is void is nothing cached is found there.
565          */
566         llog_sync(ctxt, NULL, 0);
567
568         /*
569          * Start recovery in separate thread.
570          */
571         mutex_lock(&ctxt->loc_mutex);
572         ctxt->loc_gen = *gen;
573         rc = llog_recov_thread_replay(ctxt, ctxt->llog_proc_cb, logid);
574         mutex_unlock(&ctxt->loc_mutex);
575
576         RETURN(rc);
577 }
578 EXPORT_SYMBOL(llog_obd_repl_connect);
579
580 /**
581  * Deleted objects have a commit callback that cancels the MDS
582  * log record for the deletion. The commit callback calls this
583  * function.
584  */
585 int llog_obd_repl_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
586                          struct lov_stripe_md *lsm, int count,
587                          struct llog_cookie *cookies, int flags)
588 {
589         struct llog_commit_master *lcm;
590         struct llog_canceld_ctxt *llcd;
591         int rc = 0;
592         ENTRY;
593
594         LASSERT(ctxt != NULL);
595
596         mutex_lock(&ctxt->loc_mutex);
597         if (!ctxt->loc_lcm) {
598                 CDEBUG(D_RPCTRACE, "No lcm for ctxt %p\n", ctxt);
599                 GOTO(out, rc = -ENODEV);
600         }
601         lcm = ctxt->loc_lcm;
602         CDEBUG(D_INFO, "cancel on lsm %p\n", lcm);
603
604         /*
605          * Let's check if we have all structures alive. We also check for
606          * possible shutdown. Do nothing if we're stopping.
607          */
608         if (ctxt->loc_flags & LLOG_CTXT_FLAG_STOP) {
609                 CDEBUG(D_RPCTRACE, "Last sync was done for ctxt %p\n", ctxt);
610                 GOTO(out, rc = -ENODEV);
611         }
612
613         if (ctxt->loc_imp == NULL) {
614                 CDEBUG(D_RPCTRACE, "No import for ctxt %p\n", ctxt);
615                 GOTO(out, rc = -ENODEV);
616         }
617
618         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
619                 CDEBUG(D_RPCTRACE, "Commit thread is stopping for ctxt %p\n",
620                        ctxt);
621                 GOTO(out, rc = -ENODEV);
622         }
623
624         llcd = ctxt->loc_llcd;
625
626         if (count > 0 && cookies != NULL) {
627                 /*
628                  * Get new llcd from ctxt if required.
629                  */
630                 if (!llcd) {
631                         llcd = llcd_get(ctxt);
632                         if (!llcd)
633                                 GOTO(out, rc = -ENOMEM);
634                         /*
635                          * Allocation is successful, let's check for stop
636                          * flag again to fall back as soon as possible.
637                          */
638                         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
639                                 GOTO(out, rc = -ENODEV);
640                 }
641
642                 /*
643                  * Llcd does not have enough room for @cookies. Let's push
644                  * it out and allocate new one.
645                  */
646                 if (!llcd_fit(llcd, cookies)) {
647                         rc = llcd_push(ctxt);
648                         if (rc)
649                                 GOTO(out, rc);
650                         llcd = llcd_get(ctxt);
651                         if (!llcd)
652                                 GOTO(out, rc = -ENOMEM);
653                         /*
654                          * Allocation is successful, let's check for stop
655                          * flag again to fall back as soon as possible.
656                          */
657                         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
658                                 GOTO(out, rc = -ENODEV);
659                 }
660
661                 /*
662                  * Copy cookies to @llcd, no matter old or new allocated
663                  * one.
664                  */
665                 llcd_copy(llcd, cookies);
666         }
667
668         /*
669          * Let's check if we need to send copied @cookies asap. If yes
670          * then do it.
671          */
672         if (llcd && (flags & OBD_LLOG_FL_SENDNOW)) {
673                 CDEBUG(D_RPCTRACE, "Sync llcd %p\n", llcd);
674                 rc = llcd_push(ctxt);
675                 if (rc)
676                         GOTO(out, rc);
677         }
678         EXIT;
679 out:
680         if (rc)
681                 llcd_put(ctxt);
682
683         if (flags & OBD_LLOG_FL_EXIT)
684                 ctxt->loc_flags = LLOG_CTXT_FLAG_STOP;
685
686         mutex_unlock(&ctxt->loc_mutex);
687         return rc;
688 }
689 EXPORT_SYMBOL(llog_obd_repl_cancel);
690
691 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp,
692                        int flags)
693 {
694         int rc = 0;
695         ENTRY;
696
697         /*
698          * Flush any remaining llcd.
699          */
700         mutex_lock(&ctxt->loc_mutex);
701         if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
702                 /*
703                  * This is ost->mds connection, we can't be sure that mds
704                  * can still receive cookies, let's killed the cached llcd.
705                  */
706                 CDEBUG(D_RPCTRACE, "Kill cached llcd\n");
707                 llcd_put(ctxt);
708
709                 if (flags & OBD_LLOG_FL_EXIT)
710                         ctxt->loc_flags = LLOG_CTXT_FLAG_STOP;
711
712                 mutex_unlock(&ctxt->loc_mutex);
713         } else {
714                 /*
715                  * This is either llog_sync() from generic llog code or sync
716                  * on client disconnect. In either way let's do it and send
717                  * llcds to the target with waiting for completion.
718                  */
719                 CDEBUG(D_RPCTRACE, "Sync cached llcd\n");
720                 mutex_unlock(&ctxt->loc_mutex);
721                 rc = llog_cancel(NULL, ctxt, NULL, 0, NULL,
722                                  OBD_LLOG_FL_SENDNOW | flags);
723         }
724         RETURN(rc);
725 }
726 EXPORT_SYMBOL(llog_obd_repl_sync);
727
728 #else /* !__KERNEL__ */
729
730 int llog_obd_repl_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
731                          struct lov_stripe_md *lsm, int count,
732                          struct llog_cookie *cookies, int flags)
733 {
734         return 0;
735 }
736 #endif
737
738 /**
739  * Module init time fucntion. Initializes slab for llcd objects.
740  */
741 int llog_recov_init(void)
742 {
743         int llcd_size;
744
745         llcd_size = CFS_PAGE_SIZE -
746                 lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
747         llcd_size += offsetof(struct llog_canceld_ctxt, llcd_cookies);
748         llcd_cache = cfs_mem_cache_create("llcd_cache", llcd_size, 0, 0);
749         if (!llcd_cache) {
750                 CERROR("Error allocating llcd cache\n");
751                 return -ENOMEM;
752         }
753         return 0;
754 }
755
756 /**
757  * Module fini time fucntion. Releases slab for llcd objects.
758  */
759 void llog_recov_fini(void)
760 {
761         /*
762          * Kill llcd cache when thread is stopped and we're sure no
763          * llcd in use left.
764          */
765         if (llcd_cache) {
766                 /*
767                  * In 2.6.22 cfs_mem_cache_destroy() will not return error
768                  * for busy resources. Let's check it another way.
769                  */
770                 LASSERTF(cfs_atomic_read(&llcd_count) == 0,
771                          "Can't destroy llcd cache! Number of "
772                          "busy llcds: %d\n", cfs_atomic_read(&llcd_count));
773                 cfs_mem_cache_destroy(llcd_cache);
774                 llcd_cache = NULL;
775         }
776 }