您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

posix_io.c 7.5KB

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