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
);
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
);
82 * Terminate file data transfer
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
);
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
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
)
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
);
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
)
164 * Find an available file descriptor
166 * @ret fd File descriptor, or negative error number
168 static int posix_find_free_fd ( void ) {
171 for ( fd
= POSIX_FD_MIN
; fd
<= POSIX_FD_MAX
; fd
++ ) {
172 if ( ! posix_fd_to_file ( fd
) )
175 DBG ( "POSIX could not find free file descriptor\n" );
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
;
190 /* Find a free file descriptor to use */
191 fd
= posix_find_free_fd();
195 /* Allocate and initialise structure */
196 file
= zalloc ( sizeof ( *file
) );
199 file
->refcnt
.free
= posix_file_free
;
201 file
->rc
= -EINPROGRESS
;
202 xfer_init ( &file
->xfer
, &posix_file_xfer_operations
,
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 )
210 /* Wait for open to succeed or fail */
211 while ( list_empty ( &file
->data
) ) {
215 if ( file
->rc
!= -EINPROGRESS
) {
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
);
227 posix_file_finished ( file
, rc
);
228 ref_put ( &file
->refcnt
);
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
;
244 for ( fd
= POSIX_FD_MIN
; fd
<= POSIX_FD_MAX
; fd
++ ) {
245 if ( ! FD_ISSET ( fd
, readfds
) )
247 file
= posix_fd_to_file ( fd
);
250 if ( ( list_empty ( &file
->data
) ) &&
251 ( file
->rc
== -EINPROGRESS
) )
253 /* Data is available or status has changed */
255 FD_SET ( fd
, readfds
);
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
;
281 file
= posix_fd_to_file ( fd
);
285 /* Try to fetch more data if none available */
286 if ( list_empty ( &file
->data
) )
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
);
294 copy_to_user ( buffer
, offset
, iobuf
->data
, len
);
295 iob_pull ( iobuf
, len
);
296 if ( ! iob_len ( iobuf
) ) {
297 list_del ( &iobuf
->list
);
305 /* If file has completed, return (after returning all data) */
306 if ( file
->rc
!= -EINPROGRESS
) {
307 assert ( list_empty ( &file
->data
) );
311 /* No data ready and file still in progress; return -WOULDBLOCK */
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
;
325 file
= posix_fd_to_file ( fd
);
329 return file
->filesize
;
335 * @v fd File descriptor
336 * @ret rc Return status code
338 int close ( int fd
) {
339 struct posix_file
*file
;
342 file
= posix_fd_to_file ( fd
);
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
);