mount_clnt.c 4.2 KB

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