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 kmem_cache_t *ll_remote_perm_cachep = NULL;
42 kmem_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, SLAB_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, SLAB_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. (lookup/mdc_get_remote_perm). */
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);
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 */
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);
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");
175 spin_lock(&lli->lli_lock);
177 if (!lli->lli_remote_perms)
178 lli->lli_remote_perms = perm_hash;
180 free_rmtperm_hash(perm_hash);
182 head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
185 hlist_for_each_entry(tmp, node, head, lrp_list) {
186 if (tmp->lrp_uid != current->uid)
188 if (tmp->lrp_gid != current->gid)
190 if (tmp->lrp_fsuid != current->fsuid)
192 if (tmp->lrp_fsgid != current->fsgid)
195 free_ll_remote_perm(lrp);
201 spin_unlock(&lli->lli_lock);
202 lrp = alloc_ll_remote_perm();
204 CERROR("alloc memory for ll_remote_perm failed!\n");
207 spin_lock(&lli->lli_lock);
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;
217 hlist_add_head(&lrp->lrp_list, head);
218 lli->lli_rmtperm_utime = jiffies;
219 spin_unlock(&lli->lli_lock);
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);
228 int lustre_check_remote_perm(struct inode *inode, int mask)
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;
240 utime = lli->lli_rmtperm_utime;
241 rc = do_check_remote_perm(lli, mask);
242 if (!rc || ((rc != -ENOENT) && i))
247 down(&lli->lli_rmtperm_sem);
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);
258 CERROR("check remote perm falls in dead loop!\n");
262 oc = ll_mdscapa_get(inode);
263 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc, &req);
266 up(&lli->lli_rmtperm_sem);
270 perm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, sizeof(*perm));
272 LASSERT_REPSWABBED(req, REPLY_REC_OFF + 1);
274 rc = ll_update_remote_perm(inode, perm);
275 up(&lli->lli_rmtperm_sem);
277 ptlrpc_req_finished(req);
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.
288 void ll_free_remote_perms(struct inode *inode)
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;
298 spin_lock(&lli->lli_lock);
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);
305 spin_unlock(&lli->lli_lock);