SUNRPC: remove extraneous header inclusions
[safe/jmp/linux-2.6] / fs / nfs / mount_clnt.c
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/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
31 static struct rpc_clnt *        mnt_create(char *, struct sockaddr_in *,
32                                                                 int, int);
33 static struct rpc_program       mnt_program;
34
35 struct 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  */
43 int
44 nfsroot_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         };
51         struct rpc_message msg  = {
52                 .rpc_argp       = path,
53                 .rpc_resp       = &result,
54         };
55         char                    hostname[32];
56         int                     status;
57
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
66         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);
72         return status < 0? status : (result.status? -EACCES : 0);
73 }
74
75 static struct rpc_clnt *
76 mnt_create(char *hostname, struct sockaddr_in *srvaddr, int version,
77                 int protocol)
78 {
79         struct rpc_xprt *xprt;
80         struct rpc_clnt *clnt;
81
82         xprt = xprt_create_proto(protocol, srvaddr, NULL);
83         if (IS_ERR(xprt))
84                 return (struct rpc_clnt *)xprt;
85
86         clnt = rpc_create_client(xprt, hostname,
87                                 &mnt_program, version,
88                                 RPC_AUTH_UNIX);
89         if (!IS_ERR(clnt)) {
90                 clnt->cl_softrtry = 1;
91                 clnt->cl_oneshot  = 1;
92                 clnt->cl_intr = 1;
93         }
94         return clnt;
95 }
96
97 /*
98  * XDR encode/decode functions for MOUNT
99  */
100 static int
101 xdr_encode_dirpath(struct rpc_rqst *req, u32 *p, const char *path)
102 {
103         p = xdr_encode_string(p, path);
104
105         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
106         return 0;
107 }
108
109 static int
110 xdr_decode_fhstatus(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
111 {
112         struct nfs_fh *fh = res->fh;
113
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
122 xdr_decode_fhstatus3(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
123 {
124         struct nfs_fh *fh = res->fh;
125
126         if ((res->status = ntohl(*p++)) == 0) {
127                 int size = ntohl(*p++);
128                 if (size <= NFS3_FHSIZE) {
129                         fh->size = size;
130                         memcpy(fh->data, p, size);
131                 } else
132                         res->status = -EBADHANDLE;
133         }
134         return 0;
135 }
136
137 #define MNT_dirpath_sz          (1 + 256)
138 #define MNT_fhstatus_sz         (1 + 8)
139
140 static struct rpc_procinfo      mnt_procedures[] = {
141 [MNTPROC_MNT] = {
142           .p_proc               = MNTPROC_MNT,
143           .p_encode             = (kxdrproc_t) xdr_encode_dirpath,      
144           .p_decode             = (kxdrproc_t) xdr_decode_fhstatus,
145           .p_bufsiz             = MNT_dirpath_sz << 2,
146           .p_statidx            = MNTPROC_MNT,
147           .p_name               = "MOUNT",
148         },
149 };
150
151 static struct rpc_procinfo mnt3_procedures[] = {
152 [MOUNTPROC3_MNT] = {
153           .p_proc               = MOUNTPROC3_MNT,
154           .p_encode             = (kxdrproc_t) xdr_encode_dirpath,
155           .p_decode             = (kxdrproc_t) xdr_decode_fhstatus3,
156           .p_bufsiz             = MNT_dirpath_sz << 2,
157           .p_statidx            = MOUNTPROC3_MNT,
158           .p_name               = "MOUNT",
159         },
160 };
161
162
163 static struct rpc_version       mnt_version1 = {
164                 .number         = 1,
165                 .nrprocs        = 2,
166                 .procs          = mnt_procedures
167 };
168
169 static struct rpc_version       mnt_version3 = {
170                 .number         = 3,
171                 .nrprocs        = 2,
172                 .procs          = mnt3_procedures
173 };
174
175 static struct rpc_version *     mnt_version[] = {
176         NULL,
177         &mnt_version1,
178         NULL,
179         &mnt_version3,
180 };
181
182 static struct rpc_stat          mnt_stats;
183
184 static struct rpc_program       mnt_program = {
185         .name           = "mount",
186         .number         = NFS_MNT_PROGRAM,
187         .nrvers         = ARRAY_SIZE(mnt_version),
188         .version        = mnt_version,
189         .stats          = &mnt_stats,
190 };