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