1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Supplementary groups cache.
6 * Copyright (c) 2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_SEC
26 #ifdef HAVE_KERNEL_CONFIG_H
27 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
32 #include <linux/kmod.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/version.h>
37 #include <linux/unistd.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
43 #include <linux/stat.h>
44 #include <asm/uaccess.h>
45 #include <linux/slab.h>
46 #include <asm/segment.h>
48 #include <obd_support.h>
49 #include <lustre_lib.h>
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
52 struct group_info *groups_alloc(int ngroups)
54 struct group_info *ginfo;
56 LASSERT(ngroups <= NGROUPS_SMALL);
58 OBD_ALLOC(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
61 ginfo->ngroups = ngroups;
63 ginfo->blocks[0] = ginfo->small_block;
64 atomic_set(&ginfo->usage, 1);
69 void groups_free(struct group_info *ginfo)
71 LASSERT(ginfo->ngroups <= NGROUPS_SMALL);
72 LASSERT(ginfo->nblocks == 1);
73 LASSERT(ginfo->blocks[0] == ginfo->small_block);
75 OBD_FREE(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
79 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
80 __u64 key, void *args)
82 struct upcall_cache_entry *entry;
88 UC_CACHE_SET_NEW(entry);
89 INIT_LIST_HEAD(&entry->ue_hash);
91 atomic_set(&entry->ue_refcount, 0);
92 init_waitqueue_head(&entry->ue_waitq);
93 if (cache->uc_ops->init_entry)
94 cache->uc_ops->init_entry(entry, args);
98 /* protected by cache lock */
99 static void free_entry(struct upcall_cache *cache,
100 struct upcall_cache_entry *entry)
102 if (cache->uc_ops->free_entry)
103 cache->uc_ops->free_entry(cache, entry);
105 list_del(&entry->ue_hash);
106 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
107 entry, entry->ue_key);
111 static inline int upcall_compare(struct upcall_cache *cache,
112 struct upcall_cache_entry *entry,
113 __u64 key, void *args)
115 if (entry->ue_key != key)
118 if (cache->uc_ops->upcall_compare)
119 return cache->uc_ops->upcall_compare(cache, entry, key, args);
124 static inline int downcall_compare(struct upcall_cache *cache,
125 struct upcall_cache_entry *entry,
126 __u64 key, void *args)
128 if (entry->ue_key != key)
131 if (cache->uc_ops->downcall_compare)
132 return cache->uc_ops->downcall_compare(cache, entry, key, args);
137 static inline void get_entry(struct upcall_cache_entry *entry)
139 atomic_inc(&entry->ue_refcount);
142 static inline void put_entry(struct upcall_cache *cache,
143 struct upcall_cache_entry *entry)
145 if (atomic_dec_and_test(&entry->ue_refcount) &&
146 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
147 free_entry(cache, entry);
151 static int check_unlink_entry(struct upcall_cache *cache,
152 struct upcall_cache_entry *entry)
154 if (UC_CACHE_IS_VALID(entry) &&
155 time_before(jiffies, entry->ue_expire))
158 if (UC_CACHE_IS_ACQUIRING(entry)) {
159 if (time_before(jiffies, entry->ue_acquire_expire))
162 UC_CACHE_SET_EXPIRED(entry);
163 wake_up_all(&entry->ue_waitq);
164 } else if (!UC_CACHE_IS_INVALID(entry)) {
165 UC_CACHE_SET_EXPIRED(entry);
168 list_del_init(&entry->ue_hash);
169 if (!atomic_read(&entry->ue_refcount))
170 free_entry(cache, entry);
174 static inline int refresh_entry(struct upcall_cache *cache,
175 struct upcall_cache_entry *entry)
177 LASSERT(cache->uc_ops->do_upcall);
178 return cache->uc_ops->do_upcall(cache, entry);
181 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
182 __u64 key, void *args)
184 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
185 struct list_head *head;
192 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
195 spin_lock(&cache->uc_lock);
196 list_for_each_entry_safe(entry, next, head, ue_hash) {
197 /* check invalid & expired items */
198 if (check_unlink_entry(cache, entry))
200 if (upcall_compare(cache, entry, key, args) == 0) {
206 if (!found) { /* didn't find it */
208 spin_unlock(&cache->uc_lock);
209 new = alloc_entry(cache, key, args);
211 CERROR("fail to alloc entry\n");
212 RETURN(ERR_PTR(-ENOMEM));
216 list_add(&new->ue_hash, head);
221 free_entry(cache, new);
224 list_move(&entry->ue_hash, head);
228 /* acquire for new one */
229 if (UC_CACHE_IS_NEW(entry)) {
230 UC_CACHE_SET_ACQUIRING(entry);
231 UC_CACHE_CLEAR_NEW(entry);
232 entry->ue_acquire_expire = jiffies + cache->uc_acquire_expire;
233 spin_unlock(&cache->uc_lock);
234 rc = refresh_entry(cache, entry);
235 spin_lock(&cache->uc_lock);
237 UC_CACHE_CLEAR_ACQUIRING(entry);
238 UC_CACHE_SET_INVALID(entry);
242 /* someone (and only one) is doing upcall upon
243 * this item, just wait it complete
245 if (UC_CACHE_IS_ACQUIRING(entry)) {
246 unsigned long expiry = jiffies + cache->uc_acquire_expire;
248 init_waitqueue_entry(&wait, current);
249 add_wait_queue(&entry->ue_waitq, &wait);
250 set_current_state(TASK_INTERRUPTIBLE);
251 spin_unlock(&cache->uc_lock);
253 schedule_timeout(cache->uc_acquire_expire);
255 spin_lock(&cache->uc_lock);
256 remove_wait_queue(&entry->ue_waitq, &wait);
257 if (UC_CACHE_IS_ACQUIRING(entry)) {
258 /* we're interrupted or upcall failed in the middle */
259 rc = time_before(jiffies, expiry) ? -EINTR : -ETIMEDOUT;
260 put_entry(cache, entry);
261 CERROR("acquire timeout exceeded for key "LPU64
262 "\n", entry->ue_key);
263 GOTO(out, entry = ERR_PTR(rc));
268 /* invalid means error, don't need to try again */
269 if (UC_CACHE_IS_INVALID(entry)) {
270 put_entry(cache, entry);
271 GOTO(out, entry = ERR_PTR(-EIDRM));
275 * We can't refresh the existing one because some
276 * memory might be shared by multiple processes.
278 if (check_unlink_entry(cache, entry)) {
279 /* if expired, try again. but if this entry is
280 * created by me but too quickly turn to expired
281 * without any error, should at least give a
282 * chance to use it once.
285 put_entry(cache, entry);
286 spin_unlock(&cache->uc_lock);
292 /* Now we know it's good */
294 spin_unlock(&cache->uc_lock);
297 EXPORT_SYMBOL(upcall_cache_get_entry);
299 void upcall_cache_put_entry(struct upcall_cache *cache,
300 struct upcall_cache_entry *entry)
309 LASSERT(atomic_read(&entry->ue_refcount) > 0);
310 spin_lock(&cache->uc_lock);
311 put_entry(cache, entry);
312 spin_unlock(&cache->uc_lock);
315 EXPORT_SYMBOL(upcall_cache_put_entry);
317 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
320 struct upcall_cache_entry *entry = NULL;
321 struct list_head *head;
322 int found = 0, rc = 0;
327 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
329 spin_lock(&cache->uc_lock);
330 list_for_each_entry(entry, head, ue_hash) {
331 if (downcall_compare(cache, entry, key, args) == 0) {
339 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
340 cache->uc_name, key);
341 /* haven't found, it's possible */
342 spin_unlock(&cache->uc_lock);
347 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
348 cache->uc_name, entry->ue_key, err);
349 GOTO(out, rc = -EINVAL);
352 if (!UC_CACHE_IS_ACQUIRING(entry)) {
353 CDEBUG(D_HA, "%s: found uptodate entry %p (key "LPU64")\n",
354 cache->uc_name, entry, entry->ue_key);
358 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
359 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
360 cache->uc_name, entry, entry->ue_key);
361 GOTO(out, rc = -EINVAL);
364 spin_unlock(&cache->uc_lock);
365 if (cache->uc_ops->parse_downcall)
366 rc = cache->uc_ops->parse_downcall(cache, entry, args);
367 spin_lock(&cache->uc_lock);
371 entry->ue_expire = jiffies + cache->uc_entry_expire;
372 UC_CACHE_SET_VALID(entry);
373 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
374 cache->uc_name, entry, entry->ue_key);
377 UC_CACHE_SET_INVALID(entry);
378 list_del_init(&entry->ue_hash);
380 UC_CACHE_CLEAR_ACQUIRING(entry);
381 spin_unlock(&cache->uc_lock);
382 wake_up_all(&entry->ue_waitq);
383 put_entry(cache, entry);
387 EXPORT_SYMBOL(upcall_cache_downcall);
389 static void cache_flush(struct upcall_cache *cache, int force)
391 struct upcall_cache_entry *entry, *next;
395 spin_lock(&cache->uc_lock);
396 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
397 list_for_each_entry_safe(entry, next,
398 &cache->uc_hashtable[i], ue_hash) {
399 if (!force && atomic_read(&entry->ue_refcount)) {
400 UC_CACHE_SET_EXPIRED(entry);
403 LASSERT(!atomic_read(&entry->ue_refcount));
404 free_entry(cache, entry);
407 spin_unlock(&cache->uc_lock);
411 void upcall_cache_flush_idle(struct upcall_cache *cache)
413 cache_flush(cache, 0);
415 EXPORT_SYMBOL(upcall_cache_flush_idle);
417 void upcall_cache_flush_all(struct upcall_cache *cache)
419 cache_flush(cache, 1);
421 EXPORT_SYMBOL(upcall_cache_flush_all);
423 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
425 struct list_head *head;
426 struct upcall_cache_entry *entry;
430 head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
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 "LPU64", ref %d, fl %x, "
442 "cur %lu, ex %ld/%ld\n",
443 cache->uc_name, entry, entry->ue_key,
444 atomic_read(&entry->ue_refcount), entry->ue_flags,
445 get_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 struct upcall_cache_ops *ops)
458 struct upcall_cache *cache;
462 OBD_ALLOC(cache, sizeof(*cache));
464 RETURN(ERR_PTR(-ENOMEM));
466 spin_lock_init(&cache->uc_lock);
467 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
468 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
469 strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
470 /* upcall pathname proc tunable */
471 strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
472 cache->uc_entry_expire = 10 * 60 * HZ;
473 cache->uc_acquire_expire = 15 * HZ;
478 EXPORT_SYMBOL(upcall_cache_init);
480 void upcall_cache_cleanup(struct upcall_cache *cache)
484 upcall_cache_flush_all(cache);
485 OBD_FREE(cache, sizeof(*cache));
487 EXPORT_SYMBOL(upcall_cache_cleanup);