extent-tree.c 70 KB

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