Whamcloud - gitweb
daa82bce1e59ad4d12239e72729e6cd0bdb00ccd
[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  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/capa.c
37  *
38  * Lustre Capability Hash Management
39  *
40  * Author: Lai Siyao<lsy@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_SEC
44
45 #ifdef __KERNEL__
46 #include <linux/version.h>
47 #include <linux/fs.h>
48 #include <asm/unistd.h>
49 #include <linux/slab.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52
53 #include <obd_class.h>
54 #include <lustre_debug.h>
55 #include <lustre/lustre_idl.h>
56 #else
57 #include <liblustre.h>
58 #endif
59
60 #include <libcfs/list.h>
61 #include <lustre_capa.h>
62
63 #define NR_CAPAHASH 32
64 #define CAPA_HASH_SIZE 3000              /* for MDS & OSS */
65
66 struct kmem_cache *capa_cachep;
67
68 #ifdef __KERNEL__
69 /* lock for capa hash/capa_list/fo_capa_keys */
70 DEFINE_SPINLOCK(capa_lock);
71
72 cfs_list_t capa_list[CAPA_SITE_MAX];
73
74 static struct capa_hmac_alg capa_hmac_algs[] = {
75         DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
76 };
77 #endif
78 /* capa count */
79 int capa_count[CAPA_SITE_MAX] = { 0, };
80
81 EXPORT_SYMBOL(capa_cachep);
82 EXPORT_SYMBOL(capa_list);
83 EXPORT_SYMBOL(capa_lock);
84 EXPORT_SYMBOL(capa_count);
85
86 cfs_hlist_head_t *init_capa_hash(void)
87 {
88         cfs_hlist_head_t *hash;
89         int nr_hash, i;
90
91         OBD_ALLOC(hash, PAGE_CACHE_SIZE);
92         if (!hash)
93                 return NULL;
94
95         nr_hash = PAGE_CACHE_SIZE / sizeof(cfs_hlist_head_t);
96         LASSERT(nr_hash > NR_CAPAHASH);
97
98         for (i = 0; i < NR_CAPAHASH; i++)
99                 CFS_INIT_HLIST_HEAD(hash + i);
100         return hash;
101 }
102 EXPORT_SYMBOL(init_capa_hash);
103
104 #ifdef __KERNEL__
105 static inline int capa_on_server(struct obd_capa *ocapa)
106 {
107         return ocapa->c_site == CAPA_SITE_SERVER;
108 }
109
110 static inline void capa_delete(struct obd_capa *ocapa)
111 {
112         LASSERT(capa_on_server(ocapa));
113         cfs_hlist_del_init(&ocapa->u.tgt.c_hash);
114         cfs_list_del_init(&ocapa->c_list);
115         capa_count[ocapa->c_site]--;
116         /* release the ref when alloc */
117         capa_put(ocapa);
118 }
119
120 void cleanup_capa_hash(cfs_hlist_head_t *hash)
121 {
122         int i;
123         cfs_hlist_node_t *pos, *next;
124         struct obd_capa *oc;
125
126         spin_lock(&capa_lock);
127         for (i = 0; i < NR_CAPAHASH; i++) {
128                 cfs_hlist_for_each_entry_safe(oc, pos, next, hash + i,
129                                               u.tgt.c_hash)
130                         capa_delete(oc);
131         }
132         spin_unlock(&capa_lock);
133
134         OBD_FREE(hash, PAGE_CACHE_SIZE);
135 }
136 EXPORT_SYMBOL(cleanup_capa_hash);
137
138 static inline int capa_hashfn(struct lu_fid *fid)
139 {
140         return (fid_oid(fid) ^ fid_ver(fid)) *
141                (unsigned long)(fid_seq(fid) + 1) % NR_CAPAHASH;
142 }
143
144 /* capa renewal time check is earlier than that on client, which is to prevent
145  * client renew right after obtaining it. */
146 static inline int capa_is_to_expire(struct obd_capa *oc)
147 {
148         return cfs_time_before(cfs_time_sub(oc->c_expiry,
149                                    cfs_time_seconds(oc->c_capa.lc_timeout)*2/3),
150                                cfs_time_current());
151 }
152
153 static struct obd_capa *find_capa(struct lustre_capa *capa,
154                                   cfs_hlist_head_t *head, int alive)
155 {
156         cfs_hlist_node_t *pos;
157         struct obd_capa *ocapa;
158         int len = alive ? offsetof(struct lustre_capa, lc_keyid):sizeof(*capa);
159
160         cfs_hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
161                 if (memcmp(&ocapa->c_capa, capa, len))
162                         continue;
163                 /* don't return one that will expire soon in this case */
164                 if (alive && capa_is_to_expire(ocapa))
165                         continue;
166
167                 LASSERT(capa_on_server(ocapa));
168
169                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
170                 return ocapa;
171         }
172
173         return NULL;
174 }
175
176 #define LRU_CAPA_DELETE_COUNT 12
177 static inline void capa_delete_lru(cfs_list_t *head)
178 {
179         struct obd_capa *ocapa;
180         cfs_list_t *node = head->next;
181         int count = 0;
182
183         /* free LRU_CAPA_DELETE_COUNT unused capa from head */
184         while (count++ < LRU_CAPA_DELETE_COUNT) {
185                 ocapa = cfs_list_entry(node, struct obd_capa, c_list);
186                 node = node->next;
187                 if (atomic_read(&ocapa->c_refc))
188                         continue;
189
190                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free lru");
191                 capa_delete(ocapa);
192         }
193 }
194
195 /* add or update */
196 struct obd_capa *capa_add(cfs_hlist_head_t *hash, struct lustre_capa *capa)
197 {
198         cfs_hlist_head_t *head = hash + capa_hashfn(&capa->lc_fid);
199         struct obd_capa *ocapa, *old = NULL;
200         cfs_list_t *list = &capa_list[CAPA_SITE_SERVER];
201
202         ocapa = alloc_capa(CAPA_SITE_SERVER);
203         if (IS_ERR(ocapa))
204                 return NULL;
205
206         spin_lock(&capa_lock);
207         old = find_capa(capa, head, 0);
208         if (!old) {
209                 ocapa->c_capa = *capa;
210                 set_capa_expiry(ocapa);
211                 cfs_hlist_add_head(&ocapa->u.tgt.c_hash, head);
212                 cfs_list_add_tail(&ocapa->c_list, list);
213                 capa_get(ocapa);
214                 capa_count[CAPA_SITE_SERVER]++;
215                 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
216                         capa_delete_lru(list);
217                 spin_unlock(&capa_lock);
218                 return ocapa;
219         } else {
220                 capa_get(old);
221                 spin_unlock(&capa_lock);
222                 capa_put(ocapa);
223                 return old;
224         }
225 }
226 EXPORT_SYMBOL(capa_add);
227
228 struct obd_capa *capa_lookup(cfs_hlist_head_t *hash, struct lustre_capa *capa,
229                              int alive)
230 {
231         struct obd_capa *ocapa;
232
233         spin_lock(&capa_lock);
234         ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
235         if (ocapa) {
236                 cfs_list_move_tail(&ocapa->c_list,
237                                    &capa_list[CAPA_SITE_SERVER]);
238                 capa_get(ocapa);
239         }
240         spin_unlock(&capa_lock);
241
242         return ocapa;
243 }
244 EXPORT_SYMBOL(capa_lookup);
245
246 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
247 {
248         struct crypto_hash *tfm;
249         struct capa_hmac_alg  *alg;
250         int keylen;
251         struct scatterlist sl;
252
253         if (capa_alg(capa) != CAPA_HMAC_ALG_SHA1) {
254                 CERROR("unknown capability hmac algorithm!\n");
255                 return -EFAULT;
256         }
257
258         alg = &capa_hmac_algs[capa_alg(capa)];
259
260         tfm = crypto_alloc_hash(alg->ha_name, 0, 0);
261         if (IS_ERR(tfm)) {
262                 CERROR("crypto_alloc_tfm failed, check whether your kernel"
263                        "has crypto support!\n");
264                 return PTR_ERR(tfm);
265         }
266         keylen = alg->ha_keylen;
267
268         sg_init_table(&sl, 1);
269         sg_set_page(&sl, virt_to_page(capa),
270                     offsetof(struct lustre_capa, lc_hmac),
271                     (unsigned long)(capa) % PAGE_CACHE_SIZE);
272
273         ll_crypto_hmac(tfm, key, &keylen, &sl, sl.length, hmac);
274         crypto_free_hash(tfm);
275
276         return 0;
277 }
278 EXPORT_SYMBOL(capa_hmac);
279
280 int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
281 {
282         struct crypto_blkcipher *tfm;
283         struct scatterlist sd;
284         struct scatterlist ss;
285         struct blkcipher_desc desc;
286         unsigned int min;
287         int rc;
288         char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
289         ENTRY;
290
291         /* passing "aes" in a variable instead of a constant string keeps gcc
292          * 4.3.2 happy */
293         tfm = crypto_alloc_blkcipher(alg, 0, 0 );
294         if (IS_ERR(tfm)) {
295                 CERROR("failed to load transform for aes\n");
296                 RETURN(PTR_ERR(tfm));
297         }
298
299         min = ll_crypto_tfm_alg_min_keysize(tfm);
300         if (keylen < min) {
301                 CERROR("keylen at least %d bits for aes\n", min * 8);
302                 GOTO(out, rc = -EINVAL);
303         }
304
305         rc = crypto_blkcipher_setkey(tfm, key, min);
306         if (rc) {
307                 CERROR("failed to setting key for aes\n");
308                 GOTO(out, rc);
309         }
310
311         sg_init_table(&sd, 1);
312         sg_set_page(&sd, virt_to_page(d), 16,
313                     (unsigned long)(d) % PAGE_CACHE_SIZE);
314
315         sg_init_table(&ss, 1);
316         sg_set_page(&ss, virt_to_page(s), 16,
317                     (unsigned long)(s) % PAGE_CACHE_SIZE);
318         desc.tfm   = tfm;
319         desc.info  = NULL;
320         desc.flags = 0;
321         rc = crypto_blkcipher_encrypt(&desc, &sd, &ss, 16);
322         if (rc) {
323                 CERROR("failed to encrypt for aes\n");
324                 GOTO(out, rc);
325         }
326
327         EXIT;
328
329 out:
330         crypto_free_blkcipher(tfm);
331         return rc;
332 }
333 EXPORT_SYMBOL(capa_encrypt_id);
334
335 int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
336 {
337         struct crypto_blkcipher *tfm;
338         struct scatterlist sd;
339         struct scatterlist ss;
340         struct blkcipher_desc desc;
341         unsigned int min;
342         int rc;
343         char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
344         ENTRY;
345
346         /* passing "aes" in a variable instead of a constant string keeps gcc
347          * 4.3.2 happy */
348         tfm = crypto_alloc_blkcipher(alg, 0, 0 );
349         if (IS_ERR(tfm)) {
350                 CERROR("failed to load transform for aes\n");
351                 RETURN(PTR_ERR(tfm));
352         }
353
354         min = ll_crypto_tfm_alg_min_keysize(tfm);
355         if (keylen < min) {
356                 CERROR("keylen at least %d bits for aes\n", min * 8);
357                 GOTO(out, rc = -EINVAL);
358         }
359
360         rc = crypto_blkcipher_setkey(tfm, key, min);
361         if (rc) {
362                 CERROR("failed to setting key for aes\n");
363                 GOTO(out, rc);
364         }
365
366         sg_init_table(&sd, 1);
367         sg_set_page(&sd, virt_to_page(d), 16,
368                     (unsigned long)(d) % PAGE_CACHE_SIZE);
369
370         sg_init_table(&ss, 1);
371         sg_set_page(&ss, virt_to_page(s), 16,
372                     (unsigned long)(s) % PAGE_CACHE_SIZE);
373
374         desc.tfm   = tfm;
375         desc.info  = NULL;
376         desc.flags = 0;
377         rc = crypto_blkcipher_decrypt(&desc, &sd, &ss, 16);
378         if (rc) {
379                 CERROR("failed to decrypt for aes\n");
380                 GOTO(out, rc);
381         }
382
383         EXIT;
384
385 out:
386         crypto_free_blkcipher(tfm);
387         return rc;
388 }
389 EXPORT_SYMBOL(capa_decrypt_id);
390 #endif
391
392 void capa_cpy(void *capa, struct obd_capa *ocapa)
393 {
394         spin_lock(&ocapa->c_lock);
395         *(struct lustre_capa *)capa = ocapa->c_capa;
396         spin_unlock(&ocapa->c_lock);
397 }
398 EXPORT_SYMBOL(capa_cpy);
399
400 void _debug_capa(struct lustre_capa *c,
401                  struct libcfs_debug_msg_data *msgdata,
402                  const char *fmt, ... )
403 {
404         va_list args;
405         va_start(args, fmt);
406         libcfs_debug_vmsg2(msgdata, fmt, args,
407                            " capability@%p fid "DFID" opc "LPX64" uid "LPU64
408                            " gid "LPU64" flags %u alg %d keyid %u timeout %u "
409                            "expiry %u\n", c, PFID(capa_fid(c)), capa_opc(c),
410                            capa_uid(c), capa_gid(c), capa_flags(c),
411                            capa_alg(c), capa_keyid(c), capa_timeout(c),
412                            capa_expiry(c));
413         va_end(args);
414 }
415 EXPORT_SYMBOL(_debug_capa);
416
417 /*
418  * context key constructor/destructor:
419  * lu_capainfo_key_init, lu_capainfo_key_fini
420  */
421 LU_KEY_INIT_FINI(lu_capainfo, struct lu_capainfo);
422
423 struct lu_context_key lu_capainfo_key = {
424         .lct_tags = LCT_SERVER_SESSION,
425         .lct_init = lu_capainfo_key_init,
426         .lct_fini = lu_capainfo_key_fini
427 };
428
429 struct lu_capainfo *lu_capainfo_get(const struct lu_env *env)
430 {
431         /* NB, in mdt_init0 */
432         if (env->le_ses == NULL)
433                 return NULL;
434         return lu_context_key_get(env->le_ses, &lu_capainfo_key);
435 }
436 EXPORT_SYMBOL(lu_capainfo_get);
437
438 /**
439  * Initialization of lu_capainfo_key data.
440  */
441 int lu_capainfo_init(void)
442 {
443         int rc;
444
445         LU_CONTEXT_KEY_INIT(&lu_capainfo_key);
446         rc = lu_context_key_register(&lu_capainfo_key);
447         return rc;
448 }
449
450 /**
451  * Dual to lu_capainfo_init().
452  */
453 void lu_capainfo_fini(void)
454 {
455         lu_context_key_degister(&lu_capainfo_key);
456 }