mount_clnt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. char hostname[32];
  47. int status;
  48. int call;
  49. dprintk("NFS: nfs_mount(%08x:%s)\n",
  50. (unsigned)ntohl(addr->sin_addr.s_addr), path);
  51. sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr->sin_addr.s_addr));
  52. mnt_clnt = mnt_create(hostname, addr, version, protocol);
  53. if (IS_ERR(mnt_clnt))
  54. return PTR_ERR(mnt_clnt);
  55. call = (version == NFS_MNT3_VERSION) ? MOUNTPROC3_MNT : MNTPROC_MNT;
  56. status = rpc_call(mnt_clnt, call, path, &result, 0);
  57. return status < 0? status : (result.status? -EACCES : 0);
  58. }
  59. static struct rpc_clnt *
  60. mnt_create(char *hostname, struct sockaddr_in *srvaddr, int version,
  61. int protocol)
  62. {
  63. struct rpc_xprt *xprt;
  64. struct rpc_clnt *clnt;
  65. xprt = xprt_create_proto(protocol, srvaddr, NULL);
  66. if (IS_ERR(xprt))
  67. return (struct rpc_clnt *)xprt;
  68. clnt = rpc_create_client(xprt, hostname,
  69. &mnt_program, version,
  70. RPC_AUTH_UNIX);
  71. if (!IS_ERR(clnt)) {
  72. clnt->cl_softrtry = 1;
  73. clnt->cl_oneshot = 1;
  74. clnt->cl_intr = 1;
  75. }
  76. return clnt;
  77. }
  78. /*
  79. * XDR encode/decode functions for MOUNT
  80. */
  81. static int
  82. xdr_encode_dirpath(struct rpc_rqst *req, u32 *p, const char *path)
  83. {
  84. p = xdr_encode_string(p, path);
  85. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  86. return 0;
  87. }
  88. static int
  89. xdr_decode_fhstatus(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
  90. {
  91. struct nfs_fh *fh = res->fh;
  92. if ((res->status = ntohl(*p++)) == 0) {
  93. fh->size = NFS2_FHSIZE;
  94. memcpy(fh->data, p, NFS2_FHSIZE);
  95. }
  96. return 0;
  97. }
  98. static int
  99. xdr_decode_fhstatus3(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
  100. {
  101. struct nfs_fh *fh = res->fh;
  102. if ((res->status = ntohl(*p++)) == 0) {
  103. int size = ntohl(*p++);
  104. if (size <= NFS3_FHSIZE) {
  105. fh->size = size;
  106. memcpy(fh->data, p, size);
  107. } else
  108. res->status = -EBADHANDLE;
  109. }
  110. return 0;
  111. }
  112. #define MNT_dirpath_sz (1 + 256)
  113. #define MNT_fhstatus_sz (1 + 8)
  114. static struct rpc_procinfo mnt_procedures[] = {
  115. [MNTPROC_MNT] = {
  116. .p_proc = MNTPROC_MNT,
  117. .p_encode = (kxdrproc_t) xdr_encode_dirpath,
  118. .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
  119. .p_bufsiz = MNT_dirpath_sz << 2,
  120. },
  121. };
  122. static struct rpc_procinfo mnt3_procedures[] = {
  123. [MOUNTPROC3_MNT] = {
  124. .p_proc = MOUNTPROC3_MNT,
  125. .p_encode = (kxdrproc_t) xdr_encode_dirpath,
  126. .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
  127. .p_bufsiz = MNT_dirpath_sz << 2,
  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 = sizeof(mnt_version)/sizeof(mnt_version[0]),
  151. .version = mnt_version,
  152. .stats = &mnt_stats,
  153. };