extent-tree.c 73 KB

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