Whamcloud - gitweb
Don't copy lvb into reply message on error, since that message might not have
[fs/lustre-release.git] / lustre / ptlrpc / import.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Mike Shaver <shaver@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24 #ifdef __KERNEL__
25 # include <linux/config.h>
26 # include <linux/module.h>
27 # include <linux/kmod.h>
28 #else
29 # include <liblustre.h>
30 #endif
31
32 #include <linux/obd_support.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_net.h>
35 #include <linux/lustre_import.h>
36 #include <linux/lustre_export.h>
37 #include <linux/obd.h>
38 #include <linux/obd_class.h>
39
40 #include "ptlrpc_internal.h"
41
42 struct ptlrpc_connect_async_args {
43          __u64 pcaa_peer_committed;
44         int pcaa_initial_connect;
45 };
46
47 /* A CLOSED import should remain so. */
48 #define IMPORT_SET_STATE_NOLOCK(imp, state)                                    \
49 do {                                                                           \
50         if (imp->imp_state != LUSTRE_IMP_CLOSED) {                             \
51                CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n",    \
52                       imp, imp->imp_target_uuid.uuid,                          \
53                       ptlrpc_import_state_name(imp->imp_state),                \
54                       ptlrpc_import_state_name(state));                        \
55                imp->imp_state = state;                                         \
56         }                                                                      \
57 } while(0)
58
59 #define IMPORT_SET_STATE(imp, state)                    \
60 do {                                                    \
61         unsigned long flags;                            \
62                                                         \
63         spin_lock_irqsave(&imp->imp_lock, flags);       \
64         IMPORT_SET_STATE_NOLOCK(imp, state);            \
65         spin_unlock_irqrestore(&imp->imp_lock, flags);  \
66 } while(0)
67
68
69 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
70                                     void * data, int rc);
71 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
72
73 /* Only this function is allowed to change the import state when it is
74  * CLOSED. I would rather refcount the import and free it after
75  * disconnection like we do with exports. To do that, the client_obd
76  * will need to save the peer info somewhere other than in the import,
77  * though. */
78 int ptlrpc_init_import(struct obd_import *imp)
79 {
80         unsigned long flags;
81
82         spin_lock_irqsave(&imp->imp_lock, flags);
83
84         imp->imp_generation++;
85         imp->imp_state =  LUSTRE_IMP_NEW;
86
87         spin_unlock_irqrestore(&imp->imp_lock, flags);
88
89         return 0;
90 }
91
92 /* Returns true if import was FULL, false if import was already not
93  * connected.
94  */
95 int ptlrpc_set_import_discon(struct obd_import *imp)
96 {
97         unsigned long flags;
98         int rc = 0;
99
100         spin_lock_irqsave(&imp->imp_lock, flags);
101
102         if (imp->imp_state == LUSTRE_IMP_FULL) {
103                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
104                 spin_unlock_irqrestore(&imp->imp_lock, flags);
105                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
106                 rc = 1;
107         } else {
108                 spin_unlock_irqrestore(&imp->imp_lock, flags);
109                 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
110                        imp,imp->imp_client->cli_name,
111                        ptlrpc_import_state_name(imp->imp_state));
112         }
113
114         return rc;
115 }
116
117 /*
118  * This acts as a barrier; all existing requests are rejected, and
119  * no new requests will be accepted until the import is valid again.
120  */
121 void ptlrpc_deactivate_import(struct obd_import *imp)
122 {
123         unsigned long flags;
124         ENTRY;
125
126         spin_lock_irqsave(&imp->imp_lock, flags);
127         CDEBUG(D_HA, "setting import %s INVALID\n",
128                imp->imp_target_uuid.uuid);
129         imp->imp_invalid = 1;
130         imp->imp_generation++;
131         spin_unlock_irqrestore(&imp->imp_lock, flags);
132
133         ptlrpc_abort_inflight(imp);
134         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
135 }
136
137 /*
138  * This function will invalidate the import, if necessary, then block
139  * for all the RPC completions, and finally notify the obd to
140  * invalidate its state (ie cancel locks, clear pending requests,
141  * etc).
142  *
143  * in_rpc: true if this is called while processing an rpc, like
144  *    CONNECT. It will allow for one RPC to be inflight while
145  *    waiting for requests to complete. Ugly, yes, but I don't see an
146  *    cleaner way right now.
147  */
148 void ptlrpc_invalidate_import(struct obd_import *imp, int in_rpc)
149 {
150         struct l_wait_info lwi;
151         int inflight = 0;
152         int rc;
153
154         if (!imp->imp_invalid)
155                 ptlrpc_deactivate_import(imp);
156
157         LASSERT(imp->imp_invalid);
158
159         if (in_rpc)
160                 inflight = 1;
161         /* wait for all requests to error out and call completion callbacks */
162         lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), NULL,
163                                NULL, NULL);
164         rc = l_wait_event(imp->imp_recovery_waitq,
165                           (atomic_read(&imp->imp_inflight) == inflight),
166                           &lwi);
167
168         if (rc)
169                 CERROR("%s: rc = %d waiting for callback (%d != %d)\n",
170                        imp->imp_target_uuid.uuid, rc,
171                        atomic_read(&imp->imp_inflight), inflight);
172
173         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
174 }
175
176 static void ptlrpc_activate_import(struct obd_import *imp)
177 {
178         struct obd_device *obd = imp->imp_obd;
179         unsigned long flags;
180
181         spin_lock_irqsave(&imp->imp_lock, flags);
182         imp->imp_invalid = 0;
183         spin_unlock_irqrestore(&imp->imp_lock, flags);
184
185         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
186 }
187
188 void ptlrpc_fail_import(struct obd_import *imp, int generation)
189 {
190         ENTRY;
191
192         LASSERT (!imp->imp_dlm_fake);
193
194         if (ptlrpc_set_import_discon(imp)) {
195                 unsigned long flags;
196
197                 if (!imp->imp_replayable) {
198                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
199                                "auto-deactivating\n",
200                                imp->imp_target_uuid.uuid,
201                                imp->imp_connection->c_remote_uuid.uuid,
202                                imp->imp_obd->obd_name);
203                         ptlrpc_deactivate_import(imp);
204                 }
205
206                 CDEBUG(D_HA, "%s: waking up pinger\n",
207                        imp->imp_target_uuid.uuid);
208
209                 spin_lock_irqsave(&imp->imp_lock, flags);
210                 imp->imp_force_verify = 1;
211                 spin_unlock_irqrestore(&imp->imp_lock, flags);
212
213                 ptlrpc_pinger_wake_up();
214         }
215         EXIT;
216 }
217
218 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
219 {
220         struct obd_device *obd = imp->imp_obd;
221         int initial_connect = 0;
222         int rc;
223         __u64 committed_before_reconnect = 0;
224         struct ptlrpc_request *request;
225         int size[] = {sizeof(imp->imp_target_uuid),
226                                  sizeof(obd->obd_uuid),
227                                  sizeof(imp->imp_dlm_handle)};
228         char *tmp[] = {imp->imp_target_uuid.uuid,
229                        obd->obd_uuid.uuid,
230                        (char *)&imp->imp_dlm_handle};
231         struct ptlrpc_connect_async_args *aa;
232         unsigned long flags;
233
234         spin_lock_irqsave(&imp->imp_lock, flags);
235         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
236                 spin_unlock_irqrestore(&imp->imp_lock, flags);
237                 CERROR("can't connect to a closed import\n");
238                 RETURN(-EINVAL);
239         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
240                 spin_unlock_irqrestore(&imp->imp_lock, flags);
241                 CERROR("already connected\n");
242                 RETURN(0);
243         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
244                 spin_unlock_irqrestore(&imp->imp_lock, flags);
245                 CERROR("already connecting\n");
246                 RETURN(-EALREADY);
247         }
248
249         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
250
251         imp->imp_conn_cnt++;
252         imp->imp_last_replay_transno = 0;
253
254         if (imp->imp_remote_handle.cookie == 0) {
255                 initial_connect = 1;
256         } else {
257                 committed_before_reconnect = imp->imp_peer_committed_transno;;
258
259         }
260
261
262         spin_unlock_irqrestore(&imp->imp_lock, flags);
263
264         if (new_uuid) {
265                 struct ptlrpc_connection *conn;
266                 struct obd_uuid uuid;
267                 struct obd_export *dlmexp;
268
269                 obd_str2uuid(&uuid, new_uuid);
270
271                 conn = ptlrpc_uuid_to_connection(&uuid);
272                 if (!conn)
273                         GOTO(out, rc = -ENOENT);
274
275                 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
276                        imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
277                        imp->imp_connection->c_remote_uuid.uuid,
278                        conn->c_remote_uuid.uuid);
279
280                 /* Switch the import's connection and the DLM export's
281                  * connection (which are almost certainly the same, but we
282                  * keep distinct refs just to make things clearer. I think. */
283                 if (imp->imp_connection)
284                         ptlrpc_put_connection(imp->imp_connection);
285                 /* We hand off the ref from ptlrpc_get_connection. */
286                 imp->imp_connection = conn;
287
288                 dlmexp = class_conn2export(&imp->imp_dlm_handle);
289
290                 LASSERT(dlmexp != NULL);
291
292                 if (dlmexp->exp_connection)
293                         ptlrpc_put_connection(dlmexp->exp_connection);
294                 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
295                 class_export_put(dlmexp);
296
297         }
298
299         request = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
300         if (!request)
301                 GOTO(out, rc = -ENOMEM);
302
303 #ifndef __KERNEL__
304         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
305 #endif
306
307         request->rq_send_state = LUSTRE_IMP_CONNECTING;
308         request->rq_replen = lustre_msg_size(0, NULL);
309         request->rq_interpret_reply = ptlrpc_connect_interpret;
310
311         LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
312         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
313         memset(aa, 0, sizeof *aa);
314
315         aa->pcaa_peer_committed = committed_before_reconnect;
316         aa->pcaa_initial_connect = initial_connect;
317
318         if (aa->pcaa_initial_connect)
319                 imp->imp_replayable = 1;
320
321         ptlrpcd_add_req(request);
322         rc = 0;
323 out:
324         if (rc != 0) {
325                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
326         }
327
328         RETURN(rc);
329 }
330
331 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
332                                     void * data, int rc)
333 {
334         struct ptlrpc_connect_async_args *aa = data;
335         struct obd_import *imp = request->rq_import;
336         struct lustre_handle old_hdl;
337         unsigned long flags;
338         int msg_flags;
339         ENTRY;
340
341         spin_lock_irqsave(&imp->imp_lock, flags);
342         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
343                 spin_unlock_irqrestore(&imp->imp_lock, flags);
344                 RETURN(0);
345         }
346         spin_unlock_irqrestore(&imp->imp_lock, flags);
347
348         if (rc)
349                 GOTO(out, rc);
350
351         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
352
353         if (aa->pcaa_initial_connect) {
354                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
355                         CDEBUG(D_HA, "connected to replayable target: %s\n",
356                                imp->imp_target_uuid.uuid);
357                         imp->imp_pingable = imp->imp_replayable = 1;
358                 } else {
359                         imp->imp_replayable = 0;
360                 }
361                 imp->imp_remote_handle = request->rq_repmsg->handle;
362                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
363                 GOTO(finish, rc = 0);
364         }
365
366         /* Determine what recovery state to move the import to. */
367         if (MSG_CONNECT_RECONNECT & msg_flags) {
368                 memset(&old_hdl, 0, sizeof(old_hdl));
369                 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
370                             sizeof (old_hdl))) {
371                         CERROR("%s@%s didn't like our handle "LPX64
372                                ", failed\n", imp->imp_target_uuid.uuid,
373                                imp->imp_connection->c_remote_uuid.uuid,
374                                imp->imp_dlm_handle.cookie);
375                         GOTO(out, rc = -ENOTCONN);
376                 }
377
378                 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
379                            sizeof(imp->imp_remote_handle))) {
380                         CERROR("%s@%s changed handle from "LPX64" to "LPX64
381                                "; copying, but this may foreshadow disaster\n",
382                                imp->imp_target_uuid.uuid,
383                                imp->imp_connection->c_remote_uuid.uuid,
384                                imp->imp_remote_handle.cookie,
385                                request->rq_repmsg->handle.cookie);
386                         imp->imp_remote_handle = request->rq_repmsg->handle;
387                 } else {
388                         CERROR("reconnected to %s@%s after partition\n",
389                                imp->imp_target_uuid.uuid,
390                                imp->imp_connection->c_remote_uuid.uuid);
391                 }
392
393                 if (imp->imp_invalid)
394                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
395                 else
396                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
397         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
398                 LASSERT(imp->imp_replayable);
399                 imp->imp_remote_handle = request->rq_repmsg->handle;
400                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
401         } else {
402                 imp->imp_remote_handle = request->rq_repmsg->handle;
403                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
404         }
405
406         /* Sanity checks for a reconnected import. */
407         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
408                 CERROR("imp_replayable flag does not match server "
409                        "after reconnect. We should LBUG right here.\n");
410         }
411
412         if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
413                 CERROR("%s went back in time (transno "LPD64
414                        " was previously committed, server now claims "LPD64
415                        ")! is shared storage not coherent?\n",
416                        imp->imp_target_uuid.uuid,
417                        aa->pcaa_peer_committed,
418                        request->rq_repmsg->last_committed);
419         }
420
421 finish:
422         rc = ptlrpc_import_recovery_state_machine(imp);
423         if (rc != 0) {
424                 if (rc == -ENOTCONN) {
425                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
426                                "invalidating and reconnecting\n",
427                                imp->imp_target_uuid.uuid,
428                                imp->imp_connection->c_remote_uuid.uuid);
429                         ptlrpc_connect_import(imp, NULL);
430                         RETURN(0);
431                 }
432         }
433  out:
434         if (rc != 0) {
435                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
436                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov) {
437                         ptlrpc_deactivate_import(imp);
438                 }
439                 CDEBUG(D_ERROR, "recovery of %s on %s failed (%d)\n",
440                        imp->imp_target_uuid.uuid,
441                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
442         }
443
444         wake_up(&imp->imp_recovery_waitq);
445         RETURN(rc);
446 }
447
448 static int completed_replay_interpret(struct ptlrpc_request *req,
449                                     void * data, int rc)
450 {
451         atomic_dec(&req->rq_import->imp_replay_inflight);
452         ptlrpc_import_recovery_state_machine(req->rq_import);
453         RETURN(0);
454 }
455
456 static int signal_completed_replay(struct obd_import *imp)
457  {
458         struct ptlrpc_request *req;
459         ENTRY;
460
461         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
462         atomic_inc(&imp->imp_replay_inflight);
463
464         req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
465         if (!req)
466                 RETURN(-ENOMEM);
467
468         req->rq_replen = lustre_msg_size(0, NULL);
469         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
470         req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
471         req->rq_timeout *= 3;
472         req->rq_interpret_reply = completed_replay_interpret;
473
474         ptlrpcd_add_req(req);
475         RETURN(0);
476 }
477
478 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
479 {
480         int rc = 0;
481         int inflight;
482
483         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
484                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
485                        imp->imp_target_uuid.uuid,
486                        imp->imp_connection->c_remote_uuid.uuid);
487
488                 ptlrpc_invalidate_import(imp, 1);
489
490                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
491         }
492
493         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
494                 CDEBUG(D_HA, "replay requested by %s\n",
495                        imp->imp_target_uuid.uuid);
496                 rc = ptlrpc_replay_next(imp, &inflight);
497                 if (inflight == 0 &&
498                     atomic_read(&imp->imp_replay_inflight) == 0) {
499                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
500                         rc = ldlm_replay_locks(imp);
501                         if (rc)
502                                 GOTO(out, rc);
503                 }
504                 rc = 0;
505         }
506
507         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
508                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
509                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
510                         rc = signal_completed_replay(imp);
511                         if (rc)
512                                 GOTO(out, rc);
513                 }
514
515         }
516
517         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
518                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
519                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
520                 }
521         }
522
523         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
524                 CDEBUG(D_HA, "reconnected to %s@%s\n",
525                        imp->imp_target_uuid.uuid,
526                        imp->imp_connection->c_remote_uuid.uuid);
527
528                 rc = ptlrpc_resend(imp);
529                 if (rc)
530                         GOTO(out, rc);
531                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
532                 ptlrpc_activate_import(imp);
533         }
534
535         if (imp->imp_state == LUSTRE_IMP_FULL) {
536                 wake_up(&imp->imp_recovery_waitq);
537                 ptlrpc_wake_delayed(imp);
538         }
539
540  out:
541         RETURN(rc);
542 }
543
544 static int back_to_sleep(void *unused)
545 {
546         return 0;
547 }
548
549 int ptlrpc_disconnect_import(struct obd_import *imp)
550 {
551         struct ptlrpc_request *request;
552         int rq_opc;
553         int rc = 0;
554         unsigned long flags;
555         ENTRY;
556
557         switch (imp->imp_connect_op) {
558         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
559         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
560         case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
561         default:
562                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
563                        imp->imp_target_uuid.uuid, imp->imp_connect_op);
564                 RETURN(-EINVAL);
565         }
566
567
568         if (ptlrpc_import_in_recovery(imp)) {
569                 struct l_wait_info lwi;
570                 lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), back_to_sleep,
571                                        NULL, NULL);
572                 rc = l_wait_event(imp->imp_recovery_waitq,
573                                   !ptlrpc_import_in_recovery(imp), &lwi);
574
575         }
576
577         spin_lock_irqsave(&imp->imp_lock, flags);
578         if (imp->imp_state != LUSTRE_IMP_FULL) {
579                 GOTO(out, 0);
580         }
581         spin_unlock_irqrestore(&imp->imp_lock, flags);
582
583         request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
584         if (request) {
585                 /* For non-replayable connections, don't attempt
586                    reconnect if this fails */
587                 if (!imp->imp_replayable) {
588                         request->rq_no_resend = 1;
589                         IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
590                         request->rq_send_state =  LUSTRE_IMP_CONNECTING;
591                 }
592                 request->rq_replen = lustre_msg_size(0, NULL);
593                 rc = ptlrpc_queue_wait(request);
594                 ptlrpc_req_finished(request);
595         }
596
597         spin_lock_irqsave(&imp->imp_lock, flags);
598 out:
599         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
600         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
601         spin_unlock_irqrestore(&imp->imp_lock, flags);
602
603         RETURN(rc);
604 }
605