Whamcloud - gitweb
b=11973
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_LDLM
29
30 #ifdef __KERNEL__
31 # include <libcfs/libcfs.h>
32 #else
33 # include <liblustre.h>
34 #endif
35 #include <obd.h>
36 #include <lustre_mds.h>
37 #include <lustre_dlm.h>
38 #include <lustre_net.h>
39
40 /* @priority: if non-zero, move the selected to the list head
41  * @create: if zero, only search in existed connections
42  */
43 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
44                            int priority, int create)
45 {
46         struct ptlrpc_connection *ptlrpc_conn;
47         struct obd_import_conn *imp_conn = NULL, *item;
48         int rc = 0;
49         ENTRY;
50
51         if (!create && !priority) {
52                 CDEBUG(D_HA, "Nothing to do\n");
53                 RETURN(-EINVAL);
54         }
55
56         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
57         if (!ptlrpc_conn) {
58                 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
59                 RETURN (-ENOENT);
60         }
61
62         if (create) {
63                 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
64                 if (!imp_conn) {
65                         GOTO(out_put, rc = -ENOMEM);
66                 }
67         }
68
69         spin_lock(&imp->imp_lock);
70         list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
71                 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
72                         if (priority) {
73                                 list_del(&item->oic_item);
74                                 list_add(&item->oic_item, &imp->imp_conn_list);
75                                 item->oic_last_attempt = 0;
76                         }
77                         CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
78                                imp, imp->imp_obd->obd_name, uuid->uuid,
79                                (priority ? ", moved to head" : ""));
80                         spin_unlock(&imp->imp_lock);
81                         GOTO(out_free, rc = 0);
82                 }
83         }
84         /* not found */
85         if (create) {
86                 imp_conn->oic_conn = ptlrpc_conn;
87                 imp_conn->oic_uuid = *uuid;
88                 item->oic_last_attempt = 0;
89                 if (priority)
90                         list_add(&imp_conn->oic_item, &imp->imp_conn_list);
91                 else
92                         list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
93                 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
94                        imp, imp->imp_obd->obd_name, uuid->uuid,
95                        (priority ? "head" : "tail"));
96         } else {
97                 spin_unlock(&imp->imp_lock);
98                 GOTO(out_free, rc = -ENOENT);
99
100         }
101
102         spin_unlock(&imp->imp_lock);
103         RETURN(0);
104 out_free:
105         if (imp_conn)
106                 OBD_FREE(imp_conn, sizeof(*imp_conn));
107 out_put:
108         ptlrpc_put_connection(ptlrpc_conn);
109         RETURN(rc);
110 }
111
112 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
113 {
114         return import_set_conn(imp, uuid, 1, 0);
115 }
116
117 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
118                            int priority)
119 {
120         return import_set_conn(imp, uuid, priority, 1);
121 }
122
123 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
124 {
125         struct obd_import_conn *imp_conn;
126         struct obd_export *dlmexp;
127         int rc = -ENOENT;
128         ENTRY;
129
130         spin_lock(&imp->imp_lock);
131         if (list_empty(&imp->imp_conn_list)) {
132                 LASSERT(!imp->imp_connection);
133                 GOTO(out, rc);
134         }
135
136         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
137                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
138                         continue;
139                 LASSERT(imp_conn->oic_conn);
140
141                 /* is current conn? */
142                 if (imp_conn == imp->imp_conn_current) {
143                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
144
145                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
146                             imp->imp_state != LUSTRE_IMP_DISCON) {
147                                 CERROR("can't remove current connection\n");
148                                 GOTO(out, rc = -EBUSY);
149                         }
150
151                         ptlrpc_put_connection(imp->imp_connection);
152                         imp->imp_connection = NULL;
153
154                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
155                         if (dlmexp && dlmexp->exp_connection) {
156                                 LASSERT(dlmexp->exp_connection ==
157                                         imp_conn->oic_conn);
158                                 ptlrpc_put_connection(dlmexp->exp_connection);
159                                 dlmexp->exp_connection = NULL;
160                         }
161                 }
162
163                 list_del(&imp_conn->oic_item);
164                 ptlrpc_put_connection(imp_conn->oic_conn);
165                 OBD_FREE(imp_conn, sizeof(*imp_conn));
166                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
167                        imp, imp->imp_obd->obd_name, uuid->uuid);
168                 rc = 0;
169                 break;
170         }
171 out:
172         spin_unlock(&imp->imp_lock);
173         if (rc == -ENOENT)
174                 CERROR("connection %s not found\n", uuid->uuid);
175         RETURN(rc);
176 }
177
178 /* configure an RPC client OBD device
179  *
180  * lcfg parameters:
181  * 1 - client UUID
182  * 2 - server UUID
183  * 3 - inactive-on-startup
184  */
185 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
186 {
187         struct lustre_cfg* lcfg = buf;
188         struct client_obd *cli = &obddev->u.cli;
189         struct obd_import *imp;
190         struct obd_uuid server_uuid;
191         int rq_portal, rp_portal, connect_op;
192         char *name = obddev->obd_type->typ_name;
193         int rc;
194         ENTRY;
195
196         /* In a more perfect world, we would hang a ptlrpc_client off of
197          * obd_type and just use the values from there. */
198         if (!strcmp(name, LUSTRE_OSC_NAME)) {
199 #ifdef __KERNEL__
200                 /* Can be removed in Lustre 1.8, for compatibility only */
201                 rq_portal = OST_IO_PORTAL;
202 #else
203                 rq_portal = OST_REQUEST_PORTAL;
204 #endif
205                 rp_portal = OSC_REPLY_PORTAL;
206                 connect_op = OST_CONNECT;
207         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
208                 rq_portal = MDS_REQUEST_PORTAL;
209                 rp_portal = MDC_REPLY_PORTAL;
210                 connect_op = MDS_CONNECT;
211         } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
212                 rq_portal = MGS_REQUEST_PORTAL;
213                 rp_portal = MGC_REPLY_PORTAL;
214                 connect_op = MGS_CONNECT;
215         } else {
216                 CERROR("unknown client OBD type \"%s\", can't setup\n",
217                        name);
218                 RETURN(-EINVAL);
219         }
220
221         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
222                 CERROR("requires a TARGET UUID\n");
223                 RETURN(-EINVAL);
224         }
225
226         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
227                 CERROR("client UUID must be less than 38 characters\n");
228                 RETURN(-EINVAL);
229         }
230
231         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
232                 CERROR("setup requires a SERVER UUID\n");
233                 RETURN(-EINVAL);
234         }
235
236         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
237                 CERROR("target UUID must be less than 38 characters\n");
238                 RETURN(-EINVAL);
239         }
240
241         sema_init(&cli->cl_sem, 1);
242         sema_init(&cli->cl_mgc_sem, 1);
243         cli->cl_conn_count = 0;
244         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
245                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
246                      sizeof(server_uuid)));
247
248         cli->cl_dirty = 0;
249         cli->cl_avail_grant = 0;
250         /* FIXME: should limit this for the sum of all cl_dirty_max */
251         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
252         if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > num_physpages / 8)
253                 cli->cl_dirty_max = num_physpages << (CFS_PAGE_SHIFT - 3);
254         CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
255         CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
256         CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
257         CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
258         client_obd_list_lock_init(&cli->cl_loi_list_lock);
259         cli->cl_r_in_flight = 0;
260         cli->cl_w_in_flight = 0;
261         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
262         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
263         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
264         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
265         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
266         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
267
268         /* This value may be changed at connect time in
269            ptlrpc_connect_interpret. */
270         cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
271                                         (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
272
273         if (!strcmp(name, LUSTRE_MDC_NAME)) {
274                 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
275         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
276                 cli->cl_max_rpcs_in_flight = 2;
277         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
278                 cli->cl_max_rpcs_in_flight = 3;
279         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
280                 cli->cl_max_rpcs_in_flight = 4;
281         } else {
282                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
283         }
284         rc = ldlm_get_ref();
285         if (rc) {
286                 CERROR("ldlm_get_ref failed: %d\n", rc);
287                 GOTO(err, rc);
288         }
289
290         ptlrpc_init_client(rq_portal, rp_portal, name,
291                            &obddev->obd_ldlm_client);
292
293         imp = class_new_import(obddev);
294         if (imp == NULL)
295                 GOTO(err_ldlm, rc = -ENOENT);
296         imp->imp_client = &obddev->obd_ldlm_client;
297         imp->imp_connect_op = connect_op;
298         imp->imp_initial_recov = 1;
299         imp->imp_initial_recov_bk = 0;
300         CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
301         memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
302                LUSTRE_CFG_BUFLEN(lcfg, 1));
303         class_import_put(imp);
304
305         rc = client_import_add_conn(imp, &server_uuid, 1);
306         if (rc) {
307                 CERROR("can't add initial connection\n");
308                 GOTO(err_import, rc);
309         }
310
311         cli->cl_import = imp;
312         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
313         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
314         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
315
316         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
317                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
318                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
319                                name, obddev->obd_name,
320                                cli->cl_target_uuid.uuid);
321                         imp->imp_invalid = 1;
322                 }
323         }
324
325         cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
326
327         RETURN(rc);
328
329 err_import:
330         class_destroy_import(imp);
331 err_ldlm:
332         ldlm_put_ref(0);
333 err:
334         RETURN(rc);
335
336 }
337
338 int client_obd_cleanup(struct obd_device *obddev)
339 {
340         ENTRY;
341         ldlm_put_ref(obddev->obd_force);
342
343         RETURN(0);
344 }
345
346 /* ->o_connect() method for client side (OSC and MDC and MGC) */
347 int client_connect_import(struct lustre_handle *dlm_handle,
348                           struct obd_device *obd, struct obd_uuid *cluuid,
349                           struct obd_connect_data *data)
350 {
351         struct client_obd *cli = &obd->u.cli;
352         struct obd_import *imp = cli->cl_import;
353         struct obd_export *exp;
354         struct obd_connect_data *ocd;
355         int rc;
356         ENTRY;
357
358         mutex_down(&cli->cl_sem);
359         rc = class_connect(dlm_handle, obd, cluuid);
360         if (rc)
361                 GOTO(out_sem, rc);
362
363         cli->cl_conn_count++;
364         if (cli->cl_conn_count > 1)
365                 GOTO(out_sem, rc);
366         exp = class_conn2export(dlm_handle);
367
368         if (obd->obd_namespace != NULL)
369                 CERROR("already have namespace!\n");
370         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
371                                                 LDLM_NAMESPACE_CLIENT);
372         if (obd->obd_namespace == NULL)
373                 GOTO(out_disco, rc = -ENOMEM);
374
375         imp->imp_dlm_handle = *dlm_handle;
376         rc = ptlrpc_init_import(imp);
377         if (rc != 0)
378                 GOTO(out_ldlm, rc);
379
380         ocd = &imp->imp_connect_data;
381         if (data) {
382                 *ocd = *data;
383                 imp->imp_connect_flags_orig = data->ocd_connect_flags;
384         }
385
386         rc = ptlrpc_connect_import(imp, NULL);
387         if (rc != 0) {
388                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
389                 GOTO(out_ldlm, rc);
390         }
391         LASSERT(exp->exp_connection);
392
393         if (data) {
394                 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
395                         ocd->ocd_connect_flags);
396                 data->ocd_connect_flags = ocd->ocd_connect_flags;
397         }
398
399         ptlrpc_pinger_add_import(imp);
400         EXIT;
401
402         if (rc) {
403 out_ldlm:
404                 ldlm_namespace_free(obd->obd_namespace, 0);
405                 obd->obd_namespace = NULL;
406 out_disco:
407                 cli->cl_conn_count--;
408                 class_disconnect(exp);
409         } else {
410                 class_export_put(exp);
411         }
412 out_sem:
413         mutex_up(&cli->cl_sem);
414         return rc;
415 }
416
417 int client_disconnect_export(struct obd_export *exp)
418 {
419         struct obd_device *obd = class_exp2obd(exp);
420         struct client_obd *cli;
421         struct obd_import *imp;
422         int rc = 0, err;
423         ENTRY;
424
425         if (!obd) {
426                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
427                        exp, exp ? exp->exp_handle.h_cookie : -1);
428                 RETURN(-EINVAL);
429         }
430
431         cli = &obd->u.cli;
432         imp = cli->cl_import;
433
434         mutex_down(&cli->cl_sem);
435         if (!cli->cl_conn_count) {
436                 CERROR("disconnecting disconnected device (%s)\n",
437                        obd->obd_name);
438                 GOTO(out_sem, rc = -EINVAL);
439         }
440
441         cli->cl_conn_count--;
442         if (cli->cl_conn_count)
443                 GOTO(out_no_disconnect, rc = 0);
444
445         /* Mark import deactivated now, so we don't try to reconnect if any
446          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
447          * fully deactivate the import, or that would drop all requests. */
448         imp->imp_deactive = 1;
449
450         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
451          * delete it regardless.  (It's safe to delete an import that was
452          * never added.) */
453         (void)ptlrpc_pinger_del_import(imp);
454
455         if (obd->obd_namespace != NULL) {
456                 /* obd_no_recov == local only */
457                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
458                                        obd->obd_no_recov ? LDLM_FL_LOCAL_ONLY:0,
459                                        NULL);
460                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
461                 obd->obd_namespace = NULL;
462         }
463
464         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
465         if (!obd->obd_no_recov)
466                 rc = ptlrpc_disconnect_import(imp, 0);
467
468         ptlrpc_invalidate_import(imp);
469         ptlrpc_free_rq_pool(imp->imp_rq_pool);
470         class_destroy_import(imp);
471         cli->cl_import = NULL;
472
473         EXIT;
474  out_no_disconnect:
475         err = class_disconnect(exp);
476         if (!rc && err)
477                 rc = err;
478  out_sem:
479         mutex_up(&cli->cl_sem);
480         RETURN(rc);
481 }
482
483 /* --------------------------------------------------------------------------
484  * from old lib/target.c
485  * -------------------------------------------------------------------------- */
486
487 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
488                             struct obd_uuid *cluuid)
489 {
490         ENTRY;
491         if (exp->exp_connection && exp->exp_imp_reverse) {
492                 struct lustre_handle *hdl;
493                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
494                 /* Might be a re-connect after a partition. */
495                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
496                         CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
497                               cluuid->uuid);
498                         conn->cookie = exp->exp_handle.h_cookie;
499                         /* target_handle_connect() treats EALREADY and
500                          * -EALREADY differently.  EALREADY means we are
501                          * doing a valid reconnect from the same client. */
502                         RETURN(EALREADY);
503                 } else {
504                         CERROR("%s reconnecting from %s, "
505                                "handle mismatch (ours "LPX64", theirs "
506                                LPX64")\n", cluuid->uuid,
507                                exp->exp_connection->c_remote_uuid.uuid,
508                                hdl->cookie, conn->cookie);
509                         memset(conn, 0, sizeof *conn);
510                         /* target_handle_connect() treats EALREADY and
511                          * -EALREADY differently.  -EALREADY is an error
512                          * (same UUID, different handle). */
513                         RETURN(-EALREADY);
514                 }
515         }
516
517         conn->cookie = exp->exp_handle.h_cookie;
518         CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
519                cluuid->uuid, exp, conn->cookie);
520         RETURN(0);
521 }
522
523 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
524                           int error)
525 {
526         struct obd_export *exp = cb_data;
527
528         CDEBUG(D_HA, "%s: committing for initial connect of %s\n",
529                obd->obd_name, exp->exp_client_uuid.uuid);
530         exp->exp_need_sync = 0;
531 }
532 EXPORT_SYMBOL(target_client_add_cb);
533
534 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
535 {
536         struct obd_device *target, *targref = NULL;
537         struct obd_export *export = NULL;
538         struct obd_import *revimp;
539         struct lustre_handle conn;
540         struct obd_uuid tgtuuid;
541         struct obd_uuid cluuid;
542         struct obd_uuid remote_uuid;
543         char *str, *tmp;
544         int rc = 0, abort_recovery;
545         struct obd_connect_data *data;
546         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
547         ENTRY;
548
549         OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
550
551         LASSERT_REQSWAB(req, REQ_REC_OFF);
552         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
553         if (str == NULL) {
554                 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect\n");
555                 GOTO(out, rc = -EINVAL);
556         }
557
558         obd_str2uuid (&tgtuuid, str);
559         target = class_uuid2obd(&tgtuuid);
560         /* COMPAT_146 */
561         /* old (pre 1.6) lustre_process_log tries to connect to mdsname
562            (eg. mdsA) instead of uuid. */
563         if (!target) {
564                 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
565                 target = class_uuid2obd(&tgtuuid);
566         }
567         if (!target)
568                 target = class_name2obd(str);
569         /* end COMPAT_146 */
570
571         if (!target || target->obd_stopping || !target->obd_set_up) {
572                 LCONSOLE_ERROR("UUID '%s' is not available "
573                                " for connect (%s)\n", str,
574                                !target ? "no target" :
575                                (target->obd_stopping ? "stopping" :
576                                 "not set up"));
577                 GOTO(out, rc = -ENODEV);
578         }
579
580         if (target->obd_no_conn) {
581                 LCONSOLE_WARN("%s: temporarily refusing client connection "
582                               "from %s\n", target->obd_name, 
583                               libcfs_nid2str(req->rq_peer.nid));
584                 GOTO(out, rc = -EAGAIN);
585         }
586
587         /* Make sure the target isn't cleaned up while we're here. Yes, 
588            there's still a race between the above check and our incref here. 
589            Really, class_uuid2obd should take the ref. */
590         targref = class_incref(target);
591
592         LASSERT_REQSWAB(req, REQ_REC_OFF + 1);
593         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
594                                 sizeof(cluuid) - 1);
595         if (str == NULL) {
596                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect\n");
597                 GOTO(out, rc = -EINVAL);
598         }
599
600         obd_str2uuid (&cluuid, str);
601
602         /* XXX extract a nettype and format accordingly */
603         switch (sizeof(lnet_nid_t)) {
604                 /* NB the casts only avoid compiler warnings */
605         case 8:
606                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
607                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
608                 break;
609         case 4:
610                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
611                          "NET_%x_UUID", (__u32)req->rq_peer.nid);
612                 break;
613         default:
614                 LBUG();
615         }
616
617         spin_lock_bh(&target->obd_processing_task_lock);
618         abort_recovery = target->obd_abort_recovery;
619         spin_unlock_bh(&target->obd_processing_task_lock);
620         if (abort_recovery)
621                 target_abort_recovery(target);
622
623         tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
624         if (tmp == NULL)
625                 GOTO(out, rc = -EPROTO);
626
627         memcpy(&conn, tmp, sizeof conn);
628
629         data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
630                                   lustre_swab_connect);
631         rc = lustre_pack_reply(req, 2, size, NULL);
632         if (rc)
633                 GOTO(out, rc);
634
635         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
636                 if (!data) {
637                         DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
638                                   "libclient connection attempt\n");
639                         GOTO(out, rc = -EPROTO);
640                 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
641                                                LUSTRE_VERSION_ALLOWED_OFFSET ||
642                            data->ocd_version > LUSTRE_VERSION_CODE +
643                                                LUSTRE_VERSION_ALLOWED_OFFSET) {
644                         DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
645                                   "libclient connection attempt\n",
646                                   data->ocd_version < LUSTRE_VERSION_CODE ?
647                                   "old" : "new",
648                                   OBD_OCD_VERSION_MAJOR(data->ocd_version),
649                                   OBD_OCD_VERSION_MINOR(data->ocd_version),
650                                   OBD_OCD_VERSION_PATCH(data->ocd_version),
651                                   OBD_OCD_VERSION_FIX(data->ocd_version));
652                         data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
653                                               offsetof(typeof(*data),
654                                                        ocd_version) +
655                                               sizeof(data->ocd_version));
656                         if (data) {
657                                 data->ocd_connect_flags = OBD_CONNECT_VERSION;
658                                 data->ocd_version = LUSTRE_VERSION_CODE;
659                         }
660                         GOTO(out, rc = -EPROTO);
661                 }
662         }
663
664         /* lctl gets a backstage, all-access pass. */
665         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
666                 goto dont_check_exports;
667
668         spin_lock(&target->obd_dev_lock);
669         export = lustre_hash_get_object_by_key(target->obd_uuid_hash_body, &cluuid);
670
671         if (export != NULL && export->exp_connecting) { /* bug 9635, et. al. */
672                 CWARN("%s: exp %p already connecting\n",
673                       export->exp_obd->obd_name, export);
674                 class_export_put(export);
675                 export = NULL;
676                 rc = -EALREADY;
677         } else if (export != NULL) {
678                 /* make darn sure this is coming from the same peer
679                  * if the UUIDs matched */
680               if ((export->exp_connection != NULL) &&
681                   (strcmp(libcfs_nid2str(req->rq_peer.nid),
682                           libcfs_nid2str(export->exp_connection->c_peer.nid)))) {
683                           CWARN("%s: cookie %s seen on new NID %s when "
684                                   "existing NID %s is already connected\n",
685                           target->obd_name, cluuid.uuid,
686                           libcfs_nid2str(req->rq_peer.nid),
687                           libcfs_nid2str(export->exp_connection->c_peer.nid));
688                           class_export_put(export);
689                           export = NULL;
690                           rc = -EALREADY;
691                 } else {
692                                 export->exp_connecting = 1;
693                                 class_export_put(export);
694                                 spin_unlock(&target->obd_dev_lock);
695                                 LASSERT(export->exp_obd == target);
696                                 rc = target_handle_reconnect(&conn, export, &cluuid);
697                 } 
698         } 
699
700         /* If we found an export, we already unlocked. */
701         if (!export) {
702                 spin_unlock(&target->obd_dev_lock);
703                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
704         } else if (req->rq_export == NULL &&
705                    atomic_read(&export->exp_rpc_count) > 0) {
706                 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
707                       target->obd_name, cluuid.uuid,
708                       libcfs_nid2str(req->rq_peer.nid),
709                       export, atomic_read(&export->exp_refcount));
710                 GOTO(out, rc = -EBUSY);
711         } else if (req->rq_export != NULL &&
712                    atomic_read(&export->exp_rpc_count) > 1) {
713                 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
714                       target->obd_name, cluuid.uuid,
715                       libcfs_nid2str(req->rq_peer.nid),
716                       export, atomic_read(&export->exp_rpc_count));
717                 GOTO(out, rc = -EBUSY);
718         } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
719                 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
720                        "cookies not random?\n", target->obd_name,
721                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
722                 GOTO(out, rc = -EALREADY);
723         } else {
724                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
725         }
726
727         /* We want to handle EALREADY but *not* -EALREADY from
728          * target_handle_reconnect(), return reconnection state in a flag */
729         if (rc == EALREADY) {
730                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
731                 rc = 0;
732         } else if (rc) {
733                 GOTO(out, rc);
734         }
735
736         /* Tell the client if we're in recovery. */
737         /* If this is the first client, start the recovery timer */
738         if (target->obd_recovering) {
739                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
740                 target_start_recovery_timer(target, handler);
741         }
742
743         /* Tell the client if we support replayable requests */
744         if (target->obd_replayable)
745                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
746
747         if (export == NULL) {
748                 if (target->obd_recovering) {
749                         CERROR("%s: denying connection for new client %s (%s): "
750                                "%d clients in recovery for %lds\n",
751                                target->obd_name,
752                                libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
753                                target->obd_recoverable_clients,
754                                cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
755                                                              cfs_time_current())));
756                         rc = -EBUSY;
757                 } else {
758  dont_check_exports:
759                         rc = obd_connect(&conn, target, &cluuid, data);
760                 }
761         } else {
762                 rc = obd_reconnect(export, target, &cluuid, data);
763         }
764
765         if (rc)
766                 GOTO(out, rc);
767
768         /* Return only the parts of obd_connect_data that we understand, so the
769          * client knows that we don't understand the rest. */
770         if (data)
771                 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
772                                       sizeof(*data)),
773                        data, sizeof(*data));
774
775         /* If all else goes well, this is our RPC return code. */
776         req->rq_status = 0;
777
778         lustre_msg_set_handle(req->rq_repmsg, &conn);
779
780         /* ownership of this export ref transfers to the request AFTER we
781          * drop any previous reference the request had, but we don't want
782          * that to go to zero before we get our new export reference. */
783         export = class_conn2export(&conn);
784         if (!export) {
785                 DEBUG_REQ(D_ERROR, req, "Missing export!\n");
786                 GOTO(out, rc = -ENODEV);
787         }
788
789         /* If the client and the server are the same node, we will already
790          * have an export that really points to the client's DLM export,
791          * because we have a shared handles table.
792          *
793          * XXX this will go away when shaver stops sending the "connect" handle
794          * in the real "remote handle" field of the request --phik 24 Apr 2003
795          */
796         if (req->rq_export != NULL)
797                 class_export_put(req->rq_export);
798
799         req->rq_export = export;
800
801         spin_lock(&export->exp_lock);
802         if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
803                 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
804                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
805                        export->exp_conn_cnt,
806                        lustre_msg_get_conn_cnt(req->rq_reqmsg));
807                        
808                 spin_unlock(&export->exp_lock);
809                 GOTO(out, rc = -EALREADY);
810         }
811         export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
812         spin_unlock(&export->exp_lock);
813
814         /* request from liblustre?  Don't evict it for not pinging. */
815         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
816                 export->exp_libclient = 1;
817                 spin_lock(&target->obd_dev_lock);
818                 list_del_init(&export->exp_obd_chain_timed);
819                 spin_unlock(&target->obd_dev_lock);
820         }
821
822         if (export->exp_connection != NULL)
823                 ptlrpc_put_connection(export->exp_connection);
824         export->exp_connection = ptlrpc_get_connection(req->rq_peer,
825                                                        req->rq_self,
826                                                        &remote_uuid);
827
828         spin_lock(&target->obd_dev_lock);
829         lustre_hash_additem(export->exp_obd->obd_nid_hash_body, 
830                             &export->exp_connection->c_peer.nid, 
831                             &export->exp_nid_hash);
832         spin_unlock(&target->obd_dev_lock);
833
834         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT)
835                 GOTO(out, rc = 0);
836
837         if (target->obd_recovering)
838                 target->obd_connected_clients++;
839
840         memcpy(&conn,
841                lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
842                sizeof conn);
843
844         if (export->exp_imp_reverse != NULL)
845                 class_destroy_import(export->exp_imp_reverse);
846         revimp = export->exp_imp_reverse = class_new_import(target);
847         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
848         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
849         revimp->imp_remote_handle = conn;
850         revimp->imp_dlm_fake = 1;
851         revimp->imp_state = LUSTRE_IMP_FULL;
852
853         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
854                 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
855                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
856         }
857
858         class_import_put(revimp);
859 out:
860         if (export)
861                 export->exp_connecting = 0;
862         if (targref) 
863                 class_decref(targref);
864         if (rc)
865                 req->rq_status = rc;
866         RETURN(rc);
867 }
868
869 int target_handle_disconnect(struct ptlrpc_request *req)
870 {
871         int rc;
872         ENTRY;
873
874         rc = lustre_pack_reply(req, 1, NULL, NULL);
875         if (rc)
876                 RETURN(rc);
877
878         /* keep the rq_export around so we can send the reply */
879         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
880         RETURN(0);
881 }
882
883 void target_destroy_export(struct obd_export *exp)
884 {
885         /* exports created from last_rcvd data, and "fake"
886            exports created by lctl don't have an import */
887         if (exp->exp_imp_reverse != NULL)
888                 class_destroy_import(exp->exp_imp_reverse);
889
890         /* We cancel locks at disconnect time, but this will catch any locks
891          * granted in a race with recovery-induced disconnect. */
892         if (exp->exp_obd->obd_namespace != NULL)
893                 ldlm_cancel_locks_for_export(exp);
894 }
895
896 /*
897  * Recovery functions
898  */
899
900
901 static void target_release_saved_req(struct ptlrpc_request *req)
902 {
903         if (req->rq_reply_state != NULL) {
904                 ptlrpc_rs_decref(req->rq_reply_state);
905                 /* req->rq_reply_state = NULL; */
906         }
907
908         class_export_put(req->rq_export);
909         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
910         OBD_FREE(req, sizeof *req);
911 }
912
913 static void target_finish_recovery(struct obd_device *obd)
914 {
915         struct list_head *tmp, *n;
916
917         LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
918                       obd->obd_name);
919
920         ldlm_reprocess_all_ns(obd->obd_namespace);
921
922         /* when recovery finished, cleanup orphans on mds and ost */
923         if (OBT(obd) && OBP(obd, postrecov)) {
924                 int rc = OBP(obd, postrecov)(obd);
925                 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
926                               rc < 0 ? "failed" : "complete", rc);
927         }
928
929         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
930                 struct ptlrpc_request *req;
931                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
932                 list_del(&req->rq_list);
933                 DEBUG_REQ(D_HA, req, "delayed:");
934                 ptlrpc_reply(req);
935                 target_release_saved_req(req);
936         }
937         obd->obd_recovery_end = CURRENT_SECONDS;
938 }
939
940 static void abort_recovery_queue(struct obd_device *obd)
941 {
942         struct ptlrpc_request *req;
943         struct list_head *tmp, *n;
944         int rc;
945
946         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
947                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
948                 list_del(&req->rq_list);
949                 DEBUG_REQ(D_ERROR, req, "aborted:");
950                 req->rq_status = -ENOTCONN;
951                 req->rq_type = PTL_RPC_MSG_ERR;
952                 rc = lustre_pack_reply(req, 1, NULL, NULL);
953                 if (rc == 0) {
954                         ptlrpc_reply(req);
955                 } else {
956                         DEBUG_REQ(D_ERROR, req,
957                                   "packing failed for abort-reply; skipping");
958                 }
959                 target_release_saved_req(req);
960         }
961 }
962
963 /* Called from a cleanup function if the device is being cleaned up
964    forcefully.  The exports should all have been disconnected already,
965    the only thing left to do is
966      - clear the recovery flags
967      - cancel the timer
968      - free queued requests and replies, but don't send replies
969    Because the obd_stopping flag is set, no new requests should be received.
970
971 */
972 void target_cleanup_recovery(struct obd_device *obd)
973 {
974         struct list_head *tmp, *n;
975         struct ptlrpc_request *req;
976         ENTRY;
977
978         LASSERT(obd->obd_stopping);
979
980         spin_lock_bh(&obd->obd_processing_task_lock);
981         if (!obd->obd_recovering) {
982                 spin_unlock_bh(&obd->obd_processing_task_lock);
983                 EXIT;
984                 return;
985         }
986         obd->obd_recovering = obd->obd_abort_recovery = 0;
987         target_cancel_recovery_timer(obd);
988         spin_unlock_bh(&obd->obd_processing_task_lock);
989
990         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
991                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
992                 list_del(&req->rq_list);
993                 target_release_saved_req(req);
994         }
995
996         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
997                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
998                 list_del(&req->rq_list);
999                 target_release_saved_req(req);
1000         }
1001         EXIT;
1002 }
1003
1004 void target_abort_recovery(void *data)
1005 {
1006         struct obd_device *obd = data;
1007
1008         ENTRY;
1009         spin_lock_bh(&obd->obd_processing_task_lock);
1010         if (!obd->obd_recovering) {
1011                 spin_unlock_bh(&obd->obd_processing_task_lock);
1012                 EXIT;
1013                 return;
1014         }
1015         obd->obd_recovering = obd->obd_abort_recovery = 0;
1016         obd->obd_recoverable_clients = 0;
1017         target_cancel_recovery_timer(obd);
1018         spin_unlock_bh(&obd->obd_processing_task_lock);
1019
1020         LCONSOLE_WARN("%s: recovery period over; disconnecting unfinished "
1021                       "clients.\n", obd->obd_name);
1022         class_disconnect_stale_exports(obd);
1023         abort_recovery_queue(obd);
1024
1025         target_finish_recovery(obd);
1026         CDEBUG(D_HA, "%s: recovery complete\n", obd_uuid2str(&obd->obd_uuid));
1027         EXIT;
1028 }
1029
1030 static void target_recovery_expired(unsigned long castmeharder)
1031 {
1032         struct obd_device *obd = (struct obd_device *)castmeharder;
1033         CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1034         spin_lock_bh(&obd->obd_processing_task_lock);
1035         if (obd->obd_recovering)
1036                 obd->obd_abort_recovery = 1;
1037         cfs_waitq_signal(&obd->obd_next_transno_waitq);
1038         spin_unlock_bh(&obd->obd_processing_task_lock);
1039 }
1040
1041
1042 /* obd_processing_task_lock should be held */
1043 void target_cancel_recovery_timer(struct obd_device *obd)
1044 {
1045         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1046         cfs_timer_disarm(&obd->obd_recovery_timer);
1047 }
1048
1049 static void reset_recovery_timer(struct obd_device *obd)
1050 {
1051         spin_lock_bh(&obd->obd_processing_task_lock);
1052         if (!obd->obd_recovering) {
1053                 spin_unlock_bh(&obd->obd_processing_task_lock);
1054                 return;
1055         }
1056         cfs_timer_arm(&obd->obd_recovery_timer, 
1057                       cfs_time_shift(OBD_RECOVERY_TIMEOUT));
1058         spin_unlock_bh(&obd->obd_processing_task_lock);
1059         CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
1060                OBD_RECOVERY_TIMEOUT);
1061         /* Only used for lprocfs_status */
1062         obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT;
1063 }
1064
1065
1066 /* Only start it the first time called */
1067 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
1068 {
1069         spin_lock_bh(&obd->obd_processing_task_lock);
1070         if (obd->obd_recovery_handler) {
1071                 spin_unlock_bh(&obd->obd_processing_task_lock);
1072                 return;
1073         }
1074         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1075               OBD_RECOVERY_TIMEOUT);
1076         obd->obd_recovery_handler = handler;
1077         cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1078         spin_unlock_bh(&obd->obd_processing_task_lock);
1079
1080         reset_recovery_timer(obd);
1081 }
1082
1083 static int check_for_next_transno(struct obd_device *obd)
1084 {
1085         struct ptlrpc_request *req;
1086         int wake_up = 0, connected, completed, queue_len, max;
1087         __u64 next_transno, req_transno;
1088
1089         spin_lock_bh(&obd->obd_processing_task_lock);
1090         req = list_entry(obd->obd_recovery_queue.next,
1091                          struct ptlrpc_request, rq_list);
1092         max = obd->obd_max_recoverable_clients;
1093         req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1094         connected = obd->obd_connected_clients;
1095         completed = max - obd->obd_recoverable_clients;
1096         queue_len = obd->obd_requests_queued_for_recovery;
1097         next_transno = obd->obd_next_recovery_transno;
1098
1099         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1100                "req_transno: "LPU64", next_transno: "LPU64"\n",
1101                max, connected, completed, queue_len, req_transno, next_transno);
1102         if (obd->obd_abort_recovery) {
1103                 CDEBUG(D_HA, "waking for aborted recovery\n");
1104                 wake_up = 1;
1105         } else if (!obd->obd_recovering) {
1106                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1107                 wake_up = 1;
1108         } else if (req_transno == next_transno) {
1109                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1110                 wake_up = 1;
1111         } else if (queue_len + completed == max) {
1112                 CDEBUG(D_ERROR,
1113                        "waking for skipped transno (skip: "LPD64
1114                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1115                        next_transno, queue_len, completed, max, req_transno);
1116                 obd->obd_next_recovery_transno = req_transno;
1117                 wake_up = 1;
1118         }
1119         spin_unlock_bh(&obd->obd_processing_task_lock);
1120         LASSERT(lustre_msg_get_transno(req->rq_reqmsg) >= next_transno);
1121         return wake_up;
1122 }
1123
1124 static void process_recovery_queue(struct obd_device *obd)
1125 {
1126         struct ptlrpc_request *req;
1127         int abort_recovery = 0;
1128         struct l_wait_info lwi = { 0 };
1129         ENTRY;
1130
1131         for (;;) {
1132                 spin_lock_bh(&obd->obd_processing_task_lock);
1133                 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1134                 req = list_entry(obd->obd_recovery_queue.next,
1135                                  struct ptlrpc_request, rq_list);
1136
1137                 if (lustre_msg_get_transno(req->rq_reqmsg) !=
1138                     obd->obd_next_recovery_transno) {
1139                         spin_unlock_bh(&obd->obd_processing_task_lock);
1140                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1141                                LPD64")\n",
1142                                obd->obd_next_recovery_transno,
1143                                lustre_msg_get_transno(req->rq_reqmsg));
1144                         l_wait_event(obd->obd_next_transno_waitq,
1145                                      check_for_next_transno(obd), &lwi);
1146                         spin_lock_bh(&obd->obd_processing_task_lock);
1147                         abort_recovery = obd->obd_abort_recovery;
1148                         spin_unlock_bh(&obd->obd_processing_task_lock);
1149                         if (abort_recovery) {
1150                                 target_abort_recovery(obd);
1151                                 return;
1152                         }
1153                         continue;
1154                 }
1155                 list_del_init(&req->rq_list);
1156                 obd->obd_requests_queued_for_recovery--;
1157                 spin_unlock_bh(&obd->obd_processing_task_lock);
1158
1159                 DEBUG_REQ(D_HA, req, "processing: ");
1160                 (void)obd->obd_recovery_handler(req);
1161                 obd->obd_replayed_requests++;
1162                 reset_recovery_timer(obd);
1163                 /* bug 1580: decide how to properly sync() in recovery */
1164                 //mds_fsync_super(obd->u.obt.obt_sb);
1165                 class_export_put(req->rq_export);
1166                 if (req->rq_reply_state != NULL) {
1167                         ptlrpc_rs_decref(req->rq_reply_state);
1168                         /* req->rq_reply_state = NULL; */
1169                 }
1170                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1171                 OBD_FREE(req, sizeof *req);
1172                 spin_lock_bh(&obd->obd_processing_task_lock);
1173                 obd->obd_next_recovery_transno++;
1174                 if (list_empty(&obd->obd_recovery_queue)) {
1175                         obd->obd_processing_task = 0;
1176                         spin_unlock_bh(&obd->obd_processing_task_lock);
1177                         break;
1178                 }
1179                 spin_unlock_bh(&obd->obd_processing_task_lock);
1180         }
1181         EXIT;
1182 }
1183
1184 int target_queue_recovery_request(struct ptlrpc_request *req,
1185                                   struct obd_device *obd)
1186 {
1187         struct list_head *tmp;
1188         int inserted = 0;
1189         __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1190         struct ptlrpc_request *saved_req;
1191         struct lustre_msg *reqmsg;
1192
1193         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1194          * (i.e. buflens etc are in my own byte order), but type-dependent
1195          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1196
1197         if (!transno) {
1198                 CFS_INIT_LIST_HEAD(&req->rq_list);
1199                 DEBUG_REQ(D_HA, req, "not queueing");
1200                 return 1;
1201         }
1202
1203         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1204         /* XXX just like the request-dup code in queue_final_reply */
1205         OBD_ALLOC(saved_req, sizeof *saved_req);
1206         if (!saved_req)
1207                 LBUG();
1208         OBD_ALLOC(reqmsg, req->rq_reqlen);
1209         if (!reqmsg)
1210                 LBUG();
1211
1212         spin_lock_bh(&obd->obd_processing_task_lock);
1213
1214         /* If we're processing the queue, we want don't want to queue this
1215          * message.
1216          *
1217          * Also, if this request has a transno less than the one we're waiting
1218          * for, we should process it now.  It could (and currently always will)
1219          * be an open request for a descriptor that was opened some time ago.
1220          *
1221          * Also, a resent, replayed request that has already been
1222          * handled will pass through here and be processed immediately.
1223          */
1224         if (obd->obd_processing_task == cfs_curproc_pid() ||
1225             transno < obd->obd_next_recovery_transno) {
1226                 /* Processing the queue right now, don't re-add. */
1227                 LASSERT(list_empty(&req->rq_list));
1228                 spin_unlock_bh(&obd->obd_processing_task_lock);
1229                 OBD_FREE(reqmsg, req->rq_reqlen);
1230                 OBD_FREE(saved_req, sizeof *saved_req);
1231                 return 1;
1232         }
1233
1234         /* A resent, replayed request that is still on the queue; just drop it.
1235            The queued request will handle this. */
1236         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1237             (MSG_RESENT | MSG_REPLAY)) {
1238                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1239                 spin_unlock_bh(&obd->obd_processing_task_lock);
1240                 OBD_FREE(reqmsg, req->rq_reqlen);
1241                 OBD_FREE(saved_req, sizeof *saved_req);
1242                 return 0;
1243         }
1244
1245         memcpy(saved_req, req, sizeof *req);
1246         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1247         req = saved_req;
1248         req->rq_reqmsg = reqmsg;
1249         class_export_get(req->rq_export);
1250         CFS_INIT_LIST_HEAD(&req->rq_list);
1251
1252         /* XXX O(n^2) */
1253         list_for_each(tmp, &obd->obd_recovery_queue) {
1254                 struct ptlrpc_request *reqiter =
1255                         list_entry(tmp, struct ptlrpc_request, rq_list);
1256
1257                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1258                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1259                         inserted = 1;
1260                         break;
1261                 }
1262         }
1263
1264         if (!inserted) {
1265                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1266         }
1267
1268         obd->obd_requests_queued_for_recovery++;
1269
1270         if (obd->obd_processing_task != 0) {
1271                 /* Someone else is processing this queue, we'll leave it to
1272                  * them.
1273                  */
1274                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1275                 spin_unlock_bh(&obd->obd_processing_task_lock);
1276                 return 0;
1277         }
1278
1279         /* Nobody is processing, and we know there's (at least) one to process
1280          * now, so we'll do the honours.
1281          */
1282         obd->obd_processing_task = cfs_curproc_pid();
1283         spin_unlock_bh(&obd->obd_processing_task_lock);
1284
1285         process_recovery_queue(obd);
1286         return 0;
1287 }
1288
1289 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1290 {
1291         return req->rq_export->exp_obd;
1292 }
1293
1294 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1295 {
1296         struct obd_device *obd = target_req2obd(req);
1297         struct ptlrpc_request *saved_req;
1298         struct lustre_msg *reqmsg;
1299         int recovery_done = 0;
1300
1301         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1302
1303         if (rc) {
1304                 /* Just like ptlrpc_error, but without the sending. */
1305                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1306                 LASSERT(rc == 0); /* XXX handle this */
1307                 req->rq_type = PTL_RPC_MSG_ERR;
1308         }
1309
1310         LASSERT (!req->rq_reply_state->rs_difficult);
1311         LASSERT(list_empty(&req->rq_list));
1312         /* XXX a bit like the request-dup code in queue_recovery_request */
1313         OBD_ALLOC(saved_req, sizeof *saved_req);
1314         if (!saved_req)
1315                 LBUG();
1316         OBD_ALLOC(reqmsg, req->rq_reqlen);
1317         if (!reqmsg)
1318                 LBUG();
1319         *saved_req = *req;
1320         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1321
1322         /* Don't race cleanup */
1323         spin_lock_bh(&obd->obd_processing_task_lock);
1324         if (obd->obd_stopping) {
1325                 spin_unlock_bh(&obd->obd_processing_task_lock);
1326                 OBD_FREE(reqmsg, req->rq_reqlen);
1327                 OBD_FREE(saved_req, sizeof *req);
1328                 req->rq_status = -ENOTCONN;
1329                 /* rv is ignored anyhow */
1330                 return -ENOTCONN;
1331         }
1332         ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1333         req = saved_req;
1334         req->rq_reqmsg = reqmsg;
1335         class_export_get(req->rq_export);
1336         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1337
1338         /* only count the first "replay over" request from each
1339            export */
1340         if (req->rq_export->exp_replay_needed) {
1341                 --obd->obd_recoverable_clients;
1342                 req->rq_export->exp_replay_needed = 0;
1343         }
1344         recovery_done = (obd->obd_recoverable_clients == 0);
1345         spin_unlock_bh(&obd->obd_processing_task_lock);
1346
1347         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1348         if (recovery_done) {
1349                 spin_lock_bh(&obd->obd_processing_task_lock);
1350                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1351                 target_cancel_recovery_timer(obd);
1352                 spin_unlock_bh(&obd->obd_processing_task_lock);
1353
1354                 target_finish_recovery(obd);
1355                 CDEBUG(D_HA, "%s: recovery complete\n",
1356                        obd_uuid2str(&obd->obd_uuid));
1357         } else {
1358                 CWARN("%s: %d recoverable clients remain\n",
1359                        obd->obd_name, obd->obd_recoverable_clients);
1360                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1361         }
1362
1363         return 1;
1364 }
1365
1366 int
1367 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1368 {
1369         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1370                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1371                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1372                 return (-ECOMM);
1373         }
1374
1375         if (rc) {
1376                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1377                 req->rq_status = rc;
1378                 return (ptlrpc_error(req));
1379         } else {
1380                 DEBUG_REQ(D_NET, req, "sending reply");
1381         }
1382
1383         return (ptlrpc_send_reply(req, 1));
1384 }
1385
1386 void
1387 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1388 {
1389         int                        netrc;
1390         struct ptlrpc_reply_state *rs;
1391         struct obd_device         *obd;
1392         struct obd_export         *exp;
1393         struct ptlrpc_service     *svc;
1394
1395         svc = req->rq_rqbd->rqbd_service;
1396         rs = req->rq_reply_state;
1397         if (rs == NULL || !rs->rs_difficult) {
1398                 /* no notifiers */
1399                 target_send_reply_msg (req, rc, fail_id);
1400                 return;
1401         }
1402
1403         /* must be an export if locks saved */
1404         LASSERT (req->rq_export != NULL);
1405         /* req/reply consistent */
1406         LASSERT (rs->rs_service == svc);
1407
1408         /* "fresh" reply */
1409         LASSERT (!rs->rs_scheduled);
1410         LASSERT (!rs->rs_scheduled_ever);
1411         LASSERT (!rs->rs_handled);
1412         LASSERT (!rs->rs_on_net);
1413         LASSERT (rs->rs_export == NULL);
1414         LASSERT (list_empty(&rs->rs_obd_list));
1415         LASSERT (list_empty(&rs->rs_exp_list));
1416
1417         exp = class_export_get (req->rq_export);
1418         obd = exp->exp_obd;
1419
1420         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1421         rs->rs_scheduled = 1;
1422         rs->rs_on_net    = 1;
1423         rs->rs_xid       = req->rq_xid;
1424         rs->rs_transno   = req->rq_transno;
1425         rs->rs_export    = exp;
1426
1427         spin_lock(&obd->obd_uncommitted_replies_lock);
1428
1429         if (rs->rs_transno > obd->obd_last_committed) {
1430                 /* not committed already */
1431                 list_add_tail (&rs->rs_obd_list,
1432                                &obd->obd_uncommitted_replies);
1433         }
1434
1435         spin_unlock (&obd->obd_uncommitted_replies_lock);
1436         spin_lock (&exp->exp_lock);
1437
1438         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1439
1440         spin_unlock(&exp->exp_lock);
1441
1442         netrc = target_send_reply_msg (req, rc, fail_id);
1443
1444         spin_lock(&svc->srv_lock);
1445
1446         svc->srv_n_difficult_replies++;
1447
1448         if (netrc != 0) {
1449                 /* error sending: reply is off the net.  Also we need +1
1450                  * reply ref until ptlrpc_server_handle_reply() is done
1451                  * with the reply state (if the send was successful, there
1452                  * would have been +1 ref for the net, which
1453                  * reply_out_callback leaves alone) */
1454                 rs->rs_on_net = 0;
1455                 ptlrpc_rs_addref(rs);
1456                 atomic_inc (&svc->srv_outstanding_replies);
1457         }
1458
1459         if (!rs->rs_on_net ||                   /* some notifier */
1460             list_empty(&rs->rs_exp_list) ||     /* completed already */
1461             list_empty(&rs->rs_obd_list)) {
1462                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1463                 cfs_waitq_signal (&svc->srv_waitq);
1464         } else {
1465                 list_add (&rs->rs_list, &svc->srv_active_replies);
1466                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1467         }
1468
1469         spin_unlock(&svc->srv_lock);
1470 }
1471
1472 int target_handle_ping(struct ptlrpc_request *req)
1473 {
1474         obd_ping(req->rq_export);
1475         return lustre_pack_reply(req, 1, NULL, NULL);
1476 }
1477
1478 void target_committed_to_req(struct ptlrpc_request *req)
1479 {
1480         struct obd_device *obd = req->rq_export->exp_obd;
1481
1482         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1483                 lustre_msg_set_last_committed(req->rq_repmsg,
1484                                               obd->obd_last_committed);
1485         else
1486                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1487
1488         CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1489                obd->obd_last_committed, req->rq_xid);
1490 }
1491
1492 EXPORT_SYMBOL(target_committed_to_req);
1493
1494 #ifdef HAVE_QUOTA_SUPPORT
1495 int target_handle_qc_callback(struct ptlrpc_request *req)
1496 {
1497         struct obd_quotactl *oqctl;
1498         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1499
1500         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1501                                    lustre_swab_obd_quotactl);
1502         if (oqctl == NULL) {
1503                 CERROR("Can't unpack obd_quotactl\n");
1504                 RETURN(-EPROTO);
1505         }
1506
1507         cli->cl_qchk_stat = oqctl->qc_stat;
1508
1509         return 0;
1510 }
1511
1512 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1513 {
1514 #ifdef __KERNEL__
1515         struct obd_device *obd = req->rq_export->exp_obd;
1516         struct obd_device *master_obd;
1517         struct lustre_quota_ctxt *qctxt;
1518         struct qunit_data *qdata;
1519         void* rep;
1520         struct qunit_data_old *qdata_old;
1521         int rc = 0;
1522         int repsize[2] = { sizeof(struct ptlrpc_body),
1523                            sizeof(struct qunit_data) };
1524         ENTRY;
1525         
1526         rc = lustre_pack_reply(req, 2, repsize, NULL);
1527         if (rc) {
1528                 CERROR("packing reply failed!: rc = %d\n", rc);
1529                 RETURN(rc);
1530         }
1531         LASSERT(req->rq_export);
1532
1533         /* fixed for bug10707 */
1534         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1535             !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1536                 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
1537                 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, 
1538                                      sizeof(struct qunit_data));
1539                 LASSERT(rep);
1540                 qdata = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata), 
1541                                            lustre_swab_qdata);
1542         } else {
1543                 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
1544                 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, 
1545                                      sizeof(struct qunit_data_old));
1546                 LASSERT(rep);
1547                 qdata_old = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata_old), 
1548                                                lustre_swab_qdata_old);
1549                 qdata = lustre_quota_old_to_new(qdata_old);
1550         }
1551
1552         if (qdata == NULL) {
1553                 CERROR("Can't unpack qunit_data\n");
1554                 RETURN(-EPROTO);
1555         }
1556
1557         /* we use the observer */
1558         LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1559         master_obd = obd->obd_observer->obd_observer;
1560         qctxt = &master_obd->u.obt.obt_qctxt;
1561         
1562         LASSERT(qctxt->lqc_handler);
1563         rc = qctxt->lqc_handler(master_obd, qdata,
1564                                 lustre_msg_get_opc(req->rq_reqmsg));
1565         if (rc && rc != -EDQUOT)
1566                 CDEBUG(rc == -EBUSY  ? D_QUOTA : D_ERROR, 
1567                        "dqacq failed! (rc:%d)\n", rc);
1568         
1569         /* the qd_count might be changed in lqc_handler */
1570         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1571             !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1572                 memcpy(rep,qdata,sizeof(*qdata));
1573         } else {
1574                 qdata_old = lustre_quota_new_to_old(qdata);
1575                 memcpy(rep,qdata_old,sizeof(*qdata_old));
1576         }
1577         req->rq_status = rc;
1578         rc = ptlrpc_reply(req);
1579         
1580         RETURN(rc);     
1581 #else
1582         return 0;
1583 #endif /* !__KERNEL__ */
1584 }
1585 #endif /* HAVE_QUOTA_SUPPORT */
1586
1587 ldlm_mode_t lck_compat_array[] = {
1588         [LCK_EX] LCK_COMPAT_EX,
1589         [LCK_PW] LCK_COMPAT_PW,
1590         [LCK_PR] LCK_COMPAT_PR,
1591         [LCK_CW] LCK_COMPAT_CW,
1592         [LCK_CR] LCK_COMPAT_CR,
1593         [LCK_NL] LCK_COMPAT_NL,
1594         [LCK_GROUP] LCK_COMPAT_GROUP
1595 };