Whamcloud - gitweb
- commit from zzeng
[fs/lustre-release.git] / lustre / ldlm / ldlm_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_LDLM
26
27 #ifdef __KERNEL__
28 # include <linux/module.h>
29 #else
30 # include <liblustre.h>
31 #endif
32 #include <linux/obd.h>
33 #include <linux/obd_ost.h> /* for LUSTRE_OSC_NAME */
34 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
35 #include <linux/lustre_mgmt.h>
36 #include <linux/lustre_dlm.h>
37 #include <linux/lustre_net.h>
38 /* @priority: if non-zero, move the selected to the list head
39  * @nocreate: if non-zero, only search in existed connections
40  */
41 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
42                            int priority, int nocreate)
43 {
44         struct ptlrpc_connection *ptlrpc_conn;
45         struct obd_import_conn *imp_conn = NULL, *item;
46         int rc = 0;
47         ENTRY;
48
49         LASSERT(!(nocreate && !priority));
50
51         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
52         if (!ptlrpc_conn) {
53                 CERROR("can't find connection %s\n", uuid->uuid);
54                 RETURN (-EINVAL);
55         }
56
57         if (!nocreate) {
58                 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
59                 if (!imp_conn) {
60                         CERROR("fail to alloc memory\n");
61                         GOTO(out_put, rc = -ENOMEM);
62                 }
63         }
64
65         spin_lock(&imp->imp_lock);
66         list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
67                 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
68                         if (priority) {
69                                 list_del(&item->oic_item);
70                                 list_add(&item->oic_item, &imp->imp_conn_list);
71                                 item->oic_last_attempt = 0;
72                         }
73                         CDEBUG(D_HA, "imp %p@%s: find existed conn %s%s\n",
74                                imp, imp->imp_obd->obd_name, uuid->uuid,
75                                (priority ? ", move to head." : ""));
76                         spin_unlock(&imp->imp_lock);
77                         GOTO(out_free, rc = 0);
78                 }
79         }
80         /* not found */
81         if (!nocreate) {
82                 imp_conn->oic_conn = ptlrpc_conn;
83                 imp_conn->oic_uuid = *uuid;
84                 imp_conn->oic_last_attempt = 0;
85                 if (priority)
86                         list_add(&imp_conn->oic_item, &imp->imp_conn_list);
87                 else
88                         list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
89                 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
90                        imp, imp->imp_obd->obd_name, uuid->uuid,
91                        (priority ? "head" : "tail"));
92         } else
93                 rc = -ENOENT;
94
95         spin_unlock(&imp->imp_lock);
96         RETURN(0);
97 out_free:
98         if (imp_conn)
99                 OBD_FREE(imp_conn, sizeof(*imp_conn));
100 out_put:
101         ptlrpc_put_connection(ptlrpc_conn);
102         RETURN(rc);
103 }
104
105 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
106 {
107         return import_set_conn(imp, uuid, 1, 1);
108 }
109
110 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
111                            int priority)
112 {
113         return import_set_conn(imp, uuid, priority, 0);
114 }
115
116 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
117 {
118         struct obd_import_conn *imp_conn;
119         struct obd_export *dlmexp;
120         int rc = -ENOENT;
121         ENTRY;
122
123         spin_lock(&imp->imp_lock);
124         if (list_empty(&imp->imp_conn_list)) {
125                 LASSERT(!imp->imp_conn_current);
126                 LASSERT(!imp->imp_connection);
127                 GOTO(out, rc);
128         }
129
130         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
131                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
132                         continue;
133                 LASSERT(imp_conn->oic_conn);
134
135                 /* is current conn? */
136                 if (imp_conn == imp->imp_conn_current) {
137                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
138
139                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
140                             imp->imp_state != LUSTRE_IMP_DISCON) {
141                                 CERROR("can't remove current connection\n");
142                                 GOTO(out, rc = -EBUSY);
143                         }
144
145                         ptlrpc_put_connection(imp->imp_connection);
146                         imp->imp_connection = NULL;
147
148                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
149                         if (dlmexp && dlmexp->exp_connection) {
150                                 LASSERT(dlmexp->exp_connection ==
151                                         imp_conn->oic_conn);
152                                 ptlrpc_put_connection(dlmexp->exp_connection);
153                                 dlmexp->exp_connection = NULL;
154                         }
155                 }
156
157                 list_del(&imp_conn->oic_item);
158                 ptlrpc_put_connection(imp_conn->oic_conn);
159                 OBD_FREE(imp_conn, sizeof(*imp_conn));
160                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
161                        imp, imp->imp_obd->obd_name, uuid->uuid);
162                 rc = 0;
163                 break;
164         }
165 out:
166         spin_unlock(&imp->imp_lock);
167         if (rc == -ENOENT)
168                 CERROR("connection %s not found\n", uuid->uuid);
169         RETURN(rc);
170 }
171
172 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
173 {
174         struct lustre_cfg* lcfg = buf;
175         struct client_obd *cli = &obddev->u.cli;
176         struct obd_import *imp;
177         struct obd_uuid server_uuid;
178         int rq_portal, rp_portal, connect_op;
179         char *name = obddev->obd_type->typ_name;
180         char *mgmt_name = NULL;
181         int rc;
182         struct obd_device *mgmt_obd;
183         mgmtcli_register_for_events_t register_f;
184         ENTRY;
185
186         /* In a more perfect world, we would hang a ptlrpc_client off of
187          * obd_type and just use the values from there. */
188         if (!strcmp(name, LUSTRE_OSC_NAME)) {
189                 rq_portal = OST_REQUEST_PORTAL;
190                 rp_portal = OSC_REPLY_PORTAL;
191                 connect_op = OST_CONNECT;
192         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
193                 rq_portal = MDS_REQUEST_PORTAL;
194                 rp_portal = MDC_REPLY_PORTAL;
195                 connect_op = MDS_CONNECT;
196         } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
197                 rq_portal = MGMT_REQUEST_PORTAL;
198                 rp_portal = MGMT_REPLY_PORTAL;
199                 connect_op = MGMT_CONNECT;
200         } else {
201                 CERROR("unknown client OBD type \"%s\", can't setup\n",
202                        name);
203                 RETURN(-EINVAL);
204         }
205
206         if (lcfg->lcfg_inllen1 < 1) {
207                 CERROR("requires a TARGET UUID\n");
208                 RETURN(-EINVAL);
209         }
210
211         if (lcfg->lcfg_inllen1 > 37) {
212                 CERROR("client UUID must be less than 38 characters\n");
213                 RETURN(-EINVAL);
214         }
215
216         if (lcfg->lcfg_inllen2 < 1) {
217                 CERROR("setup requires a SERVER UUID\n");
218                 RETURN(-EINVAL);
219         }
220
221         if (lcfg->lcfg_inllen2 > 37) {
222                 CERROR("target UUID must be less than 38 characters\n");
223                 RETURN(-EINVAL);
224         }
225
226         sema_init(&cli->cl_sem, 1);
227         cli->cl_conn_count = 0;
228         memcpy(server_uuid.uuid, lcfg->lcfg_inlbuf2,
229                min_t(unsigned int, lcfg->lcfg_inllen2, sizeof(server_uuid)));
230
231         cli->cl_dirty = 0;
232         cli->cl_avail_grant = 0;
233         /* FIXME: should limit this for the sum of all cl_dirty_max */
234         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
235         if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
236                 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
237         INIT_LIST_HEAD(&cli->cl_cache_waiters);
238         INIT_LIST_HEAD(&cli->cl_loi_ready_list);
239         INIT_LIST_HEAD(&cli->cl_loi_write_list);
240         INIT_LIST_HEAD(&cli->cl_loi_read_list);
241         spin_lock_init(&cli->cl_loi_list_lock);
242         cli->cl_r_in_flight = 0;
243         cli->cl_w_in_flight = 0;
244         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
245         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
246         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
247         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
248         
249         if (num_physpages <= 32768) { /* <= 128 MB */
250                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 3;
251                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 3;
252         } else {
253                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
254                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
255         }
256
257
258         rc = ldlm_get_ref();
259         if (rc) {
260                 CERROR("ldlm_get_ref failed: %d\n", rc);
261                 GOTO(err, rc);
262         }
263
264         ptlrpc_init_client(rq_portal, rp_portal, name,
265                            &obddev->obd_ldlm_client);
266
267         imp = class_new_import();
268         if (imp == NULL) 
269                 GOTO(err_ldlm, rc = -ENOENT);
270         imp->imp_client = &obddev->obd_ldlm_client;
271         imp->imp_obd = obddev;
272         imp->imp_connect_op = connect_op;
273         imp->imp_generation = 0;
274         imp->imp_initial_recov = 1;
275         INIT_LIST_HEAD(&imp->imp_pinger_chain);
276         memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
277               lcfg->lcfg_inllen1);
278         class_import_put(imp);
279
280         rc = client_import_add_conn(imp, &server_uuid, 1);
281         if (rc) {
282                 CERROR("can't add initial connection\n");
283                 GOTO(err_import, rc);
284         }
285
286         cli->cl_import = imp;
287         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
288         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
289         cli->cl_sandev = to_kdev_t(0);
290
291         if (lcfg->lcfg_inllen3 != 0) {
292                 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
293                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
294                                name, obddev->obd_name,
295                                imp->imp_target_uuid.uuid);
296                         imp->imp_invalid = 1;
297
298                         if (lcfg->lcfg_inllen4 != 0)
299                                 mgmt_name = lcfg->lcfg_inlbuf4;
300                 } else {
301                         mgmt_name = lcfg->lcfg_inlbuf3;
302                 }
303         }
304
305         if (mgmt_name != NULL) {
306                 /* Register with management client if we need to. */
307                 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
308                        obddev->obd_name, mgmt_name, server_uuid.uuid);
309
310                 mgmt_obd = class_name2obd(mgmt_name);
311                 if (!mgmt_obd) {
312                         CERROR("can't find mgmtcli %s to register\n",
313                                mgmt_name);
314                         GOTO(err_import, rc = -ENOSYS);
315                 }
316
317                 register_f = inter_module_get("mgmtcli_register_for_events");
318                 if (!register_f) {
319                         CERROR("can't i_m_g mgmtcli_register_for_events\n");
320                         GOTO(err_import, rc = -ENOSYS);
321                 }
322
323                 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
324                 inter_module_put("mgmtcli_register_for_events");
325
326                 if (!rc)
327                         cli->cl_mgmtcli_obd = mgmt_obd;
328         }
329
330         RETURN(rc);
331
332 err_import:
333         class_destroy_import(imp);
334 err_ldlm:
335         ldlm_put_ref(0);
336 err:
337         RETURN(rc);
338
339 }
340
341 int client_obd_cleanup(struct obd_device *obddev, int flags)
342 {
343         struct client_obd *cli = &obddev->u.cli;
344
345         if (!cli->cl_import)
346                 RETURN(-EINVAL);
347         if (cli->cl_mgmtcli_obd) {
348                 mgmtcli_deregister_for_events_t dereg_f;
349
350                 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
351                 dereg_f(cli->cl_mgmtcli_obd, obddev);
352                 inter_module_put("mgmtcli_deregister_for_events");
353         }
354         class_destroy_import(cli->cl_import);
355         cli->cl_import = NULL;
356
357         ldlm_put_ref(flags & OBD_OPT_FORCE);
358
359         RETURN(0);
360 }
361
362 int client_connect_import(struct lustre_handle *dlm_handle,
363                           struct obd_device *obd,
364                           struct obd_uuid *cluuid,
365                           unsigned long connect_flags)
366 {
367         struct client_obd *cli = &obd->u.cli;
368         struct obd_import *imp = cli->cl_import;
369         struct obd_export *exp;
370         int rc;
371         ENTRY;
372
373         down(&cli->cl_sem);
374         rc = class_connect(dlm_handle, obd, cluuid);
375         if (rc)
376                 GOTO(out_sem, rc);
377
378         cli->cl_conn_count++;
379         if (cli->cl_conn_count > 1)
380                 GOTO(out_sem, rc);
381         exp = class_conn2export(dlm_handle);
382
383         if (obd->obd_namespace != NULL)
384                 CERROR("already have namespace!\n");
385         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
386                                                 LDLM_NAMESPACE_CLIENT);
387         if (obd->obd_namespace == NULL)
388                 GOTO(out_disco, rc = -ENOMEM);
389
390         imp->imp_dlm_handle = *dlm_handle;
391         rc = ptlrpc_init_import(imp);
392         if (rc != 0) 
393                 GOTO(out_ldlm, rc);
394
395         imp->imp_connect_flags = connect_flags;
396         rc = ptlrpc_connect_import(imp, NULL);
397         if (rc != 0) {
398                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
399                 GOTO(out_ldlm, rc);
400         }
401         LASSERT(exp->exp_connection);
402         ptlrpc_pinger_add_import(imp);
403         EXIT;
404
405         if (rc) {
406 out_ldlm:
407                 ldlm_namespace_free(obd->obd_namespace, 0);
408                 obd->obd_namespace = NULL;
409 out_disco:
410                 cli->cl_conn_count--;
411                 class_disconnect(exp, 0);
412         } else {
413                 class_export_put(exp);
414         }
415 out_sem:
416         up(&cli->cl_sem);
417         return rc;
418 }
419
420 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
421 {
422         struct obd_device *obd = class_exp2obd(exp);
423         struct client_obd *cli = &obd->u.cli;
424         struct obd_import *imp = cli->cl_import;
425         int rc = 0, err;
426         ENTRY;
427
428         if (!obd) {
429                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
430                        exp, exp ? exp->exp_handle.h_cookie : -1);
431                 RETURN(-EINVAL);
432         }
433
434         down(&cli->cl_sem);
435         if (!cli->cl_conn_count) {
436                 CERROR("disconnecting disconnected device (%s)\n",
437                        obd->obd_name);
438                 GOTO(out_sem, rc = -EINVAL);
439         }
440
441         cli->cl_conn_count--;
442         if (cli->cl_conn_count)
443                 GOTO(out_no_disconnect, rc = 0);
444
445         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
446          * delete it regardless.  (It's safe to delete an import that was
447          * never added.) */
448         (void)ptlrpc_pinger_del_import(imp);
449
450         if (obd->obd_namespace != NULL) {
451                 /* obd_no_recov == local only */
452                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
453                                        obd->obd_no_recov, NULL);
454                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
455                 obd->obd_namespace = NULL;
456         }
457
458         /* 
459          * Yeah, obd_no_recov also (mainly) means "forced shutdown".
460          */
461         if (obd->obd_no_recov)
462                 ptlrpc_invalidate_import(imp, 0);
463         else
464                 rc = ptlrpc_disconnect_import(imp);
465
466         EXIT;
467  out_no_disconnect:
468         err = class_disconnect(exp, 0);
469         if (!rc && err)
470                 rc = err;
471  out_sem:
472         up(&cli->cl_sem);
473         RETURN(rc);
474 }
475
476 /* --------------------------------------------------------------------------
477  * from old lib/target.c
478  * -------------------------------------------------------------------------- */
479
480 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
481                             struct obd_uuid *cluuid, int initial_conn)
482 {
483         if (exp->exp_connection && !initial_conn) {
484                 struct lustre_handle *hdl;
485                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
486                 /* Might be a re-connect after a partition. */
487                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
488                         CERROR("%s reconnecting\n", cluuid->uuid);
489                         conn->cookie = exp->exp_handle.h_cookie;
490                         RETURN(EALREADY);
491                 } else {
492                         CERROR("%s reconnecting from %s, "
493                                "handle mismatch (ours "LPX64", theirs "
494                                LPX64")\n", cluuid->uuid,
495                                exp->exp_connection->c_remote_uuid.uuid,
496                                hdl->cookie, conn->cookie);
497                         memset(conn, 0, sizeof *conn);
498                         RETURN(-EALREADY);
499                 }
500         }
501
502         conn->cookie = exp->exp_handle.h_cookie;
503         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
504                cluuid->uuid, exp);
505         CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
506         RETURN(0);
507 }
508
509 static char nidstr[PTL_NALFMT_SIZE];
510 int target_handle_connect(struct ptlrpc_request *req)
511 {
512         unsigned long connect_flags = 0, *cfp;
513         struct obd_device *target;
514         struct obd_export *export = NULL;
515         struct obd_import *revimp;
516         struct lustre_handle conn;
517         struct obd_uuid tgtuuid;
518         struct obd_uuid cluuid;
519         struct obd_uuid remote_uuid;
520         struct list_head *p;
521         char *str, *tmp;
522         int rc = 0;
523         unsigned long flags;
524         int initial_conn = 0;
525         char peer_str[PTL_NALFMT_SIZE];
526         ENTRY;
527
528         OBD_RACE(OBD_FAIL_TGT_CONN_RACE); 
529
530         LASSERT_REQSWAB (req, 0);
531         str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
532         if (str == NULL) {
533                 CERROR("bad target UUID for connect\n");
534                 GOTO(out, rc = -EINVAL);
535         }
536
537         obd_str2uuid (&tgtuuid, str);
538         target = class_uuid2obd(&tgtuuid);
539         if (!target) {
540                 target = class_name2obd(str);
541         }
542         
543         if (!target || target->obd_stopping || !target->obd_set_up) {
544                 CERROR("UUID '%s' is not available for connect from NID %s\n",
545                        str, ptlrpc_peernid2str(&req->rq_peer, nidstr));
546                 GOTO(out, rc = -ENODEV);
547         }
548
549         LASSERT_REQSWAB (req, 1);
550         str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
551         if (str == NULL) {
552                 CERROR("bad client UUID for connect\n");
553                 GOTO(out, rc = -EINVAL);
554         }
555
556         obd_str2uuid (&cluuid, str);
557
558         /* XXX extract a nettype and format accordingly */
559         switch (sizeof(ptl_nid_t)) {
560                 /* NB the casts only avoid compiler warnings */
561         case 8:
562                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
563                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
564                 break;
565         case 4:
566                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
567                          "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
568                 break;
569         default:
570                 LBUG();
571         }
572
573         tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
574         if (tmp == NULL)
575                 GOTO(out, rc = -EPROTO);
576
577         memcpy(&conn, tmp, sizeof conn);
578
579         cfp = lustre_msg_buf(req->rq_reqmsg, 3, sizeof(unsigned long));
580         LASSERT(cfp != NULL);
581         connect_flags = *cfp;
582
583         rc = lustre_pack_reply(req, 0, NULL, NULL);
584         if (rc)
585                 GOTO(out, rc);
586         
587         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
588                 initial_conn = 1;
589         
590         /* lctl gets a backstage, all-access pass. */
591         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
592                 goto dont_check_exports;
593
594         spin_lock(&target->obd_dev_lock);
595         list_for_each(p, &target->obd_exports) {
596                 export = list_entry(p, struct obd_export, exp_obd_chain);
597                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
598                         spin_unlock(&target->obd_dev_lock);
599                         LASSERT(export->exp_obd == target);
600
601                         rc = target_handle_reconnect(&conn, export, &cluuid,
602                                                      initial_conn);
603                         break;
604                 }
605                 export = NULL;
606         }
607         /* If we found an export, we already unlocked. */
608         if (!export) {
609                 spin_unlock(&target->obd_dev_lock);
610         } else if (req->rq_export == NULL && 
611                    atomic_read(&export->exp_rpc_count) > 0) {
612                 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
613                       target->obd_name, cluuid.uuid,
614                       ptlrpc_peernid2str(&req->rq_peer, peer_str),
615                       export, atomic_read(&export->exp_refcount));
616                 GOTO(out, rc = -EBUSY);
617         } else if (req->rq_export != NULL &&
618                    atomic_read(&export->exp_rpc_count) > 1) {
619                 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
620                       target->obd_name, cluuid.uuid,
621                       ptlrpc_peernid2str(&req->rq_peer, peer_str),
622                       export, atomic_read(&export->exp_rpc_count));
623                 GOTO(out, rc = -EBUSY);
624         }
625         else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
626                 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
627                        cluuid.uuid);
628                 GOTO(out, rc = -EALREADY);
629         }
630
631         /* Tell the client if we're in recovery. */
632         /* If this is the first client, start the recovery timer */
633         CWARN("%s: connection from %s@%s/%lu %s\n", target->obd_name, cluuid.uuid,
634               ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
635               target->obd_recovering ? "(recovering)" : "");
636         if (target->obd_recovering) {
637                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
638                 target_start_recovery_timer(target);
639         }
640 #if 0
641         /* Tell the client if we support replayable requests */
642         if (target->obd_replayable)
643                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
644 #endif
645         if (export == NULL) {
646                 if (target->obd_recovering) {
647                         CERROR("%s denying connection for new client %s@%s: "
648                                "%d clients in recovery for %lds\n", target->obd_name, 
649                                cluuid.uuid,
650                                ptlrpc_peernid2str(&req->rq_peer, peer_str),
651                                target->obd_recoverable_clients,
652                                (target->obd_recovery_timer.expires-jiffies)/HZ);
653                         rc = -EBUSY;
654                 } else {
655  dont_check_exports:
656                         rc = obd_connect(&conn, target, &cluuid, connect_flags);
657                 }
658         }
659         /* Tell the client if we support replayable requests */
660         if (target->obd_replayable)
661                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
662
663         /* If all else goes well, this is our RPC return code. */
664         req->rq_status = 0;
665
666         if (rc && rc != EALREADY)
667                 GOTO(out, rc);
668
669         req->rq_repmsg->handle = conn;
670
671         /* If the client and the server are the same node, we will already
672          * have an export that really points to the client's DLM export,
673          * because we have a shared handles table.
674          *
675          * XXX this will go away when shaver stops sending the "connect" handle
676          * in the real "remote handle" field of the request --phik 24 Apr 2003
677          */
678         if (req->rq_export != NULL)
679                 class_export_put(req->rq_export);
680
681         /* ownership of this export ref transfers to the request */
682         export = req->rq_export = class_conn2export(&conn);
683         LASSERT(export != NULL);
684
685         spin_lock_irqsave(&export->exp_lock, flags);
686         if (initial_conn) {
687                 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
688         } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
689                 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
690                        cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
691                        export->exp_conn_cnt, 
692                        req->rq_reqmsg->conn_cnt);
693                 spin_unlock_irqrestore(&export->exp_lock, flags);
694                 GOTO(out, rc = -EALREADY);
695         } 
696         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
697         spin_unlock_irqrestore(&export->exp_lock, flags);
698
699         /* request from liblustre? */
700         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
701                 export->exp_libclient = 1;
702
703         if (export->exp_connection != NULL)
704                 ptlrpc_put_connection(export->exp_connection);
705         export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
706                                                        &remote_uuid);
707
708         if (rc == EALREADY) {
709                 /* We indicate the reconnection in a flag, not an error code. */
710                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
711                 GOTO(out, rc = 0);
712         }
713
714         if (target->obd_recovering) {
715                 target->obd_connected_clients++;
716         }
717
718         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
719                sizeof conn);
720
721         if (export->exp_imp_reverse != NULL)
722                 class_destroy_import(export->exp_imp_reverse);
723         revimp = export->exp_imp_reverse = class_new_import();
724         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
725         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
726         revimp->imp_remote_handle = conn;
727         revimp->imp_obd = target;
728         revimp->imp_dlm_fake = 1;
729         revimp->imp_state = LUSTRE_IMP_FULL;
730         class_import_put(revimp);
731
732         rc = obd_connect_post(export, connect_flags);
733 out:
734         if (rc)
735                 req->rq_status = rc;
736         RETURN(rc);
737 }
738
739 int target_handle_disconnect(struct ptlrpc_request *req)
740 {
741         struct obd_export *exp;
742         int rc;
743         ENTRY;
744
745         rc = lustre_pack_reply(req, 0, NULL, NULL);
746         if (rc)
747                 RETURN(rc);
748
749         /* keep the rq_export around so we can send the reply */
750         exp = class_export_get(req->rq_export);
751         req->rq_status = obd_disconnect(exp, 0);
752         RETURN(0);
753 }
754
755 void target_destroy_export(struct obd_export *exp)
756 {
757         /* exports created from last_rcvd data, and "fake"
758            exports created by lctl don't have an import */
759         if (exp->exp_imp_reverse != NULL)
760                 class_destroy_import(exp->exp_imp_reverse);
761
762         /* We cancel locks at disconnect time, but this will catch any locks
763          * granted in a race with recovery-induced disconnect. */
764         if (exp->exp_obd->obd_namespace != NULL)
765                 ldlm_cancel_locks_for_export(exp);
766 }
767
768 /*
769  * Recovery functions
770  */
771
772 struct ptlrpc_request *
773 ptlrpc_clone_req( struct ptlrpc_request *orig_req) 
774 {
775         struct ptlrpc_request *copy_req;
776         struct lustre_msg *copy_reqmsg;
777
778         OBD_ALLOC(copy_req, sizeof *copy_req);
779         if (!copy_req)
780                 return NULL;
781         OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
782         if (!copy_reqmsg){
783                 OBD_FREE(copy_req, sizeof *copy_req);
784                 return NULL;
785         }
786
787         memcpy(copy_req, orig_req, sizeof *copy_req);
788         memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
789         /* the copied req takes over the reply state */
790         orig_req->rq_reply_state = NULL;
791
792         copy_req->rq_reqmsg = copy_reqmsg;
793         class_export_get(copy_req->rq_export);
794         INIT_LIST_HEAD(&copy_req->rq_list);
795
796         return copy_req;
797 }
798 void ptlrpc_free_clone( struct ptlrpc_request *req) 
799 {
800         class_export_put(req->rq_export);
801         list_del(&req->rq_list);
802         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
803         OBD_FREE(req, sizeof *req);
804 }
805
806
807
808 static void target_release_saved_req(struct ptlrpc_request *req)
809 {
810         class_export_put(req->rq_export);
811         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
812         OBD_FREE(req, sizeof *req);
813 }
814
815 #ifdef __KERNEL__
816 static void target_finish_recovery(struct obd_device *obd)
817 {
818         struct list_head *tmp, *n;
819         int rc;
820
821         CWARN("%s: sending delayed replies to recovered clients\n",
822               obd->obd_name);
823
824         ldlm_reprocess_all_ns(obd->obd_namespace);
825
826         /* when recovery finished, cleanup orphans on mds and ost */
827         if (OBT(obd) && OBP(obd, postrecov)) {
828                 rc = OBP(obd, postrecov)(obd);
829                 if (rc >= 0)
830                         CWARN("%s: all clients recovered, %d MDS "
831                               "orphans deleted\n", obd->obd_name, rc);
832                 else
833                         CERROR("postrecov failed %d\n", rc);
834         }
835
836
837         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
838                 struct ptlrpc_request *req;
839                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
840                 list_del(&req->rq_list);
841                 DEBUG_REQ(D_ERROR, req, "delayed:");
842                 ptlrpc_reply(req);
843                 target_release_saved_req(req);
844         }
845         obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
846         return;
847 }
848
849 static void abort_recovery_queue(struct obd_device *obd)
850 {
851         struct ptlrpc_request *req;
852         struct list_head *tmp, *n;
853         int rc;
854
855         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
856                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
857                 list_del(&req->rq_list);
858                 DEBUG_REQ(D_ERROR, req, "aborted:");
859                 req->rq_status = -ENOTCONN;
860                 req->rq_type = PTL_RPC_MSG_ERR;
861                 rc = lustre_pack_reply(req, 0, NULL, NULL);
862                 if (rc == 0) {
863                         ptlrpc_reply(req);
864                 } else {
865                         DEBUG_REQ(D_ERROR, req,
866                                   "packing failed for abort-reply; skipping");
867                 }
868                 target_release_saved_req(req);
869         }
870 }
871 #endif
872
873 /* Called from a cleanup function if the device is being cleaned up
874    forcefully.  The exports should all have been disconnected already,
875    the only thing left to do is
876      - clear the recovery flags
877      - cancel the timer
878      - free queued requests and replies, but don't send replies
879    Because the obd_stopping flag is set, no new requests should be received.
880
881 */
882 void target_cleanup_recovery(struct obd_device *obd)
883 {
884         struct list_head *tmp, *n;
885         struct ptlrpc_request *req;
886
887         spin_lock_bh(&obd->obd_processing_task_lock);
888         if (!obd->obd_recovering) {
889                 spin_unlock_bh(&obd->obd_processing_task_lock);
890                 EXIT;
891                 return;
892         }
893         obd->obd_recovering = obd->obd_abort_recovery = 0;
894         target_cancel_recovery_timer(obd);
895         spin_unlock_bh(&obd->obd_processing_task_lock);
896
897         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
898                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
899                 list_del(&req->rq_list);
900                 LASSERT (req->rq_reply_state);
901                 lustre_free_reply_state(req->rq_reply_state);
902                 target_release_saved_req(req);
903         }
904         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
905                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
906                 list_del(&req->rq_list);
907                 LASSERT (req->rq_reply_state == 0);
908                 target_release_saved_req(req);
909          }
910 }
911
912 #ifdef __KERNEL__
913 static void target_abort_recovery(void *data)
914 {
915         struct obd_device *obd = data;
916                                                                                                                                                                                                      
917         LASSERT(!obd->obd_recovering);
918
919         class_disconnect_stale_exports(obd, 0);
920
921         CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
922                obd->obd_name);
923
924         abort_recovery_queue(obd);
925         target_finish_recovery(obd);
926         ptlrpc_run_recovery_over_upcall(obd);
927 }
928 #endif
929
930 static void target_recovery_expired(unsigned long castmeharder)
931 {
932         struct obd_device *obd = (struct obd_device *)castmeharder;
933         CERROR("recovery timed out, aborting\n");
934         spin_lock_bh(&obd->obd_processing_task_lock);
935         if (obd->obd_recovering)
936                 obd->obd_abort_recovery = 1;
937
938         wake_up(&obd->obd_next_transno_waitq);
939         spin_unlock_bh(&obd->obd_processing_task_lock);
940 }
941
942
943 /* obd_processing_task_lock should be held */
944 void target_cancel_recovery_timer(struct obd_device *obd)
945 {
946         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
947         del_timer(&obd->obd_recovery_timer);
948 }
949
950 #ifdef __KERNEL__
951 static void reset_recovery_timer(struct obd_device *obd)
952 {
953         spin_lock_bh(&obd->obd_processing_task_lock);
954         if (!obd->obd_recovering) {
955                 spin_unlock_bh(&obd->obd_processing_task_lock);
956                 return;
957         }                
958         CDEBUG(D_HA, "timer will expire in %u seconds\n",
959                OBD_RECOVERY_TIMEOUT / HZ);
960         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
961         spin_unlock_bh(&obd->obd_processing_task_lock);
962 }
963 #endif
964
965 /* Only start it the first time called */
966 void target_start_recovery_timer(struct obd_device *obd)
967 {
968         spin_lock_bh(&obd->obd_processing_task_lock);
969         if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
970                 spin_unlock_bh(&obd->obd_processing_task_lock);
971                 return;
972         }
973         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
974                OBD_RECOVERY_TIMEOUT / HZ);
975         obd->obd_recovery_timer.function = target_recovery_expired;
976         obd->obd_recovery_timer.data = (unsigned long)obd;
977         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
978         spin_unlock_bh(&obd->obd_processing_task_lock);
979 }
980
981 #ifdef __KERNEL__
982 static int check_for_next_transno(struct obd_device *obd)
983 {
984         struct ptlrpc_request *req = NULL;
985         int wake_up = 0, connected, completed, queue_len, max;
986         __u64 next_transno, req_transno;
987
988         spin_lock_bh(&obd->obd_processing_task_lock);
989         if (!list_empty(&obd->obd_recovery_queue)) {
990                 req = list_entry(obd->obd_recovery_queue.next,
991                                  struct ptlrpc_request, rq_list);
992                 req_transno = req->rq_reqmsg->transno;
993         } else {
994                 req_transno = 0;
995         }
996
997         max = obd->obd_max_recoverable_clients;
998         connected = obd->obd_connected_clients;
999         completed = max - obd->obd_recoverable_clients;
1000         queue_len = obd->obd_requests_queued_for_recovery;
1001         next_transno = obd->obd_next_recovery_transno;
1002
1003         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1004                "req_transno: "LPU64", next_transno: "LPU64"\n",
1005                max, connected, completed, queue_len, req_transno, next_transno);
1006         if (obd->obd_abort_recovery) {
1007                 CDEBUG(D_HA, "waking for aborted recovery\n");
1008                 wake_up = 1;
1009         } else if (max == completed) {
1010                 CDEBUG(D_HA, "waking for completed recovery\n");
1011                 wake_up = 1;
1012         } else if (req_transno == next_transno) {
1013                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1014                 wake_up = 1;
1015         } else if (queue_len + completed == max) {
1016                 LASSERT(req->rq_reqmsg->transno >= next_transno);
1017                 CDEBUG(D_ERROR,
1018                        "waking for skipped transno (skip: "LPD64
1019                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1020                        next_transno, queue_len, completed, max, req_transno);
1021                 obd->obd_next_recovery_transno = req_transno;
1022                 wake_up = 1;
1023         }
1024         spin_unlock_bh(&obd->obd_processing_task_lock);
1025         
1026         return wake_up;
1027 }
1028
1029 static struct ptlrpc_request *
1030 target_next_replay_req(struct obd_device *obd)
1031 {
1032         struct l_wait_info lwi = { 0 };
1033         struct ptlrpc_request *req;
1034
1035         CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1036                obd->obd_next_recovery_transno);
1037         l_wait_event(obd->obd_next_transno_waitq,
1038                      check_for_next_transno(obd), &lwi);
1039         
1040         spin_lock_bh(&obd->obd_processing_task_lock);
1041         if (obd->obd_abort_recovery) {
1042                 req = NULL;
1043         } else if (!list_empty(&obd->obd_recovery_queue)) {
1044                 req = list_entry(obd->obd_recovery_queue.next,
1045                                  struct ptlrpc_request, rq_list);
1046                 list_del_init(&req->rq_list);
1047                 obd->obd_requests_queued_for_recovery--;
1048         } else {
1049                 req = NULL;
1050         }
1051         spin_unlock_bh(&obd->obd_processing_task_lock);
1052         return req;
1053 }
1054
1055 static int target_recovery_thread(void *arg)
1056 {
1057         struct obd_device *obd = arg;
1058         struct ptlrpc_request *req;
1059         struct target_recovery_data *trd = &obd->obd_recovery_data;
1060         unsigned long flags;
1061         ENTRY;
1062
1063         kportal_daemonize("tgt-recov");
1064
1065         SIGNAL_MASK_LOCK(current, flags);
1066         sigfillset(&current->blocked);
1067         RECALC_SIGPENDING;
1068         SIGNAL_MASK_UNLOCK(current, flags);
1069
1070         CERROR("%s: started recovery thread pid %d\n", obd->obd_name, 
1071                current->pid);
1072         trd->trd_processing_task = current->pid;
1073
1074         obd->obd_recovering = 1;
1075         complete(&trd->trd_starting);
1076
1077         while (obd->obd_recovering) {
1078                 LASSERT(trd->trd_processing_task == current->pid);
1079                 req = target_next_replay_req(obd);
1080                 if (req != NULL) {
1081                         char peer_str[PTL_NALFMT_SIZE];
1082                         DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ", 
1083                                   req->rq_reqmsg->transno, 
1084                                   ptlrpc_peernid2str(&req->rq_peer, peer_str));
1085                         (void)trd->trd_recovery_handler(req);
1086                         obd->obd_replayed_requests++;
1087                         reset_recovery_timer(obd);
1088                         /* bug 1580: decide how to properly sync() in recovery*/
1089                         //mds_fsync_super(mds->mds_sb);
1090                         ptlrpc_free_clone(req);
1091                         spin_lock_bh(&obd->obd_processing_task_lock);
1092                         obd->obd_next_recovery_transno++;
1093                         spin_unlock_bh(&obd->obd_processing_task_lock);
1094                 } else {
1095                         /* recovery is over */
1096                         spin_lock_bh(&obd->obd_processing_task_lock);
1097                         obd->obd_recovering = 0;
1098                         target_cancel_recovery_timer(obd);
1099                         if (obd->obd_abort_recovery) {
1100                                 obd->obd_abort_recovery = 0;
1101                                 spin_unlock_bh(&obd->obd_processing_task_lock);
1102                                 target_abort_recovery(obd); 
1103                         } else {
1104                                 LASSERT(obd->obd_recoverable_clients == 0);
1105                                 spin_unlock_bh(&obd->obd_processing_task_lock);
1106                                 target_finish_recovery(obd);
1107                         }
1108                 }
1109         }
1110
1111         trd->trd_processing_task = 0;
1112         complete(&trd->trd_finishing);
1113         return 0;
1114 }
1115
1116 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1117 {
1118         int rc = 0;
1119         struct target_recovery_data *trd = &obd->obd_recovery_data;
1120
1121         memset(trd, 0, sizeof(*trd));
1122         init_completion(&trd->trd_starting);
1123         init_completion(&trd->trd_finishing);
1124         trd->trd_recovery_handler = handler;
1125
1126         if (kernel_thread(target_recovery_thread, obd, 0) == 0) 
1127                 wait_for_completion(&trd->trd_starting);
1128         else
1129                 rc = -ECHILD;
1130
1131         return rc;
1132 }
1133
1134 void target_stop_recovery_thread(struct obd_device *obd)
1135 {
1136         spin_lock_bh(&obd->obd_processing_task_lock);
1137         if (obd->obd_recovery_data.trd_processing_task > 0) {
1138                 struct target_recovery_data *trd = &obd->obd_recovery_data;
1139                 CERROR("%s: aborting recovery\n", obd->obd_name);
1140                 obd->obd_abort_recovery = 1;
1141                 wake_up(&obd->obd_next_transno_waitq);
1142                 spin_unlock_bh(&obd->obd_processing_task_lock);
1143                 wait_for_completion(&trd->trd_finishing);
1144         } else {
1145                 spin_unlock_bh(&obd->obd_processing_task_lock);
1146         }
1147 }
1148 #endif
1149
1150 int target_queue_recovery_request(struct ptlrpc_request *req,
1151                                   struct obd_device *obd)
1152 {
1153         struct list_head *tmp;
1154         int inserted = 0;
1155         __u64 transno = req->rq_reqmsg->transno;
1156
1157         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1158          * (i.e. buflens etc are in my own byte order), but type-dependent
1159          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1160
1161         if (!transno) {
1162                 INIT_LIST_HEAD(&req->rq_list);
1163                 DEBUG_REQ(D_HA, req, "not queueing");
1164                 return 1;
1165         }
1166
1167
1168         /* If we're processing the queue, we want don't want to queue this
1169          * message.
1170          *
1171          * Also, if this request has a transno less than the one we're waiting
1172          * for, we should process it now.  It could (and currently always will)
1173          * be an open request for a descriptor that was opened some time ago.
1174          *
1175          * Also, a resent, replayed request that has already been
1176          * handled will pass through here and be processed immediately.
1177          */
1178         spin_lock_bh(&obd->obd_processing_task_lock);
1179         if (obd->obd_recovery_data.trd_processing_task == current->pid ||
1180             transno < obd->obd_next_recovery_transno) {
1181                 /* Processing the queue right now, don't re-add. */
1182                 LASSERT(list_empty(&req->rq_list));
1183                 spin_unlock_bh(&obd->obd_processing_task_lock);
1184                 return 1;
1185         }
1186         spin_unlock_bh(&obd->obd_processing_task_lock);
1187
1188         /* A resent, replayed request that is still on the queue; just drop it.
1189            The queued request will handle this. */
1190         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1191             == (MSG_RESENT | MSG_REPLAY)) {
1192                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1193                 return 0;
1194         }
1195
1196         req = ptlrpc_clone_req(req);
1197         if (req == NULL)
1198                 return -ENOMEM;
1199
1200         spin_lock_bh(&obd->obd_processing_task_lock);
1201
1202         /* XXX O(n^2) */
1203         list_for_each(tmp, &obd->obd_recovery_queue) {
1204                 struct ptlrpc_request *reqiter =
1205                         list_entry(tmp, struct ptlrpc_request, rq_list);
1206
1207                 if (reqiter->rq_reqmsg->transno > transno) {
1208                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1209                         inserted = 1;
1210                         break;
1211                 }
1212         }
1213
1214         if (!inserted) {
1215                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1216         }
1217
1218         obd->obd_requests_queued_for_recovery++;
1219         wake_up(&obd->obd_next_transno_waitq);
1220         spin_unlock_bh(&obd->obd_processing_task_lock);
1221
1222         return 0;
1223 }
1224
1225 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1226 {
1227         return req->rq_export->exp_obd;
1228 }
1229
1230 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1231 {
1232         struct obd_device *obd = target_req2obd(req);
1233
1234         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1235
1236         if (rc) {
1237                 /* Just like ptlrpc_error, but without the sending. */
1238                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1239                 LASSERT(rc == 0); /* XXX handle this */
1240                 req->rq_type = PTL_RPC_MSG_ERR;
1241         }
1242
1243         LASSERT (!req->rq_reply_state->rs_difficult);
1244         LASSERT(list_empty(&req->rq_list));
1245         
1246         req = ptlrpc_clone_req(req);
1247
1248         spin_lock_bh(&obd->obd_processing_task_lock);
1249
1250         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1251
1252         /* only count the first "replay over" request from each
1253            export */
1254         if (req->rq_export->exp_replay_needed) {
1255                 --obd->obd_recoverable_clients;
1256                 req->rq_export->exp_replay_needed = 0;
1257                 CWARN("%s: %d recoverable clients remain\n",
1258                       obd->obd_name, obd->obd_recoverable_clients);
1259         }
1260         wake_up(&obd->obd_next_transno_waitq);
1261         spin_unlock_bh(&obd->obd_processing_task_lock);
1262         return 1;
1263 }
1264
1265 int
1266 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1267 {
1268         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1269                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1270                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1271                 /* NB this does _not_ send with ACK disabled, to simulate
1272                  * sending OK, but timing out for the ACK */
1273                 if (req->rq_reply_state != NULL) {
1274                         if (!req->rq_reply_state->rs_difficult) {
1275                                 lustre_free_reply_state (req->rq_reply_state);
1276                                 req->rq_reply_state = NULL;
1277                         } else {
1278                                 struct ptlrpc_service *svc =
1279                                         req->rq_rqbd->rqbd_srv_ni->sni_service;
1280                                 atomic_inc(&svc->srv_outstanding_replies);
1281                         }
1282                 }
1283                 return (-ECOMM);
1284         }
1285
1286         if (rc) {
1287                 req->rq_status = rc;
1288                 return (ptlrpc_error(req));
1289         } else {
1290                 DEBUG_REQ(D_NET, req, "sending reply");
1291         }
1292         
1293         return (ptlrpc_send_reply(req, 1));
1294 }
1295
1296 void 
1297 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1298 {
1299         int                        netrc;
1300         unsigned long              flags;
1301         struct ptlrpc_reply_state *rs;
1302         struct obd_device         *obd;
1303         struct obd_export         *exp;
1304         struct ptlrpc_srv_ni      *sni;
1305         struct ptlrpc_service     *svc;
1306
1307         sni = req->rq_rqbd->rqbd_srv_ni;
1308         svc = sni->sni_service;
1309         
1310         rs = req->rq_reply_state;
1311         if (rs == NULL || !rs->rs_difficult) {
1312                 /* The easy case; no notifiers and reply_out_callback()
1313                  * cleans up (i.e. we can't look inside rs after a
1314                  * successful send) */
1315                 netrc = target_send_reply_msg (req, rc, fail_id);
1316
1317                 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1318                 return;
1319         }
1320
1321         /* must be an export if locks saved */
1322         LASSERT (req->rq_export != NULL);
1323         /* req/reply consistent */
1324         LASSERT (rs->rs_srv_ni == sni);
1325
1326         /* "fresh" reply */
1327         LASSERT (!rs->rs_scheduled);
1328         LASSERT (!rs->rs_scheduled_ever);
1329         LASSERT (!rs->rs_handled);
1330         LASSERT (!rs->rs_on_net);
1331         LASSERT (rs->rs_export == NULL);
1332         LASSERT (list_empty(&rs->rs_obd_list));
1333         LASSERT (list_empty(&rs->rs_exp_list));
1334
1335         exp = class_export_get (req->rq_export);
1336         obd = exp->exp_obd;
1337
1338         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1339         rs->rs_scheduled = 1;
1340         rs->rs_on_net    = 1;
1341         rs->rs_xid       = req->rq_xid;
1342         rs->rs_transno   = req->rq_transno;
1343         rs->rs_export    = exp;
1344         
1345         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1346
1347         if (rs->rs_transno > obd->obd_last_committed) {
1348                 /* not committed already */ 
1349                 list_add_tail (&rs->rs_obd_list, 
1350                                &obd->obd_uncommitted_replies);
1351         }
1352
1353         spin_unlock (&obd->obd_uncommitted_replies_lock);
1354         spin_lock (&exp->exp_lock);
1355
1356         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1357
1358         spin_unlock_irqrestore (&exp->exp_lock, flags);
1359
1360         netrc = target_send_reply_msg (req, rc, fail_id);
1361
1362         spin_lock_irqsave (&svc->srv_lock, flags);
1363
1364         svc->srv_n_difficult_replies++;
1365
1366         if (netrc != 0) /* error sending: reply is off the net */
1367                 rs->rs_on_net = 0;
1368
1369         if (!rs->rs_on_net ||                   /* some notifier */
1370             list_empty(&rs->rs_exp_list) ||     /* completed already */
1371             list_empty(&rs->rs_obd_list)) {
1372                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1373                 wake_up (&svc->srv_waitq);
1374         } else {
1375                 list_add (&rs->rs_list, &sni->sni_active_replies);
1376                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1377         }
1378
1379         spin_unlock_irqrestore (&svc->srv_lock, flags);
1380 }
1381
1382 int target_handle_ping(struct ptlrpc_request *req)
1383 {
1384         return lustre_pack_reply(req, 0, NULL, NULL);
1385 }