extent-tree.c 76 KB

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