Whamcloud - gitweb
merge b_devel into HEAD. Includes:
[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, 2003 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 #ifdef __KERNEL__
27 # include <linux/module.h>
28 # include <linux/init.h>
29 #else
30 # include <liblustre.h>
31 #endif
32 #include <linux/obd.h>
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_ha.h>
37 #include <linux/lustre_net.h>
38 #include <linux/lprocfs_status.h>
39
40 extern int ptlrpc_init_portals(void);
41 extern void ptlrpc_exit_portals(void);
42
43 static __u32 ptlrpc_last_xid = 0;
44 static spinlock_t ptlrpc_last_xid_lock = SPIN_LOCK_UNLOCKED;
45
46 __u32 ptlrpc_next_xid(void)
47 {
48         __u32 tmp;
49         spin_lock(&ptlrpc_last_xid_lock);
50         tmp = ++ptlrpc_last_xid;
51         spin_unlock(&ptlrpc_last_xid_lock);
52         return tmp;
53 }
54
55 int connmgr_setup(struct obd_device *obddev, obd_count len, void *buf)
56 {
57         struct recovd_obd *recovd = &obddev->u.recovd;
58         int err;
59         ENTRY;
60
61         memset(recovd, 0, sizeof(*recovd));
62
63         err = recovd_setup(recovd);
64         RETURN(err);
65 }
66
67 int connmgr_cleanup(struct obd_device *dev)
68 {
69         struct recovd_obd *recovd = &dev->u.recovd;
70         int err;
71
72         err = recovd_cleanup(recovd);
73         RETURN(err);
74 }
75
76 int connmgr_iocontrol(unsigned int cmd, struct lustre_handle *hdl, int len,
77                       void *karg, void *uarg)
78 {
79         struct ptlrpc_connection *conn = NULL;
80         struct obd_device *obd = class_conn2obd(hdl);
81         struct recovd_obd *recovd = &obd->u.recovd;
82         struct obd_ioctl_data *data = karg;
83         struct list_head *tmp;
84         int rc = 0;
85
86         ENTRY;
87
88         if (cmd != OBD_IOC_RECOVD_NEWCONN && cmd != OBD_IOC_RECOVD_FAILCONN)
89                 RETURN(-EINVAL); /* XXX ENOSYS? */
90
91         /* Find the connection that's been rebuilt or has failed. */
92         spin_lock(&recovd->recovd_lock);
93         list_for_each(tmp, &recovd->recovd_troubled_items) {
94                 conn = list_entry(tmp, struct ptlrpc_connection,
95                                   c_recovd_data.rd_managed_chain);
96
97                 LASSERT(conn->c_recovd_data.rd_recovd == recovd); /* sanity */
98 #warning check buffer overflow in next line
99                 if (!strcmp(conn->c_remote_uuid.uuid, data->ioc_inlbuf1))
100                         break;
101                 conn = NULL;
102         }
103
104         if (!conn) {
105                 if (cmd == OBD_IOC_RECOVD_NEWCONN)
106                         GOTO(out, rc = -EINVAL);
107                 /* XXX macroize/inline and share with loop above */
108                 list_for_each(tmp, &recovd->recovd_managed_items) {
109                         conn = list_entry(tmp, struct ptlrpc_connection,
110                                           c_recovd_data.rd_managed_chain);
111
112                         LASSERT(conn->c_recovd_data.rd_recovd == recovd);
113
114 #warning check buffer overflow in next line
115                         if (!strcmp(conn->c_remote_uuid.uuid,
116                                     data->ioc_inlbuf1))
117                                 break;
118                         conn = NULL;
119                 }
120                 if (!conn)
121                         GOTO(out, rc = -EINVAL);
122         }
123
124         if (cmd == OBD_IOC_RECOVD_FAILCONN) {
125                 spin_unlock(&recovd->recovd_lock);
126                 recovd_conn_fail(conn);
127                 spin_lock(&recovd->recovd_lock);
128                 goto out;
129         }
130
131
132         /* else (NEWCONN) */
133         spin_lock(&conn->c_lock);
134
135         /* whatever happens, reset the INVALID flag */
136         conn->c_flags &= ~CONN_INVALID;
137
138         /* XXX is this a good check?  should we allow readdressing of
139          * XXX conns that aren't in recovery?
140          */
141         if (conn->c_recovd_data.rd_phase != RD_PREPARING) {
142                 spin_unlock(&conn->c_lock);
143                 GOTO(out, rc = -EALREADY);
144         }
145
146         if (data->ioc_inllen2) {
147                 CERROR("conn %p UUID change %s -> %s\n",
148                        conn, conn->c_remote_uuid.uuid, data->ioc_inlbuf2);
149                 obd_str2uuid(&conn->c_remote_uuid, data->ioc_inlbuf2);
150         } else {
151                 CERROR("conn %p UUID %s reconnected\n", conn,
152                        conn->c_remote_uuid.uuid);
153         }
154         ptlrpc_readdress_connection(conn, &conn->c_remote_uuid);
155         spin_unlock(&conn->c_lock);
156
157         conn->c_recovd_data.rd_phase = RD_PREPARED;
158         wake_up(&recovd->recovd_waitq);
159  out:
160         spin_unlock(&recovd->recovd_lock);
161         RETURN(rc);
162 }
163
164 static int connmgr_connect(struct lustre_handle *conn, struct obd_device *src,
165                            struct obd_uuid *cluuid, struct recovd_obd *recovd,
166                            ptlrpc_recovery_cb_t recover)
167 {
168         return class_connect(conn, src, cluuid);
169 }
170
171 int connmgr_attach(struct obd_device *dev, obd_count len, void *data)
172 {
173         struct lprocfs_static_vars lvars;
174         int rc = 0;
175
176         lprocfs_init_vars(&lvars);
177         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
178         return rc;
179 }
180
181 int conmgr_detach(struct obd_device *dev)
182 {
183         return lprocfs_obd_detach(dev);
184 }
185
186 /* use obd ops to offer management infrastructure */
187 static struct obd_ops recovd_obd_ops = {
188         o_owner:        THIS_MODULE,
189         o_attach:       connmgr_attach,
190         o_detach:       conmgr_detach,
191         o_setup:        connmgr_setup,
192         o_cleanup:      connmgr_cleanup,
193         o_iocontrol:    connmgr_iocontrol,
194         o_connect:      connmgr_connect,
195         o_disconnect:   class_disconnect
196 };
197
198
199
200 __init int ptlrpc_init(void)
201 {
202         struct lprocfs_static_vars lvars;
203         int rc;
204         ENTRY;
205
206         rc = ptlrpc_init_portals();
207         if (rc)
208                 RETURN(rc);
209         ptlrpc_init_connection();
210
211         lprocfs_init_vars(&lvars);
212         rc = class_register_type(&recovd_obd_ops, lvars.module_vars,
213                                  LUSTRE_HA_NAME);
214         if (rc)
215                 RETURN(rc);
216         ptlrpc_put_connection_superhack = ptlrpc_put_connection;
217         ptlrpc_abort_inflight_superhack = ptlrpc_abort_inflight;
218         RETURN(0);
219 }
220
221 static void __exit ptlrpc_exit(void)
222 {
223         class_unregister_type(LUSTRE_HA_NAME);
224         ptlrpc_exit_portals();
225         ptlrpc_cleanup_connection();
226 }
227
228 /* rpc.c */
229 EXPORT_SYMBOL(ptlrpc_next_xid);
230
231 /* recovd.c */
232 EXPORT_SYMBOL(ptlrpc_recovd);
233 EXPORT_SYMBOL(recovd_conn_fail);
234 EXPORT_SYMBOL(recovd_conn_manage);
235 EXPORT_SYMBOL(recovd_conn_fixed);
236 EXPORT_SYMBOL(recovd_setup);
237 EXPORT_SYMBOL(recovd_cleanup);
238
239 /* connection.c */
240 EXPORT_SYMBOL(ptlrpc_readdress_connection);
241 EXPORT_SYMBOL(ptlrpc_get_connection);
242 EXPORT_SYMBOL(ptlrpc_put_connection);
243 EXPORT_SYMBOL(ptlrpc_connection_addref);
244 EXPORT_SYMBOL(ptlrpc_init_connection);
245 EXPORT_SYMBOL(ptlrpc_cleanup_connection);
246
247 /* niobuf.c */
248 EXPORT_SYMBOL(ptlrpc_bulk_put);
249 EXPORT_SYMBOL(ptlrpc_bulk_get);
250 EXPORT_SYMBOL(ptlrpc_register_bulk_put);
251 EXPORT_SYMBOL(ptlrpc_register_bulk_get);
252 EXPORT_SYMBOL(ptlrpc_abort_bulk);
253 EXPORT_SYMBOL(ptlrpc_reply);
254 EXPORT_SYMBOL(ptlrpc_error);
255 EXPORT_SYMBOL(ptlrpc_resend_req);
256 EXPORT_SYMBOL(ptl_send_rpc);
257 EXPORT_SYMBOL(ptlrpc_link_svc_me);
258 EXPORT_SYMBOL(obd_brw_set_new);
259 EXPORT_SYMBOL(obd_brw_set_add);
260 EXPORT_SYMBOL(obd_brw_set_del);
261 EXPORT_SYMBOL(obd_brw_set_decref);
262 EXPORT_SYMBOL(obd_brw_set_addref);
263
264 /* client.c */
265 EXPORT_SYMBOL(ptlrpc_init_client);
266 EXPORT_SYMBOL(ptlrpc_cleanup_client);
267 EXPORT_SYMBOL(ptlrpc_req_to_uuid);
268 EXPORT_SYMBOL(ptlrpc_uuid_to_connection);
269 EXPORT_SYMBOL(ptlrpc_queue_wait);
270 EXPORT_SYMBOL(ptlrpc_continue_req);
271 EXPORT_SYMBOL(ptlrpc_replay_req);
272 EXPORT_SYMBOL(ptlrpc_restart_req);
273 EXPORT_SYMBOL(ptlrpc_prep_req);
274 EXPORT_SYMBOL(ptlrpc_free_req);
275 EXPORT_SYMBOL(ptlrpc_abort);
276 EXPORT_SYMBOL(ptlrpc_req_finished);
277 EXPORT_SYMBOL(ptlrpc_request_addref);
278 EXPORT_SYMBOL(ptlrpc_prep_bulk);
279 EXPORT_SYMBOL(ptlrpc_free_bulk);
280 EXPORT_SYMBOL(ptlrpc_prep_bulk_page);
281 EXPORT_SYMBOL(ptlrpc_free_bulk_page);
282 EXPORT_SYMBOL(ll_brw_sync_wait);
283 EXPORT_SYMBOL(ptlrpc_abort_inflight);
284 EXPORT_SYMBOL(ptlrpc_retain_replayable_request);
285
286 /* service.c */
287 EXPORT_SYMBOL(ptlrpc_init_svc);
288 EXPORT_SYMBOL(ptlrpc_stop_all_threads);
289 EXPORT_SYMBOL(ptlrpc_start_thread);
290 EXPORT_SYMBOL(ptlrpc_unregister_service);
291
292 /* pack_generic.c */
293 EXPORT_SYMBOL(lustre_pack_msg);
294 EXPORT_SYMBOL(lustre_msg_size);
295 EXPORT_SYMBOL(lustre_unpack_msg);
296 EXPORT_SYMBOL(lustre_msg_buf);
297
298 /* recover.c */
299 EXPORT_SYMBOL(ptlrpc_run_recovery_upcall);
300 EXPORT_SYMBOL(ptlrpc_reconnect_import);
301 EXPORT_SYMBOL(ptlrpc_replay);
302 EXPORT_SYMBOL(ptlrpc_resend);
303 EXPORT_SYMBOL(ptlrpc_wake_delayed);
304
305 #ifdef __KERNEL__
306 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
307 MODULE_DESCRIPTION("Lustre Request Processor");
308 MODULE_LICENSE("GPL");
309
310 module_init(ptlrpc_init);
311 module_exit(ptlrpc_exit);
312 #endif