fs_posix.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors
  9. #include <dirent.h>
  10. #ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
  11. #include <dlfcn.h>
  12. #endif
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #if defined(OS_LINUX)
  16. #include <linux/fs.h>
  17. #endif
  18. #include <pthread.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/mman.h>
  25. #include <sys/stat.h>
  26. #if defined(OS_LINUX) || defined(OS_SOLARIS) || defined(OS_ANDROID)
  27. #include <sys/statfs.h>
  28. #include <sys/syscall.h>
  29. #include <sys/sysmacros.h>
  30. #endif
  31. #include <sys/statvfs.h>
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #include <time.h>
  35. #include <algorithm>
  36. // Get nano time includes
  37. #if defined(OS_LINUX) || defined(OS_FREEBSD)
  38. #elif defined(__MACH__)
  39. #include <Availability.h>
  40. #include <mach/clock.h>
  41. #include <mach/mach.h>
  42. #else
  43. #include <chrono>
  44. #endif
  45. #include <deque>
  46. #include <set>
  47. #include <vector>
  48. #include "env/io_posix.h"
  49. #include "logging/logging.h"
  50. #include "logging/posix_logger.h"
  51. #include "monitoring/iostats_context_imp.h"
  52. #include "monitoring/thread_status_updater.h"
  53. #include "port/port.h"
  54. #include "rocksdb/options.h"
  55. #include "rocksdb/slice.h"
  56. #include "test_util/sync_point.h"
  57. #include "util/coding.h"
  58. #include "util/compression_context_cache.h"
  59. #include "util/random.h"
  60. #include "util/string_util.h"
  61. #include "util/thread_local.h"
  62. #include "util/threadpool_imp.h"
  63. #if !defined(TMPFS_MAGIC)
  64. #define TMPFS_MAGIC 0x01021994
  65. #endif
  66. #if !defined(XFS_SUPER_MAGIC)
  67. #define XFS_SUPER_MAGIC 0x58465342
  68. #endif
  69. #if !defined(EXT4_SUPER_MAGIC)
  70. #define EXT4_SUPER_MAGIC 0xEF53
  71. #endif
  72. namespace ROCKSDB_NAMESPACE {
  73. namespace {
  74. inline mode_t GetDBFileMode(bool allow_non_owner_access) {
  75. return allow_non_owner_access ? 0644 : 0600;
  76. }
  77. // list of pathnames that are locked
  78. static std::set<std::string> lockedFiles;
  79. static port::Mutex mutex_lockedFiles;
  80. static int LockOrUnlock(int fd, bool lock) {
  81. errno = 0;
  82. struct flock f;
  83. memset(&f, 0, sizeof(f));
  84. f.l_type = (lock ? F_WRLCK : F_UNLCK);
  85. f.l_whence = SEEK_SET;
  86. f.l_start = 0;
  87. f.l_len = 0; // Lock/unlock entire file
  88. int value = fcntl(fd, F_SETLK, &f);
  89. return value;
  90. }
  91. class PosixFileLock : public FileLock {
  92. public:
  93. int fd_;
  94. std::string filename;
  95. };
  96. int cloexec_flags(int flags, const EnvOptions* options) {
  97. // If the system supports opening the file with cloexec enabled,
  98. // do so, as this avoids a race condition if a db is opened around
  99. // the same time that a child process is forked
  100. #ifdef O_CLOEXEC
  101. if (options == nullptr || options->set_fd_cloexec) {
  102. flags |= O_CLOEXEC;
  103. }
  104. #endif
  105. return flags;
  106. }
  107. class PosixFileSystem : public FileSystem {
  108. public:
  109. PosixFileSystem();
  110. const char* Name() const override { return "Posix File System"; }
  111. ~PosixFileSystem() override {}
  112. void SetFD_CLOEXEC(int fd, const EnvOptions* options) {
  113. if ((options == nullptr || options->set_fd_cloexec) && fd > 0) {
  114. fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
  115. }
  116. }
  117. IOStatus NewSequentialFile(const std::string& fname,
  118. const FileOptions& options,
  119. std::unique_ptr<FSSequentialFile>* result,
  120. IODebugContext* /*dbg*/) override {
  121. result->reset();
  122. int fd = -1;
  123. int flags = cloexec_flags(O_RDONLY, &options);
  124. FILE* file = nullptr;
  125. if (options.use_direct_reads && !options.use_mmap_reads) {
  126. #ifdef ROCKSDB_LITE
  127. return IOStatus::IOError(fname,
  128. "Direct I/O not supported in RocksDB lite");
  129. #endif // !ROCKSDB_LITE
  130. #if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
  131. flags |= O_DIRECT;
  132. #endif
  133. }
  134. do {
  135. IOSTATS_TIMER_GUARD(open_nanos);
  136. fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
  137. } while (fd < 0 && errno == EINTR);
  138. if (fd < 0) {
  139. return IOError("While opening a file for sequentially reading", fname,
  140. errno);
  141. }
  142. SetFD_CLOEXEC(fd, &options);
  143. if (options.use_direct_reads && !options.use_mmap_reads) {
  144. #ifdef OS_MACOSX
  145. if (fcntl(fd, F_NOCACHE, 1) == -1) {
  146. close(fd);
  147. return IOError("While fcntl NoCache", fname, errno);
  148. }
  149. #endif
  150. } else {
  151. do {
  152. IOSTATS_TIMER_GUARD(open_nanos);
  153. file = fdopen(fd, "r");
  154. } while (file == nullptr && errno == EINTR);
  155. if (file == nullptr) {
  156. close(fd);
  157. return IOError("While opening file for sequentially read", fname,
  158. errno);
  159. }
  160. }
  161. result->reset(new PosixSequentialFile(fname, file, fd, options));
  162. return IOStatus::OK();
  163. }
  164. IOStatus NewRandomAccessFile(const std::string& fname,
  165. const FileOptions& options,
  166. std::unique_ptr<FSRandomAccessFile>* result,
  167. IODebugContext* /*dbg*/) override {
  168. result->reset();
  169. IOStatus s;
  170. int fd;
  171. int flags = cloexec_flags(O_RDONLY, &options);
  172. if (options.use_direct_reads && !options.use_mmap_reads) {
  173. #ifdef ROCKSDB_LITE
  174. return IOStatus::IOError(fname,
  175. "Direct I/O not supported in RocksDB lite");
  176. #endif // !ROCKSDB_LITE
  177. #if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
  178. flags |= O_DIRECT;
  179. TEST_SYNC_POINT_CALLBACK("NewRandomAccessFile:O_DIRECT", &flags);
  180. #endif
  181. }
  182. do {
  183. IOSTATS_TIMER_GUARD(open_nanos);
  184. fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
  185. } while (fd < 0 && errno == EINTR);
  186. if (fd < 0) {
  187. return IOError("While open a file for random read", fname, errno);
  188. }
  189. SetFD_CLOEXEC(fd, &options);
  190. if (options.use_mmap_reads && sizeof(void*) >= 8) {
  191. // Use of mmap for random reads has been removed because it
  192. // kills performance when storage is fast.
  193. // Use mmap when virtual address-space is plentiful.
  194. uint64_t size;
  195. IOOptions opts;
  196. s = GetFileSize(fname, opts, &size, nullptr);
  197. if (s.ok()) {
  198. void* base = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
  199. if (base != MAP_FAILED) {
  200. result->reset(
  201. new PosixMmapReadableFile(fd, fname, base, size, options));
  202. } else {
  203. s = IOError("while mmap file for read", fname, errno);
  204. close(fd);
  205. }
  206. }
  207. } else {
  208. if (options.use_direct_reads && !options.use_mmap_reads) {
  209. #ifdef OS_MACOSX
  210. if (fcntl(fd, F_NOCACHE, 1) == -1) {
  211. close(fd);
  212. return IOError("while fcntl NoCache", fname, errno);
  213. }
  214. #endif
  215. }
  216. result->reset(new PosixRandomAccessFile(fname, fd, options
  217. #if defined(ROCKSDB_IOURING_PRESENT)
  218. ,
  219. thread_local_io_urings_.get()
  220. #endif
  221. ));
  222. }
  223. return s;
  224. }
  225. virtual IOStatus OpenWritableFile(const std::string& fname,
  226. const FileOptions& options,
  227. bool reopen,
  228. std::unique_ptr<FSWritableFile>* result,
  229. IODebugContext* /*dbg*/) {
  230. result->reset();
  231. IOStatus s;
  232. int fd = -1;
  233. int flags = (reopen) ? (O_CREAT | O_APPEND) : (O_CREAT | O_TRUNC);
  234. // Direct IO mode with O_DIRECT flag or F_NOCAHCE (MAC OSX)
  235. if (options.use_direct_writes && !options.use_mmap_writes) {
  236. // Note: we should avoid O_APPEND here due to ta the following bug:
  237. // POSIX requires that opening a file with the O_APPEND flag should
  238. // have no affect on the location at which pwrite() writes data.
  239. // However, on Linux, if a file is opened with O_APPEND, pwrite()
  240. // appends data to the end of the file, regardless of the value of
  241. // offset.
  242. // More info here: https://linux.die.net/man/2/pwrite
  243. #ifdef ROCKSDB_LITE
  244. return IOStatus::IOError(fname,
  245. "Direct I/O not supported in RocksDB lite");
  246. #endif // ROCKSDB_LITE
  247. flags |= O_WRONLY;
  248. #if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
  249. flags |= O_DIRECT;
  250. #endif
  251. TEST_SYNC_POINT_CALLBACK("NewWritableFile:O_DIRECT", &flags);
  252. } else if (options.use_mmap_writes) {
  253. // non-direct I/O
  254. flags |= O_RDWR;
  255. } else {
  256. flags |= O_WRONLY;
  257. }
  258. flags = cloexec_flags(flags, &options);
  259. do {
  260. IOSTATS_TIMER_GUARD(open_nanos);
  261. fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
  262. } while (fd < 0 && errno == EINTR);
  263. if (fd < 0) {
  264. s = IOError("While open a file for appending", fname, errno);
  265. return s;
  266. }
  267. SetFD_CLOEXEC(fd, &options);
  268. if (options.use_mmap_writes) {
  269. if (!checkedDiskForMmap_) {
  270. // this will be executed once in the program's lifetime.
  271. // do not use mmapWrite on non ext-3/xfs/tmpfs systems.
  272. if (!SupportsFastAllocate(fname)) {
  273. forceMmapOff_ = true;
  274. }
  275. checkedDiskForMmap_ = true;
  276. }
  277. }
  278. if (options.use_mmap_writes && !forceMmapOff_) {
  279. result->reset(new PosixMmapFile(fname, fd, page_size_, options));
  280. } else if (options.use_direct_writes && !options.use_mmap_writes) {
  281. #ifdef OS_MACOSX
  282. if (fcntl(fd, F_NOCACHE, 1) == -1) {
  283. close(fd);
  284. s = IOError("While fcntl NoCache an opened file for appending", fname,
  285. errno);
  286. return s;
  287. }
  288. #elif defined(OS_SOLARIS)
  289. if (directio(fd, DIRECTIO_ON) == -1) {
  290. if (errno != ENOTTY) { // ZFS filesystems don't support DIRECTIO_ON
  291. close(fd);
  292. s = IOError("While calling directio()", fname, errno);
  293. return s;
  294. }
  295. }
  296. #endif
  297. result->reset(new PosixWritableFile(fname, fd, options));
  298. } else {
  299. // disable mmap writes
  300. EnvOptions no_mmap_writes_options = options;
  301. no_mmap_writes_options.use_mmap_writes = false;
  302. result->reset(new PosixWritableFile(fname, fd, no_mmap_writes_options));
  303. }
  304. return s;
  305. }
  306. IOStatus NewWritableFile(const std::string& fname, const FileOptions& options,
  307. std::unique_ptr<FSWritableFile>* result,
  308. IODebugContext* dbg) override {
  309. return OpenWritableFile(fname, options, false, result, dbg);
  310. }
  311. IOStatus ReopenWritableFile(const std::string& fname,
  312. const FileOptions& options,
  313. std::unique_ptr<FSWritableFile>* result,
  314. IODebugContext* dbg) override {
  315. return OpenWritableFile(fname, options, true, result, dbg);
  316. }
  317. IOStatus ReuseWritableFile(const std::string& fname,
  318. const std::string& old_fname,
  319. const FileOptions& options,
  320. std::unique_ptr<FSWritableFile>* result,
  321. IODebugContext* /*dbg*/) override {
  322. result->reset();
  323. IOStatus s;
  324. int fd = -1;
  325. int flags = 0;
  326. // Direct IO mode with O_DIRECT flag or F_NOCAHCE (MAC OSX)
  327. if (options.use_direct_writes && !options.use_mmap_writes) {
  328. #ifdef ROCKSDB_LITE
  329. return IOStatus::IOError(fname,
  330. "Direct I/O not supported in RocksDB lite");
  331. #endif // !ROCKSDB_LITE
  332. flags |= O_WRONLY;
  333. #if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
  334. flags |= O_DIRECT;
  335. #endif
  336. TEST_SYNC_POINT_CALLBACK("NewWritableFile:O_DIRECT", &flags);
  337. } else if (options.use_mmap_writes) {
  338. // mmap needs O_RDWR mode
  339. flags |= O_RDWR;
  340. } else {
  341. flags |= O_WRONLY;
  342. }
  343. flags = cloexec_flags(flags, &options);
  344. do {
  345. IOSTATS_TIMER_GUARD(open_nanos);
  346. fd = open(old_fname.c_str(), flags,
  347. GetDBFileMode(allow_non_owner_access_));
  348. } while (fd < 0 && errno == EINTR);
  349. if (fd < 0) {
  350. s = IOError("while reopen file for write", fname, errno);
  351. return s;
  352. }
  353. SetFD_CLOEXEC(fd, &options);
  354. // rename into place
  355. if (rename(old_fname.c_str(), fname.c_str()) != 0) {
  356. s = IOError("while rename file to " + fname, old_fname, errno);
  357. close(fd);
  358. return s;
  359. }
  360. if (options.use_mmap_writes) {
  361. if (!checkedDiskForMmap_) {
  362. // this will be executed once in the program's lifetime.
  363. // do not use mmapWrite on non ext-3/xfs/tmpfs systems.
  364. if (!SupportsFastAllocate(fname)) {
  365. forceMmapOff_ = true;
  366. }
  367. checkedDiskForMmap_ = true;
  368. }
  369. }
  370. if (options.use_mmap_writes && !forceMmapOff_) {
  371. result->reset(new PosixMmapFile(fname, fd, page_size_, options));
  372. } else if (options.use_direct_writes && !options.use_mmap_writes) {
  373. #ifdef OS_MACOSX
  374. if (fcntl(fd, F_NOCACHE, 1) == -1) {
  375. close(fd);
  376. s = IOError("while fcntl NoCache for reopened file for append", fname,
  377. errno);
  378. return s;
  379. }
  380. #elif defined(OS_SOLARIS)
  381. if (directio(fd, DIRECTIO_ON) == -1) {
  382. if (errno != ENOTTY) { // ZFS filesystems don't support DIRECTIO_ON
  383. close(fd);
  384. s = IOError("while calling directio()", fname, errno);
  385. return s;
  386. }
  387. }
  388. #endif
  389. result->reset(new PosixWritableFile(fname, fd, options));
  390. } else {
  391. // disable mmap writes
  392. FileOptions no_mmap_writes_options = options;
  393. no_mmap_writes_options.use_mmap_writes = false;
  394. result->reset(new PosixWritableFile(fname, fd, no_mmap_writes_options));
  395. }
  396. return s;
  397. }
  398. IOStatus NewRandomRWFile(const std::string& fname, const FileOptions& options,
  399. std::unique_ptr<FSRandomRWFile>* result,
  400. IODebugContext* /*dbg*/) override {
  401. int fd = -1;
  402. int flags = cloexec_flags(O_RDWR, &options);
  403. while (fd < 0) {
  404. IOSTATS_TIMER_GUARD(open_nanos);
  405. fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
  406. if (fd < 0) {
  407. // Error while opening the file
  408. if (errno == EINTR) {
  409. continue;
  410. }
  411. return IOError("While open file for random read/write", fname, errno);
  412. }
  413. }
  414. SetFD_CLOEXEC(fd, &options);
  415. result->reset(new PosixRandomRWFile(fname, fd, options));
  416. return IOStatus::OK();
  417. }
  418. IOStatus NewMemoryMappedFileBuffer(
  419. const std::string& fname,
  420. std::unique_ptr<MemoryMappedFileBuffer>* result) override {
  421. int fd = -1;
  422. IOStatus status;
  423. int flags = cloexec_flags(O_RDWR, nullptr);
  424. while (fd < 0) {
  425. IOSTATS_TIMER_GUARD(open_nanos);
  426. fd = open(fname.c_str(), flags, 0644);
  427. if (fd < 0) {
  428. // Error while opening the file
  429. if (errno == EINTR) {
  430. continue;
  431. }
  432. status =
  433. IOError("While open file for raw mmap buffer access", fname, errno);
  434. break;
  435. }
  436. }
  437. uint64_t size;
  438. if (status.ok()) {
  439. IOOptions opts;
  440. status = GetFileSize(fname, opts, &size, nullptr);
  441. }
  442. void* base = nullptr;
  443. if (status.ok()) {
  444. base = mmap(nullptr, static_cast<size_t>(size), PROT_READ | PROT_WRITE,
  445. MAP_SHARED, fd, 0);
  446. if (base == MAP_FAILED) {
  447. status = IOError("while mmap file for read", fname, errno);
  448. }
  449. }
  450. if (status.ok()) {
  451. result->reset(
  452. new PosixMemoryMappedFileBuffer(base, static_cast<size_t>(size)));
  453. }
  454. if (fd >= 0) {
  455. // don't need to keep it open after mmap has been called
  456. close(fd);
  457. }
  458. return status;
  459. }
  460. IOStatus NewDirectory(const std::string& name, const IOOptions& /*opts*/,
  461. std::unique_ptr<FSDirectory>* result,
  462. IODebugContext* /*dbg*/) override {
  463. result->reset();
  464. int fd;
  465. int flags = cloexec_flags(0, nullptr);
  466. {
  467. IOSTATS_TIMER_GUARD(open_nanos);
  468. fd = open(name.c_str(), flags);
  469. }
  470. if (fd < 0) {
  471. return IOError("While open directory", name, errno);
  472. } else {
  473. result->reset(new PosixDirectory(fd));
  474. }
  475. return IOStatus::OK();
  476. }
  477. IOStatus NewLogger(const std::string& /*fname*/, const IOOptions& /*opts*/,
  478. std::shared_ptr<ROCKSDB_NAMESPACE::Logger>* /*ptr*/,
  479. IODebugContext* /*dbg*/) override {
  480. return IOStatus::NotSupported();
  481. }
  482. IOStatus FileExists(const std::string& fname, const IOOptions& /*opts*/,
  483. IODebugContext* /*dbg*/) override {
  484. int result = access(fname.c_str(), F_OK);
  485. if (result == 0) {
  486. return IOStatus::OK();
  487. }
  488. int err = errno;
  489. switch (err) {
  490. case EACCES:
  491. case ELOOP:
  492. case ENAMETOOLONG:
  493. case ENOENT:
  494. case ENOTDIR:
  495. return IOStatus::NotFound();
  496. default:
  497. assert(err == EIO || err == ENOMEM);
  498. return IOStatus::IOError("Unexpected error(" + ToString(err) +
  499. ") accessing file `" + fname + "' ");
  500. }
  501. }
  502. IOStatus GetChildren(const std::string& dir, const IOOptions& /*opts*/,
  503. std::vector<std::string>* result,
  504. IODebugContext* /*dbg*/) override {
  505. result->clear();
  506. DIR* d = opendir(dir.c_str());
  507. if (d == nullptr) {
  508. switch (errno) {
  509. case EACCES:
  510. case ENOENT:
  511. case ENOTDIR:
  512. return IOStatus::NotFound();
  513. default:
  514. return IOError("While opendir", dir, errno);
  515. }
  516. }
  517. struct dirent* entry;
  518. while ((entry = readdir(d)) != nullptr) {
  519. result->push_back(entry->d_name);
  520. }
  521. closedir(d);
  522. return IOStatus::OK();
  523. }
  524. IOStatus DeleteFile(const std::string& fname, const IOOptions& /*opts*/,
  525. IODebugContext* /*dbg*/) override {
  526. IOStatus result;
  527. if (unlink(fname.c_str()) != 0) {
  528. result = IOError("while unlink() file", fname, errno);
  529. }
  530. return result;
  531. }
  532. IOStatus CreateDir(const std::string& name, const IOOptions& /*opts*/,
  533. IODebugContext* /*dbg*/) override {
  534. IOStatus result;
  535. if (mkdir(name.c_str(), 0755) != 0) {
  536. result = IOError("While mkdir", name, errno);
  537. }
  538. return result;
  539. }
  540. IOStatus CreateDirIfMissing(const std::string& name,
  541. const IOOptions& /*opts*/,
  542. IODebugContext* /*dbg*/) override {
  543. IOStatus result;
  544. if (mkdir(name.c_str(), 0755) != 0) {
  545. if (errno != EEXIST) {
  546. result = IOError("While mkdir if missing", name, errno);
  547. } else if (!DirExists(name)) { // Check that name is actually a
  548. // directory.
  549. // Message is taken from mkdir
  550. result =
  551. IOStatus::IOError("`" + name + "' exists but is not a directory");
  552. }
  553. }
  554. return result;
  555. }
  556. IOStatus DeleteDir(const std::string& name, const IOOptions& /*opts*/,
  557. IODebugContext* /*dbg*/) override {
  558. IOStatus result;
  559. if (rmdir(name.c_str()) != 0) {
  560. result = IOError("file rmdir", name, errno);
  561. }
  562. return result;
  563. }
  564. IOStatus GetFileSize(const std::string& fname, const IOOptions& /*opts*/,
  565. uint64_t* size, IODebugContext* /*dbg*/) override {
  566. IOStatus s;
  567. struct stat sbuf;
  568. if (stat(fname.c_str(), &sbuf) != 0) {
  569. *size = 0;
  570. s = IOError("while stat a file for size", fname, errno);
  571. } else {
  572. *size = sbuf.st_size;
  573. }
  574. return s;
  575. }
  576. IOStatus GetFileModificationTime(const std::string& fname,
  577. const IOOptions& /*opts*/,
  578. uint64_t* file_mtime,
  579. IODebugContext* /*dbg*/) override {
  580. struct stat s;
  581. if (stat(fname.c_str(), &s) != 0) {
  582. return IOError("while stat a file for modification time", fname, errno);
  583. }
  584. *file_mtime = static_cast<uint64_t>(s.st_mtime);
  585. return IOStatus::OK();
  586. }
  587. IOStatus RenameFile(const std::string& src, const std::string& target,
  588. const IOOptions& /*opts*/,
  589. IODebugContext* /*dbg*/) override {
  590. IOStatus result;
  591. if (rename(src.c_str(), target.c_str()) != 0) {
  592. result = IOError("While renaming a file to " + target, src, errno);
  593. }
  594. return result;
  595. }
  596. IOStatus LinkFile(const std::string& src, const std::string& target,
  597. const IOOptions& /*opts*/,
  598. IODebugContext* /*dbg*/) override {
  599. IOStatus result;
  600. if (link(src.c_str(), target.c_str()) != 0) {
  601. if (errno == EXDEV) {
  602. return IOStatus::NotSupported("No cross FS links allowed");
  603. }
  604. result = IOError("while link file to " + target, src, errno);
  605. }
  606. return result;
  607. }
  608. IOStatus NumFileLinks(const std::string& fname, const IOOptions& /*opts*/,
  609. uint64_t* count, IODebugContext* /*dbg*/) override {
  610. struct stat s;
  611. if (stat(fname.c_str(), &s) != 0) {
  612. return IOError("while stat a file for num file links", fname, errno);
  613. }
  614. *count = static_cast<uint64_t>(s.st_nlink);
  615. return IOStatus::OK();
  616. }
  617. IOStatus AreFilesSame(const std::string& first, const std::string& second,
  618. const IOOptions& /*opts*/, bool* res,
  619. IODebugContext* /*dbg*/) override {
  620. struct stat statbuf[2];
  621. if (stat(first.c_str(), &statbuf[0]) != 0) {
  622. return IOError("stat file", first, errno);
  623. }
  624. if (stat(second.c_str(), &statbuf[1]) != 0) {
  625. return IOError("stat file", second, errno);
  626. }
  627. if (major(statbuf[0].st_dev) != major(statbuf[1].st_dev) ||
  628. minor(statbuf[0].st_dev) != minor(statbuf[1].st_dev) ||
  629. statbuf[0].st_ino != statbuf[1].st_ino) {
  630. *res = false;
  631. } else {
  632. *res = true;
  633. }
  634. return IOStatus::OK();
  635. }
  636. IOStatus LockFile(const std::string& fname, const IOOptions& /*opts*/,
  637. FileLock** lock, IODebugContext* /*dbg*/) override {
  638. *lock = nullptr;
  639. IOStatus result;
  640. mutex_lockedFiles.Lock();
  641. // If it already exists in the lockedFiles set, then it is already locked,
  642. // and fail this lock attempt. Otherwise, insert it into lockedFiles.
  643. // This check is needed because fcntl() does not detect lock conflict
  644. // if the fcntl is issued by the same thread that earlier acquired
  645. // this lock.
  646. // We must do this check *before* opening the file:
  647. // Otherwise, we will open a new file descriptor. Locks are associated with
  648. // a process, not a file descriptor and when *any* file descriptor is
  649. // closed, all locks the process holds for that *file* are released
  650. if (lockedFiles.insert(fname).second == false) {
  651. mutex_lockedFiles.Unlock();
  652. errno = ENOLCK;
  653. return IOError("lock ", fname, errno);
  654. }
  655. int fd;
  656. int flags = cloexec_flags(O_RDWR | O_CREAT, nullptr);
  657. {
  658. IOSTATS_TIMER_GUARD(open_nanos);
  659. fd = open(fname.c_str(), flags, 0644);
  660. }
  661. if (fd < 0) {
  662. result = IOError("while open a file for lock", fname, errno);
  663. } else if (LockOrUnlock(fd, true) == -1) {
  664. // if there is an error in locking, then remove the pathname from
  665. // lockedfiles
  666. lockedFiles.erase(fname);
  667. result = IOError("While lock file", fname, errno);
  668. close(fd);
  669. } else {
  670. SetFD_CLOEXEC(fd, nullptr);
  671. PosixFileLock* my_lock = new PosixFileLock;
  672. my_lock->fd_ = fd;
  673. my_lock->filename = fname;
  674. *lock = my_lock;
  675. }
  676. mutex_lockedFiles.Unlock();
  677. return result;
  678. }
  679. IOStatus UnlockFile(FileLock* lock, const IOOptions& /*opts*/,
  680. IODebugContext* /*dbg*/) override {
  681. PosixFileLock* my_lock = reinterpret_cast<PosixFileLock*>(lock);
  682. IOStatus result;
  683. mutex_lockedFiles.Lock();
  684. // If we are unlocking, then verify that we had locked it earlier,
  685. // it should already exist in lockedFiles. Remove it from lockedFiles.
  686. if (lockedFiles.erase(my_lock->filename) != 1) {
  687. errno = ENOLCK;
  688. result = IOError("unlock", my_lock->filename, errno);
  689. } else if (LockOrUnlock(my_lock->fd_, false) == -1) {
  690. result = IOError("unlock", my_lock->filename, errno);
  691. }
  692. close(my_lock->fd_);
  693. delete my_lock;
  694. mutex_lockedFiles.Unlock();
  695. return result;
  696. }
  697. IOStatus GetAbsolutePath(const std::string& db_path,
  698. const IOOptions& /*opts*/, std::string* output_path,
  699. IODebugContext* /*dbg*/) override {
  700. if (!db_path.empty() && db_path[0] == '/') {
  701. *output_path = db_path;
  702. return IOStatus::OK();
  703. }
  704. char the_path[256];
  705. char* ret = getcwd(the_path, 256);
  706. if (ret == nullptr) {
  707. return IOStatus::IOError(strerror(errno));
  708. }
  709. *output_path = ret;
  710. return IOStatus::OK();
  711. }
  712. IOStatus GetTestDirectory(const IOOptions& /*opts*/, std::string* result,
  713. IODebugContext* /*dbg*/) override {
  714. const char* env = getenv("TEST_TMPDIR");
  715. if (env && env[0] != '\0') {
  716. *result = env;
  717. } else {
  718. char buf[100];
  719. snprintf(buf, sizeof(buf), "/tmp/rocksdbtest-%d", int(geteuid()));
  720. *result = buf;
  721. }
  722. // Directory may already exist
  723. {
  724. IOOptions opts;
  725. CreateDir(*result, opts, nullptr);
  726. }
  727. return IOStatus::OK();
  728. }
  729. IOStatus GetFreeSpace(const std::string& fname, const IOOptions& /*opts*/,
  730. uint64_t* free_space,
  731. IODebugContext* /*dbg*/) override {
  732. struct statvfs sbuf;
  733. if (statvfs(fname.c_str(), &sbuf) < 0) {
  734. return IOError("While doing statvfs", fname, errno);
  735. }
  736. *free_space = ((uint64_t)sbuf.f_bsize * sbuf.f_bfree);
  737. return IOStatus::OK();
  738. }
  739. FileOptions OptimizeForLogWrite(const FileOptions& file_options,
  740. const DBOptions& db_options) const override {
  741. FileOptions optimized = file_options;
  742. optimized.use_mmap_writes = false;
  743. optimized.use_direct_writes = false;
  744. optimized.bytes_per_sync = db_options.wal_bytes_per_sync;
  745. // TODO(icanadi) it's faster if fallocate_with_keep_size is false, but it
  746. // breaks TransactionLogIteratorStallAtLastRecord unit test. Fix the unit
  747. // test and make this false
  748. optimized.fallocate_with_keep_size = true;
  749. optimized.writable_file_max_buffer_size =
  750. db_options.writable_file_max_buffer_size;
  751. return optimized;
  752. }
  753. FileOptions OptimizeForManifestWrite(
  754. const FileOptions& file_options) const override {
  755. FileOptions optimized = file_options;
  756. optimized.use_mmap_writes = false;
  757. optimized.use_direct_writes = false;
  758. optimized.fallocate_with_keep_size = true;
  759. return optimized;
  760. }
  761. private:
  762. bool checkedDiskForMmap_;
  763. bool forceMmapOff_; // do we override Env options?
  764. // Returns true iff the named directory exists and is a directory.
  765. virtual bool DirExists(const std::string& dname) {
  766. struct stat statbuf;
  767. if (stat(dname.c_str(), &statbuf) == 0) {
  768. return S_ISDIR(statbuf.st_mode);
  769. }
  770. return false; // stat() failed return false
  771. }
  772. bool SupportsFastAllocate(const std::string& path) {
  773. #ifdef ROCKSDB_FALLOCATE_PRESENT
  774. struct statfs s;
  775. if (statfs(path.c_str(), &s)) {
  776. return false;
  777. }
  778. switch (s.f_type) {
  779. case EXT4_SUPER_MAGIC:
  780. return true;
  781. case XFS_SUPER_MAGIC:
  782. return true;
  783. case TMPFS_MAGIC:
  784. return true;
  785. default:
  786. return false;
  787. }
  788. #else
  789. (void)path;
  790. return false;
  791. #endif
  792. }
  793. #if defined(ROCKSDB_IOURING_PRESENT)
  794. // io_uring instance
  795. std::unique_ptr<ThreadLocalPtr> thread_local_io_urings_;
  796. #endif
  797. size_t page_size_;
  798. // If true, allow non owner read access for db files. Otherwise, non-owner
  799. // has no access to db files.
  800. bool allow_non_owner_access_;
  801. };
  802. PosixFileSystem::PosixFileSystem()
  803. : checkedDiskForMmap_(false),
  804. forceMmapOff_(false),
  805. page_size_(getpagesize()),
  806. allow_non_owner_access_(true) {
  807. #if defined(ROCKSDB_IOURING_PRESENT)
  808. // Test whether IOUring is supported, and if it does, create a managing
  809. // object for thread local point so that in the future thread-local
  810. // io_uring can be created.
  811. struct io_uring* new_io_uring = CreateIOUring();
  812. if (new_io_uring != nullptr) {
  813. thread_local_io_urings_.reset(new ThreadLocalPtr(DeleteIOUring));
  814. delete new_io_uring;
  815. }
  816. #endif
  817. }
  818. } // namespace
  819. //
  820. // Default Posix FileSystem
  821. //
  822. std::shared_ptr<FileSystem> FileSystem::Default() {
  823. static PosixFileSystem default_fs;
  824. static std::shared_ptr<PosixFileSystem> default_fs_ptr(
  825. &default_fs, [](PosixFileSystem*) {});
  826. return default_fs_ptr;
  827. }
  828. } // namespace ROCKSDB_NAMESPACE