You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

posix_io.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <ipxe/list.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/xfer.h>
  30. #include <ipxe/open.h>
  31. #include <ipxe/process.h>
  32. #include <ipxe/posix_io.h>
  33. /** @file
  34. *
  35. * POSIX-like I/O
  36. *
  37. * These functions provide traditional blocking I/O semantics. They
  38. * are designed to be used by the PXE TFTP API. Because they block,
  39. * they may not be used by most other portions of the iPXE codebase.
  40. */
  41. /** An open file */
  42. struct posix_file {
  43. /** Reference count for this object */
  44. struct refcnt refcnt;
  45. /** List of open files */
  46. struct list_head list;
  47. /** File descriptor */
  48. int fd;
  49. /** Overall status
  50. *
  51. * Set to -EINPROGRESS while data transfer is in progress.
  52. */
  53. int rc;
  54. /** Data transfer interface */
  55. struct interface xfer;
  56. /** Current seek position */
  57. size_t pos;
  58. /** File size */
  59. size_t filesize;
  60. /** Received data queue */
  61. struct list_head data;
  62. };
  63. /** List of open files */
  64. static LIST_HEAD ( posix_files );
  65. /**
  66. * Free open file
  67. *
  68. * @v refcnt Reference counter
  69. */
  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;
  75. list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
  76. list_del ( &iobuf->list );
  77. free_iob ( iobuf );
  78. }
  79. free ( file );
  80. }
  81. /**
  82. * Terminate file data transfer
  83. *
  84. * @v file POSIX file
  85. * @v rc Reason for termination
  86. */
  87. static void posix_file_finished ( struct posix_file *file, int rc ) {
  88. intf_shutdown ( &file->xfer, rc );
  89. file->rc = rc;
  90. }
  91. /**
  92. * Handle deliver_iob() event
  93. *
  94. * @v file POSIX file
  95. * @v iobuf I/O buffer
  96. * @v meta Data transfer metadata
  97. * @ret rc Return status code
  98. */
  99. static int posix_file_xfer_deliver ( struct posix_file *file,
  100. struct io_buffer *iobuf,
  101. struct xfer_metadata *meta ) {
  102. /* Keep track of file position solely for the filesize */
  103. if ( meta->flags & XFER_FL_ABS_OFFSET )
  104. file->pos = 0;
  105. file->pos += meta->offset;
  106. if ( file->filesize < file->pos )
  107. file->filesize = file->pos;
  108. if ( iob_len ( iobuf ) ) {
  109. list_add_tail ( &iobuf->list, &file->data );
  110. } else {
  111. free_iob ( iobuf );
  112. }
  113. return 0;
  114. }
  115. /** POSIX file data transfer interface operations */
  116. static struct interface_operation posix_file_xfer_operations[] = {
  117. INTF_OP ( xfer_deliver, struct posix_file *, posix_file_xfer_deliver ),
  118. INTF_OP ( intf_close, struct posix_file *, posix_file_finished ),
  119. };
  120. /** POSIX file data transfer interface descriptor */
  121. static struct interface_descriptor posix_file_xfer_desc =
  122. INTF_DESC ( struct posix_file, xfer, posix_file_xfer_operations );
  123. /**
  124. * Identify file by file descriptor
  125. *
  126. * @v fd File descriptor
  127. * @ret file Corresponding file, or NULL
  128. */
  129. static struct posix_file * posix_fd_to_file ( int fd ) {
  130. struct posix_file *file;
  131. list_for_each_entry ( file, &posix_files, list ) {
  132. if ( file->fd == fd )
  133. return file;
  134. }
  135. return NULL;
  136. }
  137. /**
  138. * Find an available file descriptor
  139. *
  140. * @ret fd File descriptor, or negative error number
  141. */
  142. static int posix_find_free_fd ( void ) {
  143. int fd;
  144. for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
  145. if ( ! posix_fd_to_file ( fd ) )
  146. return fd;
  147. }
  148. DBG ( "POSIX could not find free file descriptor\n" );
  149. return -ENFILE;
  150. }
  151. /**
  152. * Open file
  153. *
  154. * @v uri_string URI string
  155. * @ret fd File descriptor, or negative error number
  156. */
  157. int open ( const char *uri_string ) {
  158. struct posix_file *file;
  159. int fd;
  160. int rc;
  161. /* Find a free file descriptor to use */
  162. fd = posix_find_free_fd();
  163. if ( fd < 0 )
  164. return fd;
  165. /* Allocate and initialise structure */
  166. file = zalloc ( sizeof ( *file ) );
  167. if ( ! file )
  168. return -ENOMEM;
  169. ref_init ( &file->refcnt, posix_file_free );
  170. file->fd = fd;
  171. file->rc = -EINPROGRESS;
  172. intf_init ( &file->xfer, &posix_file_xfer_desc, &file->refcnt );
  173. INIT_LIST_HEAD ( &file->data );
  174. /* Open URI on data transfer interface */
  175. if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
  176. goto err;
  177. /* Wait for open to succeed or fail */
  178. while ( list_empty ( &file->data ) ) {
  179. step();
  180. if ( file->rc == 0 )
  181. break;
  182. if ( file->rc != -EINPROGRESS ) {
  183. rc = file->rc;
  184. goto err;
  185. }
  186. }
  187. /* Add to list of open files. List takes reference ownership. */
  188. list_add ( &file->list, &posix_files );
  189. DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
  190. return fd;
  191. err:
  192. posix_file_finished ( file, rc );
  193. ref_put ( &file->refcnt );
  194. return rc;
  195. }
  196. /**
  197. * Check file descriptors for readiness
  198. *
  199. * @v readfds File descriptors to check
  200. * @v wait Wait until data is ready
  201. * @ret nready Number of ready file descriptors
  202. */
  203. int select ( fd_set *readfds, int wait ) {
  204. struct posix_file *file;
  205. int fd;
  206. do {
  207. for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
  208. if ( ! FD_ISSET ( fd, readfds ) )
  209. continue;
  210. file = posix_fd_to_file ( fd );
  211. if ( ! file )
  212. return -EBADF;
  213. if ( ( list_empty ( &file->data ) ) &&
  214. ( file->rc == -EINPROGRESS ) )
  215. continue;
  216. /* Data is available or status has changed */
  217. FD_ZERO ( readfds );
  218. FD_SET ( fd, readfds );
  219. return 1;
  220. }
  221. step();
  222. } while ( wait );
  223. return 0;
  224. }
  225. /**
  226. * Read data from file
  227. *
  228. * @v buffer Data buffer
  229. * @v offset Starting offset within data buffer
  230. * @v len Maximum length to read
  231. * @ret len Actual length read, or negative error number
  232. *
  233. * This call is non-blocking; if no data is available to read then
  234. * -EWOULDBLOCK will be returned.
  235. */
  236. ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
  237. struct posix_file *file;
  238. struct io_buffer *iobuf;
  239. size_t len;
  240. /* Identify file */
  241. file = posix_fd_to_file ( fd );
  242. if ( ! file )
  243. return -EBADF;
  244. /* Try to fetch more data if none available */
  245. if ( list_empty ( &file->data ) )
  246. step();
  247. /* Dequeue at most one received I/O buffer into user buffer */
  248. list_for_each_entry ( iobuf, &file->data, list ) {
  249. len = iob_len ( iobuf );
  250. if ( len > max_len )
  251. len = max_len;
  252. copy_to_user ( buffer, offset, iobuf->data, len );
  253. iob_pull ( iobuf, len );
  254. if ( ! iob_len ( iobuf ) ) {
  255. list_del ( &iobuf->list );
  256. free_iob ( iobuf );
  257. }
  258. file->pos += len;
  259. assert ( len != 0 );
  260. return len;
  261. }
  262. /* If file has completed, return (after returning all data) */
  263. if ( file->rc != -EINPROGRESS ) {
  264. assert ( list_empty ( &file->data ) );
  265. return file->rc;
  266. }
  267. /* No data ready and file still in progress; return -WOULDBLOCK */
  268. return -EWOULDBLOCK;
  269. }
  270. /**
  271. * Determine file size
  272. *
  273. * @v fd File descriptor
  274. * @ret size File size, or negative error number
  275. */
  276. ssize_t fsize ( int fd ) {
  277. struct posix_file *file;
  278. /* Identify file */
  279. file = posix_fd_to_file ( fd );
  280. if ( ! file )
  281. return -EBADF;
  282. return file->filesize;
  283. }
  284. /**
  285. * Close file
  286. *
  287. * @v fd File descriptor
  288. * @ret rc Return status code
  289. */
  290. int close ( int fd ) {
  291. struct posix_file *file;
  292. /* Identify file */
  293. file = posix_fd_to_file ( fd );
  294. if ( ! file )
  295. return -EBADF;
  296. /* Terminate data transfer */
  297. posix_file_finished ( file, 0 );
  298. /* Remove from list of open files and drop reference */
  299. list_del ( &file->list );
  300. ref_put ( &file->refcnt );
  301. return 0;
  302. }