free-space-cache.c 86 KB

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