2005-04-17 02:20:36 +04: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.
|
|
|
|
*/
|
|
|
|
|
2010-09-29 23:41:49 +04:00
|
|
|
#ifdef CONFIG_NFS_USE_NEW_IDMAPPER
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/cred.h>
|
|
|
|
#include <linux/nfs_idmap.h>
|
|
|
|
#include <linux/keyctl.h>
|
|
|
|
#include <linux/key-type.h>
|
|
|
|
#include <linux/rcupdate.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
|
|
|
|
#include <keys/user-type.h>
|
|
|
|
|
|
|
|
#define NFS_UINT_MAXLEN 11
|
|
|
|
|
|
|
|
const struct cred *id_resolver_cache;
|
|
|
|
|
|
|
|
struct key_type key_type_id_resolver = {
|
|
|
|
.name = "id_resolver",
|
|
|
|
.instantiate = user_instantiate,
|
|
|
|
.match = user_match,
|
|
|
|
.revoke = user_revoke,
|
|
|
|
.destroy = user_destroy,
|
|
|
|
.describe = user_describe,
|
|
|
|
.read = user_read,
|
|
|
|
};
|
|
|
|
|
|
|
|
int nfs_idmap_init(void)
|
|
|
|
{
|
|
|
|
struct cred *cred;
|
|
|
|
struct key *keyring;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
cred->thread_keyring = keyring;
|
|
|
|
cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
|
|
|
|
id_resolver_cache = cred;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failed_put_key:
|
|
|
|
key_put(keyring);
|
|
|
|
failed_put_cred:
|
|
|
|
put_cred(cred);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfs_idmap_quit(void)
|
|
|
|
{
|
|
|
|
key_revoke(id_resolver_cache->thread_keyring);
|
|
|
|
unregister_key_type(&key_type_id_resolver);
|
|
|
|
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 10:05:57 +04:00
|
|
|
if (!*desc)
|
2010-09-29 23:41:49 +04:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
cp = *desc;
|
|
|
|
memcpy(cp, type, typelen);
|
|
|
|
cp += typelen;
|
|
|
|
*cp++ = ':';
|
|
|
|
|
|
|
|
memcpy(cp, name, namelen);
|
|
|
|
cp += namelen;
|
|
|
|
*cp = '\0';
|
|
|
|
return desclen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
|
|
|
|
const char *type, void *data, size_t data_size)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
rkey = request_key(&key_type_id_resolver, desc, "");
|
|
|
|
revert_creds(saved_cred);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ID -> Name */
|
|
|
|
static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
char id_str[NFS_UINT_MAXLEN];
|
|
|
|
int id_len;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
id_len = snprintf(id_str, sizeof(id_str), "%u", id);
|
|
|
|
ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
|
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Name -> ID */
|
|
|
|
static int nfs_idmap_lookup_id(const char *name, size_t namelen,
|
|
|
|
const char *type, __u32 *id)
|
|
|
|
{
|
|
|
|
char id_str[NFS_UINT_MAXLEN];
|
|
|
|
long id_long;
|
|
|
|
ssize_t data_size;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
|
|
|
|
if (data_size <= 0) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
} else {
|
|
|
|
ret = strict_strtol(id_str, 10, &id_long);
|
|
|
|
*id = (__u32)id_long;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
|
|
|
|
{
|
|
|
|
return nfs_idmap_lookup_id(name, namelen, "uid", uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *gid)
|
|
|
|
{
|
|
|
|
return nfs_idmap_lookup_id(name, namelen, "gid", gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
return nfs_idmap_lookup_name(uid, "user", buf, buflen);
|
|
|
|
}
|
|
|
|
int nfs_map_gid_to_group(struct nfs_client *clp, __u32 gid, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
return nfs_idmap_lookup_name(gid, "group", buf, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_NFS_USE_IDMAPPER not defined */
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/module.h>
|
2006-03-20 21:44:11 +03:00
|
|
|
#include <linux/mutex.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
|
|
|
|
#include <linux/sunrpc/clnt.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/sunrpc/rpc_pipe_fs.h>
|
|
|
|
|
|
|
|
#include <linux/nfs_fs.h>
|
|
|
|
|
|
|
|
#include <linux/nfs_idmap.h>
|
2005-06-22 21:16:21 +04:00
|
|
|
#include "nfs4_fs.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
#define IDMAP_HASH_SZ 128
|
|
|
|
|
2006-01-03 11:55:57 +03:00
|
|
|
/* Default cache timeout is 10 minutes */
|
|
|
|
unsigned int nfs_idmap_cache_timeout = 600 * HZ;
|
|
|
|
|
2006-08-23 04:06:07 +04:00
|
|
|
static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
int num = simple_strtol(val, &endp, 0);
|
|
|
|
int jif = num * HZ;
|
|
|
|
if (endp == val || *endp || num < 0 || jif < num)
|
|
|
|
return -EINVAL;
|
|
|
|
*((int *)kp->arg) = jif;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
|
|
|
|
&nfs_idmap_cache_timeout, 0644);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
struct idmap_hashent {
|
2007-12-20 22:54:35 +03:00
|
|
|
unsigned long ih_expires;
|
|
|
|
__u32 ih_id;
|
2007-12-20 22:54:49 +03:00
|
|
|
size_t ih_namelen;
|
2007-12-20 22:54:35 +03:00
|
|
|
char ih_name[IDMAP_NAMESZ];
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct idmap_hashtable {
|
2007-12-20 22:54:35 +03:00
|
|
|
__u8 h_type;
|
|
|
|
struct idmap_hashent h_entries[IDMAP_HASH_SZ];
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct idmap {
|
2007-12-20 22:54:35 +03:00
|
|
|
struct dentry *idmap_dentry;
|
|
|
|
wait_queue_head_t idmap_wq;
|
|
|
|
struct idmap_msg idmap_im;
|
|
|
|
struct mutex idmap_lock; /* Serializes upcalls */
|
|
|
|
struct mutex idmap_im_lock; /* Protects the hashtable */
|
|
|
|
struct idmap_hashtable idmap_user_hash;
|
|
|
|
struct idmap_hashtable idmap_group_hash;
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2007-12-20 22:54:35 +03:00
|
|
|
static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
|
|
|
|
char __user *, size_t);
|
|
|
|
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 02:20:36 +04:00
|
|
|
|
|
|
|
static unsigned int fnvhash32(const void *, size_t);
|
|
|
|
|
2009-08-09 23:14:15 +04:00
|
|
|
static const struct rpc_pipe_ops idmap_upcall_ops = {
|
2007-12-20 22:54:35 +03:00
|
|
|
.upcall = idmap_pipe_upcall,
|
|
|
|
.downcall = idmap_pipe_downcall,
|
|
|
|
.destroy_msg = idmap_pipe_destroy_msg,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2006-08-23 04:06:09 +04:00
|
|
|
int
|
2006-08-23 04:06:08 +04:00
|
|
|
nfs_idmap_new(struct nfs_client *clp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct idmap *idmap;
|
2006-08-23 04:06:09 +04:00
|
|
|
int error;
|
2005-04-17 02:20:36 +04: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 04:06:13 +04:00
|
|
|
BUG_ON(clp->cl_idmap != NULL);
|
2006-08-23 04:06:09 +04:00
|
|
|
|
2007-12-20 22:54:35 +03:00
|
|
|
idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
|
|
|
|
if (idmap == NULL)
|
|
|
|
return -ENOMEM;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2009-08-09 23:14:24 +04:00
|
|
|
idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
|
|
|
|
"idmap", idmap, &idmap_upcall_ops, 0);
|
2007-12-20 22:54:35 +03:00
|
|
|
if (IS_ERR(idmap->idmap_dentry)) {
|
2006-08-23 04:06:09 +04:00
|
|
|
error = PTR_ERR(idmap->idmap_dentry);
|
2005-04-17 02:20:36 +04:00
|
|
|
kfree(idmap);
|
2006-08-23 04:06:09 +04:00
|
|
|
return error;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2007-12-20 22:54:35 +03:00
|
|
|
mutex_init(&idmap->idmap_lock);
|
|
|
|
mutex_init(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
init_waitqueue_head(&idmap->idmap_wq);
|
|
|
|
idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
|
|
|
|
idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
|
|
|
|
|
|
|
|
clp->cl_idmap = idmap;
|
2006-08-23 04:06:09 +04:00
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-23 04:06:08 +04:00
|
|
|
nfs_idmap_delete(struct nfs_client *clp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct idmap *idmap = clp->cl_idmap;
|
|
|
|
|
|
|
|
if (!idmap)
|
|
|
|
return;
|
2006-08-01 01:11:48 +04:00
|
|
|
rpc_unlink(idmap->idmap_dentry);
|
2005-04-17 02:20:36 +04:00
|
|
|
clp->cl_idmap = NULL;
|
|
|
|
kfree(idmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper routines for manipulating the hashtable
|
|
|
|
*/
|
|
|
|
static inline struct idmap_hashent *
|
|
|
|
idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
|
|
|
|
{
|
|
|
|
return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct idmap_hashent *
|
|
|
|
idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
|
|
|
|
{
|
|
|
|
struct idmap_hashent *he = idmap_name_hash(h, name, len);
|
|
|
|
|
|
|
|
if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
|
|
|
|
return NULL;
|
2006-01-03 11:55:57 +03:00
|
|
|
if (time_after(jiffies, he->ih_expires))
|
|
|
|
return NULL;
|
2005-04-17 02:20:36 +04:00
|
|
|
return he;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct idmap_hashent *
|
|
|
|
idmap_id_hash(struct idmap_hashtable* h, __u32 id)
|
|
|
|
{
|
|
|
|
return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct idmap_hashent *
|
|
|
|
idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
|
|
|
|
{
|
|
|
|
struct idmap_hashent *he = idmap_id_hash(h, id);
|
|
|
|
if (he->ih_id != id || he->ih_namelen == 0)
|
|
|
|
return NULL;
|
2006-01-03 11:55:57 +03:00
|
|
|
if (time_after(jiffies, he->ih_expires))
|
|
|
|
return NULL;
|
2005-04-17 02:20:36 +04:00
|
|
|
return he;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines for allocating new entries in the hashtable.
|
|
|
|
* For now, we just have 1 entry per bucket, so it's all
|
|
|
|
* pretty trivial.
|
|
|
|
*/
|
|
|
|
static inline struct idmap_hashent *
|
2007-12-20 22:54:49 +03:00
|
|
|
idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return idmap_name_hash(h, name, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct idmap_hashent *
|
|
|
|
idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
|
|
|
|
{
|
|
|
|
return idmap_id_hash(h, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
idmap_update_entry(struct idmap_hashent *he, const char *name,
|
|
|
|
size_t namelen, __u32 id)
|
|
|
|
{
|
|
|
|
he->ih_id = id;
|
|
|
|
memcpy(he->ih_name, name, namelen);
|
|
|
|
he->ih_name[namelen] = '\0';
|
|
|
|
he->ih_namelen = namelen;
|
2006-01-03 11:55:57 +03:00
|
|
|
he->ih_expires = jiffies + nfs_idmap_cache_timeout;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Name -> ID
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
|
|
|
|
const char *name, size_t namelen, __u32 *id)
|
|
|
|
{
|
|
|
|
struct rpc_pipe_msg msg;
|
|
|
|
struct idmap_msg *im;
|
|
|
|
struct idmap_hashent *he;
|
|
|
|
DECLARE_WAITQUEUE(wq, current);
|
|
|
|
int ret = -EIO;
|
|
|
|
|
|
|
|
im = &idmap->idmap_im;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* String sanity checks
|
|
|
|
* Note that the userland daemon expects NUL terminated strings
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
if (namelen == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
if (name[namelen-1] != '\0')
|
|
|
|
break;
|
|
|
|
namelen--;
|
|
|
|
}
|
|
|
|
if (namelen >= IDMAP_NAMESZ)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_lock(&idmap->idmap_lock);
|
|
|
|
mutex_lock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
he = idmap_lookup_name(h, name, namelen);
|
|
|
|
if (he != NULL) {
|
|
|
|
*id = he->ih_id;
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(im, 0, sizeof(*im));
|
|
|
|
memcpy(im->im_name, name, namelen);
|
|
|
|
|
|
|
|
im->im_type = h->h_type;
|
|
|
|
im->im_conv = IDMAP_CONV_NAMETOID;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.data = im;
|
|
|
|
msg.len = sizeof(*im);
|
|
|
|
|
|
|
|
add_wait_queue(&idmap->idmap_wq, &wq);
|
|
|
|
if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
|
|
|
|
remove_wait_queue(&idmap->idmap_wq, &wq);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_unlock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
schedule();
|
2007-04-26 11:29:03 +04:00
|
|
|
__set_current_state(TASK_RUNNING);
|
2005-04-17 02:20:36 +04:00
|
|
|
remove_wait_queue(&idmap->idmap_wq, &wq);
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_lock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (im->im_status & IDMAP_STATUS_SUCCESS) {
|
|
|
|
*id = im->im_id;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
memset(im, 0, sizeof(*im));
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_unlock(&idmap->idmap_im_lock);
|
|
|
|
mutex_unlock(&idmap->idmap_lock);
|
2007-12-20 22:54:35 +03:00
|
|
|
return ret;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ID -> Name
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
|
|
|
|
__u32 id, char *name)
|
|
|
|
{
|
|
|
|
struct rpc_pipe_msg msg;
|
|
|
|
struct idmap_msg *im;
|
|
|
|
struct idmap_hashent *he;
|
|
|
|
DECLARE_WAITQUEUE(wq, current);
|
|
|
|
int ret = -EIO;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
im = &idmap->idmap_im;
|
|
|
|
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_lock(&idmap->idmap_lock);
|
|
|
|
mutex_lock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
he = idmap_lookup_id(h, id);
|
2008-02-21 00:03:05 +03:00
|
|
|
if (he) {
|
2005-04-17 02:20:36 +04:00
|
|
|
memcpy(name, he->ih_name, he->ih_namelen);
|
|
|
|
ret = he->ih_namelen;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(im, 0, sizeof(*im));
|
|
|
|
im->im_type = h->h_type;
|
|
|
|
im->im_conv = IDMAP_CONV_IDTONAME;
|
|
|
|
im->im_id = id;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.data = im;
|
|
|
|
msg.len = sizeof(*im);
|
|
|
|
|
|
|
|
add_wait_queue(&idmap->idmap_wq, &wq);
|
|
|
|
|
|
|
|
if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
|
|
|
|
remove_wait_queue(&idmap->idmap_wq, &wq);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_unlock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
schedule();
|
2007-04-26 11:29:03 +04:00
|
|
|
__set_current_state(TASK_RUNNING);
|
2005-04-17 02:20:36 +04:00
|
|
|
remove_wait_queue(&idmap->idmap_wq, &wq);
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_lock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (im->im_status & IDMAP_STATUS_SUCCESS) {
|
|
|
|
if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
|
|
|
|
goto out;
|
|
|
|
memcpy(name, im->im_name, len);
|
|
|
|
ret = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
memset(im, 0, sizeof(*im));
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_unlock(&idmap->idmap_im_lock);
|
|
|
|
mutex_unlock(&idmap->idmap_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* RPC pipefs upcall/downcall routines */
|
|
|
|
static ssize_t
|
|
|
|
idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
|
2007-12-20 22:54:35 +03:00
|
|
|
char __user *dst, size_t buflen)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2007-12-20 22:54:35 +03:00
|
|
|
char *data = (char *)msg->data + msg->copied;
|
2007-12-20 22:54:42 +03:00
|
|
|
size_t mlen = min(msg->len, buflen);
|
|
|
|
unsigned long left;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-12-20 22:54:35 +03:00
|
|
|
left = copy_to_user(dst, data, mlen);
|
2007-12-20 22:54:42 +03:00
|
|
|
if (left == mlen) {
|
|
|
|
msg->errno = -EFAULT;
|
|
|
|
return -EFAULT;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2007-12-20 22:54:42 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
mlen -= left;
|
|
|
|
msg->copied += mlen;
|
|
|
|
msg->errno = 0;
|
2007-12-20 22:54:35 +03:00
|
|
|
return mlen;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
|
|
|
|
{
|
2007-12-20 22:54:35 +03:00
|
|
|
struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
|
2005-04-17 02:20:36 +04:00
|
|
|
struct idmap *idmap = (struct idmap *)rpci->private;
|
|
|
|
struct idmap_msg im_in, *im = &idmap->idmap_im;
|
|
|
|
struct idmap_hashtable *h;
|
|
|
|
struct idmap_hashent *he = NULL;
|
2007-12-20 22:54:49 +03:00
|
|
|
size_t namelen_in;
|
2005-04-17 02:20:36 +04:00
|
|
|
int ret;
|
|
|
|
|
2007-12-20 22:54:35 +03:00
|
|
|
if (mlen != sizeof(im_in))
|
|
|
|
return -ENOSPC;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-12-20 22:54:35 +03:00
|
|
|
if (copy_from_user(&im_in, src, mlen) != 0)
|
|
|
|
return -EFAULT;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_lock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
ret = mlen;
|
|
|
|
im->im_status = im_in.im_status;
|
|
|
|
/* If we got an error, terminate now, and wake up pending upcalls */
|
|
|
|
if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
|
|
|
|
wake_up(&idmap->idmap_wq);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity checking of strings */
|
|
|
|
ret = -EINVAL;
|
|
|
|
namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
|
|
|
|
if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
switch (im_in.im_type) {
|
|
|
|
case IDMAP_TYPE_USER:
|
|
|
|
h = &idmap->idmap_user_hash;
|
|
|
|
break;
|
|
|
|
case IDMAP_TYPE_GROUP:
|
|
|
|
h = &idmap->idmap_group_hash;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (im_in.im_conv) {
|
|
|
|
case IDMAP_CONV_IDTONAME:
|
|
|
|
/* Did we match the current upcall? */
|
|
|
|
if (im->im_conv == IDMAP_CONV_IDTONAME
|
|
|
|
&& im->im_type == im_in.im_type
|
|
|
|
&& im->im_id == im_in.im_id) {
|
|
|
|
/* Yes: copy string, including the terminating '\0' */
|
|
|
|
memcpy(im->im_name, im_in.im_name, namelen_in);
|
|
|
|
im->im_name[namelen_in] = '\0';
|
|
|
|
wake_up(&idmap->idmap_wq);
|
|
|
|
}
|
|
|
|
he = idmap_alloc_id(h, im_in.im_id);
|
|
|
|
break;
|
|
|
|
case IDMAP_CONV_NAMETOID:
|
|
|
|
/* Did we match the current upcall? */
|
|
|
|
if (im->im_conv == IDMAP_CONV_NAMETOID
|
|
|
|
&& im->im_type == im_in.im_type
|
|
|
|
&& strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
|
|
|
|
&& memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
|
|
|
|
im->im_id = im_in.im_id;
|
|
|
|
wake_up(&idmap->idmap_wq);
|
|
|
|
}
|
|
|
|
he = idmap_alloc_name(h, im_in.im_name, namelen_in);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the entry is valid, also copy it to the cache */
|
|
|
|
if (he != NULL)
|
|
|
|
idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
|
|
|
|
ret = mlen;
|
|
|
|
out:
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_unlock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-05-06 03:16:09 +04:00
|
|
|
static void
|
2005-04-17 02:20:36 +04:00
|
|
|
idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
|
|
|
|
{
|
|
|
|
struct idmap_msg *im = msg->data;
|
|
|
|
struct idmap *idmap = container_of(im, struct idmap, idmap_im);
|
|
|
|
|
|
|
|
if (msg->errno >= 0)
|
|
|
|
return;
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_lock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
im->im_status = IDMAP_STATUS_LOOKUPFAIL;
|
|
|
|
wake_up(&idmap->idmap_wq);
|
2006-03-20 21:44:11 +03:00
|
|
|
mutex_unlock(&idmap->idmap_im_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fowler/Noll/Vo hash
|
|
|
|
* http://www.isthe.com/chongo/tech/comp/fnv/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
|
|
|
|
#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
|
|
|
|
|
|
|
|
static unsigned int fnvhash32(const void *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
const unsigned char *p, *end = (const unsigned char *)buf + buflen;
|
|
|
|
unsigned int hash = FNV_1_32;
|
|
|
|
|
|
|
|
for (p = buf; p < end; p++) {
|
|
|
|
hash *= FNV_P_32;
|
|
|
|
hash ^= (unsigned int)*p;
|
|
|
|
}
|
|
|
|
|
2007-12-20 22:54:35 +03:00
|
|
|
return hash;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2006-08-23 04:06:08 +04:00
|
|
|
int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct idmap *idmap = clp->cl_idmap;
|
|
|
|
|
|
|
|
return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
|
|
|
|
}
|
|
|
|
|
2006-08-23 04:06:08 +04:00
|
|
|
int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct idmap *idmap = clp->cl_idmap;
|
|
|
|
|
|
|
|
return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
|
|
|
|
}
|
|
|
|
|
2010-09-29 23:41:49 +04:00
|
|
|
int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct idmap *idmap = clp->cl_idmap;
|
|
|
|
|
|
|
|
return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
|
|
|
|
}
|
2010-09-29 23:41:49 +04:00
|
|
|
int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct idmap *idmap = clp->cl_idmap;
|
|
|
|
|
|
|
|
return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
|
|
|
|
}
|
|
|
|
|
2010-09-29 23:41:49 +04:00
|
|
|
#endif /* CONFIG_NFS_USE_NEW_IDMAPPER */
|