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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/idmap.c
38 * Lustre user identity mapping.
40 * Author: Fan Yong <fanyong@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_SEC
45 #include <linux/user_namespace.h>
46 #ifdef HAVE_UIDGID_HEADER
47 # include <linux/uidgid.h>
49 #include <lustre_idmap.h>
50 #include <upcall_cache.h>
51 #include <md_object.h>
52 #include <obd_support.h>
54 #define lustre_get_group_info(group_info) do { \
55 atomic_inc(&(group_info)->usage); \
58 #define lustre_put_group_info(group_info) do { \
59 if (atomic_dec_and_test(&(group_info)->usage)) \
60 groups_free(group_info); \
64 * groups_search() is copied from linux kernel!
67 static int lustre_groups_search(struct group_info *group_info,
76 right = group_info->ngroups;
77 while (left < right) {
78 int mid = (left + right) / 2;
80 from_kgid(&init_user_ns, CFS_GROUP_AT(group_info, mid));
92 void lustre_groups_from_list(struct group_info *ginfo, gid_t *glist)
95 int count = ginfo->ngroups;
97 /* fill group_info from gid array */
98 for (i = 0; i < ginfo->nblocks && count > 0; i++) {
99 int cp_count = min(CFS_NGROUPS_PER_BLOCK, count);
100 int off = i * CFS_NGROUPS_PER_BLOCK;
101 int len = cp_count * sizeof(*glist);
103 memcpy(ginfo->blocks[i], glist + off, len);
107 EXPORT_SYMBOL(lustre_groups_from_list);
109 /* groups_sort() is copied from linux kernel! */
110 /* a simple shell-metzner sort */
111 void lustre_groups_sort(struct group_info *group_info)
113 int base, max, stride;
114 int gidsetsize = group_info->ngroups;
116 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
121 max = gidsetsize - stride;
122 for (base = 0; base < max; base++) {
124 int right = left + stride;
125 gid_t tmp = from_kgid(&init_user_ns,
126 CFS_GROUP_AT(group_info, right));
129 tmp < from_kgid(&init_user_ns,
130 CFS_GROUP_AT(group_info, left))) {
131 CFS_GROUP_AT(group_info, right) =
132 CFS_GROUP_AT(group_info, left);
136 CFS_GROUP_AT(group_info, right) =
137 make_kgid(&init_user_ns, tmp);
142 EXPORT_SYMBOL(lustre_groups_sort);
144 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
148 if (grp != mu->uc_fsgid) {
149 struct group_info *group_info = NULL;
151 if (mu->uc_ginfo || !mu->uc_identity ||
152 mu->uc_valid == UCRED_OLD)
153 if (grp == mu->uc_suppgids[0] ||
154 grp == mu->uc_suppgids[1])
158 group_info = mu->uc_ginfo;
159 else if (mu->uc_identity)
160 group_info = mu->uc_identity->mi_ginfo;
165 lustre_get_group_info(group_info);
166 rc = lustre_groups_search(group_info, grp);
167 lustre_put_group_info(group_info);
171 EXPORT_SYMBOL(lustre_in_group_p);