extent-tree.c 71 KB

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