mount_clnt.c 4.5 KB

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