extent-tree.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  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 <linux/pagemap.h>
  21. #include "hash.h"
  22. #include "ctree.h"
  23. #include "disk-io.h"
  24. #include "print-tree.h"
  25. #include "transaction.h"
  26. #include "volumes.h"
  27. #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
  28. #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
  29. #define BLOCK_GROUP_SYSTEM EXTENT_NEW
  30. #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
  31. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  32. btrfs_root *extent_root);
  33. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  34. btrfs_root *extent_root);
  35. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  36. struct btrfs_root *root, u64 bytes_used,
  37. u64 type, u64 chunk_tree, u64 chunk_objectid,
  38. u64 size);
  39. static int cache_block_group(struct btrfs_root *root,
  40. struct btrfs_block_group_cache *block_group)
  41. {
  42. struct btrfs_path *path;
  43. int ret;
  44. struct btrfs_key key;
  45. struct extent_buffer *leaf;
  46. struct extent_io_tree *free_space_cache;
  47. int slot;
  48. u64 last = 0;
  49. u64 hole_size;
  50. u64 first_free;
  51. int found = 0;
  52. if (!block_group)
  53. return 0;
  54. root = root->fs_info->extent_root;
  55. free_space_cache = &root->fs_info->free_space_cache;
  56. if (block_group->cached)
  57. return 0;
  58. path = btrfs_alloc_path();
  59. if (!path)
  60. return -ENOMEM;
  61. path->reada = 2;
  62. first_free = block_group->key.objectid;
  63. key.objectid = block_group->key.objectid;
  64. key.offset = 0;
  65. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  66. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  67. if (ret < 0)
  68. return ret;
  69. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  70. if (ret < 0)
  71. return ret;
  72. if (ret == 0) {
  73. leaf = path->nodes[0];
  74. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  75. if (key.objectid + key.offset > first_free)
  76. first_free = key.objectid + key.offset;
  77. }
  78. while(1) {
  79. leaf = path->nodes[0];
  80. slot = path->slots[0];
  81. if (slot >= btrfs_header_nritems(leaf)) {
  82. ret = btrfs_next_leaf(root, path);
  83. if (ret < 0)
  84. goto err;
  85. if (ret == 0) {
  86. continue;
  87. } else {
  88. break;
  89. }
  90. }
  91. btrfs_item_key_to_cpu(leaf, &key, slot);
  92. if (key.objectid < block_group->key.objectid) {
  93. goto next;
  94. }
  95. if (key.objectid >= block_group->key.objectid +
  96. block_group->key.offset) {
  97. break;
  98. }
  99. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  100. if (!found) {
  101. last = first_free;
  102. found = 1;
  103. }
  104. if (key.objectid > last) {
  105. hole_size = key.objectid - last;
  106. set_extent_dirty(free_space_cache, last,
  107. last + hole_size - 1,
  108. GFP_NOFS);
  109. }
  110. last = key.objectid + key.offset;
  111. }
  112. next:
  113. path->slots[0]++;
  114. }
  115. if (!found)
  116. last = first_free;
  117. if (block_group->key.objectid +
  118. block_group->key.offset > last) {
  119. hole_size = block_group->key.objectid +
  120. block_group->key.offset - last;
  121. set_extent_dirty(free_space_cache, last,
  122. last + hole_size - 1, GFP_NOFS);
  123. }
  124. block_group->cached = 1;
  125. err:
  126. btrfs_free_path(path);
  127. return 0;
  128. }
  129. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  130. btrfs_fs_info *info,
  131. u64 bytenr)
  132. {
  133. struct extent_io_tree *block_group_cache;
  134. struct btrfs_block_group_cache *block_group = NULL;
  135. u64 ptr;
  136. u64 start;
  137. u64 end;
  138. int ret;
  139. block_group_cache = &info->block_group_cache;
  140. ret = find_first_extent_bit(block_group_cache,
  141. bytenr, &start, &end,
  142. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
  143. BLOCK_GROUP_SYSTEM);
  144. if (ret) {
  145. return NULL;
  146. }
  147. ret = get_state_private(block_group_cache, start, &ptr);
  148. if (ret)
  149. return NULL;
  150. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  151. if (block_group->key.objectid <= bytenr && bytenr <
  152. block_group->key.objectid + block_group->key.offset)
  153. return block_group;
  154. return NULL;
  155. }
  156. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  157. {
  158. return (cache->flags & bits) == bits;
  159. }
  160. static int noinline find_search_start(struct btrfs_root *root,
  161. struct btrfs_block_group_cache **cache_ret,
  162. u64 *start_ret, int num, int data)
  163. {
  164. int ret;
  165. struct btrfs_block_group_cache *cache = *cache_ret;
  166. struct extent_io_tree *free_space_cache;
  167. struct extent_state *state;
  168. u64 last;
  169. u64 start = 0;
  170. u64 cache_miss = 0;
  171. u64 total_fs_bytes;
  172. u64 search_start = *start_ret;
  173. int wrapped = 0;
  174. if (!cache)
  175. goto out;
  176. total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  177. free_space_cache = &root->fs_info->free_space_cache;
  178. again:
  179. ret = cache_block_group(root, cache);
  180. if (ret)
  181. goto out;
  182. last = max(search_start, cache->key.objectid);
  183. if (!block_group_bits(cache, data)) {
  184. goto new_group;
  185. }
  186. spin_lock_irq(&free_space_cache->lock);
  187. state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
  188. while(1) {
  189. if (!state) {
  190. if (!cache_miss)
  191. cache_miss = last;
  192. spin_unlock_irq(&free_space_cache->lock);
  193. goto new_group;
  194. }
  195. start = max(last, state->start);
  196. last = state->end + 1;
  197. if (last - start < num) {
  198. if (last == cache->key.objectid + cache->key.offset)
  199. cache_miss = start;
  200. do {
  201. state = extent_state_next(state);
  202. } while(state && !(state->state & EXTENT_DIRTY));
  203. continue;
  204. }
  205. spin_unlock_irq(&free_space_cache->lock);
  206. if (start + num > cache->key.objectid + cache->key.offset)
  207. goto new_group;
  208. if (start + num > total_fs_bytes)
  209. goto new_group;
  210. if (!block_group_bits(cache, data)) {
  211. printk("block group bits don't match %Lu %d\n", cache->flags, data);
  212. }
  213. *start_ret = start;
  214. return 0;
  215. }
  216. out:
  217. cache = btrfs_lookup_block_group(root->fs_info, search_start);
  218. if (!cache) {
  219. printk("Unable to find block group for %Lu\n", search_start);
  220. WARN_ON(1);
  221. }
  222. return -ENOSPC;
  223. new_group:
  224. last = cache->key.objectid + cache->key.offset;
  225. wrapped:
  226. cache = btrfs_lookup_block_group(root->fs_info, last);
  227. if (!cache || cache->key.objectid >= total_fs_bytes) {
  228. no_cache:
  229. if (!wrapped) {
  230. wrapped = 1;
  231. last = search_start;
  232. goto wrapped;
  233. }
  234. goto out;
  235. }
  236. if (cache_miss && !cache->cached) {
  237. cache_block_group(root, cache);
  238. last = cache_miss;
  239. cache = btrfs_lookup_block_group(root->fs_info, last);
  240. }
  241. cache = btrfs_find_block_group(root, cache, last, data, 0);
  242. if (!cache)
  243. goto no_cache;
  244. *cache_ret = cache;
  245. cache_miss = 0;
  246. goto again;
  247. }
  248. static u64 div_factor(u64 num, int factor)
  249. {
  250. if (factor == 10)
  251. return num;
  252. num *= factor;
  253. do_div(num, 10);
  254. return num;
  255. }
  256. static int block_group_state_bits(u64 flags)
  257. {
  258. int bits = 0;
  259. if (flags & BTRFS_BLOCK_GROUP_DATA)
  260. bits |= BLOCK_GROUP_DATA;
  261. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  262. bits |= BLOCK_GROUP_METADATA;
  263. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  264. bits |= BLOCK_GROUP_SYSTEM;
  265. return bits;
  266. }
  267. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  268. struct btrfs_block_group_cache
  269. *hint, u64 search_start,
  270. int data, int owner)
  271. {
  272. struct btrfs_block_group_cache *cache;
  273. struct extent_io_tree *block_group_cache;
  274. struct btrfs_block_group_cache *found_group = NULL;
  275. struct btrfs_fs_info *info = root->fs_info;
  276. u64 used;
  277. u64 last = 0;
  278. u64 hint_last;
  279. u64 start;
  280. u64 end;
  281. u64 free_check;
  282. u64 ptr;
  283. u64 total_fs_bytes;
  284. int bit;
  285. int ret;
  286. int full_search = 0;
  287. int factor = 8;
  288. block_group_cache = &info->block_group_cache;
  289. total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  290. if (!owner)
  291. factor = 8;
  292. bit = block_group_state_bits(data);
  293. if (search_start && search_start < total_fs_bytes) {
  294. struct btrfs_block_group_cache *shint;
  295. shint = btrfs_lookup_block_group(info, search_start);
  296. if (shint && block_group_bits(shint, data)) {
  297. used = btrfs_block_group_used(&shint->item);
  298. if (used + shint->pinned <
  299. div_factor(shint->key.offset, factor)) {
  300. return shint;
  301. }
  302. }
  303. }
  304. if (hint && block_group_bits(hint, data) &&
  305. hint->key.objectid < total_fs_bytes) {
  306. used = btrfs_block_group_used(&hint->item);
  307. if (used + hint->pinned <
  308. div_factor(hint->key.offset, factor)) {
  309. return hint;
  310. }
  311. last = hint->key.objectid + hint->key.offset;
  312. hint_last = last;
  313. } else {
  314. if (hint)
  315. hint_last = max(hint->key.objectid, search_start);
  316. else
  317. hint_last = search_start;
  318. if (hint_last >= total_fs_bytes)
  319. hint_last = search_start;
  320. last = hint_last;
  321. }
  322. again:
  323. while(1) {
  324. ret = find_first_extent_bit(block_group_cache, last,
  325. &start, &end, bit);
  326. if (ret)
  327. break;
  328. ret = get_state_private(block_group_cache, start, &ptr);
  329. if (ret)
  330. break;
  331. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  332. last = cache->key.objectid + cache->key.offset;
  333. used = btrfs_block_group_used(&cache->item);
  334. if (cache->key.objectid > total_fs_bytes)
  335. break;
  336. if (block_group_bits(cache, data)) {
  337. if (full_search)
  338. free_check = cache->key.offset;
  339. else
  340. free_check = div_factor(cache->key.offset,
  341. factor);
  342. if (used + cache->pinned < free_check) {
  343. found_group = cache;
  344. goto found;
  345. }
  346. }
  347. cond_resched();
  348. }
  349. if (!full_search) {
  350. last = search_start;
  351. full_search = 1;
  352. goto again;
  353. }
  354. found:
  355. return found_group;
  356. }
  357. static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
  358. u64 owner, u64 owner_offset)
  359. {
  360. u32 high_crc = ~(u32)0;
  361. u32 low_crc = ~(u32)0;
  362. __le64 lenum;
  363. lenum = cpu_to_le64(root_objectid);
  364. high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
  365. lenum = cpu_to_le64(ref_generation);
  366. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  367. if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
  368. lenum = cpu_to_le64(owner);
  369. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  370. lenum = cpu_to_le64(owner_offset);
  371. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  372. }
  373. return ((u64)high_crc << 32) | (u64)low_crc;
  374. }
  375. static int match_extent_ref(struct extent_buffer *leaf,
  376. struct btrfs_extent_ref *disk_ref,
  377. struct btrfs_extent_ref *cpu_ref)
  378. {
  379. int ret;
  380. int len;
  381. if (cpu_ref->objectid)
  382. len = sizeof(*cpu_ref);
  383. else
  384. len = 2 * sizeof(u64);
  385. ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
  386. len);
  387. return ret == 0;
  388. }
  389. static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
  390. struct btrfs_root *root,
  391. struct btrfs_path *path, u64 bytenr,
  392. u64 root_objectid,
  393. u64 ref_generation, u64 owner,
  394. u64 owner_offset, int del)
  395. {
  396. u64 hash;
  397. struct btrfs_key key;
  398. struct btrfs_key found_key;
  399. struct btrfs_extent_ref ref;
  400. struct extent_buffer *leaf;
  401. struct btrfs_extent_ref *disk_ref;
  402. int ret;
  403. int ret2;
  404. btrfs_set_stack_ref_root(&ref, root_objectid);
  405. btrfs_set_stack_ref_generation(&ref, ref_generation);
  406. btrfs_set_stack_ref_objectid(&ref, owner);
  407. btrfs_set_stack_ref_offset(&ref, owner_offset);
  408. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  409. owner_offset);
  410. key.offset = hash;
  411. key.objectid = bytenr;
  412. key.type = BTRFS_EXTENT_REF_KEY;
  413. while (1) {
  414. ret = btrfs_search_slot(trans, root, &key, path,
  415. del ? -1 : 0, del);
  416. if (ret < 0)
  417. goto out;
  418. leaf = path->nodes[0];
  419. if (ret != 0) {
  420. u32 nritems = btrfs_header_nritems(leaf);
  421. if (path->slots[0] >= nritems) {
  422. ret2 = btrfs_next_leaf(root, path);
  423. if (ret2)
  424. goto out;
  425. leaf = path->nodes[0];
  426. }
  427. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  428. if (found_key.objectid != bytenr ||
  429. found_key.type != BTRFS_EXTENT_REF_KEY)
  430. goto out;
  431. key.offset = found_key.offset;
  432. if (del) {
  433. btrfs_release_path(root, path);
  434. continue;
  435. }
  436. }
  437. disk_ref = btrfs_item_ptr(path->nodes[0],
  438. path->slots[0],
  439. struct btrfs_extent_ref);
  440. if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
  441. ret = 0;
  442. goto out;
  443. }
  444. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  445. key.offset = found_key.offset + 1;
  446. btrfs_release_path(root, path);
  447. }
  448. out:
  449. return ret;
  450. }
  451. /*
  452. * Back reference rules. Back refs have three main goals:
  453. *
  454. * 1) differentiate between all holders of references to an extent so that
  455. * when a reference is dropped we can make sure it was a valid reference
  456. * before freeing the extent.
  457. *
  458. * 2) Provide enough information to quickly find the holders of an extent
  459. * if we notice a given block is corrupted or bad.
  460. *
  461. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  462. * maintenance. This is actually the same as #2, but with a slightly
  463. * different use case.
  464. *
  465. * File extents can be referenced by:
  466. *
  467. * - multiple snapshots, subvolumes, or different generations in one subvol
  468. * - different files inside a single subvolume (in theory, not implemented yet)
  469. * - different offsets inside a file (bookend extents in file.c)
  470. *
  471. * The extent ref structure has fields for:
  472. *
  473. * - Objectid of the subvolume root
  474. * - Generation number of the tree holding the reference
  475. * - objectid of the file holding the reference
  476. * - offset in the file corresponding to the key holding the reference
  477. *
  478. * When a file extent is allocated the fields are filled in:
  479. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  480. *
  481. * When a leaf is cow'd new references are added for every file extent found
  482. * in the leaf. It looks the same as the create case, but trans->transid
  483. * will be different when the block is cow'd.
  484. *
  485. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  486. *
  487. * When a file extent is removed either during snapshot deletion or file
  488. * truncation, the corresponding back reference is found
  489. * by searching for:
  490. *
  491. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  492. * inode objectid, offset in file)
  493. *
  494. * Btree extents can be referenced by:
  495. *
  496. * - Different subvolumes
  497. * - Different generations of the same subvolume
  498. *
  499. * Storing sufficient information for a full reverse mapping of a btree
  500. * block would require storing the lowest key of the block in the backref,
  501. * and it would require updating that lowest key either before write out or
  502. * every time it changed. Instead, the objectid of the lowest key is stored
  503. * along with the level of the tree block. This provides a hint
  504. * about where in the btree the block can be found. Searches through the
  505. * btree only need to look for a pointer to that block, so they stop one
  506. * level higher than the level recorded in the backref.
  507. *
  508. * Some btrees do not do reference counting on their extents. These
  509. * include the extent tree and the tree of tree roots. Backrefs for these
  510. * trees always have a generation of zero.
  511. *
  512. * When a tree block is created, back references are inserted:
  513. *
  514. * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
  515. *
  516. * When a tree block is cow'd in a reference counted root,
  517. * new back references are added for all the blocks it points to.
  518. * These are of the form (trans->transid will have increased since creation):
  519. *
  520. * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
  521. *
  522. * Because the lowest_key_objectid and the level are just hints
  523. * they are not used when backrefs are deleted. When a backref is deleted:
  524. *
  525. * if backref was for a tree root:
  526. * root_objectid = root->root_key.objectid
  527. * else
  528. * root_objectid = btrfs_header_owner(parent)
  529. *
  530. * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
  531. *
  532. * Back Reference Key hashing:
  533. *
  534. * Back references have four fields, each 64 bits long. Unfortunately,
  535. * This is hashed into a single 64 bit number and placed into the key offset.
  536. * The key objectid corresponds to the first byte in the extent, and the
  537. * key type is set to BTRFS_EXTENT_REF_KEY
  538. */
  539. int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
  540. struct btrfs_root *root,
  541. struct btrfs_path *path, u64 bytenr,
  542. u64 root_objectid, u64 ref_generation,
  543. u64 owner, u64 owner_offset)
  544. {
  545. u64 hash;
  546. struct btrfs_key key;
  547. struct btrfs_extent_ref ref;
  548. struct btrfs_extent_ref *disk_ref;
  549. int ret;
  550. btrfs_set_stack_ref_root(&ref, root_objectid);
  551. btrfs_set_stack_ref_generation(&ref, ref_generation);
  552. btrfs_set_stack_ref_objectid(&ref, owner);
  553. btrfs_set_stack_ref_offset(&ref, owner_offset);
  554. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  555. owner_offset);
  556. key.offset = hash;
  557. key.objectid = bytenr;
  558. key.type = BTRFS_EXTENT_REF_KEY;
  559. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
  560. while (ret == -EEXIST) {
  561. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  562. struct btrfs_extent_ref);
  563. if (match_extent_ref(path->nodes[0], disk_ref, &ref))
  564. goto out;
  565. key.offset++;
  566. btrfs_release_path(root, path);
  567. ret = btrfs_insert_empty_item(trans, root, path, &key,
  568. sizeof(ref));
  569. }
  570. if (ret)
  571. goto out;
  572. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  573. struct btrfs_extent_ref);
  574. write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
  575. sizeof(ref));
  576. btrfs_mark_buffer_dirty(path->nodes[0]);
  577. out:
  578. btrfs_release_path(root, path);
  579. return ret;
  580. }
  581. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  582. struct btrfs_root *root,
  583. u64 bytenr, u64 num_bytes,
  584. u64 root_objectid, u64 ref_generation,
  585. u64 owner, u64 owner_offset)
  586. {
  587. struct btrfs_path *path;
  588. int ret;
  589. struct btrfs_key key;
  590. struct extent_buffer *l;
  591. struct btrfs_extent_item *item;
  592. u32 refs;
  593. WARN_ON(num_bytes < root->sectorsize);
  594. path = btrfs_alloc_path();
  595. if (!path)
  596. return -ENOMEM;
  597. path->reada = 0;
  598. key.objectid = bytenr;
  599. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  600. key.offset = num_bytes;
  601. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  602. 0, 1);
  603. if (ret < 0)
  604. return ret;
  605. if (ret != 0) {
  606. BUG();
  607. }
  608. BUG_ON(ret != 0);
  609. l = path->nodes[0];
  610. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  611. refs = btrfs_extent_refs(l, item);
  612. btrfs_set_extent_refs(l, item, refs + 1);
  613. btrfs_mark_buffer_dirty(path->nodes[0]);
  614. btrfs_release_path(root->fs_info->extent_root, path);
  615. path->reada = 0;
  616. ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
  617. path, bytenr, root_objectid,
  618. ref_generation, owner, owner_offset);
  619. BUG_ON(ret);
  620. finish_current_insert(trans, root->fs_info->extent_root);
  621. del_pending_extents(trans, root->fs_info->extent_root);
  622. btrfs_free_path(path);
  623. return 0;
  624. }
  625. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  626. struct btrfs_root *root)
  627. {
  628. finish_current_insert(trans, root->fs_info->extent_root);
  629. del_pending_extents(trans, root->fs_info->extent_root);
  630. return 0;
  631. }
  632. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  633. struct btrfs_root *root, u64 bytenr,
  634. u64 num_bytes, u32 *refs)
  635. {
  636. struct btrfs_path *path;
  637. int ret;
  638. struct btrfs_key key;
  639. struct extent_buffer *l;
  640. struct btrfs_extent_item *item;
  641. WARN_ON(num_bytes < root->sectorsize);
  642. path = btrfs_alloc_path();
  643. path->reada = 0;
  644. key.objectid = bytenr;
  645. key.offset = num_bytes;
  646. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  647. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  648. 0, 0);
  649. if (ret < 0)
  650. goto out;
  651. if (ret != 0) {
  652. btrfs_print_leaf(root, path->nodes[0]);
  653. printk("failed to find block number %Lu\n", bytenr);
  654. BUG();
  655. }
  656. l = path->nodes[0];
  657. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  658. *refs = btrfs_extent_refs(l, item);
  659. out:
  660. btrfs_free_path(path);
  661. return 0;
  662. }
  663. u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
  664. struct btrfs_path *count_path,
  665. u64 first_extent)
  666. {
  667. struct btrfs_root *extent_root = root->fs_info->extent_root;
  668. struct btrfs_path *path;
  669. u64 bytenr;
  670. u64 found_objectid;
  671. u64 root_objectid = root->root_key.objectid;
  672. u32 total_count = 0;
  673. u32 cur_count;
  674. u32 nritems;
  675. int ret;
  676. struct btrfs_key key;
  677. struct btrfs_key found_key;
  678. struct extent_buffer *l;
  679. struct btrfs_extent_item *item;
  680. struct btrfs_extent_ref *ref_item;
  681. int level = -1;
  682. path = btrfs_alloc_path();
  683. again:
  684. if (level == -1)
  685. bytenr = first_extent;
  686. else
  687. bytenr = count_path->nodes[level]->start;
  688. cur_count = 0;
  689. key.objectid = bytenr;
  690. key.offset = 0;
  691. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  692. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  693. if (ret < 0)
  694. goto out;
  695. BUG_ON(ret == 0);
  696. l = path->nodes[0];
  697. btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
  698. if (found_key.objectid != bytenr ||
  699. found_key.type != BTRFS_EXTENT_ITEM_KEY) {
  700. goto out;
  701. }
  702. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  703. while (1) {
  704. l = path->nodes[0];
  705. nritems = btrfs_header_nritems(l);
  706. if (path->slots[0] >= nritems) {
  707. ret = btrfs_next_leaf(extent_root, path);
  708. if (ret == 0)
  709. continue;
  710. break;
  711. }
  712. btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
  713. if (found_key.objectid != bytenr)
  714. break;
  715. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  716. path->slots[0]++;
  717. continue;
  718. }
  719. cur_count++;
  720. ref_item = btrfs_item_ptr(l, path->slots[0],
  721. struct btrfs_extent_ref);
  722. found_objectid = btrfs_ref_root(l, ref_item);
  723. if (found_objectid != root_objectid) {
  724. total_count = 2;
  725. goto out;
  726. }
  727. total_count = 1;
  728. path->slots[0]++;
  729. }
  730. if (cur_count == 0) {
  731. total_count = 0;
  732. goto out;
  733. }
  734. if (level >= 0 && root->node == count_path->nodes[level])
  735. goto out;
  736. level++;
  737. btrfs_release_path(root, path);
  738. goto again;
  739. out:
  740. btrfs_free_path(path);
  741. return total_count;
  742. }
  743. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  744. struct btrfs_root *root, u64 owner_objectid)
  745. {
  746. u64 generation;
  747. u64 key_objectid;
  748. u64 level;
  749. u32 nritems;
  750. struct btrfs_disk_key disk_key;
  751. level = btrfs_header_level(root->node);
  752. generation = trans->transid;
  753. nritems = btrfs_header_nritems(root->node);
  754. if (nritems > 0) {
  755. if (level == 0)
  756. btrfs_item_key(root->node, &disk_key, 0);
  757. else
  758. btrfs_node_key(root->node, &disk_key, 0);
  759. key_objectid = btrfs_disk_key_objectid(&disk_key);
  760. } else {
  761. key_objectid = 0;
  762. }
  763. return btrfs_inc_extent_ref(trans, root, root->node->start,
  764. root->node->len, owner_objectid,
  765. generation, level, key_objectid);
  766. }
  767. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  768. struct extent_buffer *buf)
  769. {
  770. u64 bytenr;
  771. u32 nritems;
  772. struct btrfs_key key;
  773. struct btrfs_file_extent_item *fi;
  774. int i;
  775. int level;
  776. int ret;
  777. int faili;
  778. if (!root->ref_cows)
  779. return 0;
  780. level = btrfs_header_level(buf);
  781. nritems = btrfs_header_nritems(buf);
  782. for (i = 0; i < nritems; i++) {
  783. if (level == 0) {
  784. u64 disk_bytenr;
  785. btrfs_item_key_to_cpu(buf, &key, i);
  786. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  787. continue;
  788. fi = btrfs_item_ptr(buf, i,
  789. struct btrfs_file_extent_item);
  790. if (btrfs_file_extent_type(buf, fi) ==
  791. BTRFS_FILE_EXTENT_INLINE)
  792. continue;
  793. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  794. if (disk_bytenr == 0)
  795. continue;
  796. ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
  797. btrfs_file_extent_disk_num_bytes(buf, fi),
  798. root->root_key.objectid, trans->transid,
  799. key.objectid, key.offset);
  800. if (ret) {
  801. faili = i;
  802. goto fail;
  803. }
  804. } else {
  805. bytenr = btrfs_node_blockptr(buf, i);
  806. btrfs_node_key_to_cpu(buf, &key, i);
  807. ret = btrfs_inc_extent_ref(trans, root, bytenr,
  808. btrfs_level_size(root, level - 1),
  809. root->root_key.objectid,
  810. trans->transid,
  811. level - 1, key.objectid);
  812. if (ret) {
  813. faili = i;
  814. goto fail;
  815. }
  816. }
  817. }
  818. return 0;
  819. fail:
  820. WARN_ON(1);
  821. #if 0
  822. for (i =0; i < faili; i++) {
  823. if (level == 0) {
  824. u64 disk_bytenr;
  825. btrfs_item_key_to_cpu(buf, &key, i);
  826. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  827. continue;
  828. fi = btrfs_item_ptr(buf, i,
  829. struct btrfs_file_extent_item);
  830. if (btrfs_file_extent_type(buf, fi) ==
  831. BTRFS_FILE_EXTENT_INLINE)
  832. continue;
  833. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  834. if (disk_bytenr == 0)
  835. continue;
  836. err = btrfs_free_extent(trans, root, disk_bytenr,
  837. btrfs_file_extent_disk_num_bytes(buf,
  838. fi), 0);
  839. BUG_ON(err);
  840. } else {
  841. bytenr = btrfs_node_blockptr(buf, i);
  842. err = btrfs_free_extent(trans, root, bytenr,
  843. btrfs_level_size(root, level - 1), 0);
  844. BUG_ON(err);
  845. }
  846. }
  847. #endif
  848. return ret;
  849. }
  850. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  851. struct btrfs_root *root,
  852. struct btrfs_path *path,
  853. struct btrfs_block_group_cache *cache)
  854. {
  855. int ret;
  856. int pending_ret;
  857. struct btrfs_root *extent_root = root->fs_info->extent_root;
  858. unsigned long bi;
  859. struct extent_buffer *leaf;
  860. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  861. if (ret < 0)
  862. goto fail;
  863. BUG_ON(ret);
  864. leaf = path->nodes[0];
  865. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  866. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  867. btrfs_mark_buffer_dirty(leaf);
  868. btrfs_release_path(extent_root, path);
  869. fail:
  870. finish_current_insert(trans, extent_root);
  871. pending_ret = del_pending_extents(trans, extent_root);
  872. if (ret)
  873. return ret;
  874. if (pending_ret)
  875. return pending_ret;
  876. return 0;
  877. }
  878. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  879. struct btrfs_root *root)
  880. {
  881. struct extent_io_tree *block_group_cache;
  882. struct btrfs_block_group_cache *cache;
  883. int ret;
  884. int err = 0;
  885. int werr = 0;
  886. struct btrfs_path *path;
  887. u64 last = 0;
  888. u64 start;
  889. u64 end;
  890. u64 ptr;
  891. block_group_cache = &root->fs_info->block_group_cache;
  892. path = btrfs_alloc_path();
  893. if (!path)
  894. return -ENOMEM;
  895. while(1) {
  896. ret = find_first_extent_bit(block_group_cache, last,
  897. &start, &end, BLOCK_GROUP_DIRTY);
  898. if (ret)
  899. break;
  900. last = end + 1;
  901. ret = get_state_private(block_group_cache, start, &ptr);
  902. if (ret)
  903. break;
  904. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  905. err = write_one_cache_group(trans, root,
  906. path, cache);
  907. /*
  908. * if we fail to write the cache group, we want
  909. * to keep it marked dirty in hopes that a later
  910. * write will work
  911. */
  912. if (err) {
  913. werr = err;
  914. continue;
  915. }
  916. clear_extent_bits(block_group_cache, start, end,
  917. BLOCK_GROUP_DIRTY, GFP_NOFS);
  918. }
  919. btrfs_free_path(path);
  920. return werr;
  921. }
  922. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  923. u64 flags)
  924. {
  925. struct list_head *head = &info->space_info;
  926. struct list_head *cur;
  927. struct btrfs_space_info *found;
  928. list_for_each(cur, head) {
  929. found = list_entry(cur, struct btrfs_space_info, list);
  930. if (found->flags == flags)
  931. return found;
  932. }
  933. return NULL;
  934. }
  935. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  936. u64 total_bytes, u64 bytes_used,
  937. struct btrfs_space_info **space_info)
  938. {
  939. struct btrfs_space_info *found;
  940. found = __find_space_info(info, flags);
  941. if (found) {
  942. found->total_bytes += total_bytes;
  943. found->bytes_used += bytes_used;
  944. WARN_ON(found->total_bytes < found->bytes_used);
  945. *space_info = found;
  946. return 0;
  947. }
  948. found = kmalloc(sizeof(*found), GFP_NOFS);
  949. if (!found)
  950. return -ENOMEM;
  951. list_add(&found->list, &info->space_info);
  952. found->flags = flags;
  953. found->total_bytes = total_bytes;
  954. found->bytes_used = bytes_used;
  955. found->bytes_pinned = 0;
  956. found->full = 0;
  957. *space_info = found;
  958. return 0;
  959. }
  960. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  961. {
  962. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  963. BTRFS_BLOCK_GROUP_RAID1 |
  964. BTRFS_BLOCK_GROUP_DUP);
  965. if (extra_flags) {
  966. if (flags & BTRFS_BLOCK_GROUP_DATA)
  967. fs_info->avail_data_alloc_bits |= extra_flags;
  968. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  969. fs_info->avail_metadata_alloc_bits |= extra_flags;
  970. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  971. fs_info->avail_system_alloc_bits |= extra_flags;
  972. }
  973. }
  974. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  975. struct btrfs_root *extent_root, u64 alloc_bytes,
  976. u64 flags)
  977. {
  978. struct btrfs_space_info *space_info;
  979. u64 thresh;
  980. u64 start;
  981. u64 num_bytes;
  982. int ret;
  983. space_info = __find_space_info(extent_root->fs_info, flags);
  984. if (!space_info) {
  985. ret = update_space_info(extent_root->fs_info, flags,
  986. 0, 0, &space_info);
  987. BUG_ON(ret);
  988. }
  989. BUG_ON(!space_info);
  990. if (space_info->full)
  991. return 0;
  992. thresh = div_factor(space_info->total_bytes, 6);
  993. if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
  994. thresh)
  995. return 0;
  996. ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
  997. if (ret == -ENOSPC) {
  998. printk("space info full %Lu\n", flags);
  999. space_info->full = 1;
  1000. return 0;
  1001. }
  1002. BUG_ON(ret);
  1003. ret = btrfs_make_block_group(trans, extent_root, 0, flags,
  1004. extent_root->fs_info->chunk_root->root_key.objectid,
  1005. start, num_bytes);
  1006. BUG_ON(ret);
  1007. set_avail_alloc_bits(extent_root->fs_info, flags);
  1008. return 0;
  1009. }
  1010. static int update_block_group(struct btrfs_trans_handle *trans,
  1011. struct btrfs_root *root,
  1012. u64 bytenr, u64 num_bytes, int alloc,
  1013. int mark_free)
  1014. {
  1015. struct btrfs_block_group_cache *cache;
  1016. struct btrfs_fs_info *info = root->fs_info;
  1017. u64 total = num_bytes;
  1018. u64 old_val;
  1019. u64 byte_in_group;
  1020. u64 start;
  1021. u64 end;
  1022. while(total) {
  1023. cache = btrfs_lookup_block_group(info, bytenr);
  1024. if (!cache) {
  1025. return -1;
  1026. }
  1027. byte_in_group = bytenr - cache->key.objectid;
  1028. WARN_ON(byte_in_group > cache->key.offset);
  1029. start = cache->key.objectid;
  1030. end = start + cache->key.offset - 1;
  1031. set_extent_bits(&info->block_group_cache, start, end,
  1032. BLOCK_GROUP_DIRTY, GFP_NOFS);
  1033. old_val = btrfs_block_group_used(&cache->item);
  1034. num_bytes = min(total, cache->key.offset - byte_in_group);
  1035. if (alloc) {
  1036. old_val += num_bytes;
  1037. cache->space_info->bytes_used += num_bytes;
  1038. } else {
  1039. old_val -= num_bytes;
  1040. cache->space_info->bytes_used -= num_bytes;
  1041. if (mark_free) {
  1042. set_extent_dirty(&info->free_space_cache,
  1043. bytenr, bytenr + num_bytes - 1,
  1044. GFP_NOFS);
  1045. }
  1046. }
  1047. btrfs_set_block_group_used(&cache->item, old_val);
  1048. total -= num_bytes;
  1049. bytenr += num_bytes;
  1050. }
  1051. return 0;
  1052. }
  1053. static int update_pinned_extents(struct btrfs_root *root,
  1054. u64 bytenr, u64 num, int pin)
  1055. {
  1056. u64 len;
  1057. struct btrfs_block_group_cache *cache;
  1058. struct btrfs_fs_info *fs_info = root->fs_info;
  1059. if (pin) {
  1060. set_extent_dirty(&fs_info->pinned_extents,
  1061. bytenr, bytenr + num - 1, GFP_NOFS);
  1062. } else {
  1063. clear_extent_dirty(&fs_info->pinned_extents,
  1064. bytenr, bytenr + num - 1, GFP_NOFS);
  1065. }
  1066. while (num > 0) {
  1067. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1068. WARN_ON(!cache);
  1069. len = min(num, cache->key.offset -
  1070. (bytenr - cache->key.objectid));
  1071. if (pin) {
  1072. cache->pinned += len;
  1073. cache->space_info->bytes_pinned += len;
  1074. fs_info->total_pinned += len;
  1075. } else {
  1076. cache->pinned -= len;
  1077. cache->space_info->bytes_pinned -= len;
  1078. fs_info->total_pinned -= len;
  1079. }
  1080. bytenr += len;
  1081. num -= len;
  1082. }
  1083. return 0;
  1084. }
  1085. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1086. {
  1087. u64 last = 0;
  1088. u64 start;
  1089. u64 end;
  1090. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1091. int ret;
  1092. while(1) {
  1093. ret = find_first_extent_bit(pinned_extents, last,
  1094. &start, &end, EXTENT_DIRTY);
  1095. if (ret)
  1096. break;
  1097. set_extent_dirty(copy, start, end, GFP_NOFS);
  1098. last = end + 1;
  1099. }
  1100. return 0;
  1101. }
  1102. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1103. struct btrfs_root *root,
  1104. struct extent_io_tree *unpin)
  1105. {
  1106. u64 start;
  1107. u64 end;
  1108. int ret;
  1109. struct extent_io_tree *free_space_cache;
  1110. free_space_cache = &root->fs_info->free_space_cache;
  1111. while(1) {
  1112. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1113. EXTENT_DIRTY);
  1114. if (ret)
  1115. break;
  1116. update_pinned_extents(root, start, end + 1 - start, 0);
  1117. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1118. set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
  1119. }
  1120. return 0;
  1121. }
  1122. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1123. struct btrfs_root *extent_root)
  1124. {
  1125. u64 start;
  1126. u64 end;
  1127. struct btrfs_fs_info *info = extent_root->fs_info;
  1128. struct extent_buffer *eb;
  1129. struct btrfs_path *path;
  1130. struct btrfs_key ins;
  1131. struct btrfs_disk_key first;
  1132. struct btrfs_extent_item extent_item;
  1133. int ret;
  1134. int level;
  1135. int err = 0;
  1136. btrfs_set_stack_extent_refs(&extent_item, 1);
  1137. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  1138. path = btrfs_alloc_path();
  1139. while(1) {
  1140. ret = find_first_extent_bit(&info->extent_ins, 0, &start,
  1141. &end, EXTENT_LOCKED);
  1142. if (ret)
  1143. break;
  1144. ins.objectid = start;
  1145. ins.offset = end + 1 - start;
  1146. err = btrfs_insert_item(trans, extent_root, &ins,
  1147. &extent_item, sizeof(extent_item));
  1148. clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
  1149. GFP_NOFS);
  1150. eb = read_tree_block(extent_root, ins.objectid, ins.offset);
  1151. level = btrfs_header_level(eb);
  1152. if (level == 0) {
  1153. btrfs_item_key(eb, &first, 0);
  1154. } else {
  1155. btrfs_node_key(eb, &first, 0);
  1156. }
  1157. err = btrfs_insert_extent_backref(trans, extent_root, path,
  1158. start, extent_root->root_key.objectid,
  1159. 0, level,
  1160. btrfs_disk_key_objectid(&first));
  1161. BUG_ON(err);
  1162. free_extent_buffer(eb);
  1163. }
  1164. btrfs_free_path(path);
  1165. return 0;
  1166. }
  1167. static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
  1168. int pending)
  1169. {
  1170. int err = 0;
  1171. struct extent_buffer *buf;
  1172. if (!pending) {
  1173. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  1174. if (buf) {
  1175. if (btrfs_buffer_uptodate(buf)) {
  1176. u64 transid =
  1177. root->fs_info->running_transaction->transid;
  1178. u64 header_transid =
  1179. btrfs_header_generation(buf);
  1180. if (header_transid == transid) {
  1181. clean_tree_block(NULL, root, buf);
  1182. free_extent_buffer(buf);
  1183. return 1;
  1184. }
  1185. }
  1186. free_extent_buffer(buf);
  1187. }
  1188. update_pinned_extents(root, bytenr, num_bytes, 1);
  1189. } else {
  1190. set_extent_bits(&root->fs_info->pending_del,
  1191. bytenr, bytenr + num_bytes - 1,
  1192. EXTENT_LOCKED, GFP_NOFS);
  1193. }
  1194. BUG_ON(err < 0);
  1195. return 0;
  1196. }
  1197. /*
  1198. * remove an extent from the root, returns 0 on success
  1199. */
  1200. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1201. *root, u64 bytenr, u64 num_bytes,
  1202. u64 root_objectid, u64 ref_generation,
  1203. u64 owner_objectid, u64 owner_offset, int pin,
  1204. int mark_free)
  1205. {
  1206. struct btrfs_path *path;
  1207. struct btrfs_key key;
  1208. struct btrfs_fs_info *info = root->fs_info;
  1209. struct btrfs_root *extent_root = info->extent_root;
  1210. struct extent_buffer *leaf;
  1211. int ret;
  1212. int extent_slot = 0;
  1213. int found_extent = 0;
  1214. int num_to_del = 1;
  1215. struct btrfs_extent_item *ei;
  1216. u32 refs;
  1217. key.objectid = bytenr;
  1218. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1219. key.offset = num_bytes;
  1220. path = btrfs_alloc_path();
  1221. if (!path)
  1222. return -ENOMEM;
  1223. path->reada = 0;
  1224. ret = lookup_extent_backref(trans, extent_root, path,
  1225. bytenr, root_objectid,
  1226. ref_generation,
  1227. owner_objectid, owner_offset, 1);
  1228. if (ret == 0) {
  1229. struct btrfs_key found_key;
  1230. extent_slot = path->slots[0];
  1231. while(extent_slot > 0) {
  1232. extent_slot--;
  1233. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1234. extent_slot);
  1235. if (found_key.objectid != bytenr)
  1236. break;
  1237. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  1238. found_key.offset == num_bytes) {
  1239. found_extent = 1;
  1240. break;
  1241. }
  1242. if (path->slots[0] - extent_slot > 5)
  1243. break;
  1244. }
  1245. if (!found_extent)
  1246. ret = btrfs_del_item(trans, extent_root, path);
  1247. } else {
  1248. btrfs_print_leaf(extent_root, path->nodes[0]);
  1249. WARN_ON(1);
  1250. printk("Unable to find ref byte nr %Lu root %Lu "
  1251. " gen %Lu owner %Lu offset %Lu\n", bytenr,
  1252. root_objectid, ref_generation, owner_objectid,
  1253. owner_offset);
  1254. }
  1255. if (!found_extent) {
  1256. btrfs_release_path(extent_root, path);
  1257. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  1258. if (ret < 0)
  1259. return ret;
  1260. BUG_ON(ret);
  1261. extent_slot = path->slots[0];
  1262. }
  1263. leaf = path->nodes[0];
  1264. ei = btrfs_item_ptr(leaf, extent_slot,
  1265. struct btrfs_extent_item);
  1266. refs = btrfs_extent_refs(leaf, ei);
  1267. BUG_ON(refs == 0);
  1268. refs -= 1;
  1269. btrfs_set_extent_refs(leaf, ei, refs);
  1270. btrfs_mark_buffer_dirty(leaf);
  1271. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  1272. /* if the back ref and the extent are next to each other
  1273. * they get deleted below in one shot
  1274. */
  1275. path->slots[0] = extent_slot;
  1276. num_to_del = 2;
  1277. } else if (found_extent) {
  1278. /* otherwise delete the extent back ref */
  1279. ret = btrfs_del_item(trans, extent_root, path);
  1280. BUG_ON(ret);
  1281. /* if refs are 0, we need to setup the path for deletion */
  1282. if (refs == 0) {
  1283. btrfs_release_path(extent_root, path);
  1284. ret = btrfs_search_slot(trans, extent_root, &key, path,
  1285. -1, 1);
  1286. if (ret < 0)
  1287. return ret;
  1288. BUG_ON(ret);
  1289. }
  1290. }
  1291. if (refs == 0) {
  1292. u64 super_used;
  1293. u64 root_used;
  1294. if (pin) {
  1295. ret = pin_down_bytes(root, bytenr, num_bytes, 0);
  1296. if (ret > 0)
  1297. mark_free = 1;
  1298. BUG_ON(ret < 0);
  1299. }
  1300. /* block accounting for super block */
  1301. super_used = btrfs_super_bytes_used(&info->super_copy);
  1302. btrfs_set_super_bytes_used(&info->super_copy,
  1303. super_used - num_bytes);
  1304. /* block accounting for root item */
  1305. root_used = btrfs_root_used(&root->root_item);
  1306. btrfs_set_root_used(&root->root_item,
  1307. root_used - num_bytes);
  1308. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  1309. num_to_del);
  1310. if (ret) {
  1311. return ret;
  1312. }
  1313. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1314. mark_free);
  1315. BUG_ON(ret);
  1316. }
  1317. btrfs_free_path(path);
  1318. finish_current_insert(trans, extent_root);
  1319. return ret;
  1320. }
  1321. /*
  1322. * find all the blocks marked as pending in the radix tree and remove
  1323. * them from the extent map
  1324. */
  1325. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1326. btrfs_root *extent_root)
  1327. {
  1328. int ret;
  1329. int err = 0;
  1330. u64 start;
  1331. u64 end;
  1332. struct extent_io_tree *pending_del;
  1333. struct extent_io_tree *pinned_extents;
  1334. pending_del = &extent_root->fs_info->pending_del;
  1335. pinned_extents = &extent_root->fs_info->pinned_extents;
  1336. while(1) {
  1337. ret = find_first_extent_bit(pending_del, 0, &start, &end,
  1338. EXTENT_LOCKED);
  1339. if (ret)
  1340. break;
  1341. update_pinned_extents(extent_root, start, end + 1 - start, 1);
  1342. clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
  1343. GFP_NOFS);
  1344. ret = __free_extent(trans, extent_root,
  1345. start, end + 1 - start,
  1346. extent_root->root_key.objectid,
  1347. 0, 0, 0, 0, 0);
  1348. if (ret)
  1349. err = ret;
  1350. }
  1351. return err;
  1352. }
  1353. /*
  1354. * remove an extent from the root, returns 0 on success
  1355. */
  1356. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1357. *root, u64 bytenr, u64 num_bytes,
  1358. u64 root_objectid, u64 ref_generation,
  1359. u64 owner_objectid, u64 owner_offset, int pin)
  1360. {
  1361. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1362. int pending_ret;
  1363. int ret;
  1364. WARN_ON(num_bytes < root->sectorsize);
  1365. if (!root->ref_cows)
  1366. ref_generation = 0;
  1367. if (root == extent_root) {
  1368. pin_down_bytes(root, bytenr, num_bytes, 1);
  1369. return 0;
  1370. }
  1371. ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
  1372. ref_generation, owner_objectid, owner_offset,
  1373. pin, pin == 0);
  1374. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  1375. return ret ? ret : pending_ret;
  1376. }
  1377. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1378. {
  1379. u64 mask = ((u64)root->stripesize - 1);
  1380. u64 ret = (val + mask) & ~mask;
  1381. return ret;
  1382. }
  1383. /*
  1384. * walks the btree of allocated extents and find a hole of a given size.
  1385. * The key ins is changed to record the hole:
  1386. * ins->objectid == block start
  1387. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1388. * ins->offset == number of blocks
  1389. * Any available blocks before search_start are skipped.
  1390. */
  1391. static int noinline find_free_extent(struct btrfs_trans_handle *trans,
  1392. struct btrfs_root *orig_root,
  1393. u64 num_bytes, u64 empty_size,
  1394. u64 search_start, u64 search_end,
  1395. u64 hint_byte, struct btrfs_key *ins,
  1396. u64 exclude_start, u64 exclude_nr,
  1397. int data)
  1398. {
  1399. int ret;
  1400. u64 orig_search_start = search_start;
  1401. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1402. struct btrfs_fs_info *info = root->fs_info;
  1403. u64 total_needed = num_bytes;
  1404. u64 *last_ptr = NULL;
  1405. struct btrfs_block_group_cache *block_group;
  1406. int full_scan = 0;
  1407. int wrapped = 0;
  1408. int empty_cluster = 2 * 1024 * 1024;
  1409. WARN_ON(num_bytes < root->sectorsize);
  1410. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1411. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  1412. last_ptr = &root->fs_info->last_alloc;
  1413. empty_cluster = 256 * 1024;
  1414. }
  1415. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
  1416. last_ptr = &root->fs_info->last_data_alloc;
  1417. }
  1418. if (last_ptr) {
  1419. if (*last_ptr)
  1420. hint_byte = *last_ptr;
  1421. else {
  1422. empty_size += empty_cluster;
  1423. }
  1424. }
  1425. if (search_end == (u64)-1)
  1426. search_end = btrfs_super_total_bytes(&info->super_copy);
  1427. if (hint_byte) {
  1428. block_group = btrfs_lookup_block_group(info, hint_byte);
  1429. if (!block_group)
  1430. hint_byte = search_start;
  1431. block_group = btrfs_find_block_group(root, block_group,
  1432. hint_byte, data, 1);
  1433. if (last_ptr && *last_ptr == 0 && block_group)
  1434. hint_byte = block_group->key.objectid;
  1435. } else {
  1436. block_group = btrfs_find_block_group(root,
  1437. trans->block_group,
  1438. search_start, data, 1);
  1439. }
  1440. search_start = max(search_start, hint_byte);
  1441. total_needed += empty_size;
  1442. check_failed:
  1443. if (!block_group) {
  1444. block_group = btrfs_lookup_block_group(info, search_start);
  1445. if (!block_group)
  1446. block_group = btrfs_lookup_block_group(info,
  1447. orig_search_start);
  1448. }
  1449. ret = find_search_start(root, &block_group, &search_start,
  1450. total_needed, data);
  1451. if (ret == -ENOSPC && last_ptr && *last_ptr) {
  1452. *last_ptr = 0;
  1453. block_group = btrfs_lookup_block_group(info,
  1454. orig_search_start);
  1455. search_start = orig_search_start;
  1456. ret = find_search_start(root, &block_group, &search_start,
  1457. total_needed, data);
  1458. }
  1459. if (ret == -ENOSPC)
  1460. goto enospc;
  1461. if (ret)
  1462. goto error;
  1463. if (last_ptr && *last_ptr && search_start != *last_ptr) {
  1464. *last_ptr = 0;
  1465. if (!empty_size) {
  1466. empty_size += empty_cluster;
  1467. total_needed += empty_size;
  1468. }
  1469. block_group = btrfs_lookup_block_group(info,
  1470. orig_search_start);
  1471. search_start = orig_search_start;
  1472. ret = find_search_start(root, &block_group,
  1473. &search_start, total_needed, data);
  1474. if (ret == -ENOSPC)
  1475. goto enospc;
  1476. if (ret)
  1477. goto error;
  1478. }
  1479. search_start = stripe_align(root, search_start);
  1480. ins->objectid = search_start;
  1481. ins->offset = num_bytes;
  1482. if (ins->objectid + num_bytes >= search_end)
  1483. goto enospc;
  1484. if (ins->objectid + num_bytes >
  1485. block_group->key.objectid + block_group->key.offset) {
  1486. search_start = block_group->key.objectid +
  1487. block_group->key.offset;
  1488. goto new_group;
  1489. }
  1490. if (test_range_bit(&info->extent_ins, ins->objectid,
  1491. ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
  1492. search_start = ins->objectid + num_bytes;
  1493. goto new_group;
  1494. }
  1495. if (test_range_bit(&info->pinned_extents, ins->objectid,
  1496. ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
  1497. search_start = ins->objectid + num_bytes;
  1498. goto new_group;
  1499. }
  1500. if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
  1501. ins->objectid < exclude_start + exclude_nr)) {
  1502. search_start = exclude_start + exclude_nr;
  1503. goto new_group;
  1504. }
  1505. if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
  1506. block_group = btrfs_lookup_block_group(info, ins->objectid);
  1507. if (block_group)
  1508. trans->block_group = block_group;
  1509. }
  1510. ins->offset = num_bytes;
  1511. if (last_ptr) {
  1512. *last_ptr = ins->objectid + ins->offset;
  1513. if (*last_ptr ==
  1514. btrfs_super_total_bytes(&root->fs_info->super_copy)) {
  1515. *last_ptr = 0;
  1516. }
  1517. }
  1518. return 0;
  1519. new_group:
  1520. if (search_start + num_bytes >= search_end) {
  1521. enospc:
  1522. search_start = orig_search_start;
  1523. if (full_scan) {
  1524. ret = -ENOSPC;
  1525. goto error;
  1526. }
  1527. if (wrapped) {
  1528. if (!full_scan)
  1529. total_needed -= empty_size;
  1530. full_scan = 1;
  1531. } else
  1532. wrapped = 1;
  1533. }
  1534. block_group = btrfs_lookup_block_group(info, search_start);
  1535. cond_resched();
  1536. block_group = btrfs_find_block_group(root, block_group,
  1537. search_start, data, 0);
  1538. goto check_failed;
  1539. error:
  1540. return ret;
  1541. }
  1542. /*
  1543. * finds a free extent and does all the dirty work required for allocation
  1544. * returns the key for the extent through ins, and a tree buffer for
  1545. * the first block of the extent through buf.
  1546. *
  1547. * returns 0 if everything worked, non-zero otherwise.
  1548. */
  1549. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  1550. struct btrfs_root *root,
  1551. u64 num_bytes, u64 root_objectid, u64 ref_generation,
  1552. u64 owner, u64 owner_offset,
  1553. u64 empty_size, u64 hint_byte,
  1554. u64 search_end, struct btrfs_key *ins, int data)
  1555. {
  1556. int ret;
  1557. int pending_ret;
  1558. u64 super_used;
  1559. u64 root_used;
  1560. u64 search_start = 0;
  1561. u64 new_hint;
  1562. u64 alloc_profile;
  1563. u32 sizes[2];
  1564. struct btrfs_fs_info *info = root->fs_info;
  1565. struct btrfs_root *extent_root = info->extent_root;
  1566. struct btrfs_extent_item *extent_item;
  1567. struct btrfs_extent_ref *ref;
  1568. struct btrfs_path *path;
  1569. struct btrfs_key keys[2];
  1570. if (data) {
  1571. alloc_profile = info->avail_data_alloc_bits &
  1572. info->data_alloc_profile;
  1573. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  1574. } else if (root == root->fs_info->chunk_root) {
  1575. alloc_profile = info->avail_system_alloc_bits &
  1576. info->system_alloc_profile;
  1577. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  1578. } else {
  1579. alloc_profile = info->avail_metadata_alloc_bits &
  1580. info->metadata_alloc_profile;
  1581. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  1582. }
  1583. if (root->ref_cows) {
  1584. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  1585. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1586. 2 * 1024 * 1024,
  1587. BTRFS_BLOCK_GROUP_METADATA |
  1588. (info->metadata_alloc_profile &
  1589. info->avail_metadata_alloc_bits));
  1590. BUG_ON(ret);
  1591. }
  1592. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1593. num_bytes + 2 * 1024 * 1024, data);
  1594. BUG_ON(ret);
  1595. }
  1596. new_hint = max(hint_byte, root->fs_info->alloc_start);
  1597. if (new_hint < btrfs_super_total_bytes(&info->super_copy))
  1598. hint_byte = new_hint;
  1599. WARN_ON(num_bytes < root->sectorsize);
  1600. ret = find_free_extent(trans, root, num_bytes, empty_size,
  1601. search_start, search_end, hint_byte, ins,
  1602. trans->alloc_exclude_start,
  1603. trans->alloc_exclude_nr, data);
  1604. BUG_ON(ret);
  1605. if (ret)
  1606. return ret;
  1607. /* block accounting for super block */
  1608. super_used = btrfs_super_bytes_used(&info->super_copy);
  1609. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  1610. /* block accounting for root item */
  1611. root_used = btrfs_root_used(&root->root_item);
  1612. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  1613. clear_extent_dirty(&root->fs_info->free_space_cache,
  1614. ins->objectid, ins->objectid + ins->offset - 1,
  1615. GFP_NOFS);
  1616. if (root == extent_root) {
  1617. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  1618. ins->objectid + ins->offset - 1,
  1619. EXTENT_LOCKED, GFP_NOFS);
  1620. goto update_block;
  1621. }
  1622. WARN_ON(trans->alloc_exclude_nr);
  1623. trans->alloc_exclude_start = ins->objectid;
  1624. trans->alloc_exclude_nr = ins->offset;
  1625. memcpy(&keys[0], ins, sizeof(*ins));
  1626. keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
  1627. owner, owner_offset);
  1628. keys[1].objectid = ins->objectid;
  1629. keys[1].type = BTRFS_EXTENT_REF_KEY;
  1630. sizes[0] = sizeof(*extent_item);
  1631. sizes[1] = sizeof(*ref);
  1632. path = btrfs_alloc_path();
  1633. BUG_ON(!path);
  1634. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  1635. sizes, 2);
  1636. BUG_ON(ret);
  1637. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1638. struct btrfs_extent_item);
  1639. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  1640. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  1641. struct btrfs_extent_ref);
  1642. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  1643. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  1644. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  1645. btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
  1646. btrfs_mark_buffer_dirty(path->nodes[0]);
  1647. trans->alloc_exclude_start = 0;
  1648. trans->alloc_exclude_nr = 0;
  1649. btrfs_free_path(path);
  1650. finish_current_insert(trans, extent_root);
  1651. pending_ret = del_pending_extents(trans, extent_root);
  1652. if (ret) {
  1653. return ret;
  1654. }
  1655. if (pending_ret) {
  1656. return pending_ret;
  1657. }
  1658. update_block:
  1659. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
  1660. if (ret) {
  1661. printk("update block group failed for %Lu %Lu\n",
  1662. ins->objectid, ins->offset);
  1663. BUG();
  1664. }
  1665. return 0;
  1666. }
  1667. /*
  1668. * helper function to allocate a block for a given tree
  1669. * returns the tree buffer or NULL.
  1670. */
  1671. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1672. struct btrfs_root *root,
  1673. u32 blocksize,
  1674. u64 root_objectid, u64 hint,
  1675. u64 empty_size)
  1676. {
  1677. u64 ref_generation;
  1678. if (root->ref_cows)
  1679. ref_generation = trans->transid;
  1680. else
  1681. ref_generation = 0;
  1682. return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
  1683. ref_generation, 0, 0, hint, empty_size);
  1684. }
  1685. /*
  1686. * helper function to allocate a block for a given tree
  1687. * returns the tree buffer or NULL.
  1688. */
  1689. struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1690. struct btrfs_root *root,
  1691. u32 blocksize,
  1692. u64 root_objectid,
  1693. u64 ref_generation,
  1694. u64 first_objectid,
  1695. int level,
  1696. u64 hint,
  1697. u64 empty_size)
  1698. {
  1699. struct btrfs_key ins;
  1700. int ret;
  1701. struct extent_buffer *buf;
  1702. ret = btrfs_alloc_extent(trans, root, blocksize,
  1703. root_objectid, ref_generation,
  1704. level, first_objectid, empty_size, hint,
  1705. (u64)-1, &ins, 0);
  1706. if (ret) {
  1707. BUG_ON(ret > 0);
  1708. return ERR_PTR(ret);
  1709. }
  1710. buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
  1711. if (!buf) {
  1712. btrfs_free_extent(trans, root, ins.objectid, blocksize,
  1713. root->root_key.objectid, ref_generation,
  1714. 0, 0, 0);
  1715. return ERR_PTR(-ENOMEM);
  1716. }
  1717. btrfs_set_header_generation(buf, trans->transid);
  1718. clean_tree_block(trans, root, buf);
  1719. wait_on_tree_block_writeback(root, buf);
  1720. btrfs_set_buffer_uptodate(buf);
  1721. if (PageDirty(buf->first_page)) {
  1722. printk("page %lu dirty\n", buf->first_page->index);
  1723. WARN_ON(1);
  1724. }
  1725. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  1726. buf->start + buf->len - 1, GFP_NOFS);
  1727. set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->io_tree,
  1728. buf->start, buf->start + buf->len - 1,
  1729. EXTENT_CSUM, GFP_NOFS);
  1730. buf->flags |= EXTENT_CSUM;
  1731. if (!btrfs_test_opt(root, SSD))
  1732. btrfs_set_buffer_defrag(buf);
  1733. trans->blocks_used++;
  1734. return buf;
  1735. }
  1736. static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
  1737. struct btrfs_root *root,
  1738. struct extent_buffer *leaf)
  1739. {
  1740. u64 leaf_owner;
  1741. u64 leaf_generation;
  1742. struct btrfs_key key;
  1743. struct btrfs_file_extent_item *fi;
  1744. int i;
  1745. int nritems;
  1746. int ret;
  1747. BUG_ON(!btrfs_is_leaf(leaf));
  1748. nritems = btrfs_header_nritems(leaf);
  1749. leaf_owner = btrfs_header_owner(leaf);
  1750. leaf_generation = btrfs_header_generation(leaf);
  1751. for (i = 0; i < nritems; i++) {
  1752. u64 disk_bytenr;
  1753. btrfs_item_key_to_cpu(leaf, &key, i);
  1754. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1755. continue;
  1756. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  1757. if (btrfs_file_extent_type(leaf, fi) ==
  1758. BTRFS_FILE_EXTENT_INLINE)
  1759. continue;
  1760. /*
  1761. * FIXME make sure to insert a trans record that
  1762. * repeats the snapshot del on crash
  1763. */
  1764. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1765. if (disk_bytenr == 0)
  1766. continue;
  1767. ret = btrfs_free_extent(trans, root, disk_bytenr,
  1768. btrfs_file_extent_disk_num_bytes(leaf, fi),
  1769. leaf_owner, leaf_generation,
  1770. key.objectid, key.offset, 0);
  1771. BUG_ON(ret);
  1772. }
  1773. return 0;
  1774. }
  1775. static void noinline reada_walk_down(struct btrfs_root *root,
  1776. struct extent_buffer *node,
  1777. int slot)
  1778. {
  1779. u64 bytenr;
  1780. u64 last = 0;
  1781. u32 nritems;
  1782. u32 refs;
  1783. u32 blocksize;
  1784. int ret;
  1785. int i;
  1786. int level;
  1787. int skipped = 0;
  1788. nritems = btrfs_header_nritems(node);
  1789. level = btrfs_header_level(node);
  1790. if (level)
  1791. return;
  1792. for (i = slot; i < nritems && skipped < 32; i++) {
  1793. bytenr = btrfs_node_blockptr(node, i);
  1794. if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
  1795. (last > bytenr && last - bytenr > 32 * 1024))) {
  1796. skipped++;
  1797. continue;
  1798. }
  1799. blocksize = btrfs_level_size(root, level - 1);
  1800. if (i != slot) {
  1801. ret = lookup_extent_ref(NULL, root, bytenr,
  1802. blocksize, &refs);
  1803. BUG_ON(ret);
  1804. if (refs != 1) {
  1805. skipped++;
  1806. continue;
  1807. }
  1808. }
  1809. mutex_unlock(&root->fs_info->fs_mutex);
  1810. ret = readahead_tree_block(root, bytenr, blocksize);
  1811. last = bytenr + blocksize;
  1812. cond_resched();
  1813. mutex_lock(&root->fs_info->fs_mutex);
  1814. if (ret)
  1815. break;
  1816. }
  1817. }
  1818. /*
  1819. * helper function for drop_snapshot, this walks down the tree dropping ref
  1820. * counts as it goes.
  1821. */
  1822. static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
  1823. struct btrfs_root *root,
  1824. struct btrfs_path *path, int *level)
  1825. {
  1826. u64 root_owner;
  1827. u64 root_gen;
  1828. u64 bytenr;
  1829. struct extent_buffer *next;
  1830. struct extent_buffer *cur;
  1831. struct extent_buffer *parent;
  1832. u32 blocksize;
  1833. int ret;
  1834. u32 refs;
  1835. WARN_ON(*level < 0);
  1836. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1837. ret = lookup_extent_ref(trans, root,
  1838. path->nodes[*level]->start,
  1839. path->nodes[*level]->len, &refs);
  1840. BUG_ON(ret);
  1841. if (refs > 1)
  1842. goto out;
  1843. /*
  1844. * walk down to the last node level and free all the leaves
  1845. */
  1846. while(*level >= 0) {
  1847. WARN_ON(*level < 0);
  1848. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1849. cur = path->nodes[*level];
  1850. if (btrfs_header_level(cur) != *level)
  1851. WARN_ON(1);
  1852. if (path->slots[*level] >=
  1853. btrfs_header_nritems(cur))
  1854. break;
  1855. if (*level == 0) {
  1856. ret = drop_leaf_ref(trans, root, cur);
  1857. BUG_ON(ret);
  1858. break;
  1859. }
  1860. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1861. blocksize = btrfs_level_size(root, *level - 1);
  1862. ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
  1863. BUG_ON(ret);
  1864. if (refs != 1) {
  1865. parent = path->nodes[*level];
  1866. root_owner = btrfs_header_owner(parent);
  1867. root_gen = btrfs_header_generation(parent);
  1868. path->slots[*level]++;
  1869. ret = btrfs_free_extent(trans, root, bytenr,
  1870. blocksize, root_owner,
  1871. root_gen, 0, 0, 1);
  1872. BUG_ON(ret);
  1873. continue;
  1874. }
  1875. next = btrfs_find_tree_block(root, bytenr, blocksize);
  1876. if (!next || !btrfs_buffer_uptodate(next)) {
  1877. free_extent_buffer(next);
  1878. reada_walk_down(root, cur, path->slots[*level]);
  1879. mutex_unlock(&root->fs_info->fs_mutex);
  1880. next = read_tree_block(root, bytenr, blocksize);
  1881. mutex_lock(&root->fs_info->fs_mutex);
  1882. /* we've dropped the lock, double check */
  1883. ret = lookup_extent_ref(trans, root, bytenr,
  1884. blocksize, &refs);
  1885. BUG_ON(ret);
  1886. if (refs != 1) {
  1887. parent = path->nodes[*level];
  1888. root_owner = btrfs_header_owner(parent);
  1889. root_gen = btrfs_header_generation(parent);
  1890. path->slots[*level]++;
  1891. free_extent_buffer(next);
  1892. ret = btrfs_free_extent(trans, root, bytenr,
  1893. blocksize,
  1894. root_owner,
  1895. root_gen, 0, 0, 1);
  1896. BUG_ON(ret);
  1897. continue;
  1898. }
  1899. } else if (next) {
  1900. btrfs_verify_block_csum(root, next);
  1901. }
  1902. WARN_ON(*level <= 0);
  1903. if (path->nodes[*level-1])
  1904. free_extent_buffer(path->nodes[*level-1]);
  1905. path->nodes[*level-1] = next;
  1906. *level = btrfs_header_level(next);
  1907. path->slots[*level] = 0;
  1908. }
  1909. out:
  1910. WARN_ON(*level < 0);
  1911. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1912. if (path->nodes[*level] == root->node) {
  1913. root_owner = root->root_key.objectid;
  1914. parent = path->nodes[*level];
  1915. } else {
  1916. parent = path->nodes[*level + 1];
  1917. root_owner = btrfs_header_owner(parent);
  1918. }
  1919. root_gen = btrfs_header_generation(parent);
  1920. ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
  1921. path->nodes[*level]->len,
  1922. root_owner, root_gen, 0, 0, 1);
  1923. free_extent_buffer(path->nodes[*level]);
  1924. path->nodes[*level] = NULL;
  1925. *level += 1;
  1926. BUG_ON(ret);
  1927. return 0;
  1928. }
  1929. /*
  1930. * helper for dropping snapshots. This walks back up the tree in the path
  1931. * to find the first node higher up where we haven't yet gone through
  1932. * all the slots
  1933. */
  1934. static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
  1935. struct btrfs_root *root,
  1936. struct btrfs_path *path, int *level)
  1937. {
  1938. u64 root_owner;
  1939. u64 root_gen;
  1940. struct btrfs_root_item *root_item = &root->root_item;
  1941. int i;
  1942. int slot;
  1943. int ret;
  1944. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1945. slot = path->slots[i];
  1946. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1947. struct extent_buffer *node;
  1948. struct btrfs_disk_key disk_key;
  1949. node = path->nodes[i];
  1950. path->slots[i]++;
  1951. *level = i;
  1952. WARN_ON(*level == 0);
  1953. btrfs_node_key(node, &disk_key, path->slots[i]);
  1954. memcpy(&root_item->drop_progress,
  1955. &disk_key, sizeof(disk_key));
  1956. root_item->drop_level = i;
  1957. return 0;
  1958. } else {
  1959. if (path->nodes[*level] == root->node) {
  1960. root_owner = root->root_key.objectid;
  1961. root_gen =
  1962. btrfs_header_generation(path->nodes[*level]);
  1963. } else {
  1964. struct extent_buffer *node;
  1965. node = path->nodes[*level + 1];
  1966. root_owner = btrfs_header_owner(node);
  1967. root_gen = btrfs_header_generation(node);
  1968. }
  1969. ret = btrfs_free_extent(trans, root,
  1970. path->nodes[*level]->start,
  1971. path->nodes[*level]->len,
  1972. root_owner, root_gen, 0, 0, 1);
  1973. BUG_ON(ret);
  1974. free_extent_buffer(path->nodes[*level]);
  1975. path->nodes[*level] = NULL;
  1976. *level = i + 1;
  1977. }
  1978. }
  1979. return 1;
  1980. }
  1981. /*
  1982. * drop the reference count on the tree rooted at 'snap'. This traverses
  1983. * the tree freeing any blocks that have a ref count of zero after being
  1984. * decremented.
  1985. */
  1986. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  1987. *root)
  1988. {
  1989. int ret = 0;
  1990. int wret;
  1991. int level;
  1992. struct btrfs_path *path;
  1993. int i;
  1994. int orig_level;
  1995. struct btrfs_root_item *root_item = &root->root_item;
  1996. path = btrfs_alloc_path();
  1997. BUG_ON(!path);
  1998. level = btrfs_header_level(root->node);
  1999. orig_level = level;
  2000. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  2001. path->nodes[level] = root->node;
  2002. extent_buffer_get(root->node);
  2003. path->slots[level] = 0;
  2004. } else {
  2005. struct btrfs_key key;
  2006. struct btrfs_disk_key found_key;
  2007. struct extent_buffer *node;
  2008. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  2009. level = root_item->drop_level;
  2010. path->lowest_level = level;
  2011. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2012. if (wret < 0) {
  2013. ret = wret;
  2014. goto out;
  2015. }
  2016. node = path->nodes[level];
  2017. btrfs_node_key(node, &found_key, path->slots[level]);
  2018. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  2019. sizeof(found_key)));
  2020. }
  2021. while(1) {
  2022. wret = walk_down_tree(trans, root, path, &level);
  2023. if (wret > 0)
  2024. break;
  2025. if (wret < 0)
  2026. ret = wret;
  2027. wret = walk_up_tree(trans, root, path, &level);
  2028. if (wret > 0)
  2029. break;
  2030. if (wret < 0)
  2031. ret = wret;
  2032. ret = -EAGAIN;
  2033. break;
  2034. }
  2035. for (i = 0; i <= orig_level; i++) {
  2036. if (path->nodes[i]) {
  2037. free_extent_buffer(path->nodes[i]);
  2038. path->nodes[i] = NULL;
  2039. }
  2040. }
  2041. out:
  2042. btrfs_free_path(path);
  2043. return ret;
  2044. }
  2045. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  2046. {
  2047. u64 start;
  2048. u64 end;
  2049. u64 ptr;
  2050. int ret;
  2051. while(1) {
  2052. ret = find_first_extent_bit(&info->block_group_cache, 0,
  2053. &start, &end, (unsigned int)-1);
  2054. if (ret)
  2055. break;
  2056. ret = get_state_private(&info->block_group_cache, start, &ptr);
  2057. if (!ret)
  2058. kfree((void *)(unsigned long)ptr);
  2059. clear_extent_bits(&info->block_group_cache, start,
  2060. end, (unsigned int)-1, GFP_NOFS);
  2061. }
  2062. while(1) {
  2063. ret = find_first_extent_bit(&info->free_space_cache, 0,
  2064. &start, &end, EXTENT_DIRTY);
  2065. if (ret)
  2066. break;
  2067. clear_extent_dirty(&info->free_space_cache, start,
  2068. end, GFP_NOFS);
  2069. }
  2070. return 0;
  2071. }
  2072. static int noinline relocate_inode_pages(struct inode *inode, u64 start,
  2073. u64 len)
  2074. {
  2075. u64 page_start;
  2076. u64 page_end;
  2077. u64 delalloc_start;
  2078. u64 existing_delalloc;
  2079. unsigned long last_index;
  2080. unsigned long i;
  2081. struct page *page;
  2082. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2083. struct file_ra_state *ra;
  2084. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2085. mutex_lock(&inode->i_mutex);
  2086. i = start >> PAGE_CACHE_SHIFT;
  2087. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  2088. file_ra_state_init(ra, inode->i_mapping);
  2089. btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
  2090. kfree(ra);
  2091. for (; i <= last_index; i++) {
  2092. page = grab_cache_page(inode->i_mapping, i);
  2093. if (!page)
  2094. goto out_unlock;
  2095. if (!PageUptodate(page)) {
  2096. btrfs_readpage(NULL, page);
  2097. lock_page(page);
  2098. if (!PageUptodate(page)) {
  2099. unlock_page(page);
  2100. page_cache_release(page);
  2101. goto out_unlock;
  2102. }
  2103. }
  2104. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2105. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2106. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2107. delalloc_start = page_start;
  2108. existing_delalloc = count_range_bits(io_tree,
  2109. &delalloc_start, page_end,
  2110. PAGE_CACHE_SIZE, EXTENT_DELALLOC);
  2111. set_extent_delalloc(io_tree, page_start,
  2112. page_end, GFP_NOFS);
  2113. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2114. set_page_dirty(page);
  2115. unlock_page(page);
  2116. page_cache_release(page);
  2117. }
  2118. out_unlock:
  2119. mutex_unlock(&inode->i_mutex);
  2120. return 0;
  2121. }
  2122. /*
  2123. * note, this releases the path
  2124. */
  2125. static int noinline relocate_one_reference(struct btrfs_root *extent_root,
  2126. struct btrfs_path *path,
  2127. struct btrfs_key *extent_key)
  2128. {
  2129. struct inode *inode;
  2130. struct btrfs_root *found_root;
  2131. struct btrfs_key *root_location;
  2132. struct btrfs_extent_ref *ref;
  2133. u64 ref_root;
  2134. u64 ref_gen;
  2135. u64 ref_objectid;
  2136. u64 ref_offset;
  2137. int ret;
  2138. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2139. struct btrfs_extent_ref);
  2140. ref_root = btrfs_ref_root(path->nodes[0], ref);
  2141. ref_gen = btrfs_ref_generation(path->nodes[0], ref);
  2142. ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
  2143. ref_offset = btrfs_ref_offset(path->nodes[0], ref);
  2144. btrfs_release_path(extent_root, path);
  2145. root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
  2146. root_location->objectid = ref_root;
  2147. if (ref_gen == 0)
  2148. root_location->offset = 0;
  2149. else
  2150. root_location->offset = (u64)-1;
  2151. root_location->type = BTRFS_ROOT_ITEM_KEY;
  2152. found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
  2153. root_location);
  2154. BUG_ON(!found_root);
  2155. kfree(root_location);
  2156. if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  2157. mutex_unlock(&extent_root->fs_info->fs_mutex);
  2158. inode = btrfs_iget_locked(extent_root->fs_info->sb,
  2159. ref_objectid, found_root);
  2160. if (inode->i_state & I_NEW) {
  2161. /* the inode and parent dir are two different roots */
  2162. BTRFS_I(inode)->root = found_root;
  2163. BTRFS_I(inode)->location.objectid = ref_objectid;
  2164. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  2165. BTRFS_I(inode)->location.offset = 0;
  2166. btrfs_read_locked_inode(inode);
  2167. unlock_new_inode(inode);
  2168. }
  2169. /* this can happen if the reference is not against
  2170. * the latest version of the tree root
  2171. */
  2172. if (is_bad_inode(inode)) {
  2173. mutex_lock(&extent_root->fs_info->fs_mutex);
  2174. goto out;
  2175. }
  2176. relocate_inode_pages(inode, ref_offset, extent_key->offset);
  2177. /* FIXME, data=ordered will help get rid of this */
  2178. filemap_fdatawrite(inode->i_mapping);
  2179. iput(inode);
  2180. mutex_lock(&extent_root->fs_info->fs_mutex);
  2181. } else {
  2182. struct btrfs_trans_handle *trans;
  2183. struct btrfs_key found_key;
  2184. struct extent_buffer *eb;
  2185. int level;
  2186. int i;
  2187. trans = btrfs_start_transaction(found_root, 1);
  2188. eb = read_tree_block(found_root, extent_key->objectid,
  2189. extent_key->offset);
  2190. level = btrfs_header_level(eb);
  2191. if (level == 0)
  2192. btrfs_item_key_to_cpu(eb, &found_key, 0);
  2193. else
  2194. btrfs_node_key_to_cpu(eb, &found_key, 0);
  2195. free_extent_buffer(eb);
  2196. path->lowest_level = level;
  2197. path->reada = 2;
  2198. ret = btrfs_search_slot(trans, found_root, &found_key, path,
  2199. 0, 1);
  2200. path->lowest_level = 0;
  2201. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2202. if (!path->nodes[i])
  2203. break;
  2204. free_extent_buffer(path->nodes[i]);
  2205. path->nodes[i] = NULL;
  2206. }
  2207. btrfs_release_path(found_root, path);
  2208. btrfs_end_transaction(trans, found_root);
  2209. }
  2210. out:
  2211. return 0;
  2212. }
  2213. static int noinline relocate_one_extent(struct btrfs_root *extent_root,
  2214. struct btrfs_path *path,
  2215. struct btrfs_key *extent_key)
  2216. {
  2217. struct btrfs_key key;
  2218. struct btrfs_key found_key;
  2219. struct extent_buffer *leaf;
  2220. u32 nritems;
  2221. u32 item_size;
  2222. int ret = 0;
  2223. key.objectid = extent_key->objectid;
  2224. key.type = BTRFS_EXTENT_REF_KEY;
  2225. key.offset = 0;
  2226. while(1) {
  2227. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2228. if (ret < 0)
  2229. goto out;
  2230. ret = 0;
  2231. leaf = path->nodes[0];
  2232. nritems = btrfs_header_nritems(leaf);
  2233. if (path->slots[0] == nritems)
  2234. goto out;
  2235. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2236. if (found_key.objectid != extent_key->objectid)
  2237. break;
  2238. if (found_key.type != BTRFS_EXTENT_REF_KEY)
  2239. break;
  2240. key.offset = found_key.offset + 1;
  2241. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2242. ret = relocate_one_reference(extent_root, path, extent_key);
  2243. if (ret)
  2244. goto out;
  2245. }
  2246. ret = 0;
  2247. out:
  2248. btrfs_release_path(extent_root, path);
  2249. return ret;
  2250. }
  2251. int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
  2252. {
  2253. struct btrfs_trans_handle *trans;
  2254. struct btrfs_root *tree_root = root->fs_info->tree_root;
  2255. struct btrfs_path *path;
  2256. u64 cur_byte;
  2257. u64 total_found;
  2258. struct btrfs_fs_info *info = root->fs_info;
  2259. struct extent_io_tree *block_group_cache;
  2260. struct btrfs_key key;
  2261. struct btrfs_key found_key;
  2262. struct extent_buffer *leaf;
  2263. u32 nritems;
  2264. int ret;
  2265. int progress = 0;
  2266. btrfs_set_super_total_bytes(&info->super_copy, new_size);
  2267. clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
  2268. GFP_NOFS);
  2269. block_group_cache = &info->block_group_cache;
  2270. path = btrfs_alloc_path();
  2271. root = root->fs_info->extent_root;
  2272. path->reada = 2;
  2273. again:
  2274. total_found = 0;
  2275. key.objectid = new_size;
  2276. key.offset = 0;
  2277. key.type = 0;
  2278. cur_byte = key.objectid;
  2279. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2280. if (ret < 0)
  2281. goto out;
  2282. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  2283. if (ret < 0)
  2284. goto out;
  2285. if (ret == 0) {
  2286. leaf = path->nodes[0];
  2287. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2288. if (found_key.objectid + found_key.offset > new_size) {
  2289. cur_byte = found_key.objectid;
  2290. key.objectid = cur_byte;
  2291. }
  2292. }
  2293. btrfs_release_path(root, path);
  2294. while(1) {
  2295. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2296. if (ret < 0)
  2297. goto out;
  2298. leaf = path->nodes[0];
  2299. nritems = btrfs_header_nritems(leaf);
  2300. next:
  2301. if (path->slots[0] >= nritems) {
  2302. ret = btrfs_next_leaf(root, path);
  2303. if (ret < 0)
  2304. goto out;
  2305. if (ret == 1) {
  2306. ret = 0;
  2307. break;
  2308. }
  2309. leaf = path->nodes[0];
  2310. nritems = btrfs_header_nritems(leaf);
  2311. }
  2312. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2313. if (progress && need_resched()) {
  2314. memcpy(&key, &found_key, sizeof(key));
  2315. mutex_unlock(&root->fs_info->fs_mutex);
  2316. cond_resched();
  2317. mutex_lock(&root->fs_info->fs_mutex);
  2318. btrfs_release_path(root, path);
  2319. btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2320. progress = 0;
  2321. goto next;
  2322. }
  2323. progress = 1;
  2324. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
  2325. found_key.objectid + found_key.offset <= cur_byte) {
  2326. path->slots[0]++;
  2327. goto next;
  2328. }
  2329. total_found++;
  2330. cur_byte = found_key.objectid + found_key.offset;
  2331. key.objectid = cur_byte;
  2332. btrfs_release_path(root, path);
  2333. ret = relocate_one_extent(root, path, &found_key);
  2334. }
  2335. btrfs_release_path(root, path);
  2336. if (total_found > 0) {
  2337. trans = btrfs_start_transaction(tree_root, 1);
  2338. btrfs_commit_transaction(trans, tree_root);
  2339. mutex_unlock(&root->fs_info->fs_mutex);
  2340. btrfs_clean_old_snapshots(tree_root);
  2341. mutex_lock(&root->fs_info->fs_mutex);
  2342. trans = btrfs_start_transaction(tree_root, 1);
  2343. btrfs_commit_transaction(trans, tree_root);
  2344. goto again;
  2345. }
  2346. trans = btrfs_start_transaction(root, 1);
  2347. key.objectid = new_size;
  2348. key.offset = 0;
  2349. key.type = 0;
  2350. while(1) {
  2351. u64 ptr;
  2352. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2353. if (ret < 0)
  2354. goto out;
  2355. leaf = path->nodes[0];
  2356. nritems = btrfs_header_nritems(leaf);
  2357. bg_next:
  2358. if (path->slots[0] >= nritems) {
  2359. ret = btrfs_next_leaf(root, path);
  2360. if (ret < 0)
  2361. break;
  2362. if (ret == 1) {
  2363. ret = 0;
  2364. break;
  2365. }
  2366. leaf = path->nodes[0];
  2367. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2368. /*
  2369. * btrfs_next_leaf doesn't cow buffers, we have to
  2370. * do the search again
  2371. */
  2372. memcpy(&key, &found_key, sizeof(key));
  2373. btrfs_release_path(root, path);
  2374. goto resched_check;
  2375. }
  2376. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2377. if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
  2378. printk("shrinker found key %Lu %u %Lu\n",
  2379. found_key.objectid, found_key.type,
  2380. found_key.offset);
  2381. path->slots[0]++;
  2382. goto bg_next;
  2383. }
  2384. ret = get_state_private(&info->block_group_cache,
  2385. found_key.objectid, &ptr);
  2386. if (!ret)
  2387. kfree((void *)(unsigned long)ptr);
  2388. clear_extent_bits(&info->block_group_cache, found_key.objectid,
  2389. found_key.objectid + found_key.offset - 1,
  2390. (unsigned int)-1, GFP_NOFS);
  2391. key.objectid = found_key.objectid + 1;
  2392. btrfs_del_item(trans, root, path);
  2393. btrfs_release_path(root, path);
  2394. resched_check:
  2395. if (need_resched()) {
  2396. mutex_unlock(&root->fs_info->fs_mutex);
  2397. cond_resched();
  2398. mutex_lock(&root->fs_info->fs_mutex);
  2399. }
  2400. }
  2401. clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
  2402. GFP_NOFS);
  2403. btrfs_commit_transaction(trans, root);
  2404. out:
  2405. btrfs_free_path(path);
  2406. return ret;
  2407. }
  2408. int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
  2409. struct btrfs_root *root, u64 new_size)
  2410. {
  2411. btrfs_set_super_total_bytes(&root->fs_info->super_copy, new_size);
  2412. return 0;
  2413. }
  2414. int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
  2415. struct btrfs_key *key)
  2416. {
  2417. int ret;
  2418. struct btrfs_key found_key;
  2419. struct extent_buffer *leaf;
  2420. int slot;
  2421. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  2422. if (ret < 0)
  2423. return ret;
  2424. while(1) {
  2425. slot = path->slots[0];
  2426. leaf = path->nodes[0];
  2427. if (slot >= btrfs_header_nritems(leaf)) {
  2428. ret = btrfs_next_leaf(root, path);
  2429. if (ret == 0)
  2430. continue;
  2431. if (ret < 0)
  2432. goto error;
  2433. break;
  2434. }
  2435. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2436. if (found_key.objectid >= key->objectid &&
  2437. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
  2438. return 0;
  2439. path->slots[0]++;
  2440. }
  2441. ret = -ENOENT;
  2442. error:
  2443. return ret;
  2444. }
  2445. int btrfs_read_block_groups(struct btrfs_root *root)
  2446. {
  2447. struct btrfs_path *path;
  2448. int ret;
  2449. int bit;
  2450. struct btrfs_block_group_cache *cache;
  2451. struct btrfs_fs_info *info = root->fs_info;
  2452. struct btrfs_space_info *space_info;
  2453. struct extent_io_tree *block_group_cache;
  2454. struct btrfs_key key;
  2455. struct btrfs_key found_key;
  2456. struct extent_buffer *leaf;
  2457. block_group_cache = &info->block_group_cache;
  2458. root = info->extent_root;
  2459. key.objectid = 0;
  2460. key.offset = 0;
  2461. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  2462. path = btrfs_alloc_path();
  2463. if (!path)
  2464. return -ENOMEM;
  2465. while(1) {
  2466. ret = find_first_block_group(root, path, &key);
  2467. if (ret > 0) {
  2468. ret = 0;
  2469. goto error;
  2470. }
  2471. if (ret != 0)
  2472. goto error;
  2473. leaf = path->nodes[0];
  2474. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2475. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  2476. if (!cache) {
  2477. ret = -ENOMEM;
  2478. break;
  2479. }
  2480. read_extent_buffer(leaf, &cache->item,
  2481. btrfs_item_ptr_offset(leaf, path->slots[0]),
  2482. sizeof(cache->item));
  2483. memcpy(&cache->key, &found_key, sizeof(found_key));
  2484. cache->cached = 0;
  2485. cache->pinned = 0;
  2486. key.objectid = found_key.objectid + found_key.offset;
  2487. btrfs_release_path(root, path);
  2488. cache->flags = btrfs_block_group_flags(&cache->item);
  2489. bit = 0;
  2490. if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
  2491. bit = BLOCK_GROUP_DATA;
  2492. } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
  2493. bit = BLOCK_GROUP_SYSTEM;
  2494. } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
  2495. bit = BLOCK_GROUP_METADATA;
  2496. }
  2497. set_avail_alloc_bits(info, cache->flags);
  2498. ret = update_space_info(info, cache->flags, found_key.offset,
  2499. btrfs_block_group_used(&cache->item),
  2500. &space_info);
  2501. BUG_ON(ret);
  2502. cache->space_info = space_info;
  2503. /* use EXTENT_LOCKED to prevent merging */
  2504. set_extent_bits(block_group_cache, found_key.objectid,
  2505. found_key.objectid + found_key.offset - 1,
  2506. bit | EXTENT_LOCKED, GFP_NOFS);
  2507. set_state_private(block_group_cache, found_key.objectid,
  2508. (unsigned long)cache);
  2509. if (key.objectid >=
  2510. btrfs_super_total_bytes(&info->super_copy))
  2511. break;
  2512. }
  2513. ret = 0;
  2514. error:
  2515. btrfs_free_path(path);
  2516. return ret;
  2517. }
  2518. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  2519. struct btrfs_root *root, u64 bytes_used,
  2520. u64 type, u64 chunk_tree, u64 chunk_objectid,
  2521. u64 size)
  2522. {
  2523. int ret;
  2524. int bit = 0;
  2525. struct btrfs_root *extent_root;
  2526. struct btrfs_block_group_cache *cache;
  2527. struct extent_io_tree *block_group_cache;
  2528. extent_root = root->fs_info->extent_root;
  2529. block_group_cache = &root->fs_info->block_group_cache;
  2530. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  2531. BUG_ON(!cache);
  2532. cache->key.objectid = chunk_objectid;
  2533. cache->key.offset = size;
  2534. cache->cached = 0;
  2535. cache->pinned = 0;
  2536. btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  2537. memset(&cache->item, 0, sizeof(cache->item));
  2538. btrfs_set_block_group_used(&cache->item, bytes_used);
  2539. btrfs_set_block_group_chunk_tree(&cache->item, chunk_tree);
  2540. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  2541. cache->flags = type;
  2542. btrfs_set_block_group_flags(&cache->item, type);
  2543. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  2544. &cache->space_info);
  2545. BUG_ON(ret);
  2546. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2547. bit = BLOCK_GROUP_DATA;
  2548. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2549. bit = BLOCK_GROUP_SYSTEM;
  2550. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2551. bit = BLOCK_GROUP_METADATA;
  2552. }
  2553. set_extent_bits(block_group_cache, chunk_objectid,
  2554. chunk_objectid + size - 1,
  2555. bit | EXTENT_LOCKED, GFP_NOFS);
  2556. set_state_private(block_group_cache, chunk_objectid,
  2557. (unsigned long)cache);
  2558. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  2559. sizeof(cache->item));
  2560. BUG_ON(ret);
  2561. finish_current_insert(trans, extent_root);
  2562. ret = del_pending_extents(trans, extent_root);
  2563. BUG_ON(ret);
  2564. return 0;
  2565. }