README.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. Virtual filesystem component
  2. ============================
  3. Overview
  4. --------
  5. Virtual filesystem (VFS) component provides a unified interface for drivers which can perform operations on file-like objects. These can be real filesystems (FAT, SPIFFS, etc.) or device drivers which provide a file-like interface.
  6. This component allows C library functions, such as fopen and fprintf, to work with FS drivers. At a high level, each FS driver is associated with some path prefix. When one of C library functions needs to open a file, the VFS component searches for the FS driver associated with the file path and forwards the call to that driver. VFS also forwards read, write, and other calls for the given file to the same FS driver.
  7. For example, one can register a FAT filesystem driver with the ``/fat`` prefix and call ``fopen("/fat/file.txt", "w")``. The VFS component will then call the function ``open`` of the FAT driver and pass the argument ``/file.txt`` to it together with appropriate mode flags. All subsequent calls to C library functions for the returned ``FILE*`` stream will also be forwarded to the FAT driver.
  8. FS registration
  9. ---------------
  10. To register an FS driver, an application needs to define an instance of the :cpp:type:`esp_vfs_t` structure and populate it with function pointers to FS APIs:
  11. .. highlight:: c
  12. ::
  13. esp_vfs_t myfs = {
  14. .flags = ESP_VFS_FLAG_DEFAULT,
  15. .write = &myfs_write,
  16. .open = &myfs_open,
  17. .fstat = &myfs_fstat,
  18. .close = &myfs_close,
  19. .read = &myfs_read,
  20. };
  21. ESP_ERROR_CHECK(esp_vfs_register("/data", &myfs, NULL));
  22. Depending on the way how the FS driver declares its API functions, either ``read``, ``write``, etc., or ``read_p``, ``write_p``, etc., should be used.
  23. Case 1: API functions are declared without an extra context pointer (the FS driver is a singleton)::
  24. ssize_t myfs_write(int fd, const void * data, size_t size);
  25. // In definition of esp_vfs_t:
  26. .flags = ESP_VFS_FLAG_DEFAULT,
  27. .write = &myfs_write,
  28. // ... other members initialized
  29. // When registering FS, context pointer (third argument) is NULL:
  30. ESP_ERROR_CHECK(esp_vfs_register("/data", &myfs, NULL));
  31. Case 2: API functions are declared with an extra context pointer (the FS driver supports multiple instances)::
  32. ssize_t myfs_write(myfs_t* fs, int fd, const void * data, size_t size);
  33. // In definition of esp_vfs_t:
  34. .flags = ESP_VFS_FLAG_CONTEXT_PTR,
  35. .write_p = &myfs_write,
  36. // ... other members initialized
  37. // When registering FS, pass the FS context pointer into the third argument
  38. // (hypothetical myfs_mount function is used for illustrative purposes)
  39. myfs_t* myfs_inst1 = myfs_mount(partition1->offset, partition1->size);
  40. ESP_ERROR_CHECK(esp_vfs_register("/data1", &myfs, myfs_inst1));
  41. // Can register another instance:
  42. myfs_t* myfs_inst2 = myfs_mount(partition2->offset, partition2->size);
  43. ESP_ERROR_CHECK(esp_vfs_register("/data2", &myfs, myfs_inst2));
  44. Synchronous input/output multiplexing
  45. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  46. Synchronous input/output multiplexing by :cpp:func:`select` is supported in the VFS component. The implementation
  47. works in the following way.
  48. 1. :cpp:func:`select` is called with file descriptors which could belong to various VFS drivers.
  49. 2. The file descriptors are divided into groups each belonging to one VFS driver.
  50. 3. The file descriptors belonging to non-socket VFS drivers are handed over to the given VFS drivers by :cpp:func:`start_select`
  51. described later on this page. This function represents the driver-specific implementation of :cpp:func:`select` for
  52. the given driver. This should be a non-blocking call which means the function should immediately return after setting up
  53. the environment for checking events related to the given file descriptors.
  54. 4. The file descriptors belonging to the socket VFS driver are handed over to the socket driver by
  55. :cpp:func:`socket_select` described later on this page. This is a blocking call which means that it will return only
  56. if there is an event related to socket file descriptors or a non-socket driver signals :cpp:func:`socket_select`
  57. to exit.
  58. 5. Results are collected from each VFS driver and all drivers are stopped by deinitiazation
  59. of the environment for checking events.
  60. 6. The :cpp:func:`select` call ends and returns the appropriate results.
  61. Non-socket VFS drivers
  62. """"""""""""""""""""""
  63. If you want to use :cpp:func:`select` with a file descriptor belonging to a non-socket VFS driver
  64. then you need to register the driver with functions :cpp:func:`start_select` and
  65. :cpp:func:`end_select` similarly to the following example:
  66. .. highlight:: c
  67. ::
  68. // In definition of esp_vfs_t:
  69. .start_select = &uart_start_select,
  70. .end_select = &uart_end_select,
  71. // ... other members initialized
  72. :cpp:func:`start_select` is called for setting up the environment for
  73. detection of read/write/error conditions on file descriptors belonging to the
  74. given VFS driver.
  75. :cpp:func:`end_select` is called to stop/deinitialize/free the
  76. environment which was setup by :cpp:func:`start_select`.
  77. .. note::
  78. :cpp:func:`end_select` might be called without a previous :cpp:func:`start_select` call in some rare
  79. circumstances. :cpp:func:`end_select` should fail gracefully if this is the case.
  80. Please refer to the
  81. reference implementation for the UART peripheral in
  82. :component_file:`vfs/vfs_uart.c` and most particularly to the functions
  83. :cpp:func:`esp_vfs_dev_uart_register`, :cpp:func:`uart_start_select`, and
  84. :cpp:func:`uart_end_select` for more information.
  85. Please check the following examples that demonstrate the use of :cpp:func:`select` with VFS file descriptors:
  86. - :example:`peripherals/uart/uart_select`
  87. - :example:`system/select`
  88. Socket VFS drivers
  89. """"""""""""""""""
  90. A socket VFS driver is using its own internal implementation of :cpp:func:`select` and non-socket VFS drivers notify
  91. it upon read/write/error conditions.
  92. A socket VFS driver needs to be registered with the following functions defined:
  93. .. highlight:: c
  94. ::
  95. // In definition of esp_vfs_t:
  96. .socket_select = &lwip_select,
  97. .get_socket_select_semaphore = &lwip_get_socket_select_semaphore,
  98. .stop_socket_select = &lwip_stop_socket_select,
  99. .stop_socket_select_isr = &lwip_stop_socket_select_isr,
  100. // ... other members initialized
  101. :cpp:func:`socket_select` is the internal implementation of :cpp:func:`select` for the socket driver. It works only
  102. with file descriptors belonging to the socket VFS.
  103. :cpp:func:`get_socket_select_semaphore` returns the signalization object (semaphore) which will be used in non-socket
  104. drivers to stop the waiting in :cpp:func:`socket_select`.
  105. :cpp:func:`stop_socket_select` call is used to stop the waiting in :cpp:func:`socket_select` by passing the object
  106. returned by :cpp:func:`get_socket_select_semaphore`.
  107. :cpp:func:`stop_socket_select_isr` has the same functionality as :cpp:func:`stop_socket_select` but it can be used
  108. from ISR.
  109. Please see :component_file:`lwip/port/esp32/vfs_lwip.c` for a reference socket driver implementation using LWIP.
  110. .. note::
  111. If you use :cpp:func:`select` for socket file descriptors only then you can enable the
  112. :envvar:`CONFIG_LWIP_USE_ONLY_LWIP_SELECT` option to reduce the code size and improve performance.
  113. .. note::
  114. Don't change the socket driver during an active :cpp:func:`select` call or you might experience some undefined
  115. behavior.
  116. Paths
  117. -----
  118. Each registered FS has a path prefix associated with it. This prefix can be considered as a "mount point" of this partition.
  119. In case when mount points are nested, the mount point with the longest matching path prefix is used when opening the file. For instance, suppose that the following filesystems are registered in VFS:
  120. - FS 1 on /data
  121. - FS 2 on /data/static
  122. Then:
  123. - FS 1 will be used when opening a file called ``/data/log.txt``
  124. - FS 2 will be used when opening a file called ``/data/static/index.html``
  125. - Even if ``/index.html"`` does not exist in FS 2, FS 1 will *not* be searched for ``/static/index.html``.
  126. As a general rule, mount point names must start with the path separator (``/``) and must contain at least one character after path separator. However, an empty mount point name is also supported and might be used in cases when an application needs to provide a "fallback" filesystem or to override VFS functionality altogether. Such filesystem will be used if no prefix matches the path given.
  127. VFS does not handle dots (``.``) in path names in any special way. VFS does not treat ``..`` as a reference to the parent directory. In the above example, using a path ``/data/static/../log.txt`` will not result in a call to FS 1 to open ``/log.txt``. Specific FS drivers (such as FATFS) might handle dots in file names differently.
  128. When opening files, the FS driver receives only relative paths to files. For example:
  129. 1. The ``myfs`` driver is registered with ``/data`` as a path prefix.
  130. 2. The application calls ``fopen("/data/config.json", ...)``.
  131. 3. The VFS component calls ``myfs_open("/config.json", ...)``.
  132. 4. The ``myfs`` driver opens the ``/config.json`` file.
  133. VFS does not impose any limit on total file path length, but it does limit the FS path prefix to ``ESP_VFS_PATH_MAX`` characters. Individual FS drivers may have their own filename length limitations.
  134. File descriptors
  135. ----------------
  136. File descriptors are small positive integers from ``0`` to ``FD_SETSIZE - 1``, where ``FD_SETSIZE`` is defined in newlib's ``sys/types.h``. The largest file descriptors (configured by ``CONFIG_LWIP_MAX_SOCKETS``) are reserved for sockets. The VFS component contains a lookup-table called ``s_fd_table`` for mapping global file descriptors to VFS driver indexes registered in the ``s_vfs`` array.
  137. Standard IO streams (stdin, stdout, stderr)
  138. -------------------------------------------
  139. If the menuconfig option ``UART for console output`` is not set to ``None``, then ``stdin``, ``stdout``, and ``stderr`` are configured to read from, and write to, a UART. It is possible to use UART0 or UART1 for standard IO. By default, UART0 is used with 115200 baud rate; TX pin is GPIO1; RX pin is GPIO3. These parameters can be changed in menuconfig.
  140. Writing to ``stdout`` or ``stderr`` will send characters to the UART transmit FIFO. Reading from ``stdin`` will retrieve characters from the UART receive FIFO.
  141. By default, VFS uses simple functions for reading from and writing to UART. Writes busy-wait until all data is put into UART FIFO, and reads are non-blocking, returning only the data present in the FIFO. Due to this non-blocking read behavior, higher level C library calls, such as ``fscanf("%d\n", &var);``, might not have desired results.
  142. Applications which use the UART driver can instruct VFS to use the driver's interrupt driven, blocking read and write functions instead. This can be done using a call to the ``esp_vfs_dev_uart_use_driver`` function. It is also possible to revert to the basic non-blocking functions using a call to ``esp_vfs_dev_uart_use_nonblocking``.
  143. VFS also provides an optional newline conversion feature for input and output. Internally, most applications send and receive lines terminated by the LF (''\n'') character. Different terminal programs may require different line termination, such as CR or CRLF. Applications can configure this separately for input and output either via menuconfig, or by calls to the functions ``esp_vfs_dev_uart_set_rx_line_endings`` and ``esp_vfs_dev_uart_set_tx_line_endings``.
  144. Standard streams and FreeRTOS tasks
  145. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  146. ``FILE`` objects for ``stdin``, ``stdout``, and ``stderr`` are shared between all FreeRTOS tasks, but the pointers to these objects are stored in per-task ``struct _reent``.
  147. The following code is transferred to ``fprintf(__getreent()->_stderr, "42\n");`` by the preprocessor:
  148. .. highlight:: c
  149. ::
  150. fprintf(stderr, "42\n");
  151. The ``__getreent()`` function returns a per-task pointer to ``struct _reent`` (:component_file:`newlib/include/sys/reent.h#L370-L417`). This structure is allocated on the TCB of each task. When a task is initialized, ``_stdin``, ``_stdout``, and ``_stderr`` members of ``struct _reent`` are set to the values of ``_stdin``, ``_stdout``, and ``_stderr`` of ``_GLOBAL_REENT`` (i.e., the structure which is used before FreeRTOS is started).
  152. Such a design has the following consequences:
  153. - It is possible to set ``stdin``, ``stdout``, and ``stderr`` for any given task without affecting other tasks, e.g., by doing ``stdin = fopen("/dev/uart/1", "r")``.
  154. - Closing default ``stdin``, ``stdout``, or ``stderr`` using ``fclose`` will close the ``FILE`` stream object, which will affect all other tasks.
  155. - To change the default ``stdin``, ``stdout``, ``stderr`` streams for new tasks, modify ``_GLOBAL_REENT->_stdin`` (``_stdout``, ``_stderr``) before creating the task.