nfs4namespace.c 7.0 KB

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