9p.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #ifndef QEMU_9P_H
  2. #define QEMU_9P_H
  3. #include <dirent.h>
  4. #include <utime.h>
  5. #include <sys/resource.h>
  6. #include "fsdev/file-op-9p.h"
  7. #include "fsdev/9p-iov-marshal.h"
  8. #include "qemu/thread.h"
  9. #include "qemu/coroutine.h"
  10. #include "qemu/qht.h"
  11. enum {
  12. P9_TLERROR = 6,
  13. P9_RLERROR,
  14. P9_TSTATFS = 8,
  15. P9_RSTATFS,
  16. P9_TLOPEN = 12,
  17. P9_RLOPEN,
  18. P9_TLCREATE = 14,
  19. P9_RLCREATE,
  20. P9_TSYMLINK = 16,
  21. P9_RSYMLINK,
  22. P9_TMKNOD = 18,
  23. P9_RMKNOD,
  24. P9_TRENAME = 20,
  25. P9_RRENAME,
  26. P9_TREADLINK = 22,
  27. P9_RREADLINK,
  28. P9_TGETATTR = 24,
  29. P9_RGETATTR,
  30. P9_TSETATTR = 26,
  31. P9_RSETATTR,
  32. P9_TXATTRWALK = 30,
  33. P9_RXATTRWALK,
  34. P9_TXATTRCREATE = 32,
  35. P9_RXATTRCREATE,
  36. P9_TREADDIR = 40,
  37. P9_RREADDIR,
  38. P9_TFSYNC = 50,
  39. P9_RFSYNC,
  40. P9_TLOCK = 52,
  41. P9_RLOCK,
  42. P9_TGETLOCK = 54,
  43. P9_RGETLOCK,
  44. P9_TLINK = 70,
  45. P9_RLINK,
  46. P9_TMKDIR = 72,
  47. P9_RMKDIR,
  48. P9_TRENAMEAT = 74,
  49. P9_RRENAMEAT,
  50. P9_TUNLINKAT = 76,
  51. P9_RUNLINKAT,
  52. P9_TVERSION = 100,
  53. P9_RVERSION,
  54. P9_TAUTH = 102,
  55. P9_RAUTH,
  56. P9_TATTACH = 104,
  57. P9_RATTACH,
  58. P9_TERROR = 106,
  59. P9_RERROR,
  60. P9_TFLUSH = 108,
  61. P9_RFLUSH,
  62. P9_TWALK = 110,
  63. P9_RWALK,
  64. P9_TOPEN = 112,
  65. P9_ROPEN,
  66. P9_TCREATE = 114,
  67. P9_RCREATE,
  68. P9_TREAD = 116,
  69. P9_RREAD,
  70. P9_TWRITE = 118,
  71. P9_RWRITE,
  72. P9_TCLUNK = 120,
  73. P9_RCLUNK,
  74. P9_TREMOVE = 122,
  75. P9_RREMOVE,
  76. P9_TSTAT = 124,
  77. P9_RSTAT,
  78. P9_TWSTAT = 126,
  79. P9_RWSTAT,
  80. };
  81. /* qid.types */
  82. enum {
  83. P9_QTDIR = 0x80,
  84. P9_QTAPPEND = 0x40,
  85. P9_QTEXCL = 0x20,
  86. P9_QTMOUNT = 0x10,
  87. P9_QTAUTH = 0x08,
  88. P9_QTTMP = 0x04,
  89. P9_QTSYMLINK = 0x02,
  90. P9_QTLINK = 0x01,
  91. P9_QTFILE = 0x00,
  92. };
  93. typedef enum P9ProtoVersion {
  94. V9FS_PROTO_2000U = 0x01,
  95. V9FS_PROTO_2000L = 0x02,
  96. } P9ProtoVersion;
  97. #define P9_NOTAG UINT16_MAX
  98. #define P9_NOFID UINT32_MAX
  99. #define P9_MAXWELEM 16
  100. #define FID_REFERENCED 0x1
  101. #define FID_NON_RECLAIMABLE 0x2
  102. static inline char *rpath(FsContext *ctx, const char *path)
  103. {
  104. return g_strdup_printf("%s/%s", ctx->fs_root, path);
  105. }
  106. /*
  107. * ample room for Twrite/Rread header
  108. * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
  109. */
  110. #define P9_IOHDRSZ 24
  111. typedef struct V9fsPDU V9fsPDU;
  112. typedef struct V9fsState V9fsState;
  113. typedef struct V9fsTransport V9fsTransport;
  114. typedef struct {
  115. uint32_t size_le;
  116. uint8_t id;
  117. uint16_t tag_le;
  118. } QEMU_PACKED P9MsgHeader;
  119. /* According to the specification, 9p messages start with a 7-byte header.
  120. * Since most of the code uses this header size in literal form, we must be
  121. * sure this is indeed the case.
  122. */
  123. QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader) != 7);
  124. struct V9fsPDU
  125. {
  126. uint32_t size;
  127. uint16_t tag;
  128. uint8_t id;
  129. uint8_t cancelled;
  130. CoQueue complete;
  131. V9fsState *s;
  132. QLIST_ENTRY(V9fsPDU) next;
  133. uint32_t idx;
  134. };
  135. /* FIXME
  136. * 1) change user needs to set groups and stuff
  137. */
  138. #define MAX_REQ 128
  139. #define MAX_TAG_LEN 32
  140. #define BUG_ON(cond) assert(!(cond))
  141. typedef struct V9fsFidState V9fsFidState;
  142. enum {
  143. P9_FID_NONE = 0,
  144. P9_FID_FILE,
  145. P9_FID_DIR,
  146. P9_FID_XATTR,
  147. };
  148. typedef struct V9fsConf
  149. {
  150. /* tag name for the device */
  151. char *tag;
  152. char *fsdev_id;
  153. } V9fsConf;
  154. /* 9p2000.L xattr flags (matches Linux values) */
  155. #define P9_XATTR_CREATE 1
  156. #define P9_XATTR_REPLACE 2
  157. typedef struct V9fsXattr
  158. {
  159. uint64_t copied_len;
  160. uint64_t len;
  161. void *value;
  162. V9fsString name;
  163. int flags;
  164. bool xattrwalk_fid;
  165. } V9fsXattr;
  166. typedef struct V9fsDir {
  167. DIR *stream;
  168. QemuMutex readdir_mutex;
  169. } V9fsDir;
  170. static inline void v9fs_readdir_lock(V9fsDir *dir)
  171. {
  172. qemu_mutex_lock(&dir->readdir_mutex);
  173. }
  174. static inline void v9fs_readdir_unlock(V9fsDir *dir)
  175. {
  176. qemu_mutex_unlock(&dir->readdir_mutex);
  177. }
  178. static inline void v9fs_readdir_init(V9fsDir *dir)
  179. {
  180. qemu_mutex_init(&dir->readdir_mutex);
  181. }
  182. /*
  183. * Filled by fs driver on open and other
  184. * calls.
  185. */
  186. union V9fsFidOpenState {
  187. int fd;
  188. V9fsDir dir;
  189. V9fsXattr xattr;
  190. /*
  191. * private pointer for fs drivers, that
  192. * have its own internal representation of
  193. * open files.
  194. */
  195. void *private;
  196. };
  197. struct V9fsFidState
  198. {
  199. int fid_type;
  200. int32_t fid;
  201. V9fsPath path;
  202. V9fsFidOpenState fs;
  203. V9fsFidOpenState fs_reclaim;
  204. int flags;
  205. int open_flags;
  206. uid_t uid;
  207. int ref;
  208. int clunked;
  209. V9fsFidState *next;
  210. V9fsFidState *rclm_lst;
  211. };
  212. typedef enum AffixType_t {
  213. AffixType_Prefix,
  214. AffixType_Suffix, /* A.k.a. postfix. */
  215. } AffixType_t;
  216. /**
  217. * @brief Unique affix of variable length.
  218. *
  219. * An affix is (currently) either a suffix or a prefix, which is either
  220. * going to be prepended (prefix) or appended (suffix) with some other
  221. * number for the goal to generate unique numbers. Accordingly the
  222. * suffixes (or prefixes) we generate @b must all have the mathematical
  223. * property of being suffix-free (or prefix-free in case of prefixes)
  224. * so that no matter what number we concatenate the affix with, that we
  225. * always reliably get unique numbers as result after concatenation.
  226. */
  227. typedef struct VariLenAffix {
  228. AffixType_t type; /* Whether this affix is a suffix or a prefix. */
  229. uint64_t value; /* Actual numerical value of this affix. */
  230. /*
  231. * Lenght of the affix, that is how many (of the lowest) bits of @c value
  232. * must be used for appending/prepending this affix to its final resulting,
  233. * unique number.
  234. */
  235. int bits;
  236. } VariLenAffix;
  237. /* See qid_inode_prefix_hash_bits(). */
  238. typedef struct {
  239. dev_t dev; /* FS device on host. */
  240. /*
  241. * How many (high) bits of the original inode number shall be used for
  242. * hashing.
  243. */
  244. int prefix_bits;
  245. } QpdEntry;
  246. /* QID path prefix entry, see stat_to_qid */
  247. typedef struct {
  248. dev_t dev;
  249. uint16_t ino_prefix;
  250. uint32_t qp_affix_index;
  251. VariLenAffix qp_affix;
  252. } QppEntry;
  253. /* QID path full entry, as above */
  254. typedef struct {
  255. dev_t dev;
  256. ino_t ino;
  257. uint64_t path;
  258. } QpfEntry;
  259. struct V9fsState
  260. {
  261. QLIST_HEAD(, V9fsPDU) free_list;
  262. QLIST_HEAD(, V9fsPDU) active_list;
  263. V9fsFidState *fid_list;
  264. FileOperations *ops;
  265. FsContext ctx;
  266. char *tag;
  267. P9ProtoVersion proto_version;
  268. int32_t msize;
  269. V9fsPDU pdus[MAX_REQ];
  270. const V9fsTransport *transport;
  271. /*
  272. * lock ensuring atomic path update
  273. * on rename.
  274. */
  275. CoRwlock rename_lock;
  276. int32_t root_fid;
  277. Error *migration_blocker;
  278. V9fsConf fsconf;
  279. V9fsQID root_qid;
  280. dev_t dev_id;
  281. struct qht qpd_table;
  282. struct qht qpp_table;
  283. struct qht qpf_table;
  284. uint64_t qp_ndevices; /* Amount of entries in qpd_table. */
  285. uint16_t qp_affix_next;
  286. uint64_t qp_fullpath_next;
  287. };
  288. /* 9p2000.L open flags */
  289. #define P9_DOTL_RDONLY 00000000
  290. #define P9_DOTL_WRONLY 00000001
  291. #define P9_DOTL_RDWR 00000002
  292. #define P9_DOTL_NOACCESS 00000003
  293. #define P9_DOTL_CREATE 00000100
  294. #define P9_DOTL_EXCL 00000200
  295. #define P9_DOTL_NOCTTY 00000400
  296. #define P9_DOTL_TRUNC 00001000
  297. #define P9_DOTL_APPEND 00002000
  298. #define P9_DOTL_NONBLOCK 00004000
  299. #define P9_DOTL_DSYNC 00010000
  300. #define P9_DOTL_FASYNC 00020000
  301. #define P9_DOTL_DIRECT 00040000
  302. #define P9_DOTL_LARGEFILE 00100000
  303. #define P9_DOTL_DIRECTORY 00200000
  304. #define P9_DOTL_NOFOLLOW 00400000
  305. #define P9_DOTL_NOATIME 01000000
  306. #define P9_DOTL_CLOEXEC 02000000
  307. #define P9_DOTL_SYNC 04000000
  308. /* 9p2000.L at flags */
  309. #define P9_DOTL_AT_REMOVEDIR 0x200
  310. /* 9P2000.L lock type */
  311. #define P9_LOCK_TYPE_RDLCK 0
  312. #define P9_LOCK_TYPE_WRLCK 1
  313. #define P9_LOCK_TYPE_UNLCK 2
  314. #define P9_LOCK_SUCCESS 0
  315. #define P9_LOCK_BLOCKED 1
  316. #define P9_LOCK_ERROR 2
  317. #define P9_LOCK_GRACE 3
  318. #define P9_LOCK_FLAGS_BLOCK 1
  319. #define P9_LOCK_FLAGS_RECLAIM 2
  320. typedef struct V9fsFlock
  321. {
  322. uint8_t type;
  323. uint32_t flags;
  324. uint64_t start; /* absolute offset */
  325. uint64_t length;
  326. uint32_t proc_id;
  327. V9fsString client_id;
  328. } V9fsFlock;
  329. typedef struct V9fsGetlock
  330. {
  331. uint8_t type;
  332. uint64_t start; /* absolute offset */
  333. uint64_t length;
  334. uint32_t proc_id;
  335. V9fsString client_id;
  336. } V9fsGetlock;
  337. extern int open_fd_hw;
  338. extern int total_open_fd;
  339. static inline void v9fs_path_write_lock(V9fsState *s)
  340. {
  341. if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
  342. qemu_co_rwlock_wrlock(&s->rename_lock);
  343. }
  344. }
  345. static inline void v9fs_path_read_lock(V9fsState *s)
  346. {
  347. if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
  348. qemu_co_rwlock_rdlock(&s->rename_lock);
  349. }
  350. }
  351. static inline void v9fs_path_unlock(V9fsState *s)
  352. {
  353. if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
  354. qemu_co_rwlock_unlock(&s->rename_lock);
  355. }
  356. }
  357. static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
  358. {
  359. return pdu->cancelled;
  360. }
  361. void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);
  362. void v9fs_path_init(V9fsPath *path);
  363. void v9fs_path_free(V9fsPath *path);
  364. void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
  365. void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src);
  366. int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
  367. const char *name, V9fsPath *path);
  368. int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
  369. Error **errp);
  370. void v9fs_device_unrealize_common(V9fsState *s, Error **errp);
  371. V9fsPDU *pdu_alloc(V9fsState *s);
  372. void pdu_free(V9fsPDU *pdu);
  373. void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr);
  374. void v9fs_reset(V9fsState *s);
  375. struct V9fsTransport {
  376. ssize_t (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
  377. va_list ap);
  378. ssize_t (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
  379. va_list ap);
  380. void (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
  381. unsigned int *pniov, size_t size);
  382. void (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
  383. unsigned int *pniov, size_t size);
  384. void (*push_and_notify)(V9fsPDU *pdu);
  385. };
  386. #endif