You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.2 KiB

9 months ago
  1. /*
  2. ISC License
  3. Copyright (c) 2024, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "func_open_create_utils.hpp"
  16. #include "procfs_get_name.hpp"
  17. /*
  18. The kernel expects being able to issue read requests when running
  19. with writeback caching enabled so we must change O_WRONLY to
  20. O_RDWR.
  21. With writeback caching enabled the kernel handles O_APPEND. Could
  22. be an issue if the underlying file changes out of band but that is
  23. true of any caching.
  24. */
  25. void
  26. utils::tweak_flags_writeback_cache(int *flags_)
  27. {
  28. if((*flags_ & O_ACCMODE) == O_WRONLY)
  29. *flags_ = ((*flags_ & ~O_ACCMODE) | O_RDWR);
  30. if(*flags_ & O_APPEND)
  31. *flags_ &= ~O_APPEND;
  32. }
  33. namespace l
  34. {
  35. static
  36. bool
  37. rdonly(const int flags_)
  38. {
  39. return ((flags_ & O_ACCMODE) == O_RDONLY);
  40. }
  41. }
  42. bool
  43. utils::calculate_flush(FlushOnClose flushonclose_,
  44. int const flags_)
  45. {
  46. switch(flushonclose_)
  47. {
  48. case FlushOnCloseEnum::NEVER:
  49. return false;
  50. case FlushOnCloseEnum::OPENED_FOR_WRITE:
  51. return !l::rdonly(flags_);
  52. case FlushOnCloseEnum::ALWAYS:
  53. return true;
  54. }
  55. return true;
  56. }
  57. void
  58. utils::cfg_to_ffi_flags(Config::Read &cfg_,
  59. int const tid_,
  60. fuse_file_info_t *ffi_)
  61. {
  62. switch(cfg_->cache_files)
  63. {
  64. case CacheFiles::ENUM::LIBFUSE:
  65. ffi_->direct_io = cfg_->direct_io;
  66. ffi_->keep_cache = cfg_->kernel_cache;
  67. ffi_->auto_cache = cfg_->auto_cache;
  68. break;
  69. case CacheFiles::ENUM::OFF:
  70. ffi_->direct_io = 1;
  71. ffi_->keep_cache = 0;
  72. ffi_->auto_cache = 0;
  73. break;
  74. case CacheFiles::ENUM::PARTIAL:
  75. ffi_->direct_io = 0;
  76. ffi_->keep_cache = 0;
  77. ffi_->auto_cache = 0;
  78. break;
  79. case CacheFiles::ENUM::FULL:
  80. ffi_->direct_io = 0;
  81. ffi_->keep_cache = 1;
  82. ffi_->auto_cache = 0;
  83. break;
  84. case CacheFiles::ENUM::AUTO_FULL:
  85. ffi_->direct_io = 0;
  86. ffi_->keep_cache = 0;
  87. ffi_->auto_cache = 1;
  88. break;
  89. case CacheFiles::ENUM::PER_PROCESS:
  90. std::string proc_name;
  91. proc_name = procfs::get_name(tid_);
  92. if(cfg_->cache_files_process_names.count(proc_name) == 0)
  93. {
  94. ffi_->direct_io = 1;
  95. ffi_->keep_cache = 0;
  96. ffi_->auto_cache = 0;
  97. }
  98. else
  99. {
  100. ffi_->direct_io = 0;
  101. ffi_->keep_cache = 0;
  102. ffi_->auto_cache = 0;
  103. }
  104. break;
  105. }
  106. if(cfg_->parallel_direct_writes == true)
  107. ffi_->parallel_direct_writes = ffi_->direct_io;
  108. }