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 <libcfs/lucache.h>
47 #include <lustre_idmap.h>
48 #include <md_object.h>
49 #include <obd_support.h>
51 #define lustre_get_group_info(group_info) do { \
52 atomic_inc(&(group_info)->usage); \
55 #define lustre_put_group_info(group_info) do { \
56 if (atomic_dec_and_test(&(group_info)->usage)) \
57 groups_free(group_info); \
61 * groups_search() is copied from linux kernel!
64 static int lustre_groups_search(struct group_info *group_info,
73 right = group_info->ngroups;
74 while (left < right) {
75 int mid = (left + right) / 2;
77 from_kgid(&init_user_ns, CFS_GROUP_AT(group_info, mid));
89 void lustre_groups_from_list(struct group_info *ginfo, gid_t *glist)
92 int count = ginfo->ngroups;
94 /* fill group_info from gid array */
95 for (i = 0; i < ginfo->nblocks && count > 0; i++) {
96 int cp_count = min(CFS_NGROUPS_PER_BLOCK, count);
97 int off = i * CFS_NGROUPS_PER_BLOCK;
98 int len = cp_count * sizeof(*glist);
100 memcpy(ginfo->blocks[i], glist + off, len);
104 EXPORT_SYMBOL(lustre_groups_from_list);
106 /* groups_sort() is copied from linux kernel! */
107 /* a simple shell-metzner sort */
108 void lustre_groups_sort(struct group_info *group_info)
110 int base, max, stride;
111 int gidsetsize = group_info->ngroups;
113 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
118 max = gidsetsize - stride;
119 for (base = 0; base < max; base++) {
121 int right = left + stride;
122 gid_t tmp = from_kgid(&init_user_ns,
123 CFS_GROUP_AT(group_info, right));
126 tmp < from_kgid(&init_user_ns,
127 CFS_GROUP_AT(group_info, left))) {
128 CFS_GROUP_AT(group_info, right) =
129 CFS_GROUP_AT(group_info, left);
133 CFS_GROUP_AT(group_info, right) =
134 make_kgid(&init_user_ns, tmp);
139 EXPORT_SYMBOL(lustre_groups_sort);
141 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
145 if (grp != mu->uc_fsgid) {
146 struct group_info *group_info = NULL;
148 if (mu->uc_ginfo || !mu->uc_identity ||
149 mu->uc_valid == UCRED_OLD)
150 if (grp == mu->uc_suppgids[0] ||
151 grp == mu->uc_suppgids[1])
155 group_info = mu->uc_ginfo;
156 else if (mu->uc_identity)
157 group_info = mu->uc_identity->mi_ginfo;
162 lustre_get_group_info(group_info);
163 rc = lustre_groups_search(group_info, grp);
164 lustre_put_group_info(group_info);
168 EXPORT_SYMBOL(lustre_in_group_p);
170 struct lustre_idmap_entry {
171 struct list_head lie_rmt_uid_hash; /* hashed as lie_rmt_uid; */
172 struct list_head lie_lcl_uid_hash; /* hashed as lie_lcl_uid; */
173 struct list_head lie_rmt_gid_hash; /* hashed as lie_rmt_gid; */
174 struct list_head lie_lcl_gid_hash; /* hashed as lie_lcl_gid; */
175 uid_t lie_rmt_uid; /* remote uid */
176 uid_t lie_lcl_uid; /* local uid */
177 gid_t lie_rmt_gid; /* remote gid */
178 gid_t lie_lcl_gid; /* local gid */
181 static inline __u32 lustre_idmap_hashfunc(__u32 id)
183 return id & (CFS_IDMAP_HASHSIZE - 1);
187 struct lustre_idmap_entry *idmap_entry_alloc(uid_t rmt_uid, uid_t lcl_uid,
188 gid_t rmt_gid, gid_t lcl_gid)
190 struct lustre_idmap_entry *e;
196 INIT_LIST_HEAD(&e->lie_rmt_uid_hash);
197 INIT_LIST_HEAD(&e->lie_lcl_uid_hash);
198 INIT_LIST_HEAD(&e->lie_rmt_gid_hash);
199 INIT_LIST_HEAD(&e->lie_lcl_gid_hash);
200 e->lie_rmt_uid = rmt_uid;
201 e->lie_lcl_uid = lcl_uid;
202 e->lie_rmt_gid = rmt_gid;
203 e->lie_lcl_gid = lcl_gid;
208 static void idmap_entry_free(struct lustre_idmap_entry *e)
210 list_del(&e->lie_rmt_uid_hash);
211 list_del(&e->lie_lcl_uid_hash);
212 list_del(&e->lie_rmt_gid_hash);
213 list_del(&e->lie_lcl_gid_hash);
219 * NULL: not found entry
220 * ERR_PTR(-EACCES): found 1(remote):N(local) mapped entry
221 * others: found normal entry
224 struct lustre_idmap_entry *idmap_search_entry(struct lustre_idmap_table *t,
225 uid_t rmt_uid, uid_t lcl_uid,
226 gid_t rmt_gid, gid_t lcl_gid)
228 struct list_head *head;
229 struct lustre_idmap_entry *e;
231 head = &t->lit_idmaps[RMT_UIDMAP_IDX][lustre_idmap_hashfunc(rmt_uid)];
232 list_for_each_entry(e, head, lie_rmt_uid_hash)
233 if (e->lie_rmt_uid == rmt_uid) {
234 if (e->lie_lcl_uid == lcl_uid) {
235 if (e->lie_rmt_gid == rmt_gid &&
236 e->lie_lcl_gid == lcl_gid)
237 /* must be quaternion match */
240 /* 1:N uid mapping */
241 CERROR("rmt uid %u already be mapped to %u"
242 " (new %u)\n", e->lie_rmt_uid,
243 e->lie_lcl_uid, lcl_uid);
244 return ERR_PTR(-EACCES);
248 head = &t->lit_idmaps[RMT_GIDMAP_IDX][lustre_idmap_hashfunc(rmt_gid)];
249 list_for_each_entry(e, head, lie_rmt_gid_hash)
250 if (e->lie_rmt_gid == rmt_gid) {
251 if (e->lie_lcl_gid == lcl_gid) {
252 if (unlikely(e->lie_rmt_uid == rmt_uid &&
253 e->lie_lcl_uid == lcl_uid))
254 /* after uid mapping search above,
255 * we should never come here */
258 /* 1:N gid mapping */
259 CERROR("rmt gid %u already be mapped to %u"
260 " (new %u)\n", e->lie_rmt_gid,
261 e->lie_lcl_gid, lcl_gid);
262 return ERR_PTR(-EACCES);
269 static __u32 idmap_lookup_uid(struct list_head *hash, int reverse,
272 struct list_head *head = &hash[lustre_idmap_hashfunc(uid)];
273 struct lustre_idmap_entry *e;
276 list_for_each_entry(e, head, lie_rmt_uid_hash)
277 if (e->lie_rmt_uid == uid)
278 return e->lie_lcl_uid;
280 list_for_each_entry(e, head, lie_lcl_uid_hash)
281 if (e->lie_lcl_uid == uid)
282 return e->lie_rmt_uid;
285 return CFS_IDMAP_NOTFOUND;
288 static __u32 idmap_lookup_gid(struct list_head *hash, int reverse, __u32 gid)
290 struct list_head *head = &hash[lustre_idmap_hashfunc(gid)];
291 struct lustre_idmap_entry *e;
294 list_for_each_entry(e, head, lie_rmt_gid_hash)
295 if (e->lie_rmt_gid == gid)
296 return e->lie_lcl_gid;
298 list_for_each_entry(e, head, lie_lcl_gid_hash)
299 if (e->lie_lcl_gid == gid)
300 return e->lie_rmt_gid;
303 return CFS_IDMAP_NOTFOUND;
306 int lustre_idmap_add(struct lustre_idmap_table *t,
307 uid_t ruid, uid_t luid,
308 gid_t rgid, gid_t lgid)
310 struct lustre_idmap_entry *e0, *e1;
314 spin_lock(&t->lit_lock);
315 e0 = idmap_search_entry(t, ruid, luid, rgid, lgid);
316 spin_unlock(&t->lit_lock);
318 e0 = idmap_entry_alloc(ruid, luid, rgid, lgid);
322 spin_lock(&t->lit_lock);
323 e1 = idmap_search_entry(t, ruid, luid, rgid, lgid);
325 list_add_tail(&e0->lie_rmt_uid_hash,
326 &t->lit_idmaps[RMT_UIDMAP_IDX]
327 [lustre_idmap_hashfunc(ruid)]);
328 list_add_tail(&e0->lie_lcl_uid_hash,
329 &t->lit_idmaps[LCL_UIDMAP_IDX]
330 [lustre_idmap_hashfunc(luid)]);
331 list_add_tail(&e0->lie_rmt_gid_hash,
332 &t->lit_idmaps[RMT_GIDMAP_IDX]
333 [lustre_idmap_hashfunc(rgid)]);
334 list_add_tail(&e0->lie_lcl_gid_hash,
335 &t->lit_idmaps[LCL_GIDMAP_IDX]
336 [lustre_idmap_hashfunc(lgid)]);
338 spin_unlock(&t->lit_lock);
340 idmap_entry_free(e0);
344 } else if (IS_ERR(e0)) {
350 EXPORT_SYMBOL(lustre_idmap_add);
352 int lustre_idmap_del(struct lustre_idmap_table *t,
353 uid_t ruid, uid_t luid,
354 gid_t rgid, gid_t lgid)
356 struct lustre_idmap_entry *e;
361 spin_lock(&t->lit_lock);
362 e = idmap_search_entry(t, ruid, luid, rgid, lgid);
367 spin_unlock(&t->lit_lock);
371 EXPORT_SYMBOL(lustre_idmap_del);
373 int lustre_idmap_lookup_uid(struct lu_ucred *mu,
374 struct lustre_idmap_table *t,
375 int reverse, uid_t uid)
377 struct list_head *hash;
379 if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
381 if (uid == mu->uc_o_uid)
383 else if (uid == mu->uc_o_fsuid)
386 if (uid == mu->uc_uid)
388 else if (uid == mu->uc_fsuid)
389 return mu->uc_o_fsuid;
394 return CFS_IDMAP_NOTFOUND;
396 hash = t->lit_idmaps[reverse ? LCL_UIDMAP_IDX : RMT_UIDMAP_IDX];
398 spin_lock(&t->lit_lock);
399 uid = idmap_lookup_uid(hash, reverse, uid);
400 spin_unlock(&t->lit_lock);
404 EXPORT_SYMBOL(lustre_idmap_lookup_uid);
406 int lustre_idmap_lookup_gid(struct lu_ucred *mu, struct lustre_idmap_table *t,
407 int reverse, gid_t gid)
409 struct list_head *hash;
411 if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
413 if (gid == mu->uc_o_gid)
415 else if (gid == mu->uc_o_fsgid)
418 if (gid == mu->uc_gid)
420 else if (gid == mu->uc_fsgid)
421 return mu->uc_o_fsgid;
426 return CFS_IDMAP_NOTFOUND;
428 hash = t->lit_idmaps[reverse ? LCL_GIDMAP_IDX : RMT_GIDMAP_IDX];
430 spin_lock(&t->lit_lock);
431 gid = idmap_lookup_gid(hash, reverse, gid);
432 spin_unlock(&t->lit_lock);
436 EXPORT_SYMBOL(lustre_idmap_lookup_gid);
438 struct lustre_idmap_table *lustre_idmap_init(void)
440 struct lustre_idmap_table *t;
444 if(unlikely(t == NULL))
445 return (ERR_PTR(-ENOMEM));
447 spin_lock_init(&t->lit_lock);
448 for (i = 0; i < ARRAY_SIZE(t->lit_idmaps); i++)
449 for (j = 0; j < ARRAY_SIZE(t->lit_idmaps[i]); j++)
450 INIT_LIST_HEAD(&t->lit_idmaps[i][j]);
454 EXPORT_SYMBOL(lustre_idmap_init);
456 void lustre_idmap_fini(struct lustre_idmap_table *t)
458 struct list_head *list;
459 struct lustre_idmap_entry *e;
463 list = t->lit_idmaps[RMT_UIDMAP_IDX];
464 spin_lock(&t->lit_lock);
465 for (i = 0; i < CFS_IDMAP_HASHSIZE; i++)
466 while (!list_empty(&list[i])) {
467 e = list_entry(list[i].next,
468 struct lustre_idmap_entry,
472 spin_unlock(&t->lit_lock);
476 EXPORT_SYMBOL(lustre_idmap_fini);