nfs4namespace.c 9.4 KB

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