extent-tree.c 40 KB

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