Whamcloud - gitweb
- unland b_fid to HEAD
[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 failover)
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);
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 static void target_finish_recovery(struct obd_device *obd)
814 {
815         struct list_head *tmp, *n;
816         int rc;
817
818         CWARN("%s: sending delayed replies to recovered clients\n",
819               obd->obd_name);
820
821         ldlm_reprocess_all_ns(obd->obd_namespace);
822
823         /* when recovery finished, cleanup orphans on mds and ost */
824         if (OBT(obd) && OBP(obd, postrecov)) {
825                 rc = OBP(obd, postrecov)(obd);
826                 if (rc >= 0)
827                         CWARN("%s: all clients recovered, %d MDS "
828                               "orphans deleted\n", obd->obd_name, rc);
829                 else
830                         CERROR("postrecov failed %d\n", rc);
831         }
832
833
834         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
835                 struct ptlrpc_request *req;
836                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
837                 list_del(&req->rq_list);
838                 DEBUG_REQ(D_ERROR, req, "delayed:");
839                 ptlrpc_reply(req);
840                 target_release_saved_req(req);
841         }
842         obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
843         return;
844 }
845
846 static void abort_recovery_queue(struct obd_device *obd)
847 {
848         struct ptlrpc_request *req;
849         struct list_head *tmp, *n;
850         int rc;
851
852         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
853                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
854                 list_del(&req->rq_list);
855                 DEBUG_REQ(D_ERROR, req, "aborted:");
856                 req->rq_status = -ENOTCONN;
857                 req->rq_type = PTL_RPC_MSG_ERR;
858                 rc = lustre_pack_reply(req, 0, NULL, NULL);
859                 if (rc == 0) {
860                         ptlrpc_reply(req);
861                 } else {
862                         DEBUG_REQ(D_ERROR, req,
863                                   "packing failed for abort-reply; skipping");
864                 }
865                 target_release_saved_req(req);
866         }
867 }
868 /* Called from a cleanup function if the device is being cleaned up
869    forcefully.  The exports should all have been disconnected already,
870    the only thing left to do is
871      - clear the recovery flags
872      - cancel the timer
873      - free queued requests and replies, but don't send replies
874    Because the obd_stopping flag is set, no new requests should be received.
875
876 */
877 void target_cleanup_recovery(struct obd_device *obd)
878 {
879         struct list_head *tmp, *n;
880         struct ptlrpc_request *req;
881
882         spin_lock_bh(&obd->obd_processing_task_lock);
883         if (!obd->obd_recovering) {
884                 spin_unlock_bh(&obd->obd_processing_task_lock);
885                 EXIT;
886                 return;
887         }
888         obd->obd_recovering = obd->obd_abort_recovery = 0;
889         target_cancel_recovery_timer(obd);
890         spin_unlock_bh(&obd->obd_processing_task_lock);
891
892         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
893                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
894                 list_del(&req->rq_list);
895                 LASSERT (req->rq_reply_state);
896                 lustre_free_reply_state(req->rq_reply_state);
897                 target_release_saved_req(req);
898         }
899         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
900                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
901                 list_del(&req->rq_list);
902                 LASSERT (req->rq_reply_state == 0);
903                 target_release_saved_req(req);
904          }
905 }
906
907 static void target_abort_recovery(void *data)
908 {
909         struct obd_device *obd = data;
910                                                                                                                                                                                                      
911         LASSERT(!obd->obd_recovering);
912
913         class_disconnect_stale_exports(obd, 0);
914
915         CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
916                obd->obd_name);
917
918         abort_recovery_queue(obd);
919         target_finish_recovery(obd);
920         ptlrpc_run_recovery_over_upcall(obd);
921 }
922
923 static void target_recovery_expired(unsigned long castmeharder)
924 {
925         struct obd_device *obd = (struct obd_device *)castmeharder;
926         CERROR("recovery timed out, aborting\n");
927         spin_lock_bh(&obd->obd_processing_task_lock);
928         if (obd->obd_recovering)
929                 obd->obd_abort_recovery = 1;
930
931         wake_up(&obd->obd_next_transno_waitq);
932         spin_unlock_bh(&obd->obd_processing_task_lock);
933 }
934
935
936 /* obd_processing_task_lock should be held */
937 void target_cancel_recovery_timer(struct obd_device *obd)
938 {
939         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
940         del_timer(&obd->obd_recovery_timer);
941 }
942
943 static void reset_recovery_timer(struct obd_device *obd)
944 {
945         spin_lock_bh(&obd->obd_processing_task_lock);
946         if (!obd->obd_recovering) {
947                 spin_unlock_bh(&obd->obd_processing_task_lock);
948                 return;
949         }                
950         CDEBUG(D_HA, "timer will expire in %u seconds\n",
951                OBD_RECOVERY_TIMEOUT / HZ);
952         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
953         spin_unlock_bh(&obd->obd_processing_task_lock);
954 }
955
956
957 /* Only start it the first time called */
958 void target_start_recovery_timer(struct obd_device *obd)
959 {
960         spin_lock_bh(&obd->obd_processing_task_lock);
961         if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
962                 spin_unlock_bh(&obd->obd_processing_task_lock);
963                 return;
964         }
965         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
966                OBD_RECOVERY_TIMEOUT / HZ);
967         obd->obd_recovery_timer.function = target_recovery_expired;
968         obd->obd_recovery_timer.data = (unsigned long)obd;
969         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
970         spin_unlock_bh(&obd->obd_processing_task_lock);
971 }
972
973 static int check_for_next_transno(struct obd_device *obd)
974 {
975         struct ptlrpc_request *req = NULL;
976         int wake_up = 0, connected, completed, queue_len, max;
977         __u64 next_transno, req_transno;
978
979         spin_lock_bh(&obd->obd_processing_task_lock);
980         if (!list_empty(&obd->obd_recovery_queue)) {
981                 req = list_entry(obd->obd_recovery_queue.next,
982                                  struct ptlrpc_request, rq_list);
983                 req_transno = req->rq_reqmsg->transno;
984         } else {
985                 req_transno = 0;
986         }
987
988         max = obd->obd_max_recoverable_clients;
989         connected = obd->obd_connected_clients;
990         completed = max - obd->obd_recoverable_clients;
991         queue_len = obd->obd_requests_queued_for_recovery;
992         next_transno = obd->obd_next_recovery_transno;
993
994         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
995                "req_transno: "LPU64", next_transno: "LPU64"\n",
996                max, connected, completed, queue_len, req_transno, next_transno);
997         if (obd->obd_abort_recovery) {
998                 CDEBUG(D_HA, "waking for aborted recovery\n");
999                 wake_up = 1;
1000         } else if (max == completed) {
1001                 CDEBUG(D_HA, "waking for completed recovery\n");
1002                 wake_up = 1;
1003         } else if (req_transno == next_transno) {
1004                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1005                 wake_up = 1;
1006         } else if (queue_len + completed == max) {
1007                 LASSERT(req->rq_reqmsg->transno >= next_transno);
1008                 CDEBUG(D_ERROR,
1009                        "waking for skipped transno (skip: "LPD64
1010                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1011                        next_transno, queue_len, completed, max, req_transno);
1012                 obd->obd_next_recovery_transno = req_transno;
1013                 wake_up = 1;
1014         }
1015         spin_unlock_bh(&obd->obd_processing_task_lock);
1016         
1017         return wake_up;
1018 }
1019
1020 static struct ptlrpc_request *
1021 target_next_replay_req(struct obd_device *obd)
1022 {
1023         struct l_wait_info lwi = { 0 };
1024         struct ptlrpc_request *req;
1025
1026         CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1027                obd->obd_next_recovery_transno);
1028         l_wait_event(obd->obd_next_transno_waitq,
1029                      check_for_next_transno(obd), &lwi);
1030         
1031         spin_lock_bh(&obd->obd_processing_task_lock);
1032         if (obd->obd_abort_recovery) {
1033                 req = NULL;
1034         } else if (!list_empty(&obd->obd_recovery_queue)) {
1035                 req = list_entry(obd->obd_recovery_queue.next,
1036                                  struct ptlrpc_request, rq_list);
1037                 list_del_init(&req->rq_list);
1038                 obd->obd_requests_queued_for_recovery--;
1039         } else {
1040                 req = NULL;
1041         }
1042         spin_unlock_bh(&obd->obd_processing_task_lock);
1043         return req;
1044 }
1045
1046 #ifdef __KERNEL__
1047 static int target_recovery_thread(void *arg)
1048 {
1049         struct obd_device *obd = arg;
1050         struct ptlrpc_request *req;
1051         struct target_recovery_data *trd = &obd->obd_recovery_data;
1052         unsigned long flags;
1053         ENTRY;
1054
1055         kportal_daemonize("tgt-recov");
1056
1057         SIGNAL_MASK_LOCK(current, flags);
1058         sigfillset(&current->blocked);
1059         RECALC_SIGPENDING;
1060         SIGNAL_MASK_UNLOCK(current, flags);
1061
1062         CERROR("%s: started recovery thread pid %d\n", obd->obd_name, 
1063                current->pid);
1064         trd->trd_processing_task = current->pid;
1065
1066         obd->obd_recovering = 1;
1067         complete(&trd->trd_starting);
1068
1069         while (obd->obd_recovering) {
1070                 LASSERT(trd->trd_processing_task == current->pid);
1071                 req = target_next_replay_req(obd);
1072                 if (req != NULL) {
1073                         char peer_str[PTL_NALFMT_SIZE];
1074                         DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ", 
1075                                   req->rq_reqmsg->transno, 
1076                                   ptlrpc_peernid2str(&req->rq_peer, peer_str));
1077                         (void)trd->trd_recovery_handler(req);
1078                         obd->obd_replayed_requests++;
1079                         reset_recovery_timer(obd);
1080                         /* bug 1580: decide how to properly sync() in recovery*/
1081                         //mds_fsync_super(mds->mds_sb);
1082                         ptlrpc_free_clone(req);
1083                         spin_lock_bh(&obd->obd_processing_task_lock);
1084                         obd->obd_next_recovery_transno++;
1085                         spin_unlock_bh(&obd->obd_processing_task_lock);
1086                 } else {
1087                         /* recovery is over */
1088                         spin_lock_bh(&obd->obd_processing_task_lock);
1089                         obd->obd_recovering = 0;
1090                         target_cancel_recovery_timer(obd);
1091                         if (obd->obd_abort_recovery) {
1092                                 obd->obd_abort_recovery = 0;
1093                                 spin_unlock_bh(&obd->obd_processing_task_lock);
1094                                 target_abort_recovery(obd); 
1095                         } else {
1096                                 LASSERT(obd->obd_recoverable_clients == 0);
1097                                 spin_unlock_bh(&obd->obd_processing_task_lock);
1098                                 target_finish_recovery(obd);
1099                         }
1100                 }
1101         }
1102
1103         trd->trd_processing_task = 0;
1104         complete(&trd->trd_finishing);
1105         return 0;
1106 }
1107
1108 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1109 {
1110         int rc = 0;
1111         struct target_recovery_data *trd = &obd->obd_recovery_data;
1112
1113         memset(trd, 0, sizeof(*trd));
1114         init_completion(&trd->trd_starting);
1115         init_completion(&trd->trd_finishing);
1116         trd->trd_recovery_handler = handler;
1117
1118         if (kernel_thread(target_recovery_thread, obd, 0) == 0) 
1119                 wait_for_completion(&trd->trd_starting);
1120         else
1121                 rc = -ECHILD;
1122
1123         return rc;
1124 }
1125
1126 void target_stop_recovery_thread(struct obd_device *obd)
1127 {
1128         spin_lock_bh(&obd->obd_processing_task_lock);
1129         if (obd->obd_recovery_data.trd_processing_task > 0) {
1130                 struct target_recovery_data *trd = &obd->obd_recovery_data;
1131                 CERROR("%s: aborting recovery\n", obd->obd_name);
1132                 obd->obd_abort_recovery = 1;
1133                 wake_up(&obd->obd_next_transno_waitq);
1134                 spin_unlock_bh(&obd->obd_processing_task_lock);
1135                 wait_for_completion(&trd->trd_finishing);
1136         } else {
1137                 spin_unlock_bh(&obd->obd_processing_task_lock);
1138         }
1139 }
1140 #endif
1141 int target_queue_recovery_request(struct ptlrpc_request *req,
1142                                   struct obd_device *obd)
1143 {
1144         struct list_head *tmp;
1145         int inserted = 0;
1146         __u64 transno = req->rq_reqmsg->transno;
1147
1148         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1149          * (i.e. buflens etc are in my own byte order), but type-dependent
1150          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1151
1152         if (!transno) {
1153                 INIT_LIST_HEAD(&req->rq_list);
1154                 DEBUG_REQ(D_HA, req, "not queueing");
1155                 return 1;
1156         }
1157
1158
1159         /* If we're processing the queue, we want don't want to queue this
1160          * message.
1161          *
1162          * Also, if this request has a transno less than the one we're waiting
1163          * for, we should process it now.  It could (and currently always will)
1164          * be an open request for a descriptor that was opened some time ago.
1165          *
1166          * Also, a resent, replayed request that has already been
1167          * handled will pass through here and be processed immediately.
1168          */
1169         spin_lock_bh(&obd->obd_processing_task_lock);
1170         if (obd->obd_recovery_data.trd_processing_task == current->pid ||
1171             transno < obd->obd_next_recovery_transno) {
1172                 /* Processing the queue right now, don't re-add. */
1173                 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1174                 LASSERT(list_empty(&req->rq_list));
1175                 spin_unlock_bh(&obd->obd_processing_task_lock);
1176                 return 1;
1177         }
1178         spin_unlock_bh(&obd->obd_processing_task_lock);
1179
1180         /* A resent, replayed request that is still on the queue; just drop it.
1181            The queued request will handle this. */
1182         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1183             == (MSG_RESENT | MSG_REPLAY)) {
1184                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1185                 return 0;
1186         }
1187
1188         req = ptlrpc_clone_req(req);
1189         if (req == NULL)
1190                 return -ENOMEM;
1191
1192         spin_lock_bh(&obd->obd_processing_task_lock);
1193
1194         /* XXX O(n^2) */
1195         list_for_each(tmp, &obd->obd_recovery_queue) {
1196                 struct ptlrpc_request *reqiter =
1197                         list_entry(tmp, struct ptlrpc_request, rq_list);
1198
1199                 if (reqiter->rq_reqmsg->transno > transno) {
1200                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1201                         inserted = 1;
1202                         break;
1203                 }
1204         }
1205
1206         if (!inserted) {
1207                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1208         }
1209
1210         obd->obd_requests_queued_for_recovery++;
1211         wake_up(&obd->obd_next_transno_waitq);
1212         spin_unlock_bh(&obd->obd_processing_task_lock);
1213
1214         return 0;
1215 }
1216
1217 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1218 {
1219         return req->rq_export->exp_obd;
1220 }
1221
1222 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1223 {
1224         struct obd_device *obd = target_req2obd(req);
1225
1226         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1227
1228         if (rc) {
1229                 /* Just like ptlrpc_error, but without the sending. */
1230                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1231                 LASSERT(rc == 0); /* XXX handle this */
1232                 req->rq_type = PTL_RPC_MSG_ERR;
1233         }
1234
1235         LASSERT (!req->rq_reply_state->rs_difficult);
1236         LASSERT(list_empty(&req->rq_list));
1237         
1238         req = ptlrpc_clone_req(req);
1239
1240         spin_lock_bh(&obd->obd_processing_task_lock);
1241
1242         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1243
1244         /* only count the first "replay over" request from each
1245            export */
1246         if (req->rq_export->exp_replay_needed) {
1247                 --obd->obd_recoverable_clients;
1248                 req->rq_export->exp_replay_needed = 0;
1249                 CWARN("%s: %d recoverable clients remain\n",
1250                       obd->obd_name, obd->obd_recoverable_clients);
1251         }
1252         wake_up(&obd->obd_next_transno_waitq);
1253         spin_unlock_bh(&obd->obd_processing_task_lock);
1254         return 1;
1255 }
1256
1257 int
1258 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1259 {
1260         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1261                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1262                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1263                 /* NB this does _not_ send with ACK disabled, to simulate
1264                  * sending OK, but timing out for the ACK */
1265                 if (req->rq_reply_state != NULL) {
1266                         if (!req->rq_reply_state->rs_difficult) {
1267                                 lustre_free_reply_state (req->rq_reply_state);
1268                                 req->rq_reply_state = NULL;
1269                         } else {
1270                                 struct ptlrpc_service *svc =
1271                                         req->rq_rqbd->rqbd_srv_ni->sni_service;
1272                                 atomic_inc(&svc->srv_outstanding_replies);
1273                         }
1274                 }
1275                 return (-ECOMM);
1276         }
1277
1278         if (rc) {
1279                 req->rq_status = rc;
1280                 return (ptlrpc_error(req));
1281         } else {
1282                 DEBUG_REQ(D_NET, req, "sending reply");
1283         }
1284         
1285         return (ptlrpc_send_reply(req, 1));
1286 }
1287
1288 void 
1289 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1290 {
1291         int                        netrc;
1292         unsigned long              flags;
1293         struct ptlrpc_reply_state *rs;
1294         struct obd_device         *obd;
1295         struct obd_export         *exp;
1296         struct ptlrpc_srv_ni      *sni;
1297         struct ptlrpc_service     *svc;
1298
1299         sni = req->rq_rqbd->rqbd_srv_ni;
1300         svc = sni->sni_service;
1301         
1302         rs = req->rq_reply_state;
1303         if (rs == NULL || !rs->rs_difficult) {
1304                 /* The easy case; no notifiers and reply_out_callback()
1305                  * cleans up (i.e. we can't look inside rs after a
1306                  * successful send) */
1307                 netrc = target_send_reply_msg (req, rc, fail_id);
1308
1309                 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1310                 return;
1311         }
1312
1313         /* must be an export if locks saved */
1314         LASSERT (req->rq_export != NULL);
1315         /* req/reply consistent */
1316         LASSERT (rs->rs_srv_ni == sni);
1317
1318         /* "fresh" reply */
1319         LASSERT (!rs->rs_scheduled);
1320         LASSERT (!rs->rs_scheduled_ever);
1321         LASSERT (!rs->rs_handled);
1322         LASSERT (!rs->rs_on_net);
1323         LASSERT (rs->rs_export == NULL);
1324         LASSERT (list_empty(&rs->rs_obd_list));
1325         LASSERT (list_empty(&rs->rs_exp_list));
1326
1327         exp = class_export_get (req->rq_export);
1328         obd = exp->exp_obd;
1329
1330         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1331         rs->rs_scheduled = 1;
1332         rs->rs_on_net    = 1;
1333         rs->rs_xid       = req->rq_xid;
1334         rs->rs_transno   = req->rq_transno;
1335         rs->rs_export    = exp;
1336         
1337         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1338
1339         if (rs->rs_transno > obd->obd_last_committed) {
1340                 /* not committed already */ 
1341                 list_add_tail (&rs->rs_obd_list, 
1342                                &obd->obd_uncommitted_replies);
1343         }
1344
1345         spin_unlock (&obd->obd_uncommitted_replies_lock);
1346         spin_lock (&exp->exp_lock);
1347
1348         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1349
1350         spin_unlock_irqrestore (&exp->exp_lock, flags);
1351
1352         netrc = target_send_reply_msg (req, rc, fail_id);
1353
1354         spin_lock_irqsave (&svc->srv_lock, flags);
1355
1356         svc->srv_n_difficult_replies++;
1357
1358         if (netrc != 0) /* error sending: reply is off the net */
1359                 rs->rs_on_net = 0;
1360
1361         if (!rs->rs_on_net ||                   /* some notifier */
1362             list_empty(&rs->rs_exp_list) ||     /* completed already */
1363             list_empty(&rs->rs_obd_list)) {
1364                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1365                 wake_up (&svc->srv_waitq);
1366         } else {
1367                 list_add (&rs->rs_list, &sni->sni_active_replies);
1368                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1369         }
1370
1371         spin_unlock_irqrestore (&svc->srv_lock, flags);
1372 }
1373
1374 int target_handle_ping(struct ptlrpc_request *req)
1375 {
1376         return lustre_pack_reply(req, 0, NULL, NULL);
1377 }