Whamcloud - gitweb
c10331f8ddb830cbe0a17711ebf05e776f5566cb
[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, Whamcloud, Inc.
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_MUTEX_LOCKED(&ctxt->loc_mutex);
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         ptlrpc_set_add_new_req(&lcm->lcm_pc, req);
290         RETURN(0);
291 exit:
292         CDEBUG(D_RPCTRACE, "Refused llcd %p\n", llcd);
293         llcd_free(llcd);
294         return rc;
295 }
296
297 /**
298  * Attach @llcd to @ctxt. Establish llcd vs. ctxt reserve connection
299  * so hat they can refer each other.
300  */
301 static int
302 llcd_attach(struct llog_ctxt *ctxt, struct llog_canceld_ctxt *llcd)
303 {
304         LASSERT(ctxt != NULL && llcd != NULL);
305         LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
306         LASSERT(ctxt->loc_llcd == NULL);
307         llcd->llcd_ctxt = llog_ctxt_get(ctxt);
308         ctxt->loc_llcd = llcd;
309
310         CDEBUG(D_RPCTRACE, "Attach llcd %p to ctxt %p\n",
311                llcd, ctxt);
312
313         return 0;
314 }
315
316 /**
317  * Opposite to llcd_attach(). Detaches llcd from its @ctxt. This makes
318  * sure that this llcd will not be found another time we try to cancel.
319  */
320 static struct llog_canceld_ctxt *llcd_detach(struct llog_ctxt *ctxt)
321 {
322         struct llog_canceld_ctxt *llcd;
323
324         LASSERT(ctxt != NULL);
325         LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
326
327         llcd = ctxt->loc_llcd;
328         if (!llcd)
329                 return NULL;
330
331         CDEBUG(D_RPCTRACE, "Detach llcd %p from ctxt %p\n",
332                llcd, ctxt);
333
334         ctxt->loc_llcd = NULL;
335         llog_ctxt_put(ctxt);
336         return llcd;
337 }
338
339 /**
340  * Return @llcd cached in @ctxt. Allocate new one if required. Attach it
341  * to ctxt so that it may be used for gathering cookies and sending.
342  */
343 static struct llog_canceld_ctxt *llcd_get(struct llog_ctxt *ctxt)
344 {
345         struct llog_canceld_ctxt *llcd;
346         LASSERT(ctxt);
347         llcd = llcd_alloc(ctxt->loc_lcm);
348         if (!llcd) {
349                 CERROR("Can't alloc an llcd for ctxt %p\n", ctxt);
350                 return NULL;
351         }
352         llcd_attach(ctxt, llcd);
353         return llcd;
354 }
355
356 /**
357  * Deatch llcd from its @ctxt. Free llcd.
358  */
359 static void llcd_put(struct llog_ctxt *ctxt)
360 {
361         struct llog_canceld_ctxt *llcd;
362
363         llcd = llcd_detach(ctxt);
364         if (llcd)
365                 llcd_free(llcd);
366 }
367
368 /**
369  * Detach llcd from its @ctxt so that nobody will find it with try to
370  * re-use. Send llcd to remote node.
371  */
372 static int llcd_push(struct llog_ctxt *ctxt)
373 {
374         struct llog_canceld_ctxt *llcd;
375         int rc;
376
377         /*
378          * Make sure that this llcd will not be sent again as we detach
379          * it from ctxt.
380          */
381         llcd = llcd_detach(ctxt);
382         if (!llcd) {
383                 CERROR("Invalid detached llcd found %p\n", llcd);
384                 llcd_print(llcd, __FUNCTION__, __LINE__);
385                 LBUG();
386         }
387
388         rc = llcd_send(llcd);
389         if (rc)
390                 CERROR("Couldn't send llcd %p (%d)\n", llcd, rc);
391         return rc;
392 }
393
394 /**
395  * Start recovery thread which actually deals llcd sending. This
396  * is all ptlrpc standard thread based so there is not much of work
397  * to do.
398  */
399 int llog_recov_thread_start(struct llog_commit_master *lcm)
400 {
401         int rc;
402         ENTRY;
403
404         rc = ptlrpcd_start(-1, 1, lcm->lcm_name, &lcm->lcm_pc);
405         if (rc) {
406                 CERROR("Error %d while starting recovery thread %s\n",
407                        rc, lcm->lcm_name);
408                 RETURN(rc);
409         }
410         RETURN(rc);
411 }
412 EXPORT_SYMBOL(llog_recov_thread_start);
413
414 /**
415  * Stop recovery thread. Complement to llog_recov_thread_start().
416  */
417 void llog_recov_thread_stop(struct llog_commit_master *lcm, int force)
418 {
419         ENTRY;
420
421         /*
422          * Let all know that we're stopping. This will also make
423          * llcd_send() refuse any new llcds.
424          */
425         cfs_set_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags);
426
427         /*
428          * Stop processing thread. No new rpcs will be accepted for
429          * for processing now.
430          */
431         ptlrpcd_stop(&lcm->lcm_pc, force);
432
433         /*
434          * By this point no alive inflight llcds should be left. Only
435          * those forgotten in sync may still be attached to ctxt. Let's
436          * print them.
437          */
438         if (cfs_atomic_read(&lcm->lcm_count) != 0) {
439                 struct llog_canceld_ctxt *llcd;
440                 cfs_list_t               *tmp;
441
442                 CERROR("Busy llcds found (%d) on lcm %p\n",
443                        cfs_atomic_read(&lcm->lcm_count), lcm);
444
445                 cfs_spin_lock(&lcm->lcm_lock);
446                 cfs_list_for_each(tmp, &lcm->lcm_llcds) {
447                         llcd = cfs_list_entry(tmp, struct llog_canceld_ctxt,
448                                               llcd_list);
449                         llcd_print(llcd, __FUNCTION__, __LINE__);
450                 }
451                 cfs_spin_unlock(&lcm->lcm_lock);
452
453                 /*
454                  * No point to go further with busy llcds at this point
455                  * as this is clear bug. It might mean we got hanging
456                  * rpc which holds import ref and this means we will not
457                  * be able to cleanup anyways.
458                  *
459                  * Or we just missed to kill them when they were not
460                  * attached to ctxt. In this case our slab will remind
461                  * us about this a bit later.
462                  */
463                 LBUG();
464         }
465         EXIT;
466 }
467 EXPORT_SYMBOL(llog_recov_thread_stop);
468
469 /**
470  * Initialize commit master structure and start recovery thread on it.
471  */
472 struct llog_commit_master *llog_recov_thread_init(char *name)
473 {
474         struct llog_commit_master *lcm;
475         int rc;
476         ENTRY;
477
478         OBD_ALLOC_PTR(lcm);
479         if (!lcm)
480                 RETURN(NULL);
481
482         /*
483          * Try to create threads with unique names.
484          */
485         snprintf(lcm->lcm_name, sizeof(lcm->lcm_name),
486                  "lcm_%s", name);
487
488         cfs_atomic_set(&lcm->lcm_count, 0);
489         cfs_atomic_set(&lcm->lcm_refcount, 1);
490         cfs_spin_lock_init(&lcm->lcm_lock);
491         CFS_INIT_LIST_HEAD(&lcm->lcm_llcds);
492         rc = llog_recov_thread_start(lcm);
493         if (rc) {
494                 CERROR("Can't start commit thread, rc %d\n", rc);
495                 GOTO(out, rc);
496         }
497         RETURN(lcm);
498 out:
499         OBD_FREE_PTR(lcm);
500         return NULL;
501 }
502 EXPORT_SYMBOL(llog_recov_thread_init);
503
504 /**
505  * Finalize commit master and its recovery thread.
506  */
507 void llog_recov_thread_fini(struct llog_commit_master *lcm, int force)
508 {
509         ENTRY;
510         llog_recov_thread_stop(lcm, force);
511         lcm_put(lcm);
512         EXIT;
513 }
514 EXPORT_SYMBOL(llog_recov_thread_fini);
515
516 static int llog_recov_thread_replay(struct llog_ctxt *ctxt,
517                                     void *cb, void *arg)
518 {
519         struct obd_device *obd = ctxt->loc_obd;
520         struct llog_process_cat_args *lpca;
521         int rc;
522         ENTRY;
523
524         if (obd->obd_stopping)
525                 RETURN(-ENODEV);
526
527         /*
528          * This will be balanced in llog_cat_process_thread()
529          */
530         OBD_ALLOC_PTR(lpca);
531         if (!lpca)
532                 RETURN(-ENOMEM);
533
534         lpca->lpca_cb = cb;
535         lpca->lpca_arg = arg;
536
537         /*
538          * This will be balanced in llog_cat_process_thread()
539          */
540         lpca->lpca_ctxt = llog_ctxt_get(ctxt);
541         if (!lpca->lpca_ctxt) {
542                 OBD_FREE_PTR(lpca);
543                 RETURN(-ENODEV);
544         }
545         rc = cfs_create_thread(llog_cat_process_thread, lpca, CFS_DAEMON_FLAGS);
546         if (rc < 0) {
547                 CERROR("Error starting llog_cat_process_thread(): %d\n", rc);
548                 OBD_FREE_PTR(lpca);
549                 llog_ctxt_put(ctxt);
550         } else {
551                 CDEBUG(D_HA, "Started llog_cat_process_thread(): %d\n", rc);
552                 rc = 0;
553         }
554
555         RETURN(rc);
556 }
557
558 int llog_obd_repl_connect(struct llog_ctxt *ctxt,
559                           struct llog_logid *logid, struct llog_gen *gen,
560                           struct obd_uuid *uuid)
561 {
562         int rc;
563         ENTRY;
564
565         /*
566          * Send back cached llcd from llog before recovery if we have any.
567          * This is void is nothing cached is found there.
568          */
569         llog_sync(ctxt, NULL);
570
571         /*
572          * Start recovery in separate thread.
573          */
574         cfs_mutex_lock(&ctxt->loc_mutex);
575         ctxt->loc_gen = *gen;
576         rc = llog_recov_thread_replay(ctxt, ctxt->llog_proc_cb, logid);
577         cfs_mutex_unlock(&ctxt->loc_mutex);
578
579         RETURN(rc);
580 }
581 EXPORT_SYMBOL(llog_obd_repl_connect);
582
583 /**
584  * Deleted objects have a commit callback that cancels the MDS
585  * log record for the deletion. The commit callback calls this
586  * function.
587  */
588 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
589                          struct lov_stripe_md *lsm, int count,
590                          struct llog_cookie *cookies, int flags)
591 {
592         struct llog_commit_master *lcm;
593         struct llog_canceld_ctxt *llcd;
594         int rc = 0;
595         ENTRY;
596
597         LASSERT(ctxt != NULL);
598
599         cfs_mutex_lock(&ctxt->loc_mutex);
600         if (!ctxt->loc_lcm) {
601                 CDEBUG(D_RPCTRACE, "No lcm for ctxt %p\n", ctxt);
602                 GOTO(out, rc = -ENODEV);
603         }
604         lcm = ctxt->loc_lcm;
605         CDEBUG(D_INFO, "cancel on lsm %p\n", lcm);
606
607         /*
608          * Let's check if we have all structures alive. We also check for
609          * possible shutdown. Do nothing if we're stopping.
610          */
611         if (ctxt->loc_imp == NULL) {
612                 CDEBUG(D_RPCTRACE, "No import for ctxt %p\n", ctxt);
613                 GOTO(out, rc = -ENODEV);
614         }
615
616         if (cfs_test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
617                 CDEBUG(D_RPCTRACE, "Commit thread is stopping for ctxt %p\n",
618                        ctxt);
619                 GOTO(out, rc = -ENODEV);
620         }
621
622         llcd = ctxt->loc_llcd;
623
624         if (count > 0 && cookies != NULL) {
625                 /*
626                  * Get new llcd from ctxt if required.
627                  */
628                 if (!llcd) {
629                         llcd = llcd_get(ctxt);
630                         if (!llcd)
631                                 GOTO(out, rc = -ENOMEM);
632                         /*
633                          * Allocation is successful, let's check for stop
634                          * flag again to fall back as soon as possible.
635                          */
636                         if (cfs_test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
637                                 GOTO(out, rc = -ENODEV);
638                 }
639
640                 /*
641                  * Llcd does not have enough room for @cookies. Let's push
642                  * it out and allocate new one.
643                  */
644                 if (!llcd_fit(llcd, cookies)) {
645                         rc = llcd_push(ctxt);
646                         if (rc)
647                                 GOTO(out, rc);
648                         llcd = llcd_get(ctxt);
649                         if (!llcd)
650                                 GOTO(out, rc = -ENOMEM);
651                         /*
652                          * Allocation is successful, let's check for stop
653                          * flag again to fall back as soon as possible.
654                          */
655                         if (cfs_test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
656                                 GOTO(out, rc = -ENODEV);
657                 }
658
659                 /*
660                  * Copy cookies to @llcd, no matter old or new allocated
661                  * one.
662                  */
663                 llcd_copy(llcd, cookies);
664         }
665
666         /*
667          * Let's check if we need to send copied @cookies asap. If yes
668          * then do it.
669          */
670         if (llcd && (flags & OBD_LLOG_FL_SENDNOW)) {
671                 CDEBUG(D_RPCTRACE, "Sync llcd %p\n", llcd);
672                 rc = llcd_push(ctxt);
673                 if (rc)
674                         GOTO(out, rc);
675         }
676         EXIT;
677 out:
678         if (rc)
679                 llcd_put(ctxt);
680         cfs_mutex_unlock(&ctxt->loc_mutex);
681         return rc;
682 }
683 EXPORT_SYMBOL(llog_obd_repl_cancel);
684
685 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
686 {
687         int rc = 0;
688         ENTRY;
689
690         /*
691          * Flush any remaining llcd.
692          */
693         cfs_mutex_lock(&ctxt->loc_mutex);
694         if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
695                 /*
696                  * This is ost->mds connection, we can't be sure that mds
697                  * can still receive cookies, let's killed the cached llcd.
698                  */
699                 CDEBUG(D_RPCTRACE, "Kill cached llcd\n");
700                 llcd_put(ctxt);
701                 cfs_mutex_unlock(&ctxt->loc_mutex);
702         } else {
703                 /*
704                  * This is either llog_sync() from generic llog code or sync
705                  * on client disconnect. In either way let's do it and send
706                  * llcds to the target with waiting for completion.
707                  */
708                 CDEBUG(D_RPCTRACE, "Sync cached llcd\n");
709                 cfs_mutex_unlock(&ctxt->loc_mutex);
710                 rc = llog_cancel(ctxt, NULL, 0, NULL, OBD_LLOG_FL_SENDNOW);
711         }
712         RETURN(rc);
713 }
714 EXPORT_SYMBOL(llog_obd_repl_sync);
715
716 #else /* !__KERNEL__ */
717
718 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
719                          struct lov_stripe_md *lsm, int count,
720                          struct llog_cookie *cookies, int flags)
721 {
722         return 0;
723 }
724 #endif
725
726 /**
727  * Module init time fucntion. Initializes slab for llcd objects.
728  */
729 int llog_recov_init(void)
730 {
731         int llcd_size;
732
733         llcd_size = CFS_PAGE_SIZE -
734                 lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
735         llcd_size += offsetof(struct llog_canceld_ctxt, llcd_cookies);
736         llcd_cache = cfs_mem_cache_create("llcd_cache", llcd_size, 0, 0);
737         if (!llcd_cache) {
738                 CERROR("Error allocating llcd cache\n");
739                 return -ENOMEM;
740         }
741         return 0;
742 }
743
744 /**
745  * Module fini time fucntion. Releases slab for llcd objects.
746  */
747 void llog_recov_fini(void)
748 {
749         /*
750          * Kill llcd cache when thread is stopped and we're sure no
751          * llcd in use left.
752          */
753         if (llcd_cache) {
754                 /*
755                  * In 2.6.22 cfs_mem_cache_destroy() will not return error
756                  * for busy resources. Let's check it another way.
757                  */
758                 LASSERTF(cfs_atomic_read(&llcd_count) == 0,
759                          "Can't destroy llcd cache! Number of "
760                          "busy llcds: %d\n", cfs_atomic_read(&llcd_count));
761                 cfs_mem_cache_destroy(llcd_cache);
762                 llcd_cache = NULL;
763         }
764 }