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 inline void write_lock_from_read(rwlock_t *lock, bool *writelock)
124 static int check_unlink_entry(struct upcall_cache *cache,
125 struct upcall_cache_entry *entry,
128 time64_t now = ktime_get_seconds();
130 if (UC_CACHE_IS_VALID(entry) && now < entry->ue_expire)
133 if (UC_CACHE_IS_ACQUIRING(entry)) {
134 if (entry->ue_acquire_expire == 0 ||
135 now < entry->ue_acquire_expire)
139 UC_CACHE_SET_EXPIRED(entry);
140 wake_up(&entry->ue_waitq);
142 } else if (!UC_CACHE_IS_INVALID(entry) && writelock) {
143 UC_CACHE_SET_EXPIRED(entry);
147 list_del_init(&entry->ue_hash);
148 if (!atomic_read(&entry->ue_refcount))
149 free_entry(cache, entry);
154 int upcall_cache_set_upcall(struct upcall_cache *cache, const char *buffer,
155 size_t count, bool path_only)
159 if (count >= UC_CACHE_UPCALL_MAXPATH)
162 OBD_ALLOC(upcall, count + 1);
166 /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
167 if (sscanf(buffer, "%s", upcall) != 1)
170 if (upcall[0] == '/')
176 if (strcasecmp(upcall, "NONE") == 0) {
177 snprintf(upcall, count + 1, "NONE");
182 OBD_FREE(upcall, count + 1);
186 down_write(&cache->uc_upcall_rwsem);
187 strcpy(cache->uc_upcall, upcall);
188 up_write(&cache->uc_upcall_rwsem);
190 OBD_FREE(upcall, count + 1);
193 EXPORT_SYMBOL(upcall_cache_set_upcall);
195 static inline int refresh_entry(struct upcall_cache *cache,
196 struct upcall_cache_entry *entry)
198 LASSERT(cache->uc_ops->do_upcall);
199 return cache->uc_ops->do_upcall(cache, entry);
202 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
203 __u64 key, void *args)
205 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
206 bool failedacquiring = false;
207 struct list_head *head;
208 wait_queue_entry_t wait;
216 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
217 cache->uc_hashsize)];
221 write_lock(&cache->uc_lock);
224 read_lock(&cache->uc_lock);
228 list_for_each_entry_safe(entry, next, head, ue_hash) {
229 /* check invalid & expired items */
230 if (check_unlink_entry(cache, entry, writelock))
232 if (upcall_compare(cache, entry, key, args) == 0) {
241 write_unlock(&cache->uc_lock);
243 read_unlock(&cache->uc_lock);
244 new = alloc_entry(cache, key, args);
246 CERROR("%s: fail to alloc entry: rc = %d\n",
247 cache->uc_name, -ENOMEM);
248 RETURN(ERR_PTR(-ENOMEM));
252 list_add(&new->ue_hash, head);
257 free_entry(cache, new);
259 } else if (!writelock) {
260 /* We found an entry while holding the read lock, so
261 * convert it to a write lock and find again, to check
262 * that entry was not modified/freed in between.
264 write_lock_from_read(&cache->uc_lock, &writelock);
268 list_move(&entry->ue_hash, head);
270 /* now we hold a write lock */
273 /* acquire for new one */
274 if (UC_CACHE_IS_NEW(entry)) {
275 UC_CACHE_SET_ACQUIRING(entry);
276 UC_CACHE_CLEAR_NEW(entry);
277 write_unlock(&cache->uc_lock);
278 rc = refresh_entry(cache, entry);
279 write_lock(&cache->uc_lock);
280 entry->ue_acquire_expire = ktime_get_seconds() +
281 cache->uc_acquire_expire;
283 UC_CACHE_CLEAR_ACQUIRING(entry);
284 UC_CACHE_SET_INVALID(entry);
285 wake_up(&entry->ue_waitq);
286 if (unlikely(rc == -EREMCHG)) {
287 put_entry(cache, entry);
288 GOTO(out, entry = ERR_PTR(rc));
292 /* someone (and only one) is doing upcall upon this item,
293 * wait it to complete */
294 if (UC_CACHE_IS_ACQUIRING(entry)) {
295 long expiry = (entry == new) ?
296 cfs_time_seconds(cache->uc_acquire_expire) :
297 MAX_SCHEDULE_TIMEOUT;
301 add_wait_queue(&entry->ue_waitq, &wait);
302 set_current_state(TASK_INTERRUPTIBLE);
303 write_unlock(&cache->uc_lock);
305 left = schedule_timeout(expiry);
307 write_lock(&cache->uc_lock);
308 remove_wait_queue(&entry->ue_waitq, &wait);
309 if (UC_CACHE_IS_ACQUIRING(entry)) {
310 /* we're interrupted or upcall failed in the middle */
311 rc = left > 0 ? -EINTR : -ETIMEDOUT;
312 /* if we waited uc_acquire_expire, we can try again
313 * with same data, but only if acquire is replayable
315 if (left <= 0 && !cache->uc_acquire_replay)
316 failedacquiring = true;
317 put_entry(cache, entry);
318 if (!failedacquiring) {
319 write_unlock(&cache->uc_lock);
320 failedacquiring = true;
323 "retry acquire for key %llu (got %d)\n",
327 wake_up_all(&entry->ue_waitq);
328 CERROR("%s: acquire for key %lld after %llu: rc = %d\n",
329 cache->uc_name, entry->ue_key,
330 cache->uc_acquire_expire, rc);
331 GOTO(out, entry = ERR_PTR(rc));
335 /* invalid means error, don't need to try again */
336 if (UC_CACHE_IS_INVALID(entry)) {
337 put_entry(cache, entry);
338 GOTO(out, entry = ERR_PTR(-EIDRM));
342 * We can't refresh the existing one because some
343 * memory might be shared by multiple processes.
345 if (check_unlink_entry(cache, entry, writelock)) {
346 /* if expired, try again. but if this entry is
347 * created by me but too quickly turn to expired
348 * without any error, should at least give a
349 * chance to use it once.
352 /* as stated above, we already hold a write lock */
353 put_entry(cache, entry);
354 write_unlock(&cache->uc_lock);
360 /* Now we know it's good */
363 write_unlock(&cache->uc_lock);
365 read_unlock(&cache->uc_lock);
368 EXPORT_SYMBOL(upcall_cache_get_entry);
370 void upcall_cache_get_entry_raw(struct upcall_cache_entry *entry)
374 EXPORT_SYMBOL(upcall_cache_get_entry_raw);
376 void upcall_cache_update_entry(struct upcall_cache *cache,
377 struct upcall_cache_entry *entry,
378 time64_t expire, int state)
380 write_lock(&cache->uc_lock);
381 entry->ue_expire = expire;
383 UC_CACHE_SET_VALID(entry);
385 entry->ue_flags |= state;
386 write_unlock(&cache->uc_lock);
388 EXPORT_SYMBOL(upcall_cache_update_entry);
390 void upcall_cache_put_entry(struct upcall_cache *cache,
391 struct upcall_cache_entry *entry)
400 LASSERT(atomic_read(&entry->ue_refcount) > 0);
401 write_lock(&cache->uc_lock);
402 put_entry(cache, entry);
403 write_unlock(&cache->uc_lock);
406 EXPORT_SYMBOL(upcall_cache_put_entry);
408 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
411 struct upcall_cache_entry *entry = NULL;
412 struct list_head *head;
413 int found = 0, rc = 0;
414 bool writelock = false;
419 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
420 cache->uc_hashsize)];
422 read_lock(&cache->uc_lock);
423 list_for_each_entry(entry, head, ue_hash) {
424 if (downcall_compare(cache, entry, key, args) == 0) {
432 CDEBUG(D_OTHER, "%s: upcall for key %llu not expected\n",
433 cache->uc_name, key);
434 /* haven't found, it's possible */
435 read_unlock(&cache->uc_lock);
440 CDEBUG(D_OTHER, "%s: upcall for key %llu returned %d\n",
441 cache->uc_name, entry->ue_key, err);
442 write_lock_from_read(&cache->uc_lock, &writelock);
446 if (!UC_CACHE_IS_ACQUIRING(entry)) {
447 CDEBUG(D_RPCTRACE, "%s: found uptodate entry %p (key %llu)"
448 "\n", cache->uc_name, entry, entry->ue_key);
449 write_lock_from_read(&cache->uc_lock, &writelock);
453 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
454 CERROR("%s: found a stale entry %p (key %llu) in ioctl\n",
455 cache->uc_name, entry, entry->ue_key);
456 write_lock_from_read(&cache->uc_lock, &writelock);
457 GOTO(out, rc = -EINVAL);
460 read_unlock(&cache->uc_lock);
461 if (cache->uc_ops->parse_downcall)
462 rc = cache->uc_ops->parse_downcall(cache, entry, args);
463 write_lock(&cache->uc_lock);
467 if (!entry->ue_expire)
468 entry->ue_expire = ktime_get_seconds() + cache->uc_entry_expire;
469 UC_CACHE_SET_VALID(entry);
470 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key %llu\n",
471 cache->uc_name, entry, entry->ue_key);
473 /* 'goto out' needs to make sure to take a write lock first */
475 UC_CACHE_SET_INVALID(entry);
476 list_del_init(&entry->ue_hash);
478 UC_CACHE_CLEAR_ACQUIRING(entry);
479 wake_up(&entry->ue_waitq);
480 put_entry(cache, entry);
481 write_unlock(&cache->uc_lock);
485 EXPORT_SYMBOL(upcall_cache_downcall);
487 void upcall_cache_flush(struct upcall_cache *cache, int force)
489 struct upcall_cache_entry *entry, *next;
493 write_lock(&cache->uc_lock);
494 for (i = 0; i < cache->uc_hashsize; i++) {
495 list_for_each_entry_safe(entry, next,
496 &cache->uc_hashtable[i], ue_hash) {
497 if (!force && atomic_read(&entry->ue_refcount)) {
498 UC_CACHE_SET_EXPIRED(entry);
501 LASSERT(!atomic_read(&entry->ue_refcount));
502 free_entry(cache, entry);
505 write_unlock(&cache->uc_lock);
508 EXPORT_SYMBOL(upcall_cache_flush);
510 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
512 struct list_head *head;
513 struct upcall_cache_entry *entry;
517 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
518 cache->uc_hashsize)];
520 write_lock(&cache->uc_lock);
521 list_for_each_entry(entry, head, ue_hash) {
522 if (upcall_compare(cache, entry, key, args) == 0) {
529 CWARN("%s: flush entry %p: key %llu, ref %d, fl %x, "
530 "cur %lld, ex %lld/%lld\n",
531 cache->uc_name, entry, entry->ue_key,
532 atomic_read(&entry->ue_refcount), entry->ue_flags,
533 ktime_get_real_seconds(), entry->ue_acquire_expire,
536 UC_CACHE_SET_EXPIRED(entry);
537 put_entry(cache, entry);
539 write_unlock(&cache->uc_lock);
541 EXPORT_SYMBOL(upcall_cache_flush_one);
543 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
544 int hashsz, time64_t entry_expire,
545 time64_t acquire_expire, bool replayable,
546 struct upcall_cache_ops *ops)
548 struct upcall_cache *cache;
552 LIBCFS_ALLOC(cache, sizeof(*cache));
554 RETURN(ERR_PTR(-ENOMEM));
556 rwlock_init(&cache->uc_lock);
557 init_rwsem(&cache->uc_upcall_rwsem);
558 cache->uc_hashsize = hashsz;
559 LIBCFS_ALLOC(cache->uc_hashtable,
560 sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
561 if (!cache->uc_hashtable)
562 RETURN(ERR_PTR(-ENOMEM));
563 for (i = 0; i < cache->uc_hashsize; i++)
564 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
565 strscpy(cache->uc_name, name, sizeof(cache->uc_name));
566 /* upcall pathname proc tunable */
567 strscpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall));
568 cache->uc_entry_expire = entry_expire;
569 cache->uc_acquire_expire = acquire_expire;
570 cache->uc_acquire_replay = replayable;
575 EXPORT_SYMBOL(upcall_cache_init);
577 void upcall_cache_cleanup(struct upcall_cache *cache)
581 upcall_cache_flush_all(cache);
582 LIBCFS_FREE(cache->uc_hashtable,
583 sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
584 LIBCFS_FREE(cache, sizeof(*cache));
586 EXPORT_SYMBOL(upcall_cache_cleanup);