Whamcloud - gitweb
Ignore all .mergeinfo files.
[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         ldlm_cancel_locks_for_export(exp);
561 }
562
563 /*
564  * Recovery functions
565  */
566
567 static void abort_delayed_replies(struct obd_device *obd)
568 {
569         struct ptlrpc_request *req;
570         struct list_head *tmp, *n;
571         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
572                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
573                 DEBUG_REQ(D_ERROR, req, "aborted:");
574                 req->rq_status = -ENOTCONN;
575                 req->rq_type = PTL_RPC_MSG_ERR;
576                 ptlrpc_reply(req);
577                 class_export_put(req->rq_export);
578                 list_del(&req->rq_list);
579                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
580                 OBD_FREE(req, sizeof *req);
581         }
582 }
583
584 static void abort_recovery_queue(struct obd_device *obd)
585 {
586         struct ptlrpc_request *req;
587         struct list_head *tmp, *n;
588         int rc;
589
590         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
591                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
592                 DEBUG_REQ(D_ERROR, req, "aborted:");
593                 req->rq_status = -ENOTCONN;
594                 req->rq_type = PTL_RPC_MSG_ERR;
595                 rc = lustre_pack_reply(req, 0, NULL, NULL);
596                 if (rc == 0) {
597                         ptlrpc_reply(req);
598                 } else {
599                         DEBUG_REQ(D_ERROR, req,
600                                   "packing failed for abort-reply; skipping");
601                 }
602                 list_del(&req->rq_list);
603                 class_export_put(req->rq_export);
604                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
605                 OBD_FREE(req, sizeof *req);
606         }
607 }
608
609 void target_abort_recovery(void *data)
610 {
611         struct obd_device *obd = data;
612         int rc;
613
614         CERROR("disconnecting clients and aborting recovery\n");
615         spin_lock_bh(&obd->obd_processing_task_lock);
616         if (!obd->obd_recovering) {
617                 spin_unlock_bh(&obd->obd_processing_task_lock);
618                 EXIT;
619                 return;
620         }
621
622         obd->obd_recovering = obd->obd_abort_recovery = 0;
623
624         wake_up(&obd->obd_next_transno_waitq);
625         target_cancel_recovery_timer(obd);
626         spin_unlock_bh(&obd->obd_processing_task_lock);
627
628         class_disconnect_exports(obd, 0);
629
630         /* when recovery was aborted, cleanup orphans on mds and ost */
631         if (OBT(obd) && OBP(obd, postrecov)) {
632                 rc = OBP(obd, postrecov)(obd);
633                 if (rc >= 0)
634                         CWARN("Cleanup %d orphans after recovery was aborted\n", rc);
635                 else
636                         CERROR("postrecov failed %d\n", rc);
637         }
638
639         abort_delayed_replies(obd);
640         abort_recovery_queue(obd);
641         ptlrpc_run_recovery_over_upcall(obd);
642 }
643
644 static void target_recovery_expired(unsigned long castmeharder)
645 {
646         struct obd_device *obd = (struct obd_device *)castmeharder;
647         CERROR("recovery timed out, aborting\n");
648         spin_lock_bh(&obd->obd_processing_task_lock);
649         obd->obd_abort_recovery = 1;
650         wake_up(&obd->obd_next_transno_waitq);
651         spin_unlock_bh(&obd->obd_processing_task_lock);
652 }
653
654
655 /* obd_processing_task_lock should be held */
656 void target_cancel_recovery_timer(struct obd_device *obd)
657 {
658         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
659         del_timer(&obd->obd_recovery_timer);
660 }
661
662 static void reset_recovery_timer(struct obd_device *obd)
663 {
664         spin_lock_bh(&obd->obd_processing_task_lock);
665         if (!obd->obd_recovering) {
666                 spin_unlock_bh(&obd->obd_processing_task_lock);
667                 return;
668         }                
669         CDEBUG(D_HA, "timer will expire in %u seconds\n",
670                OBD_RECOVERY_TIMEOUT / HZ);
671         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
672         spin_unlock_bh(&obd->obd_processing_task_lock);
673 }
674
675
676 /* Only start it the first time called */
677 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
678 {
679         spin_lock_bh(&obd->obd_processing_task_lock);
680         if (obd->obd_recovery_handler) {
681                 spin_unlock_bh(&obd->obd_processing_task_lock);
682                 return;
683         }
684         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
685                OBD_RECOVERY_TIMEOUT / HZ);
686         obd->obd_recovery_handler = handler;
687         obd->obd_recovery_timer.function = target_recovery_expired;
688         obd->obd_recovery_timer.data = (unsigned long)obd;
689         init_timer(&obd->obd_recovery_timer);
690         spin_unlock_bh(&obd->obd_processing_task_lock);
691
692         reset_recovery_timer(obd);
693 }
694
695 static int check_for_next_transno(struct obd_device *obd)
696 {
697         struct ptlrpc_request *req;
698         int wake_up = 0, connected, completed, queue_len, max;
699         __u64 next_transno, req_transno;
700
701         spin_lock_bh(&obd->obd_processing_task_lock);
702         req = list_entry(obd->obd_recovery_queue.next,
703                          struct ptlrpc_request, rq_list);
704         max = obd->obd_max_recoverable_clients;
705         req_transno = req->rq_reqmsg->transno;
706         connected = obd->obd_connected_clients;
707         completed = max - obd->obd_recoverable_clients;
708         queue_len = obd->obd_requests_queued_for_recovery;
709         next_transno = obd->obd_next_recovery_transno;
710
711         if (obd->obd_abort_recovery) {
712                 CDEBUG(D_HA, "waking for aborted recovery\n");
713                 wake_up = 1;
714         } else if (!obd->obd_recovering) {
715                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
716                 wake_up = 1;
717         } else if (req_transno == next_transno) {
718                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
719                 wake_up = 1;
720         } else if (queue_len + completed == max) {
721                 CDEBUG(D_ERROR,
722                        "waking for skipped transno (skip: "LPD64
723                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
724                        next_transno, queue_len, completed, max, req_transno);
725                 obd->obd_next_recovery_transno = req_transno;
726                 wake_up = 1;
727         }
728         spin_unlock_bh(&obd->obd_processing_task_lock);
729         LASSERT(req->rq_reqmsg->transno >= next_transno);
730         return wake_up;
731 }
732
733 static void process_recovery_queue(struct obd_device *obd)
734 {
735         struct ptlrpc_request *req;
736         int abort_recovery = 0;
737         struct l_wait_info lwi = { 0 };
738         ENTRY;
739
740         for (;;) {
741                 spin_lock_bh(&obd->obd_processing_task_lock);
742                 LASSERT(obd->obd_processing_task == current->pid);
743                 req = list_entry(obd->obd_recovery_queue.next,
744                                  struct ptlrpc_request, rq_list);
745
746                 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
747                         spin_unlock_bh(&obd->obd_processing_task_lock);
748                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
749                                LPD64")\n",
750                                obd->obd_next_recovery_transno,
751                                req->rq_reqmsg->transno);
752                         l_wait_event(obd->obd_next_transno_waitq,
753                                      check_for_next_transno(obd), &lwi);
754                         spin_lock_bh(&obd->obd_processing_task_lock);
755                         abort_recovery = obd->obd_abort_recovery;
756                         spin_unlock_bh(&obd->obd_processing_task_lock);
757                         if (abort_recovery) {
758                                 target_abort_recovery(obd);
759                                 return;
760                         }
761                         continue;
762                 }
763                 list_del_init(&req->rq_list);
764                 obd->obd_requests_queued_for_recovery--;
765                 spin_unlock_bh(&obd->obd_processing_task_lock);
766
767                 DEBUG_REQ(D_HA, req, "processing: ");
768                 (void)obd->obd_recovery_handler(req);
769                 obd->obd_replayed_requests++;
770                 reset_recovery_timer(obd);
771                 /* bug 1580: decide how to properly sync() in recovery */
772                 //mds_fsync_super(mds->mds_sb);
773                 class_export_put(req->rq_export);
774                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
775                 OBD_FREE(req, sizeof *req);
776                 spin_lock_bh(&obd->obd_processing_task_lock);
777                 obd->obd_next_recovery_transno++;
778                 if (list_empty(&obd->obd_recovery_queue)) {
779                         obd->obd_processing_task = 0;
780                         spin_unlock_bh(&obd->obd_processing_task_lock);
781                         break;
782                 }
783                 spin_unlock_bh(&obd->obd_processing_task_lock);
784         }
785         EXIT;
786 }
787
788 int target_queue_recovery_request(struct ptlrpc_request *req,
789                                   struct obd_device *obd)
790 {
791         struct list_head *tmp;
792         int inserted = 0;
793         __u64 transno = req->rq_reqmsg->transno;
794         struct ptlrpc_request *saved_req;
795         struct lustre_msg *reqmsg;
796
797         /* CAVEAT EMPTOR: The incoming request message has been swabbed
798          * (i.e. buflens etc are in my own byte order), but type-dependent
799          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
800
801         if (!transno) {
802                 INIT_LIST_HEAD(&req->rq_list);
803                 DEBUG_REQ(D_HA, req, "not queueing");
804                 return 1;
805         }
806
807         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
808         /* XXX just like the request-dup code in queue_final_reply */
809         OBD_ALLOC(saved_req, sizeof *saved_req);
810         if (!saved_req)
811                 LBUG();
812         OBD_ALLOC(reqmsg, req->rq_reqlen);
813         if (!reqmsg)
814                 LBUG();
815
816         spin_lock_bh(&obd->obd_processing_task_lock);
817
818         /* If we're processing the queue, we want don't want to queue this
819          * message.
820          *
821          * Also, if this request has a transno less than the one we're waiting
822          * for, we should process it now.  It could (and currently always will)
823          * be an open request for a descriptor that was opened some time ago.
824          */
825         if (obd->obd_processing_task == current->pid ||
826             transno < obd->obd_next_recovery_transno) {
827                 /* Processing the queue right now, don't re-add. */
828                 LASSERT(list_empty(&req->rq_list));
829                 spin_unlock_bh(&obd->obd_processing_task_lock);
830                 OBD_FREE(reqmsg, req->rq_reqlen);
831                 OBD_FREE(saved_req, sizeof *saved_req);
832                 return 1;
833         }
834
835         memcpy(saved_req, req, sizeof *req);
836         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
837         req = saved_req;
838         req->rq_reqmsg = reqmsg;
839         class_export_get(req->rq_export);
840         INIT_LIST_HEAD(&req->rq_list);
841
842         /* XXX O(n^2) */
843         list_for_each(tmp, &obd->obd_recovery_queue) {
844                 struct ptlrpc_request *reqiter =
845                         list_entry(tmp, struct ptlrpc_request, rq_list);
846
847                 if (reqiter->rq_reqmsg->transno > transno) {
848                         list_add_tail(&req->rq_list, &reqiter->rq_list);
849                         inserted = 1;
850                         break;
851                 }
852         }
853
854         if (!inserted) {
855                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
856         }
857
858         obd->obd_requests_queued_for_recovery++;
859
860         if (obd->obd_processing_task != 0) {
861                 /* Someone else is processing this queue, we'll leave it to
862                  * them.
863                  */
864                 wake_up(&obd->obd_next_transno_waitq);
865                 spin_unlock_bh(&obd->obd_processing_task_lock);
866                 return 0;
867         }
868
869         /* Nobody is processing, and we know there's (at least) one to process
870          * now, so we'll do the honours.
871          */
872         obd->obd_processing_task = current->pid;
873         spin_unlock_bh(&obd->obd_processing_task_lock);
874
875         process_recovery_queue(obd);
876         return 0;
877 }
878
879 struct obd_device * target_req2obd(struct ptlrpc_request *req)
880 {
881         return req->rq_export->exp_obd;
882 }
883
884 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
885 {
886         struct obd_device *obd = target_req2obd(req);
887         struct ptlrpc_request *saved_req;
888         struct lustre_msg *reqmsg;
889         int recovery_done = 0;
890         int rc2;
891
892         if (rc) {
893                 /* Just like ptlrpc_error, but without the sending. */
894                 rc = lustre_pack_reply(req, 0, NULL, NULL);
895                 LASSERT(rc == 0); /* XXX handle this */
896                 req->rq_type = PTL_RPC_MSG_ERR;
897         }
898
899         LASSERT(list_empty(&req->rq_list));
900         /* XXX a bit like the request-dup code in queue_recovery_request */
901         OBD_ALLOC(saved_req, sizeof *saved_req);
902         if (!saved_req)
903                 LBUG();
904         OBD_ALLOC(reqmsg, req->rq_reqlen);
905         if (!reqmsg)
906                 LBUG();
907         memcpy(saved_req, req, sizeof *saved_req);
908         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
909         req = saved_req;
910         req->rq_reqmsg = reqmsg;
911         class_export_get(req->rq_export);
912         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
913
914         spin_lock_bh(&obd->obd_processing_task_lock);
915         --obd->obd_recoverable_clients;
916         recovery_done = (obd->obd_recoverable_clients == 0);
917         spin_unlock_bh(&obd->obd_processing_task_lock);
918
919         if (recovery_done) {
920                 struct list_head *tmp, *n;
921                 ldlm_reprocess_all_ns(req->rq_export->exp_obd->obd_namespace);
922                 CWARN("%s: all clients recovered, sending delayed replies\n",
923                        obd->obd_name);
924                 spin_lock_bh(&obd->obd_processing_task_lock);
925                 obd->obd_recovering = 0;
926                 target_cancel_recovery_timer(obd);
927                 spin_unlock_bh(&obd->obd_processing_task_lock);
928
929                 /* when recovery finished, cleanup orphans on mds and ost */
930                 if (OBT(obd) && OBP(obd, postrecov)) {
931                         rc2 = OBP(obd, postrecov)(obd);
932                         if (rc2 >= 0)
933                                 CWARN("%s: all clients recovered, %d MDS "
934                                       "orphans deleted\n", obd->obd_name, rc2);
935                         else
936                                 CERROR("postrecov failed %d\n", rc2);
937                 }
938
939                 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
940                         req = list_entry(tmp, struct ptlrpc_request, rq_list);
941                         DEBUG_REQ(D_ERROR, req, "delayed:");
942                         ptlrpc_reply(req);
943                         class_export_put(req->rq_export);
944                         list_del(&req->rq_list);
945                         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
946                         OBD_FREE(req, sizeof *req);
947                 }
948                 ptlrpc_run_recovery_over_upcall(obd);
949         } else {
950                 CWARN("%s: %d recoverable clients remain\n",
951                        obd->obd_name, obd->obd_recoverable_clients);
952                 wake_up(&obd->obd_next_transno_waitq);
953         }
954
955         return 1;
956 }
957
958 static void ptlrpc_abort_reply (struct ptlrpc_request *req)
959 {
960         /* On return, we must be sure that the ACK callback has either
961          * happened or will not happen.  Note that the SENT callback will
962          * happen come what may since we successfully posted the PUT. */
963         int rc;
964         struct l_wait_info lwi;
965         unsigned long flags;
966
967  again:
968         /* serialise with ACK callback */
969         spin_lock_irqsave (&req->rq_lock, flags);
970         if (!req->rq_want_ack) {
971                 spin_unlock_irqrestore (&req->rq_lock, flags);
972                 /* The ACK callback has happened already.  Although the
973                  * SENT callback might still be outstanding (yes really) we
974                  * don't care; this is just like normal completion. */
975                 return;
976         }
977         spin_unlock_irqrestore (&req->rq_lock, flags);
978
979         /* Have a bash at unlinking the MD.  This will fail until the SENT
980          * callback has happened since the MD is busy from the PUT.  If the
981          * ACK still hasn't arrived after then, a successful unlink will
982          * ensure the ACK callback never happens. */
983         rc = PtlMDUnlink (req->rq_reply_md_h);
984         switch (rc) {
985         default:
986                 LBUG ();
987         case PTL_OK:
988                 /* SENT callback happened; ACK callback preempted */
989                 LASSERT (req->rq_want_ack);
990                 spin_lock_irqsave (&req->rq_lock, flags);
991                 req->rq_want_ack = 0;
992                 spin_unlock_irqrestore (&req->rq_lock, flags);
993                 return;
994         case PTL_INV_MD:
995                 return;
996         case PTL_MD_INUSE:
997                 /* Still sending or ACK callback in progress: wait until
998                  * either callback has completed and try again.
999                  * Actually we can't wait for the SENT callback because
1000                  * there's no state the SENT callback can touch that will
1001                  * allow it to communicate with us!  So we just wait here
1002                  * for a short time, effectively polling for the SENT
1003                  * callback by calling PtlMDUnlink() again, to see if it
1004                  * has finished.  Note that if the ACK does arrive, its
1005                  * callback wakes us in short order. --eeb */
1006                 lwi = LWI_TIMEOUT (HZ/4, NULL, NULL);
1007                 rc = l_wait_event(req->rq_reply_waitq, !req->rq_want_ack,
1008                                   &lwi);
1009                 CDEBUG (D_HA, "Retrying req %p: %d\n", req, rc);
1010                 /* NB go back and test rq_want_ack with locking, to ensure
1011                  * if ACK callback happened, it has completed stopped
1012                  * referencing this req. */
1013                 goto again;
1014         }
1015 }
1016
1017 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1018 {
1019         int i;
1020         int netrc;
1021         unsigned long flags;
1022         struct ptlrpc_req_ack_lock *ack_lock;
1023         struct l_wait_info lwi = { 0 };
1024         wait_queue_t commit_wait;
1025         struct obd_device *obd =
1026                 req->rq_export ? req->rq_export->exp_obd : NULL;
1027         struct obd_export *exp = NULL;
1028
1029         if (req->rq_export) {
1030                 for (i = 0; i < REQ_MAX_ACK_LOCKS; i++) {
1031                         if (req->rq_ack_locks[i].mode) {
1032                                 exp = req->rq_export;
1033                                 break;
1034                         }
1035                 }
1036         }
1037
1038         if (exp) {
1039                 exp->exp_outstanding_reply = req;
1040                 spin_lock_irqsave (&req->rq_lock, flags);
1041                 req->rq_want_ack = 1;
1042                 spin_unlock_irqrestore (&req->rq_lock, flags);
1043         }
1044
1045         if (!OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1046                 if (rc == 0) {
1047                         DEBUG_REQ(D_NET, req, "sending reply");
1048                         netrc = ptlrpc_reply(req);
1049                 } else if (rc == -ENOTCONN) {
1050                         DEBUG_REQ(D_HA, req, "processing error (%d)", rc);
1051                         netrc = ptlrpc_error(req);
1052                 } else {
1053                         DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1054                         netrc = ptlrpc_error(req);
1055                 }
1056         } else {
1057                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1058                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1059                 if (req->rq_repmsg) {
1060                         OBD_FREE(req->rq_repmsg, req->rq_replen);
1061                         req->rq_repmsg = NULL;
1062                 }
1063                 init_waitqueue_head(&req->rq_reply_waitq);
1064                 netrc = 0;
1065         }
1066
1067         /* a failed send simulates the callbacks */
1068         LASSERT(netrc == 0 || req->rq_want_ack == 0);
1069         if (exp == NULL) {
1070                 LASSERT(req->rq_want_ack == 0);
1071                 return;
1072         }
1073         LASSERT(obd != NULL);
1074
1075         init_waitqueue_entry(&commit_wait, current);
1076         add_wait_queue(&obd->obd_commit_waitq, &commit_wait);
1077         rc = l_wait_event(req->rq_reply_waitq,
1078                           !req->rq_want_ack || req->rq_resent ||
1079                           req->rq_transno <= obd->obd_last_committed, &lwi);
1080         remove_wait_queue(&obd->obd_commit_waitq, &commit_wait);
1081
1082         spin_lock_irqsave (&req->rq_lock, flags);
1083         /* If we got here because the ACK callback ran, this acts as a
1084          * barrier to ensure the callback completed the wakeup. */
1085         spin_unlock_irqrestore (&req->rq_lock, flags);
1086
1087         /* If we committed the transno already, then we might wake up before
1088          * the ack arrives.  We need to stop waiting for the ack before we can
1089          * reuse this request structure.  We are guaranteed by this point that
1090          * this cannot abort the sending of the actual reply.*/
1091         ptlrpc_abort_reply(req);
1092
1093         if (req->rq_resent) {
1094                 DEBUG_REQ(D_HA, req, "resent: not cancelling locks");
1095                 return;
1096         }
1097
1098         LASSERT(rc == 0);
1099         DEBUG_REQ(D_HA, req, "cancelling locks for %s",
1100                   req->rq_want_ack ? "commit" : "ack");
1101
1102         exp->exp_outstanding_reply = NULL;
1103
1104         for (ack_lock = req->rq_ack_locks, i = 0;
1105              i < REQ_MAX_ACK_LOCKS; i++, ack_lock++) {
1106                 if (!ack_lock->mode)
1107                         continue;
1108                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1109         }
1110 }
1111
1112 int target_handle_ping(struct ptlrpc_request *req)
1113 {
1114         return lustre_pack_reply(req, 0, NULL, NULL);
1115 }
1116
1117 void *ldlm_put_lock_into_req(struct ptlrpc_request *req,
1118                                 struct lustre_handle *lock, int mode)
1119 {
1120         int i;
1121
1122         for (i = 0; i < REQ_MAX_ACK_LOCKS; i++) {
1123                 if (req->rq_ack_locks[i].mode)
1124                         continue;
1125                 CDEBUG(D_HA, "saving lock "LPX64" in req %p ack_lock[%d]\n",
1126                        lock->cookie, req, i);
1127                 memcpy(&req->rq_ack_locks[i].lock, lock, sizeof(*lock));
1128                 req->rq_ack_locks[i].mode = mode;
1129                 return &req->rq_ack_locks[i];
1130         }
1131         CERROR("no space for lock in struct ptlrpc_request\n");
1132         LBUG();
1133         return NULL;
1134 }