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