fs.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/crc32.h>
  3. #include "messages.h"
  4. #include "fs.h"
  5. #include "accessors.h"
  6. #include "volumes.h"
  7. static const struct btrfs_csums {
  8. u16 size;
  9. const char name[10];
  10. } btrfs_csums[] = {
  11. [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
  12. [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
  13. [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
  14. [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b" },
  15. };
  16. /* This exists for btrfs-progs usages. */
  17. u16 btrfs_csum_type_size(u16 type)
  18. {
  19. return btrfs_csums[type].size;
  20. }
  21. int btrfs_super_csum_size(const struct btrfs_super_block *s)
  22. {
  23. u16 t = btrfs_super_csum_type(s);
  24. /* csum type is validated at mount time. */
  25. return btrfs_csum_type_size(t);
  26. }
  27. const char *btrfs_super_csum_name(u16 csum_type)
  28. {
  29. /* csum type is validated at mount time. */
  30. return btrfs_csums[csum_type].name;
  31. }
  32. size_t __attribute_const__ btrfs_get_num_csums(void)
  33. {
  34. return ARRAY_SIZE(btrfs_csums);
  35. }
  36. void btrfs_csum(u16 csum_type, const u8 *data, size_t len, u8 *out)
  37. {
  38. switch (csum_type) {
  39. case BTRFS_CSUM_TYPE_CRC32:
  40. put_unaligned_le32(~crc32c(~0, data, len), out);
  41. break;
  42. case BTRFS_CSUM_TYPE_XXHASH:
  43. put_unaligned_le64(xxh64(data, len, 0), out);
  44. break;
  45. case BTRFS_CSUM_TYPE_SHA256:
  46. sha256(data, len, out);
  47. break;
  48. case BTRFS_CSUM_TYPE_BLAKE2:
  49. blake2b(NULL, 0, data, len, out, 32);
  50. break;
  51. default:
  52. /* Checksum type is validated at mount time. */
  53. BUG();
  54. }
  55. }
  56. void btrfs_csum_init(struct btrfs_csum_ctx *ctx, u16 csum_type)
  57. {
  58. ctx->csum_type = csum_type;
  59. switch (ctx->csum_type) {
  60. case BTRFS_CSUM_TYPE_CRC32:
  61. ctx->crc32 = ~0;
  62. break;
  63. case BTRFS_CSUM_TYPE_XXHASH:
  64. xxh64_reset(&ctx->xxh64, 0);
  65. break;
  66. case BTRFS_CSUM_TYPE_SHA256:
  67. sha256_init(&ctx->sha256);
  68. break;
  69. case BTRFS_CSUM_TYPE_BLAKE2:
  70. blake2b_init(&ctx->blake2b, 32);
  71. break;
  72. default:
  73. /* Checksume type is validated at mount time. */
  74. BUG();
  75. }
  76. }
  77. void btrfs_csum_update(struct btrfs_csum_ctx *ctx, const u8 *data, size_t len)
  78. {
  79. switch (ctx->csum_type) {
  80. case BTRFS_CSUM_TYPE_CRC32:
  81. ctx->crc32 = crc32c(ctx->crc32, data, len);
  82. break;
  83. case BTRFS_CSUM_TYPE_XXHASH:
  84. xxh64_update(&ctx->xxh64, data, len);
  85. break;
  86. case BTRFS_CSUM_TYPE_SHA256:
  87. sha256_update(&ctx->sha256, data, len);
  88. break;
  89. case BTRFS_CSUM_TYPE_BLAKE2:
  90. blake2b_update(&ctx->blake2b, data, len);
  91. break;
  92. default:
  93. /* Checksum type is validated at mount time. */
  94. BUG();
  95. }
  96. }
  97. void btrfs_csum_final(struct btrfs_csum_ctx *ctx, u8 *out)
  98. {
  99. switch (ctx->csum_type) {
  100. case BTRFS_CSUM_TYPE_CRC32:
  101. put_unaligned_le32(~ctx->crc32, out);
  102. break;
  103. case BTRFS_CSUM_TYPE_XXHASH:
  104. put_unaligned_le64(xxh64_digest(&ctx->xxh64), out);
  105. break;
  106. case BTRFS_CSUM_TYPE_SHA256:
  107. sha256_final(&ctx->sha256, out);
  108. break;
  109. case BTRFS_CSUM_TYPE_BLAKE2:
  110. blake2b_final(&ctx->blake2b, out);
  111. break;
  112. default:
  113. /* Checksum type is validated at mount time. */
  114. BUG();
  115. }
  116. }
  117. /*
  118. * We support the following block sizes for all systems:
  119. *
  120. * - 4K
  121. * This is the most common block size. For PAGE SIZE > 4K cases the subpage
  122. * mode is used.
  123. *
  124. * - PAGE_SIZE
  125. * The straightforward block size to support.
  126. *
  127. * And extra support for the following block sizes based on the kernel config:
  128. *
  129. * - MIN_BLOCKSIZE
  130. * This is either 4K (regular builds) or 2K (debug builds)
  131. * This allows testing subpage routines on x86_64.
  132. */
  133. bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize)
  134. {
  135. /* @blocksize should be validated first. */
  136. ASSERT(is_power_of_2(blocksize) && blocksize >= BTRFS_MIN_BLOCKSIZE &&
  137. blocksize <= BTRFS_MAX_BLOCKSIZE);
  138. if (blocksize == PAGE_SIZE || blocksize == SZ_4K || blocksize == BTRFS_MIN_BLOCKSIZE)
  139. return true;
  140. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  141. /*
  142. * For bs > ps support it's done by specifying a minimal folio order
  143. * for filemap, thus implying large data folios.
  144. * For HIGHMEM systems, we can not always access the content of a (large)
  145. * folio in one go, but go through them page by page.
  146. *
  147. * A lot of features don't implement a proper PAGE sized loop for large
  148. * folios, this includes:
  149. *
  150. * - compression
  151. * - verity
  152. * - encoded write
  153. *
  154. * Considering HIGHMEM is such a pain to deal with and it's going
  155. * to be deprecated eventually, just reject HIGHMEM && bs > ps cases.
  156. */
  157. if (IS_ENABLED(CONFIG_HIGHMEM) && blocksize > PAGE_SIZE)
  158. return false;
  159. return true;
  160. #endif
  161. return false;
  162. }
  163. /*
  164. * Start exclusive operation @type, return true on success.
  165. */
  166. bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
  167. enum btrfs_exclusive_operation type)
  168. {
  169. bool ret = false;
  170. spin_lock(&fs_info->super_lock);
  171. if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
  172. fs_info->exclusive_operation = type;
  173. ret = true;
  174. }
  175. spin_unlock(&fs_info->super_lock);
  176. return ret;
  177. }
  178. /*
  179. * Conditionally allow to enter the exclusive operation in case it's compatible
  180. * with the running one. This must be paired with btrfs_exclop_start_unlock()
  181. * and btrfs_exclop_finish().
  182. *
  183. * Compatibility:
  184. * - the same type is already running
  185. * - when trying to add a device and balance has been paused
  186. * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller
  187. * must check the condition first that would allow none -> @type
  188. */
  189. bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
  190. enum btrfs_exclusive_operation type)
  191. {
  192. spin_lock(&fs_info->super_lock);
  193. if (fs_info->exclusive_operation == type ||
  194. (fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED &&
  195. type == BTRFS_EXCLOP_DEV_ADD))
  196. return true;
  197. spin_unlock(&fs_info->super_lock);
  198. return false;
  199. }
  200. void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info)
  201. {
  202. spin_unlock(&fs_info->super_lock);
  203. }
  204. void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
  205. {
  206. spin_lock(&fs_info->super_lock);
  207. WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
  208. spin_unlock(&fs_info->super_lock);
  209. sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
  210. }
  211. void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
  212. enum btrfs_exclusive_operation op)
  213. {
  214. switch (op) {
  215. case BTRFS_EXCLOP_BALANCE_PAUSED:
  216. spin_lock(&fs_info->super_lock);
  217. ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE ||
  218. fs_info->exclusive_operation == BTRFS_EXCLOP_DEV_ADD ||
  219. fs_info->exclusive_operation == BTRFS_EXCLOP_NONE ||
  220. fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED);
  221. fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE_PAUSED;
  222. spin_unlock(&fs_info->super_lock);
  223. break;
  224. case BTRFS_EXCLOP_BALANCE:
  225. spin_lock(&fs_info->super_lock);
  226. ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED);
  227. fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE;
  228. spin_unlock(&fs_info->super_lock);
  229. break;
  230. default:
  231. btrfs_warn(fs_info,
  232. "invalid exclop balance operation %d requested", op);
  233. }
  234. }
  235. void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
  236. const char *name)
  237. {
  238. struct btrfs_super_block *disk_super;
  239. u64 features;
  240. disk_super = fs_info->super_copy;
  241. features = btrfs_super_incompat_flags(disk_super);
  242. if (!(features & flag)) {
  243. spin_lock(&fs_info->super_lock);
  244. features = btrfs_super_incompat_flags(disk_super);
  245. if (!(features & flag)) {
  246. features |= flag;
  247. btrfs_set_super_incompat_flags(disk_super, features);
  248. btrfs_info(fs_info,
  249. "setting incompat feature flag for %s (0x%llx)",
  250. name, flag);
  251. }
  252. spin_unlock(&fs_info->super_lock);
  253. set_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags);
  254. }
  255. }
  256. void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
  257. const char *name)
  258. {
  259. struct btrfs_super_block *disk_super;
  260. u64 features;
  261. disk_super = fs_info->super_copy;
  262. features = btrfs_super_incompat_flags(disk_super);
  263. if (features & flag) {
  264. spin_lock(&fs_info->super_lock);
  265. features = btrfs_super_incompat_flags(disk_super);
  266. if (features & flag) {
  267. features &= ~flag;
  268. btrfs_set_super_incompat_flags(disk_super, features);
  269. btrfs_info(fs_info,
  270. "clearing incompat feature flag for %s (0x%llx)",
  271. name, flag);
  272. }
  273. spin_unlock(&fs_info->super_lock);
  274. set_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags);
  275. }
  276. }
  277. void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
  278. const char *name)
  279. {
  280. struct btrfs_super_block *disk_super;
  281. u64 features;
  282. disk_super = fs_info->super_copy;
  283. features = btrfs_super_compat_ro_flags(disk_super);
  284. if (!(features & flag)) {
  285. spin_lock(&fs_info->super_lock);
  286. features = btrfs_super_compat_ro_flags(disk_super);
  287. if (!(features & flag)) {
  288. features |= flag;
  289. btrfs_set_super_compat_ro_flags(disk_super, features);
  290. btrfs_info(fs_info,
  291. "setting compat-ro feature flag for %s (0x%llx)",
  292. name, flag);
  293. }
  294. spin_unlock(&fs_info->super_lock);
  295. set_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags);
  296. }
  297. }
  298. void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
  299. const char *name)
  300. {
  301. struct btrfs_super_block *disk_super;
  302. u64 features;
  303. disk_super = fs_info->super_copy;
  304. features = btrfs_super_compat_ro_flags(disk_super);
  305. if (features & flag) {
  306. spin_lock(&fs_info->super_lock);
  307. features = btrfs_super_compat_ro_flags(disk_super);
  308. if (features & flag) {
  309. features &= ~flag;
  310. btrfs_set_super_compat_ro_flags(disk_super, features);
  311. btrfs_info(fs_info,
  312. "clearing compat-ro feature flag for %s (0x%llx)",
  313. name, flag);
  314. }
  315. spin_unlock(&fs_info->super_lock);
  316. set_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags);
  317. }
  318. }