mount_clnt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/xprt.h>
  17. #include <linux/sunrpc/sched.h>
  18. #include <linux/nfs_fs.h>
  19. #ifdef RPC_DEBUG
  20. # define NFSDBG_FACILITY NFSDBG_ROOT
  21. #endif
  22. /*
  23. #define MOUNT_PROGRAM 100005
  24. #define MOUNT_VERSION 1
  25. #define MOUNT_MNT 1
  26. #define MOUNT_UMNT 3
  27. */
  28. static struct rpc_clnt * mnt_create(char *, struct sockaddr_in *,
  29. int, int);
  30. static struct rpc_program mnt_program;
  31. struct mnt_fhstatus {
  32. unsigned int status;
  33. struct nfs_fh * fh;
  34. };
  35. /*
  36. * Obtain an NFS file handle for the given host and path
  37. */
  38. int
  39. nfsroot_mount(struct sockaddr_in *addr, char *path, struct nfs_fh *fh,
  40. int version, int protocol)
  41. {
  42. struct rpc_clnt *mnt_clnt;
  43. struct mnt_fhstatus result = {
  44. .fh = fh
  45. };
  46. struct rpc_message msg = {
  47. .rpc_argp = path,
  48. .rpc_resp = &result,
  49. };
  50. char hostname[32];
  51. int status;
  52. dprintk("NFS: nfs_mount(%08x:%s)\n",
  53. (unsigned)ntohl(addr->sin_addr.s_addr), path);
  54. sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr->sin_addr.s_addr));
  55. mnt_clnt = mnt_create(hostname, addr, version, protocol);
  56. if (IS_ERR(mnt_clnt))
  57. return PTR_ERR(mnt_clnt);
  58. if (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. return status < 0? status : (result.status? -EACCES : 0);
  64. }
  65. static struct rpc_clnt *
  66. mnt_create(char *hostname, struct sockaddr_in *srvaddr, int version,
  67. int protocol)
  68. {
  69. struct rpc_xprt *xprt;
  70. struct rpc_clnt *clnt;
  71. xprt = xprt_create_proto(protocol, srvaddr, NULL);
  72. if (IS_ERR(xprt))
  73. return (struct rpc_clnt *)xprt;
  74. clnt = rpc_create_client(xprt, hostname,
  75. &mnt_program, version,
  76. RPC_AUTH_UNIX);
  77. if (!IS_ERR(clnt)) {
  78. clnt->cl_softrtry = 1;
  79. clnt->cl_oneshot = 1;
  80. clnt->cl_intr = 1;
  81. }
  82. return clnt;
  83. }
  84. /*
  85. * XDR encode/decode functions for MOUNT
  86. */
  87. static int
  88. xdr_encode_dirpath(struct rpc_rqst *req, u32 *p, 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
  95. xdr_decode_fhstatus(struct rpc_rqst *req, u32 *p, 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
  105. xdr_decode_fhstatus3(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
  106. {
  107. struct nfs_fh *fh = res->fh;
  108. if ((res->status = ntohl(*p++)) == 0) {
  109. int size = ntohl(*p++);
  110. if (size <= NFS3_FHSIZE) {
  111. fh->size = size;
  112. memcpy(fh->data, p, size);
  113. } else
  114. res->status = -EBADHANDLE;
  115. }
  116. return 0;
  117. }
  118. #define MNT_dirpath_sz (1 + 256)
  119. #define MNT_fhstatus_sz (1 + 8)
  120. static struct rpc_procinfo mnt_procedures[] = {
  121. [MNTPROC_MNT] = {
  122. .p_proc = MNTPROC_MNT,
  123. .p_encode = (kxdrproc_t) xdr_encode_dirpath,
  124. .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
  125. .p_bufsiz = MNT_dirpath_sz << 2,
  126. .p_statidx = MNTPROC_MNT,
  127. .p_name = "MOUNT",
  128. },
  129. };
  130. static struct rpc_procinfo mnt3_procedures[] = {
  131. [MOUNTPROC3_MNT] = {
  132. .p_proc = MOUNTPROC3_MNT,
  133. .p_encode = (kxdrproc_t) xdr_encode_dirpath,
  134. .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
  135. .p_bufsiz = MNT_dirpath_sz << 2,
  136. .p_statidx = MOUNTPROC3_MNT,
  137. .p_name = "MOUNT",
  138. },
  139. };
  140. static struct rpc_version mnt_version1 = {
  141. .number = 1,
  142. .nrprocs = 2,
  143. .procs = mnt_procedures
  144. };
  145. static struct rpc_version mnt_version3 = {
  146. .number = 3,
  147. .nrprocs = 2,
  148. .procs = mnt3_procedures
  149. };
  150. static struct rpc_version * mnt_version[] = {
  151. NULL,
  152. &mnt_version1,
  153. NULL,
  154. &mnt_version3,
  155. };
  156. static struct rpc_stat mnt_stats;
  157. static struct rpc_program mnt_program = {
  158. .name = "mount",
  159. .number = NFS_MNT_PROGRAM,
  160. .nrvers = ARRAY_SIZE(mnt_version),
  161. .version = mnt_version,
  162. .stats = &mnt_stats,
  163. };