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