Whamcloud - gitweb
9f85b9849cbcc10c33ee298143dce8b6cb2c5f51
[fs/lustre-release.git] / lustre / obdclass / idmap.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *   lustre/obdclass/idmap.c
5  *   Lustre user identity mapping.
6  *   Author: Fan Yong <fanyong@clusterfs.com>
7  *
8  * Copyright (C) 2004-2007 Cluster File Systems, Inc.
9  *
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #ifndef EXPORT_SYMTAB
28 # define EXPORT_SYMTAB
29 #endif
30
31 #define DEBUG_SUBSYSTEM S_SEC
32
33 #include <lustre_idmap.h>
34 #include <obd_support.h>
35
36 #define lustre_get_group_info(group_info) do {             \
37         atomic_inc(&(group_info)->usage);                  \
38 } while (0)
39
40 #define lustre_put_group_info(group_info) do {             \
41         if (atomic_dec_and_test(&(group_info)->usage))     \
42                 groups_free(group_info);                   \
43 } while (0)
44
45 /*
46  * groups_search() is copied from linux kernel!
47  * A simple bsearch.
48  */
49 static int lustre_groups_search(struct group_info *group_info, gid_t grp)
50 {
51         int left, right;
52
53         if (!group_info)
54                 return 0;
55
56         left = 0;
57         right = group_info->ngroups;
58         while (left < right) {
59                 int mid = (left + right) / 2;
60                 int cmp = grp - CFS_GROUP_AT(group_info, mid);
61
62                 if (cmp > 0)
63                         left = mid + 1;
64                 else if (cmp < 0)
65                         right = mid;
66                 else
67                         return 1;
68         }
69         return 0;
70 }
71
72 void lustre_groups_from_list(struct group_info *ginfo, gid_t *glist)
73 {
74         int i;
75         int count = ginfo->ngroups;
76
77         /* fill group_info from gid array */
78         for (i = 0; i < ginfo->nblocks && count > 0; i++) {
79                 int cp_count = min(CFS_NGROUPS_PER_BLOCK, count);
80                 int off = i * CFS_NGROUPS_PER_BLOCK;
81                 int len = cp_count * sizeof(*glist);
82
83                 memcpy(ginfo->blocks[i], glist + off, len);
84                 count -= cp_count;
85         }
86 }
87 EXPORT_SYMBOL(lustre_groups_from_list);
88
89 /* groups_sort() is copied from linux kernel! */
90 /* a simple shell-metzner sort */
91 void lustre_groups_sort(struct group_info *group_info)
92 {
93         int base, max, stride;
94         int gidsetsize = group_info->ngroups;
95
96         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
97                 ; /* nothing */
98         stride /= 3;
99
100         while (stride) {
101                 max = gidsetsize - stride;
102                 for (base = 0; base < max; base++) {
103                         int left = base;
104                         int right = left + stride;
105                         gid_t tmp = CFS_GROUP_AT(group_info, right);
106
107                         while (left >= 0 &&
108                                CFS_GROUP_AT(group_info, left) > tmp) {
109                                 CFS_GROUP_AT(group_info, right) =
110                                     CFS_GROUP_AT(group_info, left);
111                                 right = left;
112                                 left -= stride;
113                         }
114                         CFS_GROUP_AT(group_info, right) = tmp;
115                 }
116                 stride /= 3;
117         }
118 }
119 EXPORT_SYMBOL(lustre_groups_sort);
120
121 int lustre_in_group_p(struct md_ucred *mu, gid_t grp)
122 {
123         int rc = 1;
124
125         if (grp != mu->mu_fsgid) {
126                 struct group_info *group_info = NULL;
127
128                 if (mu->mu_ginfo || !mu->mu_identity ||
129                     mu->mu_valid == UCRED_OLD)
130                         if (grp == mu->mu_suppgids[0] ||
131                             grp == mu->mu_suppgids[1])
132                                 return 1;
133
134                 if (mu->mu_ginfo)
135                         group_info = mu->mu_ginfo;
136                 else if (mu->mu_identity)
137                         group_info = mu->mu_identity->mi_ginfo;
138
139                 if (!group_info)
140                         return 0;
141
142                 lustre_get_group_info(group_info);
143                 rc = lustre_groups_search(group_info, grp);
144                 lustre_put_group_info(group_info);
145         }
146         return rc;
147 }
148 EXPORT_SYMBOL(lustre_in_group_p);
149
150 struct lustre_idmap_entry {
151         struct list_head lie_rmt_uid_hash; /* hashed as lie_rmt_uid; */
152         struct list_head lie_lcl_uid_hash; /* hashed as lie_lcl_uid; */
153         struct list_head lie_rmt_gid_hash; /* hashed as lie_rmt_gid; */
154         struct list_head lie_lcl_gid_hash; /* hashed as lie_lcl_gid; */
155         uid_t            lie_rmt_uid;      /* remote uid */
156         uid_t            lie_lcl_uid;      /* local uid */
157         gid_t            lie_rmt_gid;      /* remote gid */
158         gid_t            lie_lcl_gid;      /* local gid */
159 };
160
161 static inline __u32 lustre_idmap_hashfunc(__u32 id)
162 {
163         return id & (CFS_IDMAP_HASHSIZE - 1);
164 }
165
166 static
167 struct lustre_idmap_entry *idmap_entry_alloc(uid_t rmt_uid, uid_t lcl_uid,
168                                              gid_t rmt_gid, gid_t lcl_gid)
169 {
170         struct lustre_idmap_entry *e;
171
172         OBD_ALLOC_PTR(e);
173         if (e == NULL)
174                 return NULL;
175
176         INIT_LIST_HEAD(&e->lie_rmt_uid_hash);
177         INIT_LIST_HEAD(&e->lie_lcl_uid_hash);
178         INIT_LIST_HEAD(&e->lie_rmt_gid_hash);
179         INIT_LIST_HEAD(&e->lie_lcl_gid_hash);
180         e->lie_rmt_uid = rmt_uid;
181         e->lie_lcl_uid = lcl_uid;
182         e->lie_rmt_gid = rmt_gid;
183         e->lie_lcl_gid = lcl_gid;
184
185         return e;
186 }
187
188 static void idmap_entry_free(struct lustre_idmap_entry *e)
189 {
190         if (!list_empty(&e->lie_rmt_uid_hash))
191                 list_del(&e->lie_rmt_uid_hash);
192         if (!list_empty(&e->lie_lcl_uid_hash))
193                 list_del(&e->lie_lcl_uid_hash);
194         if (!list_empty(&e->lie_rmt_gid_hash))
195                 list_del(&e->lie_rmt_gid_hash);
196         if (!list_empty(&e->lie_lcl_gid_hash))
197                 list_del(&e->lie_lcl_gid_hash);
198         OBD_FREE_PTR(e);
199 }
200
201 /*
202  * return value
203  * NULL: not found entry
204  * ERR_PTR(-EACCES): found 1(remote):N(local) mapped entry
205  * others: found normal entry
206  */
207 static
208 struct lustre_idmap_entry *idmap_search_entry(struct lustre_idmap_table *t,
209                                               uid_t rmt_uid, uid_t lcl_uid,
210                                               gid_t rmt_gid, gid_t lcl_gid)
211 {
212         struct list_head *head;
213         struct lustre_idmap_entry *e;
214
215         head = &t->lit_idmaps[RMT_UIDMAP_IDX][lustre_idmap_hashfunc(rmt_uid)];
216         list_for_each_entry(e, head, lie_rmt_uid_hash)
217                 if (e->lie_rmt_uid == rmt_uid) {
218                         if (e->lie_lcl_uid == lcl_uid) {
219                                 if (e->lie_rmt_gid == rmt_gid &&
220                                     e->lie_lcl_gid == lcl_gid)
221                                         /* must be quaternion match */
222                                         return e;
223                         } else {
224                                 /* 1:N uid mapping */
225                                 CERROR("rmt uid %u already be mapped to %u"
226                                        " (new %u)\n", e->lie_rmt_uid,
227                                        e->lie_lcl_uid, lcl_uid);
228                                 return ERR_PTR(-EACCES);
229                         }
230                 }
231
232         head = &t->lit_idmaps[RMT_GIDMAP_IDX][lustre_idmap_hashfunc(rmt_gid)];
233         list_for_each_entry(e, head, lie_rmt_gid_hash)
234                 if (e->lie_rmt_gid == rmt_gid) {
235                         if (e->lie_lcl_gid == lcl_gid) {
236                                 if (unlikely(e->lie_rmt_uid == rmt_uid &&
237                                     e->lie_lcl_uid == lcl_uid))
238                                         /* after uid mapping search above,
239                                          * we should never come here */
240                                         LBUG();
241                         } else {
242                                 /* 1:N gid mapping */
243                                 CERROR("rmt gid %u already be mapped to %u"
244                                        " (new %u)\n", e->lie_rmt_gid,
245                                        e->lie_lcl_gid, lcl_gid);
246                                 return ERR_PTR(-EACCES);
247                         }
248                 }
249
250         return NULL;
251 }
252
253 static __u32 idmap_lookup_uid(struct list_head *hash, int reverse, __u32 uid)
254 {
255         struct list_head *head = &hash[lustre_idmap_hashfunc(uid)];
256         struct lustre_idmap_entry *e;
257
258         if (!reverse) {
259                 list_for_each_entry(e, head, lie_rmt_uid_hash)
260                         if (e->lie_rmt_uid == uid)
261                                 return e->lie_lcl_uid;
262         } else {
263                 list_for_each_entry(e, head, lie_lcl_uid_hash)
264                         if (e->lie_lcl_uid == uid)
265                                 return e->lie_rmt_uid;
266         }
267
268         return CFS_IDMAP_NOTFOUND;
269 }
270
271 static __u32 idmap_lookup_gid(struct list_head *hash, int reverse, __u32 gid)
272 {
273         struct list_head *head = &hash[lustre_idmap_hashfunc(gid)];
274         struct lustre_idmap_entry *e;
275
276         if (!reverse) {
277                 list_for_each_entry(e, head, lie_rmt_gid_hash)
278                         if (e->lie_rmt_gid == gid)
279                                 return e->lie_lcl_gid;
280         } else {
281                 list_for_each_entry(e, head, lie_lcl_gid_hash)
282                         if (e->lie_lcl_gid == gid)
283                                 return e->lie_rmt_gid;
284         }
285
286         return CFS_IDMAP_NOTFOUND;
287 }
288
289 int lustre_idmap_add(struct lustre_idmap_table *t,
290                      uid_t ruid, uid_t luid,
291                      gid_t rgid, gid_t lgid)
292 {
293         struct lustre_idmap_entry *e0, *e1;
294
295         LASSERT(t);
296
297         spin_lock(&t->lit_lock);
298         e0 = idmap_search_entry(t, ruid, luid, rgid, lgid);
299         spin_unlock(&t->lit_lock);
300         if (!e0) {
301                 e0 = idmap_entry_alloc(ruid, luid, rgid, lgid);
302                 if (!e0)
303                         return -ENOMEM;
304
305                 spin_lock(&t->lit_lock);
306                 e1 = idmap_search_entry(t, ruid, luid, rgid, lgid);
307                 if (e1 == NULL) {
308                         list_add_tail(&e0->lie_rmt_uid_hash,
309                                       &t->lit_idmaps[RMT_UIDMAP_IDX]
310                                         [lustre_idmap_hashfunc(ruid)]);
311                         list_add_tail(&e0->lie_lcl_uid_hash,
312                                       &t->lit_idmaps[LCL_UIDMAP_IDX]
313                                         [lustre_idmap_hashfunc(luid)]);
314                         list_add_tail(&e0->lie_rmt_gid_hash,
315                                       &t->lit_idmaps[RMT_GIDMAP_IDX]
316                                         [lustre_idmap_hashfunc(rgid)]);
317                         list_add_tail(&e0->lie_lcl_gid_hash,
318                                       &t->lit_idmaps[LCL_GIDMAP_IDX]
319                                         [lustre_idmap_hashfunc(lgid)]);
320                 } 
321                 spin_unlock(&t->lit_lock);
322                 if (e1 != NULL) {
323                         idmap_entry_free(e0);
324                         if (IS_ERR(e1))
325                                 return PTR_ERR(e1);
326                 }
327         } else if (IS_ERR(e0)) {
328                 return PTR_ERR(e0);
329         }
330
331         return 0;
332 }
333 EXPORT_SYMBOL(lustre_idmap_add);
334
335 int lustre_idmap_del(struct lustre_idmap_table *t,
336                     uid_t ruid, uid_t luid,
337                     gid_t rgid, gid_t lgid)
338 {
339         struct lustre_idmap_entry *e;
340         int rc = 0;
341
342         LASSERT(t);
343
344         spin_lock(&t->lit_lock);
345         e = idmap_search_entry(t, ruid, luid, rgid, lgid);
346         if (IS_ERR(e))
347                 rc = PTR_ERR(e);
348         else if (e)
349                 idmap_entry_free(e);
350         spin_unlock(&t->lit_lock);
351
352         return rc;
353 }
354 EXPORT_SYMBOL(lustre_idmap_del);
355
356 int lustre_idmap_lookup_uid(struct md_ucred *mu,
357                             struct lustre_idmap_table *t,
358                             int reverse, uid_t uid)
359 {
360         struct list_head *hash;
361
362         if (mu && (mu->mu_valid == UCRED_OLD || mu->mu_valid == UCRED_NEW)) {
363                 if (!reverse) {
364                         if (uid == mu->mu_o_uid)
365                                 return mu->mu_uid;
366                         else if (uid == mu->mu_o_fsuid)
367                                 return mu->mu_fsuid;
368                 } else {
369                         if (uid == mu->mu_uid)
370                                 return mu->mu_o_uid;
371                         else if (uid == mu->mu_fsuid)
372                                 return mu->mu_o_fsuid;
373                 }
374         }
375
376         if (t == NULL)
377                 return CFS_IDMAP_NOTFOUND;
378
379         hash = t->lit_idmaps[reverse ? LCL_UIDMAP_IDX : RMT_UIDMAP_IDX];
380
381         spin_lock(&t->lit_lock);
382         uid = idmap_lookup_uid(hash, reverse, uid);
383         spin_unlock(&t->lit_lock);
384
385         return uid;
386 }
387 EXPORT_SYMBOL(lustre_idmap_lookup_uid);
388
389 int lustre_idmap_lookup_gid(struct md_ucred *mu, struct lustre_idmap_table *t,
390                             int reverse, gid_t gid)
391 {
392         struct list_head *hash;
393
394         if (mu && (mu->mu_valid == UCRED_OLD || mu->mu_valid == UCRED_NEW)) {
395                 if (!reverse) {
396                         if (gid == mu->mu_o_gid)
397                                 return mu->mu_gid;
398                         else if (gid == mu->mu_o_fsgid)
399                                 return mu->mu_fsgid;
400                 } else {
401                         if (gid == mu->mu_gid)
402                                 return mu->mu_o_gid;
403                         else if (gid == mu->mu_fsgid)
404                                 return mu->mu_o_fsgid;
405                 }
406         }
407
408         if (t == NULL)
409                 return CFS_IDMAP_NOTFOUND;
410
411         hash = t->lit_idmaps[reverse ? LCL_GIDMAP_IDX : RMT_GIDMAP_IDX];
412
413         spin_lock(&t->lit_lock);
414         gid = idmap_lookup_gid(hash, reverse, gid);
415         spin_unlock(&t->lit_lock);
416
417         return gid;
418 }
419 EXPORT_SYMBOL(lustre_idmap_lookup_gid);
420
421 struct lustre_idmap_table *lustre_idmap_init(void)
422 {
423         struct lustre_idmap_table *t;
424         int i, j;
425
426         OBD_ALLOC_PTR(t);
427         if(unlikely(t == NULL))
428                 return (ERR_PTR(-ENOMEM));
429
430         spin_lock_init(&t->lit_lock);
431         for (i = 0; i < ARRAY_SIZE(t->lit_idmaps); i++)
432                 for (j = 0; j < ARRAY_SIZE(t->lit_idmaps[i]); j++)
433                         INIT_LIST_HEAD(&t->lit_idmaps[i][j]);
434
435         return t;
436 }
437 EXPORT_SYMBOL(lustre_idmap_init);
438
439 void lustre_idmap_fini(struct lustre_idmap_table *t)
440 {
441         struct list_head *list;
442         struct lustre_idmap_entry *e;
443         int i;
444         LASSERT(t);
445
446         list = t->lit_idmaps[RMT_UIDMAP_IDX];
447         spin_lock(&t->lit_lock);
448         for (i = 0; i < CFS_IDMAP_HASHSIZE; i++)
449                 while (!list_empty(&list[i])) {
450                         e = list_entry(list[i].next, struct lustre_idmap_entry,
451                                        lie_rmt_uid_hash);
452                         idmap_entry_free(e);
453                 }
454         spin_unlock(&t->lit_lock);
455
456         OBD_FREE_PTR(t);
457 }
458 EXPORT_SYMBOL(lustre_idmap_fini);