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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/obdclass/upcall_cache.c
33 * Supplementary groups cache.
35 #define DEBUG_SUBSYSTEM S_SEC
37 #include <libcfs/libcfs.h>
38 #include <uapi/linux/lnet/lnet-types.h>
39 #include <upcall_cache.h>
41 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
42 __u64 key, void *args)
44 struct upcall_cache_entry *entry;
46 LIBCFS_ALLOC(entry, sizeof(*entry));
50 UC_CACHE_SET_NEW(entry);
51 INIT_LIST_HEAD(&entry->ue_hash);
53 atomic_set(&entry->ue_refcount, 0);
54 init_waitqueue_head(&entry->ue_waitq);
55 entry->ue_acquire_expire = 0;
57 if (cache->uc_ops->init_entry)
58 cache->uc_ops->init_entry(entry, args);
62 /* protected by cache lock */
63 static void free_entry(struct upcall_cache *cache,
64 struct upcall_cache_entry *entry)
66 if (cache->uc_ops->free_entry)
67 cache->uc_ops->free_entry(cache, entry);
69 list_del(&entry->ue_hash);
70 CDEBUG(D_OTHER, "destroy cache entry %p for key %llu\n",
71 entry, entry->ue_key);
72 LIBCFS_FREE(entry, sizeof(*entry));
75 static inline int upcall_compare(struct upcall_cache *cache,
76 struct upcall_cache_entry *entry,
77 __u64 key, void *args)
79 if (entry->ue_key != key)
82 if (cache->uc_ops->upcall_compare)
83 return cache->uc_ops->upcall_compare(cache, entry, key, args);
88 static inline int downcall_compare(struct upcall_cache *cache,
89 struct upcall_cache_entry *entry,
90 __u64 key, void *args)
92 if (entry->ue_key != key)
95 if (cache->uc_ops->downcall_compare)
96 return cache->uc_ops->downcall_compare(cache, entry, key, args);
101 static inline void get_entry(struct upcall_cache_entry *entry)
103 atomic_inc(&entry->ue_refcount);
106 static inline void put_entry(struct upcall_cache *cache,
107 struct upcall_cache_entry *entry)
109 if (atomic_dec_and_test(&entry->ue_refcount) &&
110 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
111 free_entry(cache, entry);
115 static int check_unlink_entry(struct upcall_cache *cache,
116 struct upcall_cache_entry *entry)
118 time64_t now = ktime_get_seconds();
120 if (UC_CACHE_IS_VALID(entry) && now < entry->ue_expire)
123 if (UC_CACHE_IS_ACQUIRING(entry)) {
124 if (entry->ue_acquire_expire == 0 ||
125 now < entry->ue_acquire_expire)
128 UC_CACHE_SET_EXPIRED(entry);
129 wake_up(&entry->ue_waitq);
130 } else if (!UC_CACHE_IS_INVALID(entry)) {
131 UC_CACHE_SET_EXPIRED(entry);
134 list_del_init(&entry->ue_hash);
135 if (!atomic_read(&entry->ue_refcount))
136 free_entry(cache, entry);
140 static inline int refresh_entry(struct upcall_cache *cache,
141 struct upcall_cache_entry *entry)
143 LASSERT(cache->uc_ops->do_upcall);
144 return cache->uc_ops->do_upcall(cache, entry);
147 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
148 __u64 key, void *args)
150 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
151 bool failedacquiring = false;
152 struct list_head *head;
153 wait_queue_entry_t wait;
159 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
160 cache->uc_hashsize)];
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 = ktime_get_seconds() +
204 cache->uc_acquire_expire;
206 UC_CACHE_CLEAR_ACQUIRING(entry);
207 UC_CACHE_SET_INVALID(entry);
208 wake_up(&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;
224 add_wait_queue(&entry->ue_waitq, &wait);
225 set_current_state(TASK_INTERRUPTIBLE);
226 spin_unlock(&cache->uc_lock);
228 left = schedule_timeout(expiry);
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 = left > 0 ? -EINTR : -ETIMEDOUT;
235 /* if we waited uc_acquire_expire, we can try again
236 * with same data, but only if acquire is replayable
238 if (left <= 0 && !cache->uc_acquire_replay)
239 failedacquiring = true;
240 put_entry(cache, entry);
241 if (!failedacquiring) {
242 spin_unlock(&cache->uc_lock);
243 failedacquiring = true;
246 "retry acquire for key %llu (got %d)\n",
250 CERROR("acquire for key %llu: error %d\n",
252 GOTO(out, entry = ERR_PTR(rc));
256 /* invalid means error, don't need to try again */
257 if (UC_CACHE_IS_INVALID(entry)) {
258 put_entry(cache, entry);
259 GOTO(out, entry = ERR_PTR(-EIDRM));
263 * We can't refresh the existing one because some
264 * memory might be shared by multiple processes.
266 if (check_unlink_entry(cache, entry)) {
267 /* if expired, try again. but if this entry is
268 * created by me but too quickly turn to expired
269 * without any error, should at least give a
270 * chance to use it once.
273 put_entry(cache, entry);
274 spin_unlock(&cache->uc_lock);
280 /* Now we know it's good */
282 spin_unlock(&cache->uc_lock);
285 EXPORT_SYMBOL(upcall_cache_get_entry);
287 void upcall_cache_get_entry_raw(struct upcall_cache_entry *entry)
291 EXPORT_SYMBOL(upcall_cache_get_entry_raw);
293 void upcall_cache_update_entry(struct upcall_cache *cache,
294 struct upcall_cache_entry *entry,
295 time64_t expire, int state)
297 spin_lock(&cache->uc_lock);
298 entry->ue_expire = expire;
300 UC_CACHE_SET_VALID(entry);
302 entry->ue_flags |= state;
303 spin_unlock(&cache->uc_lock);
305 EXPORT_SYMBOL(upcall_cache_update_entry);
307 void upcall_cache_put_entry(struct upcall_cache *cache,
308 struct upcall_cache_entry *entry)
317 LASSERT(atomic_read(&entry->ue_refcount) > 0);
318 spin_lock(&cache->uc_lock);
319 put_entry(cache, entry);
320 spin_unlock(&cache->uc_lock);
323 EXPORT_SYMBOL(upcall_cache_put_entry);
325 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
328 struct upcall_cache_entry *entry = NULL;
329 struct list_head *head;
330 int found = 0, rc = 0;
335 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
336 cache->uc_hashsize)];
338 spin_lock(&cache->uc_lock);
339 list_for_each_entry(entry, head, ue_hash) {
340 if (downcall_compare(cache, entry, key, args) == 0) {
348 CDEBUG(D_OTHER, "%s: upcall for key %llu not expected\n",
349 cache->uc_name, key);
350 /* haven't found, it's possible */
351 spin_unlock(&cache->uc_lock);
356 CDEBUG(D_OTHER, "%s: upcall for key %llu returned %d\n",
357 cache->uc_name, entry->ue_key, err);
361 if (!UC_CACHE_IS_ACQUIRING(entry)) {
362 CDEBUG(D_RPCTRACE, "%s: found uptodate entry %p (key %llu)"
363 "\n", cache->uc_name, entry, entry->ue_key);
367 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
368 CERROR("%s: found a stale entry %p (key %llu) in ioctl\n",
369 cache->uc_name, entry, entry->ue_key);
370 GOTO(out, rc = -EINVAL);
373 spin_unlock(&cache->uc_lock);
374 if (cache->uc_ops->parse_downcall)
375 rc = cache->uc_ops->parse_downcall(cache, entry, args);
376 spin_lock(&cache->uc_lock);
380 if (!entry->ue_expire)
381 entry->ue_expire = ktime_get_seconds() + cache->uc_entry_expire;
382 UC_CACHE_SET_VALID(entry);
383 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key %llu\n",
384 cache->uc_name, entry, entry->ue_key);
387 UC_CACHE_SET_INVALID(entry);
388 list_del_init(&entry->ue_hash);
390 UC_CACHE_CLEAR_ACQUIRING(entry);
391 spin_unlock(&cache->uc_lock);
392 wake_up(&entry->ue_waitq);
393 put_entry(cache, entry);
397 EXPORT_SYMBOL(upcall_cache_downcall);
399 void upcall_cache_flush(struct upcall_cache *cache, int force)
401 struct upcall_cache_entry *entry, *next;
405 spin_lock(&cache->uc_lock);
406 for (i = 0; i < cache->uc_hashsize; i++) {
407 list_for_each_entry_safe(entry, next,
408 &cache->uc_hashtable[i], ue_hash) {
409 if (!force && atomic_read(&entry->ue_refcount)) {
410 UC_CACHE_SET_EXPIRED(entry);
413 LASSERT(!atomic_read(&entry->ue_refcount));
414 free_entry(cache, entry);
417 spin_unlock(&cache->uc_lock);
420 EXPORT_SYMBOL(upcall_cache_flush);
422 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
424 struct list_head *head;
425 struct upcall_cache_entry *entry;
429 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
430 cache->uc_hashsize)];
432 spin_lock(&cache->uc_lock);
433 list_for_each_entry(entry, head, ue_hash) {
434 if (upcall_compare(cache, entry, key, args) == 0) {
441 CWARN("%s: flush entry %p: key %llu, ref %d, fl %x, "
442 "cur %lld, ex %lld/%lld\n",
443 cache->uc_name, entry, entry->ue_key,
444 atomic_read(&entry->ue_refcount), entry->ue_flags,
445 ktime_get_real_seconds(), entry->ue_acquire_expire,
447 UC_CACHE_SET_EXPIRED(entry);
448 if (!atomic_read(&entry->ue_refcount))
449 free_entry(cache, entry);
451 spin_unlock(&cache->uc_lock);
453 EXPORT_SYMBOL(upcall_cache_flush_one);
455 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
456 int hashsz, time64_t entry_expire,
457 time64_t acquire_expire, bool replayable,
458 struct upcall_cache_ops *ops)
460 struct upcall_cache *cache;
464 LIBCFS_ALLOC(cache, sizeof(*cache));
466 RETURN(ERR_PTR(-ENOMEM));
468 spin_lock_init(&cache->uc_lock);
469 init_rwsem(&cache->uc_upcall_rwsem);
470 cache->uc_hashsize = hashsz;
471 LIBCFS_ALLOC(cache->uc_hashtable,
472 sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
473 if (!cache->uc_hashtable)
474 RETURN(ERR_PTR(-ENOMEM));
475 for (i = 0; i < cache->uc_hashsize; i++)
476 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
477 strlcpy(cache->uc_name, name, sizeof(cache->uc_name));
478 /* upcall pathname proc tunable */
479 strlcpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall));
480 cache->uc_entry_expire = entry_expire;
481 cache->uc_acquire_expire = acquire_expire;
482 cache->uc_acquire_replay = replayable;
487 EXPORT_SYMBOL(upcall_cache_init);
489 void upcall_cache_cleanup(struct upcall_cache *cache)
493 upcall_cache_flush_all(cache);
494 LIBCFS_FREE(cache->uc_hashtable,
495 sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
496 LIBCFS_FREE(cache, sizeof(*cache));
498 EXPORT_SYMBOL(upcall_cache_cleanup);