nfs4namespace.c 6.2 KB

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