extent-tree.c 90 KB

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