Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated: Fix file truncation on resume with fallocate enabled #261

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 45 additions & 67 deletions src/data/socket_file.cc
Original file line number Diff line number Diff line change
@@ -1,43 +1,8 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include "socket_file.h"
#include "torrent/exceptions.h"
#include "torrent/utils/log.h"

#include <fcntl.h>
#include <unistd.h>
Expand All @@ -48,10 +13,15 @@
#include <sys/types.h>

#ifdef HAVE_FALLOCATE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <linux/falloc.h>
#endif

#define LT_LOG_ERROR(log_fmt, ...) \
lt_log_print(LOG_STORAGE, "socket_file->%i: " log_fmt, m_fd, __VA_ARGS__);

namespace torrent {

bool
Expand All @@ -73,7 +43,7 @@ SocketFile::open(const std::string& path, int prot, int flags, mode_t mode) {
#else
fd_type fd = ::open(path.c_str(), flags, mode);
#endif

if (fd == invalid_fd)
return false;

Expand All @@ -99,26 +69,42 @@ SocketFile::size() const {
rak::file_stat fs;

return fs.update(m_fd) ? fs.size() : 0;
}
}

bool
SocketFile::set_size(uint64_t size, int flags) const {
SocketFile::set_size(uint64_t size) const {
if (!is_open())
throw internal_error("SocketFile::set_size() called on a closed file");

#ifdef HAVE_FALLOCATE
if (flags & flag_fallocate && fallocate(m_fd, 0, 0, size) == 0)
return true;
#endif
if (ftruncate(m_fd, size) == -1) {
return false;
}

#ifdef USE_POSIX_FALLOCATE
if (flags & flag_fallocate &&
flags & flag_fallocate_blocking &&
posix_fallocate(m_fd, 0, size) == 0)
return true;
#endif
return true;
}

#ifdef SYS_DARWIN
bool
SocketFile::allocate(uint64_t size, int flags) const {
if (!is_open())
throw internal_error("SocketFile::allocate() called on a closed file");

#if defined(HAVE_FALLOCATE)
if (flags & flag_fallocate) {
if (fallocate(m_fd, 0, 0, size) == -1) {
LT_LOG_ERROR("fallocate failed : %s", strerror(errno));
return false;
}
}

#elif defined(USE_POSIX_FALLOCATE)
if (flags & flag_fallocate && flags & flag_fallocate_blocking) {
if (posix_fallocate(m_fd, 0, size) == -1) {
LT_LOG_INFO("posix_fallocate failed : %s", strerror(errno));
return false;
}
}

#elif defined(SYS_DARWIN)
if (flags & flag_fallocate) {
fstore_t fstore;
fstore.fst_flags = F_ALLOCATECONTIG;
Expand All @@ -127,29 +113,21 @@ SocketFile::set_size(uint64_t size, int flags) const {
fstore.fst_length = size;
fstore.fst_bytesalloc = 0;

// Hmm... this shouldn't really be something we fail the set_size
// on...
// This shouldn't really be something we fail the set_size
// on.
//
// Yet is somehow fails with ENOSPC...
// Yet is somehow fails with ENOSPC.
// if (fcntl(m_fd, F_PREALLOCATE, &fstore) == -1)
// throw internal_error("hack: fcntl failed" + std::string(strerror(errno)));

fcntl(m_fd, F_PREALLOCATE, &fstore); // Ignore result for now...
}
#endif
if (fcntl(m_fd, F_PREALLOCATE, &fstore) == -1)
LT_LOG_INFO("fcntl(,F_PREALLOCATE,) failed : %s", strerror(errno));

if (ftruncate(m_fd, size) == 0)
return true;

// Use workaround to resize files on vfat. It causes the whole
// client to block while it is resizing the files, this really
// should be in a seperate thread.
if (size != 0 &&
lseek(m_fd, size - 1, SEEK_SET) == (off_t)(size - 1) &&
write(m_fd, &size, 1) == 1)
return true;
}
#endif

return false;
return true;
}

MemoryChunk
Expand All @@ -165,7 +143,7 @@ SocketFile::create_chunk(uint64_t offset, uint32_t length, int prot, int flags)
uint64_t align = offset % MemoryChunk::page_size();

char* ptr = (char*)mmap(NULL, length + align, prot, flags, m_fd, offset - align);

if (ptr == MAP_FAILED)
return MemoryChunk();

Expand Down
44 changes: 5 additions & 39 deletions src/data/socket_file.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#ifndef LIBTORRENT_SOCKET_FILE_H
#define LIBTORRENT_SOCKET_FILE_H

Expand Down Expand Up @@ -68,12 +32,14 @@ class SocketFile {

bool open(const std::string& path, int prot, int flags, mode_t mode = 0666);
void close();

uint64_t size() const;
bool set_size(uint64_t s, int flags = 0) const;
bool set_size(uint64_t size) const;

bool allocate(uint64_t size, int flags = 0) const;

MemoryChunk create_chunk(uint64_t offset, uint32_t length, int prot, int flags) const;

fd_type fd() const { return m_fd; }

private:
Expand Down
52 changes: 8 additions & 44 deletions src/torrent/data/file.cc
Original file line number Diff line number Diff line change
@@ -1,41 +1,6 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include <cassert>
#include <rak/error_number.h>
#include <rak/file_stat.h>

Expand Down Expand Up @@ -76,8 +41,7 @@ File::File() :
}

File::~File() {
if (is_open())
throw internal_error("File::~File() called on an open file.");
assert(!is_open() && "File::~File() called on an open file.");
}

bool
Expand Down Expand Up @@ -177,16 +141,16 @@ File::resize_file() {
if (m_size == SocketFile(m_fd).size())
return true;

int flags = 0;
if (!SocketFile(m_fd).set_size(m_size))
return false;

// Set FS supported non-blocking allocation flag and potentially
// blocking allocation flag if fallocate flag is set.
if (m_flags & flag_fallocate) {
flags |= SocketFile::flag_fallocate;
flags |= SocketFile::flag_fallocate_blocking;
// Only do non-blocking fallocate.
if (!SocketFile(m_fd).allocate(m_size, SocketFile::flag_fallocate))
return false;
}

return SocketFile(m_fd).set_size(m_size, flags);
return true;
}

}
2 changes: 1 addition & 1 deletion src/torrent/data/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class LIBTORRENT_EXPORT lt_cacheline_aligned File {
int m_fd;
int m_protection;
int m_flags;

Path m_path;
std::string m_frozenPath;

Expand Down
Loading