2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/list.h>
23 #include <gpxe/xfer.h>
24 #include <gpxe/open.h>
25 #include <gpxe/process.h>
26 #include <gpxe/posix_io.h>
32 * These functions provide traditional blocking I/O semantics. They
33 * are designed to be used by the PXE TFTP API. Because they block,
34 * they may not be used by most other portions of the gPXE codebase.
39 /** Reference count for this object */
41 /** List of open files */
42 struct list_head list;
43 /** File descriptor */
47 * Set to -EINPROGRESS while data transfer is in progress.
50 /** Data transfer interface */
51 struct xfer_interface xfer;
52 /** Current seek position */
56 /** Received data queue */
57 struct list_head data;
60 /** List of open files */
61 static LIST_HEAD ( posix_files );
63 /** Minimum file descriptor that will ever be allocated */
64 #define POSIX_FD_MIN ( 1 )
66 /** Maximum file descriptor that will ever be allocated */
67 #define POSIX_FD_MAX ( 255 )
72 * @v refcnt Reference counter
74 static void posix_file_free ( struct refcnt *refcnt ) {
75 struct posix_file *file =
76 container_of ( refcnt, struct posix_file, refcnt );
77 struct io_buffer *iobuf;
78 struct io_buffer *tmp;
80 list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
87 * Terminate file data transfer
90 * @v rc Reason for termination
92 static void posix_file_finished ( struct posix_file *file, int rc ) {
93 xfer_nullify ( &file->xfer );
94 xfer_close ( &file->xfer, rc );
99 * Handle close() event
101 * @v xfer POSIX file data transfer interface
102 * @v rc Reason for close
104 static void posix_file_xfer_close ( struct xfer_interface *xfer, int rc ) {
105 struct posix_file *file =
106 container_of ( xfer, struct posix_file, xfer );
108 posix_file_finished ( file, rc );
112 * Handle seek() event
114 * @v xfer POSIX file data transfer interface
115 * @v pos New position
116 * @ret rc Return status code
118 static int posix_file_xfer_seek ( struct xfer_interface *xfer, off_t offset,
120 struct posix_file *file =
121 container_of ( xfer, struct posix_file, xfer );
132 if ( file->filesize < file->pos )
133 file->filesize = file->pos;
139 * Handle deliver_iob() event
141 * @v xfer POSIX file data transfer interface
142 * @v iobuf I/O buffer
143 * @ret rc Return status code
145 static int posix_file_xfer_deliver_iob ( struct xfer_interface *xfer,
146 struct io_buffer *iobuf ) {
147 struct posix_file *file =
148 container_of ( xfer, struct posix_file, xfer );
150 list_add_tail ( &iobuf->list, &file->data );
154 /** POSIX file data transfer interface operations */
155 static struct xfer_interface_operations posix_file_xfer_operations = {
156 .close = posix_file_xfer_close,
157 .vredirect = xfer_vopen,
158 .request = ignore_xfer_request,
159 .seek = posix_file_xfer_seek,
160 .alloc_iob = default_xfer_alloc_iob,
161 .deliver_iob = posix_file_xfer_deliver_iob,
162 .deliver_raw = xfer_deliver_as_iob,
166 * Identify file by file descriptor
168 * @v fd File descriptor
169 * @ret file Corresponding file, or NULL
171 static struct posix_file * posix_fd_to_file ( int fd ) {
172 struct posix_file *file;
174 list_for_each_entry ( file, &posix_files, list ) {
175 if ( file->fd == fd )
182 * Find an available file descriptor
184 * @ret fd File descriptor, or negative error number
186 static int posix_find_free_fd ( void ) {
189 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
190 if ( ! posix_fd_to_file ( fd ) )
199 * @v uri_string URI string
200 * @ret fd File descriptor, or negative error number
202 int open ( const char *uri_string ) {
203 struct posix_file *file;
207 /* Find a free file descriptor to use */
208 fd = posix_find_free_fd();
212 /* Allocate and initialise structure */
213 file = malloc ( sizeof ( *file ) );
216 memset ( file, 0, sizeof ( *file ) );
217 file->refcnt.free = posix_file_free;
219 file->rc = -EINPROGRESS;
220 xfer_init ( &file->xfer, &posix_file_xfer_operations,
222 INIT_LIST_HEAD ( &file->data );
224 /* Open URI on data transfer interface */
225 if ( ( rc = xfer_open_uri ( &file->xfer, uri_string ) ) != 0 )
229 if ( ( rc = xfer_request_all ( &file->xfer ) ) != 0 )
232 /* Wait for open to succeed or fail */
233 while ( list_empty ( &file->data ) ) {
235 if ( file->rc != -EINPROGRESS ) {
241 /* Add to list of open files. List takes reference ownership. */
242 list_add ( &file->list, &posix_files );
246 posix_file_finished ( file, rc );
247 ref_put ( &file->refcnt );
252 * Read data from file
254 * @v buffer Data buffer
255 * @v offset Starting offset within data buffer
256 * @v len Maximum length to read
257 * @ret len Actual length read, or negative error number
259 ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
260 struct posix_file *file;
261 struct io_buffer *iobuf;
266 file = posix_fd_to_file ( fd );
271 /* Try to fetch more data if none available */
272 if ( list_empty ( &file->data ) )
274 /* Dequeue at most one received I/O buffer into user buffer */
275 list_for_each_entry ( iobuf, &file->data, list ) {
276 frag_len = iob_len ( iobuf );
277 if ( frag_len > max_len )
279 copy_to_user ( buffer, offset, iobuf->data,
281 iob_pull ( iobuf, frag_len );
282 if ( ! iob_len ( iobuf ) )
284 file->pos += frag_len;
290 /* If buffer is full, return */
293 /* If file has completed, return */
294 if ( file->rc != -EINPROGRESS )
295 return ( file->rc ? file->rc : len );
300 * Determine file size
302 * @v fd File descriptor
303 * @ret size File size, or negative error number
305 ssize_t fsize ( int fd ) {
306 struct posix_file *file;
309 file = posix_fd_to_file ( fd );
313 return file->filesize;
319 * @v fd File descriptor
320 * @ret rc Return status code
322 int close ( int fd ) {
323 struct posix_file *file;
326 file = posix_fd_to_file ( fd );
330 /* Terminate data transfer */
331 posix_file_finished ( file, 0 );
333 /* Remove from list of open files and drop reference */
334 list_del ( &file->list );
335 ref_put ( &file->refcnt );