Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[fs/lustre-release.git] / lustre / lib / target.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Mike Shaver <shaver@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * Target-common OBD method implementations and utility functions.
25  */
26
27 #define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_OST /* XXX WRONG */
29
30 #include <linux/module.h>
31 #include <linux/obd_ost.h>
32 #include <linux/lustre_net.h>
33 #include <linux/lustre_dlm.h>
34
35 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
36                             char *cluuid)
37 {
38         if (exp->exp_connection) {
39                 struct lustre_handle *hdl;
40                 hdl = &exp->exp_ldlm_data.led_import.imp_handle;
41                 /* Might be a re-connect after a partition. */
42                 if (!memcmp(conn, hdl, sizeof *conn)) {
43                         CERROR("%s reconnecting\n", cluuid);
44                         conn->addr = (__u64) (unsigned long)exp;
45                         conn->cookie = exp->exp_cookie;
46                         RETURN(EALREADY);
47                 } else {
48                         CERROR("%s reconnecting from %s, "
49                                "handle mismatch (ours "LPX64"/"LPX64", "
50                                "theirs "LPX64"/"LPX64")\n", cluuid,
51                                exp->exp_connection->c_remote_uuid, hdl->addr,
52                                hdl->cookie, conn->addr, conn->cookie);
53                         /* XXX disconnect them here? */
54                         memset(conn, 0, sizeof *conn);
55                         /* This is a little scary, but right now we build this
56                          * file separately into each server module, so I won't
57                          * go _immediately_ to hell.
58                          */
59                         RETURN(-EALREADY);
60                 }
61         }
62
63         conn->addr = (__u64) (unsigned long)exp;
64         conn->cookie = exp->exp_cookie;
65         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n", cluuid, exp);
66         CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
67                (long long)conn->addr, (long long)conn->cookie);
68         RETURN(0);
69 }
70
71 int target_handle_connect(struct ptlrpc_request *req)
72 {
73         struct obd_device *target;
74         struct obd_export *export;
75         struct obd_import *dlmimp;
76         struct lustre_handle conn;
77         char *tgtuuid, *cluuid;
78         int rc, i;
79         ENTRY;
80
81         tgtuuid = lustre_msg_buf(req->rq_reqmsg, 0);
82         if (req->rq_reqmsg->buflens[0] > 37) {
83                 CERROR("bad target UUID for connect\n");
84                 GOTO(out, rc = -EINVAL);
85         }
86
87         cluuid = lustre_msg_buf(req->rq_reqmsg, 1);
88         if (req->rq_reqmsg->buflens[1] > 37) {
89                 CERROR("bad client UUID for connect\n");
90                 GOTO(out, rc = -EINVAL);
91         }
92
93         i = class_uuid2dev(tgtuuid);
94         if (i == -1) {
95                 CERROR("UUID '%s' not found for connect\n", tgtuuid);
96                 GOTO(out, rc = -ENODEV);
97         }
98
99         target = &obd_dev[i];
100         if (!target)
101                 GOTO(out, rc = -ENODEV);
102
103         conn.addr = req->rq_reqmsg->addr;
104         conn.cookie = req->rq_reqmsg->cookie;
105
106         rc = obd_connect(&conn, target, cluuid, ptlrpc_recovd,
107                          target_revoke_connection);
108         /* EALREADY indicates a reconnection, send the reply normally. */
109         if (rc && rc != EALREADY)
110                 GOTO(out, rc);
111
112         /* If all else goes well, this is our RPC return code. */
113         req->rq_status = rc;
114
115         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
116         if (rc)
117                 GOTO(out, rc);
118         req->rq_repmsg->addr = conn.addr;
119         req->rq_repmsg->cookie = conn.cookie;
120
121         export = class_conn2export(&conn);
122         LASSERT(export);
123
124         req->rq_export = export;
125         export->exp_connection = ptlrpc_get_connection(&req->rq_peer, cluuid);
126         if (req->rq_connection != NULL)
127                 ptlrpc_put_connection(req->rq_connection);
128         req->rq_connection = ptlrpc_connection_addref(export->exp_connection);
129
130         spin_lock(&export->exp_connection->c_lock);
131         list_add(&export->exp_conn_chain, &export->exp_connection->c_exports);
132         spin_unlock(&export->exp_connection->c_lock);
133         recovd_conn_manage(export->exp_connection, ptlrpc_recovd,
134                            target_revoke_connection);
135
136         dlmimp = &export->exp_ldlm_data.led_import;
137         dlmimp->imp_connection = req->rq_connection;
138         dlmimp->imp_client = &export->exp_obd->obd_ldlm_client;
139         dlmimp->imp_handle.addr = req->rq_reqmsg->addr;
140         dlmimp->imp_handle.cookie = req->rq_reqmsg->cookie;
141         dlmimp->imp_obd = /* LDLM! */ NULL;
142         dlmimp->imp_recover = NULL;
143         INIT_LIST_HEAD(&dlmimp->imp_replay_list);
144         INIT_LIST_HEAD(&dlmimp->imp_sending_list);
145         INIT_LIST_HEAD(&dlmimp->imp_delayed_list);
146         spin_lock_init(&dlmimp->imp_lock);
147         dlmimp->imp_level = LUSTRE_CONN_FULL;
148 out:
149         if (rc)
150                 req->rq_status = rc;
151         RETURN(rc);
152 }
153
154 int target_handle_disconnect(struct ptlrpc_request *req)
155 {
156         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
157         int rc;
158         ENTRY;
159
160         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
161         if (rc)
162                 RETURN(rc);
163
164         req->rq_status = obd_disconnect(conn);
165
166         RETURN(0);
167 }
168
169 static int target_disconnect_client(struct ptlrpc_connection *conn)
170 {
171         struct list_head *expiter, *n;
172         struct lustre_handle hdl;
173         struct obd_export *exp;
174         int rc;
175         ENTRY;
176
177         list_for_each_safe(expiter, n, &conn->c_exports) {
178                 exp = list_entry(expiter, struct obd_export, exp_conn_chain);
179
180                 hdl.addr = (__u64)(unsigned long)exp;
181                 hdl.cookie = exp->exp_cookie;
182                 rc = obd_disconnect(&hdl);
183                 if (rc)
184                         CERROR("disconnecting export %p failed: %d\n", exp, rc);
185         }
186
187         /* XXX spank the connection (it's frozen in _RECOVD for now!) */
188         RETURN(0);
189 }
190
191 static int target_fence_failed_connection(struct ptlrpc_connection *conn)
192 {
193         ENTRY;
194
195         conn->c_recovd_data.rd_phase = RD_PREPARED;
196
197         RETURN(0);
198 }
199
200 int target_revoke_connection(struct recovd_data *rd, int phase)
201 {
202         struct ptlrpc_connection *conn = class_rd2conn(rd);
203         
204         LASSERT(conn);
205         ENTRY;
206
207         switch (phase) {
208             case PTLRPC_RECOVD_PHASE_PREPARE:
209                 RETURN(target_fence_failed_connection(conn));
210             case PTLRPC_RECOVD_PHASE_RECOVER:
211                 RETURN(target_disconnect_client(conn));
212             case PTLRPC_RECOVD_PHASE_FAILURE:
213                 LBUG();
214                 RETURN(0);
215         }
216
217         LBUG();
218         RETURN(-ENOSYS);
219 }