1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/obdclass/capa.c
5 * Lustre Capability Hash Management
7 * Copyright (c) 2005 Cluster File Systems, Inc.
8 * Author: Lai Siyao<lsy@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 # define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_SEC
32 #include <linux/version.h>
34 #include <asm/unistd.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
39 #include <obd_class.h>
40 #include <lustre_debug.h>
41 #include <lustre/lustre_idl.h>
43 #include <liblustre.h>
46 #include <libcfs/list.h>
47 #include <lustre_capa.h>
49 #define NR_CAPAHASH 32
50 #define CAPA_HASH_SIZE 3000 /* for MDS & OSS */
52 cfs_mem_cache_t *capa_cachep = NULL;
55 /* lock for capa hash/capa_list/fo_capa_keys */
56 spinlock_t capa_lock = SPIN_LOCK_UNLOCKED;
58 struct list_head capa_list[CAPA_SITE_MAX];
60 static struct capa_hmac_alg capa_hmac_algs[] = {
61 DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
65 int capa_count[CAPA_SITE_MAX] = { 0, };
67 EXPORT_SYMBOL(capa_cachep);
68 EXPORT_SYMBOL(capa_list);
69 EXPORT_SYMBOL(capa_lock);
70 EXPORT_SYMBOL(capa_count);
72 struct hlist_head *init_capa_hash(void)
74 struct hlist_head *hash;
77 OBD_ALLOC(hash, CFS_PAGE_SIZE);
81 nr_hash = CFS_PAGE_SIZE / sizeof(struct hlist_head);
82 LASSERT(nr_hash > NR_CAPAHASH);
84 for (i = 0; i < NR_CAPAHASH; i++)
85 INIT_HLIST_HEAD(hash + i);
90 static inline int capa_on_server(struct obd_capa *ocapa)
92 return ocapa->c_site == CAPA_SITE_SERVER;
95 static inline void capa_delete(struct obd_capa *ocapa)
97 LASSERT(capa_on_server(ocapa));
98 hlist_del(&ocapa->u.tgt.c_hash);
99 list_del(&ocapa->c_list);
100 capa_count[ocapa->c_site]--;
104 void cleanup_capa_hash(struct hlist_head *hash)
107 struct hlist_node *pos, *next;
110 spin_lock(&capa_lock);
111 for (i = 0; i < NR_CAPAHASH; i++) {
112 hlist_for_each_entry_safe(oc, pos, next, hash + i, u.tgt.c_hash)
115 spin_unlock(&capa_lock);
117 OBD_FREE(hash, CFS_PAGE_SIZE);
120 static inline int const capa_hashfn(struct lu_fid *fid)
122 return (fid_oid(fid) ^ fid_ver(fid)) *
123 (unsigned long)(fid_seq(fid) + 1) % NR_CAPAHASH;
126 /* capa renewal time check is earlier than that on client, which is to prevent
127 * client renew right after obtaining it. */
128 static inline int capa_is_to_expire(struct obd_capa *oc)
130 return cfs_time_before(cfs_time_sub(oc->c_expiry,
131 cfs_time_seconds(oc->c_capa.lc_timeout)*2/3),
135 static struct obd_capa *find_capa(struct lustre_capa *capa,
136 struct hlist_head *head, int alive)
138 struct hlist_node *pos;
139 struct obd_capa *ocapa;
140 int len = alive ? offsetof(struct lustre_capa, lc_keyid):sizeof(*capa);
142 hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
143 if (memcmp(&ocapa->c_capa, capa, len))
145 /* don't return one that will expire soon in this case */
146 if (alive && capa_is_to_expire(ocapa))
149 LASSERT(capa_on_server(ocapa));
151 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
158 #define LRU_CAPA_DELETE_COUNT 12
159 static inline void capa_delete_lru(struct list_head *head)
161 struct obd_capa *ocapa;
162 struct list_head *node = head->next;
165 /* free LRU_CAPA_DELETE_COUNT unused capa from head */
166 while (count++ < LRU_CAPA_DELETE_COUNT) {
167 ocapa = list_entry(node, struct obd_capa, c_list);
169 if (atomic_read(&ocapa->c_refc))
172 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free lru");
178 struct obd_capa *capa_add(struct hlist_head *hash, struct lustre_capa *capa)
180 struct hlist_head *head = hash + capa_hashfn(&capa->lc_fid);
181 struct obd_capa *ocapa, *old = NULL;
182 struct list_head *list = &capa_list[CAPA_SITE_SERVER];
184 ocapa = alloc_capa(CAPA_SITE_SERVER);
188 spin_lock(&capa_lock);
189 old = find_capa(capa, head, 0);
191 ocapa->c_capa = *capa;
192 set_capa_expiry(ocapa);
193 hlist_add_head(&ocapa->u.tgt.c_hash, head);
194 list_add_tail(&ocapa->c_list, list);
195 capa_count[CAPA_SITE_SERVER]++;
198 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
199 capa_delete_lru(list);
201 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "new");
203 spin_unlock(&capa_lock);
208 spin_unlock(&capa_lock);
210 DEBUG_CAPA(D_SEC, &old->c_capa, "update");
216 struct obd_capa *capa_lookup(struct hlist_head *hash, struct lustre_capa *capa,
219 struct obd_capa *ocapa;
221 spin_lock(&capa_lock);
222 ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
224 list_move_tail(&ocapa->c_list, &capa_list[CAPA_SITE_SERVER]);
227 spin_unlock(&capa_lock);
232 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
234 struct ll_crypto_hash *tfm;
235 struct capa_hmac_alg *alg;
237 struct scatterlist sl = {
238 .page = virt_to_page(capa),
239 .offset = (unsigned long)(capa) % CFS_PAGE_SIZE,
240 .length = offsetof(struct lustre_capa, lc_hmac),
243 if (capa_alg(capa) != CAPA_HMAC_ALG_SHA1) {
244 CERROR("unknown capability hmac algorithm!\n");
248 alg = &capa_hmac_algs[capa_alg(capa)];
250 tfm = ll_crypto_alloc_hash(alg->ha_name, 0, 0);
252 CERROR("crypto_alloc_tfm failed, check whether your kernel"
253 "has crypto support!\n");
256 keylen = alg->ha_keylen;
258 ll_crypto_hmac(tfm, key, &keylen, &sl, sl.length, hmac);
259 ll_crypto_free_hash(tfm);
265 void capa_cpy(void *capa, struct obd_capa *ocapa)
267 spin_lock(&ocapa->c_lock);
268 *(struct lustre_capa *)capa = ocapa->c_capa;
269 spin_unlock(&ocapa->c_lock);
272 char *dump_capa_content(char *buf, char *key, int len)
276 for (i = 0; i < len; i++)
277 n += sprintf(buf + n, "%02x", (unsigned char) key[i]);
281 EXPORT_SYMBOL(init_capa_hash);
282 EXPORT_SYMBOL(cleanup_capa_hash);
284 EXPORT_SYMBOL(capa_add);
285 EXPORT_SYMBOL(capa_lookup);
287 EXPORT_SYMBOL(capa_hmac);
288 EXPORT_SYMBOL(capa_cpy);
290 EXPORT_SYMBOL(dump_capa_content);