nfs4namespace.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/slab.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. #include "nfs4_fs.h"
  20. #include "dns_resolve.h"
  21. #define NFSDBG_FACILITY NFSDBG_VFS
  22. /*
  23. * Convert the NFSv4 pathname components into a standard posix path.
  24. *
  25. * Note that the resulting string will be placed at the end of the buffer
  26. */
  27. static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
  28. char *buffer, ssize_t buflen)
  29. {
  30. char *end = buffer + buflen;
  31. int n;
  32. *--end = '\0';
  33. buflen--;
  34. n = pathname->ncomponents;
  35. while (--n >= 0) {
  36. const struct nfs4_string *component = &pathname->components[n];
  37. buflen -= component->len + 1;
  38. if (buflen < 0)
  39. goto Elong;
  40. end -= component->len;
  41. memcpy(end, component->data, component->len);
  42. *--end = '/';
  43. }
  44. return end;
  45. Elong:
  46. return ERR_PTR(-ENAMETOOLONG);
  47. }
  48. /*
  49. * return the path component of "<server>:<path>"
  50. * nfspath - the "<server>:<path>" string
  51. * end - one past the last char that could contain "<server>:"
  52. * returns NULL on failure
  53. */
  54. static char *nfs_path_component(const char *nfspath, const char *end)
  55. {
  56. char *p;
  57. if (*nfspath == '[') {
  58. /* parse [] escaped IPv6 addrs */
  59. p = strchr(nfspath, ']');
  60. if (p != NULL && ++p < end && *p == ':')
  61. return p + 1;
  62. } else {
  63. /* otherwise split on first colon */
  64. p = strchr(nfspath, ':');
  65. if (p != NULL && p < end)
  66. return p + 1;
  67. }
  68. return NULL;
  69. }
  70. /*
  71. * Determine the mount path as a string
  72. */
  73. static char *nfs4_path(struct dentry *dentry, char *buffer, ssize_t buflen)
  74. {
  75. char *limit;
  76. char *path = nfs_path(&limit, dentry, buffer, buflen,
  77. NFS_PATH_CANONICAL);
  78. if (!IS_ERR(path)) {
  79. char *path_component = nfs_path_component(path, limit);
  80. if (path_component)
  81. return path_component;
  82. }
  83. return path;
  84. }
  85. /*
  86. * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
  87. * believe to be the server path to this dentry
  88. */
  89. static int nfs4_validate_fspath(struct dentry *dentry,
  90. const struct nfs4_fs_locations *locations,
  91. char *page, char *page2)
  92. {
  93. const char *path, *fs_path;
  94. path = nfs4_path(dentry, page, PAGE_SIZE);
  95. if (IS_ERR(path))
  96. return PTR_ERR(path);
  97. fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
  98. if (IS_ERR(fs_path))
  99. return PTR_ERR(fs_path);
  100. if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
  101. dprintk("%s: path %s does not begin with fsroot %s\n",
  102. __func__, path, fs_path);
  103. return -ENOENT;
  104. }
  105. return 0;
  106. }
  107. static size_t nfs_parse_server_name(char *string, size_t len,
  108. struct sockaddr *sa, size_t salen, struct nfs_server *server)
  109. {
  110. struct net *net = rpc_net_ns(server->client);
  111. ssize_t ret;
  112. ret = rpc_pton(net, string, len, sa, salen);
  113. if (ret == 0) {
  114. ret = nfs_dns_resolve_name(net, string, len, sa, salen);
  115. if (ret < 0)
  116. ret = 0;
  117. }
  118. return ret;
  119. }
  120. rpc_authflavor_t nfs_find_best_sec(struct nfs4_secinfo_flavors *flavors)
  121. {
  122. struct gss_api_mech *mech;
  123. struct xdr_netobj oid;
  124. int i;
  125. rpc_authflavor_t pseudoflavor = RPC_AUTH_UNIX;
  126. for (i = 0; i < flavors->num_flavors; i++) {
  127. struct nfs4_secinfo_flavor *flavor;
  128. flavor = &flavors->flavors[i];
  129. if (flavor->flavor == RPC_AUTH_NULL || flavor->flavor == RPC_AUTH_UNIX) {
  130. pseudoflavor = flavor->flavor;
  131. break;
  132. } else if (flavor->flavor == RPC_AUTH_GSS) {
  133. oid.len = flavor->gss.sec_oid4.len;
  134. oid.data = flavor->gss.sec_oid4.data;
  135. mech = gss_mech_get_by_OID(&oid);
  136. if (!mech)
  137. continue;
  138. pseudoflavor = gss_svc_to_pseudoflavor(mech, flavor->gss.service);
  139. gss_mech_put(mech);
  140. break;
  141. }
  142. }
  143. return pseudoflavor;
  144. }
  145. static rpc_authflavor_t nfs4_negotiate_security(struct inode *inode, struct qstr *name)
  146. {
  147. struct page *page;
  148. struct nfs4_secinfo_flavors *flavors;
  149. rpc_authflavor_t flavor;
  150. int err;
  151. page = alloc_page(GFP_KERNEL);
  152. if (!page)
  153. return -ENOMEM;
  154. flavors = page_address(page);
  155. err = nfs4_proc_secinfo(inode, name, flavors);
  156. if (err < 0) {
  157. flavor = err;
  158. goto out;
  159. }
  160. flavor = nfs_find_best_sec(flavors);
  161. out:
  162. put_page(page);
  163. return flavor;
  164. }
  165. /*
  166. * Please call rpc_shutdown_client() when you are done with this client.
  167. */
  168. struct rpc_clnt *nfs4_create_sec_client(struct rpc_clnt *clnt, struct inode *inode,
  169. struct qstr *name)
  170. {
  171. rpc_authflavor_t flavor;
  172. flavor = nfs4_negotiate_security(inode, name);
  173. if ((int)flavor < 0)
  174. return ERR_PTR((int)flavor);
  175. return rpc_clone_client_set_auth(clnt, flavor);
  176. }
  177. static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
  178. char *page, char *page2,
  179. const struct nfs4_fs_location *location)
  180. {
  181. const size_t addr_bufsize = sizeof(struct sockaddr_storage);
  182. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  183. char *mnt_path;
  184. unsigned int maxbuflen;
  185. unsigned int s;
  186. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  187. if (IS_ERR(mnt_path))
  188. return ERR_CAST(mnt_path);
  189. mountdata->mnt_path = mnt_path;
  190. maxbuflen = mnt_path - 1 - page2;
  191. mountdata->addr = kmalloc(addr_bufsize, GFP_KERNEL);
  192. if (mountdata->addr == NULL)
  193. return ERR_PTR(-ENOMEM);
  194. for (s = 0; s < location->nservers; s++) {
  195. const struct nfs4_string *buf = &location->servers[s];
  196. if (buf->len <= 0 || buf->len >= maxbuflen)
  197. continue;
  198. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
  199. continue;
  200. mountdata->addrlen = nfs_parse_server_name(buf->data, buf->len,
  201. mountdata->addr, addr_bufsize,
  202. NFS_SB(mountdata->sb));
  203. if (mountdata->addrlen == 0)
  204. continue;
  205. rpc_set_port(mountdata->addr, NFS_PORT);
  206. memcpy(page2, buf->data, buf->len);
  207. page2[buf->len] = '\0';
  208. mountdata->hostname = page2;
  209. snprintf(page, PAGE_SIZE, "%s:%s",
  210. mountdata->hostname,
  211. mountdata->mnt_path);
  212. mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
  213. if (!IS_ERR(mnt))
  214. break;
  215. }
  216. kfree(mountdata->addr);
  217. return mnt;
  218. }
  219. /**
  220. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  221. * @dentry - parent directory
  222. * @locations - array of NFSv4 server location information
  223. *
  224. */
  225. static struct vfsmount *nfs_follow_referral(struct dentry *dentry,
  226. const struct nfs4_fs_locations *locations)
  227. {
  228. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  229. struct nfs_clone_mount mountdata = {
  230. .sb = dentry->d_sb,
  231. .dentry = dentry,
  232. .authflavor = NFS_SB(dentry->d_sb)->client->cl_auth->au_flavor,
  233. };
  234. char *page = NULL, *page2 = NULL;
  235. int loc, error;
  236. if (locations == NULL || locations->nlocations <= 0)
  237. goto out;
  238. dprintk("%s: referral at %s/%s\n", __func__,
  239. dentry->d_parent->d_name.name, dentry->d_name.name);
  240. page = (char *) __get_free_page(GFP_USER);
  241. if (!page)
  242. goto out;
  243. page2 = (char *) __get_free_page(GFP_USER);
  244. if (!page2)
  245. goto out;
  246. /* Ensure fs path is a prefix of current dentry path */
  247. error = nfs4_validate_fspath(dentry, locations, page, page2);
  248. if (error < 0) {
  249. mnt = ERR_PTR(error);
  250. goto out;
  251. }
  252. for (loc = 0; loc < locations->nlocations; loc++) {
  253. const struct nfs4_fs_location *location = &locations->locations[loc];
  254. if (location == NULL || location->nservers <= 0 ||
  255. location->rootpath.ncomponents == 0)
  256. continue;
  257. mnt = try_location(&mountdata, page, page2, location);
  258. if (!IS_ERR(mnt))
  259. break;
  260. }
  261. out:
  262. free_page((unsigned long) page);
  263. free_page((unsigned long) page2);
  264. dprintk("%s: done\n", __func__);
  265. return mnt;
  266. }
  267. /*
  268. * nfs_do_refmount - handle crossing a referral on server
  269. * @dentry - dentry of referral
  270. *
  271. */
  272. static struct vfsmount *nfs_do_refmount(struct rpc_clnt *client, struct dentry *dentry)
  273. {
  274. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  275. struct dentry *parent;
  276. struct nfs4_fs_locations *fs_locations = NULL;
  277. struct page *page;
  278. int err;
  279. /* BUG_ON(IS_ROOT(dentry)); */
  280. dprintk("%s: enter\n", __func__);
  281. page = alloc_page(GFP_KERNEL);
  282. if (page == NULL)
  283. goto out;
  284. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  285. if (fs_locations == NULL)
  286. goto out_free;
  287. /* Get locations */
  288. mnt = ERR_PTR(-ENOENT);
  289. parent = dget_parent(dentry);
  290. dprintk("%s: getting locations for %s/%s\n",
  291. __func__, parent->d_name.name, dentry->d_name.name);
  292. err = nfs4_proc_fs_locations(client, parent->d_inode, &dentry->d_name, fs_locations, page);
  293. dput(parent);
  294. if (err != 0 ||
  295. fs_locations->nlocations <= 0 ||
  296. fs_locations->fs_path.ncomponents <= 0)
  297. goto out_free;
  298. mnt = nfs_follow_referral(dentry, fs_locations);
  299. out_free:
  300. __free_page(page);
  301. kfree(fs_locations);
  302. out:
  303. dprintk("%s: done\n", __func__);
  304. return mnt;
  305. }
  306. struct vfsmount *nfs4_submount(struct nfs_server *server, struct dentry *dentry,
  307. struct nfs_fh *fh, struct nfs_fattr *fattr)
  308. {
  309. struct dentry *parent = dget_parent(dentry);
  310. struct rpc_clnt *client;
  311. struct vfsmount *mnt;
  312. /* Look it up again to get its attributes and sec flavor */
  313. client = nfs4_proc_lookup_mountpoint(parent->d_inode, &dentry->d_name, fh, fattr);
  314. dput(parent);
  315. if (IS_ERR(client))
  316. return ERR_CAST(client);
  317. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  318. mnt = nfs_do_refmount(client, dentry);
  319. else
  320. mnt = nfs_do_submount(dentry, fh, fattr, client->cl_auth->au_flavor);
  321. rpc_shutdown_client(client);
  322. return mnt;
  323. }