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