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