nfs4namespace.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/config.h>
  10. #include <linux/dcache.h>
  11. #include <linux/mount.h>
  12. #include <linux/namei.h>
  13. #include <linux/nfs_fs.h>
  14. #include <linux/string.h>
  15. #include <linux/sunrpc/clnt.h>
  16. #include <linux/vfs.h>
  17. #include <linux/inet.h>
  18. #include "internal.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. char *devname;
  119. int loc, s, error;
  120. if (locations == NULL || locations->nlocations <= 0)
  121. goto out;
  122. dprintk("%s: referral at %s/%s\n", __FUNCTION__,
  123. dentry->d_parent->d_name.name, dentry->d_name.name);
  124. page = (char *) __get_free_page(GFP_USER);
  125. if (!page)
  126. goto out;
  127. page2 = (char *) __get_free_page(GFP_USER);
  128. if (!page2)
  129. goto out;
  130. /* Ensure fs path is a prefix of current dentry path */
  131. error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2);
  132. if (error < 0) {
  133. mnt = ERR_PTR(error);
  134. goto out;
  135. }
  136. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  137. if (IS_ERR(devname)) {
  138. mnt = (struct vfsmount *)devname;
  139. goto out;
  140. }
  141. loc = 0;
  142. while (loc < locations->nlocations && IS_ERR(mnt)) {
  143. const struct nfs4_fs_location *location = &locations->locations[loc];
  144. char *mnt_path;
  145. if (location == NULL || location->nservers <= 0 ||
  146. location->rootpath.ncomponents == 0) {
  147. loc++;
  148. continue;
  149. }
  150. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  151. if (IS_ERR(mnt_path)) {
  152. loc++;
  153. continue;
  154. }
  155. mountdata.mnt_path = mnt_path;
  156. s = 0;
  157. while (s < location->nservers) {
  158. struct sockaddr_in addr = {};
  159. if (location->servers[s].len <= 0 ||
  160. valid_ipaddr4(location->servers[s].data) < 0) {
  161. s++;
  162. continue;
  163. }
  164. mountdata.hostname = location->servers[s].data;
  165. addr.sin_addr.s_addr = in_aton(mountdata.hostname);
  166. addr.sin_family = AF_INET;
  167. addr.sin_port = htons(NFS_PORT);
  168. mountdata.addr = &addr;
  169. mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, devname, &mountdata);
  170. if (!IS_ERR(mnt)) {
  171. break;
  172. }
  173. s++;
  174. }
  175. loc++;
  176. }
  177. out:
  178. free_page((unsigned long) page);
  179. free_page((unsigned long) page2);
  180. dprintk("%s: done\n", __FUNCTION__);
  181. return mnt;
  182. }
  183. /*
  184. * nfs_do_refmount - handle crossing a referral on server
  185. * @dentry - dentry of referral
  186. * @nd - nameidata info
  187. *
  188. */
  189. struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
  190. {
  191. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  192. struct dentry *parent;
  193. struct nfs4_fs_locations *fs_locations = NULL;
  194. struct page *page;
  195. int err;
  196. /* BUG_ON(IS_ROOT(dentry)); */
  197. dprintk("%s: enter\n", __FUNCTION__);
  198. page = alloc_page(GFP_KERNEL);
  199. if (page == NULL)
  200. goto out;
  201. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  202. if (fs_locations == NULL)
  203. goto out_free;
  204. /* Get locations */
  205. mnt = ERR_PTR(-ENOENT);
  206. parent = dget_parent(dentry);
  207. dprintk("%s: getting locations for %s/%s\n",
  208. __FUNCTION__, parent->d_name.name, dentry->d_name.name);
  209. err = nfs4_proc_fs_locations(parent->d_inode, dentry, fs_locations, page);
  210. dput(parent);
  211. if (err != 0 ||
  212. fs_locations->nlocations <= 0 ||
  213. fs_locations->fs_path.ncomponents <= 0)
  214. goto out_free;
  215. mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
  216. out_free:
  217. __free_page(page);
  218. kfree(fs_locations);
  219. out:
  220. dprintk("%s: done\n", __FUNCTION__);
  221. return mnt;
  222. }