Whamcloud - gitweb
2789412011a03cf9fecc157f51d3102beddac9ad
[fs/lustre-release.git] / lustre / llite / remote_perm.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) 2012, 2014, 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/llite/remote_perm.c
37  *
38  * Lustre Permission Cache for Remote Client
39  *
40  * Author: Lai Siyao <lsy@clusterfs.com>
41  * Author: Fan Yong <fanyong@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/version.h>
49 #include <linux/user_namespace.h>
50 #ifdef HAVE_UIDGID_HEADER
51 # include <linux/uidgid.h>
52 #endif
53
54 #include <lustre_ha.h>
55 #include <lustre_dlm.h>
56 #include <lprocfs_status.h>
57 #include <lustre_disk.h>
58 #include <lustre_param.h>
59 #include "llite_internal.h"
60
61 struct kmem_cache *ll_remote_perm_cachep;
62 struct kmem_cache *ll_rmtperm_hash_cachep;
63
64 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
65 {
66         struct ll_remote_perm *lrp;
67
68         OBD_SLAB_ALLOC_PTR_GFP(lrp, ll_remote_perm_cachep, GFP_KERNEL);
69         if (lrp)
70                 INIT_HLIST_NODE(&lrp->lrp_list);
71         return lrp;
72 }
73
74 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
75 {
76         if (!lrp)
77                 return;
78
79         if (!hlist_unhashed(&lrp->lrp_list))
80                 hlist_del(&lrp->lrp_list);
81         OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
82 }
83
84 static struct hlist_head *alloc_rmtperm_hash(void)
85 {
86         struct hlist_head *hash;
87         int i;
88
89         OBD_SLAB_ALLOC_GFP(hash, ll_rmtperm_hash_cachep,
90                            REMOTE_PERM_HASHSIZE * sizeof(*hash),
91                            GFP_IOFS);
92         if (!hash)
93                 return NULL;
94
95         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
96                 INIT_HLIST_HEAD(hash + i);
97
98         return hash;
99 }
100
101 void free_rmtperm_hash(struct hlist_head *hash)
102 {
103         int i;
104         struct ll_remote_perm *lrp;
105         struct hlist_node __maybe_unused *node;
106         struct hlist_node *next;
107
108         if(!hash)
109                 return;
110
111         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
112                 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
113                                               lrp_list)
114                         free_ll_remote_perm(lrp);
115         OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
116                       REMOTE_PERM_HASHSIZE * sizeof(*hash));
117 }
118
119 static inline int remote_perm_hashfunc(uid_t uid)
120 {
121         return uid & (REMOTE_PERM_HASHSIZE - 1);
122 }
123
124 /* NB: setxid permission is not checked here, instead it's done on
125  * MDT when client get remote permission. */
126 static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
127 {
128         struct hlist_head *head;
129         struct ll_remote_perm *lrp;
130         struct hlist_node __maybe_unused *node;
131         int found = 0, rc;
132         ENTRY;
133
134         if (!lli->lli_remote_perms)
135                 RETURN(-ENOENT);
136
137         head = lli->lli_remote_perms +
138                 remote_perm_hashfunc(from_kuid(&init_user_ns, current_uid()));
139
140         spin_lock(&lli->lli_lock);
141         cfs_hlist_for_each_entry(lrp, node, head, lrp_list) {
142                 if (lrp->lrp_uid != from_kuid(&init_user_ns, current_uid()))
143                         continue;
144                 if (lrp->lrp_gid != from_kgid(&init_user_ns, current_gid()))
145                         continue;
146                 if (lrp->lrp_fsuid != from_kuid(&init_user_ns, current_fsuid()))
147                         continue;
148                 if (lrp->lrp_fsgid != from_kgid(&init_user_ns, current_fsgid()))
149                         continue;
150                 found = 1;
151                 break;
152         }
153
154         if (!found)
155                 GOTO(out, rc = -ENOENT);
156
157         CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
158                lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
159                lrp->lrp_access_perm);
160         rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
161
162 out:
163         spin_unlock(&lli->lli_lock);
164         return rc;
165 }
166
167 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
168 {
169         struct ll_inode_info *lli = ll_i2info(inode);
170         struct ll_remote_perm *lrp = NULL, *tmp = NULL;
171         struct hlist_head *head, *perm_hash = NULL;
172         struct hlist_node __maybe_unused *node;
173         ENTRY;
174
175         LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
176
177 #if 0
178         if (perm->rp_uid != current->uid ||
179             perm->rp_gid != current->gid ||
180             perm->rp_fsuid != current->fsuid ||
181             perm->rp_fsgid != current->fsgid) {
182                 /* user might setxid in this small period */
183                 CDEBUG(D_SEC,
184                        "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
185                        perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
186                        perm->rp_fsgid, current->uid, current->gid,
187                        current->fsuid, current->fsgid);
188                 RETURN(-EAGAIN);
189         }
190 #endif
191
192         if (!lli->lli_remote_perms) {
193                 perm_hash = alloc_rmtperm_hash();
194                 if (perm_hash == NULL) {
195                         CERROR("alloc lli_remote_perms failed!\n");
196                         RETURN(-ENOMEM);
197                 }
198         }
199
200         spin_lock(&lli->lli_lock);
201
202         if (!lli->lli_remote_perms)
203                 lli->lli_remote_perms = perm_hash;
204         else if (perm_hash)
205                 free_rmtperm_hash(perm_hash);
206
207         head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
208
209 again:
210         cfs_hlist_for_each_entry(tmp, node, head, lrp_list) {
211                 if (tmp->lrp_uid != perm->rp_uid)
212                         continue;
213                 if (tmp->lrp_gid != perm->rp_gid)
214                         continue;
215                 if (tmp->lrp_fsuid != perm->rp_fsuid)
216                         continue;
217                 if (tmp->lrp_fsgid != perm->rp_fsgid)
218                         continue;
219                 if (lrp)
220                         free_ll_remote_perm(lrp);
221                 lrp = tmp;
222                 break;
223         }
224
225         if (!lrp) {
226                 spin_unlock(&lli->lli_lock);
227                 lrp = alloc_ll_remote_perm();
228                 if (!lrp) {
229                         CERROR("alloc memory for ll_remote_perm failed!\n");
230                         RETURN(-ENOMEM);
231                 }
232                 spin_lock(&lli->lli_lock);
233                 goto again;
234         }
235
236         lrp->lrp_access_perm = perm->rp_access_perm;
237         if (lrp != tmp) {
238                 lrp->lrp_uid         = perm->rp_uid;
239                 lrp->lrp_gid         = perm->rp_gid;
240                 lrp->lrp_fsuid       = perm->rp_fsuid;
241                 lrp->lrp_fsgid       = perm->rp_fsgid;
242                 hlist_add_head(&lrp->lrp_list, head);
243         }
244         lli->lli_rmtperm_time = cfs_time_current();
245         spin_unlock(&lli->lli_lock);
246
247         CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
248                lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
249                lrp->lrp_access_perm);
250
251         RETURN(0);
252 }
253
254 int lustre_check_remote_perm(struct inode *inode, int mask)
255 {
256         struct ll_inode_info *lli = ll_i2info(inode);
257         struct ll_sb_info *sbi = ll_i2sbi(inode);
258         struct ptlrpc_request *req = NULL;
259         struct mdt_remote_perm *perm;
260         struct obd_capa *oc;
261         cfs_time_t save;
262         int i = 0, rc;
263         ENTRY;
264
265         do {
266                 save = lli->lli_rmtperm_time;
267                 rc = do_check_remote_perm(lli, mask);
268                 if (!rc || (rc != -ENOENT && i))
269                         break;
270
271                 might_sleep();
272
273                 mutex_lock(&lli->lli_rmtperm_mutex);
274                 /* check again */
275                 if (save != lli->lli_rmtperm_time) {
276                         rc = do_check_remote_perm(lli, mask);
277                         if (!rc || (rc != -ENOENT && i)) {
278                                 mutex_unlock(&lli->lli_rmtperm_mutex);
279                                 break;
280                         }
281                 }
282
283                 if (i++ > 5) {
284                         CERROR("check remote perm falls in dead loop!\n");
285                         LBUG();
286                 }
287
288                 oc = ll_mdscapa_get(inode);
289                 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc,
290                                         ll_i2suppgid(inode), &req);
291                 capa_put(oc);
292                 if (rc) {
293                         mutex_unlock(&lli->lli_rmtperm_mutex);
294                         break;
295                 }
296
297                 perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
298                                                    lustre_swab_mdt_remote_perm);
299                 if (unlikely(perm == NULL)) {
300                         mutex_unlock(&lli->lli_rmtperm_mutex);
301                         rc = -EPROTO;
302                         break;
303                 }
304
305                 rc = ll_update_remote_perm(inode, perm);
306                 mutex_unlock(&lli->lli_rmtperm_mutex);
307                 if (rc == -ENOMEM)
308                         break;
309
310                 ptlrpc_req_finished(req);
311                 req = NULL;
312         } while (1);
313         ptlrpc_req_finished(req);
314         RETURN(rc);
315 }