free-space-tree.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Facebook. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/sched/mm.h>
  7. #include "ctree.h"
  8. #include "disk-io.h"
  9. #include "locking.h"
  10. #include "free-space-tree.h"
  11. #include "transaction.h"
  12. static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
  13. struct btrfs_block_group_cache *block_group,
  14. struct btrfs_path *path);
  15. void set_free_space_tree_thresholds(struct btrfs_block_group_cache *cache)
  16. {
  17. u32 bitmap_range;
  18. size_t bitmap_size;
  19. u64 num_bitmaps, total_bitmap_size;
  20. /*
  21. * We convert to bitmaps when the disk space required for using extents
  22. * exceeds that required for using bitmaps.
  23. */
  24. bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
  25. num_bitmaps = div_u64(cache->key.offset + bitmap_range - 1,
  26. bitmap_range);
  27. bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
  28. total_bitmap_size = num_bitmaps * bitmap_size;
  29. cache->bitmap_high_thresh = div_u64(total_bitmap_size,
  30. sizeof(struct btrfs_item));
  31. /*
  32. * We allow for a small buffer between the high threshold and low
  33. * threshold to avoid thrashing back and forth between the two formats.
  34. */
  35. if (cache->bitmap_high_thresh > 100)
  36. cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
  37. else
  38. cache->bitmap_low_thresh = 0;
  39. }
  40. static int add_new_free_space_info(struct btrfs_trans_handle *trans,
  41. struct btrfs_block_group_cache *block_group,
  42. struct btrfs_path *path)
  43. {
  44. struct btrfs_root *root = trans->fs_info->free_space_root;
  45. struct btrfs_free_space_info *info;
  46. struct btrfs_key key;
  47. struct extent_buffer *leaf;
  48. int ret;
  49. key.objectid = block_group->key.objectid;
  50. key.type = BTRFS_FREE_SPACE_INFO_KEY;
  51. key.offset = block_group->key.offset;
  52. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
  53. if (ret)
  54. goto out;
  55. leaf = path->nodes[0];
  56. info = btrfs_item_ptr(leaf, path->slots[0],
  57. struct btrfs_free_space_info);
  58. btrfs_set_free_space_extent_count(leaf, info, 0);
  59. btrfs_set_free_space_flags(leaf, info, 0);
  60. btrfs_mark_buffer_dirty(leaf);
  61. ret = 0;
  62. out:
  63. btrfs_release_path(path);
  64. return ret;
  65. }
  66. struct btrfs_free_space_info *
  67. search_free_space_info(struct btrfs_trans_handle *trans,
  68. struct btrfs_fs_info *fs_info,
  69. struct btrfs_block_group_cache *block_group,
  70. struct btrfs_path *path, int cow)
  71. {
  72. struct btrfs_root *root = fs_info->free_space_root;
  73. struct btrfs_key key;
  74. int ret;
  75. key.objectid = block_group->key.objectid;
  76. key.type = BTRFS_FREE_SPACE_INFO_KEY;
  77. key.offset = block_group->key.offset;
  78. ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
  79. if (ret < 0)
  80. return ERR_PTR(ret);
  81. if (ret != 0) {
  82. btrfs_warn(fs_info, "missing free space info for %llu",
  83. block_group->key.objectid);
  84. ASSERT(0);
  85. return ERR_PTR(-ENOENT);
  86. }
  87. return btrfs_item_ptr(path->nodes[0], path->slots[0],
  88. struct btrfs_free_space_info);
  89. }
  90. /*
  91. * btrfs_search_slot() but we're looking for the greatest key less than the
  92. * passed key.
  93. */
  94. static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
  95. struct btrfs_root *root,
  96. struct btrfs_key *key, struct btrfs_path *p,
  97. int ins_len, int cow)
  98. {
  99. int ret;
  100. ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
  101. if (ret < 0)
  102. return ret;
  103. if (ret == 0) {
  104. ASSERT(0);
  105. return -EIO;
  106. }
  107. if (p->slots[0] == 0) {
  108. ASSERT(0);
  109. return -EIO;
  110. }
  111. p->slots[0]--;
  112. return 0;
  113. }
  114. static inline u32 free_space_bitmap_size(u64 size, u32 sectorsize)
  115. {
  116. return DIV_ROUND_UP((u32)div_u64(size, sectorsize), BITS_PER_BYTE);
  117. }
  118. static unsigned long *alloc_bitmap(u32 bitmap_size)
  119. {
  120. unsigned long *ret;
  121. unsigned int nofs_flag;
  122. u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
  123. /*
  124. * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
  125. * into the filesystem as the free space bitmap can be modified in the
  126. * critical section of a transaction commit.
  127. *
  128. * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
  129. * know that recursion is unsafe.
  130. */
  131. nofs_flag = memalloc_nofs_save();
  132. ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
  133. memalloc_nofs_restore(nofs_flag);
  134. return ret;
  135. }
  136. static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
  137. {
  138. u8 *p = ((u8 *)map) + BIT_BYTE(start);
  139. const unsigned int size = start + len;
  140. int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  141. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
  142. while (len - bits_to_set >= 0) {
  143. *p |= mask_to_set;
  144. len -= bits_to_set;
  145. bits_to_set = BITS_PER_BYTE;
  146. mask_to_set = ~0;
  147. p++;
  148. }
  149. if (len) {
  150. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  151. *p |= mask_to_set;
  152. }
  153. }
  154. int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
  155. struct btrfs_block_group_cache *block_group,
  156. struct btrfs_path *path)
  157. {
  158. struct btrfs_fs_info *fs_info = trans->fs_info;
  159. struct btrfs_root *root = fs_info->free_space_root;
  160. struct btrfs_free_space_info *info;
  161. struct btrfs_key key, found_key;
  162. struct extent_buffer *leaf;
  163. unsigned long *bitmap;
  164. char *bitmap_cursor;
  165. u64 start, end;
  166. u64 bitmap_range, i;
  167. u32 bitmap_size, flags, expected_extent_count;
  168. u32 extent_count = 0;
  169. int done = 0, nr;
  170. int ret;
  171. bitmap_size = free_space_bitmap_size(block_group->key.offset,
  172. fs_info->sectorsize);
  173. bitmap = alloc_bitmap(bitmap_size);
  174. if (!bitmap) {
  175. ret = -ENOMEM;
  176. goto out;
  177. }
  178. start = block_group->key.objectid;
  179. end = block_group->key.objectid + block_group->key.offset;
  180. key.objectid = end - 1;
  181. key.type = (u8)-1;
  182. key.offset = (u64)-1;
  183. while (!done) {
  184. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  185. if (ret)
  186. goto out;
  187. leaf = path->nodes[0];
  188. nr = 0;
  189. path->slots[0]++;
  190. while (path->slots[0] > 0) {
  191. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
  192. if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
  193. ASSERT(found_key.objectid == block_group->key.objectid);
  194. ASSERT(found_key.offset == block_group->key.offset);
  195. done = 1;
  196. break;
  197. } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
  198. u64 first, last;
  199. ASSERT(found_key.objectid >= start);
  200. ASSERT(found_key.objectid < end);
  201. ASSERT(found_key.objectid + found_key.offset <= end);
  202. first = div_u64(found_key.objectid - start,
  203. fs_info->sectorsize);
  204. last = div_u64(found_key.objectid + found_key.offset - start,
  205. fs_info->sectorsize);
  206. le_bitmap_set(bitmap, first, last - first);
  207. extent_count++;
  208. nr++;
  209. path->slots[0]--;
  210. } else {
  211. ASSERT(0);
  212. }
  213. }
  214. ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
  215. if (ret)
  216. goto out;
  217. btrfs_release_path(path);
  218. }
  219. info = search_free_space_info(trans, fs_info, block_group, path, 1);
  220. if (IS_ERR(info)) {
  221. ret = PTR_ERR(info);
  222. goto out;
  223. }
  224. leaf = path->nodes[0];
  225. flags = btrfs_free_space_flags(leaf, info);
  226. flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
  227. btrfs_set_free_space_flags(leaf, info, flags);
  228. expected_extent_count = btrfs_free_space_extent_count(leaf, info);
  229. btrfs_mark_buffer_dirty(leaf);
  230. btrfs_release_path(path);
  231. if (extent_count != expected_extent_count) {
  232. btrfs_err(fs_info,
  233. "incorrect extent count for %llu; counted %u, expected %u",
  234. block_group->key.objectid, extent_count,
  235. expected_extent_count);
  236. ASSERT(0);
  237. ret = -EIO;
  238. goto out;
  239. }
  240. bitmap_cursor = (char *)bitmap;
  241. bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
  242. i = start;
  243. while (i < end) {
  244. unsigned long ptr;
  245. u64 extent_size;
  246. u32 data_size;
  247. extent_size = min(end - i, bitmap_range);
  248. data_size = free_space_bitmap_size(extent_size,
  249. fs_info->sectorsize);
  250. key.objectid = i;
  251. key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
  252. key.offset = extent_size;
  253. ret = btrfs_insert_empty_item(trans, root, path, &key,
  254. data_size);
  255. if (ret)
  256. goto out;
  257. leaf = path->nodes[0];
  258. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  259. write_extent_buffer(leaf, bitmap_cursor, ptr,
  260. data_size);
  261. btrfs_mark_buffer_dirty(leaf);
  262. btrfs_release_path(path);
  263. i += extent_size;
  264. bitmap_cursor += data_size;
  265. }
  266. ret = 0;
  267. out:
  268. kvfree(bitmap);
  269. if (ret)
  270. btrfs_abort_transaction(trans, ret);
  271. return ret;
  272. }
  273. int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
  274. struct btrfs_block_group_cache *block_group,
  275. struct btrfs_path *path)
  276. {
  277. struct btrfs_fs_info *fs_info = trans->fs_info;
  278. struct btrfs_root *root = fs_info->free_space_root;
  279. struct btrfs_free_space_info *info;
  280. struct btrfs_key key, found_key;
  281. struct extent_buffer *leaf;
  282. unsigned long *bitmap;
  283. u64 start, end;
  284. u32 bitmap_size, flags, expected_extent_count;
  285. unsigned long nrbits, start_bit, end_bit;
  286. u32 extent_count = 0;
  287. int done = 0, nr;
  288. int ret;
  289. bitmap_size = free_space_bitmap_size(block_group->key.offset,
  290. fs_info->sectorsize);
  291. bitmap = alloc_bitmap(bitmap_size);
  292. if (!bitmap) {
  293. ret = -ENOMEM;
  294. goto out;
  295. }
  296. start = block_group->key.objectid;
  297. end = block_group->key.objectid + block_group->key.offset;
  298. key.objectid = end - 1;
  299. key.type = (u8)-1;
  300. key.offset = (u64)-1;
  301. while (!done) {
  302. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  303. if (ret)
  304. goto out;
  305. leaf = path->nodes[0];
  306. nr = 0;
  307. path->slots[0]++;
  308. while (path->slots[0] > 0) {
  309. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
  310. if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
  311. ASSERT(found_key.objectid == block_group->key.objectid);
  312. ASSERT(found_key.offset == block_group->key.offset);
  313. done = 1;
  314. break;
  315. } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
  316. unsigned long ptr;
  317. char *bitmap_cursor;
  318. u32 bitmap_pos, data_size;
  319. ASSERT(found_key.objectid >= start);
  320. ASSERT(found_key.objectid < end);
  321. ASSERT(found_key.objectid + found_key.offset <= end);
  322. bitmap_pos = div_u64(found_key.objectid - start,
  323. fs_info->sectorsize *
  324. BITS_PER_BYTE);
  325. bitmap_cursor = ((char *)bitmap) + bitmap_pos;
  326. data_size = free_space_bitmap_size(found_key.offset,
  327. fs_info->sectorsize);
  328. ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
  329. read_extent_buffer(leaf, bitmap_cursor, ptr,
  330. data_size);
  331. nr++;
  332. path->slots[0]--;
  333. } else {
  334. ASSERT(0);
  335. }
  336. }
  337. ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
  338. if (ret)
  339. goto out;
  340. btrfs_release_path(path);
  341. }
  342. info = search_free_space_info(trans, fs_info, block_group, path, 1);
  343. if (IS_ERR(info)) {
  344. ret = PTR_ERR(info);
  345. goto out;
  346. }
  347. leaf = path->nodes[0];
  348. flags = btrfs_free_space_flags(leaf, info);
  349. flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
  350. btrfs_set_free_space_flags(leaf, info, flags);
  351. expected_extent_count = btrfs_free_space_extent_count(leaf, info);
  352. btrfs_mark_buffer_dirty(leaf);
  353. btrfs_release_path(path);
  354. nrbits = div_u64(block_group->key.offset, block_group->fs_info->sectorsize);
  355. start_bit = find_next_bit_le(bitmap, nrbits, 0);
  356. while (start_bit < nrbits) {
  357. end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
  358. ASSERT(start_bit < end_bit);
  359. key.objectid = start + start_bit * block_group->fs_info->sectorsize;
  360. key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
  361. key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
  362. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  363. if (ret)
  364. goto out;
  365. btrfs_release_path(path);
  366. extent_count++;
  367. start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
  368. }
  369. if (extent_count != expected_extent_count) {
  370. btrfs_err(fs_info,
  371. "incorrect extent count for %llu; counted %u, expected %u",
  372. block_group->key.objectid, extent_count,
  373. expected_extent_count);
  374. ASSERT(0);
  375. ret = -EIO;
  376. goto out;
  377. }
  378. ret = 0;
  379. out:
  380. kvfree(bitmap);
  381. if (ret)
  382. btrfs_abort_transaction(trans, ret);
  383. return ret;
  384. }
  385. static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
  386. struct btrfs_block_group_cache *block_group,
  387. struct btrfs_path *path,
  388. int new_extents)
  389. {
  390. struct btrfs_free_space_info *info;
  391. u32 flags;
  392. u32 extent_count;
  393. int ret = 0;
  394. if (new_extents == 0)
  395. return 0;
  396. info = search_free_space_info(trans, trans->fs_info, block_group, path,
  397. 1);
  398. if (IS_ERR(info)) {
  399. ret = PTR_ERR(info);
  400. goto out;
  401. }
  402. flags = btrfs_free_space_flags(path->nodes[0], info);
  403. extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
  404. extent_count += new_extents;
  405. btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
  406. btrfs_mark_buffer_dirty(path->nodes[0]);
  407. btrfs_release_path(path);
  408. if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
  409. extent_count > block_group->bitmap_high_thresh) {
  410. ret = convert_free_space_to_bitmaps(trans, block_group, path);
  411. } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
  412. extent_count < block_group->bitmap_low_thresh) {
  413. ret = convert_free_space_to_extents(trans, block_group, path);
  414. }
  415. out:
  416. return ret;
  417. }
  418. int free_space_test_bit(struct btrfs_block_group_cache *block_group,
  419. struct btrfs_path *path, u64 offset)
  420. {
  421. struct extent_buffer *leaf;
  422. struct btrfs_key key;
  423. u64 found_start, found_end;
  424. unsigned long ptr, i;
  425. leaf = path->nodes[0];
  426. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  427. ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
  428. found_start = key.objectid;
  429. found_end = key.objectid + key.offset;
  430. ASSERT(offset >= found_start && offset < found_end);
  431. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  432. i = div_u64(offset - found_start,
  433. block_group->fs_info->sectorsize);
  434. return !!extent_buffer_test_bit(leaf, ptr, i);
  435. }
  436. static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
  437. struct btrfs_path *path, u64 *start, u64 *size,
  438. int bit)
  439. {
  440. struct btrfs_fs_info *fs_info = block_group->fs_info;
  441. struct extent_buffer *leaf;
  442. struct btrfs_key key;
  443. u64 end = *start + *size;
  444. u64 found_start, found_end;
  445. unsigned long ptr, first, last;
  446. leaf = path->nodes[0];
  447. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  448. ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
  449. found_start = key.objectid;
  450. found_end = key.objectid + key.offset;
  451. ASSERT(*start >= found_start && *start < found_end);
  452. ASSERT(end > found_start);
  453. if (end > found_end)
  454. end = found_end;
  455. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  456. first = div_u64(*start - found_start, fs_info->sectorsize);
  457. last = div_u64(end - found_start, fs_info->sectorsize);
  458. if (bit)
  459. extent_buffer_bitmap_set(leaf, ptr, first, last - first);
  460. else
  461. extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
  462. btrfs_mark_buffer_dirty(leaf);
  463. *size -= end - *start;
  464. *start = end;
  465. }
  466. /*
  467. * We can't use btrfs_next_item() in modify_free_space_bitmap() because
  468. * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
  469. * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
  470. * looking for.
  471. */
  472. static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
  473. struct btrfs_root *root, struct btrfs_path *p)
  474. {
  475. struct btrfs_key key;
  476. if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
  477. p->slots[0]++;
  478. return 0;
  479. }
  480. btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
  481. btrfs_release_path(p);
  482. key.objectid += key.offset;
  483. key.type = (u8)-1;
  484. key.offset = (u64)-1;
  485. return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
  486. }
  487. /*
  488. * If remove is 1, then we are removing free space, thus clearing bits in the
  489. * bitmap. If remove is 0, then we are adding free space, thus setting bits in
  490. * the bitmap.
  491. */
  492. static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
  493. struct btrfs_block_group_cache *block_group,
  494. struct btrfs_path *path,
  495. u64 start, u64 size, int remove)
  496. {
  497. struct btrfs_root *root = block_group->fs_info->free_space_root;
  498. struct btrfs_key key;
  499. u64 end = start + size;
  500. u64 cur_start, cur_size;
  501. int prev_bit, next_bit;
  502. int new_extents;
  503. int ret;
  504. /*
  505. * Read the bit for the block immediately before the extent of space if
  506. * that block is within the block group.
  507. */
  508. if (start > block_group->key.objectid) {
  509. u64 prev_block = start - block_group->fs_info->sectorsize;
  510. key.objectid = prev_block;
  511. key.type = (u8)-1;
  512. key.offset = (u64)-1;
  513. ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
  514. if (ret)
  515. goto out;
  516. prev_bit = free_space_test_bit(block_group, path, prev_block);
  517. /* The previous block may have been in the previous bitmap. */
  518. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  519. if (start >= key.objectid + key.offset) {
  520. ret = free_space_next_bitmap(trans, root, path);
  521. if (ret)
  522. goto out;
  523. }
  524. } else {
  525. key.objectid = start;
  526. key.type = (u8)-1;
  527. key.offset = (u64)-1;
  528. ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
  529. if (ret)
  530. goto out;
  531. prev_bit = -1;
  532. }
  533. /*
  534. * Iterate over all of the bitmaps overlapped by the extent of space,
  535. * clearing/setting bits as required.
  536. */
  537. cur_start = start;
  538. cur_size = size;
  539. while (1) {
  540. free_space_set_bits(block_group, path, &cur_start, &cur_size,
  541. !remove);
  542. if (cur_size == 0)
  543. break;
  544. ret = free_space_next_bitmap(trans, root, path);
  545. if (ret)
  546. goto out;
  547. }
  548. /*
  549. * Read the bit for the block immediately after the extent of space if
  550. * that block is within the block group.
  551. */
  552. if (end < block_group->key.objectid + block_group->key.offset) {
  553. /* The next block may be in the next bitmap. */
  554. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  555. if (end >= key.objectid + key.offset) {
  556. ret = free_space_next_bitmap(trans, root, path);
  557. if (ret)
  558. goto out;
  559. }
  560. next_bit = free_space_test_bit(block_group, path, end);
  561. } else {
  562. next_bit = -1;
  563. }
  564. if (remove) {
  565. new_extents = -1;
  566. if (prev_bit == 1) {
  567. /* Leftover on the left. */
  568. new_extents++;
  569. }
  570. if (next_bit == 1) {
  571. /* Leftover on the right. */
  572. new_extents++;
  573. }
  574. } else {
  575. new_extents = 1;
  576. if (prev_bit == 1) {
  577. /* Merging with neighbor on the left. */
  578. new_extents--;
  579. }
  580. if (next_bit == 1) {
  581. /* Merging with neighbor on the right. */
  582. new_extents--;
  583. }
  584. }
  585. btrfs_release_path(path);
  586. ret = update_free_space_extent_count(trans, block_group, path,
  587. new_extents);
  588. out:
  589. return ret;
  590. }
  591. static int remove_free_space_extent(struct btrfs_trans_handle *trans,
  592. struct btrfs_block_group_cache *block_group,
  593. struct btrfs_path *path,
  594. u64 start, u64 size)
  595. {
  596. struct btrfs_root *root = trans->fs_info->free_space_root;
  597. struct btrfs_key key;
  598. u64 found_start, found_end;
  599. u64 end = start + size;
  600. int new_extents = -1;
  601. int ret;
  602. key.objectid = start;
  603. key.type = (u8)-1;
  604. key.offset = (u64)-1;
  605. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  606. if (ret)
  607. goto out;
  608. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  609. ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
  610. found_start = key.objectid;
  611. found_end = key.objectid + key.offset;
  612. ASSERT(start >= found_start && end <= found_end);
  613. /*
  614. * Okay, now that we've found the free space extent which contains the
  615. * free space that we are removing, there are four cases:
  616. *
  617. * 1. We're using the whole extent: delete the key we found and
  618. * decrement the free space extent count.
  619. * 2. We are using part of the extent starting at the beginning: delete
  620. * the key we found and insert a new key representing the leftover at
  621. * the end. There is no net change in the number of extents.
  622. * 3. We are using part of the extent ending at the end: delete the key
  623. * we found and insert a new key representing the leftover at the
  624. * beginning. There is no net change in the number of extents.
  625. * 4. We are using part of the extent in the middle: delete the key we
  626. * found and insert two new keys representing the leftovers on each
  627. * side. Where we used to have one extent, we now have two, so increment
  628. * the extent count. We may need to convert the block group to bitmaps
  629. * as a result.
  630. */
  631. /* Delete the existing key (cases 1-4). */
  632. ret = btrfs_del_item(trans, root, path);
  633. if (ret)
  634. goto out;
  635. /* Add a key for leftovers at the beginning (cases 3 and 4). */
  636. if (start > found_start) {
  637. key.objectid = found_start;
  638. key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
  639. key.offset = start - found_start;
  640. btrfs_release_path(path);
  641. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  642. if (ret)
  643. goto out;
  644. new_extents++;
  645. }
  646. /* Add a key for leftovers at the end (cases 2 and 4). */
  647. if (end < found_end) {
  648. key.objectid = end;
  649. key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
  650. key.offset = found_end - end;
  651. btrfs_release_path(path);
  652. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  653. if (ret)
  654. goto out;
  655. new_extents++;
  656. }
  657. btrfs_release_path(path);
  658. ret = update_free_space_extent_count(trans, block_group, path,
  659. new_extents);
  660. out:
  661. return ret;
  662. }
  663. int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
  664. struct btrfs_block_group_cache *block_group,
  665. struct btrfs_path *path, u64 start, u64 size)
  666. {
  667. struct btrfs_free_space_info *info;
  668. u32 flags;
  669. int ret;
  670. if (block_group->needs_free_space) {
  671. ret = __add_block_group_free_space(trans, block_group, path);
  672. if (ret)
  673. return ret;
  674. }
  675. info = search_free_space_info(NULL, trans->fs_info, block_group, path,
  676. 0);
  677. if (IS_ERR(info))
  678. return PTR_ERR(info);
  679. flags = btrfs_free_space_flags(path->nodes[0], info);
  680. btrfs_release_path(path);
  681. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
  682. return modify_free_space_bitmap(trans, block_group, path,
  683. start, size, 1);
  684. } else {
  685. return remove_free_space_extent(trans, block_group, path,
  686. start, size);
  687. }
  688. }
  689. int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
  690. u64 start, u64 size)
  691. {
  692. struct btrfs_block_group_cache *block_group;
  693. struct btrfs_path *path;
  694. int ret;
  695. if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
  696. return 0;
  697. path = btrfs_alloc_path();
  698. if (!path) {
  699. ret = -ENOMEM;
  700. goto out;
  701. }
  702. block_group = btrfs_lookup_block_group(trans->fs_info, start);
  703. if (!block_group) {
  704. ASSERT(0);
  705. ret = -ENOENT;
  706. goto out;
  707. }
  708. mutex_lock(&block_group->free_space_lock);
  709. ret = __remove_from_free_space_tree(trans, block_group, path, start,
  710. size);
  711. mutex_unlock(&block_group->free_space_lock);
  712. btrfs_put_block_group(block_group);
  713. out:
  714. btrfs_free_path(path);
  715. if (ret)
  716. btrfs_abort_transaction(trans, ret);
  717. return ret;
  718. }
  719. static int add_free_space_extent(struct btrfs_trans_handle *trans,
  720. struct btrfs_block_group_cache *block_group,
  721. struct btrfs_path *path,
  722. u64 start, u64 size)
  723. {
  724. struct btrfs_root *root = trans->fs_info->free_space_root;
  725. struct btrfs_key key, new_key;
  726. u64 found_start, found_end;
  727. u64 end = start + size;
  728. int new_extents = 1;
  729. int ret;
  730. /*
  731. * We are adding a new extent of free space, but we need to merge
  732. * extents. There are four cases here:
  733. *
  734. * 1. The new extent does not have any immediate neighbors to merge
  735. * with: add the new key and increment the free space extent count. We
  736. * may need to convert the block group to bitmaps as a result.
  737. * 2. The new extent has an immediate neighbor before it: remove the
  738. * previous key and insert a new key combining both of them. There is no
  739. * net change in the number of extents.
  740. * 3. The new extent has an immediate neighbor after it: remove the next
  741. * key and insert a new key combining both of them. There is no net
  742. * change in the number of extents.
  743. * 4. The new extent has immediate neighbors on both sides: remove both
  744. * of the keys and insert a new key combining all of them. Where we used
  745. * to have two extents, we now have one, so decrement the extent count.
  746. */
  747. new_key.objectid = start;
  748. new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
  749. new_key.offset = size;
  750. /* Search for a neighbor on the left. */
  751. if (start == block_group->key.objectid)
  752. goto right;
  753. key.objectid = start - 1;
  754. key.type = (u8)-1;
  755. key.offset = (u64)-1;
  756. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  757. if (ret)
  758. goto out;
  759. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  760. if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
  761. ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
  762. btrfs_release_path(path);
  763. goto right;
  764. }
  765. found_start = key.objectid;
  766. found_end = key.objectid + key.offset;
  767. ASSERT(found_start >= block_group->key.objectid &&
  768. found_end > block_group->key.objectid);
  769. ASSERT(found_start < start && found_end <= start);
  770. /*
  771. * Delete the neighbor on the left and absorb it into the new key (cases
  772. * 2 and 4).
  773. */
  774. if (found_end == start) {
  775. ret = btrfs_del_item(trans, root, path);
  776. if (ret)
  777. goto out;
  778. new_key.objectid = found_start;
  779. new_key.offset += key.offset;
  780. new_extents--;
  781. }
  782. btrfs_release_path(path);
  783. right:
  784. /* Search for a neighbor on the right. */
  785. if (end == block_group->key.objectid + block_group->key.offset)
  786. goto insert;
  787. key.objectid = end;
  788. key.type = (u8)-1;
  789. key.offset = (u64)-1;
  790. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  791. if (ret)
  792. goto out;
  793. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  794. if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
  795. ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
  796. btrfs_release_path(path);
  797. goto insert;
  798. }
  799. found_start = key.objectid;
  800. found_end = key.objectid + key.offset;
  801. ASSERT(found_start >= block_group->key.objectid &&
  802. found_end > block_group->key.objectid);
  803. ASSERT((found_start < start && found_end <= start) ||
  804. (found_start >= end && found_end > end));
  805. /*
  806. * Delete the neighbor on the right and absorb it into the new key
  807. * (cases 3 and 4).
  808. */
  809. if (found_start == end) {
  810. ret = btrfs_del_item(trans, root, path);
  811. if (ret)
  812. goto out;
  813. new_key.offset += key.offset;
  814. new_extents--;
  815. }
  816. btrfs_release_path(path);
  817. insert:
  818. /* Insert the new key (cases 1-4). */
  819. ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
  820. if (ret)
  821. goto out;
  822. btrfs_release_path(path);
  823. ret = update_free_space_extent_count(trans, block_group, path,
  824. new_extents);
  825. out:
  826. return ret;
  827. }
  828. int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
  829. struct btrfs_block_group_cache *block_group,
  830. struct btrfs_path *path, u64 start, u64 size)
  831. {
  832. struct btrfs_fs_info *fs_info = trans->fs_info;
  833. struct btrfs_free_space_info *info;
  834. u32 flags;
  835. int ret;
  836. if (block_group->needs_free_space) {
  837. ret = __add_block_group_free_space(trans, block_group, path);
  838. if (ret)
  839. return ret;
  840. }
  841. info = search_free_space_info(NULL, fs_info, block_group, path, 0);
  842. if (IS_ERR(info))
  843. return PTR_ERR(info);
  844. flags = btrfs_free_space_flags(path->nodes[0], info);
  845. btrfs_release_path(path);
  846. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
  847. return modify_free_space_bitmap(trans, block_group, path,
  848. start, size, 0);
  849. } else {
  850. return add_free_space_extent(trans, block_group, path, start,
  851. size);
  852. }
  853. }
  854. int add_to_free_space_tree(struct btrfs_trans_handle *trans,
  855. u64 start, u64 size)
  856. {
  857. struct btrfs_block_group_cache *block_group;
  858. struct btrfs_path *path;
  859. int ret;
  860. if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
  861. return 0;
  862. path = btrfs_alloc_path();
  863. if (!path) {
  864. ret = -ENOMEM;
  865. goto out;
  866. }
  867. block_group = btrfs_lookup_block_group(trans->fs_info, start);
  868. if (!block_group) {
  869. ASSERT(0);
  870. ret = -ENOENT;
  871. goto out;
  872. }
  873. mutex_lock(&block_group->free_space_lock);
  874. ret = __add_to_free_space_tree(trans, block_group, path, start, size);
  875. mutex_unlock(&block_group->free_space_lock);
  876. btrfs_put_block_group(block_group);
  877. out:
  878. btrfs_free_path(path);
  879. if (ret)
  880. btrfs_abort_transaction(trans, ret);
  881. return ret;
  882. }
  883. /*
  884. * Populate the free space tree by walking the extent tree. Operations on the
  885. * extent tree that happen as a result of writes to the free space tree will go
  886. * through the normal add/remove hooks.
  887. */
  888. static int populate_free_space_tree(struct btrfs_trans_handle *trans,
  889. struct btrfs_block_group_cache *block_group)
  890. {
  891. struct btrfs_root *extent_root = trans->fs_info->extent_root;
  892. struct btrfs_path *path, *path2;
  893. struct btrfs_key key;
  894. u64 start, end;
  895. int ret;
  896. path = btrfs_alloc_path();
  897. if (!path)
  898. return -ENOMEM;
  899. path->reada = READA_FORWARD;
  900. path2 = btrfs_alloc_path();
  901. if (!path2) {
  902. btrfs_free_path(path);
  903. return -ENOMEM;
  904. }
  905. ret = add_new_free_space_info(trans, block_group, path2);
  906. if (ret)
  907. goto out;
  908. mutex_lock(&block_group->free_space_lock);
  909. /*
  910. * Iterate through all of the extent and metadata items in this block
  911. * group, adding the free space between them and the free space at the
  912. * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
  913. * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
  914. * contained in.
  915. */
  916. key.objectid = block_group->key.objectid;
  917. key.type = BTRFS_EXTENT_ITEM_KEY;
  918. key.offset = 0;
  919. ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
  920. if (ret < 0)
  921. goto out_locked;
  922. ASSERT(ret == 0);
  923. start = block_group->key.objectid;
  924. end = block_group->key.objectid + block_group->key.offset;
  925. while (1) {
  926. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  927. if (key.type == BTRFS_EXTENT_ITEM_KEY ||
  928. key.type == BTRFS_METADATA_ITEM_KEY) {
  929. if (key.objectid >= end)
  930. break;
  931. if (start < key.objectid) {
  932. ret = __add_to_free_space_tree(trans,
  933. block_group,
  934. path2, start,
  935. key.objectid -
  936. start);
  937. if (ret)
  938. goto out_locked;
  939. }
  940. start = key.objectid;
  941. if (key.type == BTRFS_METADATA_ITEM_KEY)
  942. start += trans->fs_info->nodesize;
  943. else
  944. start += key.offset;
  945. } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  946. if (key.objectid != block_group->key.objectid)
  947. break;
  948. }
  949. ret = btrfs_next_item(extent_root, path);
  950. if (ret < 0)
  951. goto out_locked;
  952. if (ret)
  953. break;
  954. }
  955. if (start < end) {
  956. ret = __add_to_free_space_tree(trans, block_group, path2,
  957. start, end - start);
  958. if (ret)
  959. goto out_locked;
  960. }
  961. ret = 0;
  962. out_locked:
  963. mutex_unlock(&block_group->free_space_lock);
  964. out:
  965. btrfs_free_path(path2);
  966. btrfs_free_path(path);
  967. return ret;
  968. }
  969. int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
  970. {
  971. struct btrfs_trans_handle *trans;
  972. struct btrfs_root *tree_root = fs_info->tree_root;
  973. struct btrfs_root *free_space_root;
  974. struct btrfs_block_group_cache *block_group;
  975. struct rb_node *node;
  976. int ret;
  977. trans = btrfs_start_transaction(tree_root, 0);
  978. if (IS_ERR(trans))
  979. return PTR_ERR(trans);
  980. set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
  981. free_space_root = btrfs_create_tree(trans, fs_info,
  982. BTRFS_FREE_SPACE_TREE_OBJECTID);
  983. if (IS_ERR(free_space_root)) {
  984. ret = PTR_ERR(free_space_root);
  985. goto abort;
  986. }
  987. fs_info->free_space_root = free_space_root;
  988. node = rb_first(&fs_info->block_group_cache_tree);
  989. while (node) {
  990. block_group = rb_entry(node, struct btrfs_block_group_cache,
  991. cache_node);
  992. ret = populate_free_space_tree(trans, block_group);
  993. if (ret)
  994. goto abort;
  995. node = rb_next(node);
  996. }
  997. btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
  998. btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
  999. clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
  1000. return btrfs_commit_transaction(trans);
  1001. abort:
  1002. clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
  1003. btrfs_abort_transaction(trans, ret);
  1004. btrfs_end_transaction(trans);
  1005. return ret;
  1006. }
  1007. static int clear_free_space_tree(struct btrfs_trans_handle *trans,
  1008. struct btrfs_root *root)
  1009. {
  1010. struct btrfs_path *path;
  1011. struct btrfs_key key;
  1012. int nr;
  1013. int ret;
  1014. path = btrfs_alloc_path();
  1015. if (!path)
  1016. return -ENOMEM;
  1017. path->leave_spinning = 1;
  1018. key.objectid = 0;
  1019. key.type = 0;
  1020. key.offset = 0;
  1021. while (1) {
  1022. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1023. if (ret < 0)
  1024. goto out;
  1025. nr = btrfs_header_nritems(path->nodes[0]);
  1026. if (!nr)
  1027. break;
  1028. path->slots[0] = 0;
  1029. ret = btrfs_del_items(trans, root, path, 0, nr);
  1030. if (ret)
  1031. goto out;
  1032. btrfs_release_path(path);
  1033. }
  1034. ret = 0;
  1035. out:
  1036. btrfs_free_path(path);
  1037. return ret;
  1038. }
  1039. int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
  1040. {
  1041. struct btrfs_trans_handle *trans;
  1042. struct btrfs_root *tree_root = fs_info->tree_root;
  1043. struct btrfs_root *free_space_root = fs_info->free_space_root;
  1044. int ret;
  1045. trans = btrfs_start_transaction(tree_root, 0);
  1046. if (IS_ERR(trans))
  1047. return PTR_ERR(trans);
  1048. btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
  1049. btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
  1050. fs_info->free_space_root = NULL;
  1051. ret = clear_free_space_tree(trans, free_space_root);
  1052. if (ret)
  1053. goto abort;
  1054. ret = btrfs_del_root(trans, &free_space_root->root_key);
  1055. if (ret)
  1056. goto abort;
  1057. list_del(&free_space_root->dirty_list);
  1058. btrfs_tree_lock(free_space_root->node);
  1059. clean_tree_block(fs_info, free_space_root->node);
  1060. btrfs_tree_unlock(free_space_root->node);
  1061. btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
  1062. 0, 1);
  1063. free_extent_buffer(free_space_root->node);
  1064. free_extent_buffer(free_space_root->commit_root);
  1065. kfree(free_space_root);
  1066. return btrfs_commit_transaction(trans);
  1067. abort:
  1068. btrfs_abort_transaction(trans, ret);
  1069. btrfs_end_transaction(trans);
  1070. return ret;
  1071. }
  1072. static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
  1073. struct btrfs_block_group_cache *block_group,
  1074. struct btrfs_path *path)
  1075. {
  1076. int ret;
  1077. block_group->needs_free_space = 0;
  1078. ret = add_new_free_space_info(trans, block_group, path);
  1079. if (ret)
  1080. return ret;
  1081. return __add_to_free_space_tree(trans, block_group, path,
  1082. block_group->key.objectid,
  1083. block_group->key.offset);
  1084. }
  1085. int add_block_group_free_space(struct btrfs_trans_handle *trans,
  1086. struct btrfs_block_group_cache *block_group)
  1087. {
  1088. struct btrfs_fs_info *fs_info = trans->fs_info;
  1089. struct btrfs_path *path = NULL;
  1090. int ret = 0;
  1091. if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
  1092. return 0;
  1093. mutex_lock(&block_group->free_space_lock);
  1094. if (!block_group->needs_free_space)
  1095. goto out;
  1096. path = btrfs_alloc_path();
  1097. if (!path) {
  1098. ret = -ENOMEM;
  1099. goto out;
  1100. }
  1101. ret = __add_block_group_free_space(trans, block_group, path);
  1102. out:
  1103. btrfs_free_path(path);
  1104. mutex_unlock(&block_group->free_space_lock);
  1105. if (ret)
  1106. btrfs_abort_transaction(trans, ret);
  1107. return ret;
  1108. }
  1109. int remove_block_group_free_space(struct btrfs_trans_handle *trans,
  1110. struct btrfs_block_group_cache *block_group)
  1111. {
  1112. struct btrfs_root *root = trans->fs_info->free_space_root;
  1113. struct btrfs_path *path;
  1114. struct btrfs_key key, found_key;
  1115. struct extent_buffer *leaf;
  1116. u64 start, end;
  1117. int done = 0, nr;
  1118. int ret;
  1119. if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
  1120. return 0;
  1121. if (block_group->needs_free_space) {
  1122. /* We never added this block group to the free space tree. */
  1123. return 0;
  1124. }
  1125. path = btrfs_alloc_path();
  1126. if (!path) {
  1127. ret = -ENOMEM;
  1128. goto out;
  1129. }
  1130. start = block_group->key.objectid;
  1131. end = block_group->key.objectid + block_group->key.offset;
  1132. key.objectid = end - 1;
  1133. key.type = (u8)-1;
  1134. key.offset = (u64)-1;
  1135. while (!done) {
  1136. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  1137. if (ret)
  1138. goto out;
  1139. leaf = path->nodes[0];
  1140. nr = 0;
  1141. path->slots[0]++;
  1142. while (path->slots[0] > 0) {
  1143. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
  1144. if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
  1145. ASSERT(found_key.objectid == block_group->key.objectid);
  1146. ASSERT(found_key.offset == block_group->key.offset);
  1147. done = 1;
  1148. nr++;
  1149. path->slots[0]--;
  1150. break;
  1151. } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
  1152. found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
  1153. ASSERT(found_key.objectid >= start);
  1154. ASSERT(found_key.objectid < end);
  1155. ASSERT(found_key.objectid + found_key.offset <= end);
  1156. nr++;
  1157. path->slots[0]--;
  1158. } else {
  1159. ASSERT(0);
  1160. }
  1161. }
  1162. ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
  1163. if (ret)
  1164. goto out;
  1165. btrfs_release_path(path);
  1166. }
  1167. ret = 0;
  1168. out:
  1169. btrfs_free_path(path);
  1170. if (ret)
  1171. btrfs_abort_transaction(trans, ret);
  1172. return ret;
  1173. }
  1174. static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
  1175. struct btrfs_path *path,
  1176. u32 expected_extent_count)
  1177. {
  1178. struct btrfs_block_group_cache *block_group;
  1179. struct btrfs_fs_info *fs_info;
  1180. struct btrfs_root *root;
  1181. struct btrfs_key key;
  1182. int prev_bit = 0, bit;
  1183. /* Initialize to silence GCC. */
  1184. u64 extent_start = 0;
  1185. u64 end, offset;
  1186. u64 total_found = 0;
  1187. u32 extent_count = 0;
  1188. int ret;
  1189. block_group = caching_ctl->block_group;
  1190. fs_info = block_group->fs_info;
  1191. root = fs_info->free_space_root;
  1192. end = block_group->key.objectid + block_group->key.offset;
  1193. while (1) {
  1194. ret = btrfs_next_item(root, path);
  1195. if (ret < 0)
  1196. goto out;
  1197. if (ret)
  1198. break;
  1199. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1200. if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
  1201. break;
  1202. ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
  1203. ASSERT(key.objectid < end && key.objectid + key.offset <= end);
  1204. caching_ctl->progress = key.objectid;
  1205. offset = key.objectid;
  1206. while (offset < key.objectid + key.offset) {
  1207. bit = free_space_test_bit(block_group, path, offset);
  1208. if (prev_bit == 0 && bit == 1) {
  1209. extent_start = offset;
  1210. } else if (prev_bit == 1 && bit == 0) {
  1211. total_found += add_new_free_space(block_group,
  1212. extent_start,
  1213. offset);
  1214. if (total_found > CACHING_CTL_WAKE_UP) {
  1215. total_found = 0;
  1216. wake_up(&caching_ctl->wait);
  1217. }
  1218. extent_count++;
  1219. }
  1220. prev_bit = bit;
  1221. offset += fs_info->sectorsize;
  1222. }
  1223. }
  1224. if (prev_bit == 1) {
  1225. total_found += add_new_free_space(block_group, extent_start,
  1226. end);
  1227. extent_count++;
  1228. }
  1229. if (extent_count != expected_extent_count) {
  1230. btrfs_err(fs_info,
  1231. "incorrect extent count for %llu; counted %u, expected %u",
  1232. block_group->key.objectid, extent_count,
  1233. expected_extent_count);
  1234. ASSERT(0);
  1235. ret = -EIO;
  1236. goto out;
  1237. }
  1238. caching_ctl->progress = (u64)-1;
  1239. ret = 0;
  1240. out:
  1241. return ret;
  1242. }
  1243. static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
  1244. struct btrfs_path *path,
  1245. u32 expected_extent_count)
  1246. {
  1247. struct btrfs_block_group_cache *block_group;
  1248. struct btrfs_fs_info *fs_info;
  1249. struct btrfs_root *root;
  1250. struct btrfs_key key;
  1251. u64 end;
  1252. u64 total_found = 0;
  1253. u32 extent_count = 0;
  1254. int ret;
  1255. block_group = caching_ctl->block_group;
  1256. fs_info = block_group->fs_info;
  1257. root = fs_info->free_space_root;
  1258. end = block_group->key.objectid + block_group->key.offset;
  1259. while (1) {
  1260. ret = btrfs_next_item(root, path);
  1261. if (ret < 0)
  1262. goto out;
  1263. if (ret)
  1264. break;
  1265. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1266. if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
  1267. break;
  1268. ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
  1269. ASSERT(key.objectid < end && key.objectid + key.offset <= end);
  1270. caching_ctl->progress = key.objectid;
  1271. total_found += add_new_free_space(block_group, key.objectid,
  1272. key.objectid + key.offset);
  1273. if (total_found > CACHING_CTL_WAKE_UP) {
  1274. total_found = 0;
  1275. wake_up(&caching_ctl->wait);
  1276. }
  1277. extent_count++;
  1278. }
  1279. if (extent_count != expected_extent_count) {
  1280. btrfs_err(fs_info,
  1281. "incorrect extent count for %llu; counted %u, expected %u",
  1282. block_group->key.objectid, extent_count,
  1283. expected_extent_count);
  1284. ASSERT(0);
  1285. ret = -EIO;
  1286. goto out;
  1287. }
  1288. caching_ctl->progress = (u64)-1;
  1289. ret = 0;
  1290. out:
  1291. return ret;
  1292. }
  1293. int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
  1294. {
  1295. struct btrfs_block_group_cache *block_group;
  1296. struct btrfs_fs_info *fs_info;
  1297. struct btrfs_free_space_info *info;
  1298. struct btrfs_path *path;
  1299. u32 extent_count, flags;
  1300. int ret;
  1301. block_group = caching_ctl->block_group;
  1302. fs_info = block_group->fs_info;
  1303. path = btrfs_alloc_path();
  1304. if (!path)
  1305. return -ENOMEM;
  1306. /*
  1307. * Just like caching_thread() doesn't want to deadlock on the extent
  1308. * tree, we don't want to deadlock on the free space tree.
  1309. */
  1310. path->skip_locking = 1;
  1311. path->search_commit_root = 1;
  1312. path->reada = READA_FORWARD;
  1313. info = search_free_space_info(NULL, fs_info, block_group, path, 0);
  1314. if (IS_ERR(info)) {
  1315. ret = PTR_ERR(info);
  1316. goto out;
  1317. }
  1318. extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
  1319. flags = btrfs_free_space_flags(path->nodes[0], info);
  1320. /*
  1321. * We left path pointing to the free space info item, so now
  1322. * load_free_space_foo can just iterate through the free space tree from
  1323. * there.
  1324. */
  1325. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
  1326. ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
  1327. else
  1328. ret = load_free_space_extents(caching_ctl, path, extent_count);
  1329. out:
  1330. btrfs_free_path(path);
  1331. return ret;
  1332. }