/
partition_fsmop_dentry.go
235 lines (216 loc) · 5.67 KB
/
partition_fsmop_dentry.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
// Copyright 2018 The Chubao 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 metanode
import (
"strings"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/btree"
)
type DentryResponse struct {
Status uint8
Msg *Dentry
}
func NewDentryResponse() *DentryResponse {
return &DentryResponse{
Msg: &Dentry{},
}
}
// Insert a dentry into the dentry tree.
func (mp *metaPartition) fsmCreateDentry(dentry *Dentry,
forceUpdate bool) (status uint8) {
status = proto.OpOk
item := mp.inodeTree.CopyGet(NewInode(dentry.ParentId, 0))
var parIno *Inode
if !forceUpdate {
if item == nil {
status = proto.OpNotExistErr
return
}
parIno = item.(*Inode)
if parIno.ShouldDelete() {
status = proto.OpNotExistErr
return
}
if !proto.IsDir(parIno.Type) {
status = proto.OpArgMismatchErr
return
}
}
if item, ok := mp.dentryTree.ReplaceOrInsert(dentry, false); !ok {
//do not allow directories and files to overwrite each
// other when renaming
d := item.(*Dentry)
if proto.OsModeType(dentry.Type) != proto.OsModeType(d.Type) {
status = proto.OpArgMismatchErr
return
}
if dentry.ParentId == d.ParentId && strings.Compare(dentry.Name, d.Name) == 0 && dentry.Inode == d.Inode {
return
}
status = proto.OpExistErr
} else {
if !forceUpdate {
parIno.IncNLink()
parIno.SetMtime()
}
}
return
}
// Query a dentry from the dentry tree with specified dentry info.
func (mp *metaPartition) getDentry(dentry *Dentry) (*Dentry, uint8) {
status := proto.OpOk
item := mp.dentryTree.Get(dentry)
if item == nil {
status = proto.OpNotExistErr
return nil, status
}
dentry = item.(*Dentry)
return dentry, status
}
// Delete dentry from the dentry tree.
func (mp *metaPartition) fsmDeleteDentry(dentry *Dentry, checkInode bool) (
resp *DentryResponse) {
resp = NewDentryResponse()
resp.Status = proto.OpOk
var item interface{}
if checkInode {
item = mp.dentryTree.Execute(func(tree *btree.BTree) interface{} {
d := tree.CopyGet(dentry)
if d == nil {
return nil
}
if d.(*Dentry).Inode != dentry.Inode {
return nil
}
return mp.dentryTree.tree.Delete(dentry)
})
} else {
item = mp.dentryTree.Delete(dentry)
}
if item == nil {
resp.Status = proto.OpNotExistErr
return
} else {
mp.inodeTree.CopyFind(NewInode(dentry.ParentId, 0),
func(item BtreeItem) {
if item != nil {
ino := item.(*Inode)
if !ino.ShouldDelete() {
item.(*Inode).DecNLink()
item.(*Inode).SetMtime()
}
}
})
}
resp.Msg = item.(*Dentry)
return
}
// batch Delete dentry from the dentry tree.
func (mp *metaPartition) fsmBatchDeleteDentry(db DentryBatch) []*DentryResponse {
result := make([]*DentryResponse, 0, len(db))
for _, dentry := range db {
result = append(result, mp.fsmDeleteDentry(dentry, true))
}
return result
}
func (mp *metaPartition) fsmUpdateDentry(dentry *Dentry) (
resp *DentryResponse) {
resp = NewDentryResponse()
resp.Status = proto.OpOk
mp.dentryTree.CopyFind(dentry, func(item BtreeItem) {
if item == nil {
resp.Status = proto.OpNotExistErr
return
}
d := item.(*Dentry)
d.Inode, dentry.Inode = dentry.Inode, d.Inode
resp.Msg = dentry
})
return
}
func (mp *metaPartition) getDentryTree() *BTree {
return mp.dentryTree.GetTree()
}
func (mp *metaPartition) readDir(req *ReadDirReq) (resp *ReadDirResp) {
resp = &ReadDirResp{}
begDentry := &Dentry{
ParentId: req.ParentID,
}
endDentry := &Dentry{
ParentId: req.ParentID + 1,
}
mp.dentryTree.AscendRange(begDentry, endDentry, func(i BtreeItem) bool {
d := i.(*Dentry)
resp.Children = append(resp.Children, proto.Dentry{
Inode: d.Inode,
Type: d.Type,
Name: d.Name,
})
return true
})
return
}
// Read dentry from btree by limit count
// if req.Marker == "" and req.Limit == 0, it becomes readDir
// else if req.Marker != "" and req.Limit == 0, return dentries from pid:name to pid+1
// else if req.Marker == "" and req.Limit != 0, return dentries from pid with limit count
// else if req.Marker != "" and req.Limit != 0, return dentries from pid:marker to pid:xxxx with limit count
//
func (mp *metaPartition) readDirLimit(req *ReadDirLimitReq) (resp *ReadDirLimitResp) {
resp = &ReadDirLimitResp{}
startDentry := &Dentry{
ParentId: req.ParentID,
}
if len(req.Marker) > 0 {
startDentry.Name = req.Marker
}
endDentry := &Dentry{
ParentId: req.ParentID + 1,
}
mp.dentryTree.AscendRange(startDentry, endDentry, func(i BtreeItem) bool {
d := i.(*Dentry)
resp.Children = append(resp.Children, proto.Dentry{
Inode: d.Inode,
Type: d.Type,
Name: d.Name,
})
// Limit == 0 means no limit.
if req.Limit > 0 && uint64(len(resp.Children)) >= req.Limit {
return false
}
return true
})
return
}
func (mp *metaPartition) readDirOnly(req *ReadDirOnlyReq) (resp *ReadDirOnlyResp) {
resp = &ReadDirOnlyResp{}
begDentry := &Dentry{
ParentId: req.ParentID,
}
endDentry := &Dentry{
ParentId: req.ParentID + 1,
}
mp.dentryTree.AscendRange(begDentry, endDentry, func(i BtreeItem) bool {
d := i.(*Dentry)
if proto.IsDir(d.Type) {
resp.Children = append(resp.Children, proto.Dentry{
Inode: d.Inode,
Type: d.Type,
Name: d.Name,
})
}
return true
})
return
}