Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | /*
* GPL HEADER START
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 only,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License version 2 for more details (a copy is included
* in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; If not, see
* http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
* GPL HEADER END
*/
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
* Copyright (c) 2011, 2012, Intel Corporation.
*/
/*
* This file is part of Lustre, http://www.lustre.org/
* Lustre is a trademark of Sun Microsystems, Inc.
*
* lustre/llite/remote_perm.c
*
* Lustre Permission Cache for Remote Client
*
* Author: Lai Siyao <lsy@clusterfs.com>
* Author: Fan Yong <fanyong@clusterfs.com>
*/
#define DEBUG_SUBSYSTEM S_LLITE
#include <linux/module.h>
#include <linux/types.h>
#include "../include/lustre_lite.h"
#include "../include/lustre_ha.h"
#include "../include/lustre_dlm.h"
#include "../include/lprocfs_status.h"
#include "../include/lustre_disk.h"
#include "../include/lustre_param.h"
#include "llite_internal.h"
struct kmem_cache *ll_remote_perm_cachep = NULL;
struct kmem_cache *ll_rmtperm_hash_cachep = NULL;
static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
{
struct ll_remote_perm *lrp;
OBD_SLAB_ALLOC_PTR_GFP(lrp, ll_remote_perm_cachep, GFP_KERNEL);
if (lrp)
INIT_HLIST_NODE(&lrp->lrp_list);
return lrp;
}
static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
{
if (!lrp)
return;
if (!hlist_unhashed(&lrp->lrp_list))
hlist_del(&lrp->lrp_list);
OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
}
static struct hlist_head *alloc_rmtperm_hash(void)
{
struct hlist_head *hash;
int i;
OBD_SLAB_ALLOC_GFP(hash, ll_rmtperm_hash_cachep,
REMOTE_PERM_HASHSIZE * sizeof(*hash),
GFP_IOFS);
if (!hash)
return NULL;
for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
INIT_HLIST_HEAD(hash + i);
return hash;
}
void free_rmtperm_hash(struct hlist_head *hash)
{
int i;
struct ll_remote_perm *lrp;
struct hlist_node *next;
if (!hash)
return;
for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
hlist_for_each_entry_safe(lrp, next, hash + i,
lrp_list)
free_ll_remote_perm(lrp);
OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
REMOTE_PERM_HASHSIZE * sizeof(*hash));
}
static inline int remote_perm_hashfunc(uid_t uid)
{
return uid & (REMOTE_PERM_HASHSIZE - 1);
}
/* NB: setxid permission is not checked here, instead it's done on
* MDT when client get remote permission. */
static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
{
struct hlist_head *head;
struct ll_remote_perm *lrp;
int found = 0, rc;
if (!lli->lli_remote_perms)
return -ENOENT;
head = lli->lli_remote_perms +
remote_perm_hashfunc(from_kuid(&init_user_ns, current_uid()));
spin_lock(&lli->lli_lock);
hlist_for_each_entry(lrp, head, lrp_list) {
if (lrp->lrp_uid != from_kuid(&init_user_ns, current_uid()))
continue;
if (lrp->lrp_gid != from_kgid(&init_user_ns, current_gid()))
continue;
if (lrp->lrp_fsuid != from_kuid(&init_user_ns, current_fsuid()))
continue;
if (lrp->lrp_fsgid != from_kgid(&init_user_ns, current_fsgid()))
continue;
found = 1;
break;
}
if (!found) {
rc = -ENOENT;
goto out;
}
CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
lrp->lrp_access_perm);
rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
out:
spin_unlock(&lli->lli_lock);
return rc;
}
int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
{
struct ll_inode_info *lli = ll_i2info(inode);
struct ll_remote_perm *lrp = NULL, *tmp = NULL;
struct hlist_head *head, *perm_hash = NULL;
LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
#if 0
if (perm->rp_uid != current->uid ||
perm->rp_gid != current->gid ||
perm->rp_fsuid != current->fsuid ||
perm->rp_fsgid != current->fsgid) {
/* user might setxid in this small period */
CDEBUG(D_SEC,
"remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
perm->rp_fsgid, current->uid, current->gid,
current->fsuid, current->fsgid);
return -EAGAIN;
}
#endif
if (!lli->lli_remote_perms) {
perm_hash = alloc_rmtperm_hash();
if (perm_hash == NULL) {
CERROR("alloc lli_remote_perms failed!\n");
return -ENOMEM;
}
}
spin_lock(&lli->lli_lock);
if (!lli->lli_remote_perms)
lli->lli_remote_perms = perm_hash;
else if (perm_hash)
free_rmtperm_hash(perm_hash);
head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
again:
hlist_for_each_entry(tmp, head, lrp_list) {
if (tmp->lrp_uid != perm->rp_uid)
continue;
if (tmp->lrp_gid != perm->rp_gid)
continue;
if (tmp->lrp_fsuid != perm->rp_fsuid)
continue;
if (tmp->lrp_fsgid != perm->rp_fsgid)
continue;
if (lrp)
free_ll_remote_perm(lrp);
lrp = tmp;
break;
}
if (!lrp) {
spin_unlock(&lli->lli_lock);
lrp = alloc_ll_remote_perm();
if (!lrp) {
CERROR("alloc memory for ll_remote_perm failed!\n");
return -ENOMEM;
}
spin_lock(&lli->lli_lock);
goto again;
}
lrp->lrp_access_perm = perm->rp_access_perm;
if (lrp != tmp) {
lrp->lrp_uid = perm->rp_uid;
lrp->lrp_gid = perm->rp_gid;
lrp->lrp_fsuid = perm->rp_fsuid;
lrp->lrp_fsgid = perm->rp_fsgid;
hlist_add_head(&lrp->lrp_list, head);
}
lli->lli_rmtperm_time = cfs_time_current();
spin_unlock(&lli->lli_lock);
CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
lrp->lrp_access_perm);
return 0;
}
int lustre_check_remote_perm(struct inode *inode, int mask)
{
struct ll_inode_info *lli = ll_i2info(inode);
struct ll_sb_info *sbi = ll_i2sbi(inode);
struct ptlrpc_request *req = NULL;
struct mdt_remote_perm *perm;
struct obd_capa *oc;
unsigned long save;
int i = 0, rc;
do {
save = lli->lli_rmtperm_time;
rc = do_check_remote_perm(lli, mask);
if (!rc || (rc != -ENOENT && i))
break;
might_sleep();
mutex_lock(&lli->lli_rmtperm_mutex);
/* check again */
if (save != lli->lli_rmtperm_time) {
rc = do_check_remote_perm(lli, mask);
if (!rc || (rc != -ENOENT && i)) {
mutex_unlock(&lli->lli_rmtperm_mutex);
break;
}
}
if (i++ > 5) {
CERROR("check remote perm falls in dead loop!\n");
LBUG();
}
oc = ll_mdscapa_get(inode);
rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc,
ll_i2suppgid(inode), &req);
capa_put(oc);
if (rc) {
mutex_unlock(&lli->lli_rmtperm_mutex);
break;
}
perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
lustre_swab_mdt_remote_perm);
if (unlikely(perm == NULL)) {
mutex_unlock(&lli->lli_rmtperm_mutex);
rc = -EPROTO;
break;
}
rc = ll_update_remote_perm(inode, perm);
mutex_unlock(&lli->lli_rmtperm_mutex);
if (rc == -ENOMEM)
break;
ptlrpc_req_finished(req);
req = NULL;
} while (1);
ptlrpc_req_finished(req);
return rc;
}
#if 0 /* NB: remote perms can't be freed in ll_mdc_blocking_ast of UPDATE lock,
* because it will fail sanity test 48.
*/
void ll_free_remote_perms(struct inode *inode)
{
struct ll_inode_info *lli = ll_i2info(inode);
struct hlist_head *hash = lli->lli_remote_perms;
struct ll_remote_perm *lrp;
struct hlist_node *node, *next;
int i;
LASSERT(hash);
spin_lock(&lli->lli_lock);
for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
hlist_for_each_entry_safe(lrp, node, next, hash + i,
lrp_list)
free_ll_remote_perm(lrp);
}
spin_unlock(&lli->lli_lock);
}
#endif
|