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 cfs_mem_cache_t *capa_cachep = NULL;
52 struct list_head capa_list[CAPA_SITE_MAX];
53 spinlock_t capa_lock = SPIN_LOCK_UNLOCKED; /* lock for capa_hash/capa_list */
55 static struct hlist_head *capa_hash;
58 int capa_count[CAPA_SITE_MAX] = { 0, };
60 static struct capa_hmac_alg capa_hmac_algs[] = {
61 DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
64 static const char *capa_site_name[] = {
65 [CAPA_SITE_CLIENT] = "client",
66 [CAPA_SITE_SERVER] = "server",
67 [CAPA_SITE_MAX] = "error"
70 EXPORT_SYMBOL(capa_cachep);
71 EXPORT_SYMBOL(capa_list);
72 EXPORT_SYMBOL(capa_lock);
73 EXPORT_SYMBOL(capa_count);
75 int init_capa_hash(void)
80 OBD_ALLOC(capa_hash, PAGE_SIZE);
84 nr_hash = PAGE_SIZE / sizeof(struct hlist_head);
85 LASSERT(nr_hash > NR_CAPAHASH);
87 for (i = 0; i < NR_CAPAHASH; i++)
88 INIT_HLIST_HEAD(capa_hash + i);
89 for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
90 INIT_LIST_HEAD(&capa_list[i]);
96 void cleanup_capa_hash(void)
99 struct hlist_node *pos;
102 for (i = 0; i < NR_CAPAHASH; i++) {
103 if (hlist_empty(capa_hash + i))
105 hlist_for_each_entry(oc, pos, capa_hash + i, u.tgt.c_hash)
106 DEBUG_CAPA(D_ERROR, &oc->c_capa, "remaining cached");
109 for (i = CAPA_SITE_MAX; i < CAPA_SITE_MAX; i++) {
110 if (list_empty(&capa_list[i]))
112 list_for_each_entry(oc, &capa_list[i], c_list)
113 DEBUG_CAPA(D_ERROR, &oc->c_capa, "remaining %s",
114 capa_site_name[oc->c_site]);
117 OBD_FREE(capa_hash, 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 static inline int capa_on_server(struct obd_capa *ocapa)
128 return ocapa->c_site == CAPA_SITE_SERVER;
131 static struct obd_capa *find_capa(struct lustre_capa *capa,
132 struct hlist_head *head)
134 struct hlist_node *pos;
135 struct obd_capa *ocapa;
136 int len = capa->lc_expiry ? sizeof(*capa) :
137 offsetof(struct lustre_capa, lc_keyid);
139 hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
140 if (memcmp(&ocapa->c_capa, capa, len))
142 /* don't return an expired one in this case */
143 if (capa->lc_expiry == 0 && capa_is_to_expire(ocapa))
146 LASSERT(capa_on_server(ocapa));
148 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
155 static inline void capa_delete(struct obd_capa *ocapa)
157 LASSERT(capa_on_server(ocapa));
158 hlist_del(&ocapa->u.tgt.c_hash);
159 list_del(&ocapa->c_list);
163 static inline void free_capa_lru(struct list_head *head)
165 struct list_head *node = head->next;
166 struct obd_capa *ocapa;
169 /* free 12 unused capa from head */
170 while (node != head && count < 12) {
171 ocapa = list_entry(node, struct obd_capa, c_list);
174 LASSERT(capa_on_server(ocapa));
175 if (atomic_read(&ocapa->c_refc))
178 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free unused");
185 struct obd_capa *capa_add(struct lustre_capa *capa)
187 struct hlist_head *head = capa_hash + capa_hashfn(&capa->lc_fid);
188 struct obd_capa *ocapa, *old = NULL;
190 ocapa = alloc_capa(CAPA_SITE_SERVER);
194 spin_lock(&capa_lock);
196 old = find_capa(capa, head);
198 ocapa->c_capa = *capa;
199 set_capa_expiry(ocapa);
200 hlist_add_head(&ocapa->u.tgt.c_hash, head);
201 list_add_tail(&ocapa->c_list, &capa_list[CAPA_SITE_SERVER]);
204 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
205 free_capa_lru(&capa_list[CAPA_SITE_SERVER]);
207 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "new");
209 spin_unlock(&capa_lock);
213 list_move_tail(&old->c_list, &capa_list[CAPA_SITE_SERVER]);
216 spin_unlock(&capa_lock);
218 DEBUG_CAPA(D_SEC, &old->c_capa, "update");
224 struct obd_capa *capa_lookup(struct lustre_capa *capa)
226 struct hlist_head *head;
227 struct obd_capa *ocapa;
229 head = capa_hash + capa_hashfn(&capa->lc_fid);
231 spin_lock(&capa_lock);
232 ocapa = find_capa(capa, head);
235 spin_unlock(&capa_lock);
240 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
242 struct crypto_tfm *tfm;
243 struct capa_hmac_alg *alg;
245 struct scatterlist sl = {
246 .page = virt_to_page(capa),
247 .offset = (unsigned long)(capa) % PAGE_SIZE,
248 .length = offsetof(struct lustre_capa, lc_hmac),
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 = crypto_alloc_tfm(alg->ha_name, 0);
260 CERROR("crypto_alloc_tfm failed, check whether your kernel"
261 "has crypto support!\n");
264 keylen = alg->ha_keylen;
266 crypto_hmac(tfm, key, &keylen, &sl, 1, hmac);
267 crypto_free_tfm(tfm);
272 void cleanup_capas(int site)
274 struct obd_capa *ocapa, *tmp;
276 spin_lock(&capa_lock);
277 list_for_each_entry_safe(ocapa, tmp, &capa_list[site], c_list)
278 if (site == ocapa->c_site)
280 spin_unlock(&capa_lock);
281 LASSERTF(capa_count[site] == 0, "%s capability count is %d\n",
282 capa_site_name[site], capa_count[site]);
286 void capa_cpy(void *capa, struct obd_capa *ocapa)
288 spin_lock(&ocapa->c_lock);
289 *(struct lustre_capa *)capa = ocapa->c_capa;
290 spin_unlock(&ocapa->c_lock);
293 void dump_capa_hmac(char *buf, char *key)
297 for (i = 0; i < CAPA_HMAC_MAX_LEN; i++)
298 n += sprintf(buf + n, "%02x", (unsigned char) key[i]);
301 EXPORT_SYMBOL(capa_add);
302 EXPORT_SYMBOL(capa_lookup);
304 EXPORT_SYMBOL(capa_hmac);
305 EXPORT_SYMBOL(capa_cpy);
307 EXPORT_SYMBOL(cleanup_capas);
308 EXPORT_SYMBOL(dump_capa_hmac);