Whamcloud - gitweb
lu: make site hash table dynamically adjustable.
[fs/lustre-release.git] / lustre / obdclass / capa.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/obdclass/capa.c
5  *  Lustre Capability Hash Management
6  *
7  *  Copyright (c) 2005 Cluster File Systems, Inc.
8  *   Author: Lai Siyao<lsy@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
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.
15  *
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.
20  *
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.
24  */
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28
29 #define DEBUG_SUBSYSTEM S_SEC
30
31 #ifdef __KERNEL__
32 #include <linux/version.h>
33 #include <linux/fs.h>
34 #include <asm/unistd.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38
39 #include <obd_class.h>
40 #include <lustre_debug.h>
41 #include <lustre/lustre_idl.h>
42 #else
43 #include <liblustre.h>
44 #endif
45
46 #include <libcfs/list.h>
47 #include <lustre_capa.h>
48
49 cfs_mem_cache_t *capa_cachep = NULL;
50
51 #ifdef __KERNEL__
52 struct list_head capa_list[CAPA_SITE_MAX];
53 spinlock_t capa_lock = SPIN_LOCK_UNLOCKED; /* lock for capa_hash/capa_list */
54
55 static struct hlist_head *capa_hash;
56 #endif
57 /* capa count */
58 int capa_count[CAPA_SITE_MAX] = { 0, };
59
60 static struct capa_hmac_alg capa_hmac_algs[] = {
61         DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
62 };
63
64 static const char *capa_site_name[] = {
65         [CAPA_SITE_CLIENT] = "client",
66         [CAPA_SITE_SERVER] = "server",
67         [CAPA_SITE_MAX]    = "error"
68 };
69
70 EXPORT_SYMBOL(capa_cachep);
71 EXPORT_SYMBOL(capa_list);
72 EXPORT_SYMBOL(capa_lock);
73 EXPORT_SYMBOL(capa_count);
74
75 int init_capa_hash(void)
76 {
77 #ifdef __KERNEL__
78         int nr_hash, i;
79
80         OBD_ALLOC(capa_hash, PAGE_SIZE);
81         if (!capa_hash)
82                 return -ENOMEM;
83
84         nr_hash = PAGE_SIZE / sizeof(struct hlist_head);
85         LASSERT(nr_hash > NR_CAPAHASH);
86
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]);
91 #endif
92         return 0;
93 }
94
95 #ifdef __KERNEL__
96 void cleanup_capa_hash(void)
97 {
98         int i;
99         struct hlist_node *pos;
100         struct obd_capa *oc;
101
102         for (i = 0; i < NR_CAPAHASH; i++) {
103                 if (hlist_empty(capa_hash + i))
104                         continue;
105                 hlist_for_each_entry(oc, pos, capa_hash + i, u.tgt.c_hash)
106                         DEBUG_CAPA(D_ERROR, &oc->c_capa, "remaining cached");
107                 LBUG();
108         }
109         for (i = CAPA_SITE_MAX; i < CAPA_SITE_MAX; i++) {
110                 if (list_empty(&capa_list[i]))
111                         continue;
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]);
115                 LBUG();
116         }
117         OBD_FREE(capa_hash, PAGE_SIZE);
118 }
119
120 static inline int const capa_hashfn(struct lu_fid *fid)
121 {
122         return (fid_oid(fid) ^ fid_ver(fid)) *
123                (unsigned long)(fid_seq(fid) + 1) % NR_CAPAHASH;
124 }
125
126 static inline int capa_on_server(struct obd_capa *ocapa)
127 {
128         return ocapa->c_site == CAPA_SITE_SERVER;
129 }
130
131 static struct obd_capa *find_capa(struct lustre_capa *capa,
132                                   struct hlist_head *head)
133 {
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);
138
139         hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
140                 if (memcmp(&ocapa->c_capa, capa, len))
141                         continue;
142                 /* don't return an expired one in this case */
143                 if (capa->lc_expiry == 0 && capa_is_to_expire(ocapa))
144                         continue;
145
146                 LASSERT(capa_on_server(ocapa));
147
148                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
149                 return ocapa;
150         }
151
152         return NULL;
153 }
154
155 static inline void capa_delete(struct obd_capa *ocapa)
156 {
157         LASSERT(capa_on_server(ocapa));
158         hlist_del(&ocapa->u.tgt.c_hash);
159         list_del(&ocapa->c_list);
160         free_capa(ocapa);
161 }
162
163 static inline void free_capa_lru(struct list_head *head)
164 {
165         struct list_head *node = head->next;
166         struct obd_capa *ocapa;
167         int count = 0;
168
169         /* free 12 unused capa from head */
170         while (node != head && count < 12) {
171                 ocapa = list_entry(node, struct obd_capa, c_list);
172                 node = node->next;
173
174                 LASSERT(capa_on_server(ocapa));
175                 if (atomic_read(&ocapa->c_refc))
176                         continue;
177
178                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free unused");
179                 capa_delete(ocapa);
180                 count++;
181         }
182 }
183
184 /* add or update */
185 struct obd_capa *capa_add(struct lustre_capa *capa)
186 {
187         struct hlist_head *head = capa_hash + capa_hashfn(&capa->lc_fid);
188         struct obd_capa *ocapa, *old = NULL;
189
190         ocapa = alloc_capa(CAPA_SITE_SERVER);
191         if (!ocapa)
192                 return NULL;
193
194         spin_lock(&capa_lock);
195
196         old = find_capa(capa, head);
197         if (!old) {
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]);
202                 capa_get(ocapa);
203
204                 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
205                         free_capa_lru(&capa_list[CAPA_SITE_SERVER]);
206
207                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "new");
208                                         
209                 spin_unlock(&capa_lock);
210                 return ocapa;
211         }
212
213         list_move_tail(&old->c_list, &capa_list[CAPA_SITE_SERVER]);
214         capa_get(old);
215
216         spin_unlock(&capa_lock);
217
218         DEBUG_CAPA(D_SEC, &old->c_capa, "update");
219
220         free_capa(ocapa);
221         return old;
222 }
223
224 struct obd_capa *capa_lookup(struct lustre_capa *capa)
225 {
226         struct hlist_head *head;
227         struct obd_capa *ocapa;
228
229         head = capa_hash + capa_hashfn(&capa->lc_fid);
230
231         spin_lock(&capa_lock);
232         ocapa = find_capa(capa, head);
233         if (ocapa)
234                 capa_get(ocapa);
235         spin_unlock(&capa_lock);
236
237         return ocapa;
238 }
239
240 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
241 {
242         struct crypto_tfm *tfm;
243         struct capa_hmac_alg *alg;
244         int keylen;
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),
249         };
250
251         if (capa_alg(capa) != CAPA_HMAC_ALG_SHA1) {
252                 CERROR("unknown capability hmac algorithm!\n");
253                 return -EFAULT;
254         }
255
256         alg = &capa_hmac_algs[capa_alg(capa)];
257
258         tfm = crypto_alloc_tfm(alg->ha_name, 0);
259         if (!tfm) {
260                 CERROR("crypto_alloc_tfm failed, check whether your kernel"
261                        "has crypto support!\n");
262                 return -ENOMEM;
263         }
264         keylen = alg->ha_keylen;
265
266         crypto_hmac(tfm, key, &keylen, &sl, 1, hmac);
267         crypto_free_tfm(tfm);
268
269         return 0;
270 }
271
272 void cleanup_capas(int site)
273 {
274         struct obd_capa *ocapa, *tmp;
275
276         spin_lock(&capa_lock);
277         list_for_each_entry_safe(ocapa, tmp, &capa_list[site], c_list)
278                 if (site == ocapa->c_site)
279                         capa_delete(ocapa);
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]);
283 }
284 #endif
285
286 void capa_cpy(void *capa, struct obd_capa *ocapa)
287 {
288         spin_lock(&ocapa->c_lock);
289         *(struct lustre_capa *)capa = ocapa->c_capa;
290         spin_unlock(&ocapa->c_lock);
291 }
292
293 void dump_capa_hmac(char *buf, char *key)
294 {
295         int i, n = 0;
296
297         for (i = 0; i < CAPA_HMAC_MAX_LEN; i++)
298                 n += sprintf(buf + n, "%02x", (unsigned char) key[i]);
299 }
300
301 EXPORT_SYMBOL(capa_add);
302 EXPORT_SYMBOL(capa_lookup);
303
304 EXPORT_SYMBOL(capa_hmac);
305 EXPORT_SYMBOL(capa_cpy);
306
307 EXPORT_SYMBOL(cleanup_capas);
308 EXPORT_SYMBOL(dump_capa_hmac);