free-space-tree.c 40 KB

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