Whamcloud - gitweb
LU-14487 modules: remove references to Sun Trademark.
[fs/lustre-release.git] / lustre / ldlm / ldlm_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2010, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 /**
33  * This file deals with various client/target related logic including recovery.
34  *
35  * TODO: This code more logically belongs in the ptlrpc module than in ldlm and
36  * should be moved.
37  */
38
39 #define DEBUG_SUBSYSTEM S_LDLM
40
41 #include <cl_object.h>
42 #include <linux/jiffies.h>
43 #include <linux/kernel.h>
44 #include <linux/kthread.h>
45 #include <libcfs/libcfs.h>
46 #include <obd.h>
47 #include <obd_class.h>
48 #include <lustre_dlm.h>
49 #include <lustre_net.h>
50 #include <lustre_sec.h>
51 #include "ldlm_internal.h"
52
53 /*
54  * @priority: If non-zero, move the selected connection to the list head.
55  * @create: If zero, only search in existing connections.
56  */
57 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
58                            int priority, int create)
59 {
60         struct ptlrpc_connection *ptlrpc_conn;
61         struct obd_import_conn *imp_conn = NULL, *item;
62         lnet_nid_t nid4refnet = LNET_NID_ANY;
63         int rc = 0;
64
65         ENTRY;
66
67         if (!create && !priority) {
68                 CDEBUG(D_HA, "Nothing to do\n");
69                 RETURN(-EINVAL);
70         }
71
72         if (imp->imp_connection &&
73             imp->imp_connection->c_remote_uuid.uuid[0] == 0)
74                 /* nid4refnet is used to restrict network connections */
75                 nid4refnet = imp->imp_connection->c_self;
76         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid, nid4refnet);
77         if (!ptlrpc_conn) {
78                 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
79                 RETURN(-ENOENT);
80         }
81
82         if (create) {
83                 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
84                 if (!imp_conn)
85                         GOTO(out_put, rc = -ENOMEM);
86         }
87
88         spin_lock(&imp->imp_lock);
89         list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
90                 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
91                         if (priority) {
92                                 list_move(&item->oic_item,
93                                           &imp->imp_conn_list);
94                                 item->oic_last_attempt = 0;
95                         }
96                         CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
97                                imp, imp->imp_obd->obd_name, uuid->uuid,
98                                (priority ? ", moved to head" : ""));
99                         spin_unlock(&imp->imp_lock);
100                         GOTO(out_free, rc = 0);
101                 }
102         }
103         /* No existing import connection found for \a uuid. */
104         if (create) {
105                 imp_conn->oic_conn = ptlrpc_conn;
106                 imp_conn->oic_uuid = *uuid;
107                 imp_conn->oic_last_attempt = 0;
108                 if (priority)
109                         list_add(&imp_conn->oic_item, &imp->imp_conn_list);
110                 else
111                         list_add_tail(&imp_conn->oic_item,
112                                       &imp->imp_conn_list);
113                 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
114                        imp, imp->imp_obd->obd_name, uuid->uuid,
115                        (priority ? "head" : "tail"));
116         } else {
117                 spin_unlock(&imp->imp_lock);
118                 GOTO(out_free, rc = -ENOENT);
119         }
120
121         spin_unlock(&imp->imp_lock);
122         RETURN(0);
123 out_free:
124         if (imp_conn)
125                 OBD_FREE(imp_conn, sizeof(*imp_conn));
126 out_put:
127         ptlrpc_connection_put(ptlrpc_conn);
128         RETURN(rc);
129 }
130
131 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
132 {
133         return import_set_conn(imp, uuid, 1, 0);
134 }
135
136 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
137                            int priority)
138 {
139         return import_set_conn(imp, uuid, priority, 1);
140 }
141 EXPORT_SYMBOL(client_import_add_conn);
142
143 int client_import_dyn_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
144                                lnet_nid_t prim_nid, int priority)
145 {
146         struct ptlrpc_connection *ptlrpc_conn;
147         int rc;
148
149         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid, prim_nid);
150         if (!ptlrpc_conn) {
151                 const char *str_uuid = obd_uuid2str(uuid);
152
153                 rc = class_add_uuid(str_uuid, prim_nid);
154                 if (rc) {
155                         CERROR("%s: failed to add UUID '%s': rc = %d\n",
156                                imp->imp_obd->obd_name, str_uuid, rc);
157                         return rc;
158                 }
159         }
160         return import_set_conn(imp, uuid, priority, 1);
161 }
162 EXPORT_SYMBOL(client_import_dyn_add_conn);
163
164 int client_import_add_nids_to_conn(struct obd_import *imp, lnet_nid_t *nids,
165                                    int nid_count, struct obd_uuid *uuid)
166 {
167         struct obd_import_conn *conn;
168         int rc = -ENOENT;
169
170         ENTRY;
171         if (nid_count <= 0 || !nids)
172                 return rc;
173
174         spin_lock(&imp->imp_lock);
175         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
176                 if (class_check_uuid(&conn->oic_uuid, nids[0])) {
177                         *uuid = conn->oic_uuid;
178                         rc = class_add_nids_to_uuid(&conn->oic_uuid, nids,
179                                                     nid_count);
180                         break;
181                 }
182         }
183         spin_unlock(&imp->imp_lock);
184         RETURN(rc);
185 }
186 EXPORT_SYMBOL(client_import_add_nids_to_conn);
187
188 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
189 {
190         struct obd_import_conn *imp_conn;
191         struct obd_export *dlmexp;
192         int rc = -ENOENT;
193
194         ENTRY;
195
196         spin_lock(&imp->imp_lock);
197         if (list_empty(&imp->imp_conn_list)) {
198                 LASSERT(!imp->imp_connection);
199                 GOTO(out, rc);
200         }
201
202         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
203                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
204                         continue;
205                 LASSERT(imp_conn->oic_conn);
206
207                 if (imp_conn == imp->imp_conn_current) {
208                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
209
210                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
211                             imp->imp_state != LUSTRE_IMP_DISCON) {
212                                 CERROR("can't remove current connection\n");
213                                 GOTO(out, rc = -EBUSY);
214                         }
215
216                         ptlrpc_connection_put(imp->imp_connection);
217                         imp->imp_connection = NULL;
218
219                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
220                         if (dlmexp && dlmexp->exp_connection) {
221                                 LASSERT(dlmexp->exp_connection ==
222                                         imp_conn->oic_conn);
223                                 ptlrpc_connection_put(dlmexp->exp_connection);
224                                 dlmexp->exp_connection = NULL;
225                         }
226
227                         if (dlmexp != NULL)
228                                 class_export_put(dlmexp);
229                 }
230
231                 list_del(&imp_conn->oic_item);
232                 ptlrpc_connection_put(imp_conn->oic_conn);
233                 OBD_FREE(imp_conn, sizeof(*imp_conn));
234                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
235                        imp, imp->imp_obd->obd_name, uuid->uuid);
236                 rc = 0;
237                 break;
238         }
239 out:
240         spin_unlock(&imp->imp_lock);
241         if (rc == -ENOENT)
242                 CERROR("connection %s not found\n", uuid->uuid);
243         RETURN(rc);
244 }
245 EXPORT_SYMBOL(client_import_del_conn);
246
247 /**
248  * Find conn UUID by peer NID. \a peer is a server NID. This function is used
249  * to find a conn uuid of \a imp which can reach \a peer.
250  */
251 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
252                             struct obd_uuid *uuid)
253 {
254         struct obd_import_conn *conn;
255         int rc = -ENOENT;
256
257         ENTRY;
258
259         spin_lock(&imp->imp_lock);
260         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
261                 /* Check if conn UUID does have this peer NID. */
262                 if (class_check_uuid(&conn->oic_uuid, peer)) {
263                         *uuid = conn->oic_uuid;
264                         rc = 0;
265                         break;
266                 }
267         }
268         spin_unlock(&imp->imp_lock);
269         RETURN(rc);
270 }
271 EXPORT_SYMBOL(client_import_find_conn);
272
273 void client_destroy_import(struct obd_import *imp)
274 {
275         /*
276          * Drop security policy instance after all RPCs have finished/aborted
277          * to let all busy contexts be released.
278          */
279         class_import_get(imp);
280         class_destroy_import(imp);
281         sptlrpc_import_sec_put(imp);
282         class_import_put(imp);
283 }
284 EXPORT_SYMBOL(client_destroy_import);
285
286 /**
287  * Check whether or not the OSC is on MDT.
288  * In the config log,
289  * osc on MDT
290  *      setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
291  * osc on client
292  *      setup 0:{fsname}-OSTxxxx-osc 1:lustre-OST0000_UUID 2:NID
293  *
294  **/
295 static int osc_on_mdt(char *obdname)
296 {
297         char *ptr;
298
299         ptr = strrchr(obdname, '-');
300         if (ptr == NULL)
301                 return 0;
302
303         if (strncmp(ptr + 1, "MDT", 3) == 0)
304                 return 1;
305
306         return 0;
307 }
308
309 /*
310  * Configure an RPC client OBD device.
311  *
312  * lcfg parameters:
313  * 1 - client UUID
314  * 2 - server UUID
315  * 3 - inactive-on-startup
316  * 4 - restrictive net
317  */
318 int client_obd_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
319 {
320         struct client_obd *cli = &obd->u.cli;
321         struct obd_import *imp;
322         struct obd_uuid server_uuid;
323         int rq_portal, rp_portal, connect_op;
324         const char *name = obd->obd_type->typ_name;
325         enum ldlm_ns_type ns_type = LDLM_NS_TYPE_UNKNOWN;
326         char *cli_name = lustre_cfg_buf(lcfg, 0);
327         struct ptlrpc_connection fake_conn = { .c_self = 0,
328                                                .c_remote_uuid.uuid[0] = 0 };
329         int rc;
330
331         ENTRY;
332
333         /*
334          * In a more perfect world, we would hang a ptlrpc_client off of
335          * obd_type and just use the values from there.
336          */
337         if (!strcmp(name, LUSTRE_OSC_NAME)) {
338                 rq_portal = OST_REQUEST_PORTAL;
339                 rp_portal = OSC_REPLY_PORTAL;
340                 connect_op = OST_CONNECT;
341                 cli->cl_sp_me = LUSTRE_SP_CLI;
342                 cli->cl_sp_to = LUSTRE_SP_OST;
343                 ns_type = LDLM_NS_TYPE_OSC;
344         } else if (!strcmp(name, LUSTRE_MDC_NAME) ||
345                    !strcmp(name, LUSTRE_LWP_NAME)) {
346                 rq_portal = MDS_REQUEST_PORTAL;
347                 rp_portal = MDC_REPLY_PORTAL;
348                 connect_op = MDS_CONNECT;
349                 if (is_lwp_on_ost(cli_name))
350                         cli->cl_sp_me = LUSTRE_SP_OST;
351                 else if (is_lwp_on_mdt(cli_name))
352                         cli->cl_sp_me = LUSTRE_SP_MDT;
353                 else
354                         cli->cl_sp_me = LUSTRE_SP_CLI;
355                 cli->cl_sp_to = LUSTRE_SP_MDT;
356                 ns_type = LDLM_NS_TYPE_MDC;
357         } else if (!strcmp(name, LUSTRE_OSP_NAME)) {
358                 if (strstr(lustre_cfg_buf(lcfg, 1), "OST") == NULL) {
359                         /* OSP_on_MDT for other MDTs */
360                         connect_op = MDS_CONNECT;
361                         cli->cl_sp_to = LUSTRE_SP_MDT;
362                         ns_type = LDLM_NS_TYPE_MDC;
363                         rq_portal = OUT_PORTAL;
364                 } else {
365                         /* OSP on MDT for OST */
366                         connect_op = OST_CONNECT;
367                         cli->cl_sp_to = LUSTRE_SP_OST;
368                         ns_type = LDLM_NS_TYPE_OSC;
369                         rq_portal = OST_REQUEST_PORTAL;
370                 }
371                 rp_portal = OSC_REPLY_PORTAL;
372                 cli->cl_sp_me = LUSTRE_SP_MDT;
373         } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
374                 rq_portal = MGS_REQUEST_PORTAL;
375                 rp_portal = MGC_REPLY_PORTAL;
376                 connect_op = MGS_CONNECT;
377                 cli->cl_sp_me = LUSTRE_SP_MGC;
378                 cli->cl_sp_to = LUSTRE_SP_MGS;
379                 cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
380                 ns_type = LDLM_NS_TYPE_MGC;
381         } else {
382                 CERROR("unknown client OBD type \"%s\", can't setup\n",
383                        name);
384                 RETURN(-EINVAL);
385         }
386
387         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
388                 CERROR("requires a TARGET UUID\n");
389                 RETURN(-EINVAL);
390         }
391
392         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
393                 CERROR("client UUID must be less than 38 characters\n");
394                 RETURN(-EINVAL);
395         }
396
397         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
398                 CERROR("setup requires a SERVER UUID\n");
399                 RETURN(-EINVAL);
400         }
401
402         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
403                 CERROR("target UUID must be less than 38 characters\n");
404                 RETURN(-EINVAL);
405         }
406
407         init_rwsem(&cli->cl_sem);
408         mutex_init(&cli->cl_mgc_mutex);
409         cli->cl_seq = NULL;
410         init_rwsem(&cli->cl_seq_rwsem);
411         cli->cl_conn_count = 0;
412         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
413                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
414                      sizeof(server_uuid)));
415
416         cli->cl_dirty_pages = 0;
417         cli->cl_dirty_max_pages = 0;
418         cli->cl_avail_grant = 0;
419         /* FIXME: Should limit this for the sum of all cl_dirty_max_pages. */
420         /*
421          * cl_dirty_max_pages may be changed at connect time in
422          * ptlrpc_connect_interpret().
423          */
424         client_adjust_max_dirty(cli);
425         init_waitqueue_head(&cli->cl_cache_waiters);
426         INIT_LIST_HEAD(&cli->cl_loi_ready_list);
427         INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
428         INIT_LIST_HEAD(&cli->cl_loi_write_list);
429         INIT_LIST_HEAD(&cli->cl_loi_read_list);
430         spin_lock_init(&cli->cl_loi_list_lock);
431         atomic_set(&cli->cl_pending_w_pages, 0);
432         atomic_set(&cli->cl_pending_r_pages, 0);
433         cli->cl_r_in_flight = 0;
434         cli->cl_w_in_flight = 0;
435
436         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
437         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
438         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
439         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
440         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
441         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
442
443         /* lru for osc. */
444         INIT_LIST_HEAD(&cli->cl_lru_osc);
445         atomic_set(&cli->cl_lru_shrinkers, 0);
446         atomic_long_set(&cli->cl_lru_busy, 0);
447         atomic_long_set(&cli->cl_lru_in_list, 0);
448         INIT_LIST_HEAD(&cli->cl_lru_list);
449         spin_lock_init(&cli->cl_lru_list_lock);
450         atomic_long_set(&cli->cl_unstable_count, 0);
451         INIT_LIST_HEAD(&cli->cl_shrink_list);
452         INIT_LIST_HEAD(&cli->cl_grant_chain);
453
454         INIT_LIST_HEAD(&cli->cl_flight_waiters);
455         cli->cl_rpcs_in_flight = 0;
456
457         init_waitqueue_head(&cli->cl_destroy_waitq);
458         atomic_set(&cli->cl_destroy_in_flight, 0);
459
460
461         cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
462         cli->cl_preferred_cksum_type = 0;
463 #ifdef ENABLE_CHECKSUM
464         /* Turn on checksumming by default. */
465         cli->cl_checksum = 1;
466         /*
467          * The supported checksum types will be worked out at connect time
468          * Set cl_chksum* to CRC32 for now to avoid returning screwed info
469          * through procfs.
470          */
471         cli->cl_cksum_type = cli->cl_supp_cksum_types;
472 #endif
473         atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
474
475         /*
476          * Set it to possible maximum size. It may be reduced by ocd_brw_size
477          * from OFD after connecting.
478          */
479         cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
480
481         cli->cl_max_short_io_bytes = OBD_DEF_SHORT_IO_BYTES;
482
483         /*
484          * set cl_chunkbits default value to PAGE_SHIFT,
485          * it will be updated at OSC connection time.
486          */
487         cli->cl_chunkbits = PAGE_SHIFT;
488
489         if (!strcmp(name, LUSTRE_MDC_NAME)) {
490                 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
491         } else if (cfs_totalram_pages() >> (20 - PAGE_SHIFT) <= 128 /* MB */) {
492                 cli->cl_max_rpcs_in_flight = 2;
493         } else if (cfs_totalram_pages() >> (20 - PAGE_SHIFT) <= 256 /* MB */) {
494                 cli->cl_max_rpcs_in_flight = 3;
495         } else if (cfs_totalram_pages() >> (20 - PAGE_SHIFT) <= 512 /* MB */) {
496                 cli->cl_max_rpcs_in_flight = 4;
497         } else {
498                 if (osc_on_mdt(obd->obd_name))
499                         cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_MAX;
500                 else
501                         cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
502         }
503
504         spin_lock_init(&cli->cl_mod_rpcs_lock);
505         spin_lock_init(&cli->cl_mod_rpcs_hist.oh_lock);
506         cli->cl_max_mod_rpcs_in_flight = 0;
507         cli->cl_mod_rpcs_in_flight = 0;
508         cli->cl_close_rpcs_in_flight = 0;
509         init_waitqueue_head(&cli->cl_mod_rpcs_waitq);
510         cli->cl_mod_tag_bitmap = NULL;
511
512         INIT_LIST_HEAD(&cli->cl_chg_dev_linkage);
513
514         if (connect_op == MDS_CONNECT) {
515                 cli->cl_max_mod_rpcs_in_flight = cli->cl_max_rpcs_in_flight - 1;
516                 OBD_ALLOC(cli->cl_mod_tag_bitmap,
517                           BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
518                 if (cli->cl_mod_tag_bitmap == NULL)
519                         GOTO(err, rc = -ENOMEM);
520         }
521
522         rc = ldlm_get_ref();
523         if (rc) {
524                 CERROR("ldlm_get_ref failed: %d\n", rc);
525                 GOTO(err, rc);
526         }
527
528         ptlrpc_init_client(rq_portal, rp_portal, name,
529                            &obd->obd_ldlm_client);
530
531         imp = class_new_import(obd);
532         if (imp == NULL)
533                 GOTO(err_ldlm, rc = -ENOENT);
534         imp->imp_client = &obd->obd_ldlm_client;
535         imp->imp_connect_op = connect_op;
536         memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
537                LUSTRE_CFG_BUFLEN(lcfg, 1));
538         class_import_put(imp);
539
540         if (lustre_cfg_buf(lcfg, 4)) {
541                 __u32 refnet = libcfs_str2net(lustre_cfg_string(lcfg, 4));
542
543                 if (refnet == LNET_NET_ANY) {
544                         rc = -EINVAL;
545                         CERROR("%s: bad mount option 'network=%s': rc = %d\n",
546                                obd->obd_name, lustre_cfg_string(lcfg, 4),
547                                rc);
548                         GOTO(err_import, rc);
549                 }
550                 fake_conn.c_self = LNET_MKNID(refnet, 0);
551                 imp->imp_connection = &fake_conn;
552         }
553
554         rc = client_import_add_conn(imp, &server_uuid, 1);
555         if (rc) {
556                 CERROR("can't add initial connection\n");
557                 GOTO(err_import, rc);
558         }
559         imp->imp_connection = NULL;
560
561         cli->cl_import = imp;
562         /* cli->cl_max_mds_easize updated by mdc_init_ea_size() */
563         cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
564
565         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
566                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
567                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
568                                name, obd->obd_name,
569                                cli->cl_target_uuid.uuid);
570                         spin_lock(&imp->imp_lock);
571                         imp->imp_deactive = 1;
572                         spin_unlock(&imp->imp_lock);
573                 }
574         }
575
576         obd->obd_namespace = ldlm_namespace_new(obd, obd->obd_name,
577                                                 LDLM_NAMESPACE_CLIENT,
578                                                 LDLM_NAMESPACE_GREEDY,
579                                                 ns_type);
580         if (IS_ERR(obd->obd_namespace)) {
581                 rc = PTR_ERR(obd->obd_namespace);
582                 CERROR("%s: unable to create client namespace: rc = %d\n",
583                        obd->obd_name, rc);
584                 obd->obd_namespace = NULL;
585                 GOTO(err_import, rc);
586         }
587
588         RETURN(rc);
589
590 err_import:
591         class_destroy_import(imp);
592 err_ldlm:
593         ldlm_put_ref();
594 err:
595         if (cli->cl_mod_tag_bitmap != NULL)
596                 OBD_FREE(cli->cl_mod_tag_bitmap,
597                          BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
598         cli->cl_mod_tag_bitmap = NULL;
599
600         RETURN(rc);
601 }
602 EXPORT_SYMBOL(client_obd_setup);
603
604 int client_obd_cleanup(struct obd_device *obd)
605 {
606         struct client_obd *cli = &obd->u.cli;
607
608         ENTRY;
609
610         ldlm_namespace_free_post(obd->obd_namespace);
611         obd->obd_namespace = NULL;
612
613         obd_cleanup_client_import(obd);
614         LASSERT(obd->u.cli.cl_import == NULL);
615
616         ldlm_put_ref();
617
618         if (cli->cl_mod_tag_bitmap != NULL)
619                 OBD_FREE(cli->cl_mod_tag_bitmap,
620                          BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
621         cli->cl_mod_tag_bitmap = NULL;
622
623         RETURN(0);
624 }
625 EXPORT_SYMBOL(client_obd_cleanup);
626
627 /* ->o_connect() method for client side (OSC and MDC and MGC) */
628 int client_connect_import(const struct lu_env *env,
629                           struct obd_export **exp,
630                           struct obd_device *obd, struct obd_uuid *cluuid,
631                           struct obd_connect_data *data, void *localdata)
632 {
633         struct client_obd *cli = &obd->u.cli;
634         struct obd_import *imp = cli->cl_import;
635         struct obd_connect_data *ocd;
636         struct lustre_handle conn = { 0 };
637         int rc;
638
639         ENTRY;
640
641         *exp = NULL;
642         down_write(&cli->cl_sem);
643         if (cli->cl_conn_count > 0)
644                 GOTO(out_sem, rc = -EALREADY);
645
646         rc = class_connect(&conn, obd, cluuid);
647         if (rc)
648                 GOTO(out_sem, rc);
649
650         cli->cl_conn_count++;
651         *exp = class_conn2export(&conn);
652
653         LASSERT(obd->obd_namespace);
654
655         imp->imp_dlm_handle = conn;
656         rc = ptlrpc_init_import(imp);
657         if (rc != 0)
658                 GOTO(out_ldlm, rc);
659
660         ocd = &imp->imp_connect_data;
661         if (data) {
662                 *ocd = *data;
663                 imp->imp_connect_flags_orig = data->ocd_connect_flags;
664                 imp->imp_connect_flags2_orig = data->ocd_connect_flags2;
665         }
666
667         rc = ptlrpc_connect_import(imp);
668         if (rc != 0) {
669                 LASSERT(imp->imp_state == LUSTRE_IMP_DISCON);
670                 GOTO(out_ldlm, rc);
671         }
672         LASSERT(*exp != NULL && (*exp)->exp_connection);
673
674         if (data) {
675                 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
676                          ocd->ocd_connect_flags, "old %#llx, new %#llx\n",
677                          data->ocd_connect_flags, ocd->ocd_connect_flags);
678                 data->ocd_connect_flags = ocd->ocd_connect_flags;
679                 data->ocd_connect_flags2 = ocd->ocd_connect_flags2;
680         }
681
682         ptlrpc_pinger_add_import(imp);
683
684         EXIT;
685
686         if (rc) {
687 out_ldlm:
688                 cli->cl_conn_count--;
689                 class_disconnect(*exp);
690                 *exp = NULL;
691         }
692 out_sem:
693         up_write(&cli->cl_sem);
694
695         if (!rc && localdata) {
696                 LASSERT(cli->cl_cache == NULL); /* only once */
697                 cli->cl_cache = (struct cl_client_cache *)localdata;
698                 cl_cache_incref(cli->cl_cache);
699                 cli->cl_lru_left = &cli->cl_cache->ccc_lru_left;
700
701                 /* add this osc into entity list */
702                 LASSERT(list_empty(&cli->cl_lru_osc));
703                 spin_lock(&cli->cl_cache->ccc_lru_lock);
704                 list_add(&cli->cl_lru_osc, &cli->cl_cache->ccc_lru);
705                 spin_unlock(&cli->cl_cache->ccc_lru_lock);
706         }
707
708         return rc;
709 }
710 EXPORT_SYMBOL(client_connect_import);
711
712 int client_disconnect_export(struct obd_export *exp)
713 {
714         struct obd_device *obd = class_exp2obd(exp);
715         struct client_obd *cli;
716         struct obd_import *imp;
717         int rc = 0, err;
718
719         ENTRY;
720
721         if (!obd) {
722                 CERROR("invalid export for disconnect: exp %p cookie %#llx\n",
723                        exp, exp ? exp->exp_handle.h_cookie : -1);
724                 RETURN(-EINVAL);
725         }
726
727         cli = &obd->u.cli;
728         imp = cli->cl_import;
729
730         down_write(&cli->cl_sem);
731         CDEBUG(D_INFO, "disconnect %s - %zu\n", obd->obd_name,
732                 cli->cl_conn_count);
733
734         if (cli->cl_conn_count == 0) {
735                 CERROR("disconnecting disconnected device (%s)\n",
736                        obd->obd_name);
737                 GOTO(out_disconnect, rc = -EINVAL);
738         }
739
740         cli->cl_conn_count--;
741         if (cli->cl_conn_count != 0)
742                 GOTO(out_disconnect, rc = 0);
743
744         /*
745          * Mark import deactivated now, so we don't try to reconnect if any
746          * of the cleanup RPCs fails (e.g. LDLM cancel, etc).  We don't
747          * fully deactivate the import, or that would drop all requests.
748          */
749         spin_lock(&imp->imp_lock);
750         imp->imp_deactive = 1;
751         spin_unlock(&imp->imp_lock);
752
753         /*
754          * Some non-replayable imports (MDS's OSCs) are pinged, so just
755          * delete it regardless.  (It's safe to delete an import that was
756          * never added.)
757          */
758         (void)ptlrpc_pinger_del_import(imp);
759
760         if (obd->obd_namespace != NULL) {
761                 /* obd_force == local only */
762                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
763                                        obd->obd_force ? LCF_LOCAL : 0, NULL);
764                 ldlm_namespace_free_prior(obd->obd_namespace, imp,
765                                           obd->obd_force);
766         }
767
768         /*
769          * There's no need to hold sem while disconnecting an import,
770          * and it may actually cause deadlock in GSS.
771          */
772         up_write(&cli->cl_sem);
773         rc = ptlrpc_disconnect_import(imp, 0);
774         down_write(&cli->cl_sem);
775
776         ptlrpc_invalidate_import(imp);
777
778         EXIT;
779
780 out_disconnect:
781         /*
782          * Use server style - class_disconnect should be always called for
783          * o_disconnect.
784          */
785         err = class_disconnect(exp);
786         if (!rc && err)
787                 rc = err;
788
789         up_write(&cli->cl_sem);
790
791         RETURN(rc);
792 }
793 EXPORT_SYMBOL(client_disconnect_export);
794
795 #ifdef HAVE_SERVER_SUPPORT
796 int server_disconnect_export(struct obd_export *exp)
797 {
798         int rc;
799
800         ENTRY;
801
802         /* Disconnect early so that clients can't keep using export. */
803         rc = class_disconnect(exp);
804         /* Close import to avoid sending any requests. */
805         if (exp->exp_imp_reverse)
806                 ptlrpc_cleanup_imp(exp->exp_imp_reverse);
807
808         ldlm_bl_thread_wakeup();
809
810         /* complete all outstanding replies */
811         spin_lock(&exp->exp_lock);
812         while (!list_empty(&exp->exp_outstanding_replies)) {
813                 struct ptlrpc_reply_state *rs =
814                         list_entry(exp->exp_outstanding_replies.next,
815                                        struct ptlrpc_reply_state, rs_exp_list);
816                 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
817
818                 spin_lock(&svcpt->scp_rep_lock);
819
820                 list_del_init(&rs->rs_exp_list);
821
822                 spin_lock(&rs->rs_lock);
823                 /* clear rs_convert_lock to make sure rs is handled and put */
824                 rs->rs_convert_lock = 0;
825                 ptlrpc_schedule_difficult_reply(rs);
826                 spin_unlock(&rs->rs_lock);
827
828                 spin_unlock(&svcpt->scp_rep_lock);
829         }
830         spin_unlock(&exp->exp_lock);
831
832         RETURN(rc);
833 }
834 EXPORT_SYMBOL(server_disconnect_export);
835
836 static inline int target_check_recovery_timer(struct obd_device *target)
837 {
838         ktime_t remaining;
839         s64 timeout;
840
841         if (!target->obd_recovering || target->obd_recovery_start == 0)
842                 return 0;
843
844         remaining = hrtimer_get_remaining(&target->obd_recovery_timer);
845         timeout = ktime_divns(remaining, NSEC_PER_SEC);
846         if (timeout > -30)
847                 return 0;
848
849         /* the recovery timer should expire, but it isn't triggered,
850          * it's better to abort the recovery of this target to speed up
851          * the recovery of the whole cluster. */
852         spin_lock(&target->obd_dev_lock);
853         if (target->obd_recovering) {
854                 CERROR("%s: Aborting recovery\n", target->obd_name);
855                 target->obd_abort_recovery = 1;
856                 wake_up(&target->obd_next_transno_waitq);
857         }
858         spin_unlock(&target->obd_dev_lock);
859         return 0;
860 }
861
862 /*
863  * --------------------------------------------------------------------------
864  * from old lib/target.c
865  * --------------------------------------------------------------------------
866  */
867 static int target_handle_reconnect(struct lustre_handle *conn,
868                                    struct obd_export *exp,
869                                    struct obd_uuid *cluuid)
870 {
871         struct obd_device *target;
872         struct lustre_handle *hdl;
873         ktime_t remaining;
874         s64 timeout;
875         int rc = 0;
876
877         ENTRY;
878         hdl = &exp->exp_imp_reverse->imp_remote_handle;
879         if (!exp->exp_connection || !lustre_handle_is_used(hdl)) {
880                 conn->cookie = exp->exp_handle.h_cookie;
881                 CDEBUG(D_HA,
882                        "connect export for UUID '%s' at %p, cookie %#llx\n",
883                        cluuid->uuid, exp, conn->cookie);
884                 RETURN(0);
885         }
886
887         target = exp->exp_obd;
888
889         /* Might be a re-connect after a partition. */
890         if (memcmp(&conn->cookie, &hdl->cookie, sizeof(conn->cookie))) {
891                 LCONSOLE_WARN("%s: already connected client %s (at %s) with handle %#llx. Rejecting client with the same UUID trying to reconnect with handle %#llx\n",
892                               target->obd_name,
893                               obd_uuid2str(&exp->exp_client_uuid),
894                               obd_export_nid2str(exp),
895                               hdl->cookie, conn->cookie);
896                 memset(conn, 0, sizeof(*conn));
897                 /*
898                  * target_handle_connect() treats EALREADY and
899                  * -EALREADY differently.  -EALREADY is an error
900                  * (same UUID, different handle).
901                  */
902                 RETURN(-EALREADY);
903         }
904
905         if (!target->obd_recovering) {
906                 LCONSOLE_WARN("%s: Client %s (at %s) reconnecting\n",
907                         target->obd_name, obd_uuid2str(&exp->exp_client_uuid),
908                         obd_export_nid2str(exp));
909                 GOTO(out_already, rc);
910         }
911
912         remaining = hrtimer_get_remaining(&target->obd_recovery_timer);
913         timeout = ktime_divns(remaining, NSEC_PER_SEC);
914         if (timeout > 0) {
915                 LCONSOLE_WARN("%s: Client %s (at %s) reconnected, waiting for %d clients in recovery for %lld:%.02lld\n",
916                               target->obd_name,
917                               obd_uuid2str(&exp->exp_client_uuid),
918                               obd_export_nid2str(exp),
919                               atomic_read(&target->obd_max_recoverable_clients),
920                               timeout / 60, timeout % 60);
921         } else {
922                 struct target_distribute_txn_data *tdtd;
923                 int size = 0;
924                 int count = 0;
925                 char *buf = NULL;
926
927                 target_check_recovery_timer(target);
928
929                 tdtd = class_exp2tgt(exp)->lut_tdtd;
930                 if (tdtd && tdtd->tdtd_show_update_logs_retrievers)
931                         buf = tdtd->tdtd_show_update_logs_retrievers(
932                                 tdtd->tdtd_show_retrievers_cbdata,
933                                 &size, &count);
934
935                 if (count > 0)
936                         LCONSOLE_WARN("%s: Client %s (at %s) reconnecting, waiting for %d MDTs (%s) in recovery for %lld:%.02lld. Please wait until all MDTs recovered or you may force MDT evicition via 'lctl --device %s abort_recovery.\n",
937                                       target->obd_name,
938                                       obd_uuid2str(&exp->exp_client_uuid),
939                                       obd_export_nid2str(exp), count,
940                                       buf ? buf : "unknown (not enough RAM)",
941                                       (abs(timeout) + target->obd_recovery_timeout) / 60,
942                                       (abs(timeout) + target->obd_recovery_timeout) % 60,
943                                       target->obd_name);
944                 else
945                         LCONSOLE_WARN("%s: Recovery already passed deadline %lld:%.02lld. If you do not want to wait more, you may force taget eviction via 'lctl --device %s abort_recovery.\n",
946                                       target->obd_name, abs(timeout) / 60,
947                                       abs(timeout) % 60, target->obd_name);
948
949                 if (buf != NULL)
950                         OBD_FREE(buf, size);
951         }
952
953 out_already:
954         conn->cookie = exp->exp_handle.h_cookie;
955         /*
956          * target_handle_connect() treats EALREADY and
957          * -EALREADY differently.  EALREADY means we are
958          * doing a valid reconnect from the same client.
959          */
960         RETURN(EALREADY);
961 }
962
963 static void
964 check_and_start_recovery_timer(struct obd_device *obd,
965                                struct ptlrpc_request *req, int new_client);
966
967 /**
968  * update flags for import during reconnect process
969  */
970 static int rev_import_flags_update(struct obd_import *revimp,
971                                    struct ptlrpc_request *req)
972 {
973         int rc;
974         struct obd_connect_data *data;
975
976         data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
977
978         if (data->ocd_connect_flags & OBD_CONNECT_AT)
979                 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
980         else
981                 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
982
983         revimp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
984
985         rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx, &req->rq_flvr);
986         if (rc) {
987                 CERROR("%s: cannot get reverse import %s security: rc = %d\n",
988                         revimp->imp_client->cli_name,
989                         libcfs_id2str(req->rq_peer), rc);
990                 return rc;
991         }
992
993         return 0;
994 }
995
996 /**
997  * Allocate a new reverse import for an export.
998  *
999  * \retval -errno in case error hit
1000  * \retval 0 if reverse import correctly init
1001  **/
1002 int rev_import_init(struct obd_export *export)
1003 {
1004         struct obd_device *obd = export->exp_obd;
1005         struct obd_import *revimp;
1006
1007         LASSERT(export->exp_imp_reverse == NULL);
1008
1009         revimp = class_new_import(obd);
1010         if (revimp == NULL)
1011                 return -ENOMEM;
1012
1013         revimp->imp_remote_handle.cookie = 0ULL;
1014         revimp->imp_client = &obd->obd_ldlm_client;
1015         revimp->imp_dlm_fake = 1;
1016
1017         /* it is safe to connect import in new state as no sends possible */
1018         spin_lock(&export->exp_lock);
1019         export->exp_imp_reverse = revimp;
1020         spin_unlock(&export->exp_lock);
1021         class_import_put(revimp);
1022
1023         return 0;
1024 }
1025 EXPORT_SYMBOL(rev_import_init);
1026
1027 /**
1028  * Handle reconnect for an export.
1029  *
1030  * \param exp export to handle reconnect process
1031  * \param req client reconnect request
1032  *
1033  * \retval -rc in case securitfy flavor can't be changed
1034  * \retval 0 in case none problems
1035  */
1036 static int rev_import_reconnect(struct obd_export *exp,
1037                                 struct ptlrpc_request *req)
1038 {
1039         struct obd_import *revimp = exp->exp_imp_reverse;
1040         struct lustre_handle *lh;
1041         int rc;
1042
1043         /* avoid sending a request until import flags are changed */
1044         ptlrpc_import_enter_resend(revimp);
1045
1046         ptlrpc_connection_put(revimp->imp_connection);
1047
1048         /*
1049          * client from recovery don't have a handle so we need to take from
1050          * request. it may produce situation when wrong client connected
1051          * to recovery as we trust a client uuid
1052          */
1053         lh = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1054         revimp->imp_remote_handle = *lh;
1055
1056         /*
1057          * unknown versions will be caught in
1058          * ptlrpc_handle_server_req_in->lustre_unpack_msg()
1059          */
1060         revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
1061
1062         revimp->imp_connection = ptlrpc_connection_addref(exp->exp_connection);
1063
1064         rc = rev_import_flags_update(revimp, req);
1065         if (rc != 0) {
1066                 /*
1067                  * it is safe to still be in RECOVERY phase as we are not able
1068                  * to setup correct security flavor so requests are not able to
1069                  * be delivered correctly
1070                  */
1071                 return rc;
1072         }
1073
1074         /* resend all rpc's via new connection */
1075         return ptlrpc_import_recovery_state_machine(revimp);
1076 }
1077
1078 int target_handle_connect(struct ptlrpc_request *req)
1079 {
1080         struct obd_device *target = NULL;
1081         struct obd_export *export = NULL;
1082         /*
1083          * connect handle - filled from target_handle_reconnect in
1084          * reconnect case
1085          */
1086         struct lustre_handle conn;
1087         struct lustre_handle *tmp;
1088         struct obd_uuid cluuid;
1089         char *str;
1090         int rc = 0;
1091         char *target_start;
1092         int target_len;
1093         bool mds_conn = false, lw_client = false, initial_conn = false;
1094         bool mds_mds_conn = false;
1095         bool new_mds_mds_conn = false;
1096         struct obd_connect_data *data, *tmpdata;
1097         int size, tmpsize;
1098         lnet_nid_t *client_nid = NULL;
1099         struct ptlrpc_connection *pcon = NULL;
1100
1101         ENTRY;
1102
1103         OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
1104
1105         str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
1106         if (str == NULL) {
1107                 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
1108                 GOTO(out, rc = -EINVAL);
1109         }
1110
1111         target = class_dev_by_str(str);
1112         if (!target) {
1113                 deuuidify(str, NULL, &target_start, &target_len);
1114                 LCONSOLE_ERROR_MSG(0x137,
1115                                    "%s: not available for connect from %s (no target). If you are running an HA pair check that the target is mounted on the other server.\n",
1116                                    str, libcfs_nid2str(req->rq_peer.nid));
1117                 GOTO(out, rc = -ENODEV);
1118         }
1119
1120         spin_lock(&target->obd_dev_lock);
1121
1122         target->obd_conn_inprogress++;
1123
1124         if (target->obd_stopping || !target->obd_set_up) {
1125                 spin_unlock(&target->obd_dev_lock);
1126
1127                 deuuidify(str, NULL, &target_start, &target_len);
1128                 LCONSOLE_INFO("%.*s: Not available for connect from %s (%s)\n",
1129                               target_len, target_start,
1130                               libcfs_nid2str(req->rq_peer.nid),
1131                               (target->obd_stopping ?
1132                                "stopping" : "not set up"));
1133                 GOTO(out, rc = -ENODEV);
1134         }
1135
1136         if (target->obd_no_conn) {
1137                 spin_unlock(&target->obd_dev_lock);
1138
1139                 CDEBUG(D_INFO,
1140                        "%s: Temporarily refusing client connection from %s\n",
1141                        target->obd_name, libcfs_nid2str(req->rq_peer.nid));
1142                 GOTO(out, rc = -EAGAIN);
1143         }
1144
1145         spin_unlock(&target->obd_dev_lock);
1146
1147         str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
1148         if (str == NULL) {
1149                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
1150                 GOTO(out, rc = -EINVAL);
1151         }
1152
1153         obd_str2uuid(&cluuid, str);
1154
1155         tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1156         if (tmp == NULL)
1157                 GOTO(out, rc = -EPROTO);
1158
1159         conn = *tmp;
1160
1161         size = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
1162                                     RCL_CLIENT);
1163         if (size < 0 || size > 8 * sizeof(struct obd_connect_data))
1164                 GOTO(out, rc = -EPROTO);
1165         data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
1166         if (!data)
1167                 GOTO(out, rc = -EPROTO);
1168
1169         rc = req_capsule_server_pack(&req->rq_pill);
1170         if (rc)
1171                 GOTO(out, rc);
1172
1173 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1174         /*
1175          * Don't allow clients to connect that are using old 1.8 format
1176          * protocol conventions (LUSTRE_MSG_MAGIC_v1, !MSGHDR_CKSUM_INCOMPAT18,
1177          * ldlm_flock_policy_wire format, MDT_ATTR_xTIME_SET, etc).  The
1178          * FULL20 flag should be set on all connections since 2.0, but no
1179          * longer affects behaviour.
1180          *
1181          * Later this check will be disabled and the flag can be retired
1182          * completely once interop with 3.0 is no longer needed.
1183          */
1184         if (!(data->ocd_connect_flags & OBD_CONNECT_FULL20))
1185                 GOTO(out, rc = -EPROTO);
1186
1187         /*
1188          * Don't allow liblustre clients to connect.
1189          * - testing was disabled in v2_2_50_0-61-g6a75d65
1190          * - building was disabled in v2_5_58_0-28-g7277179
1191          * - client code was deleted in v2_6_50_0-101-gcdfbc72,
1192          * - clients were refused connect for version difference > 0.0.1.32
1193          */
1194         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1195                 DEBUG_REQ(D_WARNING, req, "Refusing libclient connection");
1196                 GOTO(out, rc = -EPROTO);
1197         }
1198 #endif
1199
1200         /*
1201          * Note: lw_client is needed in MDS-MDS failover during update log
1202          * processing, so we needs to allow lw_client to be connected at
1203          * anytime, instead of only the initial connection
1204          */
1205         lw_client = (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0;
1206
1207         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) {
1208                 initial_conn = true;
1209                 mds_conn = (data->ocd_connect_flags & OBD_CONNECT_MDS) != 0;
1210                 mds_mds_conn = (data->ocd_connect_flags &
1211                                 OBD_CONNECT_MDS_MDS) != 0;
1212
1213                 /*
1214                  * OBD_CONNECT_MNE_SWAB is removed at 2.14
1215                  * Checking OBD_CONNECT_FID can be removed in the future.
1216                  *
1217                  * Via check OBD_CONNECT_FID, we can distinguish whether
1218                  * the OBD_CONNECT_MDS_MDS/OBD_CONNECT_MNE_SWAB is from
1219                  * MGC or MDT, since MGC does not use OBD_CONNECT_FID.
1220                  */
1221                 if (!lw_client &&
1222                     (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
1223                     (data->ocd_connect_flags & OBD_CONNECT_FID) &&
1224                     (data->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1225                         __u32 major = OBD_OCD_VERSION_MAJOR(data->ocd_version);
1226                         __u32 minor = OBD_OCD_VERSION_MINOR(data->ocd_version);
1227                         __u32 patch = OBD_OCD_VERSION_PATCH(data->ocd_version);
1228
1229                         /*
1230                          * We do not support the MDT-MDT interoperations with
1231                          * different version MDT because of protocol changes.
1232                          */
1233                         if (unlikely(major != LUSTRE_MAJOR ||
1234                                      minor != LUSTRE_MINOR ||
1235                                      abs(patch - LUSTRE_PATCH) > 3)) {
1236                                 LCONSOLE_WARN("%s (%u.%u.%u.%u) refused the connection from different version MDT (%d.%d.%d.%d) %s %s\n",
1237                                               target->obd_name, LUSTRE_MAJOR,
1238                                               LUSTRE_MINOR, LUSTRE_PATCH,
1239                                               LUSTRE_FIX, major, minor, patch,
1240                                               OBD_OCD_VERSION_FIX(data->ocd_version),
1241                                               libcfs_nid2str(req->rq_peer.nid),
1242                                               str);
1243                                 GOTO(out, rc = -EPROTO);
1244                         }
1245                 }
1246         }
1247
1248         /* lctl gets a backstage, all-access pass. */
1249         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
1250                 goto dont_check_exports;
1251
1252         export = obd_uuid_lookup(target, &cluuid);
1253         if (!export)
1254                 goto no_export;
1255
1256         /* We've found an export in the hash. */
1257
1258         spin_lock(&export->exp_lock);
1259
1260         if (export->exp_connecting) { /* b=9635, et. al. */
1261                 spin_unlock(&export->exp_lock);
1262                 LCONSOLE_WARN("%s: Export %p already connecting from %s\n",
1263                               export->exp_obd->obd_name, export,
1264                               libcfs_nid2str(req->rq_peer.nid));
1265                 class_export_put(export);
1266                 export = NULL;
1267                 rc = -EALREADY;
1268         } else if ((mds_conn || (lw_client && initial_conn) ||
1269                    data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
1270                    export->exp_connection != NULL) {
1271                 spin_unlock(&export->exp_lock);
1272                 if (req->rq_peer.nid != export->exp_connection->c_peer.nid) {
1273                         /* MDS or LWP reconnected after failover. */
1274                         LCONSOLE_WARN("%s: Received %s connection from %s, removing former export from %s\n",
1275                                       target->obd_name,
1276                                       mds_conn ? "MDS" : "LWP",
1277                                       libcfs_nid2str(req->rq_peer.nid),
1278                                       libcfs_nid2str(export->exp_connection->c_peer.nid));
1279                 } else {
1280                         /* New MDS connection from the same NID. */
1281                         LCONSOLE_WARN("%s: Received new %s connection from %s, removing former export from same NID\n",
1282                                       target->obd_name,
1283                                       mds_conn ? "MDS" : "LWP",
1284                                       libcfs_nid2str(req->rq_peer.nid));
1285                 }
1286
1287                 if (req->rq_peer.nid == export->exp_connection->c_peer.nid &&
1288                     data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
1289                         /*
1290                          * Because exports between MDTs will always be
1291                          * kept, let's do not fail such export if they
1292                          * come from the same NID, otherwise it might
1293                          * cause eviction between MDTs, which might
1294                          * cause namespace inconsistency
1295                          */
1296                         spin_lock(&export->exp_lock);
1297                         export->exp_connecting = 1;
1298                         export->exp_conn_cnt = 0;
1299                         spin_unlock(&export->exp_lock);
1300                         conn.cookie = export->exp_handle.h_cookie;
1301                         rc = EALREADY;
1302                 } else {
1303                         class_fail_export(export);
1304                         class_export_put(export);
1305                         export = NULL;
1306                         rc = 0;
1307                 }
1308         } else if (export->exp_connection != NULL && initial_conn &&
1309                    req->rq_peer.nid != export->exp_connection->c_peer.nid) {
1310                 spin_unlock(&export->exp_lock);
1311                 /* In MDS failover we have static UUID but NID can change. */
1312                 LCONSOLE_WARN("%s: Client %s seen on new nid %s when existing nid %s is already connected\n",
1313                               target->obd_name, cluuid.uuid,
1314                               libcfs_nid2str(req->rq_peer.nid),
1315                               libcfs_nid2str(
1316                                         export->exp_connection->c_peer.nid));
1317                 rc = -EALREADY;
1318                 class_export_put(export);
1319                 export = NULL;
1320         } else if (OBD_FAIL_PRECHECK(OBD_FAIL_TGT_RECOVERY_CONNECT) &&
1321                    !lw_client) {
1322                 spin_unlock(&export->exp_lock);
1323                 rc = -EAGAIN;
1324         } else {
1325                 export->exp_connecting = 1;
1326                 spin_unlock(&export->exp_lock);
1327                 LASSERT(export->exp_obd == target);
1328
1329                 rc = target_handle_reconnect(&conn, export, &cluuid);
1330         }
1331
1332         /* If we found an export, we already unlocked. */
1333         if (!export) {
1334 no_export:
1335                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
1336         } else if (req->rq_export == NULL &&
1337                    atomic_read(&export->exp_rpc_count) > 0) {
1338                 LCONSOLE_WARN("%s: Client %s (at %s) refused connection, still busy with %d references\n",
1339                               target->obd_name, cluuid.uuid,
1340                               libcfs_nid2str(req->rq_peer.nid),
1341                               refcount_read(&export->exp_handle.h_ref));
1342                         GOTO(out, rc = -EBUSY);
1343         } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1 &&
1344                    rc != EALREADY) {
1345                 if (!strstr(cluuid.uuid, "mdt"))
1346                         LCONSOLE_WARN("%s: Rejecting reconnect from the known client %s (at %s) because it is indicating it is a new client\n",
1347                                       target->obd_name, cluuid.uuid,
1348                                       libcfs_nid2str(req->rq_peer.nid));
1349                 GOTO(out, rc = -EALREADY);
1350         } else {
1351                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
1352         }
1353
1354         if (rc < 0)
1355                 GOTO(out, rc);
1356
1357         CDEBUG(D_HA, "%s: connection from %s@%s %st%llu exp %p cur %lld last %lld\n",
1358                target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1359                target->obd_recovering ? "recovering/" : "", data->ocd_transno,
1360                export, ktime_get_seconds(),
1361                export ? export->exp_last_request_time : 0);
1362
1363         /*
1364          * If this is the first time a client connects, reset the recovery
1365          * timer. Discard lightweight connections which might be local.
1366          */
1367         if (!lw_client && rc == 0 && target->obd_recovering)
1368                 check_and_start_recovery_timer(target, req, export == NULL);
1369
1370         /*
1371          * We want to handle EALREADY but *not* -EALREADY from
1372          * target_handle_reconnect(), return reconnection state in a flag.
1373          */
1374         if (rc == EALREADY) {
1375                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
1376                 rc = 0;
1377         } else {
1378                 LASSERT(rc == 0);
1379         }
1380
1381         /* Tell the client if we support replayable requests. */
1382         if (target->obd_replayable)
1383                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
1384         client_nid = &req->rq_peer.nid;
1385
1386         if (export == NULL) {
1387                 /* allow lightweight connections during recovery */
1388                 /*
1389                  * allow "new" MDT to be connected during recovery, since we
1390                  * need retrieve recovery update records from it
1391                  */
1392                 if (target->obd_recovering && !lw_client && !mds_mds_conn) {
1393                         struct hrtimer *timer = &target->obd_recovery_timer;
1394                         ktime_t remaining;
1395                         s64 timeout, left;
1396                         int in_progress;
1397                         int connected;
1398                         int known;
1399                         int stale;
1400                         char *msg;
1401
1402                         connected = atomic_read(&target->obd_connected_clients);
1403                         in_progress = atomic_read(&target->obd_lock_replay_clients);
1404                         known =
1405                            atomic_read(&target->obd_max_recoverable_clients);
1406                         stale = target->obd_stale_clients;
1407                         remaining = hrtimer_get_remaining(timer);
1408                         left = ktime_divns(remaining, NSEC_PER_SEC);
1409
1410                         if (ktime_to_ns(remaining) > 0) {
1411                                 msg = "to recover in";
1412                                 timeout = left;
1413                         } else {
1414                                 msg = "already passed deadline";
1415                                 timeout = -left;
1416
1417                                 target_check_recovery_timer(target);
1418                         }
1419
1420                         LCONSOLE_WARN("%s: Denying connection for new client %s (at %s), waiting for %d known clients (%d recovered, %d in progress, and %d evicted) %s %lld:%.02lld\n",
1421                                       target->obd_name, cluuid.uuid,
1422                                       libcfs_nid2str(req->rq_peer.nid), known,
1423                                       connected - in_progress, in_progress,
1424                                       stale, msg, timeout / 60, timeout % 60);
1425                         rc = -EBUSY;
1426                 } else {
1427 dont_check_exports:
1428                         rc = obd_connect(req->rq_svc_thread->t_env,
1429                                          &export, target, &cluuid, data,
1430                                          client_nid);
1431                         if (mds_conn && OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
1432                                 lustre_msg_add_op_flags(req->rq_repmsg,
1433                                                         MSG_CONNECT_RECOVERING);
1434                         if (rc == 0) {
1435                                 conn.cookie = export->exp_handle.h_cookie;
1436                                 rc = rev_import_init(export);
1437                         }
1438
1439                         if (mds_mds_conn)
1440                                 new_mds_mds_conn = true;
1441                 }
1442         } else {
1443                 rc = obd_reconnect(req->rq_svc_thread->t_env,
1444                                    export, target, &cluuid, data, client_nid);
1445         }
1446         if (rc)
1447                 GOTO(out, rc);
1448
1449         LASSERT(target->u.obt.obt_magic == OBT_MAGIC);
1450         data->ocd_instance = target->u.obt.obt_instance;
1451
1452         /*
1453          * Return only the parts of obd_connect_data that we understand, so the
1454          * client knows that we don't understand the rest.
1455          */
1456         if (data) {
1457                 tmpsize = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
1458                                                RCL_SERVER);
1459                 tmpdata = req_capsule_server_get(&req->rq_pill,
1460                                                  &RMF_CONNECT_DATA);
1461                 /*
1462                  * Don't use struct assignment here, because the client reply
1463                  * buffer may be smaller/larger than the local struct
1464                  * obd_connect_data.
1465                  */
1466                 memcpy(tmpdata, data, min(tmpsize, size));
1467         }
1468
1469         /*
1470          * If the client and the server are the same node, we will already
1471          * have an export that really points to the client's DLM export,
1472          * because we have a shared handles table.
1473          *
1474          * XXX this will go away when shaver stops sending the "connect" handle
1475          * in the real "remote handle" field of the request --phik 24 Apr 2003
1476          */
1477         ptlrpc_request_change_export(req, export);
1478
1479         pcon = ptlrpc_connection_get(req->rq_peer, req->rq_self, &cluuid);
1480         if (pcon == NULL)
1481                 GOTO(out, rc = -ENOTCONN);
1482
1483         spin_lock(&export->exp_lock);
1484
1485         if (export->exp_disconnected) {
1486                 spin_unlock(&export->exp_lock);
1487                 GOTO(out, rc = -ENODEV);
1488         }
1489         if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
1490                 spin_unlock(&export->exp_lock);
1491                 CDEBUG(D_RPCTRACE,
1492                        "%s: %s already connected at greater or equal conn_cnt: %d >= %d\n",
1493                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1494                        export->exp_conn_cnt,
1495                        lustre_msg_get_conn_cnt(req->rq_reqmsg));
1496
1497                 GOTO(out, rc = -EALREADY);
1498         }
1499         LASSERT(lustre_msg_get_conn_cnt(req->rq_reqmsg) > 0);
1500         export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1501
1502         /* Check to see if connection came from another NID. */
1503         if (export->exp_connection != NULL &&
1504             export->exp_connection->c_peer.nid != req->rq_peer.nid) {
1505                 obd_nid_del(export->exp_obd, export);
1506                 ptlrpc_connection_put(export->exp_connection);
1507                 export->exp_connection = NULL;
1508         }
1509
1510         if (export->exp_connection == NULL) {
1511                 export->exp_connection = pcon;
1512                 pcon = NULL;
1513         }
1514         obd_nid_add(export->exp_obd, export);
1515
1516         spin_unlock(&export->exp_lock);
1517
1518         lustre_msg_set_handle(req->rq_repmsg, &conn);
1519
1520         rc = rev_import_reconnect(export, req);
1521         if (rc != 0)
1522                 GOTO(out, rc);
1523
1524         if (target->obd_recovering && !export->exp_in_recovery && !lw_client) {
1525                 int has_transno;
1526                 __u64 transno = data->ocd_transno;
1527
1528                 spin_lock(&export->exp_lock);
1529                 /*
1530                  * possible race with class_disconnect_stale_exports,
1531                  * export may be already in the eviction process
1532                  */
1533                 if (export->exp_failed) {
1534                         spin_unlock(&export->exp_lock);
1535                         GOTO(out, rc = -ENODEV);
1536                 }
1537                 export->exp_in_recovery = 1;
1538                 export->exp_req_replay_needed = 1;
1539                 export->exp_lock_replay_needed = 1;
1540                 spin_unlock(&export->exp_lock);
1541
1542                 has_transno = !!(lustre_msg_get_op_flags(req->rq_reqmsg) &
1543                                  MSG_CONNECT_TRANSNO);
1544                 if (has_transno && transno == 0)
1545                         CWARN("Connect with zero transno!\n");
1546
1547                 if (has_transno && transno > 0 &&
1548                     transno < target->obd_next_recovery_transno &&
1549                     transno > target->obd_last_committed) {
1550                         /* Another way is to use cmpxchg() to be lock-free. */
1551                         spin_lock(&target->obd_recovery_task_lock);
1552                         if (transno < target->obd_next_recovery_transno)
1553                                 target->obd_next_recovery_transno = transno;
1554                         spin_unlock(&target->obd_recovery_task_lock);
1555                 }
1556
1557                 atomic_inc(&target->obd_req_replay_clients);
1558                 atomic_inc(&target->obd_lock_replay_clients);
1559                 /*
1560                  * Note: MDS-MDS connection is allowed to be connected during
1561                  * recovery, no matter if the exports needs to be recoveried.
1562                  * Because we need retrieve updates logs from all other MDTs.
1563                  * So if the MDS-MDS export is new, obd_max_recoverable_clients
1564                  * also needs to be increased to match other recovery checking
1565                  * condition.
1566                  */
1567                 if (new_mds_mds_conn)
1568                         atomic_inc(&target->obd_max_recoverable_clients);
1569
1570                 if (atomic_inc_return(&target->obd_connected_clients) ==
1571                     atomic_read(&target->obd_max_recoverable_clients))
1572                         wake_up(&target->obd_next_transno_waitq);
1573         }
1574
1575         /* Tell the client we're in recovery, when client is involved in it. */
1576         if (target->obd_recovering && !lw_client)
1577                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
1578
1579 out:
1580         if (export) {
1581                 spin_lock(&export->exp_lock);
1582                 export->exp_connecting = 0;
1583                 spin_unlock(&export->exp_lock);
1584
1585                 class_export_put(export);
1586         }
1587         if (target != NULL) {
1588                 spin_lock(&target->obd_dev_lock);
1589                 target->obd_conn_inprogress--;
1590                 spin_unlock(&target->obd_dev_lock);
1591                 class_decref(target, "find", current);
1592         }
1593         if (pcon)
1594                 ptlrpc_connection_put(pcon);
1595         req->rq_status = rc;
1596         RETURN(rc);
1597 }
1598
1599 int target_handle_disconnect(struct ptlrpc_request *req)
1600 {
1601         int rc;
1602
1603         ENTRY;
1604
1605         rc = req_capsule_server_pack(&req->rq_pill);
1606         if (rc)
1607                 RETURN(rc);
1608
1609         /* In case of target disconnect, updating sec ctx immediately is
1610          * required in order to record latest sequence number used.
1611          * Sequence is normally updated on export destroy, but this event
1612          * can occur too late, ie after a new target connect request has
1613          * been processed.
1614          * Maintaining correct sequence when client connection becomes idle
1615          * ensures that GSS does not erroneously consider requests as replays.
1616          */
1617         rc = sptlrpc_export_update_ctx(req->rq_export);
1618         if (rc)
1619                 RETURN(rc);
1620
1621         /* Keep the rq_export around so we can send the reply. */
1622         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1623
1624         RETURN(0);
1625 }
1626
1627 void target_destroy_export(struct obd_export *exp)
1628 {
1629         struct obd_import *imp = NULL;
1630         /*
1631          * exports created from last_rcvd data, and "fake"
1632          * exports created by lctl don't have an import
1633          */
1634         spin_lock(&exp->exp_lock);
1635         if (exp->exp_imp_reverse != NULL) {
1636                 imp = exp->exp_imp_reverse;
1637                 exp->exp_imp_reverse = NULL;
1638         }
1639         spin_unlock(&exp->exp_lock);
1640         if (imp != NULL)
1641                 client_destroy_import(imp);
1642
1643         LASSERT_ATOMIC_ZERO(&exp->exp_locks_count);
1644         LASSERT_ATOMIC_ZERO(&exp->exp_rpc_count);
1645         LASSERT_ATOMIC_ZERO(&exp->exp_cb_count);
1646         LASSERT_ATOMIC_ZERO(&exp->exp_replay_count);
1647 }
1648 EXPORT_SYMBOL(target_destroy_export);
1649
1650 /*
1651  * Recovery functions
1652  */
1653 static void target_request_copy_get(struct ptlrpc_request *req)
1654 {
1655         class_export_rpc_inc(req->rq_export);
1656         LASSERT(list_empty(&req->rq_list));
1657         INIT_LIST_HEAD(&req->rq_replay_list);
1658
1659         /* Increase refcount to keep request in queue. */
1660         atomic_inc(&req->rq_refcount);
1661         /* Let export know it has replays to be handled. */
1662         atomic_inc(&req->rq_export->exp_replay_count);
1663 }
1664
1665 static void target_request_copy_put(struct ptlrpc_request *req)
1666 {
1667         LASSERT(list_empty(&req->rq_replay_list));
1668         LASSERT_ATOMIC_POS(&req->rq_export->exp_replay_count);
1669
1670         atomic_dec(&req->rq_export->exp_replay_count);
1671         class_export_rpc_dec(req->rq_export);
1672         ptlrpc_server_drop_request(req);
1673 }
1674
1675 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1676 {
1677         __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1678         struct obd_export *exp = req->rq_export;
1679         struct ptlrpc_request *reqiter;
1680         struct ptlrpc_request *dup_req = NULL;
1681         int dup = 0;
1682
1683         LASSERT(exp);
1684
1685         spin_lock(&exp->exp_lock);
1686         list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1687                             rq_replay_list) {
1688                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1689                         dup_req = reqiter;
1690                         dup = 1;
1691                         break;
1692                 }
1693         }
1694
1695         if (dup) {
1696                 /* We expect it with RESENT and REPLAY flags. */
1697                 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1698                     (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1699                         CERROR("invalid flags %x of resent replay\n",
1700                                lustre_msg_get_flags(req->rq_reqmsg));
1701
1702                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1703                         __u32 new_conn;
1704
1705                         new_conn = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1706                         if (new_conn >
1707                             lustre_msg_get_conn_cnt(dup_req->rq_reqmsg))
1708                                 lustre_msg_set_conn_cnt(dup_req->rq_reqmsg,
1709                                                         new_conn);
1710                 }
1711         } else {
1712                 list_add_tail(&req->rq_replay_list,
1713                               &exp->exp_req_replay_queue);
1714         }
1715
1716         spin_unlock(&exp->exp_lock);
1717         return dup;
1718 }
1719
1720 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1721 {
1722         LASSERT(!list_empty(&req->rq_replay_list));
1723         LASSERT(req->rq_export);
1724
1725         spin_lock(&req->rq_export->exp_lock);
1726         list_del_init(&req->rq_replay_list);
1727         spin_unlock(&req->rq_export->exp_lock);
1728 }
1729
1730 static void target_finish_recovery(struct lu_target *lut)
1731 {
1732         struct obd_device *obd = lut->lut_obd;
1733
1734         ENTRY;
1735
1736         /* Only log a recovery message when recovery has occurred. */
1737         if (obd->obd_recovery_start) {
1738                 time64_t now = ktime_get_seconds();
1739                 time64_t elapsed_time;
1740
1741                 elapsed_time = max_t(time64_t, now - obd->obd_recovery_start,
1742                                      1);
1743                 LCONSOLE_INFO("%s: Recovery over after %lld:%.02lld, of %d clients %d recovered and %d %s evicted.\n",
1744                               obd->obd_name, elapsed_time / 60,
1745                               elapsed_time % 60,
1746                               atomic_read(&obd->obd_max_recoverable_clients),
1747                               atomic_read(&obd->obd_connected_clients),
1748                               obd->obd_stale_clients,
1749                               obd->obd_stale_clients == 1 ? "was" : "were");
1750         }
1751
1752         ldlm_reprocess_recovery_done(obd->obd_namespace);
1753         spin_lock(&obd->obd_recovery_task_lock);
1754         if (!list_empty(&obd->obd_req_replay_queue) ||
1755             !list_empty(&obd->obd_lock_replay_queue) ||
1756             !list_empty(&obd->obd_final_req_queue)) {
1757                 CERROR("%s: Recovery queues ( %s%s%s) are not empty\n",
1758                        obd->obd_name,
1759                        list_empty(&obd->obd_req_replay_queue) ? "" : "req ",
1760                        list_empty(&obd->obd_lock_replay_queue) ? \
1761                                   "" : "lock ",
1762                        list_empty(&obd->obd_final_req_queue) ? \
1763                                   "" : "final ");
1764                 spin_unlock(&obd->obd_recovery_task_lock);
1765                 LBUG();
1766         }
1767         spin_unlock(&obd->obd_recovery_task_lock);
1768
1769         obd->obd_recovery_end = ktime_get_seconds();
1770
1771         /* When recovery finished, cleanup orphans on MDS and OST. */
1772         if (obd->obd_type && OBP(obd, postrecov)) {
1773                 int rc = OBP(obd, postrecov)(obd);
1774
1775                 if (rc < 0)
1776                         LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1777                                       obd->obd_name, rc);
1778         }
1779         EXIT;
1780 }
1781
1782 static void abort_req_replay_queue(struct obd_device *obd)
1783 {
1784         struct ptlrpc_request *req, *n;
1785         LIST_HEAD(abort_list);
1786
1787         spin_lock(&obd->obd_recovery_task_lock);
1788         list_splice_init(&obd->obd_req_replay_queue, &abort_list);
1789         spin_unlock(&obd->obd_recovery_task_lock);
1790         list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1791                 DEBUG_REQ(D_WARNING, req, "aborted:");
1792                 req->rq_status = -ENOTCONN;
1793                 if (ptlrpc_error(req)) {
1794                         DEBUG_REQ(D_ERROR, req,
1795                                   "failed abort_req_reply; skipping");
1796                 }
1797                 target_exp_dequeue_req_replay(req);
1798                 target_request_copy_put(req);
1799         }
1800 }
1801
1802 static void abort_lock_replay_queue(struct obd_device *obd)
1803 {
1804         struct ptlrpc_request *req, *n;
1805         LIST_HEAD(abort_list);
1806
1807         spin_lock(&obd->obd_recovery_task_lock);
1808         list_splice_init(&obd->obd_lock_replay_queue, &abort_list);
1809         spin_unlock(&obd->obd_recovery_task_lock);
1810         list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1811                 DEBUG_REQ(D_ERROR, req, "aborted:");
1812                 req->rq_status = -ENOTCONN;
1813                 if (ptlrpc_error(req)) {
1814                         DEBUG_REQ(D_ERROR, req,
1815                                   "failed abort_lock_reply; skipping");
1816                 }
1817                 target_request_copy_put(req);
1818         }
1819 }
1820
1821 /*
1822  * Called from a cleanup function if the device is being cleaned up
1823  * forcefully.  The exports should all have been disconnected already,
1824  * the only thing left to do is
1825  * - clear the recovery flags
1826  * - cancel the timer
1827  * - free queued requests and replies, but don't send replies
1828  * Because the obd_stopping flag is set, no new requests should be received.
1829  */
1830 void target_cleanup_recovery(struct obd_device *obd)
1831 {
1832         struct ptlrpc_request *req, *n;
1833         LIST_HEAD(clean_list);
1834
1835         spin_lock(&obd->obd_dev_lock);
1836         if (!obd->obd_recovering) {
1837                 spin_unlock(&obd->obd_dev_lock);
1838                 EXIT;
1839                 return;
1840         }
1841         obd->obd_recovering = obd->obd_abort_recovery = 0;
1842         spin_unlock(&obd->obd_dev_lock);
1843
1844         spin_lock(&obd->obd_recovery_task_lock);
1845         target_cancel_recovery_timer(obd);
1846         list_splice_init(&obd->obd_req_replay_queue, &clean_list);
1847         spin_unlock(&obd->obd_recovery_task_lock);
1848
1849         list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1850                 LASSERT(req->rq_reply_state == NULL);
1851                 target_exp_dequeue_req_replay(req);
1852                 target_request_copy_put(req);
1853         }
1854
1855         spin_lock(&obd->obd_recovery_task_lock);
1856         list_splice_init(&obd->obd_lock_replay_queue, &clean_list);
1857         list_splice_init(&obd->obd_final_req_queue, &clean_list);
1858         spin_unlock(&obd->obd_recovery_task_lock);
1859
1860         list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1861                 LASSERT(req->rq_reply_state == NULL);
1862                 target_request_copy_put(req);
1863         }
1864
1865         EXIT;
1866 }
1867 EXPORT_SYMBOL(target_cleanup_recovery);
1868
1869 /* obd_recovery_task_lock should be held */
1870 void target_cancel_recovery_timer(struct obd_device *obd)
1871 {
1872         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1873         hrtimer_cancel(&obd->obd_recovery_timer);
1874 }
1875
1876 static void target_start_recovery_timer(struct obd_device *obd)
1877 {
1878         ktime_t delay;
1879
1880         if (obd->obd_recovery_start != 0)
1881                 return;
1882
1883         spin_lock(&obd->obd_dev_lock);
1884         if (!obd->obd_recovering || obd->obd_abort_recovery) {
1885                 spin_unlock(&obd->obd_dev_lock);
1886                 return;
1887         }
1888
1889         LASSERT(obd->obd_recovery_timeout != 0);
1890
1891         if (obd->obd_recovery_start != 0) {
1892                 spin_unlock(&obd->obd_dev_lock);
1893                 return;
1894         }
1895
1896         obd->obd_recovery_start = ktime_get_seconds();
1897         delay = ktime_set(obd->obd_recovery_start +
1898                           obd->obd_recovery_timeout, 0);
1899         hrtimer_start(&obd->obd_recovery_timer, delay, HRTIMER_MODE_ABS);
1900         spin_unlock(&obd->obd_dev_lock);
1901
1902         LCONSOLE_WARN("%s: Will be in recovery for at least %u:%02u, or until %d client%s reconnect%s\n",
1903                       obd->obd_name,
1904                       obd->obd_recovery_timeout / 60,
1905                       obd->obd_recovery_timeout % 60,
1906                       atomic_read(&obd->obd_max_recoverable_clients),
1907                       (atomic_read(&obd->obd_max_recoverable_clients) == 1) ?
1908                       "" : "s",
1909                       (atomic_read(&obd->obd_max_recoverable_clients) == 1) ?
1910                       "s" : "");
1911 }
1912
1913 /**
1914  * extend recovery window.
1915  *
1916  * if @extend is true, extend recovery window to have @dr_timeout remaining
1917  * at least; otherwise, make sure the recovery timeout value is not less
1918  * than @dr_timeout.
1919  */
1920 static void extend_recovery_timer(struct obd_device *obd, timeout_t dr_timeout,
1921                                   bool extend)
1922 {
1923         ktime_t left_ns;
1924         timeout_t timeout;
1925         timeout_t left;
1926
1927         spin_lock(&obd->obd_dev_lock);
1928         if (!obd->obd_recovering || obd->obd_abort_recovery ||
1929             obd->obd_stopping) {
1930                 spin_unlock(&obd->obd_dev_lock);
1931                 return;
1932         }
1933         LASSERT(obd->obd_recovery_start != 0);
1934
1935         left_ns = hrtimer_get_remaining(&obd->obd_recovery_timer);
1936         left = ktime_divns(left_ns, NSEC_PER_SEC);
1937
1938         if (extend) {
1939                 timeout = obd->obd_recovery_timeout;
1940                 /* dr_timeout will happen after the hrtimer has expired.
1941                  * Add the excess time to the soft recovery timeout without
1942                  * exceeding the hard recovery timeout.
1943                  */
1944                 if (dr_timeout > left) {
1945                         timeout += dr_timeout - left;
1946                         timeout = min_t(timeout_t, obd->obd_recovery_time_hard,
1947                                         timeout);
1948                 }
1949         } else {
1950                 timeout = clamp_t(timeout_t, dr_timeout,
1951                                   obd->obd_recovery_timeout,
1952                                   obd->obd_recovery_time_hard);
1953         }
1954
1955         if (timeout == obd->obd_recovery_time_hard)
1956                 CWARN("%s: extended recovery timer reached hard limit: %d, extend: %d\n",
1957                       obd->obd_name, timeout, extend);
1958
1959         if (obd->obd_recovery_timeout < timeout) {
1960                 ktime_t end, now;
1961
1962                 obd->obd_recovery_timeout = timeout;
1963                 end = ktime_set(obd->obd_recovery_start + timeout, 0);
1964                 now = ktime_set(ktime_get_seconds(), 0);
1965                 left_ns = ktime_sub(end, now);
1966                 hrtimer_start(&obd->obd_recovery_timer, end, HRTIMER_MODE_ABS);
1967                 left = ktime_divns(left_ns, NSEC_PER_SEC);
1968         }
1969         spin_unlock(&obd->obd_dev_lock);
1970
1971         CDEBUG(D_HA, "%s: recovery timer will expire in %d seconds\n",
1972                 obd->obd_name, left);
1973 }
1974
1975 /* Reset the timer with each new client connection */
1976 /*
1977  * This timer is actually reconnect_timer, which is for making sure
1978  * the total recovery window is at least as big as my reconnect
1979  * attempt timing. So the initial recovery time_out will be set to
1980  * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1981  * from client is bigger than this, then the recovery time_out will
1982  * be extended to make sure the client could be reconnected, in the
1983  * process, the timeout from the new client should be ignored.
1984  */
1985 static void
1986 check_and_start_recovery_timer(struct obd_device *obd,
1987                                struct ptlrpc_request *req,
1988                                int new_client)
1989 {
1990         timeout_t service_timeout = lustre_msg_get_service_timeout(req->rq_reqmsg);
1991         struct obd_device_target *obt = &obd->u.obt;
1992
1993         if (!new_client && service_timeout)
1994                 /*
1995                  * Teach server about old server's estimates, as first guess
1996                  * at how long new requests will take.
1997                  */
1998                 at_measured(&req->rq_rqbd->rqbd_svcpt->scp_at_estimate,
1999                             service_timeout);
2000
2001         target_start_recovery_timer(obd);
2002
2003         /*
2004          * Convert the service time to RPC timeout,
2005          * and reuse service_timeout to limit stack usage.
2006          */
2007         service_timeout = at_est2timeout(service_timeout);
2008
2009         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_SLUGGISH_NET) &&
2010             service_timeout < at_extra)
2011                 service_timeout = at_extra;
2012
2013         /*
2014          * We expect other clients to timeout within service_timeout, then try
2015          * to reconnect, then try the failover server.  The max delay between
2016          * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL.
2017          */
2018         service_timeout += 2 * INITIAL_CONNECT_TIMEOUT;
2019
2020         LASSERT(obt->obt_magic == OBT_MAGIC);
2021         service_timeout += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC);
2022         if (service_timeout > obd->obd_recovery_timeout && !new_client)
2023                 extend_recovery_timer(obd, service_timeout, false);
2024 }
2025
2026 /** Health checking routines */
2027 static inline int exp_connect_healthy(struct obd_export *exp)
2028 {
2029         return exp->exp_in_recovery;
2030 }
2031
2032 /** if export done req_replay or has replay in queue */
2033 static inline int exp_req_replay_healthy(struct obd_export *exp)
2034 {
2035         return (!exp->exp_req_replay_needed ||
2036                 atomic_read(&exp->exp_replay_count) > 0);
2037 }
2038
2039
2040 static inline int exp_req_replay_healthy_or_from_mdt(struct obd_export *exp)
2041 {
2042         return (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) ||
2043                exp_req_replay_healthy(exp);
2044 }
2045
2046 /** if export done lock_replay or has replay in queue */
2047 static inline int exp_lock_replay_healthy(struct obd_export *exp)
2048 {
2049         return (!exp->exp_lock_replay_needed ||
2050                 atomic_read(&exp->exp_replay_count) > 0);
2051 }
2052
2053 static inline int exp_vbr_healthy(struct obd_export *exp)
2054 {
2055         return !exp->exp_vbr_failed;
2056 }
2057
2058 static inline int exp_finished(struct obd_export *exp)
2059 {
2060         return exp->exp_in_recovery && !exp->exp_lock_replay_needed;
2061 }
2062
2063 static inline int exp_finished_or_from_mdt(struct obd_export *exp)
2064 {
2065         return (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) ||
2066                 exp_finished(exp);
2067 }
2068
2069 static int check_for_next_transno(struct lu_target *lut)
2070 {
2071         struct ptlrpc_request *req = NULL;
2072         struct obd_device *obd = lut->lut_obd;
2073         struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2074         int wake_up = 0, connected, completed, queue_len;
2075         __u64 req_transno = 0;
2076         __u64 update_transno = 0;
2077         __u64 next_transno = 0;
2078
2079         ENTRY;
2080
2081         spin_lock(&obd->obd_recovery_task_lock);
2082         if (!list_empty(&obd->obd_req_replay_queue)) {
2083                 req = list_entry(obd->obd_req_replay_queue.next,
2084                                      struct ptlrpc_request, rq_list);
2085                 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
2086         }
2087
2088         if (!obd->obd_abort_recov_mdt && tdtd)
2089                 update_transno = distribute_txn_get_next_transno(tdtd);
2090
2091         connected = atomic_read(&obd->obd_connected_clients);
2092         completed = connected - atomic_read(&obd->obd_req_replay_clients);
2093         queue_len = obd->obd_requests_queued_for_recovery;
2094         next_transno = obd->obd_next_recovery_transno;
2095
2096         CDEBUG(D_HA,
2097                "max: %d, connected: %d, completed: %d, queue_len: %d, req_transno: %llu, next_transno: %llu\n",
2098                atomic_read(&obd->obd_max_recoverable_clients),
2099                connected, completed,
2100                queue_len, req_transno, next_transno);
2101
2102         if (obd->obd_abort_recovery) {
2103                 CDEBUG(D_HA, "waking for aborted recovery\n");
2104                 wake_up = 1;
2105         } else if (obd->obd_recovery_expired) {
2106                 CDEBUG(D_HA, "waking for expired recovery\n");
2107                 wake_up = 1;
2108         } else if (!obd->obd_abort_recov_mdt && tdtd && req &&
2109                    is_req_replayed_by_update(req)) {
2110                 LASSERTF(req_transno < next_transno,
2111                          "req_transno %llu next_transno%llu\n", req_transno,
2112                          next_transno);
2113                 CDEBUG(D_HA, "waking for duplicate req (%llu)\n",
2114                        req_transno);
2115                 wake_up = 1;
2116         } else if (req_transno == next_transno ||
2117                    (update_transno != 0 && update_transno <= next_transno)) {
2118                 CDEBUG(D_HA, "waking for next (%lld)\n", next_transno);
2119                 wake_up = 1;
2120         } else if (queue_len > 0 &&
2121                    queue_len == atomic_read(&obd->obd_req_replay_clients)) {
2122                 /** handle gaps occured due to lost reply or VBR */
2123                 LASSERTF(req_transno >= next_transno,
2124                          "req_transno: %llu, next_transno: %llu\n",
2125                          req_transno, next_transno);
2126                 CDEBUG(D_HA,
2127                        "%s: waking for gap in transno, VBR is %s (skip: %lld, ql: %d, comp: %d, conn: %d, next: %lld, next_update %lld last_committed: %lld)\n",
2128                        obd->obd_name, obd->obd_version_recov ? "ON" : "OFF",
2129                        next_transno, queue_len, completed, connected,
2130                        req_transno, update_transno, obd->obd_last_committed);
2131                 obd->obd_next_recovery_transno = req_transno;
2132                 wake_up = 1;
2133         } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
2134                 CDEBUG(D_HA, "waking for completed recovery\n");
2135                 wake_up = 1;
2136         } else if (OBD_FAIL_CHECK(OBD_FAIL_MDS_RECOVERY_ACCEPTS_GAPS)) {
2137                 CDEBUG(D_HA,
2138                        "accepting transno gaps is explicitly allowed by fail_lock, waking up (%lld)\n",
2139                        next_transno);
2140                 obd->obd_next_recovery_transno = req_transno;
2141                 wake_up = 1;
2142         }
2143         spin_unlock(&obd->obd_recovery_task_lock);
2144         return wake_up;
2145 }
2146
2147 static int check_for_next_lock(struct lu_target *lut)
2148 {
2149         struct obd_device *obd = lut->lut_obd;
2150         int wake_up = 0;
2151
2152         spin_lock(&obd->obd_recovery_task_lock);
2153         if (!list_empty(&obd->obd_lock_replay_queue)) {
2154                 CDEBUG(D_HA, "waking for next lock\n");
2155                 wake_up = 1;
2156         } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
2157                 CDEBUG(D_HA, "waking for completed lock replay\n");
2158                 wake_up = 1;
2159         } else if (obd->obd_abort_recovery) {
2160                 CDEBUG(D_HA, "waking for aborted recovery\n");
2161                 wake_up = 1;
2162         } else if (obd->obd_recovery_expired) {
2163                 CDEBUG(D_HA, "waking for expired recovery\n");
2164                 wake_up = 1;
2165         }
2166         spin_unlock(&obd->obd_recovery_task_lock);
2167
2168         return wake_up;
2169 }
2170
2171 static int check_update_llog(struct lu_target *lut)
2172 {
2173         struct obd_device *obd = lut->lut_obd;
2174         struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2175
2176         if (obd->obd_abort_recovery) {
2177                 CDEBUG(D_HA, "waking for aborted recovery\n");
2178                 return 1;
2179         }
2180
2181         if (atomic_read(&tdtd->tdtd_recovery_threads_count) == 0) {
2182                 CDEBUG(D_HA, "waking for completion of reading update log\n");
2183                 return 1;
2184         }
2185
2186         return 0;
2187 }
2188
2189 /**
2190  * wait for recovery events,
2191  * check its status with help of check_routine
2192  * evict dead clients via health_check
2193  */
2194 static int target_recovery_overseer(struct lu_target *lut,
2195                                     int (*check_routine)(struct lu_target *),
2196                                     int (*health_check)(struct obd_export *))
2197 {
2198         struct obd_device *obd = lut->lut_obd;
2199         struct target_distribute_txn_data *tdtd;
2200         time64_t last = 0;
2201         time64_t now;
2202 repeat:
2203         if (obd->obd_recovering && obd->obd_recovery_start == 0) {
2204                 now = ktime_get_seconds();
2205                 if (now - last > 600) {
2206                         LCONSOLE_INFO("%s: in recovery but waiting for the first client to connect\n",
2207                                       obd->obd_name);
2208                         last = now;
2209                 }
2210         }
2211         if (obd->obd_recovery_start != 0 && ktime_get_seconds() >=
2212               (obd->obd_recovery_start + obd->obd_recovery_time_hard)) {
2213                 __u64 next_update_transno = 0;
2214
2215                 /*
2216                  * Only abort the recovery if there are no update recovery
2217                  * left in the queue
2218                  */
2219                 spin_lock(&obd->obd_recovery_task_lock);
2220                 if (!obd->obd_abort_recov_mdt && lut->lut_tdtd) {
2221                         next_update_transno =
2222                                 distribute_txn_get_next_transno(lut->lut_tdtd);
2223
2224                         tdtd = lut->lut_tdtd;
2225                         /*
2226                          * If next_update_transno == 0, it probably because
2227                          * updatelog retrieve threads did not get any records
2228                          * yet, let's wait those threads stopped
2229                          */
2230                         if (next_update_transno == 0) {
2231                                 spin_unlock(&obd->obd_recovery_task_lock);
2232
2233                                 while (wait_event_timeout(
2234                                         tdtd->tdtd_recovery_threads_waitq,
2235                                         check_update_llog(lut),
2236                                         cfs_time_seconds(60)) == 0);
2237
2238                                 spin_lock(&obd->obd_recovery_task_lock);
2239                                 next_update_transno =
2240                                         distribute_txn_get_next_transno(tdtd);
2241                         }
2242                 }
2243
2244                 if (next_update_transno != 0 && !obd->obd_abort_recovery) {
2245                         obd->obd_next_recovery_transno = next_update_transno;
2246                         spin_unlock(&obd->obd_recovery_task_lock);
2247                         /*
2248                          * Disconnect unfinished exports from clients, and
2249                          * keep connection from MDT to make sure the update
2250                          * recovery will still keep trying until some one
2251                          * manually abort the recovery
2252                          */
2253                         class_disconnect_stale_exports(obd,
2254                                                 exp_finished_or_from_mdt);
2255                         /* Abort all of replay & replay lock req from clients */
2256                         abort_req_replay_queue(obd);
2257                         abort_lock_replay_queue(obd);
2258                         CDEBUG(D_HA,
2259                                "%s: there are still update replay (%#llx)in the queue.\n",
2260                                obd->obd_name, next_update_transno);
2261                 } else {
2262                         obd->obd_abort_recovery = 1;
2263                         spin_unlock(&obd->obd_recovery_task_lock);
2264                         CWARN("%s recovery is aborted by hard timeout\n",
2265                               obd->obd_name);
2266                 }
2267         }
2268
2269         while (wait_event_timeout(obd->obd_next_transno_waitq,
2270                                   check_routine(lut),
2271                                   cfs_time_seconds(60)) == 0)
2272                 ; /* wait indefinitely for event, but don't trigger watchdog */
2273
2274         if (obd->obd_abort_recovery) {
2275                 CWARN("recovery is aborted, evict exports in recovery\n");
2276                 if (lut->lut_tdtd != NULL) {
2277                         tdtd = lut->lut_tdtd;
2278                         /*
2279                          * Let's wait all of the update log recovery thread
2280                          * finished
2281                          */
2282                         wait_event_idle(
2283                                 tdtd->tdtd_recovery_threads_waitq,
2284                                 atomic_read(&tdtd->tdtd_recovery_threads_count)
2285                                 == 0);
2286                         /* Then abort the update recovery list */
2287                         dtrq_list_destroy(lut->lut_tdtd);
2288                 }
2289
2290                 /** evict exports which didn't finish recovery yet */
2291                 class_disconnect_stale_exports(obd, exp_finished);
2292                 return 1;
2293         } else if (obd->obd_recovery_expired) {
2294                 obd->obd_recovery_expired = 0;
2295
2296                 /** If some clients died being recovered, evict them */
2297                 LCONSOLE_WARN("%s: recovery is timed out, evict stale exports\n",
2298                               obd->obd_name);
2299                 /** evict cexports with no replay in queue, they are stalled */
2300                 class_disconnect_stale_exports(obd, health_check);
2301
2302                 /** continue with VBR */
2303                 spin_lock(&obd->obd_dev_lock);
2304                 obd->obd_version_recov = 1;
2305                 spin_unlock(&obd->obd_dev_lock);
2306                 /**
2307                  * reset timer, recovery will proceed with versions now,
2308                  * timeout is set just to handle reconnection delays
2309                  */
2310                 extend_recovery_timer(obd, RECONNECT_DELAY_MAX, true);
2311                 /**
2312                  * Wait for recovery events again, after evicting bad clients
2313                  */
2314                 goto repeat;
2315         }
2316         return 0;
2317 }
2318
2319 static struct ptlrpc_request *target_next_replay_lock(struct lu_target *lut)
2320 {
2321         struct obd_device *obd = lut->lut_obd;
2322         struct ptlrpc_request *req = NULL;
2323
2324         CDEBUG(D_HA, "Waiting for lock\n");
2325         if (target_recovery_overseer(lut, check_for_next_lock,
2326                                      exp_lock_replay_healthy))
2327                 abort_lock_replay_queue(obd);
2328
2329         spin_lock(&obd->obd_recovery_task_lock);
2330         if (!list_empty(&obd->obd_lock_replay_queue)) {
2331                 req = list_entry(obd->obd_lock_replay_queue.next,
2332                                      struct ptlrpc_request, rq_list);
2333                 list_del_init(&req->rq_list);
2334                 spin_unlock(&obd->obd_recovery_task_lock);
2335         } else {
2336                 spin_unlock(&obd->obd_recovery_task_lock);
2337                 LASSERT(list_empty(&obd->obd_lock_replay_queue));
2338                 LASSERT(atomic_read(&obd->obd_lock_replay_clients) == 0);
2339                 /** evict exports failed VBR */
2340                 class_disconnect_stale_exports(obd, exp_vbr_healthy);
2341         }
2342         return req;
2343 }
2344
2345 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
2346 {
2347         struct ptlrpc_request *req = NULL;
2348
2349         spin_lock(&obd->obd_recovery_task_lock);
2350         if (!list_empty(&obd->obd_final_req_queue)) {
2351                 req = list_entry(obd->obd_final_req_queue.next,
2352                                      struct ptlrpc_request, rq_list);
2353                 list_del_init(&req->rq_list);
2354                 spin_unlock(&obd->obd_recovery_task_lock);
2355                 if (req->rq_export->exp_in_recovery) {
2356                         spin_lock(&req->rq_export->exp_lock);
2357                         req->rq_export->exp_in_recovery = 0;
2358                         spin_unlock(&req->rq_export->exp_lock);
2359                 }
2360         } else {
2361                 spin_unlock(&obd->obd_recovery_task_lock);
2362         }
2363         return req;
2364 }
2365
2366 static void handle_recovery_req(struct ptlrpc_thread *thread,
2367                                 struct ptlrpc_request *req,
2368                                 svc_handler_t handler)
2369 {
2370         ENTRY;
2371
2372         /**
2373          * export can be evicted during recovery, no need to handle replays for
2374          * it after that, discard such request silently
2375          */
2376         if (req->rq_export->exp_disconnected)
2377                 RETURN_EXIT;
2378
2379         req->rq_session.lc_thread = thread;
2380         req->rq_svc_thread = thread;
2381         req->rq_svc_thread->t_env->le_ses = &req->rq_session;
2382
2383         /* thread context */
2384         lu_context_enter(&thread->t_env->le_ctx);
2385         (void)handler(req);
2386         lu_context_exit(&thread->t_env->le_ctx);
2387
2388         req->rq_svc_thread->t_env->le_ses = NULL;
2389
2390         /* don't reset timer for final stage */
2391         if (!exp_finished(req->rq_export)) {
2392                 timeout_t timeout = obd_timeout;
2393
2394                 /**
2395                  * Add request @timeout to the recovery time so next request from
2396                  * this client may come in recovery time
2397                  */
2398                 if (!AT_OFF) {
2399                         struct ptlrpc_service_part *svcpt;
2400                         timeout_t est_timeout;
2401
2402                         svcpt = req->rq_rqbd->rqbd_svcpt;
2403                         /*
2404                          * If the server sent early reply for this request,
2405                          * the client will recalculate the timeout according to
2406                          * current server estimate service time, so we will
2407                          * use the maxium timeout here for waiting the client
2408                          * sending the next req
2409                          */
2410                         est_timeout = at_get(&svcpt->scp_at_estimate);
2411                         timeout = max_t(timeout_t, at_est2timeout(est_timeout),
2412                                         lustre_msg_get_timeout(req->rq_reqmsg));
2413                         /*
2414                          * Add 2 net_latency, one for balance rq_deadline
2415                          * (see ptl_send_rpc), one for resend the req to server,
2416                          * Note: client will pack net_latency in replay req
2417                          * (see ptlrpc_replay_req)
2418                          */
2419                         timeout += 2 * lustre_msg_get_service_timeout(req->rq_reqmsg);
2420                 }
2421                 extend_recovery_timer(class_exp2obd(req->rq_export), timeout,
2422                                       true);
2423         }
2424         EXIT;
2425 }
2426
2427 /** Checking routines for recovery */
2428 static int check_for_recovery_ready(struct lu_target *lut)
2429 {
2430         struct obd_device *obd = lut->lut_obd;
2431         unsigned int clnts = atomic_read(&obd->obd_connected_clients);
2432
2433         CDEBUG(D_HA,
2434                "connected %d stale %d max_recoverable_clients %d abort %d expired %d\n",
2435                clnts, obd->obd_stale_clients,
2436                atomic_read(&obd->obd_max_recoverable_clients),
2437                obd->obd_abort_recovery, obd->obd_recovery_expired);
2438
2439         if (!obd->obd_abort_recovery && !obd->obd_recovery_expired) {
2440                 LASSERT(clnts <=
2441                         atomic_read(&obd->obd_max_recoverable_clients));
2442                 if (clnts + obd->obd_stale_clients <
2443                     atomic_read(&obd->obd_max_recoverable_clients))
2444                         return 0;
2445         }
2446
2447         if (!obd->obd_abort_recov_mdt && lut->lut_tdtd != NULL) {
2448                 if (!lut->lut_tdtd->tdtd_replay_ready &&
2449                     !obd->obd_abort_recovery && !obd->obd_stopping) {
2450                         /*
2451                          * Let's extend recovery timer, in case the recovery
2452                          * timer expired, and some clients got evicted
2453                          */
2454                         extend_recovery_timer(obd, obd->obd_recovery_timeout,
2455                                               true);
2456                         CDEBUG(D_HA,
2457                                "%s update recovery is not ready, extend recovery %d\n",
2458                                obd->obd_name, obd->obd_recovery_timeout);
2459                         return 0;
2460                 }
2461         }
2462
2463         return 1;
2464 }
2465
2466 enum {
2467         REQUEST_RECOVERY = 1,
2468         UPDATE_RECOVERY = 2,
2469 };
2470
2471 static __u64 get_next_replay_req_transno(struct obd_device *obd)
2472 {
2473         __u64 transno = 0;
2474
2475         if (!list_empty(&obd->obd_req_replay_queue)) {
2476                 struct ptlrpc_request *req;
2477
2478                 req = list_entry(obd->obd_req_replay_queue.next,
2479                                  struct ptlrpc_request, rq_list);
2480                 transno = lustre_msg_get_transno(req->rq_reqmsg);
2481         }
2482
2483         return transno;
2484 }
2485
2486 static __u64 get_next_transno(struct lu_target *lut, int *type)
2487 {
2488         struct obd_device *obd = lut->lut_obd;
2489         struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2490         __u64 transno = 0;
2491         __u64 update_transno;
2492
2493         ENTRY;
2494
2495         transno = get_next_replay_req_transno(obd);
2496         if (type != NULL)
2497                 *type = REQUEST_RECOVERY;
2498
2499         if (!tdtd || obd->obd_abort_recov_mdt)
2500                 RETURN(transno);
2501
2502         update_transno = distribute_txn_get_next_transno(tdtd);
2503         if (transno == 0 || (transno >= update_transno &&
2504                              update_transno != 0)) {
2505                 transno = update_transno;
2506                 if (type != NULL)
2507                         *type = UPDATE_RECOVERY;
2508         }
2509
2510         RETURN(transno);
2511 }
2512
2513 /**
2514  * drop duplicate replay request
2515  *
2516  * Because the operation has been replayed by update recovery, the request
2517  * with the same transno will be dropped and also notify the client to send
2518  * next replay request.
2519  *
2520  * \param[in] env       execution environment
2521  * \param[in] obd       failover obd device
2522  * \param[in] req       request to be dropped
2523  */
2524 static void drop_duplicate_replay_req(struct lu_env *env,
2525                                       struct obd_device *obd,
2526                                       struct ptlrpc_request *req)
2527 {
2528         DEBUG_REQ(D_HA, req,
2529                   "remove t%lld from %s because duplicate update records found",
2530                   lustre_msg_get_transno(req->rq_reqmsg),
2531                   libcfs_nid2str(req->rq_peer.nid));
2532
2533         /*
2534          * Right now, only for MDS reint operation update replay and
2535          * normal request replay can have the same transno
2536          */
2537         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_REINT) {
2538                 req_capsule_set(&req->rq_pill, &RQF_MDS_REINT);
2539                 req->rq_status = req_capsule_server_pack(&req->rq_pill);
2540                 if (likely(req->rq_export))
2541                         target_committed_to_req(req);
2542                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
2543                 target_send_reply(req, req->rq_status, 0);
2544         } else {
2545                 DEBUG_REQ(D_ERROR, req, "wrong opc from %s",
2546                 libcfs_nid2str(req->rq_peer.nid));
2547         }
2548         target_exp_dequeue_req_replay(req);
2549         target_request_copy_put(req);
2550         obd->obd_replayed_requests++;
2551 }
2552
2553 #define WATCHDOG_TIMEOUT (obd_timeout * 10)
2554
2555 static void replay_request_or_update(struct lu_env *env,
2556                                      struct lu_target *lut,
2557                                      struct target_recovery_data *trd,
2558                                      struct ptlrpc_thread *thread)
2559 {
2560         struct obd_device *obd = lut->lut_obd;
2561         struct ptlrpc_request *req = NULL;
2562         int type;
2563         __u64 transno;
2564
2565         ENTRY;
2566
2567         CDEBUG(D_HA, "Waiting for transno %lld\n",
2568                obd->obd_next_recovery_transno);
2569
2570         /* Replay all of request and update by transno */
2571         do {
2572                 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2573
2574                 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLAY_DELAY2, cfs_fail_val);
2575
2576                 /**
2577                  * It is needed to extend recovery window above
2578                  *  recovery_time_soft. Extending is possible only in the
2579                  *  end of recovery window (see more details in
2580                  *  handle_recovery_req()).
2581                  */
2582                 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_TGT_REPLAY_DELAY, 300);
2583
2584                 if (target_recovery_overseer(lut, check_for_next_transno,
2585                                         exp_req_replay_healthy_or_from_mdt)) {
2586                         abort_req_replay_queue(obd);
2587                         abort_lock_replay_queue(obd);
2588                         goto abort;
2589                 }
2590
2591                 spin_lock(&obd->obd_recovery_task_lock);
2592                 transno = get_next_transno(lut, &type);
2593                 if (type == REQUEST_RECOVERY && transno != 0) {
2594                         /*
2595                          * Drop replay request from client side, if the
2596                          * replay has been executed by update with the
2597                          * same transno
2598                          */
2599                         req = list_entry(obd->obd_req_replay_queue.next,
2600                                         struct ptlrpc_request, rq_list);
2601
2602                         list_del_init(&req->rq_list);
2603                         obd->obd_requests_queued_for_recovery--;
2604                         spin_unlock(&obd->obd_recovery_task_lock);
2605
2606                         /*
2607                          * Let's check if the request has been redone by
2608                          * update replay
2609                          */
2610                         if (is_req_replayed_by_update(req)) {
2611                                 struct distribute_txn_replay_req *dtrq;
2612
2613                                 dtrq = distribute_txn_lookup_finish_list(tdtd,
2614                                                                       transno);
2615                                 LASSERT(dtrq != NULL);
2616                                 spin_lock(&tdtd->tdtd_replay_list_lock);
2617                                 list_del_init(&dtrq->dtrq_list);
2618                                 spin_unlock(&tdtd->tdtd_replay_list_lock);
2619                                 dtrq_destroy(dtrq);
2620
2621                                 drop_duplicate_replay_req(env, obd, req);
2622
2623                                 continue;
2624                         }
2625
2626                         LASSERT(trd->trd_processing_task == current->pid);
2627                         DEBUG_REQ(D_HA, req, "processing x%llu t%lld from %s",
2628                                   req->rq_xid,
2629                                   lustre_msg_get_transno(req->rq_reqmsg),
2630                                   libcfs_nid2str(req->rq_peer.nid));
2631
2632                         ptlrpc_watchdog_init(&thread->t_watchdog,
2633                                              WATCHDOG_TIMEOUT);
2634                         handle_recovery_req(thread, req,
2635                                             trd->trd_recovery_handler);
2636                         ptlrpc_watchdog_disable(&thread->t_watchdog);
2637
2638                         /**
2639                          * bz18031: increase next_recovery_transno before
2640                          * target_request_copy_put() will drop exp_rpc reference
2641                          */
2642                         spin_lock(&obd->obd_recovery_task_lock);
2643                         obd->obd_next_recovery_transno++;
2644                         spin_unlock(&obd->obd_recovery_task_lock);
2645                         target_exp_dequeue_req_replay(req);
2646                         target_request_copy_put(req);
2647                         obd->obd_replayed_requests++;
2648                 } else if (type == UPDATE_RECOVERY && transno != 0) {
2649                         struct distribute_txn_replay_req *dtrq;
2650                         int rc;
2651
2652                         spin_unlock(&obd->obd_recovery_task_lock);
2653
2654                         LASSERT(tdtd != NULL);
2655                         dtrq = distribute_txn_get_next_req(tdtd);
2656                         lu_context_enter(&thread->t_env->le_ctx);
2657                         ptlrpc_watchdog_init(&thread->t_watchdog,
2658                                              WATCHDOG_TIMEOUT);
2659                         rc = tdtd->tdtd_replay_handler(env, tdtd, dtrq);
2660                         ptlrpc_watchdog_disable(&thread->t_watchdog);
2661                         lu_context_exit(&thread->t_env->le_ctx);
2662                         extend_recovery_timer(obd, obd_timeout, true);
2663
2664                         if (rc == 0 && dtrq->dtrq_xid != 0) {
2665                                 CDEBUG(D_HA,
2666                                        "Move x%llu t%llu to finish list\n",
2667                                        dtrq->dtrq_xid,
2668                                        dtrq->dtrq_master_transno);
2669
2670                                 /* Add it to the replay finish list */
2671                                 spin_lock(&tdtd->tdtd_replay_list_lock);
2672                                 list_add(&dtrq->dtrq_list,
2673                                          &tdtd->tdtd_replay_finish_list);
2674                                 spin_unlock(&tdtd->tdtd_replay_list_lock);
2675
2676                                 spin_lock(&obd->obd_recovery_task_lock);
2677                                 if (transno == obd->obd_next_recovery_transno)
2678                                         obd->obd_next_recovery_transno++;
2679                                 else if (transno >
2680                                          obd->obd_next_recovery_transno)
2681                                         obd->obd_next_recovery_transno =
2682                                                                 transno + 1;
2683                                 spin_unlock(&obd->obd_recovery_task_lock);
2684                         } else {
2685                                 dtrq_destroy(dtrq);
2686                         }
2687                 } else {
2688                         spin_unlock(&obd->obd_recovery_task_lock);
2689 abort:
2690                         LASSERT(list_empty(&obd->obd_req_replay_queue));
2691                         LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
2692                         /** evict exports failed VBR */
2693                         class_disconnect_stale_exports(obd, exp_vbr_healthy);
2694                         break;
2695                 }
2696         } while (1);
2697 }
2698
2699 static int target_recovery_thread(void *arg)
2700 {
2701         struct lu_target *lut = arg;
2702         struct obd_device *obd = lut->lut_obd;
2703         struct ptlrpc_request *req;
2704         struct target_recovery_data *trd = &obd->obd_recovery_data;
2705         unsigned long delta;
2706         struct lu_env *env;
2707         struct ptlrpc_thread *thread = NULL;
2708         int rc = 0;
2709
2710         ENTRY;
2711         OBD_ALLOC_PTR(thread);
2712         if (thread == NULL)
2713                 RETURN(-ENOMEM);
2714
2715         OBD_ALLOC_PTR(env);
2716         if (env == NULL)
2717                 GOTO(out_thread, rc = -ENOMEM);
2718         rc = lu_env_add(env);
2719         if (rc)
2720                 GOTO(out_env, rc);
2721
2722         rc = lu_context_init(&env->le_ctx, LCT_MD_THREAD | LCT_DT_THREAD);
2723         if (rc)
2724                 GOTO(out_env_remove, rc);
2725
2726         thread->t_env = env;
2727         thread->t_id = -1; /* force filter_iobuf_get/put to use local buffers */
2728         thread->t_task = current;
2729         env->le_ctx.lc_thread = thread;
2730         tgt_io_thread_init(thread); /* init thread_big_cache for IO requests */
2731
2732         CDEBUG(D_HA, "%s: started recovery thread pid %d\n", obd->obd_name,
2733                current->pid);
2734         trd->trd_processing_task = current->pid;
2735
2736         spin_lock(&obd->obd_dev_lock);
2737         obd->obd_recovering = 1;
2738         spin_unlock(&obd->obd_dev_lock);
2739         complete(&trd->trd_starting);
2740
2741         /* first of all, we have to know the first transno to replay */
2742         if (target_recovery_overseer(lut, check_for_recovery_ready,
2743                                      exp_connect_healthy)) {
2744                 abort_req_replay_queue(obd);
2745                 abort_lock_replay_queue(obd);
2746                 if (lut->lut_tdtd != NULL)
2747                         dtrq_list_destroy(lut->lut_tdtd);
2748         }
2749
2750         /* next stage: replay requests or update */
2751         delta = jiffies;
2752         CDEBUG(D_INFO, "1: request replay stage - %d clients from t%llu\n",
2753                atomic_read(&obd->obd_req_replay_clients),
2754                obd->obd_next_recovery_transno);
2755         replay_request_or_update(env, lut, trd, thread);
2756
2757         /**
2758          * The second stage: replay locks
2759          */
2760         CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
2761                atomic_read(&obd->obd_lock_replay_clients));
2762         while ((req = target_next_replay_lock(lut))) {
2763                 LASSERT(trd->trd_processing_task == current->pid);
2764                 DEBUG_REQ(D_HA, req, "processing lock from %s:",
2765                           libcfs_nid2str(req->rq_peer.nid));
2766                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_LOCK_REPLAY)) {
2767                         req->rq_status = -ENODEV;
2768                         target_request_copy_put(req);
2769                         continue;
2770                 }
2771                 handle_recovery_req(thread, req,
2772                                     trd->trd_recovery_handler);
2773                 target_request_copy_put(req);
2774                 obd->obd_replayed_locks++;
2775         }
2776
2777         /**
2778          * The third stage: reply on final pings, at this moment all clients
2779          * must have request in final queue
2780          */
2781         CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLAY_RECONNECT, cfs_fail_val);
2782         CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
2783         /** Update server last boot epoch */
2784         tgt_boot_epoch_update(lut);
2785         /*
2786          * We drop recoverying flag to forward all new requests
2787          * to regular mds_handle() since now
2788          */
2789         spin_lock(&obd->obd_dev_lock);
2790         obd->obd_recovering = obd->obd_abort_recovery = 0;
2791         spin_unlock(&obd->obd_dev_lock);
2792         spin_lock(&obd->obd_recovery_task_lock);
2793         target_cancel_recovery_timer(obd);
2794         spin_unlock(&obd->obd_recovery_task_lock);
2795         while ((req = target_next_final_ping(obd))) {
2796                 LASSERT(trd->trd_processing_task == current->pid);
2797                 DEBUG_REQ(D_HA, req, "processing final ping from %s:",
2798                           libcfs_nid2str(req->rq_peer.nid));
2799                 handle_recovery_req(thread, req,
2800                                     trd->trd_recovery_handler);
2801                 /*
2802                  * Because the waiting client can not send ping to server,
2803                  * so we need refresh the last_request_time, to avoid the
2804                  * export is being evicted
2805                  */
2806                 ptlrpc_update_export_timer(req->rq_export, 0);
2807                 target_request_copy_put(req);
2808         }
2809
2810         delta = jiffies_to_msecs(jiffies - delta) / MSEC_PER_SEC;
2811         CDEBUG(D_INFO, "4: recovery completed in %lus - %d/%d reqs/locks\n",
2812                delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
2813         if (delta > OBD_RECOVERY_TIME_SOFT) {
2814                 CWARN("too long recovery - read logs\n");
2815                 libcfs_debug_dumplog();
2816         }
2817
2818         target_finish_recovery(lut);
2819         lu_context_fini(&env->le_ctx);
2820         trd->trd_processing_task = 0;
2821         complete_all(&trd->trd_finishing);
2822         tgt_io_thread_done(thread);
2823 out_env_remove:
2824         lu_env_remove(env);
2825 out_env:
2826         OBD_FREE_PTR(env);
2827 out_thread:
2828         OBD_FREE_PTR(thread);
2829         RETURN(rc);
2830 }
2831
2832 static int target_start_recovery_thread(struct lu_target *lut,
2833                                         svc_handler_t handler)
2834 {
2835         struct obd_device *obd = lut->lut_obd;
2836         int rc = 0;
2837         struct target_recovery_data *trd = &obd->obd_recovery_data;
2838         int index;
2839
2840         memset(trd, 0, sizeof(*trd));
2841         init_completion(&trd->trd_starting);
2842         init_completion(&trd->trd_finishing);
2843         trd->trd_recovery_handler = handler;
2844
2845         rc = server_name2index(obd->obd_name, &index, NULL);
2846         if (rc < 0)
2847                 return rc;
2848
2849         if (!IS_ERR(kthread_run(target_recovery_thread,
2850                                 lut, "tgt_recover_%d", index))) {
2851                 wait_for_completion(&trd->trd_starting);
2852                 LASSERT(obd->obd_recovering != 0);
2853         } else {
2854                 rc = -ECHILD;
2855         }
2856
2857         return rc;
2858 }
2859
2860 void target_stop_recovery_thread(struct obd_device *obd)
2861 {
2862         if (obd->obd_recovery_data.trd_processing_task > 0) {
2863                 struct target_recovery_data *trd = &obd->obd_recovery_data;
2864                 /** recovery can be done but postrecovery is not yet */
2865                 spin_lock(&obd->obd_dev_lock);
2866                 if (obd->obd_recovering) {
2867                         CERROR("%s: Aborting recovery\n", obd->obd_name);
2868                         obd->obd_abort_recovery = 1;
2869                         wake_up(&obd->obd_next_transno_waitq);
2870                 }
2871                 spin_unlock(&obd->obd_dev_lock);
2872                 wait_for_completion(&trd->trd_finishing);
2873         }
2874 }
2875 EXPORT_SYMBOL(target_stop_recovery_thread);
2876
2877 void target_recovery_fini(struct obd_device *obd)
2878 {
2879         class_disconnect_exports(obd);
2880         target_stop_recovery_thread(obd);
2881         target_cleanup_recovery(obd);
2882 }
2883 EXPORT_SYMBOL(target_recovery_fini);
2884
2885 static enum hrtimer_restart target_recovery_expired(struct hrtimer *timer)
2886 {
2887         struct obd_device *obd = container_of(timer, struct obd_device,
2888                                               obd_recovery_timer);
2889
2890         CDEBUG(D_HA,
2891                "%s: recovery timed out; %d clients are still in recovery after %llu seconds (%d clients connected)\n",
2892                obd->obd_name, atomic_read(&obd->obd_lock_replay_clients),
2893                ktime_get_seconds() - obd->obd_recovery_start,
2894                atomic_read(&obd->obd_connected_clients));
2895
2896         obd->obd_recovery_expired = 1;
2897         wake_up(&obd->obd_next_transno_waitq);
2898         return HRTIMER_NORESTART;
2899 }
2900
2901 void target_recovery_init(struct lu_target *lut, svc_handler_t handler)
2902 {
2903         struct obd_device *obd = lut->lut_obd;
2904
2905         if (lut->lut_bottom->dd_rdonly)
2906                 return;
2907
2908         if (atomic_read(&obd->obd_max_recoverable_clients) == 0) {
2909                 /** Update server last boot epoch */
2910                 tgt_boot_epoch_update(lut);
2911                 return;
2912         }
2913
2914         CDEBUG(D_HA, "RECOVERY: service %s, %d recoverable clients, "
2915                "last_transno %llu\n", obd->obd_name,
2916                atomic_read(&obd->obd_max_recoverable_clients),
2917                obd->obd_last_committed);
2918         LASSERT(obd->obd_stopping == 0);
2919         obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
2920         obd->obd_recovery_start = 0;
2921         obd->obd_recovery_end = 0;
2922
2923         hrtimer_init(&obd->obd_recovery_timer, CLOCK_MONOTONIC,
2924                      HRTIMER_MODE_ABS);
2925         obd->obd_recovery_timer.function = &target_recovery_expired;
2926         target_start_recovery_thread(lut, handler);
2927 }
2928 EXPORT_SYMBOL(target_recovery_init);
2929
2930 static int target_process_req_flags(struct obd_device *obd,
2931                                     struct ptlrpc_request *req)
2932 {
2933         struct obd_export *exp = req->rq_export;
2934
2935         LASSERT(exp != NULL);
2936         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2937                 /* client declares he's ready to replay locks */
2938                 spin_lock(&exp->exp_lock);
2939                 if (exp->exp_req_replay_needed) {
2940                         exp->exp_req_replay_needed = 0;
2941                         spin_unlock(&exp->exp_lock);
2942
2943                         LASSERT_ATOMIC_POS(&obd->obd_req_replay_clients);
2944                         atomic_dec(&obd->obd_req_replay_clients);
2945                 } else {
2946                         spin_unlock(&exp->exp_lock);
2947                 }
2948         }
2949         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2950                 /*
2951                  * client declares he's ready to complete recovery
2952                  * so, we put the request on th final queue
2953                  */
2954                 spin_lock(&exp->exp_lock);
2955                 if (exp->exp_lock_replay_needed) {
2956                         exp->exp_lock_replay_needed = 0;
2957                         spin_unlock(&exp->exp_lock);
2958
2959                         LASSERT_ATOMIC_POS(&obd->obd_lock_replay_clients);
2960                         atomic_dec(&obd->obd_lock_replay_clients);
2961                 } else {
2962                         spin_unlock(&exp->exp_lock);
2963                 }
2964         }
2965         return 0;
2966 }
2967
2968 int target_queue_recovery_request(struct ptlrpc_request *req,
2969                                   struct obd_device *obd)
2970 {
2971         __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
2972         struct ptlrpc_request *reqiter;
2973         int inserted = 0;
2974
2975         ENTRY;
2976
2977         if (obd->obd_recovery_data.trd_processing_task == current->pid) {
2978                 /* Processing the queue right now, don't re-add. */
2979                 RETURN(1);
2980         }
2981
2982         target_process_req_flags(obd, req);
2983
2984         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2985                 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_TGT_RECOVERY_REQ_RACE))) {
2986                         if (cfs_fail_val == 1) {
2987                                 cfs_race_state = 1;
2988                                 cfs_fail_val = 0;
2989                                 wake_up(&cfs_race_waitq);
2990
2991                                 schedule_timeout_interruptible(
2992                                         cfs_time_seconds(1));
2993                         }
2994                 }
2995
2996                 /*
2997                  * client declares he's ready to complete recovery
2998                  * so, we put the request on th final queue
2999                  */
3000                 target_request_copy_get(req);
3001                 DEBUG_REQ(D_HA, req, "queue final req");
3002                 wake_up(&obd->obd_next_transno_waitq);
3003                 spin_lock(&obd->obd_recovery_task_lock);
3004                 if (obd->obd_recovering) {
3005                         struct ptlrpc_request *tmp;
3006                         struct ptlrpc_request *duplicate = NULL;
3007
3008                         if (likely(!req->rq_export->exp_replay_done)) {
3009                                 req->rq_export->exp_replay_done = 1;
3010                                 list_add_tail(&req->rq_list,
3011                                               &obd->obd_final_req_queue);
3012                                 spin_unlock(&obd->obd_recovery_task_lock);
3013                                 RETURN(0);
3014                         }
3015
3016                         /*
3017                          * XXX O(n), but only happens if final ping is
3018                          * timed out, probably reorganize the list as
3019                          * a hash list later
3020                          */
3021                         list_for_each_entry_safe(reqiter, tmp,
3022                                                  &obd->obd_final_req_queue,
3023                                                  rq_list) {
3024                                 if (reqiter->rq_export == req->rq_export) {
3025                                         list_del_init(&reqiter->rq_list);
3026                                         duplicate = reqiter;
3027                                         break;
3028                                 }
3029                         }
3030
3031                         list_add_tail(&req->rq_list,
3032                                       &obd->obd_final_req_queue);
3033                         req->rq_export->exp_replay_done = 1;
3034                         spin_unlock(&obd->obd_recovery_task_lock);
3035
3036                         if (duplicate != NULL) {
3037                                 DEBUG_REQ(D_HA, duplicate,
3038                                           "put prev final req");
3039                                 target_request_copy_put(duplicate);
3040                         }
3041                         RETURN(0);
3042                 } else {
3043                         spin_unlock(&obd->obd_recovery_task_lock);
3044                         target_request_copy_put(req);
3045                         RETURN(obd->obd_stopping ? -ENOTCONN : 1);
3046                 }
3047         }
3048         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
3049                 /* client declares he's ready to replay locks */
3050                 target_request_copy_get(req);
3051                 DEBUG_REQ(D_HA, req, "queue lock replay req");
3052                 wake_up(&obd->obd_next_transno_waitq);
3053                 spin_lock(&obd->obd_recovery_task_lock);
3054                 LASSERT(obd->obd_recovering);
3055                 /* usually due to recovery abort */
3056                 if (!req->rq_export->exp_in_recovery) {
3057                         spin_unlock(&obd->obd_recovery_task_lock);
3058                         target_request_copy_put(req);
3059                         RETURN(-ENOTCONN);
3060                 }
3061                 LASSERT(req->rq_export->exp_lock_replay_needed);
3062                 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
3063                 spin_unlock(&obd->obd_recovery_task_lock);
3064                 RETURN(0);
3065         }
3066
3067         /*
3068          * CAVEAT EMPTOR: The incoming request message has been swabbed
3069          * (i.e. buflens etc are in my own byte order), but type-dependent
3070          * buffers (eg mdt_body, ost_body etc) have NOT been swabbed.
3071          */
3072
3073         if (!transno) {
3074                 INIT_LIST_HEAD(&req->rq_list);
3075                 DEBUG_REQ(D_HA, req, "not queueing");
3076                 RETURN(1);
3077         }
3078
3079         /*
3080          * If we're processing the queue, we want don't want to queue this
3081          * message.
3082          *
3083          * Also, if this request has a transno less than the one we're waiting
3084          * for, we should process it now.  It could (and currently always will)
3085          * be an open request for a descriptor that was opened some time ago.
3086          *
3087          * Also, a resent, replayed request that has already been
3088          * handled will pass through here and be processed immediately.
3089          */
3090         CDEBUG(D_HA,
3091                "Next recovery transno: %llu, current: %llu, replaying\n",
3092                obd->obd_next_recovery_transno, transno);
3093
3094         /*
3095          * If the request has been replayed by update replay, then sends this
3096          * request to the recovery thread (replay_request_or_update()), where
3097          * it will be handled
3098          */
3099         spin_lock(&obd->obd_recovery_task_lock);
3100         if (transno < obd->obd_next_recovery_transno &&
3101             !is_req_replayed_by_update(req)) {
3102                 /* Processing the queue right now, don't re-add. */
3103                 LASSERT(list_empty(&req->rq_list));
3104                 spin_unlock(&obd->obd_recovery_task_lock);
3105                 RETURN(1);
3106         }
3107         spin_unlock(&obd->obd_recovery_task_lock);
3108
3109         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
3110                 RETURN(0);
3111
3112         target_request_copy_get(req);
3113         if (!req->rq_export->exp_in_recovery) {
3114                 target_request_copy_put(req);
3115                 RETURN(-ENOTCONN);
3116         }
3117         LASSERT(req->rq_export->exp_req_replay_needed);
3118
3119         if (target_exp_enqueue_req_replay(req)) {
3120                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
3121                 target_request_copy_put(req);
3122                 RETURN(0);
3123         }
3124
3125         /* XXX O(n^2) */
3126         spin_lock(&obd->obd_recovery_task_lock);
3127         LASSERT(obd->obd_recovering);
3128         list_for_each_entry(reqiter, &obd->obd_req_replay_queue, rq_list) {
3129                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
3130                         list_add_tail(&req->rq_list, &reqiter->rq_list);
3131                         inserted = 1;
3132                         goto added;
3133                 }
3134
3135                 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
3136                              transno)) {
3137                         DEBUG_REQ(D_ERROR, req,
3138                                   "dropping replay: transno has been claimed by another client");
3139                         spin_unlock(&obd->obd_recovery_task_lock);
3140                         target_exp_dequeue_req_replay(req);
3141                         target_request_copy_put(req);
3142                         RETURN(0);
3143                 }
3144         }
3145 added:
3146         if (!inserted)
3147                 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
3148
3149         obd->obd_requests_queued_for_recovery++;
3150         spin_unlock(&obd->obd_recovery_task_lock);
3151         wake_up(&obd->obd_next_transno_waitq);
3152         RETURN(0);
3153 }
3154
3155 void target_committed_to_req(struct ptlrpc_request *req)
3156 {
3157         struct obd_export *exp = req->rq_export;
3158
3159         if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL)
3160                 lustre_msg_set_last_committed(req->rq_repmsg,
3161                                               exp->exp_last_committed);
3162         else
3163                 DEBUG_REQ(D_IOCTL, req,
3164                           "not sending last_committed update (%d/%d)",
3165                           exp->exp_obd->obd_no_transno,
3166                           req->rq_repmsg == NULL);
3167
3168         CDEBUG(D_INFO, "last_committed %llu, transno %llu, xid %llu\n",
3169                exp->exp_last_committed, req->rq_transno, req->rq_xid);
3170 }
3171
3172 #endif /* HAVE_SERVER_SUPPORT */
3173
3174 /**
3175  * Packs current SLV and Limit into \a req.
3176  */
3177 int target_pack_pool_reply(struct ptlrpc_request *req)
3178 {
3179         struct obd_device *obd;
3180
3181         ENTRY;
3182
3183         /*
3184          * Check that we still have all structures alive as this may
3185          * be some late RPC at shutdown time.
3186          */
3187         if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
3188                      !exp_connect_lru_resize(req->rq_export))) {
3189                 lustre_msg_set_slv(req->rq_repmsg, 0);
3190                 lustre_msg_set_limit(req->rq_repmsg, 0);
3191                 RETURN(0);
3192         }
3193
3194         /* OBD is alive here as export is alive, which we checked above. */
3195         obd = req->rq_export->exp_obd;
3196
3197         read_lock(&obd->obd_pool_lock);
3198         lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
3199         lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
3200         read_unlock(&obd->obd_pool_lock);
3201
3202         RETURN(0);
3203 }
3204
3205 static int target_send_reply_msg(struct ptlrpc_request *req,
3206                                  int rc, int fail_id)
3207 {
3208         if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
3209                 DEBUG_REQ(D_ERROR, req, "dropping reply");
3210                 return -ECOMM;
3211         }
3212         /*
3213          * We can have a null rq_reqmsg in the event of bad signature or
3214          * no context when unwrapping
3215          */
3216         if (req->rq_reqmsg &&
3217             unlikely(lustre_msg_get_opc(req->rq_reqmsg) == MDS_REINT &&
3218             OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_MULTI_NET_REP)))
3219                 return -ECOMM;
3220
3221         if (unlikely(rc)) {
3222                 DEBUG_REQ(D_NET, req, "processing error (%d)", rc);
3223                 req->rq_status = rc;
3224                 return ptlrpc_send_error(req, 1);
3225         }
3226         DEBUG_REQ(D_NET, req, "sending reply");
3227
3228         return ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT);
3229 }
3230
3231 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
3232 {
3233         struct ptlrpc_service_part *svcpt;
3234         int netrc;
3235         struct ptlrpc_reply_state *rs;
3236         struct obd_export *exp;
3237
3238         ENTRY;
3239
3240         if (req->rq_no_reply) {
3241                 EXIT;
3242                 return;
3243         }
3244
3245         svcpt = req->rq_rqbd->rqbd_svcpt;
3246         rs = req->rq_reply_state;
3247         if (rs == NULL || !rs->rs_difficult) {
3248                 /* no notifiers */
3249                 target_send_reply_msg(req, rc, fail_id);
3250                 EXIT;
3251                 return;
3252         }
3253
3254         /* must be an export if locks saved */
3255         LASSERT(req->rq_export != NULL);
3256         /* req/reply consistent */
3257         LASSERT(rs->rs_svcpt == svcpt);
3258
3259         /* "fresh" reply */
3260         LASSERT(!rs->rs_scheduled);
3261         LASSERT(!rs->rs_scheduled_ever);
3262         LASSERT(!rs->rs_handled);
3263         LASSERT(!rs->rs_on_net);
3264         LASSERT(rs->rs_export == NULL);
3265         LASSERT(list_empty(&rs->rs_obd_list));
3266         LASSERT(list_empty(&rs->rs_exp_list));
3267
3268         exp = class_export_get(req->rq_export);
3269
3270         /* disable reply scheduling while I'm setting up */
3271         rs->rs_scheduled = 1;
3272         rs->rs_on_net    = 1;
3273         rs->rs_xid       = req->rq_xid;
3274         rs->rs_transno   = req->rq_transno;
3275         rs->rs_export    = exp;
3276         rs->rs_opc       = lustre_msg_get_opc(req->rq_reqmsg);
3277
3278         spin_lock(&exp->exp_uncommitted_replies_lock);
3279         CDEBUG(D_NET, "rs transno = %llu, last committed = %llu\n",
3280                rs->rs_transno, exp->exp_last_committed);
3281         if (rs->rs_transno > exp->exp_last_committed) {
3282                 /* not committed already */
3283                 list_add_tail(&rs->rs_obd_list,
3284                                   &exp->exp_uncommitted_replies);
3285         }
3286         spin_unlock(&exp->exp_uncommitted_replies_lock);
3287
3288         spin_lock(&exp->exp_lock);
3289         list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
3290         spin_unlock(&exp->exp_lock);
3291
3292         netrc = target_send_reply_msg(req, rc, fail_id);
3293
3294         spin_lock(&svcpt->scp_rep_lock);
3295
3296         atomic_inc(&svcpt->scp_nreps_difficult);
3297
3298         if (netrc != 0) {
3299                 /*
3300                  * error sending: reply is off the net.  Also we need +1
3301                  * reply ref until ptlrpc_handle_rs() is done
3302                  * with the reply state (if the send was successful, there
3303                  * would have been +1 ref for the net, which
3304                  * reply_out_callback leaves alone)
3305                  */
3306                 rs->rs_on_net = 0;
3307                 ptlrpc_rs_addref(rs);
3308         }
3309
3310         spin_lock(&rs->rs_lock);
3311         if (rs->rs_transno <= exp->exp_last_committed ||
3312             (!rs->rs_on_net && !rs->rs_no_ack) ||
3313             list_empty(&rs->rs_exp_list) ||     /* completed already */
3314             list_empty(&rs->rs_obd_list)) {
3315                 CDEBUG(D_HA, "Schedule reply immediately\n");
3316                 ptlrpc_dispatch_difficult_reply(rs);
3317         } else {
3318                 list_add(&rs->rs_list, &svcpt->scp_rep_active);
3319                 rs->rs_scheduled = 0;   /* allow notifier to schedule */
3320         }
3321         spin_unlock(&rs->rs_lock);
3322         spin_unlock(&svcpt->scp_rep_lock);
3323         EXIT;
3324 }
3325
3326 enum ldlm_mode lck_compat_array[] = {
3327         [LCK_EX]    = LCK_COMPAT_EX,
3328         [LCK_PW]    = LCK_COMPAT_PW,
3329         [LCK_PR]    = LCK_COMPAT_PR,
3330         [LCK_CW]    = LCK_COMPAT_CW,
3331         [LCK_CR]    = LCK_COMPAT_CR,
3332         [LCK_NL]    = LCK_COMPAT_NL,
3333         [LCK_GROUP] = LCK_COMPAT_GROUP,
3334         [LCK_COS]   = LCK_COMPAT_COS,
3335 };
3336
3337 /**
3338  * Rather arbitrary mapping from LDLM error codes to errno values. This should
3339  * not escape to the user level.
3340  */
3341 int ldlm_error2errno(enum ldlm_error error)
3342 {
3343         int result;
3344
3345         switch (error) {
3346         case ELDLM_OK:
3347         case ELDLM_LOCK_MATCHED:
3348                 result = 0;
3349                 break;
3350         case ELDLM_LOCK_CHANGED:
3351                 result = -ESTALE;
3352                 break;
3353         case ELDLM_LOCK_ABORTED:
3354                 result = -ENAVAIL;
3355                 break;
3356         case ELDLM_LOCK_REPLACED:
3357                 result = -ESRCH;
3358                 break;
3359         case ELDLM_NO_LOCK_DATA:
3360                 result = -ENOENT;
3361                 break;
3362         case ELDLM_NAMESPACE_EXISTS:
3363                 result = -EEXIST;
3364                 break;
3365         case ELDLM_BAD_NAMESPACE:
3366                 result = -EBADF;
3367                 break;
3368         default:
3369                 if (((int)error) < 0) { /* cast to signed type */
3370                         result = error; /* as ldlm_error can be unsigned */
3371                 } else {
3372                         CERROR("Invalid DLM result code: %d\n", error);
3373                         result = -EPROTO;
3374                 }
3375         }
3376         return result;
3377 }
3378 EXPORT_SYMBOL(ldlm_error2errno);
3379
3380 /**
3381  * Dual to ldlm_error2errno(): maps errno values back to enum ldlm_error.
3382  */
3383 enum ldlm_error ldlm_errno2error(int err_no)
3384 {
3385         int error;
3386
3387         switch (err_no) {
3388         case 0:
3389                 error = ELDLM_OK;
3390                 break;
3391         case -ESTALE:
3392                 error = ELDLM_LOCK_CHANGED;
3393                 break;
3394         case -ENAVAIL:
3395                 error = ELDLM_LOCK_ABORTED;
3396                 break;
3397         case -ESRCH:
3398                 error = ELDLM_LOCK_REPLACED;
3399                 break;
3400         case -ENOENT:
3401                 error = ELDLM_NO_LOCK_DATA;
3402                 break;
3403         case -EEXIST:
3404                 error = ELDLM_NAMESPACE_EXISTS;
3405                 break;
3406         case -EBADF:
3407                 error = ELDLM_BAD_NAMESPACE;
3408                 break;
3409         default:
3410                 error = err_no;
3411         }
3412         return error;
3413 }
3414
3415 #if LUSTRE_TRACKS_LOCK_EXP_REFS
3416 void ldlm_dump_export_locks(struct obd_export *exp)
3417 {
3418         spin_lock(&exp->exp_locks_list_guard);
3419         if (!list_empty(&exp->exp_locks_list)) {
3420                 struct ldlm_lock *lock;
3421
3422                 CERROR("dumping locks for export %p, ignore if the unmount doesn't hang\n",
3423                        exp);
3424                 list_for_each_entry(lock, &exp->exp_locks_list,
3425                                         l_exp_refs_link)
3426                         LDLM_ERROR(lock, "lock:");
3427         }
3428         spin_unlock(&exp->exp_locks_list_guard);
3429 }
3430 #endif
3431
3432 #ifdef HAVE_SERVER_SUPPORT
3433 static inline const char *bulk2type(struct ptlrpc_request *req)
3434 {
3435         if (req->rq_bulk_read)
3436                 return "READ";
3437         if (req->rq_bulk_write)
3438                 return "WRITE";
3439         return "UNKNOWN";
3440 }
3441
3442 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc)
3443 {
3444         struct ptlrpc_request *req = desc->bd_req;
3445         time64_t start = ktime_get_seconds();
3446         time64_t deadline;
3447         int rc = 0;
3448
3449         ENTRY;
3450
3451         /* If there is eviction in progress, wait for it to finish. */
3452         wait_event_idle(
3453                 exp->exp_obd->obd_evict_inprogress_waitq,
3454                 !atomic_read(&exp->exp_obd->obd_evict_inprogress));
3455
3456         /* Check if client was evicted or reconnected already. */
3457         if (exp->exp_failed ||
3458             exp->exp_conn_cnt > lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
3459                 rc = -ENOTCONN;
3460         } else {
3461                 if (req->rq_bulk_read)
3462                         rc = sptlrpc_svc_wrap_bulk(req, desc);
3463
3464                 if (OCD_HAS_FLAG(&exp->exp_connect_data, BULK_MBITS))
3465                         req->rq_mbits = lustre_msg_get_mbits(req->rq_reqmsg);
3466                 else /* old version, bulk matchbits is rq_xid */
3467                         req->rq_mbits = req->rq_xid;
3468
3469                 if (rc == 0)
3470                         rc = ptlrpc_start_bulk_transfer(desc);
3471         }
3472
3473         if (rc < 0) {
3474                 DEBUG_REQ(D_ERROR, req, "bulk %s failed: rc = %d",
3475                           bulk2type(req), rc);
3476                 RETURN(rc);
3477         }
3478
3479         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
3480                 ptlrpc_abort_bulk(desc);
3481                 RETURN(0);
3482         }
3483
3484         /* limit actual bulk transfer to bulk_timeout seconds */
3485         deadline = start + bulk_timeout;
3486         if (deadline > req->rq_deadline)
3487                 deadline = req->rq_deadline;
3488
3489         do {
3490                 time64_t timeoutl = deadline - ktime_get_seconds();
3491                 time64_t rq_deadline;
3492
3493                 while (timeoutl >= 0 &&
3494                        wait_event_idle_timeout(
3495                                desc->bd_waitq,
3496                                !ptlrpc_server_bulk_active(desc) ||
3497                                exp->exp_failed ||
3498                                exp->exp_conn_cnt >
3499                                lustre_msg_get_conn_cnt(req->rq_reqmsg),
3500                                timeoutl ? cfs_time_seconds(1) : 1) == 0)
3501                         timeoutl -= 1;
3502                 rc = timeoutl < 0 ? -ETIMEDOUT : 0;
3503
3504                 /* Wait again if we changed rq_deadline. */
3505                 rq_deadline = READ_ONCE(req->rq_deadline);
3506                 deadline = start + bulk_timeout;
3507                 if (deadline > rq_deadline)
3508                         deadline = rq_deadline;
3509         } while (rc == -ETIMEDOUT &&
3510                  deadline > ktime_get_seconds());
3511
3512         if (rc == -ETIMEDOUT) {
3513                 DEBUG_REQ(D_ERROR, req, "timeout on bulk %s after %lld%+llds",
3514                           bulk2type(req), deadline - start,
3515                           ktime_get_real_seconds() - deadline);
3516                 ptlrpc_abort_bulk(desc);
3517         } else if (exp->exp_failed) {
3518                 DEBUG_REQ(D_ERROR, req, "Eviction on bulk %s",
3519                           bulk2type(req));
3520                 rc = -ENOTCONN;
3521                 ptlrpc_abort_bulk(desc);
3522         } else if (exp->exp_conn_cnt >
3523                    lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
3524                 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk %s",
3525                           bulk2type(req));
3526                 /* We don't reply anyway. */
3527                 rc = -ETIMEDOUT;
3528                 ptlrpc_abort_bulk(desc);
3529         } else if (desc->bd_failure) {
3530                 DEBUG_REQ(D_ERROR, req, "network error on bulk %s",
3531                           bulk2type(req));
3532                 /* XXX should this be a different errno? */
3533                 rc = -ETIMEDOUT;
3534         } else {
3535                 if (req->rq_bulk_write)
3536                         rc = sptlrpc_svc_unwrap_bulk(req, desc);
3537                 if (rc == 0 && desc->bd_nob_transferred != desc->bd_nob) {
3538                         DEBUG_REQ(D_ERROR, req, "truncated bulk %s %d(%d)",
3539                                   bulk2type(req), desc->bd_nob_transferred,
3540                                   desc->bd_nob);
3541                         /* XXX should this be a different errno? */
3542                         rc = -ETIMEDOUT;
3543                 }
3544         }
3545
3546         RETURN(rc);
3547 }
3548 EXPORT_SYMBOL(target_bulk_io);
3549
3550 #endif /* HAVE_SERVER_SUPPORT */