blob: 445abb4d42146e445e515c6a85292d8b8ecb1969 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
32static struct rpc_clnt * mnt_create(char *, struct sockaddr_in *,
33 int, int);
34static struct rpc_program mnt_program;
35
36struct 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 */
44int
45nfsroot_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 = {
50 .fh = fh
51 };
Chuck Leverdead28d2006-03-20 13:44:23 -050052 struct rpc_message msg = {
53 .rpc_argp = path,
54 .rpc_resp = &result,
55 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 char hostname[32];
57 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 dprintk("NFS: nfs_mount(%08x:%s)\n",
60 (unsigned)ntohl(addr->sin_addr.s_addr), path);
61
62 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr->sin_addr.s_addr));
63 mnt_clnt = mnt_create(hostname, addr, version, protocol);
64 if (IS_ERR(mnt_clnt))
65 return PTR_ERR(mnt_clnt);
66
Chuck Leverdead28d2006-03-20 13:44:23 -050067 if (version == NFS_MNT3_VERSION)
68 msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
69 else
70 msg.rpc_proc = &mnt_clnt->cl_procinfo[MNTPROC_MNT];
71
72 status = rpc_call_sync(mnt_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return status < 0? status : (result.status? -EACCES : 0);
74}
75
76static struct rpc_clnt *
77mnt_create(char *hostname, struct sockaddr_in *srvaddr, int version,
78 int protocol)
79{
80 struct rpc_xprt *xprt;
81 struct rpc_clnt *clnt;
82
83 xprt = xprt_create_proto(protocol, srvaddr, NULL);
84 if (IS_ERR(xprt))
85 return (struct rpc_clnt *)xprt;
86
87 clnt = rpc_create_client(xprt, hostname,
88 &mnt_program, version,
89 RPC_AUTH_UNIX);
Trond Myklebust5b616f52005-06-22 17:16:20 +000090 if (!IS_ERR(clnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 clnt->cl_softrtry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 clnt->cl_oneshot = 1;
93 clnt->cl_intr = 1;
94 }
95 return clnt;
96}
97
98/*
99 * XDR encode/decode functions for MOUNT
100 */
101static int
102xdr_encode_dirpath(struct rpc_rqst *req, u32 *p, const char *path)
103{
104 p = xdr_encode_string(p, path);
105
106 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
107 return 0;
108}
109
110static int
111xdr_decode_fhstatus(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
112{
113 struct nfs_fh *fh = res->fh;
114
115 if ((res->status = ntohl(*p++)) == 0) {
116 fh->size = NFS2_FHSIZE;
117 memcpy(fh->data, p, NFS2_FHSIZE);
118 }
119 return 0;
120}
121
122static int
123xdr_decode_fhstatus3(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
124{
125 struct nfs_fh *fh = res->fh;
126
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
141static struct rpc_procinfo mnt_procedures[] = {
142[MNTPROC_MNT] = {
143 .p_proc = MNTPROC_MNT,
144 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
145 .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
146 .p_bufsiz = MNT_dirpath_sz << 2,
Chuck Levercc0175c2006-03-20 13:44:22 -0500147 .p_statidx = MNTPROC_MNT,
148 .p_name = "MOUNT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 },
150};
151
152static struct rpc_procinfo mnt3_procedures[] = {
153[MOUNTPROC3_MNT] = {
154 .p_proc = MOUNTPROC3_MNT,
155 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
156 .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
157 .p_bufsiz = MNT_dirpath_sz << 2,
Chuck Levercc0175c2006-03-20 13:44:22 -0500158 .p_statidx = MOUNTPROC3_MNT,
159 .p_name = "MOUNT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 },
161};
162
163
164static struct rpc_version mnt_version1 = {
165 .number = 1,
166 .nrprocs = 2,
167 .procs = mnt_procedures
168};
169
170static struct rpc_version mnt_version3 = {
171 .number = 3,
172 .nrprocs = 2,
173 .procs = mnt3_procedures
174};
175
176static struct rpc_version * mnt_version[] = {
177 NULL,
178 &mnt_version1,
179 NULL,
180 &mnt_version3,
181};
182
183static struct rpc_stat mnt_stats;
184
185static struct rpc_program mnt_program = {
186 .name = "mount",
187 .number = NFS_MNT_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800188 .nrvers = ARRAY_SIZE(mnt_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .version = mnt_version,
190 .stats = &mnt_stats,
191};