Commit b1a252a6 authored by Max Kellermann's avatar Max Kellermann

lib/nfs/Base: kludge to reduce number of NFS mounts

Creating a NfsStorage sets its own export_name as the "base". Now NfsFileReader can use this information to derive the export_name to be mounted, instead of guessing. This solves the "too many connection" problem on the NFS server while updating the database.
parent fb90b64b
...@@ -559,6 +559,7 @@ NFS_SOURCES = \ ...@@ -559,6 +559,7 @@ NFS_SOURCES = \
src/lib/nfs/Connection.cxx src/lib/nfs/Connection.hxx \ src/lib/nfs/Connection.cxx src/lib/nfs/Connection.hxx \
src/lib/nfs/Manager.cxx src/lib/nfs/Manager.hxx \ src/lib/nfs/Manager.cxx src/lib/nfs/Manager.hxx \
src/lib/nfs/Glue.cxx src/lib/nfs/Glue.hxx \ src/lib/nfs/Glue.cxx src/lib/nfs/Glue.hxx \
src/lib/nfs/Base.cxx src/lib/nfs/Base.hxx \
src/lib/nfs/FileReader.cxx src/lib/nfs/FileReader.hxx \ src/lib/nfs/FileReader.cxx src/lib/nfs/FileReader.hxx \
src/lib/nfs/Domain.cxx src/lib/nfs/Domain.hxx src/lib/nfs/Domain.cxx src/lib/nfs/Domain.hxx
......
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "Base.hxx"
#include <assert.h>
#include <string.h>
static char nfs_base_server[64];
static char nfs_base_export_name[256];
static size_t nfs_base_export_name_length;
void
nfs_set_base(const char *server, const char *export_name)
{
assert(server != nullptr);
assert(export_name != nullptr);
const size_t server_length = strlen(server);
const size_t export_name_length = strlen(export_name);
if (server_length >= sizeof(nfs_base_server) ||
export_name_length > sizeof(nfs_base_export_name))
return;
memcpy(nfs_base_server, server, server_length + 1);
memcpy(nfs_base_export_name, export_name, export_name_length);
nfs_base_export_name_length = export_name_length;
}
const char *
nfs_check_base(const char *server, const char *path)
{
assert(server != nullptr);
assert(path != nullptr);
return strcmp(nfs_base_server, server) == 0 &&
memcmp(nfs_base_export_name, path,
nfs_base_export_name_length) == 0 &&
(path[nfs_base_export_name_length] == 0 ||
path[nfs_base_export_name_length] == '/')
? path + nfs_base_export_name_length
: nullptr;
}
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_NFS_BASE_HXX
#define MPD_NFS_BASE_HXX
#include "check.h"
#include "Compiler.h"
/**
* Set the "base" NFS server and export name. This will be the
* default export that will be mounted if a file within this export is
* being opened, instead of guessing the mount point.
*
* This is a kludge that is not truly thread-safe.
*/
void
nfs_set_base(const char *server, const char *export_name);
/**
* Check if the given server and path are inside the "base"
* server/export_name. If yes, then a pointer to the portion of
* "path" after the export_name is returned; otherwise, nullptr is
* returned.
*/
gcc_pure
const char *
nfs_check_base(const char *server, const char *path);
#endif
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "FileReader.hxx" #include "FileReader.hxx"
#include "Glue.hxx" #include "Glue.hxx"
#include "Base.hxx"
#include "Connection.hxx" #include "Connection.hxx"
#include "Domain.hxx" #include "Domain.hxx"
#include "event/Call.hxx" #include "event/Call.hxx"
...@@ -100,14 +101,23 @@ NfsFileReader::Open(const char *uri, Error &error) ...@@ -100,14 +101,23 @@ NfsFileReader::Open(const char *uri, Error &error)
server = std::string(uri, slash); server = std::string(uri, slash);
uri = slash; uri = slash;
slash = strrchr(uri + 1, '/');
if (slash == nullptr || slash[1] == 0) {
error.Set(nfs_domain, "Malformed nfs:// URI");
return false;
}
export_name = std::string(uri, slash); const char *new_path = nfs_check_base(server.c_str(), uri);
path = slash; if (new_path != nullptr) {
export_name = std::string(uri, new_path);
if (*new_path == 0)
new_path = "/";
path = new_path;
} else {
slash = strrchr(uri + 1, '/');
if (slash == nullptr || slash[1] == 0) {
error.Set(nfs_domain, "Malformed nfs:// URI");
return false;
}
export_name = std::string(uri, slash);
path = slash;
}
state = State::DEFER; state = State::DEFER;
DeferredMonitor::Schedule(); DeferredMonitor::Schedule();
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "storage/StorageInterface.hxx" #include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx" #include "storage/FileInfo.hxx"
#include "lib/nfs/Domain.hxx" #include "lib/nfs/Domain.hxx"
#include "lib/nfs/Base.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
...@@ -256,6 +257,8 @@ CreateNfsStorageURI(const char *base, Error &error) ...@@ -256,6 +257,8 @@ CreateNfsStorageURI(const char *base, Error &error)
return nullptr; return nullptr;
} }
nfs_set_base(server.c_str(), mount);
return new NfsStorage(base, ctx); return new NfsStorage(base, ctx);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment