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