Commit 454e2398 authored by David Howells's avatar David Howells Committed by Linus Torvalds
Browse files

[PATCH] VFS: Permit filesystem to override root dentry on mount



Extend the get_sb() filesystem operation to take an extra argument that
permits the VFS to pass in the target vfsmount that defines the mountpoint.

The filesystem is then required to manually set the superblock and root dentry
pointers.  For most filesystems, this should be done with simple_set_mnt()
which will set the superblock pointer and then set the root dentry to the
superblock's s_root (as per the old default behaviour).

The get_sb() op now returns an integer as there's now no need to return the
superblock pointer.

This patch permits a superblock to be implicitly shared amongst several mount
points, such as can be done with NFS to avoid potential inode aliasing.  In
such a case, simple_set_mnt() would not be called, and instead the mnt_root
and mnt_sb would be set directly.

The patch also makes the following changes:

 (*) the get_sb_*() convenience functions in the core kernel now take a vfsmount
     pointer argument and return an integer, so most filesystems have to change
     very little.

 (*) If one of the convenience function is not used, then get_sb() should
     normally call simple_set_mnt() to instantiate the vfsmount. This will
     always return 0, and so can be tail-called from get_sb().

 (*) generic_shutdown_super() now calls shrink_dcache_sb() to clean up the
     dcache upon superblock destruction rather than shrink_dcache_anon().

     This is required because the superblock may now have multiple trees that
     aren't actually bound to s_root, but that still need to be cleaned up. The
     currently called functions assume that the whole tree is rooted at s_root,
     and that anonymous dentries are not the roots of trees which results in
     dentries being left unculled.

     However, with the way NFS superblock sharing are currently set to be
     implemented, these assumptions are violated: the root of the filesystem is
     simply a dummy dentry and inode (the real inode for '/' may well be
     inaccessible), and all the vfsmounts are rooted on anonymous[*] dentries
     with child trees.

     [*] Anonymous until discovered from another tree.

 (*) The documentation has been adjusted, including the additional bit of
     changing ext2_* into foo_* in the documentation.

[akpm@osdl.org: convert ipath_fs, do other stuff]
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Acked-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Cc: Nathan Scott <nathans@sgi.com>
Cc: Roland Dreier <rolandd@cisco.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 1ad55440
......@@ -769,11 +769,11 @@ static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
return(err);
}
static struct super_block *hppfs_read_super(struct file_system_type *type,
int flags, const char *dev_name,
void *data)
static int hppfs_read_super(struct file_system_type *type,
int flags, const char *dev_name,
void *data, struct vfsmount *mnt)
{
return(get_sb_nodev(type, flags, data, hppfs_fill_super));
return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
}
static struct file_system_type hppfs_type = {
......
......@@ -723,10 +723,10 @@ void hugetlb_put_quota(struct address_space *mapping)
}
}
static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int hugetlbfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt);
}
static struct file_system_type hugetlbfs_fs_type = {
......
......@@ -672,11 +672,11 @@ out:
return ret;
}
static struct super_block *
static int
inotify_get_sb(struct file_system_type *fs_type, int flags,
const char *dev_name, void *data)
const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_pseudo(fs_type, "inotify", NULL, 0xBAD1DEA);
return get_sb_pseudo(fs_type, "inotify", NULL, 0xBAD1DEA, mnt);
}
static struct file_system_type inotify_fs_type = {
......
......@@ -1399,10 +1399,11 @@ struct inode *isofs_iget(struct super_block *sb,
return inode;
}
static struct super_block *isofs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int isofs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, isofs_fill_super);
return get_sb_bdev(fs_type, flags, dev_name, data, isofs_fill_super,
mnt);
}
static struct file_system_type iso9660_fs_type = {
......
......@@ -1785,10 +1785,11 @@ static struct super_operations jffs_ops =
.remount_fs = jffs_remount,
};
static struct super_block *jffs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int jffs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, jffs_fill_super);
return get_sb_bdev(fs_type, flags, dev_name, data, jffs_fill_super,
mnt);
}
static struct file_system_type jffs_fs_type = {
......
......@@ -111,9 +111,10 @@ static int jffs2_sb_set(struct super_block *sb, void *data)
return 0;
}
static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data, struct mtd_info *mtd)
static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data, struct mtd_info *mtd,
struct vfsmount *mnt)
{
struct super_block *sb;
struct jffs2_sb_info *c;
......@@ -121,19 +122,20 @@ static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
c = kmalloc(sizeof(*c), GFP_KERNEL);
if (!c)
return ERR_PTR(-ENOMEM);
return -ENOMEM;
memset(c, 0, sizeof(*c));
c->mtd = mtd;
sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
if (IS_ERR(sb))
goto out_put;
goto out_error;
if (sb->s_root) {
/* New mountpoint for JFFS2 which is already mounted */
D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
mtd->index, mtd->name));
ret = simple_set_mnt(mnt, sb);
goto out_put;
}
......@@ -161,44 +163,47 @@ static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
/* Failure case... */
up_write(&sb->s_umount);
deactivate_super(sb);
return ERR_PTR(ret);
return ret;
}
sb->s_flags |= MS_ACTIVE;
return sb;
return simple_set_mnt(mnt, sb);
out_error:
ret = PTR_ERR(sb);
out_put:
kfree(c);
put_mtd_device(mtd);
return sb;
return ret;
}
static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data, int mtdnr)
static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data, int mtdnr,
struct vfsmount *mnt)
{
struct mtd_info *mtd;
mtd = get_mtd_device(NULL, mtdnr);
if (!mtd) {
D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
return ERR_PTR(-EINVAL);
return -EINVAL;
}
return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
}
static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data)
static int jffs2_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data, struct vfsmount *mnt)
{
int err;
struct nameidata nd;
int mtdnr;
if (!dev_name)
return ERR_PTR(-EINVAL);
return -EINVAL;
D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
......@@ -220,7 +225,7 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
mtd = get_mtd_device(NULL, mtdnr);
if (mtd) {
if (!strcmp(mtd->name, dev_name+4))
return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
put_mtd_device(mtd);
}
}
......@@ -233,7 +238,7 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
if (!*endptr) {
/* It was a valid number */
D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
}
}
}
......@@ -247,7 +252,7 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
err, nd.dentry->d_inode));
if (err)
return ERR_PTR(err);
return err;
err = -EINVAL;
......@@ -269,11 +274,11 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
mtdnr = iminor(nd.dentry->d_inode);
path_release(&nd);
return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
out:
path_release(&nd);
return ERR_PTR(err);
return err;
}
static void jffs2_put_super (struct super_block *sb)
......
......@@ -565,10 +565,11 @@ static void jfs_unlockfs(struct super_block *sb)
}
}
static struct super_block *jfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int jfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
return get_sb_bdev(fs_type, flags, dev_name, data, jfs_fill_super,
mnt);
}
static int jfs_sync_fs(struct super_block *sb, int wait)
......
......@@ -196,9 +196,9 @@ struct inode_operations simple_dir_inode_operations = {
* Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
* will never be mountable)
*/
struct super_block *
get_sb_pseudo(struct file_system_type *fs_type, char *name,
struct super_operations *ops, unsigned long magic)
int get_sb_pseudo(struct file_system_type *fs_type, char *name,
struct super_operations *ops, unsigned long magic,
struct vfsmount *mnt)
{
struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
static struct super_operations default_ops = {.statfs = simple_statfs};
......@@ -207,7 +207,7 @@ get_sb_pseudo(struct file_system_type *fs_type, char *name,
struct qstr d_name = {.name = name, .len = strlen(name)};
if (IS_ERR(s))
return s;
return PTR_ERR(s);
s->s_flags = MS_NOUSER;
s->s_maxbytes = ~0ULL;
......@@ -232,12 +232,12 @@ get_sb_pseudo(struct file_system_type *fs_type, char *name,
d_instantiate(dentry, root);
s->s_root = dentry;
s->s_flags |= MS_ACTIVE;
return s;
return simple_set_mnt(mnt, s);
Enomem:
up_write(&s->s_umount);
deactivate_super(s);
return ERR_PTR(-ENOMEM);
return -ENOMEM;
}
int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
......
......@@ -559,10 +559,11 @@ void minix_truncate(struct inode * inode)
V2_minix_truncate(inode);
}
static struct super_block *minix_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int minix_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, minix_fill_super);
return get_sb_bdev(fs_type, flags, dev_name, data, minix_fill_super,
mnt);
}
static struct file_system_type minix_fs_type = {
......
......@@ -661,11 +661,12 @@ static int msdos_fill_super(struct super_block *sb, void *data, int silent)
return 0;
}
static struct super_block *msdos_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data)
static int msdos_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, msdos_fill_super);
return get_sb_bdev(fs_type, flags, dev_name, data, msdos_fill_super,
mnt);
}
static struct file_system_type msdos_fs_type = {
......
......@@ -86,6 +86,15 @@ struct vfsmount *alloc_vfsmnt(const char *name)
return mnt;
}
int simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
{
mnt->mnt_sb = sb;
mnt->mnt_root = dget(sb->s_root);
return 0;
}
EXPORT_SYMBOL(simple_set_mnt);
void free_vfsmnt(struct vfsmount *mnt)
{
kfree(mnt->mnt_devname);
......
......@@ -957,10 +957,10 @@ out:
return result;
}
static struct super_block *ncp_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int ncp_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_nodev(fs_type, flags, data, ncp_fill_super);
return get_sb_nodev(fs_type, flags, data, ncp_fill_super, mnt);
}
static struct file_system_type ncp_fs_type = {
......
......@@ -1690,8 +1690,8 @@ static int nfs_compare_super(struct super_block *sb, void *data)
return !nfs_compare_fh(&old->fh, &server->fh);
}
static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *raw_data)
static int nfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
{
int error;
struct nfs_server *server = NULL;
......@@ -1699,14 +1699,14 @@ static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
struct nfs_fh *root;
struct nfs_mount_data *data = raw_data;
s = ERR_PTR(-EINVAL);
error = -EINVAL;
if (data == NULL) {
dprintk("%s: missing data argument\n", __FUNCTION__);
goto out_err;
goto out_err_noserver;
}
if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
dprintk("%s: bad mount version\n", __FUNCTION__);
goto out_err;
goto out_err_noserver;
}
switch (data->version) {
case 1:
......@@ -1718,7 +1718,7 @@ static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
dprintk("%s: mount structure version %d does not support NFSv3\n",
__FUNCTION__,
data->version);
goto out_err;
goto out_err_noserver;
}
data->root.size = NFS2_FHSIZE;
memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
......@@ -1727,24 +1727,24 @@ static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
dprintk("%s: mount structure version %d does not support strong security\n",
__FUNCTION__,
data->version);
goto out_err;
goto out_err_noserver;
}
case 5:
memset(data->context, 0, sizeof(data->context));
}
#ifndef CONFIG_NFS_V3
/* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
s = ERR_PTR(-EPROTONOSUPPORT);
error = -EPROTONOSUPPORT;
if (data->flags & NFS_MOUNT_VER3) {
dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
goto out_err;
goto out_err_noserver;
}
#endif /* CONFIG_NFS_V3 */
s = ERR_PTR(-ENOMEM);
error = -ENOMEM;
server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
if (!server)
goto out_err;
goto out_err_noserver;
/* Zero out the NFS state stuff */
init_nfsv4_state(server);
server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
......@@ -1754,7 +1754,7 @@ static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
root->size = data->root.size;
else
root->size = NFS2_FHSIZE;
s = ERR_PTR(-EINVAL);
error = -EINVAL;
if (root->size > sizeof(root->data)) {
dprintk("%s: invalid root filehandle\n", __FUNCTION__);
goto out_err;
......@@ -1770,15 +1770,20 @@ static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
}
/* Fire up rpciod if not yet running */
s = ERR_PTR(rpciod_up());
if (IS_ERR(s)) {
dprintk("%s: couldn't start rpciod! Error = %ld\n",
__FUNCTION__, PTR_ERR(s));
error = rpciod_up();
if (error < 0) {
dprintk("%s: couldn't start rpciod! Error = %d\n",
__FUNCTION__, error);
goto out_err;
}
s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
if (IS_ERR(s) || s->s_root)
if (IS_ERR(s)) {
error = PTR_ERR(s);
goto out_err_rpciod;
}
if (s->s_root)
goto out_rpciod_down;
s->s_flags = flags;
......@@ -1787,15 +1792,22 @@ static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
if (error) {
up_write(&s->s_umount);
deactivate_super(s);
return ERR_PTR(error);
return error;
}
s->s_flags |= MS_ACTIVE;
return s;
return simple_set_mnt(mnt, s);
out_rpciod_down:
rpciod_down();
kfree(server);
return simple_set_mnt(mnt, s);
out_err_rpciod:
rpciod_down();
out_err:
kfree(server);
return s;
out_err_noserver:
return error;
}
static void nfs_kill_super(struct super_block *s)
......@@ -2032,8 +2044,8 @@ nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
return dst;
}
static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *raw_data)
static int nfs4_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
{
int error;
struct nfs_server *server;
......@@ -2043,16 +2055,16 @@ static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
if (data == NULL) {
dprintk("%s: missing data argument\n", __FUNCTION__);
return ERR_PTR(-EINVAL);
return -EINVAL;
}
if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
dprintk("%s: bad mount version\n", __FUNCTION__);
return ERR_PTR(-EINVAL);
return -EINVAL;
}
server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
if (!server)
return ERR_PTR(-ENOMEM);
return -ENOMEM;
/* Zero out the NFS state stuff */
init_nfsv4_state(server);
server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
......@@ -2074,33 +2086,41 @@ static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
/* We now require that the mount process passes the remote address */
if (data->host_addrlen != sizeof(server->addr)) {
s = ERR_PTR(-EINVAL);
error = -EINVAL;
goto out_free;
}
if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
s = ERR_PTR(-EFAULT);
error = -EFAULT;
goto out_free;
}
if (server->addr.sin_family != AF_INET ||
server->addr.sin_addr.s_addr == INADDR_ANY) {
dprintk("%s: mount program didn't pass remote IP address!\n",
__FUNCTION__);
s = ERR_PTR(-EINVAL);
error = -EINVAL;
goto out_free;
}
/* Fire up rpciod if not yet running */
s = ERR_PTR(rpciod_up());
if (IS_ERR(s)) {
dprintk("%s: couldn't start rpciod! Error = %ld\n",
__FUNCTION__, PTR_ERR(s));
error = rpciod_up();
if (error < 0) {
dprintk("%s: couldn't start rpciod! Error = %d\n",
__FUNCTION__, error);
goto out_free;
}
s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
if (IS_ERR(s) || s->s_root)
if (IS_ERR(s)) {
error = PTR_ERR(s);
goto out_free;
}
if (s->s_root) {
kfree(server->mnt_path);
kfree(server->hostname);
kfree(server);
return simple_set_mnt(mnt, s);
}
s->s_flags = flags;
......@@ -2108,17 +2128,17 @@ static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
if (error) {
up_write(&s->s_umount);
deactivate_super(s);
return ERR_PTR(error);
return error;
}
s->s_flags |= MS_ACTIVE;
return s;
return simple_set_mnt(mnt, s);
out_err:
s = (struct super_block *)p;
error = PTR_ERR(p);
out_free:
kfree(server->mnt_path);
kfree(server->hostname);
kfree(server);
return s;
return error;
}
static void nfs4_kill_super(struct super_block *sb)
......
......@@ -494,10 +494,10 @@ static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
return simple_fill_super(sb, 0x6e667364, nfsd_files);
}
static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int nfsd_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_single(fs_type, flags, data, nfsd_fill_super);
return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
}
static struct file_system_type nfsd_fs_type = {
......
......@@ -3093,10 +3093,11 @@ struct kmem_cache *ntfs_index_ctx_cache;
/* Driver wide mutex. */
DEFINE_MUTEX(ntfs_lock);
static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int ntfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
mnt);
}
static struct file_system_type ntfs_fs_type = {
......
......@@ -574,10 +574,10 @@ static struct inode_operations dlmfs_file_inode_operations = {
.getattr = simple_getattr,
};
static struct super_block *dlmfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
static int dlmfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{