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, 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 <lustre_idmap.h>
46 #include <md_object.h>
47 #include <obd_support.h>
49 #define lustre_get_group_info(group_info) do { \
50 atomic_inc(&(group_info)->usage); \
53 #define lustre_put_group_info(group_info) do { \
54 if (atomic_dec_and_test(&(group_info)->usage)) \
55 groups_free(group_info); \
59 * groups_search() is copied from linux kernel!
62 static int lustre_groups_search(group_info_t *group_info,
71 right = group_info->ngroups;
72 while (left < right) {
73 int mid = (left + right) / 2;
75 from_kgid(&init_user_ns, CFS_GROUP_AT(group_info, mid));
87 void lustre_groups_from_list(group_info_t *ginfo, gid_t *glist)
90 int count = ginfo->ngroups;
92 /* fill group_info from gid array */
93 for (i = 0; i < ginfo->nblocks && count > 0; i++) {
94 int cp_count = min(CFS_NGROUPS_PER_BLOCK, count);
95 int off = i * CFS_NGROUPS_PER_BLOCK;
96 int len = cp_count * sizeof(*glist);
98 memcpy(ginfo->blocks[i], glist + off, len);
102 EXPORT_SYMBOL(lustre_groups_from_list);
104 /* groups_sort() is copied from linux kernel! */
105 /* a simple shell-metzner sort */
106 void lustre_groups_sort(group_info_t *group_info)
108 int base, max, stride;
109 int gidsetsize = group_info->ngroups;
111 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
116 max = gidsetsize - stride;
117 for (base = 0; base < max; base++) {
119 int right = left + stride;
120 gid_t tmp = from_kgid(&init_user_ns,
121 CFS_GROUP_AT(group_info, right));
124 tmp < from_kgid(&init_user_ns,
125 CFS_GROUP_AT(group_info, left))) {
126 CFS_GROUP_AT(group_info, right) =
127 CFS_GROUP_AT(group_info, left);
131 CFS_GROUP_AT(group_info, right) =
132 make_kgid(&init_user_ns, tmp);
137 EXPORT_SYMBOL(lustre_groups_sort);
139 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
143 if (grp != mu->uc_fsgid) {
144 group_info_t *group_info = NULL;
146 if (mu->uc_ginfo || !mu->uc_identity ||
147 mu->uc_valid == UCRED_OLD)
148 if (grp == mu->uc_suppgids[0] ||
149 grp == mu->uc_suppgids[1])
153 group_info = mu->uc_ginfo;
154 else if (mu->uc_identity)
155 group_info = mu->uc_identity->mi_ginfo;
160 lustre_get_group_info(group_info);
161 rc = lustre_groups_search(group_info, grp);
162 lustre_put_group_info(group_info);
166 EXPORT_SYMBOL(lustre_in_group_p);
168 struct lustre_idmap_entry {
169 struct list_head lie_rmt_uid_hash; /* hashed as lie_rmt_uid; */
170 struct list_head lie_lcl_uid_hash; /* hashed as lie_lcl_uid; */
171 struct list_head lie_rmt_gid_hash; /* hashed as lie_rmt_gid; */
172 struct list_head lie_lcl_gid_hash; /* hashed as lie_lcl_gid; */
173 uid_t lie_rmt_uid; /* remote uid */
174 uid_t lie_lcl_uid; /* local uid */
175 gid_t lie_rmt_gid; /* remote gid */
176 gid_t lie_lcl_gid; /* local gid */
179 static inline __u32 lustre_idmap_hashfunc(__u32 id)
181 return id & (CFS_IDMAP_HASHSIZE - 1);
185 struct lustre_idmap_entry *idmap_entry_alloc(uid_t rmt_uid, uid_t lcl_uid,
186 gid_t rmt_gid, gid_t lcl_gid)
188 struct lustre_idmap_entry *e;
194 INIT_LIST_HEAD(&e->lie_rmt_uid_hash);
195 INIT_LIST_HEAD(&e->lie_lcl_uid_hash);
196 INIT_LIST_HEAD(&e->lie_rmt_gid_hash);
197 INIT_LIST_HEAD(&e->lie_lcl_gid_hash);
198 e->lie_rmt_uid = rmt_uid;
199 e->lie_lcl_uid = lcl_uid;
200 e->lie_rmt_gid = rmt_gid;
201 e->lie_lcl_gid = lcl_gid;
206 static void idmap_entry_free(struct lustre_idmap_entry *e)
208 if (!list_empty(&e->lie_rmt_uid_hash))
209 list_del(&e->lie_rmt_uid_hash);
210 if (!list_empty(&e->lie_lcl_uid_hash))
211 list_del(&e->lie_lcl_uid_hash);
212 if (!list_empty(&e->lie_rmt_gid_hash))
213 list_del(&e->lie_rmt_gid_hash);
214 if (!list_empty(&e->lie_lcl_gid_hash))
215 list_del(&e->lie_lcl_gid_hash);
221 * NULL: not found entry
222 * ERR_PTR(-EACCES): found 1(remote):N(local) mapped entry
223 * others: found normal entry
226 struct lustre_idmap_entry *idmap_search_entry(struct lustre_idmap_table *t,
227 uid_t rmt_uid, uid_t lcl_uid,
228 gid_t rmt_gid, gid_t lcl_gid)
230 struct list_head *head;
231 struct lustre_idmap_entry *e;
233 head = &t->lit_idmaps[RMT_UIDMAP_IDX][lustre_idmap_hashfunc(rmt_uid)];
234 list_for_each_entry(e, head, lie_rmt_uid_hash)
235 if (e->lie_rmt_uid == rmt_uid) {
236 if (e->lie_lcl_uid == lcl_uid) {
237 if (e->lie_rmt_gid == rmt_gid &&
238 e->lie_lcl_gid == lcl_gid)
239 /* must be quaternion match */
242 /* 1:N uid mapping */
243 CERROR("rmt uid %u already be mapped to %u"
244 " (new %u)\n", e->lie_rmt_uid,
245 e->lie_lcl_uid, lcl_uid);
246 return ERR_PTR(-EACCES);
250 head = &t->lit_idmaps[RMT_GIDMAP_IDX][lustre_idmap_hashfunc(rmt_gid)];
251 list_for_each_entry(e, head, lie_rmt_gid_hash)
252 if (e->lie_rmt_gid == rmt_gid) {
253 if (e->lie_lcl_gid == lcl_gid) {
254 if (unlikely(e->lie_rmt_uid == rmt_uid &&
255 e->lie_lcl_uid == lcl_uid))
256 /* after uid mapping search above,
257 * we should never come here */
260 /* 1:N gid mapping */
261 CERROR("rmt gid %u already be mapped to %u"
262 " (new %u)\n", e->lie_rmt_gid,
263 e->lie_lcl_gid, lcl_gid);
264 return ERR_PTR(-EACCES);
271 static __u32 idmap_lookup_uid(struct list_head *hash, int reverse,
274 struct list_head *head = &hash[lustre_idmap_hashfunc(uid)];
275 struct lustre_idmap_entry *e;
278 list_for_each_entry(e, head, lie_rmt_uid_hash)
279 if (e->lie_rmt_uid == uid)
280 return e->lie_lcl_uid;
282 list_for_each_entry(e, head, lie_lcl_uid_hash)
283 if (e->lie_lcl_uid == uid)
284 return e->lie_rmt_uid;
287 return CFS_IDMAP_NOTFOUND;
290 static __u32 idmap_lookup_gid(struct list_head *hash, int reverse, __u32 gid)
292 struct list_head *head = &hash[lustre_idmap_hashfunc(gid)];
293 struct lustre_idmap_entry *e;
296 list_for_each_entry(e, head, lie_rmt_gid_hash)
297 if (e->lie_rmt_gid == gid)
298 return e->lie_lcl_gid;
300 list_for_each_entry(e, head, lie_lcl_gid_hash)
301 if (e->lie_lcl_gid == gid)
302 return e->lie_rmt_gid;
305 return CFS_IDMAP_NOTFOUND;
308 int lustre_idmap_add(struct lustre_idmap_table *t,
309 uid_t ruid, uid_t luid,
310 gid_t rgid, gid_t lgid)
312 struct lustre_idmap_entry *e0, *e1;
316 spin_lock(&t->lit_lock);
317 e0 = idmap_search_entry(t, ruid, luid, rgid, lgid);
318 spin_unlock(&t->lit_lock);
320 e0 = idmap_entry_alloc(ruid, luid, rgid, lgid);
324 spin_lock(&t->lit_lock);
325 e1 = idmap_search_entry(t, ruid, luid, rgid, lgid);
327 list_add_tail(&e0->lie_rmt_uid_hash,
328 &t->lit_idmaps[RMT_UIDMAP_IDX]
329 [lustre_idmap_hashfunc(ruid)]);
330 list_add_tail(&e0->lie_lcl_uid_hash,
331 &t->lit_idmaps[LCL_UIDMAP_IDX]
332 [lustre_idmap_hashfunc(luid)]);
333 list_add_tail(&e0->lie_rmt_gid_hash,
334 &t->lit_idmaps[RMT_GIDMAP_IDX]
335 [lustre_idmap_hashfunc(rgid)]);
336 list_add_tail(&e0->lie_lcl_gid_hash,
337 &t->lit_idmaps[LCL_GIDMAP_IDX]
338 [lustre_idmap_hashfunc(lgid)]);
340 spin_unlock(&t->lit_lock);
342 idmap_entry_free(e0);
346 } else if (IS_ERR(e0)) {
352 EXPORT_SYMBOL(lustre_idmap_add);
354 int lustre_idmap_del(struct lustre_idmap_table *t,
355 uid_t ruid, uid_t luid,
356 gid_t rgid, gid_t lgid)
358 struct lustre_idmap_entry *e;
363 spin_lock(&t->lit_lock);
364 e = idmap_search_entry(t, ruid, luid, rgid, lgid);
369 spin_unlock(&t->lit_lock);
373 EXPORT_SYMBOL(lustre_idmap_del);
375 int lustre_idmap_lookup_uid(struct lu_ucred *mu,
376 struct lustre_idmap_table *t,
377 int reverse, uid_t uid)
379 struct list_head *hash;
381 if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
383 if (uid == mu->uc_o_uid)
385 else if (uid == mu->uc_o_fsuid)
388 if (uid == mu->uc_uid)
390 else if (uid == mu->uc_fsuid)
391 return mu->uc_o_fsuid;
396 return CFS_IDMAP_NOTFOUND;
398 hash = t->lit_idmaps[reverse ? LCL_UIDMAP_IDX : RMT_UIDMAP_IDX];
400 spin_lock(&t->lit_lock);
401 uid = idmap_lookup_uid(hash, reverse, uid);
402 spin_unlock(&t->lit_lock);
406 EXPORT_SYMBOL(lustre_idmap_lookup_uid);
408 int lustre_idmap_lookup_gid(struct lu_ucred *mu, struct lustre_idmap_table *t,
409 int reverse, gid_t gid)
411 struct list_head *hash;
413 if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
415 if (gid == mu->uc_o_gid)
417 else if (gid == mu->uc_o_fsgid)
420 if (gid == mu->uc_gid)
422 else if (gid == mu->uc_fsgid)
423 return mu->uc_o_fsgid;
428 return CFS_IDMAP_NOTFOUND;
430 hash = t->lit_idmaps[reverse ? LCL_GIDMAP_IDX : RMT_GIDMAP_IDX];
432 spin_lock(&t->lit_lock);
433 gid = idmap_lookup_gid(hash, reverse, gid);
434 spin_unlock(&t->lit_lock);
438 EXPORT_SYMBOL(lustre_idmap_lookup_gid);
440 struct lustre_idmap_table *lustre_idmap_init(void)
442 struct lustre_idmap_table *t;
446 if(unlikely(t == NULL))
447 return (ERR_PTR(-ENOMEM));
449 spin_lock_init(&t->lit_lock);
450 for (i = 0; i < ARRAY_SIZE(t->lit_idmaps); i++)
451 for (j = 0; j < ARRAY_SIZE(t->lit_idmaps[i]); j++)
452 INIT_LIST_HEAD(&t->lit_idmaps[i][j]);
456 EXPORT_SYMBOL(lustre_idmap_init);
458 void lustre_idmap_fini(struct lustre_idmap_table *t)
460 struct list_head *list;
461 struct lustre_idmap_entry *e;
465 list = t->lit_idmaps[RMT_UIDMAP_IDX];
466 spin_lock(&t->lit_lock);
467 for (i = 0; i < CFS_IDMAP_HASHSIZE; i++)
468 while (!list_empty(&list[i])) {
469 e = list_entry(list[i].next,
470 struct lustre_idmap_entry,
474 spin_unlock(&t->lit_lock);
478 EXPORT_SYMBOL(lustre_idmap_fini);