Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

posix_io.c 7.8KB

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