Blob


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