Whamcloud - gitweb
Bug #357, no more gratituous new line..., or typecasting
[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 #include <linux/lprocfs_status.h>
34
35
36
37 extern int ptlrpc_init_portals(void);
38 extern void ptlrpc_exit_portals(void);
39
40 extern struct lprocfs_vars status_var_nm_1[];
41 extern struct lprocfs_vars status_class_var[];
42
43 int connmgr_setup(struct obd_device *obddev, obd_count len, void *buf)
44 {
45         struct recovd_obd *recovd = &obddev->u.recovd;
46         int err;
47         ENTRY;
48
49         MOD_INC_USE_COUNT;
50         memset(recovd, 0, sizeof(*recovd));
51
52         err = recovd_setup(recovd);
53         if (err) {
54                 MOD_DEC_USE_COUNT;
55                 RETURN(err);
56         }
57
58         RETURN(0);
59 }
60
61 int connmgr_cleanup(struct obd_device *dev)
62 {
63         struct recovd_obd *recovd = &dev->u.recovd;
64         int err;
65
66         err = recovd_cleanup(recovd);
67         if (err)
68                 LBUG();
69
70         MOD_DEC_USE_COUNT;
71         RETURN(0);
72 }
73
74 int connmgr_iocontrol(long cmd, struct lustre_handle *hdl, int len, void *karg,
75                       void *uarg)
76 {
77         struct ptlrpc_connection *conn = NULL;
78         struct obd_device *obd = class_conn2obd(hdl);
79         struct recovd_obd *recovd = &obd->u.recovd;
80         struct obd_ioctl_data *data = karg;
81         struct list_head *tmp;
82         int rc = 0;
83
84         ENTRY;
85
86         if (cmd != OBD_IOC_RECOVD_NEWCONN && cmd != OBD_IOC_RECOVD_FAILCONN)
87                 RETURN(-EINVAL); /* XXX ENOSYS? */
88         
89         /* Find the connection that's been rebuilt or has failed. */
90         spin_lock(&recovd->recovd_lock);
91         list_for_each(tmp, &recovd->recovd_troubled_items) {
92                 conn = list_entry(tmp, struct ptlrpc_connection,
93                                   c_recovd_data.rd_managed_chain);
94
95                 LASSERT(conn->c_recovd_data.rd_recovd == recovd); /* sanity */
96
97                 if (!strcmp(conn->c_remote_uuid, data->ioc_inlbuf1))
98                         break;
99                 conn = NULL;
100         }
101
102         if (!conn) {
103                 if (cmd == OBD_IOC_RECOVD_NEWCONN)
104                         GOTO(out, rc = -EINVAL);
105                 /* XXX macroize/inline and share with loop above */
106                 list_for_each(tmp, &recovd->recovd_managed_items) {
107                         conn = list_entry(tmp, struct ptlrpc_connection,
108                                           c_recovd_data.rd_managed_chain);
109                         
110                         LASSERT(conn->c_recovd_data.rd_recovd == recovd);
111                         
112                         if (!strcmp(conn->c_remote_uuid, data->ioc_inlbuf1))
113                                 break;
114                         conn = NULL;
115                 }
116                 if (!conn)
117                         GOTO(out, rc = -EINVAL);
118         }
119
120         if (cmd == OBD_IOC_RECOVD_FAILCONN) {
121                 spin_unlock(&recovd->recovd_lock);
122                 recovd_conn_fail(conn);
123                 spin_lock(&recovd->recovd_lock);
124
125                 /* Jump straight to the "failed" phase of recovery. */
126                 conn->c_recovd_data.rd_phase = RD_FAILED;
127                 goto out;
128         }
129
130
131         /* else (NEWCONN) */
132         spin_lock(&conn->c_lock);
133
134         /* whatever happens, reset the INVALID flag */
135         conn->c_flags &= ~CONN_INVALID;
136
137         /* XXX is this a good check?  should we allow readdressing of
138          * XXX conns that aren't in recovery?
139          */
140         if (conn->c_recovd_data.rd_phase != RD_PREPARING) {
141                 spin_unlock(&conn->c_lock);
142                 GOTO(out, rc = -EALREADY);
143         }
144
145         if (data->ioc_inllen2) {
146                 CERROR("conn %p UUID change %s -> %s\n",
147                        conn, conn->c_remote_uuid, data->ioc_inlbuf2);
148                 strcpy(conn->c_remote_uuid, data->ioc_inlbuf2);
149         } else {
150                 CERROR("conn %p UUID %s reconnected\n", conn,
151                        conn->c_remote_uuid);
152         }
153         ptlrpc_readdress_connection(conn, conn->c_remote_uuid);
154         spin_unlock(&conn->c_lock);
155         
156         conn->c_recovd_data.rd_phase = RD_PREPARED;
157         wake_up(&recovd->recovd_waitq);
158  out:
159         spin_unlock(&recovd->recovd_lock);
160         RETURN(rc);
161 }
162
163 static int connmgr_connect(struct lustre_handle *conn, struct obd_device *src,
164                            obd_uuid_t cluuid, struct recovd_obd *recovd,
165                            ptlrpc_recovery_cb_t recover)
166 {
167         return class_connect(conn, src, cluuid);
168 }
169 int connmgr_attach(struct obd_device *dev, obd_count len, void *data)
170 {
171         return lprocfs_reg_obd(dev, status_var_nm_1, dev);
172 }
173
174 int conmgr_detach(struct obd_device *dev)
175 {
176         return lprocfs_dereg_obd(dev);
177 }
178 /* use obd ops to offer management infrastructure */
179 static struct obd_ops recovd_obd_ops = {
180         o_attach:      connmgr_attach,
181         o_detach:      conmgr_detach,
182         o_setup:       connmgr_setup,
183         o_cleanup:     connmgr_cleanup,
184         o_iocontrol:   connmgr_iocontrol,
185         o_connect:     connmgr_connect,
186         o_disconnect:  class_disconnect
187 };
188
189 static int __init ptlrpc_init(void)
190 {
191         int rc; 
192         rc = ptlrpc_init_portals();
193         if (rc) 
194                 RETURN(rc);
195         ptlrpc_init_connection();
196         rc = class_register_type(&recovd_obd_ops, status_class_var,
197                                  LUSTRE_HA_NAME);
198         if (rc) 
199                 RETURN(rc);
200         ptlrpc_put_connection_superhack = ptlrpc_put_connection;
201         return 0;
202 }
203
204 static void __exit ptlrpc_exit(void)
205 {
206         class_unregister_type(LUSTRE_HA_NAME);
207         ptlrpc_exit_portals();
208         ptlrpc_cleanup_connection();
209 }
210
211 /* recovd.c */
212 EXPORT_SYMBOL(ptlrpc_recovd);
213 EXPORT_SYMBOL(recovd_conn_fail);
214 EXPORT_SYMBOL(recovd_conn_manage);
215 EXPORT_SYMBOL(recovd_conn_fixed);
216 EXPORT_SYMBOL(recovd_setup);
217 EXPORT_SYMBOL(recovd_cleanup);
218
219 /* connection.c */
220 EXPORT_SYMBOL(ptlrpc_readdress_connection);
221 EXPORT_SYMBOL(ptlrpc_get_connection);
222 EXPORT_SYMBOL(ptlrpc_put_connection);
223 EXPORT_SYMBOL(ptlrpc_connection_addref);
224 EXPORT_SYMBOL(ptlrpc_init_connection);
225 EXPORT_SYMBOL(ptlrpc_cleanup_connection);
226
227 /* niobuf.c */
228 EXPORT_SYMBOL(ptlrpc_send_bulk);
229 EXPORT_SYMBOL(ptlrpc_register_bulk);
230 EXPORT_SYMBOL(ptlrpc_abort_bulk);
231 EXPORT_SYMBOL(ptlrpc_reply);
232 EXPORT_SYMBOL(ptlrpc_error);
233 EXPORT_SYMBOL(ptlrpc_resend_req);
234 EXPORT_SYMBOL(ptl_send_rpc);
235 EXPORT_SYMBOL(ptlrpc_link_svc_me);
236
237 /* client.c */
238 EXPORT_SYMBOL(ptlrpc_init_client);
239 EXPORT_SYMBOL(ptlrpc_cleanup_client);
240 EXPORT_SYMBOL(ptlrpc_req_to_uuid);
241 EXPORT_SYMBOL(ptlrpc_uuid_to_connection);
242 EXPORT_SYMBOL(ptlrpc_queue_wait);
243 EXPORT_SYMBOL(ptlrpc_continue_req);
244 EXPORT_SYMBOL(ptlrpc_replay_req);
245 EXPORT_SYMBOL(ptlrpc_restart_req);
246 EXPORT_SYMBOL(ptlrpc_prep_req);
247 EXPORT_SYMBOL(ptlrpc_free_req);
248 EXPORT_SYMBOL(ptlrpc_req_finished);
249 EXPORT_SYMBOL(ptlrpc_prep_bulk);
250 EXPORT_SYMBOL(ptlrpc_free_bulk);
251 EXPORT_SYMBOL(ptlrpc_prep_bulk_page);
252 EXPORT_SYMBOL(ptlrpc_free_bulk_page);
253 EXPORT_SYMBOL(ptlrpc_check_status);
254
255 /* service.c */
256 EXPORT_SYMBOL(ptlrpc_init_svc);
257 EXPORT_SYMBOL(ptlrpc_stop_all_threads);
258 EXPORT_SYMBOL(ptlrpc_start_thread);
259 EXPORT_SYMBOL(ptlrpc_unregister_service);
260
261 /* pack_generic.c */
262 EXPORT_SYMBOL(lustre_pack_msg);
263 EXPORT_SYMBOL(lustre_msg_size);
264 EXPORT_SYMBOL(lustre_unpack_msg);
265 EXPORT_SYMBOL(lustre_msg_buf);
266
267 /* recover.c */
268 EXPORT_SYMBOL(ptlrpc_run_recovery_upcall);
269 EXPORT_SYMBOL(ptlrpc_reconnect_import);
270 EXPORT_SYMBOL(ptlrpc_replay);
271
272 MODULE_AUTHOR("Cluster File Systems, Inc <info@clusterfs.com>");
273 MODULE_DESCRIPTION("Lustre Request Processor v1.0");
274 MODULE_LICENSE("GPL");
275
276 module_init(ptlrpc_init);
277 module_exit(ptlrpc_exit);