mount_clnt.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 RPC_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. /*
  33. * XDR argument and result sizes
  34. */
  35. #define MNT_enc_dirpath_sz encode_dirpath_sz
  36. /*
  37. * Defined by RFC 1094, section A.5
  38. */
  39. enum {
  40. MOUNTPROC_NULL = 0,
  41. MOUNTPROC_MNT = 1,
  42. MOUNTPROC_DUMP = 2,
  43. MOUNTPROC_UMNT = 3,
  44. MOUNTPROC_UMNTALL = 4,
  45. MOUNTPROC_EXPORT = 5,
  46. };
  47. /*
  48. * Defined by RFC 1813, section 5.2
  49. */
  50. enum {
  51. MOUNTPROC3_NULL = 0,
  52. MOUNTPROC3_MNT = 1,
  53. MOUNTPROC3_DUMP = 2,
  54. MOUNTPROC3_UMNT = 3,
  55. MOUNTPROC3_UMNTALL = 4,
  56. MOUNTPROC3_EXPORT = 5,
  57. };
  58. static struct rpc_program mnt_program;
  59. /*
  60. * Defined by OpenGroup XNFS Version 3W, chapter 8
  61. */
  62. enum mountstat {
  63. MNT_OK = 0,
  64. MNT_EPERM = 1,
  65. MNT_ENOENT = 2,
  66. MNT_EACCES = 13,
  67. MNT_EINVAL = 22,
  68. };
  69. static struct {
  70. u32 status;
  71. int errno;
  72. } mnt_errtbl[] = {
  73. { .status = MNT_OK, .errno = 0, },
  74. { .status = MNT_EPERM, .errno = -EPERM, },
  75. { .status = MNT_ENOENT, .errno = -ENOENT, },
  76. { .status = MNT_EACCES, .errno = -EACCES, },
  77. { .status = MNT_EINVAL, .errno = -EINVAL, },
  78. };
  79. /*
  80. * Defined by RFC 1813, section 5.1.5
  81. */
  82. enum mountstat3 {
  83. MNT3_OK = 0, /* no error */
  84. MNT3ERR_PERM = 1, /* Not owner */
  85. MNT3ERR_NOENT = 2, /* No such file or directory */
  86. MNT3ERR_IO = 5, /* I/O error */
  87. MNT3ERR_ACCES = 13, /* Permission denied */
  88. MNT3ERR_NOTDIR = 20, /* Not a directory */
  89. MNT3ERR_INVAL = 22, /* Invalid argument */
  90. MNT3ERR_NAMETOOLONG = 63, /* Filename too long */
  91. MNT3ERR_NOTSUPP = 10004, /* Operation not supported */
  92. MNT3ERR_SERVERFAULT = 10006, /* A failure on the server */
  93. };
  94. static struct {
  95. u32 status;
  96. int errno;
  97. } mnt3_errtbl[] = {
  98. { .status = MNT3_OK, .errno = 0, },
  99. { .status = MNT3ERR_PERM, .errno = -EPERM, },
  100. { .status = MNT3ERR_NOENT, .errno = -ENOENT, },
  101. { .status = MNT3ERR_IO, .errno = -EIO, },
  102. { .status = MNT3ERR_ACCES, .errno = -EACCES, },
  103. { .status = MNT3ERR_NOTDIR, .errno = -ENOTDIR, },
  104. { .status = MNT3ERR_INVAL, .errno = -EINVAL, },
  105. { .status = MNT3ERR_NAMETOOLONG, .errno = -ENAMETOOLONG, },
  106. { .status = MNT3ERR_NOTSUPP, .errno = -ENOTSUPP, },
  107. { .status = MNT3ERR_SERVERFAULT, .errno = -ESERVERFAULT, },
  108. };
  109. struct mountres {
  110. int errno;
  111. struct nfs_fh *fh;
  112. };
  113. struct mnt_fhstatus {
  114. u32 status;
  115. struct nfs_fh *fh;
  116. };
  117. /**
  118. * nfs_mount - Obtain an NFS file handle for the given host and path
  119. * @info: pointer to mount request arguments
  120. *
  121. * Uses default timeout parameters specified by underlying transport.
  122. */
  123. int nfs_mount(struct nfs_mount_request *info)
  124. {
  125. struct mnt_fhstatus result = {
  126. .fh = info->fh
  127. };
  128. struct rpc_message msg = {
  129. .rpc_argp = info->dirpath,
  130. .rpc_resp = &result,
  131. };
  132. struct rpc_create_args args = {
  133. .protocol = info->protocol,
  134. .address = info->sap,
  135. .addrsize = info->salen,
  136. .servername = info->hostname,
  137. .program = &mnt_program,
  138. .version = info->version,
  139. .authflavor = RPC_AUTH_UNIX,
  140. };
  141. struct rpc_clnt *mnt_clnt;
  142. int status;
  143. dprintk("NFS: sending MNT request for %s:%s\n",
  144. (info->hostname ? info->hostname : "server"),
  145. info->dirpath);
  146. if (info->noresvport)
  147. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  148. mnt_clnt = rpc_create(&args);
  149. if (IS_ERR(mnt_clnt))
  150. goto out_clnt_err;
  151. if (info->version == NFS_MNT3_VERSION)
  152. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
  153. else
  154. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT];
  155. status = rpc_call_sync(mnt_clnt, &msg, 0);
  156. rpc_shutdown_client(mnt_clnt);
  157. if (status < 0)
  158. goto out_call_err;
  159. if (result.status != 0)
  160. goto out_mnt_err;
  161. dprintk("NFS: MNT request succeeded\n");
  162. status = 0;
  163. out:
  164. return status;
  165. out_clnt_err:
  166. status = PTR_ERR(mnt_clnt);
  167. dprintk("NFS: failed to create RPC client, status=%d\n", status);
  168. goto out;
  169. out_call_err:
  170. dprintk("NFS: failed to start MNT request, status=%d\n", status);
  171. goto out;
  172. out_mnt_err:
  173. dprintk("NFS: MNT server returned result %d\n", result.status);
  174. status = nfs_stat_to_errno(result.status);
  175. goto out;
  176. }
  177. /*
  178. * XDR encode/decode functions for MOUNT
  179. */
  180. static int encode_mntdirpath(struct xdr_stream *xdr, const char *pathname)
  181. {
  182. const u32 pathname_len = strlen(pathname);
  183. __be32 *p;
  184. if (unlikely(pathname_len > MNTPATHLEN))
  185. return -EIO;
  186. p = xdr_reserve_space(xdr, sizeof(u32) + pathname_len);
  187. if (unlikely(p == NULL))
  188. return -EIO;
  189. xdr_encode_opaque(p, pathname, pathname_len);
  190. return 0;
  191. }
  192. static int mnt_enc_dirpath(struct rpc_rqst *req, __be32 *p,
  193. const char *dirpath)
  194. {
  195. struct xdr_stream xdr;
  196. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  197. return encode_mntdirpath(&xdr, dirpath);
  198. }
  199. static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p,
  200. struct mnt_fhstatus *res)
  201. {
  202. struct nfs_fh *fh = res->fh;
  203. if ((res->status = ntohl(*p++)) == 0) {
  204. fh->size = NFS2_FHSIZE;
  205. memcpy(fh->data, p, NFS2_FHSIZE);
  206. }
  207. return 0;
  208. }
  209. /*
  210. * RFC 1094: "A non-zero status indicates some sort of error. In this
  211. * case, the status is a UNIX error number." This can be problematic
  212. * if the server and client use different errno values for the same
  213. * error.
  214. *
  215. * However, the OpenGroup XNFS spec provides a simple mapping that is
  216. * independent of local errno values on the server and the client.
  217. */
  218. static int decode_status(struct xdr_stream *xdr, struct mountres *res)
  219. {
  220. unsigned int i;
  221. u32 status;
  222. __be32 *p;
  223. p = xdr_inline_decode(xdr, sizeof(status));
  224. if (unlikely(p == NULL))
  225. return -EIO;
  226. status = ntohl(*p);
  227. for (i = 0; i <= ARRAY_SIZE(mnt_errtbl); i++) {
  228. if (mnt_errtbl[i].status == status) {
  229. res->errno = mnt_errtbl[i].errno;
  230. return 0;
  231. }
  232. }
  233. dprintk("NFS: unrecognized MNT status code: %u\n", status);
  234. res->errno = -EACCES;
  235. return 0;
  236. }
  237. static int decode_fhandle(struct xdr_stream *xdr, struct mountres *res)
  238. {
  239. struct nfs_fh *fh = res->fh;
  240. __be32 *p;
  241. p = xdr_inline_decode(xdr, NFS2_FHSIZE);
  242. if (unlikely(p == NULL))
  243. return -EIO;
  244. fh->size = NFS2_FHSIZE;
  245. memcpy(fh->data, p, NFS2_FHSIZE);
  246. return 0;
  247. }
  248. static int decode_fhs_status(struct xdr_stream *xdr, struct mountres *res)
  249. {
  250. unsigned int i;
  251. u32 status;
  252. __be32 *p;
  253. p = xdr_inline_decode(xdr, sizeof(status));
  254. if (unlikely(p == NULL))
  255. return -EIO;
  256. status = ntohl(*p);
  257. for (i = 0; i <= ARRAY_SIZE(mnt3_errtbl); i++) {
  258. if (mnt3_errtbl[i].status == status) {
  259. res->errno = mnt3_errtbl[i].errno;
  260. return 0;
  261. }
  262. }
  263. dprintk("NFS: unrecognized MNT3 status code: %u\n", status);
  264. res->errno = -EACCES;
  265. return 0;
  266. }
  267. static int decode_fhandle3(struct xdr_stream *xdr, struct mountres *res)
  268. {
  269. struct nfs_fh *fh = res->fh;
  270. u32 size;
  271. __be32 *p;
  272. p = xdr_inline_decode(xdr, sizeof(size));
  273. if (unlikely(p == NULL))
  274. return -EIO;
  275. size = ntohl(*p++);
  276. if (size > NFS3_FHSIZE || size == 0)
  277. return -EIO;
  278. p = xdr_inline_decode(xdr, size);
  279. if (unlikely(p == NULL))
  280. return -EIO;
  281. fh->size = size;
  282. memcpy(fh->data, p, size);
  283. return 0;
  284. }
  285. static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p,
  286. struct mnt_fhstatus *res)
  287. {
  288. struct nfs_fh *fh = res->fh;
  289. unsigned size;
  290. if ((res->status = ntohl(*p++)) == 0) {
  291. size = ntohl(*p++);
  292. if (size <= NFS3_FHSIZE && size != 0) {
  293. fh->size = size;
  294. memcpy(fh->data, p, size);
  295. } else
  296. res->status = -EBADHANDLE;
  297. }
  298. return 0;
  299. }
  300. #define MNT_fhstatus_sz (1 + 8)
  301. #define MNT_fhstatus3_sz (1 + 16)
  302. static struct rpc_procinfo mnt_procedures[] = {
  303. [MOUNTPROC_MNT] = {
  304. .p_proc = MOUNTPROC_MNT,
  305. .p_encode = (kxdrproc_t)mnt_enc_dirpath,
  306. .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
  307. .p_arglen = MNT_enc_dirpath_sz,
  308. .p_replen = MNT_fhstatus_sz,
  309. .p_statidx = MOUNTPROC_MNT,
  310. .p_name = "MOUNT",
  311. },
  312. };
  313. static struct rpc_procinfo mnt3_procedures[] = {
  314. [MOUNTPROC3_MNT] = {
  315. .p_proc = MOUNTPROC3_MNT,
  316. .p_encode = (kxdrproc_t)mnt_enc_dirpath,
  317. .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
  318. .p_arglen = MNT_enc_dirpath_sz,
  319. .p_replen = MNT_fhstatus3_sz,
  320. .p_statidx = MOUNTPROC3_MNT,
  321. .p_name = "MOUNT",
  322. },
  323. };
  324. static struct rpc_version mnt_version1 = {
  325. .number = 1,
  326. .nrprocs = 2,
  327. .procs = mnt_procedures,
  328. };
  329. static struct rpc_version mnt_version3 = {
  330. .number = 3,
  331. .nrprocs = 2,
  332. .procs = mnt3_procedures,
  333. };
  334. static struct rpc_version *mnt_version[] = {
  335. NULL,
  336. &mnt_version1,
  337. NULL,
  338. &mnt_version3,
  339. };
  340. static struct rpc_stat mnt_stats;
  341. static struct rpc_program mnt_program = {
  342. .name = "mount",
  343. .number = NFS_MNT_PROGRAM,
  344. .nrvers = ARRAY_SIZE(mnt_version),
  345. .version = mnt_version,
  346. .stats = &mnt_stats,
  347. };