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

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