mount_clnt.c 4.0 KB

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