Whamcloud - gitweb
LU-12477 libcfs: Remove obsolete config checks
[fs/lustre-release.git] / lustre / obdclass / idmap.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/idmap.c
33  *
34  * Lustre user identity mapping.
35  *
36  * Author: Fan Yong <fanyong@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_SEC
40
41 #include <linux/user_namespace.h>
42 #include <linux/uidgid.h>
43
44 #include <lustre_idmap.h>
45 #include <upcall_cache.h>
46 #include <md_object.h>
47 #include <obd_support.h>
48
49 /*
50  * groups_search() is copied from linux kernel!
51  * A simple bsearch.
52  */
53 static int lustre_groups_search(struct group_info *group_info,
54                                 gid_t grp)
55 {
56         int left, right;
57
58         if (!group_info)
59                 return 0;
60
61         left = 0;
62         right = group_info->ngroups;
63         while (left < right) {
64                 int mid = (left + right) / 2;
65                 int cmp = grp -
66                         from_kgid(&init_user_ns, CFS_GROUP_AT(group_info, mid));
67
68                 if (cmp > 0)
69                         left = mid + 1;
70                 else if (cmp < 0)
71                         right = mid;
72                 else
73                         return 1;
74         }
75         return 0;
76 }
77
78 void lustre_groups_from_list(struct group_info *ginfo, gid_t *glist)
79 {
80 #ifdef HAVE_GROUP_INFO_GID
81         memcpy(ginfo->gid, glist, ginfo->ngroups * sizeof(__u32));
82 #else
83         int i;
84         int count = ginfo->ngroups;
85
86         /* fill group_info from gid array */
87         for (i = 0; i < ginfo->nblocks && count > 0; i++) {
88                 int cp_count = min(CFS_NGROUPS_PER_BLOCK, count);
89                 int off = i * CFS_NGROUPS_PER_BLOCK;
90                 int len = cp_count * sizeof(*glist);
91
92                 memcpy(ginfo->blocks[i], glist + off, len);
93                 count -= cp_count;
94         }
95 #endif
96 }
97 EXPORT_SYMBOL(lustre_groups_from_list);
98
99 /* groups_sort() is copied from linux kernel! */
100 /* a simple shell-metzner sort */
101 void lustre_groups_sort(struct group_info *group_info)
102 {
103         int base, max, stride;
104         int gidsetsize = group_info->ngroups;
105
106         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
107                 ; /* nothing */
108         stride /= 3;
109
110         while (stride) {
111                 max = gidsetsize - stride;
112                 for (base = 0; base < max; base++) {
113                         int left = base;
114                         int right = left + stride;
115                         gid_t tmp = from_kgid(&init_user_ns,
116                                               CFS_GROUP_AT(group_info, right));
117
118                         while (left >= 0 &&
119                                tmp < from_kgid(&init_user_ns,
120                                                CFS_GROUP_AT(group_info, left))) {
121                                 CFS_GROUP_AT(group_info, right) =
122                                         CFS_GROUP_AT(group_info, left);
123                                 right = left;
124                                 left -= stride;
125                         }
126                         CFS_GROUP_AT(group_info, right) =
127                                                 make_kgid(&init_user_ns, tmp);
128                 }
129                 stride /= 3;
130         }
131 }
132 EXPORT_SYMBOL(lustre_groups_sort);
133
134 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
135 {
136         int rc = 1;
137
138         if (grp != mu->uc_fsgid) {
139                 struct group_info *group_info = NULL;
140
141                 if (mu->uc_ginfo || !mu->uc_identity ||
142                     mu->uc_valid == UCRED_OLD)
143                         if (grp == mu->uc_suppgids[0] ||
144                             grp == mu->uc_suppgids[1])
145                                 return 1;
146
147                 if (mu->uc_ginfo)
148                         group_info = mu->uc_ginfo;
149                 else if (mu->uc_identity)
150                         group_info = mu->uc_identity->mi_ginfo;
151
152                 if (!group_info)
153                         return 0;
154
155                 atomic_inc(&group_info->usage);
156                 rc = lustre_groups_search(group_info, grp);
157                 if (atomic_dec_and_test(&group_info->usage))
158                         groups_free(group_info);
159         }
160         return rc;
161 }
162 EXPORT_SYMBOL(lustre_in_group_p);