9p.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #pragma src "/sys/src/lib9p"
  2. #pragma lib "lib9p.a"
  3. /*
  4. * Maps from uint32_ts to void*s.
  5. */
  6. typedef struct Intmap Intmap;
  7. #pragma incomplete Intmap
  8. Intmap* allocmap(void (*inc)(void*));
  9. void freemap(Intmap*, void (*destroy)(void*));
  10. void* lookupkey(Intmap*, uint32_t);
  11. void* insertkey(Intmap*, uint32_t, void*);
  12. int caninsertkey(Intmap*, uint32_t, void*);
  13. void* deletekey(Intmap*, uint32_t);
  14. /*
  15. * Fid and Request structures.
  16. */
  17. typedef struct Fid Fid;
  18. typedef struct Req Req;
  19. typedef struct Fidpool Fidpool;
  20. typedef struct Reqpool Reqpool;
  21. typedef struct File File;
  22. typedef struct Filelist Filelist;
  23. typedef struct Tree Tree;
  24. typedef struct Readdir Readdir;
  25. typedef struct Srv Srv;
  26. typedef struct Reqqueue Reqqueue;
  27. typedef struct Queueelem Queueelem;
  28. #pragma incomplete Filelist
  29. #pragma incomplete Readdir
  30. struct Queueelem
  31. {
  32. Queueelem *prev, *next;
  33. void (*f)(Req *);
  34. };
  35. struct Reqqueue
  36. {
  37. QLock ql;
  38. Rendez r;
  39. Queueelem;
  40. int pid, flush;
  41. Req *cur;
  42. };
  43. struct Fid
  44. {
  45. uint32_t fid;
  46. char omode; /* -1 = not open */
  47. File* file;
  48. char* uid;
  49. Qid qid;
  50. void* aux;
  51. /* below is implementation-specific; don't use */
  52. Readdir* rdir;
  53. Ref ref;
  54. Fidpool* pool;
  55. int64_t diroffset;
  56. int32_t dirindex;
  57. };
  58. struct Req
  59. {
  60. uint32_t tag;
  61. void* aux;
  62. Fcall ifcall;
  63. Fcall ofcall;
  64. Dir d;
  65. Req* oldreq;
  66. Fid* fid;
  67. Fid* afid;
  68. Fid* newfid;
  69. Srv* srv;
  70. Queueelem qu;
  71. /* below is implementation-specific; don't use */
  72. QLock lk;
  73. Ref ref;
  74. Reqpool* pool;
  75. uint8_t* buf;
  76. uint8_t type;
  77. uint8_t responded;
  78. char* error;
  79. void* rbuf;
  80. Req** flush;
  81. int nflush;
  82. };
  83. /*
  84. * Pools to maintain Fid <-> fid and Req <-> tag maps.
  85. */
  86. struct Fidpool {
  87. Intmap *map;
  88. void (*destroy)(Fid*);
  89. Srv *srv;
  90. };
  91. struct Reqpool {
  92. Intmap *map;
  93. void (*destroy)(Req*);
  94. Srv *srv;
  95. };
  96. Fidpool* allocfidpool(void (*destroy)(Fid*));
  97. void freefidpool(Fidpool*);
  98. Fid* allocfid(Fidpool*, uint32_t);
  99. Fid* lookupfid(Fidpool*, uint32_t);
  100. void closefid(Fid*);
  101. Fid* removefid(Fidpool*, uint32_t);
  102. Reqpool* allocreqpool(void (*destroy)(Req*));
  103. void freereqpool(Reqpool*);
  104. Req* allocreq(Reqpool*, uint32_t);
  105. Req* lookupreq(Reqpool*, uint32_t);
  106. void closereq(Req*);
  107. Req* removereq(Reqpool*, uint32_t);
  108. typedef int Dirgen(int, Dir*, void*);
  109. void dirread9p(Req*, Dirgen*, void*);
  110. /*
  111. * File trees.
  112. */
  113. struct File {
  114. Ref ref;
  115. Dir;
  116. File *parent;
  117. void *aux;
  118. /* below is implementation-specific; don't use */
  119. RWLock rwl;
  120. Filelist *filelist;
  121. Tree *tree;
  122. int nchild;
  123. int allocd;
  124. int nxchild;
  125. Ref readers;
  126. };
  127. struct Tree {
  128. File *root;
  129. void (*destroy)(File *file);
  130. /* below is implementation-specific; don't use */
  131. Lock genlock;
  132. uint32_t qidgen;
  133. uint32_t dirqidgen;
  134. };
  135. Tree* alloctree(char*, char*, uint32_t, void(*destroy)(File*));
  136. void freetree(Tree*);
  137. File* createfile(File*, char*, char*, uint32_t, void*);
  138. int removefile(File*);
  139. void closefile(File*);
  140. File* walkfile(File*, char*);
  141. Readdir* opendirfile(File*);
  142. int32_t readdirfile(Readdir*, uint8_t*, int32_t);
  143. void closedirfile(Readdir*);
  144. /*
  145. * Kernel-style command parser
  146. */
  147. typedef struct Cmdbuf Cmdbuf;
  148. typedef struct Cmdtab Cmdtab;
  149. Cmdbuf* parsecmd(char *a, int n);
  150. void respondcmderror(Req*, Cmdbuf*, char*, ...);
  151. Cmdtab* lookupcmd(Cmdbuf*, Cmdtab*, int);
  152. #pragma varargck argpos respondcmderr 3
  153. struct Cmdbuf
  154. {
  155. char *buf;
  156. char **f;
  157. int nf;
  158. };
  159. struct Cmdtab
  160. {
  161. int index; /* used by client to switch on result */
  162. char *cmd; /* command name */
  163. int narg; /* expected #args; 0 ==> variadic */
  164. };
  165. /*
  166. * File service loop.
  167. */
  168. struct Srv {
  169. Tree* tree;
  170. void (*destroyfid)(Fid*);
  171. void (*destroyreq)(Req*);
  172. void (*start)(Srv*);
  173. void (*end)(Srv*);
  174. void* aux;
  175. void (*attach)(Req*);
  176. void (*auth)(Req*);
  177. void (*open)(Req*);
  178. void (*create)(Req*);
  179. void (*read)(Req*);
  180. void (*write)(Req*);
  181. void (*remove)(Req*);
  182. void (*flush)(Req*);
  183. void (*stat)(Req*);
  184. void (*wstat)(Req*);
  185. void (*walk)(Req*);
  186. char* (*clone)(Fid*, Fid*);
  187. char* (*walk1)(Fid*, char*, Qid*);
  188. int infd;
  189. int outfd;
  190. int nopipe;
  191. int srvfd;
  192. int leavefdsopen; /* magic for acme win */
  193. char* keyspec;
  194. /* below is implementation-specific; don't use */
  195. Fidpool* fpool;
  196. Reqpool* rpool;
  197. uint32_t msize;
  198. uint8_t* rbuf;
  199. QLock rlock;
  200. uint8_t* wbuf;
  201. QLock wlock;
  202. char* addr;
  203. QLock slock;
  204. Ref sref; /* srvwork procs */
  205. Ref rref; /* requests in flight */
  206. void (*free)(Srv*);
  207. };
  208. void srv(Srv*);
  209. void postmountsrv(Srv*, char*, char*, int);
  210. void _postmountsrv(Srv*, char*, char*, int);
  211. void postsharesrv(Srv*, char*, char*, char*);
  212. void _postsharesrv(Srv*, char*, char*, char*);
  213. void listensrv(Srv*, char*);
  214. void _listensrv(Srv*, char*);
  215. int postfd(char*, int);
  216. int sharefd(char*, char*, int);
  217. int chatty9p;
  218. void respond(Req*, char*);
  219. void responderror(Req*);
  220. void threadpostmountsrv(Srv*, char*, char*, int);
  221. void threadpostsharesrv(Srv*, char*, char*, char*);
  222. void threadlistensrv(Srv *s, char *addr);
  223. /*
  224. * Helper. Assumes user is same as group.
  225. */
  226. int hasperm(File*, char*, int);
  227. void* emalloc9p(uint32_t);
  228. void* erealloc9p(void*, uint32_t);
  229. char* estrdup9p(char*);
  230. enum {
  231. OMASK = 3
  232. };
  233. void readstr(Req*, char*);
  234. void readbuf(Req*, void*, int32_t);
  235. void walkandclone(Req*, char*(*walk1)(Fid*,char*,void*),
  236. char*(*clone)(Fid*,Fid*,void*), void*);
  237. void auth9p(Req*);
  238. void authread(Req*);
  239. void authwrite(Req*);
  240. void authdestroy(Fid*);
  241. int authattach(Req*);
  242. extern void (*_forker)(void (*)(void*), void*, int);
  243. void srvacquire(Srv *);
  244. void srvrelease(Srv *);
  245. Reqqueue* reqqueuecreate(void);
  246. void reqqueuepush(Reqqueue*, Req*, void (*)(Req *));
  247. void reqqueueflush(Reqqueue*, Req*);
  248. void reqqueuefree(Reqqueue*);