4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/llite/remote_perm.c
38 * Lustre Permission Cache for Remote Client
40 * Author: Lai Siyao <lsy@clusterfs.com>
41 * Author: Fan Yong <fanyong@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/version.h>
50 #include <lustre_ha.h>
51 #include <lustre_dlm.h>
52 #include <lprocfs_status.h>
53 #include <lustre_disk.h>
54 #include <lustre_param.h>
55 #include "llite_internal.h"
57 struct kmem_cache *ll_remote_perm_cachep;
58 struct kmem_cache *ll_rmtperm_hash_cachep;
60 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
62 struct ll_remote_perm *lrp;
64 OBD_SLAB_ALLOC_PTR_GFP(lrp, ll_remote_perm_cachep, GFP_KERNEL);
66 INIT_HLIST_NODE(&lrp->lrp_list);
70 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
75 if (!hlist_unhashed(&lrp->lrp_list))
76 hlist_del(&lrp->lrp_list);
77 OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
80 static struct hlist_head *alloc_rmtperm_hash(void)
82 struct hlist_head *hash;
85 OBD_SLAB_ALLOC_GFP(hash, ll_rmtperm_hash_cachep,
86 REMOTE_PERM_HASHSIZE * sizeof(*hash),
91 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
92 INIT_HLIST_HEAD(hash + i);
97 void free_rmtperm_hash(struct hlist_head *hash)
100 struct ll_remote_perm *lrp;
101 struct hlist_node __maybe_unused *node;
102 struct hlist_node *next;
107 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
108 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
110 free_ll_remote_perm(lrp);
111 OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
112 REMOTE_PERM_HASHSIZE * sizeof(*hash));
115 static inline int remote_perm_hashfunc(uid_t uid)
117 return uid & (REMOTE_PERM_HASHSIZE - 1);
120 /* NB: setxid permission is not checked here, instead it's done on
121 * MDT when client get remote permission. */
122 static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
124 struct hlist_head *head;
125 struct ll_remote_perm *lrp;
126 struct hlist_node __maybe_unused *node;
130 if (!lli->lli_remote_perms)
133 head = lli->lli_remote_perms +
134 remote_perm_hashfunc(from_kuid(&init_user_ns, current_uid()));
136 spin_lock(&lli->lli_lock);
137 cfs_hlist_for_each_entry(lrp, node, head, lrp_list) {
138 if (lrp->lrp_uid != from_kuid(&init_user_ns, current_uid()))
140 if (lrp->lrp_gid != from_kgid(&init_user_ns, current_gid()))
142 if (lrp->lrp_fsuid != from_kuid(&init_user_ns, current_fsuid()))
144 if (lrp->lrp_fsgid != from_kgid(&init_user_ns, current_fsgid()))
151 GOTO(out, rc = -ENOENT);
153 CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
154 lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
155 lrp->lrp_access_perm);
156 rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
159 spin_unlock(&lli->lli_lock);
163 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
165 struct ll_inode_info *lli = ll_i2info(inode);
166 struct ll_remote_perm *lrp = NULL, *tmp = NULL;
167 struct hlist_head *head, *perm_hash = NULL;
168 struct hlist_node __maybe_unused *node;
171 LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
174 if (perm->rp_uid != current->uid ||
175 perm->rp_gid != current->gid ||
176 perm->rp_fsuid != current->fsuid ||
177 perm->rp_fsgid != current->fsgid) {
178 /* user might setxid in this small period */
180 "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
181 perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
182 perm->rp_fsgid, current->uid, current->gid,
183 current->fsuid, current->fsgid);
188 if (!lli->lli_remote_perms) {
189 perm_hash = alloc_rmtperm_hash();
190 if (perm_hash == NULL) {
191 CERROR("alloc lli_remote_perms failed!\n");
196 spin_lock(&lli->lli_lock);
198 if (!lli->lli_remote_perms)
199 lli->lli_remote_perms = perm_hash;
201 free_rmtperm_hash(perm_hash);
203 head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
206 cfs_hlist_for_each_entry(tmp, node, head, lrp_list) {
207 if (tmp->lrp_uid != perm->rp_uid)
209 if (tmp->lrp_gid != perm->rp_gid)
211 if (tmp->lrp_fsuid != perm->rp_fsuid)
213 if (tmp->lrp_fsgid != perm->rp_fsgid)
216 free_ll_remote_perm(lrp);
222 spin_unlock(&lli->lli_lock);
223 lrp = alloc_ll_remote_perm();
225 CERROR("alloc memory for ll_remote_perm failed!\n");
228 spin_lock(&lli->lli_lock);
232 lrp->lrp_access_perm = perm->rp_access_perm;
234 lrp->lrp_uid = perm->rp_uid;
235 lrp->lrp_gid = perm->rp_gid;
236 lrp->lrp_fsuid = perm->rp_fsuid;
237 lrp->lrp_fsgid = perm->rp_fsgid;
238 hlist_add_head(&lrp->lrp_list, head);
240 lli->lli_rmtperm_time = cfs_time_current();
241 spin_unlock(&lli->lli_lock);
243 CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
244 lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
245 lrp->lrp_access_perm);
250 int lustre_check_remote_perm(struct inode *inode, int mask)
252 struct ll_inode_info *lli = ll_i2info(inode);
253 struct ll_sb_info *sbi = ll_i2sbi(inode);
254 struct ptlrpc_request *req = NULL;
255 struct mdt_remote_perm *perm;
262 save = lli->lli_rmtperm_time;
263 rc = do_check_remote_perm(lli, mask);
264 if (!rc || (rc != -ENOENT && i))
269 mutex_lock(&lli->lli_rmtperm_mutex);
271 if (save != lli->lli_rmtperm_time) {
272 rc = do_check_remote_perm(lli, mask);
273 if (!rc || (rc != -ENOENT && i)) {
274 mutex_unlock(&lli->lli_rmtperm_mutex);
280 CERROR("check remote perm falls in dead loop!\n");
284 oc = ll_mdscapa_get(inode);
285 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc,
286 ll_i2suppgid(inode), &req);
289 mutex_unlock(&lli->lli_rmtperm_mutex);
293 perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
294 lustre_swab_mdt_remote_perm);
295 if (unlikely(perm == NULL)) {
296 mutex_unlock(&lli->lli_rmtperm_mutex);
301 rc = ll_update_remote_perm(inode, perm);
302 mutex_unlock(&lli->lli_rmtperm_mutex);
306 ptlrpc_req_finished(req);
309 ptlrpc_req_finished(req);