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 
9 #include <linux/types.h>
10 #include <linux/socket.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/uio.h>
14 #include <linux/net.h>
15 #include <linux/in.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/xprt.h>
18 #include <linux/sunrpc/sched.h>
19 #include <linux/nfs_fs.h>
20 
21 #ifdef RPC_DEBUG
22 # define NFSDBG_FACILITY	NFSDBG_ROOT
23 #endif
24 
25 /*
26 #define MOUNT_PROGRAM		100005
27 #define MOUNT_VERSION		1
28 #define MOUNT_MNT		1
29 #define MOUNT_UMNT		3
30  */
31 
32 static struct rpc_clnt *	mnt_create(char *, struct sockaddr_in *,
33 								int, int);
34 struct rpc_program      mnt_program;
35 
36 struct mnt_fhstatus {
37 	unsigned int		status;
38 	struct nfs_fh *		fh;
39 };
40 
41 /*
42  * Obtain an NFS file handle for the given host and path
43  */
44 int
nfsroot_mount(struct sockaddr_in * addr,char * path,struct nfs_fh * fh,int version,int protocol)45 nfsroot_mount(struct sockaddr_in *addr, char *path, struct nfs_fh *fh,
46 		int version, int protocol)
47 {
48 	struct rpc_clnt		*mnt_clnt;
49 	struct mnt_fhstatus	result = { 0, fh };
50 	char			hostname[32];
51 	int			status;
52 	int			call;
53 
54 	dprintk("NFS:      nfs_mount(%08x:%s)\n",
55 			(unsigned)ntohl(addr->sin_addr.s_addr), path);
56 
57 	sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr->sin_addr.s_addr));
58 	if (!(mnt_clnt = mnt_create(hostname, addr, version, protocol)))
59 		return -EACCES;
60 
61 	call = (version == NFS_MNT3_VERSION)?  MOUNTPROC3_MNT : MNTPROC_MNT;
62 	status = rpc_call(mnt_clnt, call, path, &result, 0);
63 	return status < 0? status : (result.status? -EACCES : 0);
64 }
65 
66 static struct rpc_clnt *
mnt_create(char * hostname,struct sockaddr_in * srvaddr,int version,int protocol)67 mnt_create(char *hostname, struct sockaddr_in *srvaddr, int version,
68 		int protocol)
69 {
70 	struct rpc_xprt	*xprt;
71 	struct rpc_clnt	*clnt;
72 
73 	if (!(xprt = xprt_create_proto(protocol, srvaddr, NULL)))
74 		return NULL;
75 
76 	clnt = rpc_create_client(xprt, hostname,
77 				&mnt_program, version,
78 				RPC_AUTH_NULL);
79 	if (!clnt) {
80 		xprt_destroy(xprt);
81 	} else {
82 		clnt->cl_softrtry = 1;
83 		clnt->cl_chatty   = 1;
84 		clnt->cl_oneshot  = 1;
85 		clnt->cl_intr = 1;
86 	}
87 	return clnt;
88 }
89 
90 /*
91  * XDR encode/decode functions for MOUNT
92  */
93 static int
xdr_error(struct rpc_rqst * req,u32 * p,void * dummy)94 xdr_error(struct rpc_rqst *req, u32 *p, void *dummy)
95 {
96 	return -EIO;
97 }
98 
99 static int
xdr_encode_dirpath(struct rpc_rqst * req,u32 * p,const char * path)100 xdr_encode_dirpath(struct rpc_rqst *req, u32 *p, const char *path)
101 {
102 	p = xdr_encode_string(p, path);
103 
104 	req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
105 	return 0;
106 }
107 
108 static int
xdr_decode_fhstatus(struct rpc_rqst * req,u32 * p,struct mnt_fhstatus * res)109 xdr_decode_fhstatus(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
110 {
111 	struct nfs_fh *fh = res->fh;
112 
113 	memset((void *)fh, 0, sizeof(*fh));
114 	if ((res->status = ntohl(*p++)) == 0) {
115 		fh->size = NFS2_FHSIZE;
116 		memcpy(fh->data, p, NFS2_FHSIZE);
117 	}
118 	return 0;
119 }
120 
121 static int
xdr_decode_fhstatus3(struct rpc_rqst * req,u32 * p,struct mnt_fhstatus * res)122 xdr_decode_fhstatus3(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
123 {
124 	struct nfs_fh *fh = res->fh;
125 
126 	memset((void *)fh, 0, sizeof(*fh));
127 	if ((res->status = ntohl(*p++)) == 0) {
128 		int size = ntohl(*p++);
129 		if (size <= NFS3_FHSIZE) {
130 			fh->size = size;
131 			memcpy(fh->data, p, size);
132 		} else
133 			res->status = -EBADHANDLE;
134 	}
135 	return 0;
136 }
137 
138 #define MNT_dirpath_sz		(1 + 256)
139 #define MNT_fhstatus_sz		(1 + 8)
140 
141 static struct rpc_procinfo	mnt_procedures[2] = {
142 	{ "mnt_null",
143 		(kxdrproc_t) xdr_error,
144 		(kxdrproc_t) xdr_error,	0, 0 },
145 	{ "mnt_mount",
146 		(kxdrproc_t) xdr_encode_dirpath,
147 		(kxdrproc_t) xdr_decode_fhstatus,
148 		MNT_dirpath_sz << 2, 0 },
149 };
150 
151 static struct rpc_procinfo mnt3_procedures[2] = {
152 	{ "mnt3_null",
153 		(kxdrproc_t) xdr_error,
154 		(kxdrproc_t) xdr_error, 0, 0 },
155 	{ "mnt3_mount",
156 		(kxdrproc_t) xdr_encode_dirpath,
157 		(kxdrproc_t) xdr_decode_fhstatus3,
158 		MNT_dirpath_sz << 2, 0 },
159 };
160 
161 
162 static struct rpc_version	mnt_version1 = {
163 	1, 2, mnt_procedures
164 };
165 
166 static struct rpc_version       mnt_version3 = {
167 	3, 2, mnt3_procedures
168 };
169 
170 static struct rpc_version *	mnt_version[] = {
171 	NULL,
172 	&mnt_version1,
173 	NULL,
174 	&mnt_version3,
175 };
176 
177 static struct rpc_stat		mnt_stats;
178 
179 struct rpc_program	mnt_program = {
180 	"mount",
181 	NFS_MNT_PROGRAM,
182 	sizeof(mnt_version)/sizeof(mnt_version[0]),
183 	mnt_version,
184 	&mnt_stats,
185 };
186