| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
Copyright 2019 The Go Authors. All rights reserved. |
| 19 |
Use of this source code is governed by a BSD-style |
| 20 |
license that can be found in the NOTICE.md file. |
| 21 |
*/ |
| 22 |
|
| 23 |
package layer |
| 24 |
|
| 25 |
import ( |
| 26 |
"bytes" |
| 27 |
"context" |
| 28 |
"encoding/json" |
| 29 |
"errors" |
| 30 |
"fmt" |
| 31 |
"io" |
| 32 |
"os" |
| 33 |
"sort" |
| 34 |
"strings" |
| 35 |
"sync" |
| 36 |
"syscall" |
| 37 |
"time" |
| 38 |
|
| 39 |
"github.com/containerd/containerd/log" |
| 40 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 41 |
commonmetrics "github.com/containerd/stargz-snapshotter/fs/metrics/common" |
| 42 |
"github.com/containerd/stargz-snapshotter/fs/reader" |
| 43 |
"github.com/containerd/stargz-snapshotter/fs/remote" |
| 44 |
"github.com/containerd/stargz-snapshotter/metadata" |
| 45 |
fusefs "github.com/hanwen/go-fuse/v2/fs" |
| 46 |
"github.com/hanwen/go-fuse/v2/fuse" |
| 47 |
digest "github.com/opencontainers/go-digest" |
| 48 |
"github.com/sirupsen/logrus" |
| 49 |
"golang.org/x/sys/unix" |
| 50 |
) |
| 51 |
|
| 52 |
const ( |
| 53 |
blockSize = 4096 |
| 54 |
whiteoutPrefix = ".wh." |
| 55 |
whiteoutOpaqueDir = whiteoutPrefix + whiteoutPrefix + ".opq" |
| 56 |
opaqueXattrValue = "y" |
| 57 |
stateDirName = ".stargz-snapshotter" |
| 58 |
statFileMode = syscall.S_IFREG | 0400 // -r-------- |
| 59 |
stateDirMode = syscall.S_IFDIR | 0500 // dr-x------ |
| 60 |
) |
| 61 |
|
| 62 |
type OverlayOpaqueType int |
| 63 |
|
| 64 |
const ( |
| 65 |
OverlayOpaqueAll OverlayOpaqueType = iota |
| 66 |
OverlayOpaqueTrusted |
| 67 |
OverlayOpaqueUser |
| 68 |
) |
| 69 |
|
| 70 |
var opaqueXattrs = map[OverlayOpaqueType][]string{ |
| 71 |
OverlayOpaqueAll: {"trusted.overlay.opaque", "user.overlay.opaque"}, |
| 72 |
OverlayOpaqueTrusted: {"trusted.overlay.opaque"}, |
| 73 |
OverlayOpaqueUser: {"user.overlay.opaque"}, |
| 74 |
} |
| 75 |
|
| 76 |
func newNode(layerDgst digest.Digest, r reader.Reader, blob remote.Blob, baseInode uint32, opaque OverlayOpaqueType) (fusefs.InodeEmbedder, error) { |
| 77 |
rootID := r.Metadata().RootID() |
| 78 |
rootAttr, err := r.Metadata().GetAttr(rootID) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
opq, ok := opaqueXattrs[opaque] |
| 83 |
if !ok { |
| 84 |
return nil, fmt.Errorf("Unknown overlay opaque type") |
| 85 |
} |
| 86 |
ffs := &fs{ |
| 87 |
r: r, |
| 88 |
layerDigest: layerDgst, |
| 89 |
baseInode: baseInode, |
| 90 |
rootID: rootID, |
| 91 |
opaqueXattrs: opq, |
| 92 |
} |
| 93 |
ffs.s = ffs.newState(layerDgst, blob) |
| 94 |
return &node{ |
| 95 |
id: rootID, |
| 96 |
attr: rootAttr, |
| 97 |
fs: ffs, |
| 98 |
}, nil |
| 99 |
} |
| 100 |
|
| 101 |
// fs contains global metadata used by nodes |
| 102 |
type fs struct { |
| 103 |
r reader.Reader |
| 104 |
s *state |
| 105 |
layerDigest digest.Digest |
| 106 |
baseInode uint32 |
| 107 |
rootID uint32 |
| 108 |
opaqueXattrs []string |
| 109 |
} |
| 110 |
|
| 111 |
func (fs *fs) inodeOfState() uint64 { |
| 112 |
return (uint64(fs.baseInode) << 32) | 1 // reserved |
| 113 |
} |
| 114 |
|
| 115 |
func (fs *fs) inodeOfStatFile() uint64 { |
| 116 |
return (uint64(fs.baseInode) << 32) | 2 // reserved |
| 117 |
} |
| 118 |
|
| 119 |
func (fs *fs) inodeOfID(id uint32) (uint64, error) { |
| 120 |
// 0 is reserved by go-fuse 1 and 2 are reserved by the state dir |
| 121 |
if id > ^uint32(0)-3 { |
| 122 |
return 0, fmt.Errorf("too many inodes") |
| 123 |
} |
| 124 |
return (uint64(fs.baseInode) << 32) | uint64(3+id), nil |
| 125 |
} |
| 126 |
|
| 127 |
// node is a filesystem inode abstraction. |
| 128 |
type node struct { |
| 129 |
fusefs.Inode |
| 130 |
fs *fs |
| 131 |
id uint32 |
| 132 |
attr metadata.Attr |
| 133 |
ents []fuse.DirEntry |
| 134 |
entsCached bool |
| 135 |
} |
| 136 |
|
| 137 |
func (n *node) isRootNode() bool { |
| 138 |
return n.id == n.fs.rootID |
| 139 |
} |
| 140 |
|
| 141 |
func (n *node) isOpaque() bool { |
| 142 |
if _, _, err := n.fs.r.Metadata().GetChild(n.id, whiteoutOpaqueDir); err == nil { |
| 143 |
return true |
| 144 |
} |
| 145 |
return false |
| 146 |
} |
| 147 |
|
| 148 |
var _ = (fusefs.InodeEmbedder)((*node)(nil)) |
| 149 |
|
| 150 |
var _ = (fusefs.NodeReaddirer)((*node)(nil)) |
| 151 |
|
| 152 |
func (n *node) Readdir(ctx context.Context) (fusefs.DirStream, syscall.Errno) { |
| 153 |
ents, errno := n.readdir() |
| 154 |
if errno != 0 { |
| 155 |
return nil, errno |
| 156 |
} |
| 157 |
return fusefs.NewListDirStream(ents), 0 |
| 158 |
} |
| 159 |
|
| 160 |
func (n *node) readdir() ([]fuse.DirEntry, syscall.Errno) { |
| 161 |
// Measure how long node_readdir operation takes (in microseconds). |
| 162 |
start := time.Now() // set start time |
| 163 |
defer commonmetrics.MeasureLatencyInMicroseconds(commonmetrics.NodeReaddir, n.fs.layerDigest, start) |
| 164 |
|
| 165 |
if n.entsCached { |
| 166 |
return n.ents, 0 |
| 167 |
} |
| 168 |
|
| 169 |
isRoot := n.isRootNode() |
| 170 |
|
| 171 |
var ents []fuse.DirEntry |
| 172 |
whiteouts := map[string]uint32{} |
| 173 |
normalEnts := map[string]bool{} |
| 174 |
var lastErr error |
| 175 |
if err := n.fs.r.Metadata().ForeachChild(n.id, func(name string, id uint32, mode os.FileMode) bool { |
| 176 |
|
| 177 |
// We don't want to show prefetch landmarks in "/". |
| 178 |
if isRoot && (name == estargz.PrefetchLandmark || name == estargz.NoPrefetchLandmark) { |
| 179 |
return true |
| 180 |
} |
| 181 |
|
| 182 |
// We don't want to show whiteouts. |
| 183 |
if strings.HasPrefix(name, whiteoutPrefix) { |
| 184 |
if name == whiteoutOpaqueDir { |
| 185 |
return true |
| 186 |
} |
| 187 |
// Add the overlayfs-compiant whiteout later. |
| 188 |
whiteouts[name] = id |
| 189 |
return true |
| 190 |
} |
| 191 |
|
| 192 |
// This is a normal entry. |
| 193 |
normalEnts[name] = true |
| 194 |
ino, err := n.fs.inodeOfID(id) |
| 195 |
if err != nil { |
| 196 |
lastErr = err |
| 197 |
return false |
| 198 |
} |
| 199 |
ents = append(ents, fuse.DirEntry{ |
| 200 |
Mode: fileModeToSystemMode(mode), |
| 201 |
Name: name, |
| 202 |
Ino: ino, |
| 203 |
}) |
| 204 |
return true |
| 205 |
}); err != nil || lastErr != nil { |
| 206 |
n.fs.s.report(fmt.Errorf("node.Readdir: err = %v; lastErr = %v", err, lastErr)) |
| 207 |
return nil, syscall.EIO |
| 208 |
} |
| 209 |
|
| 210 |
// Append whiteouts if no entry replaces the target entry in the lower layer. |
| 211 |
for w, id := range whiteouts { |
| 212 |
if !normalEnts[w[len(whiteoutPrefix):]] { |
| 213 |
ino, err := n.fs.inodeOfID(id) |
| 214 |
if err != nil { |
| 215 |
n.fs.s.report(fmt.Errorf("node.Readdir: err = %v; lastErr = %v", err, lastErr)) |
| 216 |
return nil, syscall.EIO |
| 217 |
} |
| 218 |
ents = append(ents, fuse.DirEntry{ |
| 219 |
Mode: syscall.S_IFCHR, |
| 220 |
Name: w[len(whiteoutPrefix):], |
| 221 |
Ino: ino, |
| 222 |
}) |
| 223 |
|
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// Avoid undeterministic order of entries on each call |
| 228 |
sort.Slice(ents, func(i, j int) bool { |
| 229 |
return ents[i].Name < ents[j].Name |
| 230 |
}) |
| 231 |
n.ents, n.entsCached = ents, true // cache it |
| 232 |
|
| 233 |
return ents, 0 |
| 234 |
} |
| 235 |
|
| 236 |
var _ = (fusefs.NodeLookuper)((*node)(nil)) |
| 237 |
|
| 238 |
func (n *node) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fusefs.Inode, syscall.Errno) { |
| 239 |
|
| 240 |
isRoot := n.isRootNode() |
| 241 |
|
| 242 |
// We don't want to show prefetch landmarks in "/". |
| 243 |
if isRoot && (name == estargz.PrefetchLandmark || name == estargz.NoPrefetchLandmark) { |
| 244 |
return nil, syscall.ENOENT |
| 245 |
} |
| 246 |
|
| 247 |
// We don't want to show whiteouts. |
| 248 |
if strings.HasPrefix(name, whiteoutPrefix) { |
| 249 |
return nil, syscall.ENOENT |
| 250 |
} |
| 251 |
|
| 252 |
// state directory |
| 253 |
if isRoot && name == stateDirName { |
| 254 |
return n.NewInode(ctx, n.fs.s, n.fs.stateToAttr(&out.Attr)), 0 |
| 255 |
} |
| 256 |
|
| 257 |
// lookup on memory nodes |
| 258 |
if cn := n.GetChild(name); cn != nil { |
| 259 |
switch tn := cn.Operations().(type) { |
| 260 |
case *node: |
| 261 |
ino, err := n.fs.inodeOfID(tn.id) |
| 262 |
if err != nil { |
| 263 |
n.fs.s.report(fmt.Errorf("node.Lookup: %v", err)) |
| 264 |
return nil, syscall.EIO |
| 265 |
} |
| 266 |
entryToAttr(ino, tn.attr, &out.Attr) |
| 267 |
case *whiteout: |
| 268 |
ino, err := n.fs.inodeOfID(tn.id) |
| 269 |
if err != nil { |
| 270 |
n.fs.s.report(fmt.Errorf("node.Lookup: %v", err)) |
| 271 |
return nil, syscall.EIO |
| 272 |
} |
| 273 |
entryToAttr(ino, tn.attr, &out.Attr) |
| 274 |
default: |
| 275 |
n.fs.s.report(fmt.Errorf("node.Lookup: uknown node type detected")) |
| 276 |
return nil, syscall.EIO |
| 277 |
} |
| 278 |
return cn, 0 |
| 279 |
} |
| 280 |
|
| 281 |
// early return if this entry doesn't exist |
| 282 |
if n.entsCached { |
| 283 |
var found bool |
| 284 |
for _, e := range n.ents { |
| 285 |
if e.Name == name { |
| 286 |
found = true |
| 287 |
} |
| 288 |
} |
| 289 |
if !found { |
| 290 |
return nil, syscall.ENOENT |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
id, ce, err := n.fs.r.Metadata().GetChild(n.id, name) |
| 295 |
if err != nil { |
| 296 |
// If the entry exists as a whiteout, show an overlayfs-styled whiteout node. |
| 297 |
if whID, wh, err := n.fs.r.Metadata().GetChild(n.id, fmt.Sprintf("%s%s", whiteoutPrefix, name)); err == nil { |
| 298 |
ino, err := n.fs.inodeOfID(whID) |
| 299 |
if err != nil { |
| 300 |
n.fs.s.report(fmt.Errorf("node.Lookup: %v", err)) |
| 301 |
return nil, syscall.EIO |
| 302 |
} |
| 303 |
return n.NewInode(ctx, &whiteout{ |
| 304 |
id: whID, |
| 305 |
fs: n.fs, |
| 306 |
attr: wh, |
| 307 |
}, entryToWhAttr(ino, wh, &out.Attr)), 0 |
| 308 |
} |
| 309 |
n.readdir() // This code path is very expensive. Cache child entries here so that the next call don't reach here. |
| 310 |
return nil, syscall.ENOENT |
| 311 |
} |
| 312 |
|
| 313 |
ino, err := n.fs.inodeOfID(id) |
| 314 |
if err != nil { |
| 315 |
n.fs.s.report(fmt.Errorf("node.Lookup: %v", err)) |
| 316 |
return nil, syscall.EIO |
| 317 |
} |
| 318 |
return n.NewInode(ctx, &node{ |
| 319 |
id: id, |
| 320 |
fs: n.fs, |
| 321 |
attr: ce, |
| 322 |
}, entryToAttr(ino, ce, &out.Attr)), 0 |
| 323 |
} |
| 324 |
|
| 325 |
var _ = (fusefs.NodeOpener)((*node)(nil)) |
| 326 |
|
| 327 |
func (n *node) Open(ctx context.Context, flags uint32) (fh fusefs.FileHandle, fuseFlags uint32, errno syscall.Errno) { |
| 328 |
ra, err := n.fs.r.OpenFile(n.id) |
| 329 |
if err != nil { |
| 330 |
n.fs.s.report(fmt.Errorf("node.Open: %v", err)) |
| 331 |
return nil, 0, syscall.EIO |
| 332 |
} |
| 333 |
return &file{ |
| 334 |
n: n, |
| 335 |
ra: ra, |
| 336 |
}, fuse.FOPEN_KEEP_CACHE, 0 |
| 337 |
} |
| 338 |
|
| 339 |
var _ = (fusefs.NodeGetattrer)((*node)(nil)) |
| 340 |
|
| 341 |
func (n *node) Getattr(ctx context.Context, f fusefs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 342 |
ino, err := n.fs.inodeOfID(n.id) |
| 343 |
if err != nil { |
| 344 |
n.fs.s.report(fmt.Errorf("node.Getattr: %v", err)) |
| 345 |
return syscall.EIO |
| 346 |
} |
| 347 |
entryToAttr(ino, n.attr, &out.Attr) |
| 348 |
return 0 |
| 349 |
} |
| 350 |
|
| 351 |
var _ = (fusefs.NodeGetxattrer)((*node)(nil)) |
| 352 |
|
| 353 |
func (n *node) Getxattr(ctx context.Context, attr string, dest []byte) (uint32, syscall.Errno) { |
| 354 |
ent := n.attr |
| 355 |
opq := n.isOpaque() |
| 356 |
for _, opaqueXattr := range n.fs.opaqueXattrs { |
| 357 |
if attr == opaqueXattr && opq { |
| 358 |
// This node is an opaque directory so give overlayfs-compliant indicator. |
| 359 |
if len(dest) < len(opaqueXattrValue) { |
| 360 |
return uint32(len(opaqueXattrValue)), syscall.ERANGE |
| 361 |
} |
| 362 |
return uint32(copy(dest, opaqueXattrValue)), 0 |
| 363 |
} |
| 364 |
} |
| 365 |
if v, ok := ent.Xattrs[attr]; ok { |
| 366 |
if len(dest) < len(v) { |
| 367 |
return uint32(len(v)), syscall.ERANGE |
| 368 |
} |
| 369 |
return uint32(copy(dest, v)), 0 |
| 370 |
} |
| 371 |
return 0, syscall.ENODATA |
| 372 |
} |
| 373 |
|
| 374 |
var _ = (fusefs.NodeListxattrer)((*node)(nil)) |
| 375 |
|
| 376 |
func (n *node) Listxattr(ctx context.Context, dest []byte) (uint32, syscall.Errno) { |
| 377 |
ent := n.attr |
| 378 |
opq := n.isOpaque() |
| 379 |
var attrs []byte |
| 380 |
if opq { |
| 381 |
// This node is an opaque directory so add overlayfs-compliant indicator. |
| 382 |
for _, opaqueXattr := range n.fs.opaqueXattrs { |
| 383 |
attrs = append(attrs, []byte(opaqueXattr+"\x00")...) |
| 384 |
} |
| 385 |
} |
| 386 |
for k := range ent.Xattrs { |
| 387 |
attrs = append(attrs, []byte(k+"\x00")...) |
| 388 |
} |
| 389 |
if len(dest) < len(attrs) { |
| 390 |
return uint32(len(attrs)), syscall.ERANGE |
| 391 |
} |
| 392 |
return uint32(copy(dest, attrs)), 0 |
| 393 |
} |
| 394 |
|
| 395 |
var _ = (fusefs.NodeReadlinker)((*node)(nil)) |
| 396 |
|
| 397 |
func (n *node) Readlink(ctx context.Context) ([]byte, syscall.Errno) { |
| 398 |
ent := n.attr |
| 399 |
return []byte(ent.LinkName), 0 |
| 400 |
} |
| 401 |
|
| 402 |
var _ = (fusefs.NodeStatfser)((*node)(nil)) |
| 403 |
|
| 404 |
func (n *node) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno { |
| 405 |
defaultStatfs(out) |
| 406 |
return 0 |
| 407 |
} |
| 408 |
|
| 409 |
// file is a file abstraction which implements file handle in go-fuse. |
| 410 |
type file struct { |
| 411 |
n *node |
| 412 |
ra io.ReaderAt |
| 413 |
} |
| 414 |
|
| 415 |
var _ = (fusefs.FileReader)((*file)(nil)) |
| 416 |
|
| 417 |
func (f *file) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { |
| 418 |
defer commonmetrics.MeasureLatencyInMicroseconds(commonmetrics.ReadOnDemand, f.n.fs.layerDigest, time.Now()) // measure time for on-demand file reads (in microseconds) |
| 419 |
defer commonmetrics.IncOperationCount(commonmetrics.OnDemandReadAccessCount, f.n.fs.layerDigest) // increment the counter for on-demand file accesses |
| 420 |
n, err := f.ra.ReadAt(dest, off) |
| 421 |
if err != nil && err != io.EOF { |
| 422 |
f.n.fs.s.report(fmt.Errorf("file.Read: %v", err)) |
| 423 |
return nil, syscall.EIO |
| 424 |
} |
| 425 |
return fuse.ReadResultData(dest[:n]), 0 |
| 426 |
} |
| 427 |
|
| 428 |
var _ = (fusefs.FileGetattrer)((*file)(nil)) |
| 429 |
|
| 430 |
func (f *file) Getattr(ctx context.Context, out *fuse.AttrOut) syscall.Errno { |
| 431 |
ino, err := f.n.fs.inodeOfID(f.n.id) |
| 432 |
if err != nil { |
| 433 |
f.n.fs.s.report(fmt.Errorf("file.Getattr: %v", err)) |
| 434 |
return syscall.EIO |
| 435 |
} |
| 436 |
entryToAttr(ino, f.n.attr, &out.Attr) |
| 437 |
return 0 |
| 438 |
} |
| 439 |
|
| 440 |
// whiteout is a whiteout abstraction compliant to overlayfs. |
| 441 |
type whiteout struct { |
| 442 |
fusefs.Inode |
| 443 |
id uint32 |
| 444 |
fs *fs |
| 445 |
attr metadata.Attr |
| 446 |
} |
| 447 |
|
| 448 |
var _ = (fusefs.NodeGetattrer)((*whiteout)(nil)) |
| 449 |
|
| 450 |
func (w *whiteout) Getattr(ctx context.Context, f fusefs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 451 |
ino, err := w.fs.inodeOfID(w.id) |
| 452 |
if err != nil { |
| 453 |
w.fs.s.report(fmt.Errorf("whiteout.Getattr: %v", err)) |
| 454 |
return syscall.EIO |
| 455 |
} |
| 456 |
entryToWhAttr(ino, w.attr, &out.Attr) |
| 457 |
return 0 |
| 458 |
} |
| 459 |
|
| 460 |
var _ = (fusefs.NodeStatfser)((*whiteout)(nil)) |
| 461 |
|
| 462 |
func (w *whiteout) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno { |
| 463 |
defaultStatfs(out) |
| 464 |
return 0 |
| 465 |
} |
| 466 |
|
| 467 |
// newState provides new state directory node. |
| 468 |
// It creates statFile at the same time to give it stable inode number. |
| 469 |
func (fs *fs) newState(layerDigest digest.Digest, blob remote.Blob) *state { |
| 470 |
return &state{ |
| 471 |
statFile: &statFile{ |
| 472 |
name: layerDigest.String() + ".json", |
| 473 |
statJSON: statJSON{ |
| 474 |
Digest: layerDigest.String(), |
| 475 |
Size: blob.Size(), |
| 476 |
}, |
| 477 |
blob: blob, |
| 478 |
fs: fs, |
| 479 |
}, |
| 480 |
fs: fs, |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
// state is a directory which contain a "state file" of this layer aiming to |
| 485 |
// observability. This filesystem uses it to report something(e.g. error) to |
| 486 |
// the clients(e.g. Kubernetes's livenessProbe). |
| 487 |
// This directory has mode "dr-x------ root root". |
| 488 |
type state struct { |
| 489 |
fusefs.Inode |
| 490 |
statFile *statFile |
| 491 |
fs *fs |
| 492 |
} |
| 493 |
|
| 494 |
var _ = (fusefs.NodeReaddirer)((*state)(nil)) |
| 495 |
|
| 496 |
func (s *state) Readdir(ctx context.Context) (fusefs.DirStream, syscall.Errno) { |
| 497 |
return fusefs.NewListDirStream([]fuse.DirEntry{ |
| 498 |
{ |
| 499 |
Mode: statFileMode, |
| 500 |
Name: s.statFile.name, |
| 501 |
Ino: s.fs.inodeOfStatFile(), |
| 502 |
}, |
| 503 |
}), 0 |
| 504 |
} |
| 505 |
|
| 506 |
var _ = (fusefs.NodeLookuper)((*state)(nil)) |
| 507 |
|
| 508 |
func (s *state) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fusefs.Inode, syscall.Errno) { |
| 509 |
if name != s.statFile.name { |
| 510 |
return nil, syscall.ENOENT |
| 511 |
} |
| 512 |
attr, errno := s.statFile.attr(&out.Attr) |
| 513 |
if errno != 0 { |
| 514 |
return nil, errno |
| 515 |
} |
| 516 |
return s.NewInode(ctx, s.statFile, attr), 0 |
| 517 |
} |
| 518 |
|
| 519 |
var _ = (fusefs.NodeGetattrer)((*state)(nil)) |
| 520 |
|
| 521 |
func (s *state) Getattr(ctx context.Context, f fusefs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 522 |
s.fs.stateToAttr(&out.Attr) |
| 523 |
return 0 |
| 524 |
} |
| 525 |
|
| 526 |
var _ = (fusefs.NodeStatfser)((*state)(nil)) |
| 527 |
|
| 528 |
func (s *state) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno { |
| 529 |
defaultStatfs(out) |
| 530 |
return 0 |
| 531 |
} |
| 532 |
|
| 533 |
func (s *state) report(err error) { |
| 534 |
s.statFile.report(err) |
| 535 |
} |
| 536 |
|
| 537 |
type statJSON struct { |
| 538 |
Error string `json:"error,omitempty"` |
| 539 |
Digest string `json:"digest"` |
| 540 |
// URL is excluded for potential security reason |
| 541 |
Size int64 `json:"size"` |
| 542 |
FetchedSize int64 `json:"fetchedSize"` |
| 543 |
FetchedPercent float64 `json:"fetchedPercent"` // Fetched / Size * 100.0 |
| 544 |
} |
| 545 |
|
| 546 |
// statFile is a file which contain something to be reported from this layer. |
| 547 |
// This filesystem uses statFile.report() to report something(e.g. error) to |
| 548 |
// the clients(e.g. Kubernetes's livenessProbe). |
| 549 |
// This file has mode "-r-------- root root". |
| 550 |
type statFile struct { |
| 551 |
fusefs.Inode |
| 552 |
name string |
| 553 |
blob remote.Blob |
| 554 |
statJSON statJSON |
| 555 |
mu sync.Mutex |
| 556 |
fs *fs |
| 557 |
} |
| 558 |
|
| 559 |
var _ = (fusefs.NodeOpener)((*statFile)(nil)) |
| 560 |
|
| 561 |
func (sf *statFile) Open(ctx context.Context, flags uint32) (fh fusefs.FileHandle, fuseFlags uint32, errno syscall.Errno) { |
| 562 |
return nil, 0, 0 |
| 563 |
} |
| 564 |
|
| 565 |
var _ = (fusefs.NodeReader)((*statFile)(nil)) |
| 566 |
|
| 567 |
func (sf *statFile) Read(ctx context.Context, f fusefs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { |
| 568 |
sf.mu.Lock() |
| 569 |
defer sf.mu.Unlock() |
| 570 |
st, err := sf.updateStatUnlocked() |
| 571 |
if err != nil { |
| 572 |
return nil, syscall.EIO |
| 573 |
} |
| 574 |
n, err := bytes.NewReader(st).ReadAt(dest, off) |
| 575 |
if err != nil && err != io.EOF { |
| 576 |
return nil, syscall.EIO |
| 577 |
} |
| 578 |
return fuse.ReadResultData(dest[:n]), 0 |
| 579 |
} |
| 580 |
|
| 581 |
var _ = (fusefs.NodeGetattrer)((*statFile)(nil)) |
| 582 |
|
| 583 |
func (sf *statFile) Getattr(ctx context.Context, f fusefs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 584 |
_, errno := sf.attr(&out.Attr) |
| 585 |
return errno |
| 586 |
} |
| 587 |
|
| 588 |
var _ = (fusefs.NodeStatfser)((*statFile)(nil)) |
| 589 |
|
| 590 |
func (sf *statFile) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno { |
| 591 |
defaultStatfs(out) |
| 592 |
return 0 |
| 593 |
} |
| 594 |
|
| 595 |
// logContents puts the contents of statFile in the log |
| 596 |
// to keep that information accessible for troubleshooting. |
| 597 |
// The entries naming is kept to be consistend with the field naming in statJSON. |
| 598 |
func (sf *statFile) logContents() { |
| 599 |
ctx := context.Background() |
| 600 |
log.G(ctx).WithFields(logrus.Fields{ |
| 601 |
"digest": sf.statJSON.Digest, "size": sf.statJSON.Size, |
| 602 |
"fetchedSize": sf.statJSON.FetchedSize, "fetchedPercent": sf.statJSON.FetchedPercent, |
| 603 |
}).WithError(errors.New(sf.statJSON.Error)).Error("statFile error") |
| 604 |
} |
| 605 |
|
| 606 |
func (sf *statFile) report(err error) { |
| 607 |
sf.mu.Lock() |
| 608 |
defer sf.mu.Unlock() |
| 609 |
sf.statJSON.Error = err.Error() |
| 610 |
sf.logContents() |
| 611 |
} |
| 612 |
|
| 613 |
func (sf *statFile) attr(out *fuse.Attr) (fusefs.StableAttr, syscall.Errno) { |
| 614 |
sf.mu.Lock() |
| 615 |
defer sf.mu.Unlock() |
| 616 |
|
| 617 |
st, err := sf.updateStatUnlocked() |
| 618 |
if err != nil { |
| 619 |
return fusefs.StableAttr{}, syscall.EIO |
| 620 |
} |
| 621 |
|
| 622 |
return sf.fs.statFileToAttr(uint64(len(st)), out), 0 |
| 623 |
} |
| 624 |
|
| 625 |
func (sf *statFile) updateStatUnlocked() ([]byte, error) { |
| 626 |
sf.statJSON.FetchedSize = sf.blob.FetchedSize() |
| 627 |
sf.statJSON.FetchedPercent = float64(sf.statJSON.FetchedSize) / float64(sf.statJSON.Size) * 100.0 |
| 628 |
j, err := json.Marshal(&sf.statJSON) |
| 629 |
if err != nil { |
| 630 |
return nil, err |
| 631 |
} |
| 632 |
j = append(j, []byte("\n")...) |
| 633 |
return j, nil |
| 634 |
} |
| 635 |
|
| 636 |
// entryToAttr converts metadata.Attr to go-fuse's Attr. |
| 637 |
func entryToAttr(ino uint64, e metadata.Attr, out *fuse.Attr) fusefs.StableAttr { |
| 638 |
out.Ino = ino |
| 639 |
out.Size = uint64(e.Size) |
| 640 |
if e.Mode&os.ModeSymlink != 0 { |
| 641 |
out.Size = uint64(len(e.LinkName)) |
| 642 |
} |
| 643 |
out.Blksize = blockSize |
| 644 |
out.Blocks = out.Size / uint64(out.Blksize) |
| 645 |
if out.Size%uint64(out.Blksize) > 0 { |
| 646 |
out.Blocks++ |
| 647 |
} |
| 648 |
mtime := e.ModTime |
| 649 |
out.SetTimes(nil, &mtime, nil) |
| 650 |
out.Mode = fileModeToSystemMode(e.Mode) |
| 651 |
out.Owner = fuse.Owner{Uid: uint32(e.UID), Gid: uint32(e.GID)} |
| 652 |
out.Rdev = uint32(unix.Mkdev(uint32(e.DevMajor), uint32(e.DevMinor))) |
| 653 |
out.Nlink = uint32(e.NumLink) |
| 654 |
if out.Nlink == 0 { |
| 655 |
out.Nlink = 1 // zero "NumLink" means one. |
| 656 |
} |
| 657 |
out.Padding = 0 // TODO |
| 658 |
|
| 659 |
return fusefs.StableAttr{ |
| 660 |
Mode: out.Mode, |
| 661 |
Ino: out.Ino, |
| 662 |
// NOTE: The inode number is unique throughout the lifetime of |
| 663 |
// this filesystem so we don't consider about generation at this |
| 664 |
// moment. |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
// entryToWhAttr converts metadata.Attr to go-fuse's Attr of whiteouts. |
| 669 |
func entryToWhAttr(ino uint64, e metadata.Attr, out *fuse.Attr) fusefs.StableAttr { |
| 670 |
out.Ino = ino |
| 671 |
out.Size = 0 |
| 672 |
out.Blksize = blockSize |
| 673 |
out.Blocks = 0 |
| 674 |
mtime := e.ModTime |
| 675 |
out.SetTimes(nil, &mtime, nil) |
| 676 |
out.Mode = syscall.S_IFCHR |
| 677 |
out.Owner = fuse.Owner{Uid: 0, Gid: 0} |
| 678 |
out.Rdev = uint32(unix.Mkdev(0, 0)) |
| 679 |
out.Nlink = 1 |
| 680 |
out.Padding = 0 // TODO |
| 681 |
|
| 682 |
return fusefs.StableAttr{ |
| 683 |
Mode: out.Mode, |
| 684 |
Ino: out.Ino, |
| 685 |
// NOTE: The inode number is unique throughout the lifetime of |
| 686 |
// this filesystem so we don't consider about generation at this |
| 687 |
// moment. |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
// stateToAttr converts state directory to go-fuse's Attr. |
| 692 |
func (fs *fs) stateToAttr(out *fuse.Attr) fusefs.StableAttr { |
| 693 |
out.Ino = fs.inodeOfState() |
| 694 |
out.Size = 0 |
| 695 |
out.Blksize = blockSize |
| 696 |
out.Blocks = 0 |
| 697 |
out.Nlink = 1 |
| 698 |
|
| 699 |
// root can read and open it (dr-x------ root root). |
| 700 |
out.Mode = stateDirMode |
| 701 |
out.Owner = fuse.Owner{Uid: 0, Gid: 0} |
| 702 |
|
| 703 |
// dummy |
| 704 |
out.Mtime = 0 |
| 705 |
out.Mtimensec = 0 |
| 706 |
out.Rdev = 0 |
| 707 |
out.Padding = 0 |
| 708 |
|
| 709 |
return fusefs.StableAttr{ |
| 710 |
Mode: out.Mode, |
| 711 |
Ino: out.Ino, |
| 712 |
// NOTE: The inode number is unique throughout the lifetime of |
| 713 |
// this filesystem so we don't consider about generation at this |
| 714 |
// moment. |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
// statFileToAttr converts stat file to go-fuse's Attr. |
| 719 |
// func statFileToAttr(id uint64, sf *statFile, size uint64, out *fuse.Attr) fusefs.StableAttr { |
| 720 |
func (fs *fs) statFileToAttr(size uint64, out *fuse.Attr) fusefs.StableAttr { |
| 721 |
out.Ino = fs.inodeOfStatFile() |
| 722 |
out.Size = size |
| 723 |
out.Blksize = blockSize |
| 724 |
out.Blocks = out.Size / uint64(out.Blksize) |
| 725 |
out.Nlink = 1 |
| 726 |
|
| 727 |
// Root can read it ("-r-------- root root"). |
| 728 |
out.Mode = statFileMode |
| 729 |
out.Owner = fuse.Owner{Uid: 0, Gid: 0} |
| 730 |
|
| 731 |
// dummy |
| 732 |
out.Mtime = 0 |
| 733 |
out.Mtimensec = 0 |
| 734 |
out.Rdev = 0 |
| 735 |
out.Padding = 0 |
| 736 |
|
| 737 |
return fusefs.StableAttr{ |
| 738 |
Mode: out.Mode, |
| 739 |
Ino: out.Ino, |
| 740 |
// NOTE: The inode number is unique throughout the lifetime of |
| 741 |
// this filesystem so we don't consider about generation at this |
| 742 |
// moment. |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
func fileModeToSystemMode(m os.FileMode) uint32 { |
| 747 |
// Permission bits |
| 748 |
res := uint32(m & os.ModePerm) |
| 749 |
|
| 750 |
// File type bits |
| 751 |
switch m & os.ModeType { |
| 752 |
case os.ModeDevice: |
| 753 |
res |= syscall.S_IFBLK |
| 754 |
case os.ModeDevice | os.ModeCharDevice: |
| 755 |
res |= syscall.S_IFCHR |
| 756 |
case os.ModeDir: |
| 757 |
res |= syscall.S_IFDIR |
| 758 |
case os.ModeNamedPipe: |
| 759 |
res |= syscall.S_IFIFO |
| 760 |
case os.ModeSymlink: |
| 761 |
res |= syscall.S_IFLNK |
| 762 |
case os.ModeSocket: |
| 763 |
res |= syscall.S_IFSOCK |
| 764 |
default: // regular file. |
| 765 |
res |= syscall.S_IFREG |
| 766 |
} |
| 767 |
|
| 768 |
// suid, sgid, sticky bits |
| 769 |
if m&os.ModeSetuid != 0 { |
| 770 |
res |= syscall.S_ISUID |
| 771 |
} |
| 772 |
if m&os.ModeSetgid != 0 { |
| 773 |
res |= syscall.S_ISGID |
| 774 |
} |
| 775 |
if m&os.ModeSticky != 0 { |
| 776 |
res |= syscall.S_ISVTX |
| 777 |
} |
| 778 |
|
| 779 |
return res |
| 780 |
} |
| 781 |
|
| 782 |
func defaultStatfs(stat *fuse.StatfsOut) { |
| 783 |
|
| 784 |
// http://man7.org/linux/man-pages/man2/statfs.2.html |
| 785 |
stat.Blocks = 0 // dummy |
| 786 |
stat.Bfree = 0 |
| 787 |
stat.Bavail = 0 |
| 788 |
stat.Files = 0 // dummy |
| 789 |
stat.Ffree = 0 |
| 790 |
stat.Bsize = blockSize |
| 791 |
stat.NameLen = 1<<32 - 1 |
| 792 |
stat.Frsize = blockSize |
| 793 |
stat.Padding = 0 |
| 794 |
stat.Spare = [6]uint32{} |
| 795 |
} |