Whamcloud - gitweb
LU-812 compat: clean up mutex lock to use kernel mutex primitive
[fs/lustre-release.git] / lustre / llite / remote_perm.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/llite/remote_perm.c
39  *
40  * Lustre Permission Cache for Remote Client
41  *
42  * Author: Lai Siyao <lsy@clusterfs.com>
43  * Author: Fan Yong <fanyong@clusterfs.com>
44  */
45
46 #define DEBUG_SUBSYSTEM S_LLITE
47
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/version.h>
51
52 #include <lustre_lite.h>
53 #include <lustre_ha.h>
54 #include <lustre_dlm.h>
55 #include <lprocfs_status.h>
56 #include <lustre_disk.h>
57 #include <lustre_param.h>
58 #include "llite_internal.h"
59
60 cfs_mem_cache_t *ll_remote_perm_cachep = NULL;
61 cfs_mem_cache_t *ll_rmtperm_hash_cachep = NULL;
62
63 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
64 {
65         struct ll_remote_perm *lrp;
66
67         OBD_SLAB_ALLOC_PTR_GFP(lrp, ll_remote_perm_cachep, GFP_KERNEL);
68         if (lrp)
69                 CFS_INIT_HLIST_NODE(&lrp->lrp_list);
70         return lrp;
71 }
72
73 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
74 {
75         if (!lrp)
76                 return;
77
78         if (!cfs_hlist_unhashed(&lrp->lrp_list))
79                 cfs_hlist_del(&lrp->lrp_list);
80         OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
81 }
82
83 cfs_hlist_head_t *alloc_rmtperm_hash(void)
84 {
85         cfs_hlist_head_t *hash;
86         int i;
87
88         OBD_SLAB_ALLOC(hash, ll_rmtperm_hash_cachep, GFP_KERNEL,
89                        REMOTE_PERM_HASHSIZE * sizeof(*hash));
90
91         if (!hash)
92                 return NULL;
93
94         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
95                 CFS_INIT_HLIST_HEAD(hash + i);
96
97         return hash;
98 }
99
100 void free_rmtperm_hash(cfs_hlist_head_t *hash)
101 {
102         int i;
103         struct ll_remote_perm *lrp;
104         cfs_hlist_node_t *node, *next;
105
106         if(!hash)
107                 return;
108
109         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
110                 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
111                                               lrp_list)
112                         free_ll_remote_perm(lrp);
113         OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
114                       REMOTE_PERM_HASHSIZE * sizeof(*hash));
115 }
116
117 static inline int remote_perm_hashfunc(uid_t uid)
118 {
119         return uid & (REMOTE_PERM_HASHSIZE - 1);
120 }
121
122 /* NB: setxid permission is not checked here, instead it's done on
123  * MDT when client get remote permission. */
124 static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
125 {
126         cfs_hlist_head_t *head;
127         struct ll_remote_perm *lrp;
128         cfs_hlist_node_t *node;
129         int found = 0, rc;
130         ENTRY;
131
132         if (!lli->lli_remote_perms)
133                 RETURN(-ENOENT);
134
135         head = lli->lli_remote_perms + remote_perm_hashfunc(cfs_curproc_uid());
136
137         cfs_spin_lock(&lli->lli_lock);
138         cfs_hlist_for_each_entry(lrp, node, head, lrp_list) {
139                 if (lrp->lrp_uid != cfs_curproc_uid())
140                         continue;
141                 if (lrp->lrp_gid != cfs_curproc_gid())
142                         continue;
143                 if (lrp->lrp_fsuid != cfs_curproc_fsuid())
144                         continue;
145                 if (lrp->lrp_fsgid != cfs_curproc_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         cfs_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         cfs_hlist_head_t *head, *perm_hash = NULL;
169         cfs_hlist_node_t *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         cfs_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                 cfs_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                 cfs_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                 cfs_hlist_add_head(&lrp->lrp_list, head);
240         }
241         lli->lli_rmtperm_time = cfs_time_current();
242         cfs_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                 cfs_might_sleep();
269
270                 cfs_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                                 cfs_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                         cfs_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                         cfs_mutex_unlock(&lli->lli_rmtperm_mutex);
298                         rc = -EPROTO;
299                         break;
300                 }
301
302                 rc = ll_update_remote_perm(inode, perm);
303                 cfs_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 }
313
314 #if 0  /* NB: remote perms can't be freed in ll_mdc_blocking_ast of UPDATE lock,
315         * because it will fail sanity test 48.
316         */
317 void ll_free_remote_perms(struct inode *inode)
318 {
319         struct ll_inode_info *lli = ll_i2info(inode);
320         cfs_hlist_head_t *hash = lli->lli_remote_perms;
321         struct ll_remote_perm *lrp;
322         cfs_hlist_node_t *node, *next;
323         int i;
324
325         LASSERT(hash);
326
327         cfs_spin_lock(&lli->lli_lock);
328
329         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
330                 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
331                                               lrp_list)
332                         free_ll_remote_perm(lrp);
333         }
334
335         cfs_spin_unlock(&lli->lli_lock);
336 }
337 #endif