Whamcloud - gitweb
�� b=18948
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LDLM
41
42 #ifdef __KERNEL__
43 # include <libcfs/libcfs.h>
44 #else
45 # include <liblustre.h>
46 #endif
47 #include <obd.h>
48 #include <lustre_mds.h>
49 #include <lustre_dlm.h>
50 #include <lustre_net.h>
51 #include "ldlm_internal.h"
52
53 /* @priority: if non-zero, move the selected to the list head
54  * @create: if zero, only search in existed connections
55  */
56 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
57                            int priority, int create)
58 {
59         struct ptlrpc_connection *ptlrpc_conn;
60         struct obd_import_conn *imp_conn = NULL, *item;
61         int rc = 0;
62         ENTRY;
63
64         if (!create && !priority) {
65                 CDEBUG(D_HA, "Nothing to do\n");
66                 RETURN(-EINVAL);
67         }
68
69         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
70         if (!ptlrpc_conn) {
71                 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
72                 RETURN (-ENOENT);
73         }
74
75         if (create) {
76                 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
77                 if (!imp_conn) {
78                         GOTO(out_put, rc = -ENOMEM);
79                 }
80         }
81
82         spin_lock(&imp->imp_lock);
83         list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
84                 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
85                         if (priority) {
86                                 list_del(&item->oic_item);
87                                 list_add(&item->oic_item, &imp->imp_conn_list);
88                                 item->oic_last_attempt = 0;
89                         }
90                         CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
91                                imp, imp->imp_obd->obd_name, uuid->uuid,
92                                (priority ? ", moved to head" : ""));
93                         spin_unlock(&imp->imp_lock);
94                         GOTO(out_free, rc = 0);
95                 }
96         }
97         /* not found */
98         if (create) {
99                 imp_conn->oic_conn = ptlrpc_conn;
100                 imp_conn->oic_uuid = *uuid;
101                 imp_conn->oic_last_attempt = 0;
102                 if (priority)
103                         list_add(&imp_conn->oic_item, &imp->imp_conn_list);
104                 else
105                         list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
106                 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
107                        imp, imp->imp_obd->obd_name, uuid->uuid,
108                        (priority ? "head" : "tail"));
109         } else {
110                 spin_unlock(&imp->imp_lock);
111                 GOTO(out_free, rc = -ENOENT);
112
113         }
114
115         spin_unlock(&imp->imp_lock);
116         RETURN(0);
117 out_free:
118         if (imp_conn)
119                 OBD_FREE(imp_conn, sizeof(*imp_conn));
120 out_put:
121         ptlrpc_connection_put(ptlrpc_conn);
122         RETURN(rc);
123 }
124
125 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
126 {
127         return import_set_conn(imp, uuid, 1, 0);
128 }
129
130 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
131                            int priority)
132 {
133         return import_set_conn(imp, uuid, priority, 1);
134 }
135
136 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
137 {
138         struct obd_import_conn *imp_conn;
139         struct obd_export *dlmexp;
140         int rc = -ENOENT;
141         ENTRY;
142
143         spin_lock(&imp->imp_lock);
144         if (list_empty(&imp->imp_conn_list)) {
145                 LASSERT(!imp->imp_connection);
146                 GOTO(out, rc);
147         }
148
149         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
150                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
151                         continue;
152                 LASSERT(imp_conn->oic_conn);
153
154                 /* is current conn? */
155                 if (imp_conn == imp->imp_conn_current) {
156                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
157
158                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
159                             imp->imp_state != LUSTRE_IMP_DISCON) {
160                                 CERROR("can't remove current connection\n");
161                                 GOTO(out, rc = -EBUSY);
162                         }
163
164                         ptlrpc_connection_put(imp->imp_connection);
165                         imp->imp_connection = NULL;
166
167                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
168                         if (dlmexp && dlmexp->exp_connection) {
169                                 LASSERT(dlmexp->exp_connection ==
170                                         imp_conn->oic_conn);
171                                 ptlrpc_connection_put(dlmexp->exp_connection);
172                                 dlmexp->exp_connection = NULL;
173                         }
174                 }
175
176                 list_del(&imp_conn->oic_item);
177                 ptlrpc_connection_put(imp_conn->oic_conn);
178                 OBD_FREE(imp_conn, sizeof(*imp_conn));
179                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
180                        imp, imp->imp_obd->obd_name, uuid->uuid);
181                 rc = 0;
182                 break;
183         }
184 out:
185         spin_unlock(&imp->imp_lock);
186         if (rc == -ENOENT)
187                 CERROR("connection %s not found\n", uuid->uuid);
188         RETURN(rc);
189 }
190
191 /* configure an RPC client OBD device
192  *
193  * lcfg parameters:
194  * 1 - client UUID
195  * 2 - server UUID
196  * 3 - inactive-on-startup
197  */
198 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
199 {
200         struct lustre_cfg* lcfg = buf;
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_REQUEST_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         init_rwsem(&cli->cl_sem);
250         sema_init(&cli->cl_mgc_sem, 1);
251         cli->cl_conn_count = 0;
252         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
253                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
254                      sizeof(server_uuid)));
255
256         cli->cl_dirty = 0;
257         cli->cl_avail_grant = 0;
258         /* FIXME: should limit this for the sum of all cl_dirty_max */
259         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
260         if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > num_physpages / 8)
261                 cli->cl_dirty_max = num_physpages << (CFS_PAGE_SHIFT - 3);
262         CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
263         CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
264         CFS_INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
265         CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
266         CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
267         client_obd_list_lock_init(&cli->cl_loi_list_lock);
268         cli->cl_r_in_flight = 0;
269         cli->cl_w_in_flight = 0;
270         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
271         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
272         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
273         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
274         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
275         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
276         cfs_waitq_init(&cli->cl_destroy_waitq);
277         atomic_set(&cli->cl_destroy_in_flight, 0);
278 #ifdef ENABLE_CHECKSUM
279         /* Turn on checksumming by default. */
280         cli->cl_checksum = 1;
281         /*
282          * The supported checksum types will be worked out at connect time
283          * Set cl_chksum* to CRC32 for now to avoid returning screwed info
284          * through procfs.
285          */
286         cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
287 #endif
288         atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
289
290         /* This value may be changed at connect time in
291            ptlrpc_connect_interpret. */
292         cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
293                                         (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
294
295         if (!strcmp(name, LUSTRE_MDC_NAME)) {
296                 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
297         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
298                 cli->cl_max_rpcs_in_flight = 2;
299         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
300                 cli->cl_max_rpcs_in_flight = 3;
301         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
302                 cli->cl_max_rpcs_in_flight = 4;
303         } else {
304                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
305         }
306         rc = ldlm_get_ref();
307         if (rc) {
308                 CERROR("ldlm_get_ref failed: %d\n", rc);
309                 GOTO(err, rc);
310         }
311
312         ptlrpc_init_client(rq_portal, rp_portal, name,
313                            &obddev->obd_ldlm_client);
314
315         imp = class_new_import(obddev);
316         if (imp == NULL)
317                 GOTO(err_ldlm, rc = -ENOENT);
318         imp->imp_client = &obddev->obd_ldlm_client;
319         imp->imp_connect_op = connect_op;
320         imp->imp_initial_recov = 1;
321         imp->imp_initial_recov_bk = 0;
322         CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
323         memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
324                LUSTRE_CFG_BUFLEN(lcfg, 1));
325         class_import_put(imp);
326
327         rc = client_import_add_conn(imp, &server_uuid, 1);
328         if (rc) {
329                 CERROR("can't add initial connection\n");
330                 GOTO(err_import, rc);
331         }
332
333         cli->cl_import = imp;
334         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
335         cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
336         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
337
338         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
339                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
340                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
341                                name, obddev->obd_name,
342                                cli->cl_target_uuid.uuid);
343                         spin_lock(&imp->imp_lock);
344                         imp->imp_invalid = 1;
345                         spin_unlock(&imp->imp_lock);
346                 }
347         }
348
349         cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
350
351         RETURN(rc);
352
353 err_import:
354         class_destroy_import(imp);
355 err_ldlm:
356         ldlm_put_ref();
357 err:
358         RETURN(rc);
359
360 }
361
362 int client_obd_cleanup(struct obd_device *obddev)
363 {
364         ENTRY;
365         ldlm_put_ref();
366         RETURN(0);
367 }
368
369 /* ->o_connect() method for client side (OSC and MDC and MGC) */
370 int client_connect_import(struct lustre_handle *dlm_handle,
371                           struct obd_device *obd, struct obd_uuid *cluuid,
372                           struct obd_connect_data *data, void *localdata)
373 {
374         struct client_obd *cli = &obd->u.cli;
375         struct obd_import *imp = cli->cl_import;
376         struct obd_export **exp = localdata;
377         struct obd_connect_data *ocd;
378         struct ldlm_namespace *to_be_freed = NULL;
379         int rc;
380         ENTRY;
381
382         down_write(&cli->cl_sem);
383         if (cli->cl_conn_count > 0)
384                 GOTO(out_sem, rc = -EALREADY);
385         
386         rc = class_connect(dlm_handle, obd, cluuid);
387         if (rc)
388                 GOTO(out_sem, rc);
389
390         cli->cl_conn_count++;
391         *exp = class_conn2export(dlm_handle);
392
393         if (obd->obd_namespace != NULL)
394                 CERROR("already have namespace!\n");
395         obd->obd_namespace = ldlm_namespace_new(obd, obd->obd_name,
396                                                 LDLM_NAMESPACE_CLIENT,
397                                                 LDLM_NAMESPACE_GREEDY);
398         if (obd->obd_namespace == NULL)
399                 GOTO(out_disco, rc = -ENOMEM);
400
401         imp->imp_dlm_handle = *dlm_handle;
402         rc = ptlrpc_init_import(imp);
403         if (rc != 0)
404                 GOTO(out_ldlm, rc);
405
406         ocd = &imp->imp_connect_data;
407         if (data) {
408                 *ocd = *data;
409                 imp->imp_connect_flags_orig = data->ocd_connect_flags;
410         }
411
412         rc = ptlrpc_connect_import(imp, NULL);
413         if (rc != 0) {
414                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
415                 GOTO(out_ldlm, rc);
416         }
417         LASSERT((*exp)->exp_connection);
418
419         if (data) {
420                 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
421                         ocd->ocd_connect_flags);
422                 data->ocd_connect_flags = ocd->ocd_connect_flags;
423         }
424
425         ptlrpc_pinger_add_import(imp);
426         EXIT;
427
428         if (rc) {
429 out_ldlm:
430                 ldlm_namespace_free_prior(obd->obd_namespace, imp, 0);
431                 to_be_freed = obd->obd_namespace;
432                 obd->obd_namespace = NULL;
433 out_disco:
434                 cli->cl_conn_count--;
435                 class_disconnect(*exp);
436                 *exp = NULL;
437         } 
438 out_sem:
439         up_write(&cli->cl_sem);
440         if (to_be_freed)
441                 ldlm_namespace_free_post(to_be_freed);
442         return rc;
443 }
444
445 int client_disconnect_export(struct obd_export *exp)
446 {
447         struct obd_device *obd = class_exp2obd(exp);
448         struct client_obd *cli;
449         struct obd_import *imp;
450         struct ldlm_namespace *to_be_freed = NULL;
451         int rc = 0, err;
452         ENTRY;
453
454         if (!obd) {
455                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
456                        exp, exp ? exp->exp_handle.h_cookie : -1);
457                 RETURN(-EINVAL);
458         }
459
460         cli = &obd->u.cli;
461         imp = cli->cl_import;
462
463         down_write(&cli->cl_sem);
464         if (!cli->cl_conn_count) {
465                 CERROR("disconnecting disconnected device (%s)\n",
466                        obd->obd_name);
467                 GOTO(out_sem, rc = -EINVAL);
468         }
469
470         cli->cl_conn_count--;
471         if (cli->cl_conn_count)
472                 GOTO(out_no_disconnect, rc = 0);
473
474         /* Mark import deactivated now, so we don't try to reconnect if any
475          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
476          * fully deactivate the import, or that would drop all requests. */
477         spin_lock(&imp->imp_lock);
478         imp->imp_deactive = 1;
479         spin_unlock(&imp->imp_lock);
480
481         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
482          * delete it regardless.  (It's safe to delete an import that was
483          * never added.) */
484         (void)ptlrpc_pinger_del_import(imp);
485
486         if (obd->obd_namespace != NULL) {
487                 /* obd_force == local only */
488                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
489                                        obd->obd_force ? LDLM_FL_LOCAL_ONLY:0,
490                                        NULL);
491                 ldlm_namespace_free_prior(obd->obd_namespace, imp,
492                                           obd->obd_force);
493                 to_be_freed = obd->obd_namespace;
494         }
495
496         rc = ptlrpc_disconnect_import(imp, 0);
497
498         ptlrpc_invalidate_import(imp);
499         /* set obd_namespace to NULL only after invalidate, because we can have
500          * some connect requests in flight, and his need store a connect flags
501          * in obd_namespace. bug 14260 */
502         obd->obd_namespace = NULL;
503
504         if (imp->imp_rq_pool) {
505                 ptlrpc_free_rq_pool(imp->imp_rq_pool);
506                 imp->imp_rq_pool = NULL;
507         }
508         class_destroy_import(imp);
509         cli->cl_import = NULL;
510
511         EXIT;
512  out_no_disconnect:
513         err = class_disconnect(exp);
514         if (!rc && err)
515                 rc = err;
516  out_sem:
517         up_write(&cli->cl_sem);
518         if (to_be_freed)
519                 ldlm_namespace_free_post(to_be_freed);
520         RETURN(rc);
521 }
522
523 /* --------------------------------------------------------------------------
524  * from old lib/target.c
525  * -------------------------------------------------------------------------- */
526
527 static int target_handle_reconnect(struct lustre_handle *conn,
528                                    struct obd_export *exp,
529                                    struct obd_uuid *cluuid)
530 {
531         ENTRY;
532         if (exp->exp_connection && exp->exp_imp_reverse) {
533                 struct lustre_handle *hdl;
534                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
535                 /* Might be a re-connect after a partition. */
536                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
537                         CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
538                               cluuid->uuid);
539                         conn->cookie = exp->exp_handle.h_cookie;
540                         /* target_handle_connect() treats EALREADY and
541                          * -EALREADY differently.  EALREADY means we are
542                          * doing a valid reconnect from the same client. */
543                         RETURN(EALREADY);
544                 } else {
545                         CERROR("%s reconnecting from %s, "
546                                "handle mismatch (ours "LPX64", theirs "
547                                LPX64")\n", cluuid->uuid,
548                                exp->exp_connection->c_remote_uuid.uuid,
549                                hdl->cookie, conn->cookie);
550                         memset(conn, 0, sizeof *conn);
551                         /* target_handle_connect() treats EALREADY and
552                          * -EALREADY differently.  -EALREADY is an error
553                          * (same UUID, different handle). */
554                         RETURN(-EALREADY);
555                 }
556         }
557
558         conn->cookie = exp->exp_handle.h_cookie;
559         CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
560                cluuid->uuid, exp, conn->cookie);
561         RETURN(0);
562 }
563
564 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
565                           int error)
566 {
567         struct obd_export *exp = cb_data;
568
569         CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
570                obd->obd_name, exp->exp_client_uuid.uuid);
571
572         spin_lock(&exp->exp_lock);
573         exp->exp_need_sync = 0;
574         spin_unlock(&exp->exp_lock);
575 }
576 EXPORT_SYMBOL(target_client_add_cb);
577
578 static void
579 target_start_and_reset_recovery_timer(struct obd_device *obd,
580                                       svc_handler_t handler,
581                                       struct ptlrpc_request *req,
582                                       int new_client);
583 void target_stop_recovery(void *, int);
584 static void reset_recovery_timer(struct obd_device *obd, int duration,
585                                  int extend);
586 int target_recovery_check_and_stop(struct obd_device *obd)
587 {
588         int abort_recovery = 0;
589
590         spin_lock_bh(&obd->obd_processing_task_lock);
591         abort_recovery = obd->obd_abort_recovery;
592         obd->obd_abort_recovery = 0;
593         spin_unlock_bh(&obd->obd_processing_task_lock);
594         if (!abort_recovery)
595                 return 0;
596         /** check is fs version-capable */
597         if (target_fs_version_capable(obd)) {
598                 class_handle_stale_exports(obd);
599         } else {
600                 CWARN("Versions are not supported by ldiskfs, VBR is OFF\n");
601                 class_disconnect_stale_exports(obd, exp_flags_from_obd(obd));
602         }
603         /* VBR: no clients are remained to replay, stop recovery */
604         spin_lock_bh(&obd->obd_processing_task_lock);
605         if (obd->obd_recovering && obd->obd_recoverable_clients == 0) {
606                 spin_unlock_bh(&obd->obd_processing_task_lock);
607                 target_stop_recovery(obd, 0);
608                 return 1;
609         }
610         /* always check versions now */
611         obd->obd_version_recov = 1;
612         spin_unlock_bh(&obd->obd_processing_task_lock);
613         /* reset timer, recovery will proceed with versions now */
614         reset_recovery_timer(obd, OBD_RECOVERY_FACTOR * obd_timeout, 1);
615         return 0;
616 }
617 EXPORT_SYMBOL(target_recovery_check_and_stop);
618
619 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
620 {
621         struct obd_device *target, *targref = NULL;
622         struct obd_export *export = NULL;
623         struct obd_import *revimp;
624         struct lustre_handle conn;
625         struct obd_uuid tgtuuid;
626         struct obd_uuid cluuid;
627         struct obd_uuid remote_uuid;
628         char *str, *tmp;
629         int rc = 0;
630         struct obd_connect_data *data;
631         __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
632         lnet_nid_t *client_nid = NULL;
633         int mds_conn = 0;
634         ENTRY;
635
636         OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
637
638         lustre_set_req_swabbed(req, REQ_REC_OFF);
639         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
640         if (str == NULL) {
641                 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
642                 GOTO(out, rc = -EINVAL);
643         }
644
645         obd_str2uuid (&tgtuuid, str);
646         target = class_uuid2obd(&tgtuuid);
647         /* COMPAT_146 */
648         /* old (pre 1.6) lustre_process_log tries to connect to mdsname
649            (eg. mdsA) instead of uuid. */
650         if (!target) {
651                 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
652                 target = class_uuid2obd(&tgtuuid);
653         }
654         if (!target)
655                 target = class_name2obd(str);
656         /* end COMPAT_146 */
657
658         if (!target || target->obd_stopping || !target->obd_set_up) {
659                 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
660                                    " for connect (%s)\n", str,
661                                    !target ? "no target" :
662                                    (target->obd_stopping ? "stopping" :
663                                    "not set up"));
664                 GOTO(out, rc = -ENODEV);
665         }
666
667         if (target->obd_no_conn) {
668                 LCONSOLE_WARN("%s: temporarily refusing client connection "
669                               "from %s\n", target->obd_name,
670                               libcfs_nid2str(req->rq_peer.nid));
671                 GOTO(out, rc = -EAGAIN);
672         }
673
674         /* Make sure the target isn't cleaned up while we're here. Yes,
675            there's still a race between the above check and our incref here.
676            Really, class_uuid2obd should take the ref. */
677         targref = class_incref(target);
678
679         lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
680         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
681                                 sizeof(cluuid) - 1);
682         if (str == NULL) {
683                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
684                 GOTO(out, rc = -EINVAL);
685         }
686
687         obd_str2uuid (&cluuid, str);
688
689         /* XXX extract a nettype and format accordingly */
690         switch (sizeof(lnet_nid_t)) {
691                 /* NB the casts only avoid compiler warnings */
692         case 8:
693                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
694                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
695                 break;
696         case 4:
697                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
698                          "NET_%x_UUID", (__u32)req->rq_peer.nid);
699                 break;
700         default:
701                 LBUG();
702         }
703
704         target_recovery_check_and_stop(target);
705
706         tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
707         if (tmp == NULL)
708                 GOTO(out, rc = -EPROTO);
709
710         memcpy(&conn, tmp, sizeof conn);
711
712         data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
713                                   lustre_swab_connect);
714         rc = lustre_pack_reply(req, 2, size, NULL);
715         if (rc)
716                 GOTO(out, rc);
717
718         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
719                 if (!data) {
720                         DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
721                                   "libclient connection attempt");
722                         GOTO(out, rc = -EPROTO);
723                 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
724                                                LUSTRE_VERSION_ALLOWED_OFFSET ||
725                            data->ocd_version > LUSTRE_VERSION_CODE +
726                                                LUSTRE_VERSION_ALLOWED_OFFSET) {
727                         DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
728                                   "libclient connection attempt",
729                                   data->ocd_version < LUSTRE_VERSION_CODE ?
730                                   "old" : "new",
731                                   OBD_OCD_VERSION_MAJOR(data->ocd_version),
732                                   OBD_OCD_VERSION_MINOR(data->ocd_version),
733                                   OBD_OCD_VERSION_PATCH(data->ocd_version),
734                                   OBD_OCD_VERSION_FIX(data->ocd_version));
735                         data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
736                                               offsetof(typeof(*data),
737                                                        ocd_version) +
738                                               sizeof(data->ocd_version));
739                         if (data) {
740                                 data->ocd_connect_flags = OBD_CONNECT_VERSION;
741                                 data->ocd_version = LUSTRE_VERSION_CODE;
742                         }
743                         GOTO(out, rc = -EPROTO);
744                 }
745         }
746
747         if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) &&
748             (data->ocd_connect_flags & OBD_CONNECT_MDS))
749                 mds_conn = 1;
750
751         /* lctl gets a backstage, all-access pass. */
752         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
753                 goto dont_check_exports;
754
755         export = lustre_hash_lookup(target->obd_uuid_hash, &cluuid);
756         if (!export)
757                 goto no_export;
758
759         /* we've found an export in the hash */
760         if (export->exp_connecting) {
761                 /* bug 9635, et. al. */
762                 CWARN("%s: exp %p already connecting\n",
763                       export->exp_obd->obd_name, export);
764                 class_export_put(export);
765                 export = NULL;
766                 rc = -EALREADY;
767         } else if (mds_conn && export->exp_connection) {
768                 if (req->rq_peer.nid != export->exp_connection->c_peer.nid)
769                         /* mds reconnected after failover */
770                         CWARN("%s: received MDS connection from NID %s,"
771                               " removing former export from NID %s\n",
772                             target->obd_name, libcfs_nid2str(req->rq_peer.nid),
773                             libcfs_nid2str(export->exp_connection->c_peer.nid));
774                 else
775                         /* new mds connection from the same nid */
776                         CWARN("%s: received new MDS connection from NID %s,"
777                               " removing former export from same NID\n",
778                             target->obd_name, libcfs_nid2str(req->rq_peer.nid));
779                 class_fail_export(export);
780                 class_export_put(export);
781                 export = NULL;
782                 rc = 0;
783         } else if (export->exp_connection &&
784                    req->rq_peer.nid != export->exp_connection->c_peer.nid) {
785                 CWARN("%s: cookie %s seen on new NID %s when "
786                       "existing NID %s is already connected\n",
787                       target->obd_name, cluuid.uuid,
788                       libcfs_nid2str(req->rq_peer.nid),
789                       libcfs_nid2str(export->exp_connection->c_peer.nid));
790                 rc = -EALREADY;
791                 class_export_put(export);
792                 export = NULL;
793         } else if (export->exp_failed) { /* bug 11327 */
794                 CDEBUG(D_HA, "%s: exp %p evict in progress - new cookie needed "
795                       "for connect\n", export->exp_obd->obd_name, export);
796                 class_export_put(export);
797                 export = NULL;
798                 rc = -ENODEV;
799         } else if (export->exp_delayed &&
800                    !(data && data->ocd_connect_flags & OBD_CONNECT_VBR)) {
801                 class_fail_export(export);
802                 class_export_put(export);
803                 export = NULL;
804                 GOTO(out, rc = -ENODEV);
805         } else {
806                 spin_lock(&export->exp_lock);
807                 export->exp_connecting = 1;
808                 spin_unlock(&export->exp_lock);
809                 class_export_put(export);
810                 LASSERT(export->exp_obd == target);
811
812                 rc = target_handle_reconnect(&conn, export, &cluuid);
813         }
814
815         /* If we found an export, we already unlocked. */
816         if (!export) {
817 no_export:
818                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
819         } else if (req->rq_export == NULL &&
820                    atomic_read(&export->exp_rpc_count) > 0) {
821                 CWARN("%s: refuse connection from %s/%s to 0x%p; still busy "
822                       "with %d references\n", target->obd_name, cluuid.uuid,
823                       libcfs_nid2str(req->rq_peer.nid),
824                       export, atomic_read(&export->exp_refcount));
825                 GOTO(out, rc = -EBUSY);
826         } else if (req->rq_export != NULL &&
827                    atomic_read(&export->exp_rpc_count) > 1) {
828                 /* the current connect rpc has increased exp_rpc_count */
829                 CWARN("%s: refuse reconnection from %s@%s to 0x%p; still busy "
830                       "with %d active RPCs\n", target->obd_name, cluuid.uuid,
831                       libcfs_nid2str(req->rq_peer.nid),
832                       export, atomic_read(&export->exp_rpc_count) - 1);
833                 GOTO(out, rc = -EBUSY);
834         } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
835                 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
836                        "cookies not random?\n", target->obd_name,
837                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
838                 GOTO(out, rc = -EALREADY);
839         } else if (export->exp_delayed && target->obd_recovering) {
840                 /* VBR: don't allow delayed connection during recovery */
841                 CWARN("%s: NID %s (%s) export was already marked as delayed "
842                       "and will wait for end of recovery\n", target->obd_name,
843                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
844                 GOTO(out, rc = -EBUSY);
845         } else {
846                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
847         }
848
849         if (rc < 0)
850                 GOTO(out, rc);
851
852         /* Tell the client if we're in recovery. */
853         if (target->obd_recovering) {
854                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
855                 /* If this is the first time a client connects,
856                    reset the recovery timer */
857                 if (rc == 0)
858                         target_start_and_reset_recovery_timer(target, handler,
859                                                               req, !export);
860         }
861
862         /* We want to handle EALREADY but *not* -EALREADY from
863          * target_handle_reconnect(), return reconnection state in a flag */
864         if (rc == EALREADY) {
865                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
866                 rc = 0;
867         } else {
868                 LASSERT(rc == 0);
869         }
870
871         /* Tell the client if we support replayable requests */
872         if (target->obd_replayable)
873                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
874         client_nid = &req->rq_peer.nid;
875
876         /* VBR: for delayed connections we start recovery */
877         if (export && export->exp_delayed && !export->exp_in_recovery) {
878                 LASSERT(!target->obd_recovering);
879                 LASSERT(data && data->ocd_connect_flags & OBD_CONNECT_VBR);
880                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_DELAYED |
881                                         MSG_CONNECT_RECOVERING);
882                 spin_lock_bh(&target->obd_processing_task_lock);
883                 target->obd_version_recov = 1;
884                 spin_unlock_bh(&target->obd_processing_task_lock);
885                 target_start_and_reset_recovery_timer(target, handler, req, 1);
886         }
887
888         if (export == NULL) {
889                 if (target->obd_recovering) {
890                         CERROR("%s: denying connection for new client %s (%s): "
891                                "%d clients in recovery for %lds\n",
892                                target->obd_name,
893                                libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
894                                target->obd_recoverable_clients,
895                                cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
896                                                              cfs_time_current())));
897                         rc = -EBUSY;
898                 } else {
899  dont_check_exports:
900                         rc = obd_connect(&conn, target, &cluuid, data,
901                                          client_nid);
902                 }
903         } else {
904                 rc = obd_reconnect(export, target, &cluuid, data, client_nid);
905         }
906
907         if (rc)
908                 GOTO(out, rc);
909
910         /* Return only the parts of obd_connect_data that we understand, so the
911          * client knows that we don't understand the rest. */
912         if (data)
913                 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
914                                       sizeof(*data)),
915                        data, sizeof(*data));
916
917         /* If all else goes well, this is our RPC return code. */
918         req->rq_status = 0;
919
920         lustre_msg_set_handle(req->rq_repmsg, &conn);
921
922         /* ownership of this export ref transfers to the request AFTER we
923          * drop any previous reference the request had, but we don't want
924          * that to go to zero before we get our new export reference. */
925         export = class_conn2export(&conn);
926         if (!export) {
927                 DEBUG_REQ(D_ERROR, req, "Missing export!");
928                 GOTO(out, rc = -ENODEV);
929         }
930
931         /* If the client and the server are the same node, we will already
932          * have an export that really points to the client's DLM export,
933          * because we have a shared handles table.
934          *
935          * XXX this will go away when shaver stops sending the "connect" handle
936          * in the real "remote handle" field of the request --phik 24 Apr 2003
937          */
938         if (req->rq_export != NULL)
939                 class_export_put(req->rq_export);
940
941         req->rq_export = export;
942
943         spin_lock(&export->exp_lock);
944         if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
945                 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
946                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
947                        export->exp_conn_cnt,
948                        lustre_msg_get_conn_cnt(req->rq_reqmsg));
949
950                 spin_unlock(&export->exp_lock);
951                 GOTO(out, rc = -EALREADY);
952         }
953         export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
954
955         /* request from liblustre?  Don't evict it for not pinging. */
956         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
957                 export->exp_libclient = 1;
958                 spin_unlock(&export->exp_lock);
959
960                 spin_lock(&target->obd_dev_lock);
961                 list_del_init(&export->exp_obd_chain_timed);
962                 spin_unlock(&target->obd_dev_lock);
963         } else {
964                 spin_unlock(&export->exp_lock);
965         }
966
967         if (export->exp_connection != NULL)
968                 ptlrpc_connection_put(export->exp_connection);
969         export->exp_connection = ptlrpc_connection_get(req->rq_peer,
970                                                        req->rq_self,
971                                                        &remote_uuid);
972
973         if (hlist_unhashed(&export->exp_nid_hash)) {
974                 lustre_hash_add_unique(export->exp_obd->obd_nid_hash,
975                                        &export->exp_connection->c_peer.nid,
976                                        &export->exp_nid_hash);
977         }
978
979         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
980                 revimp = class_import_get(export->exp_imp_reverse);
981                 GOTO(set_flags, rc = 0);
982         }
983
984         if (target->obd_recovering && !export->exp_in_recovery) {
985                 spin_lock(&export->exp_lock);
986                 export->exp_in_recovery = 1;
987                 spin_unlock(&export->exp_lock);
988                 target->obd_connected_clients++;
989         }
990         memcpy(&conn,
991                lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
992                sizeof conn);
993
994         if (export->exp_imp_reverse != NULL)
995                 class_destroy_import(export->exp_imp_reverse);
996         revimp = export->exp_imp_reverse = class_new_import(target);
997         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
998         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
999         revimp->imp_remote_handle = conn;
1000         revimp->imp_dlm_fake = 1;
1001         revimp->imp_state = LUSTRE_IMP_FULL;
1002
1003 set_flags:
1004         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V1 &&
1005             lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
1006                 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
1007                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
1008         } else {
1009                 /* unknown versions will be caught in
1010                  * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
1011                 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
1012         }
1013
1014         if (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1) {
1015                 if (export->exp_connect_flags & OBD_CONNECT_AT)
1016                         revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1017                 else
1018                         revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1019         }
1020
1021         class_import_put(revimp);
1022 out:
1023         if (export) {
1024                 spin_lock(&export->exp_lock);
1025                 export->exp_connecting = 0;
1026                 spin_unlock(&export->exp_lock);
1027         }
1028         if (targref)
1029                 class_decref(targref);
1030         if (rc)
1031                 req->rq_status = rc;
1032         RETURN(rc);
1033 }
1034
1035 int target_handle_disconnect(struct ptlrpc_request *req)
1036 {
1037         int rc;
1038         ENTRY;
1039
1040         rc = lustre_pack_reply(req, 1, NULL, NULL);
1041         if (rc)
1042                 RETURN(rc);
1043
1044         /* keep the rq_export around so we can send the reply */
1045         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1046         RETURN(0);
1047 }
1048
1049 void target_destroy_export(struct obd_export *exp)
1050 {
1051         /* exports created from last_rcvd data, and "fake"
1052            exports created by lctl don't have an import */
1053         if (exp->exp_imp_reverse != NULL)
1054                 class_destroy_import(exp->exp_imp_reverse);
1055
1056         /* We cancel locks at disconnect time, but this will catch any locks
1057          * granted in a race with recovery-induced disconnect. */
1058         if (exp->exp_obd->obd_namespace != NULL)
1059                 ldlm_cancel_locks_for_export(exp);
1060 }
1061
1062 /*
1063  * Recovery functions
1064  */
1065
1066 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1067 {
1068         __u64                  transno = lustre_msg_get_transno(req->rq_reqmsg);
1069         struct obd_export     *exp = req->rq_export;
1070         struct ptlrpc_request *reqiter;
1071         int                    dup = 0;
1072
1073         LASSERT(exp);
1074
1075         spin_lock(&exp->exp_lock);
1076         list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1077                             rq_replay_list) {
1078                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1079                         dup = 1;
1080                         break;
1081                 }
1082         }
1083
1084         if (dup) {
1085                 /* we expect it with RESENT and REPLAY flags */
1086                 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1087                      (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1088                         CERROR("invalid flags %x of resent replay\n",
1089                                lustre_msg_get_flags(req->rq_reqmsg));
1090         } else {
1091                 list_add_tail(&req->rq_replay_list, &exp->exp_req_replay_queue);
1092         }
1093
1094         spin_unlock(&exp->exp_lock);
1095         return dup;
1096 }
1097
1098 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1099 {
1100         LASSERT(!list_empty(&req->rq_replay_list));
1101         LASSERT(req->rq_export);
1102
1103         spin_lock(&req->rq_export->exp_lock);
1104         list_del_init(&req->rq_replay_list);
1105         spin_unlock(&req->rq_export->exp_lock);
1106 }
1107
1108 static void target_release_saved_req(struct ptlrpc_request *req)
1109 {
1110         ptlrpc_req_drop_rs(req);
1111         class_export_put(req->rq_export);
1112         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1113         OBD_FREE(req, sizeof *req);
1114 }
1115
1116 static void target_send_delayed_replies(struct obd_device *obd)
1117 {
1118         int max_clients = obd->obd_max_recoverable_clients;
1119         struct ptlrpc_request *req, *tmp;
1120         time_t elapsed_time = max_t(time_t, 1, cfs_time_current_sec() -
1121                                     obd->obd_recovery_start);
1122
1123         LCONSOLE_INFO("%s: Recovery period over after %d:%.02d, of %d clients "
1124                       "%d recovered and %d %s evicted.\n", obd->obd_name,
1125                       (int)elapsed_time/60, (int)elapsed_time%60, max_clients,
1126                       max_clients - obd->obd_recoverable_clients,
1127                       obd->obd_stale_clients,
1128                       obd->obd_stale_clients == 1 ? "was" : "were");
1129
1130         LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1131                       obd->obd_name);
1132
1133         list_for_each_entry_safe(req, tmp, &obd->obd_delayed_reply_queue,
1134                                  rq_list) {
1135                 list_del_init(&req->rq_list);
1136                 DEBUG_REQ(D_HA, req, "delayed:");
1137                 ptlrpc_reply(req);
1138                 target_release_saved_req(req);
1139         }
1140         obd->obd_recovery_end = cfs_time_current_sec();
1141 }
1142
1143 static void target_finish_recovery(struct obd_device *obd)
1144 {
1145         ldlm_reprocess_all_ns(obd->obd_namespace);
1146         spin_lock_bh(&obd->obd_processing_task_lock);
1147         if (list_empty(&obd->obd_recovery_queue)) {
1148                 obd->obd_recovery_thread = NULL;
1149                 obd->obd_processing_task = 0;
1150         } else {
1151                 spin_unlock_bh(&obd->obd_processing_task_lock);
1152                 CERROR("%s: Recovery queue isn't empty\n", obd->obd_name);
1153                 LBUG();
1154         }
1155         spin_unlock_bh(&obd->obd_processing_task_lock);
1156                 ;
1157         /* when recovery finished, cleanup orphans on mds and ost */
1158         if (OBT(obd) && OBP(obd, postrecov)) {
1159                 int rc = OBP(obd, postrecov)(obd);
1160                 if (rc < 0)
1161                         LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1162                                       obd->obd_name, rc);
1163         }
1164         target_send_delayed_replies(obd);
1165 }
1166
1167 static void abort_recovery_queue(struct obd_device *obd)
1168 {
1169         struct ptlrpc_request *req, *n;
1170         struct list_head abort_list;
1171         int rc;
1172
1173         CFS_INIT_LIST_HEAD(&abort_list);
1174         spin_lock_bh(&obd->obd_processing_task_lock);
1175         list_splice_init(&obd->obd_recovery_queue, &abort_list);
1176         spin_unlock_bh(&obd->obd_processing_task_lock);
1177         /* process abort list unlocked */
1178         list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1179                 target_exp_dequeue_req_replay(req);
1180                 list_del_init(&req->rq_list);
1181                 DEBUG_REQ(D_ERROR, req, "aborted:");
1182                 req->rq_status = -ENOTCONN;
1183                 req->rq_type = PTL_RPC_MSG_ERR;
1184                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1185                 if (rc == 0)
1186                         ptlrpc_reply(req);
1187                 else
1188                         DEBUG_REQ(D_ERROR, req,
1189                                   "packing failed for abort-reply; skipping");
1190                 target_release_saved_req(req);
1191         }
1192 }
1193
1194 /* Called from a cleanup function if the device is being cleaned up
1195    forcefully.  The exports should all have been disconnected already,
1196    the only thing left to do is
1197      - clear the recovery flags
1198      - cancel the timer
1199      - free queued requests and replies, but don't send replies
1200    Because the obd_stopping flag is set, no new requests should be received.
1201
1202 */
1203 void target_cleanup_recovery(struct obd_device *obd)
1204 {
1205         struct list_head *tmp, *n;
1206         struct ptlrpc_request *req;
1207         struct list_head clean_list;
1208         ENTRY;
1209
1210         LASSERT(obd->obd_stopping);
1211
1212         spin_lock_bh(&obd->obd_processing_task_lock);
1213         if (!obd->obd_recovering) {
1214                 spin_unlock_bh(&obd->obd_processing_task_lock);
1215                 EXIT;
1216                 return;
1217         }
1218         obd->obd_recovering = obd->obd_abort_recovery = 0;
1219         target_cancel_recovery_timer(obd);
1220         spin_unlock_bh(&obd->obd_processing_task_lock);
1221
1222         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
1223                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1224                 list_del(&req->rq_list);
1225                 target_release_saved_req(req);
1226         }
1227
1228         CFS_INIT_LIST_HEAD(&clean_list);
1229         spin_lock_bh(&obd->obd_processing_task_lock);
1230         list_splice_init(&obd->obd_recovery_queue, &clean_list);
1231         spin_unlock_bh(&obd->obd_processing_task_lock);
1232         list_for_each_safe(tmp, n, &clean_list) {
1233                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1234                 target_exp_dequeue_req_replay(req);
1235                 list_del_init(&req->rq_list);
1236                 target_release_saved_req(req);
1237         }
1238         EXIT;
1239 }
1240
1241 void target_stop_recovery(void *data, int abort)
1242 {
1243         struct obd_device *obd = data;
1244         enum obd_option flags;
1245         ENTRY;
1246
1247         spin_lock_bh(&obd->obd_processing_task_lock);
1248         if (!obd->obd_recovering) {
1249                 spin_unlock_bh(&obd->obd_processing_task_lock);
1250                 EXIT;
1251                 return;
1252         }
1253         flags = exp_flags_from_obd(obd) | OBD_OPT_ABORT_RECOV;
1254         obd->obd_recovering = 0;
1255         obd->obd_abort_recovery = 0;
1256         obd->obd_processing_task = 0;
1257         if (abort == 0)
1258                 LASSERT(obd->obd_recoverable_clients == 0);
1259
1260         target_cancel_recovery_timer(obd);
1261         spin_unlock_bh(&obd->obd_processing_task_lock);
1262
1263         if (abort) {
1264                 LCONSOLE_WARN("%s: recovery is aborted by administrative "
1265                               "request; %d clients are not recovered "
1266                               "(%d clients did)\n", obd->obd_name,
1267                               obd->obd_recoverable_clients,
1268                               obd->obd_connected_clients);
1269                 class_disconnect_stale_exports(obd, flags);
1270         }
1271         abort_recovery_queue(obd);
1272         target_finish_recovery(obd);
1273         CDEBUG(D_HA, "%s: recovery complete\n", obd_uuid2str(&obd->obd_uuid));
1274         EXIT;
1275 }
1276
1277 void target_abort_recovery(void *data)
1278 {
1279         target_stop_recovery(data, 1);
1280 }
1281
1282 static void reset_recovery_timer(struct obd_device *, int, int);
1283 static void target_recovery_expired(unsigned long castmeharder)
1284 {
1285         struct obd_device *obd = (struct obd_device *)castmeharder;
1286         CDEBUG(D_HA, "%s: recovery period over; %d clients never reconnected "
1287                "after %lds (%d clients did)\n", obd->obd_name,
1288                obd->obd_recoverable_clients,
1289                cfs_time_current_sec() - obd->obd_recovery_start,
1290                obd->obd_connected_clients);
1291
1292         spin_lock_bh(&obd->obd_processing_task_lock);
1293         obd->obd_abort_recovery = 1;
1294         cfs_waitq_signal(&obd->obd_next_transno_waitq);
1295         spin_unlock_bh(&obd->obd_processing_task_lock);
1296 }
1297
1298 /* obd_processing_task_lock should be held */
1299 void target_cancel_recovery_timer(struct obd_device *obd)
1300 {
1301         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1302         cfs_timer_disarm(&obd->obd_recovery_timer);
1303 }
1304
1305 /* extend = 1 means require at least "duration" seconds left in the timer,
1306    extend = 0 means set the total duration (start_recovery_timer) */
1307 static void reset_recovery_timer(struct obd_device *obd, int duration,
1308                                  int extend)
1309 {
1310         cfs_time_t now = cfs_time_current_sec();
1311         cfs_duration_t left;
1312
1313         spin_lock_bh(&obd->obd_processing_task_lock);
1314         if (!obd->obd_recovering) {
1315                 spin_unlock_bh(&obd->obd_processing_task_lock);
1316                 return;
1317         }
1318
1319         left = cfs_time_sub(obd->obd_recovery_end, now);
1320
1321         if (extend && (duration > left))
1322                 obd->obd_recovery_timeout += duration - left;
1323         else if (!extend && (duration > obd->obd_recovery_timeout))
1324                 /* Track the client's largest expected replay time */
1325                 obd->obd_recovery_timeout = duration;
1326 #ifdef CRAY_XT3
1327         /*
1328          * If total recovery time already exceed the
1329          * obd_recovery_max_time, then CRAY XT3 will
1330          * abort the recovery
1331          */
1332         if(obd->obd_recovery_timeout > obd->obd_recovery_max_time)
1333                 obd->obd_recovery_timeout = obd->obd_recovery_max_time;
1334 #endif
1335         obd->obd_recovery_end = obd->obd_recovery_start +
1336                                 obd->obd_recovery_timeout;
1337         if (cfs_time_before(now, obd->obd_recovery_end)) {
1338                 left = cfs_time_sub(obd->obd_recovery_end, now);
1339                 cfs_timer_arm(&obd->obd_recovery_timer, cfs_time_shift(left));
1340         }
1341         spin_unlock_bh(&obd->obd_processing_task_lock);
1342         CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1343                obd->obd_name, (unsigned)left);
1344 }
1345
1346 static void check_and_start_recovery_timer(struct obd_device *obd,
1347                                            svc_handler_t handler)
1348 {
1349         spin_lock_bh(&obd->obd_processing_task_lock);
1350         if (obd->obd_recovery_handler) {
1351                 spin_unlock_bh(&obd->obd_processing_task_lock);
1352                 return;
1353         }
1354         CDEBUG(D_HA, "%s: starting recovery timer\n", obd->obd_name);
1355         obd->obd_recovery_start = cfs_time_current_sec();
1356         /* minimum */
1357         obd->obd_recovery_timeout = OBD_RECOVERY_FACTOR * obd_timeout;
1358         obd->obd_recovery_handler = handler;
1359         cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1360         spin_unlock_bh(&obd->obd_processing_task_lock);
1361
1362         reset_recovery_timer(obd, obd->obd_recovery_timeout, 0);
1363 }
1364
1365 /* Reset the timer with each new client connection */
1366 /*
1367  * This timer is actually reconnect_timer, which is for making sure
1368  * the total recovery window is at least as big as my reconnect
1369  * attempt timing. So the initial recovery time_out will be set to
1370  * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1371  * from client is bigger than this, then the recovery time_out will
1372  * be extend to make sure the client could be reconnected, in the
1373  * process, the timeout from the new client should be ignored.
1374  */
1375
1376 static void
1377 target_start_and_reset_recovery_timer(struct obd_device *obd,
1378                                       svc_handler_t handler,
1379                                       struct ptlrpc_request *req,
1380                                       int new_client)
1381 {
1382         int service_time = lustre_msg_get_service_time(req->rq_reqmsg);
1383
1384         if (!new_client && service_time)
1385                 /* Teach server about old server's estimates, as first guess
1386                    at how long new requests will take. */
1387                 at_add(&req->rq_rqbd->rqbd_service->srv_at_estimate,
1388                        service_time);
1389
1390         check_and_start_recovery_timer(obd, handler);
1391
1392         /* convert the service time to rpc timeout,
1393          * reuse service_time to limit stack usage */
1394         service_time = at_est2timeout(service_time);
1395
1396         /* We expect other clients to timeout within service_time, then try
1397          * to reconnect, then try the failover server.  The max delay between
1398          * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL */
1399         service_time += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC +
1400                              INITIAL_CONNECT_TIMEOUT);
1401         if (service_time > obd->obd_recovery_timeout && !new_client)
1402                 reset_recovery_timer(obd, service_time, 0);
1403 }
1404
1405 static int check_for_next_transno(struct obd_device *obd)
1406 {
1407         struct ptlrpc_request *req;
1408         int wake_up = 0, connected, completed, queue_len, max;
1409         __u64 next_transno, req_transno;
1410
1411         spin_lock_bh(&obd->obd_processing_task_lock);
1412         req = list_entry(obd->obd_recovery_queue.next,
1413                          struct ptlrpc_request, rq_list);
1414         max = obd->obd_max_recoverable_clients;
1415         req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1416         connected = obd->obd_connected_clients;
1417         completed = max - obd->obd_recoverable_clients -
1418                     obd->obd_delayed_clients;
1419         queue_len = obd->obd_requests_queued_for_recovery;
1420         next_transno = obd->obd_next_recovery_transno;
1421
1422         CDEBUG(D_HA,"max: %d, connected: %d, delayed %d, completed: %d, "
1423                "queue_len: %d, req_transno: "LPU64", next_transno: "LPU64"\n",
1424                max, connected, obd->obd_delayed_clients, completed, queue_len,
1425                req_transno, next_transno);
1426         if (obd->obd_abort_recovery) {
1427                 CDEBUG(D_HA, "waking for aborted recovery\n");
1428                 wake_up = 1;
1429         } else if (!obd->obd_recovering) {
1430                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1431                 wake_up = 1;
1432         } else if (req_transno == next_transno) {
1433                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1434                 wake_up = 1;
1435         } else if (queue_len == obd->obd_recoverable_clients) {
1436                 CDEBUG(D_ERROR,
1437                        "waking for skipped transno (skip: "LPD64
1438                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1439                        next_transno, queue_len, completed, max, req_transno);
1440                 obd->obd_next_recovery_transno = req_transno;
1441                 wake_up = 1;
1442         }
1443         spin_unlock_bh(&obd->obd_processing_task_lock);
1444         LASSERT(lustre_msg_get_transno(req->rq_reqmsg) >= next_transno);
1445         return wake_up;
1446 }
1447
1448 static void process_recovery_queue(struct obd_device *obd)
1449 {
1450         struct ptlrpc_request *req;
1451         struct l_wait_info lwi = { 0 };
1452         ENTRY;
1453
1454         for (;;) {
1455                 spin_lock_bh(&obd->obd_processing_task_lock);
1456                 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1457                 req = list_entry(obd->obd_recovery_queue.next,
1458                                  struct ptlrpc_request, rq_list);
1459
1460                 if (lustre_msg_get_transno(req->rq_reqmsg) !=
1461                     obd->obd_next_recovery_transno) {
1462                         spin_unlock_bh(&obd->obd_processing_task_lock);
1463                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1464                                LPD64", x"LPU64")\n",
1465                                obd->obd_next_recovery_transno,
1466                                lustre_msg_get_transno(req->rq_reqmsg),
1467                                req->rq_xid);
1468                         l_wait_event(obd->obd_next_transno_waitq,
1469                                      check_for_next_transno(obd), &lwi);
1470                         if (target_recovery_check_and_stop(obd))
1471                                 return;
1472                         continue;
1473                 }
1474                 list_del_init(&req->rq_list);
1475                 LASSERT(obd->obd_recovery_thread);
1476                 /* replace request initial thread with current one, bug #18221 */
1477                 req->rq_svc_thread = obd->obd_recovery_thread;
1478                 obd->obd_requests_queued_for_recovery--;
1479                 spin_unlock_bh(&obd->obd_processing_task_lock);
1480
1481                 DEBUG_REQ(D_HA, req, "processing: ");
1482                 (void)obd->obd_recovery_handler(req);
1483                 obd->obd_replayed_requests++;
1484                 /* Extend the recovery timer enough to complete the next
1485                  * replayed rpc */
1486                 reset_recovery_timer(obd, AT_OFF ? obd_timeout :
1487                        at_get(&req->rq_rqbd->rqbd_service->srv_at_estimate), 1);
1488                 /* bug 1580: decide how to properly sync() in recovery */
1489                 //mds_fsync_super(obd->u.obt.obt_sb);
1490                 spin_lock_bh(&obd->obd_processing_task_lock);
1491                 obd->obd_next_recovery_transno++;
1492                 spin_unlock_bh(&obd->obd_processing_task_lock);
1493                 target_exp_dequeue_req_replay(req);
1494                 class_export_put(req->rq_export);
1495                 ptlrpc_req_drop_rs(req);
1496                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1497                 OBD_FREE(req, sizeof *req);
1498                 OBD_RACE(OBD_FAIL_TGT_REPLAY_DELAY);
1499                 spin_lock_bh(&obd->obd_processing_task_lock);
1500                 if (list_empty(&obd->obd_recovery_queue)) {
1501                         obd->obd_processing_task = 0;
1502                         obd->obd_recovery_thread = NULL;
1503                         spin_unlock_bh(&obd->obd_processing_task_lock);
1504                         break;
1505                 }
1506                 spin_unlock_bh(&obd->obd_processing_task_lock);
1507         }
1508         EXIT;
1509 }
1510
1511 int target_queue_recovery_request(struct ptlrpc_request *req,
1512                                   struct obd_device *obd)
1513 {
1514         struct list_head *tmp;
1515         int inserted = 0;
1516         __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1517         struct ptlrpc_request *saved_req;
1518         struct lustre_msg *reqmsg;
1519         int rc = 0;
1520
1521         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1522          * (i.e. buflens etc are in my own byte order), but type-dependent
1523          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1524
1525         if (!transno) {
1526                 CFS_INIT_LIST_HEAD(&req->rq_list);
1527                 DEBUG_REQ(D_HA, req, "not queueing");
1528                 return 1;
1529         }
1530
1531         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1532         /* XXX just like the request-dup code in queue_final_reply */
1533         OBD_ALLOC(saved_req, sizeof *saved_req);
1534         if (!saved_req)
1535                 LBUG();
1536         OBD_ALLOC(reqmsg, req->rq_reqlen);
1537         if (!reqmsg)
1538                 LBUG();
1539
1540         spin_lock_bh(&obd->obd_processing_task_lock);
1541
1542         /* If we're processing the queue, we want don't want to queue this
1543          * message.
1544          *
1545          * Also, if this request has a transno less than the one we're waiting
1546          * for, we should process it now.  It could (and currently always will)
1547          * be an open request for a descriptor that was opened some time ago.
1548          *
1549          * Also, a resent, replayed request that has already been
1550          * handled will pass through here and be processed immediately.
1551          */
1552         if (obd->obd_processing_task == cfs_curproc_pid() ||
1553             transno < obd->obd_next_recovery_transno) {
1554                 /* Processing the queue right now, don't re-add. */
1555                 LASSERT(list_empty(&req->rq_list));
1556                 spin_unlock_bh(&obd->obd_processing_task_lock);
1557                 GOTO(err_free, rc = 1);
1558         }
1559
1560         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))) {
1561                 spin_unlock_bh(&obd->obd_processing_task_lock);
1562                 GOTO(err_free, rc = 0);
1563         }
1564
1565         memcpy(saved_req, req, sizeof *req);
1566         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1567         req = saved_req;
1568         req->rq_reqmsg = reqmsg;
1569         class_export_get(req->rq_export);
1570         CFS_INIT_LIST_HEAD(&req->rq_list);
1571         CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1572
1573         if (target_exp_enqueue_req_replay(req)) {
1574                 spin_unlock_bh(&obd->obd_processing_task_lock);
1575                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1576                 GOTO(err_exp, rc = 0);
1577         }
1578
1579         /* XXX O(n^2) */
1580         list_for_each(tmp, &obd->obd_recovery_queue) {
1581                 struct ptlrpc_request *reqiter =
1582                         list_entry(tmp, struct ptlrpc_request, rq_list);
1583
1584                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1585                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1586                         inserted = 1;
1587                         break;
1588                 }
1589
1590                 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
1591                              transno)) {
1592                         spin_unlock_bh(&obd->obd_processing_task_lock);
1593                         DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
1594                                   "has been claimed by another client");
1595                         target_exp_dequeue_req_replay(req);
1596                         GOTO(err_exp, rc = 0);
1597                 }
1598         }
1599
1600         if (!inserted) {
1601                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1602         }
1603
1604         obd->obd_requests_queued_for_recovery++;
1605
1606         if (obd->obd_processing_task != 0) {
1607                 /* Someone else is processing this queue, we'll leave it to
1608                  * them.
1609                  */
1610                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1611                 spin_unlock_bh(&obd->obd_processing_task_lock);
1612                 return 0;
1613         }
1614
1615         /* Nobody is processing, and we know there's (at least) one to process
1616          * now, so we'll do the honours.
1617          */
1618         obd->obd_processing_task = cfs_curproc_pid();
1619         /* save thread that handle recovery queue */
1620         obd->obd_recovery_thread = req->rq_svc_thread;
1621         spin_unlock_bh(&obd->obd_processing_task_lock);
1622
1623         process_recovery_queue(obd);
1624         return 0;
1625
1626 err_exp:
1627         class_export_put(req->rq_export);
1628 err_free:
1629         OBD_FREE(reqmsg, req->rq_reqlen);
1630         OBD_FREE(saved_req, sizeof(*saved_req));
1631         return rc;
1632 }
1633
1634 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1635 {
1636         return req->rq_export->exp_obd;
1637 }
1638
1639 int target_queue_last_replay_reply(struct ptlrpc_request *req, int rc)
1640 {
1641         struct obd_device *obd = target_req2obd(req);
1642         struct ptlrpc_request *saved_req;
1643         struct lustre_msg *reqmsg;
1644         struct obd_export *exp = req->rq_export;
1645         int recovery_done = 0, delayed_done = 0;
1646
1647         LASSERT ((rc == 0) == req->rq_packed_final);
1648
1649         if (!req->rq_packed_final) {
1650                 /* Just like ptlrpc_error, but without the sending. */
1651                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1652                 if (rc)
1653                         return rc;
1654                 req->rq_type = PTL_RPC_MSG_ERR;
1655         }
1656
1657         LASSERT(!req->rq_reply_state->rs_difficult);
1658         LASSERT(list_empty(&req->rq_list));
1659         /* XXX a bit like the request-dup code in queue_recovery_request */
1660         OBD_ALLOC(saved_req, sizeof *saved_req);
1661         if (!saved_req)
1662                 return -ENOMEM;
1663         OBD_ALLOC(reqmsg, req->rq_reqlen);
1664         if (!reqmsg) {
1665                 OBD_FREE(saved_req, sizeof *req);
1666                 return -ENOMEM;
1667         }
1668         *saved_req = *req;
1669         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1670
1671         /* Don't race cleanup */
1672         spin_lock_bh(&obd->obd_processing_task_lock);
1673         if (obd->obd_stopping) {
1674                 spin_unlock_bh(&obd->obd_processing_task_lock);
1675                 goto out_noconn;
1676         }
1677
1678         if (!exp->exp_vbr_failed) {
1679                 ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1680                 req = saved_req;
1681                 req->rq_reqmsg = reqmsg;
1682                 CFS_INIT_LIST_HEAD(&req->rq_list);
1683                 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1684                 class_export_get(exp);
1685                 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1686         }
1687
1688         /* only count the first "replay over" request from each
1689            export */
1690         if (exp->exp_replay_needed) {
1691                 spin_lock(&exp->exp_lock);
1692                 exp->exp_replay_needed = 0;
1693                 spin_unlock(&exp->exp_lock);
1694
1695                 if (!exp->exp_delayed) {
1696                         --obd->obd_recoverable_clients;
1697                 } else {
1698                         spin_lock(&exp->exp_lock);
1699                         exp->exp_delayed = 0;
1700                         spin_unlock(&exp->exp_lock);
1701                         delayed_done = 1;
1702                         if (obd->obd_delayed_clients == 0) {
1703                                 spin_unlock_bh(&obd->obd_processing_task_lock);
1704                                 LBUG();
1705                         }
1706                         --obd->obd_delayed_clients;
1707                 }
1708         }
1709         recovery_done = (obd->obd_recoverable_clients == 0);
1710         spin_unlock_bh(&obd->obd_processing_task_lock);
1711
1712         if (delayed_done) {
1713                 /* start pinging export */
1714                 spin_lock(&obd->obd_dev_lock);
1715                 list_add_tail(&exp->exp_obd_chain_timed,
1716                               &obd->obd_exports_timed);
1717                 list_move_tail(&exp->exp_obd_chain, &obd->obd_exports);
1718                 spin_unlock(&obd->obd_dev_lock);
1719                 target_send_delayed_replies(obd);
1720         }
1721
1722         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1723         if (recovery_done) {
1724                 spin_lock_bh(&obd->obd_processing_task_lock);
1725                 obd->obd_recovering = 0;
1726                 obd->obd_version_recov = 0;
1727                 obd->obd_abort_recovery = 0;
1728                 target_cancel_recovery_timer(obd);
1729                 spin_unlock_bh(&obd->obd_processing_task_lock);
1730
1731                 OBD_RACE(OBD_FAIL_TGT_REPLAY_DELAY);
1732
1733                 if (!delayed_done)
1734                         target_finish_recovery(obd);
1735                 CDEBUG(D_HA, "%s: recovery complete\n",
1736                        obd_uuid2str(&obd->obd_uuid));
1737         } else {
1738                 CWARN("%s: %d recoverable clients remain\n",
1739                       obd->obd_name, obd->obd_recoverable_clients);
1740                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1741         }
1742
1743         /* VBR: disconnect export with failed recovery */
1744         if (exp->exp_vbr_failed) {
1745                 CWARN("%s: disconnect export %s\n", obd->obd_name,
1746                       exp->exp_client_uuid.uuid);
1747                 class_fail_export(exp);
1748                 OBD_FREE(reqmsg, req->rq_reqlen);
1749                 OBD_FREE(saved_req, sizeof *req);
1750                 req->rq_status = 0;
1751                 ptlrpc_send_reply(req, 0);
1752         }
1753
1754         return 1;
1755
1756 out_noconn:
1757         OBD_FREE(reqmsg, req->rq_reqlen);
1758         OBD_FREE(saved_req, sizeof *req);
1759         req->rq_status = -ENOTCONN;
1760         /* rv is ignored anyhow */
1761         return -ENOTCONN;
1762 }
1763
1764 int target_handle_reply(struct ptlrpc_request *req, int rc, int fail)
1765 {
1766         struct obd_device *obd = NULL;
1767
1768         if (req->rq_export)
1769                 obd = target_req2obd(req);
1770
1771         /* handle replay reply for version recovery */
1772         if (obd && obd->obd_version_recov &&
1773             (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1774                 LASSERT(req->rq_repmsg);
1775                 lustre_msg_add_flags(req->rq_repmsg, MSG_VERSION_REPLAY);
1776         }
1777
1778         /* handle last replay */
1779         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1780                 if (obd &&
1781                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_DELAY_REPLAY) {
1782                         DEBUG_REQ(D_HA, req,
1783                                   "delayed LAST_REPLAY, queuing reply");
1784                         rc = target_queue_last_replay_reply(req, rc);
1785                         LASSERT(req->rq_export->exp_delayed == 0);
1786                         return rc;
1787                 }
1788
1789                 if (obd && obd->obd_recovering) { /* normal recovery */
1790                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1791                         rc = target_queue_last_replay_reply(req, rc);
1792                         return rc;
1793                 }
1794
1795                 /* Lost a race with recovery; let the error path DTRT. */
1796                 rc = req->rq_status = -ENOTCONN;
1797         }
1798         target_send_reply(req, rc, fail);
1799         return 0;
1800 }
1801
1802 static inline struct ldlm_pool *ldlm_exp2pl(struct obd_export *exp)
1803 {
1804         LASSERT(exp != NULL);
1805         return &exp->exp_obd->obd_namespace->ns_pool;
1806 }
1807
1808 int target_pack_pool_reply(struct ptlrpc_request *req)
1809 {
1810         struct obd_device *obd;
1811         ENTRY;
1812
1813         /*
1814          * Check that we still have all structures alive as this may
1815          * be some late rpc in shutdown time.
1816          */
1817         if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
1818                      !exp_connect_lru_resize(req->rq_export))) {
1819                 lustre_msg_set_slv(req->rq_repmsg, 0);
1820                 lustre_msg_set_limit(req->rq_repmsg, 0);
1821                 RETURN(0);
1822         }
1823
1824         /*
1825          * OBD is alive here as export is alive, which we checked above.
1826          */
1827         obd = req->rq_export->exp_obd;
1828
1829         read_lock(&obd->obd_pool_lock);
1830         lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
1831         lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
1832         read_unlock(&obd->obd_pool_lock);
1833
1834         RETURN(0);
1835 }
1836
1837 int
1838 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1839 {
1840         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1841                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1842                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1843                 return (-ECOMM);
1844         }
1845
1846         if (rc) {
1847                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1848                 req->rq_status = rc;
1849                 return (ptlrpc_send_error(req, 1));
1850         } else {
1851                 DEBUG_REQ(D_NET, req, "sending reply");
1852         }
1853
1854         return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
1855 }
1856
1857 void
1858 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1859 {
1860         int                        netrc;
1861         struct ptlrpc_reply_state *rs;
1862         struct obd_device         *obd;
1863         struct obd_export         *exp;
1864         struct ptlrpc_service     *svc;
1865
1866         svc = req->rq_rqbd->rqbd_service;
1867         rs = req->rq_reply_state;
1868         if (rs == NULL || !rs->rs_difficult) {
1869                 /* no notifiers */
1870                 target_send_reply_msg (req, rc, fail_id);
1871                 return;
1872         }
1873
1874         /* must be an export if locks saved */
1875         LASSERT (req->rq_export != NULL);
1876         /* req/reply consistent */
1877         LASSERT (rs->rs_service == svc);
1878
1879         /* "fresh" reply */
1880         LASSERT (!rs->rs_scheduled);
1881         LASSERT (!rs->rs_scheduled_ever);
1882         LASSERT (!rs->rs_handled);
1883         LASSERT (!rs->rs_on_net);
1884         LASSERT (rs->rs_export == NULL);
1885         LASSERT (list_empty(&rs->rs_obd_list));
1886         LASSERT (list_empty(&rs->rs_exp_list));
1887
1888         exp = class_export_get(req->rq_export);
1889         obd = exp->exp_obd;
1890
1891         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1892         rs->rs_scheduled = 1;
1893         rs->rs_on_net    = 1;
1894         rs->rs_xid       = req->rq_xid;
1895         rs->rs_transno   = req->rq_transno;
1896         rs->rs_export    = exp;
1897
1898         spin_lock(&exp->exp_uncommitted_replies_lock);
1899
1900         /* VBR: use exp_last_committed */
1901         if (rs->rs_transno > exp->exp_last_committed) {
1902                 /* not committed already */
1903                 list_add_tail (&rs->rs_obd_list,
1904                                &exp->exp_uncommitted_replies);
1905         }
1906
1907         spin_unlock (&exp->exp_uncommitted_replies_lock);
1908         spin_lock (&exp->exp_lock);
1909
1910         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1911
1912         spin_unlock(&exp->exp_lock);
1913
1914         netrc = target_send_reply_msg (req, rc, fail_id);
1915
1916         spin_lock(&svc->srv_lock);
1917
1918         svc->srv_n_difficult_replies++;
1919
1920         if (netrc != 0) {
1921                 /* error sending: reply is off the net.  Also we need +1
1922                  * reply ref until ptlrpc_server_handle_reply() is done
1923                  * with the reply state (if the send was successful, there
1924                  * would have been +1 ref for the net, which
1925                  * reply_out_callback leaves alone) */
1926                 rs->rs_on_net = 0;
1927                 ptlrpc_rs_addref(rs);
1928                 atomic_inc (&svc->srv_outstanding_replies);
1929         }
1930
1931         if (!rs->rs_on_net ||                   /* some notifier */
1932             list_empty(&rs->rs_exp_list) ||     /* completed already */
1933             list_empty(&rs->rs_obd_list)) {
1934                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1935                 cfs_waitq_signal (&svc->srv_waitq);
1936         } else {
1937                 list_add (&rs->rs_list, &svc->srv_active_replies);
1938                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1939         }
1940
1941         spin_unlock(&svc->srv_lock);
1942 }
1943
1944 int target_handle_ping(struct ptlrpc_request *req)
1945 {
1946         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY &&
1947             req->rq_export->exp_in_recovery) {
1948                 spin_lock(&req->rq_export->exp_lock);
1949                 req->rq_export->exp_in_recovery = 0;
1950                 spin_unlock(&req->rq_export->exp_lock);
1951         }
1952         obd_ping(req->rq_export);
1953         return lustre_pack_reply(req, 1, NULL, NULL);
1954 }
1955
1956 void target_committed_to_req(struct ptlrpc_request *req)
1957 {
1958         struct obd_export *exp = req->rq_export;
1959         if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL) {
1960                 lustre_msg_set_last_committed(req->rq_repmsg,
1961                                               exp->exp_last_committed);
1962         } else {
1963                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
1964                           "%d)", exp->exp_obd->obd_no_transno,
1965                           req->rq_repmsg == NULL);
1966         }
1967         CDEBUG(D_INFO, "last_committed x"LPU64", this req x"LPU64"\n",
1968                exp->exp_obd->obd_last_committed, req->rq_xid);
1969 }
1970
1971 EXPORT_SYMBOL(target_committed_to_req);
1972
1973 int target_handle_qc_callback(struct ptlrpc_request *req)
1974 {
1975         struct obd_quotactl *oqctl;
1976         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1977
1978         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1979                                    lustre_swab_obd_quotactl);
1980         if (oqctl == NULL) {
1981                 CERROR("Can't unpack obd_quatactl\n");
1982                 RETURN(-EPROTO);
1983         }
1984
1985         cli->cl_qchk_stat = oqctl->qc_stat;
1986
1987         return 0;
1988 }
1989
1990 #ifdef HAVE_QUOTA_SUPPORT
1991 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1992 {
1993 #ifdef __KERNEL__
1994         struct obd_device *obd = req->rq_export->exp_obd;
1995         struct obd_device *master_obd;
1996         struct lustre_quota_ctxt *qctxt;
1997         struct qunit_data *qdata = NULL;
1998         int rc = 0;
1999         int repsize[2] = { sizeof(struct ptlrpc_body), 0 };
2000         ENTRY;
2001
2002         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_DROP_QUOTA_REQ))
2003                 RETURN(rc);
2004
2005         repsize[1] = quota_get_qunit_data_size(req->rq_export->
2006                                                exp_connect_flags);
2007
2008         rc = lustre_pack_reply(req, 2, repsize, NULL);
2009         if (rc)
2010                 RETURN(rc);
2011
2012         LASSERT(req->rq_export);
2013
2014         /* there are three forms of qunit(historic causes), so we need to
2015          * adjust qunits from slaves to the same form here */
2016         OBD_ALLOC(qdata, sizeof(struct qunit_data));
2017         if (!qdata)
2018                 RETURN(-ENOMEM);
2019         rc = quota_get_qdata(req, qdata, QUOTA_REQUEST, QUOTA_EXPORT);
2020         if (rc < 0) {
2021                 CDEBUG(D_ERROR, "Can't unpack qunit_data(rc: %d)\n", rc);
2022                 GOTO(out, rc);
2023         }
2024
2025         /* we use the observer */
2026         if (!obd->obd_observer || !obd->obd_observer->obd_observer) {
2027                 CERROR("Can't find the observer, it is recovering\n");
2028                 req->rq_status = -EAGAIN;
2029                 GOTO(send_reply, rc = -EAGAIN);
2030         }
2031
2032         master_obd = obd->obd_observer->obd_observer;
2033         qctxt = &master_obd->u.obt.obt_qctxt;
2034
2035         if (!qctxt->lqc_setup) {
2036                 /* quota_type has not been processed yet, return EAGAIN
2037                  * until we know whether or not quotas are supposed to
2038                  * be enabled */
2039                 CDEBUG(D_QUOTA, "quota_type not processed yet, return "
2040                                 "-EAGAIN\n");
2041                 req->rq_status = -EAGAIN;
2042                 rc = ptlrpc_reply(req);
2043                 GOTO(out, rc);
2044         }
2045
2046         LASSERT(qctxt->lqc_handler);
2047         rc = qctxt->lqc_handler(master_obd, qdata,
2048                                 lustre_msg_get_opc(req->rq_reqmsg));
2049         if (rc && rc != -EDQUOT)
2050                 CDEBUG(rc == -EBUSY  ? D_QUOTA : D_ERROR,
2051                        "dqacq failed! (rc:%d)\n", rc);
2052         req->rq_status = rc;
2053
2054         /* there are three forms of qunit(historic causes), so we need to
2055          * adjust the same form to different forms slaves needed */
2056         rc = quota_copy_qdata(req, qdata, QUOTA_REPLY, QUOTA_EXPORT);
2057         if (rc < 0) {
2058                 CDEBUG(D_ERROR, "Can't pack qunit_data(rc: %d)\n", rc);
2059                 GOTO(out, rc);
2060         }
2061
2062         /* Block the quota req. b=14840 */
2063         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_BLOCK_QUOTA_REQ, obd_timeout);
2064  send_reply:
2065         rc = ptlrpc_reply(req);
2066 out:
2067         OBD_FREE(qdata, sizeof(struct qunit_data));
2068         RETURN(rc);
2069 #else
2070         return 0;
2071 #endif /* !__KERNEL__ */
2072 }
2073 #endif /* HAVE_QUOTA_SUPPORT */
2074
2075 ldlm_mode_t lck_compat_array[] = {
2076         [LCK_EX] LCK_COMPAT_EX,
2077         [LCK_PW] LCK_COMPAT_PW,
2078         [LCK_PR] LCK_COMPAT_PR,
2079         [LCK_CW] LCK_COMPAT_CW,
2080         [LCK_CR] LCK_COMPAT_CR,
2081         [LCK_NL] LCK_COMPAT_NL,
2082         [LCK_GROUP] LCK_COMPAT_GROUP
2083 };