-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path9p.go
395 lines (319 loc) · 7.7 KB
/
9p.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package ninep
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"sync"
)
var errConnShutdown = errors.New("connection shutdown")
type msgHeader struct {
size uint32
msgType uint8
tag uint16
}
func (h *msgHeader) serialize() (out [7]byte) {
enc := binary.LittleEndian
enc.PutUint32(out[0:4], h.size)
out[4] = byte(h.msgType)
enc.PutUint16(out[5:7], h.tag)
return out
}
func (h *msgHeader) readerFrom(r io.Reader) io.Reader {
hdrBuf := h.serialize()
hdrReader := bytes.NewBuffer(hdrBuf[:])
bodyReader := io.LimitReader(r, int64(h.size-7))
return io.MultiReader(hdrReader, bodyReader)
}
type callback func(d msgHeader)
// ClientConn represents a connection to a 9p server.
type ClientConn struct {
tags chan uint16
wmux sync.Mutex // Write mutex.
conn io.ReadWriteCloser
// Callbacks that get called when a message for the given tag is read.
rrmux sync.Mutex // Mutex for reqReaders.
reqReaders map[uint16]callback
// Connection preferences
msize uint32
// Shutdown helpers
cancel func(error)
wg sync.WaitGroup
// Thread-safe pool of FIDs to use
fidPool fidPool
}
func readHeader(r io.Reader) (hdr msgHeader, err error) {
var buf [7]byte
_, err = io.ReadFull(r, buf[:])
if err != nil {
return
}
return msgHeader{
size: binary.LittleEndian.Uint32(buf[0:4]),
msgType: uint8(buf[4]),
tag: binary.LittleEndian.Uint16(buf[5:7]),
}, nil
}
// run runs the background reader goroutine which dispatches requests.
func (c *ClientConn) run(ctx context.Context) error {
// TODO: The context cancelation here is poor. We should be
// able to return immediately, not only after reading the next
// message header.
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
hdr, err := readHeader(c.conn)
// TODO: Use limit reader
if err != nil {
if context.Cause(ctx) == errConnShutdown {
return nil
}
return fmt.Errorf("peek error when expecting next message: %w", err)
}
c.getReqReader(hdr.tag)(hdr) // blocking
}
}
func (c *ClientConn) getReqReader(tag uint16) callback {
c.rrmux.Lock()
defer c.rrmux.Unlock()
rr, ok := c.reqReaders[tag]
if !ok {
// Skip message and log, nothing is registered for the tag.
return func(hdr msgHeader) {
// TODO: handle errors correctly
err := skip(c.conn, int(hdr.size-7))
if err != nil {
return
}
}
}
return rr
}
func (c *ClientConn) setReqReader(tag uint16, rr callback) {
c.rrmux.Lock()
defer c.rrmux.Unlock()
c.reqReaders[tag] = rr
}
func (c *ClientConn) clearReqReader(tag uint16) {
c.rrmux.Lock()
defer c.rrmux.Unlock()
delete(c.reqReaders, tag)
}
// Close closes the 9p connection.
func (c *ClientConn) Close() error {
if c.cancel == nil {
return nil
}
defer c.wg.Wait()
c.cancel(errConnShutdown)
c.cancel = nil
return c.conn.Close()
}
type tagHandle struct {
tag uint16
// Reader run loop sends a msg header for that tag if found.
readyToRead chan msgHeader
// The handling function replies back to the reader run loop
// through this channel.
doneReading chan struct{}
// Parent ClientConn
conn *ClientConn
}
func (h *tagHandle) awaitHdr(ctx context.Context) (msgHeader, error) {
select {
case hdr := <-h.readyToRead:
return hdr, nil
case <-ctx.Done():
return msgHeader{}, ctx.Err()
}
}
// Await the response for the given tag. On success, returns a reader
// for the response message (bounded to size). Returns ctx.Err() on
// early cancelation.
func (h *tagHandle) await(ctx context.Context) (io.Reader, error) {
hdr, err := h.awaitHdr(ctx)
if err != nil {
return nil, err
}
return hdr.readerFrom(h.conn.conn), nil
}
func (c *ClientConn) acquireTag() *tagHandle {
h := &tagHandle{
conn: c,
tag: <-c.tags,
readyToRead: make(chan msgHeader),
doneReading: make(chan struct{}),
}
c.setReqReader(h.tag, func(hdr msgHeader) {
// Invoked by reader run loop to read the given message.
h.readyToRead <- hdr
<-h.doneReading
})
return h
}
func (c *ClientConn) releaseTag(h *tagHandle) {
close(h.doneReading)
c.clearReqReader(h.tag)
c.tags <- h.tag
}
// Read from an open fid.
//
// offset indicates the offset into the file where to read.
// buf is the buffer to read into and may not be larger than
// the fid's iounit as returned by Open().
func (c *ClientConn) Read(ctx context.Context, fid uint32, offset uint64, buf []byte) (n uint32, err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTread(c.conn, tag.tag, fid, offset, uint32(len(buf)))
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(ctx)
if err != nil {
c.Flush(tag.tag)
return
}
return readRread(r, buf)
}
func (c *ClientConn) Write(ctx context.Context, fid uint32, offset uint64, data []byte) (n uint32, err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTwrite(c.conn, tag.tag, fid, offset, data)
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(ctx)
if err != nil {
c.Flush(tag.tag)
return
}
return readRwrite(r)
}
func (c *ClientConn) Walk(ctx context.Context, fid, newfid uint32, wname []string) (qids []QID, err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTwalk(c.conn, tag.tag, fid, newfid, wname)
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(ctx)
if err != nil {
c.Flush(tag.tag)
return
}
return readRwalk(r)
}
func (c *ClientConn) Stat(ctx context.Context, fid uint32) (stat Stat, err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTstat(c.conn, tag.tag, fid)
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(ctx)
if err != nil {
c.Flush(tag.tag)
return
}
return readRstat(r)
}
// Modes for opening and creating files, as defined in open(9p).
const (
ORead = 0x0
OWrite = 0x1
ORdWr = 0x2
OExec = 0x3
OTrunc = 0x10 // truncate
ORClose = 0x40 // delete on clunk
)
func (c *ClientConn) Open(ctx context.Context, fid uint32, mode uint8) (qid QID, iounit uint32, err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTopen(c.conn, tag.tag, fid, mode)
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(ctx)
if err != nil {
c.Flush(tag.tag)
return
}
return readRopen(r)
}
func (c *ClientConn) Clunk(ctx context.Context, fid uint32) (err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTclunk(c.conn, tag.tag, fid)
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(ctx)
if err != nil {
c.Flush(tag.tag)
return
}
return readRclunk(r)
}
// TODO: Do callers need to check the error?
func (c *ClientConn) Flush(oldtag uint16) (err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTflush(c.conn, tag.tag, oldtag)
c.wmux.Unlock()
if err != nil {
return
}
// Note: This may not error. Servers must repond to flush.
r, _ := tag.await(context.Background())
return readRflush(r)
}
func (c *ClientConn) Attach(ctx context.Context, fid uint32, afid uint32, uname string, aname string) (qid QID, err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTattach(c.conn, tag.tag, fid, afid, uname, aname)
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(context.Background())
if err != nil {
c.Flush(tag.tag)
return
}
return readRattach(r)
}
func (c *ClientConn) Auth(ctx context.Context, afid uint32, uname, aname string) (qid QID, err error) {
tag := c.acquireTag()
defer c.releaseTag(tag)
c.wmux.Lock()
err = writeTauth(c.conn, tag.tag, afid, uname, aname)
c.wmux.Unlock()
if err != nil {
return
}
r, err := tag.await(context.Background())
if err != nil {
c.Flush(tag.tag)
return
}
return readRauth(r)
}