forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinode.go
689 lines (635 loc) · 16.8 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// Copyright 2018 The CubeFS 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 (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"sync"
"time"
"github.com/cubefs/cubefs/proto"
)
const (
DeleteMarkFlag = 1 << 0
)
var (
// InodeV1Flag uint64 = 0x01
InodeV2Flag uint64 = 0x02
)
// Inode wraps necessary properties of `Inode` information in the file system.
// Marshal exporterKey:
// +-------+-------+
// | item | Inode |
// +-------+-------+
// | bytes | 8 |
// +-------+-------+
// Marshal value:
// +-------+------+------+-----+----+----+----+--------+------------------+
// | item | Type | Size | Gen | CT | AT | MT | ExtLen | MarshaledExtents |
// +-------+------+------+-----+----+----+----+--------+------------------+
// | bytes | 4 | 8 | 8 | 8 | 8 | 8 | 4 | ExtLen |
// +-------+------+------+-----+----+----+----+--------+------------------+
// Marshal entity:
// +-------+-----------+--------------+-----------+--------------+
// | item | KeyLength | MarshaledKey | ValLength | MarshaledVal |
// +-------+-----------+--------------+-----------+--------------+
// | bytes | 4 | KeyLength | 4 | ValLength |
// +-------+-----------+--------------+-----------+--------------+
type Inode struct {
sync.RWMutex
Inode uint64 // Inode ID
Type uint32
Uid uint32
Gid uint32
Size uint64
Generation uint64
CreateTime int64
AccessTime int64
ModifyTime int64
LinkTarget []byte // SymLink target name
NLink uint32 // NodeLink counts
Flag int32
Reserved uint64 // reserved space
//Extents *ExtentsTree
Extents *SortedExtents
ObjExtents *SortedObjExtents
}
type InodeBatch []*Inode
// String returns the string format of the inode.
func (i *Inode) String() string {
i.RLock()
defer i.RUnlock()
buff := bytes.NewBuffer(nil)
buff.Grow(128)
buff.WriteString("Inode{")
buff.WriteString(fmt.Sprintf("Inode[%d]", i.Inode))
buff.WriteString(fmt.Sprintf("Type[%d]", i.Type))
buff.WriteString(fmt.Sprintf("Uid[%d]", i.Uid))
buff.WriteString(fmt.Sprintf("Gid[%d]", i.Gid))
buff.WriteString(fmt.Sprintf("Size[%d]", i.Size))
buff.WriteString(fmt.Sprintf("Gen[%d]", i.Generation))
buff.WriteString(fmt.Sprintf("CT[%d]", i.CreateTime))
buff.WriteString(fmt.Sprintf("AT[%d]", i.AccessTime))
buff.WriteString(fmt.Sprintf("MT[%d]", i.ModifyTime))
buff.WriteString(fmt.Sprintf("LinkT[%s]", i.LinkTarget))
buff.WriteString(fmt.Sprintf("NLink[%d]", i.NLink))
buff.WriteString(fmt.Sprintf("Flag[%d]", i.Flag))
buff.WriteString(fmt.Sprintf("Reserved[%d]", i.Reserved))
buff.WriteString(fmt.Sprintf("Extents[%s]", i.Extents))
buff.WriteString(fmt.Sprintf("ObjExtents[%s]", i.ObjExtents))
buff.WriteString("}")
return buff.String()
}
// NewInode returns a new Inode instance with specified Inode ID, name and type.
// The AccessTime and ModifyTime will be set to the current time.
func NewInode(ino uint64, t uint32) *Inode {
ts := Now.GetCurrentTime().Unix()
i := &Inode{
Inode: ino,
Type: t,
Generation: 1,
CreateTime: ts,
AccessTime: ts,
ModifyTime: ts,
NLink: 1,
Extents: NewSortedExtents(),
ObjExtents: NewSortedObjExtents(),
}
if proto.IsDir(t) {
i.NLink = 2
}
return i
}
// Less tests whether the current Inode item is less than the given one.
// This method is necessary fot B-Tree item implementation.
func (i *Inode) Less(than BtreeItem) bool {
ino, ok := than.(*Inode)
return ok && i.Inode < ino.Inode
}
// Copy returns a copy of the inode.
func (i *Inode) Copy() BtreeItem {
newIno := NewInode(i.Inode, i.Type)
i.RLock()
newIno.Uid = i.Uid
newIno.Gid = i.Gid
newIno.Size = i.Size
newIno.Generation = i.Generation
newIno.CreateTime = i.CreateTime
newIno.ModifyTime = i.ModifyTime
newIno.AccessTime = i.AccessTime
if size := len(i.LinkTarget); size > 0 {
newIno.LinkTarget = make([]byte, size)
copy(newIno.LinkTarget, i.LinkTarget)
}
newIno.NLink = i.NLink
newIno.Flag = i.Flag
newIno.Reserved = i.Reserved
newIno.Extents = i.Extents.Clone()
newIno.ObjExtents = i.ObjExtents.Clone()
i.RUnlock()
return newIno
}
// MarshalToJSON is the wrapper of json.Marshal.
func (i *Inode) MarshalToJSON() ([]byte, error) {
i.RLock()
defer i.RUnlock()
return json.Marshal(i)
}
// Marshal marshals the inode into a byte array.
func (i *Inode) Marshal() (result []byte, err error) {
keyBytes := i.MarshalKey()
valBytes := i.MarshalValue()
keyLen := uint32(len(keyBytes))
valLen := uint32(len(valBytes))
buff := bytes.NewBuffer(make([]byte, 0, 128))
if err = binary.Write(buff, binary.BigEndian, keyLen); err != nil {
return
}
if _, err = buff.Write(keyBytes); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, valLen); err != nil {
return
}
if _, err = buff.Write(valBytes); err != nil {
return
}
result = buff.Bytes()
return
}
// Unmarshal unmarshals the inode.
func (i *Inode) Unmarshal(raw []byte) (err error) {
var (
keyLen uint32
valLen uint32
)
buff := bytes.NewBuffer(raw)
if err = binary.Read(buff, binary.BigEndian, &keyLen); err != nil {
return
}
keyBytes := make([]byte, keyLen)
if _, err = buff.Read(keyBytes); err != nil {
return
}
if err = i.UnmarshalKey(keyBytes); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &valLen); err != nil {
return
}
valBytes := make([]byte, valLen)
if _, err = buff.Read(valBytes); err != nil {
return
}
err = i.UnmarshalValue(valBytes)
return
}
// Marshal marshals the inodeBatch into a byte array.
func (i InodeBatch) Marshal() ([]byte, error) {
buff := bytes.NewBuffer(make([]byte, 0))
if err := binary.Write(buff, binary.BigEndian, uint32(len(i))); err != nil {
return nil, err
}
for _, inode := range i {
bs, err := inode.Marshal()
if err != nil {
return nil, err
}
if err = binary.Write(buff, binary.BigEndian, uint32(len(bs))); err != nil {
return nil, err
}
if _, err := buff.Write(bs); err != nil {
return nil, err
}
}
return buff.Bytes(), nil
}
// Unmarshal unmarshals the inodeBatch.
func InodeBatchUnmarshal(raw []byte) (InodeBatch, error) {
buff := bytes.NewBuffer(raw)
var batchLen uint32
if err := binary.Read(buff, binary.BigEndian, &batchLen); err != nil {
return nil, err
}
result := make(InodeBatch, 0, int(batchLen))
var dataLen uint32
for j := 0; j < int(batchLen); j++ {
if err := binary.Read(buff, binary.BigEndian, &dataLen); err != nil {
return nil, err
}
data := make([]byte, int(dataLen))
if _, err := buff.Read(data); err != nil {
return nil, err
}
ino := NewInode(0, 0)
if err := ino.Unmarshal(data); err != nil {
return nil, err
}
result = append(result, ino)
}
return result, nil
}
// MarshalKey marshals the exporterKey to bytes.
func (i *Inode) MarshalKey() (k []byte) {
k = make([]byte, 8)
binary.BigEndian.PutUint64(k, i.Inode)
return
}
// UnmarshalKey unmarshals the exporterKey from bytes.
func (i *Inode) UnmarshalKey(k []byte) (err error) {
i.Inode = binary.BigEndian.Uint64(k)
return
}
// MarshalValue marshals the value to bytes.
func (i *Inode) MarshalValue() (val []byte) {
var err error
buff := bytes.NewBuffer(make([]byte, 0, 128))
buff.Grow(64)
i.RLock()
if err = binary.Write(buff, binary.BigEndian, &i.Type); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.Uid); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.Gid); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.Size); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.Generation); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.CreateTime); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.AccessTime); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.ModifyTime); err != nil {
panic(err)
}
// write SymLink
symSize := uint32(len(i.LinkTarget))
if err = binary.Write(buff, binary.BigEndian, &symSize); err != nil {
panic(err)
}
if _, err = buff.Write(i.LinkTarget); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.NLink); err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, &i.Flag); err != nil {
panic(err)
}
if i.ObjExtents != nil && len(i.ObjExtents.eks) > 0 {
i.Reserved = InodeV2Flag
}
if err = binary.Write(buff, binary.BigEndian, &i.Reserved); err != nil {
panic(err)
}
if i.Reserved == InodeV2Flag {
// marshal ExtentsKey
extData, err := i.Extents.MarshalBinary()
if err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, uint32(len(extData))); err != nil {
panic(err)
}
if _, err = buff.Write(extData); err != nil {
panic(err)
}
// marshal ObjExtentsKey
objExtData, err := i.ObjExtents.MarshalBinary()
if err != nil {
panic(err)
}
if err = binary.Write(buff, binary.BigEndian, uint32(len(objExtData))); err != nil {
panic(err)
}
if _, err = buff.Write(objExtData); err != nil {
panic(err)
}
} else {
// marshal ExtentsKey
extData, err := i.Extents.MarshalBinary()
if err != nil {
panic(err)
}
if _, err = buff.Write(extData); err != nil {
panic(err)
}
}
val = buff.Bytes()
i.RUnlock()
return
}
// UnmarshalValue unmarshals the value from bytes.
func (i *Inode) UnmarshalValue(val []byte) (err error) {
buff := bytes.NewBuffer(val)
if err = binary.Read(buff, binary.BigEndian, &i.Type); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.Uid); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.Gid); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.Size); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.Generation); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.CreateTime); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.AccessTime); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.ModifyTime); err != nil {
return
}
// read symLink
symSize := uint32(0)
if err = binary.Read(buff, binary.BigEndian, &symSize); err != nil {
return
}
if symSize > 0 {
i.LinkTarget = make([]byte, symSize)
if _, err = io.ReadFull(buff, i.LinkTarget); err != nil {
return
}
}
if err = binary.Read(buff, binary.BigEndian, &i.NLink); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.Flag); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &i.Reserved); err != nil {
return
}
if buff.Len() == 0 {
return
}
// unmarshal ExtentsKey
if i.Extents == nil {
i.Extents = NewSortedExtents()
}
if i.Reserved == InodeV2Flag {
extSize := uint32(0)
if err = binary.Read(buff, binary.BigEndian, &extSize); err != nil {
return
}
if extSize > 0 {
extBytes := make([]byte, extSize)
if _, err = io.ReadFull(buff, extBytes); err != nil {
return
}
if err = i.Extents.UnmarshalBinary(extBytes); err != nil {
return
}
}
// unmarshal ObjExtentsKey
if i.ObjExtents == nil {
i.ObjExtents = NewSortedObjExtents()
}
ObjExtSize := uint32(0)
if err = binary.Read(buff, binary.BigEndian, &ObjExtSize); err != nil {
return
}
if ObjExtSize > 0 {
objExtBytes := make([]byte, ObjExtSize)
if _, err = io.ReadFull(buff, objExtBytes); err != nil {
return
}
if err = i.ObjExtents.UnmarshalBinary(objExtBytes); err != nil {
return
}
}
} else {
if err = i.Extents.UnmarshalBinary(buff.Bytes()); err != nil {
return
}
}
return
}
// AppendExtents append the extent to the btree.
func (i *Inode) AppendExtents(eks []proto.ExtentKey, ct int64, volType int) (delExtents []proto.ExtentKey) {
if proto.IsCold(volType) {
return
}
i.Lock()
defer i.Unlock()
for _, ek := range eks {
delItems := i.Extents.Append(ek)
size := i.Extents.Size()
if i.Size < size {
i.Size = size
}
delExtents = append(delExtents, delItems...)
}
i.Generation++
i.ModifyTime = ct
return
}
// AppendObjExtents append the extent to the btree.
func (i *Inode) AppendObjExtents(eks []proto.ObjExtentKey, ct int64) (err error) {
i.Lock()
defer i.Unlock()
for _, ek := range eks {
err = i.ObjExtents.Append(ek)
if err != nil {
return
}
size := i.ObjExtents.Size()
if i.Size < size {
i.Size = size
}
}
i.Generation++
i.ModifyTime = ct
return
}
func (i *Inode) AppendExtentWithCheck(ek proto.ExtentKey, ct int64, discardExtents []proto.ExtentKey, volType int) (delExtents []proto.ExtentKey, status uint8) {
i.Lock()
defer i.Unlock()
delExtents, status = i.Extents.AppendWithCheck(ek, discardExtents)
if status != proto.OpOk {
return
}
if proto.IsHot(volType) {
size := i.Extents.Size()
if i.Size < size {
i.Size = size
}
i.Generation++
i.ModifyTime = ct
}
return
}
func (i *Inode) ExtentsTruncate(length uint64, ct int64) (delExtents []proto.ExtentKey) {
i.Lock()
delExtents = i.Extents.Truncate(length)
i.Size = length
i.ModifyTime = ct
i.Generation++
i.Unlock()
return
}
// IncNLink increases the nLink value by one.
func (i *Inode) IncNLink() {
i.Lock()
i.NLink++
i.Unlock()
}
// DecNLink decreases the nLink value by one.
func (i *Inode) DecNLink() {
i.Lock()
if proto.IsDir(i.Type) && i.NLink == 2 {
i.NLink--
}
if i.NLink > 0 {
i.NLink--
}
i.Unlock()
}
// GetNLink returns the nLink value.
func (i *Inode) GetNLink() uint32 {
i.RLock()
defer i.RUnlock()
return i.NLink
}
func (i *Inode) IsTempFile() bool {
i.RLock()
ok := i.NLink == 0 && !proto.IsDir(i.Type)
i.RUnlock()
return ok
}
func (i *Inode) IsEmptyDir() bool {
i.RLock()
ok := (proto.IsDir(i.Type) && i.NLink <= 2)
i.RUnlock()
return ok
}
// SetDeleteMark set the deleteMark flag. TODO markDelete or deleteMark? markDelete has been used in datanode.
func (i *Inode) SetDeleteMark() {
i.Lock()
i.Flag |= DeleteMarkFlag
i.Unlock()
}
// ShouldDelete returns if the inode has been marked as deleted.
func (i *Inode) ShouldDelete() (ok bool) {
i.RLock()
ok = i.Flag&DeleteMarkFlag == DeleteMarkFlag
i.RUnlock()
return
}
// inode should delay remove if as 3 conditions:
// 1. DeleteMarkFlag is unset
// 2. NLink == 0
// 3. AccessTime is 7 days ago
func (i *Inode) ShouldDelayDelete() (ok bool) {
i.RLock()
ok = (i.Flag&DeleteMarkFlag != DeleteMarkFlag) &&
(i.NLink == 0) &&
time.Now().Unix()-i.AccessTime < InodeNLink0DelayDeleteSeconds
i.RUnlock()
return
}
// SetAttr sets the attributes of the inode.
func (i *Inode) SetAttr(req *SetattrRequest) {
i.Lock()
if req.Valid&proto.AttrMode != 0 {
i.Type = req.Mode
}
if req.Valid&proto.AttrUid != 0 {
i.Uid = req.Uid
}
if req.Valid&proto.AttrGid != 0 {
i.Gid = req.Gid
}
if req.Valid&proto.AttrAccessTime != 0 {
i.AccessTime = req.AccessTime
}
if req.Valid&proto.AttrModifyTime != 0 {
i.ModifyTime = req.ModifyTime
}
i.Unlock()
}
func (i *Inode) DoWriteFunc(fn func()) {
i.Lock()
defer i.Unlock()
fn()
}
// DoFunc executes the given function.
func (i *Inode) DoReadFunc(fn func()) {
i.RLock()
defer i.RUnlock()
fn()
}
// SetMtime sets mtime to the current time.
func (i *Inode) SetMtime() {
mtime := Now.GetCurrentTime().Unix()
i.Lock()
defer i.Unlock()
i.ModifyTime = mtime
}
// EmptyExtents clean the inode's extent list.
func (i *Inode) EmptyExtents(mtime int64) (delExtents []proto.ExtentKey) {
i.Lock()
defer i.Unlock()
// eks is safe because extents be reset next and eks is will not be visit except del routine
delExtents = i.Extents.eks
i.Extents = NewSortedExtents()
return delExtents
}
// EmptyExtents clean the inode's extent list.
func (i *Inode) CopyTinyExtents() (delExtents []proto.ExtentKey) {
i.RLock()
defer i.RUnlock()
return i.Extents.CopyTinyExtents()
}
// ReplaceExtents replace eks with curEks, delEks are which need to deleted.
// func (i *Inode) ReplaceExtents(curEks []proto.ExtentKey, mtime int64) (delEks []proto.ExtentKey) {
// i.Lock()
// defer i.Unlock()
// oldEks := i.Extents.eks
// delEks = make([]proto.ExtentKey, len(oldEks)-len(curEks))
// for _, key := range oldEks {
// exist := false
// for _, delKey := range curEks {
// if key.FileOffset == delKey.FileOffset && key.ExtentId == delKey.ExtentId &&
// key.ExtentOffset == delKey.ExtentOffset && key.PartitionId == delKey.PartitionId &&
// key.Size == delKey.Size {
// exist = true
// break
// }
// }
// if !exist {
// delEks = append(delEks, key)
// }
// }
// i.ModifyTime = mtime
// i.Generation++
// i.Extents.eks = curEks
// return
// }