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