Whamcloud - gitweb
file configurable-x86-stack-2.4.22-rh.patch was initially added on branch b_devel.
[fs/lustre-release.git] / lustre / ptlrpc / ptlrpc_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 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 #define EXPORT_SYMTAB
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #ifdef __KERNEL__
26 # include <linux/module.h>
27 #else
28 # include <liblustre.h>
29 #endif
30 #include <linux/obd.h>
31 #include <linux/obd_ost.h>
32 #include <linux/lustre_mgmt.h>
33 #include <linux/lustre_net.h>
34 #include <linux/lustre_dlm.h>
35
36 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
37 {
38         struct ptlrpc_connection *conn;
39         struct obd_ioctl_data* data = buf;
40         struct client_obd *cli = &obddev->u.cli;
41         struct obd_import *imp;
42         struct obd_uuid server_uuid;
43         int rq_portal, rp_portal, connect_op;
44         char *name = obddev->obd_type->typ_name;
45         ENTRY;
46
47         /* In a more perfect world, we would hang a ptlrpc_client off of
48          * obd_type and just use the values from there. */
49         if (!strcmp(name, LUSTRE_OSC_NAME)) {
50                 rq_portal = OST_REQUEST_PORTAL;
51                 rp_portal = OSC_REPLY_PORTAL;
52                 connect_op = OST_CONNECT;
53         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
54                 rq_portal = MDS_REQUEST_PORTAL;
55                 rp_portal = MDC_REPLY_PORTAL;
56                 connect_op = MDS_CONNECT;
57         } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
58                 rq_portal = MGMT_REQUEST_PORTAL;
59                 rp_portal = MGMT_REPLY_PORTAL;
60                 connect_op = MGMT_CONNECT;
61         } else {
62                 CERROR("unknown client OBD type \"%s\", can't setup\n",
63                        name);
64                 RETURN(-EINVAL);
65         }
66
67         if (data->ioc_inllen1 < 1) {
68                 CERROR("requires a TARGET UUID\n");
69                 RETURN(-EINVAL);
70         }
71
72         if (data->ioc_inllen1 > 37) {
73                 CERROR("client UUID must be less than 38 characters\n");
74                 RETURN(-EINVAL);
75         }
76
77         if (data->ioc_inllen2 < 1) {
78                 CERROR("setup requires a SERVER UUID\n");
79                 RETURN(-EINVAL);
80         }
81
82         if (data->ioc_inllen2 > 37) {
83                 CERROR("target UUID must be less than 38 characters\n");
84                 RETURN(-EINVAL);
85         }
86
87         sema_init(&cli->cl_sem, 1);
88         cli->cl_conn_count = 0;
89         memcpy(server_uuid.uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
90                                                         sizeof(server_uuid)));
91
92         init_MUTEX(&cli->cl_dirty_sem);
93         cli->cl_dirty = 0;
94         cli->cl_dirty_granted = 0;
95         cli->cl_ost_can_grant = 1;
96
97         conn = ptlrpc_uuid_to_connection(&server_uuid);
98         if (conn == NULL)
99                 RETURN(-ENOENT);
100
101         ptlrpc_init_client(rq_portal, rp_portal, name,
102                            &obddev->obd_ldlm_client);
103
104         imp = class_new_import();
105         if (imp == NULL) {
106                 ptlrpc_put_connection(conn);
107                 RETURN(-ENOMEM);
108         }
109         imp->imp_connection = conn;
110         imp->imp_client = &obddev->obd_ldlm_client;
111         imp->imp_obd = obddev;
112         imp->imp_connect_op = connect_op;
113         imp->imp_generation = 0;
114         INIT_LIST_HEAD(&imp->imp_pinger_chain);
115         memcpy(imp->imp_target_uuid.uuid, data->ioc_inlbuf1, data->ioc_inllen1);
116         class_import_put(imp);
117
118         cli->cl_import = imp;
119         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
120         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
121         cli->cl_sandev = to_kdev_t(0);
122
123         /* Register with management client if we need to. */
124         if (data->ioc_inllen3 > 0) {
125                 char *mgmt_name = data->ioc_inlbuf3;
126                 int rc;
127                 struct obd_device *mgmt_obd;
128                 mgmtcli_register_for_events_t register_f;
129
130                 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
131                        obddev->obd_name, mgmt_name, server_uuid.uuid);
132
133                 mgmt_obd = class_name2obd(mgmt_name);
134                 if (!mgmt_obd) {
135                         CERROR("can't find mgmtcli %s to register\n",
136                                mgmt_name);
137                         class_destroy_import(imp);
138                         RETURN(-ENOENT);
139                 }
140                 
141                 register_f = inter_module_get("mgmtcli_register_for_events");
142                 if (!register_f) {
143                         CERROR("can't i_m_g mgmtcli_register_for_events\n");
144                         class_destroy_import(imp);
145                         RETURN(-ENOSYS);
146                 }
147                 
148                 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
149                 inter_module_put("mgmtcli_register_for_events");
150
151                 if (!rc)
152                         cli->cl_mgmtcli_obd = mgmt_obd;
153
154                 RETURN(rc);
155         }
156
157         RETURN(0);
158 }
159
160 int client_obd_cleanup(struct obd_device *obddev, int flags)
161 {
162         struct client_obd *cli = &obddev->u.cli;
163
164         if (!cli->cl_import)
165                 RETURN(-EINVAL);
166         if (cli->cl_mgmtcli_obd) {
167                 mgmtcli_deregister_for_events_t dereg_f;
168                 
169                 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
170                 dereg_f(cli->cl_mgmtcli_obd, obddev);
171                 inter_module_put("mgmtcli_deregister_for_events");
172         }
173         class_destroy_import(cli->cl_import);
174         cli->cl_import = NULL;
175         RETURN(0);
176 }