[sundance] Add reset completion check
[gpxe.git] / src / core / posix_io.c
blobe0459bdf199edaf0ae83e3c75fbac52109c685dc
1 /*
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.
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
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>
28 /** @file
30 * POSIX-like I/O
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.
37 /** An open file */
38 struct posix_file {
39 /** Reference count for this object */
40 struct refcnt refcnt;
41 /** List of open files */
42 struct list_head list;
43 /** File descriptor */
44 int fd;
45 /** Overall status
47 * Set to -EINPROGRESS while data transfer is in progress.
49 int rc;
50 /** Data transfer interface */
51 struct xfer_interface xfer;
52 /** Current seek position */
53 size_t pos;
54 /** File size */
55 size_t filesize;
56 /** Received data queue */
57 struct list_head data;
60 /** List of open files */
61 static LIST_HEAD ( posix_files );
63 /**
64 * Free open file
66 * @v refcnt Reference counter
68 static void posix_file_free ( struct refcnt *refcnt ) {
69 struct posix_file *file =
70 container_of ( refcnt, struct posix_file, refcnt );
71 struct io_buffer *iobuf;
72 struct io_buffer *tmp;
74 list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
75 list_del ( &iobuf->list );
76 free_iob ( iobuf );
78 free ( file );
81 /**
82 * Terminate file data transfer
84 * @v file POSIX file
85 * @v rc Reason for termination
87 static void posix_file_finished ( struct posix_file *file, int rc ) {
88 xfer_nullify ( &file->xfer );
89 xfer_close ( &file->xfer, rc );
90 file->rc = rc;
93 /**
94 * Handle close() event
96 * @v xfer POSIX file data transfer interface
97 * @v rc Reason for close
99 static void posix_file_xfer_close ( struct xfer_interface *xfer, int rc ) {
100 struct posix_file *file =
101 container_of ( xfer, struct posix_file, xfer );
103 posix_file_finished ( file, rc );
107 * Handle deliver_iob() event
109 * @v xfer POSIX file data transfer interface
110 * @v iobuf I/O buffer
111 * @v meta Data transfer metadata
112 * @ret rc Return status code
114 static int
115 posix_file_xfer_deliver_iob ( struct xfer_interface *xfer,
116 struct io_buffer *iobuf,
117 struct xfer_metadata *meta ) {
118 struct posix_file *file =
119 container_of ( xfer, struct posix_file, xfer );
121 /* Keep track of file position solely for the filesize */
122 if ( meta->whence != SEEK_CUR )
123 file->pos = 0;
124 file->pos += meta->offset;
125 if ( file->filesize < file->pos )
126 file->filesize = file->pos;
128 if ( iob_len ( iobuf ) ) {
129 list_add_tail ( &iobuf->list, &file->data );
130 } else {
131 free_iob ( iobuf );
134 return 0;
137 /** POSIX file data transfer interface operations */
138 static struct xfer_interface_operations posix_file_xfer_operations = {
139 .close = posix_file_xfer_close,
140 .vredirect = xfer_vopen,
141 .window = unlimited_xfer_window,
142 .alloc_iob = default_xfer_alloc_iob,
143 .deliver_iob = posix_file_xfer_deliver_iob,
144 .deliver_raw = xfer_deliver_as_iob,
148 * Identify file by file descriptor
150 * @v fd File descriptor
151 * @ret file Corresponding file, or NULL
153 static struct posix_file * posix_fd_to_file ( int fd ) {
154 struct posix_file *file;
156 list_for_each_entry ( file, &posix_files, list ) {
157 if ( file->fd == fd )
158 return file;
160 return NULL;
164 * Find an available file descriptor
166 * @ret fd File descriptor, or negative error number
168 static int posix_find_free_fd ( void ) {
169 int fd;
171 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
172 if ( ! posix_fd_to_file ( fd ) )
173 return fd;
175 DBG ( "POSIX could not find free file descriptor\n" );
176 return -ENFILE;
180 * Open file
182 * @v uri_string URI string
183 * @ret fd File descriptor, or negative error number
185 int open ( const char *uri_string ) {
186 struct posix_file *file;
187 int fd;
188 int rc;
190 /* Find a free file descriptor to use */
191 fd = posix_find_free_fd();
192 if ( fd < 0 )
193 return fd;
195 /* Allocate and initialise structure */
196 file = zalloc ( sizeof ( *file ) );
197 if ( ! file )
198 return -ENOMEM;
199 file->refcnt.free = posix_file_free;
200 file->fd = fd;
201 file->rc = -EINPROGRESS;
202 xfer_init ( &file->xfer, &posix_file_xfer_operations,
203 &file->refcnt );
204 INIT_LIST_HEAD ( &file->data );
206 /* Open URI on data transfer interface */
207 if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
208 goto err;
210 /* Wait for open to succeed or fail */
211 while ( list_empty ( &file->data ) ) {
212 step();
213 if ( file->rc == 0 )
214 break;
215 if ( file->rc != -EINPROGRESS ) {
216 rc = file->rc;
217 goto err;
221 /* Add to list of open files. List takes reference ownership. */
222 list_add ( &file->list, &posix_files );
223 DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
224 return fd;
226 err:
227 posix_file_finished ( file, rc );
228 ref_put ( &file->refcnt );
229 return rc;
233 * Check file descriptors for readiness
235 * @v readfds File descriptors to check
236 * @v wait Wait until data is ready
237 * @ret nready Number of ready file descriptors
239 int select ( fd_set *readfds, int wait ) {
240 struct posix_file *file;
241 int fd;
243 do {
244 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
245 if ( ! FD_ISSET ( fd, readfds ) )
246 continue;
247 file = posix_fd_to_file ( fd );
248 if ( ! file )
249 return -EBADF;
250 if ( ( list_empty ( &file->data ) ) &&
251 ( file->rc == -EINPROGRESS ) )
252 continue;
253 /* Data is available or status has changed */
254 FD_ZERO ( readfds );
255 FD_SET ( fd, readfds );
256 return 1;
258 step();
259 } while ( wait );
261 return 0;
265 * Read data from file
267 * @v buffer Data buffer
268 * @v offset Starting offset within data buffer
269 * @v len Maximum length to read
270 * @ret len Actual length read, or negative error number
272 * This call is non-blocking; if no data is available to read then
273 * -EWOULDBLOCK will be returned.
275 ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
276 struct posix_file *file;
277 struct io_buffer *iobuf;
278 size_t len;
280 /* Identify file */
281 file = posix_fd_to_file ( fd );
282 if ( ! file )
283 return -EBADF;
285 /* Try to fetch more data if none available */
286 if ( list_empty ( &file->data ) )
287 step();
289 /* Dequeue at most one received I/O buffer into user buffer */
290 list_for_each_entry ( iobuf, &file->data, list ) {
291 len = iob_len ( iobuf );
292 if ( len > max_len )
293 len = max_len;
294 copy_to_user ( buffer, offset, iobuf->data, len );
295 iob_pull ( iobuf, len );
296 if ( ! iob_len ( iobuf ) ) {
297 list_del ( &iobuf->list );
298 free_iob ( iobuf );
300 file->pos += len;
301 assert ( len != 0 );
302 return len;
305 /* If file has completed, return (after returning all data) */
306 if ( file->rc != -EINPROGRESS ) {
307 assert ( list_empty ( &file->data ) );
308 return file->rc;
311 /* No data ready and file still in progress; return -WOULDBLOCK */
312 return -EWOULDBLOCK;
316 * Determine file size
318 * @v fd File descriptor
319 * @ret size File size, or negative error number
321 ssize_t fsize ( int fd ) {
322 struct posix_file *file;
324 /* Identify file */
325 file = posix_fd_to_file ( fd );
326 if ( ! file )
327 return -EBADF;
329 return file->filesize;
333 * Close file
335 * @v fd File descriptor
336 * @ret rc Return status code
338 int close ( int fd ) {
339 struct posix_file *file;
341 /* Identify file */
342 file = posix_fd_to_file ( fd );
343 if ( ! file )
344 return -EBADF;
346 /* Terminate data transfer */
347 posix_file_finished ( file, 0 );
349 /* Remove from list of open files and drop reference */
350 list_del ( &file->list );
351 ref_put ( &file->refcnt );
352 return 0;