free-space-tree.c 42 KB

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