1f7b422b1SDavid Howells /* 2f7b422b1SDavid Howells * linux/fs/nfs/nfs4namespace.c 3f7b422b1SDavid Howells * 4f7b422b1SDavid Howells * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com> 554ceac45SDavid Howells * - Modified by David Howells <dhowells@redhat.com> 6f7b422b1SDavid Howells * 7f7b422b1SDavid Howells * NFSv4 namespace 8f7b422b1SDavid Howells */ 9f7b422b1SDavid Howells 10f7b422b1SDavid Howells #include <linux/dcache.h> 11f7b422b1SDavid Howells #include <linux/mount.h> 12f7b422b1SDavid Howells #include <linux/namei.h> 13f7b422b1SDavid Howells #include <linux/nfs_fs.h> 14f7b422b1SDavid Howells #include <linux/string.h> 15f7b422b1SDavid Howells #include <linux/sunrpc/clnt.h> 16f7b422b1SDavid Howells #include <linux/vfs.h> 17f7b422b1SDavid Howells #include <linux/inet.h> 18f7b422b1SDavid Howells #include "internal.h" 19c228fd3aSTrond Myklebust #include "nfs4_fs.h" 20f7b422b1SDavid Howells 21f7b422b1SDavid Howells #define NFSDBG_FACILITY NFSDBG_VFS 22f7b422b1SDavid Howells 23f7b422b1SDavid Howells /* 24f7b422b1SDavid Howells * Check if fs_root is valid 25f7b422b1SDavid Howells */ 26509de811SDavid Howells static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname, 27f7b422b1SDavid Howells char *buffer, ssize_t buflen) 28f7b422b1SDavid Howells { 29f7b422b1SDavid Howells char *end = buffer + buflen; 30f7b422b1SDavid Howells int n; 31f7b422b1SDavid Howells 32f7b422b1SDavid Howells *--end = '\0'; 33f7b422b1SDavid Howells buflen--; 34f7b422b1SDavid Howells 35f7b422b1SDavid Howells n = pathname->ncomponents; 36f7b422b1SDavid Howells while (--n >= 0) { 37509de811SDavid Howells const struct nfs4_string *component = &pathname->components[n]; 38f7b422b1SDavid Howells buflen -= component->len + 1; 39f7b422b1SDavid Howells if (buflen < 0) 40f7b422b1SDavid Howells goto Elong; 41f7b422b1SDavid Howells end -= component->len; 42f7b422b1SDavid Howells memcpy(end, component->data, component->len); 43f7b422b1SDavid Howells *--end = '/'; 44f7b422b1SDavid Howells } 45f7b422b1SDavid Howells return end; 46f7b422b1SDavid Howells Elong: 47f7b422b1SDavid Howells return ERR_PTR(-ENAMETOOLONG); 48f7b422b1SDavid Howells } 49f7b422b1SDavid Howells 5054ceac45SDavid Howells /* 5154ceac45SDavid Howells * Determine the mount path as a string 5254ceac45SDavid Howells */ 5354ceac45SDavid Howells static char *nfs4_path(const struct vfsmount *mnt_parent, 5454ceac45SDavid Howells const struct dentry *dentry, 5554ceac45SDavid Howells char *buffer, ssize_t buflen) 5654ceac45SDavid Howells { 5754ceac45SDavid Howells const char *srvpath; 5854ceac45SDavid Howells 5954ceac45SDavid Howells srvpath = strchr(mnt_parent->mnt_devname, ':'); 6054ceac45SDavid Howells if (srvpath) 6154ceac45SDavid Howells srvpath++; 6254ceac45SDavid Howells else 6354ceac45SDavid Howells srvpath = mnt_parent->mnt_devname; 6454ceac45SDavid Howells 6554ceac45SDavid Howells return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen); 6654ceac45SDavid Howells } 6754ceac45SDavid Howells 6854ceac45SDavid Howells /* 6954ceac45SDavid Howells * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we 7054ceac45SDavid Howells * believe to be the server path to this dentry 7154ceac45SDavid Howells */ 7254ceac45SDavid Howells static int nfs4_validate_fspath(const struct vfsmount *mnt_parent, 7354ceac45SDavid Howells const struct dentry *dentry, 7454ceac45SDavid Howells const struct nfs4_fs_locations *locations, 7554ceac45SDavid Howells char *page, char *page2) 7654ceac45SDavid Howells { 7754ceac45SDavid Howells const char *path, *fs_path; 7854ceac45SDavid Howells 7954ceac45SDavid Howells path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE); 8054ceac45SDavid Howells if (IS_ERR(path)) 8154ceac45SDavid Howells return PTR_ERR(path); 8254ceac45SDavid Howells 8354ceac45SDavid Howells fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE); 8454ceac45SDavid Howells if (IS_ERR(fs_path)) 8554ceac45SDavid Howells return PTR_ERR(fs_path); 8654ceac45SDavid Howells 8754ceac45SDavid Howells if (strncmp(path, fs_path, strlen(fs_path)) != 0) { 8854ceac45SDavid Howells dprintk("%s: path %s does not begin with fsroot %s\n", 893110ff80SHarvey Harrison __func__, path, fs_path); 9054ceac45SDavid Howells return -ENOENT; 9154ceac45SDavid Howells } 9254ceac45SDavid Howells 9354ceac45SDavid Howells return 0; 9454ceac45SDavid Howells } 9554ceac45SDavid Howells 964ada29d5SJ. Bruce Fields static struct vfsmount *try_location(struct nfs_clone_mount *mountdata, 974ada29d5SJ. Bruce Fields char *page, char *page2, 984ada29d5SJ. Bruce Fields const struct nfs4_fs_location *location) 994ada29d5SJ. Bruce Fields { 1004ada29d5SJ. Bruce Fields struct vfsmount *mnt = ERR_PTR(-ENOENT); 1014ada29d5SJ. Bruce Fields char *mnt_path; 102ea31a443SJ. Bruce Fields int page2len; 103460cdbc8SJ. Bruce Fields unsigned int s; 1044ada29d5SJ. Bruce Fields 1054ada29d5SJ. Bruce Fields mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE); 1064ada29d5SJ. Bruce Fields if (IS_ERR(mnt_path)) 1074ada29d5SJ. Bruce Fields return mnt; 1084ada29d5SJ. Bruce Fields mountdata->mnt_path = mnt_path; 109ea31a443SJ. Bruce Fields page2 += strlen(mnt_path) + 1; 110ea31a443SJ. Bruce Fields page2len = PAGE_SIZE - strlen(mnt_path) - 1; 1114ada29d5SJ. Bruce Fields 112460cdbc8SJ. Bruce Fields for (s = 0; s < location->nservers; s++) { 113ea31a443SJ. Bruce Fields const struct nfs4_string *buf = &location->servers[s]; 114ea31a443SJ. Bruce Fields struct sockaddr_storage addr; 1154ada29d5SJ. Bruce Fields 116ea31a443SJ. Bruce Fields if (buf->len <= 0 || buf->len >= PAGE_SIZE) 1174ada29d5SJ. Bruce Fields continue; 1184ada29d5SJ. Bruce Fields 1194ada29d5SJ. Bruce Fields mountdata->addr = (struct sockaddr *)&addr; 120ea31a443SJ. Bruce Fields 121ea31a443SJ. Bruce Fields if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len)) 122ea31a443SJ. Bruce Fields continue; 123ea31a443SJ. Bruce Fields nfs_parse_ip_address(buf->data, buf->len, 124ea31a443SJ. Bruce Fields mountdata->addr, &mountdata->addrlen); 125ea31a443SJ. Bruce Fields if (mountdata->addr->sa_family == AF_UNSPEC) 126ea31a443SJ. Bruce Fields continue; 127ea31a443SJ. Bruce Fields nfs_set_port(mountdata->addr, NFS_PORT); 128ea31a443SJ. Bruce Fields 129ea31a443SJ. Bruce Fields strncpy(page2, buf->data, page2len); 130ea31a443SJ. Bruce Fields page2[page2len] = '\0'; 131ea31a443SJ. Bruce Fields mountdata->hostname = page2; 1324ada29d5SJ. Bruce Fields 1334ada29d5SJ. Bruce Fields snprintf(page, PAGE_SIZE, "%s:%s", 1344ada29d5SJ. Bruce Fields mountdata->hostname, 1354ada29d5SJ. Bruce Fields mountdata->mnt_path); 1364ada29d5SJ. Bruce Fields 1374ada29d5SJ. Bruce Fields mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata); 1384ada29d5SJ. Bruce Fields if (!IS_ERR(mnt)) 1394ada29d5SJ. Bruce Fields break; 1404ada29d5SJ. Bruce Fields } 1414ada29d5SJ. Bruce Fields return mnt; 1424ada29d5SJ. Bruce Fields } 1434ada29d5SJ. Bruce Fields 144f7b422b1SDavid Howells /** 145f7b422b1SDavid Howells * nfs_follow_referral - set up mountpoint when hitting a referral on moved error 146f7b422b1SDavid Howells * @mnt_parent - mountpoint of parent directory 147f7b422b1SDavid Howells * @dentry - parent directory 1483f43c666SChuck Lever * @locations - array of NFSv4 server location information 149f7b422b1SDavid Howells * 150f7b422b1SDavid Howells */ 151f7b422b1SDavid Howells static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent, 152f7b422b1SDavid Howells const struct dentry *dentry, 153509de811SDavid Howells const struct nfs4_fs_locations *locations) 154f7b422b1SDavid Howells { 155f7b422b1SDavid Howells struct vfsmount *mnt = ERR_PTR(-ENOENT); 156f7b422b1SDavid Howells struct nfs_clone_mount mountdata = { 157f7b422b1SDavid Howells .sb = mnt_parent->mnt_sb, 158f7b422b1SDavid Howells .dentry = dentry, 159f7b422b1SDavid Howells .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor, 160f7b422b1SDavid Howells }; 16154ceac45SDavid Howells char *page = NULL, *page2 = NULL; 1623f43c666SChuck Lever int loc, error; 163f7b422b1SDavid Howells 164f7b422b1SDavid Howells if (locations == NULL || locations->nlocations <= 0) 165f7b422b1SDavid Howells goto out; 166f7b422b1SDavid Howells 1673110ff80SHarvey Harrison dprintk("%s: referral at %s/%s\n", __func__, 168f7b422b1SDavid Howells dentry->d_parent->d_name.name, dentry->d_name.name); 169f7b422b1SDavid Howells 170f7b422b1SDavid Howells page = (char *) __get_free_page(GFP_USER); 17154ceac45SDavid Howells if (!page) 172f7b422b1SDavid Howells goto out; 17354ceac45SDavid Howells 174f7b422b1SDavid Howells page2 = (char *) __get_free_page(GFP_USER); 17554ceac45SDavid Howells if (!page2) 176f7b422b1SDavid Howells goto out; 177f7b422b1SDavid Howells 17854ceac45SDavid Howells /* Ensure fs path is a prefix of current dentry path */ 17954ceac45SDavid Howells error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2); 18054ceac45SDavid Howells if (error < 0) { 18154ceac45SDavid Howells mnt = ERR_PTR(error); 18254ceac45SDavid Howells goto out; 183f7b422b1SDavid Howells } 184f7b422b1SDavid Howells 185460cdbc8SJ. Bruce Fields for (loc = 0; loc < locations->nlocations; loc++) { 186509de811SDavid Howells const struct nfs4_fs_location *location = &locations->locations[loc]; 187f7b422b1SDavid Howells 188f7b422b1SDavid Howells if (location == NULL || location->nservers <= 0 || 189460cdbc8SJ. Bruce Fields location->rootpath.ncomponents == 0) 190f7b422b1SDavid Howells continue; 191f7b422b1SDavid Howells 1924ada29d5SJ. Bruce Fields mnt = try_location(&mountdata, page, page2, location); 1934ada29d5SJ. Bruce Fields if (!IS_ERR(mnt)) 194f7b422b1SDavid Howells break; 195f7b422b1SDavid Howells } 196f7b422b1SDavid Howells 19754ceac45SDavid Howells out: 198f7b422b1SDavid Howells free_page((unsigned long) page); 199f7b422b1SDavid Howells free_page((unsigned long) page2); 2003110ff80SHarvey Harrison dprintk("%s: done\n", __func__); 201f7b422b1SDavid Howells return mnt; 202f7b422b1SDavid Howells } 203f7b422b1SDavid Howells 204f7b422b1SDavid Howells /* 205f7b422b1SDavid Howells * nfs_do_refmount - handle crossing a referral on server 206f7b422b1SDavid Howells * @dentry - dentry of referral 207f7b422b1SDavid Howells * @nd - nameidata info 208f7b422b1SDavid Howells * 209f7b422b1SDavid Howells */ 210f7b422b1SDavid Howells struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry) 211f7b422b1SDavid Howells { 21254ceac45SDavid Howells struct vfsmount *mnt = ERR_PTR(-ENOMEM); 213f7b422b1SDavid Howells struct dentry *parent; 214f7b422b1SDavid Howells struct nfs4_fs_locations *fs_locations = NULL; 215f7b422b1SDavid Howells struct page *page; 216f7b422b1SDavid Howells int err; 217f7b422b1SDavid Howells 218f7b422b1SDavid Howells /* BUG_ON(IS_ROOT(dentry)); */ 2193110ff80SHarvey Harrison dprintk("%s: enter\n", __func__); 220f7b422b1SDavid Howells 221f7b422b1SDavid Howells page = alloc_page(GFP_KERNEL); 222f7b422b1SDavid Howells if (page == NULL) 223f7b422b1SDavid Howells goto out; 224f7b422b1SDavid Howells 225f7b422b1SDavid Howells fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); 226f7b422b1SDavid Howells if (fs_locations == NULL) 227f7b422b1SDavid Howells goto out_free; 228f7b422b1SDavid Howells 229f7b422b1SDavid Howells /* Get locations */ 23054ceac45SDavid Howells mnt = ERR_PTR(-ENOENT); 23154ceac45SDavid Howells 232f7b422b1SDavid Howells parent = dget_parent(dentry); 23354ceac45SDavid Howells dprintk("%s: getting locations for %s/%s\n", 2343110ff80SHarvey Harrison __func__, parent->d_name.name, dentry->d_name.name); 23554ceac45SDavid Howells 236c228fd3aSTrond Myklebust err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page); 237f7b422b1SDavid Howells dput(parent); 23854ceac45SDavid Howells if (err != 0 || 23954ceac45SDavid Howells fs_locations->nlocations <= 0 || 240f7b422b1SDavid Howells fs_locations->fs_path.ncomponents <= 0) 241f7b422b1SDavid Howells goto out_free; 242f7b422b1SDavid Howells 243f7b422b1SDavid Howells mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations); 244f7b422b1SDavid Howells out_free: 245f7b422b1SDavid Howells __free_page(page); 246f7b422b1SDavid Howells kfree(fs_locations); 247f7b422b1SDavid Howells out: 2483110ff80SHarvey Harrison dprintk("%s: done\n", __func__); 249f7b422b1SDavid Howells return mnt; 250f7b422b1SDavid Howells } 251