extent-tree.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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. printk("cache block group %Lu\n", block_group->key.objectid);
  63. key.objectid = block_group->key.objectid;
  64. key.flags = 0;
  65. key.offset = 0;
  66. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  67. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  68. if (ret < 0)
  69. return ret;
  70. if (ret && path->slots[0] > 0)
  71. path->slots[0]--;
  72. limit = block_group->key.objectid + block_group->key.offset;
  73. reada_extent_leaves(root, path, limit);
  74. while(1) {
  75. leaf = btrfs_buffer_leaf(path->nodes[0]);
  76. slot = path->slots[0];
  77. if (slot >= btrfs_header_nritems(&leaf->header)) {
  78. reada_extent_leaves(root, path, limit);
  79. ret = btrfs_next_leaf(root, path);
  80. if (ret == 0) {
  81. continue;
  82. } else {
  83. if (found) {
  84. hole_size = block_group->key.objectid +
  85. block_group->key.offset - last;
  86. } else {
  87. last = block_group->key.objectid;
  88. hole_size = block_group->key.offset;
  89. }
  90. for (i = 0; i < hole_size; i++) {
  91. set_radix_bit(extent_radix,
  92. last + i);
  93. }
  94. break;
  95. }
  96. }
  97. btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
  98. if (key.objectid >= block_group->key.objectid +
  99. block_group->key.offset) {
  100. if (found) {
  101. hole_size = block_group->key.objectid +
  102. block_group->key.offset - last;
  103. } else {
  104. last = block_group->key.objectid;
  105. hole_size = block_group->key.offset;
  106. }
  107. for (i = 0; i < hole_size; i++) {
  108. set_radix_bit(extent_radix, last + i);
  109. }
  110. break;
  111. }
  112. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  113. if (!found) {
  114. last = key.objectid + key.offset;
  115. found = 1;
  116. } else {
  117. hole_size = key.objectid - last;
  118. for (i = 0; i < hole_size; i++) {
  119. set_radix_bit(extent_radix, last + i);
  120. }
  121. last = key.objectid + key.offset;
  122. }
  123. }
  124. path->slots[0]++;
  125. }
  126. block_group->cached = 1;
  127. btrfs_free_path(path);
  128. return 0;
  129. }
  130. static struct btrfs_block_group_cache *lookup_block_group(struct
  131. btrfs_fs_info *info,
  132. u64 blocknr)
  133. {
  134. struct btrfs_block_group_cache *block_group;
  135. int ret;
  136. ret = radix_tree_gang_lookup(&info->block_group_radix,
  137. (void **)&block_group,
  138. blocknr, 1);
  139. if (ret) {
  140. if (block_group->key.objectid <= blocknr && blocknr <=
  141. block_group->key.objectid + block_group->key.offset)
  142. return block_group;
  143. }
  144. ret = radix_tree_gang_lookup(&info->block_group_data_radix,
  145. (void **)&block_group,
  146. blocknr, 1);
  147. if (ret) {
  148. if (block_group->key.objectid <= blocknr && blocknr <=
  149. block_group->key.objectid + block_group->key.offset)
  150. return block_group;
  151. }
  152. return NULL;
  153. }
  154. static u64 leaf_range(struct btrfs_root *root)
  155. {
  156. u64 size = BTRFS_LEAF_DATA_SIZE(root);
  157. size = size / (sizeof(struct btrfs_extent_item) +
  158. sizeof(struct btrfs_item));
  159. return size;
  160. }
  161. static u64 find_search_start(struct btrfs_root *root,
  162. struct btrfs_block_group_cache **cache_ret,
  163. u64 search_start, int num)
  164. {
  165. unsigned long gang[8];
  166. int ret;
  167. struct btrfs_block_group_cache *cache = *cache_ret;
  168. u64 last = max(search_start, cache->key.objectid);
  169. if (cache->data)
  170. goto out;
  171. if (num > 1) {
  172. last = max(last, cache->last_prealloc);
  173. }
  174. again:
  175. cache_block_group(root, cache);
  176. while(1) {
  177. ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
  178. gang, last, ARRAY_SIZE(gang));
  179. if (!ret)
  180. goto out;
  181. last = gang[ret-1] + 1;
  182. if (num > 1) {
  183. if (ret != ARRAY_SIZE(gang)) {
  184. goto new_group;
  185. }
  186. if (gang[ret-1] - gang[0] > leaf_range(root)) {
  187. continue;
  188. }
  189. }
  190. if (gang[0] >= cache->key.objectid + cache->key.offset) {
  191. goto new_group;
  192. }
  193. return gang[0];
  194. }
  195. out:
  196. return max(cache->last_alloc, search_start);
  197. new_group:
  198. cache = lookup_block_group(root->fs_info, 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. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  208. struct btrfs_block_group_cache
  209. *hint, u64 search_start,
  210. int data, int owner)
  211. {
  212. struct btrfs_block_group_cache *cache[8];
  213. struct btrfs_block_group_cache *found_group = NULL;
  214. struct btrfs_fs_info *info = root->fs_info;
  215. struct radix_tree_root *radix;
  216. struct radix_tree_root *swap_radix;
  217. u64 used;
  218. u64 last = 0;
  219. u64 hint_last;
  220. int i;
  221. int ret;
  222. int full_search = 0;
  223. int factor = 8;
  224. int data_swap = 0;
  225. if (!owner)
  226. factor = 5;
  227. if (data) {
  228. radix = &info->block_group_data_radix;
  229. swap_radix = &info->block_group_radix;
  230. } else {
  231. radix = &info->block_group_radix;
  232. swap_radix = &info->block_group_data_radix;
  233. }
  234. if (search_start) {
  235. struct btrfs_block_group_cache *shint;
  236. shint = lookup_block_group(info, search_start);
  237. if (shint->data == data) {
  238. used = btrfs_block_group_used(&shint->item);
  239. if (used + shint->pinned <
  240. (shint->key.offset * factor) / 10) {
  241. return shint;
  242. }
  243. }
  244. }
  245. if (hint && hint->data == data) {
  246. used = btrfs_block_group_used(&hint->item);
  247. if (used + hint->pinned < (hint->key.offset * factor) / 10) {
  248. return hint;
  249. }
  250. if (used >= (hint->key.offset * 8) / 10) {
  251. radix_tree_tag_clear(radix,
  252. hint->key.objectid +
  253. hint->key.offset - 1,
  254. BTRFS_BLOCK_GROUP_AVAIL);
  255. }
  256. last = hint->key.offset * 3;
  257. if (hint->key.objectid >= last)
  258. last = max(search_start + hint->key.offset - 1,
  259. hint->key.objectid - last);
  260. else
  261. last = hint->key.objectid + hint->key.offset;
  262. hint_last = last;
  263. } else {
  264. if (hint)
  265. hint_last = max(hint->key.objectid, search_start);
  266. else
  267. hint_last = search_start;
  268. last = hint_last;
  269. }
  270. while(1) {
  271. ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
  272. last, ARRAY_SIZE(cache),
  273. BTRFS_BLOCK_GROUP_AVAIL);
  274. if (!ret)
  275. break;
  276. for (i = 0; i < ret; i++) {
  277. last = cache[i]->key.objectid +
  278. cache[i]->key.offset;
  279. used = btrfs_block_group_used(&cache[i]->item);
  280. if (used + cache[i]->pinned <
  281. (cache[i]->key.offset * factor) / 10) {
  282. found_group = cache[i];
  283. goto found;
  284. }
  285. if (used >= (cache[i]->key.offset * 8) / 10) {
  286. radix_tree_tag_clear(radix,
  287. cache[i]->key.objectid +
  288. cache[i]->key.offset - 1,
  289. BTRFS_BLOCK_GROUP_AVAIL);
  290. }
  291. }
  292. cond_resched();
  293. }
  294. last = hint_last;
  295. again:
  296. while(1) {
  297. ret = radix_tree_gang_lookup(radix, (void **)cache,
  298. last, ARRAY_SIZE(cache));
  299. if (!ret)
  300. break;
  301. for (i = 0; i < ret; i++) {
  302. last = cache[i]->key.objectid +
  303. cache[i]->key.offset;
  304. used = btrfs_block_group_used(&cache[i]->item);
  305. if (used + cache[i]->pinned < cache[i]->key.offset) {
  306. found_group = cache[i];
  307. goto found;
  308. }
  309. if (used >= cache[i]->key.offset) {
  310. radix_tree_tag_clear(radix,
  311. cache[i]->key.objectid +
  312. cache[i]->key.offset - 1,
  313. BTRFS_BLOCK_GROUP_AVAIL);
  314. }
  315. }
  316. cond_resched();
  317. }
  318. if (!full_search) {
  319. last = search_start;
  320. full_search = 1;
  321. goto again;
  322. }
  323. if (!data_swap) {
  324. struct radix_tree_root *tmp = radix;
  325. data_swap = 1;
  326. radix = swap_radix;
  327. swap_radix = tmp;
  328. last = search_start;
  329. goto again;
  330. }
  331. if (!found_group) {
  332. printk("find block group bailing to zero data %d\n", data);
  333. ret = radix_tree_gang_lookup(radix,
  334. (void **)&found_group, 0, 1);
  335. if (ret == 0) {
  336. ret = radix_tree_gang_lookup(swap_radix,
  337. (void **)&found_group,
  338. 0, 1);
  339. }
  340. BUG_ON(ret != 1);
  341. }
  342. found:
  343. return found_group;
  344. }
  345. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  346. struct btrfs_root *root,
  347. u64 blocknr, u64 num_blocks)
  348. {
  349. struct btrfs_path *path;
  350. int ret;
  351. struct btrfs_key key;
  352. struct btrfs_leaf *l;
  353. struct btrfs_extent_item *item;
  354. struct btrfs_key ins;
  355. u32 refs;
  356. find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
  357. &ins, 0);
  358. path = btrfs_alloc_path();
  359. BUG_ON(!path);
  360. btrfs_init_path(path);
  361. key.objectid = blocknr;
  362. key.flags = 0;
  363. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  364. key.offset = num_blocks;
  365. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  366. 0, 1);
  367. if (ret != 0) {
  368. printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
  369. BUG();
  370. }
  371. BUG_ON(ret != 0);
  372. l = btrfs_buffer_leaf(path->nodes[0]);
  373. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  374. refs = btrfs_extent_refs(item);
  375. btrfs_set_extent_refs(item, refs + 1);
  376. btrfs_mark_buffer_dirty(path->nodes[0]);
  377. btrfs_release_path(root->fs_info->extent_root, path);
  378. btrfs_free_path(path);
  379. finish_current_insert(trans, root->fs_info->extent_root);
  380. del_pending_extents(trans, root->fs_info->extent_root);
  381. return 0;
  382. }
  383. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  384. struct btrfs_root *root, u64 blocknr,
  385. u64 num_blocks, u32 *refs)
  386. {
  387. struct btrfs_path *path;
  388. int ret;
  389. struct btrfs_key key;
  390. struct btrfs_leaf *l;
  391. struct btrfs_extent_item *item;
  392. path = btrfs_alloc_path();
  393. btrfs_init_path(path);
  394. key.objectid = blocknr;
  395. key.offset = num_blocks;
  396. key.flags = 0;
  397. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  398. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  399. 0, 0);
  400. if (ret != 0)
  401. BUG();
  402. l = btrfs_buffer_leaf(path->nodes[0]);
  403. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  404. *refs = btrfs_extent_refs(item);
  405. btrfs_release_path(root->fs_info->extent_root, path);
  406. btrfs_free_path(path);
  407. return 0;
  408. }
  409. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  410. struct btrfs_root *root)
  411. {
  412. return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
  413. }
  414. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  415. struct buffer_head *buf)
  416. {
  417. u64 blocknr;
  418. struct btrfs_node *buf_node;
  419. struct btrfs_leaf *buf_leaf;
  420. struct btrfs_disk_key *key;
  421. struct btrfs_file_extent_item *fi;
  422. int i;
  423. int leaf;
  424. int ret;
  425. if (!root->ref_cows)
  426. return 0;
  427. buf_node = btrfs_buffer_node(buf);
  428. leaf = btrfs_is_leaf(buf_node);
  429. buf_leaf = btrfs_buffer_leaf(buf);
  430. for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
  431. if (leaf) {
  432. u64 disk_blocknr;
  433. key = &buf_leaf->items[i].key;
  434. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  435. continue;
  436. fi = btrfs_item_ptr(buf_leaf, i,
  437. struct btrfs_file_extent_item);
  438. if (btrfs_file_extent_type(fi) ==
  439. BTRFS_FILE_EXTENT_INLINE)
  440. continue;
  441. disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
  442. if (disk_blocknr == 0)
  443. continue;
  444. ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
  445. btrfs_file_extent_disk_num_blocks(fi));
  446. BUG_ON(ret);
  447. } else {
  448. blocknr = btrfs_node_blockptr(buf_node, i);
  449. ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
  450. BUG_ON(ret);
  451. }
  452. }
  453. return 0;
  454. }
  455. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  456. struct btrfs_root *root,
  457. struct btrfs_path *path,
  458. struct btrfs_block_group_cache *cache)
  459. {
  460. int ret;
  461. int pending_ret;
  462. struct btrfs_root *extent_root = root->fs_info->extent_root;
  463. struct btrfs_block_group_item *bi;
  464. struct btrfs_key ins;
  465. find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
  466. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  467. BUG_ON(ret);
  468. bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  469. struct btrfs_block_group_item);
  470. memcpy(bi, &cache->item, sizeof(*bi));
  471. mark_buffer_dirty(path->nodes[0]);
  472. btrfs_release_path(extent_root, path);
  473. finish_current_insert(trans, extent_root);
  474. pending_ret = del_pending_extents(trans, extent_root);
  475. if (ret)
  476. return ret;
  477. if (pending_ret)
  478. return pending_ret;
  479. if (cache->data)
  480. cache->last_alloc = cache->first_free;
  481. return 0;
  482. }
  483. static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
  484. struct btrfs_root *root,
  485. struct radix_tree_root *radix)
  486. {
  487. struct btrfs_block_group_cache *cache[8];
  488. int ret;
  489. int err = 0;
  490. int werr = 0;
  491. int i;
  492. struct btrfs_path *path;
  493. path = btrfs_alloc_path();
  494. if (!path)
  495. return -ENOMEM;
  496. while(1) {
  497. ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
  498. 0, ARRAY_SIZE(cache),
  499. BTRFS_BLOCK_GROUP_DIRTY);
  500. if (!ret)
  501. break;
  502. for (i = 0; i < ret; i++) {
  503. radix_tree_tag_clear(radix, cache[i]->key.objectid +
  504. cache[i]->key.offset - 1,
  505. BTRFS_BLOCK_GROUP_DIRTY);
  506. err = write_one_cache_group(trans, root,
  507. path, cache[i]);
  508. if (err)
  509. werr = err;
  510. }
  511. }
  512. btrfs_free_path(path);
  513. return werr;
  514. }
  515. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  516. struct btrfs_root *root)
  517. {
  518. int ret;
  519. int ret2;
  520. ret = write_dirty_block_radix(trans, root,
  521. &root->fs_info->block_group_radix);
  522. ret2 = write_dirty_block_radix(trans, root,
  523. &root->fs_info->block_group_data_radix);
  524. if (ret)
  525. return ret;
  526. if (ret2)
  527. return ret2;
  528. return 0;
  529. }
  530. static int update_block_group(struct btrfs_trans_handle *trans,
  531. struct btrfs_root *root,
  532. u64 blocknr, u64 num, int alloc, int mark_free,
  533. int data)
  534. {
  535. struct btrfs_block_group_cache *cache;
  536. struct btrfs_fs_info *info = root->fs_info;
  537. u64 total = num;
  538. u64 old_val;
  539. u64 block_in_group;
  540. u64 i;
  541. int ret;
  542. while(total) {
  543. cache = lookup_block_group(info, blocknr);
  544. if (!cache) {
  545. printk(KERN_CRIT "blocknr %Lu lookup failed\n",
  546. blocknr);
  547. return -1;
  548. }
  549. block_in_group = blocknr - cache->key.objectid;
  550. WARN_ON(block_in_group > cache->key.offset);
  551. radix_tree_tag_set(cache->radix, cache->key.objectid +
  552. cache->key.offset - 1,
  553. BTRFS_BLOCK_GROUP_DIRTY);
  554. old_val = btrfs_block_group_used(&cache->item);
  555. num = min(total, cache->key.offset - block_in_group);
  556. if (alloc) {
  557. if (blocknr > cache->last_alloc)
  558. cache->last_alloc = blocknr;
  559. if (!cache->data) {
  560. for (i = 0; i < num; i++) {
  561. clear_radix_bit(&info->extent_map_radix,
  562. blocknr + i);
  563. }
  564. }
  565. if (cache->data != data &&
  566. old_val < cache->key.offset / 2) {
  567. printk("changing block group %Lu from %d to %d\n", cache->key.objectid, cache->data, data);
  568. cache->data = data;
  569. radix_tree_delete(cache->radix,
  570. cache->key.objectid +
  571. cache->key.offset - 1);
  572. if (data) {
  573. cache->radix =
  574. &info->block_group_data_radix;
  575. cache->item.flags |=
  576. BTRFS_BLOCK_GROUP_DATA;
  577. } else {
  578. cache->radix = &info->block_group_radix;
  579. cache->item.flags &=
  580. ~BTRFS_BLOCK_GROUP_DATA;
  581. }
  582. ret = radix_tree_insert(cache->radix,
  583. cache->key.objectid +
  584. cache->key.offset - 1,
  585. (void *)cache);
  586. }
  587. old_val += num;
  588. } else {
  589. old_val -= num;
  590. if (blocknr < cache->first_free)
  591. cache->first_free = blocknr;
  592. if (!cache->data && mark_free) {
  593. for (i = 0; i < num; i++) {
  594. set_radix_bit(&info->extent_map_radix,
  595. blocknr + i);
  596. }
  597. }
  598. if (old_val < cache->key.offset / 2 &&
  599. old_val + num >= cache->key.offset / 2) {
  600. printk("group %Lu now available\n", cache->key.objectid);
  601. radix_tree_tag_set(cache->radix,
  602. cache->key.objectid +
  603. cache->key.offset - 1,
  604. BTRFS_BLOCK_GROUP_AVAIL);
  605. }
  606. }
  607. btrfs_set_block_group_used(&cache->item, old_val);
  608. total -= num;
  609. blocknr += num;
  610. }
  611. return 0;
  612. }
  613. static int try_remove_page(struct address_space *mapping, unsigned long index)
  614. {
  615. int ret;
  616. ret = invalidate_mapping_pages(mapping, index, index);
  617. return ret;
  618. }
  619. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  620. btrfs_root *root)
  621. {
  622. unsigned long gang[8];
  623. struct inode *btree_inode = root->fs_info->btree_inode;
  624. struct btrfs_block_group_cache *block_group;
  625. u64 first = 0;
  626. int ret;
  627. int i;
  628. struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
  629. struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
  630. while(1) {
  631. ret = find_first_radix_bit(pinned_radix, gang, 0,
  632. ARRAY_SIZE(gang));
  633. if (!ret)
  634. break;
  635. if (!first)
  636. first = gang[0];
  637. for (i = 0; i < ret; i++) {
  638. clear_radix_bit(pinned_radix, gang[i]);
  639. block_group = lookup_block_group(root->fs_info,
  640. gang[i]);
  641. if (block_group) {
  642. WARN_ON(block_group->pinned == 0);
  643. block_group->pinned--;
  644. if (gang[i] < block_group->last_alloc)
  645. block_group->last_alloc = gang[i];
  646. if (gang[i] < block_group->last_prealloc)
  647. block_group->last_prealloc = gang[i];
  648. if (!block_group->data)
  649. set_radix_bit(extent_radix, gang[i]);
  650. }
  651. try_remove_page(btree_inode->i_mapping,
  652. gang[i] << (PAGE_CACHE_SHIFT -
  653. btree_inode->i_blkbits));
  654. }
  655. }
  656. return 0;
  657. }
  658. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  659. btrfs_root *extent_root)
  660. {
  661. struct btrfs_key ins;
  662. struct btrfs_extent_item extent_item;
  663. int i;
  664. int ret;
  665. u64 super_blocks_used;
  666. struct btrfs_fs_info *info = extent_root->fs_info;
  667. btrfs_set_extent_refs(&extent_item, 1);
  668. ins.offset = 1;
  669. ins.flags = 0;
  670. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  671. btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
  672. for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
  673. ins.objectid = extent_root->fs_info->extent_tree_insert[i];
  674. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  675. btrfs_set_super_blocks_used(info->disk_super,
  676. super_blocks_used + 1);
  677. ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
  678. sizeof(extent_item));
  679. BUG_ON(ret);
  680. }
  681. extent_root->fs_info->extent_tree_insert_nr = 0;
  682. extent_root->fs_info->extent_tree_prealloc_nr = 0;
  683. return 0;
  684. }
  685. static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
  686. {
  687. int err;
  688. struct btrfs_header *header;
  689. struct buffer_head *bh;
  690. if (!pending) {
  691. bh = btrfs_find_tree_block(root, blocknr);
  692. if (bh) {
  693. if (buffer_uptodate(bh)) {
  694. u64 transid =
  695. root->fs_info->running_transaction->transid;
  696. header = btrfs_buffer_header(bh);
  697. if (btrfs_header_generation(header) ==
  698. transid) {
  699. btrfs_block_release(root, bh);
  700. return 0;
  701. }
  702. }
  703. btrfs_block_release(root, bh);
  704. }
  705. err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
  706. if (!err) {
  707. struct btrfs_block_group_cache *cache;
  708. cache = lookup_block_group(root->fs_info, blocknr);
  709. if (cache)
  710. cache->pinned++;
  711. }
  712. } else {
  713. err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
  714. }
  715. BUG_ON(err < 0);
  716. return 0;
  717. }
  718. /*
  719. * remove an extent from the root, returns 0 on success
  720. */
  721. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  722. *root, u64 blocknr, u64 num_blocks, int pin,
  723. int mark_free)
  724. {
  725. struct btrfs_path *path;
  726. struct btrfs_key key;
  727. struct btrfs_fs_info *info = root->fs_info;
  728. struct btrfs_root *extent_root = info->extent_root;
  729. int ret;
  730. struct btrfs_extent_item *ei;
  731. struct btrfs_key ins;
  732. u32 refs;
  733. key.objectid = blocknr;
  734. key.flags = 0;
  735. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  736. key.offset = num_blocks;
  737. find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
  738. path = btrfs_alloc_path();
  739. BUG_ON(!path);
  740. btrfs_init_path(path);
  741. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  742. if (ret) {
  743. printk("failed to find %Lu\n", key.objectid);
  744. btrfs_print_tree(extent_root, extent_root->node);
  745. printk("failed to find %Lu\n", key.objectid);
  746. BUG();
  747. }
  748. ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  749. struct btrfs_extent_item);
  750. BUG_ON(ei->refs == 0);
  751. refs = btrfs_extent_refs(ei) - 1;
  752. btrfs_set_extent_refs(ei, refs);
  753. btrfs_mark_buffer_dirty(path->nodes[0]);
  754. if (refs == 0) {
  755. u64 super_blocks_used;
  756. if (pin) {
  757. ret = pin_down_block(root, blocknr, 0);
  758. BUG_ON(ret);
  759. }
  760. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  761. btrfs_set_super_blocks_used(info->disk_super,
  762. super_blocks_used - num_blocks);
  763. ret = btrfs_del_item(trans, extent_root, path);
  764. if (ret)
  765. BUG();
  766. ret = update_block_group(trans, root, blocknr, num_blocks, 0,
  767. mark_free, 0);
  768. BUG_ON(ret);
  769. }
  770. btrfs_free_path(path);
  771. finish_current_insert(trans, extent_root);
  772. return ret;
  773. }
  774. /*
  775. * find all the blocks marked as pending in the radix tree and remove
  776. * them from the extent map
  777. */
  778. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  779. btrfs_root *extent_root)
  780. {
  781. int ret;
  782. int wret;
  783. int err = 0;
  784. unsigned long gang[4];
  785. int i;
  786. struct radix_tree_root *pending_radix;
  787. struct radix_tree_root *pinned_radix;
  788. struct btrfs_block_group_cache *cache;
  789. pending_radix = &extent_root->fs_info->pending_del_radix;
  790. pinned_radix = &extent_root->fs_info->pinned_radix;
  791. while(1) {
  792. ret = find_first_radix_bit(pending_radix, gang, 0,
  793. ARRAY_SIZE(gang));
  794. if (!ret)
  795. break;
  796. for (i = 0; i < ret; i++) {
  797. wret = set_radix_bit(pinned_radix, gang[i]);
  798. if (wret == 0) {
  799. cache = 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 = 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 / 2;
  934. else
  935. limit = search_start +
  936. block_group->key.offset / 2;
  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 = 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 = 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 = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
  1437. u64 used;
  1438. root = info->extent_root;
  1439. key.objectid = 0;
  1440. key.offset = group_size_blocks;
  1441. key.flags = 0;
  1442. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  1443. path = btrfs_alloc_path();
  1444. if (!path)
  1445. return -ENOMEM;
  1446. while(1) {
  1447. ret = btrfs_search_slot(NULL, info->extent_root,
  1448. &key, path, 0, 0);
  1449. if (ret != 0) {
  1450. err = ret;
  1451. break;
  1452. }
  1453. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1454. btrfs_disk_key_to_cpu(&found_key,
  1455. &leaf->items[path->slots[0]].key);
  1456. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  1457. if (!cache) {
  1458. err = -1;
  1459. break;
  1460. }
  1461. bi = btrfs_item_ptr(leaf, path->slots[0],
  1462. struct btrfs_block_group_item);
  1463. if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
  1464. radix = &info->block_group_data_radix;
  1465. cache->data = 1;
  1466. } else {
  1467. radix = &info->block_group_radix;
  1468. cache->data = 0;
  1469. }
  1470. memcpy(&cache->item, bi, sizeof(*bi));
  1471. memcpy(&cache->key, &found_key, sizeof(found_key));
  1472. cache->last_alloc = cache->key.objectid;
  1473. cache->first_free = cache->key.objectid;
  1474. cache->last_prealloc = cache->key.objectid;
  1475. cache->pinned = 0;
  1476. cache->cached = 0;
  1477. cache->radix = radix;
  1478. key.objectid = found_key.objectid + found_key.offset;
  1479. btrfs_release_path(root, path);
  1480. ret = radix_tree_insert(radix, found_key.objectid +
  1481. found_key.offset - 1,
  1482. (void *)cache);
  1483. BUG_ON(ret);
  1484. used = btrfs_block_group_used(bi);
  1485. if (used < (key.offset * 8) / 10) {
  1486. radix_tree_tag_set(radix, found_key.objectid +
  1487. found_key.offset - 1,
  1488. BTRFS_BLOCK_GROUP_AVAIL);
  1489. }
  1490. if (key.objectid >=
  1491. btrfs_super_total_blocks(info->disk_super))
  1492. break;
  1493. }
  1494. btrfs_free_path(path);
  1495. return 0;
  1496. }