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