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