free-space-tree.c 40 KB

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