Whamcloud - gitweb
Landing b_recovery
[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         int pcaa_was_invalid;
46 };
47
48 /* A CLOSED import should remain so. */
49 #define IMPORT_SET_STATE_NOLOCK(imp, state)                                    \
50 do {                                                                           \
51         if (imp->imp_state != LUSTRE_IMP_CLOSED) {                             \
52                CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n",    \
53                       imp, imp->imp_target_uuid.uuid,                          \
54                       ptlrpc_import_state_name(imp->imp_state),                \
55                       ptlrpc_import_state_name(state));                        \
56                imp->imp_state = state;                                         \
57         }                                                                      \
58 } while(0)
59
60 #define IMPORT_SET_STATE(imp, state)                    \
61 do {                                                    \
62         unsigned long flags;                            \
63                                                         \
64         spin_lock_irqsave(&imp->imp_lock, flags);       \
65         IMPORT_SET_STATE_NOLOCK(imp, state);            \
66         spin_unlock_irqrestore(&imp->imp_lock, flags);  \
67 } while(0)
68
69
70 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
71                                     void * data, int rc);
72 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
73
74 /* Only this function is allowed to change the import state when it is
75  * CLOSED. I would rather refcount the import and free it after
76  * disconnection like we do with exports. To do that, the client_obd
77  * will need to save the peer info somewhere other than in the import,
78  * though. */
79 int ptlrpc_init_import(struct obd_import *imp)
80 {
81         unsigned long flags;
82         
83         spin_lock_irqsave(&imp->imp_lock, flags);
84
85         imp->imp_generation++;
86         imp->imp_state =  LUSTRE_IMP_NEW;
87
88         spin_unlock_irqrestore(&imp->imp_lock, flags);
89
90         return 0;
91 }
92
93 /* Returns true if import was FULL, false if import was already not
94  * connected.
95  */
96 int ptlrpc_set_import_discon(struct obd_import *imp)
97 {
98         unsigned long flags;
99         int rc = 0;
100         
101         spin_lock_irqsave(&imp->imp_lock, flags);
102
103         if (imp->imp_state == LUSTRE_IMP_FULL) {
104                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
105                 rc = 1;
106         } else {
107                 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
108                        imp,imp->imp_client->cli_name, 
109                        ptlrpc_import_state_name(imp->imp_state));
110         }
111         spin_unlock_irqrestore(&imp->imp_lock, flags);
112
113         return rc;
114 }
115
116 void ptlrpc_fail_import(struct obd_import *imp, int generation)
117 {
118         ENTRY;
119
120         LASSERT (!imp->imp_dlm_fake);
121
122         if (ptlrpc_set_import_discon(imp))
123                 ptlrpc_handle_failed_import(imp);
124
125         EXIT;
126 }
127
128 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
129 {
130         struct obd_device *obd = imp->imp_obd;
131         int initial_connect = 0;
132         int rc;
133         __u64 committed_before_reconnect = 0;
134         int was_invalid = 0;
135         struct ptlrpc_request *request;
136         int size[] = {sizeof(imp->imp_target_uuid),
137                                  sizeof(obd->obd_uuid),
138                                  sizeof(imp->imp_dlm_handle)};
139         char *tmp[] = {imp->imp_target_uuid.uuid,
140                        obd->obd_uuid.uuid,
141                        (char *)&imp->imp_dlm_handle};
142         struct ptlrpc_connect_async_args *aa;
143         unsigned long flags;
144
145         spin_lock_irqsave(&imp->imp_lock, flags);
146         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
147                 spin_unlock_irqrestore(&imp->imp_lock, flags);
148                 CERROR("can't connect to a closed import\n");
149                 RETURN(-EINVAL);
150         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
151                 spin_unlock_irqrestore(&imp->imp_lock, flags);
152                 CERROR("already connected\n");
153                 RETURN(0);
154         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
155                 spin_unlock_irqrestore(&imp->imp_lock, flags);
156                 CERROR("already connecting\n");
157                 RETURN(-EALREADY);
158         }
159
160         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
161
162         imp->imp_conn_cnt++; 
163         imp->imp_last_replay_transno = 0;
164
165         if (imp->imp_remote_handle.cookie == 0) {
166                 initial_connect = 1;
167         } else {
168                 committed_before_reconnect = imp->imp_peer_committed_transno;;
169
170         }
171
172         if (imp->imp_invalid) {
173                 imp->imp_invalid = 0;
174                 was_invalid = 1;
175         }
176
177         spin_unlock_irqrestore(&imp->imp_lock, flags);
178
179         if (new_uuid) {
180                 struct ptlrpc_connection *conn;
181                 struct obd_uuid uuid;
182                 struct obd_export *dlmexp;
183
184                 obd_str2uuid(&uuid, new_uuid);
185
186                 conn = ptlrpc_uuid_to_connection(&uuid);
187                 if (!conn)
188                         GOTO(out, rc = -ENOENT);
189
190                 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
191                        imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
192                        imp->imp_connection->c_remote_uuid.uuid,
193                        conn->c_remote_uuid.uuid);
194
195                 /* Switch the import's connection and the DLM export's
196                  * connection (which are almost certainly the same, but we
197                  * keep distinct refs just to make things clearer. I think. */
198                 if (imp->imp_connection)
199                         ptlrpc_put_connection(imp->imp_connection);
200                 /* We hand off the ref from ptlrpc_get_connection. */
201                 imp->imp_connection = conn;
202
203                 dlmexp = class_conn2export(&imp->imp_dlm_handle);
204                 
205                 LASSERT(dlmexp != NULL);
206
207                 if (dlmexp->exp_connection)
208                         ptlrpc_put_connection(dlmexp->exp_connection);
209                 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
210                 class_export_put(dlmexp);
211
212         }
213
214         request = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
215         if (!request)
216                 GOTO(out, rc = -ENOMEM);
217
218         request->rq_send_state = LUSTRE_IMP_CONNECTING;
219         request->rq_replen = lustre_msg_size(0, NULL);
220         request->rq_interpret_reply = ptlrpc_connect_interpret;
221
222         LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
223         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
224         memset(aa, 0, sizeof *aa);
225
226         aa->pcaa_peer_committed = committed_before_reconnect;
227         aa->pcaa_initial_connect = initial_connect;
228         aa->pcaa_was_invalid = was_invalid;
229
230         if (aa->pcaa_initial_connect)
231                 imp->imp_replayable = 1;
232         ptlrpcd_add_req(request);
233         rc = 0;
234 out:
235         if (rc != 0) {
236                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
237         }
238
239         RETURN(rc);
240 }
241
242 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
243                                     void * data, int rc)
244 {
245         struct ptlrpc_connect_async_args *aa = data;
246         struct obd_import *imp = request->rq_import;
247         struct lustre_handle old_hdl;
248         unsigned long flags;
249         int msg_flags;
250         ENTRY;
251         
252         spin_lock_irqsave(&imp->imp_lock, flags);
253         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
254                 spin_unlock_irqrestore(&imp->imp_lock, flags);
255                 RETURN(0);
256         }
257         spin_unlock_irqrestore(&imp->imp_lock, flags);
258
259         if (rc)
260                 GOTO(out, rc);
261
262         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
263
264         if (aa->pcaa_initial_connect) {
265                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
266                         CDEBUG(D_HA, "connected to replayable target: %s\n",
267                                imp->imp_target_uuid.uuid);
268                         imp->imp_replayable = 1;
269                         ptlrpc_pinger_add_import(imp);
270                 } else {
271                         imp->imp_replayable = 0;
272                 }
273                 imp->imp_remote_handle = request->rq_repmsg->handle;
274                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
275                 GOTO(finish, rc = 0);
276         }
277
278         /* Determine what recovery state to move the import to. */
279         if (MSG_CONNECT_RECONNECT & msg_flags) {
280                 memset(&old_hdl, 0, sizeof(old_hdl));
281                 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
282                             sizeof (old_hdl))) {
283                         CERROR("%s@%s didn't like our handle "LPX64
284                                ", failed\n", imp->imp_target_uuid.uuid,
285                                imp->imp_connection->c_remote_uuid.uuid,
286                                imp->imp_dlm_handle.cookie);
287                         GOTO(out, rc = -ENOTCONN);
288                 }
289
290                 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
291                            sizeof(imp->imp_remote_handle))) {
292                         CERROR("%s@%s changed handle from "LPX64" to "LPX64
293                                "; copying, but this may foreshadow disaster\n",
294                                imp->imp_target_uuid.uuid,
295                                imp->imp_connection->c_remote_uuid.uuid,
296                                imp->imp_remote_handle.cookie,
297                                request->rq_repmsg->handle.cookie);
298                         imp->imp_remote_handle = request->rq_repmsg->handle;
299                 } else {
300                         CERROR("reconnected to %s@%s after partition\n",
301                                imp->imp_target_uuid.uuid, 
302                                imp->imp_connection->c_remote_uuid.uuid);
303                 }
304                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
305         } 
306         else if (MSG_CONNECT_RECOVERING & msg_flags) {
307                 LASSERT(imp->imp_replayable);
308                 imp->imp_state = LUSTRE_IMP_RECOVER;
309                 imp->imp_remote_handle = request->rq_repmsg->handle;
310                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
311         } 
312         else {
313                 imp->imp_remote_handle = request->rq_repmsg->handle;
314                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
315         }
316         
317         /* Sanity checks for a reconnected import. */
318         if (!(imp->imp_replayable) != 
319              !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
320                 CERROR("imp_replayable flag does not match server "
321                        "after reconnect. We should LBUG right here.\n");
322         }
323
324         if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
325                 CERROR("%s went back in time (transno "LPD64
326                        " was previously committed, server now claims "LPD64
327                        ")! is shared storage not coherent?\n",
328                        imp->imp_target_uuid.uuid,
329                        aa->pcaa_peer_committed,
330                        request->rq_repmsg->last_committed);
331         }
332
333 finish:
334         rc = ptlrpc_import_recovery_state_machine(imp);
335         if (rc != 0) {
336                 if (aa->pcaa_was_invalid) {
337                         ptlrpc_set_import_active(imp, 0);
338                 }                
339
340                 if (rc == -ENOTCONN) {
341                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
342                                "invalidating and reconnecting\n",
343                                imp->imp_target_uuid.uuid,
344                                imp->imp_connection->c_remote_uuid.uuid);
345                         ptlrpc_connect_import(imp, NULL);
346                         RETURN(0);
347                 } 
348         }
349  out:
350         if (rc != 0) {
351                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
352                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
353                         GOTO(norecov, rc);
354                 CDEBUG(D_ERROR, 
355                        "recovery of %s on %s failed (%d); restarting\n",
356                        imp->imp_target_uuid.uuid,
357                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
358                 ptlrpc_handle_failed_import(imp);
359         }
360
361 norecov:
362         wake_up(&imp->imp_recovery_waitq);
363         RETURN(rc);
364 }
365
366 static int completed_replay_interpret(struct ptlrpc_request *req,
367                                     void * data, int rc)
368 {
369         atomic_dec(&req->rq_import->imp_replay_inflight);
370         ptlrpc_import_recovery_state_machine(req->rq_import);
371         RETURN(0);
372 }
373
374 static int signal_completed_replay(struct obd_import *imp)
375  {
376         struct ptlrpc_request *req;
377         ENTRY;
378
379         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
380         atomic_inc(&imp->imp_replay_inflight);
381
382         req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
383         if (!req)
384                 RETURN(-ENOMEM);
385
386         req->rq_replen = lustre_msg_size(0, NULL);
387         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
388         req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
389         req->rq_timeout *= 3; 
390         req->rq_interpret_reply = completed_replay_interpret;
391
392         ptlrpcd_add_req(req);
393         RETURN(0);
394 }
395
396
397 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
398 {
399         int rc = 0;
400
401         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
402                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
403                        imp->imp_target_uuid.uuid,
404                        imp->imp_connection->c_remote_uuid.uuid);
405                 ptlrpc_set_import_active(imp, 0);
406                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
407         } 
408         
409         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
410                 CDEBUG(D_HA, "replay requested by %s\n",
411                        imp->imp_target_uuid.uuid);
412                 rc = ptlrpc_replay_next(imp);
413                 if (rc == 0 && atomic_read(&imp->imp_replay_inflight) == 0) {
414                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
415                         rc = ldlm_replay_locks(imp);
416                         if (rc)
417                                 GOTO(out, rc);
418                 }
419                 rc = 0;
420         }
421
422         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
423                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
424                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
425                         rc = signal_completed_replay(imp);
426                         if (rc)
427                                 GOTO(out, rc);
428                 }
429
430         }
431
432         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
433                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
434                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
435                 }
436         }
437
438         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
439                 CDEBUG(D_HA, "reconnected to %s@%s\n",
440                        imp->imp_target_uuid.uuid,
441                        imp->imp_connection->c_remote_uuid.uuid);
442
443                 ptlrpc_set_import_active(imp, 1);
444                 ptlrpc_resend(imp);
445                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
446         } 
447
448         if (imp->imp_state == LUSTRE_IMP_FULL) {
449                 wake_up(&imp->imp_recovery_waitq);
450                 ptlrpc_wake_delayed(imp);
451         }
452
453  out:
454         RETURN(rc);
455 }
456
457 static int back_to_sleep(void *unused) 
458 {
459         return 0;
460 }
461
462 int ptlrpc_disconnect_import(struct obd_import *imp)
463 {
464         struct ptlrpc_request *request;
465         int rq_opc;
466         int rc = 0;
467         unsigned long flags;
468         ENTRY;
469
470         switch (imp->imp_connect_op) {
471         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
472         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
473         case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
474         default:
475                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
476                        imp->imp_target_uuid.uuid, imp->imp_connect_op);
477                 RETURN(-EINVAL);
478         }
479
480
481         if (ptlrpc_import_in_recovery(imp)) {
482                 struct l_wait_info lwi;
483                 lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), back_to_sleep, 
484                                        NULL, NULL);
485                 rc = l_wait_event(imp->imp_recovery_waitq, 
486                                   !ptlrpc_import_in_recovery(imp), &lwi);
487
488         }
489
490         spin_lock_irqsave(&imp->imp_lock, flags);
491         if (imp->imp_state != LUSTRE_IMP_FULL) {
492                 GOTO(out, 0);
493         }
494         spin_unlock_irqrestore(&imp->imp_lock, flags);
495
496         request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
497         if (request) {
498                 /* For non-replayable connections, don't attempt
499                    reconnect if this fails */
500                 if (!imp->imp_replayable) {
501                         IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
502                         request->rq_send_state =  LUSTRE_IMP_DISCON;
503                 }
504                 request->rq_replen = lustre_msg_size(0, NULL);
505                 rc = ptlrpc_queue_wait(request);
506                 ptlrpc_req_finished(request);
507         }
508
509         spin_lock_irqsave(&imp->imp_lock, flags);
510 out:
511         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
512         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
513         spin_unlock_irqrestore(&imp->imp_lock, flags);
514
515         RETURN(rc);
516 }
517