NFS: Remake nfsroot_mount as a permanent part of NFS client
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / nfs / mount_clnt.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
17#include <linux/sunrpc/sched.h>
18#include <linux/nfs_fs.h>
19
20#ifdef RPC_DEBUG
3ea97309 21# define NFSDBG_FACILITY NFSDBG_MOUNT
1da177e4
LT
22#endif
23
24/*
25#define MOUNT_PROGRAM 100005
26#define MOUNT_VERSION 1
27#define MOUNT_MNT 1
28#define MOUNT_UMNT 3
29 */
30
1da177e4
LT
31static struct rpc_program mnt_program;
32
33struct mnt_fhstatus {
34 unsigned int status;
35 struct nfs_fh * fh;
36};
37
3ea97309
CL
38/**
39 * nfs_mount - Obtain an NFS file handle for the given host and path
40 * @addr: pointer to server's address
41 * @len: size of server's address
42 * @hostname: name of server host, or NULL
43 * @path: pointer to string containing export path to mount
44 * @version: mount version to use for this request
45 * @protocol: transport protocol to use for thie request
46 * @fh: pointer to location to place returned file handle
47 *
48 * Uses default timeout parameters specified by underlying transport.
1da177e4 49 */
3ea97309
CL
50int nfs_mount(struct sockaddr *addr, size_t len, char *hostname, char *path,
51 int version, int protocol, struct nfs_fh *fh)
1da177e4 52{
1da177e4
LT
53 struct mnt_fhstatus result = {
54 .fh = fh
55 };
dead28da
CL
56 struct rpc_message msg = {
57 .rpc_argp = path,
58 .rpc_resp = &result,
59 };
3ea97309
CL
60 struct rpc_create_args args = {
61 .protocol = protocol,
62 .address = addr,
63 .addrsize = len,
64 .servername = hostname,
65 .program = &mnt_program,
66 .version = version,
67 .authflavor = RPC_AUTH_UNIX,
68 .flags = RPC_CLNT_CREATE_INTR,
69 };
70 struct rpc_clnt *mnt_clnt;
1da177e4 71 int status;
1da177e4 72
3ea97309
CL
73 dprintk("NFS: sending MNT request for %s:%s\n",
74 (hostname ? hostname : "server"), path);
1da177e4 75
3ea97309 76 mnt_clnt = rpc_create(&args);
1da177e4
LT
77 if (IS_ERR(mnt_clnt))
78 return PTR_ERR(mnt_clnt);
79
dead28da
CL
80 if (version == NFS_MNT3_VERSION)
81 msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
82 else
83 msg.rpc_proc = &mnt_clnt->cl_procinfo[MNTPROC_MNT];
84
85 status = rpc_call_sync(mnt_clnt, &msg, 0);
90c5755f 86 rpc_shutdown_client(mnt_clnt);
1da177e4
LT
87 return status < 0? status : (result.status? -EACCES : 0);
88}
89
1da177e4
LT
90/*
91 * XDR encode/decode functions for MOUNT
92 */
93static int
d21ec0c3 94xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p, const char *path)
1da177e4
LT
95{
96 p = xdr_encode_string(p, path);
97
98 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
99 return 0;
100}
101
102static int
d21ec0c3 103xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p, struct mnt_fhstatus *res)
1da177e4
LT
104{
105 struct nfs_fh *fh = res->fh;
106
107 if ((res->status = ntohl(*p++)) == 0) {
108 fh->size = NFS2_FHSIZE;
109 memcpy(fh->data, p, NFS2_FHSIZE);
110 }
111 return 0;
112}
113
114static int
d21ec0c3 115xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p, struct mnt_fhstatus *res)
1da177e4
LT
116{
117 struct nfs_fh *fh = res->fh;
118
119 if ((res->status = ntohl(*p++)) == 0) {
120 int size = ntohl(*p++);
121 if (size <= NFS3_FHSIZE) {
122 fh->size = size;
123 memcpy(fh->data, p, size);
124 } else
125 res->status = -EBADHANDLE;
126 }
127 return 0;
128}
129
130#define MNT_dirpath_sz (1 + 256)
131#define MNT_fhstatus_sz (1 + 8)
2bea90d4 132#define MNT_fhstatus3_sz (1 + 16)
1da177e4
LT
133
134static struct rpc_procinfo mnt_procedures[] = {
135[MNTPROC_MNT] = {
136 .p_proc = MNTPROC_MNT,
137 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
138 .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
2bea90d4
CL
139 .p_arglen = MNT_dirpath_sz,
140 .p_replen = MNT_fhstatus_sz,
cc0175c1
CL
141 .p_statidx = MNTPROC_MNT,
142 .p_name = "MOUNT",
1da177e4
LT
143 },
144};
145
146static struct rpc_procinfo mnt3_procedures[] = {
147[MOUNTPROC3_MNT] = {
148 .p_proc = MOUNTPROC3_MNT,
149 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
150 .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
2bea90d4
CL
151 .p_arglen = MNT_dirpath_sz,
152 .p_replen = MNT_fhstatus3_sz,
cc0175c1
CL
153 .p_statidx = MOUNTPROC3_MNT,
154 .p_name = "MOUNT",
1da177e4
LT
155 },
156};
157
158
159static struct rpc_version mnt_version1 = {
160 .number = 1,
161 .nrprocs = 2,
162 .procs = mnt_procedures
163};
164
165static struct rpc_version mnt_version3 = {
166 .number = 3,
167 .nrprocs = 2,
168 .procs = mnt3_procedures
169};
170
171static struct rpc_version * mnt_version[] = {
172 NULL,
173 &mnt_version1,
174 NULL,
175 &mnt_version3,
176};
177
178static struct rpc_stat mnt_stats;
179
180static struct rpc_program mnt_program = {
181 .name = "mount",
182 .number = NFS_MNT_PROGRAM,
e8c96f8c 183 .nrvers = ARRAY_SIZE(mnt_version),
1da177e4
LT
184 .version = mnt_version,
185 .stats = &mnt_stats,
186};