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