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