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