nfs4namespace.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * Determine the mount path as a string
  50. */
  51. static char *nfs4_path(const struct vfsmount *mnt_parent,
  52. const struct dentry *dentry,
  53. char *buffer, ssize_t buflen)
  54. {
  55. const char *srvpath;
  56. srvpath = strchr(mnt_parent->mnt_devname, ':');
  57. if (srvpath)
  58. srvpath++;
  59. else
  60. srvpath = mnt_parent->mnt_devname;
  61. return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen);
  62. }
  63. /*
  64. * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
  65. * believe to be the server path to this dentry
  66. */
  67. static int nfs4_validate_fspath(const struct vfsmount *mnt_parent,
  68. const struct dentry *dentry,
  69. const struct nfs4_fs_locations *locations,
  70. char *page, char *page2)
  71. {
  72. const char *path, *fs_path;
  73. path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE);
  74. if (IS_ERR(path))
  75. return PTR_ERR(path);
  76. fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
  77. if (IS_ERR(fs_path))
  78. return PTR_ERR(fs_path);
  79. if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
  80. dprintk("%s: path %s does not begin with fsroot %s\n",
  81. __func__, path, fs_path);
  82. return -ENOENT;
  83. }
  84. return 0;
  85. }
  86. static size_t nfs_parse_server_name(char *string, size_t len,
  87. struct sockaddr *sa, size_t salen)
  88. {
  89. ssize_t ret;
  90. ret = rpc_pton(string, len, sa, salen);
  91. if (ret == 0) {
  92. ret = nfs_dns_resolve_name(string, len, sa, salen);
  93. if (ret < 0)
  94. ret = 0;
  95. }
  96. return ret;
  97. }
  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;
  104. unsigned int maxbuflen;
  105. unsigned int s;
  106. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  107. if (IS_ERR(mnt_path))
  108. return ERR_CAST(mnt_path);
  109. mountdata->mnt_path = mnt_path;
  110. maxbuflen = mnt_path - 1 - page2;
  111. for (s = 0; s < location->nservers; s++) {
  112. const struct nfs4_string *buf = &location->servers[s];
  113. struct sockaddr_storage addr;
  114. if (buf->len <= 0 || buf->len >= maxbuflen)
  115. continue;
  116. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
  117. continue;
  118. mountdata->addrlen = nfs_parse_server_name(buf->data, buf->len,
  119. (struct sockaddr *)&addr, sizeof(addr));
  120. if (mountdata->addrlen == 0)
  121. continue;
  122. mountdata->addr = (struct sockaddr *)&addr;
  123. rpc_set_port(mountdata->addr, NFS_PORT);
  124. memcpy(page2, buf->data, buf->len);
  125. page2[buf->len] = '\0';
  126. mountdata->hostname = page2;
  127. snprintf(page, PAGE_SIZE, "%s:%s",
  128. mountdata->hostname,
  129. mountdata->mnt_path);
  130. mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
  131. if (!IS_ERR(mnt))
  132. break;
  133. }
  134. return mnt;
  135. }
  136. /**
  137. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  138. * @mnt_parent - mountpoint of parent directory
  139. * @dentry - parent directory
  140. * @locations - array of NFSv4 server location information
  141. *
  142. */
  143. static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
  144. const struct dentry *dentry,
  145. const struct nfs4_fs_locations *locations)
  146. {
  147. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  148. struct nfs_clone_mount mountdata = {
  149. .sb = mnt_parent->mnt_sb,
  150. .dentry = dentry,
  151. .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
  152. };
  153. char *page = NULL, *page2 = NULL;
  154. int loc, error;
  155. if (locations == NULL || locations->nlocations <= 0)
  156. goto out;
  157. dprintk("%s: referral at %s/%s\n", __func__,
  158. dentry->d_parent->d_name.name, dentry->d_name.name);
  159. page = (char *) __get_free_page(GFP_USER);
  160. if (!page)
  161. goto out;
  162. page2 = (char *) __get_free_page(GFP_USER);
  163. if (!page2)
  164. goto out;
  165. /* Ensure fs path is a prefix of current dentry path */
  166. error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2);
  167. if (error < 0) {
  168. mnt = ERR_PTR(error);
  169. goto out;
  170. }
  171. for (loc = 0; loc < locations->nlocations; loc++) {
  172. const struct nfs4_fs_location *location = &locations->locations[loc];
  173. if (location == NULL || location->nservers <= 0 ||
  174. location->rootpath.ncomponents == 0)
  175. continue;
  176. mnt = try_location(&mountdata, page, page2, location);
  177. if (!IS_ERR(mnt))
  178. break;
  179. }
  180. out:
  181. free_page((unsigned long) page);
  182. free_page((unsigned long) page2);
  183. dprintk("%s: done\n", __func__);
  184. return mnt;
  185. }
  186. /*
  187. * nfs_do_refmount - handle crossing a referral on server
  188. * @dentry - dentry of referral
  189. * @nd - nameidata info
  190. *
  191. */
  192. struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
  193. {
  194. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  195. struct dentry *parent;
  196. struct nfs4_fs_locations *fs_locations = NULL;
  197. struct page *page;
  198. int err;
  199. /* BUG_ON(IS_ROOT(dentry)); */
  200. dprintk("%s: enter\n", __func__);
  201. page = alloc_page(GFP_KERNEL);
  202. if (page == NULL)
  203. goto out;
  204. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  205. if (fs_locations == NULL)
  206. goto out_free;
  207. /* Get locations */
  208. mnt = ERR_PTR(-ENOENT);
  209. parent = dget_parent(dentry);
  210. dprintk("%s: getting locations for %s/%s\n",
  211. __func__, parent->d_name.name, dentry->d_name.name);
  212. err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page);
  213. dput(parent);
  214. if (err != 0 ||
  215. fs_locations->nlocations <= 0 ||
  216. fs_locations->fs_path.ncomponents <= 0)
  217. goto out_free;
  218. mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
  219. out_free:
  220. __free_page(page);
  221. kfree(fs_locations);
  222. out:
  223. dprintk("%s: done\n", __func__);
  224. return mnt;
  225. }