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 ) {
81 list_del ( &iobuf->list );
88 * Terminate file data transfer
91 * @v rc Reason for termination
93 static void posix_file_finished ( struct posix_file *file, int rc ) {
94 xfer_nullify ( &file->xfer );
95 xfer_close ( &file->xfer, rc );
100 * Handle close() event
102 * @v xfer POSIX file data transfer interface
103 * @v rc Reason for close
105 static void posix_file_xfer_close ( struct xfer_interface *xfer, int rc ) {
106 struct posix_file *file =
107 container_of ( xfer, struct posix_file, xfer );
109 posix_file_finished ( file, rc );
113 * Handle seek() event
115 * @v xfer POSIX file data transfer interface
116 * @v pos New position
117 * @ret rc Return status code
119 static int posix_file_xfer_seek ( struct xfer_interface *xfer, off_t offset,
121 struct posix_file *file =
122 container_of ( xfer, struct posix_file, xfer );
133 if ( file->filesize < file->pos )
134 file->filesize = file->pos;
140 * Handle deliver_iob() event
142 * @v xfer POSIX file data transfer interface
143 * @v iobuf I/O buffer
144 * @v meta Data transfer metadata, or NULL
145 * @ret rc Return status code
148 posix_file_xfer_deliver_iob ( struct xfer_interface *xfer,
149 struct io_buffer *iobuf,
150 struct xfer_metadata *meta __unused ) {
151 struct posix_file *file =
152 container_of ( xfer, struct posix_file, xfer );
154 list_add_tail ( &iobuf->list, &file->data );
158 /** POSIX file data transfer interface operations */
159 static struct xfer_interface_operations posix_file_xfer_operations = {
160 .close = posix_file_xfer_close,
161 .vredirect = xfer_vopen,
162 .request = ignore_xfer_request,
163 .seek = posix_file_xfer_seek,
164 .alloc_iob = default_xfer_alloc_iob,
165 .deliver_iob = posix_file_xfer_deliver_iob,
166 .deliver_raw = xfer_deliver_as_iob,
170 * Identify file by file descriptor
172 * @v fd File descriptor
173 * @ret file Corresponding file, or NULL
175 static struct posix_file * posix_fd_to_file ( int fd ) {
176 struct posix_file *file;
178 list_for_each_entry ( file, &posix_files, list ) {
179 if ( file->fd == fd )
186 * Find an available file descriptor
188 * @ret fd File descriptor, or negative error number
190 static int posix_find_free_fd ( void ) {
193 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
194 if ( ! posix_fd_to_file ( fd ) )
197 DBG ( "POSIX could not find free file descriptor\n" );
204 * @v uri_string URI string
205 * @ret fd File descriptor, or negative error number
207 int open ( const char *uri_string ) {
208 struct posix_file *file;
212 /* Find a free file descriptor to use */
213 fd = posix_find_free_fd();
217 /* Allocate and initialise structure */
218 file = malloc ( sizeof ( *file ) );
221 memset ( file, 0, sizeof ( *file ) );
222 file->refcnt.free = posix_file_free;
224 file->rc = -EINPROGRESS;
225 xfer_init ( &file->xfer, &posix_file_xfer_operations,
227 INIT_LIST_HEAD ( &file->data );
229 /* Open URI on data transfer interface */
230 if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
233 /* Wait for open to succeed or fail */
234 while ( list_empty ( &file->data ) ) {
238 if ( file->rc != -EINPROGRESS ) {
244 /* Add to list of open files. List takes reference ownership. */
245 list_add ( &file->list, &posix_files );
246 DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
250 posix_file_finished ( file, rc );
251 ref_put ( &file->refcnt );
256 * Read data from file
258 * @v buffer Data buffer
259 * @v offset Starting offset within data buffer
260 * @v len Maximum length to read
261 * @ret len Actual length read, or negative error number
263 ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
264 struct posix_file *file;
265 struct io_buffer *iobuf;
270 file = posix_fd_to_file ( fd );
275 /* Try to fetch more data if none available */
276 if ( list_empty ( &file->data ) )
278 /* Dequeue at most one received I/O buffer into user buffer */
279 list_for_each_entry ( iobuf, &file->data, list ) {
280 frag_len = iob_len ( iobuf );
281 if ( frag_len > max_len )
283 copy_to_user ( buffer, offset, iobuf->data,
285 iob_pull ( iobuf, frag_len );
286 if ( ! iob_len ( iobuf ) ) {
287 list_del ( &iobuf-> list );
290 file->pos += frag_len;
296 /* If buffer is full, return */
299 /* If file has completed, return */
300 if ( file->rc != -EINPROGRESS )
301 return ( file->rc ? file->rc : len );
306 * Determine file size
308 * @v fd File descriptor
309 * @ret size File size, or negative error number
311 ssize_t fsize ( int fd ) {
312 struct posix_file *file;
315 file = posix_fd_to_file ( fd );
319 return file->filesize;
325 * @v fd File descriptor
326 * @ret rc Return status code
328 int close ( int fd ) {
329 struct posix_file *file;
332 file = posix_fd_to_file ( fd );
336 /* Terminate data transfer */
337 posix_file_finished ( file, 0 );
339 /* Remove from list of open files and drop reference */
340 list_del ( &file->list );
341 ref_put ( &file->refcnt );