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