aboutsummaryrefslogtreecommitdiff
path: root/weed/mount/filehandle_map.go
blob: e6e2d15c988147ba638fe1ffe198527884f546ac (plain)
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
package mount

import (
	"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
	"sync"
)

type FileHandleToInode struct {
	sync.RWMutex
	nextFh   FileHandleId
	inode2fh map[uint64]*FileHandle
	fh2inode map[FileHandleId]uint64
}

func NewFileHandleToInode() *FileHandleToInode {
	return &FileHandleToInode{
		inode2fh: make(map[uint64]*FileHandle),
		fh2inode: make(map[FileHandleId]uint64),
		nextFh:   0,
	}
}

func (i *FileHandleToInode) GetFileHandle(fh FileHandleId) *FileHandle {
	i.RLock()
	defer i.RUnlock()
	inode, found := i.fh2inode[fh]
	if found {
		return i.inode2fh[inode]
	}
	return nil
}

func (i *FileHandleToInode) FindFileHandle(inode uint64) (fh *FileHandle, found bool) {
	i.RLock()
	defer i.RUnlock()
	fh, found = i.inode2fh[inode]
	return
}

func (i *FileHandleToInode) AcquireFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry) *FileHandle {
	i.Lock()
	defer i.Unlock()
	fh, found := i.inode2fh[inode]
	if !found {
		fh = newFileHandle(wfs, i.nextFh, inode, entry)
		i.nextFh++
		i.inode2fh[inode] = fh
		i.fh2inode[fh.fh] = inode
	} else {
		fh.counter++
	}
	fh.entry = entry
	return fh
}

func (i *FileHandleToInode) ReleaseByInode(inode uint64) {
	i.Lock()
	defer i.Unlock()
	fh, found := i.inode2fh[inode]
	if found {
		fh.counter--
		if fh.counter <= 0 {
			delete(i.inode2fh, inode)
			delete(i.fh2inode, fh.fh)
			fh.Release()
		}
	}
}
func (i *FileHandleToInode) ReleaseByHandle(fh FileHandleId) {
	i.Lock()
	defer i.Unlock()
	inode, found := i.fh2inode[fh]
	if found {
		fhHandle, fhFound := i.inode2fh[inode]
		if !fhFound {
			delete(i.fh2inode, fh)
		} else {
			fhHandle.counter--
			if fhHandle.counter <= 0 {
				delete(i.inode2fh, inode)
				delete(i.fh2inode, fhHandle.fh)
				fhHandle.Release()
			}
		}

	}
}