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