mount_clnt.c 4.2 KB

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