extent-tree.c 87 KB

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