mount_clnt.c 4.2 KB

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