extent-tree.c 39 KB

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