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

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