extent-tree.c 89 KB

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