volumes.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <stdlib.h>
  3. #include <common.h>
  4. #include <fs_internal.h>
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "volumes.h"
  8. #include "extent-io.h"
  9. const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
  10. [BTRFS_RAID_RAID10] = {
  11. .sub_stripes = 2,
  12. .dev_stripes = 1,
  13. .devs_max = 0, /* 0 == as many as possible */
  14. .devs_min = 4,
  15. .tolerated_failures = 1,
  16. .devs_increment = 2,
  17. .ncopies = 2,
  18. .nparity = 0,
  19. .raid_name = "raid10",
  20. .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
  21. },
  22. [BTRFS_RAID_RAID1] = {
  23. .sub_stripes = 1,
  24. .dev_stripes = 1,
  25. .devs_max = 2,
  26. .devs_min = 2,
  27. .tolerated_failures = 1,
  28. .devs_increment = 2,
  29. .ncopies = 2,
  30. .nparity = 0,
  31. .raid_name = "raid1",
  32. .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
  33. },
  34. [BTRFS_RAID_RAID1C3] = {
  35. .sub_stripes = 1,
  36. .dev_stripes = 1,
  37. .devs_max = 3,
  38. .devs_min = 3,
  39. .tolerated_failures = 2,
  40. .devs_increment = 3,
  41. .ncopies = 3,
  42. .raid_name = "raid1c3",
  43. .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3,
  44. },
  45. [BTRFS_RAID_RAID1C4] = {
  46. .sub_stripes = 1,
  47. .dev_stripes = 1,
  48. .devs_max = 4,
  49. .devs_min = 4,
  50. .tolerated_failures = 3,
  51. .devs_increment = 4,
  52. .ncopies = 4,
  53. .raid_name = "raid1c4",
  54. .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4,
  55. },
  56. [BTRFS_RAID_DUP] = {
  57. .sub_stripes = 1,
  58. .dev_stripes = 2,
  59. .devs_max = 1,
  60. .devs_min = 1,
  61. .tolerated_failures = 0,
  62. .devs_increment = 1,
  63. .ncopies = 2,
  64. .nparity = 0,
  65. .raid_name = "dup",
  66. .bg_flag = BTRFS_BLOCK_GROUP_DUP,
  67. },
  68. [BTRFS_RAID_RAID0] = {
  69. .sub_stripes = 1,
  70. .dev_stripes = 1,
  71. .devs_max = 0,
  72. .devs_min = 2,
  73. .tolerated_failures = 0,
  74. .devs_increment = 1,
  75. .ncopies = 1,
  76. .nparity = 0,
  77. .raid_name = "raid0",
  78. .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
  79. },
  80. [BTRFS_RAID_SINGLE] = {
  81. .sub_stripes = 1,
  82. .dev_stripes = 1,
  83. .devs_max = 1,
  84. .devs_min = 1,
  85. .tolerated_failures = 0,
  86. .devs_increment = 1,
  87. .ncopies = 1,
  88. .nparity = 0,
  89. .raid_name = "single",
  90. .bg_flag = 0,
  91. },
  92. [BTRFS_RAID_RAID5] = {
  93. .sub_stripes = 1,
  94. .dev_stripes = 1,
  95. .devs_max = 0,
  96. .devs_min = 2,
  97. .tolerated_failures = 1,
  98. .devs_increment = 1,
  99. .ncopies = 1,
  100. .nparity = 1,
  101. .raid_name = "raid5",
  102. .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
  103. },
  104. [BTRFS_RAID_RAID6] = {
  105. .sub_stripes = 1,
  106. .dev_stripes = 1,
  107. .devs_max = 0,
  108. .devs_min = 3,
  109. .tolerated_failures = 2,
  110. .devs_increment = 1,
  111. .ncopies = 1,
  112. .nparity = 2,
  113. .raid_name = "raid6",
  114. .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
  115. },
  116. };
  117. struct stripe {
  118. struct btrfs_device *dev;
  119. u64 physical;
  120. };
  121. static inline int nr_parity_stripes(struct map_lookup *map)
  122. {
  123. if (map->type & BTRFS_BLOCK_GROUP_RAID5)
  124. return 1;
  125. else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  126. return 2;
  127. else
  128. return 0;
  129. }
  130. static inline int nr_data_stripes(struct map_lookup *map)
  131. {
  132. return map->num_stripes - nr_parity_stripes(map);
  133. }
  134. #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
  135. static LIST_HEAD(fs_uuids);
  136. /*
  137. * Find a device specified by @devid or @uuid in the list of @fs_devices, or
  138. * return NULL.
  139. *
  140. * If devid and uuid are both specified, the match must be exact, otherwise
  141. * only devid is used.
  142. */
  143. static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
  144. u64 devid, u8 *uuid)
  145. {
  146. struct list_head *head = &fs_devices->devices;
  147. struct btrfs_device *dev;
  148. list_for_each_entry(dev, head, dev_list) {
  149. if (dev->devid == devid &&
  150. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  151. return dev;
  152. }
  153. }
  154. return NULL;
  155. }
  156. static struct btrfs_fs_devices *find_fsid(u8 *fsid, u8 *metadata_uuid)
  157. {
  158. struct btrfs_fs_devices *fs_devices;
  159. list_for_each_entry(fs_devices, &fs_uuids, list) {
  160. if (metadata_uuid && (memcmp(fsid, fs_devices->fsid,
  161. BTRFS_FSID_SIZE) == 0) &&
  162. (memcmp(metadata_uuid, fs_devices->metadata_uuid,
  163. BTRFS_FSID_SIZE) == 0)) {
  164. return fs_devices;
  165. } else if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0){
  166. return fs_devices;
  167. }
  168. }
  169. return NULL;
  170. }
  171. static int device_list_add(struct btrfs_super_block *disk_super,
  172. u64 devid, struct blk_desc *desc,
  173. struct disk_partition *part,
  174. struct btrfs_fs_devices **fs_devices_ret)
  175. {
  176. struct btrfs_device *device;
  177. struct btrfs_fs_devices *fs_devices;
  178. u64 found_transid = btrfs_super_generation(disk_super);
  179. bool metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
  180. BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
  181. if (metadata_uuid)
  182. fs_devices = find_fsid(disk_super->fsid,
  183. disk_super->metadata_uuid);
  184. else
  185. fs_devices = find_fsid(disk_super->fsid, NULL);
  186. if (!fs_devices) {
  187. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  188. if (!fs_devices)
  189. return -ENOMEM;
  190. INIT_LIST_HEAD(&fs_devices->devices);
  191. list_add(&fs_devices->list, &fs_uuids);
  192. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  193. if (metadata_uuid)
  194. memcpy(fs_devices->metadata_uuid,
  195. disk_super->metadata_uuid, BTRFS_FSID_SIZE);
  196. else
  197. memcpy(fs_devices->metadata_uuid, fs_devices->fsid,
  198. BTRFS_FSID_SIZE);
  199. fs_devices->latest_devid = devid;
  200. fs_devices->latest_trans = found_transid;
  201. fs_devices->lowest_devid = (u64)-1;
  202. device = NULL;
  203. } else {
  204. device = find_device(fs_devices, devid,
  205. disk_super->dev_item.uuid);
  206. }
  207. if (!device) {
  208. device = kzalloc(sizeof(*device), GFP_NOFS);
  209. if (!device) {
  210. /* we can safely leave the fs_devices entry around */
  211. return -ENOMEM;
  212. }
  213. device->devid = devid;
  214. device->desc = desc;
  215. device->part = part;
  216. device->generation = found_transid;
  217. memcpy(device->uuid, disk_super->dev_item.uuid,
  218. BTRFS_UUID_SIZE);
  219. device->total_devs = btrfs_super_num_devices(disk_super);
  220. device->super_bytes_used = btrfs_super_bytes_used(disk_super);
  221. device->total_bytes =
  222. btrfs_stack_device_total_bytes(&disk_super->dev_item);
  223. device->bytes_used =
  224. btrfs_stack_device_bytes_used(&disk_super->dev_item);
  225. list_add(&device->dev_list, &fs_devices->devices);
  226. device->fs_devices = fs_devices;
  227. } else if (!device->desc || !device->part) {
  228. /*
  229. * The existing device has newer generation, so this one could
  230. * be a stale one, don't add it.
  231. */
  232. if (found_transid < device->generation) {
  233. error(
  234. "adding devid %llu gen %llu but found an existing device gen %llu",
  235. device->devid, found_transid,
  236. device->generation);
  237. return -EEXIST;
  238. } else {
  239. device->desc = desc;
  240. device->part = part;
  241. }
  242. }
  243. if (found_transid > fs_devices->latest_trans) {
  244. fs_devices->latest_devid = devid;
  245. fs_devices->latest_trans = found_transid;
  246. }
  247. if (fs_devices->lowest_devid > devid) {
  248. fs_devices->lowest_devid = devid;
  249. }
  250. *fs_devices_ret = fs_devices;
  251. return 0;
  252. }
  253. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  254. {
  255. struct btrfs_fs_devices *seed_devices;
  256. struct btrfs_device *device;
  257. int ret = 0;
  258. again:
  259. if (!fs_devices)
  260. return 0;
  261. while (!list_empty(&fs_devices->devices)) {
  262. device = list_entry(fs_devices->devices.next,
  263. struct btrfs_device, dev_list);
  264. list_del(&device->dev_list);
  265. /* free the memory */
  266. free(device);
  267. }
  268. seed_devices = fs_devices->seed;
  269. fs_devices->seed = NULL;
  270. if (seed_devices) {
  271. struct btrfs_fs_devices *orig;
  272. orig = fs_devices;
  273. fs_devices = seed_devices;
  274. list_del(&orig->list);
  275. free(orig);
  276. goto again;
  277. } else {
  278. list_del(&fs_devices->list);
  279. free(fs_devices);
  280. }
  281. return ret;
  282. }
  283. void btrfs_close_all_devices(void)
  284. {
  285. struct btrfs_fs_devices *fs_devices;
  286. while (!list_empty(&fs_uuids)) {
  287. fs_devices = list_entry(fs_uuids.next, struct btrfs_fs_devices,
  288. list);
  289. btrfs_close_devices(fs_devices);
  290. }
  291. }
  292. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices)
  293. {
  294. struct btrfs_device *device;
  295. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  296. if (!device->desc || !device->part) {
  297. printf("no device found for devid %llu, skip it \n",
  298. device->devid);
  299. continue;
  300. }
  301. }
  302. return 0;
  303. }
  304. int btrfs_scan_one_device(struct blk_desc *desc, struct disk_partition *part,
  305. struct btrfs_fs_devices **fs_devices_ret,
  306. u64 *total_devs)
  307. {
  308. struct btrfs_super_block *disk_super;
  309. char buf[BTRFS_SUPER_INFO_SIZE];
  310. int ret;
  311. u64 devid;
  312. disk_super = (struct btrfs_super_block *)buf;
  313. ret = btrfs_read_dev_super(desc, part, disk_super);
  314. if (ret < 0)
  315. return -EIO;
  316. devid = btrfs_stack_device_id(&disk_super->dev_item);
  317. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
  318. *total_devs = 1;
  319. else
  320. *total_devs = btrfs_super_num_devices(disk_super);
  321. ret = device_list_add(disk_super, devid, desc, part, fs_devices_ret);
  322. return ret;
  323. }
  324. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  325. u8 *uuid, u8 *fsid)
  326. {
  327. struct btrfs_device *device;
  328. struct btrfs_fs_devices *cur_devices;
  329. cur_devices = fs_info->fs_devices;
  330. while (cur_devices) {
  331. if (!fsid ||
  332. !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
  333. device = find_device(cur_devices, devid, uuid);
  334. if (device)
  335. return device;
  336. }
  337. cur_devices = cur_devices->seed;
  338. }
  339. return NULL;
  340. }
  341. static struct btrfs_device *fill_missing_device(u64 devid)
  342. {
  343. struct btrfs_device *device;
  344. device = kzalloc(sizeof(*device), GFP_NOFS);
  345. return device;
  346. }
  347. /*
  348. * slot == -1: SYSTEM chunk
  349. * return -EIO on error, otherwise return 0
  350. */
  351. int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
  352. struct extent_buffer *leaf,
  353. struct btrfs_chunk *chunk,
  354. int slot, u64 logical)
  355. {
  356. u64 length;
  357. u64 stripe_len;
  358. u16 num_stripes;
  359. u16 sub_stripes;
  360. u64 type;
  361. u32 chunk_ondisk_size;
  362. u32 sectorsize = fs_info->sectorsize;
  363. /*
  364. * Basic chunk item size check. Note that btrfs_chunk already contains
  365. * one stripe, so no "==" check.
  366. */
  367. if (slot >= 0 &&
  368. btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk)) {
  369. error("invalid chunk item size, have %u expect [%zu, %zu)",
  370. btrfs_item_size_nr(leaf, slot),
  371. sizeof(struct btrfs_chunk),
  372. BTRFS_LEAF_DATA_SIZE(fs_info));
  373. return -EUCLEAN;
  374. }
  375. length = btrfs_chunk_length(leaf, chunk);
  376. stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  377. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  378. sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  379. type = btrfs_chunk_type(leaf, chunk);
  380. if (num_stripes == 0) {
  381. error("invalid num_stripes, have %u expect non-zero",
  382. num_stripes);
  383. return -EUCLEAN;
  384. }
  385. if (slot >= 0 && btrfs_chunk_item_size(num_stripes) !=
  386. btrfs_item_size_nr(leaf, slot)) {
  387. error("invalid chunk item size, have %u expect %lu",
  388. btrfs_item_size_nr(leaf, slot),
  389. btrfs_chunk_item_size(num_stripes));
  390. return -EUCLEAN;
  391. }
  392. /*
  393. * These valid checks may be insufficient to cover every corner cases.
  394. */
  395. if (!IS_ALIGNED(logical, sectorsize)) {
  396. error("invalid chunk logical %llu", logical);
  397. return -EIO;
  398. }
  399. if (btrfs_chunk_sector_size(leaf, chunk) != sectorsize) {
  400. error("invalid chunk sectorsize %llu",
  401. (unsigned long long)btrfs_chunk_sector_size(leaf, chunk));
  402. return -EIO;
  403. }
  404. if (!length || !IS_ALIGNED(length, sectorsize)) {
  405. error("invalid chunk length %llu", length);
  406. return -EIO;
  407. }
  408. if (stripe_len != BTRFS_STRIPE_LEN) {
  409. error("invalid chunk stripe length: %llu", stripe_len);
  410. return -EIO;
  411. }
  412. /* Check on chunk item type */
  413. if (slot == -1 && (type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
  414. error("invalid chunk type %llu", type);
  415. return -EIO;
  416. }
  417. if (type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
  418. BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
  419. error("unrecognized chunk type: %llu",
  420. ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
  421. BTRFS_BLOCK_GROUP_PROFILE_MASK) & type);
  422. return -EIO;
  423. }
  424. if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
  425. error("missing chunk type flag: %llu", type);
  426. return -EIO;
  427. }
  428. if (!(is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) ||
  429. (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0)) {
  430. error("conflicting chunk type detected: %llu", type);
  431. return -EIO;
  432. }
  433. if ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
  434. !is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
  435. error("conflicting chunk profile detected: %llu", type);
  436. return -EIO;
  437. }
  438. chunk_ondisk_size = btrfs_chunk_item_size(num_stripes);
  439. /*
  440. * Btrfs_chunk contains at least one stripe, and for sys_chunk
  441. * it can't exceed the system chunk array size
  442. * For normal chunk, it should match its chunk item size.
  443. */
  444. if (num_stripes < 1 ||
  445. (slot == -1 && chunk_ondisk_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) ||
  446. (slot >= 0 && chunk_ondisk_size > btrfs_item_size_nr(leaf, slot))) {
  447. error("invalid num_stripes: %u", num_stripes);
  448. return -EIO;
  449. }
  450. /*
  451. * Device number check against profile
  452. */
  453. if ((type & BTRFS_BLOCK_GROUP_RAID10 && (sub_stripes != 2 ||
  454. !IS_ALIGNED(num_stripes, sub_stripes))) ||
  455. (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
  456. (type & BTRFS_BLOCK_GROUP_RAID1C3 && num_stripes < 3) ||
  457. (type & BTRFS_BLOCK_GROUP_RAID1C4 && num_stripes < 4) ||
  458. (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
  459. (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
  460. (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
  461. ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
  462. num_stripes != 1)) {
  463. error("Invalid num_stripes:sub_stripes %u:%u for profile %llu",
  464. num_stripes, sub_stripes,
  465. type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
  466. return -EIO;
  467. }
  468. return 0;
  469. }
  470. /*
  471. * Slot is used to verify the chunk item is valid
  472. *
  473. * For sys chunk in superblock, pass -1 to indicate sys chunk.
  474. */
  475. static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
  476. struct extent_buffer *leaf,
  477. struct btrfs_chunk *chunk, int slot)
  478. {
  479. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  480. struct map_lookup *map;
  481. struct cache_extent *ce;
  482. u64 logical;
  483. u64 length;
  484. u64 devid;
  485. u8 uuid[BTRFS_UUID_SIZE];
  486. int num_stripes;
  487. int ret;
  488. int i;
  489. logical = key->offset;
  490. length = btrfs_chunk_length(leaf, chunk);
  491. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  492. /* Validation check */
  493. ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, slot, logical);
  494. if (ret) {
  495. error("%s checksums match, but it has an invalid chunk, %s",
  496. (slot == -1) ? "Superblock" : "Metadata",
  497. (slot == -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : "");
  498. return ret;
  499. }
  500. ce = search_cache_extent(&map_tree->cache_tree, logical);
  501. /* already mapped? */
  502. if (ce && ce->start <= logical && ce->start + ce->size > logical) {
  503. return 0;
  504. }
  505. map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
  506. if (!map)
  507. return -ENOMEM;
  508. map->ce.start = logical;
  509. map->ce.size = length;
  510. map->num_stripes = num_stripes;
  511. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  512. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  513. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  514. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  515. map->type = btrfs_chunk_type(leaf, chunk);
  516. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  517. for (i = 0; i < num_stripes; i++) {
  518. map->stripes[i].physical =
  519. btrfs_stripe_offset_nr(leaf, chunk, i);
  520. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  521. read_extent_buffer(leaf, uuid, (unsigned long)
  522. btrfs_stripe_dev_uuid_nr(chunk, i),
  523. BTRFS_UUID_SIZE);
  524. map->stripes[i].dev = btrfs_find_device(fs_info, devid, uuid,
  525. NULL);
  526. if (!map->stripes[i].dev) {
  527. map->stripes[i].dev = fill_missing_device(devid);
  528. printf("warning, device %llu is missing\n",
  529. (unsigned long long)devid);
  530. list_add(&map->stripes[i].dev->dev_list,
  531. &fs_info->fs_devices->devices);
  532. }
  533. }
  534. ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
  535. if (ret < 0) {
  536. errno = -ret;
  537. error("failed to add chunk map start=%llu len=%llu: %d (%m)",
  538. map->ce.start, map->ce.size, ret);
  539. }
  540. return ret;
  541. }
  542. static int fill_device_from_item(struct extent_buffer *leaf,
  543. struct btrfs_dev_item *dev_item,
  544. struct btrfs_device *device)
  545. {
  546. unsigned long ptr;
  547. device->devid = btrfs_device_id(leaf, dev_item);
  548. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  549. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  550. device->type = btrfs_device_type(leaf, dev_item);
  551. device->io_align = btrfs_device_io_align(leaf, dev_item);
  552. device->io_width = btrfs_device_io_width(leaf, dev_item);
  553. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  554. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  555. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  556. return 0;
  557. }
  558. static int read_one_dev(struct btrfs_fs_info *fs_info,
  559. struct extent_buffer *leaf,
  560. struct btrfs_dev_item *dev_item)
  561. {
  562. struct btrfs_device *device;
  563. u64 devid;
  564. int ret = 0;
  565. u8 fs_uuid[BTRFS_UUID_SIZE];
  566. u8 dev_uuid[BTRFS_UUID_SIZE];
  567. devid = btrfs_device_id(leaf, dev_item);
  568. read_extent_buffer(leaf, dev_uuid,
  569. (unsigned long)btrfs_device_uuid(dev_item),
  570. BTRFS_UUID_SIZE);
  571. read_extent_buffer(leaf, fs_uuid,
  572. (unsigned long)btrfs_device_fsid(dev_item),
  573. BTRFS_FSID_SIZE);
  574. if (memcmp(fs_uuid, fs_info->fs_devices->fsid, BTRFS_UUID_SIZE)) {
  575. error("Seed device is not yet supported\n");
  576. return -ENOTSUPP;
  577. }
  578. device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
  579. if (!device) {
  580. device = kzalloc(sizeof(*device), GFP_NOFS);
  581. if (!device)
  582. return -ENOMEM;
  583. list_add(&device->dev_list,
  584. &fs_info->fs_devices->devices);
  585. }
  586. fill_device_from_item(leaf, dev_item, device);
  587. fs_info->fs_devices->total_rw_bytes +=
  588. btrfs_device_total_bytes(leaf, dev_item);
  589. return ret;
  590. }
  591. int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
  592. {
  593. struct btrfs_super_block *super_copy = fs_info->super_copy;
  594. struct extent_buffer *sb;
  595. struct btrfs_disk_key *disk_key;
  596. struct btrfs_chunk *chunk;
  597. u8 *array_ptr;
  598. unsigned long sb_array_offset;
  599. int ret = 0;
  600. u32 num_stripes;
  601. u32 array_size;
  602. u32 len = 0;
  603. u32 cur_offset;
  604. struct btrfs_key key;
  605. if (fs_info->nodesize < BTRFS_SUPER_INFO_SIZE) {
  606. printf("ERROR: nodesize %u too small to read superblock\n",
  607. fs_info->nodesize);
  608. return -EINVAL;
  609. }
  610. sb = alloc_dummy_extent_buffer(fs_info, BTRFS_SUPER_INFO_OFFSET,
  611. BTRFS_SUPER_INFO_SIZE);
  612. if (!sb)
  613. return -ENOMEM;
  614. btrfs_set_buffer_uptodate(sb);
  615. write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
  616. array_size = btrfs_super_sys_array_size(super_copy);
  617. array_ptr = super_copy->sys_chunk_array;
  618. sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
  619. cur_offset = 0;
  620. while (cur_offset < array_size) {
  621. disk_key = (struct btrfs_disk_key *)array_ptr;
  622. len = sizeof(*disk_key);
  623. if (cur_offset + len > array_size)
  624. goto out_short_read;
  625. btrfs_disk_key_to_cpu(&key, disk_key);
  626. array_ptr += len;
  627. sb_array_offset += len;
  628. cur_offset += len;
  629. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  630. chunk = (struct btrfs_chunk *)sb_array_offset;
  631. /*
  632. * At least one btrfs_chunk with one stripe must be
  633. * present, exact stripe count check comes afterwards
  634. */
  635. len = btrfs_chunk_item_size(1);
  636. if (cur_offset + len > array_size)
  637. goto out_short_read;
  638. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  639. if (!num_stripes) {
  640. printk(
  641. "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
  642. num_stripes, cur_offset);
  643. ret = -EIO;
  644. break;
  645. }
  646. len = btrfs_chunk_item_size(num_stripes);
  647. if (cur_offset + len > array_size)
  648. goto out_short_read;
  649. ret = read_one_chunk(fs_info, &key, sb, chunk, -1);
  650. if (ret)
  651. break;
  652. } else {
  653. printk(
  654. "ERROR: unexpected item type %u in sys_array at offset %u\n",
  655. (u32)key.type, cur_offset);
  656. ret = -EIO;
  657. break;
  658. }
  659. array_ptr += len;
  660. sb_array_offset += len;
  661. cur_offset += len;
  662. }
  663. free_extent_buffer(sb);
  664. return ret;
  665. out_short_read:
  666. printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
  667. len, cur_offset);
  668. free_extent_buffer(sb);
  669. return -EIO;
  670. }
  671. int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
  672. {
  673. struct btrfs_path *path;
  674. struct extent_buffer *leaf;
  675. struct btrfs_key key;
  676. struct btrfs_key found_key;
  677. struct btrfs_root *root = fs_info->chunk_root;
  678. int ret;
  679. int slot;
  680. path = btrfs_alloc_path();
  681. if (!path)
  682. return -ENOMEM;
  683. /*
  684. * Read all device items, and then all the chunk items. All
  685. * device items are found before any chunk item (their object id
  686. * is smaller than the lowest possible object id for a chunk
  687. * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
  688. */
  689. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  690. key.offset = 0;
  691. key.type = 0;
  692. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  693. if (ret < 0)
  694. goto error;
  695. while(1) {
  696. leaf = path->nodes[0];
  697. slot = path->slots[0];
  698. if (slot >= btrfs_header_nritems(leaf)) {
  699. ret = btrfs_next_leaf(root, path);
  700. if (ret == 0)
  701. continue;
  702. if (ret < 0)
  703. goto error;
  704. break;
  705. }
  706. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  707. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  708. struct btrfs_dev_item *dev_item;
  709. dev_item = btrfs_item_ptr(leaf, slot,
  710. struct btrfs_dev_item);
  711. ret = read_one_dev(fs_info, leaf, dev_item);
  712. if (ret < 0)
  713. goto error;
  714. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  715. struct btrfs_chunk *chunk;
  716. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  717. ret = read_one_chunk(fs_info, &found_key, leaf, chunk,
  718. slot);
  719. if (ret < 0)
  720. goto error;
  721. }
  722. path->slots[0]++;
  723. }
  724. ret = 0;
  725. error:
  726. btrfs_free_path(path);
  727. return ret;
  728. }
  729. /*
  730. * Get stripe length from chunk item and its stripe items
  731. *
  732. * Caller should only call this function after validating the chunk item
  733. * by using btrfs_check_chunk_valid().
  734. */
  735. u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
  736. struct extent_buffer *leaf,
  737. struct btrfs_chunk *chunk)
  738. {
  739. u64 stripe_len;
  740. u64 chunk_len;
  741. u32 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  742. u64 profile = btrfs_chunk_type(leaf, chunk) &
  743. BTRFS_BLOCK_GROUP_PROFILE_MASK;
  744. chunk_len = btrfs_chunk_length(leaf, chunk);
  745. switch (profile) {
  746. case 0: /* Single profile */
  747. case BTRFS_BLOCK_GROUP_RAID1:
  748. case BTRFS_BLOCK_GROUP_RAID1C3:
  749. case BTRFS_BLOCK_GROUP_RAID1C4:
  750. case BTRFS_BLOCK_GROUP_DUP:
  751. stripe_len = chunk_len;
  752. break;
  753. case BTRFS_BLOCK_GROUP_RAID0:
  754. stripe_len = chunk_len / num_stripes;
  755. break;
  756. case BTRFS_BLOCK_GROUP_RAID5:
  757. stripe_len = chunk_len / (num_stripes - 1);
  758. break;
  759. case BTRFS_BLOCK_GROUP_RAID6:
  760. stripe_len = chunk_len / (num_stripes - 2);
  761. break;
  762. case BTRFS_BLOCK_GROUP_RAID10:
  763. stripe_len = chunk_len / (num_stripes /
  764. btrfs_chunk_sub_stripes(leaf, chunk));
  765. break;
  766. default:
  767. /* Invalid chunk profile found */
  768. BUG_ON(1);
  769. }
  770. return stripe_len;
  771. }
  772. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  773. {
  774. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  775. struct cache_extent *ce;
  776. struct map_lookup *map;
  777. int ret;
  778. ce = search_cache_extent(&map_tree->cache_tree, logical);
  779. if (!ce) {
  780. fprintf(stderr, "No mapping for %llu-%llu\n",
  781. (unsigned long long)logical,
  782. (unsigned long long)logical+len);
  783. return 1;
  784. }
  785. if (ce->start > logical || ce->start + ce->size < logical) {
  786. fprintf(stderr, "Invalid mapping for %llu-%llu, got "
  787. "%llu-%llu\n", (unsigned long long)logical,
  788. (unsigned long long)logical+len,
  789. (unsigned long long)ce->start,
  790. (unsigned long long)ce->start + ce->size);
  791. return 1;
  792. }
  793. map = container_of(ce, struct map_lookup, ce);
  794. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  795. BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4))
  796. ret = map->num_stripes;
  797. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  798. ret = map->sub_stripes;
  799. else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
  800. ret = 2;
  801. else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  802. ret = 3;
  803. else
  804. ret = 1;
  805. return ret;
  806. }
  807. int btrfs_next_bg(struct btrfs_fs_info *fs_info, u64 *logical,
  808. u64 *size, u64 type)
  809. {
  810. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  811. struct cache_extent *ce;
  812. struct map_lookup *map;
  813. u64 cur = *logical;
  814. ce = search_cache_extent(&map_tree->cache_tree, cur);
  815. while (ce) {
  816. /*
  817. * only jump to next bg if our cur is not 0
  818. * As the initial logical for btrfs_next_bg() is 0, and
  819. * if we jump to next bg, we skipped a valid bg.
  820. */
  821. if (cur) {
  822. ce = next_cache_extent(ce);
  823. if (!ce)
  824. return -ENOENT;
  825. }
  826. cur = ce->start;
  827. map = container_of(ce, struct map_lookup, ce);
  828. if (map->type & type) {
  829. *logical = ce->start;
  830. *size = ce->size;
  831. return 0;
  832. }
  833. if (!cur)
  834. ce = next_cache_extent(ce);
  835. }
  836. return -ENOENT;
  837. }
  838. static inline int parity_smaller(u64 a, u64 b)
  839. {
  840. return a > b;
  841. }
  842. /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
  843. static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
  844. {
  845. struct btrfs_bio_stripe s;
  846. int i;
  847. u64 l;
  848. int again = 1;
  849. while (again) {
  850. again = 0;
  851. for (i = 0; i < bbio->num_stripes - 1; i++) {
  852. if (parity_smaller(raid_map[i], raid_map[i+1])) {
  853. s = bbio->stripes[i];
  854. l = raid_map[i];
  855. bbio->stripes[i] = bbio->stripes[i+1];
  856. raid_map[i] = raid_map[i+1];
  857. bbio->stripes[i+1] = s;
  858. raid_map[i+1] = l;
  859. again = 1;
  860. }
  861. }
  862. }
  863. }
  864. int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  865. u64 logical, u64 *length, u64 *type,
  866. struct btrfs_multi_bio **multi_ret, int mirror_num,
  867. u64 **raid_map_ret)
  868. {
  869. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  870. struct cache_extent *ce;
  871. struct map_lookup *map;
  872. u64 offset;
  873. u64 stripe_offset;
  874. u64 *raid_map = NULL;
  875. int stripe_nr;
  876. int stripes_allocated = 8;
  877. int stripes_required = 1;
  878. int stripe_index;
  879. int i;
  880. struct btrfs_multi_bio *multi = NULL;
  881. if (multi_ret && rw == READ) {
  882. stripes_allocated = 1;
  883. }
  884. again:
  885. ce = search_cache_extent(&map_tree->cache_tree, logical);
  886. if (!ce) {
  887. kfree(multi);
  888. *length = (u64)-1;
  889. return -ENOENT;
  890. }
  891. if (ce->start > logical) {
  892. kfree(multi);
  893. *length = ce->start - logical;
  894. return -ENOENT;
  895. }
  896. if (multi_ret) {
  897. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  898. GFP_NOFS);
  899. if (!multi)
  900. return -ENOMEM;
  901. }
  902. map = container_of(ce, struct map_lookup, ce);
  903. offset = logical - ce->start;
  904. if (rw == WRITE) {
  905. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  906. BTRFS_BLOCK_GROUP_RAID1C3 |
  907. BTRFS_BLOCK_GROUP_RAID1C4 |
  908. BTRFS_BLOCK_GROUP_DUP)) {
  909. stripes_required = map->num_stripes;
  910. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  911. stripes_required = map->sub_stripes;
  912. }
  913. }
  914. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
  915. && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
  916. /* RAID[56] write or recovery. Return all stripes */
  917. stripes_required = map->num_stripes;
  918. /* Only allocate the map if we've already got a large enough multi_ret */
  919. if (stripes_allocated >= stripes_required) {
  920. raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  921. if (!raid_map) {
  922. kfree(multi);
  923. return -ENOMEM;
  924. }
  925. }
  926. }
  927. /* if our multi bio struct is too small, back off and try again */
  928. if (multi_ret && stripes_allocated < stripes_required) {
  929. stripes_allocated = stripes_required;
  930. kfree(multi);
  931. multi = NULL;
  932. goto again;
  933. }
  934. stripe_nr = offset;
  935. /*
  936. * stripe_nr counts the total number of stripes we have to stride
  937. * to get to this block
  938. */
  939. stripe_nr = stripe_nr / map->stripe_len;
  940. stripe_offset = stripe_nr * (u64)map->stripe_len;
  941. BUG_ON(offset < stripe_offset);
  942. /* stripe_offset is the offset of this block in its stripe*/
  943. stripe_offset = offset - stripe_offset;
  944. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  945. BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4 |
  946. BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
  947. BTRFS_BLOCK_GROUP_RAID10 |
  948. BTRFS_BLOCK_GROUP_DUP)) {
  949. /* we limit the length of each bio to what fits in a stripe */
  950. *length = min_t(u64, ce->size - offset,
  951. map->stripe_len - stripe_offset);
  952. } else {
  953. *length = ce->size - offset;
  954. }
  955. if (!multi_ret)
  956. goto out;
  957. multi->num_stripes = 1;
  958. stripe_index = 0;
  959. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  960. BTRFS_BLOCK_GROUP_RAID1C3 |
  961. BTRFS_BLOCK_GROUP_RAID1C4)) {
  962. if (rw == WRITE)
  963. multi->num_stripes = map->num_stripes;
  964. else if (mirror_num)
  965. stripe_index = mirror_num - 1;
  966. else
  967. stripe_index = stripe_nr % map->num_stripes;
  968. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  969. int factor = map->num_stripes / map->sub_stripes;
  970. stripe_index = stripe_nr % factor;
  971. stripe_index *= map->sub_stripes;
  972. if (rw == WRITE)
  973. multi->num_stripes = map->sub_stripes;
  974. else if (mirror_num)
  975. stripe_index += mirror_num - 1;
  976. stripe_nr = stripe_nr / factor;
  977. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  978. if (rw == WRITE)
  979. multi->num_stripes = map->num_stripes;
  980. else if (mirror_num)
  981. stripe_index = mirror_num - 1;
  982. } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  983. BTRFS_BLOCK_GROUP_RAID6)) {
  984. if (raid_map) {
  985. int rot;
  986. u64 tmp;
  987. u64 raid56_full_stripe_start;
  988. u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
  989. /*
  990. * align the start of our data stripe in the logical
  991. * address space
  992. */
  993. raid56_full_stripe_start = offset / full_stripe_len;
  994. raid56_full_stripe_start *= full_stripe_len;
  995. /* get the data stripe number */
  996. stripe_nr = raid56_full_stripe_start / map->stripe_len;
  997. stripe_nr = stripe_nr / nr_data_stripes(map);
  998. /* Work out the disk rotation on this stripe-set */
  999. rot = stripe_nr % map->num_stripes;
  1000. /* Fill in the logical address of each stripe */
  1001. tmp = (u64)stripe_nr * nr_data_stripes(map);
  1002. for (i = 0; i < nr_data_stripes(map); i++)
  1003. raid_map[(i+rot) % map->num_stripes] =
  1004. ce->start + (tmp + i) * map->stripe_len;
  1005. raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
  1006. if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  1007. raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
  1008. *length = map->stripe_len;
  1009. stripe_index = 0;
  1010. stripe_offset = 0;
  1011. multi->num_stripes = map->num_stripes;
  1012. } else {
  1013. stripe_index = stripe_nr % nr_data_stripes(map);
  1014. stripe_nr = stripe_nr / nr_data_stripes(map);
  1015. /*
  1016. * Mirror #0 or #1 means the original data block.
  1017. * Mirror #2 is RAID5 parity block.
  1018. * Mirror #3 is RAID6 Q block.
  1019. */
  1020. if (mirror_num > 1)
  1021. stripe_index = nr_data_stripes(map) + mirror_num - 2;
  1022. /* We distribute the parity blocks across stripes */
  1023. stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
  1024. }
  1025. } else {
  1026. /*
  1027. * after this do_div call, stripe_nr is the number of stripes
  1028. * on this device we have to walk to find the data, and
  1029. * stripe_index is the number of our device in the stripe array
  1030. */
  1031. stripe_index = stripe_nr % map->num_stripes;
  1032. stripe_nr = stripe_nr / map->num_stripes;
  1033. }
  1034. BUG_ON(stripe_index >= map->num_stripes);
  1035. for (i = 0; i < multi->num_stripes; i++) {
  1036. multi->stripes[i].physical =
  1037. map->stripes[stripe_index].physical + stripe_offset +
  1038. stripe_nr * map->stripe_len;
  1039. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  1040. stripe_index++;
  1041. }
  1042. *multi_ret = multi;
  1043. if (type)
  1044. *type = map->type;
  1045. if (raid_map) {
  1046. sort_parity_stripes(multi, raid_map);
  1047. *raid_map_ret = raid_map;
  1048. }
  1049. out:
  1050. return 0;
  1051. }
  1052. int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  1053. u64 logical, u64 *length,
  1054. struct btrfs_multi_bio **multi_ret, int mirror_num,
  1055. u64 **raid_map_ret)
  1056. {
  1057. return __btrfs_map_block(fs_info, rw, logical, length, NULL,
  1058. multi_ret, mirror_num, raid_map_ret);
  1059. }