free-space-cache.c 92 KB

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