From 0f64f5b9c8bea153171c8070f423b9910820bbb1 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 4 Apr 2021 21:40:58 -0700 Subject: [PATCH] mount: add readOnly option fix https://github.com/chrislusf/seaweedfs/issues/1961 --- weed/command/mount.go | 2 ++ weed/command/mount_std.go | 1 + weed/filesys/dir.go | 29 +++++++++++++++++++++++++++++ weed/filesys/dir_link.go | 8 ++++++++ weed/filesys/dir_rename.go | 4 ++++ weed/filesys/file.go | 12 ++++++++++++ weed/filesys/filehandle.go | 4 ++++ weed/filesys/wfs.go | 1 + 8 files changed, 61 insertions(+) diff --git a/weed/command/mount.go b/weed/command/mount.go index 1e5d106fc..5811f0b99 100644 --- a/weed/command/mount.go +++ b/weed/command/mount.go @@ -25,6 +25,7 @@ type MountOptions struct { volumeServerAccess *string uidMap *string gidMap *string + readOnly *bool } var ( @@ -55,6 +56,7 @@ func init() { mountOptions.volumeServerAccess = cmdMount.Flag.String("volumeServerAccess", "direct", "access volume servers by [direct|publicUrl|filerProxy]") mountOptions.uidMap = cmdMount.Flag.String("map.uid", "", "map local uid to uid on filer, comma-separated :") mountOptions.gidMap = cmdMount.Flag.String("map.gid", "", "map local gid to gid on filer, comma-separated :") + mountOptions.readOnly = cmdMount.Flag.Bool("readOnly", false, "read only") mountCpuProfile = cmdMount.Flag.String("cpuprofile", "", "cpu profile output file") mountMemProfile = cmdMount.Flag.String("memprofile", "", "memory profile output file") diff --git a/weed/command/mount_std.go b/weed/command/mount_std.go index f6f7b1d55..8e5b7a483 100644 --- a/weed/command/mount_std.go +++ b/weed/command/mount_std.go @@ -203,6 +203,7 @@ func RunMount(option *MountOptions, umask os.FileMode) bool { VolumeServerAccess: *mountOptions.volumeServerAccess, Cipher: cipher, UidGidMapper: uidGidMapper, + ReadOnly: *option.readOnly, }) // mount diff --git a/weed/filesys/dir.go b/weed/filesys/dir.go index 33e1a0a3a..46457f858 100644 --- a/weed/filesys/dir.go +++ b/weed/filesys/dir.go @@ -128,6 +128,10 @@ func (dir *Dir) newDirectory(fullpath util.FullPath, entry *filer_pb.Entry) fs.N func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) { + if dir.wfs.option.ReadOnly { + return nil, nil, fuse.EPERM + } + request, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, req.Flags&fuse.OpenExclusive != 0) if err != nil { @@ -148,6 +152,10 @@ func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest, func (dir *Dir) Mknod(ctx context.Context, req *fuse.MknodRequest) (fs.Node, error) { + if dir.wfs.option.ReadOnly { + return nil, fuse.EPERM + } + request, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, false) if err != nil { @@ -202,6 +210,10 @@ func (dir *Dir) doCreateEntry(name string, mode os.FileMode, uid, gid uint32, ex func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) { + if dir.wfs.option.ReadOnly { + return nil, fuse.EPERM + } + glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name) newEntry := &filer_pb.Entry{ @@ -356,6 +368,11 @@ func findFileType(mode uint16) fuse.DirentType { func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { + if dir.wfs.option.ReadOnly { + return fuse.EPERM + } + + if !req.Dir { return dir.removeOneFile(req) } @@ -429,6 +446,10 @@ func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error { func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error { + if dir.wfs.option.ReadOnly { + return fuse.EPERM + } + glog.V(4).Infof("%v dir setattr %+v", dir.FullPath(), req) if err := dir.maybeLoadEntry(); err != nil { @@ -457,6 +478,10 @@ func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fus func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error { + if dir.wfs.option.ReadOnly { + return fuse.EPERM + } + glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name) if err := dir.maybeLoadEntry(); err != nil { @@ -473,6 +498,10 @@ func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error { func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error { + if dir.wfs.option.ReadOnly { + return fuse.EPERM + } + glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name) if err := dir.maybeLoadEntry(); err != nil { diff --git a/weed/filesys/dir_link.go b/weed/filesys/dir_link.go index 606e52fcb..6266e492d 100644 --- a/weed/filesys/dir_link.go +++ b/weed/filesys/dir_link.go @@ -24,6 +24,10 @@ const ( func (dir *Dir) Link(ctx context.Context, req *fuse.LinkRequest, old fs.Node) (fs.Node, error) { + if dir.wfs.option.ReadOnly { + return nil, fuse.EPERM + } + oldFile, ok := old.(*File) if !ok { glog.Errorf("old node is not a file: %+v", old) @@ -105,6 +109,10 @@ func (dir *Dir) Link(ctx context.Context, req *fuse.LinkRequest, old fs.Node) (f func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) { + if dir.wfs.option.ReadOnly { + return nil, fuse.EPERM + } + glog.V(4).Infof("Symlink: %v/%v to %v", dir.FullPath(), req.NewName, req.Target) request := &filer_pb.CreateEntryRequest{ diff --git a/weed/filesys/dir_rename.go b/weed/filesys/dir_rename.go index d2acad4b2..28316c3bd 100644 --- a/weed/filesys/dir_rename.go +++ b/weed/filesys/dir_rename.go @@ -13,6 +13,10 @@ import ( func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error { + if dir.wfs.option.ReadOnly { + return fuse.EPERM + } + newDir := newDirectory.(*Dir) newPath := util.NewFullPath(newDir.FullPath(), req.NewName) diff --git a/weed/filesys/file.go b/weed/filesys/file.go index 58605f2f1..2433be590 100644 --- a/weed/filesys/file.go +++ b/weed/filesys/file.go @@ -110,6 +110,10 @@ func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.Op func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error { + if file.wfs.option.ReadOnly { + return fuse.EPERM + } + glog.V(4).Infof("%v file setattr %+v", file.fullpath(), req) entry, err := file.maybeLoadEntry(ctx) @@ -200,6 +204,10 @@ func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *f func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error { + if file.wfs.option.ReadOnly { + return fuse.EPERM + } + glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name) entry, err := file.maybeLoadEntry(ctx) @@ -217,6 +225,10 @@ func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error { + if file.wfs.option.ReadOnly { + return fuse.EPERM + } + glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name) entry, err := file.maybeLoadEntry(ctx) diff --git a/weed/filesys/filehandle.go b/weed/filesys/filehandle.go index 6d72e62ce..4419888c4 100644 --- a/weed/filesys/filehandle.go +++ b/weed/filesys/filehandle.go @@ -154,6 +154,10 @@ func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, error) { // Write to the file handle func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error { + if fh.f.wfs.option.ReadOnly { + return fuse.EPERM + } + fh.Lock() defer fh.Unlock() diff --git a/weed/filesys/wfs.go b/weed/filesys/wfs.go index c6d9080a1..ba5eb4b6b 100644 --- a/weed/filesys/wfs.go +++ b/weed/filesys/wfs.go @@ -43,6 +43,7 @@ type Option struct { DataCenter string EntryCacheTtl time.Duration Umask os.FileMode + ReadOnly bool MountUid uint32 MountGid uint32