extent-tree.c 41 KB

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