David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/nfs4namespace.c |
| 3 | * |
| 4 | * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com> |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 5 | * - Modified by David Howells <dhowells@redhat.com> |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 6 | * |
| 7 | * NFSv4 namespace |
| 8 | */ |
| 9 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 10 | #include <linux/dcache.h> |
| 11 | #include <linux/mount.h> |
| 12 | #include <linux/namei.h> |
| 13 | #include <linux/nfs_fs.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/sunrpc/clnt.h> |
| 16 | #include <linux/vfs.h> |
| 17 | #include <linux/inet.h> |
| 18 | #include "internal.h" |
Trond Myklebust | c228fd3 | 2007-01-13 02:28:11 -0500 | [diff] [blame] | 19 | #include "nfs4_fs.h" |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 20 | |
| 21 | #define NFSDBG_FACILITY NFSDBG_VFS |
| 22 | |
| 23 | /* |
Trond Myklebust | ef95d31 | 2009-03-10 20:33:17 -0400 | [diff] [blame] | 24 | * Convert the NFSv4 pathname components into a standard posix path. |
| 25 | * |
| 26 | * Note that the resulting string will be placed at the end of the buffer |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 27 | */ |
David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 28 | static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname, |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 29 | char *buffer, ssize_t buflen) |
| 30 | { |
| 31 | char *end = buffer + buflen; |
| 32 | int n; |
| 33 | |
| 34 | *--end = '\0'; |
| 35 | buflen--; |
| 36 | |
| 37 | n = pathname->ncomponents; |
| 38 | while (--n >= 0) { |
David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 39 | const struct nfs4_string *component = &pathname->components[n]; |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 40 | buflen -= component->len + 1; |
| 41 | if (buflen < 0) |
| 42 | goto Elong; |
| 43 | end -= component->len; |
| 44 | memcpy(end, component->data, component->len); |
| 45 | *--end = '/'; |
| 46 | } |
| 47 | return end; |
| 48 | Elong: |
| 49 | return ERR_PTR(-ENAMETOOLONG); |
| 50 | } |
| 51 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 52 | /* |
| 53 | * Determine the mount path as a string |
| 54 | */ |
| 55 | static char *nfs4_path(const struct vfsmount *mnt_parent, |
| 56 | const struct dentry *dentry, |
| 57 | char *buffer, ssize_t buflen) |
| 58 | { |
| 59 | const char *srvpath; |
| 60 | |
| 61 | srvpath = strchr(mnt_parent->mnt_devname, ':'); |
| 62 | if (srvpath) |
| 63 | srvpath++; |
| 64 | else |
| 65 | srvpath = mnt_parent->mnt_devname; |
| 66 | |
| 67 | return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we |
| 72 | * believe to be the server path to this dentry |
| 73 | */ |
| 74 | static int nfs4_validate_fspath(const struct vfsmount *mnt_parent, |
| 75 | const struct dentry *dentry, |
| 76 | const struct nfs4_fs_locations *locations, |
| 77 | char *page, char *page2) |
| 78 | { |
| 79 | const char *path, *fs_path; |
| 80 | |
| 81 | path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE); |
| 82 | if (IS_ERR(path)) |
| 83 | return PTR_ERR(path); |
| 84 | |
| 85 | fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE); |
| 86 | if (IS_ERR(fs_path)) |
| 87 | return PTR_ERR(fs_path); |
| 88 | |
| 89 | if (strncmp(path, fs_path, strlen(fs_path)) != 0) { |
| 90 | dprintk("%s: path %s does not begin with fsroot %s\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 91 | __func__, path, fs_path); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 92 | return -ENOENT; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 98 | static struct vfsmount *try_location(struct nfs_clone_mount *mountdata, |
| 99 | char *page, char *page2, |
| 100 | const struct nfs4_fs_location *location) |
| 101 | { |
| 102 | struct vfsmount *mnt = ERR_PTR(-ENOENT); |
| 103 | char *mnt_path; |
Trond Myklebust | ef95d31 | 2009-03-10 20:33:17 -0400 | [diff] [blame] | 104 | unsigned int maxbuflen; |
J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 105 | unsigned int s; |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 106 | |
| 107 | mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE); |
| 108 | if (IS_ERR(mnt_path)) |
| 109 | return mnt; |
| 110 | mountdata->mnt_path = mnt_path; |
Trond Myklebust | ef95d31 | 2009-03-10 20:33:17 -0400 | [diff] [blame] | 111 | maxbuflen = mnt_path - 1 - page2; |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 112 | |
J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 113 | for (s = 0; s < location->nservers; s++) { |
J. Bruce Fields | ea31a443 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 114 | const struct nfs4_string *buf = &location->servers[s]; |
| 115 | struct sockaddr_storage addr; |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 116 | |
Trond Myklebust | ef95d31 | 2009-03-10 20:33:17 -0400 | [diff] [blame] | 117 | if (buf->len <= 0 || buf->len >= maxbuflen) |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 118 | continue; |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 119 | |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 120 | mountdata->addr = (struct sockaddr *)&addr; |
J. Bruce Fields | ea31a443 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 121 | |
| 122 | if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len)) |
| 123 | continue; |
| 124 | nfs_parse_ip_address(buf->data, buf->len, |
| 125 | mountdata->addr, &mountdata->addrlen); |
| 126 | if (mountdata->addr->sa_family == AF_UNSPEC) |
| 127 | continue; |
| 128 | nfs_set_port(mountdata->addr, NFS_PORT); |
| 129 | |
Trond Myklebust | ef95d31 | 2009-03-10 20:33:17 -0400 | [diff] [blame] | 130 | memcpy(page2, buf->data, buf->len); |
| 131 | page2[buf->len] = '\0'; |
J. Bruce Fields | ea31a443 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 132 | mountdata->hostname = page2; |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 133 | |
| 134 | snprintf(page, PAGE_SIZE, "%s:%s", |
| 135 | mountdata->hostname, |
| 136 | mountdata->mnt_path); |
| 137 | |
| 138 | mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata); |
| 139 | if (!IS_ERR(mnt)) |
| 140 | break; |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 141 | } |
| 142 | return mnt; |
| 143 | } |
| 144 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 145 | /** |
| 146 | * nfs_follow_referral - set up mountpoint when hitting a referral on moved error |
| 147 | * @mnt_parent - mountpoint of parent directory |
| 148 | * @dentry - parent directory |
Chuck Lever | 3f43c66 | 2007-12-10 14:57:31 -0500 | [diff] [blame] | 149 | * @locations - array of NFSv4 server location information |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 150 | * |
| 151 | */ |
| 152 | static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent, |
| 153 | const struct dentry *dentry, |
David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 154 | const struct nfs4_fs_locations *locations) |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 155 | { |
| 156 | struct vfsmount *mnt = ERR_PTR(-ENOENT); |
| 157 | struct nfs_clone_mount mountdata = { |
| 158 | .sb = mnt_parent->mnt_sb, |
| 159 | .dentry = dentry, |
| 160 | .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor, |
| 161 | }; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 162 | char *page = NULL, *page2 = NULL; |
Chuck Lever | 3f43c66 | 2007-12-10 14:57:31 -0500 | [diff] [blame] | 163 | int loc, error; |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 164 | |
| 165 | if (locations == NULL || locations->nlocations <= 0) |
| 166 | goto out; |
| 167 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 168 | dprintk("%s: referral at %s/%s\n", __func__, |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 169 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 170 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 171 | page = (char *) __get_free_page(GFP_USER); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 172 | if (!page) |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 173 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 174 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 175 | page2 = (char *) __get_free_page(GFP_USER); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 176 | if (!page2) |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 177 | goto out; |
| 178 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 179 | /* Ensure fs path is a prefix of current dentry path */ |
| 180 | error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2); |
| 181 | if (error < 0) { |
| 182 | mnt = ERR_PTR(error); |
| 183 | goto out; |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 184 | } |
| 185 | |
J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 186 | for (loc = 0; loc < locations->nlocations; loc++) { |
David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 187 | const struct nfs4_fs_location *location = &locations->locations[loc]; |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 188 | |
| 189 | if (location == NULL || location->nservers <= 0 || |
J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 190 | location->rootpath.ncomponents == 0) |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 191 | continue; |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 192 | |
J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 193 | mnt = try_location(&mountdata, page, page2, location); |
| 194 | if (!IS_ERR(mnt)) |
| 195 | break; |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 196 | } |
| 197 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 198 | out: |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 199 | free_page((unsigned long) page); |
| 200 | free_page((unsigned long) page2); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 201 | dprintk("%s: done\n", __func__); |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 202 | return mnt; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * nfs_do_refmount - handle crossing a referral on server |
| 207 | * @dentry - dentry of referral |
| 208 | * @nd - nameidata info |
| 209 | * |
| 210 | */ |
| 211 | struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry) |
| 212 | { |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 213 | struct vfsmount *mnt = ERR_PTR(-ENOMEM); |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 214 | struct dentry *parent; |
| 215 | struct nfs4_fs_locations *fs_locations = NULL; |
| 216 | struct page *page; |
| 217 | int err; |
| 218 | |
| 219 | /* BUG_ON(IS_ROOT(dentry)); */ |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 220 | dprintk("%s: enter\n", __func__); |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 221 | |
| 222 | page = alloc_page(GFP_KERNEL); |
| 223 | if (page == NULL) |
| 224 | goto out; |
| 225 | |
| 226 | fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); |
| 227 | if (fs_locations == NULL) |
| 228 | goto out_free; |
| 229 | |
| 230 | /* Get locations */ |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 231 | mnt = ERR_PTR(-ENOENT); |
| 232 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 233 | parent = dget_parent(dentry); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 234 | dprintk("%s: getting locations for %s/%s\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 235 | __func__, parent->d_name.name, dentry->d_name.name); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 236 | |
Trond Myklebust | c228fd3 | 2007-01-13 02:28:11 -0500 | [diff] [blame] | 237 | err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page); |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 238 | dput(parent); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 239 | if (err != 0 || |
| 240 | fs_locations->nlocations <= 0 || |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 241 | fs_locations->fs_path.ncomponents <= 0) |
| 242 | goto out_free; |
| 243 | |
| 244 | mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations); |
| 245 | out_free: |
| 246 | __free_page(page); |
| 247 | kfree(fs_locations); |
| 248 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 249 | dprintk("%s: done\n", __func__); |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 250 | return mnt; |
| 251 | } |