nfs4namespace.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #define NFSDBG_FACILITY NFSDBG_VFS
  19. /*
  20. * Check if fs_root is valid
  21. */
  22. static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
  23. char *buffer, ssize_t buflen)
  24. {
  25. char *end = buffer + buflen;
  26. int n;
  27. *--end = '\0';
  28. buflen--;
  29. n = pathname->ncomponents;
  30. while (--n >= 0) {
  31. const struct nfs4_string *component = &pathname->components[n];
  32. buflen -= component->len + 1;
  33. if (buflen < 0)
  34. goto Elong;
  35. end -= component->len;
  36. memcpy(end, component->data, component->len);
  37. *--end = '/';
  38. }
  39. return end;
  40. Elong:
  41. return ERR_PTR(-ENAMETOOLONG);
  42. }
  43. /*
  44. * Determine the mount path as a string
  45. */
  46. static char *nfs4_path(const struct vfsmount *mnt_parent,
  47. const struct dentry *dentry,
  48. char *buffer, ssize_t buflen)
  49. {
  50. const char *srvpath;
  51. srvpath = strchr(mnt_parent->mnt_devname, ':');
  52. if (srvpath)
  53. srvpath++;
  54. else
  55. srvpath = mnt_parent->mnt_devname;
  56. return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen);
  57. }
  58. /*
  59. * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
  60. * believe to be the server path to this dentry
  61. */
  62. static int nfs4_validate_fspath(const struct vfsmount *mnt_parent,
  63. const struct dentry *dentry,
  64. const struct nfs4_fs_locations *locations,
  65. char *page, char *page2)
  66. {
  67. const char *path, *fs_path;
  68. path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE);
  69. if (IS_ERR(path))
  70. return PTR_ERR(path);
  71. fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
  72. if (IS_ERR(fs_path))
  73. return PTR_ERR(fs_path);
  74. if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
  75. dprintk("%s: path %s does not begin with fsroot %s\n",
  76. __FUNCTION__, path, fs_path);
  77. return -ENOENT;
  78. }
  79. return 0;
  80. }
  81. /*
  82. * Check if the string represents a "valid" IPv4 address
  83. */
  84. static inline int valid_ipaddr4(const char *buf)
  85. {
  86. int rc, count, in[4];
  87. rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
  88. if (rc != 4)
  89. return -EINVAL;
  90. for (count = 0; count < 4; count++) {
  91. if (in[count] > 255)
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. /**
  97. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  98. * @mnt_parent - mountpoint of parent directory
  99. * @dentry - parent directory
  100. * @fspath - fs path returned in fs_locations
  101. * @mntpath - mount path to new server
  102. * @hostname - hostname of new server
  103. * @addr - host addr of new server
  104. *
  105. */
  106. static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
  107. const struct dentry *dentry,
  108. const struct nfs4_fs_locations *locations)
  109. {
  110. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  111. struct nfs_clone_mount mountdata = {
  112. .sb = mnt_parent->mnt_sb,
  113. .dentry = dentry,
  114. .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
  115. };
  116. char *page = NULL, *page2 = NULL;
  117. char *devname;
  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. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  136. if (IS_ERR(devname)) {
  137. mnt = (struct vfsmount *)devname;
  138. goto out;
  139. }
  140. loc = 0;
  141. while (loc < locations->nlocations && IS_ERR(mnt)) {
  142. const struct nfs4_fs_location *location = &locations->locations[loc];
  143. char *mnt_path;
  144. if (location == NULL || location->nservers <= 0 ||
  145. location->rootpath.ncomponents == 0) {
  146. loc++;
  147. continue;
  148. }
  149. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  150. if (IS_ERR(mnt_path)) {
  151. loc++;
  152. continue;
  153. }
  154. mountdata.mnt_path = mnt_path;
  155. s = 0;
  156. while (s < location->nservers) {
  157. struct sockaddr_in addr = {};
  158. if (location->servers[s].len <= 0 ||
  159. valid_ipaddr4(location->servers[s].data) < 0) {
  160. s++;
  161. continue;
  162. }
  163. mountdata.hostname = location->servers[s].data;
  164. addr.sin_addr.s_addr = in_aton(mountdata.hostname);
  165. addr.sin_family = AF_INET;
  166. addr.sin_port = htons(NFS_PORT);
  167. mountdata.addr = &addr;
  168. mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, devname, &mountdata);
  169. if (!IS_ERR(mnt)) {
  170. break;
  171. }
  172. s++;
  173. }
  174. loc++;
  175. }
  176. out:
  177. free_page((unsigned long) page);
  178. free_page((unsigned long) page2);
  179. dprintk("%s: done\n", __FUNCTION__);
  180. return mnt;
  181. }
  182. /*
  183. * nfs_do_refmount - handle crossing a referral on server
  184. * @dentry - dentry of referral
  185. * @nd - nameidata info
  186. *
  187. */
  188. struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
  189. {
  190. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  191. struct dentry *parent;
  192. struct nfs4_fs_locations *fs_locations = NULL;
  193. struct page *page;
  194. int err;
  195. /* BUG_ON(IS_ROOT(dentry)); */
  196. dprintk("%s: enter\n", __FUNCTION__);
  197. page = alloc_page(GFP_KERNEL);
  198. if (page == NULL)
  199. goto out;
  200. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  201. if (fs_locations == NULL)
  202. goto out_free;
  203. /* Get locations */
  204. mnt = ERR_PTR(-ENOENT);
  205. parent = dget_parent(dentry);
  206. dprintk("%s: getting locations for %s/%s\n",
  207. __FUNCTION__, parent->d_name.name, dentry->d_name.name);
  208. err = nfs4_proc_fs_locations(parent->d_inode, dentry, fs_locations, page);
  209. dput(parent);
  210. if (err != 0 ||
  211. fs_locations->nlocations <= 0 ||
  212. fs_locations->fs_path.ncomponents <= 0)
  213. goto out_free;
  214. mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
  215. out_free:
  216. __free_page(page);
  217. kfree(fs_locations);
  218. out:
  219. dprintk("%s: done\n", __FUNCTION__);
  220. return mnt;
  221. }