Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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  *
31  * lustre/obdclass/idmap.c
32  *
33  * Lustre user identity mapping.
34  *
35  * Author: Fan Yong <fanyong@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_SEC
39
40 #include <linux/user_namespace.h>
41 #include <linux/uidgid.h>
42
43 #include <lustre_idmap.h>
44 #include <upcall_cache.h>
45 #include <md_object.h>
46 #include <obd_support.h>
47
48 /*
49  * groups_search() is copied from linux kernel!
50  * A simple bsearch.
51  */
52 static int lustre_groups_search(struct group_info *group_info,
53                                 gid_t grp)
54 {
55         int left, right;
56
57         if (!group_info)
58                 return 0;
59
60         left = 0;
61         right = group_info->ngroups;
62         while (left < right) {
63                 int mid = (left + right) / 2;
64                 int cmp = grp -
65                         from_kgid(&init_user_ns, CFS_GROUP_AT(group_info, mid));
66
67                 if (cmp > 0)
68                         left = mid + 1;
69                 else if (cmp < 0)
70                         right = mid;
71                 else
72                         return 1;
73         }
74         return 0;
75 }
76
77 void lustre_groups_from_list(struct group_info *ginfo, gid_t *glist)
78 {
79 #ifdef HAVE_GROUP_INFO_GID
80         memcpy(ginfo->gid, glist, ginfo->ngroups * sizeof(__u32));
81 #else
82         int i;
83         int count = ginfo->ngroups;
84
85         /* fill group_info from gid array */
86         for (i = 0; i < ginfo->nblocks && count > 0; i++) {
87                 int cp_count = min(CFS_NGROUPS_PER_BLOCK, count);
88                 int off = i * CFS_NGROUPS_PER_BLOCK;
89                 int len = cp_count * sizeof(*glist);
90
91                 memcpy(ginfo->blocks[i], glist + off, len);
92                 count -= cp_count;
93         }
94 #endif
95 }
96 EXPORT_SYMBOL(lustre_groups_from_list);
97
98 /* groups_sort() is copied from linux kernel! */
99 /* a simple shell-metzner sort */
100 void lustre_groups_sort(struct group_info *group_info)
101 {
102         int base, max, stride;
103         int gidsetsize = group_info->ngroups;
104
105         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
106                 ; /* nothing */
107         stride /= 3;
108
109         while (stride) {
110                 max = gidsetsize - stride;
111                 for (base = 0; base < max; base++) {
112                         int left = base;
113                         int right = left + stride;
114                         gid_t tmp = from_kgid(&init_user_ns,
115                                               CFS_GROUP_AT(group_info, right));
116
117                         while (left >= 0 &&
118                                tmp < from_kgid(&init_user_ns,
119                                                CFS_GROUP_AT(group_info, left))) {
120                                 CFS_GROUP_AT(group_info, right) =
121                                         CFS_GROUP_AT(group_info, left);
122                                 right = left;
123                                 left -= stride;
124                         }
125                         CFS_GROUP_AT(group_info, right) =
126                                                 make_kgid(&init_user_ns, tmp);
127                 }
128                 stride /= 3;
129         }
130 }
131 EXPORT_SYMBOL(lustre_groups_sort);
132
133 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
134 {
135         int rc = 1;
136
137         if (grp != mu->uc_fsgid) {
138                 struct group_info *group_info = NULL;
139
140                 if (mu->uc_ginfo || !mu->uc_identity ||
141                     mu->uc_valid == UCRED_OLD)
142                         if (grp == mu->uc_suppgids[0] ||
143                             grp == mu->uc_suppgids[1])
144                                 return 1;
145
146                 if (mu->uc_ginfo)
147                         group_info = mu->uc_ginfo;
148                 else if (mu->uc_identity)
149                         group_info = mu->uc_identity->mi_ginfo;
150
151                 if (!group_info)
152                         return 0;
153
154                 atomic_inc(&group_info->usage);
155                 rc = lustre_groups_search(group_info, grp);
156                 if (atomic_dec_and_test(&group_info->usage))
157                         groups_free(group_info);
158         }
159         return rc;
160 }
161 EXPORT_SYMBOL(lustre_in_group_p);