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