extent-tree.c 97 KB

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