9p.h 5.2 KB

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