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.

155 lines
4.1 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include <fuse.h>
  15. #include <sys/statvfs.h>
  16. #include <errno.h>
  17. #include <climits>
  18. #include <set>
  19. #include <string>
  20. #include <vector>
  21. #include "config.hpp"
  22. #include "rwlock.hpp"
  23. #include "ugid.hpp"
  24. using std::string;
  25. using std::vector;
  26. using std::set;
  27. using std::pair;
  28. #define CMP(FOO) (lhs.f_##FOO < rhs.f_##FOO)
  29. struct
  30. statvfs_compare
  31. {
  32. bool
  33. operator()(const struct statvfs &lhs,
  34. const struct statvfs &rhs) const
  35. {
  36. return (CMP(bsize) &&
  37. CMP(frsize) &&
  38. CMP(blocks) &&
  39. CMP(bfree) &&
  40. CMP(bavail) &&
  41. CMP(files) &&
  42. CMP(ffree) &&
  43. CMP(favail) &&
  44. CMP(fsid) &&
  45. CMP(flag) &&
  46. CMP(namemax));
  47. }
  48. };
  49. typedef set<struct statvfs,statvfs_compare> statvfs_set;
  50. static
  51. void
  52. _normalize_statvfs(struct statvfs *fsstat,
  53. const unsigned long min_bsize,
  54. const unsigned long min_frsize,
  55. const unsigned long min_namemax)
  56. {
  57. fsstat->f_blocks = (fsblkcnt_t)((fsstat->f_blocks * fsstat->f_frsize) / min_frsize);
  58. fsstat->f_bfree = (fsblkcnt_t)((fsstat->f_bfree * fsstat->f_frsize) / min_frsize);
  59. fsstat->f_bavail = (fsblkcnt_t)((fsstat->f_bavail * fsstat->f_frsize) / min_frsize);
  60. fsstat->f_bsize = min_bsize;
  61. fsstat->f_frsize = min_frsize;
  62. fsstat->f_namemax = min_namemax;
  63. }
  64. static
  65. void
  66. _merge_statvfs(struct statvfs * const out,
  67. const struct statvfs * const in)
  68. {
  69. out->f_blocks += in->f_blocks;
  70. out->f_bfree += in->f_bfree;
  71. out->f_bavail += in->f_bavail;
  72. out->f_files += in->f_files;
  73. out->f_ffree += in->f_ffree;
  74. out->f_favail += in->f_favail;
  75. }
  76. static
  77. int
  78. _statfs(const vector<string> &srcmounts,
  79. struct statvfs &fsstat)
  80. {
  81. statvfs_set fsstats;
  82. unsigned long min_bsize = ULONG_MAX;
  83. unsigned long min_frsize = ULONG_MAX;
  84. unsigned long min_namemax = ULONG_MAX;
  85. for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
  86. {
  87. int rv;
  88. struct statvfs fsstat;
  89. rv = ::statvfs(srcmounts[i].c_str(),&fsstat);
  90. if(rv == -1)
  91. continue;
  92. if(min_bsize > fsstat.f_bsize)
  93. min_bsize = fsstat.f_bsize;
  94. if(min_frsize > fsstat.f_frsize)
  95. min_frsize = fsstat.f_frsize;
  96. if(min_namemax > fsstat.f_namemax)
  97. min_namemax = fsstat.f_namemax;
  98. fsstats.insert(fsstat);
  99. }
  100. statvfs_set::const_iterator fsstatiter = fsstats.begin();
  101. statvfs_set::const_iterator endfsstatiter = fsstats.end();
  102. if(fsstatiter != endfsstatiter)
  103. {
  104. fsstat = *fsstatiter;
  105. _normalize_statvfs(&fsstat,min_bsize,min_frsize,min_namemax);
  106. for(++fsstatiter;fsstatiter != endfsstatiter;++fsstatiter)
  107. {
  108. struct statvfs tmp = *fsstatiter;
  109. _normalize_statvfs(&tmp,min_bsize,min_frsize,min_namemax);
  110. _merge_statvfs(&fsstat,&tmp);
  111. }
  112. }
  113. return 0;
  114. }
  115. namespace mergerfs
  116. {
  117. namespace fuse
  118. {
  119. int
  120. statfs(const char *fusepath,
  121. struct statvfs *stat)
  122. {
  123. const fuse_context *fc = fuse_get_context();
  124. const Config &config = Config::get(fc);
  125. const ugid::Set ugid(fc->uid,fc->gid);
  126. const rwlock::ReadGuard readlock(&config.srcmountslock);
  127. return _statfs(config.srcmounts,
  128. *stat);
  129. }
  130. }
  131. }