extent-tree.c 79 KB

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