Whamcloud - gitweb
Landing b1_4_quotaoff to b1_4
[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
39 /* @priority: if non-zero, move the selected to the list head
40  * @create: if zero, only search in existed connections
41  */
42 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
43                            int priority, int create)
44 {
45         struct ptlrpc_connection *ptlrpc_conn;
46         struct obd_import_conn *imp_conn = NULL, *item;
47         int rc = 0;
48         ENTRY;
49
50         if (!create && !priority) {
51                 CDEBUG(D_HA, "Nothing to do\n");
52                 RETURN(-EINVAL);
53         }
54
55         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
56         if (!ptlrpc_conn) {
57                 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
58                 RETURN (-ENOENT);
59         }
60
61         if (create) {
62                 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
63                 if (!imp_conn) {
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: found existing conn %s%s\n",
77                                imp, imp->imp_obd->obd_name, uuid->uuid,
78                                (priority ? ", moved to head" : ""));
79                         spin_unlock(&imp->imp_lock);
80                         GOTO(out_free, rc = 0);
81                 }
82         }
83         /* not found */
84         if (create) {
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                 spin_unlock(&imp->imp_lock);
97                 GOTO(out_free, rc = -ENOENT);
98                 
99         }
100
101         spin_unlock(&imp->imp_lock);
102         RETURN(0);
103 out_free:
104         if (imp_conn)
105                 OBD_FREE(imp_conn, sizeof(*imp_conn));
106 out_put:
107         ptlrpc_put_connection(ptlrpc_conn);
108         RETURN(rc);
109 }
110
111 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
112 {
113         return import_set_conn(imp, uuid, 1, 0);
114 }
115
116 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
117                            int priority)
118 {
119         return import_set_conn(imp, uuid, priority, 1);
120 }
121
122 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
123 {
124         struct obd_import_conn *imp_conn;
125         struct obd_export *dlmexp;
126         int rc = -ENOENT;
127         ENTRY;
128
129         spin_lock(&imp->imp_lock);
130         if (list_empty(&imp->imp_conn_list)) {
131                 LASSERT(!imp->imp_conn_current);
132                 LASSERT(!imp->imp_connection);
133                 GOTO(out, rc);
134         }
135
136         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
137                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
138                         continue;
139                 LASSERT(imp_conn->oic_conn);
140
141                 /* is current conn? */
142                 if (imp_conn == imp->imp_conn_current) {
143                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
144
145                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
146                             imp->imp_state != LUSTRE_IMP_DISCON) {
147                                 CERROR("can't remove current connection\n");
148                                 GOTO(out, rc = -EBUSY);
149                         }
150
151                         ptlrpc_put_connection(imp->imp_connection);
152                         imp->imp_connection = NULL;
153
154                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
155                         if (dlmexp && dlmexp->exp_connection) {
156                                 LASSERT(dlmexp->exp_connection ==
157                                         imp_conn->oic_conn);
158                                 ptlrpc_put_connection(dlmexp->exp_connection);
159                                 dlmexp->exp_connection = NULL;
160                         }
161                 }
162
163                 list_del(&imp_conn->oic_item);
164                 ptlrpc_put_connection(imp_conn->oic_conn);
165                 OBD_FREE(imp_conn, sizeof(*imp_conn));
166                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
167                        imp, imp->imp_obd->obd_name, uuid->uuid);
168                 rc = 0;
169                 break;
170         }
171 out:
172         spin_unlock(&imp->imp_lock);
173         if (rc == -ENOENT)
174                 CERROR("connection %s not found\n", uuid->uuid);
175         RETURN(rc);
176 }
177
178 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
179 {
180         struct lustre_cfg* lcfg = buf;
181         struct client_obd *cli = &obddev->u.cli;
182         struct obd_import *imp;
183         struct obd_uuid server_uuid;
184         int rq_portal, rp_portal, connect_op;
185         char *name = obddev->obd_type->typ_name;
186         char *mgmt_name = NULL;
187         int rc;
188         struct obd_device *mgmt_obd;
189         mgmtcli_register_for_events_t register_f;
190         ENTRY;
191
192         /* In a more perfect world, we would hang a ptlrpc_client off of
193          * obd_type and just use the values from there. */
194         if (!strcmp(name, LUSTRE_OSC_NAME)) {
195                 rq_portal = OST_REQUEST_PORTAL;
196                 rp_portal = OSC_REPLY_PORTAL;
197                 connect_op = OST_CONNECT;
198         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
199                 rq_portal = MDS_REQUEST_PORTAL;
200                 rp_portal = MDC_REPLY_PORTAL;
201                 connect_op = MDS_CONNECT;
202         } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
203                 rq_portal = MGMT_REQUEST_PORTAL;
204                 rp_portal = MGMT_REPLY_PORTAL;
205                 connect_op = MGMT_CONNECT;
206         } else {
207                 CERROR("unknown client OBD type \"%s\", can't setup\n",
208                        name);
209                 RETURN(-EINVAL);
210         }
211
212         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
213                 CERROR("requires a TARGET UUID\n");
214                 RETURN(-EINVAL);
215         }
216
217         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
218                 CERROR("client UUID must be less than 38 characters\n");
219                 RETURN(-EINVAL);
220         }
221
222         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
223                 CERROR("setup requires a SERVER UUID\n");
224                 RETURN(-EINVAL);
225         }
226
227         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
228                 CERROR("target UUID must be less than 38 characters\n");
229                 RETURN(-EINVAL);
230         }
231
232         sema_init(&cli->cl_sem, 1);
233         cli->cl_conn_count = 0;
234         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
235                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
236                      sizeof(server_uuid)));
237
238         cli->cl_dirty = 0;
239         cli->cl_avail_grant = 0;
240         /* FIXME: should limit this for the sum of all cl_dirty_max */
241         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
242         if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
243                 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
244         INIT_LIST_HEAD(&cli->cl_cache_waiters);
245         INIT_LIST_HEAD(&cli->cl_loi_ready_list);
246         INIT_LIST_HEAD(&cli->cl_loi_write_list);
247         INIT_LIST_HEAD(&cli->cl_loi_read_list);
248         spin_lock_init(&cli->cl_loi_list_lock);
249         cli->cl_r_in_flight = 0;
250         cli->cl_w_in_flight = 0;
251         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
252         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
253         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
254         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
255         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
256         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
257         if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
258                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
259                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
260         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
261                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
262                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
263         } else {
264                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
265                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
266         }
267
268         rc = ldlm_get_ref();
269         if (rc) {
270                 CERROR("ldlm_get_ref failed: %d\n", rc);
271                 GOTO(err, rc);
272         }
273
274         ptlrpc_init_client(rq_portal, rp_portal, name,
275                            &obddev->obd_ldlm_client);
276
277         imp = class_new_import();
278         if (imp == NULL)
279                 GOTO(err_ldlm, rc = -ENOENT);
280         imp->imp_client = &obddev->obd_ldlm_client;
281         imp->imp_obd = obddev;
282         imp->imp_connect_op = connect_op;
283         imp->imp_generation = 0;
284         imp->imp_initial_recov = 1;
285         INIT_LIST_HEAD(&imp->imp_pinger_chain);
286         memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
287                LUSTRE_CFG_BUFLEN(lcfg, 1));
288         class_import_put(imp);
289
290         rc = client_import_add_conn(imp, &server_uuid, 1);
291         if (rc) {
292                 CERROR("can't add initial connection\n");
293                 GOTO(err_import, rc);
294         }
295
296         cli->cl_import = imp;
297         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
298         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
299         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
300         cli->cl_sandev = to_kdev_t(0);
301
302         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
303                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
304                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
305                                name, obddev->obd_name,
306                                imp->imp_target_uuid.uuid);
307                         imp->imp_invalid = 1;
308
309                         if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0)
310                                 mgmt_name = lustre_cfg_string(lcfg, 4);
311                 } else {
312                         mgmt_name = lustre_cfg_string(lcfg, 3);
313                 }
314         }
315
316         if (mgmt_name != NULL) {
317                 /* Register with management client if we need to. */
318                 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
319                        obddev->obd_name, mgmt_name, server_uuid.uuid);
320
321                 mgmt_obd = class_name2obd(mgmt_name);
322                 if (!mgmt_obd) {
323                         CERROR("can't find mgmtcli %s to register\n",
324                                mgmt_name);
325                         GOTO(err_import, rc = -ENOSYS);
326                 }
327
328                 register_f = inter_module_get("mgmtcli_register_for_events");
329                 if (!register_f) {
330                         CERROR("can't i_m_g mgmtcli_register_for_events\n");
331                         GOTO(err_import, rc = -ENOSYS);
332                 }
333
334                 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
335                 inter_module_put("mgmtcli_register_for_events");
336
337                 if (!rc)
338                         cli->cl_mgmtcli_obd = mgmt_obd;
339         }
340
341         spin_lock_init(&cli->cl_qchk_lock);
342         cli->cl_qchk_stat = CL_NO_QUOTACHECK;
343
344         RETURN(rc);
345
346 err_import:
347         class_destroy_import(imp);
348 err_ldlm:
349         ldlm_put_ref(0);
350 err:
351         RETURN(rc);
352
353 }
354
355 int client_obd_cleanup(struct obd_device *obddev)
356 {
357         struct client_obd *cli = &obddev->u.cli;
358
359         if (!cli->cl_import)
360                 RETURN(-EINVAL);
361         if (cli->cl_mgmtcli_obd) {
362                 mgmtcli_deregister_for_events_t dereg_f;
363
364                 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
365                 dereg_f(cli->cl_mgmtcli_obd, obddev);
366                 inter_module_put("mgmtcli_deregister_for_events");
367         }
368         class_destroy_import(cli->cl_import);
369         cli->cl_import = NULL;
370
371         ldlm_put_ref(obddev->obd_force);
372
373         RETURN(0);
374 }
375
376 int client_connect_import(struct lustre_handle *dlm_handle,
377                           struct obd_device *obd, struct obd_uuid *cluuid,
378                           struct obd_connect_data *data)
379 {
380         struct client_obd *cli = &obd->u.cli;
381         struct obd_import *imp = cli->cl_import;
382         struct obd_export *exp;
383         int rc;
384         ENTRY;
385
386         down(&cli->cl_sem);
387         rc = class_connect(dlm_handle, obd, cluuid);
388         if (rc)
389                 GOTO(out_sem, rc);
390
391         cli->cl_conn_count++;
392         if (cli->cl_conn_count > 1)
393                 GOTO(out_sem, rc);
394         exp = class_conn2export(dlm_handle);
395
396         if (obd->obd_namespace != NULL)
397                 CERROR("already have namespace!\n");
398         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
399                                                 LDLM_NAMESPACE_CLIENT);
400         if (obd->obd_namespace == NULL)
401                 GOTO(out_disco, rc = -ENOMEM);
402
403         imp->imp_dlm_handle = *dlm_handle;
404         rc = ptlrpc_init_import(imp);
405         if (rc != 0) 
406                 GOTO(out_ldlm, rc);
407
408         if (data)
409                 memcpy(&imp->imp_connect_data, data, sizeof(*data));
410         rc = ptlrpc_connect_import(imp, NULL);
411         if (rc != 0) {
412                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
413                 GOTO(out_ldlm, rc);
414         }
415         LASSERT(exp->exp_connection);
416
417         ptlrpc_pinger_add_import(imp);
418         EXIT;
419
420         if (rc) {
421 out_ldlm:
422                 ldlm_namespace_free(obd->obd_namespace, 0);
423                 obd->obd_namespace = NULL;
424 out_disco:
425                 cli->cl_conn_count--;
426                 class_disconnect(exp);
427         } else {
428                 class_export_put(exp);
429         }
430 out_sem:
431         up(&cli->cl_sem);
432         return rc;
433 }
434
435 int client_disconnect_export(struct obd_export *exp)
436 {
437         struct obd_device *obd = class_exp2obd(exp);
438         struct client_obd *cli = &obd->u.cli;
439         struct obd_import *imp = cli->cl_import;
440         int rc = 0, err;
441         ENTRY;
442
443         if (!obd) {
444                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
445                        exp, exp ? exp->exp_handle.h_cookie : -1);
446                 RETURN(-EINVAL);
447         }
448
449         down(&cli->cl_sem);
450         if (!cli->cl_conn_count) {
451                 CERROR("disconnecting disconnected device (%s)\n",
452                        obd->obd_name);
453                 GOTO(out_sem, rc = -EINVAL);
454         }
455
456         cli->cl_conn_count--;
457         if (cli->cl_conn_count)
458                 GOTO(out_no_disconnect, rc = 0);
459
460         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
461          * delete it regardless.  (It's safe to delete an import that was
462          * never added.) */
463         (void)ptlrpc_pinger_del_import(imp);
464
465         if (obd->obd_namespace != NULL) {
466                 /* obd_no_recov == local only */
467                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
468                                        obd->obd_no_recov, NULL);
469                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
470                 obd->obd_namespace = NULL;
471         }
472
473         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
474         if (obd->obd_no_recov)
475                 ptlrpc_invalidate_import(imp);
476         else
477                 rc = ptlrpc_disconnect_import(imp);
478
479         EXIT;
480  out_no_disconnect:
481         err = class_disconnect(exp);
482         if (!rc && err)
483                 rc = err;
484  out_sem:
485         up(&cli->cl_sem);
486         RETURN(rc);
487 }
488
489 /* --------------------------------------------------------------------------
490  * from old lib/target.c
491  * -------------------------------------------------------------------------- */
492
493 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
494                             struct obd_uuid *cluuid)
495 {
496         if (exp->exp_connection) {
497                 struct lustre_handle *hdl;
498                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
499                 /* Might be a re-connect after a partition. */
500                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
501                         CERROR("%s reconnecting\n", cluuid->uuid);
502                         conn->cookie = exp->exp_handle.h_cookie;
503                         /* target_handle_connect() treats EALREADY and
504                          * -EALREADY differently */
505                         RETURN(EALREADY);
506                 } else {
507                         CERROR("%s reconnecting from %s, "
508                                "handle mismatch (ours "LPX64", theirs "
509                                LPX64")\n", cluuid->uuid,
510                                exp->exp_connection->c_remote_uuid.uuid,
511                                hdl->cookie, conn->cookie);
512                         memset(conn, 0, sizeof *conn);
513                         /* target_handle_connect() treats EALREADY and
514                          * -EALREADY differently */
515                         RETURN(-EALREADY);
516                 }
517         }
518
519         conn->cookie = exp->exp_handle.h_cookie;
520         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
521                cluuid->uuid, exp);
522         CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
523         RETURN(0);
524 }
525
526 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
527 {
528         struct obd_device *target;
529         struct obd_export *export = NULL;
530         struct obd_import *revimp;
531         struct lustre_handle conn;
532         struct obd_uuid tgtuuid;
533         struct obd_uuid cluuid;
534         struct obd_uuid remote_uuid;
535         struct list_head *p;
536         char *str, *tmp;
537         int rc = 0, abort_recovery;
538         unsigned long flags;
539         struct obd_connect_data *data;
540         int size = sizeof(*data);
541         ENTRY;
542
543         OBD_RACE(OBD_FAIL_TGT_CONN_RACE); 
544
545         LASSERT_REQSWAB (req, 0);
546         str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
547         if (str == NULL) {
548                 CERROR("bad target UUID for connect\n");
549                 GOTO(out, rc = -EINVAL);
550         }
551
552         obd_str2uuid (&tgtuuid, str);
553         target = class_uuid2obd(&tgtuuid);
554         if (!target) {
555                 target = class_name2obd(str);
556         }
557
558         if (!target || target->obd_stopping || !target->obd_set_up) {
559                 CERROR("UUID '%s' is not available for connect\n", str);
560                 GOTO(out, rc = -ENODEV);
561         }
562
563         LASSERT_REQSWAB (req, 1);
564         str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
565         if (str == NULL) {
566                 CERROR("bad client UUID for connect\n");
567                 GOTO(out, rc = -EINVAL);
568         }
569
570         obd_str2uuid (&cluuid, str);
571
572         /* XXX extract a nettype and format accordingly */
573         switch (sizeof(ptl_nid_t)) {
574                 /* NB the casts only avoid compiler warnings */
575         case 8:
576                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
577                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
578                 break;
579         case 4:
580                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
581                          "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
582                 break;
583         default:
584                 LBUG();
585         }
586
587         spin_lock_bh(&target->obd_processing_task_lock);
588         abort_recovery = target->obd_abort_recovery;
589         spin_unlock_bh(&target->obd_processing_task_lock);
590         if (abort_recovery)
591                 target_abort_recovery(target);
592
593         tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
594         if (tmp == NULL)
595                 GOTO(out, rc = -EPROTO);
596
597         memcpy(&conn, tmp, sizeof conn);
598
599         data = lustre_swab_reqbuf(req, 3, sizeof(*data), lustre_swab_connect);
600         if (data == NULL)
601                 GOTO(out, rc = -EPROTO);
602
603         rc = lustre_pack_reply(req, 1, &size, NULL);
604         if (rc)
605                 GOTO(out, rc);
606
607         /* lctl gets a backstage, all-access pass. */
608         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
609                 goto dont_check_exports;
610
611         spin_lock(&target->obd_dev_lock);
612         list_for_each(p, &target->obd_exports) {
613                 export = list_entry(p, struct obd_export, exp_obd_chain);
614                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
615                         spin_unlock(&target->obd_dev_lock);
616                         LASSERT(export->exp_obd == target);
617
618                         rc = target_handle_reconnect(&conn, export, &cluuid);
619                         break;
620                 }
621                 export = NULL;
622         }
623         /* If we found an export, we already unlocked. */
624         if (!export) {
625                 spin_unlock(&target->obd_dev_lock);
626         } else if (req->rq_reqmsg->conn_cnt == 1) {
627                 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
628                        cluuid.uuid);
629                 GOTO(out, rc = -EALREADY);
630         }
631
632         /* Tell the client if we're in recovery. */
633         /* If this is the first client, start the recovery timer */
634         if (target->obd_recovering) {
635                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
636                 target_start_recovery_timer(target, handler);
637         }
638
639         /* Tell the client if we support replayable requests */
640         if (target->obd_replayable)
641                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
642
643         if (export == NULL) {
644                 if (target->obd_recovering) {
645                         CERROR("denying connection for new client %s: "
646                                "%d clients in recovery for %lds\n", cluuid.uuid,
647                                target->obd_recoverable_clients,
648                                (target->obd_recovery_timer.expires-jiffies)/HZ);
649                         rc = -EBUSY;
650                 } else {
651  dont_check_exports:
652                         rc = obd_connect(&conn, target, &cluuid, data);
653                 }
654         }
655
656         /* Return only the parts of obd_connect_data that we understand, so the
657          * client knows that we don't understand the rest. */
658         data->ocd_connect_flags &= OBD_CONNECT_SUPPORTED;
659         memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data)), data,
660                sizeof(*data));
661
662         /* If all else goes well, this is our RPC return code. */
663         req->rq_status = 0;
664
665         /* we want to handle EALREADY but *not* -EALREADY from
666          * target_handle_reconnect() */
667         if (rc && rc != EALREADY)
668                 GOTO(out, rc);
669
670         req->rq_repmsg->handle = conn;
671
672         /* If the client and the server are the same node, we will already
673          * have an export that really points to the client's DLM export,
674          * because we have a shared handles table.
675          *
676          * XXX this will go away when shaver stops sending the "connect" handle
677          * in the real "remote handle" field of the request --phik 24 Apr 2003
678          */
679         if (req->rq_export != NULL)
680                 class_export_put(req->rq_export);
681
682         /* ownership of this export ref transfers to the request */
683         export = req->rq_export = class_conn2export(&conn);
684         LASSERT(export != NULL);
685
686         spin_lock_irqsave(&export->exp_lock, flags);
687         if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
688                 CERROR("%s: already connected at a higher conn_cnt: %d > %d\n",
689                        cluuid.uuid, export->exp_conn_cnt, 
690                        req->rq_reqmsg->conn_cnt);
691                 spin_unlock_irqrestore(&export->exp_lock, flags);
692                 GOTO(out, rc = -EALREADY);
693         }
694         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
695         spin_unlock_irqrestore(&export->exp_lock, flags);
696
697         /* request from liblustre? */
698         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
699                 export->exp_libclient = 1;
700
701         if (export->exp_connection != NULL)
702                 ptlrpc_put_connection(export->exp_connection);
703         export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
704                                                        &remote_uuid);
705
706         if (rc == EALREADY) {
707                 /* We indicate the reconnection in a flag, not an error code. */
708                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
709                 GOTO(out, rc = 0);
710         }
711
712         if (target->obd_recovering)
713                 target->obd_connected_clients++;
714
715         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
716                sizeof conn);
717
718         if (export->exp_imp_reverse != NULL)
719                 class_destroy_import(export->exp_imp_reverse);
720         revimp = export->exp_imp_reverse = class_new_import();
721         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
722         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
723         revimp->imp_remote_handle = conn;
724         revimp->imp_obd = target;
725         revimp->imp_dlm_fake = 1;
726         revimp->imp_state = LUSTRE_IMP_FULL;
727         class_import_put(revimp);
728 out:
729         if (rc)
730                 req->rq_status = rc;
731         RETURN(rc);
732 }
733
734 int target_handle_disconnect(struct ptlrpc_request *req)
735 {
736         struct obd_export *exp;
737         int rc;
738         ENTRY;
739
740         rc = lustre_pack_reply(req, 0, NULL, NULL);
741         if (rc)
742                 RETURN(rc);
743
744         /* keep the rq_export around so we can send the reply */
745         exp = class_export_get(req->rq_export);
746         req->rq_status = obd_disconnect(exp);
747         RETURN(0);
748 }
749
750 void target_destroy_export(struct obd_export *exp)
751 {
752         /* exports created from last_rcvd data, and "fake"
753            exports created by lctl don't have an import */
754         if (exp->exp_imp_reverse != NULL)
755                 class_destroy_import(exp->exp_imp_reverse);
756
757         /* We cancel locks at disconnect time, but this will catch any locks
758          * granted in a race with recovery-induced disconnect. */
759         if (exp->exp_obd->obd_namespace != NULL)
760                 ldlm_cancel_locks_for_export(exp);
761 }
762
763 /*
764  * Recovery functions
765  */
766
767
768 static void target_release_saved_req(struct ptlrpc_request *req)
769 {
770         if (req->rq_reply_state != NULL) {
771                 ptlrpc_rs_decref(req->rq_reply_state);
772                 /* req->rq_reply_state = NULL; */
773         }
774
775         class_export_put(req->rq_export);
776         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
777         OBD_FREE(req, sizeof *req);
778 }
779
780 static void target_finish_recovery(struct obd_device *obd)
781 {
782         struct list_head *tmp, *n;
783         int rc;
784
785         CWARN("%s: sending delayed replies to recovered clients\n",
786               obd->obd_name);
787
788         ldlm_reprocess_all_ns(obd->obd_namespace);
789
790         /* when recovery finished, cleanup orphans on mds and ost */
791         if (OBT(obd) && OBP(obd, postrecov)) {
792                 rc = OBP(obd, postrecov)(obd);
793                 if (rc >= 0)
794                         CWARN("%s: all clients recovered, %d MDS "
795                               "orphans deleted\n", obd->obd_name, rc);
796                 else
797                         CERROR("postrecov failed %d\n", rc);
798         }
799
800         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
801                 struct ptlrpc_request *req;
802                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
803                 list_del(&req->rq_list);
804                 DEBUG_REQ(D_WARNING, req, "delayed:");
805                 ptlrpc_reply(req);
806                 target_release_saved_req(req);
807         }
808         obd->obd_recovery_end = CURRENT_SECONDS;
809         return;
810 }
811
812 static void abort_recovery_queue(struct obd_device *obd)
813 {
814         struct ptlrpc_request *req;
815         struct list_head *tmp, *n;
816         int rc;
817
818         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
819                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
820                 list_del(&req->rq_list);
821                 DEBUG_REQ(D_ERROR, req, "aborted:");
822                 req->rq_status = -ENOTCONN;
823                 req->rq_type = PTL_RPC_MSG_ERR;
824                 rc = lustre_pack_reply(req, 0, NULL, NULL);
825                 if (rc == 0) {
826                         ptlrpc_reply(req);
827                 } else {
828                         DEBUG_REQ(D_ERROR, req,
829                                   "packing failed for abort-reply; skipping");
830                 }
831                 target_release_saved_req(req);
832         }
833 }
834
835 /* Called from a cleanup function if the device is being cleaned up 
836    forcefully.  The exports should all have been disconnected already, 
837    the only thing left to do is 
838      - clear the recovery flags
839      - cancel the timer
840      - free queued requests and replies, but don't send replies
841    Because the obd_stopping flag is set, no new requests should be received.
842      
843 */
844 void target_cleanup_recovery(struct obd_device *obd)
845 {
846         struct list_head *tmp, *n;
847         struct ptlrpc_request *req;
848
849         LASSERT(obd->obd_stopping);
850
851         spin_lock_bh(&obd->obd_processing_task_lock);
852         if (!obd->obd_recovering) {
853                 spin_unlock_bh(&obd->obd_processing_task_lock);
854                 EXIT;
855                 return;
856         }
857         obd->obd_recovering = obd->obd_abort_recovery = 0;
858         target_cancel_recovery_timer(obd);
859         spin_unlock_bh(&obd->obd_processing_task_lock);
860
861         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
862                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
863                 list_del(&req->rq_list);
864                 target_release_saved_req(req);
865         }
866
867         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
868                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
869                 list_del(&req->rq_list);
870                 target_release_saved_req(req);
871         }
872 }
873
874 void target_abort_recovery(void *data)
875 {
876         struct obd_device *obd = data;
877
878         spin_lock_bh(&obd->obd_processing_task_lock);
879         if (!obd->obd_recovering) {
880                 spin_unlock_bh(&obd->obd_processing_task_lock);
881                 EXIT;
882                 return;
883         }
884         obd->obd_recovering = obd->obd_abort_recovery = 0;
885         target_cancel_recovery_timer(obd);
886         spin_unlock_bh(&obd->obd_processing_task_lock);
887
888         CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
889                obd->obd_name);
890         class_disconnect_stale_exports(obd);
891         abort_recovery_queue(obd);
892
893         target_finish_recovery(obd);
894
895         ptlrpc_run_recovery_over_upcall(obd);
896 }
897
898 static void target_recovery_expired(unsigned long castmeharder)
899 {
900         struct obd_device *obd = (struct obd_device *)castmeharder;
901         CERROR("recovery timed out, aborting\n");
902         spin_lock_bh(&obd->obd_processing_task_lock);
903         if (obd->obd_recovering)
904                 obd->obd_abort_recovery = 1;
905         wake_up(&obd->obd_next_transno_waitq);
906         spin_unlock_bh(&obd->obd_processing_task_lock);
907 }
908
909
910 /* obd_processing_task_lock should be held */
911 void target_cancel_recovery_timer(struct obd_device *obd)
912 {
913         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
914         del_timer(&obd->obd_recovery_timer);
915 }
916
917 static void reset_recovery_timer(struct obd_device *obd)
918 {
919         spin_lock_bh(&obd->obd_processing_task_lock);
920         if (!obd->obd_recovering) {
921                 spin_unlock_bh(&obd->obd_processing_task_lock);
922                 return;
923         }
924         CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
925                (int)(OBD_RECOVERY_TIMEOUT / HZ));
926         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
927         spin_unlock_bh(&obd->obd_processing_task_lock);
928 }
929
930
931 /* Only start it the first time called */
932 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
933 {
934         spin_lock_bh(&obd->obd_processing_task_lock);
935         if (obd->obd_recovery_handler) {
936                 spin_unlock_bh(&obd->obd_processing_task_lock);
937                 return;
938         }
939         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
940               (int)(OBD_RECOVERY_TIMEOUT / HZ));
941         obd->obd_recovery_handler = handler;
942         obd->obd_recovery_timer.function = target_recovery_expired;
943         obd->obd_recovery_timer.data = (unsigned long)obd;
944         spin_unlock_bh(&obd->obd_processing_task_lock);
945
946         reset_recovery_timer(obd);
947 }
948
949 static int check_for_next_transno(struct obd_device *obd)
950 {
951         struct ptlrpc_request *req;
952         int wake_up = 0, connected, completed, queue_len, max;
953         __u64 next_transno, req_transno;
954
955         spin_lock_bh(&obd->obd_processing_task_lock);
956         req = list_entry(obd->obd_recovery_queue.next,
957                          struct ptlrpc_request, rq_list);
958         max = obd->obd_max_recoverable_clients;
959         req_transno = req->rq_reqmsg->transno;
960         connected = obd->obd_connected_clients;
961         completed = max - obd->obd_recoverable_clients;
962         queue_len = obd->obd_requests_queued_for_recovery;
963         next_transno = obd->obd_next_recovery_transno;
964
965         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
966                "req_transno: "LPU64", next_transno: "LPU64"\n",
967                max, connected, completed, queue_len, req_transno, next_transno);
968         if (obd->obd_abort_recovery) {
969                 CDEBUG(D_HA, "waking for aborted recovery\n");
970                 wake_up = 1;
971         } else if (!obd->obd_recovering) {
972                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
973                 wake_up = 1;
974         } else if (req_transno == next_transno) {
975                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
976                 wake_up = 1;
977         } else if (queue_len + completed == max) {
978                 CDEBUG(D_ERROR,
979                        "waking for skipped transno (skip: "LPD64
980                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
981                        next_transno, queue_len, completed, max, req_transno);
982                 obd->obd_next_recovery_transno = req_transno;
983                 wake_up = 1;
984         }
985         spin_unlock_bh(&obd->obd_processing_task_lock);
986         LASSERT(req->rq_reqmsg->transno >= next_transno);
987         return wake_up;
988 }
989
990 static void process_recovery_queue(struct obd_device *obd)
991 {
992         struct ptlrpc_request *req;
993         int abort_recovery = 0;
994         struct l_wait_info lwi = { 0 };
995         ENTRY;
996
997         for (;;) {
998                 spin_lock_bh(&obd->obd_processing_task_lock);
999                 LASSERT(obd->obd_processing_task == current->pid);
1000                 req = list_entry(obd->obd_recovery_queue.next,
1001                                  struct ptlrpc_request, rq_list);
1002
1003                 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
1004                         spin_unlock_bh(&obd->obd_processing_task_lock);
1005                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1006                                LPD64")\n",
1007                                obd->obd_next_recovery_transno,
1008                                req->rq_reqmsg->transno);
1009                         l_wait_event(obd->obd_next_transno_waitq,
1010                                      check_for_next_transno(obd), &lwi);
1011                         spin_lock_bh(&obd->obd_processing_task_lock);
1012                         abort_recovery = obd->obd_abort_recovery;
1013                         spin_unlock_bh(&obd->obd_processing_task_lock);
1014                         if (abort_recovery) {
1015                                 target_abort_recovery(obd);
1016                                 return;
1017                         }
1018                         continue;
1019                 }
1020                 list_del_init(&req->rq_list);
1021                 obd->obd_requests_queued_for_recovery--;
1022                 spin_unlock_bh(&obd->obd_processing_task_lock);
1023
1024                 DEBUG_REQ(D_HA, req, "processing: ");
1025                 (void)obd->obd_recovery_handler(req);
1026                 obd->obd_replayed_requests++;
1027                 reset_recovery_timer(obd);
1028                 /* bug 1580: decide how to properly sync() in recovery */
1029                 //mds_fsync_super(mds->mds_sb);
1030                 class_export_put(req->rq_export);
1031                 if (req->rq_reply_state != NULL) {
1032                         ptlrpc_rs_decref(req->rq_reply_state);
1033                         /* req->rq_reply_state = NULL; */
1034                 }
1035                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1036                 OBD_FREE(req, sizeof *req);
1037                 spin_lock_bh(&obd->obd_processing_task_lock);
1038                 obd->obd_next_recovery_transno++;
1039                 if (list_empty(&obd->obd_recovery_queue)) {
1040                         obd->obd_processing_task = 0;
1041                         spin_unlock_bh(&obd->obd_processing_task_lock);
1042                         break;
1043                 }
1044                 spin_unlock_bh(&obd->obd_processing_task_lock);
1045         }
1046         EXIT;
1047 }
1048
1049 int target_queue_recovery_request(struct ptlrpc_request *req,
1050                                   struct obd_device *obd)
1051 {
1052         struct list_head *tmp;
1053         int inserted = 0;
1054         __u64 transno = req->rq_reqmsg->transno;
1055         struct ptlrpc_request *saved_req;
1056         struct lustre_msg *reqmsg;
1057
1058         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1059          * (i.e. buflens etc are in my own byte order), but type-dependent
1060          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1061
1062         if (!transno) {
1063                 INIT_LIST_HEAD(&req->rq_list);
1064                 DEBUG_REQ(D_HA, req, "not queueing");
1065                 return 1;
1066         }
1067
1068         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1069         /* XXX just like the request-dup code in queue_final_reply */
1070         OBD_ALLOC(saved_req, sizeof *saved_req);
1071         if (!saved_req)
1072                 LBUG();
1073         OBD_ALLOC(reqmsg, req->rq_reqlen);
1074         if (!reqmsg)
1075                 LBUG();
1076
1077         spin_lock_bh(&obd->obd_processing_task_lock);
1078
1079         /* If we're processing the queue, we want don't want to queue this
1080          * message.
1081          *
1082          * Also, if this request has a transno less than the one we're waiting
1083          * for, we should process it now.  It could (and currently always will)
1084          * be an open request for a descriptor that was opened some time ago.
1085          *
1086          * Also, a resent, replayed request that has already been
1087          * handled will pass through here and be processed immediately.
1088          */
1089         if (obd->obd_processing_task == current->pid ||
1090             transno < obd->obd_next_recovery_transno) {
1091                 /* Processing the queue right now, don't re-add. */
1092                 LASSERT(list_empty(&req->rq_list));
1093                 spin_unlock_bh(&obd->obd_processing_task_lock);
1094                 OBD_FREE(reqmsg, req->rq_reqlen);
1095                 OBD_FREE(saved_req, sizeof *saved_req);
1096                 return 1;
1097         }
1098
1099         /* A resent, replayed request that is still on the queue; just drop it.
1100            The queued request will handle this. */
1101         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1102             (MSG_RESENT | MSG_REPLAY)) {
1103                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1104                 spin_unlock_bh(&obd->obd_processing_task_lock);
1105                 OBD_FREE(reqmsg, req->rq_reqlen);
1106                 OBD_FREE(saved_req, sizeof *saved_req);
1107                 return 0;
1108         }
1109
1110         memcpy(saved_req, req, sizeof *req);
1111         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1112         req = saved_req;
1113         req->rq_reqmsg = reqmsg;
1114         class_export_get(req->rq_export);
1115         INIT_LIST_HEAD(&req->rq_list);
1116
1117         /* XXX O(n^2) */
1118         list_for_each(tmp, &obd->obd_recovery_queue) {
1119                 struct ptlrpc_request *reqiter =
1120                         list_entry(tmp, struct ptlrpc_request, rq_list);
1121
1122                 if (reqiter->rq_reqmsg->transno > transno) {
1123                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1124                         inserted = 1;
1125                         break;
1126                 }
1127         }
1128
1129         if (!inserted) {
1130                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1131         }
1132
1133         obd->obd_requests_queued_for_recovery++;
1134
1135         if (obd->obd_processing_task != 0) {
1136                 /* Someone else is processing this queue, we'll leave it to
1137                  * them.
1138                  */
1139                 wake_up(&obd->obd_next_transno_waitq);
1140                 spin_unlock_bh(&obd->obd_processing_task_lock);
1141                 return 0;
1142         }
1143
1144         /* Nobody is processing, and we know there's (at least) one to process
1145          * now, so we'll do the honours.
1146          */
1147         obd->obd_processing_task = current->pid;
1148         spin_unlock_bh(&obd->obd_processing_task_lock);
1149
1150         process_recovery_queue(obd);
1151         return 0;
1152 }
1153
1154 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1155 {
1156         return req->rq_export->exp_obd;
1157 }
1158
1159 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1160 {
1161         struct obd_device *obd = target_req2obd(req);
1162         struct ptlrpc_request *saved_req;
1163         struct lustre_msg *reqmsg;
1164         int recovery_done = 0;
1165
1166         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1167
1168         if (rc) {
1169                 /* Just like ptlrpc_error, but without the sending. */
1170                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1171                 LASSERT(rc == 0); /* XXX handle this */
1172                 req->rq_type = PTL_RPC_MSG_ERR;
1173         }
1174
1175         LASSERT (!req->rq_reply_state->rs_difficult);
1176         LASSERT(list_empty(&req->rq_list));
1177         /* XXX a bit like the request-dup code in queue_recovery_request */
1178         OBD_ALLOC(saved_req, sizeof *saved_req);
1179         if (!saved_req)
1180                 LBUG();
1181         OBD_ALLOC(reqmsg, req->rq_reqlen);
1182         if (!reqmsg)
1183                 LBUG();
1184         memcpy(saved_req, req, sizeof *saved_req);
1185         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1186         
1187         /* Don't race cleanup */
1188         spin_lock_bh(&obd->obd_processing_task_lock);
1189         if (obd->obd_stopping) {
1190                 spin_unlock_bh(&obd->obd_processing_task_lock);
1191                 OBD_FREE(reqmsg, req->rq_reqlen);
1192                 OBD_FREE(saved_req, sizeof *req);
1193                 req->rq_status = -ENOTCONN;
1194                 /* rv is ignored anyhow */
1195                 return -ENOTCONN;
1196         }
1197         ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1198         req = saved_req;
1199         req->rq_reqmsg = reqmsg;
1200         class_export_get(req->rq_export);
1201         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1202
1203         /* only count the first "replay over" request from each
1204            export */
1205         if (req->rq_export->exp_replay_needed) {
1206                 --obd->obd_recoverable_clients;
1207                 req->rq_export->exp_replay_needed = 0;
1208         }
1209         recovery_done = (obd->obd_recoverable_clients == 0);
1210         spin_unlock_bh(&obd->obd_processing_task_lock);
1211
1212         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1213         if (recovery_done) {
1214                 spin_lock_bh(&obd->obd_processing_task_lock);
1215                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1216                 target_cancel_recovery_timer(obd);
1217                 spin_unlock_bh(&obd->obd_processing_task_lock);
1218
1219                 target_finish_recovery(obd);
1220                 ptlrpc_run_recovery_over_upcall(obd);
1221         } else {
1222                 CWARN("%s: %d recoverable clients remain\n",
1223                        obd->obd_name, obd->obd_recoverable_clients);
1224                 wake_up(&obd->obd_next_transno_waitq);
1225         }
1226
1227         return 1;
1228 }
1229
1230 int
1231 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1232 {
1233         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1234                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1235                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1236                 return (-ECOMM);
1237         }
1238
1239         if (rc) {
1240                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1241                 req->rq_status = rc;
1242                 return (ptlrpc_error(req));
1243         } else {
1244                 DEBUG_REQ(D_NET, req, "sending reply");
1245         }
1246         
1247         return (ptlrpc_send_reply(req, 1));
1248 }
1249
1250 void 
1251 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1252 {
1253         int                        netrc;
1254         unsigned long              flags;
1255         struct ptlrpc_reply_state *rs;
1256         struct obd_device         *obd;
1257         struct obd_export         *exp;
1258         struct ptlrpc_srv_ni      *sni;
1259         struct ptlrpc_service     *svc;
1260
1261         sni = req->rq_rqbd->rqbd_srv_ni;
1262         svc = sni->sni_service;
1263         
1264         rs = req->rq_reply_state;
1265         if (rs == NULL || !rs->rs_difficult) {
1266                 /* no notifiers */
1267                 target_send_reply_msg (req, rc, fail_id);
1268                 return;
1269         }
1270
1271         /* must be an export if locks saved */
1272         LASSERT (req->rq_export != NULL);
1273         /* req/reply consistent */
1274         LASSERT (rs->rs_srv_ni == sni);
1275
1276         /* "fresh" reply */
1277         LASSERT (!rs->rs_scheduled);
1278         LASSERT (!rs->rs_scheduled_ever);
1279         LASSERT (!rs->rs_handled);
1280         LASSERT (!rs->rs_on_net);
1281         LASSERT (rs->rs_export == NULL);
1282         LASSERT (list_empty(&rs->rs_obd_list));
1283         LASSERT (list_empty(&rs->rs_exp_list));
1284
1285         exp = class_export_get (req->rq_export);
1286         obd = exp->exp_obd;
1287
1288         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1289         rs->rs_scheduled = 1;
1290         rs->rs_on_net    = 1;
1291         rs->rs_xid       = req->rq_xid;
1292         rs->rs_transno   = req->rq_transno;
1293         rs->rs_export    = exp;
1294         
1295         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1296
1297         if (rs->rs_transno > obd->obd_last_committed) {
1298                 /* not committed already */ 
1299                 list_add_tail (&rs->rs_obd_list, 
1300                                &obd->obd_uncommitted_replies);
1301         }
1302
1303         spin_unlock (&obd->obd_uncommitted_replies_lock);
1304         spin_lock (&exp->exp_lock);
1305
1306         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1307
1308         spin_unlock_irqrestore (&exp->exp_lock, flags);
1309
1310         netrc = target_send_reply_msg (req, rc, fail_id);
1311
1312         spin_lock_irqsave (&svc->srv_lock, flags);
1313
1314         svc->srv_n_difficult_replies++;
1315
1316         if (netrc != 0) {
1317                 /* error sending: reply is off the net.  Also we need +1
1318                  * reply ref until ptlrpc_server_handle_reply() is done
1319                  * with the reply state (if the send was successful, there
1320                  * would have been +1 ref for the net, which
1321                  * reply_out_callback leaves alone) */
1322                 rs->rs_on_net = 0;
1323                 ptlrpc_rs_addref(rs);
1324                 atomic_inc (&svc->srv_outstanding_replies);
1325         }
1326
1327         if (!rs->rs_on_net ||                   /* some notifier */
1328             list_empty(&rs->rs_exp_list) ||     /* completed already */
1329             list_empty(&rs->rs_obd_list)) {
1330                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1331                 wake_up (&svc->srv_waitq);
1332         } else {
1333                 list_add (&rs->rs_list, &sni->sni_active_replies);
1334                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1335         }
1336
1337         spin_unlock_irqrestore (&svc->srv_lock, flags);
1338 }
1339
1340 int target_handle_ping(struct ptlrpc_request *req)
1341 {
1342         return lustre_pack_reply(req, 0, NULL, NULL);
1343 }
1344
1345 void target_committed_to_req(struct ptlrpc_request *req)
1346 {
1347         struct obd_device *obd = req->rq_export->exp_obd;
1348
1349         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1350                 req->rq_repmsg->last_committed = obd->obd_last_committed;
1351         else
1352                 DEBUG_REQ(D_IOCTL, req,
1353                           "not sending last_committed update");
1354
1355         CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1356                obd->obd_last_committed, req->rq_xid);
1357 }
1358
1359 int target_handle_qc_callback(struct ptlrpc_request *req)
1360 {
1361         struct obd_quotactl *oqctl;
1362         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1363
1364         oqctl = lustre_swab_reqbuf(req, 0, sizeof(*oqctl),
1365                                    lustre_swab_obd_quotactl);
1366
1367         spin_lock(&cli->cl_qchk_lock);
1368         cli->cl_qchk_stat = oqctl->qc_stat;
1369         spin_unlock(&cli->cl_qchk_lock);
1370
1371         return 0;
1372 }
1373
1374 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1375 {
1376         struct obd_device *obd = req->rq_export->exp_obd;
1377         struct obd_device *master_obd;
1378         struct lustre_quota_ctxt *qctxt;
1379         struct qunit_data *qdata, *rep;
1380         int rc = 0, repsize = sizeof(struct qunit_data);
1381         ENTRY;
1382         
1383         rc = lustre_pack_reply(req, 1, &repsize, NULL);
1384         if (rc) {
1385                 CERROR("packing reply failed!: rc = %d\n", rc);
1386                 RETURN(rc);
1387         }
1388         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
1389         LASSERT(rep);
1390         
1391         qdata = lustre_swab_reqbuf(req, 0, sizeof(*qdata), lustre_swab_qdata);
1392         if (qdata == NULL) {
1393                 CERROR("unpacking request buffer failed!");
1394                 RETURN(-EPROTO);
1395         }
1396
1397         /* we use the observer */
1398         LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1399         master_obd = obd->obd_observer->obd_observer;
1400         qctxt = &master_obd->u.mds.mds_quota_ctxt;
1401         
1402         LASSERT(qctxt->lqc_handler);
1403         rc = qctxt->lqc_handler(master_obd, qdata, req->rq_reqmsg->opc);
1404         if (rc && rc != -EDQUOT)
1405                 CERROR("dqacq failed! (rc:%d)\n", rc);
1406         
1407         /* the qd_count might be changed in lqc_handler */
1408         memcpy(rep, qdata, sizeof(*rep));
1409         req->rq_status = rc;
1410         rc = ptlrpc_reply(req);
1411         
1412         RETURN(rc);     
1413 }
1414  
1415 EXPORT_SYMBOL(target_committed_to_req);