Whamcloud - gitweb
b=15908
[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  * Lustre Permission Cache for Remote Client
5  *   Author: Lai Siyao <lsy@clusterfs.com>
6  *   Author: Fan Yong <fanyong@clusterfs.com>
7  *
8  *  Copyright (c) 2004-2006 Cluster File Systems, Inc.
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #define DEBUG_SUBSYSTEM S_LLITE
27
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/random.h>
31 #include <linux/version.h>
32
33 #include <lustre_lite.h>
34 #include <lustre_ha.h>
35 #include <lustre_dlm.h>
36 #include <lprocfs_status.h>
37 #include <lustre_disk.h>
38 #include <lustre_param.h>
39 #include "llite_internal.h"
40
41 cfs_mem_cache_t *ll_remote_perm_cachep = NULL;
42 cfs_mem_cache_t *ll_rmtperm_hash_cachep = NULL;
43
44 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
45 {
46         struct ll_remote_perm *lrp;
47
48         OBD_SLAB_ALLOC(lrp, ll_remote_perm_cachep, GFP_KERNEL, sizeof(*lrp));
49         if (lrp)
50                 INIT_HLIST_NODE(&lrp->lrp_list);
51         return lrp;
52 }
53
54 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
55 {
56         if (!lrp)
57                 return;
58
59         if (!hlist_unhashed(&lrp->lrp_list))
60                 hlist_del(&lrp->lrp_list);
61         OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
62 }
63
64 struct hlist_head *alloc_rmtperm_hash(void)
65 {
66         struct hlist_head *hash;
67         int i;
68
69         OBD_SLAB_ALLOC(hash, ll_rmtperm_hash_cachep, GFP_KERNEL,
70                        REMOTE_PERM_HASHSIZE * sizeof(*hash));
71
72         if (!hash)
73                 return NULL;
74
75         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
76                 INIT_HLIST_HEAD(hash + i);
77
78         return hash;
79 }
80
81 void free_rmtperm_hash(struct hlist_head *hash)
82 {
83         int i;
84         struct ll_remote_perm *lrp;
85         struct hlist_node *node, *next;
86
87         if(!hash)
88                 return;
89
90         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
91                 hlist_for_each_entry_safe(lrp, node, next, hash + i, lrp_list)
92                         free_ll_remote_perm(lrp);
93         OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
94                       REMOTE_PERM_HASHSIZE * sizeof(*hash));
95 }
96
97 static inline int remote_perm_hashfunc(uid_t uid)
98 {
99         return uid & (REMOTE_PERM_HASHSIZE - 1);
100 }
101
102 /* NB: setxid permission is not checked here, instead it's done on
103  * MDT when client get remote permission. */
104 static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
105 {
106         struct hlist_head *head;
107         struct ll_remote_perm *lrp;
108         struct hlist_node *node;
109         int found = 0, rc;
110         ENTRY;
111
112         if (!lli->lli_remote_perms)
113                 RETURN(-ENOENT);
114
115         head = lli->lli_remote_perms + remote_perm_hashfunc(current->uid);
116
117         spin_lock(&lli->lli_lock);
118         hlist_for_each_entry(lrp, node, head, lrp_list) {
119                 if (lrp->lrp_uid != current->uid)
120                         continue;
121                 if (lrp->lrp_gid != current->gid)
122                         continue;
123                 if (lrp->lrp_fsuid != current->fsuid)
124                         continue;
125                 if (lrp->lrp_fsgid != current->fsgid)
126                         continue;
127                 found = 1;
128                 break;
129         }
130
131         if (!found)
132                 GOTO(out, rc = -ENOENT);
133
134         CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
135                lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
136                lrp->lrp_access_perm);
137         rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
138
139 out:
140         spin_unlock(&lli->lli_lock);
141         return rc;
142 }
143
144 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
145 {
146         struct ll_inode_info *lli = ll_i2info(inode);
147         struct ll_remote_perm *lrp = NULL, *tmp = NULL;
148         struct hlist_head *head, *perm_hash = NULL;
149         struct hlist_node *node;
150         ENTRY;
151
152         LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
153
154 #if 0
155         if (perm->rp_uid != current->uid ||
156             perm->rp_gid != current->gid ||
157             perm->rp_fsuid != current->fsuid ||
158             perm->rp_fsgid != current->fsgid) {
159                 /* user might setxid in this small period */
160                 CDEBUG(D_SEC,
161                        "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
162                        perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
163                        perm->rp_fsgid, current->uid, current->gid,
164                        current->fsuid, current->fsgid);
165                 RETURN(-EAGAIN);
166         }
167 #endif
168
169         if (!lli->lli_remote_perms) {
170                 perm_hash = alloc_rmtperm_hash();
171                 if (perm_hash == NULL) {
172                         CERROR("alloc lli_remote_perms failed!\n");
173                         RETURN(-ENOMEM);
174                 }
175         }
176
177         spin_lock(&lli->lli_lock);
178
179         if (!lli->lli_remote_perms)
180                 lli->lli_remote_perms = perm_hash;
181         else if (perm_hash)
182                 free_rmtperm_hash(perm_hash);
183
184         head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
185
186 again:
187         hlist_for_each_entry(tmp, node, head, lrp_list) {
188                 if (tmp->lrp_uid != perm->rp_uid)
189                         continue;
190                 if (tmp->lrp_gid != perm->rp_gid)
191                         continue;
192                 if (tmp->lrp_fsuid != perm->rp_fsuid)
193                         continue;
194                 if (tmp->lrp_fsgid != perm->rp_fsgid)
195                         continue;
196                 if (lrp)
197                         free_ll_remote_perm(lrp);
198                 lrp = tmp;
199                 break;
200         }
201
202         if (!lrp) {
203                 spin_unlock(&lli->lli_lock);
204                 lrp = alloc_ll_remote_perm();
205                 if (!lrp) {
206                         CERROR("alloc memory for ll_remote_perm failed!\n");
207                         RETURN(-ENOMEM);
208                 }
209                 spin_lock(&lli->lli_lock);
210                 goto again;
211         }
212
213         lrp->lrp_access_perm = perm->rp_access_perm;
214         if (lrp != tmp) {
215                 lrp->lrp_uid         = perm->rp_uid;
216                 lrp->lrp_gid         = perm->rp_gid;
217                 lrp->lrp_fsuid       = perm->rp_fsuid;
218                 lrp->lrp_fsgid       = perm->rp_fsgid;
219                 hlist_add_head(&lrp->lrp_list, head);
220         }
221         lli->lli_rmtperm_utime = jiffies;
222         spin_unlock(&lli->lli_lock);
223
224         CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
225                lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
226                lrp->lrp_access_perm);
227
228         RETURN(0);
229 }
230
231 int lustre_check_remote_perm(struct inode *inode, int mask)
232 {
233         struct ll_inode_info *lli = ll_i2info(inode);
234         struct ll_sb_info *sbi = ll_i2sbi(inode);
235         struct ptlrpc_request *req = NULL;
236         struct mdt_remote_perm *perm;
237         struct obd_capa *oc;
238         unsigned long utime;
239         int i = 0, rc;
240         ENTRY;
241
242         do {
243                 utime = lli->lli_rmtperm_utime;
244                 rc = do_check_remote_perm(lli, mask);
245                 if (!rc || (rc != -ENOENT && i))
246                         break;
247
248                 might_sleep();
249
250                 down(&lli->lli_rmtperm_sem);
251                 /* check again */
252                 if (utime != lli->lli_rmtperm_utime) {
253                         rc = do_check_remote_perm(lli, mask);
254                         if (!rc || (rc != -ENOENT && i)) {
255                                 up(&lli->lli_rmtperm_sem);
256                                 break;
257                         }
258                 }
259
260                 if (i++ > 5) {
261                         CERROR("check remote perm falls in dead loop!\n");
262                         LBUG();
263                 }
264
265                 oc = ll_mdscapa_get(inode);
266                 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc,
267                                         ll_i2suppgid(inode), &req);
268                 capa_put(oc);
269                 if (rc) {
270                         up(&lli->lli_rmtperm_sem);
271                         break;
272                 }
273
274                 perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
275                                                    lustre_swab_mdt_remote_perm);
276                 if (unlikely(perm == NULL)) {
277                         up(&lli->lli_rmtperm_sem);
278                         rc = -EPROTO;
279                         break;
280                 }
281
282                 rc = ll_update_remote_perm(inode, perm);
283                 up(&lli->lli_rmtperm_sem);
284                 if (rc == -ENOMEM)
285                         break;
286
287                 ptlrpc_req_finished(req);
288                 req = NULL;
289         } while (1);
290         ptlrpc_req_finished(req);
291         RETURN(rc);
292 }
293
294 #if 0  /* NB: remote perms can't be freed in ll_mdc_blocking_ast of UPDATE lock,
295         * because it will fail sanity test 48.
296         */
297 void ll_free_remote_perms(struct inode *inode)
298 {
299         struct ll_inode_info *lli = ll_i2info(inode);
300         struct hlist_head *hash = lli->lli_remote_perms;
301         struct ll_remote_perm *lrp;
302         struct hlist_node *node, *next;
303         int i;
304
305         LASSERT(hash);
306
307         spin_lock(&lli->lli_lock);
308
309         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
310                 hlist_for_each_entry_safe(lrp, node, next, hash + i, lrp_list)
311                         free_ll_remote_perm(lrp);
312         }
313
314         spin_unlock(&lli->lli_lock);
315 }
316 #endif