2019-05-19 12:08:20 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-07-16 20:39:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
|
|
|
|
*/
|
|
|
|
#include <linux/init.h>
|
2012-07-16 20:39:20 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/nfs4_mount.h>
|
2012-07-16 20:39:14 +00:00
|
|
|
#include <linux/nfs_fs.h>
|
2012-07-30 20:05:21 +00:00
|
|
|
#include "delegation.h"
|
2012-07-16 20:39:20 +00:00
|
|
|
#include "internal.h"
|
2012-07-16 20:39:14 +00:00
|
|
|
#include "nfs4_fs.h"
|
2015-04-15 17:00:05 +00:00
|
|
|
#include "nfs4idmap.h"
|
2013-06-01 15:50:58 +00:00
|
|
|
#include "dns_resolve.h"
|
2012-07-30 20:05:21 +00:00
|
|
|
#include "pnfs.h"
|
2012-07-30 20:05:16 +00:00
|
|
|
#include "nfs.h"
|
2012-07-16 20:39:13 +00:00
|
|
|
|
2012-07-16 20:39:20 +00:00
|
|
|
#define NFSDBG_FACILITY NFSDBG_VFS
|
|
|
|
|
2012-07-30 20:05:21 +00:00
|
|
|
static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
|
|
|
|
static void nfs4_evict_inode(struct inode *inode);
|
2012-07-16 20:39:20 +00:00
|
|
|
static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *raw_data);
|
|
|
|
|
|
|
|
struct file_system_type nfs4_referral_fs_type = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "nfs4",
|
|
|
|
.mount = nfs4_referral_mount,
|
|
|
|
.kill_sb = nfs_kill_super,
|
2013-02-20 16:19:05 +00:00
|
|
|
.fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
|
2012-07-16 20:39:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct super_operations nfs4_sops = {
|
|
|
|
.alloc_inode = nfs_alloc_inode,
|
2019-04-16 00:19:40 +00:00
|
|
|
.free_inode = nfs_free_inode,
|
2012-07-16 20:39:20 +00:00
|
|
|
.write_inode = nfs4_write_inode,
|
2012-12-14 19:36:36 +00:00
|
|
|
.drop_inode = nfs_drop_inode,
|
2012-07-16 20:39:20 +00:00
|
|
|
.statfs = nfs_statfs,
|
|
|
|
.evict_inode = nfs4_evict_inode,
|
|
|
|
.umount_begin = nfs_umount_begin,
|
|
|
|
.show_options = nfs_show_options,
|
|
|
|
.show_devname = nfs_show_devname,
|
|
|
|
.show_path = nfs_show_path,
|
|
|
|
.show_stats = nfs_show_stats,
|
|
|
|
.remount_fs = nfs_remount,
|
|
|
|
};
|
|
|
|
|
2012-07-30 20:05:16 +00:00
|
|
|
struct nfs_subversion nfs_v4 = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.nfs_fs = &nfs4_fs_type,
|
|
|
|
.rpc_vers = &nfs_version4,
|
|
|
|
.rpc_ops = &nfs_v4_clientops,
|
2012-07-30 20:05:20 +00:00
|
|
|
.sops = &nfs4_sops,
|
|
|
|
.xattr = nfs4_xattr_handlers,
|
2012-07-30 20:05:16 +00:00
|
|
|
};
|
|
|
|
|
2012-07-30 20:05:21 +00:00
|
|
|
static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
|
|
|
|
{
|
|
|
|
int ret = nfs_write_inode(inode, wbc);
|
|
|
|
|
2014-01-13 18:34:36 +00:00
|
|
|
if (ret == 0)
|
|
|
|
ret = pnfs_layoutcommit_inode(inode,
|
|
|
|
wbc->sync_mode == WB_SYNC_ALL);
|
2012-07-30 20:05:21 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean out any remaining NFSv4 state that might be left over due
|
|
|
|
* to open() calls that passed nfs_atomic_lookup, but failed to call
|
|
|
|
* nfs_open().
|
|
|
|
*/
|
|
|
|
static void nfs4_evict_inode(struct inode *inode)
|
|
|
|
{
|
2014-04-03 21:47:49 +00:00
|
|
|
truncate_inode_pages_final(&inode->i_data);
|
2012-07-30 20:05:21 +00:00
|
|
|
clear_inode(inode);
|
2019-10-21 18:04:00 +00:00
|
|
|
/* If we are holding a delegation, return and free it */
|
|
|
|
nfs_inode_evict_delegation(inode);
|
2015-03-25 17:26:18 +00:00
|
|
|
/* Note that above delegreturn would trigger pnfs return-on-close */
|
|
|
|
pnfs_return_layout(inode);
|
|
|
|
pnfs_destroy_layout(NFS_I(inode));
|
2012-07-30 20:05:21 +00:00
|
|
|
/* First call standard NFS clear_inode() code */
|
|
|
|
nfs_clear_inode(inode);
|
|
|
|
}
|
|
|
|
|
2012-07-16 20:39:20 +00:00
|
|
|
struct nfs_referral_count {
|
|
|
|
struct list_head list;
|
|
|
|
const struct task_struct *task;
|
|
|
|
unsigned int referral_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(nfs_referral_count_list);
|
|
|
|
static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
|
|
|
|
|
|
|
|
static struct nfs_referral_count *nfs_find_referral_count(void)
|
|
|
|
{
|
|
|
|
struct nfs_referral_count *p;
|
|
|
|
|
|
|
|
list_for_each_entry(p, &nfs_referral_count_list, list) {
|
|
|
|
if (p->task == current)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NFS_MAX_NESTED_REFERRALS 2
|
|
|
|
|
|
|
|
static int nfs_referral_loop_protect(void)
|
|
|
|
{
|
|
|
|
struct nfs_referral_count *p, *new;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
new = kmalloc(sizeof(*new), GFP_KERNEL);
|
|
|
|
if (!new)
|
|
|
|
goto out;
|
|
|
|
new->task = current;
|
|
|
|
new->referral_count = 1;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
spin_lock(&nfs_referral_count_list_lock);
|
|
|
|
p = nfs_find_referral_count();
|
|
|
|
if (p != NULL) {
|
|
|
|
if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
|
|
|
|
ret = -ELOOP;
|
|
|
|
else
|
|
|
|
p->referral_count++;
|
|
|
|
} else {
|
|
|
|
list_add(&new->list, &nfs_referral_count_list);
|
|
|
|
new = NULL;
|
|
|
|
}
|
|
|
|
spin_unlock(&nfs_referral_count_list_lock);
|
|
|
|
kfree(new);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nfs_referral_loop_unprotect(void)
|
|
|
|
{
|
|
|
|
struct nfs_referral_count *p;
|
|
|
|
|
|
|
|
spin_lock(&nfs_referral_count_list_lock);
|
|
|
|
p = nfs_find_referral_count();
|
|
|
|
p->referral_count--;
|
|
|
|
if (p->referral_count == 0)
|
|
|
|
list_del(&p->list);
|
|
|
|
else
|
|
|
|
p = NULL;
|
|
|
|
spin_unlock(&nfs_referral_count_list_lock);
|
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
|
2019-12-10 12:30:54 +00:00
|
|
|
static struct dentry *do_nfs4_mount(struct nfs_server *server, int flags,
|
|
|
|
struct nfs_mount_info *info,
|
|
|
|
const char *hostname,
|
|
|
|
const char *export_path)
|
2012-07-16 20:39:20 +00:00
|
|
|
{
|
2019-12-10 12:30:54 +00:00
|
|
|
struct vfsmount *root_mnt;
|
2012-07-16 20:39:20 +00:00
|
|
|
struct dentry *dentry;
|
2019-12-10 12:30:54 +00:00
|
|
|
char *root_devname;
|
2012-07-16 20:39:20 +00:00
|
|
|
int err;
|
2019-12-10 12:30:54 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (IS_ERR(server))
|
|
|
|
return ERR_CAST(server);
|
|
|
|
|
|
|
|
len = strlen(hostname) + 5;
|
|
|
|
root_devname = kmalloc(len, GFP_KERNEL);
|
|
|
|
if (root_devname == NULL) {
|
|
|
|
nfs_free_server(server);
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does hostname needs to be enclosed in brackets? */
|
|
|
|
if (strchr(hostname, ':'))
|
|
|
|
snprintf(root_devname, len, "[%s]:/", hostname);
|
|
|
|
else
|
|
|
|
snprintf(root_devname, len, "%s:/", hostname);
|
|
|
|
info->server = server;
|
2019-12-10 12:30:58 +00:00
|
|
|
root_mnt = vfs_kern_mount(&nfs_prepared_fs_type, flags, root_devname, info);
|
2019-12-10 12:30:54 +00:00
|
|
|
if (info->server)
|
|
|
|
nfs_free_server(info->server);
|
|
|
|
info->server = NULL;
|
|
|
|
kfree(root_devname);
|
2012-07-16 20:39:20 +00:00
|
|
|
|
|
|
|
if (IS_ERR(root_mnt))
|
|
|
|
return ERR_CAST(root_mnt);
|
|
|
|
|
|
|
|
err = nfs_referral_loop_protect();
|
|
|
|
if (err) {
|
|
|
|
mntput(root_mnt);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
dentry = mount_subtree(root_mnt, export_path);
|
|
|
|
nfs_referral_loop_unprotect();
|
|
|
|
|
|
|
|
return dentry;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dentry *nfs4_try_mount(int flags, const char *dev_name,
|
2019-12-10 12:30:57 +00:00
|
|
|
struct nfs_mount_info *mount_info)
|
2012-07-16 20:39:20 +00:00
|
|
|
{
|
2019-12-10 12:31:06 +00:00
|
|
|
struct nfs_fs_context *ctx = mount_info->ctx;
|
2019-12-10 12:30:54 +00:00
|
|
|
struct dentry *res;
|
2012-07-16 20:39:20 +00:00
|
|
|
|
|
|
|
dfprintk(MOUNT, "--> nfs4_try_mount()\n");
|
|
|
|
|
2019-12-10 12:31:00 +00:00
|
|
|
res = do_nfs4_mount(nfs4_create_server(mount_info),
|
2019-12-10 12:30:54 +00:00
|
|
|
flags, mount_info,
|
2019-12-10 12:31:06 +00:00
|
|
|
ctx->nfs_server.hostname,
|
|
|
|
ctx->nfs_server.export_path);
|
2012-07-16 20:39:20 +00:00
|
|
|
|
2013-10-14 20:24:16 +00:00
|
|
|
dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
|
|
|
|
PTR_ERR_OR_ZERO(res),
|
|
|
|
IS_ERR(res) ? " [error]" : "");
|
2012-07-16 20:39:20 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an NFS4 server record on referral traversal
|
|
|
|
*/
|
|
|
|
static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *raw_data)
|
|
|
|
{
|
|
|
|
struct nfs_clone_mount *data = raw_data;
|
2019-12-10 12:30:51 +00:00
|
|
|
struct nfs_mount_info mount_info = {
|
|
|
|
.cloned = data,
|
2019-12-10 12:30:56 +00:00
|
|
|
.nfs_mod = &nfs_v4,
|
2019-12-10 12:30:51 +00:00
|
|
|
};
|
2012-07-16 20:39:20 +00:00
|
|
|
struct dentry *res;
|
|
|
|
|
|
|
|
dprintk("--> nfs4_referral_mount()\n");
|
|
|
|
|
2019-12-10 12:30:51 +00:00
|
|
|
mount_info.mntfh = nfs_alloc_fhandle();
|
|
|
|
if (!mount_info.mntfh)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2019-12-10 12:30:54 +00:00
|
|
|
res = do_nfs4_mount(nfs4_create_referral_server(mount_info.cloned,
|
|
|
|
mount_info.mntfh),
|
|
|
|
flags, &mount_info, data->hostname, data->mnt_path);
|
2012-07-16 20:39:20 +00:00
|
|
|
|
2013-10-14 20:24:16 +00:00
|
|
|
dprintk("<-- nfs4_referral_mount() = %d%s\n",
|
|
|
|
PTR_ERR_OR_ZERO(res),
|
|
|
|
IS_ERR(res) ? " [error]" : "");
|
2019-12-10 12:30:51 +00:00
|
|
|
|
|
|
|
nfs_free_fhandle(mount_info.mntfh);
|
2012-07-16 20:39:20 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-30 20:05:25 +00:00
|
|
|
static int __init init_nfs_v4(void)
|
2012-07-16 20:39:13 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2013-06-01 15:50:58 +00:00
|
|
|
err = nfs_dns_resolver_init();
|
2012-07-16 20:39:13 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
2013-06-01 15:50:58 +00:00
|
|
|
err = nfs_idmap_init();
|
2012-07-16 20:39:14 +00:00
|
|
|
if (err)
|
|
|
|
goto out1;
|
|
|
|
|
2013-06-01 15:50:58 +00:00
|
|
|
err = nfs4_register_sysctl();
|
|
|
|
if (err)
|
|
|
|
goto out2;
|
|
|
|
|
2012-07-30 20:05:16 +00:00
|
|
|
register_nfs_version(&nfs_v4);
|
2012-07-16 20:39:13 +00:00
|
|
|
return 0;
|
2013-06-01 15:50:58 +00:00
|
|
|
out2:
|
2012-07-16 20:39:14 +00:00
|
|
|
nfs_idmap_quit();
|
2013-06-01 15:50:58 +00:00
|
|
|
out1:
|
|
|
|
nfs_dns_resolver_destroy();
|
2012-07-16 20:39:13 +00:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-07-30 20:05:25 +00:00
|
|
|
static void __exit exit_nfs_v4(void)
|
2012-07-16 20:39:13 +00:00
|
|
|
{
|
2014-05-30 10:15:59 +00:00
|
|
|
/* Not called in the _init(), conditionally loaded */
|
|
|
|
nfs4_pnfs_v3_ds_connect_unload();
|
|
|
|
|
2012-07-30 20:05:16 +00:00
|
|
|
unregister_nfs_version(&nfs_v4);
|
2012-07-16 20:39:14 +00:00
|
|
|
nfs4_unregister_sysctl();
|
2012-07-16 20:39:13 +00:00
|
|
|
nfs_idmap_quit();
|
2013-06-01 15:50:58 +00:00
|
|
|
nfs_dns_resolver_destroy();
|
2012-07-16 20:39:13 +00:00
|
|
|
}
|
2012-07-30 20:05:25 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
module_init(init_nfs_v4);
|
|
|
|
module_exit(exit_nfs_v4);
|