extent-tree.c 70 KB

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