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, 2014, 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>
49 #include <linux/user_namespace.h>
50 #ifdef HAVE_UIDGID_HEADER
51 # include <linux/uidgid.h>
54 #include <lustre_dlm.h>
55 #include <lustre_ha.h>
56 #include <lustre_param.h>
57 #include <lustre_swab.h>
58 #include <lprocfs_status.h>
59 #include <lustre_disk.h>
61 #include "llite_internal.h"
63 struct kmem_cache *ll_remote_perm_cachep;
64 struct kmem_cache *ll_rmtperm_hash_cachep;
66 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
68 struct ll_remote_perm *lrp;
70 OBD_SLAB_ALLOC_PTR_GFP(lrp, ll_remote_perm_cachep, GFP_KERNEL);
72 INIT_HLIST_NODE(&lrp->lrp_list);
76 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
81 if (!hlist_unhashed(&lrp->lrp_list))
82 hlist_del(&lrp->lrp_list);
83 OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
86 static struct hlist_head *alloc_rmtperm_hash(void)
88 struct hlist_head *hash;
91 OBD_SLAB_ALLOC_GFP(hash, ll_rmtperm_hash_cachep,
92 REMOTE_PERM_HASHSIZE * sizeof(*hash),
97 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
98 INIT_HLIST_HEAD(hash + i);
103 void free_rmtperm_hash(struct hlist_head *hash)
106 struct ll_remote_perm *lrp;
107 struct hlist_node __maybe_unused *node;
108 struct hlist_node *next;
113 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
114 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
116 free_ll_remote_perm(lrp);
117 OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
118 REMOTE_PERM_HASHSIZE * sizeof(*hash));
121 static inline int remote_perm_hashfunc(uid_t uid)
123 return uid & (REMOTE_PERM_HASHSIZE - 1);
126 /* NB: setxid permission is not checked here, instead it's done on
127 * MDT when client get remote permission. */
128 static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
130 struct hlist_head *head;
131 struct ll_remote_perm *lrp;
132 struct hlist_node __maybe_unused *node;
136 if (!lli->lli_remote_perms)
139 head = lli->lli_remote_perms +
140 remote_perm_hashfunc(from_kuid(&init_user_ns, current_uid()));
142 spin_lock(&lli->lli_lock);
143 cfs_hlist_for_each_entry(lrp, node, head, lrp_list) {
144 if (lrp->lrp_uid != from_kuid(&init_user_ns, current_uid()))
146 if (lrp->lrp_gid != from_kgid(&init_user_ns, current_gid()))
148 if (lrp->lrp_fsuid != from_kuid(&init_user_ns, current_fsuid()))
150 if (lrp->lrp_fsgid != from_kgid(&init_user_ns, current_fsgid()))
157 GOTO(out, rc = -ENOENT);
159 CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
160 lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
161 lrp->lrp_access_perm);
162 rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
165 spin_unlock(&lli->lli_lock);
169 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
171 struct ll_inode_info *lli = ll_i2info(inode);
172 struct ll_remote_perm *lrp = NULL, *tmp = NULL;
173 struct hlist_head *head, *perm_hash = NULL;
174 struct hlist_node __maybe_unused *node;
177 LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
180 if (perm->rp_uid != current->uid ||
181 perm->rp_gid != current->gid ||
182 perm->rp_fsuid != current->fsuid ||
183 perm->rp_fsgid != current->fsgid) {
184 /* user might setxid in this small period */
186 "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
187 perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
188 perm->rp_fsgid, current->uid, current->gid,
189 current->fsuid, current->fsgid);
194 if (!lli->lli_remote_perms) {
195 perm_hash = alloc_rmtperm_hash();
196 if (perm_hash == NULL) {
197 CERROR("alloc lli_remote_perms failed!\n");
202 spin_lock(&lli->lli_lock);
204 if (!lli->lli_remote_perms)
205 lli->lli_remote_perms = perm_hash;
207 free_rmtperm_hash(perm_hash);
209 head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
212 cfs_hlist_for_each_entry(tmp, node, head, lrp_list) {
213 if (tmp->lrp_uid != perm->rp_uid)
215 if (tmp->lrp_gid != perm->rp_gid)
217 if (tmp->lrp_fsuid != perm->rp_fsuid)
219 if (tmp->lrp_fsgid != perm->rp_fsgid)
222 free_ll_remote_perm(lrp);
228 spin_unlock(&lli->lli_lock);
229 lrp = alloc_ll_remote_perm();
231 CERROR("alloc memory for ll_remote_perm failed!\n");
234 spin_lock(&lli->lli_lock);
238 lrp->lrp_access_perm = perm->rp_access_perm;
240 lrp->lrp_uid = perm->rp_uid;
241 lrp->lrp_gid = perm->rp_gid;
242 lrp->lrp_fsuid = perm->rp_fsuid;
243 lrp->lrp_fsgid = perm->rp_fsgid;
244 hlist_add_head(&lrp->lrp_list, head);
246 lli->lli_rmtperm_time = cfs_time_current();
247 spin_unlock(&lli->lli_lock);
249 CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
250 lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
251 lrp->lrp_access_perm);
256 int lustre_check_remote_perm(struct inode *inode, int mask)
258 struct ll_inode_info *lli = ll_i2info(inode);
259 struct ll_sb_info *sbi = ll_i2sbi(inode);
260 struct ptlrpc_request *req = NULL;
261 struct mdt_remote_perm *perm;
267 save = lli->lli_rmtperm_time;
268 rc = do_check_remote_perm(lli, mask);
269 if (!rc || (rc != -ENOENT && i))
274 mutex_lock(&lli->lli_rmtperm_mutex);
276 if (save != lli->lli_rmtperm_time) {
277 rc = do_check_remote_perm(lli, mask);
278 if (!rc || (rc != -ENOENT && i)) {
279 mutex_unlock(&lli->lli_rmtperm_mutex);
285 CERROR("check remote perm falls in dead loop!\n");
289 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode),
290 ll_i2suppgid(inode), &req);
292 mutex_unlock(&lli->lli_rmtperm_mutex);
296 perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
297 lustre_swab_mdt_remote_perm);
298 if (unlikely(perm == NULL)) {
299 mutex_unlock(&lli->lli_rmtperm_mutex);
304 rc = ll_update_remote_perm(inode, perm);
305 mutex_unlock(&lli->lli_rmtperm_mutex);
309 ptlrpc_req_finished(req);
312 ptlrpc_req_finished(req);