extent-tree.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  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 <linux/crc32c.h>
  20. #include "hash.h"
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "print-tree.h"
  24. #include "transaction.h"
  25. #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
  26. #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
  27. #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
  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 int cache_block_group(struct btrfs_root *root,
  33. struct btrfs_block_group_cache *block_group)
  34. {
  35. struct btrfs_path *path;
  36. int ret;
  37. struct btrfs_key key;
  38. struct extent_buffer *leaf;
  39. struct extent_map_tree *free_space_cache;
  40. int slot;
  41. u64 last = 0;
  42. u64 hole_size;
  43. u64 first_free;
  44. int found = 0;
  45. if (!block_group)
  46. return 0;
  47. root = root->fs_info->extent_root;
  48. free_space_cache = &root->fs_info->free_space_cache;
  49. if (block_group->cached)
  50. return 0;
  51. path = btrfs_alloc_path();
  52. if (!path)
  53. return -ENOMEM;
  54. path->reada = 2;
  55. first_free = block_group->key.objectid;
  56. key.objectid = block_group->key.objectid;
  57. key.offset = 0;
  58. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  59. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  60. if (ret < 0)
  61. return ret;
  62. if (ret && path->slots[0] > 0)
  63. path->slots[0]--;
  64. while(1) {
  65. leaf = path->nodes[0];
  66. slot = path->slots[0];
  67. if (slot >= btrfs_header_nritems(leaf)) {
  68. ret = btrfs_next_leaf(root, path);
  69. if (ret < 0)
  70. goto err;
  71. if (ret == 0) {
  72. continue;
  73. } else {
  74. break;
  75. }
  76. }
  77. btrfs_item_key_to_cpu(leaf, &key, slot);
  78. if (key.objectid < block_group->key.objectid) {
  79. if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
  80. key.objectid + key.offset > first_free)
  81. first_free = key.objectid + key.offset;
  82. goto next;
  83. }
  84. if (key.objectid >= block_group->key.objectid +
  85. block_group->key.offset) {
  86. break;
  87. }
  88. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  89. if (!found) {
  90. last = first_free;
  91. found = 1;
  92. }
  93. if (key.objectid > last) {
  94. hole_size = key.objectid - last;
  95. set_extent_dirty(free_space_cache, last,
  96. last + hole_size - 1,
  97. GFP_NOFS);
  98. }
  99. last = key.objectid + key.offset;
  100. }
  101. next:
  102. path->slots[0]++;
  103. }
  104. if (!found)
  105. last = first_free;
  106. if (block_group->key.objectid +
  107. block_group->key.offset > last) {
  108. hole_size = block_group->key.objectid +
  109. block_group->key.offset - last;
  110. set_extent_dirty(free_space_cache, last,
  111. last + hole_size - 1, GFP_NOFS);
  112. }
  113. block_group->cached = 1;
  114. err:
  115. btrfs_free_path(path);
  116. return 0;
  117. }
  118. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  119. btrfs_fs_info *info,
  120. u64 bytenr)
  121. {
  122. struct extent_map_tree *block_group_cache;
  123. struct btrfs_block_group_cache *block_group = NULL;
  124. u64 ptr;
  125. u64 start;
  126. u64 end;
  127. int ret;
  128. block_group_cache = &info->block_group_cache;
  129. ret = find_first_extent_bit(block_group_cache,
  130. bytenr, &start, &end,
  131. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
  132. if (ret) {
  133. return NULL;
  134. }
  135. ret = get_state_private(block_group_cache, start, &ptr);
  136. if (ret)
  137. return NULL;
  138. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  139. if (block_group->key.objectid <= bytenr && bytenr <
  140. block_group->key.objectid + block_group->key.offset)
  141. return block_group;
  142. return NULL;
  143. }
  144. static u64 find_search_start(struct btrfs_root *root,
  145. struct btrfs_block_group_cache **cache_ret,
  146. u64 search_start, int num,
  147. int data, int full_scan)
  148. {
  149. int ret;
  150. struct btrfs_block_group_cache *cache = *cache_ret;
  151. u64 last;
  152. u64 start = 0;
  153. u64 end = 0;
  154. u64 cache_miss = 0;
  155. int wrapped = 0;
  156. if (!cache) {
  157. goto out;
  158. }
  159. again:
  160. ret = cache_block_group(root, cache);
  161. if (ret)
  162. goto out;
  163. last = max(search_start, cache->key.objectid);
  164. while(1) {
  165. ret = find_first_extent_bit(&root->fs_info->free_space_cache,
  166. last, &start, &end, EXTENT_DIRTY);
  167. if (ret) {
  168. if (!cache_miss)
  169. cache_miss = last;
  170. goto new_group;
  171. }
  172. start = max(last, start);
  173. last = end + 1;
  174. if (last - start < num) {
  175. if (last == cache->key.objectid + cache->key.offset)
  176. cache_miss = start;
  177. continue;
  178. }
  179. if (data != BTRFS_BLOCK_GROUP_MIXED &&
  180. start + num > cache->key.objectid + cache->key.offset)
  181. goto new_group;
  182. return start;
  183. }
  184. out:
  185. cache = btrfs_lookup_block_group(root->fs_info, search_start);
  186. if (!cache) {
  187. printk("Unable to find block group for %Lu\n",
  188. search_start);
  189. WARN_ON(1);
  190. return search_start;
  191. }
  192. return search_start;
  193. new_group:
  194. last = cache->key.objectid + cache->key.offset;
  195. wrapped:
  196. cache = btrfs_lookup_block_group(root->fs_info, last);
  197. if (!cache) {
  198. no_cache:
  199. if (!wrapped) {
  200. wrapped = 1;
  201. last = search_start;
  202. data = BTRFS_BLOCK_GROUP_MIXED;
  203. goto wrapped;
  204. }
  205. goto out;
  206. }
  207. if (cache_miss && !cache->cached) {
  208. cache_block_group(root, cache);
  209. last = cache_miss;
  210. cache = btrfs_lookup_block_group(root->fs_info, last);
  211. }
  212. cache = btrfs_find_block_group(root, cache, last, data, 0);
  213. if (!cache)
  214. goto no_cache;
  215. *cache_ret = cache;
  216. cache_miss = 0;
  217. goto again;
  218. }
  219. static u64 div_factor(u64 num, int factor)
  220. {
  221. if (factor == 10)
  222. return num;
  223. num *= factor;
  224. do_div(num, 10);
  225. return num;
  226. }
  227. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  228. struct btrfs_block_group_cache
  229. *hint, u64 search_start,
  230. int data, int owner)
  231. {
  232. struct btrfs_block_group_cache *cache;
  233. struct extent_map_tree *block_group_cache;
  234. struct btrfs_block_group_cache *found_group = NULL;
  235. struct btrfs_fs_info *info = root->fs_info;
  236. u64 used;
  237. u64 last = 0;
  238. u64 hint_last;
  239. u64 start;
  240. u64 end;
  241. u64 free_check;
  242. u64 ptr;
  243. int bit;
  244. int ret;
  245. int full_search = 0;
  246. int factor = 8;
  247. int data_swap = 0;
  248. block_group_cache = &info->block_group_cache;
  249. if (!owner)
  250. factor = 8;
  251. if (data == BTRFS_BLOCK_GROUP_MIXED) {
  252. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  253. factor = 10;
  254. } else if (data)
  255. bit = BLOCK_GROUP_DATA;
  256. else
  257. bit = BLOCK_GROUP_METADATA;
  258. if (search_start) {
  259. struct btrfs_block_group_cache *shint;
  260. shint = btrfs_lookup_block_group(info, search_start);
  261. if (shint && (shint->data == data ||
  262. shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
  263. used = btrfs_block_group_used(&shint->item);
  264. if (used + shint->pinned <
  265. div_factor(shint->key.offset, factor)) {
  266. return shint;
  267. }
  268. }
  269. }
  270. if (hint && (hint->data == data ||
  271. hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
  272. used = btrfs_block_group_used(&hint->item);
  273. if (used + hint->pinned <
  274. div_factor(hint->key.offset, factor)) {
  275. return hint;
  276. }
  277. last = hint->key.objectid + hint->key.offset;
  278. hint_last = last;
  279. } else {
  280. if (hint)
  281. hint_last = max(hint->key.objectid, search_start);
  282. else
  283. hint_last = search_start;
  284. last = hint_last;
  285. }
  286. again:
  287. while(1) {
  288. ret = find_first_extent_bit(block_group_cache, last,
  289. &start, &end, bit);
  290. if (ret)
  291. break;
  292. ret = get_state_private(block_group_cache, start, &ptr);
  293. if (ret)
  294. break;
  295. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  296. last = cache->key.objectid + cache->key.offset;
  297. used = btrfs_block_group_used(&cache->item);
  298. if (full_search)
  299. free_check = cache->key.offset;
  300. else
  301. free_check = div_factor(cache->key.offset, factor);
  302. if (used + cache->pinned < free_check) {
  303. found_group = cache;
  304. goto found;
  305. }
  306. cond_resched();
  307. }
  308. if (!full_search) {
  309. last = search_start;
  310. full_search = 1;
  311. goto again;
  312. }
  313. if (!data_swap) {
  314. data_swap = 1;
  315. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  316. last = search_start;
  317. goto again;
  318. }
  319. found:
  320. return found_group;
  321. }
  322. static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
  323. u64 owner, u64 owner_offset)
  324. {
  325. u32 high_crc = ~(u32)0;
  326. u32 low_crc = ~(u32)0;
  327. __le64 lenum;
  328. lenum = cpu_to_le64(root_objectid);
  329. high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
  330. lenum = cpu_to_le64(ref_generation);
  331. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  332. #if 0
  333. lenum = cpu_to_le64(owner);
  334. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  335. lenum = cpu_to_le64(owner_offset);
  336. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  337. #endif
  338. return ((u64)high_crc << 32) | (u64)low_crc;
  339. }
  340. static int match_extent_ref(struct extent_buffer *leaf,
  341. struct btrfs_extent_ref *disk_ref,
  342. struct btrfs_extent_ref *cpu_ref)
  343. {
  344. int ret;
  345. int len;
  346. if (cpu_ref->objectid)
  347. len = sizeof(*cpu_ref);
  348. else
  349. len = 2 * sizeof(u64);
  350. ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
  351. len);
  352. return ret == 0;
  353. }
  354. static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  355. struct btrfs_root *root,
  356. struct btrfs_path *path, u64 bytenr,
  357. u64 root_objectid, u64 ref_generation,
  358. u64 owner, u64 owner_offset, int del)
  359. {
  360. u64 hash;
  361. struct btrfs_key key;
  362. struct btrfs_key found_key;
  363. struct btrfs_extent_ref ref;
  364. struct extent_buffer *leaf;
  365. struct btrfs_extent_ref *disk_ref;
  366. int ret;
  367. int ret2;
  368. btrfs_set_stack_ref_root(&ref, root_objectid);
  369. btrfs_set_stack_ref_generation(&ref, ref_generation);
  370. btrfs_set_stack_ref_objectid(&ref, owner);
  371. btrfs_set_stack_ref_offset(&ref, owner_offset);
  372. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  373. owner_offset);
  374. key.offset = hash;
  375. key.objectid = bytenr;
  376. key.type = BTRFS_EXTENT_REF_KEY;
  377. while (1) {
  378. ret = btrfs_search_slot(trans, root, &key, path,
  379. del ? -1 : 0, del);
  380. if (ret < 0)
  381. goto out;
  382. leaf = path->nodes[0];
  383. if (ret != 0) {
  384. u32 nritems = btrfs_header_nritems(leaf);
  385. if (path->slots[0] >= nritems) {
  386. ret2 = btrfs_next_leaf(root, path);
  387. if (ret2)
  388. goto out;
  389. leaf = path->nodes[0];
  390. }
  391. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  392. if (found_key.objectid != bytenr ||
  393. found_key.type != BTRFS_EXTENT_REF_KEY)
  394. goto out;
  395. key.offset = found_key.offset;
  396. if (del) {
  397. btrfs_release_path(root, path);
  398. continue;
  399. }
  400. }
  401. disk_ref = btrfs_item_ptr(path->nodes[0],
  402. path->slots[0],
  403. struct btrfs_extent_ref);
  404. if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
  405. ret = 0;
  406. goto out;
  407. }
  408. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  409. key.offset = found_key.offset + 1;
  410. btrfs_release_path(root, path);
  411. }
  412. out:
  413. return ret;
  414. }
  415. /*
  416. * Back reference rules. Back refs have three main goals:
  417. *
  418. * 1) differentiate between all holders of references to an extent so that
  419. * when a reference is dropped we can make sure it was a valid reference
  420. * before freeing the extent.
  421. *
  422. * 2) Provide enough information to quickly find the holders of an extent
  423. * if we notice a given block is corrupted or bad.
  424. *
  425. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  426. * maintenance. This is actually the same as #2, but with a slightly
  427. * different use case.
  428. *
  429. * File extents can be referenced by:
  430. *
  431. * - multiple snapshots, subvolumes, or different generations in one subvol
  432. * - different files inside a single subvolume (in theory, not implemented yet)
  433. * - different offsets inside a file (bookend extents in file.c)
  434. *
  435. * The extent ref structure has fields for:
  436. *
  437. * - Objectid of the subvolume root
  438. * - Generation number of the tree holding the reference
  439. * - objectid of the file holding the reference
  440. * - offset in the file corresponding to the key holding the reference
  441. *
  442. * When a file extent is allocated the fields are filled in:
  443. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  444. *
  445. * When a leaf is cow'd new references are added for every file extent found
  446. * in the leaf. It looks the same as the create case, but trans->transid
  447. * will be different when the block is cow'd.
  448. *
  449. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  450. *
  451. * When a file extent is removed either during snapshot deletion or file
  452. * truncation, the corresponding back reference is found
  453. * by searching for:
  454. *
  455. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  456. * inode objectid, offset in file)
  457. *
  458. * Btree extents can be referenced by:
  459. *
  460. * - Different subvolumes
  461. * - Different generations of the same subvolume
  462. *
  463. * Storing sufficient information for a full reverse mapping of a btree
  464. * block would require storing the lowest key of the block in the backref,
  465. * and it would require updating that lowest key either before write out or
  466. * every time it changed. Instead, the objectid of the lowest key is stored
  467. * along with the level of the tree block. This provides a hint
  468. * about where in the btree the block can be found. Searches through the
  469. * btree only need to look for a pointer to that block, so they stop one
  470. * level higher than the level recorded in the backref.
  471. *
  472. * Some btrees do not do reference counting on their extents. These
  473. * include the extent tree and the tree of tree roots. Backrefs for these
  474. * trees always have a generation of zero.
  475. *
  476. * When a tree block is created, back references are inserted:
  477. *
  478. * (root->root_key.objectid, trans->transid or zero, lowest_key_objectid, level)
  479. *
  480. * When a tree block is cow'd in a reference counted root,
  481. * new back references are added for all the blocks it points to.
  482. * These are of the form (trans->transid will have increased since creation):
  483. *
  484. * (root->root_key.objectid, trans->transid, lowest_key_objectid, level)
  485. *
  486. * Because the lowest_key_objectid and the level are just hints
  487. * they are not used when backrefs are deleted. When a backref is deleted:
  488. *
  489. * if backref was for a tree root:
  490. * root_objectid = root->root_key.objectid
  491. * else
  492. * root_objectid = btrfs_header_owner(parent)
  493. *
  494. * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
  495. *
  496. * Back Reference Key hashing:
  497. *
  498. * Back references have four fields, each 64 bits long. Unfortunately,
  499. * This is hashed into a single 64 bit number and placed into the key offset.
  500. * The key objectid corresponds to the first byte in the extent, and the
  501. * key type is set to BTRFS_EXTENT_REF_KEY
  502. */
  503. int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
  504. struct btrfs_root *root,
  505. struct btrfs_path *path, u64 bytenr,
  506. u64 root_objectid, u64 ref_generation,
  507. u64 owner, u64 owner_offset)
  508. {
  509. u64 hash;
  510. struct btrfs_key key;
  511. struct btrfs_extent_ref ref;
  512. struct btrfs_extent_ref *disk_ref;
  513. int ret;
  514. btrfs_set_stack_ref_root(&ref, root_objectid);
  515. btrfs_set_stack_ref_generation(&ref, ref_generation);
  516. btrfs_set_stack_ref_objectid(&ref, owner);
  517. btrfs_set_stack_ref_offset(&ref, owner_offset);
  518. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  519. owner_offset);
  520. key.offset = hash;
  521. key.objectid = bytenr;
  522. key.type = BTRFS_EXTENT_REF_KEY;
  523. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
  524. while (ret == -EEXIST) {
  525. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  526. struct btrfs_extent_ref);
  527. if (match_extent_ref(path->nodes[0], disk_ref, &ref))
  528. goto out;
  529. key.offset++;
  530. btrfs_release_path(root, path);
  531. ret = btrfs_insert_empty_item(trans, root, path, &key,
  532. sizeof(ref));
  533. }
  534. if (ret)
  535. goto out;
  536. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  537. struct btrfs_extent_ref);
  538. write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
  539. sizeof(ref));
  540. btrfs_mark_buffer_dirty(path->nodes[0]);
  541. out:
  542. btrfs_release_path(root, path);
  543. return ret;
  544. }
  545. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  546. struct btrfs_root *root,
  547. u64 bytenr, u64 num_bytes,
  548. u64 root_objectid, u64 ref_generation,
  549. u64 owner, u64 owner_offset)
  550. {
  551. struct btrfs_path *path;
  552. int ret;
  553. struct btrfs_key key;
  554. struct extent_buffer *l;
  555. struct btrfs_extent_item *item;
  556. u32 refs;
  557. WARN_ON(num_bytes < root->sectorsize);
  558. path = btrfs_alloc_path();
  559. if (!path)
  560. return -ENOMEM;
  561. key.objectid = bytenr;
  562. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  563. key.offset = num_bytes;
  564. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  565. 0, 1);
  566. if (ret < 0)
  567. return ret;
  568. if (ret != 0) {
  569. BUG();
  570. }
  571. BUG_ON(ret != 0);
  572. l = path->nodes[0];
  573. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  574. refs = btrfs_extent_refs(l, item);
  575. btrfs_set_extent_refs(l, item, refs + 1);
  576. btrfs_mark_buffer_dirty(path->nodes[0]);
  577. btrfs_release_path(root->fs_info->extent_root, path);
  578. ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
  579. path, bytenr, root_objectid,
  580. ref_generation, owner, owner_offset);
  581. BUG_ON(ret);
  582. finish_current_insert(trans, root->fs_info->extent_root);
  583. del_pending_extents(trans, root->fs_info->extent_root);
  584. btrfs_free_path(path);
  585. return 0;
  586. }
  587. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  588. struct btrfs_root *root)
  589. {
  590. finish_current_insert(trans, root->fs_info->extent_root);
  591. del_pending_extents(trans, root->fs_info->extent_root);
  592. return 0;
  593. }
  594. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  595. struct btrfs_root *root, u64 bytenr,
  596. u64 num_bytes, u32 *refs)
  597. {
  598. struct btrfs_path *path;
  599. int ret;
  600. struct btrfs_key key;
  601. struct extent_buffer *l;
  602. struct btrfs_extent_item *item;
  603. WARN_ON(num_bytes < root->sectorsize);
  604. path = btrfs_alloc_path();
  605. key.objectid = bytenr;
  606. key.offset = num_bytes;
  607. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  608. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  609. 0, 0);
  610. if (ret < 0)
  611. goto out;
  612. if (ret != 0) {
  613. btrfs_print_leaf(root, path->nodes[0]);
  614. printk("failed to find block number %Lu\n", bytenr);
  615. BUG();
  616. }
  617. l = path->nodes[0];
  618. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  619. *refs = btrfs_extent_refs(l, item);
  620. out:
  621. btrfs_free_path(path);
  622. return 0;
  623. }
  624. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  625. struct btrfs_root *root, u64 owner_objectid)
  626. {
  627. u64 generation;
  628. u64 key_objectid;
  629. u64 level;
  630. u32 nritems;
  631. struct btrfs_disk_key disk_key;
  632. level = btrfs_header_level(root->node);
  633. generation = trans->transid;
  634. nritems = btrfs_header_nritems(root->node);
  635. if (nritems > 0) {
  636. if (level == 0)
  637. btrfs_item_key(root->node, &disk_key, 0);
  638. else
  639. btrfs_node_key(root->node, &disk_key, 0);
  640. key_objectid = btrfs_disk_key_objectid(&disk_key);
  641. } else {
  642. key_objectid = 0;
  643. }
  644. return btrfs_inc_extent_ref(trans, root, root->node->start,
  645. root->node->len, owner_objectid,
  646. generation, 0, 0);
  647. }
  648. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  649. struct extent_buffer *buf)
  650. {
  651. u64 bytenr;
  652. u32 nritems;
  653. struct btrfs_key key;
  654. struct btrfs_file_extent_item *fi;
  655. int i;
  656. int level;
  657. int ret;
  658. int faili;
  659. if (!root->ref_cows)
  660. return 0;
  661. level = btrfs_header_level(buf);
  662. nritems = btrfs_header_nritems(buf);
  663. for (i = 0; i < nritems; i++) {
  664. if (level == 0) {
  665. u64 disk_bytenr;
  666. btrfs_item_key_to_cpu(buf, &key, i);
  667. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  668. continue;
  669. fi = btrfs_item_ptr(buf, i,
  670. struct btrfs_file_extent_item);
  671. if (btrfs_file_extent_type(buf, fi) ==
  672. BTRFS_FILE_EXTENT_INLINE)
  673. continue;
  674. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  675. if (disk_bytenr == 0)
  676. continue;
  677. ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
  678. btrfs_file_extent_disk_num_bytes(buf, fi),
  679. root->root_key.objectid, trans->transid,
  680. key.objectid, key.offset);
  681. if (ret) {
  682. faili = i;
  683. goto fail;
  684. }
  685. } else {
  686. bytenr = btrfs_node_blockptr(buf, i);
  687. ret = btrfs_inc_extent_ref(trans, root, bytenr,
  688. btrfs_level_size(root, level - 1),
  689. root->root_key.objectid,
  690. trans->transid, 0, 0);
  691. if (ret) {
  692. faili = i;
  693. goto fail;
  694. }
  695. }
  696. }
  697. return 0;
  698. fail:
  699. WARN_ON(1);
  700. #if 0
  701. for (i =0; i < faili; i++) {
  702. if (level == 0) {
  703. u64 disk_bytenr;
  704. btrfs_item_key_to_cpu(buf, &key, i);
  705. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  706. continue;
  707. fi = btrfs_item_ptr(buf, i,
  708. struct btrfs_file_extent_item);
  709. if (btrfs_file_extent_type(buf, fi) ==
  710. BTRFS_FILE_EXTENT_INLINE)
  711. continue;
  712. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  713. if (disk_bytenr == 0)
  714. continue;
  715. err = btrfs_free_extent(trans, root, disk_bytenr,
  716. btrfs_file_extent_disk_num_bytes(buf,
  717. fi), 0);
  718. BUG_ON(err);
  719. } else {
  720. bytenr = btrfs_node_blockptr(buf, i);
  721. err = btrfs_free_extent(trans, root, bytenr,
  722. btrfs_level_size(root, level - 1), 0);
  723. BUG_ON(err);
  724. }
  725. }
  726. #endif
  727. return ret;
  728. }
  729. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  730. struct btrfs_root *root,
  731. struct btrfs_path *path,
  732. struct btrfs_block_group_cache *cache)
  733. {
  734. int ret;
  735. int pending_ret;
  736. struct btrfs_root *extent_root = root->fs_info->extent_root;
  737. unsigned long bi;
  738. struct extent_buffer *leaf;
  739. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  740. if (ret < 0)
  741. goto fail;
  742. BUG_ON(ret);
  743. leaf = path->nodes[0];
  744. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  745. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  746. btrfs_mark_buffer_dirty(leaf);
  747. btrfs_release_path(extent_root, path);
  748. fail:
  749. finish_current_insert(trans, extent_root);
  750. pending_ret = del_pending_extents(trans, extent_root);
  751. if (ret)
  752. return ret;
  753. if (pending_ret)
  754. return pending_ret;
  755. return 0;
  756. }
  757. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  758. struct btrfs_root *root)
  759. {
  760. struct extent_map_tree *block_group_cache;
  761. struct btrfs_block_group_cache *cache;
  762. int ret;
  763. int err = 0;
  764. int werr = 0;
  765. struct btrfs_path *path;
  766. u64 last = 0;
  767. u64 start;
  768. u64 end;
  769. u64 ptr;
  770. block_group_cache = &root->fs_info->block_group_cache;
  771. path = btrfs_alloc_path();
  772. if (!path)
  773. return -ENOMEM;
  774. while(1) {
  775. ret = find_first_extent_bit(block_group_cache, last,
  776. &start, &end, BLOCK_GROUP_DIRTY);
  777. if (ret)
  778. break;
  779. last = end + 1;
  780. ret = get_state_private(block_group_cache, start, &ptr);
  781. if (ret)
  782. break;
  783. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  784. err = write_one_cache_group(trans, root,
  785. path, cache);
  786. /*
  787. * if we fail to write the cache group, we want
  788. * to keep it marked dirty in hopes that a later
  789. * write will work
  790. */
  791. if (err) {
  792. werr = err;
  793. continue;
  794. }
  795. clear_extent_bits(block_group_cache, start, end,
  796. BLOCK_GROUP_DIRTY, GFP_NOFS);
  797. }
  798. btrfs_free_path(path);
  799. return werr;
  800. }
  801. static int update_block_group(struct btrfs_trans_handle *trans,
  802. struct btrfs_root *root,
  803. u64 bytenr, u64 num_bytes, int alloc,
  804. int mark_free, int data)
  805. {
  806. struct btrfs_block_group_cache *cache;
  807. struct btrfs_fs_info *info = root->fs_info;
  808. u64 total = num_bytes;
  809. u64 old_val;
  810. u64 byte_in_group;
  811. u64 start;
  812. u64 end;
  813. while(total) {
  814. cache = btrfs_lookup_block_group(info, bytenr);
  815. if (!cache) {
  816. return -1;
  817. }
  818. byte_in_group = bytenr - cache->key.objectid;
  819. WARN_ON(byte_in_group > cache->key.offset);
  820. start = cache->key.objectid;
  821. end = start + cache->key.offset - 1;
  822. set_extent_bits(&info->block_group_cache, start, end,
  823. BLOCK_GROUP_DIRTY, GFP_NOFS);
  824. old_val = btrfs_block_group_used(&cache->item);
  825. num_bytes = min(total, cache->key.offset - byte_in_group);
  826. if (alloc) {
  827. if (cache->data != data &&
  828. old_val < (cache->key.offset >> 1)) {
  829. int bit_to_clear;
  830. int bit_to_set;
  831. cache->data = data;
  832. if (data) {
  833. bit_to_clear = BLOCK_GROUP_METADATA;
  834. bit_to_set = BLOCK_GROUP_DATA;
  835. cache->item.flags &=
  836. ~BTRFS_BLOCK_GROUP_MIXED;
  837. cache->item.flags |=
  838. BTRFS_BLOCK_GROUP_DATA;
  839. } else {
  840. bit_to_clear = BLOCK_GROUP_DATA;
  841. bit_to_set = BLOCK_GROUP_METADATA;
  842. cache->item.flags &=
  843. ~BTRFS_BLOCK_GROUP_MIXED;
  844. cache->item.flags &=
  845. ~BTRFS_BLOCK_GROUP_DATA;
  846. }
  847. clear_extent_bits(&info->block_group_cache,
  848. start, end, bit_to_clear,
  849. GFP_NOFS);
  850. set_extent_bits(&info->block_group_cache,
  851. start, end, bit_to_set,
  852. GFP_NOFS);
  853. } else if (cache->data != data &&
  854. cache->data != BTRFS_BLOCK_GROUP_MIXED) {
  855. cache->data = BTRFS_BLOCK_GROUP_MIXED;
  856. set_extent_bits(&info->block_group_cache,
  857. start, end,
  858. BLOCK_GROUP_DATA |
  859. BLOCK_GROUP_METADATA,
  860. GFP_NOFS);
  861. }
  862. old_val += num_bytes;
  863. } else {
  864. old_val -= num_bytes;
  865. if (mark_free) {
  866. set_extent_dirty(&info->free_space_cache,
  867. bytenr, bytenr + num_bytes - 1,
  868. GFP_NOFS);
  869. }
  870. }
  871. btrfs_set_block_group_used(&cache->item, old_val);
  872. total -= num_bytes;
  873. bytenr += num_bytes;
  874. }
  875. return 0;
  876. }
  877. static int update_pinned_extents(struct btrfs_root *root,
  878. u64 bytenr, u64 num, int pin)
  879. {
  880. u64 len;
  881. struct btrfs_block_group_cache *cache;
  882. struct btrfs_fs_info *fs_info = root->fs_info;
  883. if (pin) {
  884. set_extent_dirty(&fs_info->pinned_extents,
  885. bytenr, bytenr + num - 1, GFP_NOFS);
  886. } else {
  887. clear_extent_dirty(&fs_info->pinned_extents,
  888. bytenr, bytenr + num - 1, GFP_NOFS);
  889. }
  890. while (num > 0) {
  891. cache = btrfs_lookup_block_group(fs_info, bytenr);
  892. WARN_ON(!cache);
  893. len = min(num, cache->key.offset -
  894. (bytenr - cache->key.objectid));
  895. if (pin) {
  896. cache->pinned += len;
  897. fs_info->total_pinned += len;
  898. } else {
  899. cache->pinned -= len;
  900. fs_info->total_pinned -= len;
  901. }
  902. bytenr += len;
  903. num -= len;
  904. }
  905. return 0;
  906. }
  907. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
  908. {
  909. u64 last = 0;
  910. u64 start;
  911. u64 end;
  912. struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
  913. int ret;
  914. while(1) {
  915. ret = find_first_extent_bit(pinned_extents, last,
  916. &start, &end, EXTENT_DIRTY);
  917. if (ret)
  918. break;
  919. set_extent_dirty(copy, start, end, GFP_NOFS);
  920. last = end + 1;
  921. }
  922. return 0;
  923. }
  924. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  925. struct btrfs_root *root,
  926. struct extent_map_tree *unpin)
  927. {
  928. u64 start;
  929. u64 end;
  930. int ret;
  931. struct extent_map_tree *free_space_cache;
  932. free_space_cache = &root->fs_info->free_space_cache;
  933. while(1) {
  934. ret = find_first_extent_bit(unpin, 0, &start, &end,
  935. EXTENT_DIRTY);
  936. if (ret)
  937. break;
  938. update_pinned_extents(root, start, end + 1 - start, 0);
  939. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  940. set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
  941. }
  942. return 0;
  943. }
  944. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  945. btrfs_root *extent_root)
  946. {
  947. u64 start;
  948. u64 end;
  949. struct btrfs_fs_info *info = extent_root->fs_info;
  950. struct extent_buffer *eb;
  951. struct btrfs_path *path;
  952. struct btrfs_key ins;
  953. struct btrfs_disk_key first;
  954. struct btrfs_extent_item extent_item;
  955. int ret;
  956. int level;
  957. int err = 0;
  958. btrfs_set_stack_extent_refs(&extent_item, 1);
  959. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  960. path = btrfs_alloc_path();
  961. while(1) {
  962. ret = find_first_extent_bit(&info->extent_ins, 0, &start,
  963. &end, EXTENT_LOCKED);
  964. if (ret)
  965. break;
  966. ins.objectid = start;
  967. ins.offset = end + 1 - start;
  968. err = btrfs_insert_item(trans, extent_root, &ins,
  969. &extent_item, sizeof(extent_item));
  970. clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
  971. GFP_NOFS);
  972. eb = read_tree_block(extent_root, ins.objectid, ins.offset);
  973. level = btrfs_header_level(eb);
  974. if (level == 0) {
  975. btrfs_item_key(eb, &first, 0);
  976. } else {
  977. btrfs_node_key(eb, &first, 0);
  978. }
  979. err = btrfs_insert_extent_backref(trans, extent_root, path,
  980. start, extent_root->root_key.objectid,
  981. 0, btrfs_disk_key_objectid(&first),
  982. level);
  983. BUG_ON(err);
  984. free_extent_buffer(eb);
  985. }
  986. btrfs_free_path(path);
  987. return 0;
  988. }
  989. static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
  990. int pending)
  991. {
  992. int err = 0;
  993. struct extent_buffer *buf;
  994. if (!pending) {
  995. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  996. if (buf) {
  997. if (btrfs_buffer_uptodate(buf)) {
  998. u64 transid =
  999. root->fs_info->running_transaction->transid;
  1000. if (btrfs_header_generation(buf) == transid) {
  1001. free_extent_buffer(buf);
  1002. return 1;
  1003. }
  1004. }
  1005. free_extent_buffer(buf);
  1006. }
  1007. update_pinned_extents(root, bytenr, num_bytes, 1);
  1008. } else {
  1009. set_extent_bits(&root->fs_info->pending_del,
  1010. bytenr, bytenr + num_bytes - 1,
  1011. EXTENT_LOCKED, GFP_NOFS);
  1012. }
  1013. BUG_ON(err < 0);
  1014. return 0;
  1015. }
  1016. /*
  1017. * remove an extent from the root, returns 0 on success
  1018. */
  1019. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1020. *root, u64 bytenr, u64 num_bytes,
  1021. u64 root_objectid, u64 ref_generation,
  1022. u64 owner_objectid, u64 owner_offset, int pin,
  1023. int mark_free)
  1024. {
  1025. struct btrfs_path *path;
  1026. struct btrfs_key key;
  1027. struct btrfs_fs_info *info = root->fs_info;
  1028. struct btrfs_root *extent_root = info->extent_root;
  1029. struct extent_buffer *leaf;
  1030. int ret;
  1031. struct btrfs_extent_item *ei;
  1032. u32 refs;
  1033. key.objectid = bytenr;
  1034. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1035. key.offset = num_bytes;
  1036. path = btrfs_alloc_path();
  1037. if (!path)
  1038. return -ENOMEM;
  1039. if (ref_generation && owner_objectid == 0 && root_objectid == 3) {
  1040. //printk("drop backref root %Lu gen %Lu byte %Lu\n", root_objectid, ref_generation, bytenr );
  1041. }
  1042. ret = lookup_extent_backref(trans, extent_root, path,
  1043. bytenr, root_objectid,
  1044. ref_generation,
  1045. owner_objectid, owner_offset, 1);
  1046. if (ret == 0) {
  1047. ret = btrfs_del_item(trans, extent_root, path);
  1048. } else {
  1049. btrfs_print_leaf(extent_root, path->nodes[0]);
  1050. WARN_ON(1);
  1051. printk("Unable to find ref byte nr %Lu root %Lu "
  1052. " gen %Lu owner %Lu offset %Lu\n", bytenr,
  1053. root_objectid, ref_generation, owner_objectid,
  1054. owner_offset);
  1055. }
  1056. btrfs_release_path(extent_root, path);
  1057. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  1058. if (ret < 0)
  1059. return ret;
  1060. BUG_ON(ret);
  1061. leaf = path->nodes[0];
  1062. ei = btrfs_item_ptr(leaf, path->slots[0],
  1063. struct btrfs_extent_item);
  1064. refs = btrfs_extent_refs(leaf, ei);
  1065. BUG_ON(refs == 0);
  1066. refs -= 1;
  1067. btrfs_set_extent_refs(leaf, ei, refs);
  1068. btrfs_mark_buffer_dirty(leaf);
  1069. if (refs == 0) {
  1070. u64 super_used;
  1071. u64 root_used;
  1072. if (pin) {
  1073. ret = pin_down_bytes(root, bytenr, num_bytes, 0);
  1074. if (ret > 0)
  1075. mark_free = 1;
  1076. BUG_ON(ret < 0);
  1077. }
  1078. /* block accounting for super block */
  1079. super_used = btrfs_super_bytes_used(&info->super_copy);
  1080. btrfs_set_super_bytes_used(&info->super_copy,
  1081. super_used - num_bytes);
  1082. /* block accounting for root item */
  1083. root_used = btrfs_root_used(&root->root_item);
  1084. btrfs_set_root_used(&root->root_item,
  1085. root_used - num_bytes);
  1086. ret = btrfs_del_item(trans, extent_root, path);
  1087. if (ret) {
  1088. return ret;
  1089. }
  1090. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1091. mark_free, 0);
  1092. BUG_ON(ret);
  1093. }
  1094. btrfs_free_path(path);
  1095. finish_current_insert(trans, extent_root);
  1096. return ret;
  1097. }
  1098. /*
  1099. * find all the blocks marked as pending in the radix tree and remove
  1100. * them from the extent map
  1101. */
  1102. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1103. btrfs_root *extent_root)
  1104. {
  1105. int ret;
  1106. int err = 0;
  1107. u64 start;
  1108. u64 end;
  1109. struct extent_map_tree *pending_del;
  1110. struct extent_map_tree *pinned_extents;
  1111. pending_del = &extent_root->fs_info->pending_del;
  1112. pinned_extents = &extent_root->fs_info->pinned_extents;
  1113. while(1) {
  1114. ret = find_first_extent_bit(pending_del, 0, &start, &end,
  1115. EXTENT_LOCKED);
  1116. if (ret)
  1117. break;
  1118. update_pinned_extents(extent_root, start, end + 1 - start, 1);
  1119. clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
  1120. GFP_NOFS);
  1121. ret = __free_extent(trans, extent_root,
  1122. start, end + 1 - start,
  1123. extent_root->root_key.objectid,
  1124. 0, 0, 0, 0, 0);
  1125. if (ret)
  1126. err = ret;
  1127. }
  1128. return err;
  1129. }
  1130. /*
  1131. * remove an extent from the root, returns 0 on success
  1132. */
  1133. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1134. *root, u64 bytenr, u64 num_bytes,
  1135. u64 root_objectid, u64 ref_generation,
  1136. u64 owner_objectid, u64 owner_offset, int pin)
  1137. {
  1138. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1139. int pending_ret;
  1140. int ret;
  1141. WARN_ON(num_bytes < root->sectorsize);
  1142. if (!root->ref_cows)
  1143. ref_generation = 0;
  1144. if (root == extent_root) {
  1145. pin_down_bytes(root, bytenr, num_bytes, 1);
  1146. return 0;
  1147. }
  1148. ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
  1149. ref_generation, owner_objectid, owner_offset,
  1150. pin, pin == 0);
  1151. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  1152. return ret ? ret : pending_ret;
  1153. }
  1154. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1155. {
  1156. u64 mask = ((u64)root->stripesize - 1);
  1157. u64 ret = (val + mask) & ~mask;
  1158. return ret;
  1159. }
  1160. /*
  1161. * walks the btree of allocated extents and find a hole of a given size.
  1162. * The key ins is changed to record the hole:
  1163. * ins->objectid == block start
  1164. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1165. * ins->offset == number of blocks
  1166. * Any available blocks before search_start are skipped.
  1167. */
  1168. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1169. *orig_root, u64 num_bytes, u64 empty_size,
  1170. u64 search_start, u64 search_end, u64 hint_byte,
  1171. struct btrfs_key *ins, u64 exclude_start,
  1172. u64 exclude_nr, int data)
  1173. {
  1174. struct btrfs_path *path;
  1175. struct btrfs_key key;
  1176. u64 hole_size = 0;
  1177. u64 aligned;
  1178. int ret;
  1179. int slot = 0;
  1180. u64 last_byte = 0;
  1181. u64 orig_search_start = search_start;
  1182. int start_found;
  1183. struct extent_buffer *l;
  1184. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1185. struct btrfs_fs_info *info = root->fs_info;
  1186. u64 total_needed = num_bytes;
  1187. int level;
  1188. struct btrfs_block_group_cache *block_group;
  1189. int full_scan = 0;
  1190. int wrapped = 0;
  1191. u64 cached_start;
  1192. WARN_ON(num_bytes < root->sectorsize);
  1193. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1194. level = btrfs_header_level(root->node);
  1195. if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
  1196. data = BTRFS_BLOCK_GROUP_MIXED;
  1197. }
  1198. if (search_end == (u64)-1)
  1199. search_end = btrfs_super_total_bytes(&info->super_copy);
  1200. if (hint_byte) {
  1201. block_group = btrfs_lookup_block_group(info, hint_byte);
  1202. if (!block_group)
  1203. hint_byte = search_start;
  1204. block_group = btrfs_find_block_group(root, block_group,
  1205. hint_byte, data, 1);
  1206. } else {
  1207. block_group = btrfs_find_block_group(root,
  1208. trans->block_group,
  1209. search_start, data, 1);
  1210. }
  1211. total_needed += empty_size;
  1212. path = btrfs_alloc_path();
  1213. check_failed:
  1214. search_start = find_search_start(root, &block_group, search_start,
  1215. total_needed, data, full_scan);
  1216. search_start = stripe_align(root, search_start);
  1217. cached_start = search_start;
  1218. btrfs_init_path(path);
  1219. ins->objectid = search_start;
  1220. ins->offset = 0;
  1221. start_found = 0;
  1222. path->reada = 2;
  1223. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  1224. if (ret < 0)
  1225. goto error;
  1226. if (path->slots[0] > 0) {
  1227. path->slots[0]--;
  1228. }
  1229. l = path->nodes[0];
  1230. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1231. /*
  1232. * walk backwards to find the first extent item key
  1233. */
  1234. while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
  1235. if (path->slots[0] == 0) {
  1236. ret = btrfs_prev_leaf(root, path);
  1237. if (ret != 0) {
  1238. ret = btrfs_search_slot(trans, root, ins,
  1239. path, 0, 0);
  1240. if (ret < 0)
  1241. goto error;
  1242. if (path->slots[0] > 0)
  1243. path->slots[0]--;
  1244. break;
  1245. }
  1246. } else {
  1247. path->slots[0]--;
  1248. }
  1249. l = path->nodes[0];
  1250. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1251. }
  1252. while (1) {
  1253. l = path->nodes[0];
  1254. slot = path->slots[0];
  1255. if (slot >= btrfs_header_nritems(l)) {
  1256. ret = btrfs_next_leaf(root, path);
  1257. if (ret == 0)
  1258. continue;
  1259. if (ret < 0)
  1260. goto error;
  1261. search_start = max(search_start,
  1262. block_group->key.objectid);
  1263. if (!start_found) {
  1264. aligned = stripe_align(root, search_start);
  1265. ins->objectid = aligned;
  1266. if (aligned >= search_end) {
  1267. ret = -ENOSPC;
  1268. goto error;
  1269. }
  1270. ins->offset = search_end - aligned;
  1271. start_found = 1;
  1272. goto check_pending;
  1273. }
  1274. ins->objectid = stripe_align(root,
  1275. last_byte > search_start ?
  1276. last_byte : search_start);
  1277. if (search_end <= ins->objectid) {
  1278. ret = -ENOSPC;
  1279. goto error;
  1280. }
  1281. ins->offset = search_end - ins->objectid;
  1282. BUG_ON(ins->objectid >= search_end);
  1283. goto check_pending;
  1284. }
  1285. btrfs_item_key_to_cpu(l, &key, slot);
  1286. if (key.objectid >= search_start && key.objectid > last_byte &&
  1287. start_found) {
  1288. if (last_byte < search_start)
  1289. last_byte = search_start;
  1290. aligned = stripe_align(root, last_byte);
  1291. hole_size = key.objectid - aligned;
  1292. if (key.objectid > aligned && hole_size >= num_bytes) {
  1293. ins->objectid = aligned;
  1294. ins->offset = hole_size;
  1295. goto check_pending;
  1296. }
  1297. }
  1298. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
  1299. if (!start_found && btrfs_key_type(&key) ==
  1300. BTRFS_BLOCK_GROUP_ITEM_KEY) {
  1301. last_byte = key.objectid;
  1302. start_found = 1;
  1303. }
  1304. goto next;
  1305. }
  1306. start_found = 1;
  1307. last_byte = key.objectid + key.offset;
  1308. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  1309. last_byte >= block_group->key.objectid +
  1310. block_group->key.offset) {
  1311. btrfs_release_path(root, path);
  1312. search_start = block_group->key.objectid +
  1313. block_group->key.offset;
  1314. goto new_group;
  1315. }
  1316. next:
  1317. path->slots[0]++;
  1318. cond_resched();
  1319. }
  1320. check_pending:
  1321. /* we have to make sure we didn't find an extent that has already
  1322. * been allocated by the map tree or the original allocation
  1323. */
  1324. btrfs_release_path(root, path);
  1325. BUG_ON(ins->objectid < search_start);
  1326. if (ins->objectid + num_bytes >= search_end)
  1327. goto enospc;
  1328. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  1329. ins->objectid + num_bytes > block_group->
  1330. key.objectid + block_group->key.offset) {
  1331. search_start = block_group->key.objectid +
  1332. block_group->key.offset;
  1333. goto new_group;
  1334. }
  1335. if (test_range_bit(&info->extent_ins, ins->objectid,
  1336. ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
  1337. search_start = ins->objectid + num_bytes;
  1338. goto new_group;
  1339. }
  1340. if (test_range_bit(&info->pinned_extents, ins->objectid,
  1341. ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
  1342. search_start = ins->objectid + num_bytes;
  1343. goto new_group;
  1344. }
  1345. if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
  1346. ins->objectid < exclude_start + exclude_nr)) {
  1347. search_start = exclude_start + exclude_nr;
  1348. goto new_group;
  1349. }
  1350. if (!data) {
  1351. block_group = btrfs_lookup_block_group(info, ins->objectid);
  1352. if (block_group)
  1353. trans->block_group = block_group;
  1354. }
  1355. ins->offset = num_bytes;
  1356. btrfs_free_path(path);
  1357. return 0;
  1358. new_group:
  1359. if (search_start + num_bytes >= search_end) {
  1360. enospc:
  1361. search_start = orig_search_start;
  1362. if (full_scan) {
  1363. ret = -ENOSPC;
  1364. goto error;
  1365. }
  1366. if (wrapped) {
  1367. if (!full_scan)
  1368. total_needed -= empty_size;
  1369. full_scan = 1;
  1370. data = BTRFS_BLOCK_GROUP_MIXED;
  1371. } else
  1372. wrapped = 1;
  1373. }
  1374. block_group = btrfs_lookup_block_group(info, search_start);
  1375. cond_resched();
  1376. block_group = btrfs_find_block_group(root, block_group,
  1377. search_start, data, 0);
  1378. goto check_failed;
  1379. error:
  1380. btrfs_release_path(root, path);
  1381. btrfs_free_path(path);
  1382. return ret;
  1383. }
  1384. /*
  1385. * finds a free extent and does all the dirty work required for allocation
  1386. * returns the key for the extent through ins, and a tree buffer for
  1387. * the first block of the extent through buf.
  1388. *
  1389. * returns 0 if everything worked, non-zero otherwise.
  1390. */
  1391. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  1392. struct btrfs_root *root,
  1393. u64 num_bytes, u64 root_objectid, u64 ref_generation,
  1394. u64 owner, u64 owner_offset,
  1395. u64 empty_size, u64 hint_byte,
  1396. u64 search_end, struct btrfs_key *ins, int data)
  1397. {
  1398. int ret;
  1399. int pending_ret;
  1400. u64 super_used, root_used;
  1401. u64 search_start = 0;
  1402. struct btrfs_fs_info *info = root->fs_info;
  1403. struct btrfs_root *extent_root = info->extent_root;
  1404. struct btrfs_extent_item extent_item;
  1405. struct btrfs_path *path;
  1406. btrfs_set_stack_extent_refs(&extent_item, 1);
  1407. WARN_ON(num_bytes < root->sectorsize);
  1408. ret = find_free_extent(trans, root, num_bytes, empty_size,
  1409. search_start, search_end, hint_byte, ins,
  1410. trans->alloc_exclude_start,
  1411. trans->alloc_exclude_nr, data);
  1412. BUG_ON(ret);
  1413. if (ret)
  1414. return ret;
  1415. /* block accounting for super block */
  1416. super_used = btrfs_super_bytes_used(&info->super_copy);
  1417. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  1418. /* block accounting for root item */
  1419. root_used = btrfs_root_used(&root->root_item);
  1420. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  1421. clear_extent_dirty(&root->fs_info->free_space_cache,
  1422. ins->objectid, ins->objectid + ins->offset - 1,
  1423. GFP_NOFS);
  1424. if (root == extent_root) {
  1425. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  1426. ins->objectid + ins->offset - 1,
  1427. EXTENT_LOCKED, GFP_NOFS);
  1428. WARN_ON(data == 1);
  1429. goto update_block;
  1430. }
  1431. WARN_ON(trans->alloc_exclude_nr);
  1432. trans->alloc_exclude_start = ins->objectid;
  1433. trans->alloc_exclude_nr = ins->offset;
  1434. ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
  1435. sizeof(extent_item));
  1436. trans->alloc_exclude_start = 0;
  1437. trans->alloc_exclude_nr = 0;
  1438. BUG_ON(ret);
  1439. path = btrfs_alloc_path();
  1440. BUG_ON(!path);
  1441. ret = btrfs_insert_extent_backref(trans, extent_root, path,
  1442. ins->objectid, root_objectid,
  1443. ref_generation, owner, owner_offset);
  1444. BUG_ON(ret);
  1445. btrfs_free_path(path);
  1446. finish_current_insert(trans, extent_root);
  1447. pending_ret = del_pending_extents(trans, extent_root);
  1448. if (ret) {
  1449. return ret;
  1450. }
  1451. if (pending_ret) {
  1452. return pending_ret;
  1453. }
  1454. update_block:
  1455. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
  1456. data);
  1457. BUG_ON(ret);
  1458. return 0;
  1459. }
  1460. /*
  1461. * helper function to allocate a block for a given tree
  1462. * returns the tree buffer or NULL.
  1463. */
  1464. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1465. struct btrfs_root *root,
  1466. u32 blocksize,
  1467. u64 root_objectid, u64 hint,
  1468. u64 empty_size)
  1469. {
  1470. u64 ref_generation;
  1471. if (root->ref_cows)
  1472. ref_generation = trans->transid;
  1473. else
  1474. ref_generation = 0;
  1475. return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
  1476. ref_generation, 0, 0, hint, empty_size);
  1477. }
  1478. /*
  1479. * helper function to allocate a block for a given tree
  1480. * returns the tree buffer or NULL.
  1481. */
  1482. struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1483. struct btrfs_root *root,
  1484. u32 blocksize,
  1485. u64 root_objectid,
  1486. u64 ref_generation,
  1487. u64 first_objectid,
  1488. int level,
  1489. u64 hint,
  1490. u64 empty_size)
  1491. {
  1492. struct btrfs_key ins;
  1493. int ret;
  1494. struct extent_buffer *buf;
  1495. ret = btrfs_alloc_extent(trans, root, blocksize,
  1496. root_objectid, ref_generation,
  1497. first_objectid, level, empty_size, hint,
  1498. (u64)-1, &ins, 0);
  1499. if (ret) {
  1500. BUG_ON(ret > 0);
  1501. return ERR_PTR(ret);
  1502. }
  1503. buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
  1504. if (!buf) {
  1505. btrfs_free_extent(trans, root, ins.objectid, blocksize,
  1506. root->root_key.objectid, ref_generation,
  1507. 0, 0, 0);
  1508. return ERR_PTR(-ENOMEM);
  1509. }
  1510. btrfs_set_buffer_uptodate(buf);
  1511. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  1512. buf->start + buf->len - 1, GFP_NOFS);
  1513. set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
  1514. buf->start, buf->start + buf->len - 1,
  1515. EXTENT_CSUM, GFP_NOFS);
  1516. buf->flags |= EXTENT_CSUM;
  1517. btrfs_set_buffer_defrag(buf);
  1518. trans->blocks_used++;
  1519. return buf;
  1520. }
  1521. static int drop_leaf_ref(struct btrfs_trans_handle *trans,
  1522. struct btrfs_root *root, struct extent_buffer *leaf)
  1523. {
  1524. u64 leaf_owner;
  1525. u64 leaf_generation;
  1526. struct btrfs_key key;
  1527. struct btrfs_file_extent_item *fi;
  1528. int i;
  1529. int nritems;
  1530. int ret;
  1531. BUG_ON(!btrfs_is_leaf(leaf));
  1532. nritems = btrfs_header_nritems(leaf);
  1533. leaf_owner = btrfs_header_owner(leaf);
  1534. leaf_generation = btrfs_header_generation(leaf);
  1535. for (i = 0; i < nritems; i++) {
  1536. u64 disk_bytenr;
  1537. btrfs_item_key_to_cpu(leaf, &key, i);
  1538. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1539. continue;
  1540. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  1541. if (btrfs_file_extent_type(leaf, fi) ==
  1542. BTRFS_FILE_EXTENT_INLINE)
  1543. continue;
  1544. /*
  1545. * FIXME make sure to insert a trans record that
  1546. * repeats the snapshot del on crash
  1547. */
  1548. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1549. if (disk_bytenr == 0)
  1550. continue;
  1551. ret = btrfs_free_extent(trans, root, disk_bytenr,
  1552. btrfs_file_extent_disk_num_bytes(leaf, fi),
  1553. leaf_owner, leaf_generation,
  1554. key.objectid, key.offset, 0);
  1555. BUG_ON(ret);
  1556. }
  1557. return 0;
  1558. }
  1559. static void reada_walk_down(struct btrfs_root *root,
  1560. struct extent_buffer *node)
  1561. {
  1562. int i;
  1563. u32 nritems;
  1564. u64 bytenr;
  1565. int ret;
  1566. u32 refs;
  1567. int level;
  1568. u32 blocksize;
  1569. nritems = btrfs_header_nritems(node);
  1570. level = btrfs_header_level(node);
  1571. for (i = 0; i < nritems; i++) {
  1572. bytenr = btrfs_node_blockptr(node, i);
  1573. blocksize = btrfs_level_size(root, level - 1);
  1574. ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
  1575. BUG_ON(ret);
  1576. if (refs != 1)
  1577. continue;
  1578. mutex_unlock(&root->fs_info->fs_mutex);
  1579. ret = readahead_tree_block(root, bytenr, blocksize);
  1580. cond_resched();
  1581. mutex_lock(&root->fs_info->fs_mutex);
  1582. if (ret)
  1583. break;
  1584. }
  1585. }
  1586. /*
  1587. * helper function for drop_snapshot, this walks down the tree dropping ref
  1588. * counts as it goes.
  1589. */
  1590. static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1591. *root, struct btrfs_path *path, int *level)
  1592. {
  1593. u64 root_owner;
  1594. u64 root_gen;
  1595. u64 bytenr;
  1596. struct extent_buffer *next;
  1597. struct extent_buffer *cur;
  1598. struct extent_buffer *parent;
  1599. u32 blocksize;
  1600. int ret;
  1601. u32 refs;
  1602. WARN_ON(*level < 0);
  1603. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1604. ret = lookup_extent_ref(trans, root,
  1605. path->nodes[*level]->start,
  1606. path->nodes[*level]->len, &refs);
  1607. BUG_ON(ret);
  1608. if (refs > 1)
  1609. goto out;
  1610. /*
  1611. * walk down to the last node level and free all the leaves
  1612. */
  1613. while(*level >= 0) {
  1614. WARN_ON(*level < 0);
  1615. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1616. cur = path->nodes[*level];
  1617. if (*level > 0 && path->slots[*level] == 0)
  1618. reada_walk_down(root, cur);
  1619. if (btrfs_header_level(cur) != *level)
  1620. WARN_ON(1);
  1621. if (path->slots[*level] >=
  1622. btrfs_header_nritems(cur))
  1623. break;
  1624. if (*level == 0) {
  1625. ret = drop_leaf_ref(trans, root, cur);
  1626. BUG_ON(ret);
  1627. break;
  1628. }
  1629. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1630. blocksize = btrfs_level_size(root, *level - 1);
  1631. ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
  1632. BUG_ON(ret);
  1633. if (refs != 1) {
  1634. parent = path->nodes[*level];
  1635. root_owner = btrfs_header_owner(parent);
  1636. root_gen = btrfs_header_generation(parent);
  1637. path->slots[*level]++;
  1638. ret = btrfs_free_extent(trans, root, bytenr,
  1639. blocksize, root_owner,
  1640. root_gen, 0, 0, 1);
  1641. BUG_ON(ret);
  1642. continue;
  1643. }
  1644. next = btrfs_find_tree_block(root, bytenr, blocksize);
  1645. if (!next || !btrfs_buffer_uptodate(next)) {
  1646. free_extent_buffer(next);
  1647. mutex_unlock(&root->fs_info->fs_mutex);
  1648. next = read_tree_block(root, bytenr, blocksize);
  1649. mutex_lock(&root->fs_info->fs_mutex);
  1650. /* we dropped the lock, check one more time */
  1651. ret = lookup_extent_ref(trans, root, bytenr,
  1652. blocksize, &refs);
  1653. BUG_ON(ret);
  1654. if (refs != 1) {
  1655. parent = path->nodes[*level];
  1656. root_owner = btrfs_header_owner(parent);
  1657. root_gen = btrfs_header_generation(parent);
  1658. path->slots[*level]++;
  1659. free_extent_buffer(next);
  1660. ret = btrfs_free_extent(trans, root, bytenr,
  1661. blocksize,
  1662. root_owner,
  1663. root_gen, 0, 0, 1);
  1664. BUG_ON(ret);
  1665. continue;
  1666. }
  1667. }
  1668. WARN_ON(*level <= 0);
  1669. if (path->nodes[*level-1])
  1670. free_extent_buffer(path->nodes[*level-1]);
  1671. path->nodes[*level-1] = next;
  1672. *level = btrfs_header_level(next);
  1673. path->slots[*level] = 0;
  1674. }
  1675. out:
  1676. WARN_ON(*level < 0);
  1677. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1678. if (path->nodes[*level] == root->node) {
  1679. root_owner = root->root_key.objectid;
  1680. parent = path->nodes[*level];
  1681. } else {
  1682. parent = path->nodes[*level + 1];
  1683. root_owner = btrfs_header_owner(parent);
  1684. }
  1685. root_gen = btrfs_header_generation(parent);
  1686. ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
  1687. path->nodes[*level]->len,
  1688. root_owner, root_gen, 0, 0, 1);
  1689. free_extent_buffer(path->nodes[*level]);
  1690. path->nodes[*level] = NULL;
  1691. *level += 1;
  1692. BUG_ON(ret);
  1693. return 0;
  1694. }
  1695. /*
  1696. * helper for dropping snapshots. This walks back up the tree in the path
  1697. * to find the first node higher up where we haven't yet gone through
  1698. * all the slots
  1699. */
  1700. static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1701. *root, struct btrfs_path *path, int *level)
  1702. {
  1703. u64 root_owner;
  1704. u64 root_gen;
  1705. struct btrfs_root_item *root_item = &root->root_item;
  1706. int i;
  1707. int slot;
  1708. int ret;
  1709. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1710. slot = path->slots[i];
  1711. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1712. struct extent_buffer *node;
  1713. struct btrfs_disk_key disk_key;
  1714. node = path->nodes[i];
  1715. path->slots[i]++;
  1716. *level = i;
  1717. WARN_ON(*level == 0);
  1718. btrfs_node_key(node, &disk_key, path->slots[i]);
  1719. memcpy(&root_item->drop_progress,
  1720. &disk_key, sizeof(disk_key));
  1721. root_item->drop_level = i;
  1722. return 0;
  1723. } else {
  1724. if (path->nodes[*level] == root->node) {
  1725. root_owner = root->root_key.objectid;
  1726. root_gen =
  1727. btrfs_header_generation(path->nodes[*level]);
  1728. } else {
  1729. struct extent_buffer *node;
  1730. node = path->nodes[*level + 1];
  1731. root_owner = btrfs_header_owner(node);
  1732. root_gen = btrfs_header_generation(node);
  1733. }
  1734. ret = btrfs_free_extent(trans, root,
  1735. path->nodes[*level]->start,
  1736. path->nodes[*level]->len,
  1737. root_owner, root_gen, 0, 0, 1);
  1738. BUG_ON(ret);
  1739. free_extent_buffer(path->nodes[*level]);
  1740. path->nodes[*level] = NULL;
  1741. *level = i + 1;
  1742. }
  1743. }
  1744. return 1;
  1745. }
  1746. /*
  1747. * drop the reference count on the tree rooted at 'snap'. This traverses
  1748. * the tree freeing any blocks that have a ref count of zero after being
  1749. * decremented.
  1750. */
  1751. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  1752. *root)
  1753. {
  1754. int ret = 0;
  1755. int wret;
  1756. int level;
  1757. struct btrfs_path *path;
  1758. int i;
  1759. int orig_level;
  1760. struct btrfs_root_item *root_item = &root->root_item;
  1761. path = btrfs_alloc_path();
  1762. BUG_ON(!path);
  1763. level = btrfs_header_level(root->node);
  1764. orig_level = level;
  1765. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  1766. path->nodes[level] = root->node;
  1767. extent_buffer_get(root->node);
  1768. path->slots[level] = 0;
  1769. } else {
  1770. struct btrfs_key key;
  1771. struct btrfs_disk_key found_key;
  1772. struct extent_buffer *node;
  1773. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  1774. level = root_item->drop_level;
  1775. path->lowest_level = level;
  1776. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1777. if (wret < 0) {
  1778. ret = wret;
  1779. goto out;
  1780. }
  1781. node = path->nodes[level];
  1782. btrfs_node_key(node, &found_key, path->slots[level]);
  1783. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  1784. sizeof(found_key)));
  1785. }
  1786. while(1) {
  1787. wret = walk_down_tree(trans, root, path, &level);
  1788. if (wret > 0)
  1789. break;
  1790. if (wret < 0)
  1791. ret = wret;
  1792. wret = walk_up_tree(trans, root, path, &level);
  1793. if (wret > 0)
  1794. break;
  1795. if (wret < 0)
  1796. ret = wret;
  1797. ret = -EAGAIN;
  1798. break;
  1799. }
  1800. for (i = 0; i <= orig_level; i++) {
  1801. if (path->nodes[i]) {
  1802. free_extent_buffer(path->nodes[i]);
  1803. path->nodes[i] = NULL;
  1804. }
  1805. }
  1806. out:
  1807. btrfs_free_path(path);
  1808. return ret;
  1809. }
  1810. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  1811. {
  1812. u64 start;
  1813. u64 end;
  1814. u64 ptr;
  1815. int ret;
  1816. while(1) {
  1817. ret = find_first_extent_bit(&info->block_group_cache, 0,
  1818. &start, &end, (unsigned int)-1);
  1819. if (ret)
  1820. break;
  1821. ret = get_state_private(&info->block_group_cache, start, &ptr);
  1822. if (!ret)
  1823. kfree((void *)(unsigned long)ptr);
  1824. clear_extent_bits(&info->block_group_cache, start,
  1825. end, (unsigned int)-1, GFP_NOFS);
  1826. }
  1827. while(1) {
  1828. ret = find_first_extent_bit(&info->free_space_cache, 0,
  1829. &start, &end, EXTENT_DIRTY);
  1830. if (ret)
  1831. break;
  1832. clear_extent_dirty(&info->free_space_cache, start,
  1833. end, GFP_NOFS);
  1834. }
  1835. return 0;
  1836. }
  1837. int btrfs_read_block_groups(struct btrfs_root *root)
  1838. {
  1839. struct btrfs_path *path;
  1840. int ret;
  1841. int err = 0;
  1842. int bit;
  1843. struct btrfs_block_group_cache *cache;
  1844. struct btrfs_fs_info *info = root->fs_info;
  1845. struct extent_map_tree *block_group_cache;
  1846. struct btrfs_key key;
  1847. struct btrfs_key found_key;
  1848. struct extent_buffer *leaf;
  1849. block_group_cache = &info->block_group_cache;
  1850. root = info->extent_root;
  1851. key.objectid = 0;
  1852. key.offset = BTRFS_BLOCK_GROUP_SIZE;
  1853. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  1854. path = btrfs_alloc_path();
  1855. if (!path)
  1856. return -ENOMEM;
  1857. while(1) {
  1858. ret = btrfs_search_slot(NULL, info->extent_root,
  1859. &key, path, 0, 0);
  1860. if (ret != 0) {
  1861. err = ret;
  1862. break;
  1863. }
  1864. leaf = path->nodes[0];
  1865. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1866. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  1867. if (!cache) {
  1868. err = -1;
  1869. break;
  1870. }
  1871. read_extent_buffer(leaf, &cache->item,
  1872. btrfs_item_ptr_offset(leaf, path->slots[0]),
  1873. sizeof(cache->item));
  1874. memcpy(&cache->key, &found_key, sizeof(found_key));
  1875. cache->cached = 0;
  1876. cache->pinned = 0;
  1877. key.objectid = found_key.objectid + found_key.offset;
  1878. btrfs_release_path(root, path);
  1879. if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
  1880. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  1881. cache->data = BTRFS_BLOCK_GROUP_MIXED;
  1882. } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
  1883. bit = BLOCK_GROUP_DATA;
  1884. cache->data = BTRFS_BLOCK_GROUP_DATA;
  1885. } else {
  1886. bit = BLOCK_GROUP_METADATA;
  1887. cache->data = 0;
  1888. }
  1889. /* use EXTENT_LOCKED to prevent merging */
  1890. set_extent_bits(block_group_cache, found_key.objectid,
  1891. found_key.objectid + found_key.offset - 1,
  1892. bit | EXTENT_LOCKED, GFP_NOFS);
  1893. set_state_private(block_group_cache, found_key.objectid,
  1894. (unsigned long)cache);
  1895. if (key.objectid >=
  1896. btrfs_super_total_bytes(&info->super_copy))
  1897. break;
  1898. }
  1899. btrfs_free_path(path);
  1900. return 0;
  1901. }