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