Whamcloud - gitweb
current branches now use lnet from 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 key %s, mac %s type %d\n", ckey->ck_key, ckey->ck_mac, 
75                ckey->ck_type); 
76         ptlrpc_req_finished(req);
77         
78         RETURN(rc);
79 }
80
81 static int gkc_set_info(struct obd_export *exp, obd_count keylen,
82                         void *key, obd_count vallen, void *val)
83 {
84         int rc = -EINVAL;
85         if (keylen == strlen("async") && memcmp(key, "async", keylen) == 0) {
86                 struct client_obd *cl = &exp->exp_obd->u.cli;
87                 if (vallen != sizeof(int))
88                         RETURN(-EINVAL);
89                 cl->cl_async = *(int *)val;
90                 CDEBUG(D_HA, "%s: set async = %d\n",
91                        exp->exp_obd->obd_name, cl->cl_async);
92                 RETURN(0);
93         }
94         RETURN(rc);
95 }
96
97 static int gkc_get_info(struct obd_export *exp, __u32 keylen,
98                         void *key, __u32 *vallen, void *val)
99 {
100         struct key_parms *kparms = (struct key_parms *)key;
101         struct crypto_key *ckey = (struct crypto_key *)val;
102         int rc = 0;
103         
104         ENTRY;
105        
106         LASSERT(*vallen == sizeof(*ckey));
107
108         switch (kparms->context->kc_command) {
109         case GKS_GET_KEY:
110         case GKS_DECRYPT_KEY:
111         case GKS_GET_MAC:
112                 break;
113         default:
114                 CERROR("Unknow op %d \n", kparms->context->kc_command);
115                 rc = -EINVAL; 
116                 RETURN(rc);
117         }
118         rc = gkc_get_key(exp, kparms, ckey, kparms->context->kc_command);
119         RETURN(rc); 
120 }  
121 static int gkc_setup(struct obd_device *obd, obd_count len, void *buf)
122 {
123         struct client_obd *cli = &obd->u.cli;
124         int rc;
125         ENTRY;
126
127         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
128         if (!cli->cl_rpc_lock)
129                 RETURN(-ENOMEM);
130         gkc_init_rpc_lock(cli->cl_rpc_lock);
131
132         ptlrpcd_addref();
133
134         rc = client_obd_setup(obd, len, buf);
135         if (rc)
136                 GOTO(err_rpc_lock, rc);
137
138         RETURN(rc);
139 err_rpc_lock:
140         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
141         ptlrpcd_decref();
142         RETURN(rc);
143 }
144
145 static int gkc_cleanup(struct obd_device *obd, int flags)
146 {
147         struct client_obd *cli = &obd->u.cli;
148
149         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
150
151         ptlrpcd_decref();
152
153         return client_obd_cleanup(obd, 0);
154 }
155
156 static int gkc_attach(struct obd_device *dev, obd_count len, void *data)
157 {
158         struct lprocfs_static_vars lvars;
159
160         lprocfs_init_vars(gkc, &lvars);
161         return lprocfs_obd_attach(dev, lvars.obd_vars);
162 }
163
164 static int gkc_detach(struct obd_device *dev)
165 {
166         return lprocfs_obd_detach(dev);
167 }
168
169 static struct obd_ops gkc_obd_ops = {
170         .o_owner           = THIS_MODULE,
171         .o_connect         = client_connect_import,
172         .o_disconnect      = client_disconnect_export,
173         .o_attach          = gkc_attach,
174         .o_detach          = gkc_detach,
175         .o_setup           = gkc_setup,
176         .o_cleanup         = gkc_cleanup,
177         .o_get_info         = gkc_get_info,
178         .o_set_info        = gkc_set_info,
179 };
180
181 static int __init gkc_init(void)
182 {
183         struct lprocfs_static_vars lvars;
184
185         lprocfs_init_vars(gkc, &lvars);
186         class_register_type(&gkc_obd_ops, NULL, lvars.module_vars,
187                             LUSTRE_GKC_NAME);
188         return 0;
189 }
190
191 static void gkc_exit(void)
192 {
193         class_unregister_type(LUSTRE_GKC_NAME);
194 }
195
196 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
197 MODULE_DESCRIPTION("Lustre GS Client (GS)");
198 MODULE_LICENSE("GPL");
199
200 module_init(gkc_init);
201 module_exit(gkc_exit);
202