mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-05-23 10:11:28 +00:00
* fix(nfs): accept any MOUNT3 dirpath, mirroring rclone's permissive policy
weed nfs has exactly one export per process, so the MOUNT3 dirpath
argument has no second export to disambiguate against. Strict
comparison only translated PV-path typos into the inconsistent
"mount succeeds but empty" / "mount fails completely" split that
operators see.
Match rclone's serve nfs Handler.Mount: ignore the dirpath, log an INFO
line when it differs from the configured export, and always serve the
export root. Apply the same change to the UDP MOUNT3 path so kernel
clients defaulting to mountproto=udp see identical behaviour. Access
control still goes through -allowedClients / -ip.bind, and file-handle
scoping in FromHandle is unchanged so handles still cannot escape the
export.
Replace the prior single-path reject tests with table tests covering
the shapes operators commonly hit: root, parent, sibling, deeper child,
unrelated, empty, relative form, exact match, and trailing slash, at
the Handler.Mount, UDP MOUNT3, and full RPC layers.
* feat(nfs): mount at subdirectory when MOUNT3 dirpath is under the export
Make the dirpath argument meaningful when the client asks for a subtree
of the configured export. With -filer.path=/buckets, a client mounting
<server>:/buckets/data lands directly inside /buckets/data instead of
at the export root.
- dirpath equals the export root: serve the export root.
- dirpath strictly under the export, directory entry: serve that
subdirectory; the returned filehandle encodes its inode.
- dirpath strictly under the export, missing or non-directory: reject
with NoEnt or NotDir.
- dirpath outside the export: keep the rclone-style fallback to the
export root.
TCP returns a sub-rooted seaweedFileSystem and lets go-nfs's onMount
call ToHandle to encode the FH; UDP encodes the FH itself. FromHandle
is unchanged: handles are content-addressed by inode and resolve via
the inode index, so they remain stable across mounts and across
process restarts.
The trimmed permissive tests keep their outside-export shapes; new
subexport tests cover under-export directories, missing entries, and
non-directory entries on Handler.Mount, the UDP MOUNT3 wire, and
through the full RPC stack.
* nfs: propagate request context through MOUNT3 resolution
Mount now accepts the gonfs context and threads it through
resolveMountFilesystem and lstatExportStatus so a slow filer call
during MOUNT cannot outlive a cancelled or timed-out request.
lstatExportStatus uses fileInfoForVirtualPath(ctx, "/") directly
instead of billy.Filesystem.Lstat, which would otherwise drop the
context on the floor by calling fileInfoForVirtualPathWithOptions
with context.Background().
Lower the successful subexport-mount log from V(0) to V(1). The
fallback log stays at V(0) so operator typos still surface; the
success line is per-mount churn that adds up on NFS-CSI deployments.
* nfs: mirror TCP defensive checks on the UDP MOUNT3 path
Two transport-parity bugs the rabbit caught:
(1) The exact-export-root and outside-export branches were returning
MNT3_OK unconditionally, while the TCP handler runs lstatExportStatus
on those same branches. If the configured -filer.path has been
removed from the filer, TCP returns NoEnt/ServerFault but UDP would
still hand out a synthetic root handle pointing at nothing. Add
rootMountStatus as the UDP analogue and call it on both branches.
(2) resolveSubexportFileHandle did filer I/O on the single UDP serve
loop with context.Background(). One slow filer round-trip would
block every later MOUNT packet. Wrap each MOUNT call's filer work in
context.WithTimeout(mountUDPLookupTimeout) and thread that ctx
through both rootMountStatus and resolveSubexportFileHandle.
Lower the successful subexport log to V(1) to match the TCP side.
* nfs: assert TCP/UDP MOUNT3 produce byte-identical filehandles
The existing UDP subexport assertions only checked the decoded inode
and kind. A regression that drifted the generation, exportID, or
encoding format on one transport but not the other would have slipped
through. Build the TCP Handler from the same Server, drive its Mount
with the same dirpath, and require ToHandle to match the raw UDP FH
bytes for every OK case.
* nfs: take MOUNT3 dirpath as string in resolveMountFilesystem
Convert req.Dirpath to string once at the call site instead of
sprinkling string(...) casts through every log line and conversion
inside the function. Behavior unchanged.
* nfs: share rootFS lifecycle between TCP and UDP MOUNT handlers
Server.rootFilesystem() lazily constructs the seaweedFileSystem rooted
at the configured export the first time anything asks for it, then
hands the same instance to every subsequent caller. newHandler() and
mountUDPServer.rootMountStatus() now both go through it, so:
- Both transports observe the same chunk reader cache and chunk
invalidator without depending on call order during startup.
- The UDP defensive Lstat doesn't allocate a fresh wrapper per
MOUNT request anymore; one struct lives for the life of the
Server.
The sub-rooted seaweedFileSystem the subexport branch builds in
resolveSubexportFileHandle is still per-request because actualRoot
varies with the requested dirpath.
* nfs: drive rootFilesystem before reading sharedReaderCache on UDP
The UDP listener is started before serve() calls newHandler(), so an
under-export MOUNT3 request can reach resolveSubexportFileHandle before
Server.sharedReaderCache has been assigned. Reading it directly would
hand newSeaweedFileSystem a nil cache and the sub-fs would build a
throwaway ReaderCache that never gets shared with the TCP path.
Take rootFS off Server.rootFilesystem() (which drives the sync.Once
that initializes the shared cache) and read readerCache off that
instead, so subexport sub-fs instances always share the same reader
cache as rootFS regardless of which transport sees the first MOUNT.
* nfs: collapse exact-match and outside-export MOUNT branches
The two branches return the same filesystem (export root) and the
same status; only the log line differs. Combine the conditions and
guard the fallback log inline. Behavior unchanged.
251 lines
8.0 KiB
Go
251 lines
8.0 KiB
Go
package nfs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
|
gonfs "github.com/willscott/go-nfs"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
type Option struct {
|
|
Filer pb.ServerAddress
|
|
BindIp string
|
|
Port int
|
|
FilerRootPath string
|
|
ReadOnly bool
|
|
AllowedClients []string
|
|
VolumeServerAccess string
|
|
GrpcDialOption grpc.DialOption
|
|
// PortmapBind, when non-empty, enables a built-in portmap v2 responder
|
|
// on <PortmapBind>:111 advertising the NFS v3 and MOUNT v3 services at
|
|
// Port. Empty (the default) disables portmap; clients must then bypass
|
|
// portmap with mount -o port=,mountport=,proto=tcp,mountproto=tcp.
|
|
PortmapBind string
|
|
}
|
|
|
|
type Server struct {
|
|
option *Option
|
|
exportRoot util.FullPath
|
|
exportID uint32
|
|
signature int32
|
|
handleLimit int
|
|
clientAuthorizer *clientAuthorizer
|
|
sharedReaderCache *filer.ReaderCache
|
|
chunkInvalidator chunkInvalidator
|
|
filerClient *wdclient.FilerClient
|
|
newUploader func() (chunkUploader, error)
|
|
withFilerClient filerClientExecutor
|
|
withInternalClient internalClientExecutor
|
|
|
|
rootFSOnce sync.Once
|
|
rootFS *seaweedFileSystem
|
|
}
|
|
|
|
func NewServer(option *Option) (*Server, error) {
|
|
if option == nil {
|
|
return nil, errors.New("nfs option is required")
|
|
}
|
|
if option.Port <= 0 {
|
|
return nil, fmt.Errorf("nfs port must be positive: %d", option.Port)
|
|
}
|
|
if option.FilerRootPath == "" {
|
|
option.FilerRootPath = "/"
|
|
}
|
|
if option.VolumeServerAccess == "" {
|
|
option.VolumeServerAccess = "direct"
|
|
}
|
|
if option.GrpcDialOption == nil {
|
|
option.GrpcDialOption = grpc.WithTransportCredentials(insecure.NewCredentials())
|
|
}
|
|
clientAuthorizer, err := newClientAuthorizer(option.AllowedClients)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var filerClient *wdclient.FilerClient
|
|
if option.VolumeServerAccess != "filerProxy" {
|
|
var opts *wdclient.FilerClientOption
|
|
if option.VolumeServerAccess == "publicUrl" {
|
|
opts = &wdclient.FilerClientOption{UrlPreference: wdclient.PreferPublicUrl}
|
|
}
|
|
filerClient = wdclient.NewFilerClient([]pb.ServerAddress{option.Filer}, option.GrpcDialOption, "", opts)
|
|
}
|
|
exportRoot := normalizeExportRoot(util.FullPath(option.FilerRootPath))
|
|
signature := util.RandomInt32()
|
|
return &Server{
|
|
option: option,
|
|
exportRoot: exportRoot,
|
|
exportID: exportIDForRoot(exportRoot),
|
|
signature: signature,
|
|
handleLimit: 1 << 20,
|
|
clientAuthorizer: clientAuthorizer,
|
|
filerClient: filerClient,
|
|
newUploader: newChunkUploader,
|
|
withFilerClient: newFilerClientExecutor(option, signature),
|
|
withInternalClient: newInternalClientExecutor(option, signature),
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) Start() error {
|
|
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.option.BindIp, s.option.Port))
|
|
if err != nil {
|
|
return fmt.Errorf("listen nfs on %s:%d: %w", s.option.BindIp, s.option.Port, err)
|
|
}
|
|
|
|
// MOUNT v3 over UDP runs alongside the TCP NFS listener on the same
|
|
// port. The kernel default for mountproto is UDP in many setups, so
|
|
// without this responder a plain `mount -t nfs <host>:<export> /mnt`
|
|
// gets EPROTONOSUPPORT during the MOUNT phase even though the TCP
|
|
// NFS path is fine.
|
|
mountUDP := newMountUDPServer(s.option.BindIp, s.option.Port, s)
|
|
if err := mountUDP.Start(); err != nil {
|
|
_ = listener.Close()
|
|
return fmt.Errorf("start mount udp: %w", err)
|
|
}
|
|
defer func() {
|
|
_ = mountUDP.Close()
|
|
}()
|
|
glog.V(0).Infof("MOUNT v3 UDP responder listening on %s:%d", s.option.BindIp, s.option.Port)
|
|
|
|
var portmap *portmapServer
|
|
if s.option.PortmapBind != "" {
|
|
portmap = newPortmapServer(s.option.PortmapBind, portmapPort, uint32(s.option.Port))
|
|
if pmErr := portmap.Start(); pmErr != nil {
|
|
_ = listener.Close()
|
|
return fmt.Errorf("start portmap: %w", pmErr)
|
|
}
|
|
glog.V(0).Infof("NFS portmap responder listening on %s:%d (NFS v3 tcp=%d, MOUNT v3 tcp=%d, MOUNT v3 udp=%d)",
|
|
s.option.PortmapBind, portmapPort, s.option.Port, s.option.Port, s.option.Port)
|
|
defer func() {
|
|
if portmap != nil {
|
|
_ = portmap.Close()
|
|
}
|
|
}()
|
|
}
|
|
|
|
s.logMountHint()
|
|
return s.serve(listener)
|
|
}
|
|
|
|
// logMountHint prints a copy-pasteable Linux mount command so operators can
|
|
// see at startup how to mount the export from a client.
|
|
//
|
|
// With -portmap.bind set, MOUNT is now answered over both TCP and UDP, so a
|
|
// plain `mount -t nfs host:/export /mnt` works — there is no longer any
|
|
// kernel-default mountproto path that fails. Without -portmap.bind the
|
|
// client still has to bypass portmap entirely via the explicit
|
|
// port=/mountport=/proto=/mountproto= options.
|
|
func (s *Server) logMountHint() {
|
|
exportPath := string(s.exportRoot)
|
|
if s.option.PortmapBind != "" {
|
|
glog.V(0).Infof("mount example: mount -t nfs -o nfsvers=3,nolock <host>:%s <mountpoint>", exportPath)
|
|
glog.V(0).Infof("(MOUNT v3 is served over both TCP and UDP, so no mountproto override is needed.)")
|
|
return
|
|
}
|
|
glog.V(0).Infof("mount example (bypasses portmap): mount -t nfs -o nfsvers=3,nolock,noacl,port=%d,mountport=%d,proto=tcp,mountproto=tcp <host>:%s <mountpoint>",
|
|
s.option.Port, s.option.Port, exportPath)
|
|
glog.V(0).Infof("tip: pass -portmap.bind to enable the built-in portmap responder on port 111 so plain `mount -t nfs host:%s /mnt` works.", exportPath)
|
|
}
|
|
|
|
func (s *Server) serve(listener net.Listener) error {
|
|
if s.filerClient != nil {
|
|
defer s.filerClient.Close()
|
|
}
|
|
if s.clientAuthorizer != nil && s.clientAuthorizer.enabled {
|
|
listener = &allowlistListener{
|
|
Listener: listener,
|
|
authorizer: s.clientAuthorizer,
|
|
}
|
|
}
|
|
listener = newVersionFilterListener(listener)
|
|
|
|
handler, err := s.newHandler()
|
|
if err != nil {
|
|
_ = listener.Close()
|
|
return err
|
|
}
|
|
followCtx, followCancel := context.WithCancel(context.Background())
|
|
defer followCancel()
|
|
followDone := make(chan struct{})
|
|
go func() {
|
|
defer close(followDone)
|
|
s.runMetadataInvalidationLoop(followCtx)
|
|
}()
|
|
defer func() {
|
|
followCancel()
|
|
<-followDone
|
|
}()
|
|
|
|
glog.V(0).Infof("Start Seaweed NFS Server filer=%s bind=%s export=%s exportId=%d readOnly=%t allowedClients=%d volumeServerAccess=%s",
|
|
s.option.Filer,
|
|
listener.Addr(),
|
|
s.exportRoot,
|
|
s.exportID,
|
|
s.option.ReadOnly,
|
|
len(s.option.AllowedClients),
|
|
s.option.VolumeServerAccess,
|
|
)
|
|
|
|
return gonfs.Serve(listener, handler)
|
|
}
|
|
|
|
func (s *Server) newHandler() (*Handler, error) {
|
|
if s == nil {
|
|
return nil, errors.New("nfs server is not configured")
|
|
}
|
|
return &Handler{
|
|
server: s,
|
|
rootFS: s.rootFilesystem(),
|
|
}, nil
|
|
}
|
|
|
|
// rootFilesystem returns a single seaweedFileSystem rooted at the
|
|
// configured export, building it on first call. Both the TCP handler
|
|
// (via newHandler) and the UDP MOUNT path use the same instance so
|
|
// they share the chunk reader cache and don't reconstruct a wrapper
|
|
// per request.
|
|
func (s *Server) rootFilesystem() *seaweedFileSystem {
|
|
s.rootFSOnce.Do(func() {
|
|
s.rootFS = newSeaweedFileSystem(s, s.exportRoot, s.sharedReaderCache)
|
|
if s.sharedReaderCache == nil {
|
|
s.sharedReaderCache = s.rootFS.readerCache
|
|
}
|
|
if s.chunkInvalidator == nil {
|
|
s.chunkInvalidator = s.sharedReaderCache
|
|
}
|
|
})
|
|
return s.rootFS
|
|
}
|
|
|
|
func (s *Server) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
|
|
if s == nil || s.withFilerClient == nil {
|
|
return errors.New("nfs filer client is not configured")
|
|
}
|
|
return s.withFilerClient(streamingMode, fn)
|
|
}
|
|
|
|
func (s *Server) LookupFn() wdclient.LookupFileIdFunctionType {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
if s.option != nil && s.option.VolumeServerAccess == "filerProxy" {
|
|
return func(ctx context.Context, fileID string) ([]string, error) {
|
|
return []string{fmt.Sprintf("http://%s/?proxyChunkId=%s", s.option.Filer.ToHttpAddress(), fileID)}, nil
|
|
}
|
|
}
|
|
if s.filerClient != nil {
|
|
return s.filerClient.GetLookupFileIdFunction()
|
|
}
|
|
return nil
|
|
}
|