extent-tree.c 41 KB

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