nfs4namespace.c 6.1 KB

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