mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-01-18 02:46:06 +00:00
2a4317c554
Abstract out the mechanism that we use to track clients into a set of client name tracking functions. This gives us a mechanism to plug in a new set of client tracking functions without disturbing the callers. It also gives us a way to decide on what tracking scheme to use at runtime. For now, this just looks like pointless abstraction, but later we'll add a new alternate scheme for tracking clients on stable storage. Note too that this patch anticipates the eventual containerization of this code by passing in struct net pointers in places. No attempt is made to containerize the legacy client tracker however. Signed-off-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: J. Bruce Fields <bfields@redhat.com>
543 lines
12 KiB
C
543 lines
12 KiB
C
/*
|
|
* Copyright (c) 2004 The Regents of the University of Michigan.
|
|
* All rights reserved.
|
|
*
|
|
* Andy Adamson <andros@citi.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.
|
|
*
|
|
*/
|
|
|
|
#include <linux/file.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/namei.h>
|
|
#include <linux/crypto.h>
|
|
#include <linux/sched.h>
|
|
|
|
#include "nfsd.h"
|
|
#include "state.h"
|
|
#include "vfs.h"
|
|
|
|
#define NFSDDBG_FACILITY NFSDDBG_PROC
|
|
|
|
/* Declarations */
|
|
struct nfsd4_client_tracking_ops {
|
|
int (*init)(struct net *);
|
|
void (*exit)(struct net *);
|
|
void (*create)(struct nfs4_client *);
|
|
void (*remove)(struct nfs4_client *);
|
|
int (*check)(struct nfs4_client *);
|
|
void (*grace_done)(struct net *, time_t);
|
|
};
|
|
|
|
/* Globals */
|
|
static struct file *rec_file;
|
|
static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
|
|
static struct nfsd4_client_tracking_ops *client_tracking_ops;
|
|
|
|
static int
|
|
nfs4_save_creds(const struct cred **original_creds)
|
|
{
|
|
struct cred *new;
|
|
|
|
new = prepare_creds();
|
|
if (!new)
|
|
return -ENOMEM;
|
|
|
|
new->fsuid = 0;
|
|
new->fsgid = 0;
|
|
*original_creds = override_creds(new);
|
|
put_cred(new);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
nfs4_reset_creds(const struct cred *original)
|
|
{
|
|
revert_creds(original);
|
|
}
|
|
|
|
static void
|
|
md5_to_hex(char *out, char *md5)
|
|
{
|
|
int i;
|
|
|
|
for (i=0; i<16; i++) {
|
|
unsigned char c = md5[i];
|
|
|
|
*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
|
|
*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
|
|
}
|
|
*out = '\0';
|
|
}
|
|
|
|
__be32
|
|
nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
|
|
{
|
|
struct xdr_netobj cksum;
|
|
struct hash_desc desc;
|
|
struct scatterlist sg;
|
|
__be32 status = nfserr_jukebox;
|
|
|
|
dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
|
|
clname->len, clname->data);
|
|
desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
|
|
desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
|
|
if (IS_ERR(desc.tfm))
|
|
goto out_no_tfm;
|
|
cksum.len = crypto_hash_digestsize(desc.tfm);
|
|
cksum.data = kmalloc(cksum.len, GFP_KERNEL);
|
|
if (cksum.data == NULL)
|
|
goto out;
|
|
|
|
sg_init_one(&sg, clname->data, clname->len);
|
|
|
|
if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
|
|
goto out;
|
|
|
|
md5_to_hex(dname, cksum.data);
|
|
|
|
status = nfs_ok;
|
|
out:
|
|
kfree(cksum.data);
|
|
crypto_free_hash(desc.tfm);
|
|
out_no_tfm:
|
|
return status;
|
|
}
|
|
|
|
static void
|
|
nfsd4_create_clid_dir(struct nfs4_client *clp)
|
|
{
|
|
const struct cred *original_cred;
|
|
char *dname = clp->cl_recdir;
|
|
struct dentry *dir, *dentry;
|
|
int status;
|
|
|
|
dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
|
|
|
|
if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
|
|
return;
|
|
if (!rec_file)
|
|
return;
|
|
status = nfs4_save_creds(&original_cred);
|
|
if (status < 0)
|
|
return;
|
|
|
|
dir = rec_file->f_path.dentry;
|
|
/* lock the parent */
|
|
mutex_lock(&dir->d_inode->i_mutex);
|
|
|
|
dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
|
|
if (IS_ERR(dentry)) {
|
|
status = PTR_ERR(dentry);
|
|
goto out_unlock;
|
|
}
|
|
if (dentry->d_inode)
|
|
/*
|
|
* In the 4.1 case, where we're called from
|
|
* reclaim_complete(), records from the previous reboot
|
|
* may still be left, so this is OK.
|
|
*
|
|
* In the 4.0 case, we should never get here; but we may
|
|
* as well be forgiving and just succeed silently.
|
|
*/
|
|
goto out_put;
|
|
status = mnt_want_write_file(rec_file);
|
|
if (status)
|
|
goto out_put;
|
|
status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
|
|
mnt_drop_write_file(rec_file);
|
|
out_put:
|
|
dput(dentry);
|
|
out_unlock:
|
|
mutex_unlock(&dir->d_inode->i_mutex);
|
|
if (status == 0)
|
|
vfs_fsync(rec_file, 0);
|
|
else
|
|
printk(KERN_ERR "NFSD: failed to write recovery record"
|
|
" (err %d); please check that %s exists"
|
|
" and is writeable", status,
|
|
user_recovery_dirname);
|
|
nfs4_reset_creds(original_cred);
|
|
}
|
|
|
|
typedef int (recdir_func)(struct dentry *, struct dentry *);
|
|
|
|
struct name_list {
|
|
char name[HEXDIR_LEN];
|
|
struct list_head list;
|
|
};
|
|
|
|
static int
|
|
nfsd4_build_namelist(void *arg, const char *name, int namlen,
|
|
loff_t offset, u64 ino, unsigned int d_type)
|
|
{
|
|
struct list_head *names = arg;
|
|
struct name_list *entry;
|
|
|
|
if (namlen != HEXDIR_LEN - 1)
|
|
return 0;
|
|
entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
|
|
if (entry == NULL)
|
|
return -ENOMEM;
|
|
memcpy(entry->name, name, HEXDIR_LEN - 1);
|
|
entry->name[HEXDIR_LEN - 1] = '\0';
|
|
list_add(&entry->list, names);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
nfsd4_list_rec_dir(recdir_func *f)
|
|
{
|
|
const struct cred *original_cred;
|
|
struct dentry *dir = rec_file->f_path.dentry;
|
|
LIST_HEAD(names);
|
|
int status;
|
|
|
|
status = nfs4_save_creds(&original_cred);
|
|
if (status < 0)
|
|
return status;
|
|
|
|
status = vfs_llseek(rec_file, 0, SEEK_SET);
|
|
if (status < 0) {
|
|
nfs4_reset_creds(original_cred);
|
|
return status;
|
|
}
|
|
|
|
status = vfs_readdir(rec_file, nfsd4_build_namelist, &names);
|
|
mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
|
|
while (!list_empty(&names)) {
|
|
struct name_list *entry;
|
|
entry = list_entry(names.next, struct name_list, list);
|
|
if (!status) {
|
|
struct dentry *dentry;
|
|
dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
|
|
if (IS_ERR(dentry)) {
|
|
status = PTR_ERR(dentry);
|
|
break;
|
|
}
|
|
status = f(dir, dentry);
|
|
dput(dentry);
|
|
}
|
|
list_del(&entry->list);
|
|
kfree(entry);
|
|
}
|
|
mutex_unlock(&dir->d_inode->i_mutex);
|
|
nfs4_reset_creds(original_cred);
|
|
return status;
|
|
}
|
|
|
|
static int
|
|
nfsd4_unlink_clid_dir(char *name, int namlen)
|
|
{
|
|
struct dentry *dir, *dentry;
|
|
int status;
|
|
|
|
dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
|
|
|
|
dir = rec_file->f_path.dentry;
|
|
mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
|
|
dentry = lookup_one_len(name, dir, namlen);
|
|
if (IS_ERR(dentry)) {
|
|
status = PTR_ERR(dentry);
|
|
goto out_unlock;
|
|
}
|
|
status = -ENOENT;
|
|
if (!dentry->d_inode)
|
|
goto out;
|
|
status = vfs_rmdir(dir->d_inode, dentry);
|
|
out:
|
|
dput(dentry);
|
|
out_unlock:
|
|
mutex_unlock(&dir->d_inode->i_mutex);
|
|
return status;
|
|
}
|
|
|
|
static void
|
|
nfsd4_remove_clid_dir(struct nfs4_client *clp)
|
|
{
|
|
const struct cred *original_cred;
|
|
int status;
|
|
|
|
if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
|
|
return;
|
|
|
|
status = mnt_want_write_file(rec_file);
|
|
if (status)
|
|
goto out;
|
|
clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
|
|
|
|
status = nfs4_save_creds(&original_cred);
|
|
if (status < 0)
|
|
goto out;
|
|
|
|
status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
|
|
nfs4_reset_creds(original_cred);
|
|
if (status == 0)
|
|
vfs_fsync(rec_file, 0);
|
|
mnt_drop_write_file(rec_file);
|
|
out:
|
|
if (status)
|
|
printk("NFSD: Failed to remove expired client state directory"
|
|
" %.*s\n", HEXDIR_LEN, clp->cl_recdir);
|
|
}
|
|
|
|
static int
|
|
purge_old(struct dentry *parent, struct dentry *child)
|
|
{
|
|
int status;
|
|
|
|
if (nfs4_has_reclaimed_state(child->d_name.name, false))
|
|
return 0;
|
|
|
|
status = vfs_rmdir(parent->d_inode, child);
|
|
if (status)
|
|
printk("failed to remove client recovery directory %s\n",
|
|
child->d_name.name);
|
|
/* Keep trying, success or failure: */
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
nfsd4_recdir_purge_old(struct net *net, time_t boot_time)
|
|
{
|
|
int status;
|
|
|
|
if (!rec_file)
|
|
return;
|
|
status = mnt_want_write_file(rec_file);
|
|
if (status)
|
|
goto out;
|
|
status = nfsd4_list_rec_dir(purge_old);
|
|
if (status == 0)
|
|
vfs_fsync(rec_file, 0);
|
|
mnt_drop_write_file(rec_file);
|
|
out:
|
|
if (status)
|
|
printk("nfsd4: failed to purge old clients from recovery"
|
|
" directory %s\n", rec_file->f_path.dentry->d_name.name);
|
|
}
|
|
|
|
static int
|
|
load_recdir(struct dentry *parent, struct dentry *child)
|
|
{
|
|
if (child->d_name.len != HEXDIR_LEN - 1) {
|
|
printk("nfsd4: illegal name %s in recovery directory\n",
|
|
child->d_name.name);
|
|
/* Keep trying; maybe the others are OK: */
|
|
return 0;
|
|
}
|
|
nfs4_client_to_reclaim(child->d_name.name);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
nfsd4_recdir_load(void) {
|
|
int status;
|
|
|
|
if (!rec_file)
|
|
return 0;
|
|
|
|
status = nfsd4_list_rec_dir(load_recdir);
|
|
if (status)
|
|
printk("nfsd4: failed loading clients from recovery"
|
|
" directory %s\n", rec_file->f_path.dentry->d_name.name);
|
|
return status;
|
|
}
|
|
|
|
/*
|
|
* Hold reference to the recovery directory.
|
|
*/
|
|
|
|
static int
|
|
nfsd4_init_recdir(void)
|
|
{
|
|
const struct cred *original_cred;
|
|
int status;
|
|
|
|
printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
|
|
user_recovery_dirname);
|
|
|
|
BUG_ON(rec_file);
|
|
|
|
status = nfs4_save_creds(&original_cred);
|
|
if (status < 0) {
|
|
printk("NFSD: Unable to change credentials to find recovery"
|
|
" directory: error %d\n",
|
|
status);
|
|
return status;
|
|
}
|
|
|
|
rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
|
|
if (IS_ERR(rec_file)) {
|
|
printk("NFSD: unable to find recovery directory %s\n",
|
|
user_recovery_dirname);
|
|
status = PTR_ERR(rec_file);
|
|
rec_file = NULL;
|
|
}
|
|
|
|
nfs4_reset_creds(original_cred);
|
|
return status;
|
|
}
|
|
|
|
static int
|
|
nfsd4_load_reboot_recovery_data(struct net *net)
|
|
{
|
|
int status;
|
|
|
|
nfs4_lock_state();
|
|
status = nfsd4_init_recdir();
|
|
if (!status)
|
|
status = nfsd4_recdir_load();
|
|
nfs4_unlock_state();
|
|
if (status)
|
|
printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n");
|
|
return status;
|
|
}
|
|
|
|
static void
|
|
nfsd4_shutdown_recdir(void)
|
|
{
|
|
if (!rec_file)
|
|
return;
|
|
fput(rec_file);
|
|
rec_file = NULL;
|
|
}
|
|
|
|
static void
|
|
nfsd4_legacy_tracking_exit(struct net *net)
|
|
{
|
|
nfs4_release_reclaim();
|
|
nfsd4_shutdown_recdir();
|
|
}
|
|
|
|
/*
|
|
* Change the NFSv4 recovery directory to recdir.
|
|
*/
|
|
int
|
|
nfs4_reset_recoverydir(char *recdir)
|
|
{
|
|
int status;
|
|
struct path path;
|
|
|
|
status = kern_path(recdir, LOOKUP_FOLLOW, &path);
|
|
if (status)
|
|
return status;
|
|
status = -ENOTDIR;
|
|
if (S_ISDIR(path.dentry->d_inode->i_mode)) {
|
|
strcpy(user_recovery_dirname, recdir);
|
|
status = 0;
|
|
}
|
|
path_put(&path);
|
|
return status;
|
|
}
|
|
|
|
char *
|
|
nfs4_recoverydir(void)
|
|
{
|
|
return user_recovery_dirname;
|
|
}
|
|
|
|
static int
|
|
nfsd4_check_legacy_client(struct nfs4_client *clp)
|
|
{
|
|
/* did we already find that this client is stable? */
|
|
if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
|
|
return 0;
|
|
|
|
/* look for it in the reclaim hashtable otherwise */
|
|
if (nfsd4_find_reclaim_client(clp)) {
|
|
set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
|
|
return 0;
|
|
}
|
|
|
|
return -ENOENT;
|
|
}
|
|
|
|
static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
|
|
.init = nfsd4_load_reboot_recovery_data,
|
|
.exit = nfsd4_legacy_tracking_exit,
|
|
.create = nfsd4_create_clid_dir,
|
|
.remove = nfsd4_remove_clid_dir,
|
|
.check = nfsd4_check_legacy_client,
|
|
.grace_done = nfsd4_recdir_purge_old,
|
|
};
|
|
|
|
int
|
|
nfsd4_client_tracking_init(struct net *net)
|
|
{
|
|
int status;
|
|
|
|
client_tracking_ops = &nfsd4_legacy_tracking_ops;
|
|
|
|
status = client_tracking_ops->init(net);
|
|
if (status) {
|
|
printk(KERN_WARNING "NFSD: Unable to initialize client "
|
|
"recovery tracking! (%d)\n", status);
|
|
client_tracking_ops = NULL;
|
|
}
|
|
return status;
|
|
}
|
|
|
|
void
|
|
nfsd4_client_tracking_exit(struct net *net)
|
|
{
|
|
if (client_tracking_ops) {
|
|
client_tracking_ops->exit(net);
|
|
client_tracking_ops = NULL;
|
|
}
|
|
}
|
|
|
|
void
|
|
nfsd4_client_record_create(struct nfs4_client *clp)
|
|
{
|
|
if (client_tracking_ops)
|
|
client_tracking_ops->create(clp);
|
|
}
|
|
|
|
void
|
|
nfsd4_client_record_remove(struct nfs4_client *clp)
|
|
{
|
|
if (client_tracking_ops)
|
|
client_tracking_ops->remove(clp);
|
|
}
|
|
|
|
int
|
|
nfsd4_client_record_check(struct nfs4_client *clp)
|
|
{
|
|
if (client_tracking_ops)
|
|
return client_tracking_ops->check(clp);
|
|
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
void
|
|
nfsd4_record_grace_done(struct net *net, time_t boot_time)
|
|
{
|
|
if (client_tracking_ops)
|
|
client_tracking_ops->grace_done(net, boot_time);
|
|
}
|