Whamcloud - gitweb
LU-12037 mdt: add option for cross-MDT rename
[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 "mdt_internal.h"
41
42 static void mdt_identity_entry_init(struct upcall_cache_entry *entry,
43                                     void *unused)
44 {
45         entry->u.identity.mi_uc_entry = entry;
46 }
47
48 static void mdt_identity_entry_free(struct upcall_cache *cache,
49                                     struct upcall_cache_entry *entry)
50 {
51         struct md_identity *identity = &entry->u.identity;
52
53         if (identity->mi_ginfo) {
54                 put_group_info(identity->mi_ginfo);
55                 identity->mi_ginfo = NULL;
56         }
57
58         if (identity->mi_nperms) {
59                 LASSERT(identity->mi_perms);
60                 OBD_FREE(identity->mi_perms,
61                          identity->mi_nperms * sizeof(struct md_perm));
62                 identity->mi_nperms = 0;
63         }
64 }
65
66 static int mdt_identity_do_upcall(struct upcall_cache *cache,
67                                   struct upcall_cache_entry *entry)
68 {
69         char keystr[16];
70         char *argv[] = {
71                   [0] = cache->uc_upcall,
72                   [1] = cache->uc_name,
73                   [2] = keystr,
74                   [3] = NULL
75         };
76         char *envp[] = {
77                   [0] = "HOME=/",
78                   [1] = "PATH=/sbin:/usr/sbin",
79                   [2] = NULL
80         };
81         ktime_t start, end;
82         int rc;
83         ENTRY;
84
85         /* There is race condition:
86          * "uc_upcall" was changed just after "is_identity_get_disabled" check.
87          */
88         down_read(&cache->uc_upcall_rwsem);
89         CDEBUG(D_INFO, "The upcall is: '%s'\n", cache->uc_upcall);
90
91         if (unlikely(!strcmp(cache->uc_upcall, "NONE"))) {
92                 CERROR("no upcall set\n");
93                 GOTO(out, rc = -EREMCHG);
94         }
95
96         argv[0] = cache->uc_upcall;
97         snprintf(keystr, sizeof(keystr), "%llu", entry->ue_key);
98
99         start = ktime_get();
100         rc = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
101         end = ktime_get();
102         if (rc < 0) {
103                 CERROR("%s: error invoking upcall %s %s %s: rc %d; check /proc/fs/lustre/mdt/%s/identity_upcall, time %ldus\n",
104                        cache->uc_name, argv[0], argv[1], argv[2], rc,
105                        cache->uc_name, (long)ktime_us_delta(end, start));
106         } else {
107                 CDEBUG(D_HA, "%s: invoked upcall %s %s %s, time %ldus\n",
108                        cache->uc_name, argv[0], argv[1], argv[2],
109                        (long)ktime_us_delta(end, start));
110                 rc = 0;
111         }
112         EXIT;
113 out:
114         up_read(&cache->uc_upcall_rwsem);
115         return rc;
116 }
117
118 static int mdt_identity_parse_downcall(struct upcall_cache *cache,
119                                        struct upcall_cache_entry *entry,
120                                        void *args)
121 {
122         struct md_identity *identity = &entry->u.identity;
123         struct identity_downcall_data *data = args;
124         struct group_info *ginfo = NULL;
125         struct md_perm *perms = NULL;
126         int size, i;
127         ENTRY;
128
129         LASSERT(data);
130         if (data->idd_ngroups > NGROUPS_MAX)
131                 RETURN(-E2BIG);
132
133         if (data->idd_ngroups > 0) {
134                 ginfo = groups_alloc(data->idd_ngroups);
135                 if (!ginfo) {
136                         CERROR("failed to alloc %d groups\n", data->idd_ngroups);
137                         RETURN(-ENOMEM);
138                 }
139
140                 lustre_groups_from_list(ginfo, data->idd_groups);
141                 lustre_groups_sort(ginfo);
142         }
143
144         if (data->idd_nperms) {
145                 size = data->idd_nperms * sizeof(*perms);
146                 OBD_ALLOC(perms, size);
147                 if (!perms) {
148                         CERROR("failed to alloc %d permissions\n",
149                                data->idd_nperms);
150                         if (ginfo != NULL)
151                                 put_group_info(ginfo);
152                         RETURN(-ENOMEM);
153                 }
154
155                 for (i = 0; i < data->idd_nperms; i++) {
156                         perms[i].mp_nid = data->idd_perms[i].pdd_nid;
157                         perms[i].mp_perm = data->idd_perms[i].pdd_perm;
158                 }
159         }
160
161         identity->mi_uid = data->idd_uid;
162         identity->mi_gid = data->idd_gid;
163         identity->mi_ginfo = ginfo;
164         identity->mi_nperms = data->idd_nperms;
165         identity->mi_perms = perms;
166
167         CDEBUG(D_OTHER, "parse mdt identity@%p: %d:%d, ngroups %u, nperms %u\n",
168                identity, identity->mi_uid, identity->mi_gid,
169                data->idd_ngroups, data->idd_nperms);
170
171         RETURN(0);
172 }
173
174 struct md_identity *mdt_identity_get(struct upcall_cache *cache, __u32 uid)
175 {
176         struct upcall_cache_entry *entry;
177
178         if (!cache)
179                 return ERR_PTR(-ENOENT);
180
181         entry = upcall_cache_get_entry(cache, (__u64)uid, NULL);
182         if (IS_ERR(entry))
183                 return ERR_PTR(PTR_ERR(entry));
184         else if (unlikely(!entry))
185                 return ERR_PTR(-ENOENT);
186         else
187                 return &entry->u.identity;
188 }
189
190 void mdt_identity_put(struct upcall_cache *cache, struct md_identity *identity)
191 {
192         if (!cache)
193                 return;
194
195         LASSERT(identity);
196         upcall_cache_put_entry(cache, identity->mi_uc_entry);
197 }
198
199 struct upcall_cache_ops mdt_identity_upcall_cache_ops = {
200         .init_entry     = mdt_identity_entry_init,
201         .free_entry     = mdt_identity_entry_free,
202         .do_upcall      = mdt_identity_do_upcall,
203         .parse_downcall = mdt_identity_parse_downcall,
204 };
205
206 void mdt_flush_identity(struct upcall_cache *cache, int uid)
207 {
208         if (uid < 0)
209                 upcall_cache_flush_idle(cache);
210         else
211                 upcall_cache_flush_one(cache, (__u64)uid, NULL);
212 }
213
214 /*
215  * If there is LNET_NID_ANY in perm[i].mp_nid,
216  * it must be perm[0].mp_nid, and act as default perm.
217  */
218 __u32 mdt_identity_get_perm(struct md_identity *identity, lnet_nid_t nid)
219 {
220
221         struct md_perm *perm;
222         int i;
223
224         if (!identity)
225                 return CFS_SETGRP_PERM;
226
227         perm = identity->mi_perms;
228         /* check exactly matched nid first */
229         for (i = identity->mi_nperms - 1; i > 0; i--) {
230                 if (perm[i].mp_nid != nid)
231                         continue;
232                 return perm[i].mp_perm;
233         }
234
235         /* check LNET_NID_ANY then */
236         if ((identity->mi_nperms > 0) &&
237             ((perm[0].mp_nid == nid) || (perm[0].mp_nid == LNET_NID_ANY)))
238                 return perm[0].mp_perm;
239
240         /* return default last */
241         return CFS_SETGRP_PERM;
242 }