extent-tree.c 98 KB

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