nfs4namespace.c 6.5 KB

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