extent-tree.c 44 KB

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