2022-02-12 09:54:16 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2022-02-12 09:54:16 +00:00
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (wfs *WFS) GetAttr(cancel <-chan struct{}, input *fuse.GetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
|
|
|
|
if input.NodeId == 1 {
|
|
|
|
wfs.setRootAttr(out)
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
|
2022-07-25 01:24:34 +00:00
|
|
|
inode := input.NodeId
|
2022-08-04 08:35:18 +00:00
|
|
|
_, _, entry, status := wfs.maybeReadEntry(inode)
|
2022-02-24 09:59:37 +00:00
|
|
|
if status == fuse.OK {
|
|
|
|
out.AttrValid = 1
|
2023-01-03 07:20:45 +00:00
|
|
|
wfs.setAttrByPbEntry(&out.Attr, inode, entry, true)
|
2022-02-12 09:54:16 +00:00
|
|
|
return status
|
2022-02-24 09:59:37 +00:00
|
|
|
} else {
|
2022-07-25 01:24:34 +00:00
|
|
|
if fh, found := wfs.fhmap.FindFileHandle(inode); found {
|
2022-02-24 09:59:37 +00:00
|
|
|
out.AttrValid = 1
|
2023-01-03 07:20:45 +00:00
|
|
|
wfs.setAttrByPbEntry(&out.Attr, inode, fh.entry.GetEntry(), true)
|
2022-02-24 09:59:37 +00:00
|
|
|
out.Nlink = 0
|
|
|
|
return fuse.OK
|
|
|
|
}
|
2022-02-12 09:54:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 09:59:37 +00:00
|
|
|
return status
|
2022-02-12 09:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
|
2022-02-13 07:08:56 +00:00
|
|
|
|
2022-03-06 06:10:43 +00:00
|
|
|
if wfs.IsOverQuota {
|
2022-03-07 01:04:21 +00:00
|
|
|
return fuse.Status(syscall.ENOSPC)
|
2022-03-06 06:10:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 08:35:18 +00:00
|
|
|
path, fh, entry, status := wfs.maybeReadEntry(input.NodeId)
|
2022-02-13 07:08:56 +00:00
|
|
|
if status != fuse.OK {
|
|
|
|
return status
|
|
|
|
}
|
2022-12-05 20:32:27 +00:00
|
|
|
if fh != nil {
|
|
|
|
fh.entryLock.Lock()
|
|
|
|
defer fh.entryLock.Unlock()
|
|
|
|
}
|
2022-02-13 07:08:56 +00:00
|
|
|
|
2022-07-22 22:33:37 +00:00
|
|
|
if size, ok := input.GetSize(); ok && entry != nil {
|
2022-11-15 14:33:36 +00:00
|
|
|
glog.V(4).Infof("%v setattr set size=%v chunks=%d", path, size, len(entry.GetChunks()))
|
2022-02-14 09:36:10 +00:00
|
|
|
if size < filer.FileSize(entry) {
|
|
|
|
// fmt.Printf("truncate %v \n", fullPath)
|
|
|
|
var chunks []*filer_pb.FileChunk
|
|
|
|
var truncatedChunks []*filer_pb.FileChunk
|
2022-11-15 14:33:36 +00:00
|
|
|
for _, chunk := range entry.GetChunks() {
|
2022-02-14 09:36:10 +00:00
|
|
|
int64Size := int64(chunk.Size)
|
|
|
|
if chunk.Offset+int64Size > int64(size) {
|
|
|
|
// this chunk is truncated
|
|
|
|
int64Size = int64(size) - chunk.Offset
|
|
|
|
if int64Size > 0 {
|
|
|
|
chunks = append(chunks, chunk)
|
|
|
|
glog.V(4).Infof("truncated chunk %+v from %d to %d\n", chunk.GetFileIdString(), chunk.Size, int64Size)
|
|
|
|
chunk.Size = uint64(int64Size)
|
|
|
|
} else {
|
|
|
|
glog.V(4).Infof("truncated whole chunk %+v\n", chunk.GetFileIdString())
|
|
|
|
truncatedChunks = append(truncatedChunks, chunk)
|
|
|
|
}
|
2022-08-06 20:58:45 +00:00
|
|
|
} else {
|
|
|
|
chunks = append(chunks, chunk)
|
2022-02-14 09:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// set the new chunks and reset entry cache
|
|
|
|
entry.Chunks = chunks
|
|
|
|
if fh != nil {
|
2023-01-03 07:20:45 +00:00
|
|
|
fh.entryChunkGroup.SetChunks(chunks)
|
2022-02-14 09:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
entry.Attributes.Mtime = time.Now().Unix()
|
|
|
|
entry.Attributes.FileSize = size
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:08:56 +00:00
|
|
|
if mode, ok := input.GetMode(); ok {
|
2022-02-24 09:59:37 +00:00
|
|
|
// glog.V(4).Infof("setAttr mode %o", mode)
|
2022-02-24 09:31:04 +00:00
|
|
|
entry.Attributes.FileMode = chmod(entry.Attributes.FileMode, mode)
|
2022-03-12 20:10:56 +00:00
|
|
|
if input.NodeId == 1 {
|
|
|
|
wfs.option.MountMode = os.FileMode(chmod(uint32(wfs.option.MountMode), mode))
|
|
|
|
}
|
2022-02-13 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if uid, ok := input.GetUID(); ok {
|
|
|
|
entry.Attributes.Uid = uid
|
2022-03-12 20:10:56 +00:00
|
|
|
if input.NodeId == 1 {
|
|
|
|
wfs.option.MountUid = uid
|
|
|
|
}
|
2022-02-13 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if gid, ok := input.GetGID(); ok {
|
|
|
|
entry.Attributes.Gid = gid
|
2022-03-12 20:10:56 +00:00
|
|
|
if input.NodeId == 1 {
|
|
|
|
wfs.option.MountGid = gid
|
|
|
|
}
|
2022-02-13 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 08:47:02 +00:00
|
|
|
if atime, ok := input.GetATime(); ok {
|
|
|
|
entry.Attributes.Mtime = atime.Unix()
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:56:45 +00:00
|
|
|
if mtime, ok := input.GetMTime(); ok {
|
|
|
|
entry.Attributes.Mtime = mtime.Unix()
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:08:56 +00:00
|
|
|
out.AttrValid = 1
|
2023-01-03 07:20:45 +00:00
|
|
|
size, includeSize := input.GetSize()
|
|
|
|
if includeSize {
|
|
|
|
out.Attr.Size = size
|
|
|
|
}
|
|
|
|
wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry, !includeSize)
|
2022-02-13 07:08:56 +00:00
|
|
|
|
2022-02-14 09:36:10 +00:00
|
|
|
if fh != nil {
|
|
|
|
fh.dirtyMetadata = true
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:08:56 +00:00
|
|
|
return wfs.saveEntry(path, entry)
|
|
|
|
|
2022-02-12 09:54:16 +00:00
|
|
|
}
|
2022-02-13 08:58:46 +00:00
|
|
|
|
2022-02-12 09:54:16 +00:00
|
|
|
func (wfs *WFS) setRootAttr(out *fuse.AttrOut) {
|
2022-02-13 06:21:30 +00:00
|
|
|
now := uint64(time.Now().Unix())
|
2022-02-12 09:54:16 +00:00
|
|
|
out.AttrValid = 119
|
|
|
|
out.Ino = 1
|
|
|
|
setBlksize(&out.Attr, blockSize)
|
|
|
|
out.Uid = wfs.option.MountUid
|
|
|
|
out.Gid = wfs.option.MountGid
|
|
|
|
out.Mtime = now
|
|
|
|
out.Ctime = now
|
|
|
|
out.Atime = now
|
2022-02-24 09:31:04 +00:00
|
|
|
out.Mode = toSyscallType(os.ModeDir) | uint32(wfs.option.MountMode)
|
2022-02-12 09:54:16 +00:00
|
|
|
out.Nlink = 1
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
func (wfs *WFS) setAttrByPbEntry(out *fuse.Attr, inode uint64, entry *filer_pb.Entry, calculateSize bool) {
|
2022-02-12 09:54:16 +00:00
|
|
|
out.Ino = inode
|
2023-01-10 09:07:34 +00:00
|
|
|
out.Blocks = (out.Size + blockSize - 1) / blockSize
|
|
|
|
setBlksize(out, blockSize)
|
|
|
|
if entry == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-07-25 01:24:34 +00:00
|
|
|
if entry.Attributes != nil && entry.Attributes.Inode != 0 {
|
|
|
|
out.Ino = entry.Attributes.Inode
|
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
if calculateSize {
|
|
|
|
out.Size = filer.FileSize(entry)
|
|
|
|
}
|
2022-07-27 06:12:25 +00:00
|
|
|
if entry.FileMode()&os.ModeSymlink != 0 {
|
|
|
|
out.Size = uint64(len(entry.Attributes.SymlinkTarget))
|
|
|
|
}
|
2022-04-06 17:01:45 +00:00
|
|
|
out.Mtime = uint64(entry.Attributes.Mtime)
|
|
|
|
out.Ctime = uint64(entry.Attributes.Mtime)
|
|
|
|
out.Atime = uint64(entry.Attributes.Mtime)
|
|
|
|
out.Mode = toSyscallMode(os.FileMode(entry.Attributes.FileMode))
|
2022-02-12 09:54:16 +00:00
|
|
|
if entry.HardLinkCounter > 0 {
|
|
|
|
out.Nlink = uint32(entry.HardLinkCounter)
|
2022-02-13 06:21:30 +00:00
|
|
|
} else {
|
|
|
|
out.Nlink = 1
|
2022-02-12 09:54:16 +00:00
|
|
|
}
|
2022-04-06 17:01:45 +00:00
|
|
|
out.Uid = entry.Attributes.Uid
|
|
|
|
out.Gid = entry.Attributes.Gid
|
|
|
|
out.Rdev = entry.Attributes.Rdev
|
2022-02-12 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.Entry) {
|
|
|
|
out.Ino = inode
|
2022-02-12 13:27:16 +00:00
|
|
|
out.Size = entry.FileSize
|
2022-07-27 06:12:25 +00:00
|
|
|
if entry.Mode&os.ModeSymlink != 0 {
|
|
|
|
out.Size = uint64(len(entry.SymlinkTarget))
|
|
|
|
}
|
2022-02-13 06:21:30 +00:00
|
|
|
out.Blocks = (out.Size + blockSize - 1) / blockSize
|
2022-02-12 13:27:16 +00:00
|
|
|
setBlksize(out, blockSize)
|
|
|
|
out.Atime = uint64(entry.Attr.Mtime.Unix())
|
2022-02-12 10:48:44 +00:00
|
|
|
out.Mtime = uint64(entry.Attr.Mtime.Unix())
|
|
|
|
out.Ctime = uint64(entry.Attr.Mtime.Unix())
|
2022-02-24 09:31:04 +00:00
|
|
|
out.Mode = toSyscallMode(entry.Attr.Mode)
|
2022-02-12 10:48:44 +00:00
|
|
|
if entry.HardLinkCounter > 0 {
|
|
|
|
out.Nlink = uint32(entry.HardLinkCounter)
|
2022-02-13 06:21:30 +00:00
|
|
|
} else {
|
|
|
|
out.Nlink = 1
|
2022-02-12 10:48:44 +00:00
|
|
|
}
|
2022-02-12 13:27:16 +00:00
|
|
|
out.Uid = entry.Attr.Uid
|
|
|
|
out.Gid = entry.Attr.Gid
|
2022-02-24 22:51:25 +00:00
|
|
|
out.Rdev = entry.Attr.Rdev
|
2022-02-12 13:27:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 09:34:19 +00:00
|
|
|
func (wfs *WFS) outputPbEntry(out *fuse.EntryOut, inode uint64, entry *filer_pb.Entry) {
|
|
|
|
out.NodeId = inode
|
|
|
|
out.Generation = 1
|
|
|
|
out.EntryValid = 1
|
|
|
|
out.AttrValid = 1
|
2023-01-03 07:20:45 +00:00
|
|
|
wfs.setAttrByPbEntry(&out.Attr, inode, entry, true)
|
2022-02-13 09:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wfs *WFS) outputFilerEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) {
|
2022-02-13 06:21:30 +00:00
|
|
|
out.NodeId = inode
|
|
|
|
out.Generation = 1
|
2022-02-12 13:27:16 +00:00
|
|
|
out.EntryValid = 1
|
|
|
|
out.AttrValid = 1
|
|
|
|
wfs.setAttrByFilerEntry(&out.Attr, inode, entry)
|
2022-02-12 09:54:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 09:31:04 +00:00
|
|
|
func chmod(existing uint32, mode uint32) uint32 {
|
|
|
|
return existing&^07777 | mode&07777
|
2022-02-12 09:59:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 09:31:04 +00:00
|
|
|
func toSyscallMode(mode os.FileMode) uint32 {
|
|
|
|
return toSyscallType(mode) | uint32(mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSyscallType(mode os.FileMode) uint32 {
|
2022-02-12 09:59:36 +00:00
|
|
|
switch mode & os.ModeType {
|
2022-02-12 09:54:16 +00:00
|
|
|
case os.ModeDir:
|
|
|
|
return syscall.S_IFDIR
|
|
|
|
case os.ModeSymlink:
|
|
|
|
return syscall.S_IFLNK
|
|
|
|
case os.ModeNamedPipe:
|
|
|
|
return syscall.S_IFIFO
|
|
|
|
case os.ModeSocket:
|
|
|
|
return syscall.S_IFSOCK
|
|
|
|
case os.ModeDevice:
|
|
|
|
return syscall.S_IFBLK
|
|
|
|
case os.ModeCharDevice:
|
|
|
|
return syscall.S_IFCHR
|
|
|
|
default:
|
|
|
|
return syscall.S_IFREG
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 11:09:24 +00:00
|
|
|
|
2022-02-24 09:31:04 +00:00
|
|
|
func toOsFileType(mode uint32) os.FileMode {
|
2022-02-13 11:09:24 +00:00
|
|
|
switch mode & (syscall.S_IFMT & 0xffff) {
|
|
|
|
case syscall.S_IFDIR:
|
|
|
|
return os.ModeDir
|
|
|
|
case syscall.S_IFLNK:
|
|
|
|
return os.ModeSymlink
|
|
|
|
case syscall.S_IFIFO:
|
|
|
|
return os.ModeNamedPipe
|
|
|
|
case syscall.S_IFSOCK:
|
|
|
|
return os.ModeSocket
|
|
|
|
case syscall.S_IFBLK:
|
|
|
|
return os.ModeDevice
|
|
|
|
case syscall.S_IFCHR:
|
|
|
|
return os.ModeCharDevice
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 09:31:04 +00:00
|
|
|
func toOsFileMode(mode uint32) os.FileMode {
|
|
|
|
return toOsFileType(mode) | os.FileMode(mode&07777)
|
2022-02-13 11:09:24 +00:00
|
|
|
}
|