1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/capa.c
38 * Lustre Capability Hash Management
40 * Author: Lai Siyao<lsy@clusterfs.com>
44 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_SEC
50 #include <linux/version.h>
52 #include <asm/unistd.h>
53 #include <linux/slab.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
57 #include <obd_class.h>
58 #include <lustre_debug.h>
59 #include <lustre/lustre_idl.h>
61 #include <liblustre.h>
64 #include <libcfs/list.h>
65 #include <lustre_capa.h>
67 #define NR_CAPAHASH 32
68 #define CAPA_HASH_SIZE 3000 /* for MDS & OSS */
70 cfs_mem_cache_t *capa_cachep = NULL;
73 /* lock for capa hash/capa_list/fo_capa_keys */
74 cfs_spinlock_t capa_lock = CFS_SPIN_LOCK_UNLOCKED;
76 cfs_list_t capa_list[CAPA_SITE_MAX];
78 static struct capa_hmac_alg capa_hmac_algs[] = {
79 DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
83 int capa_count[CAPA_SITE_MAX] = { 0, };
85 EXPORT_SYMBOL(capa_cachep);
86 EXPORT_SYMBOL(capa_list);
87 EXPORT_SYMBOL(capa_lock);
88 EXPORT_SYMBOL(capa_count);
90 cfs_hlist_head_t *init_capa_hash(void)
92 cfs_hlist_head_t *hash;
95 OBD_ALLOC(hash, CFS_PAGE_SIZE);
99 nr_hash = CFS_PAGE_SIZE / sizeof(cfs_hlist_head_t);
100 LASSERT(nr_hash > NR_CAPAHASH);
102 for (i = 0; i < NR_CAPAHASH; i++)
103 CFS_INIT_HLIST_HEAD(hash + i);
108 static inline int capa_on_server(struct obd_capa *ocapa)
110 return ocapa->c_site == CAPA_SITE_SERVER;
113 static inline void capa_delete(struct obd_capa *ocapa)
115 LASSERT(capa_on_server(ocapa));
116 cfs_hlist_del_init(&ocapa->u.tgt.c_hash);
117 cfs_list_del_init(&ocapa->c_list);
118 capa_count[ocapa->c_site]--;
119 /* release the ref when alloc */
123 void cleanup_capa_hash(cfs_hlist_head_t *hash)
126 cfs_hlist_node_t *pos, *next;
129 cfs_spin_lock(&capa_lock);
130 for (i = 0; i < NR_CAPAHASH; i++) {
131 cfs_hlist_for_each_entry_safe(oc, pos, next, hash + i,
135 cfs_spin_unlock(&capa_lock);
137 OBD_FREE(hash, CFS_PAGE_SIZE);
140 static inline int capa_hashfn(struct lu_fid *fid)
142 return (fid_oid(fid) ^ fid_ver(fid)) *
143 (unsigned long)(fid_seq(fid) + 1) % NR_CAPAHASH;
146 /* capa renewal time check is earlier than that on client, which is to prevent
147 * client renew right after obtaining it. */
148 static inline int capa_is_to_expire(struct obd_capa *oc)
150 return cfs_time_before(cfs_time_sub(oc->c_expiry,
151 cfs_time_seconds(oc->c_capa.lc_timeout)*2/3),
155 static struct obd_capa *find_capa(struct lustre_capa *capa,
156 cfs_hlist_head_t *head, int alive)
158 cfs_hlist_node_t *pos;
159 struct obd_capa *ocapa;
160 int len = alive ? offsetof(struct lustre_capa, lc_keyid):sizeof(*capa);
162 cfs_hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
163 if (memcmp(&ocapa->c_capa, capa, len))
165 /* don't return one that will expire soon in this case */
166 if (alive && capa_is_to_expire(ocapa))
169 LASSERT(capa_on_server(ocapa));
171 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
178 #define LRU_CAPA_DELETE_COUNT 12
179 static inline void capa_delete_lru(cfs_list_t *head)
181 struct obd_capa *ocapa;
182 cfs_list_t *node = head->next;
185 /* free LRU_CAPA_DELETE_COUNT unused capa from head */
186 while (count++ < LRU_CAPA_DELETE_COUNT) {
187 ocapa = cfs_list_entry(node, struct obd_capa, c_list);
189 if (cfs_atomic_read(&ocapa->c_refc))
192 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free lru");
198 struct obd_capa *capa_add(cfs_hlist_head_t *hash, struct lustre_capa *capa)
200 cfs_hlist_head_t *head = hash + capa_hashfn(&capa->lc_fid);
201 struct obd_capa *ocapa, *old = NULL;
202 cfs_list_t *list = &capa_list[CAPA_SITE_SERVER];
204 ocapa = alloc_capa(CAPA_SITE_SERVER);
208 cfs_spin_lock(&capa_lock);
209 old = find_capa(capa, head, 0);
211 ocapa->c_capa = *capa;
212 set_capa_expiry(ocapa);
213 cfs_hlist_add_head(&ocapa->u.tgt.c_hash, head);
214 cfs_list_add_tail(&ocapa->c_list, list);
216 capa_count[CAPA_SITE_SERVER]++;
217 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
218 capa_delete_lru(list);
219 cfs_spin_unlock(&capa_lock);
223 cfs_spin_unlock(&capa_lock);
229 struct obd_capa *capa_lookup(cfs_hlist_head_t *hash, struct lustre_capa *capa,
232 struct obd_capa *ocapa;
234 cfs_spin_lock(&capa_lock);
235 ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
237 cfs_list_move_tail(&ocapa->c_list,
238 &capa_list[CAPA_SITE_SERVER]);
241 cfs_spin_unlock(&capa_lock);
246 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
248 struct ll_crypto_hash *tfm;
249 struct capa_hmac_alg *alg;
251 struct scatterlist sl;
253 if (capa_alg(capa) != CAPA_HMAC_ALG_SHA1) {
254 CERROR("unknown capability hmac algorithm!\n");
258 alg = &capa_hmac_algs[capa_alg(capa)];
260 tfm = ll_crypto_alloc_hash(alg->ha_name, 0, 0);
262 CERROR("crypto_alloc_tfm failed, check whether your kernel"
263 "has crypto support!\n");
266 keylen = alg->ha_keylen;
268 sg_set_page(&sl, virt_to_page(capa),
269 offsetof(struct lustre_capa, lc_hmac),
270 (unsigned long)(capa) % CFS_PAGE_SIZE);
272 ll_crypto_hmac(tfm, key, &keylen, &sl, sl.length, hmac);
273 ll_crypto_free_hash(tfm);
278 int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
280 struct ll_crypto_cipher *tfm;
281 struct scatterlist sd;
282 struct scatterlist ss;
283 struct blkcipher_desc desc;
286 char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
289 /* passing "aes" in a variable instead of a constant string keeps gcc
291 tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
293 CERROR("failed to load transform for aes\n");
297 min = ll_crypto_tfm_alg_min_keysize(tfm);
299 CERROR("keylen at least %d bits for aes\n", min * 8);
300 GOTO(out, rc = -EINVAL);
303 rc = ll_crypto_blkcipher_setkey(tfm, key, min);
305 CERROR("failed to setting key for aes\n");
309 sg_set_page(&sd, virt_to_page(d), 16,
310 (unsigned long)(d) % CFS_PAGE_SIZE);
312 sg_set_page(&ss, virt_to_page(s), 16,
313 (unsigned long)(s) % CFS_PAGE_SIZE);
317 rc = ll_crypto_blkcipher_encrypt(&desc, &sd, &ss, 16);
319 CERROR("failed to encrypt for aes\n");
326 ll_crypto_free_blkcipher(tfm);
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");
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);
384 void capa_cpy(void *capa, struct obd_capa *ocapa)
386 cfs_spin_lock(&ocapa->c_lock);
387 *(struct lustre_capa *)capa = ocapa->c_capa;
388 cfs_spin_unlock(&ocapa->c_lock);
391 void _debug_capa(struct lustre_capa *c,
392 struct libcfs_debug_msg_data *msgdata,
393 const char *fmt, ... )
397 libcfs_debug_vmsg2(msgdata, fmt, args,
398 " capability@%p fid "DFID" opc "LPX64" uid "LPU64
399 " gid "LPU64" flags %u alg %d keyid %u timeout %u "
400 "expiry %u\n", c, PFID(capa_fid(c)), capa_opc(c),
401 capa_uid(c), capa_gid(c), capa_flags(c),
402 capa_alg(c), capa_keyid(c), capa_timeout(c),
406 EXPORT_SYMBOL(_debug_capa);
408 EXPORT_SYMBOL(init_capa_hash);
409 EXPORT_SYMBOL(cleanup_capa_hash);
410 EXPORT_SYMBOL(capa_add);
411 EXPORT_SYMBOL(capa_lookup);
412 EXPORT_SYMBOL(capa_hmac);
413 EXPORT_SYMBOL(capa_encrypt_id);
414 EXPORT_SYMBOL(capa_decrypt_id);
415 EXPORT_SYMBOL(capa_cpy);