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 7.8KB

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