extent-tree.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * Copyright (C) 2007 Oracle. 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/sched.h>
  19. #include "ctree.h"
  20. #include "disk-io.h"
  21. #include "print-tree.h"
  22. #include "transaction.h"
  23. #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
  24. #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
  25. #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
  26. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  27. btrfs_root *extent_root);
  28. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  29. btrfs_root *extent_root);
  30. static int cache_block_group(struct btrfs_root *root,
  31. struct btrfs_block_group_cache *block_group)
  32. {
  33. struct btrfs_path *path;
  34. int ret;
  35. struct btrfs_key key;
  36. struct extent_buffer *leaf;
  37. struct extent_map_tree *free_space_cache;
  38. int slot;
  39. u64 last = 0;
  40. u64 hole_size;
  41. u64 first_free;
  42. int found = 0;
  43. if (!block_group)
  44. return 0;
  45. root = root->fs_info->extent_root;
  46. free_space_cache = &root->fs_info->free_space_cache;
  47. if (block_group->cached)
  48. return 0;
  49. path = btrfs_alloc_path();
  50. if (!path)
  51. return -ENOMEM;
  52. path->reada = 2;
  53. first_free = block_group->key.objectid;
  54. key.objectid = block_group->key.objectid;
  55. key.offset = 0;
  56. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  57. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  58. if (ret < 0)
  59. return ret;
  60. if (ret && path->slots[0] > 0)
  61. path->slots[0]--;
  62. while(1) {
  63. leaf = path->nodes[0];
  64. slot = path->slots[0];
  65. if (slot >= btrfs_header_nritems(leaf)) {
  66. ret = btrfs_next_leaf(root, path);
  67. if (ret < 0)
  68. goto err;
  69. if (ret == 0) {
  70. continue;
  71. } else {
  72. break;
  73. }
  74. }
  75. btrfs_item_key_to_cpu(leaf, &key, slot);
  76. if (key.objectid < block_group->key.objectid) {
  77. if (key.objectid + key.offset > first_free)
  78. first_free = key.objectid + key.offset;
  79. goto next;
  80. }
  81. if (key.objectid >= block_group->key.objectid +
  82. block_group->key.offset) {
  83. break;
  84. }
  85. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  86. if (!found) {
  87. last = first_free;
  88. found = 1;
  89. }
  90. if (key.objectid > last) {
  91. hole_size = key.objectid - last;
  92. set_extent_dirty(free_space_cache, last,
  93. last + hole_size - 1,
  94. GFP_NOFS);
  95. }
  96. last = key.objectid + key.offset;
  97. }
  98. next:
  99. path->slots[0]++;
  100. }
  101. if (!found)
  102. last = first_free;
  103. if (block_group->key.objectid +
  104. block_group->key.offset > last) {
  105. hole_size = block_group->key.objectid +
  106. block_group->key.offset - last;
  107. set_extent_dirty(free_space_cache, last,
  108. last + hole_size - 1, GFP_NOFS);
  109. }
  110. block_group->cached = 1;
  111. err:
  112. btrfs_free_path(path);
  113. return 0;
  114. }
  115. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  116. btrfs_fs_info *info,
  117. u64 bytenr)
  118. {
  119. struct extent_map_tree *block_group_cache;
  120. struct btrfs_block_group_cache *block_group = NULL;
  121. u64 ptr;
  122. u64 start;
  123. u64 end;
  124. int ret;
  125. block_group_cache = &info->block_group_cache;
  126. ret = find_first_extent_bit(block_group_cache,
  127. bytenr, &start, &end,
  128. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
  129. if (ret) {
  130. return NULL;
  131. }
  132. ret = get_state_private(block_group_cache, start, &ptr);
  133. if (ret)
  134. return NULL;
  135. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  136. if (block_group->key.objectid <= bytenr && bytenr <
  137. block_group->key.objectid + block_group->key.offset)
  138. return block_group;
  139. return NULL;
  140. }
  141. static u64 find_search_start(struct btrfs_root *root,
  142. struct btrfs_block_group_cache **cache_ret,
  143. u64 search_start, int num,
  144. int data, int full_scan)
  145. {
  146. int ret;
  147. struct btrfs_block_group_cache *cache = *cache_ret;
  148. u64 last;
  149. u64 start = 0;
  150. u64 end = 0;
  151. u64 cache_miss = 0;
  152. int wrapped = 0;
  153. if (!cache) {
  154. cache = btrfs_lookup_block_group(root->fs_info, search_start);
  155. if (!cache)
  156. return search_start;
  157. }
  158. again:
  159. ret = cache_block_group(root, cache);
  160. if (ret)
  161. goto out;
  162. last = max(search_start, cache->key.objectid);
  163. while(1) {
  164. ret = find_first_extent_bit(&root->fs_info->free_space_cache,
  165. last, &start, &end, EXTENT_DIRTY);
  166. if (ret) {
  167. if (!cache_miss)
  168. cache_miss = last;
  169. goto new_group;
  170. }
  171. start = max(last, start);
  172. last = end + 1;
  173. if (last - start < num) {
  174. if (last == cache->key.objectid + cache->key.offset)
  175. cache_miss = start;
  176. continue;
  177. }
  178. if (data != BTRFS_BLOCK_GROUP_MIXED &&
  179. start + num > cache->key.objectid + cache->key.offset)
  180. goto new_group;
  181. return start;
  182. }
  183. out:
  184. return search_start;
  185. new_group:
  186. last = cache->key.objectid + cache->key.offset;
  187. wrapped:
  188. cache = btrfs_lookup_block_group(root->fs_info, last);
  189. if (!cache) {
  190. no_cache:
  191. if (!wrapped) {
  192. wrapped = 1;
  193. last = search_start;
  194. data = BTRFS_BLOCK_GROUP_MIXED;
  195. goto wrapped;
  196. }
  197. return search_start;
  198. }
  199. if (cache_miss && !cache->cached) {
  200. cache_block_group(root, cache);
  201. last = cache_miss;
  202. cache = btrfs_lookup_block_group(root->fs_info, last);
  203. }
  204. if (!full_scan)
  205. cache = btrfs_find_block_group(root, cache, last, data, 0);
  206. if (!cache)
  207. goto no_cache;
  208. *cache_ret = cache;
  209. cache_miss = 0;
  210. goto again;
  211. }
  212. static u64 div_factor(u64 num, int factor)
  213. {
  214. if (factor == 10)
  215. return num;
  216. num *= factor;
  217. do_div(num, 10);
  218. return num;
  219. }
  220. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  221. struct btrfs_block_group_cache
  222. *hint, u64 search_start,
  223. int data, int owner)
  224. {
  225. struct btrfs_block_group_cache *cache;
  226. struct extent_map_tree *block_group_cache;
  227. struct btrfs_block_group_cache *found_group = NULL;
  228. struct btrfs_fs_info *info = root->fs_info;
  229. u64 used;
  230. u64 last = 0;
  231. u64 hint_last;
  232. u64 start;
  233. u64 end;
  234. u64 free_check;
  235. u64 ptr;
  236. int bit;
  237. int ret;
  238. int full_search = 0;
  239. int factor = 8;
  240. int data_swap = 0;
  241. block_group_cache = &info->block_group_cache;
  242. if (!owner)
  243. factor = 8;
  244. if (data == BTRFS_BLOCK_GROUP_MIXED) {
  245. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  246. factor = 10;
  247. } else if (data)
  248. bit = BLOCK_GROUP_DATA;
  249. else
  250. bit = BLOCK_GROUP_METADATA;
  251. if (search_start) {
  252. struct btrfs_block_group_cache *shint;
  253. shint = btrfs_lookup_block_group(info, search_start);
  254. if (shint && (shint->data == data ||
  255. shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
  256. used = btrfs_block_group_used(&shint->item);
  257. if (used + shint->pinned <
  258. div_factor(shint->key.offset, factor)) {
  259. return shint;
  260. }
  261. }
  262. }
  263. if (hint && (hint->data == data ||
  264. hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
  265. used = btrfs_block_group_used(&hint->item);
  266. if (used + hint->pinned <
  267. div_factor(hint->key.offset, factor)) {
  268. return hint;
  269. }
  270. last = hint->key.objectid + hint->key.offset;
  271. hint_last = last;
  272. } else {
  273. if (hint)
  274. hint_last = max(hint->key.objectid, search_start);
  275. else
  276. hint_last = search_start;
  277. last = hint_last;
  278. }
  279. again:
  280. while(1) {
  281. ret = find_first_extent_bit(block_group_cache, last,
  282. &start, &end, bit);
  283. if (ret)
  284. break;
  285. ret = get_state_private(block_group_cache, start, &ptr);
  286. if (ret)
  287. break;
  288. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  289. last = cache->key.objectid + cache->key.offset;
  290. used = btrfs_block_group_used(&cache->item);
  291. if (full_search)
  292. free_check = cache->key.offset;
  293. else
  294. free_check = div_factor(cache->key.offset, factor);
  295. if (used + cache->pinned < free_check) {
  296. found_group = cache;
  297. goto found;
  298. }
  299. cond_resched();
  300. }
  301. if (!full_search) {
  302. last = search_start;
  303. full_search = 1;
  304. goto again;
  305. }
  306. if (!data_swap) {
  307. data_swap = 1;
  308. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  309. last = search_start;
  310. goto again;
  311. }
  312. found:
  313. return found_group;
  314. }
  315. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  316. struct btrfs_root *root,
  317. u64 bytenr, u64 num_bytes)
  318. {
  319. struct btrfs_path *path;
  320. int ret;
  321. struct btrfs_key key;
  322. struct extent_buffer *l;
  323. struct btrfs_extent_item *item;
  324. u32 refs;
  325. WARN_ON(num_bytes < root->sectorsize);
  326. path = btrfs_alloc_path();
  327. if (!path)
  328. return -ENOMEM;
  329. key.objectid = bytenr;
  330. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  331. key.offset = num_bytes;
  332. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  333. 0, 1);
  334. if (ret < 0)
  335. return ret;
  336. if (ret != 0) {
  337. BUG();
  338. }
  339. BUG_ON(ret != 0);
  340. l = path->nodes[0];
  341. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  342. refs = btrfs_extent_refs(l, item);
  343. btrfs_set_extent_refs(l, item, refs + 1);
  344. btrfs_mark_buffer_dirty(path->nodes[0]);
  345. btrfs_release_path(root->fs_info->extent_root, path);
  346. btrfs_free_path(path);
  347. finish_current_insert(trans, root->fs_info->extent_root);
  348. del_pending_extents(trans, root->fs_info->extent_root);
  349. return 0;
  350. }
  351. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  352. struct btrfs_root *root)
  353. {
  354. finish_current_insert(trans, root->fs_info->extent_root);
  355. del_pending_extents(trans, root->fs_info->extent_root);
  356. return 0;
  357. }
  358. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  359. struct btrfs_root *root, u64 bytenr,
  360. u64 num_bytes, u32 *refs)
  361. {
  362. struct btrfs_path *path;
  363. int ret;
  364. struct btrfs_key key;
  365. struct extent_buffer *l;
  366. struct btrfs_extent_item *item;
  367. WARN_ON(num_bytes < root->sectorsize);
  368. path = btrfs_alloc_path();
  369. key.objectid = bytenr;
  370. key.offset = num_bytes;
  371. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  372. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  373. 0, 0);
  374. if (ret < 0)
  375. goto out;
  376. if (ret != 0) {
  377. btrfs_print_leaf(root, path->nodes[0]);
  378. printk("failed to find block number %Lu\n", bytenr);
  379. BUG();
  380. }
  381. l = path->nodes[0];
  382. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  383. *refs = btrfs_extent_refs(l, item);
  384. out:
  385. btrfs_free_path(path);
  386. return 0;
  387. }
  388. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  389. struct btrfs_root *root)
  390. {
  391. return btrfs_inc_extent_ref(trans, root, root->node->start,
  392. root->node->len);
  393. }
  394. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  395. struct extent_buffer *buf)
  396. {
  397. u64 bytenr;
  398. u32 nritems;
  399. struct btrfs_key key;
  400. struct btrfs_file_extent_item *fi;
  401. int i;
  402. int level;
  403. int ret;
  404. int faili;
  405. int err;
  406. if (!root->ref_cows)
  407. return 0;
  408. level = btrfs_header_level(buf);
  409. nritems = btrfs_header_nritems(buf);
  410. for (i = 0; i < nritems; i++) {
  411. if (level == 0) {
  412. u64 disk_bytenr;
  413. btrfs_item_key_to_cpu(buf, &key, i);
  414. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  415. continue;
  416. fi = btrfs_item_ptr(buf, i,
  417. struct btrfs_file_extent_item);
  418. if (btrfs_file_extent_type(buf, fi) ==
  419. BTRFS_FILE_EXTENT_INLINE)
  420. continue;
  421. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  422. if (disk_bytenr == 0)
  423. continue;
  424. ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
  425. btrfs_file_extent_disk_num_bytes(buf, fi));
  426. if (ret) {
  427. faili = i;
  428. goto fail;
  429. }
  430. } else {
  431. bytenr = btrfs_node_blockptr(buf, i);
  432. ret = btrfs_inc_extent_ref(trans, root, bytenr,
  433. btrfs_level_size(root, level - 1));
  434. if (ret) {
  435. faili = i;
  436. goto fail;
  437. }
  438. }
  439. }
  440. return 0;
  441. fail:
  442. WARN_ON(1);
  443. for (i =0; i < faili; i++) {
  444. if (level == 0) {
  445. u64 disk_bytenr;
  446. btrfs_item_key_to_cpu(buf, &key, i);
  447. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  448. continue;
  449. fi = btrfs_item_ptr(buf, i,
  450. struct btrfs_file_extent_item);
  451. if (btrfs_file_extent_type(buf, fi) ==
  452. BTRFS_FILE_EXTENT_INLINE)
  453. continue;
  454. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  455. if (disk_bytenr == 0)
  456. continue;
  457. err = btrfs_free_extent(trans, root, disk_bytenr,
  458. btrfs_file_extent_disk_num_bytes(buf,
  459. fi), 0);
  460. BUG_ON(err);
  461. } else {
  462. bytenr = btrfs_node_blockptr(buf, i);
  463. err = btrfs_free_extent(trans, root, bytenr,
  464. btrfs_level_size(root, level - 1), 0);
  465. BUG_ON(err);
  466. }
  467. }
  468. return ret;
  469. }
  470. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  471. struct btrfs_root *root,
  472. struct btrfs_path *path,
  473. struct btrfs_block_group_cache *cache)
  474. {
  475. int ret;
  476. int pending_ret;
  477. struct btrfs_root *extent_root = root->fs_info->extent_root;
  478. unsigned long bi;
  479. struct extent_buffer *leaf;
  480. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  481. if (ret < 0)
  482. goto fail;
  483. BUG_ON(ret);
  484. leaf = path->nodes[0];
  485. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  486. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  487. btrfs_mark_buffer_dirty(leaf);
  488. btrfs_release_path(extent_root, path);
  489. fail:
  490. finish_current_insert(trans, extent_root);
  491. pending_ret = del_pending_extents(trans, extent_root);
  492. if (ret)
  493. return ret;
  494. if (pending_ret)
  495. return pending_ret;
  496. return 0;
  497. }
  498. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  499. struct btrfs_root *root)
  500. {
  501. struct extent_map_tree *block_group_cache;
  502. struct btrfs_block_group_cache *cache;
  503. int ret;
  504. int err = 0;
  505. int werr = 0;
  506. struct btrfs_path *path;
  507. u64 last = 0;
  508. u64 start;
  509. u64 end;
  510. u64 ptr;
  511. block_group_cache = &root->fs_info->block_group_cache;
  512. path = btrfs_alloc_path();
  513. if (!path)
  514. return -ENOMEM;
  515. while(1) {
  516. ret = find_first_extent_bit(block_group_cache, last,
  517. &start, &end, BLOCK_GROUP_DIRTY);
  518. if (ret)
  519. break;
  520. last = end + 1;
  521. ret = get_state_private(block_group_cache, start, &ptr);
  522. if (ret)
  523. break;
  524. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  525. err = write_one_cache_group(trans, root,
  526. path, cache);
  527. /*
  528. * if we fail to write the cache group, we want
  529. * to keep it marked dirty in hopes that a later
  530. * write will work
  531. */
  532. if (err) {
  533. werr = err;
  534. continue;
  535. }
  536. clear_extent_bits(block_group_cache, start, end,
  537. BLOCK_GROUP_DIRTY, GFP_NOFS);
  538. }
  539. btrfs_free_path(path);
  540. return werr;
  541. }
  542. static int update_block_group(struct btrfs_trans_handle *trans,
  543. struct btrfs_root *root,
  544. u64 bytenr, u64 num_bytes, int alloc,
  545. int mark_free, int data)
  546. {
  547. struct btrfs_block_group_cache *cache;
  548. struct btrfs_fs_info *info = root->fs_info;
  549. u64 total = num_bytes;
  550. u64 old_val;
  551. u64 byte_in_group;
  552. u64 start;
  553. u64 end;
  554. while(total) {
  555. cache = btrfs_lookup_block_group(info, bytenr);
  556. if (!cache) {
  557. return -1;
  558. }
  559. byte_in_group = bytenr - cache->key.objectid;
  560. WARN_ON(byte_in_group > cache->key.offset);
  561. start = cache->key.objectid;
  562. end = start + cache->key.offset - 1;
  563. set_extent_bits(&info->block_group_cache, start, end,
  564. BLOCK_GROUP_DIRTY, GFP_NOFS);
  565. old_val = btrfs_block_group_used(&cache->item);
  566. num_bytes = min(total, cache->key.offset - byte_in_group);
  567. if (alloc) {
  568. if (cache->data != data &&
  569. old_val < (cache->key.offset >> 1)) {
  570. int bit_to_clear;
  571. int bit_to_set;
  572. cache->data = data;
  573. if (data) {
  574. bit_to_clear = BLOCK_GROUP_METADATA;
  575. bit_to_set = BLOCK_GROUP_DATA;
  576. cache->item.flags &=
  577. ~BTRFS_BLOCK_GROUP_MIXED;
  578. cache->item.flags |=
  579. BTRFS_BLOCK_GROUP_DATA;
  580. } else {
  581. bit_to_clear = BLOCK_GROUP_DATA;
  582. bit_to_set = BLOCK_GROUP_METADATA;
  583. cache->item.flags &=
  584. ~BTRFS_BLOCK_GROUP_MIXED;
  585. cache->item.flags &=
  586. ~BTRFS_BLOCK_GROUP_DATA;
  587. }
  588. clear_extent_bits(&info->block_group_cache,
  589. start, end, bit_to_clear,
  590. GFP_NOFS);
  591. set_extent_bits(&info->block_group_cache,
  592. start, end, bit_to_set,
  593. GFP_NOFS);
  594. } else if (cache->data != data &&
  595. cache->data != BTRFS_BLOCK_GROUP_MIXED) {
  596. cache->data = BTRFS_BLOCK_GROUP_MIXED;
  597. set_extent_bits(&info->block_group_cache,
  598. start, end,
  599. BLOCK_GROUP_DATA |
  600. BLOCK_GROUP_METADATA,
  601. GFP_NOFS);
  602. }
  603. old_val += num_bytes;
  604. } else {
  605. old_val -= num_bytes;
  606. if (mark_free) {
  607. set_extent_dirty(&info->free_space_cache,
  608. bytenr, bytenr + num_bytes - 1,
  609. GFP_NOFS);
  610. }
  611. }
  612. btrfs_set_block_group_used(&cache->item, old_val);
  613. total -= num_bytes;
  614. bytenr += num_bytes;
  615. }
  616. return 0;
  617. }
  618. static int update_pinned_extents(struct btrfs_root *root,
  619. u64 bytenr, u64 num, int pin)
  620. {
  621. u64 len;
  622. struct btrfs_block_group_cache *cache;
  623. struct btrfs_fs_info *fs_info = root->fs_info;
  624. if (pin) {
  625. set_extent_dirty(&fs_info->pinned_extents,
  626. bytenr, bytenr + num - 1, GFP_NOFS);
  627. } else {
  628. clear_extent_dirty(&fs_info->pinned_extents,
  629. bytenr, bytenr + num - 1, GFP_NOFS);
  630. }
  631. while (num > 0) {
  632. cache = btrfs_lookup_block_group(fs_info, bytenr);
  633. WARN_ON(!cache);
  634. len = min(num, cache->key.offset -
  635. (bytenr - cache->key.objectid));
  636. if (pin) {
  637. cache->pinned += len;
  638. fs_info->total_pinned += len;
  639. } else {
  640. cache->pinned -= len;
  641. fs_info->total_pinned -= len;
  642. }
  643. bytenr += len;
  644. num -= len;
  645. }
  646. return 0;
  647. }
  648. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
  649. {
  650. u64 last = 0;
  651. u64 start;
  652. u64 end;
  653. struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
  654. int ret;
  655. while(1) {
  656. ret = find_first_extent_bit(pinned_extents, last,
  657. &start, &end, EXTENT_DIRTY);
  658. if (ret)
  659. break;
  660. set_extent_dirty(copy, start, end, GFP_NOFS);
  661. last = end + 1;
  662. }
  663. return 0;
  664. }
  665. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  666. struct btrfs_root *root,
  667. struct extent_map_tree *unpin)
  668. {
  669. u64 start;
  670. u64 end;
  671. int ret;
  672. struct extent_map_tree *free_space_cache;
  673. free_space_cache = &root->fs_info->free_space_cache;
  674. while(1) {
  675. ret = find_first_extent_bit(unpin, 0, &start, &end,
  676. EXTENT_DIRTY);
  677. if (ret)
  678. break;
  679. update_pinned_extents(root, start, end + 1 - start, 0);
  680. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  681. set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
  682. }
  683. return 0;
  684. }
  685. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  686. btrfs_root *extent_root)
  687. {
  688. struct btrfs_key ins;
  689. struct btrfs_extent_item extent_item;
  690. int ret;
  691. int err = 0;
  692. u64 start;
  693. u64 end;
  694. struct btrfs_fs_info *info = extent_root->fs_info;
  695. btrfs_set_stack_extent_refs(&extent_item, 1);
  696. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  697. btrfs_set_stack_extent_owner(&extent_item,
  698. extent_root->root_key.objectid);
  699. while(1) {
  700. ret = find_first_extent_bit(&info->extent_ins, 0, &start,
  701. &end, EXTENT_LOCKED);
  702. if (ret)
  703. break;
  704. ins.objectid = start;
  705. ins.offset = end + 1 - start;
  706. err = btrfs_insert_item(trans, extent_root, &ins,
  707. &extent_item, sizeof(extent_item));
  708. clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
  709. GFP_NOFS);
  710. }
  711. return 0;
  712. }
  713. static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
  714. int pending)
  715. {
  716. int err = 0;
  717. struct extent_buffer *buf;
  718. if (!pending) {
  719. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  720. if (buf) {
  721. if (btrfs_buffer_uptodate(buf)) {
  722. u64 transid =
  723. root->fs_info->running_transaction->transid;
  724. if (btrfs_header_generation(buf) == transid) {
  725. free_extent_buffer(buf);
  726. return 1;
  727. }
  728. }
  729. free_extent_buffer(buf);
  730. }
  731. update_pinned_extents(root, bytenr, num_bytes, 1);
  732. } else {
  733. set_extent_bits(&root->fs_info->pending_del,
  734. bytenr, bytenr + num_bytes - 1,
  735. EXTENT_LOCKED, GFP_NOFS);
  736. }
  737. BUG_ON(err < 0);
  738. return 0;
  739. }
  740. /*
  741. * remove an extent from the root, returns 0 on success
  742. */
  743. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  744. *root, u64 bytenr, u64 num_bytes, int pin,
  745. int mark_free)
  746. {
  747. struct btrfs_path *path;
  748. struct btrfs_key key;
  749. struct btrfs_fs_info *info = root->fs_info;
  750. struct btrfs_root *extent_root = info->extent_root;
  751. struct extent_buffer *leaf;
  752. int ret;
  753. struct btrfs_extent_item *ei;
  754. u32 refs;
  755. key.objectid = bytenr;
  756. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  757. key.offset = num_bytes;
  758. path = btrfs_alloc_path();
  759. if (!path)
  760. return -ENOMEM;
  761. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  762. if (ret < 0)
  763. return ret;
  764. BUG_ON(ret);
  765. leaf = path->nodes[0];
  766. ei = btrfs_item_ptr(leaf, path->slots[0],
  767. struct btrfs_extent_item);
  768. refs = btrfs_extent_refs(leaf, ei);
  769. BUG_ON(refs == 0);
  770. refs -= 1;
  771. btrfs_set_extent_refs(leaf, ei, refs);
  772. btrfs_mark_buffer_dirty(leaf);
  773. if (refs == 0) {
  774. u64 super_used;
  775. u64 root_used;
  776. if (pin) {
  777. ret = pin_down_bytes(root, bytenr, num_bytes, 0);
  778. if (ret > 0)
  779. mark_free = 1;
  780. BUG_ON(ret < 0);
  781. }
  782. /* block accounting for super block */
  783. super_used = btrfs_super_bytes_used(&info->super_copy);
  784. btrfs_set_super_bytes_used(&info->super_copy,
  785. super_used - num_bytes);
  786. /* block accounting for root item */
  787. root_used = btrfs_root_used(&root->root_item);
  788. btrfs_set_root_used(&root->root_item,
  789. root_used - num_bytes);
  790. ret = btrfs_del_item(trans, extent_root, path);
  791. if (ret) {
  792. return ret;
  793. }
  794. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  795. mark_free, 0);
  796. BUG_ON(ret);
  797. }
  798. btrfs_free_path(path);
  799. finish_current_insert(trans, extent_root);
  800. return ret;
  801. }
  802. /*
  803. * find all the blocks marked as pending in the radix tree and remove
  804. * them from the extent map
  805. */
  806. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  807. btrfs_root *extent_root)
  808. {
  809. int ret;
  810. int err = 0;
  811. u64 start;
  812. u64 end;
  813. struct extent_map_tree *pending_del;
  814. struct extent_map_tree *pinned_extents;
  815. pending_del = &extent_root->fs_info->pending_del;
  816. pinned_extents = &extent_root->fs_info->pinned_extents;
  817. while(1) {
  818. ret = find_first_extent_bit(pending_del, 0, &start, &end,
  819. EXTENT_LOCKED);
  820. if (ret)
  821. break;
  822. update_pinned_extents(extent_root, start, end + 1 - start, 1);
  823. clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
  824. GFP_NOFS);
  825. ret = __free_extent(trans, extent_root,
  826. start, end + 1 - start, 0, 0);
  827. if (ret)
  828. err = ret;
  829. }
  830. return err;
  831. }
  832. /*
  833. * remove an extent from the root, returns 0 on success
  834. */
  835. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  836. *root, u64 bytenr, u64 num_bytes, int pin)
  837. {
  838. struct btrfs_root *extent_root = root->fs_info->extent_root;
  839. int pending_ret;
  840. int ret;
  841. WARN_ON(num_bytes < root->sectorsize);
  842. if (root == extent_root) {
  843. pin_down_bytes(root, bytenr, num_bytes, 1);
  844. return 0;
  845. }
  846. ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
  847. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  848. return ret ? ret : pending_ret;
  849. }
  850. /*
  851. * walks the btree of allocated extents and find a hole of a given size.
  852. * The key ins is changed to record the hole:
  853. * ins->objectid == block start
  854. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  855. * ins->offset == number of blocks
  856. * Any available blocks before search_start are skipped.
  857. */
  858. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  859. *orig_root, u64 num_bytes, u64 empty_size,
  860. u64 search_start, u64 search_end, u64 hint_byte,
  861. struct btrfs_key *ins, u64 exclude_start,
  862. u64 exclude_nr, int data)
  863. {
  864. struct btrfs_path *path;
  865. struct btrfs_key key;
  866. int ret;
  867. u64 hole_size = 0;
  868. int slot = 0;
  869. u64 last_byte = 0;
  870. u64 orig_search_start = search_start;
  871. int start_found;
  872. struct extent_buffer *l;
  873. struct btrfs_root * root = orig_root->fs_info->extent_root;
  874. struct btrfs_fs_info *info = root->fs_info;
  875. u64 total_needed = num_bytes;
  876. int level;
  877. struct btrfs_block_group_cache *block_group;
  878. int full_scan = 0;
  879. int wrapped = 0;
  880. u64 cached_start;
  881. WARN_ON(num_bytes < root->sectorsize);
  882. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  883. level = btrfs_header_level(root->node);
  884. if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
  885. data = BTRFS_BLOCK_GROUP_MIXED;
  886. }
  887. if (search_end == (u64)-1)
  888. search_end = btrfs_super_total_bytes(&info->super_copy);
  889. if (hint_byte) {
  890. block_group = btrfs_lookup_block_group(info, hint_byte);
  891. block_group = btrfs_find_block_group(root, block_group,
  892. hint_byte, data, 1);
  893. } else {
  894. block_group = btrfs_find_block_group(root,
  895. trans->block_group, 0,
  896. data, 1);
  897. }
  898. total_needed += empty_size;
  899. path = btrfs_alloc_path();
  900. check_failed:
  901. search_start = find_search_start(root, &block_group, search_start,
  902. total_needed, data, full_scan);
  903. cached_start = search_start;
  904. btrfs_init_path(path);
  905. ins->objectid = search_start;
  906. ins->offset = 0;
  907. start_found = 0;
  908. path->reada = 2;
  909. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  910. if (ret < 0)
  911. goto error;
  912. if (path->slots[0] > 0) {
  913. path->slots[0]--;
  914. }
  915. l = path->nodes[0];
  916. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  917. /*
  918. * a rare case, go back one key if we hit a block group item
  919. * instead of an extent item
  920. */
  921. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
  922. key.objectid + key.offset >= search_start) {
  923. ins->objectid = key.objectid;
  924. ins->offset = key.offset - 1;
  925. btrfs_release_path(root, path);
  926. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  927. if (ret < 0)
  928. goto error;
  929. if (path->slots[0] > 0) {
  930. path->slots[0]--;
  931. }
  932. }
  933. while (1) {
  934. l = path->nodes[0];
  935. slot = path->slots[0];
  936. if (slot >= btrfs_header_nritems(l)) {
  937. ret = btrfs_next_leaf(root, path);
  938. if (ret == 0)
  939. continue;
  940. if (ret < 0)
  941. goto error;
  942. search_start = max(search_start,
  943. block_group->key.objectid);
  944. if (!start_found) {
  945. ins->objectid = search_start;
  946. ins->offset = search_end - search_start;
  947. start_found = 1;
  948. goto check_pending;
  949. }
  950. ins->objectid = last_byte > search_start ?
  951. last_byte : search_start;
  952. ins->offset = search_end - ins->objectid;
  953. BUG_ON(ins->objectid >= search_end);
  954. goto check_pending;
  955. }
  956. btrfs_item_key_to_cpu(l, &key, slot);
  957. if (key.objectid >= search_start && key.objectid > last_byte &&
  958. start_found) {
  959. if (last_byte < search_start)
  960. last_byte = search_start;
  961. hole_size = key.objectid - last_byte;
  962. if (hole_size >= num_bytes) {
  963. ins->objectid = last_byte;
  964. ins->offset = hole_size;
  965. goto check_pending;
  966. }
  967. }
  968. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
  969. if (!start_found) {
  970. last_byte = key.objectid;
  971. start_found = 1;
  972. }
  973. goto next;
  974. }
  975. start_found = 1;
  976. last_byte = key.objectid + key.offset;
  977. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  978. last_byte >= block_group->key.objectid +
  979. block_group->key.offset) {
  980. btrfs_release_path(root, path);
  981. search_start = block_group->key.objectid +
  982. block_group->key.offset;
  983. goto new_group;
  984. }
  985. next:
  986. path->slots[0]++;
  987. cond_resched();
  988. }
  989. check_pending:
  990. /* we have to make sure we didn't find an extent that has already
  991. * been allocated by the map tree or the original allocation
  992. */
  993. btrfs_release_path(root, path);
  994. BUG_ON(ins->objectid < search_start);
  995. if (ins->objectid + num_bytes >= search_end)
  996. goto enospc;
  997. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  998. ins->objectid + num_bytes > block_group->
  999. key.objectid + block_group->key.offset) {
  1000. search_start = block_group->key.objectid +
  1001. block_group->key.offset;
  1002. goto new_group;
  1003. }
  1004. if (test_range_bit(&info->extent_ins, ins->objectid,
  1005. ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
  1006. search_start = ins->objectid + num_bytes;
  1007. goto new_group;
  1008. }
  1009. if (test_range_bit(&info->pinned_extents, ins->objectid,
  1010. ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
  1011. search_start = ins->objectid + num_bytes;
  1012. goto new_group;
  1013. }
  1014. if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
  1015. ins->objectid < exclude_start + exclude_nr)) {
  1016. search_start = exclude_start + exclude_nr;
  1017. goto new_group;
  1018. }
  1019. if (!data) {
  1020. block_group = btrfs_lookup_block_group(info, ins->objectid);
  1021. if (block_group)
  1022. trans->block_group = block_group;
  1023. }
  1024. ins->offset = num_bytes;
  1025. btrfs_free_path(path);
  1026. return 0;
  1027. new_group:
  1028. if (search_start + num_bytes >= search_end) {
  1029. enospc:
  1030. search_start = orig_search_start;
  1031. if (full_scan) {
  1032. ret = -ENOSPC;
  1033. goto error;
  1034. }
  1035. if (wrapped) {
  1036. if (!full_scan)
  1037. total_needed -= empty_size;
  1038. full_scan = 1;
  1039. } else
  1040. wrapped = 1;
  1041. }
  1042. block_group = btrfs_lookup_block_group(info, search_start);
  1043. cond_resched();
  1044. if (!full_scan)
  1045. block_group = btrfs_find_block_group(root, block_group,
  1046. search_start, data, 0);
  1047. goto check_failed;
  1048. error:
  1049. btrfs_release_path(root, path);
  1050. btrfs_free_path(path);
  1051. return ret;
  1052. }
  1053. /*
  1054. * finds a free extent and does all the dirty work required for allocation
  1055. * returns the key for the extent through ins, and a tree buffer for
  1056. * the first block of the extent through buf.
  1057. *
  1058. * returns 0 if everything worked, non-zero otherwise.
  1059. */
  1060. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  1061. struct btrfs_root *root, u64 owner,
  1062. u64 num_bytes, u64 empty_size, u64 hint_byte,
  1063. u64 search_end, struct btrfs_key *ins, int data)
  1064. {
  1065. int ret;
  1066. int pending_ret;
  1067. u64 super_used, root_used;
  1068. u64 search_start = 0;
  1069. struct btrfs_fs_info *info = root->fs_info;
  1070. struct btrfs_root *extent_root = info->extent_root;
  1071. struct btrfs_extent_item extent_item;
  1072. btrfs_set_stack_extent_refs(&extent_item, 1);
  1073. btrfs_set_stack_extent_owner(&extent_item, owner);
  1074. WARN_ON(num_bytes < root->sectorsize);
  1075. ret = find_free_extent(trans, root, num_bytes, empty_size,
  1076. search_start, search_end, hint_byte, ins,
  1077. trans->alloc_exclude_start,
  1078. trans->alloc_exclude_nr, data);
  1079. BUG_ON(ret);
  1080. if (ret)
  1081. return ret;
  1082. /* block accounting for super block */
  1083. super_used = btrfs_super_bytes_used(&info->super_copy);
  1084. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  1085. /* block accounting for root item */
  1086. root_used = btrfs_root_used(&root->root_item);
  1087. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  1088. clear_extent_dirty(&root->fs_info->free_space_cache,
  1089. ins->objectid, ins->objectid + ins->offset - 1,
  1090. GFP_NOFS);
  1091. if (root == extent_root) {
  1092. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  1093. ins->objectid + ins->offset - 1,
  1094. EXTENT_LOCKED, GFP_NOFS);
  1095. WARN_ON(data == 1);
  1096. goto update_block;
  1097. }
  1098. WARN_ON(trans->alloc_exclude_nr);
  1099. trans->alloc_exclude_start = ins->objectid;
  1100. trans->alloc_exclude_nr = ins->offset;
  1101. ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
  1102. sizeof(extent_item));
  1103. trans->alloc_exclude_start = 0;
  1104. trans->alloc_exclude_nr = 0;
  1105. BUG_ON(ret);
  1106. finish_current_insert(trans, extent_root);
  1107. pending_ret = del_pending_extents(trans, extent_root);
  1108. if (ret) {
  1109. return ret;
  1110. }
  1111. if (pending_ret) {
  1112. return pending_ret;
  1113. }
  1114. update_block:
  1115. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
  1116. data);
  1117. BUG_ON(ret);
  1118. return 0;
  1119. }
  1120. /*
  1121. * helper function to allocate a block for a given tree
  1122. * returns the tree buffer or NULL.
  1123. */
  1124. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1125. struct btrfs_root *root,
  1126. u32 blocksize, u64 hint,
  1127. u64 empty_size)
  1128. {
  1129. struct btrfs_key ins;
  1130. int ret;
  1131. struct extent_buffer *buf;
  1132. ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
  1133. blocksize, empty_size, hint,
  1134. (u64)-1, &ins, 0);
  1135. if (ret) {
  1136. BUG_ON(ret > 0);
  1137. return ERR_PTR(ret);
  1138. }
  1139. buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
  1140. if (!buf) {
  1141. btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
  1142. return ERR_PTR(-ENOMEM);
  1143. }
  1144. btrfs_set_buffer_uptodate(buf);
  1145. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  1146. buf->start + buf->len - 1, GFP_NOFS);
  1147. set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
  1148. buf->start, buf->start + buf->len - 1,
  1149. EXTENT_CSUM, GFP_NOFS);
  1150. buf->flags |= EXTENT_CSUM;
  1151. btrfs_set_buffer_defrag(buf);
  1152. trans->blocks_used++;
  1153. return buf;
  1154. }
  1155. static int drop_leaf_ref(struct btrfs_trans_handle *trans,
  1156. struct btrfs_root *root, struct extent_buffer *leaf)
  1157. {
  1158. struct btrfs_key key;
  1159. struct btrfs_file_extent_item *fi;
  1160. int i;
  1161. int nritems;
  1162. int ret;
  1163. BUG_ON(!btrfs_is_leaf(leaf));
  1164. nritems = btrfs_header_nritems(leaf);
  1165. for (i = 0; i < nritems; i++) {
  1166. u64 disk_bytenr;
  1167. btrfs_item_key_to_cpu(leaf, &key, i);
  1168. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1169. continue;
  1170. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  1171. if (btrfs_file_extent_type(leaf, fi) ==
  1172. BTRFS_FILE_EXTENT_INLINE)
  1173. continue;
  1174. /*
  1175. * FIXME make sure to insert a trans record that
  1176. * repeats the snapshot del on crash
  1177. */
  1178. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1179. if (disk_bytenr == 0)
  1180. continue;
  1181. ret = btrfs_free_extent(trans, root, disk_bytenr,
  1182. btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
  1183. BUG_ON(ret);
  1184. }
  1185. return 0;
  1186. }
  1187. static void reada_walk_down(struct btrfs_root *root,
  1188. struct extent_buffer *node)
  1189. {
  1190. int i;
  1191. u32 nritems;
  1192. u64 bytenr;
  1193. int ret;
  1194. u32 refs;
  1195. int level;
  1196. u32 blocksize;
  1197. nritems = btrfs_header_nritems(node);
  1198. level = btrfs_header_level(node);
  1199. for (i = 0; i < nritems; i++) {
  1200. bytenr = btrfs_node_blockptr(node, i);
  1201. blocksize = btrfs_level_size(root, level - 1);
  1202. ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
  1203. BUG_ON(ret);
  1204. if (refs != 1)
  1205. continue;
  1206. mutex_unlock(&root->fs_info->fs_mutex);
  1207. ret = readahead_tree_block(root, bytenr, blocksize);
  1208. cond_resched();
  1209. mutex_lock(&root->fs_info->fs_mutex);
  1210. if (ret)
  1211. break;
  1212. }
  1213. }
  1214. /*
  1215. * helper function for drop_snapshot, this walks down the tree dropping ref
  1216. * counts as it goes.
  1217. */
  1218. static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1219. *root, struct btrfs_path *path, int *level)
  1220. {
  1221. struct extent_buffer *next;
  1222. struct extent_buffer *cur;
  1223. u64 bytenr;
  1224. u32 blocksize;
  1225. int ret;
  1226. u32 refs;
  1227. WARN_ON(*level < 0);
  1228. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1229. ret = lookup_extent_ref(trans, root,
  1230. path->nodes[*level]->start,
  1231. path->nodes[*level]->len, &refs);
  1232. BUG_ON(ret);
  1233. if (refs > 1)
  1234. goto out;
  1235. /*
  1236. * walk down to the last node level and free all the leaves
  1237. */
  1238. while(*level >= 0) {
  1239. WARN_ON(*level < 0);
  1240. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1241. cur = path->nodes[*level];
  1242. if (*level > 0 && path->slots[*level] == 0)
  1243. reada_walk_down(root, cur);
  1244. if (btrfs_header_level(cur) != *level)
  1245. WARN_ON(1);
  1246. if (path->slots[*level] >=
  1247. btrfs_header_nritems(cur))
  1248. break;
  1249. if (*level == 0) {
  1250. ret = drop_leaf_ref(trans, root, cur);
  1251. BUG_ON(ret);
  1252. break;
  1253. }
  1254. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1255. blocksize = btrfs_level_size(root, *level - 1);
  1256. ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
  1257. BUG_ON(ret);
  1258. if (refs != 1) {
  1259. path->slots[*level]++;
  1260. ret = btrfs_free_extent(trans, root, bytenr,
  1261. blocksize, 1);
  1262. BUG_ON(ret);
  1263. continue;
  1264. }
  1265. next = btrfs_find_tree_block(root, bytenr, blocksize);
  1266. if (!next || !btrfs_buffer_uptodate(next)) {
  1267. free_extent_buffer(next);
  1268. mutex_unlock(&root->fs_info->fs_mutex);
  1269. next = read_tree_block(root, bytenr, blocksize);
  1270. mutex_lock(&root->fs_info->fs_mutex);
  1271. /* we dropped the lock, check one more time */
  1272. ret = lookup_extent_ref(trans, root, bytenr,
  1273. blocksize, &refs);
  1274. BUG_ON(ret);
  1275. if (refs != 1) {
  1276. path->slots[*level]++;
  1277. free_extent_buffer(next);
  1278. ret = btrfs_free_extent(trans, root,
  1279. bytenr, blocksize, 1);
  1280. BUG_ON(ret);
  1281. continue;
  1282. }
  1283. }
  1284. WARN_ON(*level <= 0);
  1285. if (path->nodes[*level-1])
  1286. free_extent_buffer(path->nodes[*level-1]);
  1287. path->nodes[*level-1] = next;
  1288. *level = btrfs_header_level(next);
  1289. path->slots[*level] = 0;
  1290. }
  1291. out:
  1292. WARN_ON(*level < 0);
  1293. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1294. ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
  1295. path->nodes[*level]->len, 1);
  1296. free_extent_buffer(path->nodes[*level]);
  1297. path->nodes[*level] = NULL;
  1298. *level += 1;
  1299. BUG_ON(ret);
  1300. return 0;
  1301. }
  1302. /*
  1303. * helper for dropping snapshots. This walks back up the tree in the path
  1304. * to find the first node higher up where we haven't yet gone through
  1305. * all the slots
  1306. */
  1307. static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1308. *root, struct btrfs_path *path, int *level)
  1309. {
  1310. int i;
  1311. int slot;
  1312. int ret;
  1313. struct btrfs_root_item *root_item = &root->root_item;
  1314. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1315. slot = path->slots[i];
  1316. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1317. struct extent_buffer *node;
  1318. struct btrfs_disk_key disk_key;
  1319. node = path->nodes[i];
  1320. path->slots[i]++;
  1321. *level = i;
  1322. WARN_ON(*level == 0);
  1323. btrfs_node_key(node, &disk_key, path->slots[i]);
  1324. memcpy(&root_item->drop_progress,
  1325. &disk_key, sizeof(disk_key));
  1326. root_item->drop_level = i;
  1327. return 0;
  1328. } else {
  1329. ret = btrfs_free_extent(trans, root,
  1330. path->nodes[*level]->start,
  1331. path->nodes[*level]->len, 1);
  1332. BUG_ON(ret);
  1333. free_extent_buffer(path->nodes[*level]);
  1334. path->nodes[*level] = NULL;
  1335. *level = i + 1;
  1336. }
  1337. }
  1338. return 1;
  1339. }
  1340. /*
  1341. * drop the reference count on the tree rooted at 'snap'. This traverses
  1342. * the tree freeing any blocks that have a ref count of zero after being
  1343. * decremented.
  1344. */
  1345. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  1346. *root)
  1347. {
  1348. int ret = 0;
  1349. int wret;
  1350. int level;
  1351. struct btrfs_path *path;
  1352. int i;
  1353. int orig_level;
  1354. struct btrfs_root_item *root_item = &root->root_item;
  1355. path = btrfs_alloc_path();
  1356. BUG_ON(!path);
  1357. level = btrfs_header_level(root->node);
  1358. orig_level = level;
  1359. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  1360. path->nodes[level] = root->node;
  1361. extent_buffer_get(root->node);
  1362. path->slots[level] = 0;
  1363. } else {
  1364. struct btrfs_key key;
  1365. struct btrfs_disk_key found_key;
  1366. struct extent_buffer *node;
  1367. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  1368. level = root_item->drop_level;
  1369. path->lowest_level = level;
  1370. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1371. if (wret < 0) {
  1372. ret = wret;
  1373. goto out;
  1374. }
  1375. node = path->nodes[level];
  1376. btrfs_node_key(node, &found_key, path->slots[level]);
  1377. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  1378. sizeof(found_key)));
  1379. }
  1380. while(1) {
  1381. wret = walk_down_tree(trans, root, path, &level);
  1382. if (wret > 0)
  1383. break;
  1384. if (wret < 0)
  1385. ret = wret;
  1386. wret = walk_up_tree(trans, root, path, &level);
  1387. if (wret > 0)
  1388. break;
  1389. if (wret < 0)
  1390. ret = wret;
  1391. ret = -EAGAIN;
  1392. break;
  1393. }
  1394. for (i = 0; i <= orig_level; i++) {
  1395. if (path->nodes[i]) {
  1396. free_extent_buffer(path->nodes[i]);
  1397. path->nodes[i] = NULL;
  1398. }
  1399. }
  1400. out:
  1401. btrfs_free_path(path);
  1402. return ret;
  1403. }
  1404. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  1405. {
  1406. u64 start;
  1407. u64 end;
  1408. u64 ptr;
  1409. int ret;
  1410. while(1) {
  1411. ret = find_first_extent_bit(&info->block_group_cache, 0,
  1412. &start, &end, (unsigned int)-1);
  1413. if (ret)
  1414. break;
  1415. ret = get_state_private(&info->block_group_cache, start, &ptr);
  1416. if (!ret)
  1417. kfree((void *)(unsigned long)ptr);
  1418. clear_extent_bits(&info->block_group_cache, start,
  1419. end, (unsigned int)-1, GFP_NOFS);
  1420. }
  1421. while(1) {
  1422. ret = find_first_extent_bit(&info->free_space_cache, 0,
  1423. &start, &end, EXTENT_DIRTY);
  1424. if (ret)
  1425. break;
  1426. clear_extent_dirty(&info->free_space_cache, start,
  1427. end, GFP_NOFS);
  1428. }
  1429. return 0;
  1430. }
  1431. int btrfs_read_block_groups(struct btrfs_root *root)
  1432. {
  1433. struct btrfs_path *path;
  1434. int ret;
  1435. int err = 0;
  1436. int bit;
  1437. struct btrfs_block_group_cache *cache;
  1438. struct btrfs_fs_info *info = root->fs_info;
  1439. struct extent_map_tree *block_group_cache;
  1440. struct btrfs_key key;
  1441. struct btrfs_key found_key;
  1442. struct extent_buffer *leaf;
  1443. block_group_cache = &info->block_group_cache;
  1444. root = info->extent_root;
  1445. key.objectid = 0;
  1446. key.offset = BTRFS_BLOCK_GROUP_SIZE;
  1447. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  1448. path = btrfs_alloc_path();
  1449. if (!path)
  1450. return -ENOMEM;
  1451. while(1) {
  1452. ret = btrfs_search_slot(NULL, info->extent_root,
  1453. &key, path, 0, 0);
  1454. if (ret != 0) {
  1455. err = ret;
  1456. break;
  1457. }
  1458. leaf = path->nodes[0];
  1459. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1460. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  1461. if (!cache) {
  1462. err = -1;
  1463. break;
  1464. }
  1465. read_extent_buffer(leaf, &cache->item,
  1466. btrfs_item_ptr_offset(leaf, path->slots[0]),
  1467. sizeof(cache->item));
  1468. memcpy(&cache->key, &found_key, sizeof(found_key));
  1469. cache->cached = 0;
  1470. cache->pinned = 0;
  1471. key.objectid = found_key.objectid + found_key.offset;
  1472. btrfs_release_path(root, path);
  1473. if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
  1474. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  1475. cache->data = BTRFS_BLOCK_GROUP_MIXED;
  1476. } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
  1477. bit = BLOCK_GROUP_DATA;
  1478. cache->data = BTRFS_BLOCK_GROUP_DATA;
  1479. } else {
  1480. bit = BLOCK_GROUP_METADATA;
  1481. cache->data = 0;
  1482. }
  1483. /* use EXTENT_LOCKED to prevent merging */
  1484. set_extent_bits(block_group_cache, found_key.objectid,
  1485. found_key.objectid + found_key.offset - 1,
  1486. bit | EXTENT_LOCKED, GFP_NOFS);
  1487. set_state_private(block_group_cache, found_key.objectid,
  1488. (unsigned long)cache);
  1489. if (key.objectid >=
  1490. btrfs_super_total_bytes(&info->super_copy))
  1491. break;
  1492. }
  1493. btrfs_free_path(path);
  1494. return 0;
  1495. }