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.1KB

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