seaweedfs/weed/mount/weedfs_file_mkrm.go

157 lines
4.0 KiB
Go
Raw Normal View History

2022-02-13 19:09:24 +08:00
package mount
import (
"context"
"fmt"
2023-02-01 10:54:46 +08:00
"syscall"
"time"
2022-02-13 19:09:24 +08:00
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
2022-02-13 19:09:24 +08:00
)
2022-02-13 20:22:02 +08:00
/**
* Create and open a file
*
* If the file does not exist, first create it with the specified
* mode, and then open it.
*
* If this method is not implemented or under Linux kernel
* versions earlier than 2.6.15, the mknod() and open() methods
* will be called instead.
*/
func (wfs *WFS) Create(cancel <-chan struct{}, in *fuse.CreateIn, name string, out *fuse.CreateOut) (code fuse.Status) {
2022-02-13 22:02:21 +08:00
// if implemented, need to use
// inode := wfs.inodeToPath.Lookup(entryFullPath)
// to ensure nlookup counter
2022-02-13 20:22:02 +08:00
return fuse.ENOSYS
}
/** Create a file node
*
* This is called for creation of all non-directory, non-symlink
* nodes. If the filesystem defines a create() method, then for
* regular files that will be called instead.
*/
2022-02-13 19:09:24 +08:00
func (wfs *WFS) Mknod(cancel <-chan struct{}, in *fuse.MknodIn, name string, out *fuse.EntryOut) (code fuse.Status) {
2022-03-06 14:10:43 +08:00
if wfs.IsOverQuota {
return fuse.Status(syscall.ENOSPC)
2022-03-06 14:10:43 +08:00
}
2022-02-13 19:09:24 +08:00
if s := checkName(name); s != fuse.OK {
return s
}
2022-02-25 16:53:27 +08:00
dirFullPath, code := wfs.inodeToPath.GetPath(in.NodeId)
if code != fuse.OK {
return
}
entryFullPath := dirFullPath.Child(name)
fileMode := toOsFileMode(in.Mode)
2022-03-14 15:03:29 +08:00
now := time.Now().Unix()
inode := wfs.inodeToPath.AllocateInode(entryFullPath, now)
2022-02-25 16:53:27 +08:00
2022-02-13 19:09:24 +08:00
newEntry := &filer_pb.Entry{
Name: name,
IsDirectory: false,
Attributes: &filer_pb.FuseAttributes{
Mtime: now,
Crtime: now,
FileMode: uint32(fileMode),
Uid: in.Uid,
Gid: in.Gid,
TtlSec: wfs.option.TtlSec,
Rdev: in.Rdev,
Inode: inode,
2022-02-13 19:09:24 +08:00
},
}
err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
wfs.mapPbIdFromLocalToFiler(newEntry)
defer wfs.mapPbIdFromFilerToLocal(newEntry)
request := &filer_pb.CreateEntryRequest{
Directory: string(dirFullPath),
Entry: newEntry,
Signatures: []int32{wfs.signature},
SkipCheckParentDirectory: true,
2022-02-13 19:09:24 +08:00
}
glog.V(1).Infof("mknod: %v", request)
if err := filer_pb.CreateEntry(client, request); err != nil {
glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
return err
}
if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
return fmt.Errorf("local mknod %s: %v", entryFullPath, err)
}
return nil
})
2022-02-17 00:38:51 +08:00
glog.V(3).Infof("mknod %s: %v", entryFullPath, err)
2022-02-13 19:09:24 +08:00
if err != nil {
return fuse.EIO
}
// this is to increase nlookup counter
2022-03-14 15:03:29 +08:00
inode = wfs.inodeToPath.Lookup(entryFullPath, newEntry.Attributes.Crtime, false, false, inode, true)
2022-02-13 19:09:24 +08:00
wfs.outputPbEntry(out, inode, newEntry)
return fuse.OK
}
2022-02-13 20:22:02 +08:00
/** Remove a file */
2022-02-13 19:09:24 +08:00
func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
if code != fuse.OK {
2022-02-18 17:10:53 +08:00
if code == fuse.ENOENT {
return fuse.OK
}
return code
}
2022-02-13 19:09:24 +08:00
entryFullPath := dirFullPath.Child(name)
2022-02-18 17:10:53 +08:00
entry, code := wfs.maybeLoadEntry(entryFullPath)
if code != fuse.OK {
if code == fuse.ENOENT {
return fuse.OK
}
return code
2022-02-13 19:09:24 +08:00
}
if wfs.wormEnabledForEntry(entryFullPath, entry) {
return fuse.EPERM
}
2022-02-13 19:09:24 +08:00
// first, ensure the filer store can correctly delete
glog.V(3).Infof("remove file: %v", entryFullPath)
isDeleteData := entry != nil && entry.HardLinkCounter <= 1
err := filer_pb.Remove(wfs, string(dirFullPath), name, isDeleteData, false, false, false, []int32{wfs.signature})
if err != nil {
glog.V(0).Infof("remove %s: %v", entryFullPath, err)
2022-02-18 17:10:53 +08:00
return fuse.OK
2022-02-13 19:09:24 +08:00
}
// then, delete meta cache
if err = wfs.metaCache.DeleteEntry(context.Background(), entryFullPath); err != nil {
glog.V(3).Infof("local DeleteEntry %s: %v", entryFullPath, err)
return fuse.EIO
}
wfs.inodeToPath.RemovePath(entryFullPath)
return fuse.OK
}