extent-tree.c 68 KB

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