extent-tree.c 41 KB

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