ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 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         char                    hostname[32];
53         int                     status;
54         int                     call;
55
56         dprintk("NFS:      nfs_mount(%08x:%s)\n",
57                         (unsigned)ntohl(addr->sin_addr.s_addr), path);
58
59         sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr->sin_addr.s_addr));
60         mnt_clnt = mnt_create(hostname, addr, version, protocol);
61         if (IS_ERR(mnt_clnt))
62                 return PTR_ERR(mnt_clnt);
63
64         call = (version == NFS_MNT3_VERSION) ? MOUNTPROC3_MNT : MNTPROC_MNT;
65         status = rpc_call(mnt_clnt, call, path, &result, 0);
66         return status < 0? status : (result.status? -EACCES : 0);
67 }
68
69 static struct rpc_clnt *
70 mnt_create(char *hostname, struct sockaddr_in *srvaddr, int version,
71                 int protocol)
72 {
73         struct rpc_xprt *xprt;
74         struct rpc_clnt *clnt;
75
76         xprt = xprt_create_proto(protocol, srvaddr, NULL);
77         if (IS_ERR(xprt))
78                 return (struct rpc_clnt *)xprt;
79
80         clnt = rpc_create_client(xprt, hostname,
81                                 &mnt_program, version,
82                                 RPC_AUTH_UNIX);
83         if (IS_ERR(clnt)) {
84                 xprt_destroy(xprt);
85         } else {
86                 clnt->cl_softrtry = 1;
87                 clnt->cl_chatty   = 1;
88                 clnt->cl_oneshot  = 1;
89                 clnt->cl_intr = 1;
90         }
91         return clnt;
92 }
93
94 /*
95  * XDR encode/decode functions for MOUNT
96  */
97 static int
98 xdr_encode_dirpath(struct rpc_rqst *req, u32 *p, const char *path)
99 {
100         p = xdr_encode_string(p, path);
101
102         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
103         return 0;
104 }
105
106 static int
107 xdr_decode_fhstatus(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
108 {
109         struct nfs_fh *fh = res->fh;
110
111         memset((void *)fh, 0, sizeof(*fh));
112         if ((res->status = ntohl(*p++)) == 0) {
113                 fh->size = NFS2_FHSIZE;
114                 memcpy(fh->data, p, NFS2_FHSIZE);
115         }
116         return 0;
117 }
118
119 static int
120 xdr_decode_fhstatus3(struct rpc_rqst *req, u32 *p, struct mnt_fhstatus *res)
121 {
122         struct nfs_fh *fh = res->fh;
123
124         memset((void *)fh, 0, sizeof(*fh));
125         if ((res->status = ntohl(*p++)) == 0) {
126                 int size = ntohl(*p++);
127                 if (size <= NFS3_FHSIZE) {
128                         fh->size = size;
129                         memcpy(fh->data, p, size);
130                 } else
131                         res->status = -EBADHANDLE;
132         }
133         return 0;
134 }
135
136 #define MNT_dirpath_sz          (1 + 256)
137 #define MNT_fhstatus_sz         (1 + 8)
138
139 static struct rpc_procinfo      mnt_procedures[] = {
140 [MNTPROC_MNT] = {
141           .p_proc               = MNTPROC_MNT,
142           .p_encode             = (kxdrproc_t) xdr_encode_dirpath,      
143           .p_decode             = (kxdrproc_t) xdr_decode_fhstatus,
144           .p_bufsiz             = MNT_dirpath_sz << 2,
145         },
146 };
147
148 static struct rpc_procinfo mnt3_procedures[] = {
149 [MOUNTPROC3_MNT] = {
150           .p_proc               = MOUNTPROC3_MNT,
151           .p_encode             = (kxdrproc_t) xdr_encode_dirpath,
152           .p_decode             = (kxdrproc_t) xdr_decode_fhstatus3,
153           .p_bufsiz             = MNT_dirpath_sz << 2,
154         },
155 };
156
157
158 static struct rpc_version       mnt_version1 = {
159                 .number         = 1,
160                 .nrprocs        = 2,
161                 .procs          = mnt_procedures
162 };
163
164 static struct rpc_version       mnt_version3 = {
165                 .number         = 3,
166                 .nrprocs        = 2,
167                 .procs          = mnt3_procedures
168 };
169
170 static struct rpc_version *     mnt_version[] = {
171         NULL,
172         &mnt_version1,
173         NULL,
174         &mnt_version3,
175 };
176
177 static struct rpc_stat          mnt_stats;
178
179 struct rpc_program      mnt_program = {
180         .name           = "mount",
181         .number         = NFS_MNT_PROGRAM,
182         .nrvers         = sizeof(mnt_version)/sizeof(mnt_version[0]),
183         .version        = mnt_version,
184         .stats          = &mnt_stats,
185 };