Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / mdt / mdt_identity.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/mdt/mdt_identity.c
39  *
40  * Author: Lai Siyao <lsy@clusterfs.com>
41  * Author: Fan Yong <fanyong@clusterfs.com>
42  */
43
44 #ifndef EXPORT_SYMTAB
45 #define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #ifndef AUTOCONF_INCLUDED
50 #include <linux/config.h>
51 #endif
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/mm.h>
55 #include <linux/kmod.h>
56 #include <linux/string.h>
57 #include <linux/stat.h>
58 #include <linux/errno.h>
59 #include <linux/version.h>
60 #include <linux/unistd.h>
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63 #include <linux/fs.h>
64 #include <linux/stat.h>
65 #include <asm/uaccess.h>
66 #include <linux/slab.h>
67
68 #include <libcfs/libcfs.h>
69 #include <libcfs/lucache.h>
70 #include <obd.h>
71 #include <obd_class.h>
72 #include <obd_support.h>
73 #include <lustre_net.h>
74 #include <lustre_import.h>
75 #include <lustre_dlm.h>
76 #include <lustre_lib.h>
77
78 #include "mdt_internal.h"
79
80 static void mdt_identity_entry_init(struct upcall_cache_entry *entry,
81                                     void *unused)
82 {
83         entry->u.identity.mi_uc_entry = entry;
84 }
85
86 static void mdt_identity_entry_free(struct upcall_cache *cache,
87                                     struct upcall_cache_entry *entry)
88 {
89         struct md_identity *identity = &entry->u.identity;
90
91         if (identity->mi_ginfo) {
92                 cfs_put_group_info(identity->mi_ginfo);
93                 identity->mi_ginfo = NULL;
94         }
95
96         if (identity->mi_nperms) {
97                 LASSERT(identity->mi_perms);
98                 OBD_FREE(identity->mi_perms,
99                          identity->mi_nperms * sizeof(struct md_perm));
100                 identity->mi_nperms = 0;
101         }
102 }
103
104 static int mdt_identity_do_upcall(struct upcall_cache *cache,
105                                   struct upcall_cache_entry *entry)
106 {
107         char keystr[16];
108         char *argv[] = {
109                   [0] = cache->uc_upcall,
110                   [1] = cache->uc_name,
111                   [2] = keystr,
112                   [3] = NULL
113         };
114         char *envp[] = {
115                   [0] = "HOME=/",
116                   [1] = "PATH=/sbin:/usr/sbin",
117                   [2] = NULL
118         };
119         struct timeval start, end;
120         int rc;
121         ENTRY;
122
123         /* There is race condition:
124          * "uc_upcall" was changed just after "is_identity_get_disabled" check.
125          */
126         cfs_read_lock(&cache->uc_upcall_rwlock);
127         CDEBUG(D_INFO, "The upcall is: '%s'\n", cache->uc_upcall);
128
129         if (unlikely(!strcmp(cache->uc_upcall, "NONE"))) {
130                 CERROR("no upcall set\n");
131                 GOTO(out, rc = -EREMCHG);
132         }
133
134         argv[0] = cache->uc_upcall;
135         snprintf(keystr, sizeof(keystr), LPU64, entry->ue_key);
136
137         cfs_gettimeofday(&start);
138         rc = USERMODEHELPER(argv[0], argv, envp);
139         cfs_gettimeofday(&end);
140         if (rc < 0) {
141                 CERROR("%s: error invoking upcall %s %s %s: rc %d; "
142                        "check /proc/fs/lustre/mdt/%s/identity_upcall, "
143                        "time %ldus\n",
144                        cache->uc_name, argv[0], argv[1], argv[2], rc,
145                        cache->uc_name, cfs_timeval_sub(&end, &start, NULL));
146         } else {
147                 CDEBUG(D_HA, "%s: invoked upcall %s %s %s, time %ldus\n",
148                        cache->uc_name, argv[0], argv[1], argv[2],
149                        cfs_timeval_sub(&end, &start, NULL));
150                 rc = 0;
151         }
152         EXIT;
153 out:
154         cfs_read_unlock(&cache->uc_upcall_rwlock);
155         return rc;
156 }
157
158 static int mdt_identity_parse_downcall(struct upcall_cache *cache,
159                                        struct upcall_cache_entry *entry,
160                                        void *args)
161 {
162         struct md_identity *identity = &entry->u.identity;
163         struct identity_downcall_data *data = args;
164         cfs_group_info_t *ginfo = NULL;
165         struct md_perm *perms = NULL;
166         int size, i;
167         ENTRY;
168
169         LASSERT(data);
170         if (data->idd_ngroups > NGROUPS_MAX)
171                 RETURN(-E2BIG);
172
173         if (data->idd_ngroups > 0) {
174                 ginfo = cfs_groups_alloc(data->idd_ngroups);
175                 if (!ginfo) {
176                         CERROR("failed to alloc %d groups\n", data->idd_ngroups);
177                         RETURN(-ENOMEM);
178                 }
179
180                 lustre_groups_from_list(ginfo, data->idd_groups);
181                 lustre_groups_sort(ginfo);
182         }
183
184         if (data->idd_nperms) {
185                 size = data->idd_nperms * sizeof(*perms);
186                 OBD_ALLOC(perms, size);
187                 if (!perms) {
188                         CERROR("failed to alloc %d permissions\n",
189                                data->idd_nperms);
190                         if (ginfo != NULL)
191                                 cfs_put_group_info(ginfo);
192                         RETURN(-ENOMEM);
193                 }
194
195                 for (i = 0; i < data->idd_nperms; i++) {
196                         perms[i].mp_nid = data->idd_perms[i].pdd_nid;
197                         perms[i].mp_perm = data->idd_perms[i].pdd_perm;
198                 }
199         }
200
201         identity->mi_uid = data->idd_uid;
202         identity->mi_gid = data->idd_gid;
203         identity->mi_ginfo = ginfo;
204         identity->mi_nperms = data->idd_nperms;
205         identity->mi_perms = perms;
206
207         CDEBUG(D_OTHER, "parse mdt identity@%p: %d:%d, ngroups %u, nperms %u\n",
208                identity, identity->mi_uid, identity->mi_gid,
209                data->idd_ngroups, data->idd_nperms);
210
211         RETURN(0);
212 }
213
214 struct md_identity *mdt_identity_get(struct upcall_cache *cache, __u32 uid)
215 {
216         struct upcall_cache_entry *entry;
217
218         if (!cache)
219                 return ERR_PTR(-ENOENT);
220
221         entry = upcall_cache_get_entry(cache, (__u64)uid, NULL);
222         if (IS_ERR(entry))
223                 return ERR_PTR(PTR_ERR(entry));
224         else if (unlikely(!entry))
225                 return ERR_PTR(-ENOENT);
226         else
227                 return &entry->u.identity;
228 }
229
230 void mdt_identity_put(struct upcall_cache *cache, struct md_identity *identity)
231 {
232         if (!cache)
233                 return;
234
235         LASSERT(identity);
236         upcall_cache_put_entry(cache, identity->mi_uc_entry);
237 }
238
239 struct upcall_cache_ops mdt_identity_upcall_cache_ops = {
240         .init_entry     = mdt_identity_entry_init,
241         .free_entry     = mdt_identity_entry_free,
242         .do_upcall      = mdt_identity_do_upcall,
243         .parse_downcall = mdt_identity_parse_downcall,
244 };
245
246 void mdt_flush_identity(struct upcall_cache *cache, int uid)
247 {
248         if (uid < 0)
249                 upcall_cache_flush_idle(cache);
250         else
251                 upcall_cache_flush_one(cache, (__u64)uid, NULL);
252 }
253
254 /*
255  * If there is LNET_NID_ANY in perm[i].mp_nid,
256  * it must be perm[0].mp_nid, and act as default perm.
257  */
258 __u32 mdt_identity_get_perm(struct md_identity *identity,
259                             __u32 is_rmtclient, lnet_nid_t nid)
260 {
261         struct md_perm *perm;
262         int i;
263
264         if (!identity) {
265                 LASSERT(is_rmtclient == 0);
266                 return CFS_SETGRP_PERM;
267         }
268
269         perm = identity->mi_perms;
270         /* check exactly matched nid first */
271         for (i = identity->mi_nperms - 1; i > 0; i--) {
272                 if (perm[i].mp_nid != nid)
273                         continue;
274                 return perm[i].mp_perm;
275         }
276
277         /* check LNET_NID_ANY then */
278         if ((identity->mi_nperms > 0) &&
279             ((perm[0].mp_nid == nid) || (perm[0].mp_nid == LNET_NID_ANY)))
280                 return perm[0].mp_perm;
281
282         /* return default last */
283         return is_rmtclient ? 0 : CFS_SETGRP_PERM;
284 }
285
286 int mdt_pack_remote_perm(struct mdt_thread_info *info, struct mdt_object *o,
287                          void *buf)
288 {
289         struct md_ucred         *uc = mdt_ucred(info);
290         struct md_object        *next = mdt_object_child(o);
291         struct mdt_remote_perm  *perm = buf;
292
293         ENTRY;
294
295         /* remote client request always pack ptlrpc_user_desc! */
296         LASSERT(perm);
297
298         if (!exp_connect_rmtclient(info->mti_exp))
299                 RETURN(-EBADE);
300
301         if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
302                 RETURN(-EINVAL);
303
304         perm->rp_uid = uc->mu_o_uid;
305         perm->rp_gid = uc->mu_o_gid;
306         perm->rp_fsuid = uc->mu_o_fsuid;
307         perm->rp_fsgid = uc->mu_o_fsgid;
308
309         perm->rp_access_perm = 0;
310         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_READ) == 0)
311                 perm->rp_access_perm |= MAY_READ;
312         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_WRITE) == 0)
313                 perm->rp_access_perm |= MAY_WRITE;
314         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_EXEC) == 0)
315                 perm->rp_access_perm |= MAY_EXEC;
316
317         RETURN(0);
318 }