extent-tree.c 70 KB

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