mount_clnt.c 12 KB

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