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