mount_clnt.c 4.4 KB

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