Merge branch 'for-next' of git://www.jni.nu/cris into cris_merge
[linux-2.6-block.git] / fs / nfs / mount_clnt.c
CommitLineData
1da177e4 1/*
19207231 2 * In-kernel MOUNT protocol client
1da177e4
LT
3 *
4 * Copyright (C) 1997, Olaf Kirch <okir@monad.swb.de>
5 */
6
7#include <linux/types.h>
8#include <linux/socket.h>
9#include <linux/kernel.h>
10#include <linux/errno.h>
11#include <linux/uio.h>
12#include <linux/net.h>
13#include <linux/in.h>
14#include <linux/sunrpc/clnt.h>
1da177e4
LT
15#include <linux/sunrpc/sched.h>
16#include <linux/nfs_fs.h>
8491945f 17#include "internal.h"
1da177e4
LT
18
19#ifdef RPC_DEBUG
3ea97309 20# define NFSDBG_FACILITY NFSDBG_MOUNT
1da177e4
LT
21#endif
22
1da177e4
LT
23static struct rpc_program mnt_program;
24
25struct mnt_fhstatus {
19207231
CL
26 u32 status;
27 struct nfs_fh *fh;
1da177e4
LT
28};
29
3ea97309
CL
30/**
31 * nfs_mount - Obtain an NFS file handle for the given host and path
32 * @addr: pointer to server's address
33 * @len: size of server's address
34 * @hostname: name of server host, or NULL
35 * @path: pointer to string containing export path to mount
36 * @version: mount version to use for this request
37 * @protocol: transport protocol to use for thie request
38 * @fh: pointer to location to place returned file handle
39 *
40 * Uses default timeout parameters specified by underlying transport.
1da177e4 41 */
3ea97309
CL
42int nfs_mount(struct sockaddr *addr, size_t len, char *hostname, char *path,
43 int version, int protocol, struct nfs_fh *fh)
1da177e4 44{
1da177e4
LT
45 struct mnt_fhstatus result = {
46 .fh = fh
47 };
dead28da
CL
48 struct rpc_message msg = {
49 .rpc_argp = path,
50 .rpc_resp = &result,
51 };
3ea97309
CL
52 struct rpc_create_args args = {
53 .protocol = protocol,
54 .address = addr,
55 .addrsize = len,
56 .servername = hostname,
57 .program = &mnt_program,
58 .version = version,
59 .authflavor = RPC_AUTH_UNIX,
150030b7 60 .flags = 0,
3ea97309
CL
61 };
62 struct rpc_clnt *mnt_clnt;
1da177e4 63 int status;
1da177e4 64
3ea97309
CL
65 dprintk("NFS: sending MNT request for %s:%s\n",
66 (hostname ? hostname : "server"), path);
1da177e4 67
3ea97309 68 mnt_clnt = rpc_create(&args);
1da177e4 69 if (IS_ERR(mnt_clnt))
013a8c1a 70 goto out_clnt_err;
1da177e4 71
dead28da
CL
72 if (version == NFS_MNT3_VERSION)
73 msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
74 else
75 msg.rpc_proc = &mnt_clnt->cl_procinfo[MNTPROC_MNT];
76
77 status = rpc_call_sync(mnt_clnt, &msg, 0);
90c5755f 78 rpc_shutdown_client(mnt_clnt);
013a8c1a
CL
79
80 if (status < 0)
81 goto out_call_err;
82 if (result.status != 0)
83 goto out_mnt_err;
84
85 dprintk("NFS: MNT request succeeded\n");
86 status = 0;
87
88out:
89 return status;
90
91out_clnt_err:
92 status = PTR_ERR(mnt_clnt);
93 dprintk("NFS: failed to create RPC client, status=%d\n", status);
94 goto out;
95
96out_call_err:
97 dprintk("NFS: failed to start MNT request, status=%d\n", status);
98 goto out;
99
100out_mnt_err:
101 dprintk("NFS: MNT server returned result %d\n", result.status);
8491945f 102 status = nfs_stat_to_errno(result.status);
013a8c1a 103 goto out;
1da177e4
LT
104}
105
1da177e4
LT
106/*
107 * XDR encode/decode functions for MOUNT
108 */
19207231
CL
109static int xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p,
110 const char *path)
1da177e4
LT
111{
112 p = xdr_encode_string(p, path);
113
114 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
115 return 0;
116}
117
19207231
CL
118static int xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p,
119 struct mnt_fhstatus *res)
1da177e4
LT
120{
121 struct nfs_fh *fh = res->fh;
122
123 if ((res->status = ntohl(*p++)) == 0) {
124 fh->size = NFS2_FHSIZE;
125 memcpy(fh->data, p, NFS2_FHSIZE);
126 }
127 return 0;
128}
129
19207231
CL
130static int xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p,
131 struct mnt_fhstatus *res)
1da177e4
LT
132{
133 struct nfs_fh *fh = res->fh;
b7e24457 134 unsigned size;
1da177e4
LT
135
136 if ((res->status = ntohl(*p++)) == 0) {
b7e24457
TM
137 size = ntohl(*p++);
138 if (size <= NFS3_FHSIZE && size != 0) {
1da177e4
LT
139 fh->size = size;
140 memcpy(fh->data, p, size);
141 } else
142 res->status = -EBADHANDLE;
143 }
144 return 0;
145}
146
147#define MNT_dirpath_sz (1 + 256)
148#define MNT_fhstatus_sz (1 + 8)
2bea90d4 149#define MNT_fhstatus3_sz (1 + 16)
1da177e4 150
19207231
CL
151static struct rpc_procinfo mnt_procedures[] = {
152 [MNTPROC_MNT] = {
153 .p_proc = MNTPROC_MNT,
154 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
155 .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
156 .p_arglen = MNT_dirpath_sz,
157 .p_replen = MNT_fhstatus_sz,
158 .p_statidx = MNTPROC_MNT,
159 .p_name = "MOUNT",
1da177e4
LT
160 },
161};
162
163static struct rpc_procinfo mnt3_procedures[] = {
19207231
CL
164 [MOUNTPROC3_MNT] = {
165 .p_proc = MOUNTPROC3_MNT,
166 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
167 .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
168 .p_arglen = MNT_dirpath_sz,
169 .p_replen = MNT_fhstatus3_sz,
170 .p_statidx = MOUNTPROC3_MNT,
171 .p_name = "MOUNT",
1da177e4
LT
172 },
173};
174
175
19207231
CL
176static struct rpc_version mnt_version1 = {
177 .number = 1,
178 .nrprocs = 2,
179 .procs = mnt_procedures,
1da177e4
LT
180};
181
19207231
CL
182static struct rpc_version mnt_version3 = {
183 .number = 3,
184 .nrprocs = 2,
185 .procs = mnt3_procedures,
1da177e4
LT
186};
187
19207231 188static struct rpc_version *mnt_version[] = {
1da177e4
LT
189 NULL,
190 &mnt_version1,
191 NULL,
192 &mnt_version3,
193};
194
19207231 195static struct rpc_stat mnt_stats;
1da177e4 196
19207231 197static struct rpc_program mnt_program = {
1da177e4
LT
198 .name = "mount",
199 .number = NFS_MNT_PROGRAM,
e8c96f8c 200 .nrvers = ARRAY_SIZE(mnt_version),
1da177e4
LT
201 .version = mnt_version,
202 .stats = &mnt_stats,
203};