extent-tree.c 73 KB

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