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