Whamcloud - gitweb
Introduction of lu_env.
[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 <lustre_sec.h>
40 #include <lustre_ver.h>
41
42 /* @priority: if non-zero, move the selected to the list head
43  * @create: if zero, only search in existed connections
44  */
45 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
46                            int priority, int create)
47 {
48         struct ptlrpc_connection *ptlrpc_conn;
49         struct obd_import_conn *imp_conn = NULL, *item;
50         int rc = 0;
51         ENTRY;
52
53         if (!create && !priority) {
54                 CDEBUG(D_HA, "Nothing to do\n");
55                 RETURN(-EINVAL);
56         }
57
58         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
59         if (!ptlrpc_conn) {
60                 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
61                 RETURN (-ENOENT);
62         }
63
64         if (create) {
65                 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
66                 if (!imp_conn) {
67                         GOTO(out_put, rc = -ENOMEM);
68                 }
69         }
70
71         spin_lock(&imp->imp_lock);
72         list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
73                 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
74                         if (priority) {
75                                 list_del(&item->oic_item);
76                                 list_add(&item->oic_item, &imp->imp_conn_list);
77                                 item->oic_last_attempt = 0;
78                         }
79                         CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
80                                imp, imp->imp_obd->obd_name, uuid->uuid,
81                                (priority ? ", moved to head" : ""));
82                         spin_unlock(&imp->imp_lock);
83                         GOTO(out_free, rc = 0);
84                 }
85         }
86         /* not found */
87         if (create) {
88                 imp_conn->oic_conn = ptlrpc_conn;
89                 imp_conn->oic_uuid = *uuid;
90                 item->oic_last_attempt = 0;
91                 if (priority)
92                         list_add(&imp_conn->oic_item, &imp->imp_conn_list);
93                 else
94                         list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
95                 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
96                        imp, imp->imp_obd->obd_name, uuid->uuid,
97                        (priority ? "head" : "tail"));
98         } else {
99                 spin_unlock(&imp->imp_lock);
100                 GOTO(out_free, rc = -ENOENT);
101
102         }
103
104         spin_unlock(&imp->imp_lock);
105         RETURN(0);
106 out_free:
107         if (imp_conn)
108                 OBD_FREE(imp_conn, sizeof(*imp_conn));
109 out_put:
110         ptlrpc_put_connection(ptlrpc_conn);
111         RETURN(rc);
112 }
113
114 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
115 {
116         return import_set_conn(imp, uuid, 1, 0);
117 }
118
119 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
120                            int priority)
121 {
122         return import_set_conn(imp, uuid, priority, 1);
123 }
124
125 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
126 {
127         struct obd_import_conn *imp_conn;
128         struct obd_export *dlmexp;
129         int rc = -ENOENT;
130         ENTRY;
131
132         spin_lock(&imp->imp_lock);
133         if (list_empty(&imp->imp_conn_list)) {
134                 LASSERT(!imp->imp_connection);
135                 GOTO(out, rc);
136         }
137
138         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
139                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
140                         continue;
141                 LASSERT(imp_conn->oic_conn);
142
143                 /* is current conn? */
144                 if (imp_conn == imp->imp_conn_current) {
145                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
146
147                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
148                             imp->imp_state != LUSTRE_IMP_DISCON) {
149                                 CERROR("can't remove current connection\n");
150                                 GOTO(out, rc = -EBUSY);
151                         }
152
153                         ptlrpc_put_connection(imp->imp_connection);
154                         imp->imp_connection = NULL;
155
156                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
157                         if (dlmexp && dlmexp->exp_connection) {
158                                 LASSERT(dlmexp->exp_connection ==
159                                         imp_conn->oic_conn);
160                                 ptlrpc_put_connection(dlmexp->exp_connection);
161                                 dlmexp->exp_connection = NULL;
162                         }
163                 }
164
165                 list_del(&imp_conn->oic_item);
166                 ptlrpc_put_connection(imp_conn->oic_conn);
167                 OBD_FREE(imp_conn, sizeof(*imp_conn));
168                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
169                        imp, imp->imp_obd->obd_name, uuid->uuid);
170                 rc = 0;
171                 break;
172         }
173 out:
174         spin_unlock(&imp->imp_lock);
175         if (rc == -ENOENT)
176                 CERROR("connection %s not found\n", uuid->uuid);
177         RETURN(rc);
178 }
179
180 static
181 void destroy_import(struct obd_import *imp)
182 {
183         /* drop security policy instance after all rpc finished/aborted
184          * to let all busy credentials be released.
185          */
186         class_import_get(imp);
187         class_destroy_import(imp);
188         sptlrpc_import_put_sec(imp);
189         class_import_put(imp);
190 }
191
192 /* configure an RPC client OBD device
193  *
194  * lcfg parameters:
195  * 1 - client UUID
196  * 2 - server UUID
197  * 3 - inactive-on-startup
198  */
199 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
200 {
201         struct client_obd *cli = &obddev->u.cli;
202         struct obd_import *imp;
203         struct obd_uuid server_uuid;
204         int rq_portal, rp_portal, connect_op;
205         char *name = obddev->obd_type->typ_name;
206         int rc;
207         ENTRY;
208
209         /* In a more perfect world, we would hang a ptlrpc_client off of
210          * obd_type and just use the values from there. */
211         if (!strcmp(name, LUSTRE_OSC_NAME)) {
212                 rq_portal = OST_IO_PORTAL;
213                 rp_portal = OSC_REPLY_PORTAL;
214                 connect_op = OST_CONNECT;
215         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
216                 rq_portal = MDS_REQUEST_PORTAL;
217                 rp_portal = MDC_REPLY_PORTAL;
218                 connect_op = MDS_CONNECT;
219         } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
220                 rq_portal = MGS_REQUEST_PORTAL;
221                 rp_portal = MGC_REPLY_PORTAL;
222                 connect_op = MGS_CONNECT;
223         } else {
224                 CERROR("unknown client OBD type \"%s\", can't setup\n",
225                        name);
226                 RETURN(-EINVAL);
227         }
228
229         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
230                 CERROR("requires a TARGET UUID\n");
231                 RETURN(-EINVAL);
232         }
233
234         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
235                 CERROR("client UUID must be less than 38 characters\n");
236                 RETURN(-EINVAL);
237         }
238
239         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
240                 CERROR("setup requires a SERVER UUID\n");
241                 RETURN(-EINVAL);
242         }
243
244         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
245                 CERROR("target UUID must be less than 38 characters\n");
246                 RETURN(-EINVAL);
247         }
248
249         sema_init(&cli->cl_sem, 1);
250         sema_init(&cli->cl_mgc_sem, 1);
251         cli->cl_sec_conf.sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
252         cli->cl_sec_conf.sfc_bulk_csum = BULK_CSUM_ALG_NULL;
253         cli->cl_sec_conf.sfc_bulk_priv = BULK_PRIV_ALG_NULL;
254         cli->cl_sec_conf.sfc_flags = 0;
255         cli->cl_conn_count = 0;
256         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
257                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
258                      sizeof(server_uuid)));
259
260         cli->cl_dirty = 0;
261         cli->cl_avail_grant = 0;
262         /* FIXME: should limit this for the sum of all cl_dirty_max */
263         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
264         if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
265                 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
266         CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
267         CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
268         CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
269         CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
270         client_obd_list_lock_init(&cli->cl_loi_list_lock);
271         cli->cl_r_in_flight = 0;
272         cli->cl_w_in_flight = 0;
273
274         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
275         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
276         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
277         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
278         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
279         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
280         /*
281             cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
282             This filed value is seted at connect time by ocd_brw_size.
283         */
284         if (!strcmp(name, LUSTRE_MDC_NAME)) {
285                 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
286         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 128 /* MB */) {
287                 cli->cl_max_rpcs_in_flight = 2;
288         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 256 /* MB */) {
289                 cli->cl_max_rpcs_in_flight = 3;
290         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512 /* MB */) {
291                 cli->cl_max_rpcs_in_flight = 4;
292         } else {
293                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
294         }
295
296         rc = ldlm_get_ref();
297         if (rc) {
298                 CERROR("ldlm_get_ref failed: %d\n", rc);
299                 GOTO(err, rc);
300         }
301
302         ptlrpc_init_client(rq_portal, rp_portal, name,
303                            &obddev->obd_ldlm_client);
304
305         imp = class_new_import(obddev);
306         if (imp == NULL)
307                 GOTO(err_ldlm, rc = -ENOENT);
308         imp->imp_client = &obddev->obd_ldlm_client;
309         imp->imp_connect_op = connect_op;
310         imp->imp_initial_recov = 1;
311         imp->imp_initial_recov_bk = 0;
312         CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
313         memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
314                LUSTRE_CFG_BUFLEN(lcfg, 1));
315         class_import_put(imp);
316
317         rc = client_import_add_conn(imp, &server_uuid, 1);
318         if (rc) {
319                 CERROR("can't add initial connection\n");
320                 GOTO(err_import, rc);
321         }
322
323         cli->cl_import = imp;
324         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
325         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
326         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
327
328         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
329                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
330                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
331                                name, obddev->obd_name,
332                                cli->cl_target_uuid.uuid);
333                         imp->imp_invalid = 1;
334                 }
335         }
336
337         cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
338
339         RETURN(rc);
340
341 err_import:
342         class_destroy_import(imp);
343 err_ldlm:
344         ldlm_put_ref(0);
345 err:
346         RETURN(rc);
347
348 }
349
350 int client_obd_cleanup(struct obd_device *obddev)
351 {
352         ENTRY;
353         ldlm_put_ref(obddev->obd_force);
354
355         RETURN(0);
356 }
357
358 /* ->o_connect() method for client side (OSC and MDC and MGC) */
359 int client_connect_import(const struct lu_env *env,
360                           struct lustre_handle *dlm_handle,
361                           struct obd_device *obd, struct obd_uuid *cluuid,
362                           struct obd_connect_data *data)
363 {
364         struct client_obd *cli = &obd->u.cli;
365         struct obd_import *imp = cli->cl_import;
366         struct obd_export *exp;
367         struct obd_connect_data *ocd;
368         int rc;
369         ENTRY;
370
371         mutex_down(&cli->cl_sem);
372         rc = class_connect(dlm_handle, obd, cluuid);
373         if (rc)
374                 GOTO(out_sem, rc);
375
376         cli->cl_conn_count++;
377         if (cli->cl_conn_count > 1)
378                 GOTO(out_sem, rc);
379         exp = class_conn2export(dlm_handle);
380
381         if (obd->obd_namespace != NULL)
382                 CERROR("already have namespace!\n");
383         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
384                                                 LDLM_NAMESPACE_CLIENT);
385         if (obd->obd_namespace == NULL)
386                 GOTO(out_disco, rc = -ENOMEM);
387
388         imp->imp_dlm_handle = *dlm_handle;
389         rc = ptlrpc_init_import(imp);
390         if (rc != 0)
391                 GOTO(out_ldlm, rc);
392
393         rc = sptlrpc_import_get_sec(imp, NULL, cli->cl_sec_conf.sfc_rpc_flavor,
394                                     cli->cl_sec_conf.sfc_flags);
395         if (rc)
396                 GOTO(out_ldlm, rc);
397
398         ocd = &imp->imp_connect_data;
399         if (data) {
400                 *ocd = *data;
401                 imp->imp_connect_flags_orig = data->ocd_connect_flags;
402         }
403
404         rc = ptlrpc_connect_import(imp, NULL);
405         if (rc != 0) {
406                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
407                 GOTO(out_ldlm, rc);
408         }
409         LASSERT(exp->exp_connection);
410
411         if (data) {
412                 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
413                         ocd->ocd_connect_flags);
414                 data->ocd_connect_flags = ocd->ocd_connect_flags;
415         }
416
417         ptlrpc_pinger_add_import(imp);
418
419         EXIT;
420
421         if (rc) {
422 out_ldlm:
423                 ldlm_namespace_free(obd->obd_namespace, 0);
424                 obd->obd_namespace = NULL;
425 out_disco:
426                 cli->cl_conn_count--;
427                 class_disconnect(exp);
428         } else {
429                 class_export_put(exp);
430         }
431 out_sem:
432         mutex_up(&cli->cl_sem);
433         return rc;
434 }
435
436 int client_disconnect_export(struct obd_export *exp)
437 {
438         struct obd_device *obd = class_exp2obd(exp);
439         struct client_obd *cli;
440         struct obd_import *imp;
441         int rc = 0, err;
442         ENTRY;
443
444         if (!obd) {
445                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
446                        exp, exp ? exp->exp_handle.h_cookie : -1);
447                 RETURN(-EINVAL);
448         }
449
450         cli = &obd->u.cli;
451         imp = cli->cl_import;
452
453         mutex_down(&cli->cl_sem);
454         if (!cli->cl_conn_count) {
455                 CERROR("disconnecting disconnected device (%s)\n",
456                        obd->obd_name);
457                 GOTO(out_sem, rc = -EINVAL);
458         }
459
460         cli->cl_conn_count--;
461         if (cli->cl_conn_count)
462                 GOTO(out_no_disconnect, rc = 0);
463
464         /* Mark import deactivated now, so we don't try to reconnect if any
465          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
466          * fully deactivate the import, or that would drop all requests. */
467         imp->imp_deactive = 1;
468
469         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
470          * delete it regardless.  (It's safe to delete an import that was
471          * never added.) */
472         (void)ptlrpc_pinger_del_import(imp);
473
474         if (obd->obd_namespace != NULL) {
475                 /* obd_no_recov == local only */
476                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
477                                        obd->obd_no_recov ? LDLM_FL_LOCAL_ONLY:0,
478                                        NULL);
479                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
480                 obd->obd_namespace = NULL;
481         }
482
483         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
484         if (!obd->obd_no_recov)
485                 rc = ptlrpc_disconnect_import(imp);
486
487         ptlrpc_invalidate_import(imp);
488         ptlrpc_free_rq_pool(imp->imp_rq_pool);
489         destroy_import(imp);
490         cli->cl_import = NULL;
491
492         EXIT;
493  out_no_disconnect:
494         err = class_disconnect(exp);
495         if (!rc && err)
496                 rc = err;
497  out_sem:
498         mutex_up(&cli->cl_sem);
499         RETURN(rc);
500 }
501
502 /* --------------------------------------------------------------------------
503  * from old lib/target.c
504  * -------------------------------------------------------------------------- */
505
506 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
507                             struct obd_uuid *cluuid)
508 {
509         ENTRY;
510         if (exp->exp_connection && exp->exp_imp_reverse) {
511                 struct lustre_handle *hdl;
512                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
513                 /* Might be a re-connect after a partition. */
514                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
515                         CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
516                               cluuid->uuid);
517                         conn->cookie = exp->exp_handle.h_cookie;
518                         /* target_handle_connect() treats EALREADY and
519                          * -EALREADY differently.  EALREADY means we are
520                          * doing a valid reconnect from the same client. */
521                         RETURN(EALREADY);
522                 } else {
523                         CERROR("%s reconnecting from %s, "
524                                "handle mismatch (ours "LPX64", theirs "
525                                LPX64")\n", cluuid->uuid,
526                                exp->exp_connection->c_remote_uuid.uuid,
527                                hdl->cookie, conn->cookie);
528                         memset(conn, 0, sizeof *conn);
529                         /* target_handle_connect() treats EALREADY and
530                          * -EALREADY differently.  -EALREADY is an error
531                          * (same UUID, different handle). */
532                         RETURN(-EALREADY);
533                 }
534         }
535
536         conn->cookie = exp->exp_handle.h_cookie;
537         CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
538                cluuid->uuid, exp, conn->cookie);
539         RETURN(0);
540 }
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");
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                 DEBUG_REQ(D_ERROR, req, "UUID '%s' is not available "
582                           " for connect (%s)", str,
583                           !target ? "no target" :
584                           (target->obd_stopping ? "stopping" : "not set up"));
585                 GOTO(out, rc = -ENODEV);
586         }
587
588         /* Make sure the target isn't cleaned up while we're here. Yes,
589            there's still a race between the above check and our incref here.
590            Really, class_uuid2obd should take the ref. */
591         targref = class_incref(target);
592
593         LASSERT_REQSWAB(req, REQ_REC_OFF + 1);
594         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
595                                 sizeof(cluuid) - 1);
596         if (str == NULL) {
597                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
598                 GOTO(out, rc = -EINVAL);
599         }
600
601         obd_str2uuid (&cluuid, str);
602
603         /* XXX extract a nettype and format accordingly */
604         switch (sizeof(lnet_nid_t)) {
605                 /* NB the casts only avoid compiler warnings */
606         case 8:
607                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
608                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
609                 break;
610         case 4:
611                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
612                          "NET_%x_UUID", (__u32)req->rq_peer.nid);
613                 break;
614         default:
615                 LBUG();
616         }
617
618         spin_lock_bh(&target->obd_processing_task_lock);
619         abort_recovery = target->obd_abort_recovery;
620         spin_unlock_bh(&target->obd_processing_task_lock);
621         if (abort_recovery)
622                 target_abort_recovery(target);
623
624         tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
625         if (tmp == NULL)
626                 GOTO(out, rc = -EPROTO);
627
628         memcpy(&conn, tmp, sizeof conn);
629
630         data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
631                                   lustre_swab_connect);
632         rc = lustre_pack_reply(req, 2, size, NULL);
633         if (rc)
634                 GOTO(out, rc);
635
636         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
637                 if (!data) {
638                         DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
639                                   "libclient connection attempt\n");
640                         GOTO(out, rc = -EPROTO);
641                 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
642                                                LUSTRE_VERSION_ALLOWED_OFFSET) {
643                         DEBUG_REQ(D_WARNING, req, "Refusing old (%d.%d.%d.%d) "
644                                   "libclient connection attempt\n",
645                                   OBD_OCD_VERSION_MAJOR(data->ocd_version),
646                                   OBD_OCD_VERSION_MINOR(data->ocd_version),
647                                   OBD_OCD_VERSION_PATCH(data->ocd_version),
648                                   OBD_OCD_VERSION_FIX(data->ocd_version));
649                         data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
650                                               offsetof(typeof(*data),
651                                                        ocd_version) +
652                                               sizeof(data->ocd_version));
653                         if (data) {
654                                 data->ocd_connect_flags = OBD_CONNECT_VERSION;
655                                 data->ocd_version = LUSTRE_VERSION_CODE;
656                         }
657                         GOTO(out, rc = -EPROTO);
658                 }
659         }
660
661         /* lctl gets a backstage, all-access pass. */
662         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
663                 goto dont_check_exports;
664
665         spin_lock(&target->obd_dev_lock);
666         list_for_each(p, &target->obd_exports) {
667                 export = list_entry(p, struct obd_export, exp_obd_chain);
668                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
669                         if (export->exp_connecting) { /* bug 9635, et. al. */
670                                 CWARN("%s: exp %p already connecting\n",
671                                       export->exp_obd->obd_name, export);
672                                 export = NULL;
673                                 rc = -EALREADY;
674                                 break;
675                         }
676                         export->exp_connecting = 1;
677                         spin_unlock(&target->obd_dev_lock);
678                         LASSERT(export->exp_obd == target);
679
680                         rc = target_handle_reconnect(&conn, export, &cluuid);
681                         break;
682                 }
683                 export = NULL;
684         }
685         /* If we found an export, we already unlocked. */
686         if (!export) {
687                 spin_unlock(&target->obd_dev_lock);
688                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
689         } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
690                 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
691                        "cookies not random?\n", target->obd_name,
692                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
693                 GOTO(out, rc = -EALREADY);
694         } else {
695                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
696         }
697
698         /* We want to handle EALREADY but *not* -EALREADY from
699          * target_handle_reconnect(), return reconnection state in a flag */
700         if (rc == EALREADY) {
701                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
702                 rc = 0;
703         } else if (rc) {
704                 GOTO(out, rc);
705         }
706
707         /* Tell the client if we're in recovery. */
708         /* If this is the first client, start the recovery timer */
709         if (target->obd_recovering) {
710                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
711                 target_start_recovery_timer(target, handler);
712         }
713
714         /* Tell the client if we support replayable requests */
715         if (target->obd_replayable)
716                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
717
718         if (export == NULL) {
719                 if (target->obd_recovering) {
720                         CERROR("%s: denying connection for new client %s (%s): "
721                                "%d clients in recovery for %lds\n",
722                                target->obd_name,
723                                libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
724                                target->obd_recoverable_clients,
725                                cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
726                                                              cfs_time_current())));
727                         rc = -EBUSY;
728                 } else {
729  dont_check_exports:
730                         rc = obd_connect(req->rq_svc_thread->t_env,
731                                          &conn, target, &cluuid, data);
732                 }
733         } else {
734                 rc = obd_reconnect(export, target, &cluuid, data);
735         }
736
737         if (rc)
738                 GOTO(out, rc);
739
740         /* Return only the parts of obd_connect_data that we understand, so the
741          * client knows that we don't understand the rest. */
742         if (data)
743                 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
744                                       sizeof(*data)),
745                        data, sizeof(*data));
746
747         /* If all else goes well, this is our RPC return code. */
748         req->rq_status = 0;
749
750         lustre_msg_set_handle(req->rq_repmsg, &conn);
751
752         /* ownership of this export ref transfers to the request AFTER we
753          * drop any previous reference the request had, but we don't want
754          * that to go to zero before we get our new export reference. */
755         export = class_conn2export(&conn);
756         if (!export) {
757                 DEBUG_REQ(D_ERROR, req, "Missing export!\n");
758                 GOTO(out, rc = -ENODEV);
759         }
760
761         /* If the client and the server are the same node, we will already
762          * have an export that really points to the client's DLM export,
763          * because we have a shared handles table.
764          *
765          * XXX this will go away when shaver stops sending the "connect" handle
766          * in the real "remote handle" field of the request --phik 24 Apr 2003
767          */
768         if (req->rq_export != NULL)
769                 class_export_put(req->rq_export);
770
771         req->rq_export = export;
772
773         spin_lock(&export->exp_lock);
774         if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
775                 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
776                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
777                        export->exp_conn_cnt,
778                        lustre_msg_get_conn_cnt(req->rq_reqmsg));
779
780                 spin_unlock(&export->exp_lock);
781                 GOTO(out, rc = -EALREADY);
782         }
783         export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
784         spin_unlock(&export->exp_lock);
785
786         /* request from liblustre?  Don't evict it for not pinging. */
787         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
788                 export->exp_libclient = 1;
789                 spin_lock(&target->obd_dev_lock);
790                 list_del_init(&export->exp_obd_chain_timed);
791                 spin_unlock(&target->obd_dev_lock);
792         }
793
794         if (export->exp_connection != NULL)
795                 ptlrpc_put_connection(export->exp_connection);
796         export->exp_connection = ptlrpc_get_connection(req->rq_peer,
797                                                        req->rq_self,
798                                                        &remote_uuid);
799
800         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
801                 LASSERT(export->exp_imp_reverse);
802                 sptlrpc_svc_install_rvs_ctx(export->exp_imp_reverse,
803                                             req->rq_svc_ctx);
804                 GOTO(out, rc = 0);
805         }
806
807         if (target->obd_recovering)
808                 target->obd_connected_clients++;
809
810         memcpy(&conn,
811                lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
812                sizeof conn);
813
814         if (export->exp_imp_reverse != NULL)
815                 destroy_import(export->exp_imp_reverse);
816         revimp = export->exp_imp_reverse = class_new_import(target);
817         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
818         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
819         revimp->imp_remote_handle = conn;
820         revimp->imp_dlm_fake = 1;
821         revimp->imp_state = LUSTRE_IMP_FULL;
822
823         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
824                 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
825                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
826         }
827
828         rc = sptlrpc_import_get_sec(revimp, req->rq_svc_ctx,
829                                     req->rq_sec_flavor, 0);
830         if (rc) {
831                 CERROR("Failed to get sec for reverse import: %d\n", rc);
832                 export->exp_imp_reverse = NULL;
833                 class_destroy_import(revimp);
834         }
835
836         class_import_put(revimp);
837 out:
838         if (export)
839                 export->exp_connecting = 0;
840         if (targref)
841                 class_decref(targref);
842         if (rc)
843                 req->rq_status = rc;
844         RETURN(rc);
845 }
846
847 int target_handle_disconnect(struct ptlrpc_request *req)
848 {
849         int rc;
850         ENTRY;
851
852         rc = lustre_pack_reply(req, 1, NULL, NULL);
853         if (rc)
854                 RETURN(rc);
855
856         /* keep the rq_export around so we can send the reply */
857         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
858         RETURN(0);
859 }
860
861 void target_destroy_export(struct obd_export *exp)
862 {
863         /* exports created from last_rcvd data, and "fake"
864            exports created by lctl don't have an import */
865         if (exp->exp_imp_reverse != NULL)
866                 destroy_import(exp->exp_imp_reverse);
867
868         /* We cancel locks at disconnect time, but this will catch any locks
869          * granted in a race with recovery-induced disconnect. */
870         if (exp->exp_obd->obd_namespace != NULL)
871                 ldlm_cancel_locks_for_export(exp);
872 }
873
874 /*
875  * Recovery functions
876  */
877
878
879 static
880 struct ptlrpc_request *target_save_req(struct ptlrpc_request *src)
881 {
882         struct ptlrpc_request *req;
883         struct lustre_msg *reqmsg;
884
885         OBD_ALLOC_PTR(req);
886         if (!req)
887                 return NULL;
888
889         OBD_ALLOC(reqmsg, src->rq_reqlen);
890         if (!reqmsg) {
891                 OBD_FREE_PTR(req);
892                 return NULL;
893         }
894
895         *req = *src;
896         memcpy(reqmsg, src->rq_reqmsg, src->rq_reqlen);
897         req->rq_reqmsg = reqmsg;
898
899         class_export_get(req->rq_export);
900         CFS_INIT_LIST_HEAD(&req->rq_list);
901         sptlrpc_svc_ctx_addref(req);
902         if (req->rq_reply_state)
903                 ptlrpc_rs_addref(req->rq_reply_state);
904
905         /* repmsg have been taken over, in privacy mode this might point to
906          * invalid data. prevent further access on it.
907          */
908         src->rq_repmsg = NULL;
909         src->rq_replen = 0;
910
911         return req;
912 }
913
914 static
915 void target_release_saved_req(struct ptlrpc_request *req)
916 {
917         if (req->rq_reply_state) {
918                 ptlrpc_rs_decref(req->rq_reply_state);
919                 req->rq_reply_state = NULL;
920         }
921         sptlrpc_svc_ctx_decref(req);
922         class_export_put(req->rq_export);
923
924         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
925         OBD_FREE_PTR(req);
926 }
927
928 static void target_finish_recovery(struct obd_device *obd)
929 {
930         struct list_head *tmp, *n;
931
932         CWARN("%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                 CWARN("%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_WARNING, 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                 target_release_saved_req(req);
1181
1182                 spin_lock_bh(&obd->obd_processing_task_lock);
1183                 obd->obd_next_recovery_transno++;
1184                 if (list_empty(&obd->obd_recovery_queue)) {
1185                         obd->obd_processing_task = 0;
1186                         spin_unlock_bh(&obd->obd_processing_task_lock);
1187                         break;
1188                 }
1189                 spin_unlock_bh(&obd->obd_processing_task_lock);
1190         }
1191         EXIT;
1192 }
1193
1194 int target_queue_recovery_request(struct ptlrpc_request *req,
1195                                   struct obd_device *obd)
1196 {
1197         struct list_head *tmp;
1198         int inserted = 0;
1199         __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1200         struct ptlrpc_request *saved_req;
1201
1202         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1203          * (i.e. buflens etc are in my own byte order), but type-dependent
1204          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1205
1206         if (!transno) {
1207                 CFS_INIT_LIST_HEAD(&req->rq_list);
1208                 DEBUG_REQ(D_HA, req, "not queueing");
1209                 return 1;
1210         }
1211
1212         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1213         saved_req = target_save_req(req);
1214         if (!saved_req)
1215                 LBUG();
1216
1217         spin_lock_bh(&obd->obd_processing_task_lock);
1218
1219         /*
1220          * If we're processing the queue, we don't want to queue this message.
1221          *
1222          * Also, if this request has a transno less than the one we're waiting
1223          * for, we should process it now.  It could (and currently always will)
1224          * be an open request for a descriptor that was opened some time ago.
1225          *
1226          * Also, a resent, replayed request that has already been handled will
1227          * pass through here and be processed immediately.
1228          */
1229         if (obd->obd_processing_task == cfs_curproc_pid() ||
1230             transno < obd->obd_next_recovery_transno) {
1231                 /* Processing the queue right now, don't re-add. */
1232                 LASSERT(list_empty(&req->rq_list));
1233                 spin_unlock_bh(&obd->obd_processing_task_lock);
1234
1235                 target_release_saved_req(saved_req);
1236                 return 1;
1237         }
1238
1239         /* A resent, replayed request that is still on the queue; just drop it.
1240            The queued request will handle this. */
1241         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1242             (MSG_RESENT | MSG_REPLAY)) {
1243                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1244                 spin_unlock_bh(&obd->obd_processing_task_lock);
1245
1246                 target_release_saved_req(saved_req);
1247                 return 0;
1248         }
1249
1250         req = saved_req;
1251
1252         /* XXX O(n^2) */
1253         list_for_each(tmp, &obd->obd_recovery_queue) {
1254                 struct ptlrpc_request *reqiter =
1255                         list_entry(tmp, struct ptlrpc_request, rq_list);
1256
1257                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1258                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1259                         inserted = 1;
1260                         break;
1261                 }
1262         }
1263
1264         if (!inserted) {
1265                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1266         }
1267
1268         obd->obd_requests_queued_for_recovery++;
1269
1270         if (obd->obd_processing_task != 0) {
1271                 /* Someone else is processing this queue, we'll leave it to
1272                  * them.
1273                  */
1274                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1275                 spin_unlock_bh(&obd->obd_processing_task_lock);
1276                 return 0;
1277         }
1278
1279         /* Nobody is processing, and we know there's (at least) one to process
1280          * now, so we'll do the honours.
1281          */
1282         obd->obd_processing_task = cfs_curproc_pid();
1283         spin_unlock_bh(&obd->obd_processing_task_lock);
1284
1285         process_recovery_queue(obd);
1286         return 0;
1287 }
1288
1289 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1290 {
1291         return req->rq_export->exp_obd;
1292 }
1293
1294 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1295 {
1296         struct obd_device *obd = target_req2obd(req);
1297         struct ptlrpc_request *saved_req;
1298         int recovery_done = 0;
1299
1300         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1301
1302         if (rc) {
1303                 /* Just like ptlrpc_error, but without the sending. */
1304                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1305                 LASSERT(rc == 0); /* XXX handle this */
1306                 req->rq_type = PTL_RPC_MSG_ERR;
1307         }
1308
1309         LASSERT (!req->rq_reply_state->rs_difficult);
1310         LASSERT(list_empty(&req->rq_list));
1311
1312         saved_req = target_save_req(req);
1313         if (!saved_req)
1314                 LBUG();
1315
1316         /* Don't race cleanup */
1317         spin_lock_bh(&obd->obd_processing_task_lock);
1318         if (obd->obd_stopping) {
1319                 spin_unlock_bh(&obd->obd_processing_task_lock);
1320                 target_release_saved_req(saved_req);
1321                 req->rq_status = -ENOTCONN;
1322                 /* rv is ignored anyhow */
1323                 return -ENOTCONN;
1324         }
1325
1326         req = saved_req;
1327         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1328
1329         /* only count the first "replay over" request from each
1330            export */
1331         if (req->rq_export->exp_replay_needed) {
1332                 --obd->obd_recoverable_clients;
1333                 req->rq_export->exp_replay_needed = 0;
1334         }
1335         recovery_done = (obd->obd_recoverable_clients == 0);
1336         spin_unlock_bh(&obd->obd_processing_task_lock);
1337
1338         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1339         if (recovery_done) {
1340                 spin_lock_bh(&obd->obd_processing_task_lock);
1341                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1342                 target_cancel_recovery_timer(obd);
1343                 spin_unlock_bh(&obd->obd_processing_task_lock);
1344
1345                 target_finish_recovery(obd);
1346                 CDEBUG(D_HA, "%s: recovery complete\n",
1347                        obd_uuid2str(&obd->obd_uuid));
1348         } else {
1349                 CWARN("%s: %d recoverable clients remain\n",
1350                        obd->obd_name, obd->obd_recoverable_clients);
1351                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1352         }
1353
1354         return 1;
1355 }
1356
1357 int
1358 target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
1359 {
1360         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1361                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1362                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1363                 return (-ECOMM);
1364         }
1365
1366         if (rc || req->rq_reply_state == NULL) {
1367                 if (rc == 0) {
1368                         DEBUG_REQ(D_ERROR, req, "no reply message packed");
1369                         rc = -ENOMEM;
1370                 } else
1371                         DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1372                 req->rq_status = rc;
1373                 return (ptlrpc_error(req));
1374         }
1375
1376         DEBUG_REQ(D_NET, req, "sending reply");
1377         return (ptlrpc_send_reply(req, 1));
1378 }
1379
1380 void
1381 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1382 {
1383         int                        netrc;
1384         struct ptlrpc_reply_state *rs;
1385         struct obd_device         *obd;
1386         struct obd_export         *exp;
1387         struct ptlrpc_service     *svc;
1388
1389         svc = req->rq_rqbd->rqbd_service;
1390         rs = req->rq_reply_state;
1391         if (rs == NULL || !rs->rs_difficult) {
1392                 /* no notifiers */
1393                 target_send_reply_msg(req, rc, fail_id);
1394                 return;
1395         }
1396
1397         /* must be an export if locks saved */
1398         LASSERT (req->rq_export != NULL);
1399         /* req/reply consistent */
1400         LASSERT (rs->rs_service == svc);
1401
1402         /* "fresh" reply */
1403         LASSERT (!rs->rs_scheduled);
1404         LASSERT (!rs->rs_scheduled_ever);
1405         LASSERT (!rs->rs_handled);
1406         LASSERT (!rs->rs_on_net);
1407         LASSERT (rs->rs_export == NULL);
1408         LASSERT (list_empty(&rs->rs_obd_list));
1409         LASSERT (list_empty(&rs->rs_exp_list));
1410
1411         exp = class_export_get (req->rq_export);
1412         obd = exp->exp_obd;
1413
1414         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1415         rs->rs_scheduled = 1;
1416         rs->rs_on_net    = 1;
1417         rs->rs_xid       = req->rq_xid;
1418         rs->rs_transno   = req->rq_transno;
1419         rs->rs_export    = exp;
1420
1421         spin_lock(&obd->obd_uncommitted_replies_lock);
1422
1423         if (rs->rs_transno > obd->obd_last_committed) {
1424                 /* not committed already */
1425                 list_add_tail (&rs->rs_obd_list,
1426                                &obd->obd_uncommitted_replies);
1427         }
1428
1429         spin_unlock (&obd->obd_uncommitted_replies_lock);
1430         spin_lock (&exp->exp_lock);
1431
1432         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1433
1434         spin_unlock(&exp->exp_lock);
1435
1436         netrc = target_send_reply_msg(req, rc, fail_id);
1437
1438         spin_lock(&svc->srv_lock);
1439
1440         svc->srv_n_difficult_replies++;
1441
1442         if (netrc != 0) {
1443                 /* error sending: reply is off the net.  Also we need +1
1444                  * reply ref until ptlrpc_server_handle_reply() is done
1445                  * with the reply state (if the send was successful, there
1446                  * would have been +1 ref for the net, which
1447                  * reply_out_callback leaves alone) */
1448                 rs->rs_on_net = 0;
1449                 ptlrpc_rs_addref(rs);
1450                 atomic_inc (&svc->srv_outstanding_replies);
1451         }
1452
1453         if (!rs->rs_on_net ||                   /* some notifier */
1454             list_empty(&rs->rs_exp_list) ||     /* completed already */
1455             list_empty(&rs->rs_obd_list)) {
1456                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1457                 cfs_waitq_signal (&svc->srv_waitq);
1458         } else {
1459                 list_add (&rs->rs_list, &svc->srv_active_replies);
1460                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1461         }
1462
1463         spin_unlock(&svc->srv_lock);
1464 }
1465
1466 int target_handle_ping(struct ptlrpc_request *req)
1467 {
1468         obd_ping(req->rq_export);
1469         return lustre_pack_reply(req, 1, NULL, NULL);
1470 }
1471
1472 void target_committed_to_req(struct ptlrpc_request *req)
1473 {
1474         struct obd_device *obd;
1475
1476         if (req == NULL || req->rq_export == NULL)
1477                 return;
1478
1479         obd = req->rq_export->exp_obd;
1480         if (obd == NULL)
1481                 return;
1482
1483         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1484                 lustre_msg_set_last_committed(req->rq_repmsg,
1485                                               obd->obd_last_committed);
1486         else
1487                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1488
1489         CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1490                obd->obd_last_committed, req->rq_xid);
1491 }
1492
1493 EXPORT_SYMBOL(target_committed_to_req);
1494
1495 #ifdef HAVE_QUOTA_SUPPORT
1496 int target_handle_qc_callback(struct ptlrpc_request *req)
1497 {
1498         struct obd_quotactl *oqctl;
1499         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1500
1501         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1502                                    lustre_swab_obd_quotactl);
1503         if (oqctl == NULL) {
1504                 CERROR("Can't unpack obd_quotactl\n");
1505                 RETURN(-EPROTO);
1506         }
1507
1508         cli->cl_qchk_stat = oqctl->qc_stat;
1509
1510         return 0;
1511 }
1512
1513 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1514 {
1515 #ifdef __KERNEL__
1516         struct obd_device *obd = req->rq_export->exp_obd;
1517         struct obd_device *master_obd;
1518         struct lustre_quota_ctxt *qctxt;
1519         struct qunit_data *qdata;
1520         void* rep;
1521         struct qunit_data_old *qdata_old;
1522         int rc = 0;
1523         int repsize[2] = { sizeof(struct ptlrpc_body),
1524                            sizeof(struct qunit_data) };
1525         ENTRY;
1526
1527         rc = lustre_pack_reply(req, 2, repsize, NULL);
1528         if (rc) {
1529                 CERROR("packing reply failed!: rc = %d\n", rc);
1530                 RETURN(rc);
1531         }
1532         LASSERT(req->rq_export);
1533
1534         /* fixed for bug10707 */
1535         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1536             !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1537                 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
1538                 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1539                                      sizeof(struct qunit_data));
1540                 LASSERT(rep);
1541                 qdata = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata),
1542                                            lustre_swab_qdata);
1543         } else {
1544                 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
1545                 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1546                                      sizeof(struct qunit_data_old));
1547                 LASSERT(rep);
1548                 qdata_old = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata_old),
1549                                                lustre_swab_qdata_old);
1550                 qdata = lustre_quota_old_to_new(qdata_old);
1551         }
1552
1553         if (qdata == NULL) {
1554                 CERROR("Can't unpack qunit_data\n");
1555                 RETURN(-EPROTO);
1556         }
1557
1558         /* we use the observer */
1559         LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1560         master_obd = obd->obd_observer->obd_observer;
1561         qctxt = &master_obd->u.obt.obt_qctxt;
1562
1563         LASSERT(qctxt->lqc_handler);
1564         rc = qctxt->lqc_handler(master_obd, qdata,
1565                                 lustre_msg_get_opc(req->rq_reqmsg));
1566         if (rc && rc != -EDQUOT)
1567                 CDEBUG(rc == -EBUSY  ? D_QUOTA : D_ERROR,
1568                        "dqacq failed! (rc:%d)\n", rc);
1569
1570         /* the qd_count might be changed in lqc_handler */
1571         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1572             !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1573                 memcpy(rep,qdata,sizeof(*qdata));
1574         } else {
1575                 qdata_old = lustre_quota_new_to_old(qdata);
1576                 memcpy(rep,qdata_old,sizeof(*qdata_old));
1577         }
1578         req->rq_status = rc;
1579         rc = ptlrpc_reply(req);
1580
1581         RETURN(rc);
1582 #else
1583         return 0;
1584 #endif /* !__KERNEL__ */
1585 }
1586 #endif /* HAVE_QUOTA_SUPPORT */
1587
1588 ldlm_mode_t lck_compat_array[] = {
1589         [LCK_EX] LCK_COMPAT_EX,
1590         [LCK_PW] LCK_COMPAT_PW,
1591         [LCK_PR] LCK_COMPAT_PR,
1592         [LCK_CW] LCK_COMPAT_CW,
1593         [LCK_CR] LCK_COMPAT_CR,
1594         [LCK_NL] LCK_COMPAT_NL,
1595         [LCK_GROUP] LCK_COMPAT_GROUP
1596 };