nfs4namespace.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. __FUNCTION__, path, fs_path);
  78. return -ENOENT;
  79. }
  80. return 0;
  81. }
  82. /*
  83. * Check if the string represents a "valid" IPv4 address
  84. */
  85. static inline int valid_ipaddr4(const char *buf)
  86. {
  87. int rc, count, in[4];
  88. rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
  89. if (rc != 4)
  90. return -EINVAL;
  91. for (count = 0; count < 4; count++) {
  92. if (in[count] > 255)
  93. return -EINVAL;
  94. }
  95. return 0;
  96. }
  97. /**
  98. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  99. * @mnt_parent - mountpoint of parent directory
  100. * @dentry - parent directory
  101. * @fspath - fs path returned in fs_locations
  102. * @mntpath - mount path to new server
  103. * @hostname - hostname of new server
  104. * @addr - host addr of new server
  105. *
  106. */
  107. static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
  108. const struct dentry *dentry,
  109. const struct nfs4_fs_locations *locations)
  110. {
  111. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  112. struct nfs_clone_mount mountdata = {
  113. .sb = mnt_parent->mnt_sb,
  114. .dentry = dentry,
  115. .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
  116. };
  117. char *page = NULL, *page2 = NULL;
  118. int loc, s, error;
  119. if (locations == NULL || locations->nlocations <= 0)
  120. goto out;
  121. dprintk("%s: referral at %s/%s\n", __FUNCTION__,
  122. dentry->d_parent->d_name.name, dentry->d_name.name);
  123. page = (char *) __get_free_page(GFP_USER);
  124. if (!page)
  125. goto out;
  126. page2 = (char *) __get_free_page(GFP_USER);
  127. if (!page2)
  128. goto out;
  129. /* Ensure fs path is a prefix of current dentry path */
  130. error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2);
  131. if (error < 0) {
  132. mnt = ERR_PTR(error);
  133. goto out;
  134. }
  135. loc = 0;
  136. while (loc < locations->nlocations && IS_ERR(mnt)) {
  137. const struct nfs4_fs_location *location = &locations->locations[loc];
  138. char *mnt_path;
  139. if (location == NULL || location->nservers <= 0 ||
  140. location->rootpath.ncomponents == 0) {
  141. loc++;
  142. continue;
  143. }
  144. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  145. if (IS_ERR(mnt_path)) {
  146. loc++;
  147. continue;
  148. }
  149. mountdata.mnt_path = mnt_path;
  150. s = 0;
  151. while (s < location->nservers) {
  152. struct sockaddr_in addr = {};
  153. if (location->servers[s].len <= 0 ||
  154. valid_ipaddr4(location->servers[s].data) < 0) {
  155. s++;
  156. continue;
  157. }
  158. mountdata.hostname = location->servers[s].data;
  159. addr.sin_addr.s_addr = in_aton(mountdata.hostname);
  160. addr.sin_family = AF_INET;
  161. addr.sin_port = htons(NFS_PORT);
  162. mountdata.addr = &addr;
  163. snprintf(page, PAGE_SIZE, "%s:%s",
  164. mountdata.hostname,
  165. mountdata.mnt_path);
  166. mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, &mountdata);
  167. if (!IS_ERR(mnt)) {
  168. break;
  169. }
  170. s++;
  171. }
  172. loc++;
  173. }
  174. out:
  175. free_page((unsigned long) page);
  176. free_page((unsigned long) page2);
  177. dprintk("%s: done\n", __FUNCTION__);
  178. return mnt;
  179. }
  180. /*
  181. * nfs_do_refmount - handle crossing a referral on server
  182. * @dentry - dentry of referral
  183. * @nd - nameidata info
  184. *
  185. */
  186. struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
  187. {
  188. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  189. struct dentry *parent;
  190. struct nfs4_fs_locations *fs_locations = NULL;
  191. struct page *page;
  192. int err;
  193. /* BUG_ON(IS_ROOT(dentry)); */
  194. dprintk("%s: enter\n", __FUNCTION__);
  195. page = alloc_page(GFP_KERNEL);
  196. if (page == NULL)
  197. goto out;
  198. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  199. if (fs_locations == NULL)
  200. goto out_free;
  201. /* Get locations */
  202. mnt = ERR_PTR(-ENOENT);
  203. parent = dget_parent(dentry);
  204. dprintk("%s: getting locations for %s/%s\n",
  205. __FUNCTION__, parent->d_name.name, dentry->d_name.name);
  206. err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page);
  207. dput(parent);
  208. if (err != 0 ||
  209. fs_locations->nlocations <= 0 ||
  210. fs_locations->fs_path.ncomponents <= 0)
  211. goto out_free;
  212. mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
  213. out_free:
  214. __free_page(page);
  215. kfree(fs_locations);
  216. out:
  217. dprintk("%s: done\n", __FUNCTION__);
  218. return mnt;
  219. }