mount_clnt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. return PTR_ERR(mnt_clnt);
  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. return status < 0? status : (result.status? -EACCES : 0);
  70. }
  71. /*
  72. * XDR encode/decode functions for MOUNT
  73. */
  74. static int xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p,
  75. const char *path)
  76. {
  77. p = xdr_encode_string(p, path);
  78. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  79. return 0;
  80. }
  81. static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p,
  82. struct mnt_fhstatus *res)
  83. {
  84. struct nfs_fh *fh = res->fh;
  85. if ((res->status = ntohl(*p++)) == 0) {
  86. fh->size = NFS2_FHSIZE;
  87. memcpy(fh->data, p, NFS2_FHSIZE);
  88. }
  89. return 0;
  90. }
  91. static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p,
  92. struct mnt_fhstatus *res)
  93. {
  94. struct nfs_fh *fh = res->fh;
  95. if ((res->status = ntohl(*p++)) == 0) {
  96. int size = ntohl(*p++);
  97. if (size <= NFS3_FHSIZE) {
  98. fh->size = size;
  99. memcpy(fh->data, p, size);
  100. } else
  101. res->status = -EBADHANDLE;
  102. }
  103. return 0;
  104. }
  105. #define MNT_dirpath_sz (1 + 256)
  106. #define MNT_fhstatus_sz (1 + 8)
  107. #define MNT_fhstatus3_sz (1 + 16)
  108. static struct rpc_procinfo mnt_procedures[] = {
  109. [MNTPROC_MNT] = {
  110. .p_proc = MNTPROC_MNT,
  111. .p_encode = (kxdrproc_t) xdr_encode_dirpath,
  112. .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
  113. .p_arglen = MNT_dirpath_sz,
  114. .p_replen = MNT_fhstatus_sz,
  115. .p_statidx = MNTPROC_MNT,
  116. .p_name = "MOUNT",
  117. },
  118. };
  119. static struct rpc_procinfo mnt3_procedures[] = {
  120. [MOUNTPROC3_MNT] = {
  121. .p_proc = MOUNTPROC3_MNT,
  122. .p_encode = (kxdrproc_t) xdr_encode_dirpath,
  123. .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
  124. .p_arglen = MNT_dirpath_sz,
  125. .p_replen = MNT_fhstatus3_sz,
  126. .p_statidx = MOUNTPROC3_MNT,
  127. .p_name = "MOUNT",
  128. },
  129. };
  130. static struct rpc_version mnt_version1 = {
  131. .number = 1,
  132. .nrprocs = 2,
  133. .procs = mnt_procedures,
  134. };
  135. static struct rpc_version mnt_version3 = {
  136. .number = 3,
  137. .nrprocs = 2,
  138. .procs = mnt3_procedures,
  139. };
  140. static struct rpc_version *mnt_version[] = {
  141. NULL,
  142. &mnt_version1,
  143. NULL,
  144. &mnt_version3,
  145. };
  146. static struct rpc_stat mnt_stats;
  147. static struct rpc_program mnt_program = {
  148. .name = "mount",
  149. .number = NFS_MNT_PROGRAM,
  150. .nrvers = ARRAY_SIZE(mnt_version),
  151. .version = mnt_version,
  152. .stats = &mnt_stats,
  153. };