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 * lustre/obdclass/upcall_cache.c
38 * Supplementary groups cache.
40 #define DEBUG_SUBSYSTEM S_SEC
42 #include <libcfs/libcfs.h>
43 #include <lnet/types.h>
44 #include <upcall_cache.h>
46 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
47 __u64 key, void *args)
49 struct upcall_cache_entry *entry;
51 LIBCFS_ALLOC(entry, sizeof(*entry));
55 UC_CACHE_SET_NEW(entry);
56 INIT_LIST_HEAD(&entry->ue_hash);
58 atomic_set(&entry->ue_refcount, 0);
59 init_waitqueue_head(&entry->ue_waitq);
60 if (cache->uc_ops->init_entry)
61 cache->uc_ops->init_entry(entry, args);
65 /* protected by cache lock */
66 static void free_entry(struct upcall_cache *cache,
67 struct upcall_cache_entry *entry)
69 if (cache->uc_ops->free_entry)
70 cache->uc_ops->free_entry(cache, entry);
72 list_del(&entry->ue_hash);
73 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
74 entry, entry->ue_key);
75 LIBCFS_FREE(entry, sizeof(*entry));
78 static inline int upcall_compare(struct upcall_cache *cache,
79 struct upcall_cache_entry *entry,
80 __u64 key, void *args)
82 if (entry->ue_key != key)
85 if (cache->uc_ops->upcall_compare)
86 return cache->uc_ops->upcall_compare(cache, entry, key, args);
91 static inline int downcall_compare(struct upcall_cache *cache,
92 struct upcall_cache_entry *entry,
93 __u64 key, void *args)
95 if (entry->ue_key != key)
98 if (cache->uc_ops->downcall_compare)
99 return cache->uc_ops->downcall_compare(cache, entry, key, args);
104 static inline void get_entry(struct upcall_cache_entry *entry)
106 atomic_inc(&entry->ue_refcount);
109 static inline void put_entry(struct upcall_cache *cache,
110 struct upcall_cache_entry *entry)
112 if (atomic_dec_and_test(&entry->ue_refcount) &&
113 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
114 free_entry(cache, entry);
118 static int check_unlink_entry(struct upcall_cache *cache,
119 struct upcall_cache_entry *entry)
121 if (UC_CACHE_IS_VALID(entry) &&
122 cfs_time_before(cfs_time_current(), entry->ue_expire))
125 if (UC_CACHE_IS_ACQUIRING(entry)) {
126 if (entry->ue_acquire_expire == 0 ||
127 cfs_time_before(cfs_time_current(),
128 entry->ue_acquire_expire))
131 UC_CACHE_SET_EXPIRED(entry);
132 wake_up_all(&entry->ue_waitq);
133 } else if (!UC_CACHE_IS_INVALID(entry)) {
134 UC_CACHE_SET_EXPIRED(entry);
137 list_del_init(&entry->ue_hash);
138 if (!atomic_read(&entry->ue_refcount))
139 free_entry(cache, entry);
143 static inline int refresh_entry(struct upcall_cache *cache,
144 struct upcall_cache_entry *entry)
146 LASSERT(cache->uc_ops->do_upcall);
147 return cache->uc_ops->do_upcall(cache, entry);
150 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
151 __u64 key, void *args)
153 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
154 struct list_head *head;
161 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
164 spin_lock(&cache->uc_lock);
165 list_for_each_entry_safe(entry, next, head, ue_hash) {
166 /* check invalid & expired items */
167 if (check_unlink_entry(cache, entry))
169 if (upcall_compare(cache, entry, key, args) == 0) {
177 spin_unlock(&cache->uc_lock);
178 new = alloc_entry(cache, key, args);
180 CERROR("fail to alloc entry\n");
181 RETURN(ERR_PTR(-ENOMEM));
185 list_add(&new->ue_hash, head);
190 free_entry(cache, new);
193 list_move(&entry->ue_hash, head);
197 /* acquire for new one */
198 if (UC_CACHE_IS_NEW(entry)) {
199 UC_CACHE_SET_ACQUIRING(entry);
200 UC_CACHE_CLEAR_NEW(entry);
201 spin_unlock(&cache->uc_lock);
202 rc = refresh_entry(cache, entry);
203 spin_lock(&cache->uc_lock);
204 entry->ue_acquire_expire =
205 cfs_time_shift(cache->uc_acquire_expire);
207 UC_CACHE_CLEAR_ACQUIRING(entry);
208 UC_CACHE_SET_INVALID(entry);
209 wake_up_all(&entry->ue_waitq);
210 if (unlikely(rc == -EREMCHG)) {
211 put_entry(cache, entry);
212 GOTO(out, entry = ERR_PTR(rc));
216 /* someone (and only one) is doing upcall upon this item,
217 * wait it to complete */
218 if (UC_CACHE_IS_ACQUIRING(entry)) {
219 long expiry = (entry == new) ?
220 cfs_time_seconds(cache->uc_acquire_expire) :
221 MAX_SCHEDULE_TIMEOUT;
224 init_waitqueue_entry_current(&wait);
225 add_wait_queue(&entry->ue_waitq, &wait);
226 set_current_state(TASK_INTERRUPTIBLE);
227 spin_unlock(&cache->uc_lock);
229 left = waitq_timedwait(&wait, TASK_INTERRUPTIBLE,
232 spin_lock(&cache->uc_lock);
233 remove_wait_queue(&entry->ue_waitq, &wait);
234 if (UC_CACHE_IS_ACQUIRING(entry)) {
235 /* we're interrupted or upcall failed in the middle */
236 rc = left > 0 ? -EINTR : -ETIMEDOUT;
237 CERROR("acquire for key "LPU64": error %d\n",
239 put_entry(cache, entry);
240 GOTO(out, entry = ERR_PTR(rc));
244 /* invalid means error, don't need to try again */
245 if (UC_CACHE_IS_INVALID(entry)) {
246 put_entry(cache, entry);
247 GOTO(out, entry = ERR_PTR(-EIDRM));
251 * We can't refresh the existing one because some
252 * memory might be shared by multiple processes.
254 if (check_unlink_entry(cache, entry)) {
255 /* if expired, try again. but if this entry is
256 * created by me but too quickly turn to expired
257 * without any error, should at least give a
258 * chance to use it once.
261 put_entry(cache, entry);
262 spin_unlock(&cache->uc_lock);
268 /* Now we know it's good */
270 spin_unlock(&cache->uc_lock);
273 EXPORT_SYMBOL(upcall_cache_get_entry);
275 void upcall_cache_put_entry(struct upcall_cache *cache,
276 struct upcall_cache_entry *entry)
285 LASSERT(atomic_read(&entry->ue_refcount) > 0);
286 spin_lock(&cache->uc_lock);
287 put_entry(cache, entry);
288 spin_unlock(&cache->uc_lock);
291 EXPORT_SYMBOL(upcall_cache_put_entry);
293 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
296 struct upcall_cache_entry *entry = NULL;
297 struct list_head *head;
298 int found = 0, rc = 0;
303 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
305 spin_lock(&cache->uc_lock);
306 list_for_each_entry(entry, head, ue_hash) {
307 if (downcall_compare(cache, entry, key, args) == 0) {
315 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
316 cache->uc_name, key);
317 /* haven't found, it's possible */
318 spin_unlock(&cache->uc_lock);
323 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
324 cache->uc_name, entry->ue_key, err);
325 GOTO(out, rc = -EINVAL);
328 if (!UC_CACHE_IS_ACQUIRING(entry)) {
329 CDEBUG(D_RPCTRACE, "%s: found uptodate entry %p (key "LPU64")"
330 "\n", cache->uc_name, entry, entry->ue_key);
334 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
335 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
336 cache->uc_name, entry, entry->ue_key);
337 GOTO(out, rc = -EINVAL);
340 spin_unlock(&cache->uc_lock);
341 if (cache->uc_ops->parse_downcall)
342 rc = cache->uc_ops->parse_downcall(cache, entry, args);
343 spin_lock(&cache->uc_lock);
347 entry->ue_expire = cfs_time_shift(cache->uc_entry_expire);
348 UC_CACHE_SET_VALID(entry);
349 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
350 cache->uc_name, entry, entry->ue_key);
353 UC_CACHE_SET_INVALID(entry);
354 list_del_init(&entry->ue_hash);
356 UC_CACHE_CLEAR_ACQUIRING(entry);
357 spin_unlock(&cache->uc_lock);
358 wake_up_all(&entry->ue_waitq);
359 put_entry(cache, entry);
363 EXPORT_SYMBOL(upcall_cache_downcall);
365 void upcall_cache_flush(struct upcall_cache *cache, int force)
367 struct upcall_cache_entry *entry, *next;
371 spin_lock(&cache->uc_lock);
372 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
373 list_for_each_entry_safe(entry, next,
374 &cache->uc_hashtable[i], ue_hash) {
375 if (!force && atomic_read(&entry->ue_refcount)) {
376 UC_CACHE_SET_EXPIRED(entry);
379 LASSERT(!atomic_read(&entry->ue_refcount));
380 free_entry(cache, entry);
383 spin_unlock(&cache->uc_lock);
386 EXPORT_SYMBOL(upcall_cache_flush);
388 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
390 struct list_head *head;
391 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;
427 LIBCFS_ALLOC(cache, sizeof(*cache));
429 RETURN(ERR_PTR(-ENOMEM));
431 spin_lock_init(&cache->uc_lock);
432 rwlock_init(&cache->uc_upcall_rwlock);
433 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
434 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
435 strlcpy(cache->uc_name, name, sizeof(cache->uc_name));
436 /* upcall pathname proc tunable */
437 strlcpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall));
438 cache->uc_entry_expire = 20 * 60;
439 cache->uc_acquire_expire = 30;
444 EXPORT_SYMBOL(upcall_cache_init);
446 void upcall_cache_cleanup(struct upcall_cache *cache)
450 upcall_cache_flush_all(cache);
451 LIBCFS_FREE(cache, sizeof(*cache));
453 EXPORT_SYMBOL(upcall_cache_cleanup);