extent-tree.c 83 KB

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