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