extent-tree.c 97 KB

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