Whamcloud - gitweb
* 1st attempt to prevent duplicate devices being started.
[fs/lustre-release.git] / lustre / ptlrpc / rpc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24 #define DEBUG_SUBSYSTEM S_RPC
25
26 #include <linux/module.h>
27 #include <linux/obd_support.h>
28 #include <linux/obd_class.h>
29 #include <linux/lustre_lib.h>
30 #include <linux/lustre_ha.h>
31 #include <linux/lustre_net.h>
32 #include <linux/init.h>
33
34 extern int ptlrpc_init_portals(void);
35 extern void ptlrpc_exit_portals(void);
36
37 int connmgr_setup(struct obd_device *obddev, obd_count len, void *buf)
38 {
39         struct recovd_obd *recovd = &obddev->u.recovd;
40         int err;
41         ENTRY;
42
43         MOD_INC_USE_COUNT;
44         memset(recovd, 0, sizeof(*recovd));
45
46         err = recovd_setup(recovd);
47         if (err) {
48                 MOD_DEC_USE_COUNT;
49                 RETURN(err);
50         }
51
52         RETURN(0);
53 }
54
55 int connmgr_cleanup(struct obd_device *dev)
56 {
57         struct recovd_obd *recovd = &dev->u.recovd;
58         int err;
59
60         err = recovd_cleanup(recovd);
61         if (err)
62                 LBUG();
63
64         MOD_DEC_USE_COUNT;
65         RETURN(0);
66 }
67
68 int connmgr_iocontrol(long cmd, struct lustre_handle *hdl, int len, void *karg,
69                       void *uarg)
70 {
71         struct ptlrpc_connection *conn = NULL;
72         struct obd_device *obd = class_conn2obd(hdl);
73         struct recovd_obd *recovd = &obd->u.recovd;
74         struct obd_ioctl_data *data = karg;
75         struct list_head *tmp;
76         int rc = 0;
77
78         ENTRY;
79
80         if (cmd != OBD_IOC_RECOVD_NEWCONN && cmd != OBD_IOC_RECOVD_FAILCONN)
81                 RETURN(-EINVAL); /* XXX ENOSYS? */
82         
83         /* Find the connection that's been rebuilt or has failed. */
84         spin_lock(&recovd->recovd_lock);
85         list_for_each(tmp, &recovd->recovd_troubled_items) {
86                 conn = list_entry(tmp, struct ptlrpc_connection,
87                                   c_recovd_data.rd_managed_chain);
88
89                 LASSERT(conn->c_recovd_data.rd_recovd == recovd); /* sanity */
90
91                 if (!strcmp(conn->c_remote_uuid, data->ioc_inlbuf1))
92                         break;
93                 conn = NULL;
94         }
95
96         if (!conn) {
97                 if (cmd == OBD_IOC_RECOVD_NEWCONN)
98                         GOTO(out, rc = -EINVAL);
99                 /* XXX macroize/inline and share with loop above */
100                 list_for_each(tmp, &recovd->recovd_managed_items) {
101                         conn = list_entry(tmp, struct ptlrpc_connection,
102                                           c_recovd_data.rd_managed_chain);
103                         
104                         LASSERT(conn->c_recovd_data.rd_recovd == recovd);
105                         
106                         if (!strcmp(conn->c_remote_uuid, data->ioc_inlbuf1))
107                                 break;
108                         conn = NULL;
109                 }
110                 if (!conn)
111                         GOTO(out, rc = -EINVAL);
112         }
113
114         if (cmd == OBD_IOC_RECOVD_FAILCONN) {
115                 spin_unlock(&recovd->recovd_lock);
116                 recovd_conn_fail(conn);
117                 spin_lock(&recovd->recovd_lock);
118
119                 /* Jump straight to the "failed" phase of recovery. */
120                 conn->c_recovd_data.rd_phase = RD_FAILED;
121                 goto out;
122         }
123
124
125         /* else (NEWCONN) */
126         spin_lock(&conn->c_lock);
127
128         /* whatever happens, reset the INVALID flag */
129         conn->c_flags &= ~CONN_INVALID;
130
131         /* XXX is this a good check?  should we allow readdressing of
132          * XXX conns that aren't in recovery?
133          */
134         if (conn->c_recovd_data.rd_phase != RD_PREPARING) {
135                 spin_unlock(&conn->c_lock);
136                 GOTO(out, rc = -EALREADY);
137         }
138
139         if (data->ioc_inllen2) {
140                 CERROR("conn %p UUID change %s -> %s\n",
141                        conn, conn->c_remote_uuid, data->ioc_inlbuf2);
142                 strcpy(conn->c_remote_uuid, data->ioc_inlbuf2);
143         } else {
144                 CERROR("conn %p UUID %s reconnected\n", conn,
145                        conn->c_remote_uuid);
146         }
147         ptlrpc_readdress_connection(conn, conn->c_remote_uuid);
148         spin_unlock(&conn->c_lock);
149         
150         conn->c_recovd_data.rd_phase = RD_PREPARED;
151         wake_up(&recovd->recovd_waitq);
152  out:
153         spin_unlock(&recovd->recovd_lock);
154         RETURN(rc);
155 }
156
157 static int connmgr_connect(struct lustre_handle *conn, struct obd_device *src,
158                            obd_uuid_t cluuid, struct recovd_obd *recovd,
159                            ptlrpc_recovery_cb_t recover)
160 {
161         return class_connect(conn, src, cluuid);
162 }
163
164 /* use obd ops to offer management infrastructure */
165 static struct obd_ops recovd_obd_ops = {
166         o_setup:       connmgr_setup,
167         o_cleanup:     connmgr_cleanup,
168         o_iocontrol:   connmgr_iocontrol,
169         o_connect:     connmgr_connect,
170         o_disconnect:  class_disconnect
171 };
172
173 static int __init ptlrpc_init(void)
174 {
175         int rc; 
176         rc = ptlrpc_init_portals();
177         if (rc) 
178                 RETURN(rc);
179         ptlrpc_init_connection();
180         class_register_type(&recovd_obd_ops, LUSTRE_HA_NAME);
181         ptlrpc_put_connection_superhack = ptlrpc_put_connection;
182         return 0;
183 }
184
185 static void __exit ptlrpc_exit(void)
186 {
187         class_unregister_type(LUSTRE_HA_NAME);
188         ptlrpc_exit_portals();
189         ptlrpc_cleanup_connection();
190 }
191
192 /* recovd.c */
193 EXPORT_SYMBOL(ptlrpc_recovd);
194 EXPORT_SYMBOL(recovd_conn_fail);
195 EXPORT_SYMBOL(recovd_conn_manage);
196 EXPORT_SYMBOL(recovd_conn_fixed);
197 EXPORT_SYMBOL(recovd_setup);
198 EXPORT_SYMBOL(recovd_cleanup);
199
200 /* connection.c */
201 EXPORT_SYMBOL(ptlrpc_readdress_connection);
202 EXPORT_SYMBOL(ptlrpc_get_connection);
203 EXPORT_SYMBOL(ptlrpc_put_connection);
204 EXPORT_SYMBOL(ptlrpc_connection_addref);
205 EXPORT_SYMBOL(ptlrpc_init_connection);
206 EXPORT_SYMBOL(ptlrpc_cleanup_connection);
207
208 /* niobuf.c */
209 EXPORT_SYMBOL(ptlrpc_send_bulk);
210 EXPORT_SYMBOL(ptlrpc_register_bulk);
211 EXPORT_SYMBOL(ptlrpc_abort_bulk);
212 EXPORT_SYMBOL(ptlrpc_reply);
213 EXPORT_SYMBOL(ptlrpc_error);
214 EXPORT_SYMBOL(ptlrpc_resend_req);
215 EXPORT_SYMBOL(ptl_send_rpc);
216 EXPORT_SYMBOL(ptlrpc_link_svc_me);
217
218 /* client.c */
219 EXPORT_SYMBOL(ptlrpc_init_client);
220 EXPORT_SYMBOL(ptlrpc_cleanup_client);
221 EXPORT_SYMBOL(ptlrpc_req_to_uuid);
222 EXPORT_SYMBOL(ptlrpc_uuid_to_connection);
223 EXPORT_SYMBOL(ptlrpc_queue_wait);
224 EXPORT_SYMBOL(ptlrpc_continue_req);
225 EXPORT_SYMBOL(ptlrpc_replay_req);
226 EXPORT_SYMBOL(ptlrpc_restart_req);
227 EXPORT_SYMBOL(ptlrpc_prep_req);
228 EXPORT_SYMBOL(ptlrpc_free_req);
229 EXPORT_SYMBOL(ptlrpc_req_finished);
230 EXPORT_SYMBOL(ptlrpc_prep_bulk);
231 EXPORT_SYMBOL(ptlrpc_free_bulk);
232 EXPORT_SYMBOL(ptlrpc_prep_bulk_page);
233 EXPORT_SYMBOL(ptlrpc_free_bulk_page);
234 EXPORT_SYMBOL(ptlrpc_check_status);
235
236 /* service.c */
237 EXPORT_SYMBOL(ptlrpc_init_svc);
238 EXPORT_SYMBOL(ptlrpc_stop_all_threads);
239 EXPORT_SYMBOL(ptlrpc_start_thread);
240 EXPORT_SYMBOL(ptlrpc_unregister_service);
241
242 /* pack_generic.c */
243 EXPORT_SYMBOL(lustre_pack_msg);
244 EXPORT_SYMBOL(lustre_msg_size);
245 EXPORT_SYMBOL(lustre_unpack_msg);
246 EXPORT_SYMBOL(lustre_msg_buf);
247
248 /* recover.c */
249 EXPORT_SYMBOL(ptlrpc_run_recovery_upcall);
250 EXPORT_SYMBOL(ptlrpc_reconnect_import);
251 EXPORT_SYMBOL(ptlrpc_replay);
252
253 MODULE_AUTHOR("Cluster File Systems, Inc <info@clusterfs.com>");
254 MODULE_DESCRIPTION("Lustre Request Processor v1.0");
255 MODULE_LICENSE("GPL");
256
257 module_init(ptlrpc_init);
258 module_exit(ptlrpc_exit);