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, 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 <linux/libcfs/lucache.h>
44 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
45 __u64 key, void *args)
47 struct upcall_cache_entry *entry;
49 LIBCFS_ALLOC(entry, sizeof(*entry));
53 UC_CACHE_SET_NEW(entry);
54 INIT_LIST_HEAD(&entry->ue_hash);
56 atomic_set(&entry->ue_refcount, 0);
57 init_waitqueue_head(&entry->ue_waitq);
58 if (cache->uc_ops->init_entry)
59 cache->uc_ops->init_entry(entry, args);
63 /* protected by cache lock */
64 static void free_entry(struct upcall_cache *cache,
65 struct upcall_cache_entry *entry)
67 if (cache->uc_ops->free_entry)
68 cache->uc_ops->free_entry(cache, entry);
70 list_del(&entry->ue_hash);
71 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
72 entry, entry->ue_key);
73 LIBCFS_FREE(entry, sizeof(*entry));
76 static inline int upcall_compare(struct upcall_cache *cache,
77 struct upcall_cache_entry *entry,
78 __u64 key, void *args)
80 if (entry->ue_key != key)
83 if (cache->uc_ops->upcall_compare)
84 return cache->uc_ops->upcall_compare(cache, entry, key, args);
89 static inline int downcall_compare(struct upcall_cache *cache,
90 struct upcall_cache_entry *entry,
91 __u64 key, void *args)
93 if (entry->ue_key != key)
96 if (cache->uc_ops->downcall_compare)
97 return cache->uc_ops->downcall_compare(cache, entry, key, args);
102 static inline void get_entry(struct upcall_cache_entry *entry)
104 atomic_inc(&entry->ue_refcount);
107 static inline void put_entry(struct upcall_cache *cache,
108 struct upcall_cache_entry *entry)
110 if (atomic_dec_and_test(&entry->ue_refcount) &&
111 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
112 free_entry(cache, entry);
116 static int check_unlink_entry(struct upcall_cache *cache,
117 struct upcall_cache_entry *entry)
119 if (UC_CACHE_IS_VALID(entry) &&
120 cfs_time_before(cfs_time_current(), entry->ue_expire))
123 if (UC_CACHE_IS_ACQUIRING(entry)) {
124 if (entry->ue_acquire_expire == 0 ||
125 cfs_time_before(cfs_time_current(),
126 entry->ue_acquire_expire))
129 UC_CACHE_SET_EXPIRED(entry);
130 wake_up_all(&entry->ue_waitq);
131 } else if (!UC_CACHE_IS_INVALID(entry)) {
132 UC_CACHE_SET_EXPIRED(entry);
135 list_del_init(&entry->ue_hash);
136 if (!atomic_read(&entry->ue_refcount))
137 free_entry(cache, entry);
141 static inline int refresh_entry(struct upcall_cache *cache,
142 struct upcall_cache_entry *entry)
144 LASSERT(cache->uc_ops->do_upcall);
145 return cache->uc_ops->do_upcall(cache, entry);
148 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
149 __u64 key, void *args)
151 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
152 struct list_head *head;
158 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
161 spin_lock(&cache->uc_lock);
162 list_for_each_entry_safe(entry, next, head, ue_hash) {
163 /* check invalid & expired items */
164 if (check_unlink_entry(cache, entry))
166 if (upcall_compare(cache, entry, key, args) == 0) {
174 spin_unlock(&cache->uc_lock);
175 new = alloc_entry(cache, key, args);
177 CERROR("fail to alloc entry\n");
178 return ERR_PTR(-ENOMEM);
182 list_add(&new->ue_hash, head);
187 free_entry(cache, new);
190 list_move(&entry->ue_hash, head);
194 /* acquire for new one */
195 if (UC_CACHE_IS_NEW(entry)) {
196 UC_CACHE_SET_ACQUIRING(entry);
197 UC_CACHE_CLEAR_NEW(entry);
198 spin_unlock(&cache->uc_lock);
199 rc = refresh_entry(cache, entry);
200 spin_lock(&cache->uc_lock);
201 entry->ue_acquire_expire =
202 cfs_time_shift(cache->uc_acquire_expire);
204 UC_CACHE_CLEAR_ACQUIRING(entry);
205 UC_CACHE_SET_INVALID(entry);
206 wake_up_all(&entry->ue_waitq);
207 if (unlikely(rc == -EREMCHG)) {
208 put_entry(cache, entry);
209 GOTO(out, entry = ERR_PTR(rc));
213 /* someone (and only one) is doing upcall upon this item,
214 * wait it to complete */
215 if (UC_CACHE_IS_ACQUIRING(entry)) {
216 long expiry = (entry == new) ?
217 cfs_time_seconds(cache->uc_acquire_expire) :
218 MAX_SCHEDULE_TIMEOUT;
221 init_waitqueue_entry_current(&wait);
222 add_wait_queue(&entry->ue_waitq, &wait);
223 set_current_state(TASK_INTERRUPTIBLE);
224 spin_unlock(&cache->uc_lock);
226 left = waitq_timedwait(&wait, TASK_INTERRUPTIBLE,
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 = left > 0 ? -EINTR : -ETIMEDOUT;
234 CERROR("acquire for key "LPU64": error %d\n",
236 put_entry(cache, entry);
237 GOTO(out, entry = ERR_PTR(rc));
241 /* invalid means error, don't need to try again */
242 if (UC_CACHE_IS_INVALID(entry)) {
243 put_entry(cache, entry);
244 GOTO(out, entry = ERR_PTR(-EIDRM));
248 * We can't refresh the existing one because some
249 * memory might be shared by multiple processes.
251 if (check_unlink_entry(cache, entry)) {
252 /* if expired, try again. but if this entry is
253 * created by me but too quickly turn to expired
254 * without any error, should at least give a
255 * chance to use it once.
258 put_entry(cache, entry);
259 spin_unlock(&cache->uc_lock);
265 /* Now we know it's good */
267 spin_unlock(&cache->uc_lock);
270 EXPORT_SYMBOL(upcall_cache_get_entry);
272 void upcall_cache_put_entry(struct upcall_cache *cache,
273 struct upcall_cache_entry *entry)
279 LASSERT(atomic_read(&entry->ue_refcount) > 0);
280 spin_lock(&cache->uc_lock);
281 put_entry(cache, entry);
282 spin_unlock(&cache->uc_lock);
284 EXPORT_SYMBOL(upcall_cache_put_entry);
286 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
289 struct upcall_cache_entry *entry = NULL;
290 struct list_head *head;
291 int found = 0, rc = 0;
295 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
297 spin_lock(&cache->uc_lock);
298 list_for_each_entry(entry, head, ue_hash) {
299 if (downcall_compare(cache, entry, key, args) == 0) {
307 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
308 cache->uc_name, key);
309 /* haven't found, it's possible */
310 spin_unlock(&cache->uc_lock);
315 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
316 cache->uc_name, entry->ue_key, err);
317 GOTO(out, rc = -EINVAL);
320 if (!UC_CACHE_IS_ACQUIRING(entry)) {
321 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
322 cache->uc_name, entry, entry->ue_key);
326 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
327 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
328 cache->uc_name, entry, entry->ue_key);
329 GOTO(out, rc = -EINVAL);
332 spin_unlock(&cache->uc_lock);
333 if (cache->uc_ops->parse_downcall)
334 rc = cache->uc_ops->parse_downcall(cache, entry, args);
335 spin_lock(&cache->uc_lock);
339 entry->ue_expire = cfs_time_shift(cache->uc_entry_expire);
340 UC_CACHE_SET_VALID(entry);
341 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
342 cache->uc_name, entry, entry->ue_key);
345 UC_CACHE_SET_INVALID(entry);
346 list_del_init(&entry->ue_hash);
348 UC_CACHE_CLEAR_ACQUIRING(entry);
349 spin_unlock(&cache->uc_lock);
350 wake_up_all(&entry->ue_waitq);
351 put_entry(cache, entry);
355 EXPORT_SYMBOL(upcall_cache_downcall);
357 static void cache_flush(struct upcall_cache *cache, int force)
359 struct upcall_cache_entry *entry, *next;
362 spin_lock(&cache->uc_lock);
363 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
364 list_for_each_entry_safe(entry, next,
365 &cache->uc_hashtable[i], ue_hash) {
366 if (!force && atomic_read(&entry->ue_refcount)) {
367 UC_CACHE_SET_EXPIRED(entry);
370 LASSERT(!atomic_read(&entry->ue_refcount));
371 free_entry(cache, entry);
374 spin_unlock(&cache->uc_lock);
377 void upcall_cache_flush_idle(struct upcall_cache *cache)
379 cache_flush(cache, 0);
381 EXPORT_SYMBOL(upcall_cache_flush_idle);
383 void upcall_cache_flush_all(struct upcall_cache *cache)
385 cache_flush(cache, 1);
387 EXPORT_SYMBOL(upcall_cache_flush_all);
389 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
391 struct list_head *head;
392 struct upcall_cache_entry *entry;
395 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
397 spin_lock(&cache->uc_lock);
398 list_for_each_entry(entry, head, ue_hash) {
399 if (upcall_compare(cache, entry, key, args) == 0) {
406 CWARN("%s: flush entry %p: key "LPU64", ref %d, fl %x, "
407 "cur %lu, ex %ld/%ld\n",
408 cache->uc_name, entry, entry->ue_key,
409 atomic_read(&entry->ue_refcount), entry->ue_flags,
410 cfs_time_current_sec(), entry->ue_acquire_expire,
412 UC_CACHE_SET_EXPIRED(entry);
413 if (!atomic_read(&entry->ue_refcount))
414 free_entry(cache, entry);
416 spin_unlock(&cache->uc_lock);
418 EXPORT_SYMBOL(upcall_cache_flush_one);
420 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
421 struct upcall_cache_ops *ops)
423 struct upcall_cache *cache;
426 LIBCFS_ALLOC(cache, sizeof(*cache));
428 return ERR_PTR(-ENOMEM);
430 spin_lock_init(&cache->uc_lock);
431 rwlock_init(&cache->uc_upcall_rwlock);
432 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
433 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
434 strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
435 /* upcall pathname proc tunable */
436 strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
437 cache->uc_entry_expire = 20 * 60;
438 cache->uc_acquire_expire = 30;
443 EXPORT_SYMBOL(upcall_cache_init);
445 void upcall_cache_cleanup(struct upcall_cache *cache)
449 upcall_cache_flush_all(cache);
450 LIBCFS_FREE(cache, sizeof(*cache));
452 EXPORT_SYMBOL(upcall_cache_cleanup);