summaryrefslogtreecommitdiffstats
path: root/src/net/oncrpc/nfs.c
blob: b6118f91a4d2af78cc2fcc02b96a1f2f92bb744a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <byteswap.h>
#include <ipxe/time.h>
#include <ipxe/iobuf.h>
#include <ipxe/open.h>
#include <ipxe/features.h>
#include <ipxe/nfs.h>
#include <ipxe/oncrpc.h>
#include <ipxe/oncrpc_iob.h>
#include <ipxe/portmap.h>
#include <ipxe/mount.h>
#include <ipxe/settings.h>

/** @file
 *
 * Network File System protocol
 *
 */

/** NFS LOOKUP procedure */
#define NFS_LOOKUP      3
/** NFS READLINK procedure */
#define NFS_READLINK    5
/** NFS READ procedure */
#define NFS_READ        6

/**
 * Extract a file handle from the beginning of an I/O buffer
 *
 * @v io_buf            I/O buffer
 * @v fh                File handle
 * @ret size            Size of the data read
 */
size_t nfs_iob_get_fh ( struct io_buffer *io_buf, struct nfs_fh *fh ) {
	fh->size = oncrpc_iob_get_int ( io_buf );

	if ( fh->size > 64 )
		return sizeof ( uint32_t );

	memcpy (fh->fh, io_buf->data, fh->size );
	iob_pull ( io_buf, fh->size );

	return fh->size + sizeof ( uint32_t );
}

/**
 * Add a file handle to the end of an I/O buffer
 *
 * @v io_buf            I/O buffer
 * @v fh                File handle
 * @ret size            Size of the data written
 */
size_t nfs_iob_add_fh ( struct io_buffer *io_buf, const struct nfs_fh *fh ) {
	size_t s;

	s = oncrpc_iob_add_int ( io_buf, fh->size );
	memcpy ( iob_put ( io_buf, fh->size ), &fh->fh, fh->size );

	return s + fh->size;
}

/**
 * Send a LOOKUP request
 *
 * @v intf              Interface to send the request on
 * @v session           ONC RPC session
 * @v fh                The file handle of the the directory
 * @v filename          The file name
 * @ret rc              Return status code
 */
int nfs_lookup ( struct interface *intf, struct oncrpc_session *session,
                 const struct nfs_fh *fh, const char *filename ) {
	struct oncrpc_field fields[] = {
		ONCRPC_SUBFIELD ( array, fh->size, &fh->fh ),
		ONCRPC_FIELD ( str, filename ),
		ONCRPC_FIELD_END,
	};

	return oncrpc_call ( intf, session, NFS_LOOKUP, fields );
}

/**
 * Send a READLINK request
 *
 * @v intf              Interface to send the request on
 * @v session           ONC RPC session
 * @v fh                The symlink file handle
 * @ret rc              Return status code
 */
int nfs_readlink ( struct interface *intf, struct oncrpc_session *session,
                   const struct nfs_fh *fh ) {
	struct oncrpc_field fields[] = {
		ONCRPC_SUBFIELD ( array, fh->size, &fh->fh ),
		ONCRPC_FIELD_END,
	};

	return oncrpc_call ( intf, session, NFS_READLINK, fields );
}

/**
 * Send a READ request
 *
 * @v intf              Interface to send the request on
 * @v session           ONC RPC session
 * @v fh                The file handle
 * @v offset            Offset
 * @v count             Byte count
 * @ret rc              Return status code
 */
int nfs_read ( struct interface *intf, struct oncrpc_session *session,
               const struct nfs_fh *fh, uint64_t offset, uint32_t count ) {
	struct oncrpc_field fields[] = {
		ONCRPC_SUBFIELD ( array, fh->size, &fh->fh ),
		ONCRPC_FIELD ( int64, offset ),
		ONCRPC_FIELD ( int32, count ),
		ONCRPC_FIELD_END,
	};

	return oncrpc_call ( intf, session, NFS_READ, fields );
}

/**
 * Parse a LOOKUP reply
 *
 * @v lookup_reply      A structure where the data will be saved
 * @v reply             The ONC RPC reply to get data from
 * @ret rc              Return status code
 */
int nfs_get_lookup_reply ( struct nfs_lookup_reply *lookup_reply,
                           struct oncrpc_reply *reply ) {
	if ( ! lookup_reply || ! reply )
		return -EINVAL;

	lookup_reply->status = oncrpc_iob_get_int ( reply->data );
	switch ( lookup_reply->status )
	{
	case NFS3_OK:
		break;
	case NFS3ERR_PERM:
		return -EPERM;
	case NFS3ERR_NOENT:
		return -ENOENT;
	case NFS3ERR_IO:
		return -EIO;
	case NFS3ERR_ACCES:
		return -EACCES;
	case NFS3ERR_NOTDIR:
		return -ENOTDIR;
	case NFS3ERR_NAMETOOLONG:
		return -ENAMETOOLONG;
	case NFS3ERR_STALE:
		return -ESTALE;
	case NFS3ERR_BADHANDLE:
	case NFS3ERR_SERVERFAULT:
	default:
		return -EPROTO;
	}

	nfs_iob_get_fh ( reply->data, &lookup_reply->fh );

	if ( oncrpc_iob_get_int ( reply->data ) == 1 )
		lookup_reply->ent_type = oncrpc_iob_get_int ( reply->data );

	return 0;
}
/**
 * Parse a READLINK reply
 *
 * @v readlink_reply    A structure where the data will be saved
 * @v reply             The ONC RPC reply to get data from
 * @ret rc              Return status code
 */
int nfs_get_readlink_reply ( struct nfs_readlink_reply *readlink_reply,
                             struct oncrpc_reply *reply ) {
	if ( ! readlink_reply || ! reply )
		return -EINVAL;

	readlink_reply->status = oncrpc_iob_get_int ( reply->data );
	switch ( readlink_reply->status )
	{
	case NFS3_OK:
		 break;
	case NFS3ERR_IO:
		return -EIO;
	case NFS3ERR_ACCES:
		return -EACCES;
	case NFS3ERR_INVAL:
		return -EINVAL;
	case NFS3ERR_NOTSUPP:
		return -ENOTSUP;
	case NFS3ERR_STALE:
		return -ESTALE;
	case NFS3ERR_BADHANDLE:
	case NFS3ERR_SERVERFAULT:
	default:
		return -EPROTO;
	}

	if ( oncrpc_iob_get_int ( reply->data ) == 1 )
		iob_pull ( reply->data, 5 * sizeof ( uint32_t ) +
		                        8 * sizeof ( uint64_t ) );

	readlink_reply->path_len = oncrpc_iob_get_int ( reply->data );
	readlink_reply->path     = reply->data->data;

	return 0;
}

/**
 * Parse a READ reply
 *
 * @v read_reply        A structure where the data will be saved
 * @v reply             The ONC RPC reply to get data from
 * @ret rc              Return status code
 */
int nfs_get_read_reply ( struct nfs_read_reply *read_reply,
                         struct oncrpc_reply *reply ) {
	if ( ! read_reply || ! reply )
		return -EINVAL;

	read_reply->status = oncrpc_iob_get_int ( reply->data );
	switch ( read_reply->status )
	{
	case NFS3_OK:
		 break;
	case NFS3ERR_PERM:
		return -EPERM;
	case NFS3ERR_NOENT:
		return -ENOENT;
	case NFS3ERR_IO:
		return -EIO;
	case NFS3ERR_NXIO:
		return -ENXIO;
	case NFS3ERR_ACCES:
		return -EACCES;
	case NFS3ERR_INVAL:
		return -EINVAL;
	case NFS3ERR_STALE:
		return -ESTALE;
	case NFS3ERR_BADHANDLE:
	case NFS3ERR_SERVERFAULT:
	default:
		return -EPROTO;
	}

	if ( oncrpc_iob_get_int ( reply->data ) == 1 )
	{
		iob_pull ( reply->data, 5 * sizeof ( uint32_t ) );
		read_reply->filesize = oncrpc_iob_get_int64 ( reply->data );
		iob_pull ( reply->data, 7 * sizeof ( uint64_t ) );
	}

	read_reply->count    = oncrpc_iob_get_int ( reply->data );
	read_reply->eof      = oncrpc_iob_get_int ( reply->data );
	read_reply->data_len = oncrpc_iob_get_int ( reply->data );
	read_reply->data     = reply->data->data;

	if ( read_reply->count != read_reply->data_len )
		return -EPROTO;

	return 0;
}