Whamcloud - gitweb
add debug for capa_key.
[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 Cache Management
6  *
7  *  Copyright (c) 2001-2003 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 <linux/obd_class.h>
40 #include <linux/lustre_debug.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_sec.h>
43 #else
44 #include <liblustre.h>
45 #endif
46
47 #include <libcfs/list.h>
48 #include <linux/lustre_sec.h>
49
50 kmem_cache_t *capa_cachep = NULL;
51
52 /* capa_lock protect capa hash, list and content. */
53 spinlock_t capa_lock = SPIN_LOCK_UNLOCKED;
54 struct hlist_head *capa_hash;
55 struct list_head capa_list[3];
56 static int capa_count[3] = { 0 };
57
58 static char *capa_type_name[] = { "client", "mds", "filter" };
59
60 /* TODO: mdc and llite all need this, so define it here.
61  * in the future it will be moved to ll_sb_info to support multi-
62  * mount point */
63 struct timer_list ll_capa_timer;
64
65 EXPORT_SYMBOL(capa_lock);
66 EXPORT_SYMBOL(capa_hash);
67 EXPORT_SYMBOL(capa_list);
68 EXPORT_SYMBOL(ll_capa_timer);
69
70 static inline int const
71 capa_hashfn(unsigned int uid, __u64 mdsid, unsigned long ino)
72 {
73         return (ino ^ uid) * (unsigned long)(mdsid + 1) % NR_CAPAHASH;
74 }
75
76 int capa_op(int flags)
77 {
78         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
79                 return CAPA_WRITE;
80         else if (flags & FMODE_READ)
81                 return CAPA_READ;
82
83         LBUG(); /* should be either MAY_READ or MAY_WRITE */
84         return 0;
85 }
86
87 static struct obd_capa *
88 find_capa(struct hlist_head *head, uid_t uid, int capa_op, __u64 mdsid,
89           unsigned long ino, __u32 igen, int type)
90 {
91         struct hlist_node *pos;
92         struct obd_capa *ocapa;
93         uid_t ouid;
94
95         CDEBUG(D_INODE, "find capa for (uid %u, op %d, mdsid "LPU64", ino %lu"
96                " igen %u, type %d\n", (unsigned) uid, capa_op, mdsid, ino, igen, type);
97         hlist_for_each_entry(ocapa, pos, head, c_hash) {
98                 if (ocapa->c_capa.lc_ino != ino)
99                         continue;
100                 if (ocapa->c_capa.lc_igen != igen)
101                         continue;
102                 if (ocapa->c_capa.lc_mdsid != mdsid)
103                         continue;
104                 if ((ocapa->c_capa.lc_op & capa_op) != ocapa->c_capa.lc_op)
105                         continue;
106                 if (ocapa->c_type != type)
107                         continue;
108
109                 if (ocapa->c_type == CLIENT_CAPA)
110                         ouid = ocapa->c_capa.lc_ruid;
111                 else
112                         ouid = ocapa->c_capa.lc_uid;
113
114                 if (ouid != uid)
115                         continue;
116
117                 DEBUG_CAPA(D_INODE, &ocapa->c_capa, "found %s",
118                            capa_type_name[ocapa->c_type]);
119
120                 return ocapa;
121         }
122
123         return NULL;
124 }
125
126 inline void __capa_get(struct obd_capa *ocapa)
127 {
128         if (ocapa->c_type != CLIENT_CAPA)
129                 atomic_inc(&ocapa->c_refc);
130 }
131
132 static struct obd_capa *
133 find_capa_locked(struct hlist_head *head, uid_t uid, int capa_op, __u64 mdsid,
134                  unsigned long ino, __u32 igen, int type)
135 {
136         struct obd_capa *ocapa;
137
138         spin_lock(&capa_lock);
139         ocapa = find_capa(head, uid, capa_op, mdsid, ino, igen, type);
140         if (ocapa)
141                 __capa_get(ocapa);
142         spin_unlock(&capa_lock);
143
144         return ocapa;
145 }
146
147 static struct obd_capa *alloc_capa(void)
148 {
149         struct obd_capa *ocapa;
150
151         OBD_SLAB_ALLOC(ocapa, capa_cachep, SLAB_NOFS, sizeof(*ocapa));
152         if (ocapa) {
153                 INIT_HLIST_NODE(&ocapa->c_hash);
154                 INIT_LIST_HEAD(&ocapa->c_list);
155         }
156
157         return ocapa;
158 }
159
160 static void __capa_put(struct obd_capa *ocapa)
161 {
162         hlist_del_init(&ocapa->c_hash);
163         list_del_init(&ocapa->c_list);
164         capa_count[ocapa->c_type]--;
165 }
166
167 static void destroy_capa(struct obd_capa *ocapa)
168 {
169         OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
170 }
171
172 int capa_cache_init(void)
173 {
174         int nr_hash, i;
175
176         OBD_ALLOC(capa_hash, PAGE_SIZE);
177         if (!capa_hash)
178                 return -ENOMEM;
179
180         nr_hash = PAGE_SIZE / sizeof(struct hlist_head);
181         LASSERT(nr_hash > NR_CAPAHASH);
182
183         for (i = 0; i < NR_CAPAHASH; i++)
184                 INIT_HLIST_HEAD(capa_hash + i);
185
186         for (i = 0; i < 3; i++)
187                 INIT_LIST_HEAD(&capa_list[i]);
188
189         return 0;
190 }
191
192 void capa_cache_cleanup(void)
193 {
194         struct obd_capa *ocapa, *tmp;
195         int i;
196
197         for (i = MDS_CAPA; i <= FILTER_CAPA; i++) {
198                 list_for_each_entry_safe(ocapa, tmp, &capa_list[i], c_list) {
199                         __capa_put(ocapa);
200                         destroy_capa(ocapa);
201                 }
202         }
203
204         OBD_FREE(capa_hash, PAGE_SIZE);
205 }
206
207
208 static inline void list_add_capa(struct obd_capa *ocapa, struct list_head *head)
209 {
210         struct obd_capa *tmp;
211
212         /* XXX: capa is sorted in client, this could be optimized */
213         if (ocapa->c_type == CLIENT_CAPA) {
214                 list_for_each_entry_reverse(tmp, head, c_list) {
215                         if (ocapa->c_capa.lc_expiry > tmp->c_capa.lc_expiry) {
216                                 list_add(&ocapa->c_list, &tmp->c_list);
217                                 return;
218                         }
219                 }
220         }
221
222         list_add(&ocapa->c_list, head);
223 }
224
225 static inline void do_update_capa(struct obd_capa *ocapa, struct lustre_capa *capa)
226 {
227         memcpy(&ocapa->c_capa, capa, sizeof(*capa));
228 }
229
230 static struct obd_capa *
231 get_new_capa_locked(struct hlist_head *head, int type, struct lustre_capa *capa)
232 {
233         uid_t uid = capa->lc_uid;
234         int capa_op = capa->lc_op;
235         __u64 mdsid = capa->lc_mdsid;
236         unsigned long ino = capa->lc_ino;
237         struct obd_capa *ocapa, *old;
238
239         ocapa = alloc_capa();
240         if (!ocapa)
241                 return NULL;
242
243         spin_lock(&capa_lock);
244         old = find_capa(head, uid, capa_op, mdsid, ino, capa->lc_igen, type);
245         if (!old) {
246                 do_update_capa(ocapa, capa);
247                 ocapa->c_type = type;
248                 list_add_capa(ocapa, &capa_list[type]);
249                 hlist_add_head(&ocapa->c_hash, head);
250                 if (type == CLIENT_CAPA)
251                         INIT_LIST_HEAD(&ocapa->c_lli_list);
252                 __capa_get(ocapa);
253
254                 capa_count[type]++;
255
256                 DEBUG_CAPA(D_INODE, &ocapa->c_capa, "new %s",
257                            capa_type_name[type]);
258
259                 if (type != CLIENT_CAPA && capa_count[type] > CAPA_CACHE_SIZE) {
260                         struct list_head *node = capa_list[type].next;
261                         struct obd_capa *tcapa;
262                         int count = 0;
263
264                         /* free 12 unused capa from head */
265                         while (node->next != &capa_list[type] && count < 12) {
266                                 tcapa = list_entry(node, struct obd_capa,
267                                                    c_list);
268                                 node = node->next;
269                                 if (atomic_read(&tcapa->c_refc) > 0)
270                                         continue;
271                                 DEBUG_CAPA(D_INODE, &tcapa->c_capa,
272                                            "free unused %s",
273                                            capa_type_name[type]);
274                                 __capa_put(tcapa);
275                                 destroy_capa(tcapa);
276                                 count++;
277                         }
278                 }
279                                         
280                 spin_unlock(&capa_lock);
281                 return ocapa;
282         }
283         spin_unlock(&capa_lock);
284
285         destroy_capa(ocapa);
286         return old;
287 }
288
289 struct obd_capa *
290 capa_get(uid_t uid, int capa_op,__u64 mdsid, unsigned long ino,
291          __u32 igen, int type)
292 {
293         struct hlist_head *head = capa_hash + capa_hashfn(uid, mdsid, ino);
294         struct obd_capa *ocapa;
295
296         ocapa = find_capa_locked(head, uid, capa_op, mdsid, ino, igen, type);
297         
298         return ocapa;
299 }
300
301 void capa_put(struct obd_capa *ocapa)
302 {
303         if (!ocapa)
304                 return;
305
306         DEBUG_CAPA(D_INODE, &ocapa->c_capa, "put %s",
307                    capa_type_name[ocapa->c_type]);
308         spin_lock(&capa_lock);
309         if (ocapa->c_type == CLIENT_CAPA) {
310                 list_del_init(&ocapa->c_lli_list);
311                 __capa_put(ocapa);
312                 destroy_capa(ocapa);
313         } else {
314                 atomic_dec(&ocapa->c_refc);
315         }
316         spin_unlock(&capa_lock);
317 }
318
319 struct obd_capa *capa_renew(struct lustre_capa *capa, int type)
320 {
321         uid_t uid = capa->lc_uid;
322         int capa_op = capa->lc_op;
323         __u64 mdsid = capa->lc_mdsid;
324         unsigned long ino = capa->lc_ino;
325         struct hlist_head *head = capa_hash +
326                                   capa_hashfn(uid, mdsid, ino);
327         struct obd_capa *ocapa;
328
329         spin_lock(&capa_lock);
330         ocapa = find_capa(head, uid, capa_op, mdsid, ino, capa->lc_igen, type);
331         if (ocapa) {
332                 DEBUG_CAPA(D_INFO, capa, "renew %s", capa_type_name[type]);
333                 do_update_capa(ocapa, capa);
334         }
335         spin_unlock(&capa_lock);
336
337         if (!ocapa)
338                 ocapa = get_new_capa_locked(head, type, capa);
339
340         return ocapa;
341 }
342
343 static void dump_capa_key(char *buf, char *key)
344 {
345         int i, n = 0;
346
347         for (i = 0; i < CAPA_KEY_LEN; i++)
348                 n += sprintf(buf + n, "%02x", (unsigned char) key[i]);
349 }
350
351 void capa_hmac(struct crypto_tfm *tfm, __u8 *key, struct lustre_capa *capa)
352 {
353         int keylen = CAPA_KEY_LEN;
354         struct scatterlist sl = {
355                 .page   = virt_to_page(capa),
356                 .offset = (unsigned long)(capa) % PAGE_SIZE,
357                 .length = sizeof(struct lustre_capa_data),
358         };
359
360         LASSERT(tfm);
361         crypto_hmac(tfm, key, &keylen, &sl, 1, capa->lc_hmac);
362         {
363         char *buf;
364
365         OBD_ALLOC(key, keylen * 2 + 1);
366         if (key) {
367                 dump_capa_key(buf, key);
368                 DEBUG_CAPA(D_INODE, capa, "hmac with %s", buf);
369         }
370         }
371 }
372
373 void capa_dup(void *dst, struct obd_capa *ocapa)
374 {
375         spin_lock(&capa_lock);
376         memcpy(dst, &ocapa->c_capa, sizeof(ocapa->c_capa));
377         spin_unlock(&capa_lock);
378 }
379
380 void capa_dup2(void *dst, struct lustre_capa *capa)
381 {
382         spin_lock(&capa_lock);
383         memcpy(dst, capa, sizeof(*capa));
384         spin_unlock(&capa_lock);
385 }
386
387 int capa_expired(struct lustre_capa *capa)
388 {
389         struct timeval tv;
390
391         do_gettimeofday(&tv);
392         return ((unsigned long )capa->lc_expiry <= tv.tv_sec) ? 1 : 0;
393 }
394
395 int __capa_is_to_expire(struct obd_capa *ocapa, struct timeval *tv)
396 {
397         int pre_expiry = capa_pre_expiry(&ocapa->c_capa);
398
399         /* XXX: in case the clock is inaccurate, minus one more
400          * pre_expiry to make sure the expiry won't miss */
401         return ((unsigned long)ocapa->c_capa.lc_expiry -
402                 2 * pre_expiry <= tv->tv_sec)? 1 : 0;
403 }
404
405 int capa_is_to_expire(struct obd_capa *ocapa)
406 {
407         struct timeval tv;
408         int rc;
409
410         do_gettimeofday(&tv);
411         spin_lock(&capa_lock);
412         rc = __capa_is_to_expire(ocapa, &tv);
413         spin_unlock(&capa_lock);
414
415         return rc;
416 }
417
418 EXPORT_SYMBOL(capa_op);
419 EXPORT_SYMBOL(capa_get);
420 EXPORT_SYMBOL(capa_put);
421 EXPORT_SYMBOL(capa_renew);
422 EXPORT_SYMBOL(__capa_get);
423 EXPORT_SYMBOL(capa_hmac);
424 EXPORT_SYMBOL(capa_dup);
425 EXPORT_SYMBOL(capa_dup2);
426 EXPORT_SYMBOL(capa_expired);
427 EXPORT_SYMBOL(__capa_is_to_expire);
428 EXPORT_SYMBOL(capa_is_to_expire);