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