Whamcloud - gitweb
b=13201
[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 #define NR_CAPAHASH 32
50 #define CAPA_HASH_SIZE 3000              /* for MDS & OSS */
51
52 cfs_mem_cache_t *capa_cachep = NULL;
53
54 #ifdef __KERNEL__
55 /* lock for capa hash/capa_list/fo_capa_keys */
56 spinlock_t capa_lock = SPIN_LOCK_UNLOCKED;
57
58 struct list_head capa_list[CAPA_SITE_MAX];
59
60 static struct capa_hmac_alg capa_hmac_algs[] = {
61         DEF_CAPA_HMAC_ALG("sha1", SHA1, 20, 20),
62 };
63 #endif
64 /* capa count */
65 int capa_count[CAPA_SITE_MAX] = { 0, };
66
67 EXPORT_SYMBOL(capa_cachep);
68 EXPORT_SYMBOL(capa_list);
69 EXPORT_SYMBOL(capa_lock);
70 EXPORT_SYMBOL(capa_count);
71
72 struct hlist_head *init_capa_hash(void)
73 {
74         struct hlist_head *hash;
75         int nr_hash, i;
76
77         OBD_ALLOC(hash, PAGE_SIZE);
78         if (!hash)
79                 return NULL;
80
81         nr_hash = PAGE_SIZE / sizeof(struct hlist_head);
82         LASSERT(nr_hash > NR_CAPAHASH);
83
84         for (i = 0; i < NR_CAPAHASH; i++)
85                 INIT_HLIST_HEAD(hash + i);
86         return hash;
87 }
88
89 #ifdef __KERNEL__
90 static inline int capa_on_server(struct obd_capa *ocapa)
91 {
92         return ocapa->c_site == CAPA_SITE_SERVER;
93 }
94
95 static inline void capa_delete(struct obd_capa *ocapa)
96 {
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]--;
101         free_capa(ocapa);
102 }
103
104 void cleanup_capa_hash(struct hlist_head *hash)
105 {
106         int i;
107         struct hlist_node *pos, *next;
108         struct obd_capa *oc;
109
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)
113                         capa_delete(oc);
114         }
115         spin_unlock(&capa_lock);
116
117         OBD_FREE(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 /* 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)
129 {
130         return cfs_time_before(cfs_time_sub(oc->c_expiry,
131                                    cfs_time_seconds(oc->c_capa.lc_timeout)*2/3),
132                                cfs_time_current());
133 }
134
135 static struct obd_capa *find_capa(struct lustre_capa *capa,
136                                   struct hlist_head *head, int alive)
137 {
138         struct hlist_node *pos;
139         struct obd_capa *ocapa;
140         int len = alive ? offsetof(struct lustre_capa, lc_keyid):sizeof(*capa);
141
142         hlist_for_each_entry(ocapa, pos, head, u.tgt.c_hash) {
143                 if (memcmp(&ocapa->c_capa, capa, len))
144                         continue;
145                 /* don't return one that will expire soon in this case */
146                 if (alive && capa_is_to_expire(ocapa))
147                         continue;
148
149                 LASSERT(capa_on_server(ocapa));
150
151                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found");
152                 return ocapa;
153         }
154
155         return NULL;
156 }
157
158 #define LRU_CAPA_DELETE_COUNT 12
159 static inline void capa_delete_lru(struct list_head *head)
160 {
161         struct obd_capa *ocapa;
162         struct list_head *node = head->next;
163         int count = 0;
164
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);
168                 node = node->next;
169                 if (atomic_read(&ocapa->c_refc))
170                         continue;
171
172                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free lru");
173                 capa_delete(ocapa);
174         }
175 }
176
177 /* add or update */
178 struct obd_capa *capa_add(struct hlist_head *hash, struct lustre_capa *capa)
179 {
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];
183
184         ocapa = alloc_capa(CAPA_SITE_SERVER);
185         if (!ocapa)
186                 return NULL;
187
188         spin_lock(&capa_lock);
189         old = find_capa(capa, head, 0);
190         if (!old) {
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]++;
196                 capa_get(ocapa);
197
198                 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
199                         capa_delete_lru(list);
200
201                 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "new");
202                                         
203                 spin_unlock(&capa_lock);
204                 return ocapa;
205         }
206
207         capa_get(old);
208         spin_unlock(&capa_lock);
209
210         DEBUG_CAPA(D_SEC, &old->c_capa, "update");
211
212         free_capa(ocapa);
213         return old;
214 }
215
216 struct obd_capa *capa_lookup(struct hlist_head *hash, struct lustre_capa *capa,
217                              int alive)
218 {
219         struct obd_capa *ocapa;
220
221         spin_lock(&capa_lock);
222         ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
223         if (ocapa) {
224                 list_move_tail(&ocapa->c_list, &capa_list[CAPA_SITE_SERVER]);
225                 capa_get(ocapa);
226         }
227         spin_unlock(&capa_lock);
228
229         return ocapa;
230 }
231
232 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
233 {
234         struct crypto_tfm *tfm;
235         struct capa_hmac_alg *alg;
236         int keylen;
237         struct scatterlist sl = {
238                 .page   = virt_to_page(capa),
239                 .offset = (unsigned long)(capa) % PAGE_SIZE,
240                 .length = offsetof(struct lustre_capa, lc_hmac),
241         };
242
243         if (capa_alg(capa) != CAPA_HMAC_ALG_SHA1) {
244                 CERROR("unknown capability hmac algorithm!\n");
245                 return -EFAULT;
246         }
247
248         alg = &capa_hmac_algs[capa_alg(capa)];
249
250         tfm = crypto_alloc_tfm(alg->ha_name, 0);
251         if (!tfm) {
252                 CERROR("crypto_alloc_tfm failed, check whether your kernel"
253                        "has crypto support!\n");
254                 return -ENOMEM;
255         }
256         keylen = alg->ha_keylen;
257
258         crypto_hmac(tfm, key, &keylen, &sl, 1, hmac);
259         crypto_free_tfm(tfm);
260
261         return 0;
262 }
263 #endif
264
265 void capa_cpy(void *capa, struct obd_capa *ocapa)
266 {
267         spin_lock(&ocapa->c_lock);
268         *(struct lustre_capa *)capa = ocapa->c_capa;
269         spin_unlock(&ocapa->c_lock);
270 }
271
272 char *dump_capa_content(char *buf, char *key, int len)
273 {
274         int i, n = 0;
275
276         for (i = 0; i < len; i++)
277                 n += sprintf(buf + n, "%02x", (unsigned char) key[i]);
278         return buf;
279 }
280
281 EXPORT_SYMBOL(init_capa_hash);
282 EXPORT_SYMBOL(cleanup_capa_hash);
283
284 EXPORT_SYMBOL(capa_add);
285 EXPORT_SYMBOL(capa_lookup);
286
287 EXPORT_SYMBOL(capa_hmac);
288 EXPORT_SYMBOL(capa_cpy);
289
290 EXPORT_SYMBOL(dump_capa_content);