9p.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. /*
  98. * Minimum message size supported by this 9pfs server.
  99. *
  100. * A client establishes a session by sending a Tversion request along with a
  101. * 'msize' parameter which suggests the server a maximum message size ever to be
  102. * used for communication (for both requests and replies) between client and
  103. * server during that session. If client suggests a 'msize' smaller than this
  104. * value then session is denied by server with an error response.
  105. */
  106. #define P9_MIN_MSIZE 4096
  107. #define P9_NOTAG UINT16_MAX
  108. #define P9_NOFID UINT32_MAX
  109. #define P9_MAXWELEM 16
  110. #define FID_REFERENCED 0x1
  111. #define FID_NON_RECLAIMABLE 0x2
  112. static inline char *rpath(FsContext *ctx, const char *path)
  113. {
  114. return g_strdup_printf("%s/%s", ctx->fs_root, path);
  115. }
  116. /*
  117. * ample room for Twrite/Rread header
  118. * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
  119. */
  120. #define P9_IOHDRSZ 24
  121. typedef struct V9fsPDU V9fsPDU;
  122. typedef struct V9fsState V9fsState;
  123. typedef struct V9fsTransport V9fsTransport;
  124. typedef struct {
  125. uint32_t size_le;
  126. uint8_t id;
  127. uint16_t tag_le;
  128. } QEMU_PACKED P9MsgHeader;
  129. /* According to the specification, 9p messages start with a 7-byte header.
  130. * Since most of the code uses this header size in literal form, we must be
  131. * sure this is indeed the case.
  132. */
  133. QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader) != 7);
  134. struct V9fsPDU {
  135. uint32_t size;
  136. uint16_t tag;
  137. uint8_t id;
  138. uint8_t cancelled;
  139. CoQueue complete;
  140. V9fsState *s;
  141. QLIST_ENTRY(V9fsPDU) next;
  142. uint32_t idx;
  143. };
  144. /* FIXME
  145. * 1) change user needs to set groups and stuff
  146. */
  147. #define MAX_REQ 128
  148. #define MAX_TAG_LEN 32
  149. #define BUG_ON(cond) assert(!(cond))
  150. typedef struct V9fsFidState V9fsFidState;
  151. enum {
  152. P9_FID_NONE = 0,
  153. P9_FID_FILE,
  154. P9_FID_DIR,
  155. P9_FID_XATTR,
  156. };
  157. typedef struct V9fsConf
  158. {
  159. /* tag name for the device */
  160. char *tag;
  161. char *fsdev_id;
  162. } V9fsConf;
  163. /* 9p2000.L xattr flags (matches Linux values) */
  164. #define P9_XATTR_CREATE 1
  165. #define P9_XATTR_REPLACE 2
  166. typedef struct V9fsXattr
  167. {
  168. uint64_t copied_len;
  169. uint64_t len;
  170. void *value;
  171. V9fsString name;
  172. int flags;
  173. bool xattrwalk_fid;
  174. } V9fsXattr;
  175. typedef struct V9fsDir {
  176. DIR *stream;
  177. P9ProtoVersion proto_version;
  178. /* readdir mutex type used for 9P2000.u protocol variant */
  179. CoMutex readdir_mutex_u;
  180. /* readdir mutex type used for 9P2000.L protocol variant */
  181. QemuMutex readdir_mutex_L;
  182. } V9fsDir;
  183. static inline void coroutine_fn v9fs_readdir_lock(V9fsDir *dir)
  184. {
  185. if (dir->proto_version == V9FS_PROTO_2000U) {
  186. qemu_co_mutex_lock(&dir->readdir_mutex_u);
  187. } else {
  188. qemu_mutex_lock(&dir->readdir_mutex_L);
  189. }
  190. }
  191. static inline void coroutine_fn v9fs_readdir_unlock(V9fsDir *dir)
  192. {
  193. if (dir->proto_version == V9FS_PROTO_2000U) {
  194. qemu_co_mutex_unlock(&dir->readdir_mutex_u);
  195. } else {
  196. qemu_mutex_unlock(&dir->readdir_mutex_L);
  197. }
  198. }
  199. static inline void v9fs_readdir_init(P9ProtoVersion proto_version, V9fsDir *dir)
  200. {
  201. dir->proto_version = proto_version;
  202. if (proto_version == V9FS_PROTO_2000U) {
  203. qemu_co_mutex_init(&dir->readdir_mutex_u);
  204. } else {
  205. qemu_mutex_init(&dir->readdir_mutex_L);
  206. }
  207. }
  208. /*
  209. * Type for 9p fs drivers' (a.k.a. 9p backends) result of readdir requests,
  210. * which is a chained list of directory entries.
  211. */
  212. typedef struct V9fsDirEnt {
  213. /* mandatory (must not be NULL) information for all readdir requests */
  214. struct dirent *dent;
  215. /*
  216. * optional (may be NULL): A full stat of each directory entry is just
  217. * done if explicitly told to fs driver.
  218. */
  219. struct stat *st;
  220. /*
  221. * instead of an array, directory entries are always returned as
  222. * chained list, that's because the amount of entries retrieved by fs
  223. * drivers is dependent on the individual entries' name (since response
  224. * messages are size limited), so the final amount cannot be estimated
  225. * before hand
  226. */
  227. struct V9fsDirEnt *next;
  228. } V9fsDirEnt;
  229. /*
  230. * Filled by fs driver on open and other
  231. * calls.
  232. */
  233. union V9fsFidOpenState {
  234. int fd;
  235. V9fsDir dir;
  236. V9fsXattr xattr;
  237. /*
  238. * private pointer for fs drivers, that
  239. * have its own internal representation of
  240. * open files.
  241. */
  242. void *private;
  243. };
  244. struct V9fsFidState {
  245. int fid_type;
  246. int32_t fid;
  247. V9fsPath path;
  248. V9fsFidOpenState fs;
  249. V9fsFidOpenState fs_reclaim;
  250. int flags;
  251. int open_flags;
  252. uid_t uid;
  253. int ref;
  254. bool clunked;
  255. QSLIST_ENTRY(V9fsFidState) reclaim_next;
  256. };
  257. typedef enum AffixType_t {
  258. AffixType_Prefix,
  259. AffixType_Suffix, /* A.k.a. postfix. */
  260. } AffixType_t;
  261. /*
  262. * Unique affix of variable length.
  263. *
  264. * An affix is (currently) either a suffix or a prefix, which is either
  265. * going to be prepended (prefix) or appended (suffix) with some other
  266. * number for the goal to generate unique numbers. Accordingly the
  267. * suffixes (or prefixes) we generate @b must all have the mathematical
  268. * property of being suffix-free (or prefix-free in case of prefixes)
  269. * so that no matter what number we concatenate the affix with, that we
  270. * always reliably get unique numbers as result after concatenation.
  271. */
  272. typedef struct VariLenAffix {
  273. AffixType_t type; /* Whether this affix is a suffix or a prefix. */
  274. uint64_t value; /* Actual numerical value of this affix. */
  275. /*
  276. * Length of the affix, that is how many (of the lowest) bits of ``value``
  277. * must be used for appending/prepending this affix to its final resulting,
  278. * unique number.
  279. */
  280. int bits;
  281. } VariLenAffix;
  282. /* See qid_inode_prefix_hash_bits(). */
  283. typedef struct {
  284. dev_t dev; /* FS device on host. */
  285. /*
  286. * How many (high) bits of the original inode number shall be used for
  287. * hashing.
  288. */
  289. int prefix_bits;
  290. } QpdEntry;
  291. /* QID path prefix entry, see stat_to_qid */
  292. typedef struct {
  293. dev_t dev;
  294. uint16_t ino_prefix;
  295. uint32_t qp_affix_index;
  296. VariLenAffix qp_affix;
  297. } QppEntry;
  298. /* QID path full entry, as above */
  299. typedef struct {
  300. dev_t dev;
  301. ino_t ino;
  302. uint64_t path;
  303. } QpfEntry;
  304. struct V9fsState {
  305. QLIST_HEAD(, V9fsPDU) free_list;
  306. QLIST_HEAD(, V9fsPDU) active_list;
  307. GHashTable *fids;
  308. FileOperations *ops;
  309. FsContext ctx;
  310. char *tag;
  311. P9ProtoVersion proto_version;
  312. int32_t msize;
  313. V9fsPDU pdus[MAX_REQ];
  314. const V9fsTransport *transport;
  315. /*
  316. * lock ensuring atomic path update
  317. * on rename.
  318. */
  319. CoRwlock rename_lock;
  320. int32_t root_fid;
  321. Error *migration_blocker;
  322. V9fsConf fsconf;
  323. struct stat root_st;
  324. dev_t dev_id;
  325. struct qht qpd_table;
  326. struct qht qpp_table;
  327. struct qht qpf_table;
  328. uint64_t qp_ndevices; /* Amount of entries in qpd_table. */
  329. uint16_t qp_affix_next;
  330. uint64_t qp_fullpath_next;
  331. bool reclaiming;
  332. };
  333. /* 9p2000.L open flags */
  334. #define P9_DOTL_RDONLY 00000000
  335. #define P9_DOTL_WRONLY 00000001
  336. #define P9_DOTL_RDWR 00000002
  337. #define P9_DOTL_NOACCESS 00000003
  338. #define P9_DOTL_CREATE 00000100
  339. #define P9_DOTL_EXCL 00000200
  340. #define P9_DOTL_NOCTTY 00000400
  341. #define P9_DOTL_TRUNC 00001000
  342. #define P9_DOTL_APPEND 00002000
  343. #define P9_DOTL_NONBLOCK 00004000
  344. #define P9_DOTL_DSYNC 00010000
  345. #define P9_DOTL_FASYNC 00020000
  346. #define P9_DOTL_DIRECT 00040000
  347. #define P9_DOTL_LARGEFILE 00100000
  348. #define P9_DOTL_DIRECTORY 00200000
  349. #define P9_DOTL_NOFOLLOW 00400000
  350. #define P9_DOTL_NOATIME 01000000
  351. #define P9_DOTL_CLOEXEC 02000000
  352. #define P9_DOTL_SYNC 04000000
  353. /* 9p2000.L at flags */
  354. #define P9_DOTL_AT_REMOVEDIR 0x200
  355. /* 9P2000.L lock type */
  356. #define P9_LOCK_TYPE_RDLCK 0
  357. #define P9_LOCK_TYPE_WRLCK 1
  358. #define P9_LOCK_TYPE_UNLCK 2
  359. #define P9_LOCK_SUCCESS 0
  360. #define P9_LOCK_BLOCKED 1
  361. #define P9_LOCK_ERROR 2
  362. #define P9_LOCK_GRACE 3
  363. #define P9_LOCK_FLAGS_BLOCK 1
  364. #define P9_LOCK_FLAGS_RECLAIM 2
  365. typedef struct V9fsFlock
  366. {
  367. uint8_t type;
  368. uint32_t flags;
  369. uint64_t start; /* absolute offset */
  370. uint64_t length;
  371. uint32_t proc_id;
  372. V9fsString client_id;
  373. } V9fsFlock;
  374. typedef struct V9fsGetlock
  375. {
  376. uint8_t type;
  377. uint64_t start; /* absolute offset */
  378. uint64_t length;
  379. uint32_t proc_id;
  380. V9fsString client_id;
  381. } V9fsGetlock;
  382. extern int open_fd_hw;
  383. extern int total_open_fd;
  384. static inline void coroutine_fn
  385. v9fs_path_write_lock(V9fsState *s)
  386. {
  387. if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
  388. qemu_co_rwlock_wrlock(&s->rename_lock);
  389. }
  390. }
  391. static inline void coroutine_fn
  392. v9fs_path_read_lock(V9fsState *s)
  393. {
  394. if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
  395. qemu_co_rwlock_rdlock(&s->rename_lock);
  396. }
  397. }
  398. static inline void coroutine_fn
  399. v9fs_path_unlock(V9fsState *s)
  400. {
  401. if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
  402. qemu_co_rwlock_unlock(&s->rename_lock);
  403. }
  404. }
  405. static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
  406. {
  407. return pdu->cancelled;
  408. }
  409. void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);
  410. void v9fs_path_init(V9fsPath *path);
  411. void v9fs_path_free(V9fsPath *path);
  412. void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
  413. void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src);
  414. size_t v9fs_readdir_response_size(V9fsString *name);
  415. int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
  416. const char *name, V9fsPath *path);
  417. int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
  418. Error **errp);
  419. void v9fs_device_unrealize_common(V9fsState *s);
  420. V9fsPDU *pdu_alloc(V9fsState *s);
  421. void pdu_free(V9fsPDU *pdu);
  422. void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr);
  423. void v9fs_reset(V9fsState *s);
  424. struct V9fsTransport {
  425. ssize_t (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
  426. va_list ap);
  427. ssize_t (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
  428. va_list ap);
  429. void (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
  430. unsigned int *pniov, size_t size);
  431. void (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
  432. unsigned int *pniov, size_t size);
  433. void (*push_and_notify)(V9fsPDU *pdu);
  434. };
  435. #endif