1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Supplementary groups cache.
6 * Copyright (c) 2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_SEC
26 #ifndef AUTOCONF_INCLUDED
27 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
32 #include <linux/kmod.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/version.h>
37 #include <linux/unistd.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
43 #include <linux/stat.h>
44 #include <asm/uaccess.h>
45 #include <linux/slab.h>
47 # include <asm/segment.h>
50 #include <obd_support.h>
51 #include <lustre_lib.h>
53 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
54 __u64 key, void *args)
56 struct upcall_cache_entry *entry;
62 UC_CACHE_SET_NEW(entry);
63 INIT_LIST_HEAD(&entry->ue_hash);
65 atomic_set(&entry->ue_refcount, 0);
66 init_waitqueue_head(&entry->ue_waitq);
67 if (cache->uc_ops->init_entry)
68 cache->uc_ops->init_entry(entry, args);
72 /* protected by cache lock */
73 static void free_entry(struct upcall_cache *cache,
74 struct upcall_cache_entry *entry)
76 if (cache->uc_ops->free_entry)
77 cache->uc_ops->free_entry(cache, entry);
79 list_del(&entry->ue_hash);
80 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
81 entry, entry->ue_key);
85 static inline int upcall_compare(struct upcall_cache *cache,
86 struct upcall_cache_entry *entry,
87 __u64 key, void *args)
89 if (entry->ue_key != key)
92 if (cache->uc_ops->upcall_compare)
93 return cache->uc_ops->upcall_compare(cache, entry, key, args);
98 static inline int downcall_compare(struct upcall_cache *cache,
99 struct upcall_cache_entry *entry,
100 __u64 key, void *args)
102 if (entry->ue_key != key)
105 if (cache->uc_ops->downcall_compare)
106 return cache->uc_ops->downcall_compare(cache, entry, key, args);
111 static inline void get_entry(struct upcall_cache_entry *entry)
113 atomic_inc(&entry->ue_refcount);
116 static inline void put_entry(struct upcall_cache *cache,
117 struct upcall_cache_entry *entry)
119 if (atomic_dec_and_test(&entry->ue_refcount) &&
120 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
121 free_entry(cache, entry);
125 static int check_unlink_entry(struct upcall_cache *cache,
126 struct upcall_cache_entry *entry)
128 if (UC_CACHE_IS_VALID(entry) &&
129 time_before(jiffies, entry->ue_expire))
132 if (UC_CACHE_IS_ACQUIRING(entry)) {
133 if (time_before(jiffies, entry->ue_acquire_expire))
136 UC_CACHE_SET_EXPIRED(entry);
137 wake_up_all(&entry->ue_waitq);
138 } else if (!UC_CACHE_IS_INVALID(entry)) {
139 UC_CACHE_SET_EXPIRED(entry);
142 list_del_init(&entry->ue_hash);
143 if (!atomic_read(&entry->ue_refcount))
144 free_entry(cache, entry);
148 static inline int refresh_entry(struct upcall_cache *cache,
149 struct upcall_cache_entry *entry)
151 LASSERT(cache->uc_ops->do_upcall);
152 return cache->uc_ops->do_upcall(cache, entry);
155 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
156 __u64 key, void *args)
158 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
159 struct list_head *head;
166 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
169 spin_lock(&cache->uc_lock);
170 list_for_each_entry_safe(entry, next, head, ue_hash) {
171 /* check invalid & expired items */
172 if (check_unlink_entry(cache, entry))
174 if (upcall_compare(cache, entry, key, args) == 0) {
180 if (!found) { /* didn't find it */
182 spin_unlock(&cache->uc_lock);
183 new = alloc_entry(cache, key, args);
185 CERROR("fail to alloc entry\n");
186 RETURN(ERR_PTR(-ENOMEM));
190 list_add(&new->ue_hash, head);
195 free_entry(cache, new);
198 list_move(&entry->ue_hash, head);
202 /* acquire for new one */
203 if (UC_CACHE_IS_NEW(entry)) {
204 UC_CACHE_SET_ACQUIRING(entry);
205 UC_CACHE_CLEAR_NEW(entry);
206 entry->ue_acquire_expire = jiffies + cache->uc_acquire_expire;
207 spin_unlock(&cache->uc_lock);
208 rc = refresh_entry(cache, entry);
209 spin_lock(&cache->uc_lock);
211 UC_CACHE_CLEAR_ACQUIRING(entry);
212 UC_CACHE_SET_INVALID(entry);
216 /* someone (and only one) is doing upcall upon
217 * this item, just wait it complete
219 if (UC_CACHE_IS_ACQUIRING(entry)) {
220 unsigned long expiry = jiffies + cache->uc_acquire_expire;
222 init_waitqueue_entry(&wait, current);
223 add_wait_queue(&entry->ue_waitq, &wait);
224 set_current_state(TASK_INTERRUPTIBLE);
225 spin_unlock(&cache->uc_lock);
227 schedule_timeout(cache->uc_acquire_expire);
229 spin_lock(&cache->uc_lock);
230 remove_wait_queue(&entry->ue_waitq, &wait);
231 if (UC_CACHE_IS_ACQUIRING(entry)) {
232 /* we're interrupted or upcall failed in the middle */
233 rc = time_before(jiffies, expiry) ? -EINTR : -ETIMEDOUT;
234 put_entry(cache, entry);
235 CERROR("acquire timeout exceeded for key "LPU64
236 "\n", entry->ue_key);
237 GOTO(out, entry = ERR_PTR(rc));
242 /* invalid means error, don't need to try again */
243 if (UC_CACHE_IS_INVALID(entry)) {
244 put_entry(cache, entry);
245 GOTO(out, entry = ERR_PTR(-EIDRM));
249 * We can't refresh the existing one because some
250 * memory might be shared by multiple processes.
252 if (check_unlink_entry(cache, entry)) {
253 /* if expired, try again. but if this entry is
254 * created by me but too quickly turn to expired
255 * without any error, should at least give a
256 * chance to use it once.
259 put_entry(cache, entry);
260 spin_unlock(&cache->uc_lock);
266 /* Now we know it's good */
268 spin_unlock(&cache->uc_lock);
271 EXPORT_SYMBOL(upcall_cache_get_entry);
273 void upcall_cache_put_entry(struct upcall_cache *cache,
274 struct upcall_cache_entry *entry)
283 LASSERT(atomic_read(&entry->ue_refcount) > 0);
284 spin_lock(&cache->uc_lock);
285 put_entry(cache, entry);
286 spin_unlock(&cache->uc_lock);
289 EXPORT_SYMBOL(upcall_cache_put_entry);
291 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
294 struct upcall_cache_entry *entry = NULL;
295 struct list_head *head;
296 int found = 0, rc = 0;
301 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
303 spin_lock(&cache->uc_lock);
304 list_for_each_entry(entry, head, ue_hash) {
305 if (downcall_compare(cache, entry, key, args) == 0) {
313 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
314 cache->uc_name, key);
315 /* haven't found, it's possible */
316 spin_unlock(&cache->uc_lock);
321 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
322 cache->uc_name, entry->ue_key, err);
323 GOTO(out, rc = -EINVAL);
326 if (!UC_CACHE_IS_ACQUIRING(entry)) {
327 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
328 cache->uc_name, entry, entry->ue_key);
332 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
333 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
334 cache->uc_name, entry, entry->ue_key);
335 GOTO(out, rc = -EINVAL);
338 spin_unlock(&cache->uc_lock);
339 if (cache->uc_ops->parse_downcall)
340 rc = cache->uc_ops->parse_downcall(cache, entry, args);
341 spin_lock(&cache->uc_lock);
345 entry->ue_expire = jiffies + cache->uc_entry_expire;
346 UC_CACHE_SET_VALID(entry);
347 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
348 cache->uc_name, entry, entry->ue_key);
351 UC_CACHE_SET_INVALID(entry);
352 list_del_init(&entry->ue_hash);
354 UC_CACHE_CLEAR_ACQUIRING(entry);
355 spin_unlock(&cache->uc_lock);
356 wake_up_all(&entry->ue_waitq);
357 put_entry(cache, entry);
361 EXPORT_SYMBOL(upcall_cache_downcall);
363 static void cache_flush(struct upcall_cache *cache, int force)
365 struct upcall_cache_entry *entry, *next;
369 spin_lock(&cache->uc_lock);
370 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
371 list_for_each_entry_safe(entry, next,
372 &cache->uc_hashtable[i], ue_hash) {
373 if (!force && atomic_read(&entry->ue_refcount)) {
374 UC_CACHE_SET_EXPIRED(entry);
377 LASSERT(!atomic_read(&entry->ue_refcount));
378 free_entry(cache, entry);
381 spin_unlock(&cache->uc_lock);
385 void upcall_cache_flush_idle(struct upcall_cache *cache)
387 cache_flush(cache, 0);
389 EXPORT_SYMBOL(upcall_cache_flush_idle);
391 void upcall_cache_flush_all(struct upcall_cache *cache)
393 cache_flush(cache, 1);
395 EXPORT_SYMBOL(upcall_cache_flush_all);
397 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
399 struct list_head *head;
400 struct upcall_cache_entry *entry;
404 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
406 spin_lock(&cache->uc_lock);
407 list_for_each_entry(entry, head, ue_hash) {
408 if (upcall_compare(cache, entry, key, args) == 0) {
415 CWARN("%s: flush entry %p: key "LPU64", ref %d, fl %x, "
416 "cur %lu, ex %ld/%ld\n",
417 cache->uc_name, entry, entry->ue_key,
418 atomic_read(&entry->ue_refcount), entry->ue_flags,
419 get_seconds(), entry->ue_acquire_expire,
421 UC_CACHE_SET_EXPIRED(entry);
422 if (!atomic_read(&entry->ue_refcount))
423 free_entry(cache, entry);
425 spin_unlock(&cache->uc_lock);
427 EXPORT_SYMBOL(upcall_cache_flush_one);
429 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
430 struct upcall_cache_ops *ops)
432 struct upcall_cache *cache;
436 OBD_ALLOC(cache, sizeof(*cache));
438 RETURN(ERR_PTR(-ENOMEM));
440 spin_lock_init(&cache->uc_lock);
441 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
442 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
443 strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
444 /* upcall pathname proc tunable */
445 strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
446 cache->uc_entry_expire = 10 * 60 * HZ;
447 cache->uc_acquire_expire = 15 * HZ;
452 EXPORT_SYMBOL(upcall_cache_init);
454 void upcall_cache_cleanup(struct upcall_cache *cache)
458 upcall_cache_flush_all(cache);
459 OBD_FREE(cache, sizeof(*cache));
461 EXPORT_SYMBOL(upcall_cache_cleanup);