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