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