nfs4namespace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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/nfs_mount.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/sunrpc/addr.h>
  18. #include <linux/vfs.h>
  19. #include <linux/inet.h>
  20. #include "internal.h"
  21. #include "nfs4_fs.h"
  22. #include "dns_resolve.h"
  23. #define NFSDBG_FACILITY NFSDBG_VFS
  24. /*
  25. * Convert the NFSv4 pathname components into a standard posix path.
  26. *
  27. * Note that the resulting string will be placed at the end of the buffer
  28. */
  29. static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
  30. char *buffer, ssize_t buflen)
  31. {
  32. char *end = buffer + buflen;
  33. int n;
  34. *--end = '\0';
  35. buflen--;
  36. n = pathname->ncomponents;
  37. while (--n >= 0) {
  38. const struct nfs4_string *component = &pathname->components[n];
  39. buflen -= component->len + 1;
  40. if (buflen < 0)
  41. goto Elong;
  42. end -= component->len;
  43. memcpy(end, component->data, component->len);
  44. *--end = '/';
  45. }
  46. return end;
  47. Elong:
  48. return ERR_PTR(-ENAMETOOLONG);
  49. }
  50. /*
  51. * return the path component of "<server>:<path>"
  52. * nfspath - the "<server>:<path>" string
  53. * end - one past the last char that could contain "<server>:"
  54. * returns NULL on failure
  55. */
  56. static char *nfs_path_component(const char *nfspath, const char *end)
  57. {
  58. char *p;
  59. if (*nfspath == '[') {
  60. /* parse [] escaped IPv6 addrs */
  61. p = strchr(nfspath, ']');
  62. if (p != NULL && ++p < end && *p == ':')
  63. return p + 1;
  64. } else {
  65. /* otherwise split on first colon */
  66. p = strchr(nfspath, ':');
  67. if (p != NULL && p < end)
  68. return p + 1;
  69. }
  70. return NULL;
  71. }
  72. /*
  73. * Determine the mount path as a string
  74. */
  75. static char *nfs4_path(struct dentry *dentry, char *buffer, ssize_t buflen)
  76. {
  77. char *limit;
  78. char *path = nfs_path(&limit, dentry, buffer, buflen,
  79. NFS_PATH_CANONICAL);
  80. if (!IS_ERR(path)) {
  81. char *path_component = nfs_path_component(path, limit);
  82. if (path_component)
  83. return path_component;
  84. }
  85. return path;
  86. }
  87. /*
  88. * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
  89. * believe to be the server path to this dentry
  90. */
  91. static int nfs4_validate_fspath(struct dentry *dentry,
  92. const struct nfs4_fs_locations *locations,
  93. char *page, char *page2)
  94. {
  95. const char *path, *fs_path;
  96. path = nfs4_path(dentry, page, PAGE_SIZE);
  97. if (IS_ERR(path))
  98. return PTR_ERR(path);
  99. fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
  100. if (IS_ERR(fs_path))
  101. return PTR_ERR(fs_path);
  102. if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
  103. dprintk("%s: path %s does not begin with fsroot %s\n",
  104. __func__, path, fs_path);
  105. return -ENOENT;
  106. }
  107. return 0;
  108. }
  109. static size_t nfs_parse_server_name(char *string, size_t len,
  110. struct sockaddr *sa, size_t salen, struct net *net)
  111. {
  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. * @server: NFS server struct
  124. * @flavors: List of security tuples returned by SECINFO procedure
  125. *
  126. * Return an rpc client that uses the first security mechanism in
  127. * "flavors" that is locally supported. The "flavors" array
  128. * is searched in the order returned from the server, per RFC 3530
  129. * recommendation and each flavor is checked for membership in the
  130. * sec= mount option list if it exists.
  131. *
  132. * Return -EPERM if no matching flavor is found in the array.
  133. *
  134. * Please call rpc_shutdown_client() when you are done with this rpc client.
  135. *
  136. */
  137. static struct rpc_clnt *nfs_find_best_sec(struct rpc_clnt *clnt,
  138. struct nfs_server *server,
  139. struct nfs4_secinfo_flavors *flavors)
  140. {
  141. rpc_authflavor_t pflavor;
  142. struct nfs4_secinfo4 *secinfo;
  143. unsigned int i;
  144. for (i = 0; i < flavors->num_flavors; i++) {
  145. secinfo = &flavors->flavors[i];
  146. switch (secinfo->flavor) {
  147. case RPC_AUTH_NULL:
  148. case RPC_AUTH_UNIX:
  149. case RPC_AUTH_GSS:
  150. pflavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  151. &secinfo->flavor_info);
  152. /* does the pseudoflavor match a sec= mount opt? */
  153. if (pflavor != RPC_AUTH_MAXFLAVOR &&
  154. nfs_auth_info_match(&server->auth_info, pflavor)) {
  155. struct rpc_clnt *new;
  156. struct rpc_cred *cred;
  157. /* Cloning creates an rpc_auth for the flavor */
  158. new = rpc_clone_client_set_auth(clnt, pflavor);
  159. if (IS_ERR(new))
  160. continue;
  161. /**
  162. * Check that the user actually can use the
  163. * flavor. This is mostly for RPC_AUTH_GSS
  164. * where cr_init obtains a gss context
  165. */
  166. cred = rpcauth_lookupcred(new->cl_auth, 0);
  167. if (IS_ERR(cred)) {
  168. rpc_shutdown_client(new);
  169. continue;
  170. }
  171. put_rpccred(cred);
  172. return new;
  173. }
  174. }
  175. }
  176. return ERR_PTR(-EPERM);
  177. }
  178. /**
  179. * nfs4_negotiate_security - in response to an NFS4ERR_WRONGSEC on lookup,
  180. * return an rpc_clnt that uses the best available security flavor with
  181. * respect to the secinfo flavor list and the sec= mount options.
  182. *
  183. * @clnt: RPC client to clone
  184. * @inode: directory inode
  185. * @name: lookup name
  186. *
  187. * Please call rpc_shutdown_client() when you are done with this rpc client.
  188. */
  189. struct rpc_clnt *
  190. nfs4_negotiate_security(struct rpc_clnt *clnt, struct inode *inode,
  191. const struct qstr *name)
  192. {
  193. struct page *page;
  194. struct nfs4_secinfo_flavors *flavors;
  195. struct rpc_clnt *new;
  196. int err;
  197. page = alloc_page(GFP_KERNEL);
  198. if (!page)
  199. return ERR_PTR(-ENOMEM);
  200. flavors = page_address(page);
  201. err = nfs4_proc_secinfo(inode, name, flavors);
  202. if (err < 0) {
  203. new = ERR_PTR(err);
  204. goto out;
  205. }
  206. new = nfs_find_best_sec(clnt, NFS_SERVER(inode), flavors);
  207. out:
  208. put_page(page);
  209. return new;
  210. }
  211. static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
  212. char *page, char *page2,
  213. const struct nfs4_fs_location *location)
  214. {
  215. const size_t addr_bufsize = sizeof(struct sockaddr_storage);
  216. struct net *net = rpc_net_ns(NFS_SB(mountdata->sb)->client);
  217. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  218. char *mnt_path;
  219. unsigned int maxbuflen;
  220. unsigned int s;
  221. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  222. if (IS_ERR(mnt_path))
  223. return ERR_CAST(mnt_path);
  224. mountdata->mnt_path = mnt_path;
  225. maxbuflen = mnt_path - 1 - page2;
  226. mountdata->addr = kmalloc(addr_bufsize, GFP_KERNEL);
  227. if (mountdata->addr == NULL)
  228. return ERR_PTR(-ENOMEM);
  229. for (s = 0; s < location->nservers; s++) {
  230. const struct nfs4_string *buf = &location->servers[s];
  231. if (buf->len <= 0 || buf->len >= maxbuflen)
  232. continue;
  233. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
  234. continue;
  235. mountdata->addrlen = nfs_parse_server_name(buf->data, buf->len,
  236. mountdata->addr, addr_bufsize, net);
  237. if (mountdata->addrlen == 0)
  238. continue;
  239. rpc_set_port(mountdata->addr, NFS_PORT);
  240. memcpy(page2, buf->data, buf->len);
  241. page2[buf->len] = '\0';
  242. mountdata->hostname = page2;
  243. snprintf(page, PAGE_SIZE, "%s:%s",
  244. mountdata->hostname,
  245. mountdata->mnt_path);
  246. mnt = vfs_submount(mountdata->dentry, &nfs4_referral_fs_type, page, mountdata);
  247. if (!IS_ERR(mnt))
  248. break;
  249. }
  250. kfree(mountdata->addr);
  251. return mnt;
  252. }
  253. /**
  254. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  255. * @dentry - parent directory
  256. * @locations - array of NFSv4 server location information
  257. *
  258. */
  259. static struct vfsmount *nfs_follow_referral(struct dentry *dentry,
  260. const struct nfs4_fs_locations *locations)
  261. {
  262. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  263. struct nfs_clone_mount mountdata = {
  264. .sb = dentry->d_sb,
  265. .dentry = dentry,
  266. .authflavor = NFS_SB(dentry->d_sb)->client->cl_auth->au_flavor,
  267. };
  268. char *page = NULL, *page2 = NULL;
  269. int loc, error;
  270. if (locations == NULL || locations->nlocations <= 0)
  271. goto out;
  272. dprintk("%s: referral at %pd2\n", __func__, dentry);
  273. page = (char *) __get_free_page(GFP_USER);
  274. if (!page)
  275. goto out;
  276. page2 = (char *) __get_free_page(GFP_USER);
  277. if (!page2)
  278. goto out;
  279. /* Ensure fs path is a prefix of current dentry path */
  280. error = nfs4_validate_fspath(dentry, locations, page, page2);
  281. if (error < 0) {
  282. mnt = ERR_PTR(error);
  283. goto out;
  284. }
  285. for (loc = 0; loc < locations->nlocations; loc++) {
  286. const struct nfs4_fs_location *location = &locations->locations[loc];
  287. if (location == NULL || location->nservers <= 0 ||
  288. location->rootpath.ncomponents == 0)
  289. continue;
  290. mnt = try_location(&mountdata, page, page2, location);
  291. if (!IS_ERR(mnt))
  292. break;
  293. }
  294. out:
  295. free_page((unsigned long) page);
  296. free_page((unsigned long) page2);
  297. dprintk("%s: done\n", __func__);
  298. return mnt;
  299. }
  300. /*
  301. * nfs_do_refmount - handle crossing a referral on server
  302. * @dentry - dentry of referral
  303. *
  304. */
  305. static struct vfsmount *nfs_do_refmount(struct rpc_clnt *client, struct dentry *dentry)
  306. {
  307. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  308. struct dentry *parent;
  309. struct nfs4_fs_locations *fs_locations = NULL;
  310. struct page *page;
  311. int err;
  312. /* BUG_ON(IS_ROOT(dentry)); */
  313. dprintk("%s: enter\n", __func__);
  314. page = alloc_page(GFP_KERNEL);
  315. if (page == NULL)
  316. goto out;
  317. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  318. if (fs_locations == NULL)
  319. goto out_free;
  320. /* Get locations */
  321. mnt = ERR_PTR(-ENOENT);
  322. parent = dget_parent(dentry);
  323. dprintk("%s: getting locations for %pd2\n",
  324. __func__, dentry);
  325. err = nfs4_proc_fs_locations(client, d_inode(parent), &dentry->d_name, fs_locations, page);
  326. dput(parent);
  327. if (err != 0 ||
  328. fs_locations->nlocations <= 0 ||
  329. fs_locations->fs_path.ncomponents <= 0)
  330. goto out_free;
  331. mnt = nfs_follow_referral(dentry, fs_locations);
  332. out_free:
  333. __free_page(page);
  334. kfree(fs_locations);
  335. out:
  336. dprintk("%s: done\n", __func__);
  337. return mnt;
  338. }
  339. struct vfsmount *nfs4_submount(struct nfs_server *server, struct dentry *dentry,
  340. struct nfs_fh *fh, struct nfs_fattr *fattr)
  341. {
  342. rpc_authflavor_t flavor = server->client->cl_auth->au_flavor;
  343. struct dentry *parent = dget_parent(dentry);
  344. struct inode *dir = d_inode(parent);
  345. const struct qstr *name = &dentry->d_name;
  346. struct rpc_clnt *client;
  347. struct vfsmount *mnt;
  348. /* Look it up again to get its attributes and sec flavor */
  349. client = nfs4_proc_lookup_mountpoint(dir, name, fh, fattr);
  350. dput(parent);
  351. if (IS_ERR(client))
  352. return ERR_CAST(client);
  353. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  354. mnt = nfs_do_refmount(client, dentry);
  355. goto out;
  356. }
  357. if (client->cl_auth->au_flavor != flavor)
  358. flavor = client->cl_auth->au_flavor;
  359. mnt = nfs_do_submount(dentry, fh, fattr, flavor);
  360. out:
  361. rpc_shutdown_client(client);
  362. return mnt;
  363. }
  364. /*
  365. * Try one location from the fs_locations array.
  366. *
  367. * Returns zero on success, or a negative errno value.
  368. */
  369. static int nfs4_try_replacing_one_location(struct nfs_server *server,
  370. char *page, char *page2,
  371. const struct nfs4_fs_location *location)
  372. {
  373. const size_t addr_bufsize = sizeof(struct sockaddr_storage);
  374. struct net *net = rpc_net_ns(server->client);
  375. struct sockaddr *sap;
  376. unsigned int s;
  377. size_t salen;
  378. int error;
  379. sap = kmalloc(addr_bufsize, GFP_KERNEL);
  380. if (sap == NULL)
  381. return -ENOMEM;
  382. error = -ENOENT;
  383. for (s = 0; s < location->nservers; s++) {
  384. const struct nfs4_string *buf = &location->servers[s];
  385. char *hostname;
  386. if (buf->len <= 0 || buf->len > PAGE_SIZE)
  387. continue;
  388. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len) != NULL)
  389. continue;
  390. salen = nfs_parse_server_name(buf->data, buf->len,
  391. sap, addr_bufsize, net);
  392. if (salen == 0)
  393. continue;
  394. rpc_set_port(sap, NFS_PORT);
  395. error = -ENOMEM;
  396. hostname = kstrndup(buf->data, buf->len, GFP_KERNEL);
  397. if (hostname == NULL)
  398. break;
  399. error = nfs4_update_server(server, hostname, sap, salen, net);
  400. kfree(hostname);
  401. if (error == 0)
  402. break;
  403. }
  404. kfree(sap);
  405. return error;
  406. }
  407. /**
  408. * nfs4_replace_transport - set up transport to destination server
  409. *
  410. * @server: export being migrated
  411. * @locations: fs_locations array
  412. *
  413. * Returns zero on success, or a negative errno value.
  414. *
  415. * The client tries all the entries in the "locations" array, in the
  416. * order returned by the server, until one works or the end of the
  417. * array is reached.
  418. */
  419. int nfs4_replace_transport(struct nfs_server *server,
  420. const struct nfs4_fs_locations *locations)
  421. {
  422. char *page = NULL, *page2 = NULL;
  423. int loc, error;
  424. error = -ENOENT;
  425. if (locations == NULL || locations->nlocations <= 0)
  426. goto out;
  427. error = -ENOMEM;
  428. page = (char *) __get_free_page(GFP_USER);
  429. if (!page)
  430. goto out;
  431. page2 = (char *) __get_free_page(GFP_USER);
  432. if (!page2)
  433. goto out;
  434. for (loc = 0; loc < locations->nlocations; loc++) {
  435. const struct nfs4_fs_location *location =
  436. &locations->locations[loc];
  437. if (location == NULL || location->nservers <= 0 ||
  438. location->rootpath.ncomponents == 0)
  439. continue;
  440. error = nfs4_try_replacing_one_location(server, page,
  441. page2, location);
  442. if (error == 0)
  443. break;
  444. }
  445. out:
  446. free_page((unsigned long)page);
  447. free_page((unsigned long)page2);
  448. return error;
  449. }