free-space-tree.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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\n",
  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 unsigned long *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. unsigned long *bitmap;
  159. char *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. block_group->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. block_group->sectorsize);
  199. last = div_u64(found_key.objectid + found_key.offset - start,
  200. block_group->sectorsize);
  201. 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, "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 = (char *)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, root, 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. unsigned long *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. char *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 = ((char *)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 = !!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, "incorrect extent count for %llu; counted %u, expected %u",
  383. block_group->key.objectid, extent_count,
  384. expected_extent_count);
  385. ASSERT(0);
  386. ret = -EIO;
  387. goto out;
  388. }
  389. ret = 0;
  390. out:
  391. kvfree(bitmap);
  392. if (ret)
  393. btrfs_abort_transaction(trans, root, ret);
  394. return ret;
  395. }
  396. static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
  397. struct btrfs_fs_info *fs_info,
  398. struct btrfs_block_group_cache *block_group,
  399. struct btrfs_path *path,
  400. int new_extents)
  401. {
  402. struct btrfs_free_space_info *info;
  403. u32 flags;
  404. u32 extent_count;
  405. int ret = 0;
  406. if (new_extents == 0)
  407. return 0;
  408. info = search_free_space_info(trans, fs_info, block_group, path, 1);
  409. if (IS_ERR(info)) {
  410. ret = PTR_ERR(info);
  411. goto out;
  412. }
  413. flags = btrfs_free_space_flags(path->nodes[0], info);
  414. extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
  415. extent_count += new_extents;
  416. btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
  417. btrfs_mark_buffer_dirty(path->nodes[0]);
  418. btrfs_release_path(path);
  419. if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
  420. extent_count > block_group->bitmap_high_thresh) {
  421. ret = convert_free_space_to_bitmaps(trans, fs_info, block_group,
  422. path);
  423. } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
  424. extent_count < block_group->bitmap_low_thresh) {
  425. ret = convert_free_space_to_extents(trans, fs_info, block_group,
  426. path);
  427. }
  428. out:
  429. return ret;
  430. }
  431. int free_space_test_bit(struct btrfs_block_group_cache *block_group,
  432. struct btrfs_path *path, u64 offset)
  433. {
  434. struct extent_buffer *leaf;
  435. struct btrfs_key key;
  436. u64 found_start, found_end;
  437. unsigned long ptr, i;
  438. leaf = path->nodes[0];
  439. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  440. ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
  441. found_start = key.objectid;
  442. found_end = key.objectid + key.offset;
  443. ASSERT(offset >= found_start && offset < found_end);
  444. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  445. i = div_u64(offset - found_start, block_group->sectorsize);
  446. return !!extent_buffer_test_bit(leaf, ptr, i);
  447. }
  448. static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
  449. struct btrfs_path *path, u64 *start, u64 *size,
  450. int bit)
  451. {
  452. struct extent_buffer *leaf;
  453. struct btrfs_key key;
  454. u64 end = *start + *size;
  455. u64 found_start, found_end;
  456. unsigned long ptr, first, last;
  457. leaf = path->nodes[0];
  458. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  459. ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
  460. found_start = key.objectid;
  461. found_end = key.objectid + key.offset;
  462. ASSERT(*start >= found_start && *start < found_end);
  463. ASSERT(end > found_start);
  464. if (end > found_end)
  465. end = found_end;
  466. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  467. first = div_u64(*start - found_start, block_group->sectorsize);
  468. last = div_u64(end - found_start, block_group->sectorsize);
  469. if (bit)
  470. extent_buffer_bitmap_set(leaf, ptr, first, last - first);
  471. else
  472. extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
  473. btrfs_mark_buffer_dirty(leaf);
  474. *size -= end - *start;
  475. *start = end;
  476. }
  477. /*
  478. * We can't use btrfs_next_item() in modify_free_space_bitmap() because
  479. * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
  480. * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
  481. * looking for.
  482. */
  483. static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
  484. struct btrfs_root *root, struct btrfs_path *p)
  485. {
  486. struct btrfs_key key;
  487. if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
  488. p->slots[0]++;
  489. return 0;
  490. }
  491. btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
  492. btrfs_release_path(p);
  493. key.objectid += key.offset;
  494. key.type = (u8)-1;
  495. key.offset = (u64)-1;
  496. return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
  497. }
  498. /*
  499. * If remove is 1, then we are removing free space, thus clearing bits in the
  500. * bitmap. If remove is 0, then we are adding free space, thus setting bits in
  501. * the bitmap.
  502. */
  503. static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
  504. struct btrfs_fs_info *fs_info,
  505. struct btrfs_block_group_cache *block_group,
  506. struct btrfs_path *path,
  507. u64 start, u64 size, int remove)
  508. {
  509. struct btrfs_root *root = fs_info->free_space_root;
  510. struct btrfs_key key;
  511. u64 end = start + size;
  512. u64 cur_start, cur_size;
  513. int prev_bit, next_bit;
  514. int new_extents;
  515. int ret;
  516. /*
  517. * Read the bit for the block immediately before the extent of space if
  518. * that block is within the block group.
  519. */
  520. if (start > block_group->key.objectid) {
  521. u64 prev_block = start - block_group->sectorsize;
  522. key.objectid = prev_block;
  523. key.type = (u8)-1;
  524. key.offset = (u64)-1;
  525. ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
  526. if (ret)
  527. goto out;
  528. prev_bit = free_space_test_bit(block_group, path, prev_block);
  529. /* The previous block may have been in the previous bitmap. */
  530. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  531. if (start >= key.objectid + key.offset) {
  532. ret = free_space_next_bitmap(trans, root, path);
  533. if (ret)
  534. goto out;
  535. }
  536. } else {
  537. key.objectid = start;
  538. key.type = (u8)-1;
  539. key.offset = (u64)-1;
  540. ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
  541. if (ret)
  542. goto out;
  543. prev_bit = -1;
  544. }
  545. /*
  546. * Iterate over all of the bitmaps overlapped by the extent of space,
  547. * clearing/setting bits as required.
  548. */
  549. cur_start = start;
  550. cur_size = size;
  551. while (1) {
  552. free_space_set_bits(block_group, path, &cur_start, &cur_size,
  553. !remove);
  554. if (cur_size == 0)
  555. break;
  556. ret = free_space_next_bitmap(trans, root, path);
  557. if (ret)
  558. goto out;
  559. }
  560. /*
  561. * Read the bit for the block immediately after the extent of space if
  562. * that block is within the block group.
  563. */
  564. if (end < block_group->key.objectid + block_group->key.offset) {
  565. /* The next block may be in the next bitmap. */
  566. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  567. if (end >= key.objectid + key.offset) {
  568. ret = free_space_next_bitmap(trans, root, path);
  569. if (ret)
  570. goto out;
  571. }
  572. next_bit = free_space_test_bit(block_group, path, end);
  573. } else {
  574. next_bit = -1;
  575. }
  576. if (remove) {
  577. new_extents = -1;
  578. if (prev_bit == 1) {
  579. /* Leftover on the left. */
  580. new_extents++;
  581. }
  582. if (next_bit == 1) {
  583. /* Leftover on the right. */
  584. new_extents++;
  585. }
  586. } else {
  587. new_extents = 1;
  588. if (prev_bit == 1) {
  589. /* Merging with neighbor on the left. */
  590. new_extents--;
  591. }
  592. if (next_bit == 1) {
  593. /* Merging with neighbor on the right. */
  594. new_extents--;
  595. }
  596. }
  597. btrfs_release_path(path);
  598. ret = update_free_space_extent_count(trans, fs_info, block_group, path,
  599. new_extents);
  600. out:
  601. return ret;
  602. }
  603. static int remove_free_space_extent(struct btrfs_trans_handle *trans,
  604. struct btrfs_fs_info *fs_info,
  605. struct btrfs_block_group_cache *block_group,
  606. struct btrfs_path *path,
  607. u64 start, u64 size)
  608. {
  609. struct btrfs_root *root = fs_info->free_space_root;
  610. struct btrfs_key key;
  611. u64 found_start, found_end;
  612. u64 end = start + size;
  613. int new_extents = -1;
  614. int ret;
  615. key.objectid = start;
  616. key.type = (u8)-1;
  617. key.offset = (u64)-1;
  618. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  619. if (ret)
  620. goto out;
  621. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  622. ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
  623. found_start = key.objectid;
  624. found_end = key.objectid + key.offset;
  625. ASSERT(start >= found_start && end <= found_end);
  626. /*
  627. * Okay, now that we've found the free space extent which contains the
  628. * free space that we are removing, there are four cases:
  629. *
  630. * 1. We're using the whole extent: delete the key we found and
  631. * decrement the free space extent count.
  632. * 2. We are using part of the extent starting at the beginning: delete
  633. * the key we found and insert a new key representing the leftover at
  634. * the end. There is no net change in the number of extents.
  635. * 3. We are using part of the extent ending at the end: delete the key
  636. * we found and insert a new key representing the leftover at the
  637. * beginning. There is no net change in the number of extents.
  638. * 4. We are using part of the extent in the middle: delete the key we
  639. * found and insert two new keys representing the leftovers on each
  640. * side. Where we used to have one extent, we now have two, so increment
  641. * the extent count. We may need to convert the block group to bitmaps
  642. * as a result.
  643. */
  644. /* Delete the existing key (cases 1-4). */
  645. ret = btrfs_del_item(trans, root, path);
  646. if (ret)
  647. goto out;
  648. /* Add a key for leftovers at the beginning (cases 3 and 4). */
  649. if (start > found_start) {
  650. key.objectid = found_start;
  651. key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
  652. key.offset = start - found_start;
  653. btrfs_release_path(path);
  654. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  655. if (ret)
  656. goto out;
  657. new_extents++;
  658. }
  659. /* Add a key for leftovers at the end (cases 2 and 4). */
  660. if (end < found_end) {
  661. key.objectid = end;
  662. key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
  663. key.offset = found_end - end;
  664. btrfs_release_path(path);
  665. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  666. if (ret)
  667. goto out;
  668. new_extents++;
  669. }
  670. btrfs_release_path(path);
  671. ret = update_free_space_extent_count(trans, fs_info, block_group, path,
  672. new_extents);
  673. out:
  674. return ret;
  675. }
  676. int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
  677. struct btrfs_fs_info *fs_info,
  678. struct btrfs_block_group_cache *block_group,
  679. struct btrfs_path *path, u64 start, u64 size)
  680. {
  681. struct btrfs_free_space_info *info;
  682. u32 flags;
  683. int ret;
  684. if (block_group->needs_free_space) {
  685. ret = __add_block_group_free_space(trans, fs_info, block_group,
  686. path);
  687. if (ret)
  688. return ret;
  689. }
  690. info = search_free_space_info(NULL, fs_info, block_group, path, 0);
  691. if (IS_ERR(info))
  692. return PTR_ERR(info);
  693. flags = btrfs_free_space_flags(path->nodes[0], info);
  694. btrfs_release_path(path);
  695. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
  696. return modify_free_space_bitmap(trans, fs_info, block_group,
  697. path, start, size, 1);
  698. } else {
  699. return remove_free_space_extent(trans, fs_info, block_group,
  700. path, start, size);
  701. }
  702. }
  703. int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
  704. struct btrfs_fs_info *fs_info,
  705. u64 start, u64 size)
  706. {
  707. struct btrfs_block_group_cache *block_group;
  708. struct btrfs_path *path;
  709. int ret;
  710. if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
  711. return 0;
  712. path = btrfs_alloc_path();
  713. if (!path) {
  714. ret = -ENOMEM;
  715. goto out;
  716. }
  717. block_group = btrfs_lookup_block_group(fs_info, start);
  718. if (!block_group) {
  719. ASSERT(0);
  720. ret = -ENOENT;
  721. goto out;
  722. }
  723. mutex_lock(&block_group->free_space_lock);
  724. ret = __remove_from_free_space_tree(trans, fs_info, block_group, path,
  725. start, size);
  726. mutex_unlock(&block_group->free_space_lock);
  727. btrfs_put_block_group(block_group);
  728. out:
  729. btrfs_free_path(path);
  730. if (ret)
  731. btrfs_abort_transaction(trans, fs_info->free_space_root, ret);
  732. return ret;
  733. }
  734. static int add_free_space_extent(struct btrfs_trans_handle *trans,
  735. struct btrfs_fs_info *fs_info,
  736. struct btrfs_block_group_cache *block_group,
  737. struct btrfs_path *path,
  738. u64 start, u64 size)
  739. {
  740. struct btrfs_root *root = fs_info->free_space_root;
  741. struct btrfs_key key, new_key;
  742. u64 found_start, found_end;
  743. u64 end = start + size;
  744. int new_extents = 1;
  745. int ret;
  746. /*
  747. * We are adding a new extent of free space, but we need to merge
  748. * extents. There are four cases here:
  749. *
  750. * 1. The new extent does not have any immediate neighbors to merge
  751. * with: add the new key and increment the free space extent count. We
  752. * may need to convert the block group to bitmaps as a result.
  753. * 2. The new extent has an immediate neighbor before it: remove the
  754. * previous key and insert a new key combining both of them. There is no
  755. * net change in the number of extents.
  756. * 3. The new extent has an immediate neighbor after it: remove the next
  757. * key and insert a new key combining both of them. There is no net
  758. * change in the number of extents.
  759. * 4. The new extent has immediate neighbors on both sides: remove both
  760. * of the keys and insert a new key combining all of them. Where we used
  761. * to have two extents, we now have one, so decrement the extent count.
  762. */
  763. new_key.objectid = start;
  764. new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
  765. new_key.offset = size;
  766. /* Search for a neighbor on the left. */
  767. if (start == block_group->key.objectid)
  768. goto right;
  769. key.objectid = start - 1;
  770. key.type = (u8)-1;
  771. key.offset = (u64)-1;
  772. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  773. if (ret)
  774. goto out;
  775. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  776. if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
  777. ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
  778. btrfs_release_path(path);
  779. goto right;
  780. }
  781. found_start = key.objectid;
  782. found_end = key.objectid + key.offset;
  783. ASSERT(found_start >= block_group->key.objectid &&
  784. found_end > block_group->key.objectid);
  785. ASSERT(found_start < start && found_end <= start);
  786. /*
  787. * Delete the neighbor on the left and absorb it into the new key (cases
  788. * 2 and 4).
  789. */
  790. if (found_end == start) {
  791. ret = btrfs_del_item(trans, root, path);
  792. if (ret)
  793. goto out;
  794. new_key.objectid = found_start;
  795. new_key.offset += key.offset;
  796. new_extents--;
  797. }
  798. btrfs_release_path(path);
  799. right:
  800. /* Search for a neighbor on the right. */
  801. if (end == block_group->key.objectid + block_group->key.offset)
  802. goto insert;
  803. key.objectid = end;
  804. key.type = (u8)-1;
  805. key.offset = (u64)-1;
  806. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  807. if (ret)
  808. goto out;
  809. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  810. if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
  811. ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
  812. btrfs_release_path(path);
  813. goto insert;
  814. }
  815. found_start = key.objectid;
  816. found_end = key.objectid + key.offset;
  817. ASSERT(found_start >= block_group->key.objectid &&
  818. found_end > block_group->key.objectid);
  819. ASSERT((found_start < start && found_end <= start) ||
  820. (found_start >= end && found_end > end));
  821. /*
  822. * Delete the neighbor on the right and absorb it into the new key
  823. * (cases 3 and 4).
  824. */
  825. if (found_start == end) {
  826. ret = btrfs_del_item(trans, root, path);
  827. if (ret)
  828. goto out;
  829. new_key.offset += key.offset;
  830. new_extents--;
  831. }
  832. btrfs_release_path(path);
  833. insert:
  834. /* Insert the new key (cases 1-4). */
  835. ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
  836. if (ret)
  837. goto out;
  838. btrfs_release_path(path);
  839. ret = update_free_space_extent_count(trans, fs_info, block_group, path,
  840. new_extents);
  841. out:
  842. return ret;
  843. }
  844. int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
  845. struct btrfs_fs_info *fs_info,
  846. struct btrfs_block_group_cache *block_group,
  847. struct btrfs_path *path, u64 start, u64 size)
  848. {
  849. struct btrfs_free_space_info *info;
  850. u32 flags;
  851. int ret;
  852. if (block_group->needs_free_space) {
  853. ret = __add_block_group_free_space(trans, fs_info, block_group,
  854. path);
  855. if (ret)
  856. return ret;
  857. }
  858. info = search_free_space_info(NULL, fs_info, block_group, path, 0);
  859. if (IS_ERR(info))
  860. return PTR_ERR(info);
  861. flags = btrfs_free_space_flags(path->nodes[0], info);
  862. btrfs_release_path(path);
  863. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
  864. return modify_free_space_bitmap(trans, fs_info, block_group,
  865. path, start, size, 0);
  866. } else {
  867. return add_free_space_extent(trans, fs_info, block_group, path,
  868. start, size);
  869. }
  870. }
  871. int add_to_free_space_tree(struct btrfs_trans_handle *trans,
  872. struct btrfs_fs_info *fs_info,
  873. u64 start, u64 size)
  874. {
  875. struct btrfs_block_group_cache *block_group;
  876. struct btrfs_path *path;
  877. int ret;
  878. if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
  879. return 0;
  880. path = btrfs_alloc_path();
  881. if (!path) {
  882. ret = -ENOMEM;
  883. goto out;
  884. }
  885. block_group = btrfs_lookup_block_group(fs_info, start);
  886. if (!block_group) {
  887. ASSERT(0);
  888. ret = -ENOENT;
  889. goto out;
  890. }
  891. mutex_lock(&block_group->free_space_lock);
  892. ret = __add_to_free_space_tree(trans, fs_info, block_group, path, start,
  893. size);
  894. mutex_unlock(&block_group->free_space_lock);
  895. btrfs_put_block_group(block_group);
  896. out:
  897. btrfs_free_path(path);
  898. if (ret)
  899. btrfs_abort_transaction(trans, fs_info->free_space_root, ret);
  900. return ret;
  901. }
  902. /*
  903. * Populate the free space tree by walking the extent tree. Operations on the
  904. * extent tree that happen as a result of writes to the free space tree will go
  905. * through the normal add/remove hooks.
  906. */
  907. static int populate_free_space_tree(struct btrfs_trans_handle *trans,
  908. struct btrfs_fs_info *fs_info,
  909. struct btrfs_block_group_cache *block_group)
  910. {
  911. struct btrfs_root *extent_root = fs_info->extent_root;
  912. struct btrfs_path *path, *path2;
  913. struct btrfs_key key;
  914. u64 start, end;
  915. int ret;
  916. path = btrfs_alloc_path();
  917. if (!path)
  918. return -ENOMEM;
  919. path->reada = 1;
  920. path2 = btrfs_alloc_path();
  921. if (!path2) {
  922. btrfs_free_path(path);
  923. return -ENOMEM;
  924. }
  925. ret = add_new_free_space_info(trans, fs_info, block_group, path2);
  926. if (ret)
  927. goto out;
  928. mutex_lock(&block_group->free_space_lock);
  929. /*
  930. * Iterate through all of the extent and metadata items in this block
  931. * group, adding the free space between them and the free space at the
  932. * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
  933. * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
  934. * contained in.
  935. */
  936. key.objectid = block_group->key.objectid;
  937. key.type = BTRFS_EXTENT_ITEM_KEY;
  938. key.offset = 0;
  939. ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
  940. if (ret < 0)
  941. goto out_locked;
  942. ASSERT(ret == 0);
  943. start = block_group->key.objectid;
  944. end = block_group->key.objectid + block_group->key.offset;
  945. while (1) {
  946. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  947. if (key.type == BTRFS_EXTENT_ITEM_KEY ||
  948. key.type == BTRFS_METADATA_ITEM_KEY) {
  949. if (key.objectid >= end)
  950. break;
  951. if (start < key.objectid) {
  952. ret = __add_to_free_space_tree(trans, fs_info,
  953. block_group,
  954. path2, start,
  955. key.objectid -
  956. start);
  957. if (ret)
  958. goto out_locked;
  959. }
  960. start = key.objectid;
  961. if (key.type == BTRFS_METADATA_ITEM_KEY)
  962. start += fs_info->tree_root->nodesize;
  963. else
  964. start += key.offset;
  965. } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  966. if (key.objectid != block_group->key.objectid)
  967. break;
  968. }
  969. ret = btrfs_next_item(extent_root, path);
  970. if (ret < 0)
  971. goto out_locked;
  972. if (ret)
  973. break;
  974. }
  975. if (start < end) {
  976. ret = __add_to_free_space_tree(trans, fs_info, block_group,
  977. path2, start, end - start);
  978. if (ret)
  979. goto out_locked;
  980. }
  981. ret = 0;
  982. out_locked:
  983. mutex_unlock(&block_group->free_space_lock);
  984. out:
  985. btrfs_free_path(path2);
  986. btrfs_free_path(path);
  987. return ret;
  988. }
  989. int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
  990. {
  991. struct btrfs_trans_handle *trans;
  992. struct btrfs_root *tree_root = fs_info->tree_root;
  993. struct btrfs_root *free_space_root;
  994. struct btrfs_block_group_cache *block_group;
  995. struct rb_node *node;
  996. int ret;
  997. trans = btrfs_start_transaction(tree_root, 0);
  998. if (IS_ERR(trans))
  999. return PTR_ERR(trans);
  1000. fs_info->creating_free_space_tree = 1;
  1001. free_space_root = btrfs_create_tree(trans, fs_info,
  1002. BTRFS_FREE_SPACE_TREE_OBJECTID);
  1003. if (IS_ERR(free_space_root)) {
  1004. ret = PTR_ERR(free_space_root);
  1005. goto abort;
  1006. }
  1007. fs_info->free_space_root = free_space_root;
  1008. node = rb_first(&fs_info->block_group_cache_tree);
  1009. while (node) {
  1010. block_group = rb_entry(node, struct btrfs_block_group_cache,
  1011. cache_node);
  1012. ret = populate_free_space_tree(trans, fs_info, block_group);
  1013. if (ret)
  1014. goto abort;
  1015. node = rb_next(node);
  1016. }
  1017. btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
  1018. fs_info->creating_free_space_tree = 0;
  1019. ret = btrfs_commit_transaction(trans, tree_root);
  1020. if (ret)
  1021. return ret;
  1022. return 0;
  1023. abort:
  1024. fs_info->creating_free_space_tree = 0;
  1025. btrfs_abort_transaction(trans, tree_root, ret);
  1026. btrfs_end_transaction(trans, tree_root);
  1027. return ret;
  1028. }
  1029. static int clear_free_space_tree(struct btrfs_trans_handle *trans,
  1030. struct btrfs_root *root)
  1031. {
  1032. struct btrfs_path *path;
  1033. struct btrfs_key key;
  1034. int nr;
  1035. int ret;
  1036. path = btrfs_alloc_path();
  1037. if (!path)
  1038. return -ENOMEM;
  1039. path->leave_spinning = 1;
  1040. key.objectid = 0;
  1041. key.type = 0;
  1042. key.offset = 0;
  1043. while (1) {
  1044. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1045. if (ret < 0)
  1046. goto out;
  1047. nr = btrfs_header_nritems(path->nodes[0]);
  1048. if (!nr)
  1049. break;
  1050. path->slots[0] = 0;
  1051. ret = btrfs_del_items(trans, root, path, 0, nr);
  1052. if (ret)
  1053. goto out;
  1054. btrfs_release_path(path);
  1055. }
  1056. ret = 0;
  1057. out:
  1058. btrfs_free_path(path);
  1059. return ret;
  1060. }
  1061. int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
  1062. {
  1063. struct btrfs_trans_handle *trans;
  1064. struct btrfs_root *tree_root = fs_info->tree_root;
  1065. struct btrfs_root *free_space_root = fs_info->free_space_root;
  1066. int ret;
  1067. trans = btrfs_start_transaction(tree_root, 0);
  1068. if (IS_ERR(trans))
  1069. return PTR_ERR(trans);
  1070. btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
  1071. fs_info->free_space_root = NULL;
  1072. ret = clear_free_space_tree(trans, free_space_root);
  1073. if (ret)
  1074. goto abort;
  1075. ret = btrfs_del_root(trans, tree_root, &free_space_root->root_key);
  1076. if (ret)
  1077. goto abort;
  1078. list_del(&free_space_root->dirty_list);
  1079. btrfs_tree_lock(free_space_root->node);
  1080. clean_tree_block(trans, tree_root->fs_info, free_space_root->node);
  1081. btrfs_tree_unlock(free_space_root->node);
  1082. btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
  1083. 0, 1);
  1084. free_extent_buffer(free_space_root->node);
  1085. free_extent_buffer(free_space_root->commit_root);
  1086. kfree(free_space_root);
  1087. ret = btrfs_commit_transaction(trans, tree_root);
  1088. if (ret)
  1089. return ret;
  1090. return 0;
  1091. abort:
  1092. btrfs_abort_transaction(trans, tree_root, ret);
  1093. btrfs_end_transaction(trans, tree_root);
  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. u64 start, end;
  1102. int ret;
  1103. start = block_group->key.objectid;
  1104. end = block_group->key.objectid + block_group->key.offset;
  1105. block_group->needs_free_space = 0;
  1106. ret = add_new_free_space_info(trans, fs_info, block_group, path);
  1107. if (ret)
  1108. return ret;
  1109. return __add_to_free_space_tree(trans, fs_info, block_group, path,
  1110. block_group->key.objectid,
  1111. block_group->key.offset);
  1112. }
  1113. int add_block_group_free_space(struct btrfs_trans_handle *trans,
  1114. struct btrfs_fs_info *fs_info,
  1115. struct btrfs_block_group_cache *block_group)
  1116. {
  1117. struct btrfs_path *path = NULL;
  1118. int ret = 0;
  1119. if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
  1120. return 0;
  1121. mutex_lock(&block_group->free_space_lock);
  1122. if (!block_group->needs_free_space)
  1123. goto out;
  1124. path = btrfs_alloc_path();
  1125. if (!path) {
  1126. ret = -ENOMEM;
  1127. goto out;
  1128. }
  1129. ret = __add_block_group_free_space(trans, fs_info, block_group, path);
  1130. out:
  1131. btrfs_free_path(path);
  1132. mutex_unlock(&block_group->free_space_lock);
  1133. if (ret)
  1134. btrfs_abort_transaction(trans, fs_info->free_space_root, ret);
  1135. return ret;
  1136. }
  1137. int remove_block_group_free_space(struct btrfs_trans_handle *trans,
  1138. struct btrfs_fs_info *fs_info,
  1139. struct btrfs_block_group_cache *block_group)
  1140. {
  1141. struct btrfs_root *root = fs_info->free_space_root;
  1142. struct btrfs_path *path;
  1143. struct btrfs_key key, found_key;
  1144. struct extent_buffer *leaf;
  1145. u64 start, end;
  1146. int done = 0, nr;
  1147. int ret;
  1148. if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
  1149. return 0;
  1150. if (block_group->needs_free_space) {
  1151. /* We never added this block group to the free space tree. */
  1152. return 0;
  1153. }
  1154. path = btrfs_alloc_path();
  1155. if (!path) {
  1156. ret = -ENOMEM;
  1157. goto out;
  1158. }
  1159. start = block_group->key.objectid;
  1160. end = block_group->key.objectid + block_group->key.offset;
  1161. key.objectid = end - 1;
  1162. key.type = (u8)-1;
  1163. key.offset = (u64)-1;
  1164. while (!done) {
  1165. ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
  1166. if (ret)
  1167. goto out;
  1168. leaf = path->nodes[0];
  1169. nr = 0;
  1170. path->slots[0]++;
  1171. while (path->slots[0] > 0) {
  1172. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
  1173. if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
  1174. ASSERT(found_key.objectid == block_group->key.objectid);
  1175. ASSERT(found_key.offset == block_group->key.offset);
  1176. done = 1;
  1177. nr++;
  1178. path->slots[0]--;
  1179. break;
  1180. } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
  1181. found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
  1182. ASSERT(found_key.objectid >= start);
  1183. ASSERT(found_key.objectid < end);
  1184. ASSERT(found_key.objectid + found_key.offset <= end);
  1185. nr++;
  1186. path->slots[0]--;
  1187. } else {
  1188. ASSERT(0);
  1189. }
  1190. }
  1191. ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
  1192. if (ret)
  1193. goto out;
  1194. btrfs_release_path(path);
  1195. }
  1196. ret = 0;
  1197. out:
  1198. btrfs_free_path(path);
  1199. if (ret)
  1200. btrfs_abort_transaction(trans, root, ret);
  1201. return ret;
  1202. }
  1203. static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
  1204. struct btrfs_path *path,
  1205. u32 expected_extent_count)
  1206. {
  1207. struct btrfs_block_group_cache *block_group;
  1208. struct btrfs_fs_info *fs_info;
  1209. struct btrfs_root *root;
  1210. struct btrfs_key key;
  1211. int prev_bit = 0, bit;
  1212. /* Initialize to silence GCC. */
  1213. u64 extent_start = 0;
  1214. u64 end, offset;
  1215. u64 total_found = 0;
  1216. u32 extent_count = 0;
  1217. int ret;
  1218. block_group = caching_ctl->block_group;
  1219. fs_info = block_group->fs_info;
  1220. root = fs_info->free_space_root;
  1221. end = block_group->key.objectid + block_group->key.offset;
  1222. while (1) {
  1223. ret = btrfs_next_item(root, path);
  1224. if (ret < 0)
  1225. goto out;
  1226. if (ret)
  1227. break;
  1228. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1229. if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
  1230. break;
  1231. ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
  1232. ASSERT(key.objectid < end && key.objectid + key.offset <= end);
  1233. caching_ctl->progress = key.objectid;
  1234. offset = key.objectid;
  1235. while (offset < key.objectid + key.offset) {
  1236. bit = free_space_test_bit(block_group, path, offset);
  1237. if (prev_bit == 0 && bit == 1) {
  1238. extent_start = offset;
  1239. } else if (prev_bit == 1 && bit == 0) {
  1240. total_found += add_new_free_space(block_group,
  1241. fs_info,
  1242. extent_start,
  1243. offset);
  1244. if (total_found > CACHING_CTL_WAKE_UP) {
  1245. total_found = 0;
  1246. wake_up(&caching_ctl->wait);
  1247. }
  1248. extent_count++;
  1249. }
  1250. prev_bit = bit;
  1251. offset += block_group->sectorsize;
  1252. }
  1253. }
  1254. if (prev_bit == 1) {
  1255. total_found += add_new_free_space(block_group, fs_info,
  1256. extent_start, end);
  1257. extent_count++;
  1258. }
  1259. if (extent_count != expected_extent_count) {
  1260. btrfs_err(fs_info, "incorrect extent count for %llu; counted %u, expected %u",
  1261. block_group->key.objectid, extent_count,
  1262. expected_extent_count);
  1263. ASSERT(0);
  1264. ret = -EIO;
  1265. goto out;
  1266. }
  1267. caching_ctl->progress = (u64)-1;
  1268. ret = 0;
  1269. out:
  1270. return ret;
  1271. }
  1272. static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
  1273. struct btrfs_path *path,
  1274. u32 expected_extent_count)
  1275. {
  1276. struct btrfs_block_group_cache *block_group;
  1277. struct btrfs_fs_info *fs_info;
  1278. struct btrfs_root *root;
  1279. struct btrfs_key key;
  1280. u64 end;
  1281. u64 total_found = 0;
  1282. u32 extent_count = 0;
  1283. int ret;
  1284. block_group = caching_ctl->block_group;
  1285. fs_info = block_group->fs_info;
  1286. root = fs_info->free_space_root;
  1287. end = block_group->key.objectid + block_group->key.offset;
  1288. while (1) {
  1289. ret = btrfs_next_item(root, path);
  1290. if (ret < 0)
  1291. goto out;
  1292. if (ret)
  1293. break;
  1294. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1295. if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
  1296. break;
  1297. ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
  1298. ASSERT(key.objectid < end && key.objectid + key.offset <= end);
  1299. caching_ctl->progress = key.objectid;
  1300. total_found += add_new_free_space(block_group, fs_info,
  1301. key.objectid,
  1302. key.objectid + key.offset);
  1303. if (total_found > CACHING_CTL_WAKE_UP) {
  1304. total_found = 0;
  1305. wake_up(&caching_ctl->wait);
  1306. }
  1307. extent_count++;
  1308. }
  1309. if (extent_count != expected_extent_count) {
  1310. btrfs_err(fs_info, "incorrect extent count for %llu; counted %u, expected %u",
  1311. block_group->key.objectid, extent_count,
  1312. expected_extent_count);
  1313. ASSERT(0);
  1314. ret = -EIO;
  1315. goto out;
  1316. }
  1317. caching_ctl->progress = (u64)-1;
  1318. ret = 0;
  1319. out:
  1320. return ret;
  1321. }
  1322. int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
  1323. {
  1324. struct btrfs_block_group_cache *block_group;
  1325. struct btrfs_fs_info *fs_info;
  1326. struct btrfs_free_space_info *info;
  1327. struct btrfs_path *path;
  1328. u32 extent_count, flags;
  1329. int ret;
  1330. block_group = caching_ctl->block_group;
  1331. fs_info = block_group->fs_info;
  1332. path = btrfs_alloc_path();
  1333. if (!path)
  1334. return -ENOMEM;
  1335. /*
  1336. * Just like caching_thread() doesn't want to deadlock on the extent
  1337. * tree, we don't want to deadlock on the free space tree.
  1338. */
  1339. path->skip_locking = 1;
  1340. path->search_commit_root = 1;
  1341. path->reada = 1;
  1342. info = search_free_space_info(NULL, fs_info, block_group, path, 0);
  1343. if (IS_ERR(info)) {
  1344. ret = PTR_ERR(info);
  1345. goto out;
  1346. }
  1347. extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
  1348. flags = btrfs_free_space_flags(path->nodes[0], info);
  1349. /*
  1350. * We left path pointing to the free space info item, so now
  1351. * load_free_space_foo can just iterate through the free space tree from
  1352. * there.
  1353. */
  1354. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
  1355. ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
  1356. else
  1357. ret = load_free_space_extents(caching_ctl, path, extent_count);
  1358. out:
  1359. btrfs_free_path(path);
  1360. return ret;
  1361. }