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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 __unused ) {
  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. list_add_tail ( &iobuf->list, &file->data );
  117. return 0;
  118. }
  119. /** POSIX file data transfer interface operations */
  120. static struct xfer_interface_operations posix_file_xfer_operations = {
  121. .close = posix_file_xfer_close,
  122. .vredirect = xfer_vopen,
  123. .window = unlimited_xfer_window,
  124. .alloc_iob = default_xfer_alloc_iob,
  125. .deliver_iob = posix_file_xfer_deliver_iob,
  126. .deliver_raw = xfer_deliver_as_iob,
  127. };
  128. /**
  129. * Identify file by file descriptor
  130. *
  131. * @v fd File descriptor
  132. * @ret file Corresponding file, or NULL
  133. */
  134. static struct posix_file * posix_fd_to_file ( int fd ) {
  135. struct posix_file *file;
  136. list_for_each_entry ( file, &posix_files, list ) {
  137. if ( file->fd == fd )
  138. return file;
  139. }
  140. return NULL;
  141. }
  142. /**
  143. * Find an available file descriptor
  144. *
  145. * @ret fd File descriptor, or negative error number
  146. */
  147. static int posix_find_free_fd ( void ) {
  148. int fd;
  149. for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
  150. if ( ! posix_fd_to_file ( fd ) )
  151. return fd;
  152. }
  153. DBG ( "POSIX could not find free file descriptor\n" );
  154. return -ENFILE;
  155. }
  156. /**
  157. * Open file
  158. *
  159. * @v uri_string URI string
  160. * @ret fd File descriptor, or negative error number
  161. */
  162. int open ( const char *uri_string ) {
  163. struct posix_file *file;
  164. int fd;
  165. int rc;
  166. /* Find a free file descriptor to use */
  167. fd = posix_find_free_fd();
  168. if ( fd < 0 )
  169. return fd;
  170. /* Allocate and initialise structure */
  171. file = zalloc ( sizeof ( *file ) );
  172. if ( ! file )
  173. return -ENOMEM;
  174. file->refcnt.free = posix_file_free;
  175. file->fd = fd;
  176. file->rc = -EINPROGRESS;
  177. xfer_init ( &file->xfer, &posix_file_xfer_operations,
  178. &file->refcnt );
  179. INIT_LIST_HEAD ( &file->data );
  180. /* Open URI on data transfer interface */
  181. if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
  182. goto err;
  183. /* Wait for open to succeed or fail */
  184. while ( list_empty ( &file->data ) ) {
  185. step();
  186. if ( file->rc == 0 )
  187. break;
  188. if ( file->rc != -EINPROGRESS ) {
  189. rc = file->rc;
  190. goto err;
  191. }
  192. }
  193. /* Add to list of open files. List takes reference ownership. */
  194. list_add ( &file->list, &posix_files );
  195. DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
  196. return fd;
  197. err:
  198. posix_file_finished ( file, rc );
  199. ref_put ( &file->refcnt );
  200. return rc;
  201. }
  202. /**
  203. * Check file descriptors for readiness
  204. *
  205. * @v readfds File descriptors to check
  206. * @v wait Wait until data is ready
  207. * @ret nready Number of ready file descriptors
  208. */
  209. int select ( fd_set *readfds, int wait ) {
  210. struct posix_file *file;
  211. int fd;
  212. do {
  213. for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
  214. if ( ! FD_ISSET ( fd, readfds ) )
  215. continue;
  216. file = posix_fd_to_file ( fd );
  217. if ( ! file )
  218. return -EBADF;
  219. if ( ( list_empty ( &file->data ) ) &&
  220. ( file->rc == -EINPROGRESS ) )
  221. continue;
  222. /* Data is available or status has changed */
  223. FD_ZERO ( readfds );
  224. FD_SET ( fd, readfds );
  225. return 1;
  226. }
  227. step();
  228. } while ( wait );
  229. return 0;
  230. }
  231. /**
  232. * Read data from file
  233. *
  234. * @v buffer Data buffer
  235. * @v offset Starting offset within data buffer
  236. * @v len Maximum length to read
  237. * @ret len Actual length read, or negative error number
  238. *
  239. * This call is non-blocking; if no data is available to read then
  240. * -EWOULDBLOCK will be returned.
  241. */
  242. ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
  243. struct posix_file *file;
  244. struct io_buffer *iobuf;
  245. size_t len;
  246. /* Identify file */
  247. file = posix_fd_to_file ( fd );
  248. if ( ! file )
  249. return -EBADF;
  250. /* Try to fetch more data if none available */
  251. if ( list_empty ( &file->data ) )
  252. step();
  253. /* Dequeue at most one received I/O buffer into user buffer */
  254. list_for_each_entry ( iobuf, &file->data, list ) {
  255. len = iob_len ( iobuf );
  256. if ( len > max_len )
  257. len = max_len;
  258. copy_to_user ( buffer, offset, iobuf->data, len );
  259. iob_pull ( iobuf, len );
  260. if ( ! iob_len ( iobuf ) ) {
  261. list_del ( &iobuf->list );
  262. free_iob ( iobuf );
  263. }
  264. file->pos += len;
  265. return len;
  266. }
  267. /* If file has completed, return (after returning all data) */
  268. if ( file->rc != -EINPROGRESS )
  269. return file->rc;
  270. /* No data ready and file still in progress; return -WOULDBLOCK */
  271. return -EWOULDBLOCK;
  272. }
  273. /**
  274. * Determine file size
  275. *
  276. * @v fd File descriptor
  277. * @ret size File size, or negative error number
  278. */
  279. ssize_t fsize ( int fd ) {
  280. struct posix_file *file;
  281. /* Identify file */
  282. file = posix_fd_to_file ( fd );
  283. if ( ! file )
  284. return -EBADF;
  285. return file->filesize;
  286. }
  287. /**
  288. * Close file
  289. *
  290. * @v fd File descriptor
  291. * @ret rc Return status code
  292. */
  293. int close ( int fd ) {
  294. struct posix_file *file;
  295. /* Identify file */
  296. file = posix_fd_to_file ( fd );
  297. if ( ! file )
  298. return -EBADF;
  299. /* Terminate data transfer */
  300. posix_file_finished ( file, 0 );
  301. /* Remove from list of open files and drop reference */
  302. list_del ( &file->list );
  303. ref_put ( &file->refcnt );
  304. return 0;
  305. }