extent-tree.c 91 KB

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