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