mount_clnt.c 9.7 KB

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