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 <obd_support.h>
48 #include <lustre_lib.h>
50 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
51 __u64 key, void *args)
53 struct upcall_cache_entry *entry;
59 UC_CACHE_SET_NEW(entry);
60 INIT_LIST_HEAD(&entry->ue_hash);
62 atomic_set(&entry->ue_refcount, 0);
63 init_waitqueue_head(&entry->ue_waitq);
64 if (cache->uc_ops->init_entry)
65 cache->uc_ops->init_entry(entry, args);
69 /* protected by cache lock */
70 static void free_entry(struct upcall_cache *cache,
71 struct upcall_cache_entry *entry)
73 if (cache->uc_ops->free_entry)
74 cache->uc_ops->free_entry(cache, entry);
76 list_del(&entry->ue_hash);
77 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
78 entry, entry->ue_key);
82 static inline int upcall_compare(struct upcall_cache *cache,
83 struct upcall_cache_entry *entry,
84 __u64 key, void *args)
86 if (entry->ue_key != key)
89 if (cache->uc_ops->upcall_compare)
90 return cache->uc_ops->upcall_compare(cache, entry, key, args);
95 static inline int downcall_compare(struct upcall_cache *cache,
96 struct upcall_cache_entry *entry,
97 __u64 key, void *args)
99 if (entry->ue_key != key)
102 if (cache->uc_ops->downcall_compare)
103 return cache->uc_ops->downcall_compare(cache, entry, key, args);
108 static inline void get_entry(struct upcall_cache_entry *entry)
110 atomic_inc(&entry->ue_refcount);
113 static inline void put_entry(struct upcall_cache *cache,
114 struct upcall_cache_entry *entry)
116 if (atomic_dec_and_test(&entry->ue_refcount) &&
117 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
118 free_entry(cache, entry);
122 static int check_unlink_entry(struct upcall_cache *cache,
123 struct upcall_cache_entry *entry)
125 if (UC_CACHE_IS_VALID(entry) &&
126 time_before(jiffies, entry->ue_expire))
129 if (UC_CACHE_IS_ACQUIRING(entry)) {
130 if (time_before(jiffies, entry->ue_acquire_expire))
133 UC_CACHE_SET_EXPIRED(entry);
134 wake_up_all(&entry->ue_waitq);
135 } else if (!UC_CACHE_IS_INVALID(entry)) {
136 UC_CACHE_SET_EXPIRED(entry);
139 list_del_init(&entry->ue_hash);
140 if (!atomic_read(&entry->ue_refcount))
141 free_entry(cache, entry);
145 static inline int refresh_entry(struct upcall_cache *cache,
146 struct upcall_cache_entry *entry)
148 LASSERT(cache->uc_ops->do_upcall);
149 return cache->uc_ops->do_upcall(cache, entry);
152 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
153 __u64 key, void *args)
155 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
156 struct list_head *head;
163 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
166 spin_lock(&cache->uc_lock);
167 list_for_each_entry_safe(entry, next, head, ue_hash) {
168 /* check invalid & expired items */
169 if (check_unlink_entry(cache, entry))
171 if (upcall_compare(cache, entry, key, args) == 0) {
177 if (!found) { /* didn't find it */
179 spin_unlock(&cache->uc_lock);
180 new = alloc_entry(cache, key, args);
182 CERROR("fail to alloc entry\n");
183 RETURN(ERR_PTR(-ENOMEM));
187 list_add(&new->ue_hash, head);
192 free_entry(cache, new);
195 list_move(&entry->ue_hash, head);
199 /* acquire for new one */
200 if (UC_CACHE_IS_NEW(entry)) {
201 UC_CACHE_SET_ACQUIRING(entry);
202 UC_CACHE_CLEAR_NEW(entry);
203 entry->ue_acquire_expire = jiffies + cache->uc_acquire_expire;
204 spin_unlock(&cache->uc_lock);
205 rc = refresh_entry(cache, entry);
206 spin_lock(&cache->uc_lock);
208 UC_CACHE_CLEAR_ACQUIRING(entry);
209 UC_CACHE_SET_INVALID(entry);
210 if (unlikely(rc == -EREMCHG)) {
211 put_entry(cache, entry);
212 GOTO(out, entry = ERR_PTR(rc));
217 /* someone (and only one) is doing upcall upon
218 * this item, just wait it complete
220 if (UC_CACHE_IS_ACQUIRING(entry)) {
221 unsigned long expiry = jiffies + cache->uc_acquire_expire;
223 init_waitqueue_entry(&wait, current);
224 add_wait_queue(&entry->ue_waitq, &wait);
225 set_current_state(TASK_INTERRUPTIBLE);
226 spin_unlock(&cache->uc_lock);
228 schedule_timeout(cache->uc_acquire_expire);
230 spin_lock(&cache->uc_lock);
231 remove_wait_queue(&entry->ue_waitq, &wait);
232 if (UC_CACHE_IS_ACQUIRING(entry)) {
233 /* we're interrupted or upcall failed in the middle */
234 rc = time_before(jiffies, expiry) ? -EINTR : -ETIMEDOUT;
235 put_entry(cache, entry);
236 CERROR("acquire timeout exceeded for key "LPU64
237 "\n", entry->ue_key);
238 GOTO(out, entry = ERR_PTR(rc));
243 /* invalid means error, don't need to try again */
244 if (UC_CACHE_IS_INVALID(entry)) {
245 put_entry(cache, entry);
246 GOTO(out, entry = ERR_PTR(-EIDRM));
250 * We can't refresh the existing one because some
251 * memory might be shared by multiple processes.
253 if (check_unlink_entry(cache, entry)) {
254 /* if expired, try again. but if this entry is
255 * created by me but too quickly turn to expired
256 * without any error, should at least give a
257 * chance to use it once.
260 put_entry(cache, entry);
261 spin_unlock(&cache->uc_lock);
267 /* Now we know it's good */
269 spin_unlock(&cache->uc_lock);
272 EXPORT_SYMBOL(upcall_cache_get_entry);
274 void upcall_cache_put_entry(struct upcall_cache *cache,
275 struct upcall_cache_entry *entry)
284 LASSERT(atomic_read(&entry->ue_refcount) > 0);
285 spin_lock(&cache->uc_lock);
286 put_entry(cache, entry);
287 spin_unlock(&cache->uc_lock);
290 EXPORT_SYMBOL(upcall_cache_put_entry);
292 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
295 struct upcall_cache_entry *entry = NULL;
296 struct list_head *head;
297 int found = 0, rc = 0;
302 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
304 spin_lock(&cache->uc_lock);
305 list_for_each_entry(entry, head, ue_hash) {
306 if (downcall_compare(cache, entry, key, args) == 0) {
314 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
315 cache->uc_name, key);
316 /* haven't found, it's possible */
317 spin_unlock(&cache->uc_lock);
322 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
323 cache->uc_name, entry->ue_key, err);
324 GOTO(out, rc = -EINVAL);
327 if (!UC_CACHE_IS_ACQUIRING(entry)) {
328 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
329 cache->uc_name, entry, entry->ue_key);
333 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
334 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
335 cache->uc_name, entry, entry->ue_key);
336 GOTO(out, rc = -EINVAL);
339 spin_unlock(&cache->uc_lock);
340 if (cache->uc_ops->parse_downcall)
341 rc = cache->uc_ops->parse_downcall(cache, entry, args);
342 spin_lock(&cache->uc_lock);
346 entry->ue_expire = jiffies + cache->uc_entry_expire;
347 UC_CACHE_SET_VALID(entry);
348 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
349 cache->uc_name, entry, entry->ue_key);
352 UC_CACHE_SET_INVALID(entry);
353 list_del_init(&entry->ue_hash);
355 UC_CACHE_CLEAR_ACQUIRING(entry);
356 spin_unlock(&cache->uc_lock);
357 wake_up_all(&entry->ue_waitq);
358 put_entry(cache, entry);
362 EXPORT_SYMBOL(upcall_cache_downcall);
364 static void cache_flush(struct upcall_cache *cache, int force)
366 struct upcall_cache_entry *entry, *next;
370 spin_lock(&cache->uc_lock);
371 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
372 list_for_each_entry_safe(entry, next,
373 &cache->uc_hashtable[i], ue_hash) {
374 if (!force && atomic_read(&entry->ue_refcount)) {
375 UC_CACHE_SET_EXPIRED(entry);
378 LASSERT(!atomic_read(&entry->ue_refcount));
379 free_entry(cache, entry);
382 spin_unlock(&cache->uc_lock);
386 void upcall_cache_flush_idle(struct upcall_cache *cache)
388 cache_flush(cache, 0);
390 EXPORT_SYMBOL(upcall_cache_flush_idle);
392 void upcall_cache_flush_all(struct upcall_cache *cache)
394 cache_flush(cache, 1);
396 EXPORT_SYMBOL(upcall_cache_flush_all);
398 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
400 struct list_head *head;
401 struct upcall_cache_entry *entry;
405 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
407 spin_lock(&cache->uc_lock);
408 list_for_each_entry(entry, head, ue_hash) {
409 if (upcall_compare(cache, entry, key, args) == 0) {
416 CWARN("%s: flush entry %p: key "LPU64", ref %d, fl %x, "
417 "cur %lu, ex %ld/%ld\n",
418 cache->uc_name, entry, entry->ue_key,
419 atomic_read(&entry->ue_refcount), entry->ue_flags,
420 get_seconds(), entry->ue_acquire_expire,
422 UC_CACHE_SET_EXPIRED(entry);
423 if (!atomic_read(&entry->ue_refcount))
424 free_entry(cache, entry);
426 spin_unlock(&cache->uc_lock);
428 EXPORT_SYMBOL(upcall_cache_flush_one);
430 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
431 struct upcall_cache_ops *ops)
433 struct upcall_cache *cache;
437 OBD_ALLOC(cache, sizeof(*cache));
439 RETURN(ERR_PTR(-ENOMEM));
441 spin_lock_init(&cache->uc_lock);
442 rwlock_init(&cache->uc_upcall_rwlock);
443 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
444 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
445 strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
446 /* upcall pathname proc tunable */
447 strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
448 cache->uc_entry_expire = 10 * 60 * HZ;
449 cache->uc_acquire_expire = 15 * HZ;
454 EXPORT_SYMBOL(upcall_cache_init);
456 void upcall_cache_cleanup(struct upcall_cache *cache)
460 upcall_cache_flush_all(cache);
461 OBD_FREE(cache, sizeof(*cache));
463 EXPORT_SYMBOL(upcall_cache_cleanup);