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