forked from coreos/torus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode.go
187 lines (172 loc) · 4 KB
/
inode.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
package torus
import (
"encoding/binary"
"github.com/coreos/pkg/capnslog"
"github.com/coreos/torus/models"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context"
)
var (
// INodes
promINodeRequests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "torus_distributor_inode_requests_total",
Help: "Total number of inodes requested of the distributor layer",
})
promINodeFailures = prometheus.NewCounter(prometheus.CounterOpts{
Name: "torus_distributor_inode_request_failures",
Help: "Number of failed inode requests",
})
)
func init() {
prometheus.MustRegister(promINodeRequests)
prometheus.MustRegister(promINodeFailures)
}
type INodeStore struct {
bs BlockStore
name string
}
func NewINodeStore(bs BlockStore) *INodeStore {
return &INodeStore{
bs: bs,
}
}
func (b *INodeStore) Flush() error { return b.bs.Flush() }
func (b *INodeStore) Close() error {
return b.bs.Close()
}
func (b *INodeStore) WriteINode(ctx context.Context, i INodeRef, inode *models.INode) error {
if i.INode == 0 {
panic("Writing zero inode")
}
inodedata, err := inode.Marshal()
if err != nil {
return err
}
buf := make([]byte, b.bs.BlockSize())
binary.LittleEndian.PutUint32(buf[0:4], uint32(len(inodedata)))
bufoffset := 4
inodeoffset := 0
index := 1
for inodeoffset != len(inodedata) {
if bufoffset == 0 {
buf = make([]byte, b.bs.BlockSize())
}
written := copy(buf[bufoffset:], inodedata[inodeoffset:])
inodeoffset += written
ref := BlockRef{
INodeRef: i,
Index: IndexID(index),
}
ref.SetBlockType(TypeINode)
if BlockLog.LevelAt(capnslog.TRACE) {
BlockLog.Tracef("writing inode block: %s", ref)
}
err := b.bs.WriteBlock(ctx, ref, buf)
if err != nil {
return err
}
bufoffset = 0
index++
}
clog.Tracef("Wrote INode %s", i)
return nil
}
func (b *INodeStore) GetINode(ctx context.Context, i INodeRef) (*models.INode, error) {
if i.INode == 0 {
panic("Fetching zero inode")
}
promINodeRequests.Inc()
index := 1
ref := BlockRef{
INodeRef: i,
Index: IndexID(index),
}
ref.SetBlockType(TypeINode)
data, err := b.bs.GetBlock(ctx, ref)
if err != nil {
promINodeFailures.Inc()
return nil, err
}
dlen := binary.LittleEndian.Uint32(data[0:4])
buf := make([]byte, dlen)
bufoffset := 0
dataoffset := 4
for bufoffset != int(dlen) {
if dataoffset == 0 {
index++
ref := BlockRef{
INodeRef: i,
Index: IndexID(index),
}
ref.SetBlockType(TypeINode)
data, err = b.bs.GetBlock(ctx, ref)
if err != nil {
promINodeFailures.Inc()
clog.Errorf("inode: couldn't get inode block: %s -- %s", err, ref)
return nil, err
}
}
written := copy(buf[bufoffset:], data[dataoffset:])
dataoffset = 0
bufoffset += written
}
out := &models.INode{}
err = out.Unmarshal(buf)
if err != nil {
promINodeFailures.Inc()
clog.Errorf("inode: couldn't unmarshal: %s", err)
return nil, err
}
return out, nil
}
func (b *INodeStore) DeleteINode(ctx context.Context, i INodeRef) error {
if i.INode == 0 {
panic("Deleting zero inode")
}
ref := BlockRef{
INodeRef: i,
Index: IndexID(1),
}
ref.SetBlockType(TypeINode)
data, err := b.bs.GetBlock(ctx, ref)
if err != nil {
return err
}
dlen := binary.LittleEndian.Uint32(data[0:4])
nblocks := (uint64(dlen) / b.bs.BlockSize()) + 1
for j := uint64(1); j <= nblocks; j++ {
ref := BlockRef{
INodeRef: i,
Index: IndexID(j),
}
ref.SetBlockType(TypeINode)
err := b.bs.DeleteBlock(ctx, ref)
if err != nil {
return err
}
}
return nil
}
func (b *INodeStore) INodeIterator() *INodeIterator {
it := b.bs.BlockIterator()
return &INodeIterator{it}
}
type INodeIterator struct {
it BlockIterator
}
func (i *INodeIterator) Err() error { return i.it.Err() }
func (i *INodeIterator) Next() bool {
for i.it.Next() {
ref := i.it.BlockRef()
if ref.BlockType() == TypeINode && ref.Index == 1 {
return true
}
}
return false
}
func (i *INodeIterator) INodeRef() INodeRef {
return i.it.BlockRef().INodeRef
}
func (i *INodeIterator) Close() error {
return i.it.Close()
}