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