extent-tree.c 95 KB

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