free-space-tree.c 41 KB

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