nfs4namespace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 nfs_server *server)
  111. {
  112. struct net *net = rpc_net_ns(server->client);
  113. ssize_t ret;
  114. ret = rpc_pton(net, string, len, sa, salen);
  115. if (ret == 0) {
  116. ret = nfs_dns_resolve_name(net, string, len, sa, salen);
  117. if (ret < 0)
  118. ret = 0;
  119. }
  120. return ret;
  121. }
  122. /**
  123. * nfs_find_best_sec - Find a security mechanism supported locally
  124. * @server: NFS server struct
  125. * @flavors: List of security tuples returned by SECINFO procedure
  126. *
  127. * Return the pseudoflavor of the first security mechanism in
  128. * "flavors" that is locally supported. Return RPC_AUTH_UNIX if
  129. * no matching flavor is found in the array. The "flavors" array
  130. * is searched in the order returned from the server, per RFC 3530
  131. * recommendation.
  132. */
  133. static rpc_authflavor_t nfs_find_best_sec(struct nfs_server *server,
  134. struct nfs4_secinfo_flavors *flavors)
  135. {
  136. rpc_authflavor_t pseudoflavor;
  137. struct nfs4_secinfo4 *secinfo;
  138. unsigned int i;
  139. for (i = 0; i < flavors->num_flavors; i++) {
  140. secinfo = &flavors->flavors[i];
  141. switch (secinfo->flavor) {
  142. case RPC_AUTH_NULL:
  143. case RPC_AUTH_UNIX:
  144. case RPC_AUTH_GSS:
  145. pseudoflavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  146. &secinfo->flavor_info);
  147. /* make sure pseudoflavor matches sec= mount opt */
  148. if (pseudoflavor != RPC_AUTH_MAXFLAVOR &&
  149. nfs_auth_info_match(&server->auth_info,
  150. pseudoflavor))
  151. return pseudoflavor;
  152. break;
  153. }
  154. }
  155. /* if there were any sec= options then nothing matched */
  156. if (server->auth_info.flavor_len > 0)
  157. return -EPERM;
  158. return RPC_AUTH_UNIX;
  159. }
  160. static rpc_authflavor_t nfs4_negotiate_security(struct inode *inode, struct qstr *name)
  161. {
  162. struct page *page;
  163. struct nfs4_secinfo_flavors *flavors;
  164. rpc_authflavor_t flavor;
  165. int err;
  166. page = alloc_page(GFP_KERNEL);
  167. if (!page)
  168. return -ENOMEM;
  169. flavors = page_address(page);
  170. err = nfs4_proc_secinfo(inode, name, flavors);
  171. if (err < 0) {
  172. flavor = err;
  173. goto out;
  174. }
  175. flavor = nfs_find_best_sec(NFS_SERVER(inode), flavors);
  176. out:
  177. put_page(page);
  178. return flavor;
  179. }
  180. /*
  181. * Please call rpc_shutdown_client() when you are done with this client.
  182. */
  183. struct rpc_clnt *nfs4_create_sec_client(struct rpc_clnt *clnt, struct inode *inode,
  184. struct qstr *name)
  185. {
  186. rpc_authflavor_t flavor;
  187. flavor = nfs4_negotiate_security(inode, name);
  188. if ((int)flavor < 0)
  189. return ERR_PTR((int)flavor);
  190. return rpc_clone_client_set_auth(clnt, flavor);
  191. }
  192. static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
  193. char *page, char *page2,
  194. const struct nfs4_fs_location *location)
  195. {
  196. const size_t addr_bufsize = sizeof(struct sockaddr_storage);
  197. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  198. char *mnt_path;
  199. unsigned int maxbuflen;
  200. unsigned int s;
  201. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  202. if (IS_ERR(mnt_path))
  203. return ERR_CAST(mnt_path);
  204. mountdata->mnt_path = mnt_path;
  205. maxbuflen = mnt_path - 1 - page2;
  206. mountdata->addr = kmalloc(addr_bufsize, GFP_KERNEL);
  207. if (mountdata->addr == NULL)
  208. return ERR_PTR(-ENOMEM);
  209. for (s = 0; s < location->nservers; s++) {
  210. const struct nfs4_string *buf = &location->servers[s];
  211. if (buf->len <= 0 || buf->len >= maxbuflen)
  212. continue;
  213. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
  214. continue;
  215. mountdata->addrlen = nfs_parse_server_name(buf->data, buf->len,
  216. mountdata->addr, addr_bufsize,
  217. NFS_SB(mountdata->sb));
  218. if (mountdata->addrlen == 0)
  219. continue;
  220. rpc_set_port(mountdata->addr, NFS_PORT);
  221. memcpy(page2, buf->data, buf->len);
  222. page2[buf->len] = '\0';
  223. mountdata->hostname = page2;
  224. snprintf(page, PAGE_SIZE, "%s:%s",
  225. mountdata->hostname,
  226. mountdata->mnt_path);
  227. mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
  228. if (!IS_ERR(mnt))
  229. break;
  230. }
  231. kfree(mountdata->addr);
  232. return mnt;
  233. }
  234. /**
  235. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  236. * @dentry - parent directory
  237. * @locations - array of NFSv4 server location information
  238. *
  239. */
  240. static struct vfsmount *nfs_follow_referral(struct dentry *dentry,
  241. const struct nfs4_fs_locations *locations)
  242. {
  243. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  244. struct nfs_clone_mount mountdata = {
  245. .sb = dentry->d_sb,
  246. .dentry = dentry,
  247. .authflavor = NFS_SB(dentry->d_sb)->client->cl_auth->au_flavor,
  248. };
  249. char *page = NULL, *page2 = NULL;
  250. int loc, error;
  251. if (locations == NULL || locations->nlocations <= 0)
  252. goto out;
  253. dprintk("%s: referral at %s/%s\n", __func__,
  254. dentry->d_parent->d_name.name, dentry->d_name.name);
  255. page = (char *) __get_free_page(GFP_USER);
  256. if (!page)
  257. goto out;
  258. page2 = (char *) __get_free_page(GFP_USER);
  259. if (!page2)
  260. goto out;
  261. /* Ensure fs path is a prefix of current dentry path */
  262. error = nfs4_validate_fspath(dentry, locations, page, page2);
  263. if (error < 0) {
  264. mnt = ERR_PTR(error);
  265. goto out;
  266. }
  267. for (loc = 0; loc < locations->nlocations; loc++) {
  268. const struct nfs4_fs_location *location = &locations->locations[loc];
  269. if (location == NULL || location->nservers <= 0 ||
  270. location->rootpath.ncomponents == 0)
  271. continue;
  272. mnt = try_location(&mountdata, page, page2, location);
  273. if (!IS_ERR(mnt))
  274. break;
  275. }
  276. out:
  277. free_page((unsigned long) page);
  278. free_page((unsigned long) page2);
  279. dprintk("%s: done\n", __func__);
  280. return mnt;
  281. }
  282. /*
  283. * nfs_do_refmount - handle crossing a referral on server
  284. * @dentry - dentry of referral
  285. *
  286. */
  287. static struct vfsmount *nfs_do_refmount(struct rpc_clnt *client, struct dentry *dentry)
  288. {
  289. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  290. struct dentry *parent;
  291. struct nfs4_fs_locations *fs_locations = NULL;
  292. struct page *page;
  293. int err;
  294. /* BUG_ON(IS_ROOT(dentry)); */
  295. dprintk("%s: enter\n", __func__);
  296. page = alloc_page(GFP_KERNEL);
  297. if (page == NULL)
  298. goto out;
  299. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  300. if (fs_locations == NULL)
  301. goto out_free;
  302. /* Get locations */
  303. mnt = ERR_PTR(-ENOENT);
  304. parent = dget_parent(dentry);
  305. dprintk("%s: getting locations for %s/%s\n",
  306. __func__, parent->d_name.name, dentry->d_name.name);
  307. err = nfs4_proc_fs_locations(client, parent->d_inode, &dentry->d_name, fs_locations, page);
  308. dput(parent);
  309. if (err != 0 ||
  310. fs_locations->nlocations <= 0 ||
  311. fs_locations->fs_path.ncomponents <= 0)
  312. goto out_free;
  313. mnt = nfs_follow_referral(dentry, fs_locations);
  314. out_free:
  315. __free_page(page);
  316. kfree(fs_locations);
  317. out:
  318. dprintk("%s: done\n", __func__);
  319. return mnt;
  320. }
  321. struct vfsmount *nfs4_submount(struct nfs_server *server, struct dentry *dentry,
  322. struct nfs_fh *fh, struct nfs_fattr *fattr)
  323. {
  324. rpc_authflavor_t flavor = server->client->cl_auth->au_flavor;
  325. struct dentry *parent = dget_parent(dentry);
  326. struct inode *dir = parent->d_inode;
  327. struct qstr *name = &dentry->d_name;
  328. struct rpc_clnt *client;
  329. struct vfsmount *mnt;
  330. /* Look it up again to get its attributes and sec flavor */
  331. client = nfs4_proc_lookup_mountpoint(dir, name, fh, fattr);
  332. dput(parent);
  333. if (IS_ERR(client))
  334. return ERR_CAST(client);
  335. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  336. mnt = nfs_do_refmount(client, dentry);
  337. goto out;
  338. }
  339. if (client->cl_auth->au_flavor != flavor)
  340. flavor = client->cl_auth->au_flavor;
  341. else {
  342. rpc_authflavor_t new = nfs4_negotiate_security(dir, name);
  343. if ((int)new >= 0)
  344. flavor = new;
  345. }
  346. mnt = nfs_do_submount(dentry, fh, fattr, flavor);
  347. out:
  348. rpc_shutdown_client(client);
  349. return mnt;
  350. }
  351. /*
  352. * Try one location from the fs_locations array.
  353. *
  354. * Returns zero on success, or a negative errno value.
  355. */
  356. static int nfs4_try_replacing_one_location(struct nfs_server *server,
  357. char *page, char *page2,
  358. const struct nfs4_fs_location *location)
  359. {
  360. const size_t addr_bufsize = sizeof(struct sockaddr_storage);
  361. struct sockaddr *sap;
  362. unsigned int s;
  363. size_t salen;
  364. int error;
  365. sap = kmalloc(addr_bufsize, GFP_KERNEL);
  366. if (sap == NULL)
  367. return -ENOMEM;
  368. error = -ENOENT;
  369. for (s = 0; s < location->nservers; s++) {
  370. const struct nfs4_string *buf = &location->servers[s];
  371. char *hostname;
  372. if (buf->len <= 0 || buf->len > PAGE_SIZE)
  373. continue;
  374. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len) != NULL)
  375. continue;
  376. salen = nfs_parse_server_name(buf->data, buf->len,
  377. sap, addr_bufsize, server);
  378. if (salen == 0)
  379. continue;
  380. rpc_set_port(sap, NFS_PORT);
  381. error = -ENOMEM;
  382. hostname = kstrndup(buf->data, buf->len, GFP_KERNEL);
  383. if (hostname == NULL)
  384. break;
  385. error = nfs4_update_server(server, hostname, sap, salen);
  386. kfree(hostname);
  387. if (error == 0)
  388. break;
  389. }
  390. kfree(sap);
  391. return error;
  392. }
  393. /**
  394. * nfs4_replace_transport - set up transport to destination server
  395. *
  396. * @server: export being migrated
  397. * @locations: fs_locations array
  398. *
  399. * Returns zero on success, or a negative errno value.
  400. *
  401. * The client tries all the entries in the "locations" array, in the
  402. * order returned by the server, until one works or the end of the
  403. * array is reached.
  404. */
  405. int nfs4_replace_transport(struct nfs_server *server,
  406. const struct nfs4_fs_locations *locations)
  407. {
  408. char *page = NULL, *page2 = NULL;
  409. int loc, error;
  410. error = -ENOENT;
  411. if (locations == NULL || locations->nlocations <= 0)
  412. goto out;
  413. error = -ENOMEM;
  414. page = (char *) __get_free_page(GFP_USER);
  415. if (!page)
  416. goto out;
  417. page2 = (char *) __get_free_page(GFP_USER);
  418. if (!page2)
  419. goto out;
  420. for (loc = 0; loc < locations->nlocations; loc++) {
  421. const struct nfs4_fs_location *location =
  422. &locations->locations[loc];
  423. if (location == NULL || location->nservers <= 0 ||
  424. location->rootpath.ncomponents == 0)
  425. continue;
  426. error = nfs4_try_replacing_one_location(server, page,
  427. page2, location);
  428. if (error == 0)
  429. break;
  430. }
  431. out:
  432. free_page((unsigned long)page);
  433. free_page((unsigned long)page2);
  434. return error;
  435. }