Whamcloud - gitweb
85d782b5655cd717069b34a4e3ef20c22106d02b
[fs/lustre-release.git] / lustre / mdt / mdt_identity.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_identity.c
33  *
34  * Author: Lai Siyao <lsy@clusterfs.com>
35  * Author: Fan Yong <fanyong@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/kmod.h>
44 #include <linux/string.h>
45 #include <linux/stat.h>
46 #include <linux/errno.h>
47 #include <linux/version.h>
48 #include <linux/unistd.h>
49 #include <asm/uaccess.h>
50 #include <linux/fs.h>
51 #include <linux/stat.h>
52 #include <asm/uaccess.h>
53 #include <linux/slab.h>
54
55 #include <libcfs/libcfs.h>
56 #include <obd.h>
57 #include <obd_class.h>
58 #include <obd_support.h>
59 #include <lustre_net.h>
60 #include <lustre_import.h>
61 #include <lustre_dlm.h>
62 #include <lustre_lib.h>
63
64 #include "mdt_internal.h"
65
66 static void mdt_identity_entry_init(struct upcall_cache_entry *entry,
67                                     void *unused)
68 {
69         entry->u.identity.mi_uc_entry = entry;
70 }
71
72 static void mdt_identity_entry_free(struct upcall_cache *cache,
73                                     struct upcall_cache_entry *entry)
74 {
75         struct md_identity *identity = &entry->u.identity;
76
77         if (identity->mi_ginfo) {
78                 put_group_info(identity->mi_ginfo);
79                 identity->mi_ginfo = NULL;
80         }
81
82         if (identity->mi_nperms) {
83                 LASSERT(identity->mi_perms);
84                 OBD_FREE(identity->mi_perms,
85                          identity->mi_nperms * sizeof(struct md_perm));
86                 identity->mi_nperms = 0;
87         }
88 }
89
90 static int mdt_identity_do_upcall(struct upcall_cache *cache,
91                                   struct upcall_cache_entry *entry)
92 {
93         char keystr[16];
94         char *argv[] = {
95                   [0] = cache->uc_upcall,
96                   [1] = cache->uc_name,
97                   [2] = keystr,
98                   [3] = NULL
99         };
100         char *envp[] = {
101                   [0] = "HOME=/",
102                   [1] = "PATH=/sbin:/usr/sbin",
103                   [2] = NULL
104         };
105         struct timeval start, end;
106         int rc;
107         ENTRY;
108
109         /* There is race condition:
110          * "uc_upcall" was changed just after "is_identity_get_disabled" check.
111          */
112         down_read(&cache->uc_upcall_rwsem);
113         CDEBUG(D_INFO, "The upcall is: '%s'\n", cache->uc_upcall);
114
115         if (unlikely(!strcmp(cache->uc_upcall, "NONE"))) {
116                 CERROR("no upcall set\n");
117                 GOTO(out, rc = -EREMCHG);
118         }
119
120         argv[0] = cache->uc_upcall;
121         snprintf(keystr, sizeof(keystr), "%llu", entry->ue_key);
122
123         do_gettimeofday(&start);
124         rc = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
125         do_gettimeofday(&end);
126         if (rc < 0) {
127                 CERROR("%s: error invoking upcall %s %s %s: rc %d; "
128                        "check /proc/fs/lustre/mdt/%s/identity_upcall, "
129                        "time %ldus\n",
130                        cache->uc_name, argv[0], argv[1], argv[2], rc,
131                        cache->uc_name, cfs_timeval_sub(&end, &start, NULL));
132         } else {
133                 CDEBUG(D_HA, "%s: invoked upcall %s %s %s, time %ldus\n",
134                        cache->uc_name, argv[0], argv[1], argv[2],
135                        cfs_timeval_sub(&end, &start, NULL));
136                 rc = 0;
137         }
138         EXIT;
139 out:
140         up_read(&cache->uc_upcall_rwsem);
141         return rc;
142 }
143
144 static int mdt_identity_parse_downcall(struct upcall_cache *cache,
145                                        struct upcall_cache_entry *entry,
146                                        void *args)
147 {
148         struct md_identity *identity = &entry->u.identity;
149         struct identity_downcall_data *data = args;
150         struct group_info *ginfo = NULL;
151         struct md_perm *perms = NULL;
152         int size, i;
153         ENTRY;
154
155         LASSERT(data);
156         if (data->idd_ngroups > NGROUPS_MAX)
157                 RETURN(-E2BIG);
158
159         if (data->idd_ngroups > 0) {
160                 ginfo = groups_alloc(data->idd_ngroups);
161                 if (!ginfo) {
162                         CERROR("failed to alloc %d groups\n", data->idd_ngroups);
163                         RETURN(-ENOMEM);
164                 }
165
166                 lustre_groups_from_list(ginfo, data->idd_groups);
167                 lustre_groups_sort(ginfo);
168         }
169
170         if (data->idd_nperms) {
171                 size = data->idd_nperms * sizeof(*perms);
172                 OBD_ALLOC(perms, size);
173                 if (!perms) {
174                         CERROR("failed to alloc %d permissions\n",
175                                data->idd_nperms);
176                         if (ginfo != NULL)
177                                 put_group_info(ginfo);
178                         RETURN(-ENOMEM);
179                 }
180
181                 for (i = 0; i < data->idd_nperms; i++) {
182                         perms[i].mp_nid = data->idd_perms[i].pdd_nid;
183                         perms[i].mp_perm = data->idd_perms[i].pdd_perm;
184                 }
185         }
186
187         identity->mi_uid = data->idd_uid;
188         identity->mi_gid = data->idd_gid;
189         identity->mi_ginfo = ginfo;
190         identity->mi_nperms = data->idd_nperms;
191         identity->mi_perms = perms;
192
193         CDEBUG(D_OTHER, "parse mdt identity@%p: %d:%d, ngroups %u, nperms %u\n",
194                identity, identity->mi_uid, identity->mi_gid,
195                data->idd_ngroups, data->idd_nperms);
196
197         RETURN(0);
198 }
199
200 struct md_identity *mdt_identity_get(struct upcall_cache *cache, __u32 uid)
201 {
202         struct upcall_cache_entry *entry;
203
204         if (!cache)
205                 return ERR_PTR(-ENOENT);
206
207         entry = upcall_cache_get_entry(cache, (__u64)uid, NULL);
208         if (IS_ERR(entry))
209                 return ERR_PTR(PTR_ERR(entry));
210         else if (unlikely(!entry))
211                 return ERR_PTR(-ENOENT);
212         else
213                 return &entry->u.identity;
214 }
215
216 void mdt_identity_put(struct upcall_cache *cache, struct md_identity *identity)
217 {
218         if (!cache)
219                 return;
220
221         LASSERT(identity);
222         upcall_cache_put_entry(cache, identity->mi_uc_entry);
223 }
224
225 struct upcall_cache_ops mdt_identity_upcall_cache_ops = {
226         .init_entry     = mdt_identity_entry_init,
227         .free_entry     = mdt_identity_entry_free,
228         .do_upcall      = mdt_identity_do_upcall,
229         .parse_downcall = mdt_identity_parse_downcall,
230 };
231
232 void mdt_flush_identity(struct upcall_cache *cache, int uid)
233 {
234         if (uid < 0)
235                 upcall_cache_flush_idle(cache);
236         else
237                 upcall_cache_flush_one(cache, (__u64)uid, NULL);
238 }
239
240 /*
241  * If there is LNET_NID_ANY in perm[i].mp_nid,
242  * it must be perm[0].mp_nid, and act as default perm.
243  */
244 __u32 mdt_identity_get_perm(struct md_identity *identity, lnet_nid_t nid)
245 {
246
247         struct md_perm *perm;
248         int i;
249
250         if (!identity)
251                 return CFS_SETGRP_PERM;
252
253         perm = identity->mi_perms;
254         /* check exactly matched nid first */
255         for (i = identity->mi_nperms - 1; i > 0; i--) {
256                 if (perm[i].mp_nid != nid)
257                         continue;
258                 return perm[i].mp_perm;
259         }
260
261         /* check LNET_NID_ANY then */
262         if ((identity->mi_nperms > 0) &&
263             ((perm[0].mp_nid == nid) || (perm[0].mp_nid == LNET_NID_ANY)))
264                 return perm[0].mp_perm;
265
266         /* return default last */
267         return CFS_SETGRP_PERM;
268 }