extent-tree.c 74 KB

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