free-space-tree.c 43 KB

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