9p.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Maps from ulongs to void*s.
  11. */
  12. typedef struct Intmap Intmap;
  13. Intmap* allocmap(void (*inc)(void*));
  14. void freemap(Intmap*, void (*destroy)(void*));
  15. void* lookupkey(Intmap*, uint32_t);
  16. void* insertkey(Intmap*, uint32_t, void*);
  17. int caninsertkey(Intmap*, uint32_t, void*);
  18. void* deletekey(Intmap*, uint32_t);
  19. /*
  20. * Fid and Request structures.
  21. */
  22. typedef struct Fid Fid;
  23. typedef struct Req Req;
  24. typedef struct Fidpool Fidpool;
  25. typedef struct Reqpool Reqpool;
  26. typedef struct File File;
  27. typedef struct Filelist Filelist;
  28. typedef struct Tree Tree;
  29. typedef struct Readdir Readdir;
  30. typedef struct Srv Srv;
  31. struct Fid
  32. {
  33. uint32_t fid;
  34. char omode; /* -1 = not open */
  35. File* file;
  36. char* uid;
  37. Qid qid;
  38. void* aux;
  39. /* below is implementation-specific; don't use */
  40. Readdir* rdir;
  41. Ref ref;
  42. Fidpool* pool;
  43. int64_t diroffset;
  44. int32_t dirindex;
  45. };
  46. struct Req
  47. {
  48. uint32_t tag;
  49. void* aux;
  50. Fcall ifcall;
  51. Fcall ofcall;
  52. Dir d;
  53. Req* oldreq;
  54. Fid* fid;
  55. Fid* afid;
  56. Fid* newfid;
  57. Srv* srv;
  58. /* below is implementation-specific; don't use */
  59. QLock lk;
  60. Ref ref;
  61. Reqpool* pool;
  62. uint8_t* buf;
  63. uint8_t type;
  64. uint8_t responded;
  65. char* error;
  66. void* rbuf;
  67. Req** flush;
  68. int nflush;
  69. };
  70. /*
  71. * Pools to maintain Fid <-> fid and Req <-> tag maps.
  72. */
  73. struct Fidpool {
  74. Intmap *map;
  75. void (*destroy)(Fid*);
  76. Srv *srv;
  77. };
  78. struct Reqpool {
  79. Intmap *map;
  80. void (*destroy)(Req*);
  81. Srv *srv;
  82. };
  83. Fidpool* allocfidpool(void (*destroy)(Fid*));
  84. void freefidpool(Fidpool*);
  85. Fid* allocfid(Fidpool*, uint32_t);
  86. Fid* lookupfid(Fidpool*, uint32_t);
  87. void closefid(Fid*);
  88. Fid* removefid(Fidpool*, uint32_t);
  89. Reqpool* allocreqpool(void (*destroy)(Req*));
  90. void freereqpool(Reqpool*);
  91. Req* allocreq(Reqpool*, uint32_t);
  92. Req* lookupreq(Reqpool*, uint32_t);
  93. void closereq(Req*);
  94. Req* removereq(Reqpool*, uint32_t);
  95. typedef int Dirgen(int, Dir*, void*);
  96. void dirread9p(Req*, Dirgen*, void*);
  97. /*
  98. * File trees.
  99. */
  100. struct File {
  101. Ref Ref;
  102. Dir Dir;
  103. File *parent;
  104. void *aux;
  105. /* below is implementation-specific; don't use */
  106. RWLock RWLock;
  107. Filelist *filelist;
  108. Tree *tree;
  109. int nchild;
  110. int allocd;
  111. int nxchild;
  112. Ref readers;
  113. };
  114. struct Tree {
  115. File *root;
  116. void (*destroy)(File *file);
  117. /* below is implementation-specific; don't use */
  118. Lock genlock;
  119. uint32_t qidgen;
  120. uint32_t dirqidgen;
  121. };
  122. Tree* alloctree(char*, char*, uint32_t, void(*destroy)(File*));
  123. void freetree(Tree*);
  124. File* createfile(File*, char*, char*, uint32_t, void*);
  125. int removefile(File*);
  126. void closefile(File*);
  127. File* walkfile(File*, char*);
  128. Readdir* opendirfile(File*);
  129. int32_t readdirfile(Readdir*, uint8_t*, int32_t);
  130. void closedirfile(Readdir*);
  131. /*
  132. * Kernel-style command parser
  133. */
  134. typedef struct Cmdbuf Cmdbuf;
  135. typedef struct Cmdtab Cmdtab;
  136. Cmdbuf* parsecmd(char *a, int n);
  137. void respondcmderror(Req*, Cmdbuf*, char*, ...);
  138. Cmdtab* lookupcmd(Cmdbuf*, Cmdtab*, int);
  139. struct Cmdbuf
  140. {
  141. char *buf;
  142. char **f;
  143. int nf;
  144. };
  145. struct Cmdtab
  146. {
  147. int index; /* used by client to switch on result */
  148. char *cmd; /* command name */
  149. int narg; /* expected #args; 0 ==> variadic */
  150. };
  151. /*
  152. * File service loop.
  153. */
  154. struct Srv {
  155. Tree* tree;
  156. void (*destroyfid)(Fid*);
  157. void (*destroyreq)(Req*);
  158. void (*end)(Srv*);
  159. void* aux;
  160. void (*attach)(Req*);
  161. void (*auth)(Req*);
  162. void (*open)(Req*);
  163. void (*create)(Req*);
  164. void (*read)(Req*);
  165. void (*write)(Req*);
  166. void (*remove)(Req*);
  167. void (*flush)(Req*);
  168. void (*stat)(Req*);
  169. void (*wstat)(Req*);
  170. void (*walk)(Req*);
  171. char* (*clone)(Fid*, Fid*);
  172. char* (*walk1)(Fid*, char*, Qid*);
  173. int infd;
  174. int outfd;
  175. int nopipe;
  176. int srvfd;
  177. int leavefdsopen; /* magic for acme win */
  178. char* keyspec;
  179. /* below is implementation-specific; don't use */
  180. Fidpool* fpool;
  181. Reqpool* rpool;
  182. uint msize;
  183. uint8_t* rbuf;
  184. QLock rlock;
  185. uint8_t* wbuf;
  186. QLock wlock;
  187. char* addr;
  188. };
  189. void srv(Srv*);
  190. void postmountsrv(Srv*, char*, char*, int);
  191. void _postmountsrv(Srv*, char*, char*, int);
  192. void listensrv(Srv*, char*);
  193. void _listensrv(Srv*, char*);
  194. int postfd(char*, int);
  195. int chatty9p;
  196. void respond(Req*, char*);
  197. void responderror(Req*);
  198. void threadpostmountsrv(Srv*, char*, char*, int);
  199. void threadlistensrv(Srv *s, char *addr);
  200. /*
  201. * Helper. Assumes user is same as group.
  202. */
  203. int hasperm(File*, char*, int);
  204. void* emalloc9p(uint32_t);
  205. void* erealloc9p(void*, uint32_t);
  206. char* estrdup9p(char*);
  207. enum {
  208. OMASK = 3
  209. };
  210. void readstr(Req*, char*);
  211. void readbuf(Req*, void*, int32_t);
  212. void walkandclone(Req*, char*(*walk1)(Fid*,char*,void*),
  213. char*(*clone)(Fid*,Fid*,void*), void*);
  214. void auth9p(Req*);
  215. void authread(Req*);
  216. void authwrite(Req*);
  217. void authdestroy(Fid*);
  218. int authattach(Req*);
  219. extern void (*_forker)(void (*)(void*), void*, int);