Whamcloud - gitweb
fix:
[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, int capa_op, __u64 mdsid, unsigned long ino)
72 {
73         return (ino ^ uid) * (unsigned long)capa_op * (unsigned long)(mdsid + 1)
74                % 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, 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_mdsid != mdsid)
100                         continue;
101                 if ((ocapa->c_capa.lc_op & capa_op) != ocapa->c_capa.lc_op)
102                         continue;
103                 if (ocapa->c_type != type)
104                         continue;
105
106                 if (ocapa->c_type == CLIENT_CAPA)
107                         ouid = ocapa->c_capa.lc_ruid;
108                 else
109                         ouid = ocapa->c_capa.lc_uid;
110
111                 if (ouid != uid)
112                         continue;
113
114                 DEBUG_CAPA(D_CACHE, &ocapa->c_capa, "found %s",
115                            capa_type_name[ocapa->c_type]);
116
117                 return ocapa;
118         }
119
120         return NULL;
121 }
122
123 inline void __capa_get(struct obd_capa *ocapa)
124 {
125         if (ocapa->c_type != CLIENT_CAPA)
126                 atomic_inc(&ocapa->c_refc);
127 }
128
129 static struct obd_capa *
130 find_capa_locked(struct hlist_head *head, uid_t uid, int capa_op, __u64 mdsid,
131                  unsigned long ino, int type)
132 {
133         struct obd_capa *ocapa;
134
135         spin_lock(&capa_lock);
136         ocapa = find_capa(head, uid, capa_op, mdsid, ino, type);
137         if (ocapa)
138                 __capa_get(ocapa);
139         spin_unlock(&capa_lock);
140
141         return ocapa;
142 }
143
144 static struct obd_capa *alloc_capa(void)
145 {
146         struct obd_capa *ocapa;
147
148         OBD_SLAB_ALLOC(ocapa, capa_cachep, SLAB_NOFS, sizeof(*ocapa));
149         if (ocapa) {
150                 INIT_HLIST_NODE(&ocapa->c_hash);
151                 INIT_LIST_HEAD(&ocapa->c_list);
152         }
153
154         return ocapa;
155 }
156
157 static void __capa_put(struct obd_capa *ocapa)
158 {
159         hlist_del_init(&ocapa->c_hash);
160         list_del_init(&ocapa->c_list);
161         capa_count[ocapa->c_type]--;
162 }
163
164 static void destroy_capa(struct obd_capa *ocapa)
165 {
166         OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
167 }
168
169 int capa_cache_init(void)
170 {
171         int nr_hash, i;
172
173         OBD_ALLOC(capa_hash, PAGE_SIZE);
174         if (!capa_hash)
175                 return -ENOMEM;
176
177         nr_hash = PAGE_SIZE / sizeof(struct hlist_head);
178         LASSERT(nr_hash > NR_CAPAHASH);
179
180         for (i = 0; i < NR_CAPAHASH; i++)
181                 INIT_HLIST_HEAD(capa_hash + i);
182
183         for (i = 0; i < 3; i++)
184                 INIT_LIST_HEAD(&capa_list[i]);
185
186         return 0;
187 }
188
189 void capa_cache_cleanup(void)
190 {
191         struct obd_capa *ocapa, *tmp;
192         int i;
193
194         for (i = MDS_CAPA; i <= FILTER_CAPA; i++) {
195                 list_for_each_entry_safe(ocapa, tmp, &capa_list[i], c_list) {
196                         __capa_put(ocapa);
197                         destroy_capa(ocapa);
198                 }
199         }
200
201         OBD_FREE(capa_hash, PAGE_SIZE);
202 }
203
204
205 static inline void list_add_capa(struct obd_capa *ocapa, struct list_head *head)
206 {
207         struct obd_capa *tmp;
208
209         /* XXX: capa is sorted in client, this could be optimized */
210         if (ocapa->c_type == CLIENT_CAPA) {
211                 list_for_each_entry_reverse(tmp, head, c_list) {
212                         if (ocapa->c_capa.lc_expiry > tmp->c_capa.lc_expiry) {
213                                 list_add(&ocapa->c_list, &tmp->c_list);
214                                 return;
215                         }
216                 }
217         }
218
219         list_add(&ocapa->c_list, head);
220 }
221
222 static inline void do_update_capa(struct obd_capa *ocapa, struct lustre_capa *capa)
223 {
224         memcpy(&ocapa->c_capa, capa, sizeof(*capa));
225 }
226
227 static struct obd_capa *
228 get_new_capa_locked(struct hlist_head *head, int type, struct lustre_capa *capa)
229 {
230         uid_t uid = capa->lc_uid;
231         int capa_op = capa->lc_op;
232         __u64 mdsid = capa->lc_mdsid;
233         unsigned long ino = capa->lc_ino;
234         struct obd_capa *ocapa, *old;
235
236         ocapa = alloc_capa();
237         if (!ocapa)
238                 return NULL;
239
240         spin_lock(&capa_lock);
241         old = find_capa(head, uid, capa_op, mdsid, ino, type);
242         if (!old) {
243                 do_update_capa(ocapa, capa);
244                 ocapa->c_type = type;
245                 list_add_capa(ocapa, &capa_list[type]);
246                 hlist_add_head(&ocapa->c_hash, head);
247                 if (type == CLIENT_CAPA)
248                         INIT_LIST_HEAD(&ocapa->c_lli_list);
249
250                 capa_count[type]++;
251
252                 DEBUG_CAPA(D_CACHE, &ocapa->c_capa, "new %s",
253                            capa_type_name[type]);
254
255                 if (type != CLIENT_CAPA && capa_count[type] > CAPA_CACHE_SIZE) {
256                         struct list_head *node = capa_list[type].next;
257                         struct obd_capa *tcapa;
258                         int count = 0;
259
260                         /* free 12 unused capa from head */
261                         while (node->next != &capa_list[type] && count < 12) {
262                                 tcapa = list_entry(node, struct obd_capa,
263                                                    c_list);
264                                 node = node->next;
265                                 if (atomic_read(&tcapa->c_refc) > 0)
266                                         continue;
267                                 DEBUG_CAPA(D_CACHE, &tcapa->c_capa,
268                                            "free unused %s",
269                                            capa_type_name[type]);
270                                 __capa_put(tcapa);
271                                 destroy_capa(tcapa);
272                                 count++;
273                         }
274                 }
275                                         
276                 spin_unlock(&capa_lock);
277                 return ocapa;
278         }
279         spin_unlock(&capa_lock);
280
281         destroy_capa(ocapa);
282         return old;
283 }
284
285 struct obd_capa *
286 capa_get(uid_t uid, int capa_op,__u64 mdsid, unsigned long ino, int type)
287 {
288         struct hlist_head *head = capa_hash +
289                                   capa_hashfn(uid, capa_op, mdsid, ino);
290         struct obd_capa *ocapa;
291
292         ocapa = find_capa_locked(head, uid, capa_op, mdsid, ino, type);
293         
294         return ocapa;
295 }
296
297 void capa_put(struct obd_capa *ocapa)
298 {
299         if (!ocapa)
300                 return;
301
302         DEBUG_CAPA(D_CACHE, &ocapa->c_capa, "put %s",
303                    capa_type_name[ocapa->c_type]);
304         spin_lock(&capa_lock);
305         if (ocapa->c_type == CLIENT_CAPA) {
306                 list_del_init(&ocapa->c_lli_list);
307                 __capa_put(ocapa);
308                 destroy_capa(ocapa);
309         } else {
310                 atomic_dec(&ocapa->c_refc);
311         }
312         spin_unlock(&capa_lock);
313 }
314
315 struct obd_capa *capa_renew(struct lustre_capa *capa, int type)
316 {
317         uid_t uid = capa->lc_uid;
318         int capa_op = capa->lc_op;
319         __u64 mdsid = capa->lc_mdsid;
320         unsigned long ino = capa->lc_ino;
321         struct hlist_head *head = capa_hash +
322                                   capa_hashfn(uid, capa_op, mdsid, ino);
323         struct obd_capa *ocapa;
324
325         spin_lock(&capa_lock);
326         ocapa = find_capa(head, uid, capa_op, mdsid, ino, type);
327         if (ocapa) {
328                 DEBUG_CAPA(D_INFO, capa, "renew %s", capa_type_name[type]);
329                 do_update_capa(ocapa, capa);
330         }
331         spin_unlock(&capa_lock);
332
333         if (!ocapa)
334                 ocapa = get_new_capa_locked(head, type, capa);
335
336         return ocapa;
337 }
338
339 void capa_hmac(struct crypto_tfm *tfm, __u8 *key, struct lustre_capa *capa)
340 {
341         int keylen = CAPA_KEY_LEN;
342         struct scatterlist sl = {
343                 .page   = virt_to_page(capa),
344                 .offset = (unsigned long)(capa) % PAGE_SIZE,
345                 .length = sizeof(struct lustre_capa_data),
346         };
347
348         LASSERT(tfm);
349         crypto_hmac(tfm, key, &keylen, &sl, 1, capa->lc_hmac);
350 }
351
352 void capa_dup(void *dst, struct obd_capa *ocapa)
353 {
354         spin_lock(&capa_lock);
355         memcpy(dst, &ocapa->c_capa, sizeof(ocapa->c_capa));
356         spin_unlock(&capa_lock);
357 }
358
359 void capa_dup2(void *dst, struct lustre_capa *capa)
360 {
361         spin_lock(&capa_lock);
362         memcpy(dst, capa, sizeof(*capa));
363         spin_unlock(&capa_lock);
364 }
365
366 int capa_expired(struct lustre_capa *capa)
367 {
368         struct timeval tv;
369
370         do_gettimeofday(&tv);
371         return ((unsigned long )capa->lc_expiry <= tv.tv_sec) ? 1 : 0;
372 }
373
374 int __capa_is_to_expire(struct obd_capa *ocapa)
375 {
376         struct timeval tv;
377         int pre_expiry = capa_pre_expiry(&ocapa->c_capa);
378
379         do_gettimeofday(&tv);
380         /* XXX: in case the lock is inaccurate, minus one more
381          * pre_expiry to make sure the expiry won't miss */
382         return ((unsigned long)ocapa->c_capa.lc_expiry -
383                 2 * pre_expiry <= tv.tv_sec)? 1 : 0;
384 }
385
386 int capa_is_to_expire(struct obd_capa *ocapa)
387 {
388         int rc;
389
390         spin_lock(&capa_lock);
391         rc = __capa_is_to_expire(ocapa);
392         spin_unlock(&capa_lock);
393
394         return rc;
395 }
396
397 EXPORT_SYMBOL(capa_op);
398 EXPORT_SYMBOL(capa_get);
399 EXPORT_SYMBOL(capa_put);
400 EXPORT_SYMBOL(capa_renew);
401 EXPORT_SYMBOL(__capa_get);
402 EXPORT_SYMBOL(capa_hmac);
403 EXPORT_SYMBOL(capa_dup);
404 EXPORT_SYMBOL(capa_dup2);
405 EXPORT_SYMBOL(capa_expired);
406 EXPORT_SYMBOL(__capa_is_to_expire);
407 EXPORT_SYMBOL(capa_is_to_expire);