extent-tree.c 72 KB

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