disk-io.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <common.h>
  3. #include <fs_internal.h>
  4. #include <uuid.h>
  5. #include <memalign.h>
  6. #include "kernel-shared/btrfs_tree.h"
  7. #include "common/rbtree-utils.h"
  8. #include "disk-io.h"
  9. #include "ctree.h"
  10. #include "btrfs.h"
  11. #include "volumes.h"
  12. #include "extent-io.h"
  13. #include "crypto/hash.h"
  14. /* specified errno for check_tree_block */
  15. #define BTRFS_BAD_BYTENR (-1)
  16. #define BTRFS_BAD_FSID (-2)
  17. #define BTRFS_BAD_LEVEL (-3)
  18. #define BTRFS_BAD_NRITEMS (-4)
  19. /* Calculate max possible nritems for a leaf/node */
  20. static u32 max_nritems(u8 level, u32 nodesize)
  21. {
  22. if (level == 0)
  23. return ((nodesize - sizeof(struct btrfs_header)) /
  24. sizeof(struct btrfs_item));
  25. return ((nodesize - sizeof(struct btrfs_header)) /
  26. sizeof(struct btrfs_key_ptr));
  27. }
  28. static int check_tree_block(struct btrfs_fs_info *fs_info,
  29. struct extent_buffer *buf)
  30. {
  31. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  32. u32 nodesize = fs_info->nodesize;
  33. bool fsid_match = false;
  34. int ret = BTRFS_BAD_FSID;
  35. if (buf->start != btrfs_header_bytenr(buf))
  36. return BTRFS_BAD_BYTENR;
  37. if (btrfs_header_level(buf) >= BTRFS_MAX_LEVEL)
  38. return BTRFS_BAD_LEVEL;
  39. if (btrfs_header_nritems(buf) > max_nritems(btrfs_header_level(buf),
  40. nodesize))
  41. return BTRFS_BAD_NRITEMS;
  42. /* Only leaf can be empty */
  43. if (btrfs_header_nritems(buf) == 0 &&
  44. btrfs_header_level(buf) != 0)
  45. return BTRFS_BAD_NRITEMS;
  46. while (fs_devices) {
  47. /*
  48. * Checking the incompat flag is only valid for the current
  49. * fs. For seed devices it's forbidden to have their uuid
  50. * changed so reading ->fsid in this case is fine
  51. */
  52. if (fs_devices == fs_info->fs_devices &&
  53. btrfs_fs_incompat(fs_info, METADATA_UUID))
  54. fsid_match = !memcmp_extent_buffer(buf,
  55. fs_devices->metadata_uuid,
  56. btrfs_header_fsid(),
  57. BTRFS_FSID_SIZE);
  58. else
  59. fsid_match = !memcmp_extent_buffer(buf,
  60. fs_devices->fsid,
  61. btrfs_header_fsid(),
  62. BTRFS_FSID_SIZE);
  63. if (fsid_match) {
  64. ret = 0;
  65. break;
  66. }
  67. fs_devices = fs_devices->seed;
  68. }
  69. return ret;
  70. }
  71. static void print_tree_block_error(struct btrfs_fs_info *fs_info,
  72. struct extent_buffer *eb,
  73. int err)
  74. {
  75. char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
  76. char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
  77. u8 buf[BTRFS_UUID_SIZE];
  78. if (!err)
  79. return;
  80. fprintf(stderr, "bad tree block %llu, ", eb->start);
  81. switch (err) {
  82. case BTRFS_BAD_FSID:
  83. read_extent_buffer(eb, buf, btrfs_header_fsid(),
  84. BTRFS_UUID_SIZE);
  85. uuid_unparse(buf, found_uuid);
  86. uuid_unparse(fs_info->fs_devices->metadata_uuid, fs_uuid);
  87. fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
  88. fs_uuid, found_uuid);
  89. break;
  90. case BTRFS_BAD_BYTENR:
  91. fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
  92. eb->start, btrfs_header_bytenr(eb));
  93. break;
  94. case BTRFS_BAD_LEVEL:
  95. fprintf(stderr, "bad level, %u > %d\n",
  96. btrfs_header_level(eb), BTRFS_MAX_LEVEL);
  97. break;
  98. case BTRFS_BAD_NRITEMS:
  99. fprintf(stderr, "invalid nr_items: %u\n",
  100. btrfs_header_nritems(eb));
  101. break;
  102. }
  103. }
  104. int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len)
  105. {
  106. memset(out, 0, BTRFS_CSUM_SIZE);
  107. switch (csum_type) {
  108. case BTRFS_CSUM_TYPE_CRC32:
  109. return hash_crc32c(data, len, out);
  110. case BTRFS_CSUM_TYPE_XXHASH:
  111. return hash_xxhash(data, len, out);
  112. case BTRFS_CSUM_TYPE_SHA256:
  113. return hash_sha256(data, len, out);
  114. default:
  115. printf("Unknown csum type %d\n", csum_type);
  116. return -EINVAL;
  117. }
  118. }
  119. /*
  120. * Check if the super is valid:
  121. * - nodesize/sectorsize - minimum, maximum, alignment
  122. * - tree block starts - alignment
  123. * - number of devices - something sane
  124. * - sys array size - maximum
  125. */
  126. static int btrfs_check_super(struct btrfs_super_block *sb)
  127. {
  128. u8 result[BTRFS_CSUM_SIZE];
  129. u16 csum_type;
  130. int csum_size;
  131. u8 *metadata_uuid;
  132. if (btrfs_super_magic(sb) != BTRFS_MAGIC)
  133. return -EIO;
  134. csum_type = btrfs_super_csum_type(sb);
  135. if (csum_type >= btrfs_super_num_csums()) {
  136. error("unsupported checksum algorithm %u", csum_type);
  137. return -EIO;
  138. }
  139. csum_size = btrfs_super_csum_size(sb);
  140. btrfs_csum_data(csum_type, (u8 *)sb + BTRFS_CSUM_SIZE,
  141. result, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  142. if (memcmp(result, sb->csum, csum_size)) {
  143. error("superblock checksum mismatch");
  144. return -EIO;
  145. }
  146. if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
  147. error("tree_root level too big: %d >= %d",
  148. btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
  149. goto error_out;
  150. }
  151. if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
  152. error("chunk_root level too big: %d >= %d",
  153. btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
  154. goto error_out;
  155. }
  156. if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
  157. error("log_root level too big: %d >= %d",
  158. btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
  159. goto error_out;
  160. }
  161. if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
  162. error("tree_root block unaligned: %llu", btrfs_super_root(sb));
  163. goto error_out;
  164. }
  165. if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
  166. error("chunk_root block unaligned: %llu",
  167. btrfs_super_chunk_root(sb));
  168. goto error_out;
  169. }
  170. if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
  171. error("log_root block unaligned: %llu",
  172. btrfs_super_log_root(sb));
  173. goto error_out;
  174. }
  175. if (btrfs_super_nodesize(sb) < 4096) {
  176. error("nodesize too small: %u < 4096",
  177. btrfs_super_nodesize(sb));
  178. goto error_out;
  179. }
  180. if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
  181. error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
  182. goto error_out;
  183. }
  184. if (btrfs_super_sectorsize(sb) < 4096) {
  185. error("sectorsize too small: %u < 4096",
  186. btrfs_super_sectorsize(sb));
  187. goto error_out;
  188. }
  189. if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
  190. error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
  191. goto error_out;
  192. }
  193. if (btrfs_super_total_bytes(sb) == 0) {
  194. error("invalid total_bytes 0");
  195. goto error_out;
  196. }
  197. if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
  198. error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
  199. goto error_out;
  200. }
  201. if ((btrfs_super_stripesize(sb) != 4096)
  202. && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
  203. error("invalid stripesize %u", btrfs_super_stripesize(sb));
  204. goto error_out;
  205. }
  206. if (btrfs_super_incompat_flags(sb) & BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
  207. metadata_uuid = sb->metadata_uuid;
  208. else
  209. metadata_uuid = sb->fsid;
  210. if (memcmp(metadata_uuid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) {
  211. char fsid[BTRFS_UUID_UNPARSED_SIZE];
  212. char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
  213. uuid_unparse(sb->metadata_uuid, fsid);
  214. uuid_unparse(sb->dev_item.fsid, dev_fsid);
  215. error("dev_item UUID does not match fsid: %s != %s",
  216. dev_fsid, fsid);
  217. goto error_out;
  218. }
  219. /*
  220. * Hint to catch really bogus numbers, bitflips or so
  221. */
  222. if (btrfs_super_num_devices(sb) > (1UL << 31)) {
  223. error("suspicious number of devices: %llu",
  224. btrfs_super_num_devices(sb));
  225. }
  226. if (btrfs_super_num_devices(sb) == 0) {
  227. error("number of devices is 0");
  228. goto error_out;
  229. }
  230. /*
  231. * Obvious sys_chunk_array corruptions, it must hold at least one key
  232. * and one chunk
  233. */
  234. if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
  235. error("system chunk array too big %u > %u",
  236. btrfs_super_sys_array_size(sb),
  237. BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
  238. goto error_out;
  239. }
  240. if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
  241. + sizeof(struct btrfs_chunk)) {
  242. error("system chunk array too small %u < %zu",
  243. btrfs_super_sys_array_size(sb),
  244. sizeof(struct btrfs_disk_key) +
  245. sizeof(struct btrfs_chunk));
  246. goto error_out;
  247. }
  248. return 0;
  249. error_out:
  250. error("superblock checksum matches but it has invalid members");
  251. return -EIO;
  252. }
  253. /*
  254. * btrfs_read_dev_super - read a valid primary superblock from a block device
  255. * @desc,@part: file descriptor of the device
  256. * @sb: buffer where the superblock is going to be read in
  257. *
  258. * Unlike the btrfs-progs/kernel version, here we ony care about the first
  259. * super block, thus it's much simpler.
  260. */
  261. int btrfs_read_dev_super(struct blk_desc *desc, struct disk_partition *part,
  262. struct btrfs_super_block *sb)
  263. {
  264. char tmp[BTRFS_SUPER_INFO_SIZE];
  265. struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
  266. int ret;
  267. ret = __btrfs_devread(desc, part, tmp, BTRFS_SUPER_INFO_SIZE,
  268. BTRFS_SUPER_INFO_OFFSET);
  269. if (ret < BTRFS_SUPER_INFO_SIZE)
  270. return -EIO;
  271. if (btrfs_super_bytenr(buf) != BTRFS_SUPER_INFO_OFFSET)
  272. return -EIO;
  273. if (btrfs_check_super(buf))
  274. return -EIO;
  275. memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
  276. return 0;
  277. }
  278. int btrfs_read_superblock(void)
  279. {
  280. ALLOC_CACHE_ALIGN_BUFFER(char, raw_sb, BTRFS_SUPER_INFO_SIZE);
  281. struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb;
  282. int ret;
  283. btrfs_info.sb.generation = 0;
  284. ret = btrfs_read_dev_super(btrfs_blk_desc, btrfs_part_info, sb);
  285. if (ret < 0) {
  286. pr_debug("%s: No valid BTRFS superblock found!\n", __func__);
  287. return ret;
  288. }
  289. btrfs_super_block_to_cpu(sb);
  290. memcpy(&btrfs_info.sb, sb, sizeof(*sb));
  291. if (btrfs_info.sb.num_devices != 1) {
  292. printf("%s: Unsupported number of devices (%lli). This driver "
  293. "only supports filesystem on one device.\n", __func__,
  294. btrfs_info.sb.num_devices);
  295. return -1;
  296. }
  297. pr_debug("Chosen superblock with generation = %llu\n",
  298. btrfs_info.sb.generation);
  299. return 0;
  300. }
  301. static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
  302. int verify, int silent, u16 csum_type)
  303. {
  304. u8 result[BTRFS_CSUM_SIZE];
  305. u32 len;
  306. len = buf->len - BTRFS_CSUM_SIZE;
  307. btrfs_csum_data(csum_type, (u8 *)buf->data + BTRFS_CSUM_SIZE,
  308. result, len);
  309. if (verify) {
  310. if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
  311. /* FIXME: format */
  312. if (!silent)
  313. printk("checksum verify failed on %llu found %08X wanted %08X\n",
  314. (unsigned long long)buf->start,
  315. result[0],
  316. buf->data[0]);
  317. return 1;
  318. }
  319. } else {
  320. write_extent_buffer(buf, result, 0, csum_size);
  321. }
  322. return 0;
  323. }
  324. int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify,
  325. u16 csum_type)
  326. {
  327. return __csum_tree_block_size(buf, csum_size, verify, 0, csum_type);
  328. }
  329. static int csum_tree_block(struct btrfs_fs_info *fs_info,
  330. struct extent_buffer *buf, int verify)
  331. {
  332. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  333. u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
  334. return csum_tree_block_size(buf, csum_size, verify, csum_type);
  335. }
  336. struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
  337. u64 bytenr, u32 blocksize)
  338. {
  339. return find_extent_buffer(&fs_info->extent_cache,
  340. bytenr, blocksize);
  341. }
  342. struct extent_buffer* btrfs_find_create_tree_block(
  343. struct btrfs_fs_info *fs_info, u64 bytenr)
  344. {
  345. return alloc_extent_buffer(fs_info, bytenr, fs_info->nodesize);
  346. }
  347. static int verify_parent_transid(struct extent_io_tree *io_tree,
  348. struct extent_buffer *eb, u64 parent_transid,
  349. int ignore)
  350. {
  351. int ret;
  352. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  353. return 0;
  354. if (extent_buffer_uptodate(eb) &&
  355. btrfs_header_generation(eb) == parent_transid) {
  356. ret = 0;
  357. goto out;
  358. }
  359. printk("parent transid verify failed on %llu wanted %llu found %llu\n",
  360. (unsigned long long)eb->start,
  361. (unsigned long long)parent_transid,
  362. (unsigned long long)btrfs_header_generation(eb));
  363. if (ignore) {
  364. eb->flags |= EXTENT_BAD_TRANSID;
  365. printk("Ignoring transid failure\n");
  366. return 0;
  367. }
  368. ret = 1;
  369. out:
  370. clear_extent_buffer_uptodate(eb);
  371. return ret;
  372. }
  373. int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
  374. {
  375. unsigned long offset = 0;
  376. struct btrfs_multi_bio *multi = NULL;
  377. struct btrfs_device *device;
  378. int ret = 0;
  379. u64 read_len;
  380. unsigned long bytes_left = eb->len;
  381. while (bytes_left) {
  382. read_len = bytes_left;
  383. device = NULL;
  384. ret = btrfs_map_block(info, READ, eb->start + offset,
  385. &read_len, &multi, mirror, NULL);
  386. if (ret) {
  387. printk("Couldn't map the block %Lu\n", eb->start + offset);
  388. kfree(multi);
  389. return -EIO;
  390. }
  391. device = multi->stripes[0].dev;
  392. if (!device->desc || !device->part) {
  393. kfree(multi);
  394. return -EIO;
  395. }
  396. if (read_len > bytes_left)
  397. read_len = bytes_left;
  398. ret = read_extent_from_disk(device->desc, device->part,
  399. multi->stripes[0].physical, eb,
  400. offset, read_len);
  401. kfree(multi);
  402. multi = NULL;
  403. if (ret)
  404. return -EIO;
  405. offset += read_len;
  406. bytes_left -= read_len;
  407. }
  408. return 0;
  409. }
  410. struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
  411. u64 parent_transid)
  412. {
  413. int ret;
  414. struct extent_buffer *eb;
  415. u64 best_transid = 0;
  416. u32 sectorsize = fs_info->sectorsize;
  417. int mirror_num = 1;
  418. int good_mirror = 0;
  419. int candidate_mirror = 0;
  420. int num_copies;
  421. int ignore = 0;
  422. /*
  423. * Don't even try to create tree block for unaligned tree block
  424. * bytenr.
  425. * Such unaligned tree block will free overlapping extent buffer,
  426. * causing use-after-free bugs for fuzzed images.
  427. */
  428. if (bytenr < sectorsize || !IS_ALIGNED(bytenr, sectorsize)) {
  429. error("tree block bytenr %llu is not aligned to sectorsize %u",
  430. bytenr, sectorsize);
  431. return ERR_PTR(-EIO);
  432. }
  433. eb = btrfs_find_create_tree_block(fs_info, bytenr);
  434. if (!eb)
  435. return ERR_PTR(-ENOMEM);
  436. if (btrfs_buffer_uptodate(eb, parent_transid))
  437. return eb;
  438. num_copies = btrfs_num_copies(fs_info, eb->start, eb->len);
  439. while (1) {
  440. ret = read_whole_eb(fs_info, eb, mirror_num);
  441. if (ret == 0 && csum_tree_block(fs_info, eb, 1) == 0 &&
  442. check_tree_block(fs_info, eb) == 0 &&
  443. verify_parent_transid(&fs_info->extent_cache, eb,
  444. parent_transid, ignore) == 0) {
  445. /*
  446. * check_tree_block() is less strict to allow btrfs
  447. * check to get raw eb with bad key order and fix it.
  448. * But we still need to try to get a good copy if
  449. * possible, or bad key order can go into tools like
  450. * btrfs ins dump-tree.
  451. */
  452. if (btrfs_header_level(eb))
  453. ret = btrfs_check_node(fs_info, NULL, eb);
  454. else
  455. ret = btrfs_check_leaf(fs_info, NULL, eb);
  456. if (!ret || candidate_mirror == mirror_num) {
  457. btrfs_set_buffer_uptodate(eb);
  458. return eb;
  459. }
  460. if (candidate_mirror <= 0)
  461. candidate_mirror = mirror_num;
  462. }
  463. if (ignore) {
  464. if (candidate_mirror > 0) {
  465. mirror_num = candidate_mirror;
  466. continue;
  467. }
  468. if (check_tree_block(fs_info, eb))
  469. print_tree_block_error(fs_info, eb,
  470. check_tree_block(fs_info, eb));
  471. else
  472. fprintf(stderr, "Csum didn't match\n");
  473. ret = -EIO;
  474. break;
  475. }
  476. if (num_copies == 1) {
  477. ignore = 1;
  478. continue;
  479. }
  480. if (btrfs_header_generation(eb) > best_transid) {
  481. best_transid = btrfs_header_generation(eb);
  482. good_mirror = mirror_num;
  483. }
  484. mirror_num++;
  485. if (mirror_num > num_copies) {
  486. if (candidate_mirror > 0)
  487. mirror_num = candidate_mirror;
  488. else
  489. mirror_num = good_mirror;
  490. ignore = 1;
  491. continue;
  492. }
  493. }
  494. /*
  495. * We failed to read this tree block, it be should deleted right now
  496. * to avoid stale cache populate the cache.
  497. */
  498. free_extent_buffer(eb);
  499. return ERR_PTR(ret);
  500. }
  501. void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
  502. u64 objectid)
  503. {
  504. root->node = NULL;
  505. root->track_dirty = 0;
  506. root->fs_info = fs_info;
  507. root->objectid = objectid;
  508. root->last_trans = 0;
  509. root->last_inode_alloc = 0;
  510. memset(&root->root_key, 0, sizeof(root->root_key));
  511. memset(&root->root_item, 0, sizeof(root->root_item));
  512. root->root_key.objectid = objectid;
  513. }
  514. static int find_and_setup_root(struct btrfs_root *tree_root,
  515. struct btrfs_fs_info *fs_info,
  516. u64 objectid, struct btrfs_root *root)
  517. {
  518. int ret;
  519. u64 generation;
  520. btrfs_setup_root(root, fs_info, objectid);
  521. ret = btrfs_find_last_root(tree_root, objectid,
  522. &root->root_item, &root->root_key);
  523. if (ret)
  524. return ret;
  525. generation = btrfs_root_generation(&root->root_item);
  526. root->node = read_tree_block(fs_info,
  527. btrfs_root_bytenr(&root->root_item), generation);
  528. if (!extent_buffer_uptodate(root->node))
  529. return -EIO;
  530. return 0;
  531. }
  532. int btrfs_free_fs_root(struct btrfs_root *root)
  533. {
  534. if (root->node)
  535. free_extent_buffer(root->node);
  536. kfree(root);
  537. return 0;
  538. }
  539. static void __free_fs_root(struct rb_node *node)
  540. {
  541. struct btrfs_root *root;
  542. root = container_of(node, struct btrfs_root, rb_node);
  543. btrfs_free_fs_root(root);
  544. }
  545. FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
  546. struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
  547. struct btrfs_key *location)
  548. {
  549. struct btrfs_root *root;
  550. struct btrfs_root *tree_root = fs_info->tree_root;
  551. struct btrfs_path *path;
  552. struct extent_buffer *l;
  553. u64 generation;
  554. int ret = 0;
  555. root = calloc(1, sizeof(*root));
  556. if (!root)
  557. return ERR_PTR(-ENOMEM);
  558. if (location->offset == (u64)-1) {
  559. ret = find_and_setup_root(tree_root, fs_info,
  560. location->objectid, root);
  561. if (ret) {
  562. free(root);
  563. return ERR_PTR(ret);
  564. }
  565. goto insert;
  566. }
  567. btrfs_setup_root(root, fs_info,
  568. location->objectid);
  569. path = btrfs_alloc_path();
  570. if (!path) {
  571. free(root);
  572. return ERR_PTR(-ENOMEM);
  573. }
  574. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  575. if (ret != 0) {
  576. if (ret > 0)
  577. ret = -ENOENT;
  578. goto out;
  579. }
  580. l = path->nodes[0];
  581. read_extent_buffer(l, &root->root_item,
  582. btrfs_item_ptr_offset(l, path->slots[0]),
  583. sizeof(root->root_item));
  584. memcpy(&root->root_key, location, sizeof(*location));
  585. /* If this root is already an orphan, no need to read */
  586. if (btrfs_root_refs(&root->root_item) == 0) {
  587. ret = -ENOENT;
  588. goto out;
  589. }
  590. ret = 0;
  591. out:
  592. btrfs_free_path(path);
  593. if (ret) {
  594. free(root);
  595. return ERR_PTR(ret);
  596. }
  597. generation = btrfs_root_generation(&root->root_item);
  598. root->node = read_tree_block(fs_info,
  599. btrfs_root_bytenr(&root->root_item), generation);
  600. if (!extent_buffer_uptodate(root->node)) {
  601. free(root);
  602. return ERR_PTR(-EIO);
  603. }
  604. insert:
  605. root->ref_cows = 1;
  606. return root;
  607. }
  608. static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
  609. void *data)
  610. {
  611. u64 objectid = *((u64 *)data);
  612. struct btrfs_root *root;
  613. root = rb_entry(node, struct btrfs_root, rb_node);
  614. if (objectid > root->objectid)
  615. return 1;
  616. else if (objectid < root->objectid)
  617. return -1;
  618. else
  619. return 0;
  620. }
  621. int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2)
  622. {
  623. struct btrfs_root *root;
  624. root = rb_entry(node2, struct btrfs_root, rb_node);
  625. return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
  626. }
  627. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  628. struct btrfs_key *location)
  629. {
  630. struct btrfs_root *root;
  631. struct rb_node *node;
  632. int ret;
  633. u64 objectid = location->objectid;
  634. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  635. return fs_info->tree_root;
  636. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  637. return fs_info->chunk_root;
  638. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  639. return fs_info->csum_root;
  640. BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
  641. location->offset != (u64)-1);
  642. node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
  643. btrfs_fs_roots_compare_objectids, NULL);
  644. if (node)
  645. return container_of(node, struct btrfs_root, rb_node);
  646. root = btrfs_read_fs_root_no_cache(fs_info, location);
  647. if (IS_ERR(root))
  648. return root;
  649. ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
  650. btrfs_fs_roots_compare_roots);
  651. BUG_ON(ret);
  652. return root;
  653. }
  654. void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
  655. {
  656. free(fs_info->tree_root);
  657. free(fs_info->chunk_root);
  658. free(fs_info->csum_root);
  659. free(fs_info->super_copy);
  660. free(fs_info);
  661. }
  662. struct btrfs_fs_info *btrfs_new_fs_info(void)
  663. {
  664. struct btrfs_fs_info *fs_info;
  665. fs_info = calloc(1, sizeof(struct btrfs_fs_info));
  666. if (!fs_info)
  667. return NULL;
  668. fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
  669. fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
  670. fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
  671. fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
  672. if (!fs_info->tree_root || !fs_info->chunk_root ||
  673. !fs_info->csum_root || !fs_info->super_copy)
  674. goto free_all;
  675. extent_io_tree_init(&fs_info->extent_cache);
  676. fs_info->fs_root_tree = RB_ROOT;
  677. cache_tree_init(&fs_info->mapping_tree.cache_tree);
  678. mutex_init(&fs_info->fs_mutex);
  679. return fs_info;
  680. free_all:
  681. btrfs_free_fs_info(fs_info);
  682. return NULL;
  683. }
  684. static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
  685. struct btrfs_root *info_root,
  686. u64 objectid, char *str)
  687. {
  688. struct btrfs_root *root = fs_info->tree_root;
  689. int ret;
  690. ret = find_and_setup_root(root, fs_info, objectid, info_root);
  691. if (ret) {
  692. error("could not setup %s tree", str);
  693. return -EIO;
  694. }
  695. return 0;
  696. }
  697. int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info)
  698. {
  699. struct btrfs_super_block *sb = fs_info->super_copy;
  700. struct btrfs_root *root;
  701. struct btrfs_key key;
  702. u64 root_tree_bytenr;
  703. u64 generation;
  704. int ret;
  705. root = fs_info->tree_root;
  706. btrfs_setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  707. generation = btrfs_super_generation(sb);
  708. root_tree_bytenr = btrfs_super_root(sb);
  709. root->node = read_tree_block(fs_info, root_tree_bytenr, generation);
  710. if (!extent_buffer_uptodate(root->node)) {
  711. fprintf(stderr, "Couldn't read tree root\n");
  712. return -EIO;
  713. }
  714. ret = setup_root_or_create_block(fs_info, fs_info->csum_root,
  715. BTRFS_CSUM_TREE_OBJECTID, "csum");
  716. if (ret)
  717. return ret;
  718. fs_info->csum_root->track_dirty = 1;
  719. fs_info->last_trans_committed = generation;
  720. key.objectid = BTRFS_FS_TREE_OBJECTID;
  721. key.type = BTRFS_ROOT_ITEM_KEY;
  722. key.offset = (u64)-1;
  723. fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
  724. if (IS_ERR(fs_info->fs_root))
  725. return -EIO;
  726. return 0;
  727. }
  728. void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
  729. {
  730. if (fs_info->csum_root)
  731. free_extent_buffer(fs_info->csum_root->node);
  732. if (fs_info->tree_root)
  733. free_extent_buffer(fs_info->tree_root->node);
  734. if (fs_info->chunk_root)
  735. free_extent_buffer(fs_info->chunk_root->node);
  736. }
  737. static void free_map_lookup(struct cache_extent *ce)
  738. {
  739. struct map_lookup *map;
  740. map = container_of(ce, struct map_lookup, ce);
  741. kfree(map);
  742. }
  743. FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
  744. void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
  745. {
  746. free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
  747. extent_io_tree_cleanup(&fs_info->extent_cache);
  748. }
  749. static int btrfs_scan_fs_devices(struct blk_desc *desc,
  750. struct disk_partition *part,
  751. struct btrfs_fs_devices **fs_devices)
  752. {
  753. u64 total_devs;
  754. int ret;
  755. if (round_up(BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
  756. desc->blksz) > (part->size << desc->log2blksz)) {
  757. error("superblock end %u is larger than device size " LBAFU,
  758. BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
  759. part->size << desc->log2blksz);
  760. return -EINVAL;
  761. }
  762. ret = btrfs_scan_one_device(desc, part, fs_devices, &total_devs);
  763. if (ret) {
  764. fprintf(stderr, "No valid Btrfs found\n");
  765. return ret;
  766. }
  767. return 0;
  768. }
  769. int btrfs_check_fs_compatibility(struct btrfs_super_block *sb)
  770. {
  771. u64 features;
  772. features = btrfs_super_incompat_flags(sb) &
  773. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  774. if (features) {
  775. printk("couldn't open because of unsupported "
  776. "option features (%llx).\n",
  777. (unsigned long long)features);
  778. return -ENOTSUPP;
  779. }
  780. features = btrfs_super_incompat_flags(sb);
  781. if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
  782. features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
  783. btrfs_set_super_incompat_flags(sb, features);
  784. }
  785. return 0;
  786. }
  787. static int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
  788. {
  789. struct btrfs_super_block *sb = fs_info->super_copy;
  790. u64 chunk_root_bytenr;
  791. u64 generation;
  792. int ret;
  793. btrfs_setup_root(fs_info->chunk_root, fs_info,
  794. BTRFS_CHUNK_TREE_OBJECTID);
  795. ret = btrfs_read_sys_array(fs_info);
  796. if (ret)
  797. return ret;
  798. generation = btrfs_super_chunk_root_generation(sb);
  799. chunk_root_bytenr = btrfs_super_chunk_root(sb);
  800. fs_info->chunk_root->node = read_tree_block(fs_info,
  801. chunk_root_bytenr,
  802. generation);
  803. if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
  804. error("cannot read chunk root");
  805. return -EIO;
  806. }
  807. ret = btrfs_read_chunk_tree(fs_info);
  808. if (ret) {
  809. fprintf(stderr, "Couldn't read chunk tree\n");
  810. return ret;
  811. }
  812. return 0;
  813. }
  814. struct btrfs_fs_info *open_ctree_fs_info(struct blk_desc *desc,
  815. struct disk_partition *part)
  816. {
  817. struct btrfs_fs_info *fs_info;
  818. struct btrfs_super_block *disk_super;
  819. struct btrfs_fs_devices *fs_devices = NULL;
  820. struct extent_buffer *eb;
  821. int ret;
  822. fs_info = btrfs_new_fs_info();
  823. if (!fs_info) {
  824. fprintf(stderr, "Failed to allocate memory for fs_info\n");
  825. return NULL;
  826. }
  827. ret = btrfs_scan_fs_devices(desc, part, &fs_devices);
  828. if (ret)
  829. goto out;
  830. fs_info->fs_devices = fs_devices;
  831. ret = btrfs_open_devices(fs_devices);
  832. if (ret)
  833. goto out;
  834. disk_super = fs_info->super_copy;
  835. ret = btrfs_read_dev_super(desc, part, disk_super);
  836. if (ret) {
  837. printk("No valid btrfs found\n");
  838. goto out_devices;
  839. }
  840. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID) {
  841. fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
  842. goto out_devices;
  843. }
  844. ASSERT(!memcmp(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE));
  845. if (btrfs_fs_incompat(fs_info, METADATA_UUID))
  846. ASSERT(!memcmp(disk_super->metadata_uuid,
  847. fs_devices->metadata_uuid, BTRFS_FSID_SIZE));
  848. fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
  849. fs_info->nodesize = btrfs_super_nodesize(disk_super);
  850. fs_info->stripesize = btrfs_super_stripesize(disk_super);
  851. ret = btrfs_check_fs_compatibility(fs_info->super_copy);
  852. if (ret)
  853. goto out_devices;
  854. ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
  855. if (ret)
  856. goto out_chunk;
  857. /* Chunk tree root is unable to read, return directly */
  858. if (!fs_info->chunk_root)
  859. return fs_info;
  860. eb = fs_info->chunk_root->node;
  861. read_extent_buffer(eb, fs_info->chunk_tree_uuid,
  862. btrfs_header_chunk_tree_uuid(eb),
  863. BTRFS_UUID_SIZE);
  864. ret = btrfs_setup_all_roots(fs_info);
  865. if (ret)
  866. goto out_chunk;
  867. return fs_info;
  868. out_chunk:
  869. btrfs_release_all_roots(fs_info);
  870. btrfs_cleanup_all_caches(fs_info);
  871. out_devices:
  872. btrfs_close_devices(fs_devices);
  873. out:
  874. btrfs_free_fs_info(fs_info);
  875. return NULL;
  876. }
  877. int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
  878. {
  879. int ret;
  880. int err = 0;
  881. free_fs_roots_tree(&fs_info->fs_root_tree);
  882. btrfs_release_all_roots(fs_info);
  883. ret = btrfs_close_devices(fs_info->fs_devices);
  884. btrfs_cleanup_all_caches(fs_info);
  885. btrfs_free_fs_info(fs_info);
  886. if (!err)
  887. err = ret;
  888. return err;
  889. }
  890. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  891. {
  892. int ret;
  893. ret = extent_buffer_uptodate(buf);
  894. if (!ret)
  895. return ret;
  896. ret = verify_parent_transid(&buf->fs_info->extent_cache, buf,
  897. parent_transid, 1);
  898. return !ret;
  899. }
  900. int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
  901. {
  902. return set_extent_buffer_uptodate(eb);
  903. }