mount_clnt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /*
  29. * XDR argument and result sizes
  30. */
  31. #define MNT_enc_dirpath_sz encode_dirpath_sz
  32. /*
  33. * Defined by RFC 1094, section A.5
  34. */
  35. enum {
  36. MOUNTPROC_NULL = 0,
  37. MOUNTPROC_MNT = 1,
  38. MOUNTPROC_DUMP = 2,
  39. MOUNTPROC_UMNT = 3,
  40. MOUNTPROC_UMNTALL = 4,
  41. MOUNTPROC_EXPORT = 5,
  42. };
  43. /*
  44. * Defined by RFC 1813, section 5.2
  45. */
  46. enum {
  47. MOUNTPROC3_NULL = 0,
  48. MOUNTPROC3_MNT = 1,
  49. MOUNTPROC3_DUMP = 2,
  50. MOUNTPROC3_UMNT = 3,
  51. MOUNTPROC3_UMNTALL = 4,
  52. MOUNTPROC3_EXPORT = 5,
  53. };
  54. static struct rpc_program mnt_program;
  55. struct mnt_fhstatus {
  56. u32 status;
  57. struct nfs_fh *fh;
  58. };
  59. /**
  60. * nfs_mount - Obtain an NFS file handle for the given host and path
  61. * @info: pointer to mount request arguments
  62. *
  63. * Uses default timeout parameters specified by underlying transport.
  64. */
  65. int nfs_mount(struct nfs_mount_request *info)
  66. {
  67. struct mnt_fhstatus result = {
  68. .fh = info->fh
  69. };
  70. struct rpc_message msg = {
  71. .rpc_argp = info->dirpath,
  72. .rpc_resp = &result,
  73. };
  74. struct rpc_create_args args = {
  75. .protocol = info->protocol,
  76. .address = info->sap,
  77. .addrsize = info->salen,
  78. .servername = info->hostname,
  79. .program = &mnt_program,
  80. .version = info->version,
  81. .authflavor = RPC_AUTH_UNIX,
  82. };
  83. struct rpc_clnt *mnt_clnt;
  84. int status;
  85. dprintk("NFS: sending MNT request for %s:%s\n",
  86. (info->hostname ? info->hostname : "server"),
  87. info->dirpath);
  88. if (info->noresvport)
  89. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  90. mnt_clnt = rpc_create(&args);
  91. if (IS_ERR(mnt_clnt))
  92. goto out_clnt_err;
  93. if (info->version == NFS_MNT3_VERSION)
  94. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
  95. else
  96. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT];
  97. status = rpc_call_sync(mnt_clnt, &msg, 0);
  98. rpc_shutdown_client(mnt_clnt);
  99. if (status < 0)
  100. goto out_call_err;
  101. if (result.status != 0)
  102. goto out_mnt_err;
  103. dprintk("NFS: MNT request succeeded\n");
  104. status = 0;
  105. out:
  106. return status;
  107. out_clnt_err:
  108. status = PTR_ERR(mnt_clnt);
  109. dprintk("NFS: failed to create RPC client, status=%d\n", status);
  110. goto out;
  111. out_call_err:
  112. dprintk("NFS: failed to start MNT request, status=%d\n", status);
  113. goto out;
  114. out_mnt_err:
  115. dprintk("NFS: MNT server returned result %d\n", result.status);
  116. status = nfs_stat_to_errno(result.status);
  117. goto out;
  118. }
  119. /*
  120. * XDR encode/decode functions for MOUNT
  121. */
  122. static int encode_mntdirpath(struct xdr_stream *xdr, const char *pathname)
  123. {
  124. const u32 pathname_len = strlen(pathname);
  125. __be32 *p;
  126. if (unlikely(pathname_len > MNTPATHLEN))
  127. return -EIO;
  128. p = xdr_reserve_space(xdr, sizeof(u32) + pathname_len);
  129. if (unlikely(p == NULL))
  130. return -EIO;
  131. xdr_encode_opaque(p, pathname, pathname_len);
  132. return 0;
  133. }
  134. static int mnt_enc_dirpath(struct rpc_rqst *req, __be32 *p,
  135. const char *dirpath)
  136. {
  137. struct xdr_stream xdr;
  138. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  139. return encode_mntdirpath(&xdr, dirpath);
  140. }
  141. static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p,
  142. struct mnt_fhstatus *res)
  143. {
  144. struct nfs_fh *fh = res->fh;
  145. if ((res->status = ntohl(*p++)) == 0) {
  146. fh->size = NFS2_FHSIZE;
  147. memcpy(fh->data, p, NFS2_FHSIZE);
  148. }
  149. return 0;
  150. }
  151. static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p,
  152. struct mnt_fhstatus *res)
  153. {
  154. struct nfs_fh *fh = res->fh;
  155. unsigned size;
  156. if ((res->status = ntohl(*p++)) == 0) {
  157. size = ntohl(*p++);
  158. if (size <= NFS3_FHSIZE && size != 0) {
  159. fh->size = size;
  160. memcpy(fh->data, p, size);
  161. } else
  162. res->status = -EBADHANDLE;
  163. }
  164. return 0;
  165. }
  166. #define MNT_fhstatus_sz (1 + 8)
  167. #define MNT_fhstatus3_sz (1 + 16)
  168. static struct rpc_procinfo mnt_procedures[] = {
  169. [MOUNTPROC_MNT] = {
  170. .p_proc = MOUNTPROC_MNT,
  171. .p_encode = (kxdrproc_t)mnt_enc_dirpath,
  172. .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
  173. .p_arglen = MNT_enc_dirpath_sz,
  174. .p_replen = MNT_fhstatus_sz,
  175. .p_statidx = MOUNTPROC_MNT,
  176. .p_name = "MOUNT",
  177. },
  178. };
  179. static struct rpc_procinfo mnt3_procedures[] = {
  180. [MOUNTPROC3_MNT] = {
  181. .p_proc = MOUNTPROC3_MNT,
  182. .p_encode = (kxdrproc_t)mnt_enc_dirpath,
  183. .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
  184. .p_arglen = MNT_enc_dirpath_sz,
  185. .p_replen = MNT_fhstatus3_sz,
  186. .p_statidx = MOUNTPROC3_MNT,
  187. .p_name = "MOUNT",
  188. },
  189. };
  190. static struct rpc_version mnt_version1 = {
  191. .number = 1,
  192. .nrprocs = 2,
  193. .procs = mnt_procedures,
  194. };
  195. static struct rpc_version mnt_version3 = {
  196. .number = 3,
  197. .nrprocs = 2,
  198. .procs = mnt3_procedures,
  199. };
  200. static struct rpc_version *mnt_version[] = {
  201. NULL,
  202. &mnt_version1,
  203. NULL,
  204. &mnt_version3,
  205. };
  206. static struct rpc_stat mnt_stats;
  207. static struct rpc_program mnt_program = {
  208. .name = "mount",
  209. .number = NFS_MNT_PROGRAM,
  210. .nrvers = ARRAY_SIZE(mnt_version),
  211. .version = mnt_version,
  212. .stats = &mnt_stats,
  213. };