Whamcloud - gitweb
595a6d052464b1459597a2ba14324a700f9d2fe9
[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.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 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/mdt/mdt_identity.c
37  *
38  * Author: Lai Siyao <lsy@clusterfs.com>
39  * Author: Fan Yong <fanyong@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_MDS
43
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/mm.h>
47 #include <linux/kmod.h>
48 #include <linux/string.h>
49 #include <linux/stat.h>
50 #include <linux/errno.h>
51 #include <linux/version.h>
52 #include <linux/unistd.h>
53 #include <asm/uaccess.h>
54 #include <linux/fs.h>
55 #include <linux/stat.h>
56 #include <asm/uaccess.h>
57 #include <linux/slab.h>
58
59 #include <libcfs/libcfs.h>
60 #include <obd.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre_net.h>
64 #include <lustre_import.h>
65 #include <lustre_dlm.h>
66 #include <lustre_lib.h>
67
68 #include "mdt_internal.h"
69
70 static void mdt_identity_entry_init(struct upcall_cache_entry *entry,
71                                     void *unused)
72 {
73         entry->u.identity.mi_uc_entry = entry;
74 }
75
76 static void mdt_identity_entry_free(struct upcall_cache *cache,
77                                     struct upcall_cache_entry *entry)
78 {
79         struct md_identity *identity = &entry->u.identity;
80
81         if (identity->mi_ginfo) {
82                 put_group_info(identity->mi_ginfo);
83                 identity->mi_ginfo = NULL;
84         }
85
86         if (identity->mi_nperms) {
87                 LASSERT(identity->mi_perms);
88                 OBD_FREE(identity->mi_perms,
89                          identity->mi_nperms * sizeof(struct md_perm));
90                 identity->mi_nperms = 0;
91         }
92 }
93
94 static int mdt_identity_do_upcall(struct upcall_cache *cache,
95                                   struct upcall_cache_entry *entry)
96 {
97         char keystr[16];
98         char *argv[] = {
99                   [0] = cache->uc_upcall,
100                   [1] = cache->uc_name,
101                   [2] = keystr,
102                   [3] = NULL
103         };
104         char *envp[] = {
105                   [0] = "HOME=/",
106                   [1] = "PATH=/sbin:/usr/sbin",
107                   [2] = NULL
108         };
109         struct timeval start, end;
110         int rc;
111         ENTRY;
112
113         /* There is race condition:
114          * "uc_upcall" was changed just after "is_identity_get_disabled" check.
115          */
116         read_lock(&cache->uc_upcall_rwlock);
117         CDEBUG(D_INFO, "The upcall is: '%s'\n", cache->uc_upcall);
118
119         if (unlikely(!strcmp(cache->uc_upcall, "NONE"))) {
120                 CERROR("no upcall set\n");
121                 GOTO(out, rc = -EREMCHG);
122         }
123
124         argv[0] = cache->uc_upcall;
125         snprintf(keystr, sizeof(keystr), LPU64, entry->ue_key);
126
127         do_gettimeofday(&start);
128         rc = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
129         do_gettimeofday(&end);
130         if (rc < 0) {
131                 CERROR("%s: error invoking upcall %s %s %s: rc %d; "
132                        "check /proc/fs/lustre/mdt/%s/identity_upcall, "
133                        "time %ldus\n",
134                        cache->uc_name, argv[0], argv[1], argv[2], rc,
135                        cache->uc_name, cfs_timeval_sub(&end, &start, NULL));
136         } else {
137                 CDEBUG(D_HA, "%s: invoked upcall %s %s %s, time %ldus\n",
138                        cache->uc_name, argv[0], argv[1], argv[2],
139                        cfs_timeval_sub(&end, &start, NULL));
140                 rc = 0;
141         }
142         EXIT;
143 out:
144         read_unlock(&cache->uc_upcall_rwlock);
145         return rc;
146 }
147
148 static int mdt_identity_parse_downcall(struct upcall_cache *cache,
149                                        struct upcall_cache_entry *entry,
150                                        void *args)
151 {
152         struct md_identity *identity = &entry->u.identity;
153         struct identity_downcall_data *data = args;
154         struct group_info *ginfo = NULL;
155         struct md_perm *perms = NULL;
156         int size, i;
157         ENTRY;
158
159         LASSERT(data);
160         if (data->idd_ngroups > NGROUPS_MAX)
161                 RETURN(-E2BIG);
162
163         if (data->idd_ngroups > 0) {
164                 ginfo = groups_alloc(data->idd_ngroups);
165                 if (!ginfo) {
166                         CERROR("failed to alloc %d groups\n", data->idd_ngroups);
167                         RETURN(-ENOMEM);
168                 }
169
170                 lustre_groups_from_list(ginfo, data->idd_groups);
171                 lustre_groups_sort(ginfo);
172         }
173
174         if (data->idd_nperms) {
175                 size = data->idd_nperms * sizeof(*perms);
176                 OBD_ALLOC(perms, size);
177                 if (!perms) {
178                         CERROR("failed to alloc %d permissions\n",
179                                data->idd_nperms);
180                         if (ginfo != NULL)
181                                 put_group_info(ginfo);
182                         RETURN(-ENOMEM);
183                 }
184
185                 for (i = 0; i < data->idd_nperms; i++) {
186                         perms[i].mp_nid = data->idd_perms[i].pdd_nid;
187                         perms[i].mp_perm = data->idd_perms[i].pdd_perm;
188                 }
189         }
190
191         identity->mi_uid = data->idd_uid;
192         identity->mi_gid = data->idd_gid;
193         identity->mi_ginfo = ginfo;
194         identity->mi_nperms = data->idd_nperms;
195         identity->mi_perms = perms;
196
197         CDEBUG(D_OTHER, "parse mdt identity@%p: %d:%d, ngroups %u, nperms %u\n",
198                identity, identity->mi_uid, identity->mi_gid,
199                data->idd_ngroups, data->idd_nperms);
200
201         RETURN(0);
202 }
203
204 struct md_identity *mdt_identity_get(struct upcall_cache *cache, __u32 uid)
205 {
206         struct upcall_cache_entry *entry;
207
208         if (!cache)
209                 return ERR_PTR(-ENOENT);
210
211         entry = upcall_cache_get_entry(cache, (__u64)uid, NULL);
212         if (IS_ERR(entry))
213                 return ERR_PTR(PTR_ERR(entry));
214         else if (unlikely(!entry))
215                 return ERR_PTR(-ENOENT);
216         else
217                 return &entry->u.identity;
218 }
219
220 void mdt_identity_put(struct upcall_cache *cache, struct md_identity *identity)
221 {
222         if (!cache)
223                 return;
224
225         LASSERT(identity);
226         upcall_cache_put_entry(cache, identity->mi_uc_entry);
227 }
228
229 struct upcall_cache_ops mdt_identity_upcall_cache_ops = {
230         .init_entry     = mdt_identity_entry_init,
231         .free_entry     = mdt_identity_entry_free,
232         .do_upcall      = mdt_identity_do_upcall,
233         .parse_downcall = mdt_identity_parse_downcall,
234 };
235
236 void mdt_flush_identity(struct upcall_cache *cache, int uid)
237 {
238         if (uid < 0)
239                 upcall_cache_flush_idle(cache);
240         else
241                 upcall_cache_flush_one(cache, (__u64)uid, NULL);
242 }
243
244 /*
245  * If there is LNET_NID_ANY in perm[i].mp_nid,
246  * it must be perm[0].mp_nid, and act as default perm.
247  */
248 __u32 mdt_identity_get_perm(struct md_identity *identity,
249                             __u32 is_rmtclient, lnet_nid_t nid)
250 {
251         struct md_perm *perm;
252         int i;
253
254         if (!identity) {
255                 LASSERT(is_rmtclient == 0);
256                 return CFS_SETGRP_PERM;
257         }
258
259         perm = identity->mi_perms;
260         /* check exactly matched nid first */
261         for (i = identity->mi_nperms - 1; i > 0; i--) {
262                 if (perm[i].mp_nid != nid)
263                         continue;
264                 return perm[i].mp_perm;
265         }
266
267         /* check LNET_NID_ANY then */
268         if ((identity->mi_nperms > 0) &&
269             ((perm[0].mp_nid == nid) || (perm[0].mp_nid == LNET_NID_ANY)))
270                 return perm[0].mp_perm;
271
272         /* return default last */
273         return is_rmtclient ? 0 : CFS_SETGRP_PERM;
274 }
275
276 int mdt_pack_remote_perm(struct mdt_thread_info *info, struct mdt_object *o,
277                          void *buf)
278 {
279         struct lu_ucred         *uc = mdt_ucred_check(info);
280         struct md_object        *next = mdt_object_child(o);
281         struct mdt_remote_perm  *perm = buf;
282
283         ENTRY;
284
285         /* remote client request always pack ptlrpc_user_desc! */
286         LASSERT(perm);
287
288         if (!exp_connect_rmtclient(info->mti_exp))
289                 RETURN(-EBADE);
290
291         if (uc == NULL)
292                 RETURN(-EINVAL);
293
294         perm->rp_uid = uc->uc_o_uid;
295         perm->rp_gid = uc->uc_o_gid;
296         perm->rp_fsuid = uc->uc_o_fsuid;
297         perm->rp_fsgid = uc->uc_o_fsgid;
298
299         perm->rp_access_perm = 0;
300         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_READ) == 0)
301                 perm->rp_access_perm |= MAY_READ;
302         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_WRITE) == 0)
303                 perm->rp_access_perm |= MAY_WRITE;
304         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_EXEC) == 0)
305                 perm->rp_access_perm |= MAY_EXEC;
306
307         RETURN(0);
308 }