extent-tree.c 70 KB

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