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