mount_clnt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. #define NFSDBG_FACILITY NFSDBG_MOUNT
  18. /*
  19. * Defined by RFC 1094, section A.3; and RFC 1813, section 5.1.4
  20. */
  21. #define MNTPATHLEN (1024)
  22. /*
  23. * XDR data type sizes
  24. */
  25. #define encode_dirpath_sz (1 + XDR_QUADLEN(MNTPATHLEN))
  26. #define MNT_status_sz (1)
  27. #define MNT_fhs_status_sz (1)
  28. #define MNT_fhandle_sz XDR_QUADLEN(NFS2_FHSIZE)
  29. #define MNT_fhandle3_sz (1 + 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_fhandle_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. *
  125. * Uses default timeout parameters specified by underlying transport. On
  126. * successful return, the auth_flavs list and auth_flav_len will be populated
  127. * with the list from the server or a faked-up list if the server didn't
  128. * provide one.
  129. */
  130. int nfs_mount(struct nfs_mount_request *info)
  131. {
  132. struct mountres result = {
  133. .fh = info->fh,
  134. .auth_count = info->auth_flav_len,
  135. .auth_flavors = info->auth_flavs,
  136. };
  137. struct rpc_message msg = {
  138. .rpc_argp = info->dirpath,
  139. .rpc_resp = &result,
  140. };
  141. struct rpc_create_args args = {
  142. .net = info->net,
  143. .protocol = info->protocol,
  144. .address = info->sap,
  145. .addrsize = info->salen,
  146. .servername = info->hostname,
  147. .program = &mnt_program,
  148. .version = info->version,
  149. .authflavor = RPC_AUTH_UNIX,
  150. };
  151. struct rpc_clnt *mnt_clnt;
  152. int status;
  153. dprintk("NFS: sending MNT request for %s:%s\n",
  154. (info->hostname ? info->hostname : "server"),
  155. info->dirpath);
  156. if (strlen(info->dirpath) > MNTPATHLEN)
  157. return -ENAMETOOLONG;
  158. if (info->noresvport)
  159. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  160. mnt_clnt = rpc_create(&args);
  161. if (IS_ERR(mnt_clnt))
  162. goto out_clnt_err;
  163. if (info->version == NFS_MNT3_VERSION)
  164. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
  165. else
  166. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT];
  167. status = rpc_call_sync(mnt_clnt, &msg, RPC_TASK_SOFT|RPC_TASK_TIMEOUT);
  168. rpc_shutdown_client(mnt_clnt);
  169. if (status < 0)
  170. goto out_call_err;
  171. if (result.errno != 0)
  172. goto out_mnt_err;
  173. dprintk("NFS: MNT request succeeded\n");
  174. status = 0;
  175. /*
  176. * If the server didn't provide a flavor list, allow the
  177. * client to try any flavor.
  178. */
  179. if (info->version != NFS_MNT3_VERSION || *info->auth_flav_len == 0) {
  180. dprintk("NFS: Faking up auth_flavs list\n");
  181. info->auth_flavs[0] = RPC_AUTH_NULL;
  182. *info->auth_flav_len = 1;
  183. }
  184. out:
  185. return status;
  186. out_clnt_err:
  187. status = PTR_ERR(mnt_clnt);
  188. dprintk("NFS: failed to create MNT RPC client, status=%d\n", status);
  189. goto out;
  190. out_call_err:
  191. dprintk("NFS: MNT request failed, status=%d\n", status);
  192. goto out;
  193. out_mnt_err:
  194. dprintk("NFS: MNT server returned result %d\n", result.errno);
  195. status = result.errno;
  196. goto out;
  197. }
  198. /**
  199. * nfs_umount - Notify a server that we have unmounted this export
  200. * @info: pointer to umount request arguments
  201. *
  202. * MOUNTPROC_UMNT is advisory, so we set a short timeout, and always
  203. * use UDP.
  204. */
  205. void nfs_umount(const struct nfs_mount_request *info)
  206. {
  207. static const struct rpc_timeout nfs_umnt_timeout = {
  208. .to_initval = 1 * HZ,
  209. .to_maxval = 3 * HZ,
  210. .to_retries = 2,
  211. };
  212. struct rpc_create_args args = {
  213. .net = info->net,
  214. .protocol = IPPROTO_UDP,
  215. .address = info->sap,
  216. .addrsize = info->salen,
  217. .timeout = &nfs_umnt_timeout,
  218. .servername = info->hostname,
  219. .program = &mnt_program,
  220. .version = info->version,
  221. .authflavor = RPC_AUTH_UNIX,
  222. .flags = RPC_CLNT_CREATE_NOPING,
  223. };
  224. struct rpc_message msg = {
  225. .rpc_argp = info->dirpath,
  226. };
  227. struct rpc_clnt *clnt;
  228. int status;
  229. if (strlen(info->dirpath) > MNTPATHLEN)
  230. return;
  231. if (info->noresvport)
  232. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  233. clnt = rpc_create(&args);
  234. if (IS_ERR(clnt))
  235. goto out_clnt_err;
  236. dprintk("NFS: sending UMNT request for %s:%s\n",
  237. (info->hostname ? info->hostname : "server"), info->dirpath);
  238. if (info->version == NFS_MNT3_VERSION)
  239. msg.rpc_proc = &clnt->cl_procinfo[MOUNTPROC3_UMNT];
  240. else
  241. msg.rpc_proc = &clnt->cl_procinfo[MOUNTPROC_UMNT];
  242. status = rpc_call_sync(clnt, &msg, 0);
  243. rpc_shutdown_client(clnt);
  244. if (unlikely(status < 0))
  245. goto out_call_err;
  246. return;
  247. out_clnt_err:
  248. dprintk("NFS: failed to create UMNT RPC client, status=%ld\n",
  249. PTR_ERR(clnt));
  250. return;
  251. out_call_err:
  252. dprintk("NFS: UMNT request failed, status=%d\n", status);
  253. }
  254. /*
  255. * XDR encode/decode functions for MOUNT
  256. */
  257. static void encode_mntdirpath(struct xdr_stream *xdr, const char *pathname)
  258. {
  259. const u32 pathname_len = strlen(pathname);
  260. __be32 *p;
  261. p = xdr_reserve_space(xdr, 4 + pathname_len);
  262. xdr_encode_opaque(p, pathname, pathname_len);
  263. }
  264. static void mnt_xdr_enc_dirpath(struct rpc_rqst *req, struct xdr_stream *xdr,
  265. const char *dirpath)
  266. {
  267. encode_mntdirpath(xdr, dirpath);
  268. }
  269. /*
  270. * RFC 1094: "A non-zero status indicates some sort of error. In this
  271. * case, the status is a UNIX error number." This can be problematic
  272. * if the server and client use different errno values for the same
  273. * error.
  274. *
  275. * However, the OpenGroup XNFS spec provides a simple mapping that is
  276. * independent of local errno values on the server and the client.
  277. */
  278. static int decode_status(struct xdr_stream *xdr, struct mountres *res)
  279. {
  280. unsigned int i;
  281. u32 status;
  282. __be32 *p;
  283. p = xdr_inline_decode(xdr, 4);
  284. if (unlikely(p == NULL))
  285. return -EIO;
  286. status = be32_to_cpup(p);
  287. for (i = 0; i < ARRAY_SIZE(mnt_errtbl); i++) {
  288. if (mnt_errtbl[i].status == status) {
  289. res->errno = mnt_errtbl[i].errno;
  290. return 0;
  291. }
  292. }
  293. dprintk("NFS: unrecognized MNT status code: %u\n", status);
  294. res->errno = -EACCES;
  295. return 0;
  296. }
  297. static int decode_fhandle(struct xdr_stream *xdr, struct mountres *res)
  298. {
  299. struct nfs_fh *fh = res->fh;
  300. __be32 *p;
  301. p = xdr_inline_decode(xdr, NFS2_FHSIZE);
  302. if (unlikely(p == NULL))
  303. return -EIO;
  304. fh->size = NFS2_FHSIZE;
  305. memcpy(fh->data, p, NFS2_FHSIZE);
  306. return 0;
  307. }
  308. static int mnt_xdr_dec_mountres(struct rpc_rqst *req,
  309. struct xdr_stream *xdr,
  310. struct mountres *res)
  311. {
  312. int status;
  313. status = decode_status(xdr, res);
  314. if (unlikely(status != 0 || res->errno != 0))
  315. return status;
  316. return decode_fhandle(xdr, res);
  317. }
  318. static int decode_fhs_status(struct xdr_stream *xdr, struct mountres *res)
  319. {
  320. unsigned int i;
  321. u32 status;
  322. __be32 *p;
  323. p = xdr_inline_decode(xdr, 4);
  324. if (unlikely(p == NULL))
  325. return -EIO;
  326. status = be32_to_cpup(p);
  327. for (i = 0; i < ARRAY_SIZE(mnt3_errtbl); i++) {
  328. if (mnt3_errtbl[i].status == status) {
  329. res->errno = mnt3_errtbl[i].errno;
  330. return 0;
  331. }
  332. }
  333. dprintk("NFS: unrecognized MNT3 status code: %u\n", status);
  334. res->errno = -EACCES;
  335. return 0;
  336. }
  337. static int decode_fhandle3(struct xdr_stream *xdr, struct mountres *res)
  338. {
  339. struct nfs_fh *fh = res->fh;
  340. u32 size;
  341. __be32 *p;
  342. p = xdr_inline_decode(xdr, 4);
  343. if (unlikely(p == NULL))
  344. return -EIO;
  345. size = be32_to_cpup(p);
  346. if (size > NFS3_FHSIZE || size == 0)
  347. return -EIO;
  348. p = xdr_inline_decode(xdr, size);
  349. if (unlikely(p == NULL))
  350. return -EIO;
  351. fh->size = size;
  352. memcpy(fh->data, p, size);
  353. return 0;
  354. }
  355. static int decode_auth_flavors(struct xdr_stream *xdr, struct mountres *res)
  356. {
  357. rpc_authflavor_t *flavors = res->auth_flavors;
  358. unsigned int *count = res->auth_count;
  359. u32 entries, i;
  360. __be32 *p;
  361. if (*count == 0)
  362. return 0;
  363. p = xdr_inline_decode(xdr, 4);
  364. if (unlikely(p == NULL))
  365. return -EIO;
  366. entries = be32_to_cpup(p);
  367. dprintk("NFS: received %u auth flavors\n", entries);
  368. if (entries > NFS_MAX_SECFLAVORS)
  369. entries = NFS_MAX_SECFLAVORS;
  370. p = xdr_inline_decode(xdr, 4 * entries);
  371. if (unlikely(p == NULL))
  372. return -EIO;
  373. if (entries > *count)
  374. entries = *count;
  375. for (i = 0; i < entries; i++) {
  376. flavors[i] = be32_to_cpup(p++);
  377. dprintk("NFS: auth flavor[%u]: %d\n", i, flavors[i]);
  378. }
  379. *count = i;
  380. return 0;
  381. }
  382. static int mnt_xdr_dec_mountres3(struct rpc_rqst *req,
  383. struct xdr_stream *xdr,
  384. struct mountres *res)
  385. {
  386. int status;
  387. status = decode_fhs_status(xdr, res);
  388. if (unlikely(status != 0 || res->errno != 0))
  389. return status;
  390. status = decode_fhandle3(xdr, res);
  391. if (unlikely(status != 0)) {
  392. res->errno = -EBADHANDLE;
  393. return 0;
  394. }
  395. return decode_auth_flavors(xdr, res);
  396. }
  397. static struct rpc_procinfo mnt_procedures[] = {
  398. [MOUNTPROC_MNT] = {
  399. .p_proc = MOUNTPROC_MNT,
  400. .p_encode = (kxdreproc_t)mnt_xdr_enc_dirpath,
  401. .p_decode = (kxdrdproc_t)mnt_xdr_dec_mountres,
  402. .p_arglen = MNT_enc_dirpath_sz,
  403. .p_replen = MNT_dec_mountres_sz,
  404. .p_statidx = MOUNTPROC_MNT,
  405. .p_name = "MOUNT",
  406. },
  407. [MOUNTPROC_UMNT] = {
  408. .p_proc = MOUNTPROC_UMNT,
  409. .p_encode = (kxdreproc_t)mnt_xdr_enc_dirpath,
  410. .p_arglen = MNT_enc_dirpath_sz,
  411. .p_statidx = MOUNTPROC_UMNT,
  412. .p_name = "UMOUNT",
  413. },
  414. };
  415. static struct rpc_procinfo mnt3_procedures[] = {
  416. [MOUNTPROC3_MNT] = {
  417. .p_proc = MOUNTPROC3_MNT,
  418. .p_encode = (kxdreproc_t)mnt_xdr_enc_dirpath,
  419. .p_decode = (kxdrdproc_t)mnt_xdr_dec_mountres3,
  420. .p_arglen = MNT_enc_dirpath_sz,
  421. .p_replen = MNT_dec_mountres3_sz,
  422. .p_statidx = MOUNTPROC3_MNT,
  423. .p_name = "MOUNT",
  424. },
  425. [MOUNTPROC3_UMNT] = {
  426. .p_proc = MOUNTPROC3_UMNT,
  427. .p_encode = (kxdreproc_t)mnt_xdr_enc_dirpath,
  428. .p_arglen = MNT_enc_dirpath_sz,
  429. .p_statidx = MOUNTPROC3_UMNT,
  430. .p_name = "UMOUNT",
  431. },
  432. };
  433. static const struct rpc_version mnt_version1 = {
  434. .number = 1,
  435. .nrprocs = ARRAY_SIZE(mnt_procedures),
  436. .procs = mnt_procedures,
  437. };
  438. static const struct rpc_version mnt_version3 = {
  439. .number = 3,
  440. .nrprocs = ARRAY_SIZE(mnt3_procedures),
  441. .procs = mnt3_procedures,
  442. };
  443. static const struct rpc_version *mnt_version[] = {
  444. NULL,
  445. &mnt_version1,
  446. NULL,
  447. &mnt_version3,
  448. };
  449. static struct rpc_stat mnt_stats;
  450. static const struct rpc_program mnt_program = {
  451. .name = "mount",
  452. .number = NFS_MNT_PROGRAM,
  453. .nrvers = ARRAY_SIZE(mnt_version),
  454. .version = mnt_version,
  455. .stats = &mnt_stats,
  456. };