Whamcloud - gitweb
r=zab,phil
[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_dirty_granted = 0;
101         cli->cl_dirty_max = 64*1024*1024; /* some default */
102         cli->cl_ost_can_grant = 1;
103         INIT_LIST_HEAD(&cli->cl_cache_waiters);
104         INIT_LIST_HEAD(&cli->cl_loi_ready_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 = 8;
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         INIT_LIST_HEAD(&imp->imp_pinger_chain);
138         memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
139               lcfg->lcfg_inllen1);
140         class_import_put(imp);
141
142         cli->cl_import = imp;
143         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
144         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
145         cli->cl_sandev = to_kdev_t(0);
146
147         if (lcfg->lcfg_inllen3 != 0) {
148                 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
149                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
150                                name, obddev->obd_name,
151                                imp->imp_target_uuid.uuid);
152                         imp->imp_invalid = 1;
153
154                         if (lcfg->lcfg_inllen4 != 0)
155                                 mgmt_name = lcfg->lcfg_inlbuf4;
156                 } else {
157                         mgmt_name = lcfg->lcfg_inlbuf3;
158                 }
159         }
160
161         if (mgmt_name != NULL) {
162                 /* Register with management client if we need to. */
163                 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
164                        obddev->obd_name, mgmt_name, server_uuid.uuid);
165
166                 mgmt_obd = class_name2obd(mgmt_name);
167                 if (!mgmt_obd) {
168                         CERROR("can't find mgmtcli %s to register\n",
169                                mgmt_name);
170                         GOTO(err_import, rc = -ENOSYS);
171                 }
172
173                 register_f = inter_module_get("mgmtcli_register_for_events");
174                 if (!register_f) {
175                         CERROR("can't i_m_g mgmtcli_register_for_events\n");
176                         GOTO(err_import, rc = -ENOSYS);
177                 }
178
179                 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
180                 inter_module_put("mgmtcli_register_for_events");
181
182                 if (!rc)
183                         cli->cl_mgmtcli_obd = mgmt_obd;
184         }
185
186         RETURN(rc);
187
188 err_import:
189         class_destroy_import(imp);
190 err_ldlm:
191         ldlm_put_ref(0);
192 err:
193         RETURN(rc);
194
195 }
196
197 int client_obd_cleanup(struct obd_device *obddev, int flags)
198 {
199         struct client_obd *cli = &obddev->u.cli;
200
201         if (!cli->cl_import)
202                 RETURN(-EINVAL);
203         if (cli->cl_mgmtcli_obd) {
204                 mgmtcli_deregister_for_events_t dereg_f;
205
206                 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
207                 dereg_f(cli->cl_mgmtcli_obd, obddev);
208                 inter_module_put("mgmtcli_deregister_for_events");
209         }
210         class_destroy_import(cli->cl_import);
211         cli->cl_import = NULL;
212
213         ldlm_put_ref(flags & OBD_OPT_FORCE);
214
215         RETURN(0);
216 }
217
218 int client_connect_import(struct lustre_handle *dlm_handle,
219                           struct obd_device *obd,
220                           struct obd_uuid *cluuid)
221 {
222         struct client_obd *cli = &obd->u.cli;
223         struct obd_import *imp = cli->cl_import;
224         struct obd_export *exp;
225         int rc;
226         ENTRY;
227
228         down(&cli->cl_sem);
229         rc = class_connect(dlm_handle, obd, cluuid);
230         if (rc)
231                 GOTO(out_sem, rc);
232
233         cli->cl_conn_count++;
234         if (cli->cl_conn_count > 1)
235                 GOTO(out_sem, rc);
236         exp = class_conn2export(dlm_handle);
237
238         if (obd->obd_namespace != NULL)
239                 CERROR("already have namespace!\n");
240         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
241                                                 LDLM_NAMESPACE_CLIENT);
242         if (obd->obd_namespace == NULL)
243                 GOTO(out_disco, rc = -ENOMEM);
244
245         imp->imp_dlm_handle = *dlm_handle;
246         imp->imp_state = LUSTRE_IMP_DISCON;
247
248         rc = ptlrpc_connect_import(imp);
249         if (rc != 0) {
250                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
251                 GOTO(out_ldlm, rc);
252         }
253
254         LASSERT (imp->imp_state == LUSTRE_IMP_FULL);
255
256         exp->exp_connection = ptlrpc_connection_addref(imp->imp_connection);
257
258         if (imp->imp_replayable) {
259                 CDEBUG(D_HA, "connected to replayable target: %s\n",
260                        imp->imp_target_uuid.uuid);
261                 ptlrpc_pinger_add_import(imp);
262         }
263
264         CDEBUG(D_HA, "local import: %p, remote handle: "LPX64"\n", imp,
265                imp->imp_remote_handle.cookie);
266
267         EXIT;
268
269         if (rc) {
270 out_ldlm:
271                 ldlm_namespace_free(obd->obd_namespace, 0);
272                 obd->obd_namespace = NULL;
273 out_disco:
274                 cli->cl_conn_count--;
275                 class_disconnect(exp, 0);
276         } else {
277                 class_export_put(exp);
278         }
279 out_sem:
280         up(&cli->cl_sem);
281         return rc;
282 }
283
284 int client_disconnect_export(struct obd_export *exp, int failover)
285 {
286         struct obd_device *obd = class_exp2obd(exp);
287         struct client_obd *cli = &obd->u.cli;
288         struct obd_import *imp = cli->cl_import;
289         int rc = 0, err;
290         ENTRY;
291
292         if (!obd) {
293                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
294                        exp, exp ? exp->exp_handle.h_cookie : -1);
295                 RETURN(-EINVAL);
296         }
297
298         down(&cli->cl_sem);
299         if (!cli->cl_conn_count) {
300                 CERROR("disconnecting disconnected device (%s)\n",
301                        obd->obd_name);
302                 GOTO(out_sem, rc = -EINVAL);
303         }
304
305         cli->cl_conn_count--;
306         if (cli->cl_conn_count)
307                 GOTO(out_no_disconnect, rc = 0);
308
309         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
310          * delete it regardless.  (It's safe to delete an import that was
311          * never added.) */
312         (void)ptlrpc_pinger_del_import(imp);
313
314         if (obd->obd_namespace != NULL) {
315                 /* obd_no_recov == local only */
316                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
317                                        obd->obd_no_recov, NULL);
318                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
319                 obd->obd_namespace = NULL;
320         }
321
322         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
323         if (obd->obd_no_recov)
324                 ptlrpc_set_import_active(imp, 0);
325         else
326                 rc = ptlrpc_disconnect_import(imp);
327
328         imp->imp_state = LUSTRE_IMP_NEW;
329
330         EXIT;
331  out_no_disconnect:
332         err = class_disconnect(exp, 0);
333         if (!rc && err)
334                 rc = err;
335  out_sem:
336         up(&cli->cl_sem);
337         RETURN(rc);
338 }
339
340 /* --------------------------------------------------------------------------
341  * from old lib/target.c
342  * -------------------------------------------------------------------------- */
343
344 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
345                             struct obd_uuid *cluuid)
346 {
347         if (exp->exp_connection) {
348                 struct lustre_handle *hdl;
349                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
350                 /* Might be a re-connect after a partition. */
351                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
352                         CERROR("%s reconnecting\n", cluuid->uuid);
353                         conn->cookie = exp->exp_handle.h_cookie;
354                         RETURN(EALREADY);
355                 } else {
356                         CERROR("%s reconnecting from %s, "
357                                "handle mismatch (ours "LPX64", theirs "
358                                LPX64")\n", cluuid->uuid,
359                                exp->exp_connection->c_remote_uuid.uuid,
360                                hdl->cookie, conn->cookie);
361                         memset(conn, 0, sizeof *conn);
362                         RETURN(-EALREADY);
363                 }
364         }
365
366         conn->cookie = exp->exp_handle.h_cookie;
367         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
368                cluuid->uuid, exp);
369         CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
370         RETURN(0);
371 }
372
373 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
374 {
375         struct obd_device *target;
376         struct obd_export *export = NULL;
377         struct obd_import *revimp;
378         struct lustre_handle conn;
379         struct obd_uuid tgtuuid;
380         struct obd_uuid cluuid;
381         struct obd_uuid remote_uuid;
382         struct list_head *p;
383         char *str, *tmp;
384         int rc = 0, abort_recovery;
385         ENTRY;
386
387         LASSERT_REQSWAB (req, 0);
388         str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
389         if (str == NULL) {
390                 CERROR("bad target UUID for connect\n");
391                 GOTO(out, rc = -EINVAL);
392         }
393
394         obd_str2uuid (&tgtuuid, str);
395         target = class_uuid2obd(&tgtuuid);
396         if (!target) {
397                 target = class_name2obd(str);
398         }
399
400         if (!target || target->obd_stopping || !target->obd_set_up) {
401                 CERROR("UUID '%s' is not available for connect\n", str);
402                 GOTO(out, rc = -ENODEV);
403         }
404
405         LASSERT_REQSWAB (req, 1);
406         str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
407         if (str == NULL) {
408                 CERROR("bad client UUID for connect\n");
409                 GOTO(out, rc = -EINVAL);
410         }
411
412         obd_str2uuid (&cluuid, str);
413
414         /* XXX extract a nettype and format accordingly */
415         snprintf(remote_uuid.uuid, sizeof remote_uuid,
416                  "NET_"LPX64"_UUID", req->rq_peer.peer_nid);
417
418         spin_lock_bh(&target->obd_processing_task_lock);
419         abort_recovery = target->obd_abort_recovery;
420         spin_unlock_bh(&target->obd_processing_task_lock);
421         if (abort_recovery)
422                 target_abort_recovery(target);
423
424         tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
425         if (tmp == NULL)
426                 GOTO(out, rc = -EPROTO);
427
428         memcpy(&conn, tmp, sizeof conn);
429
430         rc = lustre_pack_reply(req, 0, NULL, NULL);
431         if (rc)
432                 GOTO(out, rc);
433
434         /* lctl gets a backstage, all-access pass. */
435         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
436                 goto dont_check_exports;
437
438         spin_lock(&target->obd_dev_lock);
439         list_for_each(p, &target->obd_exports) {
440                 export = list_entry(p, struct obd_export, exp_obd_chain);
441                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
442                         spin_unlock(&target->obd_dev_lock);
443                         LASSERT(export->exp_obd == target);
444
445                         rc = target_handle_reconnect(&conn, export, &cluuid);
446                         break;
447                 }
448                 export = NULL;
449         }
450         /* If we found an export, we already unlocked. */
451         if (!export) {
452                 spin_unlock(&target->obd_dev_lock);
453         } else if (req->rq_reqmsg->conn_cnt == 1) {
454                 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
455                        cluuid.uuid);
456                 GOTO(out, rc = -EALREADY);
457         }
458
459         /* Tell the client if we're in recovery. */
460         /* If this is the first client, start the recovery timer */
461         if (target->obd_recovering) {
462                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
463                 target_start_recovery_timer(target, handler);
464         }
465
466         /* Tell the client if we support replayable requests */
467         if (target->obd_replayable)
468                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
469
470         if (export == NULL) {
471                 if (target->obd_recovering) {
472                         CERROR("denying connection for new client %s: "
473                                "%d clients in recovery for %lds\n", cluuid.uuid,
474                                target->obd_recoverable_clients,
475                                (target->obd_recovery_timer.expires-jiffies)/HZ);
476                         rc = -EBUSY;
477                 } else {
478  dont_check_exports:
479                         rc = obd_connect(&conn, target, &cluuid);
480                 }
481         }
482
483         /* If all else goes well, this is our RPC return code. */
484         req->rq_status = 0;
485
486         if (rc && rc != EALREADY)
487                 GOTO(out, rc);
488
489         /* XXX track this all the time? */
490         if (target->obd_recovering) {
491                 target->obd_connected_clients++;
492         }
493
494         req->rq_repmsg->handle = conn;
495
496         /* If the client and the server are the same node, we will already
497          * have an export that really points to the client's DLM export,
498          * because we have a shared handles table.
499          *
500          * XXX this will go away when shaver stops sending the "connect" handle
501          * in the real "remote handle" field of the request --phik 24 Apr 2003
502          */
503         if (req->rq_export != NULL)
504                 class_export_put(req->rq_export);
505
506         /* ownership of this export ref transfers to the request */
507         export = req->rq_export = class_conn2export(&conn);
508         LASSERT(export != NULL);
509
510         if (req->rq_connection != NULL)
511                 ptlrpc_put_connection(req->rq_connection);
512         if (export->exp_connection != NULL)
513                 ptlrpc_put_connection(export->exp_connection);
514         export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
515                                                        &remote_uuid);
516         req->rq_connection = ptlrpc_connection_addref(export->exp_connection);
517
518         LASSERT(export->exp_conn_cnt < req->rq_reqmsg->conn_cnt);
519         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
520
521         if (rc == EALREADY) {
522                 /* We indicate the reconnection in a flag, not an error code. */
523                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
524                 GOTO(out, rc = 0);
525         }
526
527         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
528                sizeof conn);
529
530         if (export->exp_imp_reverse != NULL)
531                 class_destroy_import(export->exp_imp_reverse);
532         revimp = export->exp_imp_reverse = class_new_import();
533         revimp->imp_connection = ptlrpc_connection_addref(req->rq_connection);
534         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
535         revimp->imp_remote_handle = conn;
536         revimp->imp_obd = target;
537         revimp->imp_dlm_fake = 1;
538         revimp->imp_state = LUSTRE_IMP_FULL;
539         class_import_put(revimp);
540 out:
541         if (rc)
542                 req->rq_status = rc;
543         RETURN(rc);
544 }
545
546 int target_handle_disconnect(struct ptlrpc_request *req)
547 {
548         int rc;
549         ENTRY;
550
551         rc = lustre_pack_reply(req, 0, NULL, NULL);
552         if (rc)
553                 RETURN(rc);
554
555         req->rq_status = obd_disconnect(req->rq_export, 0);
556         req->rq_export = NULL;
557         RETURN(0);
558 }
559
560 void target_destroy_export(struct obd_export *exp)
561 {
562         /* exports created from last_rcvd data, and "fake"
563            exports created by lctl don't have an import */
564         if (exp->exp_imp_reverse != NULL)
565                 class_destroy_import(exp->exp_imp_reverse);
566
567         /* We cancel locks at disconnect time, but this will catch any locks
568          * granted in a race with recovery-induced disconnect. */
569         ldlm_cancel_locks_for_export(exp);
570 }
571
572 /*
573  * Recovery functions
574  */
575
576 void target_cancel_recovery_timer(struct obd_device *obd)
577 {
578         del_timer(&obd->obd_recovery_timer);
579 }
580
581 static void abort_delayed_replies(struct obd_device *obd)
582 {
583         struct ptlrpc_request *req;
584         struct list_head *tmp, *n;
585         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
586                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
587                 DEBUG_REQ(D_ERROR, req, "aborted:");
588                 req->rq_status = -ENOTCONN;
589                 req->rq_type = PTL_RPC_MSG_ERR;
590                 ptlrpc_reply(req);
591                 list_del(&req->rq_list);
592                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
593                 OBD_FREE(req, sizeof *req);
594         }
595 }
596
597 static void abort_recovery_queue(struct obd_device *obd)
598 {
599         struct ptlrpc_request *req;
600         struct list_head *tmp, *n;
601         int rc;
602
603         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
604                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
605                 DEBUG_REQ(D_ERROR, req, "aborted:");
606                 req->rq_status = -ENOTCONN;
607                 req->rq_type = PTL_RPC_MSG_ERR;
608                 rc = lustre_pack_reply(req, 0, NULL, NULL);
609                 if (rc == 0) {
610                         ptlrpc_reply(req);
611                 } else {
612                         DEBUG_REQ(D_ERROR, req,
613                                   "packing failed for abort-reply; skipping");
614                 }
615                 list_del(&req->rq_list);
616                 class_export_put(req->rq_export);
617                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
618                 OBD_FREE(req, sizeof *req);
619         }
620 }
621
622 void target_abort_recovery(void *data)
623 {
624         struct obd_device *obd = data;
625         int rc;
626
627         CERROR("disconnecting clients and aborting recovery\n");
628         spin_lock_bh(&obd->obd_processing_task_lock);
629         if (!obd->obd_recovering) {
630                 spin_unlock_bh(&obd->obd_processing_task_lock);
631                 EXIT;
632                 return;
633         }
634
635         obd->obd_recovering = obd->obd_abort_recovery = 0;
636
637         wake_up(&obd->obd_next_transno_waitq);
638         target_cancel_recovery_timer(obd);
639         spin_unlock_bh(&obd->obd_processing_task_lock);
640
641         class_disconnect_exports(obd, 0);
642
643         /* when recovery was abort, cleanup orphans for mds */
644         if (OBT(obd) && OBP(obd, postrecov)) {
645                 rc = OBP(obd, postrecov)(obd);
646                 if (rc >= 0)
647                         CWARN("Cleanup %d orphans after recovery was aborted\n", rc);
648                 else
649                         CERROR("postrecov failed %d\n", rc);
650         }
651
652         abort_delayed_replies(obd);
653         abort_recovery_queue(obd);
654         ptlrpc_run_recovery_over_upcall(obd);
655 }
656
657 static void target_recovery_expired(unsigned long castmeharder)
658 {
659         struct obd_device *obd = (struct obd_device *)castmeharder;
660         CERROR("recovery timed out, aborting\n");
661         spin_lock_bh(&obd->obd_processing_task_lock);
662         obd->obd_abort_recovery = 1;
663         wake_up(&obd->obd_next_transno_waitq);
664         spin_unlock_bh(&obd->obd_processing_task_lock);
665 }
666
667 static void reset_recovery_timer(struct obd_device *obd)
668 {
669         int recovering;
670         spin_lock(&obd->obd_dev_lock);
671         recovering = obd->obd_recovering;
672         spin_unlock(&obd->obd_dev_lock);
673
674         if (!recovering)
675                 return;
676         CDEBUG(D_HA, "timer will expire in %u seconds\n",
677                OBD_RECOVERY_TIMEOUT / HZ);
678         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
679 }
680
681
682 /* Only start it the first time called */
683 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
684 {
685         spin_lock_bh(&obd->obd_processing_task_lock);
686         if (obd->obd_recovery_handler) {
687                 spin_unlock_bh(&obd->obd_processing_task_lock);
688                 return;
689         }
690         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
691                OBD_RECOVERY_TIMEOUT / HZ);
692         obd->obd_recovery_handler = handler;
693         obd->obd_recovery_timer.function = target_recovery_expired;
694         obd->obd_recovery_timer.data = (unsigned long)obd;
695         init_timer(&obd->obd_recovery_timer);
696         spin_unlock_bh(&obd->obd_processing_task_lock);
697
698         reset_recovery_timer(obd);
699 }
700
701 static int check_for_next_transno(struct obd_device *obd)
702 {
703         struct ptlrpc_request *req;
704         int wake_up = 0, connected, completed, queue_len, max;
705         __u64 next_transno, req_transno;
706
707         spin_lock_bh(&obd->obd_processing_task_lock);
708         req = list_entry(obd->obd_recovery_queue.next,
709                          struct ptlrpc_request, rq_list);
710         max = obd->obd_max_recoverable_clients;
711         req_transno = req->rq_reqmsg->transno;
712         connected = obd->obd_connected_clients;
713         completed = max - obd->obd_recoverable_clients;
714         queue_len = obd->obd_requests_queued_for_recovery;
715         next_transno = obd->obd_next_recovery_transno;
716
717         if (obd->obd_abort_recovery) {
718                 CDEBUG(D_HA, "waking for aborted recovery\n");
719                 wake_up = 1;
720         } else if (!obd->obd_recovering) {
721                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
722                 wake_up = 1;
723         } else if (req_transno == next_transno) {
724                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
725                 wake_up = 1;
726         } else if (queue_len + completed == max) {
727                 CDEBUG(D_ERROR,
728                        "waking for skipped transno (skip: "LPD64
729                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
730                        next_transno, queue_len, completed, max, req_transno);
731                 obd->obd_next_recovery_transno = req_transno;
732                 wake_up = 1;
733         }
734         spin_unlock_bh(&obd->obd_processing_task_lock);
735         LASSERT(req->rq_reqmsg->transno >= next_transno);
736         return wake_up;
737 }
738
739 static void process_recovery_queue(struct obd_device *obd)
740 {
741         struct ptlrpc_request *req;
742         int abort_recovery = 0;
743         struct l_wait_info lwi = { 0 };
744         ENTRY;
745
746         for (;;) {
747                 spin_lock_bh(&obd->obd_processing_task_lock);
748                 LASSERT(obd->obd_processing_task == current->pid);
749                 req = list_entry(obd->obd_recovery_queue.next,
750                                  struct ptlrpc_request, rq_list);
751
752                 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
753                         spin_unlock_bh(&obd->obd_processing_task_lock);
754                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
755                                LPD64")\n",
756                                obd->obd_next_recovery_transno,
757                                req->rq_reqmsg->transno);
758                         l_wait_event(obd->obd_next_transno_waitq,
759                                      check_for_next_transno(obd), &lwi);
760                         spin_lock_bh(&obd->obd_processing_task_lock);
761                         abort_recovery = obd->obd_abort_recovery;
762                         spin_unlock_bh(&obd->obd_processing_task_lock);
763                         if (abort_recovery) {
764                                 target_abort_recovery(obd);
765                                 return;
766                         }
767                         continue;
768                 }
769                 list_del_init(&req->rq_list);
770                 obd->obd_requests_queued_for_recovery--;
771                 spin_unlock_bh(&obd->obd_processing_task_lock);
772
773                 DEBUG_REQ(D_HA, req, "processing: ");
774                 (void)obd->obd_recovery_handler(req);
775                 obd->obd_replayed_requests++;
776                 reset_recovery_timer(obd);
777                 /* bug 1580: decide how to properly sync() in recovery */
778                 //mds_fsync_super(mds->mds_sb);
779                 class_export_put(req->rq_export);
780                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
781                 OBD_FREE(req, sizeof *req);
782                 spin_lock_bh(&obd->obd_processing_task_lock);
783                 obd->obd_next_recovery_transno++;
784                 if (list_empty(&obd->obd_recovery_queue)) {
785                         obd->obd_processing_task = 0;
786                         spin_unlock_bh(&obd->obd_processing_task_lock);
787                         break;
788                 }
789                 spin_unlock_bh(&obd->obd_processing_task_lock);
790         }
791         EXIT;
792 }
793
794 int target_queue_recovery_request(struct ptlrpc_request *req,
795                                   struct obd_device *obd)
796 {
797         struct list_head *tmp;
798         int inserted = 0;
799         __u64 transno = req->rq_reqmsg->transno;
800         struct ptlrpc_request *saved_req;
801         struct lustre_msg *reqmsg;
802
803         /* CAVEAT EMPTOR: The incoming request message has been swabbed
804          * (i.e. buflens etc are in my own byte order), but type-dependent
805          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
806
807         if (!transno) {
808                 INIT_LIST_HEAD(&req->rq_list);
809                 DEBUG_REQ(D_HA, req, "not queueing");
810                 return 1;
811         }
812
813         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
814         /* XXX just like the request-dup code in queue_final_reply */
815         OBD_ALLOC(saved_req, sizeof *saved_req);
816         if (!saved_req)
817                 LBUG();
818         OBD_ALLOC(reqmsg, req->rq_reqlen);
819         if (!reqmsg)
820                 LBUG();
821
822         spin_lock_bh(&obd->obd_processing_task_lock);
823
824         /* If we're processing the queue, we want don't want to queue this
825          * message.
826          *
827          * Also, if this request has a transno less than the one we're waiting
828          * for, we should process it now.  It could (and currently always will)
829          * be an open request for a descriptor that was opened some time ago.
830          */
831         if (obd->obd_processing_task == current->pid ||
832             transno < obd->obd_next_recovery_transno) {
833                 /* Processing the queue right now, don't re-add. */
834                 LASSERT(list_empty(&req->rq_list));
835                 spin_unlock_bh(&obd->obd_processing_task_lock);
836                 OBD_FREE(reqmsg, req->rq_reqlen);
837                 OBD_FREE(saved_req, sizeof *saved_req);
838                 return 1;
839         }
840
841         memcpy(saved_req, req, sizeof *req);
842         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
843         req = saved_req;
844         req->rq_reqmsg = reqmsg;
845         class_export_get(req->rq_export);
846         INIT_LIST_HEAD(&req->rq_list);
847
848         /* XXX O(n^2) */
849         list_for_each(tmp, &obd->obd_recovery_queue) {
850                 struct ptlrpc_request *reqiter =
851                         list_entry(tmp, struct ptlrpc_request, rq_list);
852
853                 if (reqiter->rq_reqmsg->transno > transno) {
854                         list_add_tail(&req->rq_list, &reqiter->rq_list);
855                         inserted = 1;
856                         break;
857                 }
858         }
859
860         if (!inserted) {
861                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
862         }
863
864         obd->obd_requests_queued_for_recovery++;
865
866         if (obd->obd_processing_task != 0) {
867                 /* Someone else is processing this queue, we'll leave it to
868                  * them.
869                  */
870                 wake_up(&obd->obd_next_transno_waitq);
871                 spin_unlock_bh(&obd->obd_processing_task_lock);
872                 return 0;
873         }
874
875         /* Nobody is processing, and we know there's (at least) one to process
876          * now, so we'll do the honours.
877          */
878         obd->obd_processing_task = current->pid;
879         spin_unlock_bh(&obd->obd_processing_task_lock);
880
881         process_recovery_queue(obd);
882         return 0;
883 }
884
885 struct obd_device * target_req2obd(struct ptlrpc_request *req)
886 {
887         return req->rq_export->exp_obd;
888 }
889
890 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
891 {
892         struct obd_device *obd = target_req2obd(req);
893         struct ptlrpc_request *saved_req;
894         struct lustre_msg *reqmsg;
895         int recovery_done = 0;
896         int rc2;
897
898         if (rc) {
899                 /* Just like ptlrpc_error, but without the sending. */
900                 rc = lustre_pack_reply(req, 0, NULL, NULL);
901                 LASSERT(rc == 0); /* XXX handle this */
902                 req->rq_type = PTL_RPC_MSG_ERR;
903         }
904
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         req = saved_req;
916         req->rq_reqmsg = reqmsg;
917         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
918
919         spin_lock_bh(&obd->obd_processing_task_lock);
920         --obd->obd_recoverable_clients;
921         recovery_done = (obd->obd_recoverable_clients == 0);
922         spin_unlock_bh(&obd->obd_processing_task_lock);
923
924         if (recovery_done) {
925                 struct list_head *tmp, *n;
926                 ldlm_reprocess_all_ns(req->rq_export->exp_obd->obd_namespace);
927                 CWARN("%s: all clients recovered, sending delayed replies\n",
928                        obd->obd_name);
929                 obd->obd_recovering = 0;
930
931                 /* when recovering finished, cleanup orphans for mds */
932                 if (OBT(obd) && OBP(obd, postrecov)) {
933                         rc2 = OBP(obd, postrecov)(obd);
934                         if (rc2 >= 0)
935                                 CWARN("%s: all clients recovered, %d MDS orphans "
936                                        "deleted\n", obd->obd_name, rc2);
937                         else
938                                 CERROR("postrecov failed %d\n", rc2);
939                 }
940
941                 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
942                         req = list_entry(tmp, struct ptlrpc_request, rq_list);
943                         DEBUG_REQ(D_ERROR, req, "delayed:");
944                         ptlrpc_reply(req);
945                         list_del(&req->rq_list);
946                         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
947                         OBD_FREE(req, sizeof *req);
948                 }
949                 target_cancel_recovery_timer(obd);
950         } else {
951                 CERROR("%s: %d recoverable clients remain\n",
952                        obd->obd_name, obd->obd_recoverable_clients);
953                 wake_up(&obd->obd_next_transno_waitq);
954         }
955
956         return 1;
957 }
958
959 static void ptlrpc_abort_reply (struct ptlrpc_request *req)
960 {
961         /* On return, we must be sure that the ACK callback has either
962          * happened or will not happen.  Note that the SENT callback will
963          * happen come what may since we successfully posted the PUT. */
964         int rc;
965         struct l_wait_info lwi;
966         unsigned long flags;
967
968  again:
969         /* serialise with ACK callback */
970         spin_lock_irqsave (&req->rq_lock, flags);
971         if (!req->rq_want_ack) {
972                 spin_unlock_irqrestore (&req->rq_lock, flags);
973                 /* The ACK callback has happened already.  Although the
974                  * SENT callback might still be outstanding (yes really) we
975                  * don't care; this is just like normal completion. */
976                 return;
977         }
978         spin_unlock_irqrestore (&req->rq_lock, flags);
979
980         /* Have a bash at unlinking the MD.  This will fail until the SENT
981          * callback has happened since the MD is busy from the PUT.  If the
982          * ACK still hasn't arrived after then, a successful unlink will
983          * ensure the ACK callback never happens. */
984         rc = PtlMDUnlink (req->rq_reply_md_h);
985         switch (rc) {
986         default:
987                 LBUG ();
988         case PTL_OK:
989                 /* SENT callback happened; ACK callback preempted */
990                 LASSERT (req->rq_want_ack);
991                 spin_lock_irqsave (&req->rq_lock, flags);
992                 req->rq_want_ack = 0;
993                 spin_unlock_irqrestore (&req->rq_lock, flags);
994                 return;
995         case PTL_INV_MD:
996                 return;
997         case PTL_MD_INUSE:
998                 /* Still sending or ACK callback in progress: wait until
999                  * either callback has completed and try again.
1000                  * Actually we can't wait for the SENT callback because
1001                  * there's no state the SENT callback can touch that will
1002                  * allow it to communicate with us!  So we just wait here
1003                  * for a short time, effectively polling for the SENT
1004                  * callback by calling PtlMDUnlink() again, to see if it
1005                  * has finished.  Note that if the ACK does arrive, its
1006                  * callback wakes us in short order. --eeb */
1007                 lwi = LWI_TIMEOUT (HZ/4, NULL, NULL);
1008                 rc = l_wait_event(req->rq_reply_waitq, !req->rq_want_ack,
1009                                   &lwi);
1010                 CDEBUG (D_HA, "Retrying req %p: %d\n", req, rc);
1011                 /* NB go back and test rq_want_ack with locking, to ensure
1012                  * if ACK callback happened, it has completed stopped
1013                  * referencing this req. */
1014                 goto again;
1015         }
1016 }
1017
1018 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1019 {
1020         int i;
1021         int netrc;
1022         unsigned long flags;
1023         struct ptlrpc_req_ack_lock *ack_lock;
1024         struct l_wait_info lwi = { 0 };
1025         wait_queue_t commit_wait;
1026         struct obd_device *obd =
1027                 req->rq_export ? req->rq_export->exp_obd : NULL;
1028         struct obd_export *exp = NULL;
1029
1030         if (req->rq_export) {
1031                 for (i = 0; i < REQ_MAX_ACK_LOCKS; i++) {
1032                         if (req->rq_ack_locks[i].mode) {
1033                                 exp = req->rq_export;
1034                                 break;
1035                         }
1036                 }
1037         }
1038
1039         if (exp) {
1040                 exp->exp_outstanding_reply = req;
1041                 spin_lock_irqsave (&req->rq_lock, flags);
1042                 req->rq_want_ack = 1;
1043                 spin_unlock_irqrestore (&req->rq_lock, flags);
1044         }
1045
1046         if (!OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1047                 if (rc == 0) {
1048                         DEBUG_REQ(D_NET, req, "sending reply");
1049                         netrc = ptlrpc_reply(req);
1050                 } else if (rc == -ENOTCONN) {
1051                         DEBUG_REQ(D_HA, req, "processing error (%d)", rc);
1052                         netrc = ptlrpc_error(req);
1053                 } else {
1054                         DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1055                         netrc = ptlrpc_error(req);
1056                 }
1057         } else {
1058                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1059                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1060                 if (req->rq_repmsg) {
1061                         OBD_FREE(req->rq_repmsg, req->rq_replen);
1062                         req->rq_repmsg = NULL;
1063                 }
1064                 init_waitqueue_head(&req->rq_reply_waitq);
1065                 netrc = 0;
1066         }
1067
1068         /* a failed send simulates the callbacks */
1069         LASSERT(netrc == 0 || req->rq_want_ack == 0);
1070         if (exp == NULL) {
1071                 LASSERT(req->rq_want_ack == 0);
1072                 return;
1073         }
1074         LASSERT(obd != NULL);
1075
1076         init_waitqueue_entry(&commit_wait, current);
1077         add_wait_queue(&obd->obd_commit_waitq, &commit_wait);
1078         rc = l_wait_event(req->rq_reply_waitq,
1079                           !req->rq_want_ack || req->rq_resent ||
1080                           req->rq_transno <= obd->obd_last_committed, &lwi);
1081         remove_wait_queue(&obd->obd_commit_waitq, &commit_wait);
1082
1083         spin_lock_irqsave (&req->rq_lock, flags);
1084         /* If we got here because the ACK callback ran, this acts as a
1085          * barrier to ensure the callback completed the wakeup. */
1086         spin_unlock_irqrestore (&req->rq_lock, flags);
1087
1088         /* If we committed the transno already, then we might wake up before
1089          * the ack arrives.  We need to stop waiting for the ack before we can
1090          * reuse this request structure.  We are guaranteed by this point that
1091          * this cannot abort the sending of the actual reply.*/
1092         ptlrpc_abort_reply(req);
1093
1094         if (req->rq_resent) {
1095                 DEBUG_REQ(D_HA, req, "resent: not cancelling locks");
1096                 return;
1097         }
1098
1099         LASSERT(rc == 0);
1100         DEBUG_REQ(D_HA, req, "cancelling locks for %s",
1101                   req->rq_want_ack ? "commit" : "ack");
1102
1103         exp->exp_outstanding_reply = NULL;
1104
1105         for (ack_lock = req->rq_ack_locks, i = 0;
1106              i < REQ_MAX_ACK_LOCKS; i++, ack_lock++) {
1107                 if (!ack_lock->mode)
1108                         continue;
1109                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1110         }
1111 }
1112
1113 int target_handle_ping(struct ptlrpc_request *req)
1114 {
1115         return lustre_pack_reply(req, 0, NULL, NULL);
1116 }
1117
1118 void *ldlm_put_lock_into_req(struct ptlrpc_request *req,
1119                                 struct lustre_handle *lock, int mode)
1120 {
1121         int i;
1122
1123         for (i = 0; i < REQ_MAX_ACK_LOCKS; i++) {
1124                 if (req->rq_ack_locks[i].mode)
1125                         continue;
1126                 memcpy(&req->rq_ack_locks[i].lock, lock, sizeof(*lock));
1127                 req->rq_ack_locks[i].mode = mode;
1128                 return &req->rq_ack_locks[i];
1129         }
1130         CERROR("no space for lock in struct ptlrpc_request\n");
1131         LBUG();
1132         return NULL;
1133 }
1134