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