/
union9p.go
416 lines (373 loc) · 11.4 KB
/
union9p.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"errors"
"io"
"os"
"reflect"
"syscall"
"github.com/hugelgupf/p9/p9"
)
// Bind is a single bind
// For a given Twalk, the walk []string will be compared
// to the string slice in the Twalk. If there is match,
// the mount is called with the complete Twalk []string.
type UnionMount struct {
walk []string
mount p9.File
}
// Union9P is a p9.Attacher.
type Union9P struct {
mounts []UnionMount
}
// Union9pFID implements p9.File.
// The only operations it need implement
// are WalkGetAttr, GetAttr, Open, Walk and Readdir.
// The GetAttr is mostly a stub.
// The Open is required to properly support Readdir.
// Walk is used to walk to one of the underlying
// file systems. Readdir reads the union of the
// top level of all the underlying file systems,
// as in Plan 9. E.g., if the tables
// include home and a cpio, Readdir will return
// the top level of home and the cpio, including
// duplicates. There is no whiteout in this
// union file system.
type union9PFID struct {
u *Union9P
f p9.File
}
// NewUnionMount creates a new Union Mount from a
// []string and a p9.File.
func NewUnionMount(w []string, m p9.File) UnionMount {
return UnionMount{walk: w, mount: m}
}
// NewUnion9P returns a Union9P, properly initialized,
// from a []UnionMount. Each UnionMount has a []string that defines
// a walk path.
// The []string argument is matched to each walk path in the []UnionMount
// in turn. As in Plan 9, the first match is used; if the walk to that
// server fails, the code returns the error; it does not go any further.
//
// I.e., if /home and /home/rminnich are in the table, they need
// to be in the order
// /home/rminnich
// /home
// in the case that the second mount does not include /home/rminnich.
// (it could be from a different 9p server, for example).
// Having a UnionMount with an empty []string is allowed; this will match
// any walk []string and hence acts as a default.
//
// For example, in Sidecore, the code looks like this:
// home, err := NewCPU9P(...)
// container, err := NewCPIO9(...)
// m1 := NewUnionMount([]string{"/home"}, home)
// m2 := NewUnionMount([]string{}, container)
// u := NewUnion9P([]UnionMount{home, container})
// This ensures /home matches first, and the container CPIO matches the rest.
//
// If a default is not
// desired, callers should only use Union Mount structs with non-empty []string.
// Only one Mount with an empty walk slice should be used, as the search will
// always stop there.
// It is allowed to have multiple Mounts for a single p9.File.
// E.g, give a p9.File, f, once can:
// m1 := NewUnionMount([]string{"/etc", f)
// m2 := NewUnionMount([]string{"/bin", f)
// u := NewUnion9P([]UnionMount{m1, m2})
// and no matter what other directories exist in f, only /etc and /bin will match.
//
// Again, to add a default case, using, e.g., another p9.File, one might have
// m1 := NewUnionMount([]string{"/etc}", f)
// m2 := NewUnionMount([]string{"/bin}", f)
// mdefault := NewUnionMount([]string{""}, fi2)
// u := NewUnion9P([]UnionMount{m1, m2, mdefault})
func NewUnion9P(mounts []UnionMount) (*Union9P, error) {
// Index 0 is always the self pointer.
// It matches /
// Interesting that this is exactly
// how it is done in the Plan 9 bind table!
// Hand craft this one; it's kind of like
// PID 1 in Unix.
root := union9PFID{}
root.f = &root
u := &Union9P{
mounts: append([]UnionMount{UnionMount{walk: []string{"/"}, mount: &root}}, mounts...),
}
root.u = u
return u, nil
}
// Attach implements p9.Attacher.Attach.
func (u *Union9P) Attach() (p9.File, error) {
return &union9PFID{f: u.mounts[0].mount, u: u}, nil
}
var (
_ p9.File = &union9PFID{}
_ p9.Attacher = &Union9P{}
)
// WalkGetAttr implements File.WalkGetAttr.
func (u *union9PFID) WalkGetAttr(names []string) ([]p9.QID, p9.File, p9.AttrMask, p9.Attr, error) {
v("union9p:WalkGetAttr")
q, f, err := u.Walk(names)
if err != nil {
return nil, nil, p9.AttrMask{}, p9.Attr{}, err
}
v("walk to %q got %v", names, q)
valid := p9.AttrMask{
Mode: true,
UID: true,
GID: true,
NLink: true,
RDev: true,
Size: true,
Blocks: true,
ATime: true,
MTime: true,
CTime: true,
}
_, m, a, err := f.GetAttr(valid)
if err != nil {
return q, f, p9.AttrMask{}, p9.Attr{}, err
}
v("union9p: walkgetattr returns QID %v", q)
return q, f, m, a, err
}
// Walk implements p9.File.Walk.
func (u *union9PFID) Walk(names []string) ([]p9.QID, p9.File, error) {
v("union9p: walk(%q)", names)
if len(names) == 0 {
v("union9p:clonewalk")
return []p9.QID{p9.QID{Type: p9.TypeDir, Path: 1, Version: 0}}, &union9PFID{u: u.u, f: u.f}, nil
}
ix := -1
for x, bind := range u.u.mounts {
if x == 0 {
continue
}
v("union9p: bind.walk %q, names %q", bind.walk, names)
i := len(names)
if len(bind.walk) < i {
i = len(bind.walk)
}
v("union9p:Check if bind.Walk %q == names %q", bind.walk[:i], names[:i])
if !reflect.DeepEqual(bind.walk[:i], names[:i]) {
v("union9p:no match")
continue
}
ix = x
v("union9p:ix is %d", ix)
break
}
// this can happen if they fail to have a []string
// as the last entry.
if ix <= 0 {
return nil, nil, os.ErrNotExist
}
v("union9p:Walk to %q from %v", names, u.u.mounts[ix])
q, f, err := u.u.mounts[ix].mount.Walk(names)
v("union9p:return(%v, %v, %v", q, f, err)
return q, f, err
}
// FSync implements p9.File.FSync.
func (u *union9PFID) FSync() error {
v("union9p:fsync")
return nil
}
// Close implements p9.File.Close.
func (u *union9PFID) Close() error {
v("union9p:close")
return nil
}
// Open implements p9.File.Open.
// Basically a no op: nothing to do really.
func (u *union9PFID) Open(mode p9.OpenFlags) (p9.QID, uint32, error) {
v("union9p:open")
if mode.Mode() != p9.ReadOnly {
return p9.QID{}, 0, os.ErrPermission
}
return p9.QID{}, 0, nil
}
// Read implements p9.File.ReadAt.
func (u *union9PFID) ReadAt(p []byte, offset int64) (int, error) {
v("union9p:readat")
return -1, os.ErrPermission
}
// Write implements p9.File.WriteAt.
func (u *union9PFID) WriteAt(p []byte, offset int64) (int, error) {
v("union9p:writeat")
return -1, os.ErrPermission
}
// Create implements p9.File.Create.
func (u *union9PFID) Create(name string, mode p9.OpenFlags, permissions p9.FileMode, _ p9.UID, _ p9.GID) (p9.File, p9.QID, uint32, error) {
v("union9p:create")
return nil, p9.QID{Type: p9.TypeDir, Path: 0x09109, Version: 0x314}, 0555, os.ErrPermission
}
// Mkdir implements p9.File.Mkdir.
//
// Not properly implemented.
func (u *union9PFID) Mkdir(name string, permissions p9.FileMode, _ p9.UID, _ p9.GID) (p9.QID, error) {
v("union9p:mkdir")
return p9.QID{}, os.ErrPermission
}
// Symlink implements p9.File.Symlink.
//
// Not properly implemented.
func (u *union9PFID) Symlink(oldname string, newname string, _ p9.UID, _ p9.GID) (p9.QID, error) {
v("union9p:symlink")
return p9.QID{}, os.ErrPermission
}
// Link implements p9.File.Link.
func (u *union9PFID) Link(target p9.File, newname string) error {
v("union9p:link")
return os.ErrPermission
}
// Readdir implements p9.File.Readdir.
func (u *union9PFID) Readdir(offset uint64, count uint32) (p9.Dirents, error) {
v("union9p:readdir u %v", u)
v("union9p:readdir u %v", u.u)
v("union9p:readdir u %v", u.u.mounts)
var errs error
var all p9.Dirents
// There can only be on '.'. But that is the ONLY one we elide
var dot bool
for _, bind := range u.u.mounts[1:] {
_, dir, err := bind.mount.Walk([]string{})
if err != nil {
errs = errors.Join(errs, err)
continue
}
// Must open and close each time. But it's cheap.
if _, _, err := dir.Open(0); err != nil {
errs = errors.Join(errs, err)
continue
}
defer dir.Close()
d, err := dir.Readdir(offset, count)
if err != nil {
errs = errors.Join(errs, err)
}
v("union9p:readdir %v", dir)
for _, de := range d {
if de.Name == "." {
if dot {
continue
}
dot = true
}
all = append(all, de)
}
}
if offset >= uint64(len(all)) {
return nil, io.EOF
}
v("union9p:%q, errs %v", all, errs)
return all, errs
}
// Readlink implements p9.File.Readlink.
func (u *union9PFID) Readlink() (string, error) {
v("union9p:readlink")
return "", os.ErrPermission
}
// Flush implements p9.File.Flush.
func (u *union9PFID) Flush() error {
v("union9p:flush")
return nil
}
// Renamed implements p9.File.Renamed.
func (u *union9PFID) Renamed(parent p9.File, newName string) {
}
// UnlinkAt implements p9.File.UnlinkAt.
func (u *union9PFID) UnlinkAt(name string, flags uint32) error {
v("union9p:unlinkat")
return os.ErrPermission
}
// Mknod implements p9.File.Mknod.
func (*union9PFID) Mknod(name string, mode p9.FileMode, major uint32, minor uint32, _ p9.UID, _ p9.GID) (p9.QID, error) {
v("union9p:mknod")
return p9.QID{}, syscall.ENOSYS
}
// Rename implements p9.File.Rename.
func (*union9PFID) Rename(directory p9.File, name string) error {
v("union9p:rename")
return syscall.ENOSYS
}
// RenameAt implements p9.File.RenameAt.
func (u *union9PFID) RenameAt(oldName string, newDir p9.File, newName string) error {
v("union9p:renameat")
return syscall.ENOSYS
}
// StatFS implements p9.File.StatFS.
func (*union9PFID) StatFS() (p9.FSStat, error) {
v("union9p:statfs")
return p9.FSStat{}, syscall.ENOSYS
}
// SetAttr implements SetAttr.
func (u *union9PFID) SetAttr(mask p9.SetAttrMask, attr p9.SetAttr) error {
v("union9p:setattr")
return os.ErrPermission
}
// Lock implements lock by doing nothing.
func (u *union9PFID) Lock(pid int, locktype p9.LockType, flags p9.LockFlags, start, length uint64, client string) (p9.LockStatus, error) {
return 0, nil
}
// GetAttr implements p9.File.GetAttr.
func (u *union9PFID) GetAttr(req p9.AttrMask) (p9.QID, p9.AttrMask, p9.Attr, error) {
v("union9p: getattr")
attr := p9.Attr{
Mode: p9.FileMode(0777) | p9.ModeDirectory,
UID: p9.UID(0),
GID: p9.GID(0),
NLink: p9.NLink(1 + len(u.u.mounts)),
RDev: p9.Dev(0),
Size: uint64(0),
BlockSize: uint64(4096),
Blocks: uint64(0),
ATimeSeconds: uint64(0),
ATimeNanoSeconds: uint64(0),
MTimeSeconds: uint64(0),
MTimeNanoSeconds: uint64(0),
CTimeSeconds: 0,
CTimeNanoSeconds: 0,
}
valid := p9.AttrMask{
Mode: true,
UID: true,
GID: true,
NLink: true,
RDev: true,
Size: true,
Blocks: true,
ATime: true,
MTime: true,
CTime: true,
}
return p9.QID{Type: p9.TypeDir, Path: 0, Version: 0}, valid, attr, nil
}
// SetXattr implements p9.File.SetXattr
func (u *union9PFID) SetXattr(attr string, data []byte, flags p9.XattrFlags) error {
return syscall.ENOSYS
}
// ListXattrs implements p9.File.ListXattrs
func (u *union9PFID) ListXattrs() ([]string, error) {
return nil, syscall.ENOSYS
}
// GetXattr implements p9.File.GetXattr
func (u *union9PFID) GetXattr(attr string) ([]byte, error) {
return nil, syscall.ENOSYS
}
// RemoveXattr implements p9.File.RemoveXattr
func (u *union9PFID) RemoveXattr(attr string) error {
return syscall.ENOSYS
}