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