staging/lustre: fix build when CONFIG_UIDGID_STRICT_TYPE_CHECKS is on
[platform/adaptation/renesas_rcar/renesas_kernel.git] / 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(group_info_t *group_info,
63                                 gid_t grp)
64 {
65         int left, right;
66
67         if (!group_info)
68                 return 0;
69
70         left = 0;
71         right = group_info->ngroups;
72         while (left < right) {
73                 int mid = (left + right) / 2;
74                 int cmp = grp -
75                         from_kgid(&init_user_ns, CFS_GROUP_AT(group_info, mid));
76
77                 if (cmp > 0)
78                         left = mid + 1;
79                 else if (cmp < 0)
80                         right = mid;
81                 else
82                         return 1;
83         }
84         return 0;
85 }
86
87 void lustre_groups_from_list(group_info_t *ginfo, gid_t *glist)
88 {
89         int i;
90         int count = ginfo->ngroups;
91
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);
97
98                 memcpy(ginfo->blocks[i], glist + off, len);
99                 count -= cp_count;
100         }
101 }
102 EXPORT_SYMBOL(lustre_groups_from_list);
103
104 /* groups_sort() is copied from linux kernel! */
105 /* a simple shell-metzner sort */
106 void lustre_groups_sort(group_info_t *group_info)
107 {
108         int base, max, stride;
109         int gidsetsize = group_info->ngroups;
110
111         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
112                 ; /* nothing */
113         stride /= 3;
114
115         while (stride) {
116                 max = gidsetsize - stride;
117                 for (base = 0; base < max; base++) {
118                         int left = base;
119                         int right = left + stride;
120                         gid_t tmp = from_kgid(&init_user_ns,
121                                               CFS_GROUP_AT(group_info, right));
122
123                         while (left >= 0 &&
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);
128                                 right = left;
129                                 left -= stride;
130                         }
131                         CFS_GROUP_AT(group_info, right) =
132                                                 make_kgid(&init_user_ns, tmp);
133                 }
134                 stride /= 3;
135         }
136 }
137 EXPORT_SYMBOL(lustre_groups_sort);
138
139 int lustre_in_group_p(struct lu_ucred *mu, gid_t grp)
140 {
141         int rc = 1;
142
143         if (grp != mu->uc_fsgid) {
144                 group_info_t *group_info = NULL;
145
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])
150                                 return 1;
151
152                 if (mu->uc_ginfo)
153                         group_info = mu->uc_ginfo;
154                 else if (mu->uc_identity)
155                         group_info = mu->uc_identity->mi_ginfo;
156
157                 if (!group_info)
158                         return 0;
159
160                 lustre_get_group_info(group_info);
161                 rc = lustre_groups_search(group_info, grp);
162                 lustre_put_group_info(group_info);
163         }
164         return rc;
165 }
166 EXPORT_SYMBOL(lustre_in_group_p);
167
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 */
177 };
178
179 static inline __u32 lustre_idmap_hashfunc(__u32 id)
180 {
181         return id & (CFS_IDMAP_HASHSIZE - 1);
182 }
183
184 static
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)
187 {
188         struct lustre_idmap_entry *e;
189
190         OBD_ALLOC_PTR(e);
191         if (e == NULL)
192                 return NULL;
193
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;
202
203         return e;
204 }
205
206 static void idmap_entry_free(struct lustre_idmap_entry *e)
207 {
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);
216         OBD_FREE_PTR(e);
217 }
218
219 /*
220  * return value
221  * NULL: not found entry
222  * ERR_PTR(-EACCES): found 1(remote):N(local) mapped entry
223  * others: found normal entry
224  */
225 static
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)
229 {
230         struct list_head *head;
231         struct lustre_idmap_entry *e;
232
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 */
240                                         return e;
241                         } else {
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);
247                         }
248                 }
249
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 */
258                                         LBUG();
259                         } else {
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);
265                         }
266                 }
267
268         return NULL;
269 }
270
271 static __u32 idmap_lookup_uid(struct list_head *hash, int reverse,
272                               __u32 uid)
273 {
274         struct list_head *head = &hash[lustre_idmap_hashfunc(uid)];
275         struct lustre_idmap_entry *e;
276
277         if (!reverse) {
278                 list_for_each_entry(e, head, lie_rmt_uid_hash)
279                         if (e->lie_rmt_uid == uid)
280                                 return e->lie_lcl_uid;
281         } else {
282                 list_for_each_entry(e, head, lie_lcl_uid_hash)
283                         if (e->lie_lcl_uid == uid)
284                                 return e->lie_rmt_uid;
285         }
286
287         return CFS_IDMAP_NOTFOUND;
288 }
289
290 static __u32 idmap_lookup_gid(struct list_head *hash, int reverse, __u32 gid)
291 {
292         struct list_head *head = &hash[lustre_idmap_hashfunc(gid)];
293         struct lustre_idmap_entry *e;
294
295         if (!reverse) {
296                 list_for_each_entry(e, head, lie_rmt_gid_hash)
297                         if (e->lie_rmt_gid == gid)
298                                 return e->lie_lcl_gid;
299         } else {
300                 list_for_each_entry(e, head, lie_lcl_gid_hash)
301                         if (e->lie_lcl_gid == gid)
302                                 return e->lie_rmt_gid;
303         }
304
305         return CFS_IDMAP_NOTFOUND;
306 }
307
308 int lustre_idmap_add(struct lustre_idmap_table *t,
309                      uid_t ruid, uid_t luid,
310                      gid_t rgid, gid_t lgid)
311 {
312         struct lustre_idmap_entry *e0, *e1;
313
314         LASSERT(t);
315
316         spin_lock(&t->lit_lock);
317         e0 = idmap_search_entry(t, ruid, luid, rgid, lgid);
318         spin_unlock(&t->lit_lock);
319         if (!e0) {
320                 e0 = idmap_entry_alloc(ruid, luid, rgid, lgid);
321                 if (!e0)
322                         return -ENOMEM;
323
324                 spin_lock(&t->lit_lock);
325                 e1 = idmap_search_entry(t, ruid, luid, rgid, lgid);
326                 if (e1 == NULL) {
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)]);
339                 }
340                 spin_unlock(&t->lit_lock);
341                 if (e1 != NULL) {
342                         idmap_entry_free(e0);
343                         if (IS_ERR(e1))
344                                 return PTR_ERR(e1);
345                 }
346         } else if (IS_ERR(e0)) {
347                 return PTR_ERR(e0);
348         }
349
350         return 0;
351 }
352 EXPORT_SYMBOL(lustre_idmap_add);
353
354 int lustre_idmap_del(struct lustre_idmap_table *t,
355                     uid_t ruid, uid_t luid,
356                     gid_t rgid, gid_t lgid)
357 {
358         struct lustre_idmap_entry *e;
359         int rc = 0;
360
361         LASSERT(t);
362
363         spin_lock(&t->lit_lock);
364         e = idmap_search_entry(t, ruid, luid, rgid, lgid);
365         if (IS_ERR(e))
366                 rc = PTR_ERR(e);
367         else if (e)
368                 idmap_entry_free(e);
369         spin_unlock(&t->lit_lock);
370
371         return rc;
372 }
373 EXPORT_SYMBOL(lustre_idmap_del);
374
375 int lustre_idmap_lookup_uid(struct lu_ucred *mu,
376                             struct lustre_idmap_table *t,
377                             int reverse, uid_t uid)
378 {
379         struct list_head *hash;
380
381         if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
382                 if (!reverse) {
383                         if (uid == mu->uc_o_uid)
384                                 return mu->uc_uid;
385                         else if (uid == mu->uc_o_fsuid)
386                                 return mu->uc_fsuid;
387                 } else {
388                         if (uid == mu->uc_uid)
389                                 return mu->uc_o_uid;
390                         else if (uid == mu->uc_fsuid)
391                                 return mu->uc_o_fsuid;
392                 }
393         }
394
395         if (t == NULL)
396                 return CFS_IDMAP_NOTFOUND;
397
398         hash = t->lit_idmaps[reverse ? LCL_UIDMAP_IDX : RMT_UIDMAP_IDX];
399
400         spin_lock(&t->lit_lock);
401         uid = idmap_lookup_uid(hash, reverse, uid);
402         spin_unlock(&t->lit_lock);
403
404         return uid;
405 }
406 EXPORT_SYMBOL(lustre_idmap_lookup_uid);
407
408 int lustre_idmap_lookup_gid(struct lu_ucred *mu, struct lustre_idmap_table *t,
409                             int reverse, gid_t gid)
410 {
411         struct list_head *hash;
412
413         if (mu && (mu->uc_valid == UCRED_OLD || mu->uc_valid == UCRED_NEW)) {
414                 if (!reverse) {
415                         if (gid == mu->uc_o_gid)
416                                 return mu->uc_gid;
417                         else if (gid == mu->uc_o_fsgid)
418                                 return mu->uc_fsgid;
419                 } else {
420                         if (gid == mu->uc_gid)
421                                 return mu->uc_o_gid;
422                         else if (gid == mu->uc_fsgid)
423                                 return mu->uc_o_fsgid;
424                 }
425         }
426
427         if (t == NULL)
428                 return CFS_IDMAP_NOTFOUND;
429
430         hash = t->lit_idmaps[reverse ? LCL_GIDMAP_IDX : RMT_GIDMAP_IDX];
431
432         spin_lock(&t->lit_lock);
433         gid = idmap_lookup_gid(hash, reverse, gid);
434         spin_unlock(&t->lit_lock);
435
436         return gid;
437 }
438 EXPORT_SYMBOL(lustre_idmap_lookup_gid);
439
440 struct lustre_idmap_table *lustre_idmap_init(void)
441 {
442         struct lustre_idmap_table *t;
443         int i, j;
444
445         OBD_ALLOC_PTR(t);
446         if(unlikely(t == NULL))
447                 return (ERR_PTR(-ENOMEM));
448
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]);
453
454         return t;
455 }
456 EXPORT_SYMBOL(lustre_idmap_init);
457
458 void lustre_idmap_fini(struct lustre_idmap_table *t)
459 {
460         struct list_head *list;
461         struct lustre_idmap_entry *e;
462         int i;
463         LASSERT(t);
464
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,
471                                            lie_rmt_uid_hash);
472                         idmap_entry_free(e);
473                 }
474         spin_unlock(&t->lit_lock);
475
476         OBD_FREE_PTR(t);
477 }
478 EXPORT_SYMBOL(lustre_idmap_fini);