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