extent-tree.c 105 KB

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