free-space-tree.c 41 KB

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