Whamcloud - gitweb
Branch: HEAD
[fs/lustre-release.git] / lustre / sec / gks / gks_client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * lustre GS client 
5  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26 #define DEBUG_SUBSYSTEM S_GSS
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/obd_class.h>
31 #include <linux/lustre_gs.h>
32
33 #include "gks_internal.h"
34
35 static int gkc_get_key(struct obd_export *exp, struct key_parms *kparms,
36                        struct crypto_key *ckey, int op)
37 {
38         struct ptlrpc_request *req;
39         struct crypto_key *rep_key;
40         int rc, bufcount = 1, size[3] = {0, 0, 0};
41         void *buf;
42         ENTRY;
43
44         size[0] = lustre_secdesc_size();
45
46         size[bufcount++] = kparms->context_size;
47         if (kparms->perm && kparms->perm_size > 0) {
48                 size[bufcount++] = kparms->perm_size;
49         }
50         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_GKS_VERSION,
51                               op, bufcount, size, NULL);
52         if (req == NULL)
53                 RETURN(-ENOMEM);
54
55         lustre_pack_secdesc(req, size[0]);
56         
57         buf = lustre_msg_buf(req->rq_reqmsg, 1, kparms->context_size);
58         memcpy(buf, kparms->context, kparms->context_size);
59
60         if (kparms->perm && kparms->perm_size) {
61                 buf = lustre_msg_buf(req->rq_reqmsg, 2, kparms->perm_size);
62                 memcpy(buf, kparms->perm, kparms->perm_size);
63         } 
64         
65         size[0] = sizeof(struct crypto_key);
66         req->rq_replen = lustre_msg_size(1, size);
67
68         rc = ptlrpc_queue_wait(req);
69         
70         rep_key = lustre_msg_buf(req->rq_repmsg, 0, sizeof(struct crypto_key)); 
71
72         memcpy(ckey, rep_key, sizeof(*rep_key));
73
74         CDEBUG(D_INFO, "get enkey %s, mac %s\n", ckey->ck_key, ckey->ck_mac); 
75         ptlrpc_req_finished(req);
76         
77         RETURN(rc);
78 }
79
80 static int gkc_set_info(struct obd_export *exp, obd_count keylen,
81                         void *key, obd_count vallen, void *val)
82 {
83         int rc = -EINVAL;
84         if (keylen == strlen("async") && memcmp(key, "async", keylen) == 0) {
85                 struct client_obd *cl = &exp->exp_obd->u.cli;
86                 if (vallen != sizeof(int))
87                         RETURN(-EINVAL);
88                 cl->cl_async = *(int *)val;
89                 CDEBUG(D_HA, "%s: set async = %d\n",
90                        exp->exp_obd->obd_name, cl->cl_async);
91                 RETURN(0);
92         }
93         RETURN(rc);
94 }
95
96 static int gkc_get_info(struct obd_export *exp, __u32 keylen,
97                         void *key, __u32 *vallen, void *val)
98 {
99         struct key_parms *kparms = (struct key_parms *)key;
100         struct crypto_key *ckey = (struct crypto_key *)val;
101         int rc = 0;
102         
103         ENTRY;
104        
105         LASSERT(*vallen == sizeof(*ckey));
106
107         switch (kparms->context->kc_command) {
108         case GKS_GET_KEY:
109         case GKS_DECRYPT_KEY:
110         case GKS_GET_MAC:
111                 break;
112         default:
113                 CERROR("Unknow op %d \n", kparms->context->kc_command);
114                 rc = -EINVAL; 
115                 RETURN(rc);
116         }
117         rc = gkc_get_key(exp, kparms, ckey, kparms->context->kc_command);
118         RETURN(rc); 
119 }  
120 static int gkc_setup(struct obd_device *obd, obd_count len, void *buf)
121 {
122         struct client_obd *cli = &obd->u.cli;
123         int rc;
124         ENTRY;
125
126         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
127         if (!cli->cl_rpc_lock)
128                 RETURN(-ENOMEM);
129         gkc_init_rpc_lock(cli->cl_rpc_lock);
130
131         ptlrpcd_addref();
132
133         rc = client_obd_setup(obd, len, buf);
134         if (rc)
135                 GOTO(err_rpc_lock, rc);
136
137         RETURN(rc);
138 err_rpc_lock:
139         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
140         ptlrpcd_decref();
141         RETURN(rc);
142 }
143
144 static int gkc_cleanup(struct obd_device *obd, int flags)
145 {
146         struct client_obd *cli = &obd->u.cli;
147
148         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
149
150         ptlrpcd_decref();
151
152         return client_obd_cleanup(obd, 0);
153 }
154
155 static int gkc_attach(struct obd_device *dev, obd_count len, void *data)
156 {
157         struct lprocfs_static_vars lvars;
158
159         lprocfs_init_vars(gkc, &lvars);
160         return lprocfs_obd_attach(dev, lvars.obd_vars);
161 }
162
163 static int gkc_detach(struct obd_device *dev)
164 {
165         return lprocfs_obd_detach(dev);
166 }
167
168 static struct obd_ops gkc_obd_ops = {
169         .o_owner           = THIS_MODULE,
170         .o_connect         = client_connect_import,
171         .o_disconnect      = client_disconnect_export,
172         .o_attach          = gkc_attach,
173         .o_detach          = gkc_detach,
174         .o_setup           = gkc_setup,
175         .o_cleanup         = gkc_cleanup,
176         .o_get_info         = gkc_get_info,
177         .o_set_info        = gkc_set_info,
178 };
179
180 static int __init gkc_init(void)
181 {
182         struct lprocfs_static_vars lvars;
183
184         lprocfs_init_vars(gkc, &lvars);
185         class_register_type(&gkc_obd_ops, NULL, lvars.module_vars,
186                             LUSTRE_GKC_NAME);
187         return 0;
188 }
189
190 static void gkc_exit(void)
191 {
192         class_unregister_type(LUSTRE_GKC_NAME);
193 }
194
195 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
196 MODULE_DESCRIPTION("Lustre GS Client (GS)");
197 MODULE_LICENSE("GPL");
198
199 module_init(gkc_init);
200 module_exit(gkc_exit);
201