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 #ifndef AUTOCONF_INCLUDED
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>
47 # include <asm/segment.h>
50 #include <obd_support.h>
51 #include <lustre_lib.h>
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
54 struct group_info *groups_alloc(int ngroups)
56 struct group_info *ginfo;
58 LASSERT(ngroups <= NGROUPS_SMALL);
60 OBD_ALLOC(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
63 ginfo->ngroups = ngroups;
65 ginfo->blocks[0] = ginfo->small_block;
66 atomic_set(&ginfo->usage, 1);
71 void groups_free(struct group_info *ginfo)
73 LASSERT(ginfo->ngroups <= NGROUPS_SMALL);
74 LASSERT(ginfo->nblocks == 1);
75 LASSERT(ginfo->blocks[0] == ginfo->small_block);
77 OBD_FREE(ginfo, sizeof(*ginfo) + 1 * sizeof(gid_t *));
81 static struct upcall_cache_entry *alloc_entry(__u64 key)
83 struct upcall_cache_entry *entry;
85 OBD_ALLOC(entry, sizeof(*entry));
89 UC_CACHE_SET_NEW(entry);
90 INIT_LIST_HEAD(&entry->ue_hash);
92 atomic_set(&entry->ue_refcount, 0);
93 init_waitqueue_head(&entry->ue_waitq);
97 /* protected by hash lock */
98 static void free_entry(struct upcall_cache_entry *entry)
100 if (entry->ue_group_info)
101 groups_free(entry->ue_group_info);
102 list_del(&entry->ue_hash);
103 CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
104 entry, entry->ue_key);
105 OBD_FREE(entry, sizeof(*entry));
108 static void get_entry(struct upcall_cache_entry *entry)
110 atomic_inc(&entry->ue_refcount);
113 static void put_entry(struct upcall_cache_entry *entry)
115 if (atomic_dec_and_test(&entry->ue_refcount) &&
116 (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
121 static int check_unlink_entry(struct upcall_cache_entry *entry)
123 if (UC_CACHE_IS_VALID(entry) &&
124 time_before(jiffies, entry->ue_expire))
127 if (UC_CACHE_IS_ACQUIRING(entry)) {
128 if (time_before(jiffies, 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))
143 static int refresh_entry(struct upcall_cache *hash,
144 struct upcall_cache_entry *entry)
152 snprintf(keystr, 16, LPU64, entry->ue_key);
154 CDEBUG(D_INFO, "The groups upcall is: %s \n", hash->uc_upcall);
155 argv[0] = hash->uc_upcall;
156 argv[1] = hash->uc_name;
161 envp[1] = "PATH=/sbin:/usr/sbin";
164 rc = USERMODEHELPER(argv[0], argv, envp);
166 CERROR("%s: error invoking getgroups upcall %s %s %s: rc %d; "
167 "check /proc/fs/lustre/mds/%s/group_upcall\n",
168 hash->uc_name, argv[0], argv[1], argv[2], rc, argv[1]);
170 CDEBUG(D_HA, "%s: invoked upcall %s %s %s\n", hash->uc_name,
171 argv[0], argv[1], argv[2]);
177 static int entry_set_group_info(struct upcall_cache_entry *entry, __u32 primary,
178 __u32 ngroups, __u32 *groups)
180 struct group_info *ginfo;
184 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
185 if (ngroups > NGROUPS)
189 if (ngroups > NGROUPS_MAX) {
190 CERROR("using first %d supplementary groups for uid "LPU64"\n",
191 NGROUPS_MAX, entry->ue_key);
192 ngroups = NGROUPS_MAX;
195 ginfo = groups_alloc(ngroups);
197 CERROR("uid "LPU64" update can't alloc ginfo for %d groups\n",
198 entry->ue_key, ngroups);
201 entry->ue_group_info = ginfo;
202 entry->ue_primary = primary;
204 for (i = 0; i < ginfo->nblocks; i++) {
205 int cp_count = min(NGROUPS_PER_BLOCK, (int)ngroups);
206 int off = i * NGROUPS_PER_BLOCK;
208 for (j = 0; j < cp_count; j++)
209 ginfo->blocks[i][j] = groups[off + j];
216 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *hash,
217 __u64 key, __u32 primary,
218 __u32 ngroups, __u32 *groups)
220 struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
221 struct list_head *head;
228 if (strcmp(hash->uc_upcall, "NONE") == 0) {
229 new = alloc_entry(key);
231 CERROR("fail to alloc entry\n");
236 /* We have to sort the groups for 2.6 kernels */
237 LASSERT(ngroups <= 2);
238 if (ngroups == 2 && groups[1] == -1)
240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
241 /* 2.6 needs groups array sorted */
242 if (ngroups == 2 && groups[0] > groups[1]) {
243 __u32 tmp = groups[1];
244 groups[1] = groups[0];
248 if (ngroups > 0 && groups[0] == -1) {
249 groups[0] = groups[1];
253 rc = entry_set_group_info(new, primary, ngroups, groups);
255 /* We can't cache this entry as it only has a subset of
256 * the user's groups, as sent in suppgid1, suppgid2. */
257 UC_CACHE_SET_EXPIRED(new);
260 head = &hash->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
263 spin_lock(&hash->uc_lock);
264 list_for_each_entry_safe(entry, next, head, ue_hash) {
265 /* check invalid & expired items */
266 if (check_unlink_entry(entry))
268 if (entry->ue_key == key) {
274 if (!found) { /* didn't find it */
276 spin_unlock(&hash->uc_lock);
277 new = alloc_entry(key);
279 CERROR("fail to alloc entry\n");
280 RETURN(ERR_PTR(-ENOMEM));
284 list_add(&new->ue_hash, head);
292 list_move(&entry->ue_hash, head);
296 /* acquire for new one */
297 if (UC_CACHE_IS_NEW(entry)) {
298 UC_CACHE_SET_ACQUIRING(entry);
299 UC_CACHE_CLEAR_NEW(entry);
300 entry->ue_acquire_expire = jiffies + hash->uc_acquire_expire;
301 spin_unlock(&hash->uc_lock);
302 rc = refresh_entry(hash, entry);
303 spin_lock(&hash->uc_lock);
305 UC_CACHE_CLEAR_ACQUIRING(entry);
306 UC_CACHE_SET_INVALID(entry);
310 /* someone (and only one) is doing upcall upon
311 * this item, just wait it complete
313 if (UC_CACHE_IS_ACQUIRING(entry)) {
314 init_waitqueue_entry(&wait, current);
315 add_wait_queue(&entry->ue_waitq, &wait);
316 set_current_state(TASK_INTERRUPTIBLE);
317 spin_unlock(&hash->uc_lock);
319 schedule_timeout(hash->uc_acquire_expire);
321 spin_lock(&hash->uc_lock);
322 remove_wait_queue(&entry->ue_waitq, &wait);
323 if (UC_CACHE_IS_ACQUIRING(entry)) {
324 static unsigned long next;
325 /* we're interrupted or upcall failed in the middle */
326 if (time_after(jiffies, next)) {
327 CERROR("acquire timeout exceeded for key "LPU64
328 "\n", entry->ue_key);
329 next = jiffies + 1800;
332 GOTO(out, entry = ERR_PTR(-EIDRM));
337 /* invalid means error, don't need to try again */
338 if (UC_CACHE_IS_INVALID(entry)) {
340 GOTO(out, entry = ERR_PTR(-EIDRM));
344 * We can't refresh the existing one because some
345 * memory might be shared by multiple processes.
347 if (check_unlink_entry(entry)) {
348 /* if expired, try again. but if this entry is
349 * created by me but too quickly turn to expired
350 * without any error, should at least give a
351 * chance to use it once.
355 spin_unlock(&hash->uc_lock);
361 /* Now we know it's good */
363 spin_unlock(&hash->uc_lock);
366 EXPORT_SYMBOL(upcall_cache_get_entry);
368 void upcall_cache_put_entry(struct upcall_cache *hash,
369 struct upcall_cache_entry *entry)
378 LASSERT(atomic_read(&entry->ue_refcount) > 0);
379 spin_lock(&hash->uc_lock);
381 spin_unlock(&hash->uc_lock);
384 EXPORT_SYMBOL(upcall_cache_put_entry);
386 int upcall_cache_downcall(struct upcall_cache *hash, __u32 err, __u64 key,
387 __u32 primary, __u32 ngroups, __u32 *groups)
389 struct upcall_cache_entry *entry = NULL;
390 struct list_head *head;
391 int found = 0, rc = 0;
396 head = &hash->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
398 spin_lock(&hash->uc_lock);
399 list_for_each_entry(entry, head, ue_hash) {
400 if (entry->ue_key == key) {
408 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
409 hash->uc_name, entry->ue_key);
410 /* haven't found, it's possible */
411 spin_unlock(&hash->uc_lock);
416 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
417 hash->uc_name, entry->ue_key, err);
418 GOTO(out, rc = -EINVAL);
421 if (!UC_CACHE_IS_ACQUIRING(entry)) {
422 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
423 hash->uc_name, entry, entry->ue_key);
427 if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
428 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
429 hash->uc_name, entry, entry->ue_key);
430 GOTO(out, rc = -EINVAL);
433 spin_unlock(&hash->uc_lock);
434 rc = entry_set_group_info(entry, primary, ngroups, groups);
435 spin_lock(&hash->uc_lock);
439 entry->ue_expire = jiffies + hash->uc_entry_expire;
440 UC_CACHE_SET_VALID(entry);
441 CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
442 hash->uc_name, entry, entry->ue_key);
445 UC_CACHE_SET_INVALID(entry);
446 list_del_init(&entry->ue_hash);
448 UC_CACHE_CLEAR_ACQUIRING(entry);
449 spin_unlock(&hash->uc_lock);
450 wake_up_all(&entry->ue_waitq);
455 EXPORT_SYMBOL(upcall_cache_downcall);
457 static void cache_flush(struct upcall_cache *hash, int force)
459 struct upcall_cache_entry *entry, *next;
463 spin_lock(&hash->uc_lock);
464 for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
465 list_for_each_entry_safe(entry, next,
466 &hash->uc_hashtable[i], ue_hash) {
467 if (!force && atomic_read(&entry->ue_refcount)) {
468 UC_CACHE_SET_EXPIRED(entry);
471 LASSERT(!atomic_read(&entry->ue_refcount));
475 spin_unlock(&hash->uc_lock);
479 void upcall_cache_flush_idle(struct upcall_cache *cache)
481 cache_flush(cache, 0);
483 EXPORT_SYMBOL(upcall_cache_flush_idle);
485 void upcall_cache_flush_all(struct upcall_cache *cache)
487 cache_flush(cache, 1);
489 EXPORT_SYMBOL(upcall_cache_flush_all);
491 struct upcall_cache *upcall_cache_init(const char *name)
493 struct upcall_cache *hash;
497 OBD_ALLOC(hash, sizeof(*hash));
499 RETURN(ERR_PTR(-ENOMEM));
501 spin_lock_init(&hash->uc_lock);
502 for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
503 INIT_LIST_HEAD(&hash->uc_hashtable[i]);
504 strncpy(hash->uc_name, name, sizeof(hash->uc_name) - 1);
505 /* set default value, proc tunable */
506 strcpy(hash->uc_upcall, "NONE");
507 hash->uc_entry_expire = 10 * 60 * HZ;
508 hash->uc_acquire_expire = 15 * HZ;
512 EXPORT_SYMBOL(upcall_cache_init);
514 void upcall_cache_cleanup(struct upcall_cache *hash)
518 upcall_cache_flush_all(hash);
519 OBD_FREE(hash, sizeof(*hash));
521 EXPORT_SYMBOL(upcall_cache_cleanup);