nfs4namespace.c 6.1 KB

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