2023-10-05 01:28:41 +08:00
|
|
|
//go:build unix
|
|
|
|
|
|
|
|
package nfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-26 00:19:21 +08:00
|
|
|
"fmt"
|
2023-10-05 01:28:41 +08:00
|
|
|
"net"
|
2024-02-06 15:53:12 +08:00
|
|
|
"strings"
|
2023-10-05 01:28:41 +08:00
|
|
|
|
|
|
|
"github.com/go-git/go-billy/v5"
|
2024-01-26 00:19:21 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2024-06-25 18:18:39 +08:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
2023-10-05 01:28:41 +08:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/willscott/go-nfs"
|
|
|
|
nfshelper "github.com/willscott/go-nfs/helpers"
|
|
|
|
)
|
|
|
|
|
2024-06-25 18:18:39 +08:00
|
|
|
// Handler returns a NFS backing that exposes a given file system in response to all mount requests.
|
|
|
|
type Handler struct {
|
|
|
|
vfs *vfs.VFS
|
|
|
|
opt *Options
|
|
|
|
billyFS *FS
|
|
|
|
Cache
|
|
|
|
}
|
|
|
|
|
2024-06-24 23:32:09 +08:00
|
|
|
// NewHandler creates a handler for the provided filesystem
|
2024-06-25 18:18:39 +08:00
|
|
|
func NewHandler(vfs *vfs.VFS, opt *Options) (nfs.Handler, error) {
|
2024-06-24 23:32:09 +08:00
|
|
|
handler := &Handler{
|
2024-06-25 17:19:25 +08:00
|
|
|
vfs: vfs,
|
|
|
|
opt: opt,
|
|
|
|
billyFS: &FS{vfs: vfs},
|
2024-01-26 00:19:21 +08:00
|
|
|
}
|
|
|
|
handler.opt.HandleLimit = handler.opt.Limit()
|
2024-06-25 18:18:39 +08:00
|
|
|
err := handler.setCache()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to make cache: %w", err)
|
|
|
|
}
|
|
|
|
handler.Cache = nfshelper.NewCachingHandler(handler, handler.opt.HandleLimit)
|
|
|
|
nfs.SetLogger(&logIntercepter{Level: nfs.DebugLevel})
|
|
|
|
return handler, nil
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount backs Mount RPC Requests, allowing for access control policies.
|
2024-06-24 23:32:09 +08:00
|
|
|
func (h *Handler) Mount(ctx context.Context, conn net.Conn, req nfs.MountRequest) (status nfs.MountStatus, hndl billy.Filesystem, auths []nfs.AuthFlavor) {
|
2023-10-05 01:28:41 +08:00
|
|
|
auths = []nfs.AuthFlavor{nfs.AuthFlavorNull}
|
2024-06-25 17:19:25 +08:00
|
|
|
return nfs.MountStatusOk, h.billyFS, auths
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change provides an interface for updating file attributes.
|
2024-06-24 23:32:09 +08:00
|
|
|
func (h *Handler) Change(fs billy.Filesystem) billy.Change {
|
2023-10-05 01:28:41 +08:00
|
|
|
if c, ok := fs.(billy.Change); ok {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FSStat provides information about a filesystem.
|
2024-06-24 23:32:09 +08:00
|
|
|
func (h *Handler) FSStat(ctx context.Context, f billy.Filesystem, s *nfs.FSStat) error {
|
2023-10-05 01:28:41 +08:00
|
|
|
total, _, free := h.vfs.Statfs()
|
|
|
|
s.TotalSize = uint64(total)
|
|
|
|
s.FreeSize = uint64(free)
|
|
|
|
s.AvailableSize = uint64(free)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-25 18:18:39 +08:00
|
|
|
// ToHandle takes a file and represents it with an opaque handle to reference it.
|
|
|
|
// In stateless nfs (when it's serving a unix fs) this can be the device + inode
|
|
|
|
// but we can generalize with a stateful local cache of handed out IDs.
|
|
|
|
func (h *Handler) ToHandle(f billy.Filesystem, s []string) (b []byte) {
|
|
|
|
defer log.Trace("nfs", "path=%q", s)("handle=%X", &b)
|
|
|
|
return h.Cache.ToHandle(f, s)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
2024-06-25 18:18:39 +08:00
|
|
|
// FromHandle converts from an opaque handle to the file it represents
|
|
|
|
func (h *Handler) FromHandle(b []byte) (f billy.Filesystem, s []string, err error) {
|
|
|
|
defer log.Trace("nfs", "handle=%X", b)("path=%q, err=%v", &s, &err)
|
|
|
|
return h.Cache.FromHandle(b)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
2024-06-25 18:18:39 +08:00
|
|
|
// HandleLimit exports how many file handles can be safely stored by this cache.
|
2024-06-24 23:32:09 +08:00
|
|
|
func (h *Handler) HandleLimit() int {
|
2024-06-25 18:18:39 +08:00
|
|
|
return h.Cache.HandleLimit()
|
2024-01-26 00:19:21 +08:00
|
|
|
}
|
|
|
|
|
2024-06-25 18:18:39 +08:00
|
|
|
// Invalidate the handle passed - used on rename and delete
|
|
|
|
func (h *Handler) InvalidateHandle(f billy.Filesystem, b []byte) (err error) {
|
|
|
|
defer log.Trace("nfs", "handle=%X", b)("err=%v", &err)
|
|
|
|
return h.Cache.InvalidateHandle(f, b)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
2024-01-26 00:19:21 +08:00
|
|
|
|
|
|
|
// Limit overrides the --nfs-cache-handle-limit value if out-of-range
|
|
|
|
func (o *Options) Limit() int {
|
|
|
|
if o.HandleLimit < 0 {
|
|
|
|
return 1000000
|
|
|
|
}
|
|
|
|
if o.HandleLimit <= 5 {
|
|
|
|
return 5
|
|
|
|
}
|
|
|
|
return o.HandleLimit
|
|
|
|
}
|
|
|
|
|
2024-02-06 15:53:12 +08:00
|
|
|
// OnUnmountFunc registers a function to call when externally unmounted
|
|
|
|
var OnUnmountFunc func()
|
|
|
|
|
|
|
|
func onUnmount() {
|
|
|
|
fs.Infof(nil, "unmount detected")
|
|
|
|
if OnUnmountFunc != nil {
|
|
|
|
OnUnmountFunc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 23:32:09 +08:00
|
|
|
// logIntercepter intercepts noisy go-nfs logs and reroutes them to DEBUG
|
|
|
|
type logIntercepter struct {
|
2024-01-26 00:19:21 +08:00
|
|
|
Level nfs.LogLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intercept intercepts go-nfs logs and calls fs.Debugf instead
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Intercept(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
args = append([]interface{}{"[NFS DEBUG] "}, args...)
|
|
|
|
argsS := fmt.Sprint(args...)
|
|
|
|
fs.Debugf(nil, "%v", argsS)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interceptf intercepts go-nfs logs and calls fs.Debugf instead
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Interceptf(format string, args ...interface{}) {
|
2024-02-06 15:53:12 +08:00
|
|
|
argsS := fmt.Sprint(args...)
|
|
|
|
// bit of a workaround... the real fix is probably https://github.com/willscott/go-nfs/pull/28
|
|
|
|
if strings.Contains(argsS, "mount.Umnt") {
|
|
|
|
onUnmount()
|
|
|
|
}
|
|
|
|
|
2024-01-26 00:19:21 +08:00
|
|
|
fs.Debugf(nil, "[NFS DEBUG] "+format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug reroutes go-nfs Debug messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Debug(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf reroutes go-nfs Debugf messages to Interceptf
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Debugf(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error reroutes go-nfs Error messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Error(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf reroutes go-nfs Errorf messages to Interceptf
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Errorf(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal reroutes go-nfs Fatal messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Fatal(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf reroutes go-nfs Fatalf messages to Interceptf
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Fatalf(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel returns the nfs.LogLevel
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) GetLevel() nfs.LogLevel {
|
2024-01-26 00:19:21 +08:00
|
|
|
return l.Level
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info reroutes go-nfs Info messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Info(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Infof reroutes go-nfs Infof messages to Interceptf
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Infof(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Panic reroutes go-nfs Panic messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Panic(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Panicf reroutes go-nfs Panicf messages to Interceptf
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Panicf(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseLevel parses the nfs.LogLevel
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) ParseLevel(level string) (nfs.LogLevel, error) {
|
2024-01-26 00:19:21 +08:00
|
|
|
return nfs.Log.ParseLevel(level)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print reroutes go-nfs Print messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Print(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf reroutes go-nfs Printf messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Printf(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel sets the nfs.LogLevel
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) SetLevel(level nfs.LogLevel) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Level = level
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace reroutes go-nfs Trace messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Trace(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracef reroutes go-nfs Tracef messages to Interceptf
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Tracef(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn reroutes go-nfs Warn messages to Intercept
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Warn(args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Intercept(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf reroutes go-nfs Warnf messages to Interceptf
|
2024-06-24 23:32:09 +08:00
|
|
|
func (l *logIntercepter) Warnf(format string, args ...interface{}) {
|
2024-01-26 00:19:21 +08:00
|
|
|
l.Interceptf(format, args...)
|
|
|
|
}
|