1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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>
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 CFS_INIT_LIST_HEAD(&entry->ue_hash);
56 cfs_atomic_set(&entry->ue_refcount, 0);
57 cfs_waitq_init(&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 cfs_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 cfs_atomic_inc(&entry->ue_refcount);
107 static inline void put_entry(struct upcall_cache *cache,
108 struct upcall_cache_entry *entry)
110 if (cfs_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 cfs_waitq_broadcast(&entry->ue_waitq);
131 } else if (!UC_CACHE_IS_INVALID(entry)) {
132 UC_CACHE_SET_EXPIRED(entry);
135 cfs_list_del_init(&entry->ue_hash);
136 if (!cfs_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;
159 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
162 cfs_spin_lock(&cache->uc_lock);
163 cfs_list_for_each_entry_safe(entry, next, head, ue_hash) {
164 /* check invalid & expired items */
165 if (check_unlink_entry(cache, entry))
167 if (upcall_compare(cache, entry, key, args) == 0) {
175 cfs_spin_unlock(&cache->uc_lock);
176 new = alloc_entry(cache, key, args);
178 CERROR("fail to alloc entry\n");
179 RETURN(ERR_PTR(-ENOMEM));
183 cfs_list_add(&new->ue_hash, head);
188 free_entry(cache, new);
191 cfs_list_move(&entry->ue_hash, head);
195 /* acquire for new one */
196 if (UC_CACHE_IS_NEW(entry)) {
197 UC_CACHE_SET_ACQUIRING(entry);
198 UC_CACHE_CLEAR_NEW(entry);
199 cfs_spin_unlock(&cache->uc_lock);
200 rc = refresh_entry(cache, entry);
201 cfs_spin_lock(&cache->uc_lock);
202 entry->ue_acquire_expire =
203 cfs_time_shift(cache->uc_acquire_expire);
205 UC_CACHE_CLEAR_ACQUIRING(entry);
206 UC_CACHE_SET_INVALID(entry);
207 cfs_waitq_broadcast(&entry->ue_waitq);
208 if (unlikely(rc == -EREMCHG)) {
209 put_entry(cache, entry);
210 GOTO(out, entry = ERR_PTR(rc));
214 /* someone (and only one) is doing upcall upon this item,
215 * wait it to complete */
216 if (UC_CACHE_IS_ACQUIRING(entry)) {
217 long expiry = (entry == new) ?
218 cfs_time_seconds(cache->uc_acquire_expire) :
219 CFS_MAX_SCHEDULE_TIMEOUT;
222 cfs_waitlink_init(&wait);
223 cfs_waitq_add(&entry->ue_waitq, &wait);
224 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
225 cfs_spin_unlock(&cache->uc_lock);
227 left = cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE,
230 cfs_spin_lock(&cache->uc_lock);
231 cfs_waitq_del(&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 CERROR("acquire for key "LPU64": error %d\n",
237 put_entry(cache, entry);
238 GOTO(out, entry = ERR_PTR(rc));
242 /* invalid means error, don't need to try again */
243 if (UC_CACHE_IS_INVALID(entry)) {
244 put_entry(cache, entry);
245 GOTO(out, entry = ERR_PTR(-EIDRM));
249 * We can't refresh the existing one because some
250 * memory might be shared by multiple processes.
252 if (check_unlink_entry(cache, entry)) {
253 /* if expired, try again. but if this entry is
254 * created by me but too quickly turn to expired
255 * without any error, should at least give a
256 * chance to use it once.
259 put_entry(cache, entry);
260 cfs_spin_unlock(&cache->uc_lock);
266 /* Now we know it's good */
268 cfs_spin_unlock(&cache->uc_lock);
271 EXPORT_SYMBOL(upcall_cache_get_entry);
273 void upcall_cache_put_entry(struct upcall_cache *cache,
274 struct upcall_cache_entry *entry)
283 LASSERT(cfs_atomic_read(&entry->ue_refcount) > 0);
284 cfs_spin_lock(&cache->uc_lock);
285 put_entry(cache, entry);
286 cfs_spin_unlock(&cache->uc_lock);
289 EXPORT_SYMBOL(upcall_cache_put_entry);
291 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
294 struct upcall_cache_entry *entry = NULL;
296 int found = 0, rc = 0;
301 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
303 cfs_spin_lock(&cache->uc_lock);
304 cfs_list_for_each_entry(entry, head, ue_hash) {
305 if (downcall_compare(cache, entry, key, args) == 0) {
313 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
314 cache->uc_name, key);
315 /* haven't found, it's possible */
316 cfs_spin_unlock(&cache->uc_lock);
321 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
322 cache->uc_name, entry->ue_key, err);
323 GOTO(out, rc = -EINVAL);
326 if (!UC_CACHE_IS_ACQUIRING(entry)) {
327 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
328 cache->uc_name, entry, entry->ue_key);
332 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
333 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
334 cache->uc_name, entry, entry->ue_key);
335 GOTO(out, rc = -EINVAL);
338 cfs_spin_unlock(&cache->uc_lock);
339 if (cache->uc_ops->parse_downcall)
340 rc = cache->uc_ops->parse_downcall(cache, entry, args);
341 cfs_spin_lock(&cache->uc_lock);
345 entry->ue_expire = cfs_time_shift(cache->uc_entry_expire);
346 UC_CACHE_SET_VALID(entry);
347 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
348 cache->uc_name, entry, entry->ue_key);
351 UC_CACHE_SET_INVALID(entry);
352 cfs_list_del_init(&entry->ue_hash);
354 UC_CACHE_CLEAR_ACQUIRING(entry);
355 cfs_spin_unlock(&cache->uc_lock);
356 cfs_waitq_broadcast(&entry->ue_waitq);
357 put_entry(cache, entry);
361 EXPORT_SYMBOL(upcall_cache_downcall);
363 static void cache_flush(struct upcall_cache *cache, int force)
365 struct upcall_cache_entry *entry, *next;
369 cfs_spin_lock(&cache->uc_lock);
370 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
371 cfs_list_for_each_entry_safe(entry, next,
372 &cache->uc_hashtable[i], ue_hash) {
373 if (!force && cfs_atomic_read(&entry->ue_refcount)) {
374 UC_CACHE_SET_EXPIRED(entry);
377 LASSERT(!cfs_atomic_read(&entry->ue_refcount));
378 free_entry(cache, entry);
381 cfs_spin_unlock(&cache->uc_lock);
385 void upcall_cache_flush_idle(struct upcall_cache *cache)
387 cache_flush(cache, 0);
389 EXPORT_SYMBOL(upcall_cache_flush_idle);
391 void upcall_cache_flush_all(struct upcall_cache *cache)
393 cache_flush(cache, 1);
395 EXPORT_SYMBOL(upcall_cache_flush_all);
397 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
400 struct upcall_cache_entry *entry;
404 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
406 cfs_spin_lock(&cache->uc_lock);
407 cfs_list_for_each_entry(entry, head, ue_hash) {
408 if (upcall_compare(cache, entry, key, args) == 0) {
415 CWARN("%s: flush entry %p: key "LPU64", ref %d, fl %x, "
416 "cur %lu, ex %ld/%ld\n",
417 cache->uc_name, entry, entry->ue_key,
418 cfs_atomic_read(&entry->ue_refcount), entry->ue_flags,
419 cfs_time_current_sec(), entry->ue_acquire_expire,
421 UC_CACHE_SET_EXPIRED(entry);
422 if (!cfs_atomic_read(&entry->ue_refcount))
423 free_entry(cache, entry);
425 cfs_spin_unlock(&cache->uc_lock);
427 EXPORT_SYMBOL(upcall_cache_flush_one);
429 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
430 struct upcall_cache_ops *ops)
432 struct upcall_cache *cache;
436 LIBCFS_ALLOC(cache, sizeof(*cache));
438 RETURN(ERR_PTR(-ENOMEM));
440 cfs_spin_lock_init(&cache->uc_lock);
441 cfs_rwlock_init(&cache->uc_upcall_rwlock);
442 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
443 CFS_INIT_LIST_HEAD(&cache->uc_hashtable[i]);
444 strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
445 /* upcall pathname proc tunable */
446 strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
447 cache->uc_entry_expire = 20 * 60;
448 cache->uc_acquire_expire = 30;
453 EXPORT_SYMBOL(upcall_cache_init);
455 void upcall_cache_cleanup(struct upcall_cache *cache)
459 upcall_cache_flush_all(cache);
460 LIBCFS_FREE(cache, sizeof(*cache));
462 EXPORT_SYMBOL(upcall_cache_cleanup);