Whamcloud - gitweb
- landing b_fid.
[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, int 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         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
459         if (obd->obd_no_recov)
460                 ptlrpc_invalidate_import(imp, 0);
461         else
462                 rc = ptlrpc_disconnect_import(imp);
463
464         EXIT;
465  out_no_disconnect:
466         err = class_disconnect(exp, 0);
467         if (!rc && err)
468                 rc = err;
469  out_sem:
470         up(&cli->cl_sem);
471         RETURN(rc);
472 }
473
474 /* --------------------------------------------------------------------------
475  * from old lib/target.c
476  * -------------------------------------------------------------------------- */
477
478 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
479                             struct obd_uuid *cluuid, int initial_conn)
480 {
481         if (exp->exp_connection && !initial_conn) {
482                 struct lustre_handle *hdl;
483                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
484                 /* Might be a re-connect after a partition. */
485                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
486                         CERROR("%s reconnecting\n", cluuid->uuid);
487                         conn->cookie = exp->exp_handle.h_cookie;
488                         RETURN(EALREADY);
489                 } else {
490                         CERROR("%s reconnecting from %s, "
491                                "handle mismatch (ours "LPX64", theirs "
492                                LPX64")\n", cluuid->uuid,
493                                exp->exp_connection->c_remote_uuid.uuid,
494                                hdl->cookie, conn->cookie);
495                         memset(conn, 0, sizeof *conn);
496                         RETURN(-EALREADY);
497                 }
498         }
499
500         conn->cookie = exp->exp_handle.h_cookie;
501         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
502                cluuid->uuid, exp);
503         CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
504         RETURN(0);
505 }
506
507 static char nidstr[PTL_NALFMT_SIZE];
508 int target_handle_connect(struct ptlrpc_request *req)
509 {
510         unsigned long connect_flags = 0, *cfp;
511         struct obd_device *target;
512         struct obd_export *export = NULL;
513         struct obd_import *revimp;
514         struct lustre_handle conn;
515         struct obd_uuid tgtuuid;
516         struct obd_uuid cluuid;
517         struct obd_uuid remote_uuid;
518         struct list_head *p;
519         char *str, *tmp;
520         int rc = 0;
521         unsigned long flags;
522         int initial_conn = 0;
523         char peer_str[PTL_NALFMT_SIZE];
524         ENTRY;
525
526         OBD_RACE(OBD_FAIL_TGT_CONN_RACE); 
527
528         LASSERT_REQSWAB (req, 0);
529         str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
530         if (str == NULL) {
531                 CERROR("bad target UUID for connect\n");
532                 GOTO(out, rc = -EINVAL);
533         }
534
535         obd_str2uuid (&tgtuuid, str);
536         target = class_uuid2obd(&tgtuuid);
537         if (!target) {
538                 target = class_name2obd(str);
539         }
540         
541         if (!target || target->obd_stopping || !target->obd_set_up) {
542                 CERROR("UUID '%s' is not available for connect from NID %s\n",
543                        str, ptlrpc_peernid2str(&req->rq_peer, nidstr));
544                 GOTO(out, rc = -ENODEV);
545         }
546
547         LASSERT_REQSWAB (req, 1);
548         str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
549         if (str == NULL) {
550                 CERROR("bad client UUID for connect\n");
551                 GOTO(out, rc = -EINVAL);
552         }
553
554         obd_str2uuid (&cluuid, str);
555
556         /* XXX extract a nettype and format accordingly */
557         switch (sizeof(ptl_nid_t)) {
558                 /* NB the casts only avoid compiler warnings */
559         case 8:
560                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
561                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
562                 break;
563         case 4:
564                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
565                          "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
566                 break;
567         default:
568                 LBUG();
569         }
570
571         tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
572         if (tmp == NULL)
573                 GOTO(out, rc = -EPROTO);
574
575         memcpy(&conn, tmp, sizeof conn);
576
577         cfp = lustre_msg_buf(req->rq_reqmsg, 3, sizeof(unsigned long));
578         LASSERT(cfp != NULL);
579         connect_flags = *cfp;
580
581         rc = lustre_pack_reply(req, 0, NULL, NULL);
582         if (rc)
583                 GOTO(out, rc);
584         
585         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
586                 initial_conn = 1;
587         
588         /* lctl gets a backstage, all-access pass. */
589         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
590                 goto dont_check_exports;
591
592         spin_lock(&target->obd_dev_lock);
593         list_for_each(p, &target->obd_exports) {
594                 export = list_entry(p, struct obd_export, exp_obd_chain);
595                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
596                         spin_unlock(&target->obd_dev_lock);
597                         LASSERT(export->exp_obd == target);
598
599                         rc = target_handle_reconnect(&conn, export, &cluuid,
600                                                      initial_conn);
601                         break;
602                 }
603                 export = NULL;
604         }
605         /* If we found an export, we already unlocked. */
606         if (!export) {
607                 spin_unlock(&target->obd_dev_lock);
608         } else if (req->rq_export == NULL && 
609                    atomic_read(&export->exp_rpc_count) > 0) {
610                 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
611                       target->obd_name, cluuid.uuid,
612                       ptlrpc_peernid2str(&req->rq_peer, peer_str),
613                       export, atomic_read(&export->exp_refcount));
614                 GOTO(out, rc = -EBUSY);
615         } else if (req->rq_export != NULL &&
616                    atomic_read(&export->exp_rpc_count) > 1) {
617                 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
618                       target->obd_name, cluuid.uuid,
619                       ptlrpc_peernid2str(&req->rq_peer, peer_str),
620                       export, atomic_read(&export->exp_rpc_count));
621                 GOTO(out, rc = -EBUSY);
622         }
623         else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
624                 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
625                        cluuid.uuid);
626                 GOTO(out, rc = -EALREADY);
627         }
628
629         /* Tell the client if we're in recovery. */
630         /* If this is the first client, start the recovery timer */
631         CWARN("%s: connection from %s@%s/%lu %s\n", target->obd_name, cluuid.uuid,
632               ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
633               target->obd_recovering ? "(recovering)" : "");
634         if (target->obd_recovering) {
635                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
636                 target_start_recovery_timer(target);
637         }
638 #if 0
639         /* Tell the client if we support replayable requests */
640         if (target->obd_replayable)
641                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
642 #endif
643         if (export == NULL) {
644                 if (target->obd_recovering) {
645                         CERROR("%s denying connection for new client %s@%s: "
646                                "%d clients in recovery for %lds\n", target->obd_name, 
647                                cluuid.uuid,
648                                ptlrpc_peernid2str(&req->rq_peer, peer_str),
649                                target->obd_recoverable_clients,
650                                (target->obd_recovery_timer.expires-jiffies)/HZ);
651                         rc = -EBUSY;
652                 } else {
653  dont_check_exports:
654                         rc = obd_connect(&conn, target, &cluuid, connect_flags);
655                 }
656         }
657         /* Tell the client if we support replayable requests */
658         if (target->obd_replayable)
659                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
660
661         /* If all else goes well, this is our RPC return code. */
662         req->rq_status = 0;
663
664         if (rc && rc != EALREADY)
665                 GOTO(out, rc);
666
667         req->rq_repmsg->handle = conn;
668
669         /* If the client and the server are the same node, we will already
670          * have an export that really points to the client's DLM export,
671          * because we have a shared handles table.
672          *
673          * XXX this will go away when shaver stops sending the "connect" handle
674          * in the real "remote handle" field of the request --phik 24 Apr 2003
675          */
676         if (req->rq_export != NULL)
677                 class_export_put(req->rq_export);
678
679         /* ownership of this export ref transfers to the request */
680         export = req->rq_export = class_conn2export(&conn);
681         LASSERT(export != NULL);
682
683         spin_lock_irqsave(&export->exp_lock, flags);
684         if (initial_conn) {
685                 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
686         } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
687                 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
688                        cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
689                        export->exp_conn_cnt, 
690                        req->rq_reqmsg->conn_cnt);
691                 spin_unlock_irqrestore(&export->exp_lock, flags);
692                 GOTO(out, rc = -EALREADY);
693         } 
694         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
695         spin_unlock_irqrestore(&export->exp_lock, flags);
696
697         /* request from liblustre? */
698         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
699                 export->exp_libclient = 1;
700
701         if (export->exp_connection != NULL)
702                 ptlrpc_put_connection(export->exp_connection);
703         export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
704                                                        &remote_uuid);
705
706         if (rc == EALREADY) {
707                 /* We indicate the reconnection in a flag, not an error code. */
708                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
709                 GOTO(out, rc = 0);
710         }
711
712         if (target->obd_recovering) {
713                 target->obd_connected_clients++;
714         }
715
716         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
717                sizeof conn);
718
719         if (export->exp_imp_reverse != NULL)
720                 class_destroy_import(export->exp_imp_reverse);
721         revimp = export->exp_imp_reverse = class_new_import();
722         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
723         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
724         revimp->imp_remote_handle = conn;
725         revimp->imp_obd = target;
726         revimp->imp_dlm_fake = 1;
727         revimp->imp_state = LUSTRE_IMP_FULL;
728         class_import_put(revimp);
729
730         rc = obd_connect_post(export, connect_flags);
731 out:
732         if (rc)
733                 req->rq_status = rc;
734         RETURN(rc);
735 }
736
737 int target_handle_disconnect(struct ptlrpc_request *req)
738 {
739         struct obd_export *exp;
740         int rc;
741         ENTRY;
742
743         rc = lustre_pack_reply(req, 0, NULL, NULL);
744         if (rc)
745                 RETURN(rc);
746
747         /* keep the rq_export around so we can send the reply */
748         exp = class_export_get(req->rq_export);
749         req->rq_status = obd_disconnect(exp, 0);
750         RETURN(0);
751 }
752
753 void target_destroy_export(struct obd_export *exp)
754 {
755         /* exports created from last_rcvd data, and "fake"
756            exports created by lctl don't have an import */
757         if (exp->exp_imp_reverse != NULL)
758                 class_destroy_import(exp->exp_imp_reverse);
759
760         /* We cancel locks at disconnect time, but this will catch any locks
761          * granted in a race with recovery-induced disconnect. */
762         if (exp->exp_obd->obd_namespace != NULL)
763                 ldlm_cancel_locks_for_export(exp);
764 }
765
766 /*
767  * Recovery functions
768  */
769
770 struct ptlrpc_request *
771 ptlrpc_clone_req( struct ptlrpc_request *orig_req) 
772 {
773         struct ptlrpc_request *copy_req;
774         struct lustre_msg *copy_reqmsg;
775
776         OBD_ALLOC(copy_req, sizeof *copy_req);
777         if (!copy_req)
778                 return NULL;
779         OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
780         if (!copy_reqmsg){
781                 OBD_FREE(copy_req, sizeof *copy_req);
782                 return NULL;
783         }
784
785         memcpy(copy_req, orig_req, sizeof *copy_req);
786         memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
787         /* the copied req takes over the reply state */
788         orig_req->rq_reply_state = NULL;
789
790         copy_req->rq_reqmsg = copy_reqmsg;
791         class_export_get(copy_req->rq_export);
792         INIT_LIST_HEAD(&copy_req->rq_list);
793
794         return copy_req;
795 }
796 void ptlrpc_free_clone( struct ptlrpc_request *req) 
797 {
798         class_export_put(req->rq_export);
799         list_del(&req->rq_list);
800         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
801         OBD_FREE(req, sizeof *req);
802 }
803
804
805
806 static void target_release_saved_req(struct ptlrpc_request *req)
807 {
808         class_export_put(req->rq_export);
809         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
810         OBD_FREE(req, sizeof *req);
811 }
812
813 #ifdef __KERNEL__
814 static void target_finish_recovery(struct obd_device *obd)
815 {
816         struct list_head *tmp, *n;
817         int rc;
818
819         CWARN("%s: sending delayed replies to recovered clients\n",
820               obd->obd_name);
821
822         ldlm_reprocess_all_ns(obd->obd_namespace);
823
824         /* when recovery finished, cleanup orphans on mds and ost */
825         if (OBT(obd) && OBP(obd, postrecov)) {
826                 rc = OBP(obd, postrecov)(obd);
827                 if (rc >= 0)
828                         CWARN("%s: all clients recovered, %d MDS "
829                               "orphans deleted\n", obd->obd_name, rc);
830                 else
831                         CERROR("postrecov failed %d\n", rc);
832         }
833
834
835         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
836                 struct ptlrpc_request *req;
837                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
838                 list_del(&req->rq_list);
839                 DEBUG_REQ(D_ERROR, req, "delayed:");
840                 ptlrpc_reply(req);
841                 target_release_saved_req(req);
842         }
843         obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
844         return;
845 }
846
847 static void abort_recovery_queue(struct obd_device *obd)
848 {
849         struct ptlrpc_request *req;
850         struct list_head *tmp, *n;
851         int rc;
852
853         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
854                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
855                 list_del(&req->rq_list);
856                 DEBUG_REQ(D_ERROR, req, "aborted:");
857                 req->rq_status = -ENOTCONN;
858                 req->rq_type = PTL_RPC_MSG_ERR;
859                 rc = lustre_pack_reply(req, 0, NULL, NULL);
860                 if (rc == 0) {
861                         ptlrpc_reply(req);
862                 } else {
863                         DEBUG_REQ(D_ERROR, req,
864                                   "packing failed for abort-reply; skipping");
865                 }
866                 target_release_saved_req(req);
867         }
868 }
869 #endif
870
871 /* Called from a cleanup function if the device is being cleaned up
872    forcefully.  The exports should all have been disconnected already,
873    the only thing left to do is
874      - clear the recovery flags
875      - cancel the timer
876      - free queued requests and replies, but don't send replies
877    Because the obd_stopping flag is set, no new requests should be received.
878
879 */
880 void target_cleanup_recovery(struct obd_device *obd)
881 {
882         struct list_head *tmp, *n;
883         struct ptlrpc_request *req;
884
885         spin_lock_bh(&obd->obd_processing_task_lock);
886         if (!obd->obd_recovering) {
887                 spin_unlock_bh(&obd->obd_processing_task_lock);
888                 EXIT;
889                 return;
890         }
891         obd->obd_recovering = obd->obd_abort_recovery = 0;
892         target_cancel_recovery_timer(obd);
893         spin_unlock_bh(&obd->obd_processing_task_lock);
894
895         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
896                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
897                 list_del(&req->rq_list);
898                 LASSERT (req->rq_reply_state);
899                 lustre_free_reply_state(req->rq_reply_state);
900                 target_release_saved_req(req);
901         }
902         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
903                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
904                 list_del(&req->rq_list);
905                 LASSERT (req->rq_reply_state == 0);
906                 target_release_saved_req(req);
907          }
908 }
909
910 #ifdef __KERNEL__
911 static void target_abort_recovery(void *data)
912 {
913         struct obd_device *obd = data;
914                                                                                                                                                                                                      
915         LASSERT(!obd->obd_recovering);
916
917         class_disconnect_stale_exports(obd, 0);
918
919         CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
920                obd->obd_name);
921
922         abort_recovery_queue(obd);
923         target_finish_recovery(obd);
924         ptlrpc_run_recovery_over_upcall(obd);
925 }
926 #endif
927
928 static void target_recovery_expired(unsigned long castmeharder)
929 {
930         struct obd_device *obd = (struct obd_device *)castmeharder;
931         CERROR("recovery timed out, aborting\n");
932         spin_lock_bh(&obd->obd_processing_task_lock);
933         if (obd->obd_recovering)
934                 obd->obd_abort_recovery = 1;
935
936         wake_up(&obd->obd_next_transno_waitq);
937         spin_unlock_bh(&obd->obd_processing_task_lock);
938 }
939
940
941 /* obd_processing_task_lock should be held */
942 void target_cancel_recovery_timer(struct obd_device *obd)
943 {
944         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
945         del_timer(&obd->obd_recovery_timer);
946 }
947
948 #ifdef __KERNEL__
949 static void reset_recovery_timer(struct obd_device *obd)
950 {
951         spin_lock_bh(&obd->obd_processing_task_lock);
952         if (!obd->obd_recovering) {
953                 spin_unlock_bh(&obd->obd_processing_task_lock);
954                 return;
955         }                
956         CDEBUG(D_HA, "timer will expire in %u seconds\n",
957                OBD_RECOVERY_TIMEOUT / HZ);
958         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
959         spin_unlock_bh(&obd->obd_processing_task_lock);
960 }
961 #endif
962
963 /* Only start it the first time called */
964 void target_start_recovery_timer(struct obd_device *obd)
965 {
966         spin_lock_bh(&obd->obd_processing_task_lock);
967         if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
968                 spin_unlock_bh(&obd->obd_processing_task_lock);
969                 return;
970         }
971         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
972                OBD_RECOVERY_TIMEOUT / HZ);
973         obd->obd_recovery_timer.function = target_recovery_expired;
974         obd->obd_recovery_timer.data = (unsigned long)obd;
975         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
976         spin_unlock_bh(&obd->obd_processing_task_lock);
977 }
978
979 #ifdef __KERNEL__
980 static int check_for_next_transno(struct obd_device *obd)
981 {
982         struct ptlrpc_request *req = NULL;
983         int wake_up = 0, connected, completed, queue_len, max;
984         __u64 next_transno, req_transno;
985
986         spin_lock_bh(&obd->obd_processing_task_lock);
987         if (!list_empty(&obd->obd_recovery_queue)) {
988                 req = list_entry(obd->obd_recovery_queue.next,
989                                  struct ptlrpc_request, rq_list);
990                 req_transno = req->rq_reqmsg->transno;
991         } else {
992                 req_transno = 0;
993         }
994
995         max = obd->obd_max_recoverable_clients;
996         connected = obd->obd_connected_clients;
997         completed = max - obd->obd_recoverable_clients;
998         queue_len = obd->obd_requests_queued_for_recovery;
999         next_transno = obd->obd_next_recovery_transno;
1000
1001         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1002                "req_transno: "LPU64", next_transno: "LPU64"\n",
1003                max, connected, completed, queue_len, req_transno, next_transno);
1004         if (obd->obd_abort_recovery) {
1005                 CDEBUG(D_HA, "waking for aborted recovery\n");
1006                 wake_up = 1;
1007         } else if (max == completed) {
1008                 CDEBUG(D_HA, "waking for completed recovery\n");
1009                 wake_up = 1;
1010         } else if (req_transno == next_transno) {
1011                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1012                 wake_up = 1;
1013         } else if (queue_len + completed == max) {
1014                 LASSERT(req->rq_reqmsg->transno >= next_transno);
1015                 CDEBUG(D_ERROR,
1016                        "waking for skipped transno (skip: "LPD64
1017                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1018                        next_transno, queue_len, completed, max, req_transno);
1019                 obd->obd_next_recovery_transno = req_transno;
1020                 wake_up = 1;
1021         }
1022         spin_unlock_bh(&obd->obd_processing_task_lock);
1023         
1024         return wake_up;
1025 }
1026
1027 static struct ptlrpc_request *
1028 target_next_replay_req(struct obd_device *obd)
1029 {
1030         struct l_wait_info lwi = { 0 };
1031         struct ptlrpc_request *req;
1032
1033         CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1034                obd->obd_next_recovery_transno);
1035         l_wait_event(obd->obd_next_transno_waitq,
1036                      check_for_next_transno(obd), &lwi);
1037         
1038         spin_lock_bh(&obd->obd_processing_task_lock);
1039         if (obd->obd_abort_recovery) {
1040                 req = NULL;
1041         } else if (!list_empty(&obd->obd_recovery_queue)) {
1042                 req = list_entry(obd->obd_recovery_queue.next,
1043                                  struct ptlrpc_request, rq_list);
1044                 list_del_init(&req->rq_list);
1045                 obd->obd_requests_queued_for_recovery--;
1046         } else {
1047                 req = NULL;
1048         }
1049         spin_unlock_bh(&obd->obd_processing_task_lock);
1050         return req;
1051 }
1052
1053 static int target_recovery_thread(void *arg)
1054 {
1055         struct obd_device *obd = arg;
1056         struct ptlrpc_request *req;
1057         struct target_recovery_data *trd = &obd->obd_recovery_data;
1058         unsigned long flags;
1059         ENTRY;
1060
1061         kportal_daemonize("tgt-recov");
1062
1063         SIGNAL_MASK_LOCK(current, flags);
1064         sigfillset(&current->blocked);
1065         RECALC_SIGPENDING;
1066         SIGNAL_MASK_UNLOCK(current, flags);
1067
1068         CERROR("%s: started recovery thread pid %d\n", obd->obd_name, 
1069                current->pid);
1070         trd->trd_processing_task = current->pid;
1071
1072         obd->obd_recovering = 1;
1073         complete(&trd->trd_starting);
1074
1075         while (obd->obd_recovering) {
1076                 LASSERT(trd->trd_processing_task == current->pid);
1077                 req = target_next_replay_req(obd);
1078                 if (req != NULL) {
1079                         char peer_str[PTL_NALFMT_SIZE];
1080                         DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ", 
1081                                   req->rq_reqmsg->transno, 
1082                                   ptlrpc_peernid2str(&req->rq_peer, peer_str));
1083                         (void)trd->trd_recovery_handler(req);
1084                         obd->obd_replayed_requests++;
1085                         reset_recovery_timer(obd);
1086                         /* bug 1580: decide how to properly sync() in recovery*/
1087                         //mds_fsync_super(mds->mds_sb);
1088                         ptlrpc_free_clone(req);
1089                         spin_lock_bh(&obd->obd_processing_task_lock);
1090                         obd->obd_next_recovery_transno++;
1091                         spin_unlock_bh(&obd->obd_processing_task_lock);
1092                 } else {
1093                         /* recovery is over */
1094                         spin_lock_bh(&obd->obd_processing_task_lock);
1095                         obd->obd_recovering = 0;
1096                         target_cancel_recovery_timer(obd);
1097                         if (obd->obd_abort_recovery) {
1098                                 obd->obd_abort_recovery = 0;
1099                                 spin_unlock_bh(&obd->obd_processing_task_lock);
1100                                 target_abort_recovery(obd); 
1101                         } else {
1102                                 LASSERT(obd->obd_recoverable_clients == 0);
1103                                 spin_unlock_bh(&obd->obd_processing_task_lock);
1104                                 target_finish_recovery(obd);
1105                         }
1106                 }
1107         }
1108
1109         trd->trd_processing_task = 0;
1110         complete(&trd->trd_finishing);
1111         return 0;
1112 }
1113
1114 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1115 {
1116         int rc = 0;
1117         struct target_recovery_data *trd = &obd->obd_recovery_data;
1118
1119         memset(trd, 0, sizeof(*trd));
1120         init_completion(&trd->trd_starting);
1121         init_completion(&trd->trd_finishing);
1122         trd->trd_recovery_handler = handler;
1123
1124         if (kernel_thread(target_recovery_thread, obd, 0) == 0) 
1125                 wait_for_completion(&trd->trd_starting);
1126         else
1127                 rc = -ECHILD;
1128
1129         return rc;
1130 }
1131
1132 void target_stop_recovery_thread(struct obd_device *obd)
1133 {
1134         spin_lock_bh(&obd->obd_processing_task_lock);
1135         if (obd->obd_recovery_data.trd_processing_task > 0) {
1136                 struct target_recovery_data *trd = &obd->obd_recovery_data;
1137                 CERROR("%s: aborting recovery\n", obd->obd_name);
1138                 obd->obd_abort_recovery = 1;
1139                 wake_up(&obd->obd_next_transno_waitq);
1140                 spin_unlock_bh(&obd->obd_processing_task_lock);
1141                 wait_for_completion(&trd->trd_finishing);
1142         } else {
1143                 spin_unlock_bh(&obd->obd_processing_task_lock);
1144         }
1145 }
1146 #endif
1147
1148 int target_queue_recovery_request(struct ptlrpc_request *req,
1149                                   struct obd_device *obd)
1150 {
1151         struct list_head *tmp;
1152         int inserted = 0;
1153         __u64 transno = req->rq_reqmsg->transno;
1154
1155         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1156          * (i.e. buflens etc are in my own byte order), but type-dependent
1157          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1158
1159         if (!transno) {
1160                 INIT_LIST_HEAD(&req->rq_list);
1161                 DEBUG_REQ(D_HA, req, "not queueing");
1162                 return 1;
1163         }
1164
1165
1166         /* If we're processing the queue, we want don't want to queue this
1167          * message.
1168          *
1169          * Also, if this request has a transno less than the one we're waiting
1170          * for, we should process it now.  It could (and currently always will)
1171          * be an open request for a descriptor that was opened some time ago.
1172          *
1173          * Also, a resent, replayed request that has already been
1174          * handled will pass through here and be processed immediately.
1175          */
1176         spin_lock_bh(&obd->obd_processing_task_lock);
1177         if (obd->obd_recovery_data.trd_processing_task == current->pid ||
1178             transno < obd->obd_next_recovery_transno) {
1179                 /* Processing the queue right now, don't re-add. */
1180                 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1181                 LASSERT(list_empty(&req->rq_list));
1182                 spin_unlock_bh(&obd->obd_processing_task_lock);
1183                 return 1;
1184         }
1185         spin_unlock_bh(&obd->obd_processing_task_lock);
1186
1187         /* A resent, replayed request that is still on the queue; just drop it.
1188            The queued request will handle this. */
1189         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1190             == (MSG_RESENT | MSG_REPLAY)) {
1191                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1192                 return 0;
1193         }
1194
1195         req = ptlrpc_clone_req(req);
1196         if (req == NULL)
1197                 return -ENOMEM;
1198
1199         spin_lock_bh(&obd->obd_processing_task_lock);
1200
1201         /* XXX O(n^2) */
1202         list_for_each(tmp, &obd->obd_recovery_queue) {
1203                 struct ptlrpc_request *reqiter =
1204                         list_entry(tmp, struct ptlrpc_request, rq_list);
1205
1206                 if (reqiter->rq_reqmsg->transno > transno) {
1207                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1208                         inserted = 1;
1209                         break;
1210                 }
1211         }
1212
1213         if (!inserted) {
1214                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1215         }
1216
1217         obd->obd_requests_queued_for_recovery++;
1218         wake_up(&obd->obd_next_transno_waitq);
1219         spin_unlock_bh(&obd->obd_processing_task_lock);
1220
1221         return 0;
1222 }
1223
1224 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1225 {
1226         return req->rq_export->exp_obd;
1227 }
1228
1229 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1230 {
1231         struct obd_device *obd = target_req2obd(req);
1232
1233         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1234
1235         if (rc) {
1236                 /* Just like ptlrpc_error, but without the sending. */
1237                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1238                 LASSERT(rc == 0); /* XXX handle this */
1239                 req->rq_type = PTL_RPC_MSG_ERR;
1240         }
1241
1242         LASSERT (!req->rq_reply_state->rs_difficult);
1243         LASSERT(list_empty(&req->rq_list));
1244         
1245         req = ptlrpc_clone_req(req);
1246
1247         spin_lock_bh(&obd->obd_processing_task_lock);
1248
1249         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1250
1251         /* only count the first "replay over" request from each
1252            export */
1253         if (req->rq_export->exp_replay_needed) {
1254                 --obd->obd_recoverable_clients;
1255                 req->rq_export->exp_replay_needed = 0;
1256                 CWARN("%s: %d recoverable clients remain\n",
1257                       obd->obd_name, obd->obd_recoverable_clients);
1258         }
1259         wake_up(&obd->obd_next_transno_waitq);
1260         spin_unlock_bh(&obd->obd_processing_task_lock);
1261         return 1;
1262 }
1263
1264 int
1265 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1266 {
1267         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1268                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1269                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1270                 /* NB this does _not_ send with ACK disabled, to simulate
1271                  * sending OK, but timing out for the ACK */
1272                 if (req->rq_reply_state != NULL) {
1273                         if (!req->rq_reply_state->rs_difficult) {
1274                                 lustre_free_reply_state (req->rq_reply_state);
1275                                 req->rq_reply_state = NULL;
1276                         } else {
1277                                 struct ptlrpc_service *svc =
1278                                         req->rq_rqbd->rqbd_srv_ni->sni_service;
1279                                 atomic_inc(&svc->srv_outstanding_replies);
1280                         }
1281                 }
1282                 return (-ECOMM);
1283         }
1284
1285         if (rc) {
1286                 req->rq_status = rc;
1287                 return (ptlrpc_error(req));
1288         } else {
1289                 DEBUG_REQ(D_NET, req, "sending reply");
1290         }
1291         
1292         return (ptlrpc_send_reply(req, 1));
1293 }
1294
1295 void 
1296 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1297 {
1298         int                        netrc;
1299         unsigned long              flags;
1300         struct ptlrpc_reply_state *rs;
1301         struct obd_device         *obd;
1302         struct obd_export         *exp;
1303         struct ptlrpc_srv_ni      *sni;
1304         struct ptlrpc_service     *svc;
1305
1306         sni = req->rq_rqbd->rqbd_srv_ni;
1307         svc = sni->sni_service;
1308         
1309         rs = req->rq_reply_state;
1310         if (rs == NULL || !rs->rs_difficult) {
1311                 /* The easy case; no notifiers and reply_out_callback()
1312                  * cleans up (i.e. we can't look inside rs after a
1313                  * successful send) */
1314                 netrc = target_send_reply_msg (req, rc, fail_id);
1315
1316                 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1317                 return;
1318         }
1319
1320         /* must be an export if locks saved */
1321         LASSERT (req->rq_export != NULL);
1322         /* req/reply consistent */
1323         LASSERT (rs->rs_srv_ni == sni);
1324
1325         /* "fresh" reply */
1326         LASSERT (!rs->rs_scheduled);
1327         LASSERT (!rs->rs_scheduled_ever);
1328         LASSERT (!rs->rs_handled);
1329         LASSERT (!rs->rs_on_net);
1330         LASSERT (rs->rs_export == NULL);
1331         LASSERT (list_empty(&rs->rs_obd_list));
1332         LASSERT (list_empty(&rs->rs_exp_list));
1333
1334         exp = class_export_get (req->rq_export);
1335         obd = exp->exp_obd;
1336
1337         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1338         rs->rs_scheduled = 1;
1339         rs->rs_on_net    = 1;
1340         rs->rs_xid       = req->rq_xid;
1341         rs->rs_transno   = req->rq_transno;
1342         rs->rs_export    = exp;
1343         
1344         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1345
1346         if (rs->rs_transno > obd->obd_last_committed) {
1347                 /* not committed already */ 
1348                 list_add_tail (&rs->rs_obd_list, 
1349                                &obd->obd_uncommitted_replies);
1350         }
1351
1352         spin_unlock (&obd->obd_uncommitted_replies_lock);
1353         spin_lock (&exp->exp_lock);
1354
1355         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1356
1357         spin_unlock_irqrestore (&exp->exp_lock, flags);
1358
1359         netrc = target_send_reply_msg (req, rc, fail_id);
1360
1361         spin_lock_irqsave (&svc->srv_lock, flags);
1362
1363         svc->srv_n_difficult_replies++;
1364
1365         if (netrc != 0) /* error sending: reply is off the net */
1366                 rs->rs_on_net = 0;
1367
1368         if (!rs->rs_on_net ||                   /* some notifier */
1369             list_empty(&rs->rs_exp_list) ||     /* completed already */
1370             list_empty(&rs->rs_obd_list)) {
1371                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1372                 wake_up (&svc->srv_waitq);
1373         } else {
1374                 list_add (&rs->rs_list, &sni->sni_active_replies);
1375                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1376         }
1377
1378         spin_unlock_irqrestore (&svc->srv_lock, flags);
1379 }
1380
1381 int target_handle_ping(struct ptlrpc_request *req)
1382 {
1383         return lustre_pack_reply(req, 0, NULL, NULL);
1384 }