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