free-space-tree.c 41 KB

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