dfs_9pfs.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-25 GuEe-GUI the first version
  9. */
  10. #ifndef __DFS_9PFS_H__
  11. #define __DFS_9PFS_H__
  12. #include <rtdef.h>
  13. #include <dfs.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #include <bitmap.h>
  17. #define P9_ERROR -1
  18. #define P9_UNKNOWN_VERSION -2
  19. #define P9_R_ERROR -3
  20. #define P9_MSG_TOO_LONG -4
  21. #define P9_UNEXPECTED_MSG -5
  22. #define P9_UNEXPECTED_TAG -6
  23. #define P9_TRANSPORT_ERROR -7
  24. #define P9_NO_TRANSPORT -8
  25. #define P9_NULL_PATH -9
  26. #define P9_PATH_ELEMENT_TOO_LONG -10
  27. #define P9_READ_UNEXPECTED_DATA -11
  28. #define P9_NO_BUFFER -12
  29. #define P9_MSG_SIZE_TOO_BIG -13
  30. #define P9_PARTIAL_WALK 1
  31. #define P9_VERSION "9P2000.u"
  32. #define P9_VERSION_UNKNOWN "unknown"
  33. #define P9_MSG_F_TYPE 7
  34. #define P9_MSG_F_BSIZE 11
  35. #define P9_MSG_F_BLOCKS 15
  36. #define P9_MSG_F_BFREE 23
  37. #define P9_MSG_F_BAVAIL 31
  38. #define P9_MSG_F_FILES 39
  39. #define P9_MSG_F_FFREE 47
  40. #define P9_MSG_FSID_VAL 55
  41. #define P9_MSG_F_NAMELEN 63
  42. #define P9_MSG_SIZE 0
  43. #define P9_MSG_ID 4
  44. #define P9_MSG_ERR 0x6b
  45. #define P9_MSG_ERR_STR 9
  46. #define P9_MSG_ERR_STR_LEN 7
  47. #define P9_MSG_TAG 5
  48. #define P9_MSG_VER_MSIZE 7
  49. #define P9_MSG_VER_STR_LEN 11
  50. #define P9_MSG_VER_STR 13
  51. #define P9_MSG_WALK_TX_ELMT 15
  52. #define P9_MSG_WALK_RX_ELMT 7
  53. #define P9_MSG_WALK_MAX_ELMT 16
  54. #define P9_MSG_QID_SIZE 13
  55. #define P9_MSG_WALK_RX_HDR_SIZE 9
  56. #define P9_MSG_OPEN_IOUNIT 20
  57. #define P9_MSG_OPEN_MODE_MASK 0x5f
  58. #define P9_MSG_CREATE_IOUNIT 20
  59. #define P9_MSG_READ_COUNT 7
  60. #define P9_MSG_READ_DATA 11
  61. #define P9_MSG_WRITE_COUNT 7
  62. #define P9_MSG_WRITE_DATA 23
  63. #define P9_MSG_RENAME_DATA 15
  64. #define P9_MSG_STAT_SIZE 9
  65. #define P9_MSG_STAT_TYPE 11
  66. #define P9_MSG_STAT_DEV 13
  67. #define P9_MSG_STAT_QID_TYPE 17
  68. #define P9_MSG_STAT_QID_VERSION 18
  69. #define P9_MSG_STAT_QID_PATH 22
  70. #define P9_MSG_STAT_MODE 30
  71. #define P9_MSG_STAT_ATIME 34
  72. #define P9_MSG_STAT_MTIME 38
  73. #define P9_MSG_STAT_LEN 42
  74. #define P9_MSG_STAT_NAME_LEN 50
  75. #define P9_MSG_STAT_NAME 52
  76. #define P9_HD_TSTATFS 8
  77. #define P9_HD_RSTATFS (P9_HD_TSTATFS + 1)
  78. #define P9_HD_TRENAME 20
  79. #define P9_HD_RRENAME (P9_HD_TRENAME + 1)
  80. #define P9_HD_TFSYNC 50
  81. #define P9_HD_RFSYNC (P9_HD_TFSYNC + 1)
  82. #define P9_HD_TVERSION 100
  83. #define P9_HD_RVERSION (P9_HD_TVERSION + 1)
  84. #define P9_HD_TAUTH 102
  85. #define P9_HD_RAUTH (P9_HD_TAUTH + 1)
  86. #define P9_HD_TATTACH 104
  87. #define P9_HD_RATTACH (P9_HD_TATTACH + 1)
  88. #define P9_HD_TERROR 106
  89. #define P9_HD_RERROR (P9_HD_TERROR + 1)
  90. #define P9_HD_TFLUSH 108
  91. #define P9_HD_RFLUSH (P9_HD_TFLUSH + 1)
  92. #define P9_HD_TWALK 110
  93. #define P9_HD_RWALK (P9_HD_TWALK + 1)
  94. #define P9_HD_TOPEN 112
  95. #define P9_HD_ROPEN (P9_HD_TOPEN + 1)
  96. #define P9_HD_TCREATE 114
  97. #define P9_HD_RCREATE (P9_HD_TCREATE + 1)
  98. #define P9_HD_TREAD 116
  99. #define P9_HD_RREAD (P9_HD_TREAD + 1)
  100. #define P9_HD_TWRITE 118
  101. #define P9_HD_RWRITE (P9_HD_TWRITE + 1)
  102. #define P9_HD_TCLUNK 120
  103. #define P9_HD_RCLUNK (P9_HD_TCLUNK + 1)
  104. #define P9_HD_TREMOVE 122
  105. #define P9_HD_RREMOVE (P9_HD_TREMOVE + 1)
  106. #define P9_HD_TSTAT 124
  107. #define P9_HD_RSTAT (P9_HD_TSTAT + 1)
  108. #define P9_STAT_MODE_DIR 0x80000000
  109. #define P9_STAT_MODE_APPEND 0x40000000
  110. #define P9_STAT_MODE_EXCL 0x20000000
  111. #define P9_STAT_MODE_MOUNT 0x10000000
  112. #define P9_STAT_MODE_AUTH 0x08000000
  113. #define P9_STAT_MODE_TMP 0x04000000
  114. #define P9_STAT_MODE_SYMLINK 0x02000000
  115. #define P9_STAT_MODE_LINK 0x01000000
  116. #define P9_STAT_MODE_DEVICE 0x00800000
  117. #define P9_STAT_MODE_NAMED_PIPE 0x00200000
  118. #define P9_STAT_MODE_SOCKET 0x00100000
  119. #define P9_STAT_MODE_SETUID 0x00080000
  120. #define P9_STAT_MODE_SETGID 0x00040000
  121. #define P9_STAT_MODE_SETVTX 0x00010000
  122. struct p9_protocol
  123. {
  124. rt_list_t list;
  125. char *name;
  126. const char *tag;
  127. rt_err_t (*transport)(struct p9_protocol *p9p,
  128. rt_uint8_t *tx_data, rt_uint32_t tx_size,
  129. rt_uint8_t *rx_data, rt_uint32_t *ref_rx_size);
  130. };
  131. struct p9_connection
  132. {
  133. char *uname; /* User name */
  134. char *aname; /* Tree/mount name/path */
  135. rt_uint32_t fid; /* Represents mount point */
  136. rt_uint32_t msg_size;
  137. char error[200];
  138. rt_uint8_t *rx_buffer;
  139. rt_uint8_t *tx_buffer;
  140. struct rt_mutex lock;
  141. struct p9_protocol *protocol;
  142. RT_BITMAP_DECLARE(fid_map, DFS_FD_MAX);
  143. };
  144. struct p9_file
  145. {
  146. #define P9_ROOT_FID 1
  147. #define P9_FILE_FID 2
  148. rt_uint32_t fid; /* Identifies the file to P9 server */
  149. rt_uint32_t iounit; /* Maximum read size in bytes */
  150. struct p9_connection *connection;
  151. };
  152. /**
  153. * @brief Allocate a new 9P connection.
  154. *
  155. * @param p9p Pointer to the 9P protocol structure.
  156. * @param aname Mount name/path.
  157. * @param buffer_size Size of the buffer to allocate.
  158. * @return Pointer to the allocated p9_connection structure, or RT_NULL on failure.
  159. */
  160. struct p9_connection *p9_connection_alloc(struct p9_protocol *p9p,
  161. const char *aname, rt_uint32_t buffer_size);
  162. /**
  163. * @brief Free a 9P connection and release its resources.
  164. *
  165. * @param conn Pointer to the p9_connection to free.
  166. * @return RT_EOK on success, error code otherwise.
  167. */
  168. rt_err_t p9_connection_free(struct p9_connection *conn);
  169. /**
  170. * @brief Perform a 9P transaction.
  171. *
  172. * @param conn Pointer to the p9_connection.
  173. * @param tx_size Size of the transmit buffer.
  174. * @param out_rx_size Pointer to store the received buffer size.
  175. * @return 0 on success, negative error code otherwise.
  176. */
  177. int p9_transaction(struct p9_connection *conn,
  178. rt_uint32_t tx_size, rt_uint32_t *out_rx_size);
  179. /**
  180. * @brief Negotiate 9P protocol version with the server.
  181. *
  182. * @param conn Pointer to the p9_connection.
  183. * @return 0 on success, negative error code otherwise.
  184. */
  185. int p9_version(struct p9_connection *conn);
  186. /**
  187. * @brief Attach to the 9P server.
  188. *
  189. * @param conn Pointer to the p9_connection.
  190. * @return 0 on success, negative error code otherwise.
  191. */
  192. int p9_attach(struct p9_connection *conn);
  193. /**
  194. * @brief Clunk (close) a file identifier (fid) on the 9P server.
  195. *
  196. * @param conn Pointer to the p9_connection.
  197. * @param fid File identifier to clunk.
  198. * @return 0 on success, negative error code otherwise.
  199. */
  200. int p9_clunk(struct p9_connection *conn, rt_uint32_t fid);
  201. /**
  202. * @brief Register a 9P protocol tag.
  203. *
  204. * @param p9p Pointer to the p9_protocol structure.
  205. * @return RT_EOK on success, error code otherwise.
  206. */
  207. rt_err_t dfs_9pfs_add_tag(struct p9_protocol *p9p);
  208. /**
  209. * @brief Unregister a 9P protocol tag.
  210. *
  211. * @param p9p Pointer to the p9_protocol structure.
  212. * @return RT_EOK on success, error code otherwise.
  213. */
  214. rt_err_t dfs_9pfs_del_tag(struct p9_protocol *p9p);
  215. /**
  216. * @brief Initialize the 9P filesystem.
  217. *
  218. * @return 0 on success, negative error code otherwise.
  219. */
  220. int dfs_9pfs_init(void);
  221. #endif /* __DFS_9PFS_H__ */