extent-tree.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764
  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. struct btrfs_extent_item *ei;
  1163. u32 refs;
  1164. key.objectid = bytenr;
  1165. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1166. key.offset = num_bytes;
  1167. path = btrfs_alloc_path();
  1168. if (!path)
  1169. return -ENOMEM;
  1170. path->reada = 0;
  1171. ret = lookup_extent_backref(trans, extent_root, path,
  1172. bytenr, root_objectid,
  1173. ref_generation,
  1174. owner_objectid, owner_offset, 1);
  1175. if (ret == 0) {
  1176. ret = btrfs_del_item(trans, extent_root, path);
  1177. } else {
  1178. btrfs_print_leaf(extent_root, path->nodes[0]);
  1179. WARN_ON(1);
  1180. printk("Unable to find ref byte nr %Lu root %Lu "
  1181. " gen %Lu owner %Lu offset %Lu\n", bytenr,
  1182. root_objectid, ref_generation, owner_objectid,
  1183. owner_offset);
  1184. }
  1185. btrfs_release_path(extent_root, path);
  1186. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  1187. if (ret < 0)
  1188. return ret;
  1189. BUG_ON(ret);
  1190. leaf = path->nodes[0];
  1191. ei = btrfs_item_ptr(leaf, path->slots[0],
  1192. struct btrfs_extent_item);
  1193. refs = btrfs_extent_refs(leaf, ei);
  1194. BUG_ON(refs == 0);
  1195. refs -= 1;
  1196. btrfs_set_extent_refs(leaf, ei, refs);
  1197. btrfs_mark_buffer_dirty(leaf);
  1198. if (refs == 0) {
  1199. u64 super_used;
  1200. u64 root_used;
  1201. if (pin) {
  1202. ret = pin_down_bytes(root, bytenr, num_bytes, 0);
  1203. if (ret > 0)
  1204. mark_free = 1;
  1205. BUG_ON(ret < 0);
  1206. }
  1207. /* block accounting for super block */
  1208. super_used = btrfs_super_bytes_used(&info->super_copy);
  1209. btrfs_set_super_bytes_used(&info->super_copy,
  1210. super_used - num_bytes);
  1211. /* block accounting for root item */
  1212. root_used = btrfs_root_used(&root->root_item);
  1213. btrfs_set_root_used(&root->root_item,
  1214. root_used - num_bytes);
  1215. ret = btrfs_del_item(trans, extent_root, path);
  1216. if (ret) {
  1217. return ret;
  1218. }
  1219. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1220. mark_free, 0);
  1221. BUG_ON(ret);
  1222. }
  1223. btrfs_free_path(path);
  1224. finish_current_insert(trans, extent_root);
  1225. return ret;
  1226. }
  1227. /*
  1228. * find all the blocks marked as pending in the radix tree and remove
  1229. * them from the extent map
  1230. */
  1231. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1232. btrfs_root *extent_root)
  1233. {
  1234. int ret;
  1235. int err = 0;
  1236. u64 start;
  1237. u64 end;
  1238. struct extent_io_tree *pending_del;
  1239. struct extent_io_tree *pinned_extents;
  1240. pending_del = &extent_root->fs_info->pending_del;
  1241. pinned_extents = &extent_root->fs_info->pinned_extents;
  1242. while(1) {
  1243. ret = find_first_extent_bit(pending_del, 0, &start, &end,
  1244. EXTENT_LOCKED);
  1245. if (ret)
  1246. break;
  1247. update_pinned_extents(extent_root, start, end + 1 - start, 1);
  1248. clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
  1249. GFP_NOFS);
  1250. ret = __free_extent(trans, extent_root,
  1251. start, end + 1 - start,
  1252. extent_root->root_key.objectid,
  1253. 0, 0, 0, 0, 0);
  1254. if (ret)
  1255. err = ret;
  1256. }
  1257. return err;
  1258. }
  1259. /*
  1260. * remove an extent from the root, returns 0 on success
  1261. */
  1262. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1263. *root, u64 bytenr, u64 num_bytes,
  1264. u64 root_objectid, u64 ref_generation,
  1265. u64 owner_objectid, u64 owner_offset, int pin)
  1266. {
  1267. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1268. int pending_ret;
  1269. int ret;
  1270. WARN_ON(num_bytes < root->sectorsize);
  1271. if (!root->ref_cows)
  1272. ref_generation = 0;
  1273. if (root == extent_root) {
  1274. pin_down_bytes(root, bytenr, num_bytes, 1);
  1275. return 0;
  1276. }
  1277. ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
  1278. ref_generation, owner_objectid, owner_offset,
  1279. pin, pin == 0);
  1280. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  1281. return ret ? ret : pending_ret;
  1282. }
  1283. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1284. {
  1285. u64 mask = ((u64)root->stripesize - 1);
  1286. u64 ret = (val + mask) & ~mask;
  1287. return ret;
  1288. }
  1289. /*
  1290. * walks the btree of allocated extents and find a hole of a given size.
  1291. * The key ins is changed to record the hole:
  1292. * ins->objectid == block start
  1293. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1294. * ins->offset == number of blocks
  1295. * Any available blocks before search_start are skipped.
  1296. */
  1297. static int noinline find_free_extent(struct btrfs_trans_handle *trans,
  1298. struct btrfs_root *orig_root,
  1299. u64 num_bytes, u64 empty_size,
  1300. u64 search_start, u64 search_end,
  1301. u64 hint_byte, struct btrfs_key *ins,
  1302. u64 exclude_start, u64 exclude_nr,
  1303. int data)
  1304. {
  1305. struct btrfs_path *path;
  1306. struct btrfs_key key;
  1307. u64 hole_size = 0;
  1308. u64 aligned;
  1309. int ret;
  1310. int slot = 0;
  1311. u64 last_byte = 0;
  1312. u64 *last_ptr = NULL;
  1313. u64 orig_search_start = search_start;
  1314. int start_found;
  1315. struct extent_buffer *l;
  1316. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1317. struct btrfs_fs_info *info = root->fs_info;
  1318. u64 total_needed = num_bytes;
  1319. int level;
  1320. struct btrfs_block_group_cache *block_group;
  1321. int full_scan = 0;
  1322. int wrapped = 0;
  1323. int empty_cluster;
  1324. u64 cached_start;
  1325. WARN_ON(num_bytes < root->sectorsize);
  1326. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1327. level = btrfs_header_level(root->node);
  1328. if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
  1329. data = BTRFS_BLOCK_GROUP_MIXED;
  1330. }
  1331. if (!data) {
  1332. last_ptr = &root->fs_info->last_alloc;
  1333. empty_cluster = 128 * 1024;
  1334. }
  1335. if (data && btrfs_test_opt(root, SSD)) {
  1336. last_ptr = &root->fs_info->last_data_alloc;
  1337. empty_cluster = 2 * 1024 * 1024;
  1338. }
  1339. if (last_ptr) {
  1340. if (*last_ptr)
  1341. hint_byte = *last_ptr;
  1342. else {
  1343. hint_byte = hint_byte &
  1344. ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
  1345. empty_size += empty_cluster;
  1346. }
  1347. search_start = max(search_start, hint_byte);
  1348. }
  1349. search_end = min(search_end,
  1350. btrfs_super_total_bytes(&info->super_copy));
  1351. if (hint_byte) {
  1352. block_group = btrfs_lookup_block_group(info, hint_byte);
  1353. if (!block_group)
  1354. hint_byte = search_start;
  1355. block_group = btrfs_find_block_group(root, block_group,
  1356. hint_byte, data, 1);
  1357. } else {
  1358. block_group = btrfs_find_block_group(root,
  1359. trans->block_group,
  1360. search_start, data, 1);
  1361. }
  1362. total_needed += empty_size;
  1363. path = btrfs_alloc_path();
  1364. check_failed:
  1365. if (!block_group) {
  1366. block_group = btrfs_lookup_block_group(info, search_start);
  1367. if (!block_group)
  1368. block_group = btrfs_lookup_block_group(info,
  1369. orig_search_start);
  1370. }
  1371. search_start = find_search_start(root, &block_group, search_start,
  1372. total_needed, data);
  1373. if (last_ptr && *last_ptr && search_start != *last_ptr) {
  1374. *last_ptr = 0;
  1375. if (!empty_size) {
  1376. empty_size += empty_cluster;
  1377. total_needed += empty_size;
  1378. }
  1379. search_start = find_search_start(root, &block_group,
  1380. search_start, total_needed,
  1381. data);
  1382. }
  1383. search_start = stripe_align(root, search_start);
  1384. cached_start = search_start;
  1385. btrfs_init_path(path);
  1386. ins->objectid = search_start;
  1387. ins->offset = 0;
  1388. start_found = 0;
  1389. path->reada = 2;
  1390. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  1391. if (ret < 0)
  1392. goto error;
  1393. ret = find_previous_extent(root, path);
  1394. if (ret < 0)
  1395. goto error;
  1396. l = path->nodes[0];
  1397. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1398. while (1) {
  1399. l = path->nodes[0];
  1400. slot = path->slots[0];
  1401. if (slot >= btrfs_header_nritems(l)) {
  1402. ret = btrfs_next_leaf(root, path);
  1403. if (ret == 0)
  1404. continue;
  1405. if (ret < 0)
  1406. goto error;
  1407. search_start = max(search_start,
  1408. block_group->key.objectid);
  1409. if (!start_found) {
  1410. aligned = stripe_align(root, search_start);
  1411. ins->objectid = aligned;
  1412. if (aligned >= search_end) {
  1413. ret = -ENOSPC;
  1414. goto error;
  1415. }
  1416. ins->offset = search_end - aligned;
  1417. start_found = 1;
  1418. goto check_pending;
  1419. }
  1420. ins->objectid = stripe_align(root,
  1421. last_byte > search_start ?
  1422. last_byte : search_start);
  1423. if (search_end <= ins->objectid) {
  1424. ret = -ENOSPC;
  1425. goto error;
  1426. }
  1427. ins->offset = search_end - ins->objectid;
  1428. BUG_ON(ins->objectid >= search_end);
  1429. goto check_pending;
  1430. }
  1431. btrfs_item_key_to_cpu(l, &key, slot);
  1432. if (key.objectid >= search_start && key.objectid > last_byte &&
  1433. start_found) {
  1434. if (last_byte < search_start)
  1435. last_byte = search_start;
  1436. aligned = stripe_align(root, last_byte);
  1437. hole_size = key.objectid - aligned;
  1438. if (key.objectid > aligned && hole_size >= num_bytes) {
  1439. ins->objectid = aligned;
  1440. ins->offset = hole_size;
  1441. goto check_pending;
  1442. }
  1443. }
  1444. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
  1445. if (!start_found && btrfs_key_type(&key) ==
  1446. BTRFS_BLOCK_GROUP_ITEM_KEY) {
  1447. last_byte = key.objectid;
  1448. start_found = 1;
  1449. }
  1450. goto next;
  1451. }
  1452. start_found = 1;
  1453. last_byte = key.objectid + key.offset;
  1454. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  1455. last_byte >= block_group->key.objectid +
  1456. block_group->key.offset) {
  1457. btrfs_release_path(root, path);
  1458. search_start = block_group->key.objectid +
  1459. block_group->key.offset;
  1460. goto new_group;
  1461. }
  1462. next:
  1463. path->slots[0]++;
  1464. cond_resched();
  1465. }
  1466. check_pending:
  1467. /* we have to make sure we didn't find an extent that has already
  1468. * been allocated by the map tree or the original allocation
  1469. */
  1470. btrfs_release_path(root, path);
  1471. BUG_ON(ins->objectid < search_start);
  1472. if (ins->objectid + num_bytes >= search_end)
  1473. goto enospc;
  1474. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  1475. ins->objectid + num_bytes > block_group->
  1476. key.objectid + block_group->key.offset) {
  1477. search_start = block_group->key.objectid +
  1478. block_group->key.offset;
  1479. goto new_group;
  1480. }
  1481. if (test_range_bit(&info->extent_ins, ins->objectid,
  1482. ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
  1483. search_start = ins->objectid + num_bytes;
  1484. goto new_group;
  1485. }
  1486. if (test_range_bit(&info->pinned_extents, ins->objectid,
  1487. ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
  1488. search_start = ins->objectid + num_bytes;
  1489. goto new_group;
  1490. }
  1491. if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
  1492. ins->objectid < exclude_start + exclude_nr)) {
  1493. search_start = exclude_start + exclude_nr;
  1494. goto new_group;
  1495. }
  1496. if (!data) {
  1497. block_group = btrfs_lookup_block_group(info, ins->objectid);
  1498. if (block_group)
  1499. trans->block_group = block_group;
  1500. }
  1501. ins->offset = num_bytes;
  1502. btrfs_free_path(path);
  1503. if (last_ptr) {
  1504. *last_ptr = ins->objectid + ins->offset;
  1505. if (*last_ptr ==
  1506. btrfs_super_total_bytes(&root->fs_info->super_copy)) {
  1507. *last_ptr = 0;
  1508. }
  1509. }
  1510. return 0;
  1511. new_group:
  1512. if (search_start + num_bytes >= search_end) {
  1513. enospc:
  1514. search_start = orig_search_start;
  1515. if (full_scan) {
  1516. ret = -ENOSPC;
  1517. goto error;
  1518. }
  1519. if (wrapped) {
  1520. if (!full_scan)
  1521. total_needed -= empty_size;
  1522. full_scan = 1;
  1523. data = BTRFS_BLOCK_GROUP_MIXED;
  1524. } else
  1525. wrapped = 1;
  1526. }
  1527. block_group = btrfs_lookup_block_group(info, search_start);
  1528. cond_resched();
  1529. block_group = btrfs_find_block_group(root, block_group,
  1530. search_start, data, 0);
  1531. goto check_failed;
  1532. error:
  1533. btrfs_release_path(root, path);
  1534. btrfs_free_path(path);
  1535. return ret;
  1536. }
  1537. /*
  1538. * finds a free extent and does all the dirty work required for allocation
  1539. * returns the key for the extent through ins, and a tree buffer for
  1540. * the first block of the extent through buf.
  1541. *
  1542. * returns 0 if everything worked, non-zero otherwise.
  1543. */
  1544. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  1545. struct btrfs_root *root,
  1546. u64 num_bytes, u64 root_objectid, u64 ref_generation,
  1547. u64 owner, u64 owner_offset,
  1548. u64 empty_size, u64 hint_byte,
  1549. u64 search_end, struct btrfs_key *ins, int data)
  1550. {
  1551. int ret;
  1552. int pending_ret;
  1553. u64 super_used;
  1554. u64 root_used;
  1555. u64 search_start = 0;
  1556. u64 new_hint;
  1557. u32 sizes[2];
  1558. struct btrfs_fs_info *info = root->fs_info;
  1559. struct btrfs_root *extent_root = info->extent_root;
  1560. struct btrfs_extent_item *extent_item;
  1561. struct btrfs_extent_ref *ref;
  1562. struct btrfs_path *path;
  1563. struct btrfs_key keys[2];
  1564. new_hint = max(hint_byte, root->fs_info->alloc_start);
  1565. if (new_hint < btrfs_super_total_bytes(&info->super_copy))
  1566. hint_byte = new_hint;
  1567. WARN_ON(num_bytes < root->sectorsize);
  1568. ret = find_free_extent(trans, root, num_bytes, empty_size,
  1569. search_start, search_end, hint_byte, ins,
  1570. trans->alloc_exclude_start,
  1571. trans->alloc_exclude_nr, data);
  1572. BUG_ON(ret);
  1573. if (ret)
  1574. return ret;
  1575. /* block accounting for super block */
  1576. super_used = btrfs_super_bytes_used(&info->super_copy);
  1577. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  1578. /* block accounting for root item */
  1579. root_used = btrfs_root_used(&root->root_item);
  1580. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  1581. clear_extent_dirty(&root->fs_info->free_space_cache,
  1582. ins->objectid, ins->objectid + ins->offset - 1,
  1583. GFP_NOFS);
  1584. if (root == extent_root) {
  1585. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  1586. ins->objectid + ins->offset - 1,
  1587. EXTENT_LOCKED, GFP_NOFS);
  1588. WARN_ON(data == 1);
  1589. goto update_block;
  1590. }
  1591. WARN_ON(trans->alloc_exclude_nr);
  1592. trans->alloc_exclude_start = ins->objectid;
  1593. trans->alloc_exclude_nr = ins->offset;
  1594. memcpy(&keys[0], ins, sizeof(*ins));
  1595. keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
  1596. owner, owner_offset);
  1597. keys[1].objectid = ins->objectid;
  1598. keys[1].type = BTRFS_EXTENT_REF_KEY;
  1599. sizes[0] = sizeof(*extent_item);
  1600. sizes[1] = sizeof(*ref);
  1601. path = btrfs_alloc_path();
  1602. BUG_ON(!path);
  1603. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  1604. sizes, 2);
  1605. BUG_ON(ret);
  1606. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1607. struct btrfs_extent_item);
  1608. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  1609. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  1610. struct btrfs_extent_ref);
  1611. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  1612. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  1613. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  1614. btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
  1615. btrfs_mark_buffer_dirty(path->nodes[0]);
  1616. trans->alloc_exclude_start = 0;
  1617. trans->alloc_exclude_nr = 0;
  1618. btrfs_free_path(path);
  1619. finish_current_insert(trans, extent_root);
  1620. pending_ret = del_pending_extents(trans, extent_root);
  1621. if (ret) {
  1622. return ret;
  1623. }
  1624. if (pending_ret) {
  1625. return pending_ret;
  1626. }
  1627. update_block:
  1628. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
  1629. data);
  1630. if (ret) {
  1631. printk("update block group failed for %Lu %Lu\n",
  1632. ins->objectid, ins->offset);
  1633. BUG();
  1634. }
  1635. return 0;
  1636. }
  1637. /*
  1638. * helper function to allocate a block for a given tree
  1639. * returns the tree buffer or NULL.
  1640. */
  1641. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1642. struct btrfs_root *root,
  1643. u32 blocksize,
  1644. u64 root_objectid, u64 hint,
  1645. u64 empty_size)
  1646. {
  1647. u64 ref_generation;
  1648. if (root->ref_cows)
  1649. ref_generation = trans->transid;
  1650. else
  1651. ref_generation = 0;
  1652. return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
  1653. ref_generation, 0, 0, hint, empty_size);
  1654. }
  1655. /*
  1656. * helper function to allocate a block for a given tree
  1657. * returns the tree buffer or NULL.
  1658. */
  1659. struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1660. struct btrfs_root *root,
  1661. u32 blocksize,
  1662. u64 root_objectid,
  1663. u64 ref_generation,
  1664. u64 first_objectid,
  1665. int level,
  1666. u64 hint,
  1667. u64 empty_size)
  1668. {
  1669. struct btrfs_key ins;
  1670. int ret;
  1671. struct extent_buffer *buf;
  1672. ret = btrfs_alloc_extent(trans, root, blocksize,
  1673. root_objectid, ref_generation,
  1674. level, first_objectid, empty_size, hint,
  1675. (u64)-1, &ins, 0);
  1676. if (ret) {
  1677. BUG_ON(ret > 0);
  1678. return ERR_PTR(ret);
  1679. }
  1680. buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
  1681. if (!buf) {
  1682. btrfs_free_extent(trans, root, ins.objectid, blocksize,
  1683. root->root_key.objectid, ref_generation,
  1684. 0, 0, 0);
  1685. return ERR_PTR(-ENOMEM);
  1686. }
  1687. btrfs_set_header_generation(buf, trans->transid);
  1688. clean_tree_block(trans, root, buf);
  1689. wait_on_tree_block_writeback(root, buf);
  1690. btrfs_set_buffer_uptodate(buf);
  1691. if (PageDirty(buf->first_page)) {
  1692. printk("page %lu dirty\n", buf->first_page->index);
  1693. WARN_ON(1);
  1694. }
  1695. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  1696. buf->start + buf->len - 1, GFP_NOFS);
  1697. set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->io_tree,
  1698. buf->start, buf->start + buf->len - 1,
  1699. EXTENT_CSUM, GFP_NOFS);
  1700. buf->flags |= EXTENT_CSUM;
  1701. if (!btrfs_test_opt(root, SSD))
  1702. btrfs_set_buffer_defrag(buf);
  1703. trans->blocks_used++;
  1704. return buf;
  1705. }
  1706. static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
  1707. struct btrfs_root *root,
  1708. struct extent_buffer *leaf)
  1709. {
  1710. u64 leaf_owner;
  1711. u64 leaf_generation;
  1712. struct btrfs_key key;
  1713. struct btrfs_file_extent_item *fi;
  1714. int i;
  1715. int nritems;
  1716. int ret;
  1717. BUG_ON(!btrfs_is_leaf(leaf));
  1718. nritems = btrfs_header_nritems(leaf);
  1719. leaf_owner = btrfs_header_owner(leaf);
  1720. leaf_generation = btrfs_header_generation(leaf);
  1721. for (i = 0; i < nritems; i++) {
  1722. u64 disk_bytenr;
  1723. btrfs_item_key_to_cpu(leaf, &key, i);
  1724. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1725. continue;
  1726. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  1727. if (btrfs_file_extent_type(leaf, fi) ==
  1728. BTRFS_FILE_EXTENT_INLINE)
  1729. continue;
  1730. /*
  1731. * FIXME make sure to insert a trans record that
  1732. * repeats the snapshot del on crash
  1733. */
  1734. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1735. if (disk_bytenr == 0)
  1736. continue;
  1737. ret = btrfs_free_extent(trans, root, disk_bytenr,
  1738. btrfs_file_extent_disk_num_bytes(leaf, fi),
  1739. leaf_owner, leaf_generation,
  1740. key.objectid, key.offset, 0);
  1741. BUG_ON(ret);
  1742. }
  1743. return 0;
  1744. }
  1745. static void noinline reada_walk_down(struct btrfs_root *root,
  1746. struct extent_buffer *node,
  1747. int slot)
  1748. {
  1749. u64 bytenr;
  1750. u64 last = 0;
  1751. u32 nritems;
  1752. u32 refs;
  1753. u32 blocksize;
  1754. int ret;
  1755. int i;
  1756. int level;
  1757. int skipped = 0;
  1758. nritems = btrfs_header_nritems(node);
  1759. level = btrfs_header_level(node);
  1760. if (level)
  1761. return;
  1762. for (i = slot; i < nritems && skipped < 32; i++) {
  1763. bytenr = btrfs_node_blockptr(node, i);
  1764. if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
  1765. (last > bytenr && last - bytenr > 32 * 1024))) {
  1766. skipped++;
  1767. continue;
  1768. }
  1769. blocksize = btrfs_level_size(root, level - 1);
  1770. if (i != slot) {
  1771. ret = lookup_extent_ref(NULL, root, bytenr,
  1772. blocksize, &refs);
  1773. BUG_ON(ret);
  1774. if (refs != 1) {
  1775. skipped++;
  1776. continue;
  1777. }
  1778. }
  1779. mutex_unlock(&root->fs_info->fs_mutex);
  1780. ret = readahead_tree_block(root, bytenr, blocksize);
  1781. last = bytenr + blocksize;
  1782. cond_resched();
  1783. mutex_lock(&root->fs_info->fs_mutex);
  1784. if (ret)
  1785. break;
  1786. }
  1787. }
  1788. /*
  1789. * helper function for drop_snapshot, this walks down the tree dropping ref
  1790. * counts as it goes.
  1791. */
  1792. static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
  1793. struct btrfs_root *root,
  1794. struct btrfs_path *path, int *level)
  1795. {
  1796. u64 root_owner;
  1797. u64 root_gen;
  1798. u64 bytenr;
  1799. struct extent_buffer *next;
  1800. struct extent_buffer *cur;
  1801. struct extent_buffer *parent;
  1802. u32 blocksize;
  1803. int ret;
  1804. u32 refs;
  1805. WARN_ON(*level < 0);
  1806. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1807. ret = lookup_extent_ref(trans, root,
  1808. path->nodes[*level]->start,
  1809. path->nodes[*level]->len, &refs);
  1810. BUG_ON(ret);
  1811. if (refs > 1)
  1812. goto out;
  1813. /*
  1814. * walk down to the last node level and free all the leaves
  1815. */
  1816. while(*level >= 0) {
  1817. WARN_ON(*level < 0);
  1818. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1819. cur = path->nodes[*level];
  1820. if (btrfs_header_level(cur) != *level)
  1821. WARN_ON(1);
  1822. if (path->slots[*level] >=
  1823. btrfs_header_nritems(cur))
  1824. break;
  1825. if (*level == 0) {
  1826. ret = drop_leaf_ref(trans, root, cur);
  1827. BUG_ON(ret);
  1828. break;
  1829. }
  1830. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1831. blocksize = btrfs_level_size(root, *level - 1);
  1832. ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
  1833. BUG_ON(ret);
  1834. if (refs != 1) {
  1835. parent = path->nodes[*level];
  1836. root_owner = btrfs_header_owner(parent);
  1837. root_gen = btrfs_header_generation(parent);
  1838. path->slots[*level]++;
  1839. ret = btrfs_free_extent(trans, root, bytenr,
  1840. blocksize, root_owner,
  1841. root_gen, 0, 0, 1);
  1842. BUG_ON(ret);
  1843. continue;
  1844. }
  1845. next = btrfs_find_tree_block(root, bytenr, blocksize);
  1846. if (!next || !btrfs_buffer_uptodate(next)) {
  1847. free_extent_buffer(next);
  1848. reada_walk_down(root, cur, path->slots[*level]);
  1849. mutex_unlock(&root->fs_info->fs_mutex);
  1850. next = read_tree_block(root, bytenr, blocksize);
  1851. mutex_lock(&root->fs_info->fs_mutex);
  1852. /* we dropped the lock, check one more time */
  1853. ret = lookup_extent_ref(trans, root, bytenr,
  1854. blocksize, &refs);
  1855. BUG_ON(ret);
  1856. if (refs != 1) {
  1857. parent = path->nodes[*level];
  1858. root_owner = btrfs_header_owner(parent);
  1859. root_gen = btrfs_header_generation(parent);
  1860. path->slots[*level]++;
  1861. free_extent_buffer(next);
  1862. ret = btrfs_free_extent(trans, root, bytenr,
  1863. blocksize,
  1864. root_owner,
  1865. root_gen, 0, 0, 1);
  1866. BUG_ON(ret);
  1867. continue;
  1868. }
  1869. }
  1870. WARN_ON(*level <= 0);
  1871. if (path->nodes[*level-1])
  1872. free_extent_buffer(path->nodes[*level-1]);
  1873. path->nodes[*level-1] = next;
  1874. *level = btrfs_header_level(next);
  1875. path->slots[*level] = 0;
  1876. }
  1877. out:
  1878. WARN_ON(*level < 0);
  1879. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1880. if (path->nodes[*level] == root->node) {
  1881. root_owner = root->root_key.objectid;
  1882. parent = path->nodes[*level];
  1883. } else {
  1884. parent = path->nodes[*level + 1];
  1885. root_owner = btrfs_header_owner(parent);
  1886. }
  1887. root_gen = btrfs_header_generation(parent);
  1888. ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
  1889. path->nodes[*level]->len,
  1890. root_owner, root_gen, 0, 0, 1);
  1891. free_extent_buffer(path->nodes[*level]);
  1892. path->nodes[*level] = NULL;
  1893. *level += 1;
  1894. BUG_ON(ret);
  1895. return 0;
  1896. }
  1897. /*
  1898. * helper for dropping snapshots. This walks back up the tree in the path
  1899. * to find the first node higher up where we haven't yet gone through
  1900. * all the slots
  1901. */
  1902. static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
  1903. struct btrfs_root *root,
  1904. struct btrfs_path *path, int *level)
  1905. {
  1906. u64 root_owner;
  1907. u64 root_gen;
  1908. struct btrfs_root_item *root_item = &root->root_item;
  1909. int i;
  1910. int slot;
  1911. int ret;
  1912. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1913. slot = path->slots[i];
  1914. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1915. struct extent_buffer *node;
  1916. struct btrfs_disk_key disk_key;
  1917. node = path->nodes[i];
  1918. path->slots[i]++;
  1919. *level = i;
  1920. WARN_ON(*level == 0);
  1921. btrfs_node_key(node, &disk_key, path->slots[i]);
  1922. memcpy(&root_item->drop_progress,
  1923. &disk_key, sizeof(disk_key));
  1924. root_item->drop_level = i;
  1925. return 0;
  1926. } else {
  1927. if (path->nodes[*level] == root->node) {
  1928. root_owner = root->root_key.objectid;
  1929. root_gen =
  1930. btrfs_header_generation(path->nodes[*level]);
  1931. } else {
  1932. struct extent_buffer *node;
  1933. node = path->nodes[*level + 1];
  1934. root_owner = btrfs_header_owner(node);
  1935. root_gen = btrfs_header_generation(node);
  1936. }
  1937. ret = btrfs_free_extent(trans, root,
  1938. path->nodes[*level]->start,
  1939. path->nodes[*level]->len,
  1940. root_owner, root_gen, 0, 0, 1);
  1941. BUG_ON(ret);
  1942. free_extent_buffer(path->nodes[*level]);
  1943. path->nodes[*level] = NULL;
  1944. *level = i + 1;
  1945. }
  1946. }
  1947. return 1;
  1948. }
  1949. /*
  1950. * drop the reference count on the tree rooted at 'snap'. This traverses
  1951. * the tree freeing any blocks that have a ref count of zero after being
  1952. * decremented.
  1953. */
  1954. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  1955. *root)
  1956. {
  1957. int ret = 0;
  1958. int wret;
  1959. int level;
  1960. struct btrfs_path *path;
  1961. int i;
  1962. int orig_level;
  1963. struct btrfs_root_item *root_item = &root->root_item;
  1964. path = btrfs_alloc_path();
  1965. BUG_ON(!path);
  1966. level = btrfs_header_level(root->node);
  1967. orig_level = level;
  1968. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  1969. path->nodes[level] = root->node;
  1970. extent_buffer_get(root->node);
  1971. path->slots[level] = 0;
  1972. } else {
  1973. struct btrfs_key key;
  1974. struct btrfs_disk_key found_key;
  1975. struct extent_buffer *node;
  1976. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  1977. level = root_item->drop_level;
  1978. path->lowest_level = level;
  1979. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1980. if (wret < 0) {
  1981. ret = wret;
  1982. goto out;
  1983. }
  1984. node = path->nodes[level];
  1985. btrfs_node_key(node, &found_key, path->slots[level]);
  1986. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  1987. sizeof(found_key)));
  1988. }
  1989. while(1) {
  1990. wret = walk_down_tree(trans, root, path, &level);
  1991. if (wret > 0)
  1992. break;
  1993. if (wret < 0)
  1994. ret = wret;
  1995. wret = walk_up_tree(trans, root, path, &level);
  1996. if (wret > 0)
  1997. break;
  1998. if (wret < 0)
  1999. ret = wret;
  2000. ret = -EAGAIN;
  2001. break;
  2002. }
  2003. for (i = 0; i <= orig_level; i++) {
  2004. if (path->nodes[i]) {
  2005. free_extent_buffer(path->nodes[i]);
  2006. path->nodes[i] = NULL;
  2007. }
  2008. }
  2009. out:
  2010. btrfs_free_path(path);
  2011. return ret;
  2012. }
  2013. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  2014. {
  2015. u64 start;
  2016. u64 end;
  2017. u64 ptr;
  2018. int ret;
  2019. while(1) {
  2020. ret = find_first_extent_bit(&info->block_group_cache, 0,
  2021. &start, &end, (unsigned int)-1);
  2022. if (ret)
  2023. break;
  2024. ret = get_state_private(&info->block_group_cache, start, &ptr);
  2025. if (!ret)
  2026. kfree((void *)(unsigned long)ptr);
  2027. clear_extent_bits(&info->block_group_cache, start,
  2028. end, (unsigned int)-1, GFP_NOFS);
  2029. }
  2030. while(1) {
  2031. ret = find_first_extent_bit(&info->free_space_cache, 0,
  2032. &start, &end, EXTENT_DIRTY);
  2033. if (ret)
  2034. break;
  2035. clear_extent_dirty(&info->free_space_cache, start,
  2036. end, GFP_NOFS);
  2037. }
  2038. return 0;
  2039. }
  2040. static int noinline relocate_inode_pages(struct inode *inode, u64 start,
  2041. u64 len)
  2042. {
  2043. u64 page_start;
  2044. u64 page_end;
  2045. u64 delalloc_start;
  2046. u64 existing_delalloc;
  2047. unsigned long last_index;
  2048. unsigned long i;
  2049. struct page *page;
  2050. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2051. struct file_ra_state *ra;
  2052. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2053. mutex_lock(&inode->i_mutex);
  2054. i = start >> PAGE_CACHE_SHIFT;
  2055. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  2056. file_ra_state_init(ra, inode->i_mapping);
  2057. btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
  2058. kfree(ra);
  2059. for (; i <= last_index; i++) {
  2060. page = grab_cache_page(inode->i_mapping, i);
  2061. if (!page)
  2062. goto out_unlock;
  2063. if (!PageUptodate(page)) {
  2064. btrfs_readpage(NULL, page);
  2065. lock_page(page);
  2066. if (!PageUptodate(page)) {
  2067. unlock_page(page);
  2068. page_cache_release(page);
  2069. goto out_unlock;
  2070. }
  2071. }
  2072. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2073. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2074. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2075. delalloc_start = page_start;
  2076. existing_delalloc = count_range_bits(io_tree,
  2077. &delalloc_start, page_end,
  2078. PAGE_CACHE_SIZE, EXTENT_DELALLOC);
  2079. set_extent_delalloc(io_tree, page_start,
  2080. page_end, GFP_NOFS);
  2081. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2082. set_page_dirty(page);
  2083. unlock_page(page);
  2084. page_cache_release(page);
  2085. }
  2086. out_unlock:
  2087. mutex_unlock(&inode->i_mutex);
  2088. return 0;
  2089. }
  2090. /*
  2091. * note, this releases the path
  2092. */
  2093. static int noinline relocate_one_reference(struct btrfs_root *extent_root,
  2094. struct btrfs_path *path,
  2095. struct btrfs_key *extent_key)
  2096. {
  2097. struct inode *inode;
  2098. struct btrfs_root *found_root;
  2099. struct btrfs_key *root_location;
  2100. struct btrfs_extent_ref *ref;
  2101. u64 ref_root;
  2102. u64 ref_gen;
  2103. u64 ref_objectid;
  2104. u64 ref_offset;
  2105. int ret;
  2106. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2107. struct btrfs_extent_ref);
  2108. ref_root = btrfs_ref_root(path->nodes[0], ref);
  2109. ref_gen = btrfs_ref_generation(path->nodes[0], ref);
  2110. ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
  2111. ref_offset = btrfs_ref_offset(path->nodes[0], ref);
  2112. btrfs_release_path(extent_root, path);
  2113. root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
  2114. root_location->objectid = ref_root;
  2115. if (ref_gen == 0)
  2116. root_location->offset = 0;
  2117. else
  2118. root_location->offset = (u64)-1;
  2119. root_location->type = BTRFS_ROOT_ITEM_KEY;
  2120. found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
  2121. root_location);
  2122. BUG_ON(!found_root);
  2123. kfree(root_location);
  2124. if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  2125. mutex_unlock(&extent_root->fs_info->fs_mutex);
  2126. inode = btrfs_iget_locked(extent_root->fs_info->sb,
  2127. ref_objectid, found_root);
  2128. if (inode->i_state & I_NEW) {
  2129. /* the inode and parent dir are two different roots */
  2130. BTRFS_I(inode)->root = found_root;
  2131. BTRFS_I(inode)->location.objectid = ref_objectid;
  2132. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  2133. BTRFS_I(inode)->location.offset = 0;
  2134. btrfs_read_locked_inode(inode);
  2135. unlock_new_inode(inode);
  2136. }
  2137. /* this can happen if the reference is not against
  2138. * the latest version of the tree root
  2139. */
  2140. if (is_bad_inode(inode)) {
  2141. mutex_lock(&extent_root->fs_info->fs_mutex);
  2142. goto out;
  2143. }
  2144. relocate_inode_pages(inode, ref_offset, extent_key->offset);
  2145. /* FIXME, data=ordered will help get rid of this */
  2146. filemap_fdatawrite(inode->i_mapping);
  2147. iput(inode);
  2148. mutex_lock(&extent_root->fs_info->fs_mutex);
  2149. } else {
  2150. struct btrfs_trans_handle *trans;
  2151. struct btrfs_key found_key;
  2152. struct extent_buffer *eb;
  2153. int level;
  2154. int i;
  2155. trans = btrfs_start_transaction(found_root, 1);
  2156. eb = read_tree_block(found_root, extent_key->objectid,
  2157. extent_key->offset);
  2158. level = btrfs_header_level(eb);
  2159. if (level == 0)
  2160. btrfs_item_key_to_cpu(eb, &found_key, 0);
  2161. else
  2162. btrfs_node_key_to_cpu(eb, &found_key, 0);
  2163. free_extent_buffer(eb);
  2164. path->lowest_level = level;
  2165. path->reada = 2;
  2166. ret = btrfs_search_slot(trans, found_root, &found_key, path,
  2167. 0, 1);
  2168. path->lowest_level = 0;
  2169. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2170. if (!path->nodes[i])
  2171. break;
  2172. free_extent_buffer(path->nodes[i]);
  2173. path->nodes[i] = NULL;
  2174. }
  2175. btrfs_release_path(found_root, path);
  2176. btrfs_end_transaction(trans, found_root);
  2177. }
  2178. out:
  2179. return 0;
  2180. }
  2181. static int noinline relocate_one_extent(struct btrfs_root *extent_root,
  2182. struct btrfs_path *path,
  2183. struct btrfs_key *extent_key)
  2184. {
  2185. struct btrfs_key key;
  2186. struct btrfs_key found_key;
  2187. struct extent_buffer *leaf;
  2188. u32 nritems;
  2189. u32 item_size;
  2190. int ret = 0;
  2191. key.objectid = extent_key->objectid;
  2192. key.type = BTRFS_EXTENT_REF_KEY;
  2193. key.offset = 0;
  2194. while(1) {
  2195. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2196. if (ret < 0)
  2197. goto out;
  2198. ret = 0;
  2199. leaf = path->nodes[0];
  2200. nritems = btrfs_header_nritems(leaf);
  2201. if (path->slots[0] == nritems)
  2202. goto out;
  2203. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2204. if (found_key.objectid != extent_key->objectid)
  2205. break;
  2206. if (found_key.type != BTRFS_EXTENT_REF_KEY)
  2207. break;
  2208. key.offset = found_key.offset + 1;
  2209. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2210. ret = relocate_one_reference(extent_root, path, extent_key);
  2211. if (ret)
  2212. goto out;
  2213. }
  2214. ret = 0;
  2215. out:
  2216. btrfs_release_path(extent_root, path);
  2217. return ret;
  2218. }
  2219. int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
  2220. {
  2221. struct btrfs_trans_handle *trans;
  2222. struct btrfs_root *tree_root = root->fs_info->tree_root;
  2223. struct btrfs_path *path;
  2224. u64 cur_byte;
  2225. u64 total_found;
  2226. struct btrfs_fs_info *info = root->fs_info;
  2227. struct extent_io_tree *block_group_cache;
  2228. struct btrfs_key key;
  2229. struct btrfs_key found_key;
  2230. struct extent_buffer *leaf;
  2231. u32 nritems;
  2232. int ret;
  2233. int progress = 0;
  2234. btrfs_set_super_total_bytes(&info->super_copy, new_size);
  2235. clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
  2236. GFP_NOFS);
  2237. block_group_cache = &info->block_group_cache;
  2238. path = btrfs_alloc_path();
  2239. root = root->fs_info->extent_root;
  2240. path->reada = 2;
  2241. again:
  2242. total_found = 0;
  2243. key.objectid = new_size;
  2244. key.offset = 0;
  2245. key.type = 0;
  2246. cur_byte = key.objectid;
  2247. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2248. if (ret < 0)
  2249. goto out;
  2250. ret = find_previous_extent(root, path);
  2251. if (ret < 0)
  2252. goto out;
  2253. if (ret == 0) {
  2254. leaf = path->nodes[0];
  2255. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2256. if (found_key.objectid + found_key.offset > new_size) {
  2257. cur_byte = found_key.objectid;
  2258. key.objectid = cur_byte;
  2259. }
  2260. }
  2261. btrfs_release_path(root, path);
  2262. while(1) {
  2263. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2264. if (ret < 0)
  2265. goto out;
  2266. leaf = path->nodes[0];
  2267. nritems = btrfs_header_nritems(leaf);
  2268. next:
  2269. if (path->slots[0] >= nritems) {
  2270. ret = btrfs_next_leaf(root, path);
  2271. if (ret < 0)
  2272. goto out;
  2273. if (ret == 1) {
  2274. ret = 0;
  2275. break;
  2276. }
  2277. leaf = path->nodes[0];
  2278. nritems = btrfs_header_nritems(leaf);
  2279. }
  2280. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2281. if (progress && need_resched()) {
  2282. memcpy(&key, &found_key, sizeof(key));
  2283. mutex_unlock(&root->fs_info->fs_mutex);
  2284. cond_resched();
  2285. mutex_lock(&root->fs_info->fs_mutex);
  2286. btrfs_release_path(root, path);
  2287. btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2288. progress = 0;
  2289. goto next;
  2290. }
  2291. progress = 1;
  2292. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
  2293. found_key.objectid + found_key.offset <= cur_byte) {
  2294. path->slots[0]++;
  2295. goto next;
  2296. }
  2297. total_found++;
  2298. cur_byte = found_key.objectid + found_key.offset;
  2299. key.objectid = cur_byte;
  2300. btrfs_release_path(root, path);
  2301. ret = relocate_one_extent(root, path, &found_key);
  2302. }
  2303. btrfs_release_path(root, path);
  2304. if (total_found > 0) {
  2305. trans = btrfs_start_transaction(tree_root, 1);
  2306. btrfs_commit_transaction(trans, tree_root);
  2307. mutex_unlock(&root->fs_info->fs_mutex);
  2308. btrfs_clean_old_snapshots(tree_root);
  2309. mutex_lock(&root->fs_info->fs_mutex);
  2310. trans = btrfs_start_transaction(tree_root, 1);
  2311. btrfs_commit_transaction(trans, tree_root);
  2312. goto again;
  2313. }
  2314. trans = btrfs_start_transaction(root, 1);
  2315. key.objectid = new_size;
  2316. key.offset = 0;
  2317. key.type = 0;
  2318. while(1) {
  2319. u64 ptr;
  2320. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2321. if (ret < 0)
  2322. goto out;
  2323. leaf = path->nodes[0];
  2324. nritems = btrfs_header_nritems(leaf);
  2325. bg_next:
  2326. if (path->slots[0] >= nritems) {
  2327. ret = btrfs_next_leaf(root, path);
  2328. if (ret < 0)
  2329. break;
  2330. if (ret == 1) {
  2331. ret = 0;
  2332. break;
  2333. }
  2334. leaf = path->nodes[0];
  2335. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2336. /*
  2337. * btrfs_next_leaf doesn't cow buffers, we have to
  2338. * do the search again
  2339. */
  2340. memcpy(&key, &found_key, sizeof(key));
  2341. btrfs_release_path(root, path);
  2342. goto resched_check;
  2343. }
  2344. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2345. if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
  2346. printk("shrinker found key %Lu %u %Lu\n",
  2347. found_key.objectid, found_key.type,
  2348. found_key.offset);
  2349. path->slots[0]++;
  2350. goto bg_next;
  2351. }
  2352. ret = get_state_private(&info->block_group_cache,
  2353. found_key.objectid, &ptr);
  2354. if (!ret)
  2355. kfree((void *)(unsigned long)ptr);
  2356. clear_extent_bits(&info->block_group_cache, found_key.objectid,
  2357. found_key.objectid + found_key.offset - 1,
  2358. (unsigned int)-1, GFP_NOFS);
  2359. key.objectid = found_key.objectid + 1;
  2360. btrfs_del_item(trans, root, path);
  2361. btrfs_release_path(root, path);
  2362. resched_check:
  2363. if (need_resched()) {
  2364. mutex_unlock(&root->fs_info->fs_mutex);
  2365. cond_resched();
  2366. mutex_lock(&root->fs_info->fs_mutex);
  2367. }
  2368. }
  2369. clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
  2370. GFP_NOFS);
  2371. btrfs_commit_transaction(trans, root);
  2372. out:
  2373. btrfs_free_path(path);
  2374. return ret;
  2375. }
  2376. int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
  2377. struct btrfs_root *root, u64 new_size)
  2378. {
  2379. struct btrfs_path *path;
  2380. u64 nr = 0;
  2381. u64 cur_byte;
  2382. u64 old_size;
  2383. unsigned long rem;
  2384. struct btrfs_block_group_cache *cache;
  2385. struct btrfs_block_group_item *item;
  2386. struct btrfs_fs_info *info = root->fs_info;
  2387. struct extent_io_tree *block_group_cache;
  2388. struct btrfs_key key;
  2389. struct extent_buffer *leaf;
  2390. int ret;
  2391. int bit;
  2392. old_size = btrfs_super_total_bytes(&info->super_copy);
  2393. block_group_cache = &info->block_group_cache;
  2394. root = info->extent_root;
  2395. cache = btrfs_lookup_block_group(root->fs_info, old_size - 1);
  2396. cur_byte = cache->key.objectid + cache->key.offset;
  2397. if (cur_byte >= new_size)
  2398. goto set_size;
  2399. key.offset = BTRFS_BLOCK_GROUP_SIZE;
  2400. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  2401. path = btrfs_alloc_path();
  2402. if (!path)
  2403. return -ENOMEM;
  2404. while(cur_byte < new_size) {
  2405. key.objectid = cur_byte;
  2406. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2407. sizeof(struct btrfs_block_group_item));
  2408. BUG_ON(ret);
  2409. leaf = path->nodes[0];
  2410. item = btrfs_item_ptr(leaf, path->slots[0],
  2411. struct btrfs_block_group_item);
  2412. btrfs_set_disk_block_group_used(leaf, item, 0);
  2413. div_long_long_rem(nr, 3, &rem);
  2414. if (rem) {
  2415. btrfs_set_disk_block_group_flags(leaf, item,
  2416. BTRFS_BLOCK_GROUP_DATA);
  2417. } else {
  2418. btrfs_set_disk_block_group_flags(leaf, item, 0);
  2419. }
  2420. nr++;
  2421. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  2422. BUG_ON(!cache);
  2423. read_extent_buffer(leaf, &cache->item, (unsigned long)item,
  2424. sizeof(cache->item));
  2425. memcpy(&cache->key, &key, sizeof(key));
  2426. cache->cached = 0;
  2427. cache->pinned = 0;
  2428. cur_byte = key.objectid + key.offset;
  2429. btrfs_release_path(root, path);
  2430. if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
  2431. bit = BLOCK_GROUP_DATA;
  2432. cache->data = BTRFS_BLOCK_GROUP_DATA;
  2433. } else {
  2434. bit = BLOCK_GROUP_METADATA;
  2435. cache->data = 0;
  2436. }
  2437. /* use EXTENT_LOCKED to prevent merging */
  2438. set_extent_bits(block_group_cache, key.objectid,
  2439. key.objectid + key.offset - 1,
  2440. bit | EXTENT_LOCKED, GFP_NOFS);
  2441. set_state_private(block_group_cache, key.objectid,
  2442. (unsigned long)cache);
  2443. }
  2444. btrfs_free_path(path);
  2445. set_size:
  2446. btrfs_set_super_total_bytes(&info->super_copy, new_size);
  2447. return 0;
  2448. }
  2449. int btrfs_read_block_groups(struct btrfs_root *root)
  2450. {
  2451. struct btrfs_path *path;
  2452. int ret;
  2453. int err = 0;
  2454. int bit;
  2455. struct btrfs_block_group_cache *cache;
  2456. struct btrfs_fs_info *info = root->fs_info;
  2457. struct extent_io_tree *block_group_cache;
  2458. struct btrfs_key key;
  2459. struct btrfs_key found_key;
  2460. struct extent_buffer *leaf;
  2461. block_group_cache = &info->block_group_cache;
  2462. root = info->extent_root;
  2463. key.objectid = 0;
  2464. key.offset = BTRFS_BLOCK_GROUP_SIZE;
  2465. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  2466. path = btrfs_alloc_path();
  2467. if (!path)
  2468. return -ENOMEM;
  2469. while(1) {
  2470. ret = btrfs_search_slot(NULL, info->extent_root,
  2471. &key, path, 0, 0);
  2472. if (ret != 0) {
  2473. err = ret;
  2474. break;
  2475. }
  2476. leaf = path->nodes[0];
  2477. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2478. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  2479. if (!cache) {
  2480. err = -1;
  2481. break;
  2482. }
  2483. read_extent_buffer(leaf, &cache->item,
  2484. btrfs_item_ptr_offset(leaf, path->slots[0]),
  2485. sizeof(cache->item));
  2486. memcpy(&cache->key, &found_key, sizeof(found_key));
  2487. cache->cached = 0;
  2488. cache->pinned = 0;
  2489. key.objectid = found_key.objectid + found_key.offset;
  2490. btrfs_release_path(root, path);
  2491. if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
  2492. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  2493. cache->data = BTRFS_BLOCK_GROUP_MIXED;
  2494. } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
  2495. bit = BLOCK_GROUP_DATA;
  2496. cache->data = BTRFS_BLOCK_GROUP_DATA;
  2497. } else {
  2498. bit = BLOCK_GROUP_METADATA;
  2499. cache->data = 0;
  2500. }
  2501. /* use EXTENT_LOCKED to prevent merging */
  2502. set_extent_bits(block_group_cache, found_key.objectid,
  2503. found_key.objectid + found_key.offset - 1,
  2504. bit | EXTENT_LOCKED, GFP_NOFS);
  2505. set_state_private(block_group_cache, found_key.objectid,
  2506. (unsigned long)cache);
  2507. if (key.objectid >=
  2508. btrfs_super_total_bytes(&info->super_copy))
  2509. break;
  2510. }
  2511. btrfs_free_path(path);
  2512. return 0;
  2513. }