4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/libcfs/upcall_cache.c
36 * Supplementary groups cache.
38 #define DEBUG_SUBSYSTEM S_SEC
40 #include <libcfs/lucache.h>
42 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
43 __u64 key, void *args)
45 struct upcall_cache_entry *entry;
47 LIBCFS_ALLOC(entry, sizeof(*entry));
51 UC_CACHE_SET_NEW(entry);
52 CFS_INIT_LIST_HEAD(&entry->ue_hash);
54 cfs_atomic_set(&entry->ue_refcount, 0);
55 cfs_waitq_init(&entry->ue_waitq);
56 if (cache->uc_ops->init_entry)
57 cache->uc_ops->init_entry(entry, args);
61 /* protected by cache lock */
62 static void free_entry(struct upcall_cache *cache,
63 struct upcall_cache_entry *entry)
65 if (cache->uc_ops->free_entry)
66 cache->uc_ops->free_entry(cache, entry);
68 cfs_list_del(&entry->ue_hash);
69 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
70 entry, entry->ue_key);
71 LIBCFS_FREE(entry, sizeof(*entry));
74 static inline int upcall_compare(struct upcall_cache *cache,
75 struct upcall_cache_entry *entry,
76 __u64 key, void *args)
78 if (entry->ue_key != key)
81 if (cache->uc_ops->upcall_compare)
82 return cache->uc_ops->upcall_compare(cache, entry, key, args);
87 static inline int downcall_compare(struct upcall_cache *cache,
88 struct upcall_cache_entry *entry,
89 __u64 key, void *args)
91 if (entry->ue_key != key)
94 if (cache->uc_ops->downcall_compare)
95 return cache->uc_ops->downcall_compare(cache, entry, key, args);
100 static inline void get_entry(struct upcall_cache_entry *entry)
102 cfs_atomic_inc(&entry->ue_refcount);
105 static inline void put_entry(struct upcall_cache *cache,
106 struct upcall_cache_entry *entry)
108 if (cfs_atomic_dec_and_test(&entry->ue_refcount) &&
109 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
110 free_entry(cache, entry);
114 static int check_unlink_entry(struct upcall_cache *cache,
115 struct upcall_cache_entry *entry)
117 if (UC_CACHE_IS_VALID(entry) &&
118 cfs_time_before(cfs_time_current(), entry->ue_expire))
121 if (UC_CACHE_IS_ACQUIRING(entry)) {
122 if (entry->ue_acquire_expire == 0 ||
123 cfs_time_before(cfs_time_current(),
124 entry->ue_acquire_expire))
127 UC_CACHE_SET_EXPIRED(entry);
128 cfs_waitq_broadcast(&entry->ue_waitq);
129 } else if (!UC_CACHE_IS_INVALID(entry)) {
130 UC_CACHE_SET_EXPIRED(entry);
133 cfs_list_del_init(&entry->ue_hash);
134 if (!cfs_atomic_read(&entry->ue_refcount))
135 free_entry(cache, entry);
139 static inline int refresh_entry(struct upcall_cache *cache,
140 struct upcall_cache_entry *entry)
142 LASSERT(cache->uc_ops->do_upcall);
143 return cache->uc_ops->do_upcall(cache, entry);
146 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
147 __u64 key, void *args)
149 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
157 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
160 cfs_spin_lock(&cache->uc_lock);
161 cfs_list_for_each_entry_safe(entry, next, head, ue_hash) {
162 /* check invalid & expired items */
163 if (check_unlink_entry(cache, entry))
165 if (upcall_compare(cache, entry, key, args) == 0) {
173 cfs_spin_unlock(&cache->uc_lock);
174 new = alloc_entry(cache, key, args);
176 CERROR("fail to alloc entry\n");
177 RETURN(ERR_PTR(-ENOMEM));
181 cfs_list_add(&new->ue_hash, head);
186 free_entry(cache, new);
189 cfs_list_move(&entry->ue_hash, head);
193 /* acquire for new one */
194 if (UC_CACHE_IS_NEW(entry)) {
195 UC_CACHE_SET_ACQUIRING(entry);
196 UC_CACHE_CLEAR_NEW(entry);
197 cfs_spin_unlock(&cache->uc_lock);
198 rc = refresh_entry(cache, entry);
199 cfs_spin_lock(&cache->uc_lock);
200 entry->ue_acquire_expire =
201 cfs_time_shift(cache->uc_acquire_expire);
203 UC_CACHE_CLEAR_ACQUIRING(entry);
204 UC_CACHE_SET_INVALID(entry);
205 cfs_waitq_broadcast(&entry->ue_waitq);
206 if (unlikely(rc == -EREMCHG)) {
207 put_entry(cache, entry);
208 GOTO(out, entry = ERR_PTR(rc));
212 /* someone (and only one) is doing upcall upon this item,
213 * wait it to complete */
214 if (UC_CACHE_IS_ACQUIRING(entry)) {
215 long expiry = (entry == new) ?
216 cfs_time_seconds(cache->uc_acquire_expire) :
217 CFS_MAX_SCHEDULE_TIMEOUT;
220 cfs_waitlink_init(&wait);
221 cfs_waitq_add(&entry->ue_waitq, &wait);
222 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
223 cfs_spin_unlock(&cache->uc_lock);
225 left = cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE,
228 cfs_spin_lock(&cache->uc_lock);
229 cfs_waitq_del(&entry->ue_waitq, &wait);
230 if (UC_CACHE_IS_ACQUIRING(entry)) {
231 /* we're interrupted or upcall failed in the middle */
232 rc = left > 0 ? -EINTR : -ETIMEDOUT;
233 CERROR("acquire for key "LPU64": error %d\n",
235 put_entry(cache, entry);
236 GOTO(out, entry = ERR_PTR(rc));
240 /* invalid means error, don't need to try again */
241 if (UC_CACHE_IS_INVALID(entry)) {
242 put_entry(cache, entry);
243 GOTO(out, entry = ERR_PTR(-EIDRM));
247 * We can't refresh the existing one because some
248 * memory might be shared by multiple processes.
250 if (check_unlink_entry(cache, entry)) {
251 /* if expired, try again. but if this entry is
252 * created by me but too quickly turn to expired
253 * without any error, should at least give a
254 * chance to use it once.
257 put_entry(cache, entry);
258 cfs_spin_unlock(&cache->uc_lock);
264 /* Now we know it's good */
266 cfs_spin_unlock(&cache->uc_lock);
269 EXPORT_SYMBOL(upcall_cache_get_entry);
271 void upcall_cache_put_entry(struct upcall_cache *cache,
272 struct upcall_cache_entry *entry)
281 LASSERT(cfs_atomic_read(&entry->ue_refcount) > 0);
282 cfs_spin_lock(&cache->uc_lock);
283 put_entry(cache, entry);
284 cfs_spin_unlock(&cache->uc_lock);
287 EXPORT_SYMBOL(upcall_cache_put_entry);
289 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
292 struct upcall_cache_entry *entry = NULL;
294 int found = 0, rc = 0;
299 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
301 cfs_spin_lock(&cache->uc_lock);
302 cfs_list_for_each_entry(entry, head, ue_hash) {
303 if (downcall_compare(cache, entry, key, args) == 0) {
311 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
312 cache->uc_name, key);
313 /* haven't found, it's possible */
314 cfs_spin_unlock(&cache->uc_lock);
319 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
320 cache->uc_name, entry->ue_key, err);
321 GOTO(out, rc = -EINVAL);
324 if (!UC_CACHE_IS_ACQUIRING(entry)) {
325 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
326 cache->uc_name, entry, entry->ue_key);
330 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
331 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
332 cache->uc_name, entry, entry->ue_key);
333 GOTO(out, rc = -EINVAL);
336 cfs_spin_unlock(&cache->uc_lock);
337 if (cache->uc_ops->parse_downcall)
338 rc = cache->uc_ops->parse_downcall(cache, entry, args);
339 cfs_spin_lock(&cache->uc_lock);
343 entry->ue_expire = cfs_time_shift(cache->uc_entry_expire);
344 UC_CACHE_SET_VALID(entry);
345 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
346 cache->uc_name, entry, entry->ue_key);
349 UC_CACHE_SET_INVALID(entry);
350 cfs_list_del_init(&entry->ue_hash);
352 UC_CACHE_CLEAR_ACQUIRING(entry);
353 cfs_spin_unlock(&cache->uc_lock);
354 cfs_waitq_broadcast(&entry->ue_waitq);
355 put_entry(cache, entry);
359 EXPORT_SYMBOL(upcall_cache_downcall);
361 static void cache_flush(struct upcall_cache *cache, int force)
363 struct upcall_cache_entry *entry, *next;
367 cfs_spin_lock(&cache->uc_lock);
368 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
369 cfs_list_for_each_entry_safe(entry, next,
370 &cache->uc_hashtable[i], ue_hash) {
371 if (!force && cfs_atomic_read(&entry->ue_refcount)) {
372 UC_CACHE_SET_EXPIRED(entry);
375 LASSERT(!cfs_atomic_read(&entry->ue_refcount));
376 free_entry(cache, entry);
379 cfs_spin_unlock(&cache->uc_lock);
383 void upcall_cache_flush_idle(struct upcall_cache *cache)
385 cache_flush(cache, 0);
387 EXPORT_SYMBOL(upcall_cache_flush_idle);
389 void upcall_cache_flush_all(struct upcall_cache *cache)
391 cache_flush(cache, 1);
393 EXPORT_SYMBOL(upcall_cache_flush_all);
395 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
398 struct upcall_cache_entry *entry;
402 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
404 cfs_spin_lock(&cache->uc_lock);
405 cfs_list_for_each_entry(entry, head, ue_hash) {
406 if (upcall_compare(cache, entry, key, args) == 0) {
413 CWARN("%s: flush entry %p: key "LPU64", ref %d, fl %x, "
414 "cur %lu, ex %ld/%ld\n",
415 cache->uc_name, entry, entry->ue_key,
416 cfs_atomic_read(&entry->ue_refcount), entry->ue_flags,
417 cfs_time_current_sec(), entry->ue_acquire_expire,
419 UC_CACHE_SET_EXPIRED(entry);
420 if (!cfs_atomic_read(&entry->ue_refcount))
421 free_entry(cache, entry);
423 cfs_spin_unlock(&cache->uc_lock);
425 EXPORT_SYMBOL(upcall_cache_flush_one);
427 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
428 struct upcall_cache_ops *ops)
430 struct upcall_cache *cache;
434 LIBCFS_ALLOC(cache, sizeof(*cache));
436 RETURN(ERR_PTR(-ENOMEM));
438 cfs_spin_lock_init(&cache->uc_lock);
439 cfs_rwlock_init(&cache->uc_upcall_rwlock);
440 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
441 CFS_INIT_LIST_HEAD(&cache->uc_hashtable[i]);
442 strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
443 /* upcall pathname proc tunable */
444 strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
445 cache->uc_entry_expire = 20 * 60;
446 cache->uc_acquire_expire = 30;
451 EXPORT_SYMBOL(upcall_cache_init);
453 void upcall_cache_cleanup(struct upcall_cache *cache)
457 upcall_cache_flush_all(cache);
458 LIBCFS_FREE(cache, sizeof(*cache));
460 EXPORT_SYMBOL(upcall_cache_cleanup);