Whamcloud - gitweb
b=12007
[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         struct list_head *p;
544         char *str, *tmp;
545         int rc = 0, abort_recovery;
546         struct obd_connect_data *data;
547         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
548         ENTRY;
549
550         OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
551
552         LASSERT_REQSWAB(req, REQ_REC_OFF);
553         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
554         if (str == NULL) {
555                 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect\n");
556                 GOTO(out, rc = -EINVAL);
557         }
558
559         obd_str2uuid (&tgtuuid, str);
560         target = class_uuid2obd(&tgtuuid);
561         /* COMPAT_146 */
562         /* old (pre 1.6) lustre_process_log tries to connect to mdsname
563            (eg. mdsA) instead of uuid. */
564         if (!target) {
565                 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
566                 target = class_uuid2obd(&tgtuuid);
567         }
568         if (!target)
569                 target = class_name2obd(str);
570         /* end COMPAT_146 */
571
572         if (!target || target->obd_stopping || !target->obd_set_up) {
573                 LCONSOLE_ERROR("UUID '%s' is not available "
574                                " for connect (%s)\n", str,
575                                !target ? "no target" :
576                                (target->obd_stopping ? "stopping" :
577                                 "not set up"));
578                 GOTO(out, rc = -ENODEV);
579         }
580
581         if (target->obd_no_conn) {
582                 LCONSOLE_WARN("%s: temporarily refusing client connection "
583                               "from %s\n", target->obd_name, 
584                               libcfs_nid2str(req->rq_peer.nid));
585                 GOTO(out, rc = -EAGAIN);
586         }
587
588         /* Make sure the target isn't cleaned up while we're here. Yes, 
589            there's still a race between the above check and our incref here. 
590            Really, class_uuid2obd should take the ref. */
591         targref = class_incref(target);
592
593         LASSERT_REQSWAB(req, REQ_REC_OFF + 1);
594         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
595                                 sizeof(cluuid) - 1);
596         if (str == NULL) {
597                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect\n");
598                 GOTO(out, rc = -EINVAL);
599         }
600
601         obd_str2uuid (&cluuid, str);
602
603         /* XXX extract a nettype and format accordingly */
604         switch (sizeof(lnet_nid_t)) {
605                 /* NB the casts only avoid compiler warnings */
606         case 8:
607                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
608                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
609                 break;
610         case 4:
611                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
612                          "NET_%x_UUID", (__u32)req->rq_peer.nid);
613                 break;
614         default:
615                 LBUG();
616         }
617
618         spin_lock_bh(&target->obd_processing_task_lock);
619         abort_recovery = target->obd_abort_recovery;
620         spin_unlock_bh(&target->obd_processing_task_lock);
621         if (abort_recovery)
622                 target_abort_recovery(target);
623
624         tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
625         if (tmp == NULL)
626                 GOTO(out, rc = -EPROTO);
627
628         memcpy(&conn, tmp, sizeof conn);
629
630         data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
631                                   lustre_swab_connect);
632         rc = lustre_pack_reply(req, 2, size, NULL);
633         if (rc)
634                 GOTO(out, rc);
635
636         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
637                 if (!data) {
638                         DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
639                                   "libclient connection attempt\n");
640                         GOTO(out, rc = -EPROTO);
641                 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
642                                                LUSTRE_VERSION_ALLOWED_OFFSET ||
643                            data->ocd_version > LUSTRE_VERSION_CODE +
644                                                LUSTRE_VERSION_ALLOWED_OFFSET) {
645                         DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
646                                   "libclient connection attempt\n",
647                                   data->ocd_version < LUSTRE_VERSION_CODE ?
648                                   "old" : "new",
649                                   OBD_OCD_VERSION_MAJOR(data->ocd_version),
650                                   OBD_OCD_VERSION_MINOR(data->ocd_version),
651                                   OBD_OCD_VERSION_PATCH(data->ocd_version),
652                                   OBD_OCD_VERSION_FIX(data->ocd_version));
653                         data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
654                                               offsetof(typeof(*data),
655                                                        ocd_version) +
656                                               sizeof(data->ocd_version));
657                         if (data) {
658                                 data->ocd_connect_flags = OBD_CONNECT_VERSION;
659                                 data->ocd_version = LUSTRE_VERSION_CODE;
660                         }
661                         GOTO(out, rc = -EPROTO);
662                 }
663         }
664
665         /* lctl gets a backstage, all-access pass. */
666         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
667                 goto dont_check_exports;
668
669         spin_lock(&target->obd_dev_lock);
670         list_for_each(p, &target->obd_exports) {
671                 export = list_entry(p, struct obd_export, exp_obd_chain);
672                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
673                         if (export->exp_connecting) { /* bug 9635, et. al. */
674                                 CWARN("%s: exp %p already connecting\n",
675                                       export->exp_obd->obd_name, export);
676                                 export = NULL;
677                                 rc = -EALREADY;
678                                 break;
679                         }
680
681                         /* make darn sure this is coming from the same peer
682                          * if the UUIDs matched */
683                         if ((export->exp_connection != NULL) &&
684                                         (strcmp(libcfs_nid2str(req->rq_peer.nid),
685                                                 libcfs_nid2str(export->exp_connection->c_peer.nid)))) {
686                                         CWARN("%s: cookie %s seen on new NID %s when "
687                                                         "existing NID %s is already connected\n",
688                                                         target->obd_name, cluuid.uuid,
689                                                         libcfs_nid2str(req->rq_peer.nid),
690                                                         libcfs_nid2str(export->exp_connection->c_peer.nid));
691                                         export = NULL;
692                                         rc = -EALREADY;
693                                         break;
694                         }
695
696                         export->exp_connecting = 1;
697                         spin_unlock(&target->obd_dev_lock);
698                         LASSERT(export->exp_obd == target);
699
700                         rc = target_handle_reconnect(&conn, export, &cluuid);
701                         break;
702                 }
703                 export = NULL;
704         }
705         /* If we found an export, we already unlocked. */
706         if (!export) {
707                 spin_unlock(&target->obd_dev_lock);
708                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
709         } else if (req->rq_export == NULL &&
710                    atomic_read(&export->exp_rpc_count) > 0) {
711                 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
712                       target->obd_name, cluuid.uuid,
713                       libcfs_nid2str(req->rq_peer.nid),
714                       export, atomic_read(&export->exp_refcount));
715                 GOTO(out, rc = -EBUSY);
716         } else if (req->rq_export != NULL &&
717                    atomic_read(&export->exp_rpc_count) > 1) {
718                 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
719                       target->obd_name, cluuid.uuid,
720                       libcfs_nid2str(req->rq_peer.nid),
721                       export, atomic_read(&export->exp_rpc_count));
722                 GOTO(out, rc = -EBUSY);
723         } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
724                 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
725                        "cookies not random?\n", target->obd_name,
726                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
727                 GOTO(out, rc = -EALREADY);
728         } else {
729                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
730         }
731
732         /* We want to handle EALREADY but *not* -EALREADY from
733          * target_handle_reconnect(), return reconnection state in a flag */
734         if (rc == EALREADY) {
735                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
736                 rc = 0;
737         } else if (rc) {
738                 GOTO(out, rc);
739         }
740
741         /* Tell the client if we're in recovery. */
742         /* If this is the first client, start the recovery timer */
743         if (target->obd_recovering) {
744                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
745                 target_start_recovery_timer(target, handler);
746         }
747
748         /* Tell the client if we support replayable requests */
749         if (target->obd_replayable)
750                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
751
752         if (export == NULL) {
753                 if (target->obd_recovering) {
754                         CERROR("%s: denying connection for new client %s (%s): "
755                                "%d clients in recovery for %lds\n",
756                                target->obd_name,
757                                libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
758                                target->obd_recoverable_clients,
759                                cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
760                                                              cfs_time_current())));
761                         rc = -EBUSY;
762                 } else {
763  dont_check_exports:
764                         rc = obd_connect(&conn, target, &cluuid, data);
765                 }
766         } else {
767                 rc = obd_reconnect(export, target, &cluuid, data);
768         }
769
770         if (rc)
771                 GOTO(out, rc);
772
773         /* Return only the parts of obd_connect_data that we understand, so the
774          * client knows that we don't understand the rest. */
775         if (data)
776                 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
777                                       sizeof(*data)),
778                        data, sizeof(*data));
779
780         /* If all else goes well, this is our RPC return code. */
781         req->rq_status = 0;
782
783         lustre_msg_set_handle(req->rq_repmsg, &conn);
784
785         /* ownership of this export ref transfers to the request AFTER we
786          * drop any previous reference the request had, but we don't want
787          * that to go to zero before we get our new export reference. */
788         export = class_conn2export(&conn);
789         if (!export) {
790                 DEBUG_REQ(D_ERROR, req, "Missing export!\n");
791                 GOTO(out, rc = -ENODEV);
792         }
793
794         /* If the client and the server are the same node, we will already
795          * have an export that really points to the client's DLM export,
796          * because we have a shared handles table.
797          *
798          * XXX this will go away when shaver stops sending the "connect" handle
799          * in the real "remote handle" field of the request --phik 24 Apr 2003
800          */
801         if (req->rq_export != NULL)
802                 class_export_put(req->rq_export);
803
804         req->rq_export = export;
805
806         spin_lock(&export->exp_lock);
807         if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
808                 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
809                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
810                        export->exp_conn_cnt,
811                        lustre_msg_get_conn_cnt(req->rq_reqmsg));
812                        
813                 spin_unlock(&export->exp_lock);
814                 GOTO(out, rc = -EALREADY);
815         }
816         export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
817         spin_unlock(&export->exp_lock);
818
819         /* request from liblustre?  Don't evict it for not pinging. */
820         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
821                 export->exp_libclient = 1;
822                 spin_lock(&target->obd_dev_lock);
823                 list_del_init(&export->exp_obd_chain_timed);
824                 spin_unlock(&target->obd_dev_lock);
825         }
826
827         if (export->exp_connection != NULL)
828                 ptlrpc_put_connection(export->exp_connection);
829         export->exp_connection = ptlrpc_get_connection(req->rq_peer,
830                                                        req->rq_self,
831                                                        &remote_uuid);
832
833         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT)
834                 GOTO(out, rc = 0);
835
836         if (target->obd_recovering)
837                 target->obd_connected_clients++;
838
839         memcpy(&conn,
840                lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
841                sizeof conn);
842
843         if (export->exp_imp_reverse != NULL)
844                 class_destroy_import(export->exp_imp_reverse);
845         revimp = export->exp_imp_reverse = class_new_import(target);
846         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
847         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
848         revimp->imp_remote_handle = conn;
849         revimp->imp_dlm_fake = 1;
850         revimp->imp_state = LUSTRE_IMP_FULL;
851
852         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
853                 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
854                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
855         }
856
857         class_import_put(revimp);
858 out:
859         if (export)
860                 export->exp_connecting = 0;
861         if (targref) 
862                 class_decref(targref);
863         if (rc)
864                 req->rq_status = rc;
865         RETURN(rc);
866 }
867
868 int target_handle_disconnect(struct ptlrpc_request *req)
869 {
870         int rc;
871         ENTRY;
872
873         rc = lustre_pack_reply(req, 1, NULL, NULL);
874         if (rc)
875                 RETURN(rc);
876
877         /* keep the rq_export around so we can send the reply */
878         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
879         RETURN(0);
880 }
881
882 void target_destroy_export(struct obd_export *exp)
883 {
884         /* exports created from last_rcvd data, and "fake"
885            exports created by lctl don't have an import */
886         if (exp->exp_imp_reverse != NULL)
887                 class_destroy_import(exp->exp_imp_reverse);
888
889         /* We cancel locks at disconnect time, but this will catch any locks
890          * granted in a race with recovery-induced disconnect. */
891         if (exp->exp_obd->obd_namespace != NULL)
892                 ldlm_cancel_locks_for_export(exp);
893 }
894
895 /*
896  * Recovery functions
897  */
898
899
900 static void target_release_saved_req(struct ptlrpc_request *req)
901 {
902         if (req->rq_reply_state != NULL) {
903                 ptlrpc_rs_decref(req->rq_reply_state);
904                 /* req->rq_reply_state = NULL; */
905         }
906
907         class_export_put(req->rq_export);
908         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
909         OBD_FREE(req, sizeof *req);
910 }
911
912 static void target_finish_recovery(struct obd_device *obd)
913 {
914         struct list_head *tmp, *n;
915
916         LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
917                       obd->obd_name);
918
919         ldlm_reprocess_all_ns(obd->obd_namespace);
920
921         /* when recovery finished, cleanup orphans on mds and ost */
922         if (OBT(obd) && OBP(obd, postrecov)) {
923                 int rc = OBP(obd, postrecov)(obd);
924                 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
925                               rc < 0 ? "failed" : "complete", rc);
926         }
927
928         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
929                 struct ptlrpc_request *req;
930                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
931                 list_del(&req->rq_list);
932                 DEBUG_REQ(D_HA, req, "delayed:");
933                 ptlrpc_reply(req);
934                 target_release_saved_req(req);
935         }
936         obd->obd_recovery_end = CURRENT_SECONDS;
937 }
938
939 static void abort_recovery_queue(struct obd_device *obd)
940 {
941         struct ptlrpc_request *req;
942         struct list_head *tmp, *n;
943         int rc;
944
945         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
946                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
947                 list_del(&req->rq_list);
948                 DEBUG_REQ(D_ERROR, req, "aborted:");
949                 req->rq_status = -ENOTCONN;
950                 req->rq_type = PTL_RPC_MSG_ERR;
951                 rc = lustre_pack_reply(req, 1, NULL, NULL);
952                 if (rc == 0) {
953                         ptlrpc_reply(req);
954                 } else {
955                         DEBUG_REQ(D_ERROR, req,
956                                   "packing failed for abort-reply; skipping");
957                 }
958                 target_release_saved_req(req);
959         }
960 }
961
962 /* Called from a cleanup function if the device is being cleaned up
963    forcefully.  The exports should all have been disconnected already,
964    the only thing left to do is
965      - clear the recovery flags
966      - cancel the timer
967      - free queued requests and replies, but don't send replies
968    Because the obd_stopping flag is set, no new requests should be received.
969
970 */
971 void target_cleanup_recovery(struct obd_device *obd)
972 {
973         struct list_head *tmp, *n;
974         struct ptlrpc_request *req;
975         ENTRY;
976
977         LASSERT(obd->obd_stopping);
978
979         spin_lock_bh(&obd->obd_processing_task_lock);
980         if (!obd->obd_recovering) {
981                 spin_unlock_bh(&obd->obd_processing_task_lock);
982                 EXIT;
983                 return;
984         }
985         obd->obd_recovering = obd->obd_abort_recovery = 0;
986         target_cancel_recovery_timer(obd);
987         spin_unlock_bh(&obd->obd_processing_task_lock);
988
989         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
990                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
991                 list_del(&req->rq_list);
992                 target_release_saved_req(req);
993         }
994
995         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
996                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
997                 list_del(&req->rq_list);
998                 target_release_saved_req(req);
999         }
1000         EXIT;
1001 }
1002
1003 void target_abort_recovery(void *data)
1004 {
1005         struct obd_device *obd = data;
1006
1007         ENTRY;
1008         spin_lock_bh(&obd->obd_processing_task_lock);
1009         if (!obd->obd_recovering) {
1010                 spin_unlock_bh(&obd->obd_processing_task_lock);
1011                 EXIT;
1012                 return;
1013         }
1014         obd->obd_recovering = obd->obd_abort_recovery = 0;
1015         obd->obd_recoverable_clients = 0;
1016         target_cancel_recovery_timer(obd);
1017         spin_unlock_bh(&obd->obd_processing_task_lock);
1018
1019         LCONSOLE_WARN("%s: recovery period over; disconnecting unfinished "
1020                       "clients.\n", obd->obd_name);
1021         class_disconnect_stale_exports(obd);
1022         abort_recovery_queue(obd);
1023
1024         target_finish_recovery(obd);
1025         CDEBUG(D_HA, "%s: recovery complete\n", obd_uuid2str(&obd->obd_uuid));
1026         EXIT;
1027 }
1028
1029 static void target_recovery_expired(unsigned long castmeharder)
1030 {
1031         struct obd_device *obd = (struct obd_device *)castmeharder;
1032         CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1033         spin_lock_bh(&obd->obd_processing_task_lock);
1034         if (obd->obd_recovering)
1035                 obd->obd_abort_recovery = 1;
1036         cfs_waitq_signal(&obd->obd_next_transno_waitq);
1037         spin_unlock_bh(&obd->obd_processing_task_lock);
1038 }
1039
1040
1041 /* obd_processing_task_lock should be held */
1042 void target_cancel_recovery_timer(struct obd_device *obd)
1043 {
1044         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1045         cfs_timer_disarm(&obd->obd_recovery_timer);
1046 }
1047
1048 static void reset_recovery_timer(struct obd_device *obd)
1049 {
1050         spin_lock_bh(&obd->obd_processing_task_lock);
1051         if (!obd->obd_recovering) {
1052                 spin_unlock_bh(&obd->obd_processing_task_lock);
1053                 return;
1054         }
1055         cfs_timer_arm(&obd->obd_recovery_timer, 
1056                       cfs_time_shift(OBD_RECOVERY_TIMEOUT));
1057         spin_unlock_bh(&obd->obd_processing_task_lock);
1058         CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
1059                OBD_RECOVERY_TIMEOUT);
1060         /* Only used for lprocfs_status */
1061         obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT;
1062 }
1063
1064
1065 /* Only start it the first time called */
1066 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
1067 {
1068         spin_lock_bh(&obd->obd_processing_task_lock);
1069         if (obd->obd_recovery_handler) {
1070                 spin_unlock_bh(&obd->obd_processing_task_lock);
1071                 return;
1072         }
1073         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1074               OBD_RECOVERY_TIMEOUT);
1075         obd->obd_recovery_handler = handler;
1076         cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1077         spin_unlock_bh(&obd->obd_processing_task_lock);
1078
1079         reset_recovery_timer(obd);
1080 }
1081
1082 static int check_for_next_transno(struct obd_device *obd)
1083 {
1084         struct ptlrpc_request *req;
1085         int wake_up = 0, connected, completed, queue_len, max;
1086         __u64 next_transno, req_transno;
1087
1088         spin_lock_bh(&obd->obd_processing_task_lock);
1089         req = list_entry(obd->obd_recovery_queue.next,
1090                          struct ptlrpc_request, rq_list);
1091         max = obd->obd_max_recoverable_clients;
1092         req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1093         connected = obd->obd_connected_clients;
1094         completed = max - obd->obd_recoverable_clients;
1095         queue_len = obd->obd_requests_queued_for_recovery;
1096         next_transno = obd->obd_next_recovery_transno;
1097
1098         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1099                "req_transno: "LPU64", next_transno: "LPU64"\n",
1100                max, connected, completed, queue_len, req_transno, next_transno);
1101         if (obd->obd_abort_recovery) {
1102                 CDEBUG(D_HA, "waking for aborted recovery\n");
1103                 wake_up = 1;
1104         } else if (!obd->obd_recovering) {
1105                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1106                 wake_up = 1;
1107         } else if (req_transno == next_transno) {
1108                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1109                 wake_up = 1;
1110         } else if (queue_len + completed == max) {
1111                 CDEBUG(D_ERROR,
1112                        "waking for skipped transno (skip: "LPD64
1113                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1114                        next_transno, queue_len, completed, max, req_transno);
1115                 obd->obd_next_recovery_transno = req_transno;
1116                 wake_up = 1;
1117         }
1118         spin_unlock_bh(&obd->obd_processing_task_lock);
1119         LASSERT(lustre_msg_get_transno(req->rq_reqmsg) >= next_transno);
1120         return wake_up;
1121 }
1122
1123 static void process_recovery_queue(struct obd_device *obd)
1124 {
1125         struct ptlrpc_request *req;
1126         int abort_recovery = 0;
1127         struct l_wait_info lwi = { 0 };
1128         ENTRY;
1129
1130         for (;;) {
1131                 spin_lock_bh(&obd->obd_processing_task_lock);
1132                 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1133                 req = list_entry(obd->obd_recovery_queue.next,
1134                                  struct ptlrpc_request, rq_list);
1135
1136                 if (lustre_msg_get_transno(req->rq_reqmsg) !=
1137                     obd->obd_next_recovery_transno) {
1138                         spin_unlock_bh(&obd->obd_processing_task_lock);
1139                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1140                                LPD64")\n",
1141                                obd->obd_next_recovery_transno,
1142                                lustre_msg_get_transno(req->rq_reqmsg));
1143                         l_wait_event(obd->obd_next_transno_waitq,
1144                                      check_for_next_transno(obd), &lwi);
1145                         spin_lock_bh(&obd->obd_processing_task_lock);
1146                         abort_recovery = obd->obd_abort_recovery;
1147                         spin_unlock_bh(&obd->obd_processing_task_lock);
1148                         if (abort_recovery) {
1149                                 target_abort_recovery(obd);
1150                                 return;
1151                         }
1152                         continue;
1153                 }
1154                 list_del_init(&req->rq_list);
1155                 obd->obd_requests_queued_for_recovery--;
1156                 spin_unlock_bh(&obd->obd_processing_task_lock);
1157
1158                 DEBUG_REQ(D_HA, req, "processing: ");
1159                 (void)obd->obd_recovery_handler(req);
1160                 obd->obd_replayed_requests++;
1161                 reset_recovery_timer(obd);
1162                 /* bug 1580: decide how to properly sync() in recovery */
1163                 //mds_fsync_super(obd->u.obt.obt_sb);
1164                 class_export_put(req->rq_export);
1165                 if (req->rq_reply_state != NULL) {
1166                         ptlrpc_rs_decref(req->rq_reply_state);
1167                         /* req->rq_reply_state = NULL; */
1168                 }
1169                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1170                 OBD_FREE(req, sizeof *req);
1171                 spin_lock_bh(&obd->obd_processing_task_lock);
1172                 obd->obd_next_recovery_transno++;
1173                 if (list_empty(&obd->obd_recovery_queue)) {
1174                         obd->obd_processing_task = 0;
1175                         spin_unlock_bh(&obd->obd_processing_task_lock);
1176                         break;
1177                 }
1178                 spin_unlock_bh(&obd->obd_processing_task_lock);
1179         }
1180         EXIT;
1181 }
1182
1183 int target_queue_recovery_request(struct ptlrpc_request *req,
1184                                   struct obd_device *obd)
1185 {
1186         struct list_head *tmp;
1187         int inserted = 0;
1188         __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1189         struct ptlrpc_request *saved_req;
1190         struct lustre_msg *reqmsg;
1191
1192         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1193          * (i.e. buflens etc are in my own byte order), but type-dependent
1194          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1195
1196         if (!transno) {
1197                 CFS_INIT_LIST_HEAD(&req->rq_list);
1198                 DEBUG_REQ(D_HA, req, "not queueing");
1199                 return 1;
1200         }
1201
1202         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1203         /* XXX just like the request-dup code in queue_final_reply */
1204         OBD_ALLOC(saved_req, sizeof *saved_req);
1205         if (!saved_req)
1206                 LBUG();
1207         OBD_ALLOC(reqmsg, req->rq_reqlen);
1208         if (!reqmsg)
1209                 LBUG();
1210
1211         spin_lock_bh(&obd->obd_processing_task_lock);
1212
1213         /* If we're processing the queue, we want don't want to queue this
1214          * message.
1215          *
1216          * Also, if this request has a transno less than the one we're waiting
1217          * for, we should process it now.  It could (and currently always will)
1218          * be an open request for a descriptor that was opened some time ago.
1219          *
1220          * Also, a resent, replayed request that has already been
1221          * handled will pass through here and be processed immediately.
1222          */
1223         if (obd->obd_processing_task == cfs_curproc_pid() ||
1224             transno < obd->obd_next_recovery_transno) {
1225                 /* Processing the queue right now, don't re-add. */
1226                 LASSERT(list_empty(&req->rq_list));
1227                 spin_unlock_bh(&obd->obd_processing_task_lock);
1228                 OBD_FREE(reqmsg, req->rq_reqlen);
1229                 OBD_FREE(saved_req, sizeof *saved_req);
1230                 return 1;
1231         }
1232
1233         /* A resent, replayed request that is still on the queue; just drop it.
1234            The queued request will handle this. */
1235         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1236             (MSG_RESENT | MSG_REPLAY)) {
1237                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1238                 spin_unlock_bh(&obd->obd_processing_task_lock);
1239                 OBD_FREE(reqmsg, req->rq_reqlen);
1240                 OBD_FREE(saved_req, sizeof *saved_req);
1241                 return 0;
1242         }
1243
1244         memcpy(saved_req, req, sizeof *req);
1245         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1246         req = saved_req;
1247         req->rq_reqmsg = reqmsg;
1248         class_export_get(req->rq_export);
1249         CFS_INIT_LIST_HEAD(&req->rq_list);
1250
1251         /* XXX O(n^2) */
1252         list_for_each(tmp, &obd->obd_recovery_queue) {
1253                 struct ptlrpc_request *reqiter =
1254                         list_entry(tmp, struct ptlrpc_request, rq_list);
1255
1256                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1257                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1258                         inserted = 1;
1259                         break;
1260                 }
1261         }
1262
1263         if (!inserted) {
1264                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1265         }
1266
1267         obd->obd_requests_queued_for_recovery++;
1268
1269         if (obd->obd_processing_task != 0) {
1270                 /* Someone else is processing this queue, we'll leave it to
1271                  * them.
1272                  */
1273                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1274                 spin_unlock_bh(&obd->obd_processing_task_lock);
1275                 return 0;
1276         }
1277
1278         /* Nobody is processing, and we know there's (at least) one to process
1279          * now, so we'll do the honours.
1280          */
1281         obd->obd_processing_task = cfs_curproc_pid();
1282         spin_unlock_bh(&obd->obd_processing_task_lock);
1283
1284         process_recovery_queue(obd);
1285         return 0;
1286 }
1287
1288 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1289 {
1290         return req->rq_export->exp_obd;
1291 }
1292
1293 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1294 {
1295         struct obd_device *obd = target_req2obd(req);
1296         struct ptlrpc_request *saved_req;
1297         struct lustre_msg *reqmsg;
1298         int recovery_done = 0;
1299
1300         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1301
1302         if (rc) {
1303                 /* Just like ptlrpc_error, but without the sending. */
1304                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1305                 LASSERT(rc == 0); /* XXX handle this */
1306                 req->rq_type = PTL_RPC_MSG_ERR;
1307         }
1308
1309         LASSERT (!req->rq_reply_state->rs_difficult);
1310         LASSERT(list_empty(&req->rq_list));
1311         /* XXX a bit like the request-dup code in queue_recovery_request */
1312         OBD_ALLOC(saved_req, sizeof *saved_req);
1313         if (!saved_req)
1314                 LBUG();
1315         OBD_ALLOC(reqmsg, req->rq_reqlen);
1316         if (!reqmsg)
1317                 LBUG();
1318         *saved_req = *req;
1319         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1320
1321         /* Don't race cleanup */
1322         spin_lock_bh(&obd->obd_processing_task_lock);
1323         if (obd->obd_stopping) {
1324                 spin_unlock_bh(&obd->obd_processing_task_lock);
1325                 OBD_FREE(reqmsg, req->rq_reqlen);
1326                 OBD_FREE(saved_req, sizeof *req);
1327                 req->rq_status = -ENOTCONN;
1328                 /* rv is ignored anyhow */
1329                 return -ENOTCONN;
1330         }
1331         ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1332         req = saved_req;
1333         req->rq_reqmsg = reqmsg;
1334         class_export_get(req->rq_export);
1335         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1336
1337         /* only count the first "replay over" request from each
1338            export */
1339         if (req->rq_export->exp_replay_needed) {
1340                 --obd->obd_recoverable_clients;
1341                 req->rq_export->exp_replay_needed = 0;
1342         }
1343         recovery_done = (obd->obd_recoverable_clients == 0);
1344         spin_unlock_bh(&obd->obd_processing_task_lock);
1345
1346         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1347         if (recovery_done) {
1348                 spin_lock_bh(&obd->obd_processing_task_lock);
1349                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1350                 target_cancel_recovery_timer(obd);
1351                 spin_unlock_bh(&obd->obd_processing_task_lock);
1352
1353                 target_finish_recovery(obd);
1354                 CDEBUG(D_HA, "%s: recovery complete\n",
1355                        obd_uuid2str(&obd->obd_uuid));
1356         } else {
1357                 CWARN("%s: %d recoverable clients remain\n",
1358                        obd->obd_name, obd->obd_recoverable_clients);
1359                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1360         }
1361
1362         return 1;
1363 }
1364
1365 int
1366 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1367 {
1368         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1369                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1370                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1371                 return (-ECOMM);
1372         }
1373
1374         if (rc) {
1375                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1376                 req->rq_status = rc;
1377                 return (ptlrpc_error(req));
1378         } else {
1379                 DEBUG_REQ(D_NET, req, "sending reply");
1380         }
1381
1382         return (ptlrpc_send_reply(req, 1));
1383 }
1384
1385 void
1386 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1387 {
1388         int                        netrc;
1389         struct ptlrpc_reply_state *rs;
1390         struct obd_device         *obd;
1391         struct obd_export         *exp;
1392         struct ptlrpc_service     *svc;
1393
1394         svc = req->rq_rqbd->rqbd_service;
1395         rs = req->rq_reply_state;
1396         if (rs == NULL || !rs->rs_difficult) {
1397                 /* no notifiers */
1398                 target_send_reply_msg (req, rc, fail_id);
1399                 return;
1400         }
1401
1402         /* must be an export if locks saved */
1403         LASSERT (req->rq_export != NULL);
1404         /* req/reply consistent */
1405         LASSERT (rs->rs_service == svc);
1406
1407         /* "fresh" reply */
1408         LASSERT (!rs->rs_scheduled);
1409         LASSERT (!rs->rs_scheduled_ever);
1410         LASSERT (!rs->rs_handled);
1411         LASSERT (!rs->rs_on_net);
1412         LASSERT (rs->rs_export == NULL);
1413         LASSERT (list_empty(&rs->rs_obd_list));
1414         LASSERT (list_empty(&rs->rs_exp_list));
1415
1416         exp = class_export_get (req->rq_export);
1417         obd = exp->exp_obd;
1418
1419         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1420         rs->rs_scheduled = 1;
1421         rs->rs_on_net    = 1;
1422         rs->rs_xid       = req->rq_xid;
1423         rs->rs_transno   = req->rq_transno;
1424         rs->rs_export    = exp;
1425
1426         spin_lock(&obd->obd_uncommitted_replies_lock);
1427
1428         if (rs->rs_transno > obd->obd_last_committed) {
1429                 /* not committed already */
1430                 list_add_tail (&rs->rs_obd_list,
1431                                &obd->obd_uncommitted_replies);
1432         }
1433
1434         spin_unlock (&obd->obd_uncommitted_replies_lock);
1435         spin_lock (&exp->exp_lock);
1436
1437         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1438
1439         spin_unlock(&exp->exp_lock);
1440
1441         netrc = target_send_reply_msg (req, rc, fail_id);
1442
1443         spin_lock(&svc->srv_lock);
1444
1445         svc->srv_n_difficult_replies++;
1446
1447         if (netrc != 0) {
1448                 /* error sending: reply is off the net.  Also we need +1
1449                  * reply ref until ptlrpc_server_handle_reply() is done
1450                  * with the reply state (if the send was successful, there
1451                  * would have been +1 ref for the net, which
1452                  * reply_out_callback leaves alone) */
1453                 rs->rs_on_net = 0;
1454                 ptlrpc_rs_addref(rs);
1455                 atomic_inc (&svc->srv_outstanding_replies);
1456         }
1457
1458         if (!rs->rs_on_net ||                   /* some notifier */
1459             list_empty(&rs->rs_exp_list) ||     /* completed already */
1460             list_empty(&rs->rs_obd_list)) {
1461                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1462                 cfs_waitq_signal (&svc->srv_waitq);
1463         } else {
1464                 list_add (&rs->rs_list, &svc->srv_active_replies);
1465                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1466         }
1467
1468         spin_unlock(&svc->srv_lock);
1469 }
1470
1471 int target_handle_ping(struct ptlrpc_request *req)
1472 {
1473         obd_ping(req->rq_export);
1474         return lustre_pack_reply(req, 1, NULL, NULL);
1475 }
1476
1477 void target_committed_to_req(struct ptlrpc_request *req)
1478 {
1479         struct obd_device *obd = req->rq_export->exp_obd;
1480
1481         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1482                 lustre_msg_set_last_committed(req->rq_repmsg,
1483                                               obd->obd_last_committed);
1484         else
1485                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1486
1487         CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1488                obd->obd_last_committed, req->rq_xid);
1489 }
1490
1491 EXPORT_SYMBOL(target_committed_to_req);
1492
1493 #ifdef HAVE_QUOTA_SUPPORT
1494 int target_handle_qc_callback(struct ptlrpc_request *req)
1495 {
1496         struct obd_quotactl *oqctl;
1497         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1498
1499         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1500                                    lustre_swab_obd_quotactl);
1501         if (oqctl == NULL) {
1502                 CERROR("Can't unpack obd_quotactl\n");
1503                 RETURN(-EPROTO);
1504         }
1505
1506         cli->cl_qchk_stat = oqctl->qc_stat;
1507
1508         return 0;
1509 }
1510
1511 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1512 {
1513 #ifdef __KERNEL__
1514         struct obd_device *obd = req->rq_export->exp_obd;
1515         struct obd_device *master_obd;
1516         struct lustre_quota_ctxt *qctxt;
1517         struct qunit_data *qdata;
1518         void* rep;
1519         struct qunit_data_old *qdata_old;
1520         int rc = 0;
1521         int repsize[2] = { sizeof(struct ptlrpc_body),
1522                            sizeof(struct qunit_data) };
1523         ENTRY;
1524         
1525         rc = lustre_pack_reply(req, 2, repsize, NULL);
1526         if (rc) {
1527                 CERROR("packing reply failed!: rc = %d\n", rc);
1528                 RETURN(rc);
1529         }
1530         LASSERT(req->rq_export);
1531
1532         /* fixed for bug10707 */
1533         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1534             !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1535                 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
1536                 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, 
1537                                      sizeof(struct qunit_data));
1538                 LASSERT(rep);
1539                 qdata = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata), 
1540                                            lustre_swab_qdata);
1541         } else {
1542                 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
1543                 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, 
1544                                      sizeof(struct qunit_data_old));
1545                 LASSERT(rep);
1546                 qdata_old = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata_old), 
1547                                                lustre_swab_qdata_old);
1548                 qdata = lustre_quota_old_to_new(qdata_old);
1549         }
1550
1551         if (qdata == NULL) {
1552                 CERROR("Can't unpack qunit_data\n");
1553                 RETURN(-EPROTO);
1554         }
1555
1556         /* we use the observer */
1557         LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1558         master_obd = obd->obd_observer->obd_observer;
1559         qctxt = &master_obd->u.obt.obt_qctxt;
1560         
1561         LASSERT(qctxt->lqc_handler);
1562         rc = qctxt->lqc_handler(master_obd, qdata,
1563                                 lustre_msg_get_opc(req->rq_reqmsg));
1564         if (rc && rc != -EDQUOT)
1565                 CDEBUG(rc == -EBUSY  ? D_QUOTA : D_ERROR, 
1566                        "dqacq failed! (rc:%d)\n", rc);
1567         
1568         /* the qd_count might be changed in lqc_handler */
1569         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1570             !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1571                 memcpy(rep,qdata,sizeof(*qdata));
1572         } else {
1573                 qdata_old = lustre_quota_new_to_old(qdata);
1574                 memcpy(rep,qdata_old,sizeof(*qdata_old));
1575         }
1576         req->rq_status = rc;
1577         rc = ptlrpc_reply(req);
1578         
1579         RETURN(rc);     
1580 #else
1581         return 0;
1582 #endif /* !__KERNEL__ */
1583 }
1584 #endif /* HAVE_QUOTA_SUPPORT */
1585
1586 ldlm_mode_t lck_compat_array[] = {
1587         [LCK_EX] LCK_COMPAT_EX,
1588         [LCK_PW] LCK_COMPAT_PW,
1589         [LCK_PR] LCK_COMPAT_PR,
1590         [LCK_CW] LCK_COMPAT_CW,
1591         [LCK_CR] LCK_COMPAT_CR,
1592         [LCK_NL] LCK_COMPAT_NL,
1593         [LCK_GROUP] LCK_COMPAT_GROUP
1594 };