]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/obdclass/idmap.c
Linux 3.14
[~andy/linux] / drivers / staging / lustre / 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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/idmap.c
37  *
38  * Lustre user identity mapping.
39  *
40  * Author: Fan Yong <fanyong@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_SEC
44
45 #include <lustre_idmap.h>
46 #include <md_object.h>
47 #include <obd_support.h>
48
49 #define lustre_get_group_info(group_info) do {       \
50         atomic_inc(&(group_info)->usage);             \
51 } while (0)
52
53 #define lustre_put_group_info(group_info) do {       \
54         if (atomic_dec_and_test(&(group_info)->usage)) \
55                 groups_free(group_info);               \
56 } while (0)
57
58 /*
59  * groups_search() is copied from linux kernel!
60  * A simple bsearch.
61  */
62 static int lustre_groups_search(const struct group_info *group_info, gid_t grp)
63 {
64         int left, right;
65
66         if (!group_info)
67                 return 0;
68
69         left = 0;
70         right = group_info->ngroups;
71         while (left < right) {
72                 int mid = (left + right) / 2;
73                 int cmp = grp -
74                         from_kgid(&init_user_ns, CFS_GROUP_AT(group_info, mid));
75
76                 if (cmp > 0)
77                         left = mid + 1;
78                 else if (cmp < 0)
79                         right = mid;
80                 else
81                         return 1;
82         }
83         return 0;
84 }
85
86 void lustre_groups_from_list(struct group_info *ginfo, gid_t *glist)
87 {
88         int i;
89         int count = ginfo->ngroups;
90
91         /* fill group_info from gid array */
92         for (i = 0; i < ginfo->nblocks && count > 0; i++) {
93                 int cp_count = min(CFS_NGROUPS_PER_BLOCK, count);
94                 int off = i * CFS_NGROUPS_PER_BLOCK;
95                 int len = cp_count * sizeof(*glist);
96
97                 memcpy(ginfo->blocks[i], glist + off, len);
98                 count -= cp_count;
99         }
100 }
101 EXPORT_SYMBOL(lustre_groups_from_list);
102
103 /* groups_sort() is copied from linux kernel! */
104 /* a simple shell-metzner sort */
105 void lustre_groups_sort(struct group_info *group_info)
106 {
107         int base, max, stride;
108         int gidsetsize = group_info->ngroups;
109
110         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
111                 ; /* nothing */
112         stride /= 3;
113
114         while (stride) {
115                 max = gidsetsize - stride;
116                 for (base = 0; base < max; base++) {
117                         int left = base;
118                         int right = left + stride;
119                         gid_t tmp = from_kgid(&init_user_ns,
120                                               CFS_GROUP_AT(group_info, right));
121
122                         while (left >= 0 &&
123                                tmp < from_kgid(&init_user_ns,
124                                                CFS_GROUP_AT(group_info, left))) {
125                                 CFS_GROUP_AT(group_info, right) =
126                                     CFS_GROUP_AT(group_info, left);
127                                 right = left;
128                                 left -= stride;
129                         }
130                         CFS_GROUP_AT(group_info, right) =
131                                                 make_kgid(&init_user_ns, tmp);
132                 }
133                 stride /= 3;
134         }
135 }
136 EXPORT_SYMBOL(lustre_groups_sort);
137
138 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
139 {
140         int rc = 1;
141
142         if (grp != mu->uc_fsgid) {
143                 struct group_info *group_info = NULL;
144
145                 if (mu->uc_ginfo || !mu->uc_identity ||
146                     mu->uc_valid == UCRED_OLD)
147                         if (grp == mu->uc_suppgids[0] ||
148                             grp == mu->uc_suppgids[1])
149                                 return 1;
150
151                 if (mu->uc_ginfo)
152                         group_info = mu->uc_ginfo;
153                 else if (mu->uc_identity)
154                         group_info = mu->uc_identity->mi_ginfo;
155
156                 if (!group_info)
157                         return 0;
158
159                 lustre_get_group_info(group_info);
160                 rc = lustre_groups_search(group_info, grp);
161                 lustre_put_group_info(group_info);
162         }
163         return rc;
164 }
165 EXPORT_SYMBOL(lustre_in_group_p);
166
167 struct lustre_idmap_entry {
168         struct list_head       lie_rmt_uid_hash; /* hashed as lie_rmt_uid; */
169         struct list_head       lie_lcl_uid_hash; /* hashed as lie_lcl_uid; */
170         struct list_head       lie_rmt_gid_hash; /* hashed as lie_rmt_gid; */
171         struct list_head       lie_lcl_gid_hash; /* hashed as lie_lcl_gid; */
172         uid_t       lie_rmt_uid;      /* remote uid */
173         uid_t       lie_lcl_uid;      /* local uid */
174         gid_t       lie_rmt_gid;      /* remote gid */
175         gid_t       lie_lcl_gid;      /* local gid */
176 };
177
178 static inline __u32 lustre_idmap_hashfunc(__u32 id)
179 {
180         return id & (CFS_IDMAP_HASHSIZE - 1);
181 }
182
183 static
184 struct lustre_idmap_entry *idmap_entry_alloc(uid_t rmt_uid, uid_t lcl_uid,
185                                              gid_t rmt_gid, gid_t lcl_gid)
186 {
187         struct lustre_idmap_entry *e;
188
189         OBD_ALLOC_PTR(e);
190         if (e == NULL)
191                 return NULL;
192
193         INIT_LIST_HEAD(&e->lie_rmt_uid_hash);
194         INIT_LIST_HEAD(&e->lie_lcl_uid_hash);
195         INIT_LIST_HEAD(&e->lie_rmt_gid_hash);
196         INIT_LIST_HEAD(&e->lie_lcl_gid_hash);
197         e->lie_rmt_uid = rmt_uid;
198         e->lie_lcl_uid = lcl_uid;
199         e->lie_rmt_gid = rmt_gid;
200         e->lie_lcl_gid = lcl_gid;
201
202         return e;
203 }
204
205 static void idmap_entry_free(struct lustre_idmap_entry *e)
206 {
207         if (!list_empty(&e->lie_rmt_uid_hash))
208                 list_del(&e->lie_rmt_uid_hash);
209         if (!list_empty(&e->lie_lcl_uid_hash))
210                 list_del(&e->lie_lcl_uid_hash);
211         if (!list_empty(&e->lie_rmt_gid_hash))
212                 list_del(&e->lie_rmt_gid_hash);
213         if (!list_empty(&e->lie_lcl_gid_hash))
214                 list_del(&e->lie_lcl_gid_hash);
215         OBD_FREE_PTR(e);
216 }
217
218 /*
219  * return value
220  * NULL: not found entry
221  * ERR_PTR(-EACCES): found 1(remote):N(local) mapped entry
222  * others: found normal entry
223  */
224 static
225 struct lustre_idmap_entry *idmap_search_entry(struct lustre_idmap_table *t,
226                                               uid_t rmt_uid, uid_t lcl_uid,
227                                               gid_t rmt_gid, gid_t lcl_gid)
228 {
229         struct list_head *head;
230         struct lustre_idmap_entry *e;
231
232         head = &t->lit_idmaps[RMT_UIDMAP_IDX][lustre_idmap_hashfunc(rmt_uid)];
233         list_for_each_entry(e, head, lie_rmt_uid_hash)
234                 if (e->lie_rmt_uid == rmt_uid) {
235                         if (e->lie_lcl_uid == lcl_uid) {
236                                 if (e->lie_rmt_gid == rmt_gid &&
237                                     e->lie_lcl_gid == lcl_gid)
238                                         /* must be quaternion match */
239                                         return e;
240                         } else {
241                                 /* 1:N uid mapping */
242                                 CERROR("rmt uid %u already be mapped to %u"
243                                        " (new %u)\n", e->lie_rmt_uid,
244                                        e->lie_lcl_uid, lcl_uid);
245                                 return ERR_PTR(-EACCES);
246                         }
247                 }
248
249         head = &t->lit_idmaps[RMT_GIDMAP_IDX][lustre_idmap_hashfunc(rmt_gid)];
250         list_for_each_entry(e, head, lie_rmt_gid_hash)
251                 if (e->lie_rmt_gid == rmt_gid) {
252                         if (e->lie_lcl_gid == lcl_gid) {
253                                 if (unlikely(e->lie_rmt_uid == rmt_uid &&
254                                     e->lie_lcl_uid == lcl_uid))
255                                         /* after uid mapping search above,
256                                          * we should never come here */
257                                         LBUG();
258                         } else {
259                                 /* 1:N gid mapping */
260                                 CERROR("rmt gid %u already be mapped to %u"
261                                        " (new %u)\n", e->lie_rmt_gid,
262                                        e->lie_lcl_gid, lcl_gid);
263                                 return ERR_PTR(-EACCES);
264                         }
265                 }
266
267         return NULL;
268 }
269
270 static __u32 idmap_lookup_uid(struct list_head *hash, int reverse,
271                               __u32 uid)
272 {
273         struct list_head *head = &hash[lustre_idmap_hashfunc(uid)];
274         struct lustre_idmap_entry *e;
275
276         if (!reverse) {
277                 list_for_each_entry(e, head, lie_rmt_uid_hash)
278                         if (e->lie_rmt_uid == uid)
279                                 return e->lie_lcl_uid;
280         } else {
281                 list_for_each_entry(e, head, lie_lcl_uid_hash)
282                         if (e->lie_lcl_uid == uid)
283                                 return e->lie_rmt_uid;
284         }
285
286         return CFS_IDMAP_NOTFOUND;
287 }
288
289 static __u32 idmap_lookup_gid(struct list_head *hash, int reverse, __u32 gid)
290 {
291         struct list_head *head = &hash[lustre_idmap_hashfunc(gid)];
292         struct lustre_idmap_entry *e;
293
294         if (!reverse) {
295                 list_for_each_entry(e, head, lie_rmt_gid_hash)
296                         if (e->lie_rmt_gid == gid)
297                                 return e->lie_lcl_gid;
298         } else {
299                 list_for_each_entry(e, head, lie_lcl_gid_hash)
300                         if (e->lie_lcl_gid == gid)
301                                 return e->lie_rmt_gid;
302         }
303
304         return CFS_IDMAP_NOTFOUND;
305 }
306
307 int lustre_idmap_add(struct lustre_idmap_table *t,
308                      uid_t ruid, uid_t luid,
309                      gid_t rgid, gid_t lgid)
310 {
311         struct lustre_idmap_entry *e0, *e1;
312
313         LASSERT(t);
314
315         spin_lock(&t->lit_lock);
316         e0 = idmap_search_entry(t, ruid, luid, rgid, lgid);
317         spin_unlock(&t->lit_lock);
318         if (!e0) {
319                 e0 = idmap_entry_alloc(ruid, luid, rgid, lgid);
320                 if (!e0)
321                         return -ENOMEM;
322
323                 spin_lock(&t->lit_lock);
324                 e1 = idmap_search_entry(t, ruid, luid, rgid, lgid);
325                 if (e1 == NULL) {
326                         list_add_tail(&e0->lie_rmt_uid_hash,
327                                           &t->lit_idmaps[RMT_UIDMAP_IDX]
328                                           [lustre_idmap_hashfunc(ruid)]);
329                         list_add_tail(&e0->lie_lcl_uid_hash,
330                                           &t->lit_idmaps[LCL_UIDMAP_IDX]
331                                           [lustre_idmap_hashfunc(luid)]);
332                         list_add_tail(&e0->lie_rmt_gid_hash,
333                                           &t->lit_idmaps[RMT_GIDMAP_IDX]
334                                           [lustre_idmap_hashfunc(rgid)]);
335                         list_add_tail(&e0->lie_lcl_gid_hash,
336                                           &t->lit_idmaps[LCL_GIDMAP_IDX]
337                                           [lustre_idmap_hashfunc(lgid)]);
338                 }
339                 spin_unlock(&t->lit_lock);
340                 if (e1 != NULL) {
341                         idmap_entry_free(e0);
342                         if (IS_ERR(e1))
343                                 return PTR_ERR(e1);
344                 }
345         } else if (IS_ERR(e0)) {
346                 return PTR_ERR(e0);
347         }
348
349         return 0;
350 }
351 EXPORT_SYMBOL(lustre_idmap_add);
352
353 int lustre_idmap_del(struct lustre_idmap_table *t,
354                     uid_t ruid, uid_t luid,
355                     gid_t rgid, gid_t lgid)
356 {
357         struct lustre_idmap_entry *e;
358         int rc = 0;
359
360         LASSERT(t);
361
362         spin_lock(&t->lit_lock);
363         e = idmap_search_entry(t, ruid, luid, rgid, lgid);
364         if (IS_ERR(e))
365                 rc = PTR_ERR(e);
366         else if (e)
367                 idmap_entry_free(e);
368         spin_unlock(&t->lit_lock);
369
370         return rc;
371 }
372 EXPORT_SYMBOL(lustre_idmap_del);
373
374 int lustre_idmap_lookup_uid(struct lu_ucred *mu,
375                             struct lustre_idmap_table *t,
376                             int reverse, uid_t uid)
377 {
378         struct list_head *hash;
379
380         if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
381                 if (!reverse) {
382                         if (uid == mu->uc_o_uid)
383                                 return mu->uc_uid;
384                         else if (uid == mu->uc_o_fsuid)
385                                 return mu->uc_fsuid;
386                 } else {
387                         if (uid == mu->uc_uid)
388                                 return mu->uc_o_uid;
389                         else if (uid == mu->uc_fsuid)
390                                 return mu->uc_o_fsuid;
391                 }
392         }
393
394         if (t == NULL)
395                 return CFS_IDMAP_NOTFOUND;
396
397         hash = t->lit_idmaps[reverse ? LCL_UIDMAP_IDX : RMT_UIDMAP_IDX];
398
399         spin_lock(&t->lit_lock);
400         uid = idmap_lookup_uid(hash, reverse, uid);
401         spin_unlock(&t->lit_lock);
402
403         return uid;
404 }
405 EXPORT_SYMBOL(lustre_idmap_lookup_uid);
406
407 int lustre_idmap_lookup_gid(struct lu_ucred *mu, struct lustre_idmap_table *t,
408                             int reverse, gid_t gid)
409 {
410         struct list_head *hash;
411
412         if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
413                 if (!reverse) {
414                         if (gid == mu->uc_o_gid)
415                                 return mu->uc_gid;
416                         else if (gid == mu->uc_o_fsgid)
417                                 return mu->uc_fsgid;
418                 } else {
419                         if (gid == mu->uc_gid)
420                                 return mu->uc_o_gid;
421                         else if (gid == mu->uc_fsgid)
422                                 return mu->uc_o_fsgid;
423                 }
424         }
425
426         if (t == NULL)
427                 return CFS_IDMAP_NOTFOUND;
428
429         hash = t->lit_idmaps[reverse ? LCL_GIDMAP_IDX : RMT_GIDMAP_IDX];
430
431         spin_lock(&t->lit_lock);
432         gid = idmap_lookup_gid(hash, reverse, gid);
433         spin_unlock(&t->lit_lock);
434
435         return gid;
436 }
437 EXPORT_SYMBOL(lustre_idmap_lookup_gid);
438
439 struct lustre_idmap_table *lustre_idmap_init(void)
440 {
441         struct lustre_idmap_table *t;
442         int i, j;
443
444         OBD_ALLOC_PTR(t);
445         if(unlikely(t == NULL))
446                 return (ERR_PTR(-ENOMEM));
447
448         spin_lock_init(&t->lit_lock);
449         for (i = 0; i < ARRAY_SIZE(t->lit_idmaps); i++)
450                 for (j = 0; j < ARRAY_SIZE(t->lit_idmaps[i]); j++)
451                         INIT_LIST_HEAD(&t->lit_idmaps[i][j]);
452
453         return t;
454 }
455 EXPORT_SYMBOL(lustre_idmap_init);
456
457 void lustre_idmap_fini(struct lustre_idmap_table *t)
458 {
459         struct list_head *list;
460         struct lustre_idmap_entry *e;
461         int i;
462         LASSERT(t);
463
464         list = t->lit_idmaps[RMT_UIDMAP_IDX];
465         spin_lock(&t->lit_lock);
466         for (i = 0; i < CFS_IDMAP_HASHSIZE; i++)
467                 while (!list_empty(&list[i])) {
468                         e = list_entry(list[i].next,
469                                            struct lustre_idmap_entry,
470                                            lie_rmt_uid_hash);
471                         idmap_entry_free(e);
472                 }
473         spin_unlock(&t->lit_lock);
474
475         OBD_FREE_PTR(t);
476 }
477 EXPORT_SYMBOL(lustre_idmap_fini);