mount_clnt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * In-kernel MOUNT protocol client
  4. *
  5. * Copyright (C) 1997, Olaf Kirch <[email protected]>
  6. */
  7. #include <linux/types.h>
  8. #include <linux/socket.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/uio.h>
  12. #include <linux/net.h>
  13. #include <linux/in.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/sunrpc/sched.h>
  16. #include <linux/nfs_fs.h>
  17. #include "internal.h"
  18. #define NFSDBG_FACILITY NFSDBG_MOUNT
  19. /*
  20. * Defined by RFC 1094, section A.3; and RFC 1813, section 5.1.4
  21. */
  22. #define MNTPATHLEN (1024)
  23. /*
  24. * XDR data type sizes
  25. */
  26. #define encode_dirpath_sz (1 + XDR_QUADLEN(MNTPATHLEN))
  27. #define MNT_status_sz (1)
  28. #define MNT_fhandle_sz XDR_QUADLEN(NFS2_FHSIZE)
  29. #define MNT_fhandlev3_sz XDR_QUADLEN(NFS3_FHSIZE)
  30. #define MNT_authflav3_sz (1 + NFS_MAX_SECFLAVORS)
  31. /*
  32. * XDR argument and result sizes
  33. */
  34. #define MNT_enc_dirpath_sz encode_dirpath_sz
  35. #define MNT_dec_mountres_sz (MNT_status_sz + MNT_fhandle_sz)
  36. #define MNT_dec_mountres3_sz (MNT_status_sz + MNT_fhandlev3_sz + \
  37. MNT_authflav3_sz)
  38. /*
  39. * Defined by RFC 1094, section A.5
  40. */
  41. enum {
  42. MOUNTPROC_NULL = 0,
  43. MOUNTPROC_MNT = 1,
  44. MOUNTPROC_DUMP = 2,
  45. MOUNTPROC_UMNT = 3,
  46. MOUNTPROC_UMNTALL = 4,
  47. MOUNTPROC_EXPORT = 5,
  48. };
  49. /*
  50. * Defined by RFC 1813, section 5.2
  51. */
  52. enum {
  53. MOUNTPROC3_NULL = 0,
  54. MOUNTPROC3_MNT = 1,
  55. MOUNTPROC3_DUMP = 2,
  56. MOUNTPROC3_UMNT = 3,
  57. MOUNTPROC3_UMNTALL = 4,
  58. MOUNTPROC3_EXPORT = 5,
  59. };
  60. static const struct rpc_program mnt_program;
  61. /*
  62. * Defined by OpenGroup XNFS Version 3W, chapter 8
  63. */
  64. enum mountstat {
  65. MNT_OK = 0,
  66. MNT_EPERM = 1,
  67. MNT_ENOENT = 2,
  68. MNT_EACCES = 13,
  69. MNT_EINVAL = 22,
  70. };
  71. static struct {
  72. u32 status;
  73. int errno;
  74. } mnt_errtbl[] = {
  75. { .status = MNT_OK, .errno = 0, },
  76. { .status = MNT_EPERM, .errno = -EPERM, },
  77. { .status = MNT_ENOENT, .errno = -ENOENT, },
  78. { .status = MNT_EACCES, .errno = -EACCES, },
  79. { .status = MNT_EINVAL, .errno = -EINVAL, },
  80. };
  81. /*
  82. * Defined by RFC 1813, section 5.1.5
  83. */
  84. enum mountstat3 {
  85. MNT3_OK = 0, /* no error */
  86. MNT3ERR_PERM = 1, /* Not owner */
  87. MNT3ERR_NOENT = 2, /* No such file or directory */
  88. MNT3ERR_IO = 5, /* I/O error */
  89. MNT3ERR_ACCES = 13, /* Permission denied */
  90. MNT3ERR_NOTDIR = 20, /* Not a directory */
  91. MNT3ERR_INVAL = 22, /* Invalid argument */
  92. MNT3ERR_NAMETOOLONG = 63, /* Filename too long */
  93. MNT3ERR_NOTSUPP = 10004, /* Operation not supported */
  94. MNT3ERR_SERVERFAULT = 10006, /* A failure on the server */
  95. };
  96. static struct {
  97. u32 status;
  98. int errno;
  99. } mnt3_errtbl[] = {
  100. { .status = MNT3_OK, .errno = 0, },
  101. { .status = MNT3ERR_PERM, .errno = -EPERM, },
  102. { .status = MNT3ERR_NOENT, .errno = -ENOENT, },
  103. { .status = MNT3ERR_IO, .errno = -EIO, },
  104. { .status = MNT3ERR_ACCES, .errno = -EACCES, },
  105. { .status = MNT3ERR_NOTDIR, .errno = -ENOTDIR, },
  106. { .status = MNT3ERR_INVAL, .errno = -EINVAL, },
  107. { .status = MNT3ERR_NAMETOOLONG, .errno = -ENAMETOOLONG, },
  108. { .status = MNT3ERR_NOTSUPP, .errno = -ENOTSUPP, },
  109. { .status = MNT3ERR_SERVERFAULT, .errno = -EREMOTEIO, },
  110. };
  111. struct mountres {
  112. int errno;
  113. struct nfs_fh *fh;
  114. unsigned int *auth_count;
  115. rpc_authflavor_t *auth_flavors;
  116. };
  117. struct mnt_fhstatus {
  118. u32 status;
  119. struct nfs_fh *fh;
  120. };
  121. /**
  122. * nfs_mount - Obtain an NFS file handle for the given host and path
  123. * @info: pointer to mount request arguments
  124. * @timeo: deciseconds the mount waits for a response before it retries
  125. * @retrans: number of times the mount retries a request
  126. *
  127. * Uses timeout parameters specified by caller. On successful return, the
  128. * auth_flavs list and auth_flav_len will be populated with the list from the
  129. * server or a faked-up list if the server didn't provide one.
  130. */
  131. int nfs_mount(struct nfs_mount_request *info, int timeo, int retrans)
  132. {
  133. struct rpc_timeout mnt_timeout;
  134. struct mountres result = {
  135. .fh = info->fh,
  136. .auth_count = info->auth_flav_len,
  137. .auth_flavors = info->auth_flavs,
  138. };
  139. struct rpc_message msg = {
  140. .rpc_argp = info->dirpath,
  141. .rpc_resp = &result,
  142. };
  143. struct rpc_create_args args = {
  144. .net = info->net,
  145. .protocol = info->protocol,
  146. .address = (struct sockaddr *)info->sap,
  147. .addrsize = info->salen,
  148. .timeout = &mnt_timeout,
  149. .servername = info->hostname,
  150. .program = &mnt_program,
  151. .version = info->version,
  152. .authflavor = RPC_AUTH_UNIX,
  153. .cred = current_cred(),
  154. };
  155. struct rpc_clnt *mnt_clnt;
  156. int status;
  157. dprintk("NFS: sending MNT request for %s:%s\n",
  158. (info->hostname ? info->hostname : "server"),
  159. info->dirpath);
  160. if (strlen(info->dirpath) > MNTPATHLEN)
  161. return -ENAMETOOLONG;
  162. if (info->noresvport)
  163. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  164. nfs_init_timeout_values(&mnt_timeout, info->protocol, timeo, retrans);
  165. mnt_clnt = rpc_create(&args);
  166. if (IS_ERR(mnt_clnt))
  167. goto out_clnt_err;
  168. if (info->version == NFS_MNT3_VERSION)
  169. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
  170. else
  171. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT];
  172. status = rpc_call_sync(mnt_clnt, &msg, RPC_TASK_SOFT|RPC_TASK_TIMEOUT);
  173. rpc_shutdown_client(mnt_clnt);
  174. if (status < 0)
  175. goto out_call_err;
  176. if (result.errno != 0)
  177. goto out_mnt_err;
  178. dprintk("NFS: MNT request succeeded\n");
  179. status = 0;
  180. /*
  181. * If the server didn't provide a flavor list, allow the
  182. * client to try any flavor.
  183. */
  184. if (info->version != NFS_MNT3_VERSION || *info->auth_flav_len == 0) {
  185. dprintk("NFS: Faking up auth_flavs list\n");
  186. info->auth_flavs[0] = RPC_AUTH_NULL;
  187. *info->auth_flav_len = 1;
  188. }
  189. out:
  190. return status;
  191. out_clnt_err:
  192. status = PTR_ERR(mnt_clnt);
  193. dprintk("NFS: failed to create MNT RPC client, status=%d\n", status);
  194. goto out;
  195. out_call_err:
  196. dprintk("NFS: MNT request failed, status=%d\n", status);
  197. goto out;
  198. out_mnt_err:
  199. dprintk("NFS: MNT server returned result %d\n", result.errno);
  200. status = result.errno;
  201. goto out;
  202. }
  203. /**
  204. * nfs_umount - Notify a server that we have unmounted this export
  205. * @info: pointer to umount request arguments
  206. *
  207. * MOUNTPROC_UMNT is advisory, so we set a short timeout, and always
  208. * use UDP.
  209. */
  210. void nfs_umount(const struct nfs_mount_request *info)
  211. {
  212. static const struct rpc_timeout nfs_umnt_timeout = {
  213. .to_initval = 1 * HZ,
  214. .to_maxval = 3 * HZ,
  215. .to_retries = 2,
  216. };
  217. struct rpc_create_args args = {
  218. .net = info->net,
  219. .protocol = IPPROTO_UDP,
  220. .address = (struct sockaddr *)info->sap,
  221. .addrsize = info->salen,
  222. .timeout = &nfs_umnt_timeout,
  223. .servername = info->hostname,
  224. .program = &mnt_program,
  225. .version = info->version,
  226. .authflavor = RPC_AUTH_UNIX,
  227. .flags = RPC_CLNT_CREATE_NOPING,
  228. .cred = current_cred(),
  229. };
  230. struct rpc_message msg = {
  231. .rpc_argp = info->dirpath,
  232. };
  233. struct rpc_clnt *clnt;
  234. int status;
  235. if (strlen(info->dirpath) > MNTPATHLEN)
  236. return;
  237. if (info->noresvport)
  238. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  239. clnt = rpc_create(&args);
  240. if (IS_ERR(clnt))
  241. goto out_clnt_err;
  242. dprintk("NFS: sending UMNT request for %s:%s\n",
  243. (info->hostname ? info->hostname : "server"), info->dirpath);
  244. if (info->version == NFS_MNT3_VERSION)
  245. msg.rpc_proc = &clnt->cl_procinfo[MOUNTPROC3_UMNT];
  246. else
  247. msg.rpc_proc = &clnt->cl_procinfo[MOUNTPROC_UMNT];
  248. status = rpc_call_sync(clnt, &msg, 0);
  249. rpc_shutdown_client(clnt);
  250. if (unlikely(status < 0))
  251. goto out_call_err;
  252. return;
  253. out_clnt_err:
  254. dprintk("NFS: failed to create UMNT RPC client, status=%ld\n",
  255. PTR_ERR(clnt));
  256. return;
  257. out_call_err:
  258. dprintk("NFS: UMNT request failed, status=%d\n", status);
  259. }
  260. /*
  261. * XDR encode/decode functions for MOUNT
  262. */
  263. static void encode_mntdirpath(struct xdr_stream *xdr, const char *pathname)
  264. {
  265. const u32 pathname_len = strlen(pathname);
  266. __be32 *p;
  267. p = xdr_reserve_space(xdr, 4 + pathname_len);
  268. xdr_encode_opaque(p, pathname, pathname_len);
  269. }
  270. static void mnt_xdr_enc_dirpath(struct rpc_rqst *req, struct xdr_stream *xdr,
  271. const void *dirpath)
  272. {
  273. encode_mntdirpath(xdr, dirpath);
  274. }
  275. /*
  276. * RFC 1094: "A non-zero status indicates some sort of error. In this
  277. * case, the status is a UNIX error number." This can be problematic
  278. * if the server and client use different errno values for the same
  279. * error.
  280. *
  281. * However, the OpenGroup XNFS spec provides a simple mapping that is
  282. * independent of local errno values on the server and the client.
  283. */
  284. static int decode_status(struct xdr_stream *xdr, struct mountres *res)
  285. {
  286. unsigned int i;
  287. u32 status;
  288. __be32 *p;
  289. p = xdr_inline_decode(xdr, 4);
  290. if (unlikely(p == NULL))
  291. return -EIO;
  292. status = be32_to_cpup(p);
  293. for (i = 0; i < ARRAY_SIZE(mnt_errtbl); i++) {
  294. if (mnt_errtbl[i].status == status) {
  295. res->errno = mnt_errtbl[i].errno;
  296. return 0;
  297. }
  298. }
  299. dprintk("NFS: unrecognized MNT status code: %u\n", status);
  300. res->errno = -EACCES;
  301. return 0;
  302. }
  303. static int decode_fhandle(struct xdr_stream *xdr, struct mountres *res)
  304. {
  305. struct nfs_fh *fh = res->fh;
  306. __be32 *p;
  307. p = xdr_inline_decode(xdr, NFS2_FHSIZE);
  308. if (unlikely(p == NULL))
  309. return -EIO;
  310. fh->size = NFS2_FHSIZE;
  311. memcpy(fh->data, p, NFS2_FHSIZE);
  312. return 0;
  313. }
  314. static int mnt_xdr_dec_mountres(struct rpc_rqst *req,
  315. struct xdr_stream *xdr,
  316. void *data)
  317. {
  318. struct mountres *res = data;
  319. int status;
  320. status = decode_status(xdr, res);
  321. if (unlikely(status != 0 || res->errno != 0))
  322. return status;
  323. return decode_fhandle(xdr, res);
  324. }
  325. static int decode_fhs_status(struct xdr_stream *xdr, struct mountres *res)
  326. {
  327. unsigned int i;
  328. u32 status;
  329. __be32 *p;
  330. p = xdr_inline_decode(xdr, 4);
  331. if (unlikely(p == NULL))
  332. return -EIO;
  333. status = be32_to_cpup(p);
  334. for (i = 0; i < ARRAY_SIZE(mnt3_errtbl); i++) {
  335. if (mnt3_errtbl[i].status == status) {
  336. res->errno = mnt3_errtbl[i].errno;
  337. return 0;
  338. }
  339. }
  340. dprintk("NFS: unrecognized MNT3 status code: %u\n", status);
  341. res->errno = -EACCES;
  342. return 0;
  343. }
  344. static int decode_fhandle3(struct xdr_stream *xdr, struct mountres *res)
  345. {
  346. struct nfs_fh *fh = res->fh;
  347. u32 size;
  348. __be32 *p;
  349. p = xdr_inline_decode(xdr, 4);
  350. if (unlikely(p == NULL))
  351. return -EIO;
  352. size = be32_to_cpup(p);
  353. if (size > NFS3_FHSIZE || size == 0)
  354. return -EIO;
  355. p = xdr_inline_decode(xdr, size);
  356. if (unlikely(p == NULL))
  357. return -EIO;
  358. fh->size = size;
  359. memcpy(fh->data, p, size);
  360. return 0;
  361. }
  362. static int decode_auth_flavors(struct xdr_stream *xdr, struct mountres *res)
  363. {
  364. rpc_authflavor_t *flavors = res->auth_flavors;
  365. unsigned int *count = res->auth_count;
  366. u32 entries, i;
  367. __be32 *p;
  368. if (*count == 0)
  369. return 0;
  370. p = xdr_inline_decode(xdr, 4);
  371. if (unlikely(p == NULL))
  372. return -EIO;
  373. entries = be32_to_cpup(p);
  374. dprintk("NFS: received %u auth flavors\n", entries);
  375. if (entries > NFS_MAX_SECFLAVORS)
  376. entries = NFS_MAX_SECFLAVORS;
  377. p = xdr_inline_decode(xdr, 4 * entries);
  378. if (unlikely(p == NULL))
  379. return -EIO;
  380. if (entries > *count)
  381. entries = *count;
  382. for (i = 0; i < entries; i++) {
  383. flavors[i] = be32_to_cpup(p++);
  384. dprintk("NFS: auth flavor[%u]: %d\n", i, flavors[i]);
  385. }
  386. *count = i;
  387. return 0;
  388. }
  389. static int mnt_xdr_dec_mountres3(struct rpc_rqst *req,
  390. struct xdr_stream *xdr,
  391. void *data)
  392. {
  393. struct mountres *res = data;
  394. int status;
  395. status = decode_fhs_status(xdr, res);
  396. if (unlikely(status != 0 || res->errno != 0))
  397. return status;
  398. status = decode_fhandle3(xdr, res);
  399. if (unlikely(status != 0)) {
  400. res->errno = -EBADHANDLE;
  401. return 0;
  402. }
  403. return decode_auth_flavors(xdr, res);
  404. }
  405. static const struct rpc_procinfo mnt_procedures[] = {
  406. [MOUNTPROC_MNT] = {
  407. .p_proc = MOUNTPROC_MNT,
  408. .p_encode = mnt_xdr_enc_dirpath,
  409. .p_decode = mnt_xdr_dec_mountres,
  410. .p_arglen = MNT_enc_dirpath_sz,
  411. .p_replen = MNT_dec_mountres_sz,
  412. .p_statidx = MOUNTPROC_MNT,
  413. .p_name = "MOUNT",
  414. },
  415. [MOUNTPROC_UMNT] = {
  416. .p_proc = MOUNTPROC_UMNT,
  417. .p_encode = mnt_xdr_enc_dirpath,
  418. .p_arglen = MNT_enc_dirpath_sz,
  419. .p_statidx = MOUNTPROC_UMNT,
  420. .p_name = "UMOUNT",
  421. },
  422. };
  423. static const struct rpc_procinfo mnt3_procedures[] = {
  424. [MOUNTPROC3_MNT] = {
  425. .p_proc = MOUNTPROC3_MNT,
  426. .p_encode = mnt_xdr_enc_dirpath,
  427. .p_decode = mnt_xdr_dec_mountres3,
  428. .p_arglen = MNT_enc_dirpath_sz,
  429. .p_replen = MNT_dec_mountres3_sz,
  430. .p_statidx = MOUNTPROC3_MNT,
  431. .p_name = "MOUNT",
  432. },
  433. [MOUNTPROC3_UMNT] = {
  434. .p_proc = MOUNTPROC3_UMNT,
  435. .p_encode = mnt_xdr_enc_dirpath,
  436. .p_arglen = MNT_enc_dirpath_sz,
  437. .p_statidx = MOUNTPROC3_UMNT,
  438. .p_name = "UMOUNT",
  439. },
  440. };
  441. static unsigned int mnt_counts[ARRAY_SIZE(mnt_procedures)];
  442. static const struct rpc_version mnt_version1 = {
  443. .number = 1,
  444. .nrprocs = ARRAY_SIZE(mnt_procedures),
  445. .procs = mnt_procedures,
  446. .counts = mnt_counts,
  447. };
  448. static unsigned int mnt3_counts[ARRAY_SIZE(mnt3_procedures)];
  449. static const struct rpc_version mnt_version3 = {
  450. .number = 3,
  451. .nrprocs = ARRAY_SIZE(mnt3_procedures),
  452. .procs = mnt3_procedures,
  453. .counts = mnt3_counts,
  454. };
  455. static const struct rpc_version *mnt_version[] = {
  456. NULL,
  457. &mnt_version1,
  458. NULL,
  459. &mnt_version3,
  460. };
  461. static struct rpc_stat mnt_stats;
  462. static const struct rpc_program mnt_program = {
  463. .name = "mount",
  464. .number = NFS_MNT_PROGRAM,
  465. .nrvers = ARRAY_SIZE(mnt_version),
  466. .version = mnt_version,
  467. .stats = &mnt_stats,
  468. };