1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/obdclass/capa.c
5 * Lustre Capability Cache Management
7 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * Author: Lai Siyao<lsy@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
26 # define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_SEC
32 #include <linux/version.h>
34 #include <asm/unistd.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
39 #include <linux/obd_class.h>
40 #include <linux/lustre_debug.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_sec.h>
44 #include <liblustre.h>
47 #include <libcfs/list.h>
48 #include <linux/lustre_sec.h>
50 kmem_cache_t *capa_cachep = NULL;
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 };
58 static char *capa_type_name[] = { "client", "mds", "filter" };
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-
63 struct timer_list ll_capa_timer;
65 EXPORT_SYMBOL(capa_lock);
66 EXPORT_SYMBOL(capa_hash);
67 EXPORT_SYMBOL(capa_list);
68 EXPORT_SYMBOL(ll_capa_timer);
70 static inline int const
71 capa_hashfn(unsigned int uid, __u64 mdsid, unsigned long ino)
73 return (ino ^ uid) * (unsigned long)(mdsid + 1) % NR_CAPAHASH;
76 int capa_op(int flags)
78 if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
80 else if (flags & FMODE_READ)
83 LBUG(); /* should be either MAY_READ or MAY_WRITE */
87 static struct obd_capa *
88 find_capa(struct hlist_head *head, uid_t uid, int capa_op, __u64 mdsid,
89 unsigned long ino, __u32 igen, int type)
91 struct hlist_node *pos;
92 struct obd_capa *ocapa;
95 hlist_for_each_entry(ocapa, pos, head, c_hash) {
96 if (ocapa->c_capa.lc_ino != ino)
98 if (ocapa->c_capa.lc_igen != igen)
100 if (ocapa->c_capa.lc_mdsid != mdsid)
102 if ((ocapa->c_capa.lc_op & capa_op) != ocapa->c_capa.lc_op)
104 if (ocapa->c_type != type)
107 if (ocapa->c_type == CLIENT_CAPA)
108 ouid = ocapa->c_capa.lc_ruid;
110 ouid = ocapa->c_capa.lc_uid;
115 DEBUG_CAPA(D_CACHE, &ocapa->c_capa, "found %s",
116 capa_type_name[ocapa->c_type]);
124 inline void __capa_get(struct obd_capa *ocapa)
126 if (ocapa->c_type != CLIENT_CAPA)
127 atomic_inc(&ocapa->c_refc);
130 static struct obd_capa *
131 find_capa_locked(struct hlist_head *head, uid_t uid, int capa_op, __u64 mdsid,
132 unsigned long ino, __u32 igen, int type)
134 struct obd_capa *ocapa;
136 spin_lock(&capa_lock);
137 ocapa = find_capa(head, uid, capa_op, mdsid, ino, igen, type);
140 spin_unlock(&capa_lock);
145 static struct obd_capa *alloc_capa(void)
147 struct obd_capa *ocapa;
149 OBD_SLAB_ALLOC(ocapa, capa_cachep, SLAB_NOFS, sizeof(*ocapa));
151 INIT_HLIST_NODE(&ocapa->c_hash);
152 INIT_LIST_HEAD(&ocapa->c_list);
158 static void __capa_put(struct obd_capa *ocapa)
160 hlist_del_init(&ocapa->c_hash);
161 list_del_init(&ocapa->c_list);
162 capa_count[ocapa->c_type]--;
165 static void destroy_capa(struct obd_capa *ocapa)
167 OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
170 int capa_cache_init(void)
174 OBD_ALLOC(capa_hash, PAGE_SIZE);
178 nr_hash = PAGE_SIZE / sizeof(struct hlist_head);
179 LASSERT(nr_hash > NR_CAPAHASH);
181 for (i = 0; i < NR_CAPAHASH; i++)
182 INIT_HLIST_HEAD(capa_hash + i);
184 for (i = 0; i < 3; i++)
185 INIT_LIST_HEAD(&capa_list[i]);
190 void capa_cache_cleanup(void)
192 struct obd_capa *ocapa, *tmp;
195 for (i = MDS_CAPA; i <= FILTER_CAPA; i++) {
196 list_for_each_entry_safe(ocapa, tmp, &capa_list[i], c_list) {
202 OBD_FREE(capa_hash, PAGE_SIZE);
206 static inline void list_add_capa(struct obd_capa *ocapa, struct list_head *head)
208 struct obd_capa *tmp;
210 /* XXX: capa is sorted in client, this could be optimized */
211 if (ocapa->c_type == CLIENT_CAPA) {
212 list_for_each_entry_reverse(tmp, head, c_list) {
213 if (ocapa->c_capa.lc_expiry > tmp->c_capa.lc_expiry) {
214 list_add(&ocapa->c_list, &tmp->c_list);
220 list_add(&ocapa->c_list, head);
223 static inline void do_update_capa(struct obd_capa *ocapa, struct lustre_capa *capa)
225 memcpy(&ocapa->c_capa, capa, sizeof(*capa));
228 static struct obd_capa *
229 get_new_capa_locked(struct hlist_head *head, int type, struct lustre_capa *capa)
231 uid_t uid = capa->lc_uid;
232 int capa_op = capa->lc_op;
233 __u64 mdsid = capa->lc_mdsid;
234 unsigned long ino = capa->lc_ino;
235 struct obd_capa *ocapa, *old;
237 ocapa = alloc_capa();
241 spin_lock(&capa_lock);
242 old = find_capa(head, uid, capa_op, mdsid, ino, capa->lc_igen, type);
244 do_update_capa(ocapa, capa);
245 ocapa->c_type = type;
246 list_add_capa(ocapa, &capa_list[type]);
247 hlist_add_head(&ocapa->c_hash, head);
248 if (type == CLIENT_CAPA)
249 INIT_LIST_HEAD(&ocapa->c_lli_list);
254 DEBUG_CAPA(D_CACHE, &ocapa->c_capa, "new %s",
255 capa_type_name[type]);
257 if (type != CLIENT_CAPA && capa_count[type] > CAPA_CACHE_SIZE) {
258 struct list_head *node = capa_list[type].next;
259 struct obd_capa *tcapa;
262 /* free 12 unused capa from head */
263 while (node->next != &capa_list[type] && count < 12) {
264 tcapa = list_entry(node, struct obd_capa,
267 if (atomic_read(&tcapa->c_refc) > 0)
269 DEBUG_CAPA(D_CACHE, &tcapa->c_capa,
271 capa_type_name[type]);
278 spin_unlock(&capa_lock);
281 spin_unlock(&capa_lock);
288 capa_get(uid_t uid, int capa_op,__u64 mdsid, unsigned long ino,
289 __u32 igen, int type)
291 struct hlist_head *head = capa_hash + capa_hashfn(uid, mdsid, ino);
292 struct obd_capa *ocapa;
294 ocapa = find_capa_locked(head, uid, capa_op, mdsid, ino, igen, type);
299 void capa_put(struct obd_capa *ocapa)
304 DEBUG_CAPA(D_CACHE, &ocapa->c_capa, "put %s",
305 capa_type_name[ocapa->c_type]);
306 spin_lock(&capa_lock);
307 if (ocapa->c_type == CLIENT_CAPA) {
308 list_del_init(&ocapa->c_lli_list);
312 atomic_dec(&ocapa->c_refc);
314 spin_unlock(&capa_lock);
317 struct obd_capa *capa_renew(struct lustre_capa *capa, int type)
319 uid_t uid = capa->lc_uid;
320 int capa_op = capa->lc_op;
321 __u64 mdsid = capa->lc_mdsid;
322 unsigned long ino = capa->lc_ino;
323 struct hlist_head *head = capa_hash +
324 capa_hashfn(uid, mdsid, ino);
325 struct obd_capa *ocapa;
327 spin_lock(&capa_lock);
328 ocapa = find_capa(head, uid, capa_op, mdsid, ino, capa->lc_igen, type);
330 DEBUG_CAPA(D_INFO, capa, "renew %s", capa_type_name[type]);
331 do_update_capa(ocapa, capa);
333 spin_unlock(&capa_lock);
336 ocapa = get_new_capa_locked(head, type, capa);
341 void capa_hmac(struct crypto_tfm *tfm, __u8 *key, struct lustre_capa *capa)
343 int keylen = CAPA_KEY_LEN;
344 struct scatterlist sl = {
345 .page = virt_to_page(capa),
346 .offset = (unsigned long)(capa) % PAGE_SIZE,
347 .length = sizeof(struct lustre_capa_data),
351 crypto_hmac(tfm, key, &keylen, &sl, 1, capa->lc_hmac);
354 void capa_dup(void *dst, struct obd_capa *ocapa)
356 spin_lock(&capa_lock);
357 memcpy(dst, &ocapa->c_capa, sizeof(ocapa->c_capa));
358 spin_unlock(&capa_lock);
361 void capa_dup2(void *dst, struct lustre_capa *capa)
363 spin_lock(&capa_lock);
364 memcpy(dst, capa, sizeof(*capa));
365 spin_unlock(&capa_lock);
368 int capa_expired(struct lustre_capa *capa)
372 do_gettimeofday(&tv);
373 return ((unsigned long )capa->lc_expiry <= tv.tv_sec) ? 1 : 0;
376 int __capa_is_to_expire(struct obd_capa *ocapa, struct timeval *tv)
378 int pre_expiry = capa_pre_expiry(&ocapa->c_capa);
380 /* XXX: in case the clock 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;
386 int capa_is_to_expire(struct obd_capa *ocapa)
391 do_gettimeofday(&tv);
392 spin_lock(&capa_lock);
393 rc = __capa_is_to_expire(ocapa, &tv);
394 spin_unlock(&capa_lock);
399 EXPORT_SYMBOL(capa_op);
400 EXPORT_SYMBOL(capa_get);
401 EXPORT_SYMBOL(capa_put);
402 EXPORT_SYMBOL(capa_renew);
403 EXPORT_SYMBOL(__capa_get);
404 EXPORT_SYMBOL(capa_hmac);
405 EXPORT_SYMBOL(capa_dup);
406 EXPORT_SYMBOL(capa_dup2);
407 EXPORT_SYMBOL(capa_expired);
408 EXPORT_SYMBOL(__capa_is_to_expire);
409 EXPORT_SYMBOL(capa_is_to_expire);