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