extent-tree.c 93 KB

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