extent-tree.c 41 KB

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