extent-tree.c 85 KB

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