2013-01-30 01:38:37 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2023-09-26 17:51:38 +03:00
|
|
|
#include "BaseVFS.h"
|
|
|
|
|
2013-01-30 01:38:37 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include "sqlite3.h"
|
2018-06-12 23:22:50 +03:00
|
|
|
#include "mozilla/net/IOActivityMonitor.h"
|
2023-05-10 14:51:49 +03:00
|
|
|
|
|
|
|
namespace {
|
2020-06-24 19:08:45 +03:00
|
|
|
|
2013-10-23 16:58:06 +04:00
|
|
|
// The last VFS version for which this file has been updated.
|
2023-05-10 14:51:49 +03:00
|
|
|
constexpr int kLastKnowVfsVersion = 3;
|
2013-10-23 16:58:06 +04:00
|
|
|
|
|
|
|
// The last io_methods version for which this file has been updated.
|
2023-05-10 14:51:49 +03:00
|
|
|
constexpr int kLastKnownIOMethodsVersion = 3;
|
2013-01-30 01:38:37 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2018-06-12 23:22:50 +03:00
|
|
|
using namespace mozilla::net;
|
2013-01-30 01:38:37 +04:00
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
struct BaseFile {
|
2013-01-30 01:38:37 +04:00
|
|
|
// Base class. Must be first
|
|
|
|
sqlite3_file base;
|
2018-06-12 23:22:50 +03:00
|
|
|
// The filename
|
|
|
|
char* location;
|
2023-05-10 14:51:49 +03:00
|
|
|
// This points to the underlying sqlite3_file
|
2013-01-30 01:38:37 +04:00
|
|
|
sqlite3_file pReal[1];
|
|
|
|
};
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseClose(sqlite3_file* pFile) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
delete[] p->location;
|
|
|
|
return p->pReal->pMethods->xClose(p->pReal);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseRead(sqlite3_file* pFile, void* zBuf, int iAmt, sqlite_int64 iOfst) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
int rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
|
2018-06-12 23:22:50 +03:00
|
|
|
if (rc == SQLITE_OK && IOActivityMonitor::IsActive()) {
|
|
|
|
IOActivityMonitor::Read(nsDependentCString(p->location), iAmt);
|
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseWrite(sqlite3_file* pFile, const void* zBuf, int iAmt,
|
|
|
|
sqlite_int64 iOfst) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
int rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
|
2018-06-12 23:22:50 +03:00
|
|
|
if (rc == SQLITE_OK && IOActivityMonitor::IsActive()) {
|
|
|
|
IOActivityMonitor::Write(nsDependentCString(p->location), iAmt);
|
|
|
|
}
|
2015-01-24 19:16:26 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseTruncate(sqlite3_file* pFile, sqlite_int64 size) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xTruncate(p->pReal, size);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseSync(sqlite3_file* pFile, int flags) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
return p->pReal->pMethods->xSync(p->pReal, flags);
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseFileSize(sqlite3_file* pFile, sqlite_int64* pSize) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xFileSize(p->pReal, pSize);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseLock(sqlite3_file* pFile, int eLock) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xLock(p->pReal, eLock);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseUnlock(sqlite3_file* pFile, int eLock) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xUnlock(p->pReal, eLock);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseCheckReservedLock(sqlite3_file* pFile, int* pResOut) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseFileControl(sqlite3_file* pFile, int op, void* pArg) {
|
2023-08-01 11:02:34 +03:00
|
|
|
#if defined(MOZ_SQLITE_PERSIST_AUXILIARY_FILES)
|
2023-05-12 12:45:02 +03:00
|
|
|
// Persist auxiliary files (-shm and -wal) on disk, because creating and
|
|
|
|
// deleting them may be expensive on slow storage.
|
|
|
|
// Only do this when there is a journal size limit, so the journal is
|
|
|
|
// truncated instead of deleted on shutdown, that feels safer if the user
|
|
|
|
// moves a database file around without its auxiliary files.
|
|
|
|
MOZ_ASSERT(
|
|
|
|
::sqlite3_compileoption_used("DEFAULT_JOURNAL_SIZE_LIMIT"),
|
|
|
|
"A journal size limit ensures the journal is truncated on shutdown");
|
|
|
|
if (op == SQLITE_FCNTL_PERSIST_WAL) {
|
|
|
|
*static_cast<int*>(pArg) = 1;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2023-08-01 11:02:34 +03:00
|
|
|
#endif
|
2023-05-10 14:51:49 +03:00
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseSectorSize(sqlite3_file* pFile) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xSectorSize(p->pReal);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseDeviceCharacteristics(sqlite3_file* pFile) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseShmMap(sqlite3_file* pFile, int iPg, int pgsz, int bExtend,
|
|
|
|
void volatile** pp) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xShmMap(p->pReal, iPg, pgsz, bExtend, pp);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseShmLock(sqlite3_file* pFile, int offset, int n, int flags) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xShmLock(p->pReal, offset, n, flags);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
void BaseShmBarrier(sqlite3_file* pFile) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xShmBarrier(p->pReal);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
2013-06-01 20:26:17 +04:00
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseShmUnmap(sqlite3_file* pFile, int deleteFlag) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xShmUnmap(p->pReal, deleteFlag);
|
2013-06-01 20:26:17 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseFetch(sqlite3_file* pFile, sqlite3_int64 iOfst, int iAmt, void** pp) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xFetch(p->pReal, iOfst, iAmt, pp);
|
2013-06-01 20:26:17 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseUnfetch(sqlite3_file* pFile, sqlite3_int64 iOfst, void* pPage) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
|
|
|
return p->pReal->pMethods->xUnfetch(p->pReal, iOfst, pPage);
|
|
|
|
}
|
2018-06-12 23:22:50 +03:00
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
int BaseOpen(sqlite3_vfs* vfs, const char* zName, sqlite3_file* pFile,
|
|
|
|
int flags, int* pOutFlags) {
|
|
|
|
BaseFile* p = (BaseFile*)pFile;
|
2018-06-12 23:22:50 +03:00
|
|
|
if (zName) {
|
|
|
|
p->location = new char[7 + strlen(zName) + 1];
|
|
|
|
strcpy(p->location, "file://");
|
|
|
|
strcpy(p->location + 7, zName);
|
|
|
|
} else {
|
|
|
|
p->location = new char[8];
|
|
|
|
strcpy(p->location, "file://");
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
sqlite3_vfs* origVfs = (sqlite3_vfs*)(vfs->pAppData);
|
|
|
|
int rc = origVfs->xOpen(origVfs, zName, p->pReal, flags, pOutFlags);
|
|
|
|
if (rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
if (p->pReal->pMethods) {
|
2013-10-23 16:58:06 +04:00
|
|
|
// If the io_methods version is higher than the last known one, you should
|
|
|
|
// update this VFS adding appropriate IO methods for any methods added in
|
|
|
|
// the version change.
|
2023-05-10 14:51:49 +03:00
|
|
|
MOZ_ASSERT(p->pReal->pMethods->iVersion == kLastKnownIOMethodsVersion);
|
|
|
|
static const sqlite3_io_methods IOmethods = {
|
|
|
|
kLastKnownIOMethodsVersion, /* iVersion */
|
|
|
|
BaseClose, /* xClose */
|
|
|
|
BaseRead, /* xRead */
|
|
|
|
BaseWrite, /* xWrite */
|
|
|
|
BaseTruncate, /* xTruncate */
|
|
|
|
BaseSync, /* xSync */
|
|
|
|
BaseFileSize, /* xFileSize */
|
|
|
|
BaseLock, /* xLock */
|
|
|
|
BaseUnlock, /* xUnlock */
|
|
|
|
BaseCheckReservedLock, /* xCheckReservedLock */
|
|
|
|
BaseFileControl, /* xFileControl */
|
|
|
|
BaseSectorSize, /* xSectorSize */
|
|
|
|
BaseDeviceCharacteristics, /* xDeviceCharacteristics */
|
|
|
|
BaseShmMap, /* xShmMap */
|
|
|
|
BaseShmLock, /* xShmLock */
|
|
|
|
BaseShmBarrier, /* xShmBarrier */
|
|
|
|
BaseShmUnmap, /* xShmUnmap */
|
|
|
|
BaseFetch, /* xFetch */
|
|
|
|
BaseUnfetch /* xUnfetch */
|
|
|
|
};
|
|
|
|
pFile->pMethods = &IOmethods;
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:49 +03:00
|
|
|
return SQLITE_OK;
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-01-30 01:38:37 +04:00
|
|
|
|
2023-09-26 17:51:38 +03:00
|
|
|
namespace mozilla::storage::basevfs {
|
2013-01-30 01:38:37 +04:00
|
|
|
|
2023-09-26 17:51:38 +03:00
|
|
|
const char* GetVFSName(bool exclusive) {
|
2023-05-10 14:51:48 +03:00
|
|
|
return exclusive ? "base-vfs-excl" : "base-vfs";
|
Bug 1650201 - Fix mozStorage prefs read before profile and fallback to a non-exclusive VFS when it can't get an exclusive lock. r=asuth,geckoview-reviewers,agi
mozStorage used to read prefs on service init, because they could only be read
on the main-thread. When service init was moved earlier, it started trying
to read prefs too early, before the profile was set up, thus it ended up always
reading the default value.
This patch moves the only relevant pref to mirrored StaticPrefs that can be accessed
from different threads, and removes two preferences that apparently are not necessary
(they have been broken from a long time) for now.
In particular, providing a global synchronous setting is a footgun, each consumer should
decide about their synchronous needs, rather than abusing a dangerous "go fast" setting.
The page size is something we don't change from quite some time, and it's unlikely to be
used to run experiments in the wild before doing local measurements first, for which Try
builds are enough.
The remaining exclusiveLock pref is a bit controversial, because in general exclusive lock
is better for various reasons, and mostly it is necessary to use WAL on network shares.
Though developers may find it useful for debugging, and some third parties are doing
dangerous things (like copying over databases) to work around it, for which it's safer to
provide a less dangerous alternative.
Note exclusive lock only works on Unix-derived systems for now (no Windows implementation).
Finally, this introduces a fallback to exclusive lock, so that if a third party is using our
databases, so that we can't get an exclusive lock, we'll fallback to normal locking.
Differential Revision: https://phabricator.services.mozilla.com/D82717
2020-07-11 00:45:53 +03:00
|
|
|
}
|
2017-09-29 14:25:06 +03:00
|
|
|
|
2023-09-26 17:51:38 +03:00
|
|
|
UniquePtr<sqlite3_vfs> ConstructVFS(bool exclusive) {
|
2013-01-30 01:38:37 +04:00
|
|
|
#if defined(XP_WIN)
|
2018-07-13 19:54:57 +03:00
|
|
|
# define EXPECTED_VFS "win32"
|
|
|
|
# define EXPECTED_VFS_EXCL "win32"
|
2013-01-30 01:38:37 +04:00
|
|
|
#else
|
2018-07-13 19:54:57 +03:00
|
|
|
# define EXPECTED_VFS "unix"
|
|
|
|
# define EXPECTED_VFS_EXCL "unix-excl"
|
2013-01-30 01:38:37 +04:00
|
|
|
#endif
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2023-09-26 17:51:38 +03:00
|
|
|
if (sqlite3_vfs_find(GetVFSName(exclusive))) {
|
2023-05-10 14:51:49 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found;
|
|
|
|
sqlite3_vfs* origVfs;
|
Bug 1650201 - Fix mozStorage prefs read before profile and fallback to a non-exclusive VFS when it can't get an exclusive lock. r=asuth,geckoview-reviewers,agi
mozStorage used to read prefs on service init, because they could only be read
on the main-thread. When service init was moved earlier, it started trying
to read prefs too early, before the profile was set up, thus it ended up always
reading the default value.
This patch moves the only relevant pref to mirrored StaticPrefs that can be accessed
from different threads, and removes two preferences that apparently are not necessary
(they have been broken from a long time) for now.
In particular, providing a global synchronous setting is a footgun, each consumer should
decide about their synchronous needs, rather than abusing a dangerous "go fast" setting.
The page size is something we don't change from quite some time, and it's unlikely to be
used to run experiments in the wild before doing local measurements first, for which Try
builds are enough.
The remaining exclusiveLock pref is a bit controversial, because in general exclusive lock
is better for various reasons, and mostly it is necessary to use WAL on network shares.
Though developers may find it useful for debugging, and some third parties are doing
dangerous things (like copying over databases) to work around it, for which it's safer to
provide a less dangerous alternative.
Note exclusive lock only works on Unix-derived systems for now (no Windows implementation).
Finally, this introduces a fallback to exclusive lock, so that if a third party is using our
databases, so that we can't get an exclusive lock, we'll fallback to normal locking.
Differential Revision: https://phabricator.services.mozilla.com/D82717
2020-07-11 00:45:53 +03:00
|
|
|
if (!exclusive) {
|
2018-07-13 19:54:57 +03:00
|
|
|
// Use the non-exclusive VFS.
|
2023-05-10 14:51:49 +03:00
|
|
|
origVfs = sqlite3_vfs_find(nullptr);
|
|
|
|
found = origVfs && origVfs->zName && !strcmp(origVfs->zName, EXPECTED_VFS);
|
2018-07-13 19:54:57 +03:00
|
|
|
} else {
|
2023-05-10 14:51:49 +03:00
|
|
|
origVfs = sqlite3_vfs_find(EXPECTED_VFS_EXCL);
|
|
|
|
found = (origVfs != nullptr);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
2023-05-10 14:51:49 +03:00
|
|
|
if (!found) {
|
2013-07-31 19:44:58 +04:00
|
|
|
return nullptr;
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 16:58:06 +04:00
|
|
|
// If the VFS version is higher than the last known one, you should update
|
|
|
|
// this VFS adding appropriate methods for any methods added in the version
|
|
|
|
// change.
|
2023-05-10 14:51:49 +03:00
|
|
|
MOZ_ASSERT(origVfs->iVersion == kLastKnowVfsVersion);
|
|
|
|
|
|
|
|
sqlite3_vfs vfs = {
|
|
|
|
kLastKnowVfsVersion, /* iVersion */
|
|
|
|
origVfs->szOsFile + static_cast<int>(sizeof(BaseFile)), /* szOsFile */
|
|
|
|
origVfs->mxPathname, /* mxPathname */
|
|
|
|
nullptr, /* pNext */
|
2023-09-26 17:51:38 +03:00
|
|
|
GetVFSName(exclusive), /* zName */
|
2023-05-10 14:51:49 +03:00
|
|
|
origVfs, /* pAppData */
|
|
|
|
BaseOpen, /* xOpen */
|
|
|
|
origVfs->xDelete, /* xDelete */
|
|
|
|
origVfs->xAccess, /* xAccess */
|
|
|
|
origVfs->xFullPathname, /* xFullPathname */
|
|
|
|
origVfs->xDlOpen, /* xDlOpen */
|
|
|
|
origVfs->xDlError, /* xDlError */
|
|
|
|
origVfs->xDlSym, /* xDlSym */
|
|
|
|
origVfs->xDlClose, /* xDlClose */
|
|
|
|
origVfs->xRandomness, /* xRandomness */
|
|
|
|
origVfs->xSleep, /* xSleep */
|
|
|
|
origVfs->xCurrentTime, /* xCurrentTime */
|
|
|
|
origVfs->xGetLastError, /* xGetLastError */
|
|
|
|
origVfs->xCurrentTimeInt64, /* xCurrentTimeInt64 */
|
|
|
|
origVfs->xSetSystemCall, /* xSetSystemCall */
|
|
|
|
origVfs->xGetSystemCall, /* xGetSystemCall */
|
|
|
|
origVfs->xNextSystemCall /* xNextSystemCall */
|
|
|
|
};
|
|
|
|
|
|
|
|
return MakeUnique<sqlite3_vfs>(vfs);
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:51:38 +03:00
|
|
|
} // namespace mozilla::storage::basevfs
|