Whamcloud - gitweb
add highmem-split patch from HEAD to series
[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 kmem_cache_t *ll_remote_perm_cachep = NULL;
42 kmem_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, SLAB_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, SLAB_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. (lookup/mdc_get_remote_perm). */
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         GOTO(out, rc);
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 (perm->rp_uid != current->uid ||
155             perm->rp_gid != current->gid ||
156             perm->rp_fsuid != current->fsuid ||
157             perm->rp_fsgid != current->fsgid) {
158                 /* user might setxid in this small period */
159                 CDEBUG(D_SEC,
160                        "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
161                        perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
162                        perm->rp_fsgid, current->uid, current->gid,
163                        current->fsuid, current->fsgid);
164                 RETURN(-EAGAIN);
165         }
166
167         if (!lli->lli_remote_perms) {
168                 perm_hash = alloc_rmtperm_hash();
169                 if (perm_hash == NULL) {
170                         CERROR("alloc lli_remote_perms failed!\n");
171                         RETURN(-ENOMEM);
172                 }
173         }
174
175         spin_lock(&lli->lli_lock);
176
177         if (!lli->lli_remote_perms)
178                 lli->lli_remote_perms = perm_hash;
179         else if (perm_hash)
180                 free_rmtperm_hash(perm_hash);
181
182         head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
183
184 again:
185         hlist_for_each_entry(tmp, node, head, lrp_list) {
186                 if (tmp->lrp_uid != current->uid)
187                         continue;
188                 if (tmp->lrp_gid != current->gid)
189                         continue;
190                 if (tmp->lrp_fsuid != current->fsuid)
191                         continue;
192                 if (tmp->lrp_fsgid != current->fsgid)
193                         continue;
194                 if (lrp)
195                         free_ll_remote_perm(lrp);
196                 lrp = tmp;
197                 break;
198         }
199
200         if (!lrp) {
201                 spin_unlock(&lli->lli_lock);
202                 lrp = alloc_ll_remote_perm();
203                 if (!lrp) {
204                         CERROR("alloc memory for ll_remote_perm failed!\n");
205                         RETURN(-ENOMEM);
206                 }
207                 spin_lock(&lli->lli_lock);
208                 goto again;
209         }
210
211         lrp->lrp_uid         = perm->rp_uid;
212         lrp->lrp_gid         = perm->rp_gid;
213         lrp->lrp_fsuid       = perm->rp_fsuid;
214         lrp->lrp_fsgid       = perm->rp_fsgid;
215         lrp->lrp_access_perm = perm->rp_access_perm;
216         if (lrp != tmp)
217                 hlist_add_head(&lrp->lrp_list, head);
218         lli->lli_rmtperm_utime = jiffies;
219         spin_unlock(&lli->lli_lock);
220
221         CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
222                lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
223                lrp->lrp_access_perm);
224
225         RETURN(0);
226 }
227
228 int lustre_check_remote_perm(struct inode *inode, int mask)
229 {
230         struct ll_inode_info *lli = ll_i2info(inode);
231         struct ll_sb_info *sbi = ll_i2sbi(inode);
232         struct ptlrpc_request *req = NULL;
233         struct mdt_remote_perm *perm;
234         struct obd_capa *oc;
235         unsigned long utime;
236         int i = 0, rc;
237         ENTRY;
238
239 check:
240         utime = lli->lli_rmtperm_utime;
241         rc = do_check_remote_perm(lli, mask);
242         if (!rc || ((rc != -ENOENT) && i))
243                 RETURN(rc);
244
245         might_sleep();
246
247         down(&lli->lli_rmtperm_sem);
248         /* check again */
249         if (utime != lli->lli_rmtperm_utime) {
250                 rc = do_check_remote_perm(lli, mask);
251                 if (!rc || ((rc != -ENOENT) && i)) {
252                         up(&lli->lli_rmtperm_sem);
253                         RETURN(rc);
254                 }
255         }
256
257         if (i++ > 5) {
258                 CERROR("check remote perm falls in dead loop!\n");
259                 LBUG();
260         }
261
262         oc = ll_mdscapa_get(inode);
263         rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc, &req);
264         capa_put(oc);
265         if (rc) {
266                 up(&lli->lli_rmtperm_sem);
267                 RETURN(rc);
268         }
269
270         perm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, sizeof(*perm));
271         LASSERT(perm);
272         LASSERT_REPSWABBED(req, REPLY_REC_OFF + 1);
273
274         rc = ll_update_remote_perm(inode, perm);
275         up(&lli->lli_rmtperm_sem);
276
277         ptlrpc_req_finished(req);
278
279         if (rc == -ENOMEM)
280                 RETURN(rc);
281
282         goto check;
283 }
284
285 #if 0  /* NB: remote perms can't be freed in ll_mdc_blocking_ast of UPDATE lock,
286         * because it will fail sanity test 48.
287         */
288 void ll_free_remote_perms(struct inode *inode)
289 {
290         struct ll_inode_info *lli = ll_i2info(inode);
291         struct hlist_head *hash = lli->lli_remote_perms;
292         struct ll_remote_perm *lrp;
293         struct hlist_node *node, *next;
294         int i;
295
296         LASSERT(hash);
297
298         spin_lock(&lli->lli_lock);
299
300         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
301                 hlist_for_each_entry_safe(lrp, node, next, hash + i, lrp_list)
302                         free_ll_remote_perm(lrp);
303         }
304
305         spin_unlock(&lli->lli_lock);
306 }
307 #endif