1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Permission Cache for Remote Client
5 * Author: Lai Siyao <lsy@clusterfs.com>
6 * Author: Fan Yong <fanyong@clusterfs.com>
8 * Copyright (c) 2004-2006 Cluster File Systems, Inc.
10 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
26 #define DEBUG_SUBSYSTEM S_LLITE
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/random.h>
31 #include <linux/version.h>
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"
41 cfs_mem_cache_t *ll_remote_perm_cachep = NULL;
42 cfs_mem_cache_t *ll_rmtperm_hash_cachep = NULL;
44 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
46 struct ll_remote_perm *lrp;
48 OBD_SLAB_ALLOC(lrp, ll_remote_perm_cachep, GFP_KERNEL, sizeof(*lrp));
50 INIT_HLIST_NODE(&lrp->lrp_list);
54 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
59 if (!hlist_unhashed(&lrp->lrp_list))
60 hlist_del(&lrp->lrp_list);
61 OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
64 struct hlist_head *alloc_rmtperm_hash(void)
66 struct hlist_head *hash;
69 OBD_SLAB_ALLOC(hash, ll_rmtperm_hash_cachep, GFP_KERNEL,
70 REMOTE_PERM_HASHSIZE * sizeof(*hash));
75 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
76 INIT_HLIST_HEAD(hash + i);
81 void free_rmtperm_hash(struct hlist_head *hash)
84 struct ll_remote_perm *lrp;
85 struct hlist_node *node, *next;
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));
97 static inline int remote_perm_hashfunc(uid_t uid)
99 return uid & (REMOTE_PERM_HASHSIZE - 1);
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)
106 struct hlist_head *head;
107 struct ll_remote_perm *lrp;
108 struct hlist_node *node;
112 if (!lli->lli_remote_perms)
115 head = lli->lli_remote_perms + remote_perm_hashfunc(current->uid);
117 spin_lock(&lli->lli_lock);
118 hlist_for_each_entry(lrp, node, head, lrp_list) {
119 if (lrp->lrp_uid != current->uid)
121 if (lrp->lrp_gid != current->gid)
123 if (lrp->lrp_fsuid != current->fsuid)
125 if (lrp->lrp_fsgid != current->fsgid)
132 GOTO(out, rc = -ENOENT);
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;
140 spin_unlock(&lli->lli_lock);
144 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
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;
152 LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
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 */
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);
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");
177 spin_lock(&lli->lli_lock);
179 if (!lli->lli_remote_perms)
180 lli->lli_remote_perms = perm_hash;
182 free_rmtperm_hash(perm_hash);
184 head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
187 hlist_for_each_entry(tmp, node, head, lrp_list) {
188 if (tmp->lrp_uid != perm->rp_uid)
190 if (tmp->lrp_gid != perm->rp_gid)
192 if (tmp->lrp_fsuid != perm->rp_fsuid)
194 if (tmp->lrp_fsgid != perm->rp_fsgid)
197 free_ll_remote_perm(lrp);
203 spin_unlock(&lli->lli_lock);
204 lrp = alloc_ll_remote_perm();
206 CERROR("alloc memory for ll_remote_perm failed!\n");
209 spin_lock(&lli->lli_lock);
213 lrp->lrp_access_perm = perm->rp_access_perm;
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);
221 lli->lli_rmtperm_utime = jiffies;
222 spin_unlock(&lli->lli_lock);
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);
231 int lustre_check_remote_perm(struct inode *inode, int mask)
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;
243 utime = lli->lli_rmtperm_utime;
244 rc = do_check_remote_perm(lli, mask);
245 if (!rc || (rc != -ENOENT && i))
250 down(&lli->lli_rmtperm_sem);
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);
261 CERROR("check remote perm falls in dead loop!\n");
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);
270 up(&lli->lli_rmtperm_sem);
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);
282 rc = ll_update_remote_perm(inode, perm);
283 up(&lli->lli_rmtperm_sem);
287 ptlrpc_req_finished(req);
289 ptlrpc_req_finished(req);
293 #if 0 /* NB: remote perms can't be freed in ll_mdc_blocking_ast of UPDATE lock,
294 * because it will fail sanity test 48.
296 void ll_free_remote_perms(struct inode *inode)
298 struct ll_inode_info *lli = ll_i2info(inode);
299 struct hlist_head *hash = lli->lli_remote_perms;
300 struct ll_remote_perm *lrp;
301 struct hlist_node *node, *next;
306 spin_lock(&lli->lli_lock);
308 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
309 hlist_for_each_entry_safe(lrp, node, next, hash + i, lrp_list)
310 free_ll_remote_perm(lrp);
313 spin_unlock(&lli->lli_lock);