mount_clnt.c 4.6 KB

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