Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[linux-2.6.git] / 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/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
32 static struct rpc_clnt *        mnt_create(char *, struct sockaddr_in *,
33                                                                 int, int);
34 static struct rpc_program       mnt_program;
35
36 struct 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  */
44 int
45 nfsroot_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         };
52         struct rpc_message msg  = {
53                 .rpc_argp       = path,
54                 .rpc_resp       = &result,
55         };
56         char                    hostname[32];
57         int                     status;
58
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
67         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);
73         return status < 0? status : (result.status? -EACCES : 0);
74 }
75
76 static struct rpc_clnt *
77 mnt_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);
90         if (!IS_ERR(clnt)) {
91                 clnt->cl_softrtry = 1;
92                 clnt->cl_oneshot  = 1;
93                 clnt->cl_intr = 1;
94                 clnt->cl_tagxid = 1;
95         }
96         return clnt;
97 }
98
99 /*
100  * XDR encode/decode functions for MOUNT
101  */
102 static int
103 xdr_encode_dirpath(struct rpc_rqst *req, u32 *p, const char *path)
104 {
105         p = xdr_encode_string(p, path);
106
107         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
108         return 0;
109 }
110
111 static int
112 xdr_decode_fhstatus(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
113 {
114         struct nfs_fh *fh = res->fh;
115
116         if ((res->status = ntohl(*p++)) == 0) {
117                 fh->size = NFS2_FHSIZE;
118                 memcpy(fh->data, p, NFS2_FHSIZE);
119         }
120         return 0;
121 }
122
123 static int
124 xdr_decode_fhstatus3(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
125 {
126         struct nfs_fh *fh = res->fh;
127
128         if ((res->status = ntohl(*p++)) == 0) {
129                 int size = ntohl(*p++);
130                 if (size <= NFS3_FHSIZE) {
131                         fh->size = size;
132                         memcpy(fh->data, p, size);
133                 } else
134                         res->status = -EBADHANDLE;
135         }
136         return 0;
137 }
138
139 #define MNT_dirpath_sz          (1 + 256)
140 #define MNT_fhstatus_sz         (1 + 8)
141
142 static struct rpc_procinfo      mnt_procedures[] = {
143 [MNTPROC_MNT] = {
144           .p_proc               = MNTPROC_MNT,
145           .p_encode             = (kxdrproc_t) xdr_encode_dirpath,      
146           .p_decode             = (kxdrproc_t) xdr_decode_fhstatus,
147           .p_bufsiz             = MNT_dirpath_sz << 2,
148           .p_statidx            = MNTPROC_MNT,
149           .p_name               = "MOUNT",
150         },
151 };
152
153 static struct rpc_procinfo mnt3_procedures[] = {
154 [MOUNTPROC3_MNT] = {
155           .p_proc               = MOUNTPROC3_MNT,
156           .p_encode             = (kxdrproc_t) xdr_encode_dirpath,
157           .p_decode             = (kxdrproc_t) xdr_decode_fhstatus3,
158           .p_bufsiz             = MNT_dirpath_sz << 2,
159           .p_statidx            = MOUNTPROC3_MNT,
160           .p_name               = "MOUNT",
161         },
162 };
163
164
165 static struct rpc_version       mnt_version1 = {
166                 .number         = 1,
167                 .nrprocs        = 2,
168                 .procs          = mnt_procedures
169 };
170
171 static struct rpc_version       mnt_version3 = {
172                 .number         = 3,
173                 .nrprocs        = 2,
174                 .procs          = mnt3_procedures
175 };
176
177 static struct rpc_version *     mnt_version[] = {
178         NULL,
179         &mnt_version1,
180         NULL,
181         &mnt_version3,
182 };
183
184 static struct rpc_stat          mnt_stats;
185
186 static struct rpc_program       mnt_program = {
187         .name           = "mount",
188         .number         = NFS_MNT_PROGRAM,
189         .nrvers         = ARRAY_SIZE(mnt_version),
190         .version        = mnt_version,
191         .stats          = &mnt_stats,
192 };