blob: ca5a266a3140430266e7c57a2292134218cad6c1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/sunrpc/sched.h>
18#include <linux/nfs_fs.h>
19
20#ifdef RPC_DEBUG
21# define NFSDBG_FACILITY NFSDBG_ROOT
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
31static struct rpc_clnt * mnt_create(char *, struct sockaddr_in *,
32 int, int);
33static struct rpc_program mnt_program;
34
35struct mnt_fhstatus {
36 unsigned int status;
37 struct nfs_fh * fh;
38};
39
40/*
41 * Obtain an NFS file handle for the given host and path
42 */
43int
44nfsroot_mount(struct sockaddr_in *addr, char *path, struct nfs_fh *fh,
45 int version, int protocol)
46{
47 struct rpc_clnt *mnt_clnt;
48 struct mnt_fhstatus result = {
49 .fh = fh
50 };
Chuck Leverdead28d2006-03-20 13:44:23 -050051 struct rpc_message msg = {
52 .rpc_argp = path,
53 .rpc_resp = &result,
54 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char hostname[32];
56 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 dprintk("NFS: nfs_mount(%08x:%s)\n",
59 (unsigned)ntohl(addr->sin_addr.s_addr), path);
60
61 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr->sin_addr.s_addr));
62 mnt_clnt = mnt_create(hostname, addr, version, protocol);
63 if (IS_ERR(mnt_clnt))
64 return PTR_ERR(mnt_clnt);
65
Chuck Leverdead28d2006-03-20 13:44:23 -050066 if (version == NFS_MNT3_VERSION)
67 msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
68 else
69 msg.rpc_proc = &mnt_clnt->cl_procinfo[MNTPROC_MNT];
70
71 status = rpc_call_sync(mnt_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return status < 0? status : (result.status? -EACCES : 0);
73}
74
75static struct rpc_clnt *
76mnt_create(char *hostname, struct sockaddr_in *srvaddr, int version,
77 int protocol)
78{
Chuck Lever058ad9c2006-08-27 17:23:53 -040079 struct rpc_create_args args = {
80 .protocol = protocol,
81 .address = (struct sockaddr *)srvaddr,
82 .addrsize = sizeof(*srvaddr),
83 .servername = hostname,
84 .program = &mnt_program,
85 .version = version,
86 .authflavor = RPC_AUTH_UNIX,
87 .flags = (RPC_CLNT_CREATE_ONESHOT |
88 RPC_CLNT_CREATE_INTR),
89 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Chuck Lever058ad9c2006-08-27 17:23:53 -040091 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94/*
95 * XDR encode/decode functions for MOUNT
96 */
97static int
Al Virod21ec0c2006-10-19 23:28:52 -070098xdr_encode_dirpath(struct rpc_rqst *req, __be32 *p, const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 p = xdr_encode_string(p, path);
101
102 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
103 return 0;
104}
105
106static int
Al Virod21ec0c2006-10-19 23:28:52 -0700107xdr_decode_fhstatus(struct rpc_rqst *req, __be32 *p, struct mnt_fhstatus *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct nfs_fh *fh = res->fh;
110
111 if ((res->status = ntohl(*p++)) == 0) {
112 fh->size = NFS2_FHSIZE;
113 memcpy(fh->data, p, NFS2_FHSIZE);
114 }
115 return 0;
116}
117
118static int
Al Virod21ec0c2006-10-19 23:28:52 -0700119xdr_decode_fhstatus3(struct rpc_rqst *req, __be32 *p, struct mnt_fhstatus *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct nfs_fh *fh = res->fh;
122
123 if ((res->status = ntohl(*p++)) == 0) {
124 int size = ntohl(*p++);
125 if (size <= NFS3_FHSIZE) {
126 fh->size = size;
127 memcpy(fh->data, p, size);
128 } else
129 res->status = -EBADHANDLE;
130 }
131 return 0;
132}
133
134#define MNT_dirpath_sz (1 + 256)
135#define MNT_fhstatus_sz (1 + 8)
Chuck Lever2bea90d2007-03-29 16:47:53 -0400136#define MNT_fhstatus3_sz (1 + 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138static struct rpc_procinfo mnt_procedures[] = {
139[MNTPROC_MNT] = {
140 .p_proc = MNTPROC_MNT,
141 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
142 .p_decode = (kxdrproc_t) xdr_decode_fhstatus,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400143 .p_arglen = MNT_dirpath_sz,
144 .p_replen = MNT_fhstatus_sz,
Chuck Levercc0175c2006-03-20 13:44:22 -0500145 .p_statidx = MNTPROC_MNT,
146 .p_name = "MOUNT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 },
148};
149
150static struct rpc_procinfo mnt3_procedures[] = {
151[MOUNTPROC3_MNT] = {
152 .p_proc = MOUNTPROC3_MNT,
153 .p_encode = (kxdrproc_t) xdr_encode_dirpath,
154 .p_decode = (kxdrproc_t) xdr_decode_fhstatus3,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400155 .p_arglen = MNT_dirpath_sz,
156 .p_replen = MNT_fhstatus3_sz,
Chuck Levercc0175c2006-03-20 13:44:22 -0500157 .p_statidx = MOUNTPROC3_MNT,
158 .p_name = "MOUNT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 },
160};
161
162
163static struct rpc_version mnt_version1 = {
164 .number = 1,
165 .nrprocs = 2,
166 .procs = mnt_procedures
167};
168
169static struct rpc_version mnt_version3 = {
170 .number = 3,
171 .nrprocs = 2,
172 .procs = mnt3_procedures
173};
174
175static struct rpc_version * mnt_version[] = {
176 NULL,
177 &mnt_version1,
178 NULL,
179 &mnt_version3,
180};
181
182static struct rpc_stat mnt_stats;
183
184static struct rpc_program mnt_program = {
185 .name = "mount",
186 .number = NFS_MNT_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800187 .nrvers = ARRAY_SIZE(mnt_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .version = mnt_version,
189 .stats = &mnt_stats,
190};