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