nfs4namespace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * linux/fs/nfs/nfs4namespace.c
  3. *
  4. * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
  5. *
  6. * NFSv4 namespace
  7. */
  8. #include <linux/config.h>
  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(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. 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. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  45. * @mnt_parent - mountpoint of parent directory
  46. * @dentry - parent directory
  47. * @fspath - fs path returned in fs_locations
  48. * @mntpath - mount path to new server
  49. * @hostname - hostname of new server
  50. * @addr - host addr of new server
  51. *
  52. */
  53. static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
  54. const struct dentry *dentry,
  55. struct nfs4_fs_locations *locations)
  56. {
  57. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  58. struct nfs_clone_mount mountdata = {
  59. .sb = mnt_parent->mnt_sb,
  60. .dentry = dentry,
  61. .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
  62. };
  63. char *page, *page2;
  64. char *path, *fs_path;
  65. char *devname;
  66. int loc, s;
  67. if (locations == NULL || locations->nlocations <= 0)
  68. goto out;
  69. dprintk("%s: referral at %s/%s\n", __FUNCTION__,
  70. dentry->d_parent->d_name.name, dentry->d_name.name);
  71. /* Ensure fs path is a prefix of current dentry path */
  72. page = (char *) __get_free_page(GFP_USER);
  73. if (page == NULL)
  74. goto out;
  75. page2 = (char *) __get_free_page(GFP_USER);
  76. if (page2 == NULL)
  77. goto out;
  78. path = nfs4_path(dentry, page, PAGE_SIZE);
  79. if (IS_ERR(path))
  80. goto out_free;
  81. fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
  82. if (IS_ERR(fs_path))
  83. goto out_free;
  84. if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
  85. dprintk("%s: path %s does not begin with fsroot %s\n", __FUNCTION__, path, fs_path);
  86. goto out_free;
  87. }
  88. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  89. if (IS_ERR(devname)) {
  90. mnt = (struct vfsmount *)devname;
  91. goto out_free;
  92. }
  93. loc = 0;
  94. while (loc < locations->nlocations && IS_ERR(mnt)) {
  95. struct nfs4_fs_location *location = &locations->locations[loc];
  96. char *mnt_path;
  97. if (location == NULL || location->nservers <= 0 ||
  98. location->rootpath.ncomponents == 0) {
  99. loc++;
  100. continue;
  101. }
  102. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  103. if (IS_ERR(mnt_path)) {
  104. loc++;
  105. continue;
  106. }
  107. mountdata.mnt_path = mnt_path;
  108. s = 0;
  109. while (s < location->nservers) {
  110. struct sockaddr_in addr = {};
  111. if (location->servers[s].len <= 0 ||
  112. valid_ipaddr4(location->servers[s].data) < 0) {
  113. s++;
  114. continue;
  115. }
  116. mountdata.hostname = location->servers[s].data;
  117. addr.sin_addr.s_addr = in_aton(mountdata.hostname);
  118. addr.sin_family = AF_INET;
  119. addr.sin_port = htons(NFS_PORT);
  120. mountdata.addr = &addr;
  121. mnt = vfs_kern_mount(&nfs_referral_nfs4_fs_type, 0, devname, &mountdata);
  122. if (!IS_ERR(mnt)) {
  123. break;
  124. }
  125. s++;
  126. }
  127. loc++;
  128. }
  129. out_free:
  130. free_page((unsigned long)page);
  131. free_page((unsigned long)page2);
  132. out:
  133. dprintk("%s: done\n", __FUNCTION__);
  134. return mnt;
  135. }
  136. /*
  137. * nfs_do_refmount - handle crossing a referral on server
  138. * @dentry - dentry of referral
  139. * @nd - nameidata info
  140. *
  141. */
  142. struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
  143. {
  144. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  145. struct dentry *parent;
  146. struct nfs4_fs_locations *fs_locations = NULL;
  147. struct page *page;
  148. int err;
  149. /* BUG_ON(IS_ROOT(dentry)); */
  150. dprintk("%s: enter\n", __FUNCTION__);
  151. page = alloc_page(GFP_KERNEL);
  152. if (page == NULL)
  153. goto out;
  154. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  155. if (fs_locations == NULL)
  156. goto out_free;
  157. /* Get locations */
  158. parent = dget_parent(dentry);
  159. dprintk("%s: getting locations for %s/%s\n", __FUNCTION__, parent->d_name.name, dentry->d_name.name);
  160. err = nfs4_proc_fs_locations(parent->d_inode, dentry, fs_locations, page);
  161. dput(parent);
  162. if (err != 0 || fs_locations->nlocations <= 0 ||
  163. fs_locations->fs_path.ncomponents <= 0)
  164. goto out_free;
  165. mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
  166. out_free:
  167. __free_page(page);
  168. kfree(fs_locations);
  169. out:
  170. dprintk("%s: done\n", __FUNCTION__);
  171. return mnt;
  172. }