Whamcloud - gitweb
b=21587 don't LBUG if transno has changed during replay
[fs/lustre-release.git] / lustre / ptlrpc / recov_thread.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/recov_thread.c
37  *
38  * OST<->MDS recovery logging thread.
39  * Invariants in implementation:
40  * - we do not share logs among different OST<->MDS connections, so that
41  *   if an OST or MDS fails it need only look at log(s) relevant to itself
42  *
43  * Author: Andreas Dilger   <adilger@clusterfs.com>
44  *         Yury Umanets     <yury.umanets@sun.com>
45  *         Alexey Lyashkov  <alexey.lyashkov@sun.com>
46  */
47
48 #define DEBUG_SUBSYSTEM S_LOG
49
50 #ifndef EXPORT_SYMTAB
51 # define EXPORT_SYMTAB
52 #endif
53
54 #ifdef __KERNEL__
55 # include <libcfs/libcfs.h>
56 #else
57 # include <libcfs/list.h>
58 # include <liblustre.h>
59 #endif
60
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <obd_class.h>
64 #include <lustre_net.h>
65 #include <lnet/types.h>
66 #include <libcfs/list.h>
67 #include <lustre_log.h>
68 #include "ptlrpc_internal.h"
69
70 static atomic_t                   llcd_count = ATOMIC_INIT(0);
71 static cfs_mem_cache_t           *llcd_cache = NULL;
72
73 #ifdef __KERNEL__
74 enum {
75         LLOG_LCM_FL_START       = 1 << 0,
76         LLOG_LCM_FL_EXIT        = 1 << 1
77 };
78
79 static void llcd_print(struct llog_canceld_ctxt *llcd,
80                        const char *func, int line)
81 {
82         CDEBUG(D_RPCTRACE, "Llcd (%p) at %s:%d:\n", llcd, func, line);
83         CDEBUG(D_RPCTRACE, "  size: %d\n", llcd->llcd_size);
84         CDEBUG(D_RPCTRACE, "  ctxt: %p\n", llcd->llcd_ctxt);
85         CDEBUG(D_RPCTRACE, "  lcm : %p\n", llcd->llcd_lcm);
86         CDEBUG(D_RPCTRACE, "  cookiebytes : %d\n", llcd->llcd_cookiebytes);
87 }
88
89 /**
90  * Allocate new llcd from cache, init it and return to caller.
91  * Bumps number of objects allocated.
92  */
93 static struct llog_canceld_ctxt *llcd_alloc(struct llog_commit_master *lcm)
94 {
95         struct llog_canceld_ctxt *llcd;
96         int size, overhead;
97
98         LASSERT(lcm != NULL);
99
100         /*
101          * We want to send one page of cookies with rpc header. This buffer
102          * will be assigned later to the rpc, this is why we preserve the
103          * space for rpc header.
104          */
105         size = CFS_PAGE_SIZE - lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
106         overhead =  offsetof(struct llog_canceld_ctxt, llcd_cookies);
107         OBD_SLAB_ALLOC(llcd, llcd_cache, CFS_ALLOC_STD, size + overhead);
108         if (!llcd)
109                 return NULL;
110
111         CFS_INIT_LIST_HEAD(&llcd->llcd_list);
112         llcd->llcd_cookiebytes = 0;
113         llcd->llcd_size = size;
114
115         spin_lock(&lcm->lcm_lock);
116         llcd->llcd_lcm = lcm;
117         atomic_inc(&lcm->lcm_count);
118         list_add_tail(&llcd->llcd_list, &lcm->lcm_llcds);
119         spin_unlock(&lcm->lcm_lock);
120         atomic_inc(&llcd_count);
121
122         CDEBUG(D_RPCTRACE, "Alloc llcd %p on lcm %p (%d)\n",
123                llcd, lcm, atomic_read(&lcm->lcm_count));
124
125         return llcd;
126 }
127
128 /**
129  * Returns passed llcd to cache.
130  */
131 static void llcd_free(struct llog_canceld_ctxt *llcd)
132 {
133         struct llog_commit_master *lcm = llcd->llcd_lcm;
134         int size;
135
136         if (lcm) {
137                 if (atomic_read(&lcm->lcm_count) == 0) {
138                         CERROR("Invalid llcd free %p\n", llcd);
139                         llcd_print(llcd, __FUNCTION__, __LINE__);
140                         LBUG();
141                 }
142                 spin_lock(&lcm->lcm_lock);
143                 LASSERT(!list_empty(&llcd->llcd_list));
144                 list_del_init(&llcd->llcd_list);
145                 atomic_dec(&lcm->lcm_count);
146                 spin_unlock(&lcm->lcm_lock);
147
148                 CDEBUG(D_RPCTRACE, "Free llcd %p on lcm %p (%d)\n",
149                        llcd, lcm, atomic_read(&lcm->lcm_count));
150         }
151
152         LASSERT(atomic_read(&llcd_count) > 0);
153         atomic_dec(&llcd_count);
154
155         size = offsetof(struct llog_canceld_ctxt, llcd_cookies) +
156             llcd->llcd_size;
157         OBD_SLAB_FREE(llcd, llcd_cache, size);
158 }
159
160 /**
161  * Checks if passed cookie fits into llcd free space buffer. Returns
162  * 1 if yes and 0 otherwise.
163  */
164 static inline int
165 llcd_fit(struct llog_canceld_ctxt *llcd, struct llog_cookie *cookies)
166 {
167         return (llcd->llcd_size - llcd->llcd_cookiebytes >= sizeof(*cookies));
168 }
169
170 /**
171  * Copy passed @cookies to @llcd.
172  */
173 static inline void
174 llcd_copy(struct llog_canceld_ctxt *llcd, struct llog_cookie *cookies)
175 {
176         LASSERT(llcd_fit(llcd, cookies));
177         memcpy((char *)llcd->llcd_cookies + llcd->llcd_cookiebytes,
178               cookies, sizeof(*cookies));
179         llcd->llcd_cookiebytes += sizeof(*cookies);
180 }
181
182 /**
183  * Llcd completion function. Called uppon llcd send finish regardless
184  * sending result. Error is passed in @rc. Note, that this will be called
185  * in cleanup time when all inflight rpcs aborted.
186  */
187 static int
188 llcd_interpret(struct ptlrpc_request *req, void *noused, int rc)
189 {
190         struct llog_canceld_ctxt *llcd = req->rq_async_args.pointer_arg[0];
191         CDEBUG(D_RPCTRACE, "Sent llcd %p (%d) - killing it\n", llcd, rc);
192         llcd_free(llcd);
193         return 0;
194 }
195
196 /**
197  * Send @llcd to remote node. Free llcd uppon completion or error. Sending
198  * is performed in async style so this function will return asap without
199  * blocking.
200  */
201 static int llcd_send(struct llog_canceld_ctxt *llcd)
202 {
203         int size[2] = { sizeof(struct ptlrpc_body),
204                         llcd->llcd_cookiebytes };
205         char *bufs[2] = { NULL, (char *)llcd->llcd_cookies };
206         struct obd_import *import = NULL;
207         struct llog_commit_master *lcm;
208         struct ptlrpc_request *req;
209         struct llog_ctxt *ctxt;
210         int rc;
211         ENTRY;
212
213         ctxt = llcd->llcd_ctxt;
214         if (!ctxt) {
215                 CERROR("Invalid llcd with NULL ctxt found (%p)\n",
216                        llcd);
217                 llcd_print(llcd, __FUNCTION__, __LINE__);
218                 LBUG();
219         }
220         LASSERT_SEM_LOCKED(&ctxt->loc_sem);
221
222         if (llcd->llcd_cookiebytes == 0)
223                 GOTO(exit, rc = 0);
224
225         lcm = llcd->llcd_lcm;
226
227         /*
228          * Check if we're in exit stage. Do not send llcd in
229          * this case.
230          */
231         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
232                 GOTO(exit, rc = -ENODEV);
233
234         CDEBUG(D_RPCTRACE, "Sending llcd %p\n", llcd);
235
236         import = llcd->llcd_ctxt->loc_imp;
237         if (!import || (import == LP_POISON) ||
238             (import->imp_client == LP_POISON)) {
239                 CERROR("Invalid import %p for llcd %p\n",
240                        import, llcd);
241                 GOTO(exit, rc = -ENODEV);
242         }
243
244         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_RECOV, 10);
245
246         /*
247          * No need to get import here as it is already done in
248          * llog_receptor_accept().
249          */
250         req = ptlrpc_prep_req(import, LUSTRE_LOG_VERSION,
251                               OBD_LOG_CANCEL, 2, size, bufs);
252         if (req == NULL) {
253                 CERROR("Can't allocate request for sending llcd %p\n",
254                        llcd);
255                 GOTO(exit, rc = -ENOMEM);
256         }
257
258         /*
259          * Check if we're in exit stage again. Do not send llcd in
260          * this case.
261          */
262         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
263                 ptlrpc_req_finished(req);
264                 GOTO(exit, rc = -ENODEV);
265         }
266
267         /* bug 5515 */
268         req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
269         req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
270         ptlrpc_req_set_repsize(req, 1, NULL);
271         ptlrpc_at_set_req_timeout(req);
272         req->rq_interpret_reply = llcd_interpret;
273         req->rq_async_args.pointer_arg[0] = llcd;
274
275         /* llog cancels will be replayed after reconnect so this will do twice
276          * first from replay llog, second for resended rpc */
277         req->rq_no_delay = req->rq_no_resend = 1;
278
279         rc = ptlrpc_set_add_new_req(&lcm->lcm_pc, req);
280         if (rc) {
281                 ptlrpc_req_finished(req);
282                 GOTO(exit, rc);
283         }
284         RETURN(rc);
285 exit:
286         CDEBUG(D_RPCTRACE, "Refused llcd %p\n", llcd);
287         llcd_free(llcd);
288         return rc;
289 }
290
291 /**
292  * Attach @llcd to @ctxt. Establish llcd vs. ctxt reserve connection
293  * so hat they can refer each other.
294  */
295 static int
296 llcd_attach(struct llog_ctxt *ctxt, struct llog_canceld_ctxt *llcd)
297 {
298         LASSERT(ctxt != NULL && llcd != NULL);
299         LASSERT_SEM_LOCKED(&ctxt->loc_sem);
300         LASSERT(ctxt->loc_llcd == NULL);
301         llcd->llcd_ctxt = llog_ctxt_get(ctxt);
302         ctxt->loc_llcd = llcd;
303
304         CDEBUG(D_RPCTRACE, "Attach llcd %p to ctxt %p\n",
305                llcd, ctxt);
306
307         return 0;
308 }
309
310 /**
311  * Opposite to llcd_attach(). Detaches llcd from its @ctxt. This makes
312  * sure that this llcd will not be found another time we try to cancel.
313  */
314 static struct llog_canceld_ctxt *llcd_detach(struct llog_ctxt *ctxt)
315 {
316         struct llog_canceld_ctxt *llcd;
317
318         LASSERT(ctxt != NULL);
319         LASSERT_SEM_LOCKED(&ctxt->loc_sem);
320
321         llcd = ctxt->loc_llcd;
322         if (!llcd)
323                 return NULL;
324
325         CDEBUG(D_RPCTRACE, "Detach llcd %p from ctxt %p\n",
326                llcd, ctxt);
327
328         ctxt->loc_llcd = NULL;
329         llog_ctxt_put(ctxt);
330         return llcd;
331 }
332
333 /**
334  * Return @llcd cached in @ctxt. Allocate new one if required. Attach it
335  * to ctxt so that it may be used for gathering cookies and sending.
336  */
337 static struct llog_canceld_ctxt *llcd_get(struct llog_ctxt *ctxt)
338 {
339         struct llog_canceld_ctxt *llcd;
340
341         llcd = llcd_alloc(ctxt->loc_lcm);
342         if (!llcd) {
343                 CERROR("Can't alloc an llcd for ctxt %p\n", ctxt);
344                 return NULL;
345         }
346         llcd_attach(ctxt, llcd);
347         return llcd;
348 }
349
350 /**
351  * Deatch llcd from its @ctxt. Free llcd.
352  */
353 static void llcd_put(struct llog_ctxt *ctxt)
354 {
355         struct llog_canceld_ctxt *llcd;
356
357         llcd = llcd_detach(ctxt);
358         if (llcd)
359                 llcd_free(llcd);
360 }
361
362 /**
363  * Detach llcd from its @ctxt so that nobody will find it with try to
364  * re-use. Send llcd to remote node.
365  */
366 static int llcd_push(struct llog_ctxt *ctxt)
367 {
368         struct llog_canceld_ctxt *llcd;
369         int rc;
370
371         /*
372          * Make sure that this llcd will not be sent again as we detach
373          * it from ctxt.
374          */
375         llcd = llcd_detach(ctxt);
376         if (!llcd) {
377                 CERROR("Invalid detached llcd found %p\n", llcd);
378                 llcd_print(llcd, __FUNCTION__, __LINE__);
379                 LBUG();
380         }
381
382         rc = llcd_send(llcd);
383         if (rc)
384                 CERROR("Couldn't send llcd %p (%d)\n", llcd, rc);
385         return rc;
386 }
387
388 /**
389  * Start recovery thread which actually deals llcd sending. This
390  * is all ptlrpc standard thread based so there is not much of work
391  * to do.
392  */
393 int llog_recov_thread_start(struct llog_commit_master *lcm)
394 {
395         int rc;
396         ENTRY;
397
398         rc = ptlrpcd_start(lcm->lcm_name, &lcm->lcm_pc);
399         if (rc) {
400                 CERROR("Error %d while starting recovery thread %s\n",
401                        rc, lcm->lcm_name);
402                 RETURN(rc);
403         }
404         RETURN(rc);
405 }
406 EXPORT_SYMBOL(llog_recov_thread_start);
407
408 /**
409  * Stop recovery thread. Complement to llog_recov_thread_start().
410  */
411 void llog_recov_thread_stop(struct llog_commit_master *lcm, int force)
412 {
413         ENTRY;
414
415         /*
416          * Let all know that we're stopping. This will also make
417          * llcd_send() refuse any new llcds.
418          */
419         set_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags);
420
421         /*
422          * Stop processing thread. No new rpcs will be accepted for
423          * for processing now.
424          */
425         ptlrpcd_stop(&lcm->lcm_pc, force);
426
427         /*
428          * By this point no alive inflight llcds should be left. Only
429          * those forgotten in sync may still be attached to ctxt. Let's
430          * print them.
431          */
432         if (atomic_read(&lcm->lcm_count) != 0) {
433                 struct llog_canceld_ctxt *llcd;
434                 struct list_head         *tmp;
435
436                 CERROR("Busy llcds found (%d) on lcm %p\n",
437                        atomic_read(&lcm->lcm_count) == 0, lcm);
438
439                 spin_lock(&lcm->lcm_lock);
440                 list_for_each(tmp, &lcm->lcm_llcds) {
441                         llcd = list_entry(tmp, struct llog_canceld_ctxt,
442                                           llcd_list);
443                         llcd_print(llcd, __FUNCTION__, __LINE__);
444                 }
445                 spin_unlock(&lcm->lcm_lock);
446
447                 /*
448                  * No point to go further with busy llcds at this point
449                  * as this is clear bug. It might mean we got hanging
450                  * rpc which holds import ref and this means we will not
451                  * be able to cleanup anyways.
452                  *
453                  * Or we just missed to kill them when they were not
454                  * attached to ctxt. In this case our slab will remind
455                  * us about this a bit later.
456                  */
457                 LBUG();
458         }
459         EXIT;
460 }
461 EXPORT_SYMBOL(llog_recov_thread_stop);
462
463 /**
464  * Initialize commit master structure and start recovery thread on it.
465  */
466 struct llog_commit_master *llog_recov_thread_init(char *name)
467 {
468         struct llog_commit_master *lcm;
469         int rc;
470         ENTRY;
471
472         OBD_ALLOC_PTR(lcm);
473         if (!lcm)
474                 RETURN(NULL);
475
476         /*
477          * Try to create threads with unique names.
478          */
479         snprintf(lcm->lcm_name, sizeof(lcm->lcm_name),
480                  "lcm_%s", name);
481
482         atomic_set(&lcm->lcm_count, 0);
483         spin_lock_init(&lcm->lcm_lock);
484         CFS_INIT_LIST_HEAD(&lcm->lcm_llcds);
485         rc = llog_recov_thread_start(lcm);
486         if (rc) {
487                 CERROR("Can't start commit thread, rc %d\n", rc);
488                 GOTO(out, rc);
489         }
490         RETURN(lcm);
491 out:
492         OBD_FREE_PTR(lcm);
493         return NULL;
494 }
495 EXPORT_SYMBOL(llog_recov_thread_init);
496
497 /**
498  * Finalize commit master and its recovery thread.
499  */
500 void llog_recov_thread_fini(struct llog_commit_master *lcm, int force)
501 {
502         ENTRY;
503         llog_recov_thread_stop(lcm, force);
504         OBD_FREE_PTR(lcm);
505         EXIT;
506 }
507 EXPORT_SYMBOL(llog_recov_thread_fini);
508
509 static int llog_recov_thread_replay(struct llog_ctxt *ctxt,
510                                     void *cb, void *arg)
511 {
512         struct obd_device *obd = ctxt->loc_obd;
513         struct llog_process_cat_args *lpca;
514         int rc;
515         ENTRY;
516
517         if (obd->obd_stopping)
518                 RETURN(-ENODEV);
519
520         /*
521          * This will be balanced in llog_cat_process_thread()
522          */
523         OBD_ALLOC_PTR(lpca);
524         if (!lpca)
525                 RETURN(-ENOMEM);
526
527         lpca->lpca_cb = cb;
528         lpca->lpca_arg = arg;
529
530         /*
531          * This will be balanced in llog_cat_process_thread()
532          */
533         lpca->lpca_ctxt = llog_ctxt_get(ctxt);
534         if (!lpca->lpca_ctxt) {
535                 OBD_FREE_PTR(lpca);
536                 RETURN(-ENODEV);
537         }
538         rc = cfs_kernel_thread(llog_cat_process_thread, lpca,
539                                CLONE_VM | CLONE_FILES);
540         if (rc < 0) {
541                 CERROR("Error starting llog_cat_process_thread(): %d\n", rc);
542                 OBD_FREE_PTR(lpca);
543                 llog_ctxt_put(ctxt);
544         } else {
545                 CDEBUG(D_HA, "Started llog_cat_process_thread(): %d\n", rc);
546                 rc = 0;
547         }
548
549         RETURN(rc);
550 }
551
552 int llog_obd_repl_connect(struct llog_ctxt *ctxt,
553                           struct llog_logid *logid, struct llog_gen *gen,
554                           struct obd_uuid *uuid)
555 {
556         int rc;
557         ENTRY;
558
559         /*
560          * Send back cached llcd from llog before recovery if we have any.
561          * This is void is nothing cached is found there.
562          */
563         llog_sync(ctxt, NULL);
564
565         /*
566          * Start recovery in separate thread.
567          */
568         mutex_down(&ctxt->loc_sem);
569         ctxt->loc_gen = *gen;
570         rc = llog_recov_thread_replay(ctxt, ctxt->llog_proc_cb, logid);
571         mutex_up(&ctxt->loc_sem);
572
573         RETURN(rc);
574 }
575 EXPORT_SYMBOL(llog_obd_repl_connect);
576
577 /**
578  * Deleted objects have a commit callback that cancels the MDS
579  * log record for the deletion. The commit callback calls this
580  * function.
581  */
582 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
583                          struct lov_stripe_md *lsm, int count,
584                          struct llog_cookie *cookies, int flags)
585 {
586         struct llog_commit_master *lcm;
587         struct llog_canceld_ctxt *llcd;
588         int rc = 0;
589         ENTRY;
590
591         LASSERT(ctxt != NULL);
592
593         mutex_down(&ctxt->loc_sem);
594         lcm = ctxt->loc_lcm;
595         CDEBUG(D_INFO, "cancel on lsm %p\n", lcm);
596
597         /*
598          * Let's check if we have all structures alive. We also check for
599          * possible shutdown. Do nothing if we're stopping.
600          */
601         if (ctxt->loc_imp == NULL) {
602                 CDEBUG(D_RPCTRACE, "No import for ctxt %p\n", ctxt);
603                 GOTO(out, rc = -ENODEV);
604         }
605
606         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
607                 CDEBUG(D_RPCTRACE, "Commit thread is stopping for ctxt %p\n",
608                        ctxt);
609                 GOTO(out, rc = -ENODEV);
610         }
611
612         llcd = ctxt->loc_llcd;
613
614         if (count > 0 && cookies != NULL) {
615                 /*
616                  * Get new llcd from ctxt if required.
617                  */
618                 if (!llcd) {
619                         llcd = llcd_get(ctxt);
620                         if (!llcd)
621                                 GOTO(out, rc = -ENOMEM);
622                         /*
623                          * Allocation is successful, let's check for stop
624                          * flag again to fall back as soon as possible.
625                          */
626                         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
627                                 GOTO(out, rc = -ENODEV);
628                 }
629
630                 /*
631                  * Llcd does not have enough room for @cookies. Let's push
632                  * it out and allocate new one.
633                  */
634                 if (!llcd_fit(llcd, cookies)) {
635                         rc = llcd_push(ctxt);
636                         if (rc)
637                                 GOTO(out, rc);
638                         llcd = llcd_get(ctxt);
639                         if (!llcd)
640                                 GOTO(out, rc = -ENOMEM);
641                         /*
642                          * Allocation is successful, let's check for stop
643                          * flag again to fall back as soon as possible.
644                          */
645                         if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
646                                 GOTO(out, rc = -ENODEV);
647                 }
648
649                 /*
650                  * Copy cookies to @llcd, no matter old or new allocated
651                  * one.
652                  */
653                 llcd_copy(llcd, cookies);
654         }
655
656         /*
657          * Let's check if we need to send copied @cookies asap. If yes
658          * then do it.
659          */
660         if (llcd && (flags & OBD_LLOG_FL_SENDNOW)) {
661                 CDEBUG(D_RPCTRACE, "Sync llcd %p\n", llcd);
662                 rc = llcd_push(ctxt);
663                 if (rc)
664                         GOTO(out, rc);
665         }
666         EXIT;
667 out:
668         if (rc)
669                 llcd_put(ctxt);
670         mutex_up(&ctxt->loc_sem);
671         return rc;
672 }
673 EXPORT_SYMBOL(llog_obd_repl_cancel);
674
675 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
676 {
677         int rc = 0;
678         ENTRY;
679
680         /*
681          * Flush any remaining llcd.
682          */
683         mutex_down(&ctxt->loc_sem);
684         if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
685                 /*
686                  * This is ost->mds connection, we can't be sure that mds
687                  * can still receive cookies, let's killed the cached llcd.
688                  */
689                 CDEBUG(D_RPCTRACE, "Kill cached llcd\n");
690                 llcd_put(ctxt);
691                 mutex_up(&ctxt->loc_sem);
692         } else {
693                 /*
694                  * This is either llog_sync() from generic llog code or sync
695                  * on client disconnect. In either way let's do it and send
696                  * llcds to the target with waiting for completion.
697                  */
698                 CDEBUG(D_RPCTRACE, "Sync cached llcd\n");
699                 mutex_up(&ctxt->loc_sem);
700                 rc = llog_cancel(ctxt, NULL, 0, NULL, OBD_LLOG_FL_SENDNOW);
701         }
702         RETURN(rc);
703 }
704 EXPORT_SYMBOL(llog_obd_repl_sync);
705
706 #else /* !__KERNEL__ */
707
708 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
709                          struct lov_stripe_md *lsm, int count,
710                          struct llog_cookie *cookies, int flags)
711 {
712         return 0;
713 }
714 #endif
715
716 /**
717  * Module init time fucntion. Initializes slab for llcd objects.
718  */
719 int llog_recov_init(void)
720 {
721         int llcd_size;
722
723         llcd_size = CFS_PAGE_SIZE -
724                 lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
725         llcd_size += offsetof(struct llog_canceld_ctxt, llcd_cookies);
726         llcd_cache = cfs_mem_cache_create("llcd_cache", llcd_size, 0, 0);
727         if (!llcd_cache) {
728                 CERROR("Error allocating llcd cache\n");
729                 return -ENOMEM;
730         }
731         return 0;
732 }
733
734 /**
735  * Module fini time fucntion. Releases slab for llcd objects.
736  */
737 void llog_recov_fini(void)
738 {
739         /*
740          * Kill llcd cache when thread is stopped and we're sure no
741          * llcd in use left.
742          */
743         if (llcd_cache) {
744                 /*
745                  * In 2.6.22 cfs_mem_cache_destroy() will not return error
746                  * for busy resources. Let's check it another way.
747                  */
748                 LASSERTF(atomic_read(&llcd_count) == 0,
749                          "Can't destroy llcd cache! Number of "
750                          "busy llcds: %d\n", atomic_read(&llcd_count));
751                 cfs_mem_cache_destroy(llcd_cache);
752                 llcd_cache = NULL;
753         }
754 }