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 FILE_LICENCE ( GPL2_OR_LATER
);
24 #include <gpxe/list.h>
25 #include <gpxe/xfer.h>
26 #include <gpxe/open.h>
27 #include <gpxe/process.h>
28 #include <gpxe/posix_io.h>
34 * These functions provide traditional blocking I/O semantics. They
35 * are designed to be used by the PXE TFTP API. Because they block,
36 * they may not be used by most other portions of the gPXE codebase.
41 /** Reference count for this object */
43 /** List of open files */
44 struct list_head list
;
45 /** File descriptor */
49 * Set to -EINPROGRESS while data transfer is in progress.
52 /** Data transfer interface */
53 struct xfer_interface xfer
;
54 /** Current seek position */
58 /** Received data queue */
59 struct list_head data
;
62 /** List of open files */
63 static LIST_HEAD ( posix_files
);
68 * @v refcnt Reference counter
70 static void posix_file_free ( struct refcnt
*refcnt
) {
71 struct posix_file
*file
=
72 container_of ( refcnt
, struct posix_file
, refcnt
);
73 struct io_buffer
*iobuf
;
74 struct io_buffer
*tmp
;
76 list_for_each_entry_safe ( iobuf
, tmp
, &file
->data
, list
) {
77 list_del ( &iobuf
->list
);
84 * Terminate file data transfer
87 * @v rc Reason for termination
89 static void posix_file_finished ( struct posix_file
*file
, int rc
) {
90 xfer_nullify ( &file
->xfer
);
91 xfer_close ( &file
->xfer
, rc
);
96 * Handle close() event
98 * @v xfer POSIX file data transfer interface
99 * @v rc Reason for close
101 static void posix_file_xfer_close ( struct xfer_interface
*xfer
, int rc
) {
102 struct posix_file
*file
=
103 container_of ( xfer
, struct posix_file
, xfer
);
105 posix_file_finished ( file
, rc
);
109 * Handle deliver_iob() event
111 * @v xfer POSIX file data transfer interface
112 * @v iobuf I/O buffer
113 * @v meta Data transfer metadata
114 * @ret rc Return status code
117 posix_file_xfer_deliver_iob ( struct xfer_interface
*xfer
,
118 struct io_buffer
*iobuf
,
119 struct xfer_metadata
*meta
) {
120 struct posix_file
*file
=
121 container_of ( xfer
, struct posix_file
, xfer
);
123 /* Keep track of file position solely for the filesize */
124 if ( meta
->whence
!= SEEK_CUR
)
126 file
->pos
+= meta
->offset
;
127 if ( file
->filesize
< file
->pos
)
128 file
->filesize
= file
->pos
;
130 if ( iob_len ( iobuf
) ) {
131 list_add_tail ( &iobuf
->list
, &file
->data
);
139 /** POSIX file data transfer interface operations */
140 static struct xfer_interface_operations posix_file_xfer_operations
= {
141 .close
= posix_file_xfer_close
,
142 .vredirect
= xfer_vreopen
,
143 .window
= unlimited_xfer_window
,
144 .alloc_iob
= default_xfer_alloc_iob
,
145 .deliver_iob
= posix_file_xfer_deliver_iob
,
146 .deliver_raw
= xfer_deliver_as_iob
,
150 * Identify file by file descriptor
152 * @v fd File descriptor
153 * @ret file Corresponding file, or NULL
155 static struct posix_file
* posix_fd_to_file ( int fd
) {
156 struct posix_file
*file
;
158 list_for_each_entry ( file
, &posix_files
, list
) {
159 if ( file
->fd
== fd
)
166 * Find an available file descriptor
168 * @ret fd File descriptor, or negative error number
170 static int posix_find_free_fd ( void ) {
173 for ( fd
= POSIX_FD_MIN
; fd
<= POSIX_FD_MAX
; fd
++ ) {
174 if ( ! posix_fd_to_file ( fd
) )
177 DBG ( "POSIX could not find free file descriptor\n" );
184 * @v uri_string URI string
185 * @ret fd File descriptor, or negative error number
187 int open ( const char *uri_string
) {
188 struct posix_file
*file
;
192 /* Find a free file descriptor to use */
193 fd
= posix_find_free_fd();
197 /* Allocate and initialise structure */
198 file
= zalloc ( sizeof ( *file
) );
201 ref_init ( &file
->refcnt
, posix_file_free
);
203 file
->rc
= -EINPROGRESS
;
204 xfer_init ( &file
->xfer
, &posix_file_xfer_operations
,
206 INIT_LIST_HEAD ( &file
->data
);
208 /* Open URI on data transfer interface */
209 if ( ( rc
= xfer_open_uri_string ( &file
->xfer
, uri_string
) ) != 0 )
212 /* Wait for open to succeed or fail */
213 while ( list_empty ( &file
->data
) ) {
217 if ( file
->rc
!= -EINPROGRESS
) {
223 /* Add to list of open files. List takes reference ownership. */
224 list_add ( &file
->list
, &posix_files
);
225 DBG ( "POSIX opened %s as file %d\n", uri_string
, fd
);
229 posix_file_finished ( file
, rc
);
230 ref_put ( &file
->refcnt
);
235 * Check file descriptors for readiness
237 * @v readfds File descriptors to check
238 * @v wait Wait until data is ready
239 * @ret nready Number of ready file descriptors
241 int select ( fd_set
*readfds
, int wait
) {
242 struct posix_file
*file
;
246 for ( fd
= POSIX_FD_MIN
; fd
<= POSIX_FD_MAX
; fd
++ ) {
247 if ( ! FD_ISSET ( fd
, readfds
) )
249 file
= posix_fd_to_file ( fd
);
252 if ( ( list_empty ( &file
->data
) ) &&
253 ( file
->rc
== -EINPROGRESS
) )
255 /* Data is available or status has changed */
257 FD_SET ( fd
, readfds
);
267 * Read data from file
269 * @v buffer Data buffer
270 * @v offset Starting offset within data buffer
271 * @v len Maximum length to read
272 * @ret len Actual length read, or negative error number
274 * This call is non-blocking; if no data is available to read then
275 * -EWOULDBLOCK will be returned.
277 ssize_t
read_user ( int fd
, userptr_t buffer
, off_t offset
, size_t max_len
) {
278 struct posix_file
*file
;
279 struct io_buffer
*iobuf
;
283 file
= posix_fd_to_file ( fd
);
287 /* Try to fetch more data if none available */
288 if ( list_empty ( &file
->data
) )
291 /* Dequeue at most one received I/O buffer into user buffer */
292 list_for_each_entry ( iobuf
, &file
->data
, list
) {
293 len
= iob_len ( iobuf
);
296 copy_to_user ( buffer
, offset
, iobuf
->data
, len
);
297 iob_pull ( iobuf
, len
);
298 if ( ! iob_len ( iobuf
) ) {
299 list_del ( &iobuf
->list
);
307 /* If file has completed, return (after returning all data) */
308 if ( file
->rc
!= -EINPROGRESS
) {
309 assert ( list_empty ( &file
->data
) );
313 /* No data ready and file still in progress; return -WOULDBLOCK */
318 * Determine file size
320 * @v fd File descriptor
321 * @ret size File size, or negative error number
323 ssize_t
fsize ( int fd
) {
324 struct posix_file
*file
;
327 file
= posix_fd_to_file ( fd
);
331 return file
->filesize
;
337 * @v fd File descriptor
338 * @ret rc Return status code
340 int close ( int fd
) {
341 struct posix_file
*file
;
344 file
= posix_fd_to_file ( fd
);
348 /* Terminate data transfer */
349 posix_file_finished ( file
, 0 );
351 /* Remove from list of open files and drop reference */
352 list_del ( &file
->list
);
353 ref_put ( &file
->refcnt
);