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" 207d7ea882STrond Myklebust #include "dns_resolve.h" 21f7b422b1SDavid Howells 22f7b422b1SDavid Howells #define NFSDBG_FACILITY NFSDBG_VFS 23f7b422b1SDavid Howells 24f7b422b1SDavid Howells /* 25ef95d31eSTrond Myklebust * Convert the NFSv4 pathname components into a standard posix path. 26ef95d31eSTrond Myklebust * 27ef95d31eSTrond Myklebust * Note that the resulting string will be placed at the end of the buffer 28f7b422b1SDavid Howells */ 29509de811SDavid Howells static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname, 30f7b422b1SDavid Howells char *buffer, ssize_t buflen) 31f7b422b1SDavid Howells { 32f7b422b1SDavid Howells char *end = buffer + buflen; 33f7b422b1SDavid Howells int n; 34f7b422b1SDavid Howells 35f7b422b1SDavid Howells *--end = '\0'; 36f7b422b1SDavid Howells buflen--; 37f7b422b1SDavid Howells 38f7b422b1SDavid Howells n = pathname->ncomponents; 39f7b422b1SDavid Howells while (--n >= 0) { 40509de811SDavid Howells const struct nfs4_string *component = &pathname->components[n]; 41f7b422b1SDavid Howells buflen -= component->len + 1; 42f7b422b1SDavid Howells if (buflen < 0) 43f7b422b1SDavid Howells goto Elong; 44f7b422b1SDavid Howells end -= component->len; 45f7b422b1SDavid Howells memcpy(end, component->data, component->len); 46f7b422b1SDavid Howells *--end = '/'; 47f7b422b1SDavid Howells } 48f7b422b1SDavid Howells return end; 49f7b422b1SDavid Howells Elong: 50f7b422b1SDavid Howells return ERR_PTR(-ENAMETOOLONG); 51f7b422b1SDavid Howells } 52f7b422b1SDavid Howells 5354ceac45SDavid Howells /* 5454ceac45SDavid Howells * Determine the mount path as a string 5554ceac45SDavid Howells */ 5654ceac45SDavid Howells static char *nfs4_path(const struct vfsmount *mnt_parent, 5754ceac45SDavid Howells const struct dentry *dentry, 5854ceac45SDavid Howells char *buffer, ssize_t buflen) 5954ceac45SDavid Howells { 6054ceac45SDavid Howells const char *srvpath; 6154ceac45SDavid Howells 6254ceac45SDavid Howells srvpath = strchr(mnt_parent->mnt_devname, ':'); 6354ceac45SDavid Howells if (srvpath) 6454ceac45SDavid Howells srvpath++; 6554ceac45SDavid Howells else 6654ceac45SDavid Howells srvpath = mnt_parent->mnt_devname; 6754ceac45SDavid Howells 6854ceac45SDavid Howells return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen); 6954ceac45SDavid Howells } 7054ceac45SDavid Howells 7154ceac45SDavid Howells /* 7254ceac45SDavid Howells * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we 7354ceac45SDavid Howells * believe to be the server path to this dentry 7454ceac45SDavid Howells */ 7554ceac45SDavid Howells static int nfs4_validate_fspath(const struct vfsmount *mnt_parent, 7654ceac45SDavid Howells const struct dentry *dentry, 7754ceac45SDavid Howells const struct nfs4_fs_locations *locations, 7854ceac45SDavid Howells char *page, char *page2) 7954ceac45SDavid Howells { 8054ceac45SDavid Howells const char *path, *fs_path; 8154ceac45SDavid Howells 8254ceac45SDavid Howells path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE); 8354ceac45SDavid Howells if (IS_ERR(path)) 8454ceac45SDavid Howells return PTR_ERR(path); 8554ceac45SDavid Howells 8654ceac45SDavid Howells fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE); 8754ceac45SDavid Howells if (IS_ERR(fs_path)) 8854ceac45SDavid Howells return PTR_ERR(fs_path); 8954ceac45SDavid Howells 9054ceac45SDavid Howells if (strncmp(path, fs_path, strlen(fs_path)) != 0) { 9154ceac45SDavid Howells dprintk("%s: path %s does not begin with fsroot %s\n", 923110ff80SHarvey Harrison __func__, path, fs_path); 9354ceac45SDavid Howells return -ENOENT; 9454ceac45SDavid Howells } 9554ceac45SDavid Howells 9654ceac45SDavid Howells return 0; 9754ceac45SDavid Howells } 9854ceac45SDavid Howells 997d7ea882STrond Myklebust static size_t nfs_parse_server_name(char *string, size_t len, 1007d7ea882STrond Myklebust struct sockaddr *sa, size_t salen) 1017d7ea882STrond Myklebust { 1027d7ea882STrond Myklebust ssize_t ret; 1037d7ea882STrond Myklebust 1047d7ea882STrond Myklebust ret = rpc_pton(string, len, sa, salen); 1057d7ea882STrond Myklebust if (ret == 0) { 1067d7ea882STrond Myklebust ret = nfs_dns_resolve_name(string, len, sa, salen); 1077d7ea882STrond Myklebust if (ret < 0) 1087d7ea882STrond Myklebust ret = 0; 1097d7ea882STrond Myklebust } 1107d7ea882STrond Myklebust return ret; 1117d7ea882STrond Myklebust } 1127d7ea882STrond Myklebust 1134ada29d5SJ. Bruce Fields static struct vfsmount *try_location(struct nfs_clone_mount *mountdata, 1144ada29d5SJ. Bruce Fields char *page, char *page2, 1154ada29d5SJ. Bruce Fields const struct nfs4_fs_location *location) 1164ada29d5SJ. Bruce Fields { 1174ada29d5SJ. Bruce Fields struct vfsmount *mnt = ERR_PTR(-ENOENT); 1184ada29d5SJ. Bruce Fields char *mnt_path; 119ef95d31eSTrond Myklebust unsigned int maxbuflen; 120460cdbc8SJ. Bruce Fields unsigned int s; 1214ada29d5SJ. Bruce Fields 1224ada29d5SJ. Bruce Fields mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE); 1234ada29d5SJ. Bruce Fields if (IS_ERR(mnt_path)) 1244ada29d5SJ. Bruce Fields return mnt; 1254ada29d5SJ. Bruce Fields mountdata->mnt_path = mnt_path; 126ef95d31eSTrond Myklebust maxbuflen = mnt_path - 1 - page2; 1274ada29d5SJ. Bruce Fields 128460cdbc8SJ. Bruce Fields for (s = 0; s < location->nservers; s++) { 129ea31a443SJ. Bruce Fields const struct nfs4_string *buf = &location->servers[s]; 130ea31a443SJ. Bruce Fields struct sockaddr_storage addr; 1314ada29d5SJ. Bruce Fields 132ef95d31eSTrond Myklebust if (buf->len <= 0 || buf->len >= maxbuflen) 1334ada29d5SJ. Bruce Fields continue; 1344ada29d5SJ. Bruce Fields 1354ada29d5SJ. Bruce Fields mountdata->addr = (struct sockaddr *)&addr; 136ea31a443SJ. Bruce Fields 137ea31a443SJ. Bruce Fields if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len)) 138ea31a443SJ. Bruce Fields continue; 1397d7ea882STrond Myklebust mountdata->addrlen = nfs_parse_server_name(buf->data, 1407d7ea882STrond Myklebust buf->len, 14153a0b9c4SChuck Lever mountdata->addr, mountdata->addrlen); 14253a0b9c4SChuck Lever if (mountdata->addrlen == 0) 143ea31a443SJ. Bruce Fields continue; 144ec6ee612SChuck Lever rpc_set_port(mountdata->addr, NFS_PORT); 145ea31a443SJ. Bruce Fields 146ef95d31eSTrond Myklebust memcpy(page2, buf->data, buf->len); 147ef95d31eSTrond Myklebust page2[buf->len] = '\0'; 148ea31a443SJ. Bruce Fields mountdata->hostname = page2; 1494ada29d5SJ. Bruce Fields 1504ada29d5SJ. Bruce Fields snprintf(page, PAGE_SIZE, "%s:%s", 1514ada29d5SJ. Bruce Fields mountdata->hostname, 1524ada29d5SJ. Bruce Fields mountdata->mnt_path); 1534ada29d5SJ. Bruce Fields 1544ada29d5SJ. Bruce Fields mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata); 1554ada29d5SJ. Bruce Fields if (!IS_ERR(mnt)) 1564ada29d5SJ. Bruce Fields break; 1574ada29d5SJ. Bruce Fields } 1584ada29d5SJ. Bruce Fields return mnt; 1594ada29d5SJ. Bruce Fields } 1604ada29d5SJ. Bruce Fields 161f7b422b1SDavid Howells /** 162f7b422b1SDavid Howells * nfs_follow_referral - set up mountpoint when hitting a referral on moved error 163f7b422b1SDavid Howells * @mnt_parent - mountpoint of parent directory 164f7b422b1SDavid Howells * @dentry - parent directory 1653f43c666SChuck Lever * @locations - array of NFSv4 server location information 166f7b422b1SDavid Howells * 167f7b422b1SDavid Howells */ 168f7b422b1SDavid Howells static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent, 169f7b422b1SDavid Howells const struct dentry *dentry, 170509de811SDavid Howells const struct nfs4_fs_locations *locations) 171f7b422b1SDavid Howells { 172f7b422b1SDavid Howells struct vfsmount *mnt = ERR_PTR(-ENOENT); 173f7b422b1SDavid Howells struct nfs_clone_mount mountdata = { 174f7b422b1SDavid Howells .sb = mnt_parent->mnt_sb, 175f7b422b1SDavid Howells .dentry = dentry, 176f7b422b1SDavid Howells .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor, 177f7b422b1SDavid Howells }; 17854ceac45SDavid Howells char *page = NULL, *page2 = NULL; 1793f43c666SChuck Lever int loc, error; 180f7b422b1SDavid Howells 181f7b422b1SDavid Howells if (locations == NULL || locations->nlocations <= 0) 182f7b422b1SDavid Howells goto out; 183f7b422b1SDavid Howells 1843110ff80SHarvey Harrison dprintk("%s: referral at %s/%s\n", __func__, 185f7b422b1SDavid Howells dentry->d_parent->d_name.name, dentry->d_name.name); 186f7b422b1SDavid Howells 187f7b422b1SDavid Howells page = (char *) __get_free_page(GFP_USER); 18854ceac45SDavid Howells if (!page) 189f7b422b1SDavid Howells goto out; 19054ceac45SDavid Howells 191f7b422b1SDavid Howells page2 = (char *) __get_free_page(GFP_USER); 19254ceac45SDavid Howells if (!page2) 193f7b422b1SDavid Howells goto out; 194f7b422b1SDavid Howells 19554ceac45SDavid Howells /* Ensure fs path is a prefix of current dentry path */ 19654ceac45SDavid Howells error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2); 19754ceac45SDavid Howells if (error < 0) { 19854ceac45SDavid Howells mnt = ERR_PTR(error); 19954ceac45SDavid Howells goto out; 200f7b422b1SDavid Howells } 201f7b422b1SDavid Howells 202460cdbc8SJ. Bruce Fields for (loc = 0; loc < locations->nlocations; loc++) { 203509de811SDavid Howells const struct nfs4_fs_location *location = &locations->locations[loc]; 204f7b422b1SDavid Howells 205f7b422b1SDavid Howells if (location == NULL || location->nservers <= 0 || 206460cdbc8SJ. Bruce Fields location->rootpath.ncomponents == 0) 207f7b422b1SDavid Howells continue; 208f7b422b1SDavid Howells 2094ada29d5SJ. Bruce Fields mnt = try_location(&mountdata, page, page2, location); 2104ada29d5SJ. Bruce Fields if (!IS_ERR(mnt)) 211f7b422b1SDavid Howells break; 212f7b422b1SDavid Howells } 213f7b422b1SDavid Howells 21454ceac45SDavid Howells out: 215f7b422b1SDavid Howells free_page((unsigned long) page); 216f7b422b1SDavid Howells free_page((unsigned long) page2); 2173110ff80SHarvey Harrison dprintk("%s: done\n", __func__); 218f7b422b1SDavid Howells return mnt; 219f7b422b1SDavid Howells } 220f7b422b1SDavid Howells 221f7b422b1SDavid Howells /* 222f7b422b1SDavid Howells * nfs_do_refmount - handle crossing a referral on server 223f7b422b1SDavid Howells * @dentry - dentry of referral 224f7b422b1SDavid Howells * @nd - nameidata info 225f7b422b1SDavid Howells * 226f7b422b1SDavid Howells */ 227f7b422b1SDavid Howells struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry) 228f7b422b1SDavid Howells { 22954ceac45SDavid Howells struct vfsmount *mnt = ERR_PTR(-ENOMEM); 230f7b422b1SDavid Howells struct dentry *parent; 231f7b422b1SDavid Howells struct nfs4_fs_locations *fs_locations = NULL; 232f7b422b1SDavid Howells struct page *page; 233f7b422b1SDavid Howells int err; 234f7b422b1SDavid Howells 235f7b422b1SDavid Howells /* BUG_ON(IS_ROOT(dentry)); */ 2363110ff80SHarvey Harrison dprintk("%s: enter\n", __func__); 237f7b422b1SDavid Howells 238f7b422b1SDavid Howells page = alloc_page(GFP_KERNEL); 239f7b422b1SDavid Howells if (page == NULL) 240f7b422b1SDavid Howells goto out; 241f7b422b1SDavid Howells 242f7b422b1SDavid Howells fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); 243f7b422b1SDavid Howells if (fs_locations == NULL) 244f7b422b1SDavid Howells goto out_free; 245f7b422b1SDavid Howells 246f7b422b1SDavid Howells /* Get locations */ 24754ceac45SDavid Howells mnt = ERR_PTR(-ENOENT); 24854ceac45SDavid Howells 249f7b422b1SDavid Howells parent = dget_parent(dentry); 25054ceac45SDavid Howells dprintk("%s: getting locations for %s/%s\n", 2513110ff80SHarvey Harrison __func__, parent->d_name.name, dentry->d_name.name); 25254ceac45SDavid Howells 253c228fd3aSTrond Myklebust err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page); 254f7b422b1SDavid Howells dput(parent); 25554ceac45SDavid Howells if (err != 0 || 25654ceac45SDavid Howells fs_locations->nlocations <= 0 || 257f7b422b1SDavid Howells fs_locations->fs_path.ncomponents <= 0) 258f7b422b1SDavid Howells goto out_free; 259f7b422b1SDavid Howells 260f7b422b1SDavid Howells mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations); 261f7b422b1SDavid Howells out_free: 262f7b422b1SDavid Howells __free_page(page); 263f7b422b1SDavid Howells kfree(fs_locations); 264f7b422b1SDavid Howells out: 2653110ff80SHarvey Harrison dprintk("%s: done\n", __func__); 266f7b422b1SDavid Howells return mnt; 267f7b422b1SDavid Howells } 268