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