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