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.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/upcall_cache.c
38 * Supplementary groups cache.
40 #define DEBUG_SUBSYSTEM S_SEC
42 #include <libcfs/lucache.h>
43 #include <lnet/types.h>
45 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
46 __u64 key, void *args)
48 struct upcall_cache_entry *entry;
50 LIBCFS_ALLOC(entry, sizeof(*entry));
54 UC_CACHE_SET_NEW(entry);
55 INIT_LIST_HEAD(&entry->ue_hash);
57 atomic_set(&entry->ue_refcount, 0);
58 init_waitqueue_head(&entry->ue_waitq);
59 if (cache->uc_ops->init_entry)
60 cache->uc_ops->init_entry(entry, args);
64 /* protected by cache lock */
65 static void free_entry(struct upcall_cache *cache,
66 struct upcall_cache_entry *entry)
68 if (cache->uc_ops->free_entry)
69 cache->uc_ops->free_entry(cache, entry);
71 list_del(&entry->ue_hash);
72 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
73 entry, entry->ue_key);
74 LIBCFS_FREE(entry, sizeof(*entry));
77 static inline int upcall_compare(struct upcall_cache *cache,
78 struct upcall_cache_entry *entry,
79 __u64 key, void *args)
81 if (entry->ue_key != key)
84 if (cache->uc_ops->upcall_compare)
85 return cache->uc_ops->upcall_compare(cache, entry, key, args);
90 static inline int downcall_compare(struct upcall_cache *cache,
91 struct upcall_cache_entry *entry,
92 __u64 key, void *args)
94 if (entry->ue_key != key)
97 if (cache->uc_ops->downcall_compare)
98 return cache->uc_ops->downcall_compare(cache, entry, key, args);
103 static inline void get_entry(struct upcall_cache_entry *entry)
105 atomic_inc(&entry->ue_refcount);
108 static inline void put_entry(struct upcall_cache *cache,
109 struct upcall_cache_entry *entry)
111 if (atomic_dec_and_test(&entry->ue_refcount) &&
112 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
113 free_entry(cache, entry);
117 static int check_unlink_entry(struct upcall_cache *cache,
118 struct upcall_cache_entry *entry)
120 if (UC_CACHE_IS_VALID(entry) &&
121 cfs_time_before(cfs_time_current(), entry->ue_expire))
124 if (UC_CACHE_IS_ACQUIRING(entry)) {
125 if (entry->ue_acquire_expire == 0 ||
126 cfs_time_before(cfs_time_current(),
127 entry->ue_acquire_expire))
130 UC_CACHE_SET_EXPIRED(entry);
131 wake_up_all(&entry->ue_waitq);
132 } else if (!UC_CACHE_IS_INVALID(entry)) {
133 UC_CACHE_SET_EXPIRED(entry);
136 list_del_init(&entry->ue_hash);
137 if (!atomic_read(&entry->ue_refcount))
138 free_entry(cache, entry);
142 static inline int refresh_entry(struct upcall_cache *cache,
143 struct upcall_cache_entry *entry)
145 LASSERT(cache->uc_ops->do_upcall);
146 return cache->uc_ops->do_upcall(cache, entry);
149 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
150 __u64 key, void *args)
152 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
153 struct list_head *head;
160 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
163 spin_lock(&cache->uc_lock);
164 list_for_each_entry_safe(entry, next, head, ue_hash) {
165 /* check invalid & expired items */
166 if (check_unlink_entry(cache, entry))
168 if (upcall_compare(cache, entry, key, args) == 0) {
176 spin_unlock(&cache->uc_lock);
177 new = alloc_entry(cache, key, args);
179 CERROR("fail to alloc entry\n");
180 RETURN(ERR_PTR(-ENOMEM));
184 list_add(&new->ue_hash, head);
189 free_entry(cache, new);
192 list_move(&entry->ue_hash, head);
196 /* acquire for new one */
197 if (UC_CACHE_IS_NEW(entry)) {
198 UC_CACHE_SET_ACQUIRING(entry);
199 UC_CACHE_CLEAR_NEW(entry);
200 spin_unlock(&cache->uc_lock);
201 rc = refresh_entry(cache, entry);
202 spin_lock(&cache->uc_lock);
203 entry->ue_acquire_expire =
204 cfs_time_shift(cache->uc_acquire_expire);
206 UC_CACHE_CLEAR_ACQUIRING(entry);
207 UC_CACHE_SET_INVALID(entry);
208 wake_up_all(&entry->ue_waitq);
209 if (unlikely(rc == -EREMCHG)) {
210 put_entry(cache, entry);
211 GOTO(out, entry = ERR_PTR(rc));
215 /* someone (and only one) is doing upcall upon this item,
216 * wait it to complete */
217 if (UC_CACHE_IS_ACQUIRING(entry)) {
218 long expiry = (entry == new) ?
219 cfs_time_seconds(cache->uc_acquire_expire) :
220 MAX_SCHEDULE_TIMEOUT;
223 init_waitqueue_entry_current(&wait);
224 add_wait_queue(&entry->ue_waitq, &wait);
225 set_current_state(TASK_INTERRUPTIBLE);
226 spin_unlock(&cache->uc_lock);
228 left = waitq_timedwait(&wait, TASK_INTERRUPTIBLE,
231 spin_lock(&cache->uc_lock);
232 remove_wait_queue(&entry->ue_waitq, &wait);
233 if (UC_CACHE_IS_ACQUIRING(entry)) {
234 /* we're interrupted or upcall failed in the middle */
235 rc = left > 0 ? -EINTR : -ETIMEDOUT;
236 CERROR("acquire for key "LPU64": error %d\n",
238 put_entry(cache, entry);
239 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 = cfs_time_shift(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 cfs_time_current_sec(), 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 LIBCFS_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 strlcpy(cache->uc_name, name, sizeof(cache->uc_name));
446 /* upcall pathname proc tunable */
447 strlcpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall));
448 cache->uc_entry_expire = 20 * 60;
449 cache->uc_acquire_expire = 30;
454 EXPORT_SYMBOL(upcall_cache_init);
456 void upcall_cache_cleanup(struct upcall_cache *cache)
460 upcall_cache_flush_all(cache);
461 LIBCFS_FREE(cache, sizeof(*cache));
463 EXPORT_SYMBOL(upcall_cache_cleanup);