free-space-cache.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493
  1. /*
  2. * Copyright (C) 2008 Red Hat. 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/pagemap.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/math64.h>
  22. #include <linux/ratelimit.h>
  23. #include "ctree.h"
  24. #include "free-space-cache.h"
  25. #include "transaction.h"
  26. #include "disk-io.h"
  27. #include "extent_io.h"
  28. #include "inode-map.h"
  29. #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
  30. #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
  31. static int link_free_space(struct btrfs_free_space_ctl *ctl,
  32. struct btrfs_free_space *info);
  33. static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
  34. struct btrfs_free_space *info);
  35. static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
  36. struct btrfs_path *path,
  37. u64 offset)
  38. {
  39. struct btrfs_key key;
  40. struct btrfs_key location;
  41. struct btrfs_disk_key disk_key;
  42. struct btrfs_free_space_header *header;
  43. struct extent_buffer *leaf;
  44. struct inode *inode = NULL;
  45. int ret;
  46. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  47. key.offset = offset;
  48. key.type = 0;
  49. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  50. if (ret < 0)
  51. return ERR_PTR(ret);
  52. if (ret > 0) {
  53. btrfs_release_path(path);
  54. return ERR_PTR(-ENOENT);
  55. }
  56. leaf = path->nodes[0];
  57. header = btrfs_item_ptr(leaf, path->slots[0],
  58. struct btrfs_free_space_header);
  59. btrfs_free_space_key(leaf, header, &disk_key);
  60. btrfs_disk_key_to_cpu(&location, &disk_key);
  61. btrfs_release_path(path);
  62. inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
  63. if (!inode)
  64. return ERR_PTR(-ENOENT);
  65. if (IS_ERR(inode))
  66. return inode;
  67. if (is_bad_inode(inode)) {
  68. iput(inode);
  69. return ERR_PTR(-ENOENT);
  70. }
  71. mapping_set_gfp_mask(inode->i_mapping,
  72. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  73. return inode;
  74. }
  75. struct inode *lookup_free_space_inode(struct btrfs_root *root,
  76. struct btrfs_block_group_cache
  77. *block_group, struct btrfs_path *path)
  78. {
  79. struct inode *inode = NULL;
  80. u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
  81. spin_lock(&block_group->lock);
  82. if (block_group->inode)
  83. inode = igrab(block_group->inode);
  84. spin_unlock(&block_group->lock);
  85. if (inode)
  86. return inode;
  87. inode = __lookup_free_space_inode(root, path,
  88. block_group->key.objectid);
  89. if (IS_ERR(inode))
  90. return inode;
  91. spin_lock(&block_group->lock);
  92. if (!((BTRFS_I(inode)->flags & flags) == flags)) {
  93. btrfs_info(root->fs_info,
  94. "Old style space inode found, converting.");
  95. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
  96. BTRFS_INODE_NODATACOW;
  97. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  98. }
  99. if (!block_group->iref) {
  100. block_group->inode = igrab(inode);
  101. block_group->iref = 1;
  102. }
  103. spin_unlock(&block_group->lock);
  104. return inode;
  105. }
  106. int __create_free_space_inode(struct btrfs_root *root,
  107. struct btrfs_trans_handle *trans,
  108. struct btrfs_path *path, u64 ino, u64 offset)
  109. {
  110. struct btrfs_key key;
  111. struct btrfs_disk_key disk_key;
  112. struct btrfs_free_space_header *header;
  113. struct btrfs_inode_item *inode_item;
  114. struct extent_buffer *leaf;
  115. u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
  116. int ret;
  117. ret = btrfs_insert_empty_inode(trans, root, path, ino);
  118. if (ret)
  119. return ret;
  120. /* We inline crc's for the free disk space cache */
  121. if (ino != BTRFS_FREE_INO_OBJECTID)
  122. flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
  123. leaf = path->nodes[0];
  124. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  125. struct btrfs_inode_item);
  126. btrfs_item_key(leaf, &disk_key, path->slots[0]);
  127. memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
  128. sizeof(*inode_item));
  129. btrfs_set_inode_generation(leaf, inode_item, trans->transid);
  130. btrfs_set_inode_size(leaf, inode_item, 0);
  131. btrfs_set_inode_nbytes(leaf, inode_item, 0);
  132. btrfs_set_inode_uid(leaf, inode_item, 0);
  133. btrfs_set_inode_gid(leaf, inode_item, 0);
  134. btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
  135. btrfs_set_inode_flags(leaf, inode_item, flags);
  136. btrfs_set_inode_nlink(leaf, inode_item, 1);
  137. btrfs_set_inode_transid(leaf, inode_item, trans->transid);
  138. btrfs_set_inode_block_group(leaf, inode_item, offset);
  139. btrfs_mark_buffer_dirty(leaf);
  140. btrfs_release_path(path);
  141. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  142. key.offset = offset;
  143. key.type = 0;
  144. ret = btrfs_insert_empty_item(trans, root, path, &key,
  145. sizeof(struct btrfs_free_space_header));
  146. if (ret < 0) {
  147. btrfs_release_path(path);
  148. return ret;
  149. }
  150. leaf = path->nodes[0];
  151. header = btrfs_item_ptr(leaf, path->slots[0],
  152. struct btrfs_free_space_header);
  153. memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
  154. btrfs_set_free_space_key(leaf, header, &disk_key);
  155. btrfs_mark_buffer_dirty(leaf);
  156. btrfs_release_path(path);
  157. return 0;
  158. }
  159. int create_free_space_inode(struct btrfs_root *root,
  160. struct btrfs_trans_handle *trans,
  161. struct btrfs_block_group_cache *block_group,
  162. struct btrfs_path *path)
  163. {
  164. int ret;
  165. u64 ino;
  166. ret = btrfs_find_free_objectid(root, &ino);
  167. if (ret < 0)
  168. return ret;
  169. return __create_free_space_inode(root, trans, path, ino,
  170. block_group->key.objectid);
  171. }
  172. int btrfs_truncate_free_space_cache(struct btrfs_root *root,
  173. struct btrfs_trans_handle *trans,
  174. struct btrfs_path *path,
  175. struct inode *inode)
  176. {
  177. struct btrfs_block_rsv *rsv;
  178. u64 needed_bytes;
  179. loff_t oldsize;
  180. int ret = 0;
  181. rsv = trans->block_rsv;
  182. trans->block_rsv = &root->fs_info->global_block_rsv;
  183. /* 1 for slack space, 1 for updating the inode */
  184. needed_bytes = btrfs_calc_trunc_metadata_size(root, 1) +
  185. btrfs_calc_trans_metadata_size(root, 1);
  186. spin_lock(&trans->block_rsv->lock);
  187. if (trans->block_rsv->reserved < needed_bytes) {
  188. spin_unlock(&trans->block_rsv->lock);
  189. trans->block_rsv = rsv;
  190. return -ENOSPC;
  191. }
  192. spin_unlock(&trans->block_rsv->lock);
  193. oldsize = i_size_read(inode);
  194. btrfs_i_size_write(inode, 0);
  195. truncate_pagecache(inode, oldsize, 0);
  196. /*
  197. * We don't need an orphan item because truncating the free space cache
  198. * will never be split across transactions.
  199. */
  200. ret = btrfs_truncate_inode_items(trans, root, inode,
  201. 0, BTRFS_EXTENT_DATA_KEY);
  202. if (ret) {
  203. trans->block_rsv = rsv;
  204. btrfs_abort_transaction(trans, root, ret);
  205. return ret;
  206. }
  207. ret = btrfs_update_inode(trans, root, inode);
  208. if (ret)
  209. btrfs_abort_transaction(trans, root, ret);
  210. trans->block_rsv = rsv;
  211. return ret;
  212. }
  213. static int readahead_cache(struct inode *inode)
  214. {
  215. struct file_ra_state *ra;
  216. unsigned long last_index;
  217. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  218. if (!ra)
  219. return -ENOMEM;
  220. file_ra_state_init(ra, inode->i_mapping);
  221. last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  222. page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
  223. kfree(ra);
  224. return 0;
  225. }
  226. struct io_ctl {
  227. void *cur, *orig;
  228. struct page *page;
  229. struct page **pages;
  230. struct btrfs_root *root;
  231. unsigned long size;
  232. int index;
  233. int num_pages;
  234. unsigned check_crcs:1;
  235. };
  236. static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
  237. struct btrfs_root *root)
  238. {
  239. memset(io_ctl, 0, sizeof(struct io_ctl));
  240. io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  241. PAGE_CACHE_SHIFT;
  242. io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages,
  243. GFP_NOFS);
  244. if (!io_ctl->pages)
  245. return -ENOMEM;
  246. io_ctl->root = root;
  247. if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
  248. io_ctl->check_crcs = 1;
  249. return 0;
  250. }
  251. static void io_ctl_free(struct io_ctl *io_ctl)
  252. {
  253. kfree(io_ctl->pages);
  254. }
  255. static void io_ctl_unmap_page(struct io_ctl *io_ctl)
  256. {
  257. if (io_ctl->cur) {
  258. kunmap(io_ctl->page);
  259. io_ctl->cur = NULL;
  260. io_ctl->orig = NULL;
  261. }
  262. }
  263. static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
  264. {
  265. BUG_ON(io_ctl->index >= io_ctl->num_pages);
  266. io_ctl->page = io_ctl->pages[io_ctl->index++];
  267. io_ctl->cur = kmap(io_ctl->page);
  268. io_ctl->orig = io_ctl->cur;
  269. io_ctl->size = PAGE_CACHE_SIZE;
  270. if (clear)
  271. memset(io_ctl->cur, 0, PAGE_CACHE_SIZE);
  272. }
  273. static void io_ctl_drop_pages(struct io_ctl *io_ctl)
  274. {
  275. int i;
  276. io_ctl_unmap_page(io_ctl);
  277. for (i = 0; i < io_ctl->num_pages; i++) {
  278. if (io_ctl->pages[i]) {
  279. ClearPageChecked(io_ctl->pages[i]);
  280. unlock_page(io_ctl->pages[i]);
  281. page_cache_release(io_ctl->pages[i]);
  282. }
  283. }
  284. }
  285. static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode,
  286. int uptodate)
  287. {
  288. struct page *page;
  289. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  290. int i;
  291. for (i = 0; i < io_ctl->num_pages; i++) {
  292. page = find_or_create_page(inode->i_mapping, i, mask);
  293. if (!page) {
  294. io_ctl_drop_pages(io_ctl);
  295. return -ENOMEM;
  296. }
  297. io_ctl->pages[i] = page;
  298. if (uptodate && !PageUptodate(page)) {
  299. btrfs_readpage(NULL, page);
  300. lock_page(page);
  301. if (!PageUptodate(page)) {
  302. printk(KERN_ERR "btrfs: error reading free "
  303. "space cache\n");
  304. io_ctl_drop_pages(io_ctl);
  305. return -EIO;
  306. }
  307. }
  308. }
  309. for (i = 0; i < io_ctl->num_pages; i++) {
  310. clear_page_dirty_for_io(io_ctl->pages[i]);
  311. set_page_extent_mapped(io_ctl->pages[i]);
  312. }
  313. return 0;
  314. }
  315. static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation)
  316. {
  317. __le64 *val;
  318. io_ctl_map_page(io_ctl, 1);
  319. /*
  320. * Skip the csum areas. If we don't check crcs then we just have a
  321. * 64bit chunk at the front of the first page.
  322. */
  323. if (io_ctl->check_crcs) {
  324. io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
  325. io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
  326. } else {
  327. io_ctl->cur += sizeof(u64);
  328. io_ctl->size -= sizeof(u64) * 2;
  329. }
  330. val = io_ctl->cur;
  331. *val = cpu_to_le64(generation);
  332. io_ctl->cur += sizeof(u64);
  333. }
  334. static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
  335. {
  336. __le64 *gen;
  337. /*
  338. * Skip the crc area. If we don't check crcs then we just have a 64bit
  339. * chunk at the front of the first page.
  340. */
  341. if (io_ctl->check_crcs) {
  342. io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
  343. io_ctl->size -= sizeof(u64) +
  344. (sizeof(u32) * io_ctl->num_pages);
  345. } else {
  346. io_ctl->cur += sizeof(u64);
  347. io_ctl->size -= sizeof(u64) * 2;
  348. }
  349. gen = io_ctl->cur;
  350. if (le64_to_cpu(*gen) != generation) {
  351. printk_ratelimited(KERN_ERR "btrfs: space cache generation "
  352. "(%Lu) does not match inode (%Lu)\n", *gen,
  353. generation);
  354. io_ctl_unmap_page(io_ctl);
  355. return -EIO;
  356. }
  357. io_ctl->cur += sizeof(u64);
  358. return 0;
  359. }
  360. static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
  361. {
  362. u32 *tmp;
  363. u32 crc = ~(u32)0;
  364. unsigned offset = 0;
  365. if (!io_ctl->check_crcs) {
  366. io_ctl_unmap_page(io_ctl);
  367. return;
  368. }
  369. if (index == 0)
  370. offset = sizeof(u32) * io_ctl->num_pages;
  371. crc = btrfs_csum_data(io_ctl->orig + offset, crc,
  372. PAGE_CACHE_SIZE - offset);
  373. btrfs_csum_final(crc, (char *)&crc);
  374. io_ctl_unmap_page(io_ctl);
  375. tmp = kmap(io_ctl->pages[0]);
  376. tmp += index;
  377. *tmp = crc;
  378. kunmap(io_ctl->pages[0]);
  379. }
  380. static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
  381. {
  382. u32 *tmp, val;
  383. u32 crc = ~(u32)0;
  384. unsigned offset = 0;
  385. if (!io_ctl->check_crcs) {
  386. io_ctl_map_page(io_ctl, 0);
  387. return 0;
  388. }
  389. if (index == 0)
  390. offset = sizeof(u32) * io_ctl->num_pages;
  391. tmp = kmap(io_ctl->pages[0]);
  392. tmp += index;
  393. val = *tmp;
  394. kunmap(io_ctl->pages[0]);
  395. io_ctl_map_page(io_ctl, 0);
  396. crc = btrfs_csum_data(io_ctl->orig + offset, crc,
  397. PAGE_CACHE_SIZE - offset);
  398. btrfs_csum_final(crc, (char *)&crc);
  399. if (val != crc) {
  400. printk_ratelimited(KERN_ERR "btrfs: csum mismatch on free "
  401. "space cache\n");
  402. io_ctl_unmap_page(io_ctl);
  403. return -EIO;
  404. }
  405. return 0;
  406. }
  407. static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes,
  408. void *bitmap)
  409. {
  410. struct btrfs_free_space_entry *entry;
  411. if (!io_ctl->cur)
  412. return -ENOSPC;
  413. entry = io_ctl->cur;
  414. entry->offset = cpu_to_le64(offset);
  415. entry->bytes = cpu_to_le64(bytes);
  416. entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
  417. BTRFS_FREE_SPACE_EXTENT;
  418. io_ctl->cur += sizeof(struct btrfs_free_space_entry);
  419. io_ctl->size -= sizeof(struct btrfs_free_space_entry);
  420. if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
  421. return 0;
  422. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  423. /* No more pages to map */
  424. if (io_ctl->index >= io_ctl->num_pages)
  425. return 0;
  426. /* map the next page */
  427. io_ctl_map_page(io_ctl, 1);
  428. return 0;
  429. }
  430. static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap)
  431. {
  432. if (!io_ctl->cur)
  433. return -ENOSPC;
  434. /*
  435. * If we aren't at the start of the current page, unmap this one and
  436. * map the next one if there is any left.
  437. */
  438. if (io_ctl->cur != io_ctl->orig) {
  439. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  440. if (io_ctl->index >= io_ctl->num_pages)
  441. return -ENOSPC;
  442. io_ctl_map_page(io_ctl, 0);
  443. }
  444. memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
  445. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  446. if (io_ctl->index < io_ctl->num_pages)
  447. io_ctl_map_page(io_ctl, 0);
  448. return 0;
  449. }
  450. static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl)
  451. {
  452. /*
  453. * If we're not on the boundary we know we've modified the page and we
  454. * need to crc the page.
  455. */
  456. if (io_ctl->cur != io_ctl->orig)
  457. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  458. else
  459. io_ctl_unmap_page(io_ctl);
  460. while (io_ctl->index < io_ctl->num_pages) {
  461. io_ctl_map_page(io_ctl, 1);
  462. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  463. }
  464. }
  465. static int io_ctl_read_entry(struct io_ctl *io_ctl,
  466. struct btrfs_free_space *entry, u8 *type)
  467. {
  468. struct btrfs_free_space_entry *e;
  469. int ret;
  470. if (!io_ctl->cur) {
  471. ret = io_ctl_check_crc(io_ctl, io_ctl->index);
  472. if (ret)
  473. return ret;
  474. }
  475. e = io_ctl->cur;
  476. entry->offset = le64_to_cpu(e->offset);
  477. entry->bytes = le64_to_cpu(e->bytes);
  478. *type = e->type;
  479. io_ctl->cur += sizeof(struct btrfs_free_space_entry);
  480. io_ctl->size -= sizeof(struct btrfs_free_space_entry);
  481. if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
  482. return 0;
  483. io_ctl_unmap_page(io_ctl);
  484. return 0;
  485. }
  486. static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
  487. struct btrfs_free_space *entry)
  488. {
  489. int ret;
  490. ret = io_ctl_check_crc(io_ctl, io_ctl->index);
  491. if (ret)
  492. return ret;
  493. memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
  494. io_ctl_unmap_page(io_ctl);
  495. return 0;
  496. }
  497. /*
  498. * Since we attach pinned extents after the fact we can have contiguous sections
  499. * of free space that are split up in entries. This poses a problem with the
  500. * tree logging stuff since it could have allocated across what appears to be 2
  501. * entries since we would have merged the entries when adding the pinned extents
  502. * back to the free space cache. So run through the space cache that we just
  503. * loaded and merge contiguous entries. This will make the log replay stuff not
  504. * blow up and it will make for nicer allocator behavior.
  505. */
  506. static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
  507. {
  508. struct btrfs_free_space *e, *prev = NULL;
  509. struct rb_node *n;
  510. again:
  511. spin_lock(&ctl->tree_lock);
  512. for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
  513. e = rb_entry(n, struct btrfs_free_space, offset_index);
  514. if (!prev)
  515. goto next;
  516. if (e->bitmap || prev->bitmap)
  517. goto next;
  518. if (prev->offset + prev->bytes == e->offset) {
  519. unlink_free_space(ctl, prev);
  520. unlink_free_space(ctl, e);
  521. prev->bytes += e->bytes;
  522. kmem_cache_free(btrfs_free_space_cachep, e);
  523. link_free_space(ctl, prev);
  524. prev = NULL;
  525. spin_unlock(&ctl->tree_lock);
  526. goto again;
  527. }
  528. next:
  529. prev = e;
  530. }
  531. spin_unlock(&ctl->tree_lock);
  532. }
  533. int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
  534. struct btrfs_free_space_ctl *ctl,
  535. struct btrfs_path *path, u64 offset)
  536. {
  537. struct btrfs_free_space_header *header;
  538. struct extent_buffer *leaf;
  539. struct io_ctl io_ctl;
  540. struct btrfs_key key;
  541. struct btrfs_free_space *e, *n;
  542. struct list_head bitmaps;
  543. u64 num_entries;
  544. u64 num_bitmaps;
  545. u64 generation;
  546. u8 type;
  547. int ret = 0;
  548. INIT_LIST_HEAD(&bitmaps);
  549. /* Nothing in the space cache, goodbye */
  550. if (!i_size_read(inode))
  551. return 0;
  552. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  553. key.offset = offset;
  554. key.type = 0;
  555. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  556. if (ret < 0)
  557. return 0;
  558. else if (ret > 0) {
  559. btrfs_release_path(path);
  560. return 0;
  561. }
  562. ret = -1;
  563. leaf = path->nodes[0];
  564. header = btrfs_item_ptr(leaf, path->slots[0],
  565. struct btrfs_free_space_header);
  566. num_entries = btrfs_free_space_entries(leaf, header);
  567. num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
  568. generation = btrfs_free_space_generation(leaf, header);
  569. btrfs_release_path(path);
  570. if (BTRFS_I(inode)->generation != generation) {
  571. btrfs_err(root->fs_info,
  572. "free space inode generation (%llu) "
  573. "did not match free space cache generation (%llu)",
  574. (unsigned long long)BTRFS_I(inode)->generation,
  575. (unsigned long long)generation);
  576. return 0;
  577. }
  578. if (!num_entries)
  579. return 0;
  580. ret = io_ctl_init(&io_ctl, inode, root);
  581. if (ret)
  582. return ret;
  583. ret = readahead_cache(inode);
  584. if (ret)
  585. goto out;
  586. ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
  587. if (ret)
  588. goto out;
  589. ret = io_ctl_check_crc(&io_ctl, 0);
  590. if (ret)
  591. goto free_cache;
  592. ret = io_ctl_check_generation(&io_ctl, generation);
  593. if (ret)
  594. goto free_cache;
  595. while (num_entries) {
  596. e = kmem_cache_zalloc(btrfs_free_space_cachep,
  597. GFP_NOFS);
  598. if (!e)
  599. goto free_cache;
  600. ret = io_ctl_read_entry(&io_ctl, e, &type);
  601. if (ret) {
  602. kmem_cache_free(btrfs_free_space_cachep, e);
  603. goto free_cache;
  604. }
  605. if (!e->bytes) {
  606. kmem_cache_free(btrfs_free_space_cachep, e);
  607. goto free_cache;
  608. }
  609. if (type == BTRFS_FREE_SPACE_EXTENT) {
  610. spin_lock(&ctl->tree_lock);
  611. ret = link_free_space(ctl, e);
  612. spin_unlock(&ctl->tree_lock);
  613. if (ret) {
  614. btrfs_err(root->fs_info,
  615. "Duplicate entries in free space cache, dumping");
  616. kmem_cache_free(btrfs_free_space_cachep, e);
  617. goto free_cache;
  618. }
  619. } else {
  620. BUG_ON(!num_bitmaps);
  621. num_bitmaps--;
  622. e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  623. if (!e->bitmap) {
  624. kmem_cache_free(
  625. btrfs_free_space_cachep, e);
  626. goto free_cache;
  627. }
  628. spin_lock(&ctl->tree_lock);
  629. ret = link_free_space(ctl, e);
  630. ctl->total_bitmaps++;
  631. ctl->op->recalc_thresholds(ctl);
  632. spin_unlock(&ctl->tree_lock);
  633. if (ret) {
  634. btrfs_err(root->fs_info,
  635. "Duplicate entries in free space cache, dumping");
  636. kmem_cache_free(btrfs_free_space_cachep, e);
  637. goto free_cache;
  638. }
  639. list_add_tail(&e->list, &bitmaps);
  640. }
  641. num_entries--;
  642. }
  643. io_ctl_unmap_page(&io_ctl);
  644. /*
  645. * We add the bitmaps at the end of the entries in order that
  646. * the bitmap entries are added to the cache.
  647. */
  648. list_for_each_entry_safe(e, n, &bitmaps, list) {
  649. list_del_init(&e->list);
  650. ret = io_ctl_read_bitmap(&io_ctl, e);
  651. if (ret)
  652. goto free_cache;
  653. }
  654. io_ctl_drop_pages(&io_ctl);
  655. merge_space_tree(ctl);
  656. ret = 1;
  657. out:
  658. io_ctl_free(&io_ctl);
  659. return ret;
  660. free_cache:
  661. io_ctl_drop_pages(&io_ctl);
  662. __btrfs_remove_free_space_cache(ctl);
  663. goto out;
  664. }
  665. int load_free_space_cache(struct btrfs_fs_info *fs_info,
  666. struct btrfs_block_group_cache *block_group)
  667. {
  668. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  669. struct btrfs_root *root = fs_info->tree_root;
  670. struct inode *inode;
  671. struct btrfs_path *path;
  672. int ret = 0;
  673. bool matched;
  674. u64 used = btrfs_block_group_used(&block_group->item);
  675. /*
  676. * If this block group has been marked to be cleared for one reason or
  677. * another then we can't trust the on disk cache, so just return.
  678. */
  679. spin_lock(&block_group->lock);
  680. if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
  681. spin_unlock(&block_group->lock);
  682. return 0;
  683. }
  684. spin_unlock(&block_group->lock);
  685. path = btrfs_alloc_path();
  686. if (!path)
  687. return 0;
  688. path->search_commit_root = 1;
  689. path->skip_locking = 1;
  690. inode = lookup_free_space_inode(root, block_group, path);
  691. if (IS_ERR(inode)) {
  692. btrfs_free_path(path);
  693. return 0;
  694. }
  695. /* We may have converted the inode and made the cache invalid. */
  696. spin_lock(&block_group->lock);
  697. if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
  698. spin_unlock(&block_group->lock);
  699. btrfs_free_path(path);
  700. goto out;
  701. }
  702. spin_unlock(&block_group->lock);
  703. ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
  704. path, block_group->key.objectid);
  705. btrfs_free_path(path);
  706. if (ret <= 0)
  707. goto out;
  708. spin_lock(&ctl->tree_lock);
  709. matched = (ctl->free_space == (block_group->key.offset - used -
  710. block_group->bytes_super));
  711. spin_unlock(&ctl->tree_lock);
  712. if (!matched) {
  713. __btrfs_remove_free_space_cache(ctl);
  714. btrfs_err(fs_info, "block group %llu has wrong amount of free space",
  715. block_group->key.objectid);
  716. ret = -1;
  717. }
  718. out:
  719. if (ret < 0) {
  720. /* This cache is bogus, make sure it gets cleared */
  721. spin_lock(&block_group->lock);
  722. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  723. spin_unlock(&block_group->lock);
  724. ret = 0;
  725. btrfs_err(fs_info, "failed to load free space cache for block group %llu",
  726. block_group->key.objectid);
  727. }
  728. iput(inode);
  729. return ret;
  730. }
  731. /**
  732. * __btrfs_write_out_cache - write out cached info to an inode
  733. * @root - the root the inode belongs to
  734. * @ctl - the free space cache we are going to write out
  735. * @block_group - the block_group for this cache if it belongs to a block_group
  736. * @trans - the trans handle
  737. * @path - the path to use
  738. * @offset - the offset for the key we'll insert
  739. *
  740. * This function writes out a free space cache struct to disk for quick recovery
  741. * on mount. This will return 0 if it was successfull in writing the cache out,
  742. * and -1 if it was not.
  743. */
  744. int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
  745. struct btrfs_free_space_ctl *ctl,
  746. struct btrfs_block_group_cache *block_group,
  747. struct btrfs_trans_handle *trans,
  748. struct btrfs_path *path, u64 offset)
  749. {
  750. struct btrfs_free_space_header *header;
  751. struct extent_buffer *leaf;
  752. struct rb_node *node;
  753. struct list_head *pos, *n;
  754. struct extent_state *cached_state = NULL;
  755. struct btrfs_free_cluster *cluster = NULL;
  756. struct extent_io_tree *unpin = NULL;
  757. struct io_ctl io_ctl;
  758. struct list_head bitmap_list;
  759. struct btrfs_key key;
  760. u64 start, extent_start, extent_end, len;
  761. int entries = 0;
  762. int bitmaps = 0;
  763. int ret;
  764. int err = -1;
  765. INIT_LIST_HEAD(&bitmap_list);
  766. if (!i_size_read(inode))
  767. return -1;
  768. ret = io_ctl_init(&io_ctl, inode, root);
  769. if (ret)
  770. return -1;
  771. /* Get the cluster for this block_group if it exists */
  772. if (block_group && !list_empty(&block_group->cluster_list))
  773. cluster = list_entry(block_group->cluster_list.next,
  774. struct btrfs_free_cluster,
  775. block_group_list);
  776. /* Lock all pages first so we can lock the extent safely. */
  777. io_ctl_prepare_pages(&io_ctl, inode, 0);
  778. lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  779. 0, &cached_state);
  780. node = rb_first(&ctl->free_space_offset);
  781. if (!node && cluster) {
  782. node = rb_first(&cluster->root);
  783. cluster = NULL;
  784. }
  785. /* Make sure we can fit our crcs into the first page */
  786. if (io_ctl.check_crcs &&
  787. (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) {
  788. WARN_ON(1);
  789. goto out_nospc;
  790. }
  791. io_ctl_set_generation(&io_ctl, trans->transid);
  792. /* Write out the extent entries */
  793. while (node) {
  794. struct btrfs_free_space *e;
  795. e = rb_entry(node, struct btrfs_free_space, offset_index);
  796. entries++;
  797. ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes,
  798. e->bitmap);
  799. if (ret)
  800. goto out_nospc;
  801. if (e->bitmap) {
  802. list_add_tail(&e->list, &bitmap_list);
  803. bitmaps++;
  804. }
  805. node = rb_next(node);
  806. if (!node && cluster) {
  807. node = rb_first(&cluster->root);
  808. cluster = NULL;
  809. }
  810. }
  811. /*
  812. * We want to add any pinned extents to our free space cache
  813. * so we don't leak the space
  814. */
  815. /*
  816. * We shouldn't have switched the pinned extents yet so this is the
  817. * right one
  818. */
  819. unpin = root->fs_info->pinned_extents;
  820. if (block_group)
  821. start = block_group->key.objectid;
  822. while (block_group && (start < block_group->key.objectid +
  823. block_group->key.offset)) {
  824. ret = find_first_extent_bit(unpin, start,
  825. &extent_start, &extent_end,
  826. EXTENT_DIRTY, NULL);
  827. if (ret) {
  828. ret = 0;
  829. break;
  830. }
  831. /* This pinned extent is out of our range */
  832. if (extent_start >= block_group->key.objectid +
  833. block_group->key.offset)
  834. break;
  835. extent_start = max(extent_start, start);
  836. extent_end = min(block_group->key.objectid +
  837. block_group->key.offset, extent_end + 1);
  838. len = extent_end - extent_start;
  839. entries++;
  840. ret = io_ctl_add_entry(&io_ctl, extent_start, len, NULL);
  841. if (ret)
  842. goto out_nospc;
  843. start = extent_end;
  844. }
  845. /* Write out the bitmaps */
  846. list_for_each_safe(pos, n, &bitmap_list) {
  847. struct btrfs_free_space *entry =
  848. list_entry(pos, struct btrfs_free_space, list);
  849. ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap);
  850. if (ret)
  851. goto out_nospc;
  852. list_del_init(&entry->list);
  853. }
  854. /* Zero out the rest of the pages just to make sure */
  855. io_ctl_zero_remaining_pages(&io_ctl);
  856. ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
  857. 0, i_size_read(inode), &cached_state);
  858. io_ctl_drop_pages(&io_ctl);
  859. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  860. i_size_read(inode) - 1, &cached_state, GFP_NOFS);
  861. if (ret)
  862. goto out;
  863. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  864. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  865. key.offset = offset;
  866. key.type = 0;
  867. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  868. if (ret < 0) {
  869. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
  870. EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
  871. GFP_NOFS);
  872. goto out;
  873. }
  874. leaf = path->nodes[0];
  875. if (ret > 0) {
  876. struct btrfs_key found_key;
  877. BUG_ON(!path->slots[0]);
  878. path->slots[0]--;
  879. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  880. if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
  881. found_key.offset != offset) {
  882. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
  883. inode->i_size - 1,
  884. EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
  885. NULL, GFP_NOFS);
  886. btrfs_release_path(path);
  887. goto out;
  888. }
  889. }
  890. BTRFS_I(inode)->generation = trans->transid;
  891. header = btrfs_item_ptr(leaf, path->slots[0],
  892. struct btrfs_free_space_header);
  893. btrfs_set_free_space_entries(leaf, header, entries);
  894. btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
  895. btrfs_set_free_space_generation(leaf, header, trans->transid);
  896. btrfs_mark_buffer_dirty(leaf);
  897. btrfs_release_path(path);
  898. err = 0;
  899. out:
  900. io_ctl_free(&io_ctl);
  901. if (err) {
  902. invalidate_inode_pages2(inode->i_mapping);
  903. BTRFS_I(inode)->generation = 0;
  904. }
  905. btrfs_update_inode(trans, root, inode);
  906. return err;
  907. out_nospc:
  908. list_for_each_safe(pos, n, &bitmap_list) {
  909. struct btrfs_free_space *entry =
  910. list_entry(pos, struct btrfs_free_space, list);
  911. list_del_init(&entry->list);
  912. }
  913. io_ctl_drop_pages(&io_ctl);
  914. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  915. i_size_read(inode) - 1, &cached_state, GFP_NOFS);
  916. goto out;
  917. }
  918. int btrfs_write_out_cache(struct btrfs_root *root,
  919. struct btrfs_trans_handle *trans,
  920. struct btrfs_block_group_cache *block_group,
  921. struct btrfs_path *path)
  922. {
  923. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  924. struct inode *inode;
  925. int ret = 0;
  926. root = root->fs_info->tree_root;
  927. spin_lock(&block_group->lock);
  928. if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
  929. spin_unlock(&block_group->lock);
  930. return 0;
  931. }
  932. spin_unlock(&block_group->lock);
  933. inode = lookup_free_space_inode(root, block_group, path);
  934. if (IS_ERR(inode))
  935. return 0;
  936. ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
  937. path, block_group->key.objectid);
  938. if (ret) {
  939. spin_lock(&block_group->lock);
  940. block_group->disk_cache_state = BTRFS_DC_ERROR;
  941. spin_unlock(&block_group->lock);
  942. ret = 0;
  943. #ifdef DEBUG
  944. btrfs_err(root->fs_info,
  945. "failed to write free space cache for block group %llu",
  946. block_group->key.objectid);
  947. #endif
  948. }
  949. iput(inode);
  950. return ret;
  951. }
  952. static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
  953. u64 offset)
  954. {
  955. BUG_ON(offset < bitmap_start);
  956. offset -= bitmap_start;
  957. return (unsigned long)(div_u64(offset, unit));
  958. }
  959. static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
  960. {
  961. return (unsigned long)(div_u64(bytes, unit));
  962. }
  963. static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
  964. u64 offset)
  965. {
  966. u64 bitmap_start;
  967. u64 bytes_per_bitmap;
  968. bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
  969. bitmap_start = offset - ctl->start;
  970. bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
  971. bitmap_start *= bytes_per_bitmap;
  972. bitmap_start += ctl->start;
  973. return bitmap_start;
  974. }
  975. static int tree_insert_offset(struct rb_root *root, u64 offset,
  976. struct rb_node *node, int bitmap)
  977. {
  978. struct rb_node **p = &root->rb_node;
  979. struct rb_node *parent = NULL;
  980. struct btrfs_free_space *info;
  981. while (*p) {
  982. parent = *p;
  983. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  984. if (offset < info->offset) {
  985. p = &(*p)->rb_left;
  986. } else if (offset > info->offset) {
  987. p = &(*p)->rb_right;
  988. } else {
  989. /*
  990. * we could have a bitmap entry and an extent entry
  991. * share the same offset. If this is the case, we want
  992. * the extent entry to always be found first if we do a
  993. * linear search through the tree, since we want to have
  994. * the quickest allocation time, and allocating from an
  995. * extent is faster than allocating from a bitmap. So
  996. * if we're inserting a bitmap and we find an entry at
  997. * this offset, we want to go right, or after this entry
  998. * logically. If we are inserting an extent and we've
  999. * found a bitmap, we want to go left, or before
  1000. * logically.
  1001. */
  1002. if (bitmap) {
  1003. if (info->bitmap) {
  1004. WARN_ON_ONCE(1);
  1005. return -EEXIST;
  1006. }
  1007. p = &(*p)->rb_right;
  1008. } else {
  1009. if (!info->bitmap) {
  1010. WARN_ON_ONCE(1);
  1011. return -EEXIST;
  1012. }
  1013. p = &(*p)->rb_left;
  1014. }
  1015. }
  1016. }
  1017. rb_link_node(node, parent, p);
  1018. rb_insert_color(node, root);
  1019. return 0;
  1020. }
  1021. /*
  1022. * searches the tree for the given offset.
  1023. *
  1024. * fuzzy - If this is set, then we are trying to make an allocation, and we just
  1025. * want a section that has at least bytes size and comes at or after the given
  1026. * offset.
  1027. */
  1028. static struct btrfs_free_space *
  1029. tree_search_offset(struct btrfs_free_space_ctl *ctl,
  1030. u64 offset, int bitmap_only, int fuzzy)
  1031. {
  1032. struct rb_node *n = ctl->free_space_offset.rb_node;
  1033. struct btrfs_free_space *entry, *prev = NULL;
  1034. /* find entry that is closest to the 'offset' */
  1035. while (1) {
  1036. if (!n) {
  1037. entry = NULL;
  1038. break;
  1039. }
  1040. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1041. prev = entry;
  1042. if (offset < entry->offset)
  1043. n = n->rb_left;
  1044. else if (offset > entry->offset)
  1045. n = n->rb_right;
  1046. else
  1047. break;
  1048. }
  1049. if (bitmap_only) {
  1050. if (!entry)
  1051. return NULL;
  1052. if (entry->bitmap)
  1053. return entry;
  1054. /*
  1055. * bitmap entry and extent entry may share same offset,
  1056. * in that case, bitmap entry comes after extent entry.
  1057. */
  1058. n = rb_next(n);
  1059. if (!n)
  1060. return NULL;
  1061. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1062. if (entry->offset != offset)
  1063. return NULL;
  1064. WARN_ON(!entry->bitmap);
  1065. return entry;
  1066. } else if (entry) {
  1067. if (entry->bitmap) {
  1068. /*
  1069. * if previous extent entry covers the offset,
  1070. * we should return it instead of the bitmap entry
  1071. */
  1072. n = rb_prev(&entry->offset_index);
  1073. if (n) {
  1074. prev = rb_entry(n, struct btrfs_free_space,
  1075. offset_index);
  1076. if (!prev->bitmap &&
  1077. prev->offset + prev->bytes > offset)
  1078. entry = prev;
  1079. }
  1080. }
  1081. return entry;
  1082. }
  1083. if (!prev)
  1084. return NULL;
  1085. /* find last entry before the 'offset' */
  1086. entry = prev;
  1087. if (entry->offset > offset) {
  1088. n = rb_prev(&entry->offset_index);
  1089. if (n) {
  1090. entry = rb_entry(n, struct btrfs_free_space,
  1091. offset_index);
  1092. BUG_ON(entry->offset > offset);
  1093. } else {
  1094. if (fuzzy)
  1095. return entry;
  1096. else
  1097. return NULL;
  1098. }
  1099. }
  1100. if (entry->bitmap) {
  1101. n = rb_prev(&entry->offset_index);
  1102. if (n) {
  1103. prev = rb_entry(n, struct btrfs_free_space,
  1104. offset_index);
  1105. if (!prev->bitmap &&
  1106. prev->offset + prev->bytes > offset)
  1107. return prev;
  1108. }
  1109. if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
  1110. return entry;
  1111. } else if (entry->offset + entry->bytes > offset)
  1112. return entry;
  1113. if (!fuzzy)
  1114. return NULL;
  1115. while (1) {
  1116. if (entry->bitmap) {
  1117. if (entry->offset + BITS_PER_BITMAP *
  1118. ctl->unit > offset)
  1119. break;
  1120. } else {
  1121. if (entry->offset + entry->bytes > offset)
  1122. break;
  1123. }
  1124. n = rb_next(&entry->offset_index);
  1125. if (!n)
  1126. return NULL;
  1127. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1128. }
  1129. return entry;
  1130. }
  1131. static inline void
  1132. __unlink_free_space(struct btrfs_free_space_ctl *ctl,
  1133. struct btrfs_free_space *info)
  1134. {
  1135. rb_erase(&info->offset_index, &ctl->free_space_offset);
  1136. ctl->free_extents--;
  1137. }
  1138. static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
  1139. struct btrfs_free_space *info)
  1140. {
  1141. __unlink_free_space(ctl, info);
  1142. ctl->free_space -= info->bytes;
  1143. }
  1144. static int link_free_space(struct btrfs_free_space_ctl *ctl,
  1145. struct btrfs_free_space *info)
  1146. {
  1147. int ret = 0;
  1148. BUG_ON(!info->bitmap && !info->bytes);
  1149. ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
  1150. &info->offset_index, (info->bitmap != NULL));
  1151. if (ret)
  1152. return ret;
  1153. ctl->free_space += info->bytes;
  1154. ctl->free_extents++;
  1155. return ret;
  1156. }
  1157. static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
  1158. {
  1159. struct btrfs_block_group_cache *block_group = ctl->private;
  1160. u64 max_bytes;
  1161. u64 bitmap_bytes;
  1162. u64 extent_bytes;
  1163. u64 size = block_group->key.offset;
  1164. u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
  1165. int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
  1166. max_bitmaps = max(max_bitmaps, 1);
  1167. BUG_ON(ctl->total_bitmaps > max_bitmaps);
  1168. /*
  1169. * The goal is to keep the total amount of memory used per 1gb of space
  1170. * at or below 32k, so we need to adjust how much memory we allow to be
  1171. * used by extent based free space tracking
  1172. */
  1173. if (size < 1024 * 1024 * 1024)
  1174. max_bytes = MAX_CACHE_BYTES_PER_GIG;
  1175. else
  1176. max_bytes = MAX_CACHE_BYTES_PER_GIG *
  1177. div64_u64(size, 1024 * 1024 * 1024);
  1178. /*
  1179. * we want to account for 1 more bitmap than what we have so we can make
  1180. * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
  1181. * we add more bitmaps.
  1182. */
  1183. bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
  1184. if (bitmap_bytes >= max_bytes) {
  1185. ctl->extents_thresh = 0;
  1186. return;
  1187. }
  1188. /*
  1189. * we want the extent entry threshold to always be at most 1/2 the maxw
  1190. * bytes we can have, or whatever is less than that.
  1191. */
  1192. extent_bytes = max_bytes - bitmap_bytes;
  1193. extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
  1194. ctl->extents_thresh =
  1195. div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
  1196. }
  1197. static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  1198. struct btrfs_free_space *info,
  1199. u64 offset, u64 bytes)
  1200. {
  1201. unsigned long start, count;
  1202. start = offset_to_bit(info->offset, ctl->unit, offset);
  1203. count = bytes_to_bits(bytes, ctl->unit);
  1204. BUG_ON(start + count > BITS_PER_BITMAP);
  1205. bitmap_clear(info->bitmap, start, count);
  1206. info->bytes -= bytes;
  1207. }
  1208. static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  1209. struct btrfs_free_space *info, u64 offset,
  1210. u64 bytes)
  1211. {
  1212. __bitmap_clear_bits(ctl, info, offset, bytes);
  1213. ctl->free_space -= bytes;
  1214. }
  1215. static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
  1216. struct btrfs_free_space *info, u64 offset,
  1217. u64 bytes)
  1218. {
  1219. unsigned long start, count;
  1220. start = offset_to_bit(info->offset, ctl->unit, offset);
  1221. count = bytes_to_bits(bytes, ctl->unit);
  1222. BUG_ON(start + count > BITS_PER_BITMAP);
  1223. bitmap_set(info->bitmap, start, count);
  1224. info->bytes += bytes;
  1225. ctl->free_space += bytes;
  1226. }
  1227. static int search_bitmap(struct btrfs_free_space_ctl *ctl,
  1228. struct btrfs_free_space *bitmap_info, u64 *offset,
  1229. u64 *bytes)
  1230. {
  1231. unsigned long found_bits = 0;
  1232. unsigned long bits, i;
  1233. unsigned long next_zero;
  1234. i = offset_to_bit(bitmap_info->offset, ctl->unit,
  1235. max_t(u64, *offset, bitmap_info->offset));
  1236. bits = bytes_to_bits(*bytes, ctl->unit);
  1237. for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
  1238. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  1239. BITS_PER_BITMAP, i);
  1240. if ((next_zero - i) >= bits) {
  1241. found_bits = next_zero - i;
  1242. break;
  1243. }
  1244. i = next_zero;
  1245. }
  1246. if (found_bits) {
  1247. *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
  1248. *bytes = (u64)(found_bits) * ctl->unit;
  1249. return 0;
  1250. }
  1251. return -1;
  1252. }
  1253. static struct btrfs_free_space *
  1254. find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
  1255. unsigned long align)
  1256. {
  1257. struct btrfs_free_space *entry;
  1258. struct rb_node *node;
  1259. u64 ctl_off;
  1260. u64 tmp;
  1261. u64 align_off;
  1262. int ret;
  1263. if (!ctl->free_space_offset.rb_node)
  1264. return NULL;
  1265. entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
  1266. if (!entry)
  1267. return NULL;
  1268. for (node = &entry->offset_index; node; node = rb_next(node)) {
  1269. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1270. if (entry->bytes < *bytes)
  1271. continue;
  1272. /* make sure the space returned is big enough
  1273. * to match our requested alignment
  1274. */
  1275. if (*bytes >= align) {
  1276. ctl_off = entry->offset - ctl->start;
  1277. tmp = ctl_off + align - 1;;
  1278. do_div(tmp, align);
  1279. tmp = tmp * align + ctl->start;
  1280. align_off = tmp - entry->offset;
  1281. } else {
  1282. align_off = 0;
  1283. tmp = entry->offset;
  1284. }
  1285. if (entry->bytes < *bytes + align_off)
  1286. continue;
  1287. if (entry->bitmap) {
  1288. ret = search_bitmap(ctl, entry, &tmp, bytes);
  1289. if (!ret) {
  1290. *offset = tmp;
  1291. return entry;
  1292. }
  1293. continue;
  1294. }
  1295. *offset = tmp;
  1296. *bytes = entry->bytes - align_off;
  1297. return entry;
  1298. }
  1299. return NULL;
  1300. }
  1301. static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
  1302. struct btrfs_free_space *info, u64 offset)
  1303. {
  1304. info->offset = offset_to_bitmap(ctl, offset);
  1305. info->bytes = 0;
  1306. INIT_LIST_HEAD(&info->list);
  1307. link_free_space(ctl, info);
  1308. ctl->total_bitmaps++;
  1309. ctl->op->recalc_thresholds(ctl);
  1310. }
  1311. static void free_bitmap(struct btrfs_free_space_ctl *ctl,
  1312. struct btrfs_free_space *bitmap_info)
  1313. {
  1314. unlink_free_space(ctl, bitmap_info);
  1315. kfree(bitmap_info->bitmap);
  1316. kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
  1317. ctl->total_bitmaps--;
  1318. ctl->op->recalc_thresholds(ctl);
  1319. }
  1320. static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
  1321. struct btrfs_free_space *bitmap_info,
  1322. u64 *offset, u64 *bytes)
  1323. {
  1324. u64 end;
  1325. u64 search_start, search_bytes;
  1326. int ret;
  1327. again:
  1328. end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
  1329. /*
  1330. * We need to search for bits in this bitmap. We could only cover some
  1331. * of the extent in this bitmap thanks to how we add space, so we need
  1332. * to search for as much as it as we can and clear that amount, and then
  1333. * go searching for the next bit.
  1334. */
  1335. search_start = *offset;
  1336. search_bytes = ctl->unit;
  1337. search_bytes = min(search_bytes, end - search_start + 1);
  1338. ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
  1339. BUG_ON(ret < 0 || search_start != *offset);
  1340. /* We may have found more bits than what we need */
  1341. search_bytes = min(search_bytes, *bytes);
  1342. /* Cannot clear past the end of the bitmap */
  1343. search_bytes = min(search_bytes, end - search_start + 1);
  1344. bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
  1345. *offset += search_bytes;
  1346. *bytes -= search_bytes;
  1347. if (*bytes) {
  1348. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1349. if (!bitmap_info->bytes)
  1350. free_bitmap(ctl, bitmap_info);
  1351. /*
  1352. * no entry after this bitmap, but we still have bytes to
  1353. * remove, so something has gone wrong.
  1354. */
  1355. if (!next)
  1356. return -EINVAL;
  1357. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1358. offset_index);
  1359. /*
  1360. * if the next entry isn't a bitmap we need to return to let the
  1361. * extent stuff do its work.
  1362. */
  1363. if (!bitmap_info->bitmap)
  1364. return -EAGAIN;
  1365. /*
  1366. * Ok the next item is a bitmap, but it may not actually hold
  1367. * the information for the rest of this free space stuff, so
  1368. * look for it, and if we don't find it return so we can try
  1369. * everything over again.
  1370. */
  1371. search_start = *offset;
  1372. search_bytes = ctl->unit;
  1373. ret = search_bitmap(ctl, bitmap_info, &search_start,
  1374. &search_bytes);
  1375. if (ret < 0 || search_start != *offset)
  1376. return -EAGAIN;
  1377. goto again;
  1378. } else if (!bitmap_info->bytes)
  1379. free_bitmap(ctl, bitmap_info);
  1380. return 0;
  1381. }
  1382. static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
  1383. struct btrfs_free_space *info, u64 offset,
  1384. u64 bytes)
  1385. {
  1386. u64 bytes_to_set = 0;
  1387. u64 end;
  1388. end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
  1389. bytes_to_set = min(end - offset, bytes);
  1390. bitmap_set_bits(ctl, info, offset, bytes_to_set);
  1391. return bytes_to_set;
  1392. }
  1393. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  1394. struct btrfs_free_space *info)
  1395. {
  1396. struct btrfs_block_group_cache *block_group = ctl->private;
  1397. /*
  1398. * If we are below the extents threshold then we can add this as an
  1399. * extent, and don't have to deal with the bitmap
  1400. */
  1401. if (ctl->free_extents < ctl->extents_thresh) {
  1402. /*
  1403. * If this block group has some small extents we don't want to
  1404. * use up all of our free slots in the cache with them, we want
  1405. * to reserve them to larger extents, however if we have plent
  1406. * of cache left then go ahead an dadd them, no sense in adding
  1407. * the overhead of a bitmap if we don't have to.
  1408. */
  1409. if (info->bytes <= block_group->sectorsize * 4) {
  1410. if (ctl->free_extents * 2 <= ctl->extents_thresh)
  1411. return false;
  1412. } else {
  1413. return false;
  1414. }
  1415. }
  1416. /*
  1417. * The original block groups from mkfs can be really small, like 8
  1418. * megabytes, so don't bother with a bitmap for those entries. However
  1419. * some block groups can be smaller than what a bitmap would cover but
  1420. * are still large enough that they could overflow the 32k memory limit,
  1421. * so allow those block groups to still be allowed to have a bitmap
  1422. * entry.
  1423. */
  1424. if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
  1425. return false;
  1426. return true;
  1427. }
  1428. static struct btrfs_free_space_op free_space_op = {
  1429. .recalc_thresholds = recalculate_thresholds,
  1430. .use_bitmap = use_bitmap,
  1431. };
  1432. static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
  1433. struct btrfs_free_space *info)
  1434. {
  1435. struct btrfs_free_space *bitmap_info;
  1436. struct btrfs_block_group_cache *block_group = NULL;
  1437. int added = 0;
  1438. u64 bytes, offset, bytes_added;
  1439. int ret;
  1440. bytes = info->bytes;
  1441. offset = info->offset;
  1442. if (!ctl->op->use_bitmap(ctl, info))
  1443. return 0;
  1444. if (ctl->op == &free_space_op)
  1445. block_group = ctl->private;
  1446. again:
  1447. /*
  1448. * Since we link bitmaps right into the cluster we need to see if we
  1449. * have a cluster here, and if so and it has our bitmap we need to add
  1450. * the free space to that bitmap.
  1451. */
  1452. if (block_group && !list_empty(&block_group->cluster_list)) {
  1453. struct btrfs_free_cluster *cluster;
  1454. struct rb_node *node;
  1455. struct btrfs_free_space *entry;
  1456. cluster = list_entry(block_group->cluster_list.next,
  1457. struct btrfs_free_cluster,
  1458. block_group_list);
  1459. spin_lock(&cluster->lock);
  1460. node = rb_first(&cluster->root);
  1461. if (!node) {
  1462. spin_unlock(&cluster->lock);
  1463. goto no_cluster_bitmap;
  1464. }
  1465. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1466. if (!entry->bitmap) {
  1467. spin_unlock(&cluster->lock);
  1468. goto no_cluster_bitmap;
  1469. }
  1470. if (entry->offset == offset_to_bitmap(ctl, offset)) {
  1471. bytes_added = add_bytes_to_bitmap(ctl, entry,
  1472. offset, bytes);
  1473. bytes -= bytes_added;
  1474. offset += bytes_added;
  1475. }
  1476. spin_unlock(&cluster->lock);
  1477. if (!bytes) {
  1478. ret = 1;
  1479. goto out;
  1480. }
  1481. }
  1482. no_cluster_bitmap:
  1483. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  1484. 1, 0);
  1485. if (!bitmap_info) {
  1486. BUG_ON(added);
  1487. goto new_bitmap;
  1488. }
  1489. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
  1490. bytes -= bytes_added;
  1491. offset += bytes_added;
  1492. added = 0;
  1493. if (!bytes) {
  1494. ret = 1;
  1495. goto out;
  1496. } else
  1497. goto again;
  1498. new_bitmap:
  1499. if (info && info->bitmap) {
  1500. add_new_bitmap(ctl, info, offset);
  1501. added = 1;
  1502. info = NULL;
  1503. goto again;
  1504. } else {
  1505. spin_unlock(&ctl->tree_lock);
  1506. /* no pre-allocated info, allocate a new one */
  1507. if (!info) {
  1508. info = kmem_cache_zalloc(btrfs_free_space_cachep,
  1509. GFP_NOFS);
  1510. if (!info) {
  1511. spin_lock(&ctl->tree_lock);
  1512. ret = -ENOMEM;
  1513. goto out;
  1514. }
  1515. }
  1516. /* allocate the bitmap */
  1517. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1518. spin_lock(&ctl->tree_lock);
  1519. if (!info->bitmap) {
  1520. ret = -ENOMEM;
  1521. goto out;
  1522. }
  1523. goto again;
  1524. }
  1525. out:
  1526. if (info) {
  1527. if (info->bitmap)
  1528. kfree(info->bitmap);
  1529. kmem_cache_free(btrfs_free_space_cachep, info);
  1530. }
  1531. return ret;
  1532. }
  1533. static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
  1534. struct btrfs_free_space *info, bool update_stat)
  1535. {
  1536. struct btrfs_free_space *left_info;
  1537. struct btrfs_free_space *right_info;
  1538. bool merged = false;
  1539. u64 offset = info->offset;
  1540. u64 bytes = info->bytes;
  1541. /*
  1542. * first we want to see if there is free space adjacent to the range we
  1543. * are adding, if there is remove that struct and add a new one to
  1544. * cover the entire range
  1545. */
  1546. right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
  1547. if (right_info && rb_prev(&right_info->offset_index))
  1548. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1549. struct btrfs_free_space, offset_index);
  1550. else
  1551. left_info = tree_search_offset(ctl, offset - 1, 0, 0);
  1552. if (right_info && !right_info->bitmap) {
  1553. if (update_stat)
  1554. unlink_free_space(ctl, right_info);
  1555. else
  1556. __unlink_free_space(ctl, right_info);
  1557. info->bytes += right_info->bytes;
  1558. kmem_cache_free(btrfs_free_space_cachep, right_info);
  1559. merged = true;
  1560. }
  1561. if (left_info && !left_info->bitmap &&
  1562. left_info->offset + left_info->bytes == offset) {
  1563. if (update_stat)
  1564. unlink_free_space(ctl, left_info);
  1565. else
  1566. __unlink_free_space(ctl, left_info);
  1567. info->offset = left_info->offset;
  1568. info->bytes += left_info->bytes;
  1569. kmem_cache_free(btrfs_free_space_cachep, left_info);
  1570. merged = true;
  1571. }
  1572. return merged;
  1573. }
  1574. int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
  1575. u64 offset, u64 bytes)
  1576. {
  1577. struct btrfs_free_space *info;
  1578. int ret = 0;
  1579. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  1580. if (!info)
  1581. return -ENOMEM;
  1582. info->offset = offset;
  1583. info->bytes = bytes;
  1584. spin_lock(&ctl->tree_lock);
  1585. if (try_merge_free_space(ctl, info, true))
  1586. goto link;
  1587. /*
  1588. * There was no extent directly to the left or right of this new
  1589. * extent then we know we're going to have to allocate a new extent, so
  1590. * before we do that see if we need to drop this into a bitmap
  1591. */
  1592. ret = insert_into_bitmap(ctl, info);
  1593. if (ret < 0) {
  1594. goto out;
  1595. } else if (ret) {
  1596. ret = 0;
  1597. goto out;
  1598. }
  1599. link:
  1600. ret = link_free_space(ctl, info);
  1601. if (ret)
  1602. kmem_cache_free(btrfs_free_space_cachep, info);
  1603. out:
  1604. spin_unlock(&ctl->tree_lock);
  1605. if (ret) {
  1606. printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
  1607. BUG_ON(ret == -EEXIST);
  1608. }
  1609. return ret;
  1610. }
  1611. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1612. u64 offset, u64 bytes)
  1613. {
  1614. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1615. struct btrfs_free_space *info;
  1616. int ret;
  1617. bool re_search = false;
  1618. spin_lock(&ctl->tree_lock);
  1619. again:
  1620. ret = 0;
  1621. if (!bytes)
  1622. goto out_lock;
  1623. info = tree_search_offset(ctl, offset, 0, 0);
  1624. if (!info) {
  1625. /*
  1626. * oops didn't find an extent that matched the space we wanted
  1627. * to remove, look for a bitmap instead
  1628. */
  1629. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  1630. 1, 0);
  1631. if (!info) {
  1632. /*
  1633. * If we found a partial bit of our free space in a
  1634. * bitmap but then couldn't find the other part this may
  1635. * be a problem, so WARN about it.
  1636. */
  1637. WARN_ON(re_search);
  1638. goto out_lock;
  1639. }
  1640. }
  1641. re_search = false;
  1642. if (!info->bitmap) {
  1643. unlink_free_space(ctl, info);
  1644. if (offset == info->offset) {
  1645. u64 to_free = min(bytes, info->bytes);
  1646. info->bytes -= to_free;
  1647. info->offset += to_free;
  1648. if (info->bytes) {
  1649. ret = link_free_space(ctl, info);
  1650. WARN_ON(ret);
  1651. } else {
  1652. kmem_cache_free(btrfs_free_space_cachep, info);
  1653. }
  1654. offset += to_free;
  1655. bytes -= to_free;
  1656. goto again;
  1657. } else {
  1658. u64 old_end = info->bytes + info->offset;
  1659. info->bytes = offset - info->offset;
  1660. ret = link_free_space(ctl, info);
  1661. WARN_ON(ret);
  1662. if (ret)
  1663. goto out_lock;
  1664. /* Not enough bytes in this entry to satisfy us */
  1665. if (old_end < offset + bytes) {
  1666. bytes -= old_end - offset;
  1667. offset = old_end;
  1668. goto again;
  1669. } else if (old_end == offset + bytes) {
  1670. /* all done */
  1671. goto out_lock;
  1672. }
  1673. spin_unlock(&ctl->tree_lock);
  1674. ret = btrfs_add_free_space(block_group, offset + bytes,
  1675. old_end - (offset + bytes));
  1676. WARN_ON(ret);
  1677. goto out;
  1678. }
  1679. }
  1680. ret = remove_from_bitmap(ctl, info, &offset, &bytes);
  1681. if (ret == -EAGAIN) {
  1682. re_search = true;
  1683. goto again;
  1684. }
  1685. BUG_ON(ret); /* logic error */
  1686. out_lock:
  1687. spin_unlock(&ctl->tree_lock);
  1688. out:
  1689. return ret;
  1690. }
  1691. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  1692. u64 bytes)
  1693. {
  1694. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1695. struct btrfs_free_space *info;
  1696. struct rb_node *n;
  1697. int count = 0;
  1698. for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
  1699. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1700. if (info->bytes >= bytes && !block_group->ro)
  1701. count++;
  1702. printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
  1703. (unsigned long long)info->offset,
  1704. (unsigned long long)info->bytes,
  1705. (info->bitmap) ? "yes" : "no");
  1706. }
  1707. printk(KERN_INFO "block group has cluster?: %s\n",
  1708. list_empty(&block_group->cluster_list) ? "no" : "yes");
  1709. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  1710. "\n", count);
  1711. }
  1712. void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
  1713. {
  1714. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1715. spin_lock_init(&ctl->tree_lock);
  1716. ctl->unit = block_group->sectorsize;
  1717. ctl->start = block_group->key.objectid;
  1718. ctl->private = block_group;
  1719. ctl->op = &free_space_op;
  1720. /*
  1721. * we only want to have 32k of ram per block group for keeping
  1722. * track of free space, and if we pass 1/2 of that we want to
  1723. * start converting things over to using bitmaps
  1724. */
  1725. ctl->extents_thresh = ((1024 * 32) / 2) /
  1726. sizeof(struct btrfs_free_space);
  1727. }
  1728. /*
  1729. * for a given cluster, put all of its extents back into the free
  1730. * space cache. If the block group passed doesn't match the block group
  1731. * pointed to by the cluster, someone else raced in and freed the
  1732. * cluster already. In that case, we just return without changing anything
  1733. */
  1734. static int
  1735. __btrfs_return_cluster_to_free_space(
  1736. struct btrfs_block_group_cache *block_group,
  1737. struct btrfs_free_cluster *cluster)
  1738. {
  1739. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1740. struct btrfs_free_space *entry;
  1741. struct rb_node *node;
  1742. spin_lock(&cluster->lock);
  1743. if (cluster->block_group != block_group)
  1744. goto out;
  1745. cluster->block_group = NULL;
  1746. cluster->window_start = 0;
  1747. list_del_init(&cluster->block_group_list);
  1748. node = rb_first(&cluster->root);
  1749. while (node) {
  1750. bool bitmap;
  1751. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1752. node = rb_next(&entry->offset_index);
  1753. rb_erase(&entry->offset_index, &cluster->root);
  1754. bitmap = (entry->bitmap != NULL);
  1755. if (!bitmap)
  1756. try_merge_free_space(ctl, entry, false);
  1757. tree_insert_offset(&ctl->free_space_offset,
  1758. entry->offset, &entry->offset_index, bitmap);
  1759. }
  1760. cluster->root = RB_ROOT;
  1761. out:
  1762. spin_unlock(&cluster->lock);
  1763. btrfs_put_block_group(block_group);
  1764. return 0;
  1765. }
  1766. void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
  1767. {
  1768. struct btrfs_free_space *info;
  1769. struct rb_node *node;
  1770. while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
  1771. info = rb_entry(node, struct btrfs_free_space, offset_index);
  1772. if (!info->bitmap) {
  1773. unlink_free_space(ctl, info);
  1774. kmem_cache_free(btrfs_free_space_cachep, info);
  1775. } else {
  1776. free_bitmap(ctl, info);
  1777. }
  1778. if (need_resched()) {
  1779. spin_unlock(&ctl->tree_lock);
  1780. cond_resched();
  1781. spin_lock(&ctl->tree_lock);
  1782. }
  1783. }
  1784. }
  1785. void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
  1786. {
  1787. spin_lock(&ctl->tree_lock);
  1788. __btrfs_remove_free_space_cache_locked(ctl);
  1789. spin_unlock(&ctl->tree_lock);
  1790. }
  1791. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  1792. {
  1793. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1794. struct btrfs_free_cluster *cluster;
  1795. struct list_head *head;
  1796. spin_lock(&ctl->tree_lock);
  1797. while ((head = block_group->cluster_list.next) !=
  1798. &block_group->cluster_list) {
  1799. cluster = list_entry(head, struct btrfs_free_cluster,
  1800. block_group_list);
  1801. WARN_ON(cluster->block_group != block_group);
  1802. __btrfs_return_cluster_to_free_space(block_group, cluster);
  1803. if (need_resched()) {
  1804. spin_unlock(&ctl->tree_lock);
  1805. cond_resched();
  1806. spin_lock(&ctl->tree_lock);
  1807. }
  1808. }
  1809. __btrfs_remove_free_space_cache_locked(ctl);
  1810. spin_unlock(&ctl->tree_lock);
  1811. }
  1812. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  1813. u64 offset, u64 bytes, u64 empty_size)
  1814. {
  1815. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1816. struct btrfs_free_space *entry = NULL;
  1817. u64 bytes_search = bytes + empty_size;
  1818. u64 ret = 0;
  1819. u64 align_gap = 0;
  1820. u64 align_gap_len = 0;
  1821. spin_lock(&ctl->tree_lock);
  1822. entry = find_free_space(ctl, &offset, &bytes_search,
  1823. block_group->full_stripe_len);
  1824. if (!entry)
  1825. goto out;
  1826. ret = offset;
  1827. if (entry->bitmap) {
  1828. bitmap_clear_bits(ctl, entry, offset, bytes);
  1829. if (!entry->bytes)
  1830. free_bitmap(ctl, entry);
  1831. } else {
  1832. unlink_free_space(ctl, entry);
  1833. align_gap_len = offset - entry->offset;
  1834. align_gap = entry->offset;
  1835. entry->offset = offset + bytes;
  1836. WARN_ON(entry->bytes < bytes + align_gap_len);
  1837. entry->bytes -= bytes + align_gap_len;
  1838. if (!entry->bytes)
  1839. kmem_cache_free(btrfs_free_space_cachep, entry);
  1840. else
  1841. link_free_space(ctl, entry);
  1842. }
  1843. out:
  1844. spin_unlock(&ctl->tree_lock);
  1845. if (align_gap_len)
  1846. __btrfs_add_free_space(ctl, align_gap, align_gap_len);
  1847. return ret;
  1848. }
  1849. /*
  1850. * given a cluster, put all of its extents back into the free space
  1851. * cache. If a block group is passed, this function will only free
  1852. * a cluster that belongs to the passed block group.
  1853. *
  1854. * Otherwise, it'll get a reference on the block group pointed to by the
  1855. * cluster and remove the cluster from it.
  1856. */
  1857. int btrfs_return_cluster_to_free_space(
  1858. struct btrfs_block_group_cache *block_group,
  1859. struct btrfs_free_cluster *cluster)
  1860. {
  1861. struct btrfs_free_space_ctl *ctl;
  1862. int ret;
  1863. /* first, get a safe pointer to the block group */
  1864. spin_lock(&cluster->lock);
  1865. if (!block_group) {
  1866. block_group = cluster->block_group;
  1867. if (!block_group) {
  1868. spin_unlock(&cluster->lock);
  1869. return 0;
  1870. }
  1871. } else if (cluster->block_group != block_group) {
  1872. /* someone else has already freed it don't redo their work */
  1873. spin_unlock(&cluster->lock);
  1874. return 0;
  1875. }
  1876. atomic_inc(&block_group->count);
  1877. spin_unlock(&cluster->lock);
  1878. ctl = block_group->free_space_ctl;
  1879. /* now return any extents the cluster had on it */
  1880. spin_lock(&ctl->tree_lock);
  1881. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  1882. spin_unlock(&ctl->tree_lock);
  1883. /* finally drop our ref */
  1884. btrfs_put_block_group(block_group);
  1885. return ret;
  1886. }
  1887. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  1888. struct btrfs_free_cluster *cluster,
  1889. struct btrfs_free_space *entry,
  1890. u64 bytes, u64 min_start)
  1891. {
  1892. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1893. int err;
  1894. u64 search_start = cluster->window_start;
  1895. u64 search_bytes = bytes;
  1896. u64 ret = 0;
  1897. search_start = min_start;
  1898. search_bytes = bytes;
  1899. err = search_bitmap(ctl, entry, &search_start, &search_bytes);
  1900. if (err)
  1901. return 0;
  1902. ret = search_start;
  1903. __bitmap_clear_bits(ctl, entry, ret, bytes);
  1904. return ret;
  1905. }
  1906. /*
  1907. * given a cluster, try to allocate 'bytes' from it, returns 0
  1908. * if it couldn't find anything suitably large, or a logical disk offset
  1909. * if things worked out
  1910. */
  1911. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  1912. struct btrfs_free_cluster *cluster, u64 bytes,
  1913. u64 min_start)
  1914. {
  1915. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1916. struct btrfs_free_space *entry = NULL;
  1917. struct rb_node *node;
  1918. u64 ret = 0;
  1919. spin_lock(&cluster->lock);
  1920. if (bytes > cluster->max_size)
  1921. goto out;
  1922. if (cluster->block_group != block_group)
  1923. goto out;
  1924. node = rb_first(&cluster->root);
  1925. if (!node)
  1926. goto out;
  1927. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1928. while(1) {
  1929. if (entry->bytes < bytes ||
  1930. (!entry->bitmap && entry->offset < min_start)) {
  1931. node = rb_next(&entry->offset_index);
  1932. if (!node)
  1933. break;
  1934. entry = rb_entry(node, struct btrfs_free_space,
  1935. offset_index);
  1936. continue;
  1937. }
  1938. if (entry->bitmap) {
  1939. ret = btrfs_alloc_from_bitmap(block_group,
  1940. cluster, entry, bytes,
  1941. cluster->window_start);
  1942. if (ret == 0) {
  1943. node = rb_next(&entry->offset_index);
  1944. if (!node)
  1945. break;
  1946. entry = rb_entry(node, struct btrfs_free_space,
  1947. offset_index);
  1948. continue;
  1949. }
  1950. cluster->window_start += bytes;
  1951. } else {
  1952. ret = entry->offset;
  1953. entry->offset += bytes;
  1954. entry->bytes -= bytes;
  1955. }
  1956. if (entry->bytes == 0)
  1957. rb_erase(&entry->offset_index, &cluster->root);
  1958. break;
  1959. }
  1960. out:
  1961. spin_unlock(&cluster->lock);
  1962. if (!ret)
  1963. return 0;
  1964. spin_lock(&ctl->tree_lock);
  1965. ctl->free_space -= bytes;
  1966. if (entry->bytes == 0) {
  1967. ctl->free_extents--;
  1968. if (entry->bitmap) {
  1969. kfree(entry->bitmap);
  1970. ctl->total_bitmaps--;
  1971. ctl->op->recalc_thresholds(ctl);
  1972. }
  1973. kmem_cache_free(btrfs_free_space_cachep, entry);
  1974. }
  1975. spin_unlock(&ctl->tree_lock);
  1976. return ret;
  1977. }
  1978. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  1979. struct btrfs_free_space *entry,
  1980. struct btrfs_free_cluster *cluster,
  1981. u64 offset, u64 bytes,
  1982. u64 cont1_bytes, u64 min_bytes)
  1983. {
  1984. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1985. unsigned long next_zero;
  1986. unsigned long i;
  1987. unsigned long want_bits;
  1988. unsigned long min_bits;
  1989. unsigned long found_bits;
  1990. unsigned long start = 0;
  1991. unsigned long total_found = 0;
  1992. int ret;
  1993. i = offset_to_bit(entry->offset, ctl->unit,
  1994. max_t(u64, offset, entry->offset));
  1995. want_bits = bytes_to_bits(bytes, ctl->unit);
  1996. min_bits = bytes_to_bits(min_bytes, ctl->unit);
  1997. again:
  1998. found_bits = 0;
  1999. for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
  2000. next_zero = find_next_zero_bit(entry->bitmap,
  2001. BITS_PER_BITMAP, i);
  2002. if (next_zero - i >= min_bits) {
  2003. found_bits = next_zero - i;
  2004. break;
  2005. }
  2006. i = next_zero;
  2007. }
  2008. if (!found_bits)
  2009. return -ENOSPC;
  2010. if (!total_found) {
  2011. start = i;
  2012. cluster->max_size = 0;
  2013. }
  2014. total_found += found_bits;
  2015. if (cluster->max_size < found_bits * ctl->unit)
  2016. cluster->max_size = found_bits * ctl->unit;
  2017. if (total_found < want_bits || cluster->max_size < cont1_bytes) {
  2018. i = next_zero + 1;
  2019. goto again;
  2020. }
  2021. cluster->window_start = start * ctl->unit + entry->offset;
  2022. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  2023. ret = tree_insert_offset(&cluster->root, entry->offset,
  2024. &entry->offset_index, 1);
  2025. BUG_ON(ret); /* -EEXIST; Logic error */
  2026. trace_btrfs_setup_cluster(block_group, cluster,
  2027. total_found * ctl->unit, 1);
  2028. return 0;
  2029. }
  2030. /*
  2031. * This searches the block group for just extents to fill the cluster with.
  2032. * Try to find a cluster with at least bytes total bytes, at least one
  2033. * extent of cont1_bytes, and other clusters of at least min_bytes.
  2034. */
  2035. static noinline int
  2036. setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
  2037. struct btrfs_free_cluster *cluster,
  2038. struct list_head *bitmaps, u64 offset, u64 bytes,
  2039. u64 cont1_bytes, u64 min_bytes)
  2040. {
  2041. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2042. struct btrfs_free_space *first = NULL;
  2043. struct btrfs_free_space *entry = NULL;
  2044. struct btrfs_free_space *last;
  2045. struct rb_node *node;
  2046. u64 window_start;
  2047. u64 window_free;
  2048. u64 max_extent;
  2049. u64 total_size = 0;
  2050. entry = tree_search_offset(ctl, offset, 0, 1);
  2051. if (!entry)
  2052. return -ENOSPC;
  2053. /*
  2054. * We don't want bitmaps, so just move along until we find a normal
  2055. * extent entry.
  2056. */
  2057. while (entry->bitmap || entry->bytes < min_bytes) {
  2058. if (entry->bitmap && list_empty(&entry->list))
  2059. list_add_tail(&entry->list, bitmaps);
  2060. node = rb_next(&entry->offset_index);
  2061. if (!node)
  2062. return -ENOSPC;
  2063. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2064. }
  2065. window_start = entry->offset;
  2066. window_free = entry->bytes;
  2067. max_extent = entry->bytes;
  2068. first = entry;
  2069. last = entry;
  2070. for (node = rb_next(&entry->offset_index); node;
  2071. node = rb_next(&entry->offset_index)) {
  2072. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2073. if (entry->bitmap) {
  2074. if (list_empty(&entry->list))
  2075. list_add_tail(&entry->list, bitmaps);
  2076. continue;
  2077. }
  2078. if (entry->bytes < min_bytes)
  2079. continue;
  2080. last = entry;
  2081. window_free += entry->bytes;
  2082. if (entry->bytes > max_extent)
  2083. max_extent = entry->bytes;
  2084. }
  2085. if (window_free < bytes || max_extent < cont1_bytes)
  2086. return -ENOSPC;
  2087. cluster->window_start = first->offset;
  2088. node = &first->offset_index;
  2089. /*
  2090. * now we've found our entries, pull them out of the free space
  2091. * cache and put them into the cluster rbtree
  2092. */
  2093. do {
  2094. int ret;
  2095. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2096. node = rb_next(&entry->offset_index);
  2097. if (entry->bitmap || entry->bytes < min_bytes)
  2098. continue;
  2099. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  2100. ret = tree_insert_offset(&cluster->root, entry->offset,
  2101. &entry->offset_index, 0);
  2102. total_size += entry->bytes;
  2103. BUG_ON(ret); /* -EEXIST; Logic error */
  2104. } while (node && entry != last);
  2105. cluster->max_size = max_extent;
  2106. trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
  2107. return 0;
  2108. }
  2109. /*
  2110. * This specifically looks for bitmaps that may work in the cluster, we assume
  2111. * that we have already failed to find extents that will work.
  2112. */
  2113. static noinline int
  2114. setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
  2115. struct btrfs_free_cluster *cluster,
  2116. struct list_head *bitmaps, u64 offset, u64 bytes,
  2117. u64 cont1_bytes, u64 min_bytes)
  2118. {
  2119. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2120. struct btrfs_free_space *entry;
  2121. int ret = -ENOSPC;
  2122. u64 bitmap_offset = offset_to_bitmap(ctl, offset);
  2123. if (ctl->total_bitmaps == 0)
  2124. return -ENOSPC;
  2125. /*
  2126. * The bitmap that covers offset won't be in the list unless offset
  2127. * is just its start offset.
  2128. */
  2129. entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
  2130. if (entry->offset != bitmap_offset) {
  2131. entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
  2132. if (entry && list_empty(&entry->list))
  2133. list_add(&entry->list, bitmaps);
  2134. }
  2135. list_for_each_entry(entry, bitmaps, list) {
  2136. if (entry->bytes < bytes)
  2137. continue;
  2138. ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
  2139. bytes, cont1_bytes, min_bytes);
  2140. if (!ret)
  2141. return 0;
  2142. }
  2143. /*
  2144. * The bitmaps list has all the bitmaps that record free space
  2145. * starting after offset, so no more search is required.
  2146. */
  2147. return -ENOSPC;
  2148. }
  2149. /*
  2150. * here we try to find a cluster of blocks in a block group. The goal
  2151. * is to find at least bytes+empty_size.
  2152. * We might not find them all in one contiguous area.
  2153. *
  2154. * returns zero and sets up cluster if things worked out, otherwise
  2155. * it returns -enospc
  2156. */
  2157. int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
  2158. struct btrfs_root *root,
  2159. struct btrfs_block_group_cache *block_group,
  2160. struct btrfs_free_cluster *cluster,
  2161. u64 offset, u64 bytes, u64 empty_size)
  2162. {
  2163. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2164. struct btrfs_free_space *entry, *tmp;
  2165. LIST_HEAD(bitmaps);
  2166. u64 min_bytes;
  2167. u64 cont1_bytes;
  2168. int ret;
  2169. /*
  2170. * Choose the minimum extent size we'll require for this
  2171. * cluster. For SSD_SPREAD, don't allow any fragmentation.
  2172. * For metadata, allow allocates with smaller extents. For
  2173. * data, keep it dense.
  2174. */
  2175. if (btrfs_test_opt(root, SSD_SPREAD)) {
  2176. cont1_bytes = min_bytes = bytes + empty_size;
  2177. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  2178. cont1_bytes = bytes;
  2179. min_bytes = block_group->sectorsize;
  2180. } else {
  2181. cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
  2182. min_bytes = block_group->sectorsize;
  2183. }
  2184. spin_lock(&ctl->tree_lock);
  2185. /*
  2186. * If we know we don't have enough space to make a cluster don't even
  2187. * bother doing all the work to try and find one.
  2188. */
  2189. if (ctl->free_space < bytes) {
  2190. spin_unlock(&ctl->tree_lock);
  2191. return -ENOSPC;
  2192. }
  2193. spin_lock(&cluster->lock);
  2194. /* someone already found a cluster, hooray */
  2195. if (cluster->block_group) {
  2196. ret = 0;
  2197. goto out;
  2198. }
  2199. trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
  2200. min_bytes);
  2201. INIT_LIST_HEAD(&bitmaps);
  2202. ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
  2203. bytes + empty_size,
  2204. cont1_bytes, min_bytes);
  2205. if (ret)
  2206. ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
  2207. offset, bytes + empty_size,
  2208. cont1_bytes, min_bytes);
  2209. /* Clear our temporary list */
  2210. list_for_each_entry_safe(entry, tmp, &bitmaps, list)
  2211. list_del_init(&entry->list);
  2212. if (!ret) {
  2213. atomic_inc(&block_group->count);
  2214. list_add_tail(&cluster->block_group_list,
  2215. &block_group->cluster_list);
  2216. cluster->block_group = block_group;
  2217. } else {
  2218. trace_btrfs_failed_cluster_setup(block_group);
  2219. }
  2220. out:
  2221. spin_unlock(&cluster->lock);
  2222. spin_unlock(&ctl->tree_lock);
  2223. return ret;
  2224. }
  2225. /*
  2226. * simple code to zero out a cluster
  2227. */
  2228. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  2229. {
  2230. spin_lock_init(&cluster->lock);
  2231. spin_lock_init(&cluster->refill_lock);
  2232. cluster->root = RB_ROOT;
  2233. cluster->max_size = 0;
  2234. INIT_LIST_HEAD(&cluster->block_group_list);
  2235. cluster->block_group = NULL;
  2236. }
  2237. static int do_trimming(struct btrfs_block_group_cache *block_group,
  2238. u64 *total_trimmed, u64 start, u64 bytes,
  2239. u64 reserved_start, u64 reserved_bytes)
  2240. {
  2241. struct btrfs_space_info *space_info = block_group->space_info;
  2242. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2243. int ret;
  2244. int update = 0;
  2245. u64 trimmed = 0;
  2246. spin_lock(&space_info->lock);
  2247. spin_lock(&block_group->lock);
  2248. if (!block_group->ro) {
  2249. block_group->reserved += reserved_bytes;
  2250. space_info->bytes_reserved += reserved_bytes;
  2251. update = 1;
  2252. }
  2253. spin_unlock(&block_group->lock);
  2254. spin_unlock(&space_info->lock);
  2255. ret = btrfs_error_discard_extent(fs_info->extent_root,
  2256. start, bytes, &trimmed);
  2257. if (!ret)
  2258. *total_trimmed += trimmed;
  2259. btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
  2260. if (update) {
  2261. spin_lock(&space_info->lock);
  2262. spin_lock(&block_group->lock);
  2263. if (block_group->ro)
  2264. space_info->bytes_readonly += reserved_bytes;
  2265. block_group->reserved -= reserved_bytes;
  2266. space_info->bytes_reserved -= reserved_bytes;
  2267. spin_unlock(&space_info->lock);
  2268. spin_unlock(&block_group->lock);
  2269. }
  2270. return ret;
  2271. }
  2272. static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
  2273. u64 *total_trimmed, u64 start, u64 end, u64 minlen)
  2274. {
  2275. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2276. struct btrfs_free_space *entry;
  2277. struct rb_node *node;
  2278. int ret = 0;
  2279. u64 extent_start;
  2280. u64 extent_bytes;
  2281. u64 bytes;
  2282. while (start < end) {
  2283. spin_lock(&ctl->tree_lock);
  2284. if (ctl->free_space < minlen) {
  2285. spin_unlock(&ctl->tree_lock);
  2286. break;
  2287. }
  2288. entry = tree_search_offset(ctl, start, 0, 1);
  2289. if (!entry) {
  2290. spin_unlock(&ctl->tree_lock);
  2291. break;
  2292. }
  2293. /* skip bitmaps */
  2294. while (entry->bitmap) {
  2295. node = rb_next(&entry->offset_index);
  2296. if (!node) {
  2297. spin_unlock(&ctl->tree_lock);
  2298. goto out;
  2299. }
  2300. entry = rb_entry(node, struct btrfs_free_space,
  2301. offset_index);
  2302. }
  2303. if (entry->offset >= end) {
  2304. spin_unlock(&ctl->tree_lock);
  2305. break;
  2306. }
  2307. extent_start = entry->offset;
  2308. extent_bytes = entry->bytes;
  2309. start = max(start, extent_start);
  2310. bytes = min(extent_start + extent_bytes, end) - start;
  2311. if (bytes < minlen) {
  2312. spin_unlock(&ctl->tree_lock);
  2313. goto next;
  2314. }
  2315. unlink_free_space(ctl, entry);
  2316. kmem_cache_free(btrfs_free_space_cachep, entry);
  2317. spin_unlock(&ctl->tree_lock);
  2318. ret = do_trimming(block_group, total_trimmed, start, bytes,
  2319. extent_start, extent_bytes);
  2320. if (ret)
  2321. break;
  2322. next:
  2323. start += bytes;
  2324. if (fatal_signal_pending(current)) {
  2325. ret = -ERESTARTSYS;
  2326. break;
  2327. }
  2328. cond_resched();
  2329. }
  2330. out:
  2331. return ret;
  2332. }
  2333. static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
  2334. u64 *total_trimmed, u64 start, u64 end, u64 minlen)
  2335. {
  2336. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2337. struct btrfs_free_space *entry;
  2338. int ret = 0;
  2339. int ret2;
  2340. u64 bytes;
  2341. u64 offset = offset_to_bitmap(ctl, start);
  2342. while (offset < end) {
  2343. bool next_bitmap = false;
  2344. spin_lock(&ctl->tree_lock);
  2345. if (ctl->free_space < minlen) {
  2346. spin_unlock(&ctl->tree_lock);
  2347. break;
  2348. }
  2349. entry = tree_search_offset(ctl, offset, 1, 0);
  2350. if (!entry) {
  2351. spin_unlock(&ctl->tree_lock);
  2352. next_bitmap = true;
  2353. goto next;
  2354. }
  2355. bytes = minlen;
  2356. ret2 = search_bitmap(ctl, entry, &start, &bytes);
  2357. if (ret2 || start >= end) {
  2358. spin_unlock(&ctl->tree_lock);
  2359. next_bitmap = true;
  2360. goto next;
  2361. }
  2362. bytes = min(bytes, end - start);
  2363. if (bytes < minlen) {
  2364. spin_unlock(&ctl->tree_lock);
  2365. goto next;
  2366. }
  2367. bitmap_clear_bits(ctl, entry, start, bytes);
  2368. if (entry->bytes == 0)
  2369. free_bitmap(ctl, entry);
  2370. spin_unlock(&ctl->tree_lock);
  2371. ret = do_trimming(block_group, total_trimmed, start, bytes,
  2372. start, bytes);
  2373. if (ret)
  2374. break;
  2375. next:
  2376. if (next_bitmap) {
  2377. offset += BITS_PER_BITMAP * ctl->unit;
  2378. } else {
  2379. start += bytes;
  2380. if (start >= offset + BITS_PER_BITMAP * ctl->unit)
  2381. offset += BITS_PER_BITMAP * ctl->unit;
  2382. }
  2383. if (fatal_signal_pending(current)) {
  2384. ret = -ERESTARTSYS;
  2385. break;
  2386. }
  2387. cond_resched();
  2388. }
  2389. return ret;
  2390. }
  2391. int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
  2392. u64 *trimmed, u64 start, u64 end, u64 minlen)
  2393. {
  2394. int ret;
  2395. *trimmed = 0;
  2396. ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
  2397. if (ret)
  2398. return ret;
  2399. ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
  2400. return ret;
  2401. }
  2402. /*
  2403. * Find the left-most item in the cache tree, and then return the
  2404. * smallest inode number in the item.
  2405. *
  2406. * Note: the returned inode number may not be the smallest one in
  2407. * the tree, if the left-most item is a bitmap.
  2408. */
  2409. u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
  2410. {
  2411. struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
  2412. struct btrfs_free_space *entry = NULL;
  2413. u64 ino = 0;
  2414. spin_lock(&ctl->tree_lock);
  2415. if (RB_EMPTY_ROOT(&ctl->free_space_offset))
  2416. goto out;
  2417. entry = rb_entry(rb_first(&ctl->free_space_offset),
  2418. struct btrfs_free_space, offset_index);
  2419. if (!entry->bitmap) {
  2420. ino = entry->offset;
  2421. unlink_free_space(ctl, entry);
  2422. entry->offset++;
  2423. entry->bytes--;
  2424. if (!entry->bytes)
  2425. kmem_cache_free(btrfs_free_space_cachep, entry);
  2426. else
  2427. link_free_space(ctl, entry);
  2428. } else {
  2429. u64 offset = 0;
  2430. u64 count = 1;
  2431. int ret;
  2432. ret = search_bitmap(ctl, entry, &offset, &count);
  2433. /* Logic error; Should be empty if it can't find anything */
  2434. BUG_ON(ret);
  2435. ino = offset;
  2436. bitmap_clear_bits(ctl, entry, offset, 1);
  2437. if (entry->bytes == 0)
  2438. free_bitmap(ctl, entry);
  2439. }
  2440. out:
  2441. spin_unlock(&ctl->tree_lock);
  2442. return ino;
  2443. }
  2444. struct inode *lookup_free_ino_inode(struct btrfs_root *root,
  2445. struct btrfs_path *path)
  2446. {
  2447. struct inode *inode = NULL;
  2448. spin_lock(&root->cache_lock);
  2449. if (root->cache_inode)
  2450. inode = igrab(root->cache_inode);
  2451. spin_unlock(&root->cache_lock);
  2452. if (inode)
  2453. return inode;
  2454. inode = __lookup_free_space_inode(root, path, 0);
  2455. if (IS_ERR(inode))
  2456. return inode;
  2457. spin_lock(&root->cache_lock);
  2458. if (!btrfs_fs_closing(root->fs_info))
  2459. root->cache_inode = igrab(inode);
  2460. spin_unlock(&root->cache_lock);
  2461. return inode;
  2462. }
  2463. int create_free_ino_inode(struct btrfs_root *root,
  2464. struct btrfs_trans_handle *trans,
  2465. struct btrfs_path *path)
  2466. {
  2467. return __create_free_space_inode(root, trans, path,
  2468. BTRFS_FREE_INO_OBJECTID, 0);
  2469. }
  2470. int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  2471. {
  2472. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2473. struct btrfs_path *path;
  2474. struct inode *inode;
  2475. int ret = 0;
  2476. u64 root_gen = btrfs_root_generation(&root->root_item);
  2477. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2478. return 0;
  2479. /*
  2480. * If we're unmounting then just return, since this does a search on the
  2481. * normal root and not the commit root and we could deadlock.
  2482. */
  2483. if (btrfs_fs_closing(fs_info))
  2484. return 0;
  2485. path = btrfs_alloc_path();
  2486. if (!path)
  2487. return 0;
  2488. inode = lookup_free_ino_inode(root, path);
  2489. if (IS_ERR(inode))
  2490. goto out;
  2491. if (root_gen != BTRFS_I(inode)->generation)
  2492. goto out_put;
  2493. ret = __load_free_space_cache(root, inode, ctl, path, 0);
  2494. if (ret < 0)
  2495. btrfs_err(fs_info,
  2496. "failed to load free ino cache for root %llu",
  2497. root->root_key.objectid);
  2498. out_put:
  2499. iput(inode);
  2500. out:
  2501. btrfs_free_path(path);
  2502. return ret;
  2503. }
  2504. int btrfs_write_out_ino_cache(struct btrfs_root *root,
  2505. struct btrfs_trans_handle *trans,
  2506. struct btrfs_path *path)
  2507. {
  2508. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2509. struct inode *inode;
  2510. int ret;
  2511. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2512. return 0;
  2513. inode = lookup_free_ino_inode(root, path);
  2514. if (IS_ERR(inode))
  2515. return 0;
  2516. ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
  2517. if (ret) {
  2518. btrfs_delalloc_release_metadata(inode, inode->i_size);
  2519. #ifdef DEBUG
  2520. btrfs_err(root->fs_info,
  2521. "failed to write free ino cache for root %llu",
  2522. root->root_key.objectid);
  2523. #endif
  2524. }
  2525. iput(inode);
  2526. return ret;
  2527. }
  2528. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  2529. static struct btrfs_block_group_cache *init_test_block_group(void)
  2530. {
  2531. struct btrfs_block_group_cache *cache;
  2532. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  2533. if (!cache)
  2534. return NULL;
  2535. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  2536. GFP_NOFS);
  2537. if (!cache->free_space_ctl) {
  2538. kfree(cache);
  2539. return NULL;
  2540. }
  2541. cache->key.objectid = 0;
  2542. cache->key.offset = 1024 * 1024 * 1024;
  2543. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  2544. cache->sectorsize = 4096;
  2545. spin_lock_init(&cache->lock);
  2546. INIT_LIST_HEAD(&cache->list);
  2547. INIT_LIST_HEAD(&cache->cluster_list);
  2548. INIT_LIST_HEAD(&cache->new_bg_list);
  2549. btrfs_init_free_space_ctl(cache);
  2550. return cache;
  2551. }
  2552. /*
  2553. * Checks to see if the given range is in the free space cache. This is really
  2554. * just used to check the absence of space, so if there is free space in the
  2555. * range at all we will return 1.
  2556. */
  2557. static int check_exists(struct btrfs_block_group_cache *cache, u64 offset,
  2558. u64 bytes)
  2559. {
  2560. struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
  2561. struct btrfs_free_space *info;
  2562. int ret = 0;
  2563. spin_lock(&ctl->tree_lock);
  2564. info = tree_search_offset(ctl, offset, 0, 0);
  2565. if (!info) {
  2566. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  2567. 1, 0);
  2568. if (!info)
  2569. goto out;
  2570. }
  2571. have_info:
  2572. if (info->bitmap) {
  2573. u64 bit_off, bit_bytes;
  2574. struct rb_node *n;
  2575. struct btrfs_free_space *tmp;
  2576. bit_off = offset;
  2577. bit_bytes = ctl->unit;
  2578. ret = search_bitmap(ctl, info, &bit_off, &bit_bytes);
  2579. if (!ret) {
  2580. if (bit_off == offset) {
  2581. ret = 1;
  2582. goto out;
  2583. } else if (bit_off > offset &&
  2584. offset + bytes > bit_off) {
  2585. ret = 1;
  2586. goto out;
  2587. }
  2588. }
  2589. n = rb_prev(&info->offset_index);
  2590. while (n) {
  2591. tmp = rb_entry(n, struct btrfs_free_space,
  2592. offset_index);
  2593. if (tmp->offset + tmp->bytes < offset)
  2594. break;
  2595. if (offset + bytes < tmp->offset) {
  2596. n = rb_prev(&info->offset_index);
  2597. continue;
  2598. }
  2599. info = tmp;
  2600. goto have_info;
  2601. }
  2602. n = rb_next(&info->offset_index);
  2603. while (n) {
  2604. tmp = rb_entry(n, struct btrfs_free_space,
  2605. offset_index);
  2606. if (offset + bytes < tmp->offset)
  2607. break;
  2608. if (tmp->offset + tmp->bytes < offset) {
  2609. n = rb_next(&info->offset_index);
  2610. continue;
  2611. }
  2612. info = tmp;
  2613. goto have_info;
  2614. }
  2615. goto out;
  2616. }
  2617. if (info->offset == offset) {
  2618. ret = 1;
  2619. goto out;
  2620. }
  2621. if (offset > info->offset && offset < info->offset + info->bytes)
  2622. ret = 1;
  2623. out:
  2624. spin_unlock(&ctl->tree_lock);
  2625. return ret;
  2626. }
  2627. /*
  2628. * Use this if you need to make a bitmap or extent entry specifically, it
  2629. * doesn't do any of the merging that add_free_space does, this acts a lot like
  2630. * how the free space cache loading stuff works, so you can get really weird
  2631. * configurations.
  2632. */
  2633. static int add_free_space_entry(struct btrfs_block_group_cache *cache,
  2634. u64 offset, u64 bytes, bool bitmap)
  2635. {
  2636. struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
  2637. struct btrfs_free_space *info = NULL, *bitmap_info;
  2638. void *map = NULL;
  2639. u64 bytes_added;
  2640. int ret;
  2641. again:
  2642. if (!info) {
  2643. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  2644. if (!info)
  2645. return -ENOMEM;
  2646. }
  2647. if (!bitmap) {
  2648. spin_lock(&ctl->tree_lock);
  2649. info->offset = offset;
  2650. info->bytes = bytes;
  2651. ret = link_free_space(ctl, info);
  2652. spin_unlock(&ctl->tree_lock);
  2653. if (ret)
  2654. kmem_cache_free(btrfs_free_space_cachep, info);
  2655. return ret;
  2656. }
  2657. if (!map) {
  2658. map = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  2659. if (!map) {
  2660. kmem_cache_free(btrfs_free_space_cachep, info);
  2661. return -ENOMEM;
  2662. }
  2663. }
  2664. spin_lock(&ctl->tree_lock);
  2665. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  2666. 1, 0);
  2667. if (!bitmap_info) {
  2668. info->bitmap = map;
  2669. map = NULL;
  2670. add_new_bitmap(ctl, info, offset);
  2671. bitmap_info = info;
  2672. }
  2673. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
  2674. bytes -= bytes_added;
  2675. offset += bytes_added;
  2676. spin_unlock(&ctl->tree_lock);
  2677. if (bytes)
  2678. goto again;
  2679. if (map)
  2680. kfree(map);
  2681. return 0;
  2682. }
  2683. /*
  2684. * This test just does basic sanity checking, making sure we can add an exten
  2685. * entry and remove space from either end and the middle, and make sure we can
  2686. * remove space that covers adjacent extent entries.
  2687. */
  2688. static int test_extents(struct btrfs_block_group_cache *cache)
  2689. {
  2690. int ret = 0;
  2691. printk(KERN_ERR "Running extent only tests\n");
  2692. /* First just make sure we can remove an entire entry */
  2693. ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
  2694. if (ret) {
  2695. printk(KERN_ERR "Error adding initial extents %d\n", ret);
  2696. return ret;
  2697. }
  2698. ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
  2699. if (ret) {
  2700. printk(KERN_ERR "Error removing extent %d\n", ret);
  2701. return ret;
  2702. }
  2703. if (check_exists(cache, 0, 4 * 1024 * 1024)) {
  2704. printk(KERN_ERR "Full remove left some lingering space\n");
  2705. return -1;
  2706. }
  2707. /* Ok edge and middle cases now */
  2708. ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
  2709. if (ret) {
  2710. printk(KERN_ERR "Error adding half extent %d\n", ret);
  2711. return ret;
  2712. }
  2713. ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 1 * 1024 * 1024);
  2714. if (ret) {
  2715. printk(KERN_ERR "Error removing tail end %d\n", ret);
  2716. return ret;
  2717. }
  2718. ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
  2719. if (ret) {
  2720. printk(KERN_ERR "Error removing front end %d\n", ret);
  2721. return ret;
  2722. }
  2723. ret = btrfs_remove_free_space(cache, 2 * 1024 * 1024, 4096);
  2724. if (ret) {
  2725. printk(KERN_ERR "Error removing middle peice %d\n", ret);
  2726. return ret;
  2727. }
  2728. if (check_exists(cache, 0, 1 * 1024 * 1024)) {
  2729. printk(KERN_ERR "Still have space at the front\n");
  2730. return -1;
  2731. }
  2732. if (check_exists(cache, 2 * 1024 * 1024, 4096)) {
  2733. printk(KERN_ERR "Still have space in the middle\n");
  2734. return -1;
  2735. }
  2736. if (check_exists(cache, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
  2737. printk(KERN_ERR "Still have space at the end\n");
  2738. return -1;
  2739. }
  2740. /* Cleanup */
  2741. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  2742. return 0;
  2743. }
  2744. static int test_bitmaps(struct btrfs_block_group_cache *cache)
  2745. {
  2746. u64 next_bitmap_offset;
  2747. int ret;
  2748. printk(KERN_ERR "Running bitmap only tests\n");
  2749. ret = add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
  2750. if (ret) {
  2751. printk(KERN_ERR "Couldn't create a bitmap entry %d\n", ret);
  2752. return ret;
  2753. }
  2754. ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
  2755. if (ret) {
  2756. printk(KERN_ERR "Error removing bitmap full range %d\n", ret);
  2757. return ret;
  2758. }
  2759. if (check_exists(cache, 0, 4 * 1024 * 1024)) {
  2760. printk(KERN_ERR "Left some space in bitmap\n");
  2761. return -1;
  2762. }
  2763. ret = add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
  2764. if (ret) {
  2765. printk(KERN_ERR "Couldn't add to our bitmap entry %d\n", ret);
  2766. return ret;
  2767. }
  2768. ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 2 * 1024 * 1024);
  2769. if (ret) {
  2770. printk(KERN_ERR "Couldn't remove middle chunk %d\n", ret);
  2771. return ret;
  2772. }
  2773. /*
  2774. * The first bitmap we have starts at offset 0 so the next one is just
  2775. * at the end of the first bitmap.
  2776. */
  2777. next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
  2778. /* Test a bit straddling two bitmaps */
  2779. ret = add_free_space_entry(cache, next_bitmap_offset -
  2780. (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
  2781. if (ret) {
  2782. printk(KERN_ERR "Couldn't add space that straddles two bitmaps"
  2783. " %d\n", ret);
  2784. return ret;
  2785. }
  2786. ret = btrfs_remove_free_space(cache, next_bitmap_offset -
  2787. (1 * 1024 * 1024), 2 * 1024 * 1024);
  2788. if (ret) {
  2789. printk(KERN_ERR "Couldn't remove overlapping space %d\n", ret);
  2790. return ret;
  2791. }
  2792. if (check_exists(cache, next_bitmap_offset - (1 * 1024 * 1024),
  2793. 2 * 1024 * 1024)) {
  2794. printk(KERN_ERR "Left some space when removing overlapping\n");
  2795. return -1;
  2796. }
  2797. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  2798. return 0;
  2799. }
  2800. /* This is the high grade jackassery */
  2801. static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
  2802. {
  2803. u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
  2804. int ret;
  2805. printk(KERN_ERR "Running bitmap and extent tests\n");
  2806. /*
  2807. * First let's do something simple, an extent at the same offset as the
  2808. * bitmap, but the free space completely in the extent and then
  2809. * completely in the bitmap.
  2810. */
  2811. ret = add_free_space_entry(cache, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
  2812. if (ret) {
  2813. printk(KERN_ERR "Couldn't create bitmap entry %d\n", ret);
  2814. return ret;
  2815. }
  2816. ret = add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
  2817. if (ret) {
  2818. printk(KERN_ERR "Couldn't add extent entry %d\n", ret);
  2819. return ret;
  2820. }
  2821. ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
  2822. if (ret) {
  2823. printk(KERN_ERR "Couldn't remove extent entry %d\n", ret);
  2824. return ret;
  2825. }
  2826. if (check_exists(cache, 0, 1 * 1024 * 1024)) {
  2827. printk(KERN_ERR "Left remnants after our remove\n");
  2828. return -1;
  2829. }
  2830. /* Now to add back the extent entry and remove from the bitmap */
  2831. ret = add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
  2832. if (ret) {
  2833. printk(KERN_ERR "Couldn't re-add extent entry %d\n", ret);
  2834. return ret;
  2835. }
  2836. ret = btrfs_remove_free_space(cache, 4 * 1024 * 1024, 1 * 1024 * 1024);
  2837. if (ret) {
  2838. printk(KERN_ERR "Couldn't remove from bitmap %d\n", ret);
  2839. return ret;
  2840. }
  2841. if (check_exists(cache, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
  2842. printk(KERN_ERR "Left remnants in the bitmap\n");
  2843. return -1;
  2844. }
  2845. /*
  2846. * Ok so a little more evil, extent entry and bitmap at the same offset,
  2847. * removing an overlapping chunk.
  2848. */
  2849. ret = add_free_space_entry(cache, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
  2850. if (ret) {
  2851. printk(KERN_ERR "Couldn't add to a bitmap %d\n", ret);
  2852. return ret;
  2853. }
  2854. ret = btrfs_remove_free_space(cache, 512 * 1024, 3 * 1024 * 1024);
  2855. if (ret) {
  2856. printk(KERN_ERR "Couldn't remove overlapping space %d\n", ret);
  2857. return ret;
  2858. }
  2859. if (check_exists(cache, 512 * 1024, 3 * 1024 * 1024)) {
  2860. printk(KERN_ERR "Left over peices after removing "
  2861. "overlapping\n");
  2862. return -1;
  2863. }
  2864. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  2865. /* Now with the extent entry offset into the bitmap */
  2866. ret = add_free_space_entry(cache, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
  2867. if (ret) {
  2868. printk(KERN_ERR "Couldn't add space to the bitmap %d\n", ret);
  2869. return ret;
  2870. }
  2871. ret = add_free_space_entry(cache, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
  2872. if (ret) {
  2873. printk(KERN_ERR "Couldn't add extent to the cache %d\n", ret);
  2874. return ret;
  2875. }
  2876. ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 4 * 1024 * 1024);
  2877. if (ret) {
  2878. printk(KERN_ERR "Problem removing overlapping space %d\n", ret);
  2879. return ret;
  2880. }
  2881. if (check_exists(cache, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
  2882. printk(KERN_ERR "Left something behind when removing space");
  2883. return -1;
  2884. }
  2885. /*
  2886. * This has blown up in the past, the extent entry starts before the
  2887. * bitmap entry, but we're trying to remove an offset that falls
  2888. * completely within the bitmap range and is in both the extent entry
  2889. * and the bitmap entry, looks like this
  2890. *
  2891. * [ extent ]
  2892. * [ bitmap ]
  2893. * [ del ]
  2894. */
  2895. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  2896. ret = add_free_space_entry(cache, bitmap_offset + 4 * 1024 * 1024,
  2897. 4 * 1024 * 1024, 1);
  2898. if (ret) {
  2899. printk(KERN_ERR "Couldn't add bitmap %d\n", ret);
  2900. return ret;
  2901. }
  2902. ret = add_free_space_entry(cache, bitmap_offset - 1 * 1024 * 1024,
  2903. 5 * 1024 * 1024, 0);
  2904. if (ret) {
  2905. printk(KERN_ERR "Couldn't add extent entry %d\n", ret);
  2906. return ret;
  2907. }
  2908. ret = btrfs_remove_free_space(cache, bitmap_offset + 1 * 1024 * 1024,
  2909. 5 * 1024 * 1024);
  2910. if (ret) {
  2911. printk(KERN_ERR "Failed to free our space %d\n", ret);
  2912. return ret;
  2913. }
  2914. if (check_exists(cache, bitmap_offset + 1 * 1024 * 1024,
  2915. 5 * 1024 * 1024)) {
  2916. printk(KERN_ERR "Left stuff over\n");
  2917. return -1;
  2918. }
  2919. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  2920. /*
  2921. * This blew up before, we have part of the free space in a bitmap and
  2922. * then the entirety of the rest of the space in an extent. This used
  2923. * to return -EAGAIN back from btrfs_remove_extent, make sure this
  2924. * doesn't happen.
  2925. */
  2926. ret = add_free_space_entry(cache, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
  2927. if (ret) {
  2928. printk(KERN_ERR "Couldn't add bitmap entry %d\n", ret);
  2929. return ret;
  2930. }
  2931. ret = add_free_space_entry(cache, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
  2932. if (ret) {
  2933. printk(KERN_ERR "Couldn't add extent entry %d\n", ret);
  2934. return ret;
  2935. }
  2936. ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 3 * 1024 * 1024);
  2937. if (ret) {
  2938. printk(KERN_ERR "Error removing bitmap and extent "
  2939. "overlapping %d\n", ret);
  2940. return ret;
  2941. }
  2942. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  2943. return 0;
  2944. }
  2945. void btrfs_test_free_space_cache(void)
  2946. {
  2947. struct btrfs_block_group_cache *cache;
  2948. printk(KERN_ERR "Running btrfs free space cache tests\n");
  2949. cache = init_test_block_group();
  2950. if (!cache) {
  2951. printk(KERN_ERR "Couldn't run the tests\n");
  2952. return;
  2953. }
  2954. if (test_extents(cache))
  2955. goto out;
  2956. if (test_bitmaps(cache))
  2957. goto out;
  2958. if (test_bitmaps_and_extents(cache))
  2959. goto out;
  2960. out:
  2961. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  2962. kfree(cache->free_space_ctl);
  2963. kfree(cache);
  2964. printk(KERN_ERR "Free space cache tests finished\n");
  2965. }
  2966. #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */