2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* fs/nfs/idmap.c
|
|
|
|
*
|
|
|
|
* UID and GID to name mapping for clients.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 The Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Marius Aamodt Eriksen <marius@umich.edu>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2011-02-23 07:44:31 +08:00
|
|
|
#include <linux/types.h>
|
2012-02-25 03:14:51 +08:00
|
|
|
#include <linux/parser.h>
|
|
|
|
#include <linux/fs.h>
|
2011-06-20 22:08:07 +08:00
|
|
|
#include <linux/nfs_idmap.h>
|
2012-02-25 03:14:51 +08:00
|
|
|
#include <net/net_namespace.h>
|
|
|
|
#include <linux/sunrpc/rpc_pipe_fs.h>
|
2012-01-08 02:22:46 +08:00
|
|
|
#include <linux/nfs_fs.h>
|
2012-01-27 05:54:24 +08:00
|
|
|
#include <linux/nfs_fs_sb.h>
|
2012-02-25 03:14:51 +08:00
|
|
|
#include <linux/key.h>
|
2012-01-27 05:54:24 +08:00
|
|
|
#include <linux/keyctl.h>
|
|
|
|
#include <linux/key-type.h>
|
|
|
|
#include <keys/user-type.h>
|
|
|
|
#include <linux/module.h>
|
2012-02-25 03:14:51 +08:00
|
|
|
|
2012-01-27 05:54:24 +08:00
|
|
|
#include "internal.h"
|
2012-01-26 19:11:41 +08:00
|
|
|
#include "netns.h"
|
2012-01-27 05:54:24 +08:00
|
|
|
|
|
|
|
#define NFS_UINT_MAXLEN 11
|
|
|
|
|
2012-03-12 01:11:00 +08:00
|
|
|
static const struct cred *id_resolver_cache;
|
|
|
|
static struct key_type key_type_id_resolver_legacy;
|
2012-01-27 05:54:24 +08:00
|
|
|
|
2012-06-21 02:35:28 +08:00
|
|
|
struct idmap {
|
|
|
|
struct rpc_pipe *idmap_pipe;
|
|
|
|
struct key_construction *idmap_key_cons;
|
|
|
|
struct mutex idmap_mutex;
|
|
|
|
};
|
2012-01-08 02:22:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
|
|
|
|
* @fattr: fully initialised struct nfs_fattr
|
|
|
|
* @owner_name: owner name string cache
|
|
|
|
* @group_name: group name string cache
|
|
|
|
*/
|
|
|
|
void nfs_fattr_init_names(struct nfs_fattr *fattr,
|
|
|
|
struct nfs4_string *owner_name,
|
|
|
|
struct nfs4_string *group_name)
|
|
|
|
{
|
|
|
|
fattr->owner_name = owner_name;
|
|
|
|
fattr->group_name = group_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
|
|
|
|
{
|
|
|
|
fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
|
|
|
|
kfree(fattr->owner_name->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
|
|
|
|
{
|
|
|
|
fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
|
|
|
|
kfree(fattr->group_name->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
|
|
|
|
{
|
|
|
|
struct nfs4_string *owner = fattr->owner_name;
|
|
|
|
__u32 uid;
|
|
|
|
|
|
|
|
if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
|
|
|
|
return false;
|
|
|
|
if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
|
|
|
|
fattr->uid = uid;
|
|
|
|
fattr->valid |= NFS_ATTR_FATTR_OWNER;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
|
|
|
|
{
|
|
|
|
struct nfs4_string *group = fattr->group_name;
|
|
|
|
__u32 gid;
|
|
|
|
|
|
|
|
if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
|
|
|
|
return false;
|
|
|
|
if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
|
|
|
|
fattr->gid = gid;
|
|
|
|
fattr->valid |= NFS_ATTR_FATTR_GROUP;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nfs_fattr_free_names - free up the NFSv4 owner and group strings
|
|
|
|
* @fattr: a fully initialised nfs_fattr structure
|
|
|
|
*/
|
|
|
|
void nfs_fattr_free_names(struct nfs_fattr *fattr)
|
|
|
|
{
|
|
|
|
if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
|
|
|
|
nfs_fattr_free_owner_name(fattr);
|
|
|
|
if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
|
|
|
|
nfs_fattr_free_group_name(fattr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
|
|
|
|
* @server: pointer to the filesystem nfs_server structure
|
|
|
|
* @fattr: a fully initialised nfs_fattr structure
|
|
|
|
*
|
|
|
|
* This helper maps the cached NFSv4 owner/group strings in fattr into
|
|
|
|
* their numeric uid/gid equivalents, and then frees the cached strings.
|
|
|
|
*/
|
|
|
|
void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
|
|
|
|
{
|
|
|
|
if (nfs_fattr_map_owner_name(server, fattr))
|
|
|
|
nfs_fattr_free_owner_name(fattr);
|
|
|
|
if (nfs_fattr_map_group_name(server, fattr))
|
|
|
|
nfs_fattr_free_group_name(fattr);
|
|
|
|
}
|
2011-02-23 07:44:31 +08:00
|
|
|
|
|
|
|
static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
|
|
|
|
{
|
|
|
|
unsigned long val;
|
|
|
|
char buf[16];
|
|
|
|
|
|
|
|
if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
|
|
|
|
return 0;
|
|
|
|
memcpy(buf, name, namelen);
|
|
|
|
buf[namelen] = '\0';
|
|
|
|
if (strict_strtoul(buf, 0, &val) != 0)
|
|
|
|
return 0;
|
|
|
|
*res = val;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 07:44:31 +08:00
|
|
|
static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
return snprintf(buf, buflen, "%u", id);
|
|
|
|
}
|
|
|
|
|
2012-03-12 01:11:00 +08:00
|
|
|
static struct key_type key_type_id_resolver = {
|
2010-09-30 03:41:49 +08:00
|
|
|
.name = "id_resolver",
|
|
|
|
.instantiate = user_instantiate,
|
|
|
|
.match = user_match,
|
|
|
|
.revoke = user_revoke,
|
|
|
|
.destroy = user_destroy,
|
|
|
|
.describe = user_describe,
|
|
|
|
.read = user_read,
|
|
|
|
};
|
|
|
|
|
2012-01-27 05:54:23 +08:00
|
|
|
static int nfs_idmap_init_keyring(void)
|
2010-09-30 03:41:49 +08:00
|
|
|
{
|
|
|
|
struct cred *cred;
|
|
|
|
struct key *keyring;
|
|
|
|
int ret = 0;
|
|
|
|
|
2012-01-27 02:32:22 +08:00
|
|
|
printk(KERN_NOTICE "NFS: Registering the %s key type\n",
|
|
|
|
key_type_id_resolver.name);
|
2010-09-30 03:41:49 +08:00
|
|
|
|
|
|
|
cred = prepare_kernel_cred(NULL);
|
|
|
|
if (!cred)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
|
|
|
|
(KEY_POS_ALL & ~KEY_POS_SETATTR) |
|
|
|
|
KEY_USR_VIEW | KEY_USR_READ,
|
|
|
|
KEY_ALLOC_NOT_IN_QUOTA);
|
|
|
|
if (IS_ERR(keyring)) {
|
|
|
|
ret = PTR_ERR(keyring);
|
|
|
|
goto failed_put_cred;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
goto failed_put_key;
|
|
|
|
|
|
|
|
ret = register_key_type(&key_type_id_resolver);
|
|
|
|
if (ret < 0)
|
|
|
|
goto failed_put_key;
|
|
|
|
|
2012-07-25 23:53:36 +08:00
|
|
|
ret = register_key_type(&key_type_id_resolver_legacy);
|
|
|
|
if (ret < 0)
|
|
|
|
goto failed_reg_legacy;
|
|
|
|
|
2012-01-18 23:31:45 +08:00
|
|
|
set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
|
2010-09-30 03:41:49 +08:00
|
|
|
cred->thread_keyring = keyring;
|
|
|
|
cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
|
|
|
|
id_resolver_cache = cred;
|
|
|
|
return 0;
|
|
|
|
|
2012-07-25 23:53:36 +08:00
|
|
|
failed_reg_legacy:
|
|
|
|
unregister_key_type(&key_type_id_resolver);
|
2010-09-30 03:41:49 +08:00
|
|
|
failed_put_key:
|
|
|
|
key_put(keyring);
|
|
|
|
failed_put_cred:
|
|
|
|
put_cred(cred);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:54:23 +08:00
|
|
|
static void nfs_idmap_quit_keyring(void)
|
2010-09-30 03:41:49 +08:00
|
|
|
{
|
|
|
|
key_revoke(id_resolver_cache->thread_keyring);
|
|
|
|
unregister_key_type(&key_type_id_resolver);
|
2012-07-25 23:53:36 +08:00
|
|
|
unregister_key_type(&key_type_id_resolver_legacy);
|
2010-09-30 03:41:49 +08:00
|
|
|
put_cred(id_resolver_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assemble the description to pass to request_key()
|
|
|
|
* This function will allocate a new string and update dest to point
|
|
|
|
* at it. The caller is responsible for freeing dest.
|
|
|
|
*
|
|
|
|
* On error 0 is returned. Otherwise, the length of dest is returned.
|
|
|
|
*/
|
|
|
|
static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
|
|
|
|
const char *type, size_t typelen, char **desc)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
size_t desclen = typelen + namelen + 2;
|
|
|
|
|
|
|
|
*desc = kmalloc(desclen, GFP_KERNEL);
|
2010-10-28 14:05:57 +08:00
|
|
|
if (!*desc)
|
2010-09-30 03:41:49 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
cp = *desc;
|
|
|
|
memcpy(cp, type, typelen);
|
|
|
|
cp += typelen;
|
|
|
|
*cp++ = ':';
|
|
|
|
|
|
|
|
memcpy(cp, name, namelen);
|
|
|
|
cp += namelen;
|
|
|
|
*cp = '\0';
|
|
|
|
return desclen;
|
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static ssize_t nfs_idmap_request_key(struct key_type *key_type,
|
|
|
|
const char *name, size_t namelen,
|
|
|
|
const char *type, void *data,
|
|
|
|
size_t data_size, struct idmap *idmap)
|
2010-09-30 03:41:49 +08:00
|
|
|
{
|
|
|
|
const struct cred *saved_cred;
|
|
|
|
struct key *rkey;
|
|
|
|
char *desc;
|
|
|
|
struct user_key_payload *payload;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
saved_cred = override_creds(id_resolver_cache);
|
2012-02-25 03:14:51 +08:00
|
|
|
if (idmap)
|
|
|
|
rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
|
|
|
|
else
|
|
|
|
rkey = request_key(&key_type_id_resolver, desc, "");
|
2010-09-30 03:41:49 +08:00
|
|
|
revert_creds(saved_cred);
|
2012-02-25 03:14:51 +08:00
|
|
|
|
2010-09-30 03:41:49 +08:00
|
|
|
kfree(desc);
|
|
|
|
if (IS_ERR(rkey)) {
|
|
|
|
ret = PTR_ERR(rkey);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
rkey->perm |= KEY_USR_VIEW;
|
|
|
|
|
|
|
|
ret = key_validate(rkey);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_up;
|
|
|
|
|
|
|
|
payload = rcu_dereference(rkey->payload.data);
|
|
|
|
if (IS_ERR_OR_NULL(payload)) {
|
|
|
|
ret = PTR_ERR(payload);
|
|
|
|
goto out_up;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = payload->datalen;
|
|
|
|
if (ret > 0 && ret <= data_size)
|
|
|
|
memcpy(data, payload->data, ret);
|
|
|
|
else
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
|
|
|
out_up:
|
|
|
|
rcu_read_unlock();
|
|
|
|
key_put(rkey);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
|
|
|
|
const char *type, void *data,
|
|
|
|
size_t data_size, struct idmap *idmap)
|
|
|
|
{
|
|
|
|
ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
|
|
|
|
name, namelen, type, data,
|
|
|
|
data_size, NULL);
|
|
|
|
if (ret < 0) {
|
2012-06-21 02:35:28 +08:00
|
|
|
mutex_lock(&idmap->idmap_mutex);
|
2012-02-25 03:14:51 +08:00
|
|
|
ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
|
|
|
|
name, namelen, type, data,
|
|
|
|
data_size, idmap);
|
2012-06-21 02:35:28 +08:00
|
|
|
mutex_unlock(&idmap->idmap_mutex);
|
2012-02-25 03:14:51 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2010-09-30 03:41:49 +08:00
|
|
|
|
|
|
|
/* ID -> Name */
|
2012-02-25 03:14:51 +08:00
|
|
|
static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
|
|
|
|
size_t buflen, struct idmap *idmap)
|
2010-09-30 03:41:49 +08:00
|
|
|
{
|
|
|
|
char id_str[NFS_UINT_MAXLEN];
|
|
|
|
int id_len;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
id_len = snprintf(id_str, sizeof(id_str), "%u", id);
|
2012-02-25 03:14:51 +08:00
|
|
|
ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
|
2010-09-30 03:41:49 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Name -> ID */
|
2012-02-25 03:14:51 +08:00
|
|
|
static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
|
|
|
|
__u32 *id, struct idmap *idmap)
|
2010-09-30 03:41:49 +08:00
|
|
|
{
|
|
|
|
char id_str[NFS_UINT_MAXLEN];
|
|
|
|
long id_long;
|
|
|
|
ssize_t data_size;
|
|
|
|
int ret = 0;
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
|
2010-09-30 03:41:49 +08:00
|
|
|
if (data_size <= 0) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
} else {
|
|
|
|
ret = strict_strtol(id_str, 10, &id_long);
|
|
|
|
*id = (__u32)id_long;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:54:23 +08:00
|
|
|
/* idmap classic begins here */
|
2011-02-23 07:44:31 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
enum {
|
|
|
|
Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static const match_table_t nfs_idmap_tokens = {
|
|
|
|
{ Opt_find_uid, "uid:%s" },
|
|
|
|
{ Opt_find_gid, "gid:%s" },
|
|
|
|
{ Opt_find_user, "user:%s" },
|
|
|
|
{ Opt_find_group, "group:%s" },
|
|
|
|
{ Opt_find_err, NULL }
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
|
2007-12-21 03:54:35 +08:00
|
|
|
static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
|
|
|
|
size_t);
|
|
|
|
static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-10 03:14:15 +08:00
|
|
|
static const struct rpc_pipe_ops idmap_upcall_ops = {
|
2011-09-23 09:50:10 +08:00
|
|
|
.upcall = rpc_pipe_generic_upcall,
|
2007-12-21 03:54:35 +08:00
|
|
|
.downcall = idmap_pipe_downcall,
|
|
|
|
.destroy_msg = idmap_pipe_destroy_msg,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2012-03-12 01:11:00 +08:00
|
|
|
static struct key_type key_type_id_resolver_legacy = {
|
2012-07-25 23:53:36 +08:00
|
|
|
.name = "id_legacy",
|
2012-02-25 03:14:51 +08:00
|
|
|
.instantiate = user_instantiate,
|
|
|
|
.match = user_match,
|
|
|
|
.revoke = user_revoke,
|
|
|
|
.destroy = user_destroy,
|
|
|
|
.describe = user_describe,
|
|
|
|
.read = user_read,
|
|
|
|
.request_key = nfs_idmap_legacy_upcall,
|
|
|
|
};
|
|
|
|
|
2012-01-10 20:13:11 +08:00
|
|
|
static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
|
|
|
|
{
|
|
|
|
if (pipe->dentry)
|
|
|
|
rpc_unlink(pipe->dentry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __nfs_idmap_register(struct dentry *dir,
|
|
|
|
struct idmap *idmap,
|
|
|
|
struct rpc_pipe *pipe)
|
|
|
|
{
|
|
|
|
struct dentry *dentry;
|
|
|
|
|
|
|
|
dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
|
|
|
|
if (IS_ERR(dentry))
|
|
|
|
return PTR_ERR(dentry);
|
|
|
|
pipe->dentry = dentry;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nfs_idmap_unregister(struct nfs_client *clp,
|
|
|
|
struct rpc_pipe *pipe)
|
|
|
|
{
|
2012-05-22 10:44:50 +08:00
|
|
|
struct net *net = clp->cl_net;
|
2012-01-10 20:13:11 +08:00
|
|
|
struct super_block *pipefs_sb;
|
|
|
|
|
|
|
|
pipefs_sb = rpc_get_sb_net(net);
|
|
|
|
if (pipefs_sb) {
|
|
|
|
__nfs_idmap_unregister(pipe);
|
|
|
|
rpc_put_sb_net(net);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nfs_idmap_register(struct nfs_client *clp,
|
|
|
|
struct idmap *idmap,
|
|
|
|
struct rpc_pipe *pipe)
|
|
|
|
{
|
2012-05-22 10:44:50 +08:00
|
|
|
struct net *net = clp->cl_net;
|
2012-01-10 20:13:11 +08:00
|
|
|
struct super_block *pipefs_sb;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
pipefs_sb = rpc_get_sb_net(net);
|
|
|
|
if (pipefs_sb) {
|
|
|
|
if (clp->cl_rpcclient->cl_dentry)
|
|
|
|
err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
|
|
|
|
idmap, pipe);
|
|
|
|
rpc_put_sb_net(net);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-08-23 08:06:09 +08:00
|
|
|
int
|
2006-08-23 08:06:08 +08:00
|
|
|
nfs_idmap_new(struct nfs_client *clp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct idmap *idmap;
|
2011-12-26 20:44:06 +08:00
|
|
|
struct rpc_pipe *pipe;
|
2006-08-23 08:06:09 +08:00
|
|
|
int error;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
NFS: Share NFS superblocks per-protocol per-server per-FSID
The attached patch makes NFS share superblocks between mounts from the same
server and FSID over the same protocol.
It does this by creating each superblock with a false root and returning the
real root dentry in the vfsmount presented by get_sb(). The root dentry set
starts off as an anonymous dentry if we don't already have the dentry for its
inode, otherwise it simply returns the dentry we already have.
We may thus end up with several trees of dentries in the superblock, and if at
some later point one of anonymous tree roots is discovered by normal filesystem
activity to be located in another tree within the superblock, the anonymous
root is named and materialises attached to the second tree at the appropriate
point.
Why do it this way? Why not pass an extra argument to the mount() syscall to
indicate the subpath and then pathwalk from the server root to the desired
directory? You can't guarantee this will work for two reasons:
(1) The root and intervening nodes may not be accessible to the client.
With NFS2 and NFS3, for instance, mountd is called on the server to get
the filehandle for the tip of a path. mountd won't give us handles for
anything we don't have permission to access, and so we can't set up NFS
inodes for such nodes, and so can't easily set up dentries (we'd have to
have ghost inodes or something).
With this patch we don't actually create dentries until we get handles
from the server that we can use to set up their inodes, and we don't
actually bind them into the tree until we know for sure where they go.
(2) Inaccessible symbolic links.
If we're asked to mount two exports from the server, eg:
mount warthog:/warthog/aaa/xxx /mmm
mount warthog:/warthog/bbb/yyy /nnn
We may not be able to access anything nearer the root than xxx and yyy,
but we may find out later that /mmm/www/yyy, say, is actually the same
directory as the one mounted on /nnn. What we might then find out, for
example, is that /warthog/bbb was actually a symbolic link to
/warthog/aaa/xxx/www, but we can't actually determine that by talking to
the server until /warthog is made available by NFS.
This would lead to having constructed an errneous dentry tree which we
can't easily fix. We can end up with a dentry marked as a directory when
it should actually be a symlink, or we could end up with an apparently
hardlinked directory.
With this patch we need not make assumptions about the type of a dentry
for which we can't retrieve information, nor need we assume we know its
place in the grand scheme of things until we actually see that place.
This patch reduces the possibility of aliasing in the inode and page caches for
inodes that may be accessed by more than one NFS export. It also reduces the
number of superblocks required for NFS where there are many NFS exports being
used from a server (home directory server + autofs for example).
This in turn makes it simpler to do local caching of network filesystems, as it
can then be guaranteed that there won't be links from multiple inodes in
separate superblocks to the same cache file.
Obviously, cache aliasing between different levels of NFS protocol could still
be a problem, but at least that gives us another key to use when indexing the
cache.
This patch makes the following changes:
(1) The server record construction/destruction has been abstracted out into
its own set of functions to make things easier to get right. These have
been moved into fs/nfs/client.c.
All the code in fs/nfs/client.c has to do with the management of
connections to servers, and doesn't touch superblocks in any way; the
remaining code in fs/nfs/super.c has to do with VFS superblock management.
(2) The sequence of events undertaken by NFS mount is now reordered:
(a) A volume representation (struct nfs_server) is allocated.
(b) A server representation (struct nfs_client) is acquired. This may be
allocated or shared, and is keyed on server address, port and NFS
version.
(c) If allocated, the client representation is initialised. The state
member variable of nfs_client is used to prevent a race during
initialisation from two mounts.
(d) For NFS4 a simple pathwalk is performed, walking from FH to FH to find
the root filehandle for the mount (fs/nfs/getroot.c). For NFS2/3 we
are given the root FH in advance.
(e) The volume FSID is probed for on the root FH.
(f) The volume representation is initialised from the FSINFO record
retrieved on the root FH.
(g) sget() is called to acquire a superblock. This may be allocated or
shared, keyed on client pointer and FSID.
(h) If allocated, the superblock is initialised.
(i) If the superblock is shared, then the new nfs_server record is
discarded.
(j) The root dentry for this mount is looked up from the root FH.
(k) The root dentry for this mount is assigned to the vfsmount.
(3) nfs_readdir_lookup() creates dentries for each of the entries readdir()
returns; this function now attaches disconnected trees from alternate
roots that happen to be discovered attached to a directory being read (in
the same way nfs_lookup() is made to do for lookup ops).
The new d_materialise_unique() function is now used to do this, thus
permitting the whole thing to be done under one set of locks, and thus
avoiding any race between mount and lookup operations on the same
directory.
(4) The client management code uses a new debug facility: NFSDBG_CLIENT which
is set by echoing 1024 to /proc/net/sunrpc/nfs_debug.
(5) Clone mounts are now called xdev mounts.
(6) Use the dentry passed to the statfs() op as the handle for retrieving fs
statistics rather than the root dentry of the superblock (which is now a
dummy).
Signed-Off-By: David Howells <dhowells@redhat.com>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
2006-08-23 08:06:13 +08:00
|
|
|
BUG_ON(clp->cl_idmap != NULL);
|
2006-08-23 08:06:09 +08:00
|
|
|
|
2007-12-21 03:54:35 +08:00
|
|
|
idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
|
|
|
|
if (idmap == NULL)
|
|
|
|
return -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-12-26 20:44:06 +08:00
|
|
|
pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
|
|
|
|
if (IS_ERR(pipe)) {
|
|
|
|
error = PTR_ERR(pipe);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(idmap);
|
2006-08-23 08:06:09 +08:00
|
|
|
return error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2012-01-10 20:13:11 +08:00
|
|
|
error = nfs_idmap_register(clp, idmap, pipe);
|
|
|
|
if (error) {
|
2011-12-26 20:44:06 +08:00
|
|
|
rpc_destroy_pipe_data(pipe);
|
|
|
|
kfree(idmap);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
idmap->idmap_pipe = pipe;
|
2012-06-21 02:35:28 +08:00
|
|
|
mutex_init(&idmap->idmap_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
clp->cl_idmap = idmap;
|
2006-08-23 08:06:09 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-23 08:06:08 +08:00
|
|
|
nfs_idmap_delete(struct nfs_client *clp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct idmap *idmap = clp->cl_idmap;
|
|
|
|
|
|
|
|
if (!idmap)
|
|
|
|
return;
|
2012-01-10 20:13:11 +08:00
|
|
|
nfs_idmap_unregister(clp, idmap->idmap_pipe);
|
2011-12-26 20:44:06 +08:00
|
|
|
rpc_destroy_pipe_data(idmap->idmap_pipe);
|
2005-04-17 06:20:36 +08:00
|
|
|
clp->cl_idmap = NULL;
|
|
|
|
kfree(idmap);
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:13:19 +08:00
|
|
|
static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
|
|
|
|
struct super_block *sb)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-10 20:13:19 +08:00
|
|
|
int err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-10 20:13:19 +08:00
|
|
|
switch (event) {
|
|
|
|
case RPC_PIPEFS_MOUNT:
|
|
|
|
BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
|
|
|
|
err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
|
|
|
|
clp->cl_idmap,
|
|
|
|
clp->cl_idmap->idmap_pipe);
|
|
|
|
break;
|
|
|
|
case RPC_PIPEFS_UMOUNT:
|
|
|
|
if (clp->cl_idmap->idmap_pipe) {
|
|
|
|
struct dentry *parent;
|
|
|
|
|
|
|
|
parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
|
|
|
|
__nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
|
|
|
|
/*
|
|
|
|
* Note: This is a dirty hack. SUNRPC hook has been
|
|
|
|
* called already but simple_rmdir() call for the
|
|
|
|
* directory returned with error because of idmap pipe
|
|
|
|
* inside. Thus now we have to remove this directory
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
if (rpc_rmdir(parent))
|
2012-01-27 02:32:23 +08:00
|
|
|
printk(KERN_ERR "NFS: %s: failed to remove "
|
|
|
|
"clnt dir!\n", __func__);
|
2012-01-10 20:13:19 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-01-27 02:32:23 +08:00
|
|
|
printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
|
|
|
|
event);
|
2012-01-10 20:13:19 +08:00
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-02-28 02:05:37 +08:00
|
|
|
static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
|
2012-01-10 20:13:19 +08:00
|
|
|
{
|
2012-02-28 02:05:37 +08:00
|
|
|
struct nfs_net *nn = net_generic(net, nfs_net_id);
|
|
|
|
struct dentry *cl_dentry;
|
2012-01-10 20:13:19 +08:00
|
|
|
struct nfs_client *clp;
|
2012-05-24 01:24:36 +08:00
|
|
|
int err;
|
2012-01-10 20:13:19 +08:00
|
|
|
|
2012-05-24 01:24:36 +08:00
|
|
|
restart:
|
2012-01-24 01:26:31 +08:00
|
|
|
spin_lock(&nn->nfs_client_lock);
|
2012-01-24 01:26:05 +08:00
|
|
|
list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
|
2012-05-24 01:24:36 +08:00
|
|
|
/* Wait for initialisation to finish */
|
|
|
|
if (clp->cl_cons_state == NFS_CS_INITING) {
|
|
|
|
atomic_inc(&clp->cl_count);
|
|
|
|
spin_unlock(&nn->nfs_client_lock);
|
|
|
|
err = nfs_wait_client_init_complete(clp);
|
|
|
|
nfs_put_client(clp);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
/* Skip nfs_clients that failed to initialise */
|
|
|
|
if (clp->cl_cons_state < 0)
|
|
|
|
continue;
|
2012-05-24 01:26:10 +08:00
|
|
|
smp_rmb();
|
2012-01-10 20:13:19 +08:00
|
|
|
if (clp->rpc_ops != &nfs_v4_clientops)
|
|
|
|
continue;
|
2012-02-28 02:05:37 +08:00
|
|
|
cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
|
|
|
|
if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
|
|
|
|
((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
|
|
|
|
continue;
|
|
|
|
atomic_inc(&clp->cl_count);
|
|
|
|
spin_unlock(&nn->nfs_client_lock);
|
|
|
|
return clp;
|
|
|
|
}
|
|
|
|
spin_unlock(&nn->nfs_client_lock);
|
|
|
|
return NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 02:05:37 +08:00
|
|
|
static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
|
|
|
|
void *ptr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-02-28 02:05:37 +08:00
|
|
|
struct super_block *sb = ptr;
|
|
|
|
struct nfs_client *clp;
|
|
|
|
int error = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-04-28 23:32:21 +08:00
|
|
|
if (!try_module_get(THIS_MODULE))
|
|
|
|
return 0;
|
|
|
|
|
2012-02-28 02:05:37 +08:00
|
|
|
while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
|
2012-01-10 20:13:19 +08:00
|
|
|
error = __rpc_pipefs_event(clp, event, sb);
|
2012-02-28 02:05:37 +08:00
|
|
|
nfs_put_client(clp);
|
2012-01-10 20:13:19 +08:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2012-04-28 23:32:21 +08:00
|
|
|
module_put(THIS_MODULE);
|
2012-01-10 20:13:19 +08:00
|
|
|
return error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:13:19 +08:00
|
|
|
#define PIPEFS_NFS_PRIO 1
|
|
|
|
|
|
|
|
static struct notifier_block nfs_idmap_block = {
|
|
|
|
.notifier_call = rpc_pipefs_event,
|
|
|
|
.priority = SUNRPC_PIPEFS_NFS_PRIO,
|
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-10 20:13:19 +08:00
|
|
|
int nfs_idmap_init(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-27 05:54:23 +08:00
|
|
|
int ret;
|
|
|
|
ret = nfs_idmap_init_keyring();
|
|
|
|
if (ret != 0)
|
|
|
|
goto out;
|
|
|
|
ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
|
|
|
|
if (ret != 0)
|
|
|
|
nfs_idmap_quit_keyring();
|
|
|
|
out:
|
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:13:19 +08:00
|
|
|
void nfs_idmap_quit(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-10 20:13:19 +08:00
|
|
|
rpc_pipefs_notifier_unregister(&nfs_idmap_block);
|
2012-01-27 05:54:23 +08:00
|
|
|
nfs_idmap_quit_keyring();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static int nfs_idmap_prepare_message(char *desc, struct idmap_msg *im,
|
|
|
|
struct rpc_pipe_msg *msg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-02-25 03:14:51 +08:00
|
|
|
substring_t substr;
|
|
|
|
int token, ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
memset(im, 0, sizeof(*im));
|
|
|
|
memset(msg, 0, sizeof(*msg));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
im->im_type = IDMAP_TYPE_GROUP;
|
|
|
|
token = match_token(desc, nfs_idmap_tokens, &substr);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
switch (token) {
|
|
|
|
case Opt_find_uid:
|
|
|
|
im->im_type = IDMAP_TYPE_USER;
|
|
|
|
case Opt_find_gid:
|
|
|
|
im->im_conv = IDMAP_CONV_NAMETOID;
|
|
|
|
ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
case Opt_find_user:
|
|
|
|
im->im_type = IDMAP_TYPE_USER;
|
|
|
|
case Opt_find_group:
|
|
|
|
im->im_conv = IDMAP_CONV_IDTONAME;
|
|
|
|
ret = match_int(&substr, &im->im_id);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
msg->data = im;
|
|
|
|
msg->len = sizeof(struct idmap_msg);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
out:
|
2007-12-21 03:54:35 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static int nfs_idmap_legacy_upcall(struct key_construction *cons,
|
|
|
|
const char *op,
|
|
|
|
void *aux)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-02-25 03:14:51 +08:00
|
|
|
struct rpc_pipe_msg *msg;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct idmap_msg *im;
|
2012-02-25 03:14:51 +08:00
|
|
|
struct idmap *idmap = (struct idmap *)aux;
|
|
|
|
struct key *key = cons->key;
|
2012-05-15 03:45:28 +08:00
|
|
|
int ret = -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
/* msg and im are freed in idmap_pipe_destroy_msg */
|
|
|
|
msg = kmalloc(sizeof(*msg), GFP_KERNEL);
|
2012-05-15 03:45:28 +08:00
|
|
|
if (!msg)
|
2012-02-25 03:14:51 +08:00
|
|
|
goto out0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
im = kmalloc(sizeof(*im), GFP_KERNEL);
|
2012-05-15 03:45:28 +08:00
|
|
|
if (!im)
|
2012-02-25 03:14:51 +08:00
|
|
|
goto out1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
ret = nfs_idmap_prepare_message(key->description, im, msg);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out2;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-07-25 23:53:36 +08:00
|
|
|
BUG_ON(idmap->idmap_key_cons != NULL);
|
2012-02-25 03:14:51 +08:00
|
|
|
idmap->idmap_key_cons = cons;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-03-12 23:33:00 +08:00
|
|
|
ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out2;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-03-12 23:33:00 +08:00
|
|
|
return ret;
|
2012-02-25 03:14:51 +08:00
|
|
|
|
|
|
|
out2:
|
|
|
|
kfree(im);
|
|
|
|
out1:
|
|
|
|
kfree(msg);
|
|
|
|
out0:
|
2012-07-25 23:53:36 +08:00
|
|
|
complete_request_key(cons, ret);
|
2007-12-21 03:54:35 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-02-25 03:14:51 +08:00
|
|
|
return key_instantiate_and_link(key, data, strlen(data) + 1,
|
|
|
|
id_resolver_cache->thread_keyring,
|
|
|
|
authkey);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey)
|
|
|
|
{
|
|
|
|
char id_str[NFS_UINT_MAXLEN];
|
|
|
|
int ret = -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
switch (im->im_conv) {
|
|
|
|
case IDMAP_CONV_NAMETOID:
|
|
|
|
sprintf(id_str, "%d", im->im_id);
|
|
|
|
ret = nfs_idmap_instantiate(key, authkey, id_str);
|
|
|
|
break;
|
|
|
|
case IDMAP_CONV_IDTONAME:
|
|
|
|
ret = nfs_idmap_instantiate(key, authkey, im->im_name);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
|
|
|
|
{
|
2007-12-21 03:54:35 +08:00
|
|
|
struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct idmap *idmap = (struct idmap *)rpci->private;
|
2012-07-25 23:53:36 +08:00
|
|
|
struct key_construction *cons;
|
2012-02-25 03:14:51 +08:00
|
|
|
struct idmap_msg im;
|
2007-12-21 03:54:49 +08:00
|
|
|
size_t namelen_in;
|
2005-04-17 06:20:36 +08:00
|
|
|
int ret;
|
|
|
|
|
2012-07-25 23:53:36 +08:00
|
|
|
/* If instantiation is successful, anyone waiting for key construction
|
|
|
|
* will have been woken up and someone else may now have used
|
|
|
|
* idmap_key_cons - so after this point we may no longer touch it.
|
|
|
|
*/
|
|
|
|
cons = ACCESS_ONCE(idmap->idmap_key_cons);
|
|
|
|
idmap->idmap_key_cons = NULL;
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
if (mlen != sizeof(im)) {
|
|
|
|
ret = -ENOSPC;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
if (copy_from_user(&im, src, mlen) != 0) {
|
|
|
|
ret = -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
2012-02-25 03:14:51 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
|
|
|
|
ret = mlen;
|
2012-07-25 23:53:36 +08:00
|
|
|
complete_request_key(cons, -ENOKEY);
|
2012-02-25 03:14:51 +08:00
|
|
|
goto out_incomplete;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
|
|
|
|
if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
|
|
|
|
ret = -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-02-25 03:14:51 +08:00
|
|
|
ret = nfs_idmap_read_message(&im, cons->key, cons->authkey);
|
|
|
|
if (ret >= 0) {
|
|
|
|
key_set_timeout(cons->key, nfs_idmap_cache_timeout);
|
|
|
|
ret = mlen;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
out:
|
2012-07-25 23:53:36 +08:00
|
|
|
complete_request_key(cons, ret);
|
2012-02-25 03:14:51 +08:00
|
|
|
out_incomplete:
|
2005-04-17 06:20:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-05-06 07:16:09 +08:00
|
|
|
static void
|
2005-04-17 06:20:36 +08:00
|
|
|
idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
|
|
|
|
{
|
2012-02-25 03:14:51 +08:00
|
|
|
/* Free memory allocated in nfs_idmap_legacy_upcall() */
|
|
|
|
kfree(msg->data);
|
|
|
|
kfree(msg);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 07:44:31 +08:00
|
|
|
int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-02-23 07:44:31 +08:00
|
|
|
struct idmap *idmap = server->nfs_client->cl_idmap;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 07:44:31 +08:00
|
|
|
if (nfs_map_string_to_numeric(name, namelen, uid))
|
|
|
|
return 0;
|
2012-02-25 03:14:51 +08:00
|
|
|
return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:54:23 +08:00
|
|
|
int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-02-23 07:44:31 +08:00
|
|
|
struct idmap *idmap = server->nfs_client->cl_idmap;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-27 05:54:23 +08:00
|
|
|
if (nfs_map_string_to_numeric(name, namelen, gid))
|
2011-02-23 07:44:31 +08:00
|
|
|
return 0;
|
2012-02-25 03:14:51 +08:00
|
|
|
return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 07:44:31 +08:00
|
|
|
int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-02-23 07:44:31 +08:00
|
|
|
struct idmap *idmap = server->nfs_client->cl_idmap;
|
2011-02-23 07:44:32 +08:00
|
|
|
int ret = -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 07:44:32 +08:00
|
|
|
if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
|
2012-02-25 03:14:51 +08:00
|
|
|
ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
|
2011-02-23 07:44:31 +08:00
|
|
|
if (ret < 0)
|
|
|
|
ret = nfs_map_numeric_to_string(uid, buf, buflen);
|
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2012-01-27 05:54:23 +08:00
|
|
|
int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-02-23 07:44:31 +08:00
|
|
|
struct idmap *idmap = server->nfs_client->cl_idmap;
|
2011-02-23 07:44:32 +08:00
|
|
|
int ret = -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-23 07:44:32 +08:00
|
|
|
if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
|
2012-02-25 03:14:51 +08:00
|
|
|
ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
|
2011-02-23 07:44:31 +08:00
|
|
|
if (ret < 0)
|
2012-01-27 05:54:23 +08:00
|
|
|
ret = nfs_map_numeric_to_string(gid, buf, buflen);
|
2011-02-23 07:44:31 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|