Whamcloud - gitweb
LU-2753 lvfs: cleanup lvfs.h and collateral
[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.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 <libcfs/lucache.h>
46
47 #include <lustre_idmap.h>
48 #include <md_object.h>
49 #include <obd_support.h>
50
51 #define lustre_get_group_info(group_info) do {             \
52         atomic_inc(&(group_info)->usage);              \
53 } while (0)
54
55 #define lustre_put_group_info(group_info) do {             \
56         if (atomic_dec_and_test(&(group_info)->usage)) \
57                 groups_free(group_info);                   \
58 } while (0)
59
60 /*
61  * groups_search() is copied from linux kernel!
62  * A simple bsearch.
63  */
64 static int lustre_groups_search(struct group_info *group_info,
65                                 gid_t grp)
66 {
67         int left, right;
68
69         if (!group_info)
70                 return 0;
71
72         left = 0;
73         right = group_info->ngroups;
74         while (left < right) {
75                 int mid = (left + right) / 2;
76                 int cmp = grp - 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 = CFS_GROUP_AT(group_info, right);
122
123                         while (left >= 0 &&
124                                CFS_GROUP_AT(group_info, left) > tmp) {
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) = tmp;
131                 }
132                 stride /= 3;
133         }
134 }
135 EXPORT_SYMBOL(lustre_groups_sort);
136
137 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
138 {
139         int rc = 1;
140
141         if (grp != mu->uc_fsgid) {
142                 struct group_info *group_info = NULL;
143
144                 if (mu->uc_ginfo || !mu->uc_identity ||
145                     mu->uc_valid == UCRED_OLD)
146                         if (grp == mu->uc_suppgids[0] ||
147                             grp == mu->uc_suppgids[1])
148                                 return 1;
149
150                 if (mu->uc_ginfo)
151                         group_info = mu->uc_ginfo;
152                 else if (mu->uc_identity)
153                         group_info = mu->uc_identity->mi_ginfo;
154
155                 if (!group_info)
156                         return 0;
157
158                 lustre_get_group_info(group_info);
159                 rc = lustre_groups_search(group_info, grp);
160                 lustre_put_group_info(group_info);
161         }
162         return rc;
163 }
164 EXPORT_SYMBOL(lustre_in_group_p);
165
166 struct lustre_idmap_entry {
167         cfs_list_t       lie_rmt_uid_hash; /* hashed as lie_rmt_uid; */
168         cfs_list_t       lie_lcl_uid_hash; /* hashed as lie_lcl_uid; */
169         cfs_list_t       lie_rmt_gid_hash; /* hashed as lie_rmt_gid; */
170         cfs_list_t       lie_lcl_gid_hash; /* hashed as lie_lcl_gid; */
171         uid_t            lie_rmt_uid;      /* remote uid */
172         uid_t            lie_lcl_uid;      /* local uid */
173         gid_t            lie_rmt_gid;      /* remote gid */
174         gid_t            lie_lcl_gid;      /* local gid */
175 };
176
177 static inline __u32 lustre_idmap_hashfunc(__u32 id)
178 {
179         return id & (CFS_IDMAP_HASHSIZE - 1);
180 }
181
182 static
183 struct lustre_idmap_entry *idmap_entry_alloc(uid_t rmt_uid, uid_t lcl_uid,
184                                              gid_t rmt_gid, gid_t lcl_gid)
185 {
186         struct lustre_idmap_entry *e;
187
188         OBD_ALLOC_PTR(e);
189         if (e == NULL)
190                 return NULL;
191
192         CFS_INIT_LIST_HEAD(&e->lie_rmt_uid_hash);
193         CFS_INIT_LIST_HEAD(&e->lie_lcl_uid_hash);
194         CFS_INIT_LIST_HEAD(&e->lie_rmt_gid_hash);
195         CFS_INIT_LIST_HEAD(&e->lie_lcl_gid_hash);
196         e->lie_rmt_uid = rmt_uid;
197         e->lie_lcl_uid = lcl_uid;
198         e->lie_rmt_gid = rmt_gid;
199         e->lie_lcl_gid = lcl_gid;
200
201         return e;
202 }
203
204 static void idmap_entry_free(struct lustre_idmap_entry *e)
205 {
206         if (!cfs_list_empty(&e->lie_rmt_uid_hash))
207                 cfs_list_del(&e->lie_rmt_uid_hash);
208         if (!cfs_list_empty(&e->lie_lcl_uid_hash))
209                 cfs_list_del(&e->lie_lcl_uid_hash);
210         if (!cfs_list_empty(&e->lie_rmt_gid_hash))
211                 cfs_list_del(&e->lie_rmt_gid_hash);
212         if (!cfs_list_empty(&e->lie_lcl_gid_hash))
213                 cfs_list_del(&e->lie_lcl_gid_hash);
214         OBD_FREE_PTR(e);
215 }
216
217 /*
218  * return value
219  * NULL: not found entry
220  * ERR_PTR(-EACCES): found 1(remote):N(local) mapped entry
221  * others: found normal entry
222  */
223 static
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)
227 {
228         cfs_list_t *head;
229         struct lustre_idmap_entry *e;
230
231         head = &t->lit_idmaps[RMT_UIDMAP_IDX][lustre_idmap_hashfunc(rmt_uid)];
232         cfs_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 */
238                                         return e;
239                         } else {
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);
245                         }
246                 }
247
248         head = &t->lit_idmaps[RMT_GIDMAP_IDX][lustre_idmap_hashfunc(rmt_gid)];
249         cfs_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 */
256                                         LBUG();
257                         } else {
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);
263                         }
264                 }
265
266         return NULL;
267 }
268
269 static __u32 idmap_lookup_uid(cfs_list_t *hash, int reverse,
270                               __u32 uid)
271 {
272         cfs_list_t *head = &hash[lustre_idmap_hashfunc(uid)];
273         struct lustre_idmap_entry *e;
274
275         if (!reverse) {
276                 cfs_list_for_each_entry(e, head, lie_rmt_uid_hash)
277                         if (e->lie_rmt_uid == uid)
278                                 return e->lie_lcl_uid;
279         } else {
280                 cfs_list_for_each_entry(e, head, lie_lcl_uid_hash)
281                         if (e->lie_lcl_uid == uid)
282                                 return e->lie_rmt_uid;
283         }
284
285         return CFS_IDMAP_NOTFOUND;
286 }
287
288 static __u32 idmap_lookup_gid(cfs_list_t *hash, int reverse, __u32 gid)
289 {
290         cfs_list_t *head = &hash[lustre_idmap_hashfunc(gid)];
291         struct lustre_idmap_entry *e;
292
293         if (!reverse) {
294                 cfs_list_for_each_entry(e, head, lie_rmt_gid_hash)
295                         if (e->lie_rmt_gid == gid)
296                                 return e->lie_lcl_gid;
297         } else {
298                 cfs_list_for_each_entry(e, head, lie_lcl_gid_hash)
299                         if (e->lie_lcl_gid == gid)
300                                 return e->lie_rmt_gid;
301         }
302
303         return CFS_IDMAP_NOTFOUND;
304 }
305
306 int lustre_idmap_add(struct lustre_idmap_table *t,
307                      uid_t ruid, uid_t luid,
308                      gid_t rgid, gid_t lgid)
309 {
310         struct lustre_idmap_entry *e0, *e1;
311
312         LASSERT(t);
313
314         spin_lock(&t->lit_lock);
315         e0 = idmap_search_entry(t, ruid, luid, rgid, lgid);
316         spin_unlock(&t->lit_lock);
317         if (!e0) {
318                 e0 = idmap_entry_alloc(ruid, luid, rgid, lgid);
319                 if (!e0)
320                         return -ENOMEM;
321
322                 spin_lock(&t->lit_lock);
323                 e1 = idmap_search_entry(t, ruid, luid, rgid, lgid);
324                 if (e1 == NULL) {
325                         cfs_list_add_tail(&e0->lie_rmt_uid_hash,
326                                           &t->lit_idmaps[RMT_UIDMAP_IDX]
327                                           [lustre_idmap_hashfunc(ruid)]);
328                         cfs_list_add_tail(&e0->lie_lcl_uid_hash,
329                                           &t->lit_idmaps[LCL_UIDMAP_IDX]
330                                           [lustre_idmap_hashfunc(luid)]);
331                         cfs_list_add_tail(&e0->lie_rmt_gid_hash,
332                                           &t->lit_idmaps[RMT_GIDMAP_IDX]
333                                           [lustre_idmap_hashfunc(rgid)]);
334                         cfs_list_add_tail(&e0->lie_lcl_gid_hash,
335                                           &t->lit_idmaps[LCL_GIDMAP_IDX]
336                                           [lustre_idmap_hashfunc(lgid)]);
337                 }
338                 spin_unlock(&t->lit_lock);
339                 if (e1 != NULL) {
340                         idmap_entry_free(e0);
341                         if (IS_ERR(e1))
342                                 return PTR_ERR(e1);
343                 }
344         } else if (IS_ERR(e0)) {
345                 return PTR_ERR(e0);
346         }
347
348         return 0;
349 }
350 EXPORT_SYMBOL(lustre_idmap_add);
351
352 int lustre_idmap_del(struct lustre_idmap_table *t,
353                     uid_t ruid, uid_t luid,
354                     gid_t rgid, gid_t lgid)
355 {
356         struct lustre_idmap_entry *e;
357         int rc = 0;
358
359         LASSERT(t);
360
361         spin_lock(&t->lit_lock);
362         e = idmap_search_entry(t, ruid, luid, rgid, lgid);
363         if (IS_ERR(e))
364                 rc = PTR_ERR(e);
365         else if (e)
366                 idmap_entry_free(e);
367         spin_unlock(&t->lit_lock);
368
369         return rc;
370 }
371 EXPORT_SYMBOL(lustre_idmap_del);
372
373 int lustre_idmap_lookup_uid(struct lu_ucred *mu,
374                             struct lustre_idmap_table *t,
375                             int reverse, uid_t uid)
376 {
377         cfs_list_t *hash;
378
379         if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
380                 if (!reverse) {
381                         if (uid == mu->uc_o_uid)
382                                 return mu->uc_uid;
383                         else if (uid == mu->uc_o_fsuid)
384                                 return mu->uc_fsuid;
385                 } else {
386                         if (uid == mu->uc_uid)
387                                 return mu->uc_o_uid;
388                         else if (uid == mu->uc_fsuid)
389                                 return mu->uc_o_fsuid;
390                 }
391         }
392
393         if (t == NULL)
394                 return CFS_IDMAP_NOTFOUND;
395
396         hash = t->lit_idmaps[reverse ? LCL_UIDMAP_IDX : RMT_UIDMAP_IDX];
397
398         spin_lock(&t->lit_lock);
399         uid = idmap_lookup_uid(hash, reverse, uid);
400         spin_unlock(&t->lit_lock);
401
402         return uid;
403 }
404 EXPORT_SYMBOL(lustre_idmap_lookup_uid);
405
406 int lustre_idmap_lookup_gid(struct lu_ucred *mu, struct lustre_idmap_table *t,
407                             int reverse, gid_t gid)
408 {
409         cfs_list_t *hash;
410
411         if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
412                 if (!reverse) {
413                         if (gid == mu->uc_o_gid)
414                                 return mu->uc_gid;
415                         else if (gid == mu->uc_o_fsgid)
416                                 return mu->uc_fsgid;
417                 } else {
418                         if (gid == mu->uc_gid)
419                                 return mu->uc_o_gid;
420                         else if (gid == mu->uc_fsgid)
421                                 return mu->uc_o_fsgid;
422                 }
423         }
424
425         if (t == NULL)
426                 return CFS_IDMAP_NOTFOUND;
427
428         hash = t->lit_idmaps[reverse ? LCL_GIDMAP_IDX : RMT_GIDMAP_IDX];
429
430         spin_lock(&t->lit_lock);
431         gid = idmap_lookup_gid(hash, reverse, gid);
432         spin_unlock(&t->lit_lock);
433
434         return gid;
435 }
436 EXPORT_SYMBOL(lustre_idmap_lookup_gid);
437
438 struct lustre_idmap_table *lustre_idmap_init(void)
439 {
440         struct lustre_idmap_table *t;
441         int i, j;
442
443         OBD_ALLOC_PTR(t);
444         if(unlikely(t == NULL))
445                 return (ERR_PTR(-ENOMEM));
446
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                         CFS_INIT_LIST_HEAD(&t->lit_idmaps[i][j]);
451
452         return t;
453 }
454 EXPORT_SYMBOL(lustre_idmap_init);
455
456 void lustre_idmap_fini(struct lustre_idmap_table *t)
457 {
458         cfs_list_t *list;
459         struct lustre_idmap_entry *e;
460         int i;
461         LASSERT(t);
462
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 (!cfs_list_empty(&list[i])) {
467                         e = cfs_list_entry(list[i].next,
468                                            struct lustre_idmap_entry,
469                                            lie_rmt_uid_hash);
470                         idmap_entry_free(e);
471                 }
472         spin_unlock(&t->lit_lock);
473
474         OBD_FREE_PTR(t);
475 }
476 EXPORT_SYMBOL(lustre_idmap_fini);