4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdclass/capa.c
36 * Lustre Capability Hash Management
38 * Author: Lai Siyao<lsy@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_SEC
44 #include <linux/version.h>
46 #include <asm/unistd.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/init.h>
51 #include <obd_class.h>
52 #include <lustre_debug.h>
53 #include <lustre/lustre_idl.h>
55 #include <liblustre.h>
58 #include <libcfs/list.h>
59 #include <lustre_capa.h>
61 #define NR_CAPAHASH 32
62 #define CAPA_HASH_SIZE 3000 /* for MDS & OSS */
64 cfs_mem_cache_t *capa_cachep = NULL;
67 /* lock for capa hash/capa_list/fo_capa_keys */
68 DEFINE_SPINLOCK(capa_lock);
70 cfs_list_t capa_list[CAPA_SITE_MAX];
72 static struct capa_hmac_alg capa_hmac_algs[] = {
73 DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
77 int capa_count[CAPA_SITE_MAX] = { 0, };
79 EXPORT_SYMBOL(capa_cachep);
80 EXPORT_SYMBOL(capa_list);
81 EXPORT_SYMBOL(capa_lock);
82 EXPORT_SYMBOL(capa_count);
84 cfs_hlist_head_t *init_capa_hash(void)
86 cfs_hlist_head_t *hash;
89 OBD_ALLOC(hash, CFS_PAGE_SIZE);
93 nr_hash = CFS_PAGE_SIZE / sizeof(cfs_hlist_head_t);
94 LASSERT(nr_hash > NR_CAPAHASH);
96 for (i = 0; i < NR_CAPAHASH; i++)
97 CFS_INIT_HLIST_HEAD(hash + i);
100 EXPORT_SYMBOL(init_capa_hash);
103 static inline int capa_on_server(struct obd_capa *ocapa)
105 return ocapa->c_site == CAPA_SITE_SERVER;
108 static inline void capa_delete(struct obd_capa *ocapa)
110 LASSERT(capa_on_server(ocapa));
111 cfs_hlist_del_init(&ocapa->u.tgt.c_hash);
112 cfs_list_del_init(&ocapa->c_list);
113 capa_count[ocapa->c_site]--;
114 /* release the ref when alloc */
118 void cleanup_capa_hash(cfs_hlist_head_t *hash)
121 cfs_hlist_node_t *pos, *next;
124 spin_lock(&capa_lock);
125 for (i = 0; i < NR_CAPAHASH; i++) {
126 cfs_hlist_for_each_entry_safe(oc, pos, next, hash + i,
130 spin_unlock(&capa_lock);
132 OBD_FREE(hash, CFS_PAGE_SIZE);
134 EXPORT_SYMBOL(cleanup_capa_hash);
136 static inline int capa_hashfn(struct lu_fid *fid)
138 return (fid_oid(fid) ^ fid_ver(fid)) *
139 (unsigned long)(fid_seq(fid) + 1) % NR_CAPAHASH;
142 /* capa renewal time check is earlier than that on client, which is to prevent
143 * client renew right after obtaining it. */
144 static inline int capa_is_to_expire(struct obd_capa *oc)
146 return cfs_time_before(cfs_time_sub(oc->c_expiry,
147 cfs_time_seconds(oc->c_capa.lc_timeout)*2/3),
151 static struct obd_capa *find_capa(struct lustre_capa *capa,
152 cfs_hlist_head_t *head, int alive)
154 cfs_hlist_node_t *pos;
155 struct obd_capa *ocapa;
156 int len = alive ? offsetof(struct lustre_capa, lc_keyid):sizeof(*capa);
158 cfs_hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
159 if (memcmp(&ocapa->c_capa, capa, len))
161 /* don't return one that will expire soon in this case */
162 if (alive && capa_is_to_expire(ocapa))
165 LASSERT(capa_on_server(ocapa));
167 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
174 #define LRU_CAPA_DELETE_COUNT 12
175 static inline void capa_delete_lru(cfs_list_t *head)
177 struct obd_capa *ocapa;
178 cfs_list_t *node = head->next;
181 /* free LRU_CAPA_DELETE_COUNT unused capa from head */
182 while (count++ < LRU_CAPA_DELETE_COUNT) {
183 ocapa = cfs_list_entry(node, struct obd_capa, c_list);
185 if (cfs_atomic_read(&ocapa->c_refc))
188 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free lru");
194 struct obd_capa *capa_add(cfs_hlist_head_t *hash, struct lustre_capa *capa)
196 cfs_hlist_head_t *head = hash + capa_hashfn(&capa->lc_fid);
197 struct obd_capa *ocapa, *old = NULL;
198 cfs_list_t *list = &capa_list[CAPA_SITE_SERVER];
200 ocapa = alloc_capa(CAPA_SITE_SERVER);
204 spin_lock(&capa_lock);
205 old = find_capa(capa, head, 0);
207 ocapa->c_capa = *capa;
208 set_capa_expiry(ocapa);
209 cfs_hlist_add_head(&ocapa->u.tgt.c_hash, head);
210 cfs_list_add_tail(&ocapa->c_list, list);
212 capa_count[CAPA_SITE_SERVER]++;
213 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
214 capa_delete_lru(list);
215 spin_unlock(&capa_lock);
219 spin_unlock(&capa_lock);
224 EXPORT_SYMBOL(capa_add);
226 struct obd_capa *capa_lookup(cfs_hlist_head_t *hash, struct lustre_capa *capa,
229 struct obd_capa *ocapa;
231 spin_lock(&capa_lock);
232 ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
234 cfs_list_move_tail(&ocapa->c_list,
235 &capa_list[CAPA_SITE_SERVER]);
238 spin_unlock(&capa_lock);
242 EXPORT_SYMBOL(capa_lookup);
244 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
246 struct ll_crypto_hash *tfm;
247 struct capa_hmac_alg *alg;
249 struct scatterlist sl;
251 if (capa_alg(capa) != CAPA_HMAC_ALG_SHA1) {
252 CERROR("unknown capability hmac algorithm!\n");
256 alg = &capa_hmac_algs[capa_alg(capa)];
258 tfm = ll_crypto_alloc_hash(alg->ha_name, 0, 0);
260 CERROR("crypto_alloc_tfm failed, check whether your kernel"
261 "has crypto support!\n");
264 keylen = alg->ha_keylen;
266 sg_set_page(&sl, virt_to_page(capa),
267 offsetof(struct lustre_capa, lc_hmac),
268 (unsigned long)(capa) % CFS_PAGE_SIZE);
270 ll_crypto_hmac(tfm, key, &keylen, &sl, sl.length, hmac);
271 ll_crypto_free_hash(tfm);
275 EXPORT_SYMBOL(capa_hmac);
277 int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
279 struct ll_crypto_cipher *tfm;
280 struct scatterlist sd;
281 struct scatterlist ss;
282 struct blkcipher_desc desc;
285 char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
288 /* passing "aes" in a variable instead of a constant string keeps gcc
290 tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
292 CERROR("failed to load transform for aes\n");
293 RETURN(PTR_ERR(tfm));
296 min = ll_crypto_tfm_alg_min_keysize(tfm);
298 CERROR("keylen at least %d bits for aes\n", min * 8);
299 GOTO(out, rc = -EINVAL);
302 rc = ll_crypto_blkcipher_setkey(tfm, key, min);
304 CERROR("failed to setting key for aes\n");
308 sg_set_page(&sd, virt_to_page(d), 16,
309 (unsigned long)(d) % CFS_PAGE_SIZE);
311 sg_set_page(&ss, virt_to_page(s), 16,
312 (unsigned long)(s) % CFS_PAGE_SIZE);
316 rc = ll_crypto_blkcipher_encrypt(&desc, &sd, &ss, 16);
318 CERROR("failed to encrypt for aes\n");
325 ll_crypto_free_blkcipher(tfm);
328 EXPORT_SYMBOL(capa_encrypt_id);
330 int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
332 struct ll_crypto_cipher *tfm;
333 struct scatterlist sd;
334 struct scatterlist ss;
335 struct blkcipher_desc desc;
338 char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
341 /* passing "aes" in a variable instead of a constant string keeps gcc
343 tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
345 CERROR("failed to load transform for aes\n");
346 RETURN(PTR_ERR(tfm));
349 min = ll_crypto_tfm_alg_min_keysize(tfm);
351 CERROR("keylen at least %d bits for aes\n", min * 8);
352 GOTO(out, rc = -EINVAL);
355 rc = ll_crypto_blkcipher_setkey(tfm, key, min);
357 CERROR("failed to setting key for aes\n");
361 sg_set_page(&sd, virt_to_page(d), 16,
362 (unsigned long)(d) % CFS_PAGE_SIZE);
364 sg_set_page(&ss, virt_to_page(s), 16,
365 (unsigned long)(s) % CFS_PAGE_SIZE);
370 rc = ll_crypto_blkcipher_decrypt(&desc, &sd, &ss, 16);
372 CERROR("failed to decrypt for aes\n");
379 ll_crypto_free_blkcipher(tfm);
382 EXPORT_SYMBOL(capa_decrypt_id);
385 void capa_cpy(void *capa, struct obd_capa *ocapa)
387 spin_lock(&ocapa->c_lock);
388 *(struct lustre_capa *)capa = ocapa->c_capa;
389 spin_unlock(&ocapa->c_lock);
391 EXPORT_SYMBOL(capa_cpy);
393 void _debug_capa(struct lustre_capa *c,
394 struct libcfs_debug_msg_data *msgdata,
395 const char *fmt, ... )
399 libcfs_debug_vmsg2(msgdata, fmt, args,
400 " capability@%p fid "DFID" opc "LPX64" uid "LPU64
401 " gid "LPU64" flags %u alg %d keyid %u timeout %u "
402 "expiry %u\n", c, PFID(capa_fid(c)), capa_opc(c),
403 capa_uid(c), capa_gid(c), capa_flags(c),
404 capa_alg(c), capa_keyid(c), capa_timeout(c),
408 EXPORT_SYMBOL(_debug_capa);