iPXE
posix_io.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <ipxe/list.h>
30#include <ipxe/iobuf.h>
31#include <ipxe/xfer.h>
32#include <ipxe/open.h>
33#include <ipxe/process.h>
34#include <ipxe/posix_io.h>
35
36/** @file
37 *
38 * POSIX-like I/O
39 *
40 * These functions provide traditional blocking I/O semantics. They
41 * are designed to be used by the PXE TFTP API. Because they block,
42 * they may not be used by most other portions of the iPXE codebase.
43 */
44
45/** An open file */
46struct posix_file {
47 /** Reference count for this object */
48 struct refcnt refcnt;
49 /** List of open files */
51 /** File descriptor */
52 int fd;
53 /** Overall status
54 *
55 * Set to -EINPROGRESS while data transfer is in progress.
56 */
57 int rc;
58 /** Data transfer interface */
60 /** Current seek position */
61 size_t pos;
62 /** File size */
63 size_t filesize;
64 /** Received data queue */
66};
67
68/** List of open files */
69static LIST_HEAD ( posix_files );
70
71/**
72 * Free open file
73 *
74 * @v refcnt Reference counter
75 */
76static void posix_file_free ( struct refcnt *refcnt ) {
77 struct posix_file *file =
79 struct io_buffer *iobuf;
80 struct io_buffer *tmp;
81
82 list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
83 list_del ( &iobuf->list );
84 free_iob ( iobuf );
85 }
86 free ( file );
87}
88
89/**
90 * Terminate file data transfer
91 *
92 * @v file POSIX file
93 * @v rc Reason for termination
94 */
95static void posix_file_finished ( struct posix_file *file, int rc ) {
96 intf_shutdown ( &file->xfer, rc );
97 file->rc = rc;
98}
99
100/**
101 * Handle deliver_iob() event
102 *
103 * @v file POSIX file
104 * @v iobuf I/O buffer
105 * @v meta Data transfer metadata
106 * @ret rc Return status code
107 */
108static int posix_file_xfer_deliver ( struct posix_file *file,
109 struct io_buffer *iobuf,
110 struct xfer_metadata *meta ) {
111
112 /* Keep track of file position solely for the filesize */
113 if ( meta->flags & XFER_FL_ABS_OFFSET )
114 file->pos = 0;
115 file->pos += meta->offset;
116 if ( file->filesize < file->pos )
117 file->filesize = file->pos;
118
119 if ( iob_len ( iobuf ) ) {
120 list_add_tail ( &iobuf->list, &file->data );
121 } else {
122 free_iob ( iobuf );
123 }
124
125 return 0;
126}
127
128/** POSIX file data transfer interface operations */
133
134/** POSIX file data transfer interface descriptor */
137
138/**
139 * Identify file by file descriptor
140 *
141 * @v fd File descriptor
142 * @ret file Corresponding file, or NULL
143 */
144static struct posix_file * posix_fd_to_file ( int fd ) {
145 struct posix_file *file;
146
147 list_for_each_entry ( file, &posix_files, list ) {
148 if ( file->fd == fd )
149 return file;
150 }
151 return NULL;
152}
153
154/**
155 * Find an available file descriptor
156 *
157 * @ret fd File descriptor, or negative error number
158 */
159static int posix_find_free_fd ( void ) {
160 int fd;
161
162 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
163 if ( ! posix_fd_to_file ( fd ) )
164 return fd;
165 }
166 DBG ( "POSIX could not find free file descriptor\n" );
167 return -ENFILE;
168}
169
170/**
171 * Open file
172 *
173 * @v uri_string URI string
174 * @ret fd File descriptor, or negative error number
175 */
176int open ( const char *uri_string ) {
177 struct posix_file *file;
178 int fd;
179 int rc;
180
181 /* Find a free file descriptor to use */
183 if ( fd < 0 )
184 return fd;
185
186 /* Allocate and initialise structure */
187 file = zalloc ( sizeof ( *file ) );
188 if ( ! file )
189 return -ENOMEM;
190 ref_init ( &file->refcnt, posix_file_free );
191 file->fd = fd;
192 file->rc = -EINPROGRESS;
193 intf_init ( &file->xfer, &posix_file_xfer_desc, &file->refcnt );
194 INIT_LIST_HEAD ( &file->data );
195
196 /* Open URI on data transfer interface */
197 if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
198 goto err;
199
200 /* Wait for open to succeed or fail */
201 while ( list_empty ( &file->data ) ) {
202 step();
203 if ( file->rc == 0 )
204 break;
205 if ( file->rc != -EINPROGRESS ) {
206 rc = file->rc;
207 goto err;
208 }
209 }
210
211 /* Add to list of open files. List takes reference ownership. */
212 list_add ( &file->list, &posix_files );
213 DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
214 return fd;
215
216 err:
217 posix_file_finished ( file, rc );
218 ref_put ( &file->refcnt );
219 return rc;
220}
221
222/**
223 * Check file descriptors for readiness
224 *
225 * @v readfds File descriptors to check
226 * @v wait Wait until data is ready
227 * @ret nready Number of ready file descriptors
228 */
229int select ( fd_set *readfds, int wait ) {
230 struct posix_file *file;
231 int fd;
232
233 do {
234 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
235 if ( ! FD_ISSET ( fd, readfds ) )
236 continue;
237 file = posix_fd_to_file ( fd );
238 if ( ! file )
239 return -EBADF;
240 if ( ( list_empty ( &file->data ) ) &&
241 ( file->rc == -EINPROGRESS ) )
242 continue;
243 /* Data is available or status has changed */
244 FD_ZERO ( readfds );
245 FD_SET ( fd, readfds );
246 return 1;
247 }
248 step();
249 } while ( wait );
250
251 return 0;
252}
253
254/**
255 * Read data from file
256 *
257 * @v buf Data buffer
258 * @v max_len Maximum length to read
259 * @ret len Actual length read, or negative error number
260 *
261 * This call is non-blocking; if no data is available to read then
262 * -EWOULDBLOCK will be returned.
263 */
264ssize_t read ( int fd, void *buf, size_t max_len ) {
265 struct posix_file *file;
266 struct io_buffer *iobuf;
267 size_t len;
268
269 /* Identify file */
270 file = posix_fd_to_file ( fd );
271 if ( ! file )
272 return -EBADF;
273
274 /* Try to fetch more data if none available */
275 if ( list_empty ( &file->data ) )
276 step();
277
278 /* Dequeue at most one received I/O buffer into user buffer */
279 list_for_each_entry ( iobuf, &file->data, list ) {
280 len = iob_len ( iobuf );
281 if ( len > max_len )
282 len = max_len;
283 memcpy ( buf, iobuf->data, len );
284 iob_pull ( iobuf, len );
285 if ( ! iob_len ( iobuf ) ) {
286 list_del ( &iobuf->list );
287 free_iob ( iobuf );
288 }
289 file->pos += len;
290 assert ( len != 0 );
291 return len;
292 }
293
294 /* If file has completed, return (after returning all data) */
295 if ( file->rc != -EINPROGRESS ) {
296 assert ( list_empty ( &file->data ) );
297 return file->rc;
298 }
299
300 /* No data ready and file still in progress; return -WOULDBLOCK */
301 return -EWOULDBLOCK;
302}
303
304/**
305 * Determine file size
306 *
307 * @v fd File descriptor
308 * @ret size File size, or negative error number
309 */
310ssize_t fsize ( int fd ) {
311 struct posix_file *file;
312
313 /* Identify file */
314 file = posix_fd_to_file ( fd );
315 if ( ! file )
316 return -EBADF;
317
318 return file->filesize;
319}
320
321/**
322 * Close file
323 *
324 * @v fd File descriptor
325 * @ret rc Return status code
326 */
327int close ( int fd ) {
328 struct posix_file *file;
329
330 /* Identify file */
331 file = posix_fd_to_file ( fd );
332 if ( ! file )
333 return -EBADF;
334
335 /* Terminate data transfer */
336 posix_file_finished ( file, 0 );
337
338 /* Remove from list of open files and drop reference */
339 list_del ( &file->list );
340 ref_put ( &file->refcnt );
341 return 0;
342}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
signed long ssize_t
Definition stdint.h:7
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
ring len
Length.
Definition dwmac.h:226
uint8_t meta
Metadata flags.
Definition ena.h:3
Error codes.
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EWOULDBLOCK
Operation would block.
Definition errno.h:680
#define ENFILE
Too many open files in system.
Definition errno.h:494
#define ENOMEM
Not enough space.
Definition errno.h:535
#define EINPROGRESS
Operation in progress.
Definition errno.h:419
#define EBADF
Bad file descriptor.
Definition errno.h:329
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition interface.c:250
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition interface.c:279
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition interface.h:81
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition interface.h:204
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition interface.h:33
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition iobuf.c:153
I/O buffers.
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:160
#define iob_pull(iobuf, len)
Definition iobuf.h:107
unsigned long tmp
Definition linux_pci.h:65
Linked lists.
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition list.h:459
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
#define list_del(list)
Delete an entry from a list.
Definition list.h:120
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition list.h:46
#define list_empty(list)
Test whether a list is empty.
Definition list.h:137
#define LIST_HEAD(list)
Declare a static list head.
Definition list.h:38
#define list_add(new, head)
Add a new entry to the head of a list.
Definition list.h:70
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
struct option_descriptor read[1]
Definition nvo_cmd.c:116
int xfer_open_uri_string(struct interface *intf, const char *uri_string)
Open URI string.
Definition open.c:116
Data transfer interface opening.
static struct posix_file * posix_fd_to_file(int fd)
Identify file by file descriptor.
Definition posix_io.c:144
int select(fd_set *readfds, int wait)
Check file descriptors for readiness.
Definition posix_io.c:229
ssize_t fsize(int fd)
Determine file size.
Definition posix_io.c:310
static int posix_find_free_fd(void)
Find an available file descriptor.
Definition posix_io.c:159
int open(const char *uri_string)
Open file.
Definition posix_io.c:176
static struct interface_operation posix_file_xfer_operations[]
POSIX file data transfer interface operations.
Definition posix_io.c:129
static int posix_file_xfer_deliver(struct posix_file *file, struct io_buffer *iobuf, struct xfer_metadata *meta)
Handle deliver_iob() event.
Definition posix_io.c:108
static struct interface_descriptor posix_file_xfer_desc
POSIX file data transfer interface descriptor.
Definition posix_io.c:135
static void posix_file_free(struct refcnt *refcnt)
Free open file.
Definition posix_io.c:76
static void posix_file_finished(struct posix_file *file, int rc)
Terminate file data transfer.
Definition posix_io.c:95
POSIX-like I/O.
uint32_t fd_set
File descriptor set as used for select()
Definition posix_io.h:21
#define POSIX_FD_MAX
Maximum file descriptor that will ever be allocated.
Definition posix_io.h:18
#define POSIX_FD_MIN
Minimum file descriptor that will ever be allocated.
Definition posix_io.h:15
void step(void)
Single-step a single process.
Definition process.c:99
Processes.
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
An object interface descriptor.
Definition interface.h:56
An object interface operation.
Definition interface.h:18
An object interface.
Definition interface.h:125
A persistent I/O buffer.
Definition iobuf.h:38
void * data
Start of data.
Definition iobuf.h:53
struct list_head list
List of which this buffer is a member.
Definition iobuf.h:45
A doubly-linked list entry (or list head)
Definition list.h:19
An open file.
Definition posix_io.c:46
struct interface xfer
Data transfer interface.
Definition posix_io.c:59
size_t filesize
File size.
Definition posix_io.c:63
size_t pos
Current seek position.
Definition posix_io.c:61
struct refcnt refcnt
Reference count for this object.
Definition posix_io.c:48
int fd
File descriptor.
Definition posix_io.c:52
struct list_head data
Received data queue.
Definition posix_io.c:65
struct list_head list
List of open files.
Definition posix_io.c:50
int rc
Overall status.
Definition posix_io.c:57
A reference counter.
Definition refcnt.h:27
Data transfer metadata.
Definition xfer.h:23
static struct evtchn_close * close
Definition xenevent.h:24
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition xfer.c:195
Data transfer interfaces.
#define XFER_FL_ABS_OFFSET
Offset is absolute.
Definition xfer.h:48