extent-tree.c 88 KB

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