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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. /** Minimum file descriptor that will ever be allocated */
  59. #define POSIX_FD_MIN ( 1 )
  60. /** Maximum file descriptor that will ever be allocated */
  61. #define POSIX_FD_MAX ( 255 )
  62. /**
  63. * Free open file
  64. *
  65. * @v refcnt Reference counter
  66. */
  67. static void posix_file_free ( struct refcnt *refcnt ) {
  68. struct posix_file *file =
  69. container_of ( refcnt, struct posix_file, refcnt );
  70. struct io_buffer *iobuf;
  71. struct io_buffer *tmp;
  72. list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
  73. list_del ( &iobuf->list );
  74. free_iob ( iobuf );
  75. }
  76. free ( file );
  77. }
  78. /**
  79. * Terminate file data transfer
  80. *
  81. * @v file POSIX file
  82. * @v rc Reason for termination
  83. */
  84. static void posix_file_finished ( struct posix_file *file, int rc ) {
  85. xfer_nullify ( &file->xfer );
  86. xfer_close ( &file->xfer, rc );
  87. file->rc = rc;
  88. }
  89. /**
  90. * Handle close() event
  91. *
  92. * @v xfer POSIX file data transfer interface
  93. * @v rc Reason for close
  94. */
  95. static void posix_file_xfer_close ( struct xfer_interface *xfer, int rc ) {
  96. struct posix_file *file =
  97. container_of ( xfer, struct posix_file, xfer );
  98. posix_file_finished ( file, rc );
  99. }
  100. /**
  101. * Handle seek() event
  102. *
  103. * @v xfer POSIX file data transfer interface
  104. * @v pos New position
  105. * @ret rc Return status code
  106. */
  107. static int posix_file_xfer_seek ( struct xfer_interface *xfer, off_t offset,
  108. int whence ) {
  109. struct posix_file *file =
  110. container_of ( xfer, struct posix_file, xfer );
  111. switch ( whence ) {
  112. case SEEK_SET:
  113. file->pos = offset;
  114. break;
  115. case SEEK_CUR:
  116. file->pos += offset;
  117. break;
  118. }
  119. if ( file->filesize < file->pos )
  120. file->filesize = file->pos;
  121. return 0;
  122. }
  123. /**
  124. * Handle deliver_iob() event
  125. *
  126. * @v xfer POSIX file data transfer interface
  127. * @v iobuf I/O buffer
  128. * @v meta Data transfer metadata, or NULL
  129. * @ret rc Return status code
  130. */
  131. static int
  132. posix_file_xfer_deliver_iob ( struct xfer_interface *xfer,
  133. struct io_buffer *iobuf,
  134. struct xfer_metadata *meta __unused ) {
  135. struct posix_file *file =
  136. container_of ( xfer, struct posix_file, xfer );
  137. list_add_tail ( &iobuf->list, &file->data );
  138. return 0;
  139. }
  140. /** POSIX file data transfer interface operations */
  141. static struct xfer_interface_operations posix_file_xfer_operations = {
  142. .close = posix_file_xfer_close,
  143. .vredirect = xfer_vopen,
  144. .seek = posix_file_xfer_seek,
  145. .window = unlimited_xfer_window,
  146. .alloc_iob = default_xfer_alloc_iob,
  147. .deliver_iob = posix_file_xfer_deliver_iob,
  148. .deliver_raw = xfer_deliver_as_iob,
  149. };
  150. /**
  151. * Identify file by file descriptor
  152. *
  153. * @v fd File descriptor
  154. * @ret file Corresponding file, or NULL
  155. */
  156. static struct posix_file * posix_fd_to_file ( int fd ) {
  157. struct posix_file *file;
  158. list_for_each_entry ( file, &posix_files, list ) {
  159. if ( file->fd == fd )
  160. return file;
  161. }
  162. return NULL;
  163. }
  164. /**
  165. * Find an available file descriptor
  166. *
  167. * @ret fd File descriptor, or negative error number
  168. */
  169. static int posix_find_free_fd ( void ) {
  170. int fd;
  171. for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
  172. if ( ! posix_fd_to_file ( fd ) )
  173. return fd;
  174. }
  175. DBG ( "POSIX could not find free file descriptor\n" );
  176. return -ENFILE;
  177. }
  178. /**
  179. * Open file
  180. *
  181. * @v uri_string URI string
  182. * @ret fd File descriptor, or negative error number
  183. */
  184. int open ( const char *uri_string ) {
  185. struct posix_file *file;
  186. int fd;
  187. int rc;
  188. /* Find a free file descriptor to use */
  189. fd = posix_find_free_fd();
  190. if ( fd < 0 )
  191. return fd;
  192. /* Allocate and initialise structure */
  193. file = zalloc ( sizeof ( *file ) );
  194. if ( ! file )
  195. return -ENOMEM;
  196. file->refcnt.free = posix_file_free;
  197. file->fd = fd;
  198. file->rc = -EINPROGRESS;
  199. xfer_init ( &file->xfer, &posix_file_xfer_operations,
  200. &file->refcnt );
  201. INIT_LIST_HEAD ( &file->data );
  202. /* Open URI on data transfer interface */
  203. if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
  204. goto err;
  205. /* Wait for open to succeed or fail */
  206. while ( list_empty ( &file->data ) ) {
  207. step();
  208. if ( file->rc == 0 )
  209. break;
  210. if ( file->rc != -EINPROGRESS ) {
  211. rc = file->rc;
  212. goto err;
  213. }
  214. }
  215. /* Add to list of open files. List takes reference ownership. */
  216. list_add ( &file->list, &posix_files );
  217. DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
  218. return fd;
  219. err:
  220. posix_file_finished ( file, rc );
  221. ref_put ( &file->refcnt );
  222. return rc;
  223. }
  224. /**
  225. * Read data from file
  226. *
  227. * @v buffer Data buffer
  228. * @v offset Starting offset within data buffer
  229. * @v len Maximum length to read
  230. * @ret len Actual length read, or negative error number
  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 frag_len;
  236. ssize_t len = 0;
  237. /* Identify file */
  238. file = posix_fd_to_file ( fd );
  239. if ( ! file )
  240. return -EBADF;
  241. while ( 1 ) {
  242. /* Try to fetch more data if none available */
  243. if ( list_empty ( &file->data ) )
  244. step();
  245. /* Dequeue at most one received I/O buffer into user buffer */
  246. list_for_each_entry ( iobuf, &file->data, list ) {
  247. frag_len = iob_len ( iobuf );
  248. if ( frag_len > max_len )
  249. frag_len = max_len;
  250. copy_to_user ( buffer, offset, iobuf->data,
  251. frag_len );
  252. iob_pull ( iobuf, frag_len );
  253. if ( ! iob_len ( iobuf ) ) {
  254. list_del ( &iobuf-> list );
  255. free_iob ( iobuf );
  256. }
  257. file->pos += frag_len;
  258. len += frag_len;
  259. offset += frag_len;
  260. max_len -= frag_len;
  261. break;
  262. }
  263. /* If buffer is full, return */
  264. if ( ! max_len )
  265. return len;
  266. /* If file has completed, return */
  267. if ( file->rc != -EINPROGRESS )
  268. return ( file->rc ? file->rc : len );
  269. }
  270. }
  271. /**
  272. * Determine file size
  273. *
  274. * @v fd File descriptor
  275. * @ret size File size, or negative error number
  276. */
  277. ssize_t fsize ( int fd ) {
  278. struct posix_file *file;
  279. /* Identify file */
  280. file = posix_fd_to_file ( fd );
  281. if ( ! file )
  282. return -EBADF;
  283. return file->filesize;
  284. }
  285. /**
  286. * Close file
  287. *
  288. * @v fd File descriptor
  289. * @ret rc Return status code
  290. */
  291. int close ( int fd ) {
  292. struct posix_file *file;
  293. /* Identify file */
  294. file = posix_fd_to_file ( fd );
  295. if ( ! file )
  296. return -EBADF;
  297. /* Terminate data transfer */
  298. posix_file_finished ( file, 0 );
  299. /* Remove from list of open files and drop reference */
  300. list_del ( &file->list );
  301. ref_put ( &file->refcnt );
  302. return 0;
  303. }