Whamcloud - gitweb
539910ce7d646eae5e66eca25e8692c75441f7d5
[fs/lustre-release.git] / lustre / obdclass / capa.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdclass/capa.c
35  *
36  * Lustre Capability Hash Management
37  *
38  * Author: Lai Siyao<lsy@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #ifdef __KERNEL__
44 #include <linux/version.h>
45 #include <linux/fs.h>
46 #include <asm/unistd.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/init.h>
50
51 #include <obd_class.h>
52 #include <lustre_debug.h>
53 #include <lustre/lustre_idl.h>
54 #else
55 #include <liblustre.h>
56 #endif
57
58 #include <libcfs/list.h>
59 #include <lustre_capa.h>
60
61 #define NR_CAPAHASH 32
62 #define CAPA_HASH_SIZE 3000              /* for MDS & OSS */
63
64 cfs_mem_cache_t *capa_cachep = NULL;
65
66 #ifdef __KERNEL__
67 /* lock for capa hash/capa_list/fo_capa_keys */
68 cfs_spinlock_t capa_lock = CFS_SPIN_LOCK_UNLOCKED;
69
70 cfs_list_t capa_list[CAPA_SITE_MAX];
71
72 static struct capa_hmac_alg capa_hmac_algs[] = {
73         DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
74 };
75 #endif
76 /* capa count */
77 int capa_count[CAPA_SITE_MAX] = { 0, };
78
79 EXPORT_SYMBOL(capa_cachep);
80 EXPORT_SYMBOL(capa_list);
81 EXPORT_SYMBOL(capa_lock);
82 EXPORT_SYMBOL(capa_count);
83
84 cfs_hlist_head_t *init_capa_hash(void)
85 {
86         cfs_hlist_head_t *hash;
87         int nr_hash, i;
88
89         OBD_ALLOC(hash, CFS_PAGE_SIZE);
90         if (!hash)
91                 return NULL;
92
93         nr_hash = CFS_PAGE_SIZE / sizeof(cfs_hlist_head_t);
94         LASSERT(nr_hash > NR_CAPAHASH);
95
96         for (i = 0; i < NR_CAPAHASH; i++)
97                 CFS_INIT_HLIST_HEAD(hash + i);
98         return hash;
99 }
100
101 #ifdef __KERNEL__
102 static inline int capa_on_server(struct obd_capa *ocapa)
103 {
104         return ocapa->c_site == CAPA_SITE_SERVER;
105 }
106
107 static inline void capa_delete(struct obd_capa *ocapa)
108 {
109         LASSERT(capa_on_server(ocapa));
110         cfs_hlist_del_init(&ocapa->u.tgt.c_hash);
111         cfs_list_del_init(&ocapa->c_list);
112         capa_count[ocapa->c_site]--;
113         /* release the ref when alloc */
114         capa_put(ocapa);
115 }
116
117 void cleanup_capa_hash(cfs_hlist_head_t *hash)
118 {
119         int i;
120         cfs_hlist_node_t *pos, *next;
121         struct obd_capa *oc;
122
123         cfs_spin_lock(&capa_lock);
124         for (i = 0; i < NR_CAPAHASH; i++) {
125                 cfs_hlist_for_each_entry_safe(oc, pos, next, hash + i,
126                                               u.tgt.c_hash)
127                         capa_delete(oc);
128         }
129         cfs_spin_unlock(&capa_lock);
130
131         OBD_FREE(hash, CFS_PAGE_SIZE);
132 }
133
134 static inline int capa_hashfn(struct lu_fid *fid)
135 {
136         return (fid_oid(fid) ^ fid_ver(fid)) *
137                (unsigned long)(fid_seq(fid) + 1) % NR_CAPAHASH;
138 }
139
140 /* capa renewal time check is earlier than that on client, which is to prevent
141  * client renew right after obtaining it. */
142 static inline int capa_is_to_expire(struct obd_capa *oc)
143 {
144         return cfs_time_before(cfs_time_sub(oc->c_expiry,
145                                    cfs_time_seconds(oc->c_capa.lc_timeout)*2/3),
146                                cfs_time_current());
147 }
148
149 static struct obd_capa *find_capa(struct lustre_capa *capa,
150                                   cfs_hlist_head_t *head, int alive)
151 {
152         cfs_hlist_node_t *pos;
153         struct obd_capa *ocapa;
154         int len = alive ? offsetof(struct lustre_capa, lc_keyid):sizeof(*capa);
155
156         cfs_hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
157                 if (memcmp(&ocapa->c_capa, capa, len))
158                         continue;
159                 /* don't return one that will expire soon in this case */
160                 if (alive && capa_is_to_expire(ocapa))
161                         continue;
162
163                 LASSERT(capa_on_server(ocapa));
164
165                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
166                 return ocapa;
167         }
168
169         return NULL;
170 }
171
172 #define LRU_CAPA_DELETE_COUNT 12
173 static inline void capa_delete_lru(cfs_list_t *head)
174 {
175         struct obd_capa *ocapa;
176         cfs_list_t *node = head->next;
177         int count = 0;
178
179         /* free LRU_CAPA_DELETE_COUNT unused capa from head */
180         while (count++ < LRU_CAPA_DELETE_COUNT) {
181                 ocapa = cfs_list_entry(node, struct obd_capa, c_list);
182                 node = node->next;
183                 if (cfs_atomic_read(&ocapa->c_refc))
184                         continue;
185
186                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free lru");
187                 capa_delete(ocapa);
188         }
189 }
190
191 /* add or update */
192 struct obd_capa *capa_add(cfs_hlist_head_t *hash, struct lustre_capa *capa)
193 {
194         cfs_hlist_head_t *head = hash + capa_hashfn(&capa->lc_fid);
195         struct obd_capa *ocapa, *old = NULL;
196         cfs_list_t *list = &capa_list[CAPA_SITE_SERVER];
197
198         ocapa = alloc_capa(CAPA_SITE_SERVER);
199         if (IS_ERR(ocapa))
200                 return NULL;
201
202         cfs_spin_lock(&capa_lock);
203         old = find_capa(capa, head, 0);
204         if (!old) {
205                 ocapa->c_capa = *capa;
206                 set_capa_expiry(ocapa);
207                 cfs_hlist_add_head(&ocapa->u.tgt.c_hash, head);
208                 cfs_list_add_tail(&ocapa->c_list, list);
209                 capa_get(ocapa);
210                 capa_count[CAPA_SITE_SERVER]++;
211                 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
212                         capa_delete_lru(list);
213                 cfs_spin_unlock(&capa_lock);
214                 return ocapa;
215         } else {
216                 capa_get(old);
217                 cfs_spin_unlock(&capa_lock);
218                 capa_put(ocapa);
219                 return old;
220         }
221 }
222
223 struct obd_capa *capa_lookup(cfs_hlist_head_t *hash, struct lustre_capa *capa,
224                              int alive)
225 {
226         struct obd_capa *ocapa;
227
228         cfs_spin_lock(&capa_lock);
229         ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
230         if (ocapa) {
231                 cfs_list_move_tail(&ocapa->c_list,
232                                    &capa_list[CAPA_SITE_SERVER]);
233                 capa_get(ocapa);
234         }
235         cfs_spin_unlock(&capa_lock);
236
237         return ocapa;
238 }
239
240 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
241 {
242         struct ll_crypto_hash *tfm;
243         struct capa_hmac_alg  *alg;
244         int keylen;
245         struct scatterlist sl;
246
247         if (capa_alg(capa) != CAPA_HMAC_ALG_SHA1) {
248                 CERROR("unknown capability hmac algorithm!\n");
249                 return -EFAULT;
250         }
251
252         alg = &capa_hmac_algs[capa_alg(capa)];
253
254         tfm = ll_crypto_alloc_hash(alg->ha_name, 0, 0);
255         if (!tfm) {
256                 CERROR("crypto_alloc_tfm failed, check whether your kernel"
257                        "has crypto support!\n");
258                 return -ENOMEM;
259         }
260         keylen = alg->ha_keylen;
261
262         sg_set_page(&sl, virt_to_page(capa),
263                     offsetof(struct lustre_capa, lc_hmac),
264                     (unsigned long)(capa) % CFS_PAGE_SIZE);
265
266         ll_crypto_hmac(tfm, key, &keylen, &sl, sl.length, hmac);
267         ll_crypto_free_hash(tfm);
268
269         return 0;
270 }
271
272 int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
273 {
274         struct ll_crypto_cipher *tfm;
275         struct scatterlist sd;
276         struct scatterlist ss;
277         struct blkcipher_desc desc;
278         unsigned int min;
279         int rc;
280         char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
281         ENTRY;
282
283         /* passing "aes" in a variable instead of a constant string keeps gcc
284          * 4.3.2 happy */
285         tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
286         if (IS_ERR(tfm)) {
287                 CERROR("failed to load transform for aes\n");
288                 RETURN(PTR_ERR(tfm));
289         }
290
291         min = ll_crypto_tfm_alg_min_keysize(tfm);
292         if (keylen < min) {
293                 CERROR("keylen at least %d bits for aes\n", min * 8);
294                 GOTO(out, rc = -EINVAL);
295         }
296
297         rc = ll_crypto_blkcipher_setkey(tfm, key, min);
298         if (rc) {
299                 CERROR("failed to setting key for aes\n");
300                 GOTO(out, rc);
301         }
302
303         sg_set_page(&sd, virt_to_page(d), 16,
304                     (unsigned long)(d) % CFS_PAGE_SIZE);
305
306         sg_set_page(&ss, virt_to_page(s), 16,
307                     (unsigned long)(s) % CFS_PAGE_SIZE);
308         desc.tfm   = tfm;
309         desc.info  = NULL;
310         desc.flags = 0;
311         rc = ll_crypto_blkcipher_encrypt(&desc, &sd, &ss, 16);
312         if (rc) {
313                 CERROR("failed to encrypt for aes\n");
314                 GOTO(out, rc);
315         }
316
317         EXIT;
318
319 out:
320         ll_crypto_free_blkcipher(tfm);
321         return rc;
322 }
323
324 int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
325 {
326         struct ll_crypto_cipher *tfm;
327         struct scatterlist sd;
328         struct scatterlist ss;
329         struct blkcipher_desc desc;
330         unsigned int min;
331         int rc;
332         char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
333         ENTRY;
334
335         /* passing "aes" in a variable instead of a constant string keeps gcc
336          * 4.3.2 happy */
337         tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
338         if (IS_ERR(tfm)) {
339                 CERROR("failed to load transform for aes\n");
340                 RETURN(PTR_ERR(tfm));
341         }
342
343         min = ll_crypto_tfm_alg_min_keysize(tfm);
344         if (keylen < min) {
345                 CERROR("keylen at least %d bits for aes\n", min * 8);
346                 GOTO(out, rc = -EINVAL);
347         }
348
349         rc = ll_crypto_blkcipher_setkey(tfm, key, min);
350         if (rc) {
351                 CERROR("failed to setting key for aes\n");
352                 GOTO(out, rc);
353         }
354
355         sg_set_page(&sd, virt_to_page(d), 16,
356                     (unsigned long)(d) % CFS_PAGE_SIZE);
357
358         sg_set_page(&ss, virt_to_page(s), 16,
359                     (unsigned long)(s) % CFS_PAGE_SIZE);
360
361         desc.tfm   = tfm;
362         desc.info  = NULL;
363         desc.flags = 0;
364         rc = ll_crypto_blkcipher_decrypt(&desc, &sd, &ss, 16);
365         if (rc) {
366                 CERROR("failed to decrypt for aes\n");
367                 GOTO(out, rc);
368         }
369
370         EXIT;
371
372 out:
373         ll_crypto_free_blkcipher(tfm);
374         return rc;
375 }
376 #endif
377
378 void capa_cpy(void *capa, struct obd_capa *ocapa)
379 {
380         cfs_spin_lock(&ocapa->c_lock);
381         *(struct lustre_capa *)capa = ocapa->c_capa;
382         cfs_spin_unlock(&ocapa->c_lock);
383 }
384
385 void _debug_capa(struct lustre_capa *c,
386                  struct libcfs_debug_msg_data *msgdata,
387                  const char *fmt, ... )
388 {
389         va_list args;
390         va_start(args, fmt);
391         libcfs_debug_vmsg2(msgdata, fmt, args,
392                            " capability@%p fid "DFID" opc "LPX64" uid "LPU64
393                            " gid "LPU64" flags %u alg %d keyid %u timeout %u "
394                            "expiry %u\n", c, PFID(capa_fid(c)), capa_opc(c),
395                            capa_uid(c), capa_gid(c), capa_flags(c),
396                            capa_alg(c), capa_keyid(c), capa_timeout(c),
397                            capa_expiry(c));
398         va_end(args);
399 }
400 EXPORT_SYMBOL(_debug_capa);
401
402 EXPORT_SYMBOL(init_capa_hash);
403 EXPORT_SYMBOL(cleanup_capa_hash);
404 EXPORT_SYMBOL(capa_add);
405 EXPORT_SYMBOL(capa_lookup);
406 EXPORT_SYMBOL(capa_hmac);
407 EXPORT_SYMBOL(capa_encrypt_id);
408 EXPORT_SYMBOL(capa_decrypt_id);
409 EXPORT_SYMBOL(capa_cpy);