extent-tree.c 68 KB

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