extent-tree.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642
  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/pagemap.h>
  20. #include <linux/writeback.h>
  21. #include "hash.h"
  22. #include "crc32c.h"
  23. #include "ctree.h"
  24. #include "disk-io.h"
  25. #include "print-tree.h"
  26. #include "transaction.h"
  27. #include "volumes.h"
  28. #include "locking.h"
  29. #include "ref-cache.h"
  30. #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
  31. #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
  32. #define BLOCK_GROUP_SYSTEM EXTENT_NEW
  33. #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
  34. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  35. btrfs_root *extent_root);
  36. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  37. btrfs_root *extent_root);
  38. static struct btrfs_block_group_cache *
  39. __btrfs_find_block_group(struct btrfs_root *root,
  40. struct btrfs_block_group_cache *hint,
  41. u64 search_start, int data, int owner);
  42. void maybe_lock_mutex(struct btrfs_root *root)
  43. {
  44. if (root != root->fs_info->extent_root &&
  45. root != root->fs_info->chunk_root &&
  46. root != root->fs_info->dev_root) {
  47. mutex_lock(&root->fs_info->alloc_mutex);
  48. }
  49. }
  50. void maybe_unlock_mutex(struct btrfs_root *root)
  51. {
  52. if (root != root->fs_info->extent_root &&
  53. root != root->fs_info->chunk_root &&
  54. root != root->fs_info->dev_root) {
  55. mutex_unlock(&root->fs_info->alloc_mutex);
  56. }
  57. }
  58. static int cache_block_group(struct btrfs_root *root,
  59. struct btrfs_block_group_cache *block_group)
  60. {
  61. struct btrfs_path *path;
  62. int ret;
  63. struct btrfs_key key;
  64. struct extent_buffer *leaf;
  65. struct extent_io_tree *free_space_cache;
  66. int slot;
  67. u64 last = 0;
  68. u64 hole_size;
  69. u64 first_free;
  70. int found = 0;
  71. if (!block_group)
  72. return 0;
  73. root = root->fs_info->extent_root;
  74. free_space_cache = &root->fs_info->free_space_cache;
  75. if (block_group->cached)
  76. return 0;
  77. path = btrfs_alloc_path();
  78. if (!path)
  79. return -ENOMEM;
  80. path->reada = 2;
  81. /*
  82. * we get into deadlocks with paths held by callers of this function.
  83. * since the alloc_mutex is protecting things right now, just
  84. * skip the locking here
  85. */
  86. path->skip_locking = 1;
  87. first_free = block_group->key.objectid;
  88. key.objectid = block_group->key.objectid;
  89. key.offset = 0;
  90. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  91. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  92. if (ret < 0)
  93. return ret;
  94. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  95. if (ret < 0)
  96. return ret;
  97. if (ret == 0) {
  98. leaf = path->nodes[0];
  99. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  100. if (key.objectid + key.offset > first_free)
  101. first_free = key.objectid + key.offset;
  102. }
  103. while(1) {
  104. leaf = path->nodes[0];
  105. slot = path->slots[0];
  106. if (slot >= btrfs_header_nritems(leaf)) {
  107. ret = btrfs_next_leaf(root, path);
  108. if (ret < 0)
  109. goto err;
  110. if (ret == 0) {
  111. continue;
  112. } else {
  113. break;
  114. }
  115. }
  116. btrfs_item_key_to_cpu(leaf, &key, slot);
  117. if (key.objectid < block_group->key.objectid) {
  118. goto next;
  119. }
  120. if (key.objectid >= block_group->key.objectid +
  121. block_group->key.offset) {
  122. break;
  123. }
  124. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  125. if (!found) {
  126. last = first_free;
  127. found = 1;
  128. }
  129. if (key.objectid > last) {
  130. hole_size = key.objectid - last;
  131. set_extent_dirty(free_space_cache, last,
  132. last + hole_size - 1,
  133. GFP_NOFS);
  134. }
  135. last = key.objectid + key.offset;
  136. }
  137. next:
  138. path->slots[0]++;
  139. }
  140. if (!found)
  141. last = first_free;
  142. if (block_group->key.objectid +
  143. block_group->key.offset > last) {
  144. hole_size = block_group->key.objectid +
  145. block_group->key.offset - last;
  146. set_extent_dirty(free_space_cache, last,
  147. last + hole_size - 1, GFP_NOFS);
  148. }
  149. block_group->cached = 1;
  150. err:
  151. btrfs_free_path(path);
  152. return 0;
  153. }
  154. struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
  155. btrfs_fs_info *info,
  156. u64 bytenr)
  157. {
  158. struct extent_io_tree *block_group_cache;
  159. struct btrfs_block_group_cache *block_group = NULL;
  160. u64 ptr;
  161. u64 start;
  162. u64 end;
  163. int ret;
  164. bytenr = max_t(u64, bytenr,
  165. BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
  166. block_group_cache = &info->block_group_cache;
  167. ret = find_first_extent_bit(block_group_cache,
  168. bytenr, &start, &end,
  169. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
  170. BLOCK_GROUP_SYSTEM);
  171. if (ret) {
  172. return NULL;
  173. }
  174. ret = get_state_private(block_group_cache, start, &ptr);
  175. if (ret)
  176. return NULL;
  177. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  178. return block_group;
  179. }
  180. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  181. btrfs_fs_info *info,
  182. u64 bytenr)
  183. {
  184. struct extent_io_tree *block_group_cache;
  185. struct btrfs_block_group_cache *block_group = NULL;
  186. u64 ptr;
  187. u64 start;
  188. u64 end;
  189. int ret;
  190. bytenr = max_t(u64, bytenr,
  191. BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
  192. block_group_cache = &info->block_group_cache;
  193. ret = find_first_extent_bit(block_group_cache,
  194. bytenr, &start, &end,
  195. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
  196. BLOCK_GROUP_SYSTEM);
  197. if (ret) {
  198. return NULL;
  199. }
  200. ret = get_state_private(block_group_cache, start, &ptr);
  201. if (ret)
  202. return NULL;
  203. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  204. if (block_group->key.objectid <= bytenr && bytenr <
  205. block_group->key.objectid + block_group->key.offset)
  206. return block_group;
  207. return NULL;
  208. }
  209. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  210. {
  211. return (cache->flags & bits) == bits;
  212. }
  213. static int noinline find_search_start(struct btrfs_root *root,
  214. struct btrfs_block_group_cache **cache_ret,
  215. u64 *start_ret, u64 num, int data)
  216. {
  217. int ret;
  218. struct btrfs_block_group_cache *cache = *cache_ret;
  219. struct extent_io_tree *free_space_cache;
  220. struct extent_state *state;
  221. u64 last;
  222. u64 start = 0;
  223. u64 cache_miss = 0;
  224. u64 total_fs_bytes;
  225. u64 search_start = *start_ret;
  226. int wrapped = 0;
  227. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  228. total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  229. free_space_cache = &root->fs_info->free_space_cache;
  230. if (!cache)
  231. goto out;
  232. again:
  233. ret = cache_block_group(root, cache);
  234. if (ret) {
  235. goto out;
  236. }
  237. last = max(search_start, cache->key.objectid);
  238. if (!block_group_bits(cache, data) || cache->ro)
  239. goto new_group;
  240. spin_lock_irq(&free_space_cache->lock);
  241. state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
  242. while(1) {
  243. if (!state) {
  244. if (!cache_miss)
  245. cache_miss = last;
  246. spin_unlock_irq(&free_space_cache->lock);
  247. goto new_group;
  248. }
  249. start = max(last, state->start);
  250. last = state->end + 1;
  251. if (last - start < num) {
  252. do {
  253. state = extent_state_next(state);
  254. } while(state && !(state->state & EXTENT_DIRTY));
  255. continue;
  256. }
  257. spin_unlock_irq(&free_space_cache->lock);
  258. if (cache->ro) {
  259. goto new_group;
  260. }
  261. if (start + num > cache->key.objectid + cache->key.offset)
  262. goto new_group;
  263. if (!block_group_bits(cache, data)) {
  264. printk("block group bits don't match %Lu %d\n", cache->flags, data);
  265. }
  266. *start_ret = start;
  267. return 0;
  268. }
  269. out:
  270. cache = btrfs_lookup_block_group(root->fs_info, search_start);
  271. if (!cache) {
  272. printk("Unable to find block group for %Lu\n", search_start);
  273. WARN_ON(1);
  274. }
  275. return -ENOSPC;
  276. new_group:
  277. last = cache->key.objectid + cache->key.offset;
  278. wrapped:
  279. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  280. if (!cache || cache->key.objectid >= total_fs_bytes) {
  281. no_cache:
  282. if (!wrapped) {
  283. wrapped = 1;
  284. last = search_start;
  285. goto wrapped;
  286. }
  287. goto out;
  288. }
  289. if (cache_miss && !cache->cached) {
  290. cache_block_group(root, cache);
  291. last = cache_miss;
  292. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  293. }
  294. cache_miss = 0;
  295. cache = btrfs_find_block_group(root, cache, last, data, 0);
  296. if (!cache)
  297. goto no_cache;
  298. *cache_ret = cache;
  299. goto again;
  300. }
  301. static u64 div_factor(u64 num, int factor)
  302. {
  303. if (factor == 10)
  304. return num;
  305. num *= factor;
  306. do_div(num, 10);
  307. return num;
  308. }
  309. static int block_group_state_bits(u64 flags)
  310. {
  311. int bits = 0;
  312. if (flags & BTRFS_BLOCK_GROUP_DATA)
  313. bits |= BLOCK_GROUP_DATA;
  314. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  315. bits |= BLOCK_GROUP_METADATA;
  316. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  317. bits |= BLOCK_GROUP_SYSTEM;
  318. return bits;
  319. }
  320. static struct btrfs_block_group_cache *
  321. __btrfs_find_block_group(struct btrfs_root *root,
  322. struct btrfs_block_group_cache *hint,
  323. u64 search_start, int data, int owner)
  324. {
  325. struct btrfs_block_group_cache *cache;
  326. struct extent_io_tree *block_group_cache;
  327. struct btrfs_block_group_cache *found_group = NULL;
  328. struct btrfs_fs_info *info = root->fs_info;
  329. u64 used;
  330. u64 last = 0;
  331. u64 start;
  332. u64 end;
  333. u64 free_check;
  334. u64 ptr;
  335. int bit;
  336. int ret;
  337. int full_search = 0;
  338. int factor = 10;
  339. int wrapped = 0;
  340. block_group_cache = &info->block_group_cache;
  341. if (data & BTRFS_BLOCK_GROUP_METADATA)
  342. factor = 9;
  343. bit = block_group_state_bits(data);
  344. if (search_start) {
  345. struct btrfs_block_group_cache *shint;
  346. shint = btrfs_lookup_first_block_group(info, search_start);
  347. if (shint && block_group_bits(shint, data) && !shint->ro) {
  348. spin_lock(&shint->lock);
  349. used = btrfs_block_group_used(&shint->item);
  350. if (used + shint->pinned <
  351. div_factor(shint->key.offset, factor)) {
  352. spin_unlock(&shint->lock);
  353. return shint;
  354. }
  355. spin_unlock(&shint->lock);
  356. }
  357. }
  358. if (hint && !hint->ro && block_group_bits(hint, data)) {
  359. spin_lock(&hint->lock);
  360. used = btrfs_block_group_used(&hint->item);
  361. if (used + hint->pinned <
  362. div_factor(hint->key.offset, factor)) {
  363. spin_unlock(&hint->lock);
  364. return hint;
  365. }
  366. spin_unlock(&hint->lock);
  367. last = hint->key.objectid + hint->key.offset;
  368. } else {
  369. if (hint)
  370. last = max(hint->key.objectid, search_start);
  371. else
  372. last = search_start;
  373. }
  374. again:
  375. while(1) {
  376. ret = find_first_extent_bit(block_group_cache, last,
  377. &start, &end, bit);
  378. if (ret)
  379. break;
  380. ret = get_state_private(block_group_cache, start, &ptr);
  381. if (ret) {
  382. last = end + 1;
  383. continue;
  384. }
  385. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  386. spin_lock(&cache->lock);
  387. last = cache->key.objectid + cache->key.offset;
  388. used = btrfs_block_group_used(&cache->item);
  389. if (!cache->ro && block_group_bits(cache, data)) {
  390. free_check = div_factor(cache->key.offset, factor);
  391. if (used + cache->pinned < free_check) {
  392. found_group = cache;
  393. spin_unlock(&cache->lock);
  394. goto found;
  395. }
  396. }
  397. spin_unlock(&cache->lock);
  398. cond_resched();
  399. }
  400. if (!wrapped) {
  401. last = search_start;
  402. wrapped = 1;
  403. goto again;
  404. }
  405. if (!full_search && factor < 10) {
  406. last = search_start;
  407. full_search = 1;
  408. factor = 10;
  409. goto again;
  410. }
  411. found:
  412. return found_group;
  413. }
  414. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  415. struct btrfs_block_group_cache
  416. *hint, u64 search_start,
  417. int data, int owner)
  418. {
  419. struct btrfs_block_group_cache *ret;
  420. ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
  421. return ret;
  422. }
  423. static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
  424. u64 owner, u64 owner_offset)
  425. {
  426. u32 high_crc = ~(u32)0;
  427. u32 low_crc = ~(u32)0;
  428. __le64 lenum;
  429. lenum = cpu_to_le64(root_objectid);
  430. high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
  431. lenum = cpu_to_le64(ref_generation);
  432. low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
  433. if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
  434. lenum = cpu_to_le64(owner);
  435. low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
  436. lenum = cpu_to_le64(owner_offset);
  437. low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
  438. }
  439. return ((u64)high_crc << 32) | (u64)low_crc;
  440. }
  441. static int match_extent_ref(struct extent_buffer *leaf,
  442. struct btrfs_extent_ref *disk_ref,
  443. struct btrfs_extent_ref *cpu_ref)
  444. {
  445. int ret;
  446. int len;
  447. if (cpu_ref->objectid)
  448. len = sizeof(*cpu_ref);
  449. else
  450. len = 2 * sizeof(u64);
  451. ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
  452. len);
  453. return ret == 0;
  454. }
  455. static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
  456. struct btrfs_root *root,
  457. struct btrfs_path *path, u64 bytenr,
  458. u64 root_objectid,
  459. u64 ref_generation, u64 owner,
  460. u64 owner_offset, int del)
  461. {
  462. u64 hash;
  463. struct btrfs_key key;
  464. struct btrfs_key found_key;
  465. struct btrfs_extent_ref ref;
  466. struct extent_buffer *leaf;
  467. struct btrfs_extent_ref *disk_ref;
  468. int ret;
  469. int ret2;
  470. btrfs_set_stack_ref_root(&ref, root_objectid);
  471. btrfs_set_stack_ref_generation(&ref, ref_generation);
  472. btrfs_set_stack_ref_objectid(&ref, owner);
  473. btrfs_set_stack_ref_offset(&ref, owner_offset);
  474. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  475. owner_offset);
  476. key.offset = hash;
  477. key.objectid = bytenr;
  478. key.type = BTRFS_EXTENT_REF_KEY;
  479. while (1) {
  480. ret = btrfs_search_slot(trans, root, &key, path,
  481. del ? -1 : 0, del);
  482. if (ret < 0)
  483. goto out;
  484. leaf = path->nodes[0];
  485. if (ret != 0) {
  486. u32 nritems = btrfs_header_nritems(leaf);
  487. if (path->slots[0] >= nritems) {
  488. ret2 = btrfs_next_leaf(root, path);
  489. if (ret2)
  490. goto out;
  491. leaf = path->nodes[0];
  492. }
  493. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  494. if (found_key.objectid != bytenr ||
  495. found_key.type != BTRFS_EXTENT_REF_KEY)
  496. goto out;
  497. key.offset = found_key.offset;
  498. if (del) {
  499. btrfs_release_path(root, path);
  500. continue;
  501. }
  502. }
  503. disk_ref = btrfs_item_ptr(path->nodes[0],
  504. path->slots[0],
  505. struct btrfs_extent_ref);
  506. if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
  507. ret = 0;
  508. goto out;
  509. }
  510. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  511. key.offset = found_key.offset + 1;
  512. btrfs_release_path(root, path);
  513. }
  514. out:
  515. return ret;
  516. }
  517. /*
  518. * Back reference rules. Back refs have three main goals:
  519. *
  520. * 1) differentiate between all holders of references to an extent so that
  521. * when a reference is dropped we can make sure it was a valid reference
  522. * before freeing the extent.
  523. *
  524. * 2) Provide enough information to quickly find the holders of an extent
  525. * if we notice a given block is corrupted or bad.
  526. *
  527. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  528. * maintenance. This is actually the same as #2, but with a slightly
  529. * different use case.
  530. *
  531. * File extents can be referenced by:
  532. *
  533. * - multiple snapshots, subvolumes, or different generations in one subvol
  534. * - different files inside a single subvolume (in theory, not implemented yet)
  535. * - different offsets inside a file (bookend extents in file.c)
  536. *
  537. * The extent ref structure has fields for:
  538. *
  539. * - Objectid of the subvolume root
  540. * - Generation number of the tree holding the reference
  541. * - objectid of the file holding the reference
  542. * - offset in the file corresponding to the key holding the reference
  543. *
  544. * When a file extent is allocated the fields are filled in:
  545. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  546. *
  547. * When a leaf is cow'd new references are added for every file extent found
  548. * in the leaf. It looks the same as the create case, but trans->transid
  549. * will be different when the block is cow'd.
  550. *
  551. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  552. *
  553. * When a file extent is removed either during snapshot deletion or file
  554. * truncation, the corresponding back reference is found
  555. * by searching for:
  556. *
  557. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  558. * inode objectid, offset in file)
  559. *
  560. * Btree extents can be referenced by:
  561. *
  562. * - Different subvolumes
  563. * - Different generations of the same subvolume
  564. *
  565. * Storing sufficient information for a full reverse mapping of a btree
  566. * block would require storing the lowest key of the block in the backref,
  567. * and it would require updating that lowest key either before write out or
  568. * every time it changed. Instead, the objectid of the lowest key is stored
  569. * along with the level of the tree block. This provides a hint
  570. * about where in the btree the block can be found. Searches through the
  571. * btree only need to look for a pointer to that block, so they stop one
  572. * level higher than the level recorded in the backref.
  573. *
  574. * Some btrees do not do reference counting on their extents. These
  575. * include the extent tree and the tree of tree roots. Backrefs for these
  576. * trees always have a generation of zero.
  577. *
  578. * When a tree block is created, back references are inserted:
  579. *
  580. * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
  581. *
  582. * When a tree block is cow'd in a reference counted root,
  583. * new back references are added for all the blocks it points to.
  584. * These are of the form (trans->transid will have increased since creation):
  585. *
  586. * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
  587. *
  588. * Because the lowest_key_objectid and the level are just hints
  589. * they are not used when backrefs are deleted. When a backref is deleted:
  590. *
  591. * if backref was for a tree root:
  592. * root_objectid = root->root_key.objectid
  593. * else
  594. * root_objectid = btrfs_header_owner(parent)
  595. *
  596. * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
  597. *
  598. * Back Reference Key hashing:
  599. *
  600. * Back references have four fields, each 64 bits long. Unfortunately,
  601. * This is hashed into a single 64 bit number and placed into the key offset.
  602. * The key objectid corresponds to the first byte in the extent, and the
  603. * key type is set to BTRFS_EXTENT_REF_KEY
  604. */
  605. int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
  606. struct btrfs_root *root,
  607. struct btrfs_path *path, u64 bytenr,
  608. u64 root_objectid, u64 ref_generation,
  609. u64 owner, u64 owner_offset)
  610. {
  611. u64 hash;
  612. struct btrfs_key key;
  613. struct btrfs_extent_ref ref;
  614. struct btrfs_extent_ref *disk_ref;
  615. int ret;
  616. btrfs_set_stack_ref_root(&ref, root_objectid);
  617. btrfs_set_stack_ref_generation(&ref, ref_generation);
  618. btrfs_set_stack_ref_objectid(&ref, owner);
  619. btrfs_set_stack_ref_offset(&ref, owner_offset);
  620. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  621. owner_offset);
  622. key.offset = hash;
  623. key.objectid = bytenr;
  624. key.type = BTRFS_EXTENT_REF_KEY;
  625. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
  626. while (ret == -EEXIST) {
  627. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  628. struct btrfs_extent_ref);
  629. if (match_extent_ref(path->nodes[0], disk_ref, &ref))
  630. goto out;
  631. key.offset++;
  632. btrfs_release_path(root, path);
  633. ret = btrfs_insert_empty_item(trans, root, path, &key,
  634. sizeof(ref));
  635. }
  636. if (ret)
  637. goto out;
  638. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  639. struct btrfs_extent_ref);
  640. write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
  641. sizeof(ref));
  642. btrfs_mark_buffer_dirty(path->nodes[0]);
  643. out:
  644. btrfs_release_path(root, path);
  645. return ret;
  646. }
  647. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  648. struct btrfs_root *root,
  649. u64 bytenr, u64 num_bytes,
  650. u64 root_objectid, u64 ref_generation,
  651. u64 owner, u64 owner_offset)
  652. {
  653. struct btrfs_path *path;
  654. int ret;
  655. struct btrfs_key key;
  656. struct extent_buffer *l;
  657. struct btrfs_extent_item *item;
  658. u32 refs;
  659. WARN_ON(num_bytes < root->sectorsize);
  660. path = btrfs_alloc_path();
  661. if (!path)
  662. return -ENOMEM;
  663. path->reada = 1;
  664. key.objectid = bytenr;
  665. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  666. key.offset = num_bytes;
  667. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  668. 0, 1);
  669. if (ret < 0)
  670. return ret;
  671. if (ret != 0) {
  672. BUG();
  673. }
  674. BUG_ON(ret != 0);
  675. l = path->nodes[0];
  676. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  677. refs = btrfs_extent_refs(l, item);
  678. btrfs_set_extent_refs(l, item, refs + 1);
  679. btrfs_mark_buffer_dirty(path->nodes[0]);
  680. btrfs_release_path(root->fs_info->extent_root, path);
  681. path->reada = 1;
  682. ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
  683. path, bytenr, root_objectid,
  684. ref_generation, owner, owner_offset);
  685. BUG_ON(ret);
  686. finish_current_insert(trans, root->fs_info->extent_root);
  687. del_pending_extents(trans, root->fs_info->extent_root);
  688. btrfs_free_path(path);
  689. return 0;
  690. }
  691. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  692. struct btrfs_root *root,
  693. u64 bytenr, u64 num_bytes,
  694. u64 root_objectid, u64 ref_generation,
  695. u64 owner, u64 owner_offset)
  696. {
  697. int ret;
  698. mutex_lock(&root->fs_info->alloc_mutex);
  699. ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
  700. root_objectid, ref_generation,
  701. owner, owner_offset);
  702. mutex_unlock(&root->fs_info->alloc_mutex);
  703. return ret;
  704. }
  705. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  706. struct btrfs_root *root)
  707. {
  708. finish_current_insert(trans, root->fs_info->extent_root);
  709. del_pending_extents(trans, root->fs_info->extent_root);
  710. return 0;
  711. }
  712. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  713. struct btrfs_root *root, u64 bytenr,
  714. u64 num_bytes, u32 *refs)
  715. {
  716. struct btrfs_path *path;
  717. int ret;
  718. struct btrfs_key key;
  719. struct extent_buffer *l;
  720. struct btrfs_extent_item *item;
  721. WARN_ON(num_bytes < root->sectorsize);
  722. path = btrfs_alloc_path();
  723. path->reada = 1;
  724. key.objectid = bytenr;
  725. key.offset = num_bytes;
  726. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  727. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  728. 0, 0);
  729. if (ret < 0)
  730. goto out;
  731. if (ret != 0) {
  732. btrfs_print_leaf(root, path->nodes[0]);
  733. printk("failed to find block number %Lu\n", bytenr);
  734. BUG();
  735. }
  736. l = path->nodes[0];
  737. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  738. *refs = btrfs_extent_refs(l, item);
  739. out:
  740. btrfs_free_path(path);
  741. return 0;
  742. }
  743. static int get_reference_status(struct btrfs_root *root, u64 bytenr,
  744. u64 parent_gen, u64 ref_objectid,
  745. u64 *min_generation, u32 *ref_count)
  746. {
  747. struct btrfs_root *extent_root = root->fs_info->extent_root;
  748. struct btrfs_path *path;
  749. struct extent_buffer *leaf;
  750. struct btrfs_extent_ref *ref_item;
  751. struct btrfs_key key;
  752. struct btrfs_key found_key;
  753. u64 root_objectid = root->root_key.objectid;
  754. u64 ref_generation;
  755. u32 nritems;
  756. int ret;
  757. key.objectid = bytenr;
  758. key.offset = 0;
  759. key.type = BTRFS_EXTENT_ITEM_KEY;
  760. path = btrfs_alloc_path();
  761. mutex_lock(&root->fs_info->alloc_mutex);
  762. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  763. if (ret < 0)
  764. goto out;
  765. BUG_ON(ret == 0);
  766. leaf = path->nodes[0];
  767. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  768. if (found_key.objectid != bytenr ||
  769. found_key.type != BTRFS_EXTENT_ITEM_KEY) {
  770. ret = 1;
  771. goto out;
  772. }
  773. *ref_count = 0;
  774. *min_generation = (u64)-1;
  775. while (1) {
  776. leaf = path->nodes[0];
  777. nritems = btrfs_header_nritems(leaf);
  778. if (path->slots[0] >= nritems) {
  779. ret = btrfs_next_leaf(extent_root, path);
  780. if (ret < 0)
  781. goto out;
  782. if (ret == 0)
  783. continue;
  784. break;
  785. }
  786. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  787. if (found_key.objectid != bytenr)
  788. break;
  789. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  790. path->slots[0]++;
  791. continue;
  792. }
  793. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  794. struct btrfs_extent_ref);
  795. ref_generation = btrfs_ref_generation(leaf, ref_item);
  796. /*
  797. * For (parent_gen > 0 && parent_gen > ref_gen):
  798. *
  799. * we reach here through the oldest root, therefore
  800. * all other reference from same snapshot should have
  801. * a larger generation.
  802. */
  803. if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
  804. (parent_gen > 0 && parent_gen > ref_generation) ||
  805. (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
  806. ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
  807. if (ref_count)
  808. *ref_count = 2;
  809. break;
  810. }
  811. *ref_count = 1;
  812. if (*min_generation > ref_generation)
  813. *min_generation = ref_generation;
  814. path->slots[0]++;
  815. }
  816. ret = 0;
  817. out:
  818. mutex_unlock(&root->fs_info->alloc_mutex);
  819. btrfs_free_path(path);
  820. return ret;
  821. }
  822. int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
  823. struct btrfs_root *root,
  824. struct btrfs_key *key, u64 bytenr)
  825. {
  826. struct btrfs_root *old_root;
  827. struct btrfs_path *path = NULL;
  828. struct extent_buffer *eb;
  829. struct btrfs_file_extent_item *item;
  830. u64 ref_generation;
  831. u64 min_generation;
  832. u64 extent_start;
  833. u32 ref_count;
  834. int level;
  835. int ret;
  836. BUG_ON(trans == NULL);
  837. BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
  838. ret = get_reference_status(root, bytenr, 0, key->objectid,
  839. &min_generation, &ref_count);
  840. if (ret)
  841. return ret;
  842. if (ref_count != 1)
  843. return 1;
  844. old_root = root->dirty_root->root;
  845. ref_generation = old_root->root_key.offset;
  846. /* all references are created in running transaction */
  847. if (min_generation > ref_generation) {
  848. ret = 0;
  849. goto out;
  850. }
  851. path = btrfs_alloc_path();
  852. if (!path) {
  853. ret = -ENOMEM;
  854. goto out;
  855. }
  856. path->skip_locking = 1;
  857. /* if no item found, the extent is referenced by other snapshot */
  858. ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
  859. if (ret)
  860. goto out;
  861. eb = path->nodes[0];
  862. item = btrfs_item_ptr(eb, path->slots[0],
  863. struct btrfs_file_extent_item);
  864. if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
  865. btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
  866. ret = 1;
  867. goto out;
  868. }
  869. for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
  870. if (level >= 0) {
  871. eb = path->nodes[level];
  872. if (!eb)
  873. continue;
  874. extent_start = eb->start;
  875. } else
  876. extent_start = bytenr;
  877. ret = get_reference_status(root, extent_start, ref_generation,
  878. 0, &min_generation, &ref_count);
  879. if (ret)
  880. goto out;
  881. if (ref_count != 1) {
  882. ret = 1;
  883. goto out;
  884. }
  885. if (level >= 0)
  886. ref_generation = btrfs_header_generation(eb);
  887. }
  888. ret = 0;
  889. out:
  890. if (path)
  891. btrfs_free_path(path);
  892. return ret;
  893. }
  894. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  895. struct extent_buffer *buf, int cache_ref)
  896. {
  897. u64 bytenr;
  898. u32 nritems;
  899. struct btrfs_key key;
  900. struct btrfs_file_extent_item *fi;
  901. int i;
  902. int level;
  903. int ret;
  904. int faili;
  905. int nr_file_extents = 0;
  906. if (!root->ref_cows)
  907. return 0;
  908. level = btrfs_header_level(buf);
  909. nritems = btrfs_header_nritems(buf);
  910. for (i = 0; i < nritems; i++) {
  911. cond_resched();
  912. if (level == 0) {
  913. u64 disk_bytenr;
  914. btrfs_item_key_to_cpu(buf, &key, i);
  915. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  916. continue;
  917. fi = btrfs_item_ptr(buf, i,
  918. struct btrfs_file_extent_item);
  919. if (btrfs_file_extent_type(buf, fi) ==
  920. BTRFS_FILE_EXTENT_INLINE)
  921. continue;
  922. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  923. if (disk_bytenr == 0)
  924. continue;
  925. if (buf != root->commit_root)
  926. nr_file_extents++;
  927. mutex_lock(&root->fs_info->alloc_mutex);
  928. ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
  929. btrfs_file_extent_disk_num_bytes(buf, fi),
  930. root->root_key.objectid, trans->transid,
  931. key.objectid, key.offset);
  932. mutex_unlock(&root->fs_info->alloc_mutex);
  933. if (ret) {
  934. faili = i;
  935. WARN_ON(1);
  936. goto fail;
  937. }
  938. } else {
  939. bytenr = btrfs_node_blockptr(buf, i);
  940. btrfs_node_key_to_cpu(buf, &key, i);
  941. mutex_lock(&root->fs_info->alloc_mutex);
  942. ret = __btrfs_inc_extent_ref(trans, root, bytenr,
  943. btrfs_level_size(root, level - 1),
  944. root->root_key.objectid,
  945. trans->transid,
  946. level - 1, key.objectid);
  947. mutex_unlock(&root->fs_info->alloc_mutex);
  948. if (ret) {
  949. faili = i;
  950. WARN_ON(1);
  951. goto fail;
  952. }
  953. }
  954. }
  955. /* cache orignal leaf block's references */
  956. if (level == 0 && cache_ref && buf != root->commit_root) {
  957. struct btrfs_leaf_ref *ref;
  958. struct btrfs_extent_info *info;
  959. ref = btrfs_alloc_leaf_ref(root, nr_file_extents);
  960. if (!ref) {
  961. WARN_ON(1);
  962. goto out;
  963. }
  964. ref->root_gen = root->root_key.offset;
  965. ref->bytenr = buf->start;
  966. ref->owner = btrfs_header_owner(buf);
  967. ref->generation = btrfs_header_generation(buf);
  968. ref->nritems = nr_file_extents;
  969. info = ref->extents;
  970. for (i = 0; nr_file_extents > 0 && i < nritems; i++) {
  971. u64 disk_bytenr;
  972. btrfs_item_key_to_cpu(buf, &key, i);
  973. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  974. continue;
  975. fi = btrfs_item_ptr(buf, i,
  976. struct btrfs_file_extent_item);
  977. if (btrfs_file_extent_type(buf, fi) ==
  978. BTRFS_FILE_EXTENT_INLINE)
  979. continue;
  980. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  981. if (disk_bytenr == 0)
  982. continue;
  983. info->bytenr = disk_bytenr;
  984. info->num_bytes =
  985. btrfs_file_extent_disk_num_bytes(buf, fi);
  986. info->objectid = key.objectid;
  987. info->offset = key.offset;
  988. info++;
  989. }
  990. BUG_ON(!root->ref_tree);
  991. ret = btrfs_add_leaf_ref(root, ref);
  992. WARN_ON(ret);
  993. btrfs_free_leaf_ref(root, ref);
  994. }
  995. out:
  996. return 0;
  997. fail:
  998. WARN_ON(1);
  999. #if 0
  1000. for (i =0; i < faili; i++) {
  1001. if (level == 0) {
  1002. u64 disk_bytenr;
  1003. btrfs_item_key_to_cpu(buf, &key, i);
  1004. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1005. continue;
  1006. fi = btrfs_item_ptr(buf, i,
  1007. struct btrfs_file_extent_item);
  1008. if (btrfs_file_extent_type(buf, fi) ==
  1009. BTRFS_FILE_EXTENT_INLINE)
  1010. continue;
  1011. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1012. if (disk_bytenr == 0)
  1013. continue;
  1014. err = btrfs_free_extent(trans, root, disk_bytenr,
  1015. btrfs_file_extent_disk_num_bytes(buf,
  1016. fi), 0);
  1017. BUG_ON(err);
  1018. } else {
  1019. bytenr = btrfs_node_blockptr(buf, i);
  1020. err = btrfs_free_extent(trans, root, bytenr,
  1021. btrfs_level_size(root, level - 1), 0);
  1022. BUG_ON(err);
  1023. }
  1024. }
  1025. #endif
  1026. return ret;
  1027. }
  1028. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1029. struct btrfs_root *root,
  1030. struct btrfs_path *path,
  1031. struct btrfs_block_group_cache *cache)
  1032. {
  1033. int ret;
  1034. int pending_ret;
  1035. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1036. unsigned long bi;
  1037. struct extent_buffer *leaf;
  1038. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1039. if (ret < 0)
  1040. goto fail;
  1041. BUG_ON(ret);
  1042. leaf = path->nodes[0];
  1043. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1044. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1045. btrfs_mark_buffer_dirty(leaf);
  1046. btrfs_release_path(extent_root, path);
  1047. fail:
  1048. finish_current_insert(trans, extent_root);
  1049. pending_ret = del_pending_extents(trans, extent_root);
  1050. if (ret)
  1051. return ret;
  1052. if (pending_ret)
  1053. return pending_ret;
  1054. return 0;
  1055. }
  1056. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1057. struct btrfs_root *root)
  1058. {
  1059. struct extent_io_tree *block_group_cache;
  1060. struct btrfs_block_group_cache *cache;
  1061. int ret;
  1062. int err = 0;
  1063. int werr = 0;
  1064. struct btrfs_path *path;
  1065. u64 last = 0;
  1066. u64 start;
  1067. u64 end;
  1068. u64 ptr;
  1069. block_group_cache = &root->fs_info->block_group_cache;
  1070. path = btrfs_alloc_path();
  1071. if (!path)
  1072. return -ENOMEM;
  1073. mutex_lock(&root->fs_info->alloc_mutex);
  1074. while(1) {
  1075. ret = find_first_extent_bit(block_group_cache, last,
  1076. &start, &end, BLOCK_GROUP_DIRTY);
  1077. if (ret)
  1078. break;
  1079. last = end + 1;
  1080. ret = get_state_private(block_group_cache, start, &ptr);
  1081. if (ret)
  1082. break;
  1083. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  1084. err = write_one_cache_group(trans, root,
  1085. path, cache);
  1086. /*
  1087. * if we fail to write the cache group, we want
  1088. * to keep it marked dirty in hopes that a later
  1089. * write will work
  1090. */
  1091. if (err) {
  1092. werr = err;
  1093. continue;
  1094. }
  1095. clear_extent_bits(block_group_cache, start, end,
  1096. BLOCK_GROUP_DIRTY, GFP_NOFS);
  1097. }
  1098. btrfs_free_path(path);
  1099. mutex_unlock(&root->fs_info->alloc_mutex);
  1100. return werr;
  1101. }
  1102. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  1103. u64 flags)
  1104. {
  1105. struct list_head *head = &info->space_info;
  1106. struct list_head *cur;
  1107. struct btrfs_space_info *found;
  1108. list_for_each(cur, head) {
  1109. found = list_entry(cur, struct btrfs_space_info, list);
  1110. if (found->flags == flags)
  1111. return found;
  1112. }
  1113. return NULL;
  1114. }
  1115. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1116. u64 total_bytes, u64 bytes_used,
  1117. struct btrfs_space_info **space_info)
  1118. {
  1119. struct btrfs_space_info *found;
  1120. found = __find_space_info(info, flags);
  1121. if (found) {
  1122. found->total_bytes += total_bytes;
  1123. found->bytes_used += bytes_used;
  1124. found->full = 0;
  1125. *space_info = found;
  1126. return 0;
  1127. }
  1128. found = kmalloc(sizeof(*found), GFP_NOFS);
  1129. if (!found)
  1130. return -ENOMEM;
  1131. list_add(&found->list, &info->space_info);
  1132. found->flags = flags;
  1133. found->total_bytes = total_bytes;
  1134. found->bytes_used = bytes_used;
  1135. found->bytes_pinned = 0;
  1136. found->full = 0;
  1137. found->force_alloc = 0;
  1138. *space_info = found;
  1139. return 0;
  1140. }
  1141. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1142. {
  1143. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1144. BTRFS_BLOCK_GROUP_RAID1 |
  1145. BTRFS_BLOCK_GROUP_RAID10 |
  1146. BTRFS_BLOCK_GROUP_DUP);
  1147. if (extra_flags) {
  1148. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1149. fs_info->avail_data_alloc_bits |= extra_flags;
  1150. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1151. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1152. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1153. fs_info->avail_system_alloc_bits |= extra_flags;
  1154. }
  1155. }
  1156. static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1157. {
  1158. u64 num_devices = root->fs_info->fs_devices->num_devices;
  1159. if (num_devices == 1)
  1160. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1161. if (num_devices < 4)
  1162. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1163. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1164. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1165. BTRFS_BLOCK_GROUP_RAID10))) {
  1166. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1167. }
  1168. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1169. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1170. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1171. }
  1172. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1173. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1174. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1175. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1176. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1177. return flags;
  1178. }
  1179. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1180. struct btrfs_root *extent_root, u64 alloc_bytes,
  1181. u64 flags, int force)
  1182. {
  1183. struct btrfs_space_info *space_info;
  1184. u64 thresh;
  1185. u64 start;
  1186. u64 num_bytes;
  1187. int ret;
  1188. flags = reduce_alloc_profile(extent_root, flags);
  1189. space_info = __find_space_info(extent_root->fs_info, flags);
  1190. if (!space_info) {
  1191. ret = update_space_info(extent_root->fs_info, flags,
  1192. 0, 0, &space_info);
  1193. BUG_ON(ret);
  1194. }
  1195. BUG_ON(!space_info);
  1196. if (space_info->force_alloc) {
  1197. force = 1;
  1198. space_info->force_alloc = 0;
  1199. }
  1200. if (space_info->full)
  1201. goto out;
  1202. thresh = div_factor(space_info->total_bytes, 6);
  1203. if (!force &&
  1204. (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
  1205. thresh)
  1206. goto out;
  1207. mutex_lock(&extent_root->fs_info->chunk_mutex);
  1208. ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
  1209. if (ret == -ENOSPC) {
  1210. printk("space info full %Lu\n", flags);
  1211. space_info->full = 1;
  1212. goto out_unlock;
  1213. }
  1214. BUG_ON(ret);
  1215. ret = btrfs_make_block_group(trans, extent_root, 0, flags,
  1216. BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
  1217. BUG_ON(ret);
  1218. out_unlock:
  1219. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1220. out:
  1221. return 0;
  1222. }
  1223. static int update_block_group(struct btrfs_trans_handle *trans,
  1224. struct btrfs_root *root,
  1225. u64 bytenr, u64 num_bytes, int alloc,
  1226. int mark_free)
  1227. {
  1228. struct btrfs_block_group_cache *cache;
  1229. struct btrfs_fs_info *info = root->fs_info;
  1230. u64 total = num_bytes;
  1231. u64 old_val;
  1232. u64 byte_in_group;
  1233. u64 start;
  1234. u64 end;
  1235. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1236. while(total) {
  1237. cache = btrfs_lookup_block_group(info, bytenr);
  1238. if (!cache) {
  1239. return -1;
  1240. }
  1241. byte_in_group = bytenr - cache->key.objectid;
  1242. WARN_ON(byte_in_group > cache->key.offset);
  1243. start = cache->key.objectid;
  1244. end = start + cache->key.offset - 1;
  1245. set_extent_bits(&info->block_group_cache, start, end,
  1246. BLOCK_GROUP_DIRTY, GFP_NOFS);
  1247. spin_lock(&cache->lock);
  1248. old_val = btrfs_block_group_used(&cache->item);
  1249. num_bytes = min(total, cache->key.offset - byte_in_group);
  1250. if (alloc) {
  1251. old_val += num_bytes;
  1252. cache->space_info->bytes_used += num_bytes;
  1253. btrfs_set_block_group_used(&cache->item, old_val);
  1254. spin_unlock(&cache->lock);
  1255. } else {
  1256. old_val -= num_bytes;
  1257. cache->space_info->bytes_used -= num_bytes;
  1258. btrfs_set_block_group_used(&cache->item, old_val);
  1259. spin_unlock(&cache->lock);
  1260. if (mark_free) {
  1261. set_extent_dirty(&info->free_space_cache,
  1262. bytenr, bytenr + num_bytes - 1,
  1263. GFP_NOFS);
  1264. }
  1265. }
  1266. total -= num_bytes;
  1267. bytenr += num_bytes;
  1268. }
  1269. return 0;
  1270. }
  1271. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1272. {
  1273. u64 start;
  1274. u64 end;
  1275. int ret;
  1276. ret = find_first_extent_bit(&root->fs_info->block_group_cache,
  1277. search_start, &start, &end,
  1278. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
  1279. BLOCK_GROUP_SYSTEM);
  1280. if (ret)
  1281. return 0;
  1282. return start;
  1283. }
  1284. static int update_pinned_extents(struct btrfs_root *root,
  1285. u64 bytenr, u64 num, int pin)
  1286. {
  1287. u64 len;
  1288. struct btrfs_block_group_cache *cache;
  1289. struct btrfs_fs_info *fs_info = root->fs_info;
  1290. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1291. if (pin) {
  1292. set_extent_dirty(&fs_info->pinned_extents,
  1293. bytenr, bytenr + num - 1, GFP_NOFS);
  1294. } else {
  1295. clear_extent_dirty(&fs_info->pinned_extents,
  1296. bytenr, bytenr + num - 1, GFP_NOFS);
  1297. }
  1298. while (num > 0) {
  1299. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1300. if (!cache) {
  1301. u64 first = first_logical_byte(root, bytenr);
  1302. WARN_ON(first < bytenr);
  1303. len = min(first - bytenr, num);
  1304. } else {
  1305. len = min(num, cache->key.offset -
  1306. (bytenr - cache->key.objectid));
  1307. }
  1308. if (pin) {
  1309. if (cache) {
  1310. spin_lock(&cache->lock);
  1311. cache->pinned += len;
  1312. cache->space_info->bytes_pinned += len;
  1313. spin_unlock(&cache->lock);
  1314. }
  1315. fs_info->total_pinned += len;
  1316. } else {
  1317. if (cache) {
  1318. spin_lock(&cache->lock);
  1319. cache->pinned -= len;
  1320. cache->space_info->bytes_pinned -= len;
  1321. spin_unlock(&cache->lock);
  1322. }
  1323. fs_info->total_pinned -= len;
  1324. }
  1325. bytenr += len;
  1326. num -= len;
  1327. }
  1328. return 0;
  1329. }
  1330. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1331. {
  1332. u64 last = 0;
  1333. u64 start;
  1334. u64 end;
  1335. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1336. int ret;
  1337. while(1) {
  1338. ret = find_first_extent_bit(pinned_extents, last,
  1339. &start, &end, EXTENT_DIRTY);
  1340. if (ret)
  1341. break;
  1342. set_extent_dirty(copy, start, end, GFP_NOFS);
  1343. last = end + 1;
  1344. }
  1345. return 0;
  1346. }
  1347. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1348. struct btrfs_root *root,
  1349. struct extent_io_tree *unpin)
  1350. {
  1351. u64 start;
  1352. u64 end;
  1353. int ret;
  1354. struct extent_io_tree *free_space_cache;
  1355. free_space_cache = &root->fs_info->free_space_cache;
  1356. mutex_lock(&root->fs_info->alloc_mutex);
  1357. while(1) {
  1358. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1359. EXTENT_DIRTY);
  1360. if (ret)
  1361. break;
  1362. update_pinned_extents(root, start, end + 1 - start, 0);
  1363. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1364. set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
  1365. if (need_resched()) {
  1366. mutex_unlock(&root->fs_info->alloc_mutex);
  1367. cond_resched();
  1368. mutex_lock(&root->fs_info->alloc_mutex);
  1369. }
  1370. }
  1371. mutex_unlock(&root->fs_info->alloc_mutex);
  1372. return 0;
  1373. }
  1374. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1375. struct btrfs_root *extent_root)
  1376. {
  1377. u64 start;
  1378. u64 end;
  1379. struct btrfs_fs_info *info = extent_root->fs_info;
  1380. struct extent_buffer *eb;
  1381. struct btrfs_path *path;
  1382. struct btrfs_key ins;
  1383. struct btrfs_disk_key first;
  1384. struct btrfs_extent_item extent_item;
  1385. int ret;
  1386. int level;
  1387. int err = 0;
  1388. WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
  1389. btrfs_set_stack_extent_refs(&extent_item, 1);
  1390. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  1391. path = btrfs_alloc_path();
  1392. while(1) {
  1393. ret = find_first_extent_bit(&info->extent_ins, 0, &start,
  1394. &end, EXTENT_LOCKED);
  1395. if (ret)
  1396. break;
  1397. ins.objectid = start;
  1398. ins.offset = end + 1 - start;
  1399. err = btrfs_insert_item(trans, extent_root, &ins,
  1400. &extent_item, sizeof(extent_item));
  1401. clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
  1402. GFP_NOFS);
  1403. eb = btrfs_find_tree_block(extent_root, ins.objectid,
  1404. ins.offset);
  1405. if (!btrfs_buffer_uptodate(eb, trans->transid)) {
  1406. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  1407. btrfs_read_buffer(eb, trans->transid);
  1408. mutex_lock(&extent_root->fs_info->alloc_mutex);
  1409. }
  1410. btrfs_tree_lock(eb);
  1411. level = btrfs_header_level(eb);
  1412. if (level == 0) {
  1413. btrfs_item_key(eb, &first, 0);
  1414. } else {
  1415. btrfs_node_key(eb, &first, 0);
  1416. }
  1417. btrfs_tree_unlock(eb);
  1418. free_extent_buffer(eb);
  1419. /*
  1420. * the first key is just a hint, so the race we've created
  1421. * against reading it is fine
  1422. */
  1423. err = btrfs_insert_extent_backref(trans, extent_root, path,
  1424. start, extent_root->root_key.objectid,
  1425. 0, level,
  1426. btrfs_disk_key_objectid(&first));
  1427. BUG_ON(err);
  1428. if (need_resched()) {
  1429. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  1430. cond_resched();
  1431. mutex_lock(&extent_root->fs_info->alloc_mutex);
  1432. }
  1433. }
  1434. btrfs_free_path(path);
  1435. return 0;
  1436. }
  1437. static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
  1438. int pending)
  1439. {
  1440. int err = 0;
  1441. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1442. if (!pending) {
  1443. struct extent_buffer *buf;
  1444. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  1445. if (buf) {
  1446. if (btrfs_buffer_uptodate(buf, 0) &&
  1447. btrfs_try_tree_lock(buf)) {
  1448. u64 transid =
  1449. root->fs_info->running_transaction->transid;
  1450. u64 header_transid =
  1451. btrfs_header_generation(buf);
  1452. if (header_transid == transid &&
  1453. !btrfs_header_flag(buf,
  1454. BTRFS_HEADER_FLAG_WRITTEN)) {
  1455. clean_tree_block(NULL, root, buf);
  1456. btrfs_tree_unlock(buf);
  1457. free_extent_buffer(buf);
  1458. return 1;
  1459. }
  1460. btrfs_tree_unlock(buf);
  1461. }
  1462. free_extent_buffer(buf);
  1463. }
  1464. update_pinned_extents(root, bytenr, num_bytes, 1);
  1465. } else {
  1466. set_extent_bits(&root->fs_info->pending_del,
  1467. bytenr, bytenr + num_bytes - 1,
  1468. EXTENT_LOCKED, GFP_NOFS);
  1469. }
  1470. BUG_ON(err < 0);
  1471. return 0;
  1472. }
  1473. /*
  1474. * remove an extent from the root, returns 0 on success
  1475. */
  1476. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1477. *root, u64 bytenr, u64 num_bytes,
  1478. u64 root_objectid, u64 ref_generation,
  1479. u64 owner_objectid, u64 owner_offset, int pin,
  1480. int mark_free)
  1481. {
  1482. struct btrfs_path *path;
  1483. struct btrfs_key key;
  1484. struct btrfs_fs_info *info = root->fs_info;
  1485. struct btrfs_root *extent_root = info->extent_root;
  1486. struct extent_buffer *leaf;
  1487. int ret;
  1488. int extent_slot = 0;
  1489. int found_extent = 0;
  1490. int num_to_del = 1;
  1491. struct btrfs_extent_item *ei;
  1492. u32 refs;
  1493. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1494. key.objectid = bytenr;
  1495. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1496. key.offset = num_bytes;
  1497. path = btrfs_alloc_path();
  1498. if (!path)
  1499. return -ENOMEM;
  1500. path->reada = 1;
  1501. ret = lookup_extent_backref(trans, extent_root, path,
  1502. bytenr, root_objectid,
  1503. ref_generation,
  1504. owner_objectid, owner_offset, 1);
  1505. if (ret == 0) {
  1506. struct btrfs_key found_key;
  1507. extent_slot = path->slots[0];
  1508. while(extent_slot > 0) {
  1509. extent_slot--;
  1510. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1511. extent_slot);
  1512. if (found_key.objectid != bytenr)
  1513. break;
  1514. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  1515. found_key.offset == num_bytes) {
  1516. found_extent = 1;
  1517. break;
  1518. }
  1519. if (path->slots[0] - extent_slot > 5)
  1520. break;
  1521. }
  1522. if (!found_extent)
  1523. ret = btrfs_del_item(trans, extent_root, path);
  1524. } else {
  1525. btrfs_print_leaf(extent_root, path->nodes[0]);
  1526. WARN_ON(1);
  1527. printk("Unable to find ref byte nr %Lu root %Lu "
  1528. " gen %Lu owner %Lu offset %Lu\n", bytenr,
  1529. root_objectid, ref_generation, owner_objectid,
  1530. owner_offset);
  1531. }
  1532. if (!found_extent) {
  1533. btrfs_release_path(extent_root, path);
  1534. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  1535. if (ret < 0)
  1536. return ret;
  1537. BUG_ON(ret);
  1538. extent_slot = path->slots[0];
  1539. }
  1540. leaf = path->nodes[0];
  1541. ei = btrfs_item_ptr(leaf, extent_slot,
  1542. struct btrfs_extent_item);
  1543. refs = btrfs_extent_refs(leaf, ei);
  1544. BUG_ON(refs == 0);
  1545. refs -= 1;
  1546. btrfs_set_extent_refs(leaf, ei, refs);
  1547. btrfs_mark_buffer_dirty(leaf);
  1548. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  1549. /* if the back ref and the extent are next to each other
  1550. * they get deleted below in one shot
  1551. */
  1552. path->slots[0] = extent_slot;
  1553. num_to_del = 2;
  1554. } else if (found_extent) {
  1555. /* otherwise delete the extent back ref */
  1556. ret = btrfs_del_item(trans, extent_root, path);
  1557. BUG_ON(ret);
  1558. /* if refs are 0, we need to setup the path for deletion */
  1559. if (refs == 0) {
  1560. btrfs_release_path(extent_root, path);
  1561. ret = btrfs_search_slot(trans, extent_root, &key, path,
  1562. -1, 1);
  1563. if (ret < 0)
  1564. return ret;
  1565. BUG_ON(ret);
  1566. }
  1567. }
  1568. if (refs == 0) {
  1569. u64 super_used;
  1570. u64 root_used;
  1571. if (pin) {
  1572. ret = pin_down_bytes(root, bytenr, num_bytes, 0);
  1573. if (ret > 0)
  1574. mark_free = 1;
  1575. BUG_ON(ret < 0);
  1576. }
  1577. /* block accounting for super block */
  1578. spin_lock_irq(&info->delalloc_lock);
  1579. super_used = btrfs_super_bytes_used(&info->super_copy);
  1580. btrfs_set_super_bytes_used(&info->super_copy,
  1581. super_used - num_bytes);
  1582. spin_unlock_irq(&info->delalloc_lock);
  1583. /* block accounting for root item */
  1584. root_used = btrfs_root_used(&root->root_item);
  1585. btrfs_set_root_used(&root->root_item,
  1586. root_used - num_bytes);
  1587. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  1588. num_to_del);
  1589. if (ret) {
  1590. return ret;
  1591. }
  1592. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1593. mark_free);
  1594. BUG_ON(ret);
  1595. }
  1596. btrfs_free_path(path);
  1597. finish_current_insert(trans, extent_root);
  1598. return ret;
  1599. }
  1600. /*
  1601. * find all the blocks marked as pending in the radix tree and remove
  1602. * them from the extent map
  1603. */
  1604. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1605. btrfs_root *extent_root)
  1606. {
  1607. int ret;
  1608. int err = 0;
  1609. u64 start;
  1610. u64 end;
  1611. struct extent_io_tree *pending_del;
  1612. struct extent_io_tree *pinned_extents;
  1613. WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
  1614. pending_del = &extent_root->fs_info->pending_del;
  1615. pinned_extents = &extent_root->fs_info->pinned_extents;
  1616. while(1) {
  1617. ret = find_first_extent_bit(pending_del, 0, &start, &end,
  1618. EXTENT_LOCKED);
  1619. if (ret)
  1620. break;
  1621. clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
  1622. GFP_NOFS);
  1623. if (!test_range_bit(&extent_root->fs_info->extent_ins,
  1624. start, end, EXTENT_LOCKED, 0)) {
  1625. update_pinned_extents(extent_root, start,
  1626. end + 1 - start, 1);
  1627. ret = __free_extent(trans, extent_root,
  1628. start, end + 1 - start,
  1629. extent_root->root_key.objectid,
  1630. 0, 0, 0, 0, 0);
  1631. } else {
  1632. clear_extent_bits(&extent_root->fs_info->extent_ins,
  1633. start, end, EXTENT_LOCKED, GFP_NOFS);
  1634. }
  1635. if (ret)
  1636. err = ret;
  1637. if (need_resched()) {
  1638. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  1639. cond_resched();
  1640. mutex_lock(&extent_root->fs_info->alloc_mutex);
  1641. }
  1642. }
  1643. return err;
  1644. }
  1645. /*
  1646. * remove an extent from the root, returns 0 on success
  1647. */
  1648. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  1649. struct btrfs_root *root, u64 bytenr,
  1650. u64 num_bytes, u64 root_objectid,
  1651. u64 ref_generation, u64 owner_objectid,
  1652. u64 owner_offset, int pin)
  1653. {
  1654. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1655. int pending_ret;
  1656. int ret;
  1657. WARN_ON(num_bytes < root->sectorsize);
  1658. if (!root->ref_cows)
  1659. ref_generation = 0;
  1660. if (root == extent_root) {
  1661. pin_down_bytes(root, bytenr, num_bytes, 1);
  1662. return 0;
  1663. }
  1664. ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
  1665. ref_generation, owner_objectid, owner_offset,
  1666. pin, pin == 0);
  1667. finish_current_insert(trans, root->fs_info->extent_root);
  1668. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  1669. return ret ? ret : pending_ret;
  1670. }
  1671. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  1672. struct btrfs_root *root, u64 bytenr,
  1673. u64 num_bytes, u64 root_objectid,
  1674. u64 ref_generation, u64 owner_objectid,
  1675. u64 owner_offset, int pin)
  1676. {
  1677. int ret;
  1678. maybe_lock_mutex(root);
  1679. ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
  1680. root_objectid, ref_generation,
  1681. owner_objectid, owner_offset, pin);
  1682. maybe_unlock_mutex(root);
  1683. return ret;
  1684. }
  1685. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1686. {
  1687. u64 mask = ((u64)root->stripesize - 1);
  1688. u64 ret = (val + mask) & ~mask;
  1689. return ret;
  1690. }
  1691. /*
  1692. * walks the btree of allocated extents and find a hole of a given size.
  1693. * The key ins is changed to record the hole:
  1694. * ins->objectid == block start
  1695. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1696. * ins->offset == number of blocks
  1697. * Any available blocks before search_start are skipped.
  1698. */
  1699. static int noinline find_free_extent(struct btrfs_trans_handle *trans,
  1700. struct btrfs_root *orig_root,
  1701. u64 num_bytes, u64 empty_size,
  1702. u64 search_start, u64 search_end,
  1703. u64 hint_byte, struct btrfs_key *ins,
  1704. u64 exclude_start, u64 exclude_nr,
  1705. int data)
  1706. {
  1707. int ret;
  1708. u64 orig_search_start;
  1709. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1710. struct btrfs_fs_info *info = root->fs_info;
  1711. u64 total_needed = num_bytes;
  1712. u64 *last_ptr = NULL;
  1713. struct btrfs_block_group_cache *block_group;
  1714. int full_scan = 0;
  1715. int wrapped = 0;
  1716. int chunk_alloc_done = 0;
  1717. int empty_cluster = 2 * 1024 * 1024;
  1718. int allowed_chunk_alloc = 0;
  1719. WARN_ON(num_bytes < root->sectorsize);
  1720. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1721. if (orig_root->ref_cows || empty_size)
  1722. allowed_chunk_alloc = 1;
  1723. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  1724. last_ptr = &root->fs_info->last_alloc;
  1725. empty_cluster = 256 * 1024;
  1726. }
  1727. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
  1728. last_ptr = &root->fs_info->last_data_alloc;
  1729. }
  1730. if (last_ptr) {
  1731. if (*last_ptr)
  1732. hint_byte = *last_ptr;
  1733. else {
  1734. empty_size += empty_cluster;
  1735. }
  1736. }
  1737. search_start = max(search_start, first_logical_byte(root, 0));
  1738. orig_search_start = search_start;
  1739. if (search_end == (u64)-1)
  1740. search_end = btrfs_super_total_bytes(&info->super_copy);
  1741. if (hint_byte) {
  1742. block_group = btrfs_lookup_first_block_group(info, hint_byte);
  1743. if (!block_group)
  1744. hint_byte = search_start;
  1745. block_group = btrfs_find_block_group(root, block_group,
  1746. hint_byte, data, 1);
  1747. if (last_ptr && *last_ptr == 0 && block_group)
  1748. hint_byte = block_group->key.objectid;
  1749. } else {
  1750. block_group = btrfs_find_block_group(root,
  1751. trans->block_group,
  1752. search_start, data, 1);
  1753. }
  1754. search_start = max(search_start, hint_byte);
  1755. total_needed += empty_size;
  1756. check_failed:
  1757. if (!block_group) {
  1758. block_group = btrfs_lookup_first_block_group(info,
  1759. search_start);
  1760. if (!block_group)
  1761. block_group = btrfs_lookup_first_block_group(info,
  1762. orig_search_start);
  1763. }
  1764. if (full_scan && !chunk_alloc_done) {
  1765. if (allowed_chunk_alloc) {
  1766. do_chunk_alloc(trans, root,
  1767. num_bytes + 2 * 1024 * 1024, data, 1);
  1768. allowed_chunk_alloc = 0;
  1769. } else if (block_group && block_group_bits(block_group, data)) {
  1770. block_group->space_info->force_alloc = 1;
  1771. }
  1772. chunk_alloc_done = 1;
  1773. }
  1774. ret = find_search_start(root, &block_group, &search_start,
  1775. total_needed, data);
  1776. if (ret == -ENOSPC && last_ptr && *last_ptr) {
  1777. *last_ptr = 0;
  1778. block_group = btrfs_lookup_first_block_group(info,
  1779. orig_search_start);
  1780. search_start = orig_search_start;
  1781. ret = find_search_start(root, &block_group, &search_start,
  1782. total_needed, data);
  1783. }
  1784. if (ret == -ENOSPC)
  1785. goto enospc;
  1786. if (ret)
  1787. goto error;
  1788. if (last_ptr && *last_ptr && search_start != *last_ptr) {
  1789. *last_ptr = 0;
  1790. if (!empty_size) {
  1791. empty_size += empty_cluster;
  1792. total_needed += empty_size;
  1793. }
  1794. block_group = btrfs_lookup_first_block_group(info,
  1795. orig_search_start);
  1796. search_start = orig_search_start;
  1797. ret = find_search_start(root, &block_group,
  1798. &search_start, total_needed, data);
  1799. if (ret == -ENOSPC)
  1800. goto enospc;
  1801. if (ret)
  1802. goto error;
  1803. }
  1804. search_start = stripe_align(root, search_start);
  1805. ins->objectid = search_start;
  1806. ins->offset = num_bytes;
  1807. if (ins->objectid + num_bytes >= search_end)
  1808. goto enospc;
  1809. if (ins->objectid + num_bytes >
  1810. block_group->key.objectid + block_group->key.offset) {
  1811. search_start = block_group->key.objectid +
  1812. block_group->key.offset;
  1813. goto new_group;
  1814. }
  1815. if (test_range_bit(&info->extent_ins, ins->objectid,
  1816. ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
  1817. search_start = ins->objectid + num_bytes;
  1818. goto new_group;
  1819. }
  1820. if (test_range_bit(&info->pinned_extents, ins->objectid,
  1821. ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
  1822. search_start = ins->objectid + num_bytes;
  1823. goto new_group;
  1824. }
  1825. if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
  1826. ins->objectid < exclude_start + exclude_nr)) {
  1827. search_start = exclude_start + exclude_nr;
  1828. goto new_group;
  1829. }
  1830. if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
  1831. block_group = btrfs_lookup_block_group(info, ins->objectid);
  1832. if (block_group)
  1833. trans->block_group = block_group;
  1834. }
  1835. ins->offset = num_bytes;
  1836. if (last_ptr) {
  1837. *last_ptr = ins->objectid + ins->offset;
  1838. if (*last_ptr ==
  1839. btrfs_super_total_bytes(&root->fs_info->super_copy)) {
  1840. *last_ptr = 0;
  1841. }
  1842. }
  1843. return 0;
  1844. new_group:
  1845. if (search_start + num_bytes >= search_end) {
  1846. enospc:
  1847. search_start = orig_search_start;
  1848. if (full_scan) {
  1849. ret = -ENOSPC;
  1850. goto error;
  1851. }
  1852. if (wrapped) {
  1853. if (!full_scan)
  1854. total_needed -= empty_size;
  1855. full_scan = 1;
  1856. } else
  1857. wrapped = 1;
  1858. }
  1859. block_group = btrfs_lookup_first_block_group(info, search_start);
  1860. cond_resched();
  1861. block_group = btrfs_find_block_group(root, block_group,
  1862. search_start, data, 0);
  1863. goto check_failed;
  1864. error:
  1865. return ret;
  1866. }
  1867. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  1868. struct btrfs_root *root,
  1869. u64 num_bytes, u64 min_alloc_size,
  1870. u64 empty_size, u64 hint_byte,
  1871. u64 search_end, struct btrfs_key *ins,
  1872. u64 data)
  1873. {
  1874. int ret;
  1875. u64 search_start = 0;
  1876. u64 alloc_profile;
  1877. struct btrfs_fs_info *info = root->fs_info;
  1878. if (data) {
  1879. alloc_profile = info->avail_data_alloc_bits &
  1880. info->data_alloc_profile;
  1881. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  1882. } else if (root == root->fs_info->chunk_root) {
  1883. alloc_profile = info->avail_system_alloc_bits &
  1884. info->system_alloc_profile;
  1885. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  1886. } else {
  1887. alloc_profile = info->avail_metadata_alloc_bits &
  1888. info->metadata_alloc_profile;
  1889. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  1890. }
  1891. again:
  1892. data = reduce_alloc_profile(root, data);
  1893. /*
  1894. * the only place that sets empty_size is btrfs_realloc_node, which
  1895. * is not called recursively on allocations
  1896. */
  1897. if (empty_size || root->ref_cows) {
  1898. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  1899. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1900. 2 * 1024 * 1024,
  1901. BTRFS_BLOCK_GROUP_METADATA |
  1902. (info->metadata_alloc_profile &
  1903. info->avail_metadata_alloc_bits), 0);
  1904. BUG_ON(ret);
  1905. }
  1906. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1907. num_bytes + 2 * 1024 * 1024, data, 0);
  1908. BUG_ON(ret);
  1909. }
  1910. WARN_ON(num_bytes < root->sectorsize);
  1911. ret = find_free_extent(trans, root, num_bytes, empty_size,
  1912. search_start, search_end, hint_byte, ins,
  1913. trans->alloc_exclude_start,
  1914. trans->alloc_exclude_nr, data);
  1915. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  1916. num_bytes = num_bytes >> 1;
  1917. num_bytes = max(num_bytes, min_alloc_size);
  1918. do_chunk_alloc(trans, root->fs_info->extent_root,
  1919. num_bytes, data, 1);
  1920. goto again;
  1921. }
  1922. if (ret) {
  1923. printk("allocation failed flags %Lu\n", data);
  1924. BUG();
  1925. }
  1926. clear_extent_dirty(&root->fs_info->free_space_cache,
  1927. ins->objectid, ins->objectid + ins->offset - 1,
  1928. GFP_NOFS);
  1929. return 0;
  1930. }
  1931. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  1932. {
  1933. maybe_lock_mutex(root);
  1934. set_extent_dirty(&root->fs_info->free_space_cache,
  1935. start, start + len - 1, GFP_NOFS);
  1936. maybe_unlock_mutex(root);
  1937. return 0;
  1938. }
  1939. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  1940. struct btrfs_root *root,
  1941. u64 num_bytes, u64 min_alloc_size,
  1942. u64 empty_size, u64 hint_byte,
  1943. u64 search_end, struct btrfs_key *ins,
  1944. u64 data)
  1945. {
  1946. int ret;
  1947. maybe_lock_mutex(root);
  1948. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  1949. empty_size, hint_byte, search_end, ins,
  1950. data);
  1951. maybe_unlock_mutex(root);
  1952. return ret;
  1953. }
  1954. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  1955. struct btrfs_root *root,
  1956. u64 root_objectid, u64 ref_generation,
  1957. u64 owner, u64 owner_offset,
  1958. struct btrfs_key *ins)
  1959. {
  1960. int ret;
  1961. int pending_ret;
  1962. u64 super_used;
  1963. u64 root_used;
  1964. u64 num_bytes = ins->offset;
  1965. u32 sizes[2];
  1966. struct btrfs_fs_info *info = root->fs_info;
  1967. struct btrfs_root *extent_root = info->extent_root;
  1968. struct btrfs_extent_item *extent_item;
  1969. struct btrfs_extent_ref *ref;
  1970. struct btrfs_path *path;
  1971. struct btrfs_key keys[2];
  1972. /* block accounting for super block */
  1973. spin_lock_irq(&info->delalloc_lock);
  1974. super_used = btrfs_super_bytes_used(&info->super_copy);
  1975. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  1976. spin_unlock_irq(&info->delalloc_lock);
  1977. /* block accounting for root item */
  1978. root_used = btrfs_root_used(&root->root_item);
  1979. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  1980. if (root == extent_root) {
  1981. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  1982. ins->objectid + ins->offset - 1,
  1983. EXTENT_LOCKED, GFP_NOFS);
  1984. goto update_block;
  1985. }
  1986. memcpy(&keys[0], ins, sizeof(*ins));
  1987. keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
  1988. owner, owner_offset);
  1989. keys[1].objectid = ins->objectid;
  1990. keys[1].type = BTRFS_EXTENT_REF_KEY;
  1991. sizes[0] = sizeof(*extent_item);
  1992. sizes[1] = sizeof(*ref);
  1993. path = btrfs_alloc_path();
  1994. BUG_ON(!path);
  1995. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  1996. sizes, 2);
  1997. BUG_ON(ret);
  1998. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1999. struct btrfs_extent_item);
  2000. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  2001. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2002. struct btrfs_extent_ref);
  2003. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2004. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2005. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2006. btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
  2007. btrfs_mark_buffer_dirty(path->nodes[0]);
  2008. trans->alloc_exclude_start = 0;
  2009. trans->alloc_exclude_nr = 0;
  2010. btrfs_free_path(path);
  2011. finish_current_insert(trans, extent_root);
  2012. pending_ret = del_pending_extents(trans, extent_root);
  2013. if (ret)
  2014. goto out;
  2015. if (pending_ret) {
  2016. ret = pending_ret;
  2017. goto out;
  2018. }
  2019. update_block:
  2020. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
  2021. if (ret) {
  2022. printk("update block group failed for %Lu %Lu\n",
  2023. ins->objectid, ins->offset);
  2024. BUG();
  2025. }
  2026. out:
  2027. return ret;
  2028. }
  2029. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2030. struct btrfs_root *root,
  2031. u64 root_objectid, u64 ref_generation,
  2032. u64 owner, u64 owner_offset,
  2033. struct btrfs_key *ins)
  2034. {
  2035. int ret;
  2036. maybe_lock_mutex(root);
  2037. ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
  2038. ref_generation, owner,
  2039. owner_offset, ins);
  2040. maybe_unlock_mutex(root);
  2041. return ret;
  2042. }
  2043. /*
  2044. * finds a free extent and does all the dirty work required for allocation
  2045. * returns the key for the extent through ins, and a tree buffer for
  2046. * the first block of the extent through buf.
  2047. *
  2048. * returns 0 if everything worked, non-zero otherwise.
  2049. */
  2050. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  2051. struct btrfs_root *root,
  2052. u64 num_bytes, u64 min_alloc_size,
  2053. u64 root_objectid, u64 ref_generation,
  2054. u64 owner, u64 owner_offset,
  2055. u64 empty_size, u64 hint_byte,
  2056. u64 search_end, struct btrfs_key *ins, u64 data)
  2057. {
  2058. int ret;
  2059. maybe_lock_mutex(root);
  2060. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  2061. min_alloc_size, empty_size, hint_byte,
  2062. search_end, ins, data);
  2063. BUG_ON(ret);
  2064. ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
  2065. ref_generation, owner,
  2066. owner_offset, ins);
  2067. BUG_ON(ret);
  2068. maybe_unlock_mutex(root);
  2069. return ret;
  2070. }
  2071. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  2072. struct btrfs_root *root,
  2073. u64 bytenr, u32 blocksize)
  2074. {
  2075. struct extent_buffer *buf;
  2076. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  2077. if (!buf)
  2078. return ERR_PTR(-ENOMEM);
  2079. btrfs_set_header_generation(buf, trans->transid);
  2080. btrfs_tree_lock(buf);
  2081. clean_tree_block(trans, root, buf);
  2082. btrfs_set_buffer_uptodate(buf);
  2083. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  2084. buf->start + buf->len - 1, GFP_NOFS);
  2085. trans->blocks_used++;
  2086. return buf;
  2087. }
  2088. /*
  2089. * helper function to allocate a block for a given tree
  2090. * returns the tree buffer or NULL.
  2091. */
  2092. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  2093. struct btrfs_root *root,
  2094. u32 blocksize,
  2095. u64 root_objectid,
  2096. u64 ref_generation,
  2097. u64 first_objectid,
  2098. int level,
  2099. u64 hint,
  2100. u64 empty_size)
  2101. {
  2102. struct btrfs_key ins;
  2103. int ret;
  2104. struct extent_buffer *buf;
  2105. ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
  2106. root_objectid, ref_generation,
  2107. level, first_objectid, empty_size, hint,
  2108. (u64)-1, &ins, 0);
  2109. if (ret) {
  2110. BUG_ON(ret > 0);
  2111. return ERR_PTR(ret);
  2112. }
  2113. buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
  2114. return buf;
  2115. }
  2116. static int noinline drop_leaf_ref_no_cache(struct btrfs_trans_handle *trans,
  2117. struct btrfs_root *root,
  2118. struct extent_buffer *leaf)
  2119. {
  2120. u64 leaf_owner;
  2121. u64 leaf_generation;
  2122. struct btrfs_key key;
  2123. struct btrfs_file_extent_item *fi;
  2124. int i;
  2125. int nritems;
  2126. int ret;
  2127. BUG_ON(!btrfs_is_leaf(leaf));
  2128. nritems = btrfs_header_nritems(leaf);
  2129. leaf_owner = btrfs_header_owner(leaf);
  2130. leaf_generation = btrfs_header_generation(leaf);
  2131. for (i = 0; i < nritems; i++) {
  2132. u64 disk_bytenr;
  2133. cond_resched();
  2134. btrfs_item_key_to_cpu(leaf, &key, i);
  2135. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2136. continue;
  2137. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  2138. if (btrfs_file_extent_type(leaf, fi) ==
  2139. BTRFS_FILE_EXTENT_INLINE)
  2140. continue;
  2141. /*
  2142. * FIXME make sure to insert a trans record that
  2143. * repeats the snapshot del on crash
  2144. */
  2145. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  2146. if (disk_bytenr == 0)
  2147. continue;
  2148. mutex_lock(&root->fs_info->alloc_mutex);
  2149. ret = __btrfs_free_extent(trans, root, disk_bytenr,
  2150. btrfs_file_extent_disk_num_bytes(leaf, fi),
  2151. leaf_owner, leaf_generation,
  2152. key.objectid, key.offset, 0);
  2153. mutex_unlock(&root->fs_info->alloc_mutex);
  2154. atomic_inc(&root->fs_info->throttle_gen);
  2155. wake_up(&root->fs_info->transaction_throttle);
  2156. cond_resched();
  2157. BUG_ON(ret);
  2158. }
  2159. return 0;
  2160. }
  2161. static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
  2162. struct btrfs_root *root,
  2163. struct btrfs_leaf_ref *ref)
  2164. {
  2165. int i;
  2166. int ret;
  2167. struct btrfs_extent_info *info = ref->extents;
  2168. for (i = 0; i < ref->nritems; i++) {
  2169. mutex_lock(&root->fs_info->alloc_mutex);
  2170. ret = __btrfs_free_extent(trans, root,
  2171. info->bytenr, info->num_bytes,
  2172. ref->owner, ref->generation,
  2173. info->objectid, info->offset, 0);
  2174. mutex_unlock(&root->fs_info->alloc_mutex);
  2175. atomic_inc(&root->fs_info->throttle_gen);
  2176. wake_up(&root->fs_info->transaction_throttle);
  2177. cond_resched();
  2178. BUG_ON(ret);
  2179. info++;
  2180. }
  2181. return 0;
  2182. }
  2183. int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
  2184. u32 *refs)
  2185. {
  2186. int ret;
  2187. ret = lookup_extent_ref(NULL, root, start, len, refs);
  2188. BUG_ON(ret);
  2189. #if 0 // some debugging code in case we see problems here
  2190. /* if the refs count is one, it won't get increased again. But
  2191. * if the ref count is > 1, someone may be decreasing it at
  2192. * the same time we are.
  2193. */
  2194. if (*refs != 1) {
  2195. struct extent_buffer *eb = NULL;
  2196. eb = btrfs_find_create_tree_block(root, start, len);
  2197. if (eb)
  2198. btrfs_tree_lock(eb);
  2199. mutex_lock(&root->fs_info->alloc_mutex);
  2200. ret = lookup_extent_ref(NULL, root, start, len, refs);
  2201. BUG_ON(ret);
  2202. mutex_unlock(&root->fs_info->alloc_mutex);
  2203. if (eb) {
  2204. btrfs_tree_unlock(eb);
  2205. free_extent_buffer(eb);
  2206. }
  2207. if (*refs == 1) {
  2208. printk("block %llu went down to one during drop_snap\n",
  2209. (unsigned long long)start);
  2210. }
  2211. }
  2212. #endif
  2213. cond_resched();
  2214. return ret;
  2215. }
  2216. /*
  2217. * helper function for drop_snapshot, this walks down the tree dropping ref
  2218. * counts as it goes.
  2219. */
  2220. static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
  2221. struct btrfs_root *root,
  2222. struct btrfs_path *path, int *level)
  2223. {
  2224. u64 root_owner;
  2225. u64 root_gen;
  2226. u64 bytenr;
  2227. u64 ptr_gen;
  2228. struct extent_buffer *next;
  2229. struct extent_buffer *cur;
  2230. struct extent_buffer *parent;
  2231. struct btrfs_leaf_ref *ref;
  2232. u32 blocksize;
  2233. int ret;
  2234. u32 refs;
  2235. WARN_ON(*level < 0);
  2236. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2237. ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
  2238. path->nodes[*level]->len, &refs);
  2239. BUG_ON(ret);
  2240. if (refs > 1)
  2241. goto out;
  2242. /*
  2243. * walk down to the last node level and free all the leaves
  2244. */
  2245. while(*level >= 0) {
  2246. WARN_ON(*level < 0);
  2247. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2248. cur = path->nodes[*level];
  2249. if (btrfs_header_level(cur) != *level)
  2250. WARN_ON(1);
  2251. if (path->slots[*level] >=
  2252. btrfs_header_nritems(cur))
  2253. break;
  2254. if (*level == 0) {
  2255. ret = drop_leaf_ref_no_cache(trans, root, cur);
  2256. BUG_ON(ret);
  2257. break;
  2258. }
  2259. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2260. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2261. blocksize = btrfs_level_size(root, *level - 1);
  2262. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  2263. BUG_ON(ret);
  2264. if (refs != 1) {
  2265. parent = path->nodes[*level];
  2266. root_owner = btrfs_header_owner(parent);
  2267. root_gen = btrfs_header_generation(parent);
  2268. path->slots[*level]++;
  2269. mutex_lock(&root->fs_info->alloc_mutex);
  2270. ret = __btrfs_free_extent(trans, root, bytenr,
  2271. blocksize, root_owner,
  2272. root_gen, 0, 0, 1);
  2273. BUG_ON(ret);
  2274. mutex_unlock(&root->fs_info->alloc_mutex);
  2275. atomic_inc(&root->fs_info->throttle_gen);
  2276. wake_up(&root->fs_info->transaction_throttle);
  2277. cond_resched();
  2278. continue;
  2279. }
  2280. /*
  2281. * at this point, we have a single ref, and since the
  2282. * only place referencing this extent is a dead root
  2283. * the reference count should never go higher.
  2284. * So, we don't need to check it again
  2285. */
  2286. if (*level == 1) {
  2287. struct btrfs_key key;
  2288. btrfs_node_key_to_cpu(cur, &key, path->slots[*level]);
  2289. ref = btrfs_lookup_leaf_ref(root, bytenr);
  2290. if (ref) {
  2291. ret = drop_leaf_ref(trans, root, ref);
  2292. BUG_ON(ret);
  2293. btrfs_remove_leaf_ref(root, ref);
  2294. btrfs_free_leaf_ref(root, ref);
  2295. *level = 0;
  2296. break;
  2297. }
  2298. if (printk_ratelimit())
  2299. printk("leaf ref miss for bytenr %llu\n",
  2300. (unsigned long long)bytenr);
  2301. }
  2302. next = btrfs_find_tree_block(root, bytenr, blocksize);
  2303. if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
  2304. free_extent_buffer(next);
  2305. next = read_tree_block(root, bytenr, blocksize,
  2306. ptr_gen);
  2307. cond_resched();
  2308. #if 0
  2309. /*
  2310. * this is a debugging check and can go away
  2311. * the ref should never go all the way down to 1
  2312. * at this point
  2313. */
  2314. ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
  2315. &refs);
  2316. BUG_ON(ret);
  2317. WARN_ON(refs != 1);
  2318. #endif
  2319. }
  2320. WARN_ON(*level <= 0);
  2321. if (path->nodes[*level-1])
  2322. free_extent_buffer(path->nodes[*level-1]);
  2323. path->nodes[*level-1] = next;
  2324. *level = btrfs_header_level(next);
  2325. path->slots[*level] = 0;
  2326. cond_resched();
  2327. }
  2328. out:
  2329. WARN_ON(*level < 0);
  2330. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2331. if (path->nodes[*level] == root->node) {
  2332. parent = path->nodes[*level];
  2333. bytenr = path->nodes[*level]->start;
  2334. } else {
  2335. parent = path->nodes[*level + 1];
  2336. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  2337. }
  2338. blocksize = btrfs_level_size(root, *level);
  2339. root_owner = btrfs_header_owner(parent);
  2340. root_gen = btrfs_header_generation(parent);
  2341. mutex_lock(&root->fs_info->alloc_mutex);
  2342. ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
  2343. root_owner, root_gen, 0, 0, 1);
  2344. free_extent_buffer(path->nodes[*level]);
  2345. path->nodes[*level] = NULL;
  2346. *level += 1;
  2347. BUG_ON(ret);
  2348. mutex_unlock(&root->fs_info->alloc_mutex);
  2349. cond_resched();
  2350. return 0;
  2351. }
  2352. /*
  2353. * helper for dropping snapshots. This walks back up the tree in the path
  2354. * to find the first node higher up where we haven't yet gone through
  2355. * all the slots
  2356. */
  2357. static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
  2358. struct btrfs_root *root,
  2359. struct btrfs_path *path, int *level)
  2360. {
  2361. u64 root_owner;
  2362. u64 root_gen;
  2363. struct btrfs_root_item *root_item = &root->root_item;
  2364. int i;
  2365. int slot;
  2366. int ret;
  2367. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2368. slot = path->slots[i];
  2369. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  2370. struct extent_buffer *node;
  2371. struct btrfs_disk_key disk_key;
  2372. node = path->nodes[i];
  2373. path->slots[i]++;
  2374. *level = i;
  2375. WARN_ON(*level == 0);
  2376. btrfs_node_key(node, &disk_key, path->slots[i]);
  2377. memcpy(&root_item->drop_progress,
  2378. &disk_key, sizeof(disk_key));
  2379. root_item->drop_level = i;
  2380. return 0;
  2381. } else {
  2382. if (path->nodes[*level] == root->node) {
  2383. root_owner = root->root_key.objectid;
  2384. root_gen =
  2385. btrfs_header_generation(path->nodes[*level]);
  2386. } else {
  2387. struct extent_buffer *node;
  2388. node = path->nodes[*level + 1];
  2389. root_owner = btrfs_header_owner(node);
  2390. root_gen = btrfs_header_generation(node);
  2391. }
  2392. ret = btrfs_free_extent(trans, root,
  2393. path->nodes[*level]->start,
  2394. path->nodes[*level]->len,
  2395. root_owner, root_gen, 0, 0, 1);
  2396. BUG_ON(ret);
  2397. free_extent_buffer(path->nodes[*level]);
  2398. path->nodes[*level] = NULL;
  2399. *level = i + 1;
  2400. }
  2401. }
  2402. return 1;
  2403. }
  2404. /*
  2405. * drop the reference count on the tree rooted at 'snap'. This traverses
  2406. * the tree freeing any blocks that have a ref count of zero after being
  2407. * decremented.
  2408. */
  2409. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  2410. *root)
  2411. {
  2412. int ret = 0;
  2413. int wret;
  2414. int level;
  2415. struct btrfs_path *path;
  2416. int i;
  2417. int orig_level;
  2418. struct btrfs_root_item *root_item = &root->root_item;
  2419. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  2420. path = btrfs_alloc_path();
  2421. BUG_ON(!path);
  2422. level = btrfs_header_level(root->node);
  2423. orig_level = level;
  2424. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  2425. path->nodes[level] = root->node;
  2426. extent_buffer_get(root->node);
  2427. path->slots[level] = 0;
  2428. } else {
  2429. struct btrfs_key key;
  2430. struct btrfs_disk_key found_key;
  2431. struct extent_buffer *node;
  2432. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  2433. level = root_item->drop_level;
  2434. path->lowest_level = level;
  2435. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2436. if (wret < 0) {
  2437. ret = wret;
  2438. goto out;
  2439. }
  2440. node = path->nodes[level];
  2441. btrfs_node_key(node, &found_key, path->slots[level]);
  2442. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  2443. sizeof(found_key)));
  2444. /*
  2445. * unlock our path, this is safe because only this
  2446. * function is allowed to delete this snapshot
  2447. */
  2448. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  2449. if (path->nodes[i] && path->locks[i]) {
  2450. path->locks[i] = 0;
  2451. btrfs_tree_unlock(path->nodes[i]);
  2452. }
  2453. }
  2454. }
  2455. while(1) {
  2456. wret = walk_down_tree(trans, root, path, &level);
  2457. if (wret > 0)
  2458. break;
  2459. if (wret < 0)
  2460. ret = wret;
  2461. wret = walk_up_tree(trans, root, path, &level);
  2462. if (wret > 0)
  2463. break;
  2464. if (wret < 0)
  2465. ret = wret;
  2466. if (trans->transaction->in_commit) {
  2467. ret = -EAGAIN;
  2468. break;
  2469. }
  2470. atomic_inc(&root->fs_info->throttle_gen);
  2471. wake_up(&root->fs_info->transaction_throttle);
  2472. }
  2473. for (i = 0; i <= orig_level; i++) {
  2474. if (path->nodes[i]) {
  2475. free_extent_buffer(path->nodes[i]);
  2476. path->nodes[i] = NULL;
  2477. }
  2478. }
  2479. out:
  2480. btrfs_free_path(path);
  2481. return ret;
  2482. }
  2483. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  2484. {
  2485. u64 start;
  2486. u64 end;
  2487. u64 ptr;
  2488. int ret;
  2489. mutex_lock(&info->alloc_mutex);
  2490. while(1) {
  2491. ret = find_first_extent_bit(&info->block_group_cache, 0,
  2492. &start, &end, (unsigned int)-1);
  2493. if (ret)
  2494. break;
  2495. ret = get_state_private(&info->block_group_cache, start, &ptr);
  2496. if (!ret)
  2497. kfree((void *)(unsigned long)ptr);
  2498. clear_extent_bits(&info->block_group_cache, start,
  2499. end, (unsigned int)-1, GFP_NOFS);
  2500. }
  2501. while(1) {
  2502. ret = find_first_extent_bit(&info->free_space_cache, 0,
  2503. &start, &end, EXTENT_DIRTY);
  2504. if (ret)
  2505. break;
  2506. clear_extent_dirty(&info->free_space_cache, start,
  2507. end, GFP_NOFS);
  2508. }
  2509. mutex_unlock(&info->alloc_mutex);
  2510. return 0;
  2511. }
  2512. static unsigned long calc_ra(unsigned long start, unsigned long last,
  2513. unsigned long nr)
  2514. {
  2515. return min(last, start + nr - 1);
  2516. }
  2517. static int noinline relocate_inode_pages(struct inode *inode, u64 start,
  2518. u64 len)
  2519. {
  2520. u64 page_start;
  2521. u64 page_end;
  2522. unsigned long last_index;
  2523. unsigned long i;
  2524. struct page *page;
  2525. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2526. struct file_ra_state *ra;
  2527. unsigned long total_read = 0;
  2528. unsigned long ra_pages;
  2529. struct btrfs_ordered_extent *ordered;
  2530. struct btrfs_trans_handle *trans;
  2531. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2532. mutex_lock(&inode->i_mutex);
  2533. i = start >> PAGE_CACHE_SHIFT;
  2534. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  2535. ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
  2536. file_ra_state_init(ra, inode->i_mapping);
  2537. for (; i <= last_index; i++) {
  2538. if (total_read % ra_pages == 0) {
  2539. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  2540. calc_ra(i, last_index, ra_pages));
  2541. }
  2542. total_read++;
  2543. again:
  2544. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  2545. goto truncate_racing;
  2546. page = grab_cache_page(inode->i_mapping, i);
  2547. if (!page) {
  2548. goto out_unlock;
  2549. }
  2550. if (!PageUptodate(page)) {
  2551. btrfs_readpage(NULL, page);
  2552. lock_page(page);
  2553. if (!PageUptodate(page)) {
  2554. unlock_page(page);
  2555. page_cache_release(page);
  2556. goto out_unlock;
  2557. }
  2558. }
  2559. wait_on_page_writeback(page);
  2560. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2561. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2562. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2563. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2564. if (ordered) {
  2565. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2566. unlock_page(page);
  2567. page_cache_release(page);
  2568. btrfs_start_ordered_extent(inode, ordered, 1);
  2569. btrfs_put_ordered_extent(ordered);
  2570. goto again;
  2571. }
  2572. set_page_extent_mapped(page);
  2573. /*
  2574. * make sure page_mkwrite is called for this page if userland
  2575. * wants to change it from mmap
  2576. */
  2577. clear_page_dirty_for_io(page);
  2578. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2579. set_page_dirty(page);
  2580. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2581. unlock_page(page);
  2582. page_cache_release(page);
  2583. }
  2584. out_unlock:
  2585. /* we have to start the IO in order to get the ordered extents
  2586. * instantiated. This allows the relocation to code to wait
  2587. * for all the ordered extents to hit the disk.
  2588. *
  2589. * Otherwise, it would constantly loop over the same extents
  2590. * because the old ones don't get deleted until the IO is
  2591. * started
  2592. */
  2593. btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
  2594. WB_SYNC_NONE);
  2595. kfree(ra);
  2596. trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
  2597. if (trans) {
  2598. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  2599. mark_inode_dirty(inode);
  2600. }
  2601. mutex_unlock(&inode->i_mutex);
  2602. return 0;
  2603. truncate_racing:
  2604. vmtruncate(inode, inode->i_size);
  2605. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  2606. total_read);
  2607. goto out_unlock;
  2608. }
  2609. /*
  2610. * The back references tell us which tree holds a ref on a block,
  2611. * but it is possible for the tree root field in the reference to
  2612. * reflect the original root before a snapshot was made. In this
  2613. * case we should search through all the children of a given root
  2614. * to find potential holders of references on a block.
  2615. *
  2616. * Instead, we do something a little less fancy and just search
  2617. * all the roots for a given key/block combination.
  2618. */
  2619. static int find_root_for_ref(struct btrfs_root *root,
  2620. struct btrfs_path *path,
  2621. struct btrfs_key *key0,
  2622. int level,
  2623. int file_key,
  2624. struct btrfs_root **found_root,
  2625. u64 bytenr)
  2626. {
  2627. struct btrfs_key root_location;
  2628. struct btrfs_root *cur_root = *found_root;
  2629. struct btrfs_file_extent_item *file_extent;
  2630. u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
  2631. u64 found_bytenr;
  2632. int ret;
  2633. root_location.offset = (u64)-1;
  2634. root_location.type = BTRFS_ROOT_ITEM_KEY;
  2635. path->lowest_level = level;
  2636. path->reada = 0;
  2637. while(1) {
  2638. ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
  2639. found_bytenr = 0;
  2640. if (ret == 0 && file_key) {
  2641. struct extent_buffer *leaf = path->nodes[0];
  2642. file_extent = btrfs_item_ptr(leaf, path->slots[0],
  2643. struct btrfs_file_extent_item);
  2644. if (btrfs_file_extent_type(leaf, file_extent) ==
  2645. BTRFS_FILE_EXTENT_REG) {
  2646. found_bytenr =
  2647. btrfs_file_extent_disk_bytenr(leaf,
  2648. file_extent);
  2649. }
  2650. } else if (!file_key) {
  2651. if (path->nodes[level])
  2652. found_bytenr = path->nodes[level]->start;
  2653. }
  2654. btrfs_release_path(cur_root, path);
  2655. if (found_bytenr == bytenr) {
  2656. *found_root = cur_root;
  2657. ret = 0;
  2658. goto out;
  2659. }
  2660. ret = btrfs_search_root(root->fs_info->tree_root,
  2661. root_search_start, &root_search_start);
  2662. if (ret)
  2663. break;
  2664. root_location.objectid = root_search_start;
  2665. cur_root = btrfs_read_fs_root_no_name(root->fs_info,
  2666. &root_location);
  2667. if (!cur_root) {
  2668. ret = 1;
  2669. break;
  2670. }
  2671. }
  2672. out:
  2673. path->lowest_level = 0;
  2674. return ret;
  2675. }
  2676. /*
  2677. * note, this releases the path
  2678. */
  2679. static int noinline relocate_one_reference(struct btrfs_root *extent_root,
  2680. struct btrfs_path *path,
  2681. struct btrfs_key *extent_key,
  2682. u64 *last_file_objectid,
  2683. u64 *last_file_offset,
  2684. u64 *last_file_root,
  2685. u64 last_extent)
  2686. {
  2687. struct inode *inode;
  2688. struct btrfs_root *found_root;
  2689. struct btrfs_key root_location;
  2690. struct btrfs_key found_key;
  2691. struct btrfs_extent_ref *ref;
  2692. u64 ref_root;
  2693. u64 ref_gen;
  2694. u64 ref_objectid;
  2695. u64 ref_offset;
  2696. int ret;
  2697. int level;
  2698. WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
  2699. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2700. struct btrfs_extent_ref);
  2701. ref_root = btrfs_ref_root(path->nodes[0], ref);
  2702. ref_gen = btrfs_ref_generation(path->nodes[0], ref);
  2703. ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
  2704. ref_offset = btrfs_ref_offset(path->nodes[0], ref);
  2705. btrfs_release_path(extent_root, path);
  2706. root_location.objectid = ref_root;
  2707. if (ref_gen == 0)
  2708. root_location.offset = 0;
  2709. else
  2710. root_location.offset = (u64)-1;
  2711. root_location.type = BTRFS_ROOT_ITEM_KEY;
  2712. found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
  2713. &root_location);
  2714. BUG_ON(!found_root);
  2715. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  2716. if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  2717. found_key.objectid = ref_objectid;
  2718. found_key.type = BTRFS_EXTENT_DATA_KEY;
  2719. found_key.offset = ref_offset;
  2720. level = 0;
  2721. if (last_extent == extent_key->objectid &&
  2722. *last_file_objectid == ref_objectid &&
  2723. *last_file_offset == ref_offset &&
  2724. *last_file_root == ref_root)
  2725. goto out;
  2726. ret = find_root_for_ref(extent_root, path, &found_key,
  2727. level, 1, &found_root,
  2728. extent_key->objectid);
  2729. if (ret)
  2730. goto out;
  2731. if (last_extent == extent_key->objectid &&
  2732. *last_file_objectid == ref_objectid &&
  2733. *last_file_offset == ref_offset &&
  2734. *last_file_root == ref_root)
  2735. goto out;
  2736. inode = btrfs_iget_locked(extent_root->fs_info->sb,
  2737. ref_objectid, found_root);
  2738. if (inode->i_state & I_NEW) {
  2739. /* the inode and parent dir are two different roots */
  2740. BTRFS_I(inode)->root = found_root;
  2741. BTRFS_I(inode)->location.objectid = ref_objectid;
  2742. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  2743. BTRFS_I(inode)->location.offset = 0;
  2744. btrfs_read_locked_inode(inode);
  2745. unlock_new_inode(inode);
  2746. }
  2747. /* this can happen if the reference is not against
  2748. * the latest version of the tree root
  2749. */
  2750. if (is_bad_inode(inode))
  2751. goto out;
  2752. *last_file_objectid = inode->i_ino;
  2753. *last_file_root = found_root->root_key.objectid;
  2754. *last_file_offset = ref_offset;
  2755. relocate_inode_pages(inode, ref_offset, extent_key->offset);
  2756. iput(inode);
  2757. } else {
  2758. struct btrfs_trans_handle *trans;
  2759. struct extent_buffer *eb;
  2760. int needs_lock = 0;
  2761. eb = read_tree_block(found_root, extent_key->objectid,
  2762. extent_key->offset, 0);
  2763. btrfs_tree_lock(eb);
  2764. level = btrfs_header_level(eb);
  2765. if (level == 0)
  2766. btrfs_item_key_to_cpu(eb, &found_key, 0);
  2767. else
  2768. btrfs_node_key_to_cpu(eb, &found_key, 0);
  2769. btrfs_tree_unlock(eb);
  2770. free_extent_buffer(eb);
  2771. ret = find_root_for_ref(extent_root, path, &found_key,
  2772. level, 0, &found_root,
  2773. extent_key->objectid);
  2774. if (ret)
  2775. goto out;
  2776. /*
  2777. * right here almost anything could happen to our key,
  2778. * but that's ok. The cow below will either relocate it
  2779. * or someone else will have relocated it. Either way,
  2780. * it is in a different spot than it was before and
  2781. * we're happy.
  2782. */
  2783. trans = btrfs_start_transaction(found_root, 1);
  2784. if (found_root == extent_root->fs_info->extent_root ||
  2785. found_root == extent_root->fs_info->chunk_root ||
  2786. found_root == extent_root->fs_info->dev_root) {
  2787. needs_lock = 1;
  2788. mutex_lock(&extent_root->fs_info->alloc_mutex);
  2789. }
  2790. path->lowest_level = level;
  2791. path->reada = 2;
  2792. ret = btrfs_search_slot(trans, found_root, &found_key, path,
  2793. 0, 1);
  2794. path->lowest_level = 0;
  2795. btrfs_release_path(found_root, path);
  2796. if (found_root == found_root->fs_info->extent_root)
  2797. btrfs_extent_post_op(trans, found_root);
  2798. if (needs_lock)
  2799. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  2800. btrfs_end_transaction(trans, found_root);
  2801. }
  2802. out:
  2803. mutex_lock(&extent_root->fs_info->alloc_mutex);
  2804. return 0;
  2805. }
  2806. static int noinline del_extent_zero(struct btrfs_root *extent_root,
  2807. struct btrfs_path *path,
  2808. struct btrfs_key *extent_key)
  2809. {
  2810. int ret;
  2811. struct btrfs_trans_handle *trans;
  2812. trans = btrfs_start_transaction(extent_root, 1);
  2813. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  2814. if (ret > 0) {
  2815. ret = -EIO;
  2816. goto out;
  2817. }
  2818. if (ret < 0)
  2819. goto out;
  2820. ret = btrfs_del_item(trans, extent_root, path);
  2821. out:
  2822. btrfs_end_transaction(trans, extent_root);
  2823. return ret;
  2824. }
  2825. static int noinline relocate_one_extent(struct btrfs_root *extent_root,
  2826. struct btrfs_path *path,
  2827. struct btrfs_key *extent_key)
  2828. {
  2829. struct btrfs_key key;
  2830. struct btrfs_key found_key;
  2831. struct extent_buffer *leaf;
  2832. u64 last_file_objectid = 0;
  2833. u64 last_file_root = 0;
  2834. u64 last_file_offset = (u64)-1;
  2835. u64 last_extent = 0;
  2836. u32 nritems;
  2837. u32 item_size;
  2838. int ret = 0;
  2839. if (extent_key->objectid == 0) {
  2840. ret = del_extent_zero(extent_root, path, extent_key);
  2841. goto out;
  2842. }
  2843. key.objectid = extent_key->objectid;
  2844. key.type = BTRFS_EXTENT_REF_KEY;
  2845. key.offset = 0;
  2846. while(1) {
  2847. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2848. if (ret < 0)
  2849. goto out;
  2850. ret = 0;
  2851. leaf = path->nodes[0];
  2852. nritems = btrfs_header_nritems(leaf);
  2853. if (path->slots[0] == nritems) {
  2854. ret = btrfs_next_leaf(extent_root, path);
  2855. if (ret > 0) {
  2856. ret = 0;
  2857. goto out;
  2858. }
  2859. if (ret < 0)
  2860. goto out;
  2861. leaf = path->nodes[0];
  2862. }
  2863. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2864. if (found_key.objectid != extent_key->objectid) {
  2865. break;
  2866. }
  2867. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  2868. break;
  2869. }
  2870. key.offset = found_key.offset + 1;
  2871. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2872. ret = relocate_one_reference(extent_root, path, extent_key,
  2873. &last_file_objectid,
  2874. &last_file_offset,
  2875. &last_file_root, last_extent);
  2876. if (ret)
  2877. goto out;
  2878. last_extent = extent_key->objectid;
  2879. }
  2880. ret = 0;
  2881. out:
  2882. btrfs_release_path(extent_root, path);
  2883. return ret;
  2884. }
  2885. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  2886. {
  2887. u64 num_devices;
  2888. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  2889. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  2890. num_devices = root->fs_info->fs_devices->num_devices;
  2891. if (num_devices == 1) {
  2892. stripped |= BTRFS_BLOCK_GROUP_DUP;
  2893. stripped = flags & ~stripped;
  2894. /* turn raid0 into single device chunks */
  2895. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  2896. return stripped;
  2897. /* turn mirroring into duplication */
  2898. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2899. BTRFS_BLOCK_GROUP_RAID10))
  2900. return stripped | BTRFS_BLOCK_GROUP_DUP;
  2901. return flags;
  2902. } else {
  2903. /* they already had raid on here, just return */
  2904. if (flags & stripped)
  2905. return flags;
  2906. stripped |= BTRFS_BLOCK_GROUP_DUP;
  2907. stripped = flags & ~stripped;
  2908. /* switch duplicated blocks with raid1 */
  2909. if (flags & BTRFS_BLOCK_GROUP_DUP)
  2910. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  2911. /* turn single device chunks into raid0 */
  2912. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  2913. }
  2914. return flags;
  2915. }
  2916. int __alloc_chunk_for_shrink(struct btrfs_root *root,
  2917. struct btrfs_block_group_cache *shrink_block_group,
  2918. int force)
  2919. {
  2920. struct btrfs_trans_handle *trans;
  2921. u64 new_alloc_flags;
  2922. u64 calc;
  2923. spin_lock(&shrink_block_group->lock);
  2924. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  2925. spin_unlock(&shrink_block_group->lock);
  2926. mutex_unlock(&root->fs_info->alloc_mutex);
  2927. trans = btrfs_start_transaction(root, 1);
  2928. mutex_lock(&root->fs_info->alloc_mutex);
  2929. spin_lock(&shrink_block_group->lock);
  2930. new_alloc_flags = update_block_group_flags(root,
  2931. shrink_block_group->flags);
  2932. if (new_alloc_flags != shrink_block_group->flags) {
  2933. calc =
  2934. btrfs_block_group_used(&shrink_block_group->item);
  2935. } else {
  2936. calc = shrink_block_group->key.offset;
  2937. }
  2938. spin_unlock(&shrink_block_group->lock);
  2939. do_chunk_alloc(trans, root->fs_info->extent_root,
  2940. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  2941. mutex_unlock(&root->fs_info->alloc_mutex);
  2942. btrfs_end_transaction(trans, root);
  2943. mutex_lock(&root->fs_info->alloc_mutex);
  2944. } else
  2945. spin_unlock(&shrink_block_group->lock);
  2946. return 0;
  2947. }
  2948. int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
  2949. {
  2950. struct btrfs_trans_handle *trans;
  2951. struct btrfs_root *tree_root = root->fs_info->tree_root;
  2952. struct btrfs_path *path;
  2953. u64 cur_byte;
  2954. u64 total_found;
  2955. u64 shrink_last_byte;
  2956. struct btrfs_block_group_cache *shrink_block_group;
  2957. struct btrfs_fs_info *info = root->fs_info;
  2958. struct btrfs_key key;
  2959. struct btrfs_key found_key;
  2960. struct extent_buffer *leaf;
  2961. u32 nritems;
  2962. int ret;
  2963. int progress;
  2964. mutex_lock(&root->fs_info->alloc_mutex);
  2965. shrink_block_group = btrfs_lookup_block_group(root->fs_info,
  2966. shrink_start);
  2967. BUG_ON(!shrink_block_group);
  2968. shrink_last_byte = shrink_block_group->key.objectid +
  2969. shrink_block_group->key.offset;
  2970. shrink_block_group->space_info->total_bytes -=
  2971. shrink_block_group->key.offset;
  2972. path = btrfs_alloc_path();
  2973. root = root->fs_info->extent_root;
  2974. path->reada = 2;
  2975. printk("btrfs relocating block group %llu flags %llu\n",
  2976. (unsigned long long)shrink_start,
  2977. (unsigned long long)shrink_block_group->flags);
  2978. __alloc_chunk_for_shrink(root, shrink_block_group, 1);
  2979. again:
  2980. shrink_block_group->ro = 1;
  2981. total_found = 0;
  2982. progress = 0;
  2983. key.objectid = shrink_start;
  2984. key.offset = 0;
  2985. key.type = 0;
  2986. cur_byte = key.objectid;
  2987. mutex_unlock(&root->fs_info->alloc_mutex);
  2988. btrfs_start_delalloc_inodes(root);
  2989. btrfs_wait_ordered_extents(tree_root, 0);
  2990. mutex_lock(&root->fs_info->alloc_mutex);
  2991. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2992. if (ret < 0)
  2993. goto out;
  2994. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  2995. if (ret < 0)
  2996. goto out;
  2997. if (ret == 0) {
  2998. leaf = path->nodes[0];
  2999. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3000. if (found_key.objectid + found_key.offset > shrink_start &&
  3001. found_key.objectid < shrink_last_byte) {
  3002. cur_byte = found_key.objectid;
  3003. key.objectid = cur_byte;
  3004. }
  3005. }
  3006. btrfs_release_path(root, path);
  3007. while(1) {
  3008. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3009. if (ret < 0)
  3010. goto out;
  3011. next:
  3012. leaf = path->nodes[0];
  3013. nritems = btrfs_header_nritems(leaf);
  3014. if (path->slots[0] >= nritems) {
  3015. ret = btrfs_next_leaf(root, path);
  3016. if (ret < 0)
  3017. goto out;
  3018. if (ret == 1) {
  3019. ret = 0;
  3020. break;
  3021. }
  3022. leaf = path->nodes[0];
  3023. nritems = btrfs_header_nritems(leaf);
  3024. }
  3025. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3026. if (found_key.objectid >= shrink_last_byte)
  3027. break;
  3028. if (progress && need_resched()) {
  3029. memcpy(&key, &found_key, sizeof(key));
  3030. cond_resched();
  3031. btrfs_release_path(root, path);
  3032. btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3033. progress = 0;
  3034. goto next;
  3035. }
  3036. progress = 1;
  3037. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
  3038. found_key.objectid + found_key.offset <= cur_byte) {
  3039. memcpy(&key, &found_key, sizeof(key));
  3040. key.offset++;
  3041. path->slots[0]++;
  3042. goto next;
  3043. }
  3044. total_found++;
  3045. cur_byte = found_key.objectid + found_key.offset;
  3046. key.objectid = cur_byte;
  3047. btrfs_release_path(root, path);
  3048. ret = relocate_one_extent(root, path, &found_key);
  3049. __alloc_chunk_for_shrink(root, shrink_block_group, 0);
  3050. }
  3051. btrfs_release_path(root, path);
  3052. if (total_found > 0) {
  3053. printk("btrfs relocate found %llu last extent was %llu\n",
  3054. (unsigned long long)total_found,
  3055. (unsigned long long)found_key.objectid);
  3056. mutex_unlock(&root->fs_info->alloc_mutex);
  3057. trans = btrfs_start_transaction(tree_root, 1);
  3058. btrfs_commit_transaction(trans, tree_root);
  3059. btrfs_clean_old_snapshots(tree_root);
  3060. btrfs_start_delalloc_inodes(root);
  3061. btrfs_wait_ordered_extents(tree_root, 0);
  3062. trans = btrfs_start_transaction(tree_root, 1);
  3063. btrfs_commit_transaction(trans, tree_root);
  3064. mutex_lock(&root->fs_info->alloc_mutex);
  3065. goto again;
  3066. }
  3067. /*
  3068. * we've freed all the extents, now remove the block
  3069. * group item from the tree
  3070. */
  3071. mutex_unlock(&root->fs_info->alloc_mutex);
  3072. trans = btrfs_start_transaction(root, 1);
  3073. mutex_lock(&root->fs_info->alloc_mutex);
  3074. memcpy(&key, &shrink_block_group->key, sizeof(key));
  3075. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3076. if (ret > 0)
  3077. ret = -EIO;
  3078. if (ret < 0) {
  3079. btrfs_end_transaction(trans, root);
  3080. goto out;
  3081. }
  3082. clear_extent_bits(&info->block_group_cache, key.objectid,
  3083. key.objectid + key.offset - 1,
  3084. (unsigned int)-1, GFP_NOFS);
  3085. clear_extent_bits(&info->free_space_cache,
  3086. key.objectid, key.objectid + key.offset - 1,
  3087. (unsigned int)-1, GFP_NOFS);
  3088. /*
  3089. memset(shrink_block_group, 0, sizeof(*shrink_block_group));
  3090. kfree(shrink_block_group);
  3091. */
  3092. btrfs_del_item(trans, root, path);
  3093. btrfs_release_path(root, path);
  3094. mutex_unlock(&root->fs_info->alloc_mutex);
  3095. btrfs_commit_transaction(trans, root);
  3096. mutex_lock(&root->fs_info->alloc_mutex);
  3097. /* the code to unpin extents might set a few bits in the free
  3098. * space cache for this range again
  3099. */
  3100. clear_extent_bits(&info->free_space_cache,
  3101. key.objectid, key.objectid + key.offset - 1,
  3102. (unsigned int)-1, GFP_NOFS);
  3103. out:
  3104. btrfs_free_path(path);
  3105. mutex_unlock(&root->fs_info->alloc_mutex);
  3106. return ret;
  3107. }
  3108. int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
  3109. struct btrfs_key *key)
  3110. {
  3111. int ret = 0;
  3112. struct btrfs_key found_key;
  3113. struct extent_buffer *leaf;
  3114. int slot;
  3115. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  3116. if (ret < 0)
  3117. goto out;
  3118. while(1) {
  3119. slot = path->slots[0];
  3120. leaf = path->nodes[0];
  3121. if (slot >= btrfs_header_nritems(leaf)) {
  3122. ret = btrfs_next_leaf(root, path);
  3123. if (ret == 0)
  3124. continue;
  3125. if (ret < 0)
  3126. goto out;
  3127. break;
  3128. }
  3129. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3130. if (found_key.objectid >= key->objectid &&
  3131. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  3132. ret = 0;
  3133. goto out;
  3134. }
  3135. path->slots[0]++;
  3136. }
  3137. ret = -ENOENT;
  3138. out:
  3139. return ret;
  3140. }
  3141. int btrfs_read_block_groups(struct btrfs_root *root)
  3142. {
  3143. struct btrfs_path *path;
  3144. int ret;
  3145. int bit;
  3146. struct btrfs_block_group_cache *cache;
  3147. struct btrfs_fs_info *info = root->fs_info;
  3148. struct btrfs_space_info *space_info;
  3149. struct extent_io_tree *block_group_cache;
  3150. struct btrfs_key key;
  3151. struct btrfs_key found_key;
  3152. struct extent_buffer *leaf;
  3153. block_group_cache = &info->block_group_cache;
  3154. root = info->extent_root;
  3155. key.objectid = 0;
  3156. key.offset = 0;
  3157. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  3158. path = btrfs_alloc_path();
  3159. if (!path)
  3160. return -ENOMEM;
  3161. mutex_lock(&root->fs_info->alloc_mutex);
  3162. while(1) {
  3163. ret = find_first_block_group(root, path, &key);
  3164. if (ret > 0) {
  3165. ret = 0;
  3166. goto error;
  3167. }
  3168. if (ret != 0)
  3169. goto error;
  3170. leaf = path->nodes[0];
  3171. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3172. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  3173. if (!cache) {
  3174. ret = -ENOMEM;
  3175. break;
  3176. }
  3177. spin_lock_init(&cache->lock);
  3178. read_extent_buffer(leaf, &cache->item,
  3179. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3180. sizeof(cache->item));
  3181. memcpy(&cache->key, &found_key, sizeof(found_key));
  3182. key.objectid = found_key.objectid + found_key.offset;
  3183. btrfs_release_path(root, path);
  3184. cache->flags = btrfs_block_group_flags(&cache->item);
  3185. bit = 0;
  3186. if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
  3187. bit = BLOCK_GROUP_DATA;
  3188. } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
  3189. bit = BLOCK_GROUP_SYSTEM;
  3190. } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
  3191. bit = BLOCK_GROUP_METADATA;
  3192. }
  3193. set_avail_alloc_bits(info, cache->flags);
  3194. ret = update_space_info(info, cache->flags, found_key.offset,
  3195. btrfs_block_group_used(&cache->item),
  3196. &space_info);
  3197. BUG_ON(ret);
  3198. cache->space_info = space_info;
  3199. /* use EXTENT_LOCKED to prevent merging */
  3200. set_extent_bits(block_group_cache, found_key.objectid,
  3201. found_key.objectid + found_key.offset - 1,
  3202. EXTENT_LOCKED, GFP_NOFS);
  3203. set_state_private(block_group_cache, found_key.objectid,
  3204. (unsigned long)cache);
  3205. set_extent_bits(block_group_cache, found_key.objectid,
  3206. found_key.objectid + found_key.offset - 1,
  3207. bit | EXTENT_LOCKED, GFP_NOFS);
  3208. if (key.objectid >=
  3209. btrfs_super_total_bytes(&info->super_copy))
  3210. break;
  3211. }
  3212. ret = 0;
  3213. error:
  3214. btrfs_free_path(path);
  3215. mutex_unlock(&root->fs_info->alloc_mutex);
  3216. return ret;
  3217. }
  3218. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  3219. struct btrfs_root *root, u64 bytes_used,
  3220. u64 type, u64 chunk_objectid, u64 chunk_offset,
  3221. u64 size)
  3222. {
  3223. int ret;
  3224. int bit = 0;
  3225. struct btrfs_root *extent_root;
  3226. struct btrfs_block_group_cache *cache;
  3227. struct extent_io_tree *block_group_cache;
  3228. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  3229. extent_root = root->fs_info->extent_root;
  3230. block_group_cache = &root->fs_info->block_group_cache;
  3231. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  3232. BUG_ON(!cache);
  3233. cache->key.objectid = chunk_offset;
  3234. cache->key.offset = size;
  3235. spin_lock_init(&cache->lock);
  3236. btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  3237. btrfs_set_block_group_used(&cache->item, bytes_used);
  3238. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  3239. cache->flags = type;
  3240. btrfs_set_block_group_flags(&cache->item, type);
  3241. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  3242. &cache->space_info);
  3243. BUG_ON(ret);
  3244. bit = block_group_state_bits(type);
  3245. set_extent_bits(block_group_cache, chunk_offset,
  3246. chunk_offset + size - 1,
  3247. EXTENT_LOCKED, GFP_NOFS);
  3248. set_state_private(block_group_cache, chunk_offset,
  3249. (unsigned long)cache);
  3250. set_extent_bits(block_group_cache, chunk_offset,
  3251. chunk_offset + size - 1,
  3252. bit | EXTENT_LOCKED, GFP_NOFS);
  3253. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  3254. sizeof(cache->item));
  3255. BUG_ON(ret);
  3256. finish_current_insert(trans, extent_root);
  3257. ret = del_pending_extents(trans, extent_root);
  3258. BUG_ON(ret);
  3259. set_avail_alloc_bits(extent_root->fs_info, type);
  3260. return 0;
  3261. }