extent-tree.c 56 KB

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