Whamcloud - gitweb
land b_eq on HEAD
[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 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
40 {
41         struct ptlrpc_connection *conn;
42         struct lustre_cfg* lcfg = buf;
43         struct client_obd *cli = &obddev->u.cli;
44         struct obd_import *imp;
45         struct obd_uuid server_uuid;
46         int rq_portal, rp_portal, connect_op;
47         char *name = obddev->obd_type->typ_name;
48         char *mgmt_name = NULL;
49         int rc = 0;
50         struct obd_device *mgmt_obd;
51         mgmtcli_register_for_events_t register_f;
52         ENTRY;
53
54         /* In a more perfect world, we would hang a ptlrpc_client off of
55          * obd_type and just use the values from there. */
56         if (!strcmp(name, LUSTRE_OSC_NAME)) {
57                 rq_portal = OST_REQUEST_PORTAL;
58                 rp_portal = OSC_REPLY_PORTAL;
59                 connect_op = OST_CONNECT;
60         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
61                 rq_portal = MDS_REQUEST_PORTAL;
62                 rp_portal = MDC_REPLY_PORTAL;
63                 connect_op = MDS_CONNECT;
64         } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
65                 rq_portal = MGMT_REQUEST_PORTAL;
66                 rp_portal = MGMT_REPLY_PORTAL;
67                 connect_op = MGMT_CONNECT;
68         } else {
69                 CERROR("unknown client OBD type \"%s\", can't setup\n",
70                        name);
71                 RETURN(-EINVAL);
72         }
73
74         if (lcfg->lcfg_inllen1 < 1) {
75                 CERROR("requires a TARGET UUID\n");
76                 RETURN(-EINVAL);
77         }
78
79         if (lcfg->lcfg_inllen1 > 37) {
80                 CERROR("client UUID must be less than 38 characters\n");
81                 RETURN(-EINVAL);
82         }
83
84         if (lcfg->lcfg_inllen2 < 1) {
85                 CERROR("setup requires a SERVER UUID\n");
86                 RETURN(-EINVAL);
87         }
88
89         if (lcfg->lcfg_inllen2 > 37) {
90                 CERROR("target UUID must be less than 38 characters\n");
91                 RETURN(-EINVAL);
92         }
93
94         sema_init(&cli->cl_sem, 1);
95         cli->cl_conn_count = 0;
96         memcpy(server_uuid.uuid, lcfg->lcfg_inlbuf2, min(lcfg->lcfg_inllen2,
97                                                         sizeof(server_uuid)));
98
99         cli->cl_dirty = 0;
100         cli->cl_avail_grant = 0;
101         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
102         INIT_LIST_HEAD(&cli->cl_cache_waiters);
103         INIT_LIST_HEAD(&cli->cl_loi_ready_list);
104         INIT_LIST_HEAD(&cli->cl_loi_write_list);
105         spin_lock_init(&cli->cl_loi_list_lock);
106         cli->cl_brw_in_flight = 0;
107         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
108         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
109         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
110         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
111         cli->cl_max_pages_per_rpc = PTL_MD_MAX_PAGES;
112         cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
113
114         ldlm_get_ref();
115         if (rc) {
116                 CERROR("ldlm_get_ref failed: %d\n", rc);
117                 GOTO(err, rc);
118         }
119
120         conn = ptlrpc_uuid_to_connection(&server_uuid);
121         if (conn == NULL)
122                 GOTO(err_ldlm, rc = -ENOENT);
123
124         ptlrpc_init_client(rq_portal, rp_portal, name,
125                            &obddev->obd_ldlm_client);
126
127         imp = class_new_import();
128         if (imp == NULL) {
129                 ptlrpc_put_connection(conn);
130                 GOTO(err_ldlm, rc = -ENOENT);
131         }
132         imp->imp_connection = conn;
133         imp->imp_client = &obddev->obd_ldlm_client;
134         imp->imp_obd = obddev;
135         imp->imp_connect_op = connect_op;
136         imp->imp_generation = 0;
137         imp->imp_initial_recov = 1;
138         INIT_LIST_HEAD(&imp->imp_pinger_chain);
139         memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
140               lcfg->lcfg_inllen1);
141         class_import_put(imp);
142
143         cli->cl_import = imp;
144         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
145         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
146         cli->cl_sandev = to_kdev_t(0);
147
148         if (lcfg->lcfg_inllen3 != 0) {
149                 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
150                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
151                                name, obddev->obd_name,
152                                imp->imp_target_uuid.uuid);
153                         imp->imp_invalid = 1;
154
155                         if (lcfg->lcfg_inllen4 != 0)
156                                 mgmt_name = lcfg->lcfg_inlbuf4;
157                 } else {
158                         mgmt_name = lcfg->lcfg_inlbuf3;
159                 }
160         }
161
162         if (mgmt_name != NULL) {
163                 /* Register with management client if we need to. */
164                 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
165                        obddev->obd_name, mgmt_name, server_uuid.uuid);
166
167                 mgmt_obd = class_name2obd(mgmt_name);
168                 if (!mgmt_obd) {
169                         CERROR("can't find mgmtcli %s to register\n",
170                                mgmt_name);
171                         GOTO(err_import, rc = -ENOSYS);
172                 }
173
174                 register_f = inter_module_get("mgmtcli_register_for_events");
175                 if (!register_f) {
176                         CERROR("can't i_m_g mgmtcli_register_for_events\n");
177                         GOTO(err_import, rc = -ENOSYS);
178                 }
179
180                 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
181                 inter_module_put("mgmtcli_register_for_events");
182
183                 if (!rc)
184                         cli->cl_mgmtcli_obd = mgmt_obd;
185         }
186
187         RETURN(rc);
188
189 err_import:
190         class_destroy_import(imp);
191 err_ldlm:
192         ldlm_put_ref(0);
193 err:
194         RETURN(rc);
195
196 }
197
198 int client_obd_cleanup(struct obd_device *obddev, int flags)
199 {
200         struct client_obd *cli = &obddev->u.cli;
201
202         if (!cli->cl_import)
203                 RETURN(-EINVAL);
204         if (cli->cl_mgmtcli_obd) {
205                 mgmtcli_deregister_for_events_t dereg_f;
206
207                 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
208                 dereg_f(cli->cl_mgmtcli_obd, obddev);
209                 inter_module_put("mgmtcli_deregister_for_events");
210         }
211         class_destroy_import(cli->cl_import);
212         cli->cl_import = NULL;
213
214         ldlm_put_ref(flags & OBD_OPT_FORCE);
215
216         RETURN(0);
217 }
218
219 int client_connect_import(struct lustre_handle *dlm_handle,
220                           struct obd_device *obd,
221                           struct obd_uuid *cluuid)
222 {
223         struct client_obd *cli = &obd->u.cli;
224         struct obd_import *imp = cli->cl_import;
225         struct obd_export *exp;
226         int rc;
227         ENTRY;
228
229         down(&cli->cl_sem);
230         rc = class_connect(dlm_handle, obd, cluuid);
231         if (rc)
232                 GOTO(out_sem, rc);
233
234         cli->cl_conn_count++;
235         if (cli->cl_conn_count > 1)
236                 GOTO(out_sem, rc);
237         exp = class_conn2export(dlm_handle);
238
239         if (obd->obd_namespace != NULL)
240                 CERROR("already have namespace!\n");
241         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
242                                                 LDLM_NAMESPACE_CLIENT);
243         if (obd->obd_namespace == NULL)
244                 GOTO(out_disco, rc = -ENOMEM);
245
246         imp->imp_dlm_handle = *dlm_handle;
247         rc = ptlrpc_init_import(imp);
248         if (rc != 0) 
249                 GOTO(out_ldlm, rc);
250
251         exp->exp_connection = ptlrpc_connection_addref(imp->imp_connection);
252         rc = ptlrpc_connect_import(imp, NULL);
253         if (rc != 0) {
254                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
255                 GOTO(out_ldlm, rc);
256         }
257
258         EXIT;
259
260         if (rc) {
261 out_ldlm:
262                 ldlm_namespace_free(obd->obd_namespace, 0);
263                 obd->obd_namespace = NULL;
264 out_disco:
265                 cli->cl_conn_count--;
266                 class_disconnect(exp, 0);
267         } else {
268                 class_export_put(exp);
269         }
270 out_sem:
271         up(&cli->cl_sem);
272         return rc;
273 }
274
275 int client_disconnect_export(struct obd_export *exp, int failover)
276 {
277         struct obd_device *obd = class_exp2obd(exp);
278         struct client_obd *cli = &obd->u.cli;
279         struct obd_import *imp = cli->cl_import;
280         int rc = 0, err;
281         ENTRY;
282
283         if (!obd) {
284                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
285                        exp, exp ? exp->exp_handle.h_cookie : -1);
286                 RETURN(-EINVAL);
287         }
288
289         down(&cli->cl_sem);
290         if (!cli->cl_conn_count) {
291                 CERROR("disconnecting disconnected device (%s)\n",
292                        obd->obd_name);
293                 GOTO(out_sem, rc = -EINVAL);
294         }
295
296         cli->cl_conn_count--;
297         if (cli->cl_conn_count)
298                 GOTO(out_no_disconnect, rc = 0);
299
300         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
301          * delete it regardless.  (It's safe to delete an import that was
302          * never added.) */
303         (void)ptlrpc_pinger_del_import(imp);
304
305         if (obd->obd_namespace != NULL) {
306                 /* obd_no_recov == local only */
307                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
308                                        obd->obd_no_recov, NULL);
309                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
310                 obd->obd_namespace = NULL;
311         }
312
313         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
314         if (obd->obd_no_recov)
315                 ptlrpc_set_import_active(imp, 0);
316         else
317                 rc = ptlrpc_disconnect_import(imp);
318
319         EXIT;
320  out_no_disconnect:
321         err = class_disconnect(exp, 0);
322         if (!rc && err)
323                 rc = err;
324  out_sem:
325         up(&cli->cl_sem);
326         RETURN(rc);
327 }
328
329 /* --------------------------------------------------------------------------
330  * from old lib/target.c
331  * -------------------------------------------------------------------------- */
332
333 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
334                             struct obd_uuid *cluuid)
335 {
336         if (exp->exp_connection) {
337                 struct lustre_handle *hdl;
338                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
339                 /* Might be a re-connect after a partition. */
340                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
341                         CERROR("%s reconnecting\n", cluuid->uuid);
342                         conn->cookie = exp->exp_handle.h_cookie;
343                         RETURN(EALREADY);
344                 } else {
345                         CERROR("%s reconnecting from %s, "
346                                "handle mismatch (ours "LPX64", theirs "
347                                LPX64")\n", cluuid->uuid,
348                                exp->exp_connection->c_remote_uuid.uuid,
349                                hdl->cookie, conn->cookie);
350                         memset(conn, 0, sizeof *conn);
351                         RETURN(-EALREADY);
352                 }
353         }
354
355         conn->cookie = exp->exp_handle.h_cookie;
356         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
357                cluuid->uuid, exp);
358         CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
359         RETURN(0);
360 }
361
362 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
363 {
364         struct obd_device *target;
365         struct obd_export *export = NULL;
366         struct obd_import *revimp;
367         struct lustre_handle conn;
368         struct obd_uuid tgtuuid;
369         struct obd_uuid cluuid;
370         struct obd_uuid remote_uuid;
371         struct list_head *p;
372         char *str, *tmp;
373         int rc = 0, abort_recovery;
374         ENTRY;
375
376         LASSERT_REQSWAB (req, 0);
377         str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
378         if (str == NULL) {
379                 CERROR("bad target UUID for connect\n");
380                 GOTO(out, rc = -EINVAL);
381         }
382
383         obd_str2uuid (&tgtuuid, str);
384         target = class_uuid2obd(&tgtuuid);
385         if (!target) {
386                 target = class_name2obd(str);
387         }
388
389         if (!target || target->obd_stopping || !target->obd_set_up) {
390                 CERROR("UUID '%s' is not available for connect\n", str);
391                 GOTO(out, rc = -ENODEV);
392         }
393
394         LASSERT_REQSWAB (req, 1);
395         str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
396         if (str == NULL) {
397                 CERROR("bad client UUID for connect\n");
398                 GOTO(out, rc = -EINVAL);
399         }
400
401         obd_str2uuid (&cluuid, str);
402
403         /* XXX extract a nettype and format accordingly */
404         snprintf(remote_uuid.uuid, sizeof remote_uuid,
405                  "NET_"LPX64"_UUID", req->rq_peer.peer_nid);
406
407         spin_lock_bh(&target->obd_processing_task_lock);
408         abort_recovery = target->obd_abort_recovery;
409         spin_unlock_bh(&target->obd_processing_task_lock);
410         if (abort_recovery)
411                 target_abort_recovery(target);
412
413         tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
414         if (tmp == NULL)
415                 GOTO(out, rc = -EPROTO);
416
417         memcpy(&conn, tmp, sizeof conn);
418
419         rc = lustre_pack_reply(req, 0, NULL, NULL);
420         if (rc)
421                 GOTO(out, rc);
422
423         /* lctl gets a backstage, all-access pass. */
424         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
425                 goto dont_check_exports;
426
427         spin_lock(&target->obd_dev_lock);
428         list_for_each(p, &target->obd_exports) {
429                 export = list_entry(p, struct obd_export, exp_obd_chain);
430                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
431                         spin_unlock(&target->obd_dev_lock);
432                         LASSERT(export->exp_obd == target);
433
434                         rc = target_handle_reconnect(&conn, export, &cluuid);
435                         break;
436                 }
437                 export = NULL;
438         }
439         /* If we found an export, we already unlocked. */
440         if (!export) {
441                 spin_unlock(&target->obd_dev_lock);
442         } else if (req->rq_reqmsg->conn_cnt == 1) {
443                 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
444                        cluuid.uuid);
445                 GOTO(out, rc = -EALREADY);
446         }
447
448         /* Tell the client if we're in recovery. */
449         /* If this is the first client, start the recovery timer */
450         if (target->obd_recovering) {
451                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
452                 target_start_recovery_timer(target, handler);
453         }
454
455         /* Tell the client if we support replayable requests */
456         if (target->obd_replayable)
457                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
458
459         if (export == NULL) {
460                 if (target->obd_recovering) {
461                         CERROR("denying connection for new client %s: "
462                                "%d clients in recovery for %lds\n", cluuid.uuid,
463                                target->obd_recoverable_clients,
464                                (target->obd_recovery_timer.expires-jiffies)/HZ);
465                         rc = -EBUSY;
466                 } else {
467  dont_check_exports:
468                         rc = obd_connect(&conn, target, &cluuid);
469                 }
470         }
471
472
473         /* If all else goes well, this is our RPC return code. */
474         req->rq_status = 0;
475
476         if (rc && rc != EALREADY)
477                 GOTO(out, rc);
478
479         /* XXX track this all the time? */
480         if (target->obd_recovering) {
481                 target->obd_connected_clients++;
482         }
483
484         req->rq_repmsg->handle = conn;
485
486         /* If the client and the server are the same node, we will already
487          * have an export that really points to the client's DLM export,
488          * because we have a shared handles table.
489          *
490          * XXX this will go away when shaver stops sending the "connect" handle
491          * in the real "remote handle" field of the request --phik 24 Apr 2003
492          */
493         if (req->rq_export != NULL)
494                 class_export_put(req->rq_export);
495
496         /* ownership of this export ref transfers to the request */
497         export = req->rq_export = class_conn2export(&conn);
498         LASSERT(export != NULL);
499
500         /* request from liblustre? */
501         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
502                 export->exp_libclient = 1;
503
504         if (export->exp_connection != NULL)
505                 ptlrpc_put_connection(export->exp_connection);
506         export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
507                                                        &remote_uuid);
508
509         LASSERT(export->exp_conn_cnt < req->rq_reqmsg->conn_cnt);
510         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
511
512         if (rc == EALREADY) {
513                 /* We indicate the reconnection in a flag, not an error code. */
514                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
515                 GOTO(out, rc = 0);
516         }
517
518         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
519                sizeof conn);
520
521         if (export->exp_imp_reverse != NULL)
522                 class_destroy_import(export->exp_imp_reverse);
523         revimp = export->exp_imp_reverse = class_new_import();
524         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
525         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
526         revimp->imp_remote_handle = conn;
527         revimp->imp_obd = target;
528         revimp->imp_dlm_fake = 1;
529         revimp->imp_state = LUSTRE_IMP_FULL;
530         class_import_put(revimp);
531 out:
532         if (rc)
533                 req->rq_status = rc;
534         RETURN(rc);
535 }
536
537 int target_handle_disconnect(struct ptlrpc_request *req)
538 {
539         struct obd_export *exp;
540         int rc;
541         ENTRY;
542
543         rc = lustre_pack_reply(req, 0, NULL, NULL);
544         if (rc)
545                 RETURN(rc);
546
547         /* keep the rq_export around so we can send the reply */
548         exp = class_export_get(req->rq_export);
549         req->rq_status = obd_disconnect(exp, 0);
550         RETURN(0);
551 }
552
553 void target_destroy_export(struct obd_export *exp)
554 {
555         /* exports created from last_rcvd data, and "fake"
556            exports created by lctl don't have an import */
557         if (exp->exp_imp_reverse != NULL)
558                 class_destroy_import(exp->exp_imp_reverse);
559
560         /* We cancel locks at disconnect time, but this will catch any locks
561          * granted in a race with recovery-induced disconnect. */
562         if (exp->exp_obd->obd_namespace != NULL)
563                 ldlm_cancel_locks_for_export(exp);
564 }
565
566 /*
567  * Recovery functions
568  */
569
570 static void abort_delayed_replies(struct obd_device *obd)
571 {
572         struct ptlrpc_request *req;
573         struct list_head *tmp, *n;
574         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
575                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
576                 DEBUG_REQ(D_ERROR, req, "aborted:");
577                 req->rq_status = -ENOTCONN;
578                 req->rq_type = PTL_RPC_MSG_ERR;
579                 ptlrpc_reply(req);
580                 class_export_put(req->rq_export);
581                 list_del(&req->rq_list);
582                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
583                 OBD_FREE(req, sizeof *req);
584         }
585 }
586
587 static void abort_recovery_queue(struct obd_device *obd)
588 {
589         struct ptlrpc_request *req;
590         struct list_head *tmp, *n;
591         int rc;
592
593         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
594                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
595                 DEBUG_REQ(D_ERROR, req, "aborted:");
596                 req->rq_status = -ENOTCONN;
597                 req->rq_type = PTL_RPC_MSG_ERR;
598                 rc = lustre_pack_reply(req, 0, NULL, NULL);
599                 if (rc == 0) {
600                         ptlrpc_reply(req);
601                 } else {
602                         DEBUG_REQ(D_ERROR, req,
603                                   "packing failed for abort-reply; skipping");
604                 }
605                 list_del(&req->rq_list);
606                 class_export_put(req->rq_export);
607                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
608                 OBD_FREE(req, sizeof *req);
609         }
610 }
611
612 void target_abort_recovery(void *data)
613 {
614         struct obd_device *obd = data;
615         int rc;
616
617         CERROR("disconnecting clients and aborting recovery\n");
618         spin_lock_bh(&obd->obd_processing_task_lock);
619         if (!obd->obd_recovering) {
620                 spin_unlock_bh(&obd->obd_processing_task_lock);
621                 EXIT;
622                 return;
623         }
624
625         obd->obd_recovering = obd->obd_abort_recovery = 0;
626
627         wake_up(&obd->obd_next_transno_waitq);
628         target_cancel_recovery_timer(obd);
629         spin_unlock_bh(&obd->obd_processing_task_lock);
630
631         class_disconnect_exports(obd, 0);
632
633         /* when recovery was aborted, cleanup orphans on mds and ost */
634         if (OBT(obd) && OBP(obd, postrecov)) {
635                 rc = OBP(obd, postrecov)(obd);
636                 if (rc >= 0)
637                         CWARN("Cleanup %d orphans after recovery was aborted\n", rc);
638                 else
639                         CERROR("postrecov failed %d\n", rc);
640         }
641
642         abort_delayed_replies(obd);
643         abort_recovery_queue(obd);
644         ptlrpc_run_recovery_over_upcall(obd);
645 }
646
647 static void target_recovery_expired(unsigned long castmeharder)
648 {
649         struct obd_device *obd = (struct obd_device *)castmeharder;
650         CERROR("recovery timed out, aborting\n");
651         spin_lock_bh(&obd->obd_processing_task_lock);
652         obd->obd_abort_recovery = 1;
653         wake_up(&obd->obd_next_transno_waitq);
654         spin_unlock_bh(&obd->obd_processing_task_lock);
655 }
656
657
658 /* obd_processing_task_lock should be held */
659 void target_cancel_recovery_timer(struct obd_device *obd)
660 {
661         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
662         del_timer(&obd->obd_recovery_timer);
663 }
664
665 static void reset_recovery_timer(struct obd_device *obd)
666 {
667         spin_lock_bh(&obd->obd_processing_task_lock);
668         if (!obd->obd_recovering) {
669                 spin_unlock_bh(&obd->obd_processing_task_lock);
670                 return;
671         }                
672         CDEBUG(D_HA, "timer will expire in %u seconds\n",
673                OBD_RECOVERY_TIMEOUT / HZ);
674         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
675         spin_unlock_bh(&obd->obd_processing_task_lock);
676 }
677
678
679 /* Only start it the first time called */
680 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
681 {
682         spin_lock_bh(&obd->obd_processing_task_lock);
683         if (obd->obd_recovery_handler) {
684                 spin_unlock_bh(&obd->obd_processing_task_lock);
685                 return;
686         }
687         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
688                OBD_RECOVERY_TIMEOUT / HZ);
689         obd->obd_recovery_handler = handler;
690         obd->obd_recovery_timer.function = target_recovery_expired;
691         obd->obd_recovery_timer.data = (unsigned long)obd;
692         init_timer(&obd->obd_recovery_timer);
693         spin_unlock_bh(&obd->obd_processing_task_lock);
694
695         reset_recovery_timer(obd);
696 }
697
698 static int check_for_next_transno(struct obd_device *obd)
699 {
700         struct ptlrpc_request *req;
701         int wake_up = 0, connected, completed, queue_len, max;
702         __u64 next_transno, req_transno;
703
704         spin_lock_bh(&obd->obd_processing_task_lock);
705         req = list_entry(obd->obd_recovery_queue.next,
706                          struct ptlrpc_request, rq_list);
707         max = obd->obd_max_recoverable_clients;
708         req_transno = req->rq_reqmsg->transno;
709         connected = obd->obd_connected_clients;
710         completed = max - obd->obd_recoverable_clients;
711         queue_len = obd->obd_requests_queued_for_recovery;
712         next_transno = obd->obd_next_recovery_transno;
713
714         if (obd->obd_abort_recovery) {
715                 CDEBUG(D_HA, "waking for aborted recovery\n");
716                 wake_up = 1;
717         } else if (!obd->obd_recovering) {
718                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
719                 wake_up = 1;
720         } else if (req_transno == next_transno) {
721                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
722                 wake_up = 1;
723         } else if (queue_len + completed == max) {
724                 CDEBUG(D_ERROR,
725                        "waking for skipped transno (skip: "LPD64
726                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
727                        next_transno, queue_len, completed, max, req_transno);
728                 obd->obd_next_recovery_transno = req_transno;
729                 wake_up = 1;
730         }
731         spin_unlock_bh(&obd->obd_processing_task_lock);
732         LASSERT(req->rq_reqmsg->transno >= next_transno);
733         return wake_up;
734 }
735
736 static void process_recovery_queue(struct obd_device *obd)
737 {
738         struct ptlrpc_request *req;
739         int abort_recovery = 0;
740         struct l_wait_info lwi = { 0 };
741         ENTRY;
742
743         for (;;) {
744                 spin_lock_bh(&obd->obd_processing_task_lock);
745                 LASSERT(obd->obd_processing_task == current->pid);
746                 req = list_entry(obd->obd_recovery_queue.next,
747                                  struct ptlrpc_request, rq_list);
748
749                 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
750                         spin_unlock_bh(&obd->obd_processing_task_lock);
751                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
752                                LPD64")\n",
753                                obd->obd_next_recovery_transno,
754                                req->rq_reqmsg->transno);
755                         l_wait_event(obd->obd_next_transno_waitq,
756                                      check_for_next_transno(obd), &lwi);
757                         spin_lock_bh(&obd->obd_processing_task_lock);
758                         abort_recovery = obd->obd_abort_recovery;
759                         spin_unlock_bh(&obd->obd_processing_task_lock);
760                         if (abort_recovery) {
761                                 target_abort_recovery(obd);
762                                 return;
763                         }
764                         continue;
765                 }
766                 list_del_init(&req->rq_list);
767                 obd->obd_requests_queued_for_recovery--;
768                 spin_unlock_bh(&obd->obd_processing_task_lock);
769
770                 DEBUG_REQ(D_HA, req, "processing: ");
771                 (void)obd->obd_recovery_handler(req);
772                 obd->obd_replayed_requests++;
773                 reset_recovery_timer(obd);
774                 /* bug 1580: decide how to properly sync() in recovery */
775                 //mds_fsync_super(mds->mds_sb);
776                 class_export_put(req->rq_export);
777                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
778                 OBD_FREE(req, sizeof *req);
779                 spin_lock_bh(&obd->obd_processing_task_lock);
780                 obd->obd_next_recovery_transno++;
781                 if (list_empty(&obd->obd_recovery_queue)) {
782                         obd->obd_processing_task = 0;
783                         spin_unlock_bh(&obd->obd_processing_task_lock);
784                         break;
785                 }
786                 spin_unlock_bh(&obd->obd_processing_task_lock);
787         }
788         EXIT;
789 }
790
791 int target_queue_recovery_request(struct ptlrpc_request *req,
792                                   struct obd_device *obd)
793 {
794         struct list_head *tmp;
795         int inserted = 0;
796         __u64 transno = req->rq_reqmsg->transno;
797         struct ptlrpc_request *saved_req;
798         struct lustre_msg *reqmsg;
799
800         /* CAVEAT EMPTOR: The incoming request message has been swabbed
801          * (i.e. buflens etc are in my own byte order), but type-dependent
802          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
803
804         if (!transno) {
805                 INIT_LIST_HEAD(&req->rq_list);
806                 DEBUG_REQ(D_HA, req, "not queueing");
807                 return 1;
808         }
809
810         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
811         /* XXX just like the request-dup code in queue_final_reply */
812         OBD_ALLOC(saved_req, sizeof *saved_req);
813         if (!saved_req)
814                 LBUG();
815         OBD_ALLOC(reqmsg, req->rq_reqlen);
816         if (!reqmsg)
817                 LBUG();
818
819         spin_lock_bh(&obd->obd_processing_task_lock);
820
821         /* If we're processing the queue, we want don't want to queue this
822          * message.
823          *
824          * Also, if this request has a transno less than the one we're waiting
825          * for, we should process it now.  It could (and currently always will)
826          * be an open request for a descriptor that was opened some time ago.
827          */
828         if (obd->obd_processing_task == current->pid ||
829             transno < obd->obd_next_recovery_transno) {
830                 /* Processing the queue right now, don't re-add. */
831                 LASSERT(list_empty(&req->rq_list));
832                 spin_unlock_bh(&obd->obd_processing_task_lock);
833                 OBD_FREE(reqmsg, req->rq_reqlen);
834                 OBD_FREE(saved_req, sizeof *saved_req);
835                 return 1;
836         }
837
838         memcpy(saved_req, req, sizeof *req);
839         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
840         req = saved_req;
841         req->rq_reqmsg = reqmsg;
842         class_export_get(req->rq_export);
843         INIT_LIST_HEAD(&req->rq_list);
844
845         /* XXX O(n^2) */
846         list_for_each(tmp, &obd->obd_recovery_queue) {
847                 struct ptlrpc_request *reqiter =
848                         list_entry(tmp, struct ptlrpc_request, rq_list);
849
850                 if (reqiter->rq_reqmsg->transno > transno) {
851                         list_add_tail(&req->rq_list, &reqiter->rq_list);
852                         inserted = 1;
853                         break;
854                 }
855         }
856
857         if (!inserted) {
858                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
859         }
860
861         obd->obd_requests_queued_for_recovery++;
862
863         if (obd->obd_processing_task != 0) {
864                 /* Someone else is processing this queue, we'll leave it to
865                  * them.
866                  */
867                 wake_up(&obd->obd_next_transno_waitq);
868                 spin_unlock_bh(&obd->obd_processing_task_lock);
869                 return 0;
870         }
871
872         /* Nobody is processing, and we know there's (at least) one to process
873          * now, so we'll do the honours.
874          */
875         obd->obd_processing_task = current->pid;
876         spin_unlock_bh(&obd->obd_processing_task_lock);
877
878         process_recovery_queue(obd);
879         return 0;
880 }
881
882 struct obd_device * target_req2obd(struct ptlrpc_request *req)
883 {
884         return req->rq_export->exp_obd;
885 }
886
887 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
888 {
889         struct obd_device *obd = target_req2obd(req);
890         struct ptlrpc_request *saved_req;
891         struct lustre_msg *reqmsg;
892         int recovery_done = 0;
893         int rc2;
894
895         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
896
897         if (rc) {
898                 /* Just like ptlrpc_error, but without the sending. */
899                 rc = lustre_pack_reply(req, 0, NULL, NULL);
900                 LASSERT(rc == 0); /* XXX handle this */
901                 req->rq_type = PTL_RPC_MSG_ERR;
902         }
903
904         LASSERT (!req->rq_reply_state->rs_difficult);
905         LASSERT(list_empty(&req->rq_list));
906         /* XXX a bit like the request-dup code in queue_recovery_request */
907         OBD_ALLOC(saved_req, sizeof *saved_req);
908         if (!saved_req)
909                 LBUG();
910         OBD_ALLOC(reqmsg, req->rq_reqlen);
911         if (!reqmsg)
912                 LBUG();
913         memcpy(saved_req, req, sizeof *saved_req);
914         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
915         /* the copied req takes over the reply state */
916         req->rq_reply_state = NULL;
917         req = saved_req;
918         req->rq_reqmsg = reqmsg;
919         class_export_get(req->rq_export);
920         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
921
922         spin_lock_bh(&obd->obd_processing_task_lock);
923         --obd->obd_recoverable_clients;
924         recovery_done = (obd->obd_recoverable_clients == 0);
925         spin_unlock_bh(&obd->obd_processing_task_lock);
926
927         if (recovery_done) {
928                 struct list_head *tmp, *n;
929                 ldlm_reprocess_all_ns(req->rq_export->exp_obd->obd_namespace);
930                 CWARN("%s: all clients recovered, sending delayed replies\n",
931                        obd->obd_name);
932                 spin_lock_bh(&obd->obd_processing_task_lock);
933                 obd->obd_recovering = 0;
934                 target_cancel_recovery_timer(obd);
935                 spin_unlock_bh(&obd->obd_processing_task_lock);
936
937                 /* when recovery finished, cleanup orphans on mds and ost */
938                 if (OBT(obd) && OBP(obd, postrecov)) {
939                         rc2 = OBP(obd, postrecov)(obd);
940                         if (rc2 >= 0)
941                                 CWARN("%s: all clients recovered, %d MDS "
942                                       "orphans deleted\n", obd->obd_name, rc2);
943                         else
944                                 CERROR("postrecov failed %d\n", rc2);
945                 }
946
947                 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
948                         req = list_entry(tmp, struct ptlrpc_request, rq_list);
949                         DEBUG_REQ(D_ERROR, req, "delayed:");
950                         ptlrpc_reply(req);
951                         class_export_put(req->rq_export);
952                         list_del(&req->rq_list);
953                         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
954                         OBD_FREE(req, sizeof *req);
955                 }
956                 ptlrpc_run_recovery_over_upcall(obd);
957         } else {
958                 CWARN("%s: %d recoverable clients remain\n",
959                        obd->obd_name, obd->obd_recoverable_clients);
960                 wake_up(&obd->obd_next_transno_waitq);
961         }
962
963         return 1;
964 }
965
966 int
967 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
968 {
969         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
970                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
971                 DEBUG_REQ(D_ERROR, req, "dropping reply");
972                 /* NB this does _not_ send with ACK disabled, to simulate
973                  * sending OK, but timing out for the ACK */
974                 if (req->rq_reply_state != NULL) {
975                         if (!req->rq_reply_state->rs_difficult) {
976                                 lustre_free_reply_state (req->rq_reply_state);
977                                 req->rq_reply_state = NULL;
978                         } else {
979                                 struct ptlrpc_service *svc =
980                                         req->rq_rqbd->rqbd_srv_ni->sni_service;
981                                 atomic_inc(&svc->srv_outstanding_replies);
982                         }
983                 }
984                 return (-ECOMM);
985         }
986
987         if (rc) {
988                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
989                 if (req->rq_reply_state == NULL) {
990                         rc = lustre_pack_reply (req, 0, NULL, NULL);
991                         if (rc != 0) {
992                                 CERROR ("can't allocate reply\n");
993                                 return (rc);
994                         }
995                 }
996                 req->rq_type = PTL_RPC_MSG_ERR;
997         } else {
998                 DEBUG_REQ(D_NET, req, "sending reply");
999         }
1000         
1001         return (ptlrpc_send_reply(req, 1));
1002 }
1003
1004 void 
1005 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1006 {
1007         int                        netrc;
1008         unsigned long              flags;
1009         struct ptlrpc_reply_state *rs;
1010         struct obd_device         *obd;
1011         struct obd_export         *exp;
1012         struct ptlrpc_srv_ni      *sni;
1013         struct ptlrpc_service     *svc;
1014
1015         sni = req->rq_rqbd->rqbd_srv_ni;
1016         svc = sni->sni_service;
1017         
1018         rs = req->rq_reply_state;
1019         if (rs == NULL || !rs->rs_difficult) {
1020                 /* The easy case; no notifiers and reply_out_callback()
1021                  * cleans up (i.e. we can't look inside rs after a
1022                  * successful send) */
1023                 netrc = target_send_reply_msg (req, rc, fail_id);
1024
1025                 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1026                 return;
1027         }
1028
1029         /* must be an export if locks saved */
1030         LASSERT (req->rq_export != NULL);
1031         /* req/reply consistent */
1032         LASSERT (rs->rs_srv_ni == sni);
1033
1034         /* "fresh" reply */
1035         LASSERT (!rs->rs_scheduled);
1036         LASSERT (!rs->rs_scheduled_ever);
1037         LASSERT (!rs->rs_handled);
1038         LASSERT (!rs->rs_on_net);
1039         LASSERT (rs->rs_export == NULL);
1040         LASSERT (list_empty(&rs->rs_obd_list));
1041         LASSERT (list_empty(&rs->rs_exp_list));
1042
1043         exp = class_export_get (req->rq_export);
1044         obd = exp->exp_obd;
1045
1046         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1047         rs->rs_scheduled = 1;
1048         rs->rs_on_net    = 1;
1049         rs->rs_xid       = req->rq_xid;
1050         rs->rs_transno   = req->rq_transno;
1051         rs->rs_export    = exp;
1052         
1053         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1054
1055         if (rs->rs_transno > obd->obd_last_committed) {
1056                 /* not committed already */ 
1057                 list_add_tail (&rs->rs_obd_list, 
1058                                &obd->obd_uncommitted_replies);
1059         }
1060
1061         spin_unlock (&obd->obd_uncommitted_replies_lock);
1062         spin_lock (&exp->exp_lock);
1063
1064         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1065
1066         spin_unlock_irqrestore (&exp->exp_lock, flags);
1067
1068         netrc = target_send_reply_msg (req, rc, fail_id);
1069
1070         spin_lock_irqsave (&svc->srv_lock, flags);
1071
1072         svc->srv_n_difficult_replies++;
1073
1074         if (netrc != 0) /* error sending: reply is off the net */
1075                 rs->rs_on_net = 0;
1076
1077         if (!rs->rs_on_net ||                   /* some notifier */
1078             list_empty(&rs->rs_exp_list) ||     /* completed already */
1079             list_empty(&rs->rs_obd_list)) {
1080                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1081                 wake_up (&svc->srv_waitq);
1082         } else {
1083                 list_add (&rs->rs_list, &sni->sni_active_replies);
1084                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1085         }
1086
1087         spin_unlock_irqrestore (&svc->srv_lock, flags);
1088 }
1089
1090 int target_handle_ping(struct ptlrpc_request *req)
1091 {
1092         return lustre_pack_reply(req, 0, NULL, NULL);
1093 }