free-space-cache.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Red Hat. All rights reserved.
  4. */
  5. #include <linux/pagemap.h>
  6. #include <linux/sched.h>
  7. #include <linux/sched/signal.h>
  8. #include <linux/slab.h>
  9. #include <linux/math64.h>
  10. #include <linux/ratelimit.h>
  11. #include <linux/error-injection.h>
  12. #include <linux/sched/mm.h>
  13. #include "ctree.h"
  14. #include "fs.h"
  15. #include "messages.h"
  16. #include "misc.h"
  17. #include "free-space-cache.h"
  18. #include "transaction.h"
  19. #include "disk-io.h"
  20. #include "extent_io.h"
  21. #include "space-info.h"
  22. #include "block-group.h"
  23. #include "discard.h"
  24. #include "subpage.h"
  25. #include "inode-item.h"
  26. #include "accessors.h"
  27. #include "file-item.h"
  28. #include "file.h"
  29. #include "super.h"
  30. #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
  31. #define MAX_CACHE_BYTES_PER_GIG SZ_64K
  32. #define FORCE_EXTENT_THRESHOLD SZ_1M
  33. static struct kmem_cache *btrfs_free_space_cachep;
  34. static struct kmem_cache *btrfs_free_space_bitmap_cachep;
  35. struct btrfs_trim_range {
  36. u64 start;
  37. u64 bytes;
  38. struct list_head list;
  39. };
  40. static int link_free_space(struct btrfs_free_space_ctl *ctl,
  41. struct btrfs_free_space *info);
  42. static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
  43. struct btrfs_free_space *info, bool update_stat);
  44. static int search_bitmap(struct btrfs_free_space_ctl *ctl,
  45. struct btrfs_free_space *bitmap_info, u64 *offset,
  46. u64 *bytes, bool for_alloc);
  47. static void free_bitmap(struct btrfs_free_space_ctl *ctl,
  48. struct btrfs_free_space *bitmap_info);
  49. static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  50. struct btrfs_free_space *info, u64 offset,
  51. u64 bytes, bool update_stats);
  52. static void btrfs_crc32c_final(u32 crc, u8 *result)
  53. {
  54. put_unaligned_le32(~crc, result);
  55. }
  56. static void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
  57. {
  58. struct btrfs_free_space *info;
  59. struct rb_node *node;
  60. while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
  61. info = rb_entry(node, struct btrfs_free_space, offset_index);
  62. if (!info->bitmap) {
  63. unlink_free_space(ctl, info, true);
  64. kmem_cache_free(btrfs_free_space_cachep, info);
  65. } else {
  66. free_bitmap(ctl, info);
  67. }
  68. cond_resched_lock(&ctl->tree_lock);
  69. }
  70. }
  71. static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
  72. struct btrfs_path *path,
  73. u64 offset)
  74. {
  75. struct btrfs_key key;
  76. struct btrfs_key location;
  77. struct btrfs_disk_key disk_key;
  78. struct btrfs_free_space_header *header;
  79. struct extent_buffer *leaf;
  80. struct inode *inode = NULL;
  81. unsigned nofs_flag;
  82. int ret;
  83. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  84. key.offset = offset;
  85. key.type = 0;
  86. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  87. if (ret < 0)
  88. return ERR_PTR(ret);
  89. if (ret > 0) {
  90. btrfs_release_path(path);
  91. return ERR_PTR(-ENOENT);
  92. }
  93. leaf = path->nodes[0];
  94. header = btrfs_item_ptr(leaf, path->slots[0],
  95. struct btrfs_free_space_header);
  96. btrfs_free_space_key(leaf, header, &disk_key);
  97. btrfs_disk_key_to_cpu(&location, &disk_key);
  98. btrfs_release_path(path);
  99. /*
  100. * We are often under a trans handle at this point, so we need to make
  101. * sure NOFS is set to keep us from deadlocking.
  102. */
  103. nofs_flag = memalloc_nofs_save();
  104. inode = btrfs_iget_path(location.objectid, root, path);
  105. btrfs_release_path(path);
  106. memalloc_nofs_restore(nofs_flag);
  107. if (IS_ERR(inode))
  108. return inode;
  109. mapping_set_gfp_mask(inode->i_mapping,
  110. mapping_gfp_constraint(inode->i_mapping,
  111. ~(__GFP_FS | __GFP_HIGHMEM)));
  112. return inode;
  113. }
  114. struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
  115. struct btrfs_path *path)
  116. {
  117. struct btrfs_fs_info *fs_info = block_group->fs_info;
  118. struct inode *inode = NULL;
  119. u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
  120. spin_lock(&block_group->lock);
  121. if (block_group->inode)
  122. inode = igrab(&block_group->inode->vfs_inode);
  123. spin_unlock(&block_group->lock);
  124. if (inode)
  125. return inode;
  126. inode = __lookup_free_space_inode(fs_info->tree_root, path,
  127. block_group->start);
  128. if (IS_ERR(inode))
  129. return inode;
  130. spin_lock(&block_group->lock);
  131. if (!((BTRFS_I(inode)->flags & flags) == flags)) {
  132. btrfs_info(fs_info, "Old style space inode found, converting.");
  133. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
  134. BTRFS_INODE_NODATACOW;
  135. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  136. }
  137. if (!test_and_set_bit(BLOCK_GROUP_FLAG_IREF, &block_group->runtime_flags))
  138. block_group->inode = BTRFS_I(igrab(inode));
  139. spin_unlock(&block_group->lock);
  140. return inode;
  141. }
  142. static int __create_free_space_inode(struct btrfs_root *root,
  143. struct btrfs_trans_handle *trans,
  144. struct btrfs_path *path,
  145. u64 ino, u64 offset)
  146. {
  147. struct btrfs_key key;
  148. struct btrfs_disk_key disk_key;
  149. struct btrfs_free_space_header *header;
  150. struct btrfs_inode_item *inode_item;
  151. struct extent_buffer *leaf;
  152. /* We inline CRCs for the free disk space cache */
  153. const u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC |
  154. BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
  155. int ret;
  156. ret = btrfs_insert_empty_inode(trans, root, path, ino);
  157. if (ret)
  158. return ret;
  159. leaf = path->nodes[0];
  160. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  161. struct btrfs_inode_item);
  162. btrfs_item_key(leaf, &disk_key, path->slots[0]);
  163. memzero_extent_buffer(leaf, (unsigned long)inode_item,
  164. sizeof(*inode_item));
  165. btrfs_set_inode_generation(leaf, inode_item, trans->transid);
  166. btrfs_set_inode_size(leaf, inode_item, 0);
  167. btrfs_set_inode_nbytes(leaf, inode_item, 0);
  168. btrfs_set_inode_uid(leaf, inode_item, 0);
  169. btrfs_set_inode_gid(leaf, inode_item, 0);
  170. btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
  171. btrfs_set_inode_flags(leaf, inode_item, flags);
  172. btrfs_set_inode_nlink(leaf, inode_item, 1);
  173. btrfs_set_inode_transid(leaf, inode_item, trans->transid);
  174. btrfs_set_inode_block_group(leaf, inode_item, offset);
  175. btrfs_mark_buffer_dirty(trans, leaf);
  176. btrfs_release_path(path);
  177. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  178. key.offset = offset;
  179. key.type = 0;
  180. ret = btrfs_insert_empty_item(trans, root, path, &key,
  181. sizeof(struct btrfs_free_space_header));
  182. if (ret < 0) {
  183. btrfs_release_path(path);
  184. return ret;
  185. }
  186. leaf = path->nodes[0];
  187. header = btrfs_item_ptr(leaf, path->slots[0],
  188. struct btrfs_free_space_header);
  189. memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
  190. btrfs_set_free_space_key(leaf, header, &disk_key);
  191. btrfs_mark_buffer_dirty(trans, leaf);
  192. btrfs_release_path(path);
  193. return 0;
  194. }
  195. int create_free_space_inode(struct btrfs_trans_handle *trans,
  196. struct btrfs_block_group *block_group,
  197. struct btrfs_path *path)
  198. {
  199. int ret;
  200. u64 ino;
  201. ret = btrfs_get_free_objectid(trans->fs_info->tree_root, &ino);
  202. if (ret < 0)
  203. return ret;
  204. return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
  205. ino, block_group->start);
  206. }
  207. /*
  208. * inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode
  209. * handles lookup, otherwise it takes ownership and iputs the inode.
  210. * Don't reuse an inode pointer after passing it into this function.
  211. */
  212. int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
  213. struct inode *inode,
  214. struct btrfs_block_group *block_group)
  215. {
  216. struct btrfs_path *path;
  217. struct btrfs_key key;
  218. int ret = 0;
  219. path = btrfs_alloc_path();
  220. if (!path)
  221. return -ENOMEM;
  222. if (!inode)
  223. inode = lookup_free_space_inode(block_group, path);
  224. if (IS_ERR(inode)) {
  225. if (PTR_ERR(inode) != -ENOENT)
  226. ret = PTR_ERR(inode);
  227. goto out;
  228. }
  229. ret = btrfs_orphan_add(trans, BTRFS_I(inode));
  230. if (ret) {
  231. btrfs_add_delayed_iput(BTRFS_I(inode));
  232. goto out;
  233. }
  234. clear_nlink(inode);
  235. /* One for the block groups ref */
  236. spin_lock(&block_group->lock);
  237. if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF, &block_group->runtime_flags)) {
  238. block_group->inode = NULL;
  239. spin_unlock(&block_group->lock);
  240. iput(inode);
  241. } else {
  242. spin_unlock(&block_group->lock);
  243. }
  244. /* One for the lookup ref */
  245. btrfs_add_delayed_iput(BTRFS_I(inode));
  246. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  247. key.type = 0;
  248. key.offset = block_group->start;
  249. ret = btrfs_search_slot(trans, trans->fs_info->tree_root, &key, path,
  250. -1, 1);
  251. if (ret) {
  252. if (ret > 0)
  253. ret = 0;
  254. goto out;
  255. }
  256. ret = btrfs_del_item(trans, trans->fs_info->tree_root, path);
  257. out:
  258. btrfs_free_path(path);
  259. return ret;
  260. }
  261. int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
  262. struct btrfs_block_group *block_group,
  263. struct inode *vfs_inode)
  264. {
  265. struct btrfs_truncate_control control = {
  266. .inode = BTRFS_I(vfs_inode),
  267. .new_size = 0,
  268. .ino = btrfs_ino(BTRFS_I(vfs_inode)),
  269. .min_type = BTRFS_EXTENT_DATA_KEY,
  270. .clear_extent_range = true,
  271. };
  272. struct btrfs_inode *inode = BTRFS_I(vfs_inode);
  273. struct btrfs_root *root = inode->root;
  274. struct extent_state *cached_state = NULL;
  275. int ret = 0;
  276. bool locked = false;
  277. if (block_group) {
  278. struct btrfs_path *path = btrfs_alloc_path();
  279. if (!path) {
  280. ret = -ENOMEM;
  281. goto fail;
  282. }
  283. locked = true;
  284. mutex_lock(&trans->transaction->cache_write_mutex);
  285. if (!list_empty(&block_group->io_list)) {
  286. list_del_init(&block_group->io_list);
  287. btrfs_wait_cache_io(trans, block_group, path);
  288. btrfs_put_block_group(block_group);
  289. }
  290. /*
  291. * now that we've truncated the cache away, its no longer
  292. * setup or written
  293. */
  294. spin_lock(&block_group->lock);
  295. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  296. spin_unlock(&block_group->lock);
  297. btrfs_free_path(path);
  298. }
  299. btrfs_i_size_write(inode, 0);
  300. truncate_pagecache(vfs_inode, 0);
  301. lock_extent(&inode->io_tree, 0, (u64)-1, &cached_state);
  302. btrfs_drop_extent_map_range(inode, 0, (u64)-1, false);
  303. /*
  304. * We skip the throttling logic for free space cache inodes, so we don't
  305. * need to check for -EAGAIN.
  306. */
  307. ret = btrfs_truncate_inode_items(trans, root, &control);
  308. inode_sub_bytes(&inode->vfs_inode, control.sub_bytes);
  309. btrfs_inode_safe_disk_i_size_write(inode, control.last_size);
  310. unlock_extent(&inode->io_tree, 0, (u64)-1, &cached_state);
  311. if (ret)
  312. goto fail;
  313. ret = btrfs_update_inode(trans, inode);
  314. fail:
  315. if (locked)
  316. mutex_unlock(&trans->transaction->cache_write_mutex);
  317. if (ret)
  318. btrfs_abort_transaction(trans, ret);
  319. return ret;
  320. }
  321. static void readahead_cache(struct inode *inode)
  322. {
  323. struct file_ra_state ra;
  324. unsigned long last_index;
  325. file_ra_state_init(&ra, inode->i_mapping);
  326. last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  327. page_cache_sync_readahead(inode->i_mapping, &ra, NULL, 0, last_index);
  328. }
  329. static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
  330. int write)
  331. {
  332. int num_pages;
  333. num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  334. /* Make sure we can fit our crcs and generation into the first page */
  335. if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
  336. return -ENOSPC;
  337. memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
  338. io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
  339. if (!io_ctl->pages)
  340. return -ENOMEM;
  341. io_ctl->num_pages = num_pages;
  342. io_ctl->fs_info = inode_to_fs_info(inode);
  343. io_ctl->inode = inode;
  344. return 0;
  345. }
  346. ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
  347. static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
  348. {
  349. kfree(io_ctl->pages);
  350. io_ctl->pages = NULL;
  351. }
  352. static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
  353. {
  354. if (io_ctl->cur) {
  355. io_ctl->cur = NULL;
  356. io_ctl->orig = NULL;
  357. }
  358. }
  359. static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
  360. {
  361. ASSERT(io_ctl->index < io_ctl->num_pages);
  362. io_ctl->page = io_ctl->pages[io_ctl->index++];
  363. io_ctl->cur = page_address(io_ctl->page);
  364. io_ctl->orig = io_ctl->cur;
  365. io_ctl->size = PAGE_SIZE;
  366. if (clear)
  367. clear_page(io_ctl->cur);
  368. }
  369. static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
  370. {
  371. int i;
  372. io_ctl_unmap_page(io_ctl);
  373. for (i = 0; i < io_ctl->num_pages; i++) {
  374. if (io_ctl->pages[i]) {
  375. btrfs_folio_clear_checked(io_ctl->fs_info,
  376. page_folio(io_ctl->pages[i]),
  377. page_offset(io_ctl->pages[i]),
  378. PAGE_SIZE);
  379. unlock_page(io_ctl->pages[i]);
  380. put_page(io_ctl->pages[i]);
  381. }
  382. }
  383. }
  384. static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
  385. {
  386. struct page *page;
  387. struct inode *inode = io_ctl->inode;
  388. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  389. int i;
  390. for (i = 0; i < io_ctl->num_pages; i++) {
  391. int ret;
  392. page = find_or_create_page(inode->i_mapping, i, mask);
  393. if (!page) {
  394. io_ctl_drop_pages(io_ctl);
  395. return -ENOMEM;
  396. }
  397. ret = set_page_extent_mapped(page);
  398. if (ret < 0) {
  399. unlock_page(page);
  400. put_page(page);
  401. io_ctl_drop_pages(io_ctl);
  402. return ret;
  403. }
  404. io_ctl->pages[i] = page;
  405. if (uptodate && !PageUptodate(page)) {
  406. btrfs_read_folio(NULL, page_folio(page));
  407. lock_page(page);
  408. if (page->mapping != inode->i_mapping) {
  409. btrfs_err(BTRFS_I(inode)->root->fs_info,
  410. "free space cache page truncated");
  411. io_ctl_drop_pages(io_ctl);
  412. return -EIO;
  413. }
  414. if (!PageUptodate(page)) {
  415. btrfs_err(BTRFS_I(inode)->root->fs_info,
  416. "error reading free space cache");
  417. io_ctl_drop_pages(io_ctl);
  418. return -EIO;
  419. }
  420. }
  421. }
  422. for (i = 0; i < io_ctl->num_pages; i++)
  423. clear_page_dirty_for_io(io_ctl->pages[i]);
  424. return 0;
  425. }
  426. static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
  427. {
  428. io_ctl_map_page(io_ctl, 1);
  429. /*
  430. * Skip the csum areas. If we don't check crcs then we just have a
  431. * 64bit chunk at the front of the first page.
  432. */
  433. io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
  434. io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
  435. put_unaligned_le64(generation, io_ctl->cur);
  436. io_ctl->cur += sizeof(u64);
  437. }
  438. static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
  439. {
  440. u64 cache_gen;
  441. /*
  442. * Skip the crc area. If we don't check crcs then we just have a 64bit
  443. * chunk at the front of the first page.
  444. */
  445. io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
  446. io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
  447. cache_gen = get_unaligned_le64(io_ctl->cur);
  448. if (cache_gen != generation) {
  449. btrfs_err_rl(io_ctl->fs_info,
  450. "space cache generation (%llu) does not match inode (%llu)",
  451. cache_gen, generation);
  452. io_ctl_unmap_page(io_ctl);
  453. return -EIO;
  454. }
  455. io_ctl->cur += sizeof(u64);
  456. return 0;
  457. }
  458. static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
  459. {
  460. u32 *tmp;
  461. u32 crc = ~(u32)0;
  462. unsigned offset = 0;
  463. if (index == 0)
  464. offset = sizeof(u32) * io_ctl->num_pages;
  465. crc = crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
  466. btrfs_crc32c_final(crc, (u8 *)&crc);
  467. io_ctl_unmap_page(io_ctl);
  468. tmp = page_address(io_ctl->pages[0]);
  469. tmp += index;
  470. *tmp = crc;
  471. }
  472. static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
  473. {
  474. u32 *tmp, val;
  475. u32 crc = ~(u32)0;
  476. unsigned offset = 0;
  477. if (index == 0)
  478. offset = sizeof(u32) * io_ctl->num_pages;
  479. tmp = page_address(io_ctl->pages[0]);
  480. tmp += index;
  481. val = *tmp;
  482. io_ctl_map_page(io_ctl, 0);
  483. crc = crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
  484. btrfs_crc32c_final(crc, (u8 *)&crc);
  485. if (val != crc) {
  486. btrfs_err_rl(io_ctl->fs_info,
  487. "csum mismatch on free space cache");
  488. io_ctl_unmap_page(io_ctl);
  489. return -EIO;
  490. }
  491. return 0;
  492. }
  493. static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
  494. void *bitmap)
  495. {
  496. struct btrfs_free_space_entry *entry;
  497. if (!io_ctl->cur)
  498. return -ENOSPC;
  499. entry = io_ctl->cur;
  500. put_unaligned_le64(offset, &entry->offset);
  501. put_unaligned_le64(bytes, &entry->bytes);
  502. entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
  503. BTRFS_FREE_SPACE_EXTENT;
  504. io_ctl->cur += sizeof(struct btrfs_free_space_entry);
  505. io_ctl->size -= sizeof(struct btrfs_free_space_entry);
  506. if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
  507. return 0;
  508. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  509. /* No more pages to map */
  510. if (io_ctl->index >= io_ctl->num_pages)
  511. return 0;
  512. /* map the next page */
  513. io_ctl_map_page(io_ctl, 1);
  514. return 0;
  515. }
  516. static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
  517. {
  518. if (!io_ctl->cur)
  519. return -ENOSPC;
  520. /*
  521. * If we aren't at the start of the current page, unmap this one and
  522. * map the next one if there is any left.
  523. */
  524. if (io_ctl->cur != io_ctl->orig) {
  525. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  526. if (io_ctl->index >= io_ctl->num_pages)
  527. return -ENOSPC;
  528. io_ctl_map_page(io_ctl, 0);
  529. }
  530. copy_page(io_ctl->cur, bitmap);
  531. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  532. if (io_ctl->index < io_ctl->num_pages)
  533. io_ctl_map_page(io_ctl, 0);
  534. return 0;
  535. }
  536. static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
  537. {
  538. /*
  539. * If we're not on the boundary we know we've modified the page and we
  540. * need to crc the page.
  541. */
  542. if (io_ctl->cur != io_ctl->orig)
  543. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  544. else
  545. io_ctl_unmap_page(io_ctl);
  546. while (io_ctl->index < io_ctl->num_pages) {
  547. io_ctl_map_page(io_ctl, 1);
  548. io_ctl_set_crc(io_ctl, io_ctl->index - 1);
  549. }
  550. }
  551. static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
  552. struct btrfs_free_space *entry, u8 *type)
  553. {
  554. struct btrfs_free_space_entry *e;
  555. int ret;
  556. if (!io_ctl->cur) {
  557. ret = io_ctl_check_crc(io_ctl, io_ctl->index);
  558. if (ret)
  559. return ret;
  560. }
  561. e = io_ctl->cur;
  562. entry->offset = get_unaligned_le64(&e->offset);
  563. entry->bytes = get_unaligned_le64(&e->bytes);
  564. *type = e->type;
  565. io_ctl->cur += sizeof(struct btrfs_free_space_entry);
  566. io_ctl->size -= sizeof(struct btrfs_free_space_entry);
  567. if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
  568. return 0;
  569. io_ctl_unmap_page(io_ctl);
  570. return 0;
  571. }
  572. static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
  573. struct btrfs_free_space *entry)
  574. {
  575. int ret;
  576. ret = io_ctl_check_crc(io_ctl, io_ctl->index);
  577. if (ret)
  578. return ret;
  579. copy_page(entry->bitmap, io_ctl->cur);
  580. io_ctl_unmap_page(io_ctl);
  581. return 0;
  582. }
  583. static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
  584. {
  585. struct btrfs_block_group *block_group = ctl->block_group;
  586. u64 max_bytes;
  587. u64 bitmap_bytes;
  588. u64 extent_bytes;
  589. u64 size = block_group->length;
  590. u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
  591. u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
  592. max_bitmaps = max_t(u64, max_bitmaps, 1);
  593. if (ctl->total_bitmaps > max_bitmaps)
  594. btrfs_err(block_group->fs_info,
  595. "invalid free space control: bg start=%llu len=%llu total_bitmaps=%u unit=%u max_bitmaps=%llu bytes_per_bg=%llu",
  596. block_group->start, block_group->length,
  597. ctl->total_bitmaps, ctl->unit, max_bitmaps,
  598. bytes_per_bg);
  599. ASSERT(ctl->total_bitmaps <= max_bitmaps);
  600. /*
  601. * We are trying to keep the total amount of memory used per 1GiB of
  602. * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation
  603. * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of
  604. * bitmaps, we may end up using more memory than this.
  605. */
  606. if (size < SZ_1G)
  607. max_bytes = MAX_CACHE_BYTES_PER_GIG;
  608. else
  609. max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
  610. bitmap_bytes = ctl->total_bitmaps * ctl->unit;
  611. /*
  612. * we want the extent entry threshold to always be at most 1/2 the max
  613. * bytes we can have, or whatever is less than that.
  614. */
  615. extent_bytes = max_bytes - bitmap_bytes;
  616. extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
  617. ctl->extents_thresh =
  618. div_u64(extent_bytes, sizeof(struct btrfs_free_space));
  619. }
  620. static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
  621. struct btrfs_free_space_ctl *ctl,
  622. struct btrfs_path *path, u64 offset)
  623. {
  624. struct btrfs_fs_info *fs_info = root->fs_info;
  625. struct btrfs_free_space_header *header;
  626. struct extent_buffer *leaf;
  627. struct btrfs_io_ctl io_ctl;
  628. struct btrfs_key key;
  629. struct btrfs_free_space *e, *n;
  630. LIST_HEAD(bitmaps);
  631. u64 num_entries;
  632. u64 num_bitmaps;
  633. u64 generation;
  634. u8 type;
  635. int ret = 0;
  636. /* Nothing in the space cache, goodbye */
  637. if (!i_size_read(inode))
  638. return 0;
  639. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  640. key.offset = offset;
  641. key.type = 0;
  642. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  643. if (ret < 0)
  644. return 0;
  645. else if (ret > 0) {
  646. btrfs_release_path(path);
  647. return 0;
  648. }
  649. ret = -1;
  650. leaf = path->nodes[0];
  651. header = btrfs_item_ptr(leaf, path->slots[0],
  652. struct btrfs_free_space_header);
  653. num_entries = btrfs_free_space_entries(leaf, header);
  654. num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
  655. generation = btrfs_free_space_generation(leaf, header);
  656. btrfs_release_path(path);
  657. if (!BTRFS_I(inode)->generation) {
  658. btrfs_info(fs_info,
  659. "the free space cache file (%llu) is invalid, skip it",
  660. offset);
  661. return 0;
  662. }
  663. if (BTRFS_I(inode)->generation != generation) {
  664. btrfs_err(fs_info,
  665. "free space inode generation (%llu) did not match free space cache generation (%llu)",
  666. BTRFS_I(inode)->generation, generation);
  667. return 0;
  668. }
  669. if (!num_entries)
  670. return 0;
  671. ret = io_ctl_init(&io_ctl, inode, 0);
  672. if (ret)
  673. return ret;
  674. readahead_cache(inode);
  675. ret = io_ctl_prepare_pages(&io_ctl, true);
  676. if (ret)
  677. goto out;
  678. ret = io_ctl_check_crc(&io_ctl, 0);
  679. if (ret)
  680. goto free_cache;
  681. ret = io_ctl_check_generation(&io_ctl, generation);
  682. if (ret)
  683. goto free_cache;
  684. while (num_entries) {
  685. e = kmem_cache_zalloc(btrfs_free_space_cachep,
  686. GFP_NOFS);
  687. if (!e) {
  688. ret = -ENOMEM;
  689. goto free_cache;
  690. }
  691. ret = io_ctl_read_entry(&io_ctl, e, &type);
  692. if (ret) {
  693. kmem_cache_free(btrfs_free_space_cachep, e);
  694. goto free_cache;
  695. }
  696. if (!e->bytes) {
  697. ret = -1;
  698. kmem_cache_free(btrfs_free_space_cachep, e);
  699. goto free_cache;
  700. }
  701. if (type == BTRFS_FREE_SPACE_EXTENT) {
  702. spin_lock(&ctl->tree_lock);
  703. ret = link_free_space(ctl, e);
  704. spin_unlock(&ctl->tree_lock);
  705. if (ret) {
  706. btrfs_err(fs_info,
  707. "Duplicate entries in free space cache, dumping");
  708. kmem_cache_free(btrfs_free_space_cachep, e);
  709. goto free_cache;
  710. }
  711. } else {
  712. ASSERT(num_bitmaps);
  713. num_bitmaps--;
  714. e->bitmap = kmem_cache_zalloc(
  715. btrfs_free_space_bitmap_cachep, GFP_NOFS);
  716. if (!e->bitmap) {
  717. ret = -ENOMEM;
  718. kmem_cache_free(
  719. btrfs_free_space_cachep, e);
  720. goto free_cache;
  721. }
  722. spin_lock(&ctl->tree_lock);
  723. ret = link_free_space(ctl, e);
  724. if (ret) {
  725. spin_unlock(&ctl->tree_lock);
  726. btrfs_err(fs_info,
  727. "Duplicate entries in free space cache, dumping");
  728. kmem_cache_free(btrfs_free_space_bitmap_cachep, e->bitmap);
  729. kmem_cache_free(btrfs_free_space_cachep, e);
  730. goto free_cache;
  731. }
  732. ctl->total_bitmaps++;
  733. recalculate_thresholds(ctl);
  734. spin_unlock(&ctl->tree_lock);
  735. list_add_tail(&e->list, &bitmaps);
  736. }
  737. num_entries--;
  738. }
  739. io_ctl_unmap_page(&io_ctl);
  740. /*
  741. * We add the bitmaps at the end of the entries in order that
  742. * the bitmap entries are added to the cache.
  743. */
  744. list_for_each_entry_safe(e, n, &bitmaps, list) {
  745. list_del_init(&e->list);
  746. ret = io_ctl_read_bitmap(&io_ctl, e);
  747. if (ret)
  748. goto free_cache;
  749. }
  750. io_ctl_drop_pages(&io_ctl);
  751. ret = 1;
  752. out:
  753. io_ctl_free(&io_ctl);
  754. return ret;
  755. free_cache:
  756. io_ctl_drop_pages(&io_ctl);
  757. spin_lock(&ctl->tree_lock);
  758. __btrfs_remove_free_space_cache(ctl);
  759. spin_unlock(&ctl->tree_lock);
  760. goto out;
  761. }
  762. static int copy_free_space_cache(struct btrfs_block_group *block_group,
  763. struct btrfs_free_space_ctl *ctl)
  764. {
  765. struct btrfs_free_space *info;
  766. struct rb_node *n;
  767. int ret = 0;
  768. while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) {
  769. info = rb_entry(n, struct btrfs_free_space, offset_index);
  770. if (!info->bitmap) {
  771. const u64 offset = info->offset;
  772. const u64 bytes = info->bytes;
  773. unlink_free_space(ctl, info, true);
  774. spin_unlock(&ctl->tree_lock);
  775. kmem_cache_free(btrfs_free_space_cachep, info);
  776. ret = btrfs_add_free_space(block_group, offset, bytes);
  777. spin_lock(&ctl->tree_lock);
  778. } else {
  779. u64 offset = info->offset;
  780. u64 bytes = ctl->unit;
  781. ret = search_bitmap(ctl, info, &offset, &bytes, false);
  782. if (ret == 0) {
  783. bitmap_clear_bits(ctl, info, offset, bytes, true);
  784. spin_unlock(&ctl->tree_lock);
  785. ret = btrfs_add_free_space(block_group, offset,
  786. bytes);
  787. spin_lock(&ctl->tree_lock);
  788. } else {
  789. free_bitmap(ctl, info);
  790. ret = 0;
  791. }
  792. }
  793. cond_resched_lock(&ctl->tree_lock);
  794. }
  795. return ret;
  796. }
  797. static struct lock_class_key btrfs_free_space_inode_key;
  798. int load_free_space_cache(struct btrfs_block_group *block_group)
  799. {
  800. struct btrfs_fs_info *fs_info = block_group->fs_info;
  801. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  802. struct btrfs_free_space_ctl tmp_ctl = {};
  803. struct inode *inode;
  804. struct btrfs_path *path;
  805. int ret = 0;
  806. bool matched;
  807. u64 used = block_group->used;
  808. /*
  809. * Because we could potentially discard our loaded free space, we want
  810. * to load everything into a temporary structure first, and then if it's
  811. * valid copy it all into the actual free space ctl.
  812. */
  813. btrfs_init_free_space_ctl(block_group, &tmp_ctl);
  814. /*
  815. * If this block group has been marked to be cleared for one reason or
  816. * another then we can't trust the on disk cache, so just return.
  817. */
  818. spin_lock(&block_group->lock);
  819. if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
  820. spin_unlock(&block_group->lock);
  821. return 0;
  822. }
  823. spin_unlock(&block_group->lock);
  824. path = btrfs_alloc_path();
  825. if (!path)
  826. return 0;
  827. path->search_commit_root = 1;
  828. path->skip_locking = 1;
  829. /*
  830. * We must pass a path with search_commit_root set to btrfs_iget in
  831. * order to avoid a deadlock when allocating extents for the tree root.
  832. *
  833. * When we are COWing an extent buffer from the tree root, when looking
  834. * for a free extent, at extent-tree.c:find_free_extent(), we can find
  835. * block group without its free space cache loaded. When we find one
  836. * we must load its space cache which requires reading its free space
  837. * cache's inode item from the root tree. If this inode item is located
  838. * in the same leaf that we started COWing before, then we end up in
  839. * deadlock on the extent buffer (trying to read lock it when we
  840. * previously write locked it).
  841. *
  842. * It's safe to read the inode item using the commit root because
  843. * block groups, once loaded, stay in memory forever (until they are
  844. * removed) as well as their space caches once loaded. New block groups
  845. * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
  846. * we will never try to read their inode item while the fs is mounted.
  847. */
  848. inode = lookup_free_space_inode(block_group, path);
  849. if (IS_ERR(inode)) {
  850. btrfs_free_path(path);
  851. return 0;
  852. }
  853. /* We may have converted the inode and made the cache invalid. */
  854. spin_lock(&block_group->lock);
  855. if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
  856. spin_unlock(&block_group->lock);
  857. btrfs_free_path(path);
  858. goto out;
  859. }
  860. spin_unlock(&block_group->lock);
  861. /*
  862. * Reinitialize the class of struct inode's mapping->invalidate_lock for
  863. * free space inodes to prevent false positives related to locks for normal
  864. * inodes.
  865. */
  866. lockdep_set_class(&(&inode->i_data)->invalidate_lock,
  867. &btrfs_free_space_inode_key);
  868. ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl,
  869. path, block_group->start);
  870. btrfs_free_path(path);
  871. if (ret <= 0)
  872. goto out;
  873. matched = (tmp_ctl.free_space == (block_group->length - used -
  874. block_group->bytes_super));
  875. if (matched) {
  876. spin_lock(&tmp_ctl.tree_lock);
  877. ret = copy_free_space_cache(block_group, &tmp_ctl);
  878. spin_unlock(&tmp_ctl.tree_lock);
  879. /*
  880. * ret == 1 means we successfully loaded the free space cache,
  881. * so we need to re-set it here.
  882. */
  883. if (ret == 0)
  884. ret = 1;
  885. } else {
  886. /*
  887. * We need to call the _locked variant so we don't try to update
  888. * the discard counters.
  889. */
  890. spin_lock(&tmp_ctl.tree_lock);
  891. __btrfs_remove_free_space_cache(&tmp_ctl);
  892. spin_unlock(&tmp_ctl.tree_lock);
  893. btrfs_warn(fs_info,
  894. "block group %llu has wrong amount of free space",
  895. block_group->start);
  896. ret = -1;
  897. }
  898. out:
  899. if (ret < 0) {
  900. /* This cache is bogus, make sure it gets cleared */
  901. spin_lock(&block_group->lock);
  902. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  903. spin_unlock(&block_group->lock);
  904. ret = 0;
  905. btrfs_warn(fs_info,
  906. "failed to load free space cache for block group %llu, rebuilding it now",
  907. block_group->start);
  908. }
  909. spin_lock(&ctl->tree_lock);
  910. btrfs_discard_update_discardable(block_group);
  911. spin_unlock(&ctl->tree_lock);
  912. iput(inode);
  913. return ret;
  914. }
  915. static noinline_for_stack
  916. int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
  917. struct btrfs_free_space_ctl *ctl,
  918. struct btrfs_block_group *block_group,
  919. int *entries, int *bitmaps,
  920. struct list_head *bitmap_list)
  921. {
  922. int ret;
  923. struct btrfs_free_cluster *cluster = NULL;
  924. struct btrfs_free_cluster *cluster_locked = NULL;
  925. struct rb_node *node = rb_first(&ctl->free_space_offset);
  926. struct btrfs_trim_range *trim_entry;
  927. /* Get the cluster for this block_group if it exists */
  928. if (block_group && !list_empty(&block_group->cluster_list)) {
  929. cluster = list_entry(block_group->cluster_list.next,
  930. struct btrfs_free_cluster,
  931. block_group_list);
  932. }
  933. if (!node && cluster) {
  934. cluster_locked = cluster;
  935. spin_lock(&cluster_locked->lock);
  936. node = rb_first(&cluster->root);
  937. cluster = NULL;
  938. }
  939. /* Write out the extent entries */
  940. while (node) {
  941. struct btrfs_free_space *e;
  942. e = rb_entry(node, struct btrfs_free_space, offset_index);
  943. *entries += 1;
  944. ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
  945. e->bitmap);
  946. if (ret)
  947. goto fail;
  948. if (e->bitmap) {
  949. list_add_tail(&e->list, bitmap_list);
  950. *bitmaps += 1;
  951. }
  952. node = rb_next(node);
  953. if (!node && cluster) {
  954. node = rb_first(&cluster->root);
  955. cluster_locked = cluster;
  956. spin_lock(&cluster_locked->lock);
  957. cluster = NULL;
  958. }
  959. }
  960. if (cluster_locked) {
  961. spin_unlock(&cluster_locked->lock);
  962. cluster_locked = NULL;
  963. }
  964. /*
  965. * Make sure we don't miss any range that was removed from our rbtree
  966. * because trimming is running. Otherwise after a umount+mount (or crash
  967. * after committing the transaction) we would leak free space and get
  968. * an inconsistent free space cache report from fsck.
  969. */
  970. list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
  971. ret = io_ctl_add_entry(io_ctl, trim_entry->start,
  972. trim_entry->bytes, NULL);
  973. if (ret)
  974. goto fail;
  975. *entries += 1;
  976. }
  977. return 0;
  978. fail:
  979. if (cluster_locked)
  980. spin_unlock(&cluster_locked->lock);
  981. return -ENOSPC;
  982. }
  983. static noinline_for_stack int
  984. update_cache_item(struct btrfs_trans_handle *trans,
  985. struct btrfs_root *root,
  986. struct inode *inode,
  987. struct btrfs_path *path, u64 offset,
  988. int entries, int bitmaps)
  989. {
  990. struct btrfs_key key;
  991. struct btrfs_free_space_header *header;
  992. struct extent_buffer *leaf;
  993. int ret;
  994. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  995. key.offset = offset;
  996. key.type = 0;
  997. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  998. if (ret < 0) {
  999. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
  1000. EXTENT_DELALLOC, NULL);
  1001. goto fail;
  1002. }
  1003. leaf = path->nodes[0];
  1004. if (ret > 0) {
  1005. struct btrfs_key found_key;
  1006. ASSERT(path->slots[0]);
  1007. path->slots[0]--;
  1008. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1009. if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
  1010. found_key.offset != offset) {
  1011. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
  1012. inode->i_size - 1, EXTENT_DELALLOC,
  1013. NULL);
  1014. btrfs_release_path(path);
  1015. goto fail;
  1016. }
  1017. }
  1018. BTRFS_I(inode)->generation = trans->transid;
  1019. header = btrfs_item_ptr(leaf, path->slots[0],
  1020. struct btrfs_free_space_header);
  1021. btrfs_set_free_space_entries(leaf, header, entries);
  1022. btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
  1023. btrfs_set_free_space_generation(leaf, header, trans->transid);
  1024. btrfs_mark_buffer_dirty(trans, leaf);
  1025. btrfs_release_path(path);
  1026. return 0;
  1027. fail:
  1028. return -1;
  1029. }
  1030. static noinline_for_stack int write_pinned_extent_entries(
  1031. struct btrfs_trans_handle *trans,
  1032. struct btrfs_block_group *block_group,
  1033. struct btrfs_io_ctl *io_ctl,
  1034. int *entries)
  1035. {
  1036. u64 start, extent_start, extent_end, len;
  1037. struct extent_io_tree *unpin = NULL;
  1038. int ret;
  1039. if (!block_group)
  1040. return 0;
  1041. /*
  1042. * We want to add any pinned extents to our free space cache
  1043. * so we don't leak the space
  1044. *
  1045. * We shouldn't have switched the pinned extents yet so this is the
  1046. * right one
  1047. */
  1048. unpin = &trans->transaction->pinned_extents;
  1049. start = block_group->start;
  1050. while (start < block_group->start + block_group->length) {
  1051. if (!find_first_extent_bit(unpin, start,
  1052. &extent_start, &extent_end,
  1053. EXTENT_DIRTY, NULL))
  1054. return 0;
  1055. /* This pinned extent is out of our range */
  1056. if (extent_start >= block_group->start + block_group->length)
  1057. return 0;
  1058. extent_start = max(extent_start, start);
  1059. extent_end = min(block_group->start + block_group->length,
  1060. extent_end + 1);
  1061. len = extent_end - extent_start;
  1062. *entries += 1;
  1063. ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
  1064. if (ret)
  1065. return -ENOSPC;
  1066. start = extent_end;
  1067. }
  1068. return 0;
  1069. }
  1070. static noinline_for_stack int
  1071. write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
  1072. {
  1073. struct btrfs_free_space *entry, *next;
  1074. int ret;
  1075. /* Write out the bitmaps */
  1076. list_for_each_entry_safe(entry, next, bitmap_list, list) {
  1077. ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
  1078. if (ret)
  1079. return -ENOSPC;
  1080. list_del_init(&entry->list);
  1081. }
  1082. return 0;
  1083. }
  1084. static int flush_dirty_cache(struct inode *inode)
  1085. {
  1086. int ret;
  1087. ret = btrfs_wait_ordered_range(BTRFS_I(inode), 0, (u64)-1);
  1088. if (ret)
  1089. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
  1090. EXTENT_DELALLOC, NULL);
  1091. return ret;
  1092. }
  1093. static void noinline_for_stack
  1094. cleanup_bitmap_list(struct list_head *bitmap_list)
  1095. {
  1096. struct btrfs_free_space *entry, *next;
  1097. list_for_each_entry_safe(entry, next, bitmap_list, list)
  1098. list_del_init(&entry->list);
  1099. }
  1100. static void noinline_for_stack
  1101. cleanup_write_cache_enospc(struct inode *inode,
  1102. struct btrfs_io_ctl *io_ctl,
  1103. struct extent_state **cached_state)
  1104. {
  1105. io_ctl_drop_pages(io_ctl);
  1106. unlock_extent(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  1107. cached_state);
  1108. }
  1109. static int __btrfs_wait_cache_io(struct btrfs_root *root,
  1110. struct btrfs_trans_handle *trans,
  1111. struct btrfs_block_group *block_group,
  1112. struct btrfs_io_ctl *io_ctl,
  1113. struct btrfs_path *path, u64 offset)
  1114. {
  1115. int ret;
  1116. struct inode *inode = io_ctl->inode;
  1117. if (!inode)
  1118. return 0;
  1119. /* Flush the dirty pages in the cache file. */
  1120. ret = flush_dirty_cache(inode);
  1121. if (ret)
  1122. goto out;
  1123. /* Update the cache item to tell everyone this cache file is valid. */
  1124. ret = update_cache_item(trans, root, inode, path, offset,
  1125. io_ctl->entries, io_ctl->bitmaps);
  1126. out:
  1127. if (ret) {
  1128. invalidate_inode_pages2(inode->i_mapping);
  1129. BTRFS_I(inode)->generation = 0;
  1130. if (block_group)
  1131. btrfs_debug(root->fs_info,
  1132. "failed to write free space cache for block group %llu error %d",
  1133. block_group->start, ret);
  1134. }
  1135. btrfs_update_inode(trans, BTRFS_I(inode));
  1136. if (block_group) {
  1137. /* the dirty list is protected by the dirty_bgs_lock */
  1138. spin_lock(&trans->transaction->dirty_bgs_lock);
  1139. /* the disk_cache_state is protected by the block group lock */
  1140. spin_lock(&block_group->lock);
  1141. /*
  1142. * only mark this as written if we didn't get put back on
  1143. * the dirty list while waiting for IO. Otherwise our
  1144. * cache state won't be right, and we won't get written again
  1145. */
  1146. if (!ret && list_empty(&block_group->dirty_list))
  1147. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  1148. else if (ret)
  1149. block_group->disk_cache_state = BTRFS_DC_ERROR;
  1150. spin_unlock(&block_group->lock);
  1151. spin_unlock(&trans->transaction->dirty_bgs_lock);
  1152. io_ctl->inode = NULL;
  1153. iput(inode);
  1154. }
  1155. return ret;
  1156. }
  1157. int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
  1158. struct btrfs_block_group *block_group,
  1159. struct btrfs_path *path)
  1160. {
  1161. return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
  1162. block_group, &block_group->io_ctl,
  1163. path, block_group->start);
  1164. }
  1165. /*
  1166. * Write out cached info to an inode.
  1167. *
  1168. * @inode: freespace inode we are writing out
  1169. * @ctl: free space cache we are going to write out
  1170. * @block_group: block_group for this cache if it belongs to a block_group
  1171. * @io_ctl: holds context for the io
  1172. * @trans: the trans handle
  1173. *
  1174. * This function writes out a free space cache struct to disk for quick recovery
  1175. * on mount. This will return 0 if it was successful in writing the cache out,
  1176. * or an errno if it was not.
  1177. */
  1178. static int __btrfs_write_out_cache(struct inode *inode,
  1179. struct btrfs_free_space_ctl *ctl,
  1180. struct btrfs_block_group *block_group,
  1181. struct btrfs_io_ctl *io_ctl,
  1182. struct btrfs_trans_handle *trans)
  1183. {
  1184. struct extent_state *cached_state = NULL;
  1185. LIST_HEAD(bitmap_list);
  1186. int entries = 0;
  1187. int bitmaps = 0;
  1188. int ret;
  1189. int must_iput = 0;
  1190. if (!i_size_read(inode))
  1191. return -EIO;
  1192. WARN_ON(io_ctl->pages);
  1193. ret = io_ctl_init(io_ctl, inode, 1);
  1194. if (ret)
  1195. return ret;
  1196. if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
  1197. down_write(&block_group->data_rwsem);
  1198. spin_lock(&block_group->lock);
  1199. if (block_group->delalloc_bytes) {
  1200. block_group->disk_cache_state = BTRFS_DC_WRITTEN;
  1201. spin_unlock(&block_group->lock);
  1202. up_write(&block_group->data_rwsem);
  1203. BTRFS_I(inode)->generation = 0;
  1204. ret = 0;
  1205. must_iput = 1;
  1206. goto out;
  1207. }
  1208. spin_unlock(&block_group->lock);
  1209. }
  1210. /* Lock all pages first so we can lock the extent safely. */
  1211. ret = io_ctl_prepare_pages(io_ctl, false);
  1212. if (ret)
  1213. goto out_unlock;
  1214. lock_extent(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  1215. &cached_state);
  1216. io_ctl_set_generation(io_ctl, trans->transid);
  1217. mutex_lock(&ctl->cache_writeout_mutex);
  1218. /* Write out the extent entries in the free space cache */
  1219. spin_lock(&ctl->tree_lock);
  1220. ret = write_cache_extent_entries(io_ctl, ctl,
  1221. block_group, &entries, &bitmaps,
  1222. &bitmap_list);
  1223. if (ret)
  1224. goto out_nospc_locked;
  1225. /*
  1226. * Some spaces that are freed in the current transaction are pinned,
  1227. * they will be added into free space cache after the transaction is
  1228. * committed, we shouldn't lose them.
  1229. *
  1230. * If this changes while we are working we'll get added back to
  1231. * the dirty list and redo it. No locking needed
  1232. */
  1233. ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries);
  1234. if (ret)
  1235. goto out_nospc_locked;
  1236. /*
  1237. * At last, we write out all the bitmaps and keep cache_writeout_mutex
  1238. * locked while doing it because a concurrent trim can be manipulating
  1239. * or freeing the bitmap.
  1240. */
  1241. ret = write_bitmap_entries(io_ctl, &bitmap_list);
  1242. spin_unlock(&ctl->tree_lock);
  1243. mutex_unlock(&ctl->cache_writeout_mutex);
  1244. if (ret)
  1245. goto out_nospc;
  1246. /* Zero out the rest of the pages just to make sure */
  1247. io_ctl_zero_remaining_pages(io_ctl);
  1248. /* Everything is written out, now we dirty the pages in the file. */
  1249. ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages,
  1250. io_ctl->num_pages, 0, i_size_read(inode),
  1251. &cached_state, false);
  1252. if (ret)
  1253. goto out_nospc;
  1254. if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
  1255. up_write(&block_group->data_rwsem);
  1256. /*
  1257. * Release the pages and unlock the extent, we will flush
  1258. * them out later
  1259. */
  1260. io_ctl_drop_pages(io_ctl);
  1261. io_ctl_free(io_ctl);
  1262. unlock_extent(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  1263. &cached_state);
  1264. /*
  1265. * at this point the pages are under IO and we're happy,
  1266. * The caller is responsible for waiting on them and updating
  1267. * the cache and the inode
  1268. */
  1269. io_ctl->entries = entries;
  1270. io_ctl->bitmaps = bitmaps;
  1271. ret = btrfs_fdatawrite_range(BTRFS_I(inode), 0, (u64)-1);
  1272. if (ret)
  1273. goto out;
  1274. return 0;
  1275. out_nospc_locked:
  1276. cleanup_bitmap_list(&bitmap_list);
  1277. spin_unlock(&ctl->tree_lock);
  1278. mutex_unlock(&ctl->cache_writeout_mutex);
  1279. out_nospc:
  1280. cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
  1281. out_unlock:
  1282. if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
  1283. up_write(&block_group->data_rwsem);
  1284. out:
  1285. io_ctl->inode = NULL;
  1286. io_ctl_free(io_ctl);
  1287. if (ret) {
  1288. invalidate_inode_pages2(inode->i_mapping);
  1289. BTRFS_I(inode)->generation = 0;
  1290. }
  1291. btrfs_update_inode(trans, BTRFS_I(inode));
  1292. if (must_iput)
  1293. iput(inode);
  1294. return ret;
  1295. }
  1296. int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
  1297. struct btrfs_block_group *block_group,
  1298. struct btrfs_path *path)
  1299. {
  1300. struct btrfs_fs_info *fs_info = trans->fs_info;
  1301. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1302. struct inode *inode;
  1303. int ret = 0;
  1304. spin_lock(&block_group->lock);
  1305. if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
  1306. spin_unlock(&block_group->lock);
  1307. return 0;
  1308. }
  1309. spin_unlock(&block_group->lock);
  1310. inode = lookup_free_space_inode(block_group, path);
  1311. if (IS_ERR(inode))
  1312. return 0;
  1313. ret = __btrfs_write_out_cache(inode, ctl, block_group,
  1314. &block_group->io_ctl, trans);
  1315. if (ret) {
  1316. btrfs_debug(fs_info,
  1317. "failed to write free space cache for block group %llu error %d",
  1318. block_group->start, ret);
  1319. spin_lock(&block_group->lock);
  1320. block_group->disk_cache_state = BTRFS_DC_ERROR;
  1321. spin_unlock(&block_group->lock);
  1322. block_group->io_ctl.inode = NULL;
  1323. iput(inode);
  1324. }
  1325. /*
  1326. * if ret == 0 the caller is expected to call btrfs_wait_cache_io
  1327. * to wait for IO and put the inode
  1328. */
  1329. return ret;
  1330. }
  1331. static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
  1332. u64 offset)
  1333. {
  1334. ASSERT(offset >= bitmap_start);
  1335. offset -= bitmap_start;
  1336. return (unsigned long)(div_u64(offset, unit));
  1337. }
  1338. static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
  1339. {
  1340. return (unsigned long)(div_u64(bytes, unit));
  1341. }
  1342. static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
  1343. u64 offset)
  1344. {
  1345. u64 bitmap_start;
  1346. u64 bytes_per_bitmap;
  1347. bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
  1348. bitmap_start = offset - ctl->start;
  1349. bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
  1350. bitmap_start *= bytes_per_bitmap;
  1351. bitmap_start += ctl->start;
  1352. return bitmap_start;
  1353. }
  1354. static int tree_insert_offset(struct btrfs_free_space_ctl *ctl,
  1355. struct btrfs_free_cluster *cluster,
  1356. struct btrfs_free_space *new_entry)
  1357. {
  1358. struct rb_root *root;
  1359. struct rb_node **p;
  1360. struct rb_node *parent = NULL;
  1361. lockdep_assert_held(&ctl->tree_lock);
  1362. if (cluster) {
  1363. lockdep_assert_held(&cluster->lock);
  1364. root = &cluster->root;
  1365. } else {
  1366. root = &ctl->free_space_offset;
  1367. }
  1368. p = &root->rb_node;
  1369. while (*p) {
  1370. struct btrfs_free_space *info;
  1371. parent = *p;
  1372. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  1373. if (new_entry->offset < info->offset) {
  1374. p = &(*p)->rb_left;
  1375. } else if (new_entry->offset > info->offset) {
  1376. p = &(*p)->rb_right;
  1377. } else {
  1378. /*
  1379. * we could have a bitmap entry and an extent entry
  1380. * share the same offset. If this is the case, we want
  1381. * the extent entry to always be found first if we do a
  1382. * linear search through the tree, since we want to have
  1383. * the quickest allocation time, and allocating from an
  1384. * extent is faster than allocating from a bitmap. So
  1385. * if we're inserting a bitmap and we find an entry at
  1386. * this offset, we want to go right, or after this entry
  1387. * logically. If we are inserting an extent and we've
  1388. * found a bitmap, we want to go left, or before
  1389. * logically.
  1390. */
  1391. if (new_entry->bitmap) {
  1392. if (info->bitmap) {
  1393. WARN_ON_ONCE(1);
  1394. return -EEXIST;
  1395. }
  1396. p = &(*p)->rb_right;
  1397. } else {
  1398. if (!info->bitmap) {
  1399. WARN_ON_ONCE(1);
  1400. return -EEXIST;
  1401. }
  1402. p = &(*p)->rb_left;
  1403. }
  1404. }
  1405. }
  1406. rb_link_node(&new_entry->offset_index, parent, p);
  1407. rb_insert_color(&new_entry->offset_index, root);
  1408. return 0;
  1409. }
  1410. /*
  1411. * This is a little subtle. We *only* have ->max_extent_size set if we actually
  1412. * searched through the bitmap and figured out the largest ->max_extent_size,
  1413. * otherwise it's 0. In the case that it's 0 we don't want to tell the
  1414. * allocator the wrong thing, we want to use the actual real max_extent_size
  1415. * we've found already if it's larger, or we want to use ->bytes.
  1416. *
  1417. * This matters because find_free_space() will skip entries who's ->bytes is
  1418. * less than the required bytes. So if we didn't search down this bitmap, we
  1419. * may pick some previous entry that has a smaller ->max_extent_size than we
  1420. * have. For example, assume we have two entries, one that has
  1421. * ->max_extent_size set to 4K and ->bytes set to 1M. A second entry hasn't set
  1422. * ->max_extent_size yet, has ->bytes set to 8K and it's contiguous. We will
  1423. * call into find_free_space(), and return with max_extent_size == 4K, because
  1424. * that first bitmap entry had ->max_extent_size set, but the second one did
  1425. * not. If instead we returned 8K we'd come in searching for 8K, and find the
  1426. * 8K contiguous range.
  1427. *
  1428. * Consider the other case, we have 2 8K chunks in that second entry and still
  1429. * don't have ->max_extent_size set. We'll return 16K, and the next time the
  1430. * allocator comes in it'll fully search our second bitmap, and this time it'll
  1431. * get an uptodate value of 8K as the maximum chunk size. Then we'll get the
  1432. * right allocation the next loop through.
  1433. */
  1434. static inline u64 get_max_extent_size(const struct btrfs_free_space *entry)
  1435. {
  1436. if (entry->bitmap && entry->max_extent_size)
  1437. return entry->max_extent_size;
  1438. return entry->bytes;
  1439. }
  1440. /*
  1441. * We want the largest entry to be leftmost, so this is inverted from what you'd
  1442. * normally expect.
  1443. */
  1444. static bool entry_less(struct rb_node *node, const struct rb_node *parent)
  1445. {
  1446. const struct btrfs_free_space *entry, *exist;
  1447. entry = rb_entry(node, struct btrfs_free_space, bytes_index);
  1448. exist = rb_entry(parent, struct btrfs_free_space, bytes_index);
  1449. return get_max_extent_size(exist) < get_max_extent_size(entry);
  1450. }
  1451. /*
  1452. * searches the tree for the given offset.
  1453. *
  1454. * fuzzy - If this is set, then we are trying to make an allocation, and we just
  1455. * want a section that has at least bytes size and comes at or after the given
  1456. * offset.
  1457. */
  1458. static struct btrfs_free_space *
  1459. tree_search_offset(struct btrfs_free_space_ctl *ctl,
  1460. u64 offset, int bitmap_only, int fuzzy)
  1461. {
  1462. struct rb_node *n = ctl->free_space_offset.rb_node;
  1463. struct btrfs_free_space *entry = NULL, *prev = NULL;
  1464. lockdep_assert_held(&ctl->tree_lock);
  1465. /* find entry that is closest to the 'offset' */
  1466. while (n) {
  1467. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1468. prev = entry;
  1469. if (offset < entry->offset)
  1470. n = n->rb_left;
  1471. else if (offset > entry->offset)
  1472. n = n->rb_right;
  1473. else
  1474. break;
  1475. entry = NULL;
  1476. }
  1477. if (bitmap_only) {
  1478. if (!entry)
  1479. return NULL;
  1480. if (entry->bitmap)
  1481. return entry;
  1482. /*
  1483. * bitmap entry and extent entry may share same offset,
  1484. * in that case, bitmap entry comes after extent entry.
  1485. */
  1486. n = rb_next(n);
  1487. if (!n)
  1488. return NULL;
  1489. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1490. if (entry->offset != offset)
  1491. return NULL;
  1492. WARN_ON(!entry->bitmap);
  1493. return entry;
  1494. } else if (entry) {
  1495. if (entry->bitmap) {
  1496. /*
  1497. * if previous extent entry covers the offset,
  1498. * we should return it instead of the bitmap entry
  1499. */
  1500. n = rb_prev(&entry->offset_index);
  1501. if (n) {
  1502. prev = rb_entry(n, struct btrfs_free_space,
  1503. offset_index);
  1504. if (!prev->bitmap &&
  1505. prev->offset + prev->bytes > offset)
  1506. entry = prev;
  1507. }
  1508. }
  1509. return entry;
  1510. }
  1511. if (!prev)
  1512. return NULL;
  1513. /* find last entry before the 'offset' */
  1514. entry = prev;
  1515. if (entry->offset > offset) {
  1516. n = rb_prev(&entry->offset_index);
  1517. if (n) {
  1518. entry = rb_entry(n, struct btrfs_free_space,
  1519. offset_index);
  1520. ASSERT(entry->offset <= offset);
  1521. } else {
  1522. if (fuzzy)
  1523. return entry;
  1524. else
  1525. return NULL;
  1526. }
  1527. }
  1528. if (entry->bitmap) {
  1529. n = rb_prev(&entry->offset_index);
  1530. if (n) {
  1531. prev = rb_entry(n, struct btrfs_free_space,
  1532. offset_index);
  1533. if (!prev->bitmap &&
  1534. prev->offset + prev->bytes > offset)
  1535. return prev;
  1536. }
  1537. if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
  1538. return entry;
  1539. } else if (entry->offset + entry->bytes > offset)
  1540. return entry;
  1541. if (!fuzzy)
  1542. return NULL;
  1543. while (1) {
  1544. n = rb_next(&entry->offset_index);
  1545. if (!n)
  1546. return NULL;
  1547. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1548. if (entry->bitmap) {
  1549. if (entry->offset + BITS_PER_BITMAP *
  1550. ctl->unit > offset)
  1551. break;
  1552. } else {
  1553. if (entry->offset + entry->bytes > offset)
  1554. break;
  1555. }
  1556. }
  1557. return entry;
  1558. }
  1559. static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl,
  1560. struct btrfs_free_space *info,
  1561. bool update_stat)
  1562. {
  1563. lockdep_assert_held(&ctl->tree_lock);
  1564. rb_erase(&info->offset_index, &ctl->free_space_offset);
  1565. rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes);
  1566. ctl->free_extents--;
  1567. if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
  1568. ctl->discardable_extents[BTRFS_STAT_CURR]--;
  1569. ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes;
  1570. }
  1571. if (update_stat)
  1572. ctl->free_space -= info->bytes;
  1573. }
  1574. static int link_free_space(struct btrfs_free_space_ctl *ctl,
  1575. struct btrfs_free_space *info)
  1576. {
  1577. int ret = 0;
  1578. lockdep_assert_held(&ctl->tree_lock);
  1579. ASSERT(info->bytes || info->bitmap);
  1580. ret = tree_insert_offset(ctl, NULL, info);
  1581. if (ret)
  1582. return ret;
  1583. rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less);
  1584. if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
  1585. ctl->discardable_extents[BTRFS_STAT_CURR]++;
  1586. ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
  1587. }
  1588. ctl->free_space += info->bytes;
  1589. ctl->free_extents++;
  1590. return ret;
  1591. }
  1592. static void relink_bitmap_entry(struct btrfs_free_space_ctl *ctl,
  1593. struct btrfs_free_space *info)
  1594. {
  1595. ASSERT(info->bitmap);
  1596. /*
  1597. * If our entry is empty it's because we're on a cluster and we don't
  1598. * want to re-link it into our ctl bytes index.
  1599. */
  1600. if (RB_EMPTY_NODE(&info->bytes_index))
  1601. return;
  1602. lockdep_assert_held(&ctl->tree_lock);
  1603. rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes);
  1604. rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less);
  1605. }
  1606. static inline void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  1607. struct btrfs_free_space *info,
  1608. u64 offset, u64 bytes, bool update_stat)
  1609. {
  1610. unsigned long start, count, end;
  1611. int extent_delta = -1;
  1612. start = offset_to_bit(info->offset, ctl->unit, offset);
  1613. count = bytes_to_bits(bytes, ctl->unit);
  1614. end = start + count;
  1615. ASSERT(end <= BITS_PER_BITMAP);
  1616. bitmap_clear(info->bitmap, start, count);
  1617. info->bytes -= bytes;
  1618. if (info->max_extent_size > ctl->unit)
  1619. info->max_extent_size = 0;
  1620. relink_bitmap_entry(ctl, info);
  1621. if (start && test_bit(start - 1, info->bitmap))
  1622. extent_delta++;
  1623. if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
  1624. extent_delta++;
  1625. info->bitmap_extents += extent_delta;
  1626. if (!btrfs_free_space_trimmed(info)) {
  1627. ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
  1628. ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
  1629. }
  1630. if (update_stat)
  1631. ctl->free_space -= bytes;
  1632. }
  1633. static void btrfs_bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
  1634. struct btrfs_free_space *info, u64 offset,
  1635. u64 bytes)
  1636. {
  1637. unsigned long start, count, end;
  1638. int extent_delta = 1;
  1639. start = offset_to_bit(info->offset, ctl->unit, offset);
  1640. count = bytes_to_bits(bytes, ctl->unit);
  1641. end = start + count;
  1642. ASSERT(end <= BITS_PER_BITMAP);
  1643. bitmap_set(info->bitmap, start, count);
  1644. /*
  1645. * We set some bytes, we have no idea what the max extent size is
  1646. * anymore.
  1647. */
  1648. info->max_extent_size = 0;
  1649. info->bytes += bytes;
  1650. ctl->free_space += bytes;
  1651. relink_bitmap_entry(ctl, info);
  1652. if (start && test_bit(start - 1, info->bitmap))
  1653. extent_delta--;
  1654. if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
  1655. extent_delta--;
  1656. info->bitmap_extents += extent_delta;
  1657. if (!btrfs_free_space_trimmed(info)) {
  1658. ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
  1659. ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes;
  1660. }
  1661. }
  1662. /*
  1663. * If we can not find suitable extent, we will use bytes to record
  1664. * the size of the max extent.
  1665. */
  1666. static int search_bitmap(struct btrfs_free_space_ctl *ctl,
  1667. struct btrfs_free_space *bitmap_info, u64 *offset,
  1668. u64 *bytes, bool for_alloc)
  1669. {
  1670. unsigned long found_bits = 0;
  1671. unsigned long max_bits = 0;
  1672. unsigned long bits, i;
  1673. unsigned long next_zero;
  1674. unsigned long extent_bits;
  1675. /*
  1676. * Skip searching the bitmap if we don't have a contiguous section that
  1677. * is large enough for this allocation.
  1678. */
  1679. if (for_alloc &&
  1680. bitmap_info->max_extent_size &&
  1681. bitmap_info->max_extent_size < *bytes) {
  1682. *bytes = bitmap_info->max_extent_size;
  1683. return -1;
  1684. }
  1685. i = offset_to_bit(bitmap_info->offset, ctl->unit,
  1686. max_t(u64, *offset, bitmap_info->offset));
  1687. bits = bytes_to_bits(*bytes, ctl->unit);
  1688. for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
  1689. if (for_alloc && bits == 1) {
  1690. found_bits = 1;
  1691. break;
  1692. }
  1693. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  1694. BITS_PER_BITMAP, i);
  1695. extent_bits = next_zero - i;
  1696. if (extent_bits >= bits) {
  1697. found_bits = extent_bits;
  1698. break;
  1699. } else if (extent_bits > max_bits) {
  1700. max_bits = extent_bits;
  1701. }
  1702. i = next_zero;
  1703. }
  1704. if (found_bits) {
  1705. *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
  1706. *bytes = (u64)(found_bits) * ctl->unit;
  1707. return 0;
  1708. }
  1709. *bytes = (u64)(max_bits) * ctl->unit;
  1710. bitmap_info->max_extent_size = *bytes;
  1711. relink_bitmap_entry(ctl, bitmap_info);
  1712. return -1;
  1713. }
  1714. /* Cache the size of the max extent in bytes */
  1715. static struct btrfs_free_space *
  1716. find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
  1717. unsigned long align, u64 *max_extent_size, bool use_bytes_index)
  1718. {
  1719. struct btrfs_free_space *entry;
  1720. struct rb_node *node;
  1721. u64 tmp;
  1722. u64 align_off;
  1723. int ret;
  1724. if (!ctl->free_space_offset.rb_node)
  1725. goto out;
  1726. again:
  1727. if (use_bytes_index) {
  1728. node = rb_first_cached(&ctl->free_space_bytes);
  1729. } else {
  1730. entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset),
  1731. 0, 1);
  1732. if (!entry)
  1733. goto out;
  1734. node = &entry->offset_index;
  1735. }
  1736. for (; node; node = rb_next(node)) {
  1737. if (use_bytes_index)
  1738. entry = rb_entry(node, struct btrfs_free_space,
  1739. bytes_index);
  1740. else
  1741. entry = rb_entry(node, struct btrfs_free_space,
  1742. offset_index);
  1743. /*
  1744. * If we are using the bytes index then all subsequent entries
  1745. * in this tree are going to be < bytes, so simply set the max
  1746. * extent size and exit the loop.
  1747. *
  1748. * If we're using the offset index then we need to keep going
  1749. * through the rest of the tree.
  1750. */
  1751. if (entry->bytes < *bytes) {
  1752. *max_extent_size = max(get_max_extent_size(entry),
  1753. *max_extent_size);
  1754. if (use_bytes_index)
  1755. break;
  1756. continue;
  1757. }
  1758. /* make sure the space returned is big enough
  1759. * to match our requested alignment
  1760. */
  1761. if (*bytes >= align) {
  1762. tmp = entry->offset - ctl->start + align - 1;
  1763. tmp = div64_u64(tmp, align);
  1764. tmp = tmp * align + ctl->start;
  1765. align_off = tmp - entry->offset;
  1766. } else {
  1767. align_off = 0;
  1768. tmp = entry->offset;
  1769. }
  1770. /*
  1771. * We don't break here if we're using the bytes index because we
  1772. * may have another entry that has the correct alignment that is
  1773. * the right size, so we don't want to miss that possibility.
  1774. * At worst this adds another loop through the logic, but if we
  1775. * broke here we could prematurely ENOSPC.
  1776. */
  1777. if (entry->bytes < *bytes + align_off) {
  1778. *max_extent_size = max(get_max_extent_size(entry),
  1779. *max_extent_size);
  1780. continue;
  1781. }
  1782. if (entry->bitmap) {
  1783. struct rb_node *old_next = rb_next(node);
  1784. u64 size = *bytes;
  1785. ret = search_bitmap(ctl, entry, &tmp, &size, true);
  1786. if (!ret) {
  1787. *offset = tmp;
  1788. *bytes = size;
  1789. return entry;
  1790. } else {
  1791. *max_extent_size =
  1792. max(get_max_extent_size(entry),
  1793. *max_extent_size);
  1794. }
  1795. /*
  1796. * The bitmap may have gotten re-arranged in the space
  1797. * index here because the max_extent_size may have been
  1798. * updated. Start from the beginning again if this
  1799. * happened.
  1800. */
  1801. if (use_bytes_index && old_next != rb_next(node))
  1802. goto again;
  1803. continue;
  1804. }
  1805. *offset = tmp;
  1806. *bytes = entry->bytes - align_off;
  1807. return entry;
  1808. }
  1809. out:
  1810. return NULL;
  1811. }
  1812. static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
  1813. struct btrfs_free_space *info, u64 offset)
  1814. {
  1815. info->offset = offset_to_bitmap(ctl, offset);
  1816. info->bytes = 0;
  1817. info->bitmap_extents = 0;
  1818. INIT_LIST_HEAD(&info->list);
  1819. link_free_space(ctl, info);
  1820. ctl->total_bitmaps++;
  1821. recalculate_thresholds(ctl);
  1822. }
  1823. static void free_bitmap(struct btrfs_free_space_ctl *ctl,
  1824. struct btrfs_free_space *bitmap_info)
  1825. {
  1826. /*
  1827. * Normally when this is called, the bitmap is completely empty. However,
  1828. * if we are blowing up the free space cache for one reason or another
  1829. * via __btrfs_remove_free_space_cache(), then it may not be freed and
  1830. * we may leave stats on the table.
  1831. */
  1832. if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) {
  1833. ctl->discardable_extents[BTRFS_STAT_CURR] -=
  1834. bitmap_info->bitmap_extents;
  1835. ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes;
  1836. }
  1837. unlink_free_space(ctl, bitmap_info, true);
  1838. kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
  1839. kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
  1840. ctl->total_bitmaps--;
  1841. recalculate_thresholds(ctl);
  1842. }
  1843. static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
  1844. struct btrfs_free_space *bitmap_info,
  1845. u64 *offset, u64 *bytes)
  1846. {
  1847. u64 end;
  1848. u64 search_start, search_bytes;
  1849. int ret;
  1850. again:
  1851. end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
  1852. /*
  1853. * We need to search for bits in this bitmap. We could only cover some
  1854. * of the extent in this bitmap thanks to how we add space, so we need
  1855. * to search for as much as it as we can and clear that amount, and then
  1856. * go searching for the next bit.
  1857. */
  1858. search_start = *offset;
  1859. search_bytes = ctl->unit;
  1860. search_bytes = min(search_bytes, end - search_start + 1);
  1861. ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
  1862. false);
  1863. if (ret < 0 || search_start != *offset)
  1864. return -EINVAL;
  1865. /* We may have found more bits than what we need */
  1866. search_bytes = min(search_bytes, *bytes);
  1867. /* Cannot clear past the end of the bitmap */
  1868. search_bytes = min(search_bytes, end - search_start + 1);
  1869. bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes, true);
  1870. *offset += search_bytes;
  1871. *bytes -= search_bytes;
  1872. if (*bytes) {
  1873. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1874. if (!bitmap_info->bytes)
  1875. free_bitmap(ctl, bitmap_info);
  1876. /*
  1877. * no entry after this bitmap, but we still have bytes to
  1878. * remove, so something has gone wrong.
  1879. */
  1880. if (!next)
  1881. return -EINVAL;
  1882. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1883. offset_index);
  1884. /*
  1885. * if the next entry isn't a bitmap we need to return to let the
  1886. * extent stuff do its work.
  1887. */
  1888. if (!bitmap_info->bitmap)
  1889. return -EAGAIN;
  1890. /*
  1891. * Ok the next item is a bitmap, but it may not actually hold
  1892. * the information for the rest of this free space stuff, so
  1893. * look for it, and if we don't find it return so we can try
  1894. * everything over again.
  1895. */
  1896. search_start = *offset;
  1897. search_bytes = ctl->unit;
  1898. ret = search_bitmap(ctl, bitmap_info, &search_start,
  1899. &search_bytes, false);
  1900. if (ret < 0 || search_start != *offset)
  1901. return -EAGAIN;
  1902. goto again;
  1903. } else if (!bitmap_info->bytes)
  1904. free_bitmap(ctl, bitmap_info);
  1905. return 0;
  1906. }
  1907. static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
  1908. struct btrfs_free_space *info, u64 offset,
  1909. u64 bytes, enum btrfs_trim_state trim_state)
  1910. {
  1911. u64 bytes_to_set = 0;
  1912. u64 end;
  1913. /*
  1914. * This is a tradeoff to make bitmap trim state minimal. We mark the
  1915. * whole bitmap untrimmed if at any point we add untrimmed regions.
  1916. */
  1917. if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) {
  1918. if (btrfs_free_space_trimmed(info)) {
  1919. ctl->discardable_extents[BTRFS_STAT_CURR] +=
  1920. info->bitmap_extents;
  1921. ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
  1922. }
  1923. info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  1924. }
  1925. end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
  1926. bytes_to_set = min(end - offset, bytes);
  1927. btrfs_bitmap_set_bits(ctl, info, offset, bytes_to_set);
  1928. return bytes_to_set;
  1929. }
  1930. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  1931. struct btrfs_free_space *info)
  1932. {
  1933. struct btrfs_block_group *block_group = ctl->block_group;
  1934. struct btrfs_fs_info *fs_info = block_group->fs_info;
  1935. bool forced = false;
  1936. #ifdef CONFIG_BTRFS_DEBUG
  1937. if (btrfs_should_fragment_free_space(block_group))
  1938. forced = true;
  1939. #endif
  1940. /* This is a way to reclaim large regions from the bitmaps. */
  1941. if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD)
  1942. return false;
  1943. /*
  1944. * If we are below the extents threshold then we can add this as an
  1945. * extent, and don't have to deal with the bitmap
  1946. */
  1947. if (!forced && ctl->free_extents < ctl->extents_thresh) {
  1948. /*
  1949. * If this block group has some small extents we don't want to
  1950. * use up all of our free slots in the cache with them, we want
  1951. * to reserve them to larger extents, however if we have plenty
  1952. * of cache left then go ahead an dadd them, no sense in adding
  1953. * the overhead of a bitmap if we don't have to.
  1954. */
  1955. if (info->bytes <= fs_info->sectorsize * 8) {
  1956. if (ctl->free_extents * 3 <= ctl->extents_thresh)
  1957. return false;
  1958. } else {
  1959. return false;
  1960. }
  1961. }
  1962. /*
  1963. * The original block groups from mkfs can be really small, like 8
  1964. * megabytes, so don't bother with a bitmap for those entries. However
  1965. * some block groups can be smaller than what a bitmap would cover but
  1966. * are still large enough that they could overflow the 32k memory limit,
  1967. * so allow those block groups to still be allowed to have a bitmap
  1968. * entry.
  1969. */
  1970. if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
  1971. return false;
  1972. return true;
  1973. }
  1974. static const struct btrfs_free_space_op free_space_op = {
  1975. .use_bitmap = use_bitmap,
  1976. };
  1977. static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
  1978. struct btrfs_free_space *info)
  1979. {
  1980. struct btrfs_free_space *bitmap_info;
  1981. struct btrfs_block_group *block_group = NULL;
  1982. int added = 0;
  1983. u64 bytes, offset, bytes_added;
  1984. enum btrfs_trim_state trim_state;
  1985. int ret;
  1986. bytes = info->bytes;
  1987. offset = info->offset;
  1988. trim_state = info->trim_state;
  1989. if (!ctl->op->use_bitmap(ctl, info))
  1990. return 0;
  1991. if (ctl->op == &free_space_op)
  1992. block_group = ctl->block_group;
  1993. again:
  1994. /*
  1995. * Since we link bitmaps right into the cluster we need to see if we
  1996. * have a cluster here, and if so and it has our bitmap we need to add
  1997. * the free space to that bitmap.
  1998. */
  1999. if (block_group && !list_empty(&block_group->cluster_list)) {
  2000. struct btrfs_free_cluster *cluster;
  2001. struct rb_node *node;
  2002. struct btrfs_free_space *entry;
  2003. cluster = list_entry(block_group->cluster_list.next,
  2004. struct btrfs_free_cluster,
  2005. block_group_list);
  2006. spin_lock(&cluster->lock);
  2007. node = rb_first(&cluster->root);
  2008. if (!node) {
  2009. spin_unlock(&cluster->lock);
  2010. goto no_cluster_bitmap;
  2011. }
  2012. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2013. if (!entry->bitmap) {
  2014. spin_unlock(&cluster->lock);
  2015. goto no_cluster_bitmap;
  2016. }
  2017. if (entry->offset == offset_to_bitmap(ctl, offset)) {
  2018. bytes_added = add_bytes_to_bitmap(ctl, entry, offset,
  2019. bytes, trim_state);
  2020. bytes -= bytes_added;
  2021. offset += bytes_added;
  2022. }
  2023. spin_unlock(&cluster->lock);
  2024. if (!bytes) {
  2025. ret = 1;
  2026. goto out;
  2027. }
  2028. }
  2029. no_cluster_bitmap:
  2030. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  2031. 1, 0);
  2032. if (!bitmap_info) {
  2033. ASSERT(added == 0);
  2034. goto new_bitmap;
  2035. }
  2036. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
  2037. trim_state);
  2038. bytes -= bytes_added;
  2039. offset += bytes_added;
  2040. added = 0;
  2041. if (!bytes) {
  2042. ret = 1;
  2043. goto out;
  2044. } else
  2045. goto again;
  2046. new_bitmap:
  2047. if (info && info->bitmap) {
  2048. add_new_bitmap(ctl, info, offset);
  2049. added = 1;
  2050. info = NULL;
  2051. goto again;
  2052. } else {
  2053. spin_unlock(&ctl->tree_lock);
  2054. /* no pre-allocated info, allocate a new one */
  2055. if (!info) {
  2056. info = kmem_cache_zalloc(btrfs_free_space_cachep,
  2057. GFP_NOFS);
  2058. if (!info) {
  2059. spin_lock(&ctl->tree_lock);
  2060. ret = -ENOMEM;
  2061. goto out;
  2062. }
  2063. }
  2064. /* allocate the bitmap */
  2065. info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
  2066. GFP_NOFS);
  2067. info->trim_state = BTRFS_TRIM_STATE_TRIMMED;
  2068. spin_lock(&ctl->tree_lock);
  2069. if (!info->bitmap) {
  2070. ret = -ENOMEM;
  2071. goto out;
  2072. }
  2073. goto again;
  2074. }
  2075. out:
  2076. if (info) {
  2077. if (info->bitmap)
  2078. kmem_cache_free(btrfs_free_space_bitmap_cachep,
  2079. info->bitmap);
  2080. kmem_cache_free(btrfs_free_space_cachep, info);
  2081. }
  2082. return ret;
  2083. }
  2084. /*
  2085. * Free space merging rules:
  2086. * 1) Merge trimmed areas together
  2087. * 2) Let untrimmed areas coalesce with trimmed areas
  2088. * 3) Always pull neighboring regions from bitmaps
  2089. *
  2090. * The above rules are for when we merge free space based on btrfs_trim_state.
  2091. * Rules 2 and 3 are subtle because they are suboptimal, but are done for the
  2092. * same reason: to promote larger extent regions which makes life easier for
  2093. * find_free_extent(). Rule 2 enables coalescing based on the common path
  2094. * being returning free space from btrfs_finish_extent_commit(). So when free
  2095. * space is trimmed, it will prevent aggregating trimmed new region and
  2096. * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents
  2097. * and provide find_free_extent() with the largest extents possible hoping for
  2098. * the reuse path.
  2099. */
  2100. static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
  2101. struct btrfs_free_space *info, bool update_stat)
  2102. {
  2103. struct btrfs_free_space *left_info = NULL;
  2104. struct btrfs_free_space *right_info;
  2105. bool merged = false;
  2106. u64 offset = info->offset;
  2107. u64 bytes = info->bytes;
  2108. const bool is_trimmed = btrfs_free_space_trimmed(info);
  2109. struct rb_node *right_prev = NULL;
  2110. /*
  2111. * first we want to see if there is free space adjacent to the range we
  2112. * are adding, if there is remove that struct and add a new one to
  2113. * cover the entire range
  2114. */
  2115. right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
  2116. if (right_info)
  2117. right_prev = rb_prev(&right_info->offset_index);
  2118. if (right_prev)
  2119. left_info = rb_entry(right_prev, struct btrfs_free_space, offset_index);
  2120. else if (!right_info)
  2121. left_info = tree_search_offset(ctl, offset - 1, 0, 0);
  2122. /* See try_merge_free_space() comment. */
  2123. if (right_info && !right_info->bitmap &&
  2124. (!is_trimmed || btrfs_free_space_trimmed(right_info))) {
  2125. unlink_free_space(ctl, right_info, update_stat);
  2126. info->bytes += right_info->bytes;
  2127. kmem_cache_free(btrfs_free_space_cachep, right_info);
  2128. merged = true;
  2129. }
  2130. /* See try_merge_free_space() comment. */
  2131. if (left_info && !left_info->bitmap &&
  2132. left_info->offset + left_info->bytes == offset &&
  2133. (!is_trimmed || btrfs_free_space_trimmed(left_info))) {
  2134. unlink_free_space(ctl, left_info, update_stat);
  2135. info->offset = left_info->offset;
  2136. info->bytes += left_info->bytes;
  2137. kmem_cache_free(btrfs_free_space_cachep, left_info);
  2138. merged = true;
  2139. }
  2140. return merged;
  2141. }
  2142. static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
  2143. struct btrfs_free_space *info,
  2144. bool update_stat)
  2145. {
  2146. struct btrfs_free_space *bitmap;
  2147. unsigned long i;
  2148. unsigned long j;
  2149. const u64 end = info->offset + info->bytes;
  2150. const u64 bitmap_offset = offset_to_bitmap(ctl, end);
  2151. u64 bytes;
  2152. bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
  2153. if (!bitmap)
  2154. return false;
  2155. i = offset_to_bit(bitmap->offset, ctl->unit, end);
  2156. j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
  2157. if (j == i)
  2158. return false;
  2159. bytes = (j - i) * ctl->unit;
  2160. info->bytes += bytes;
  2161. /* See try_merge_free_space() comment. */
  2162. if (!btrfs_free_space_trimmed(bitmap))
  2163. info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  2164. bitmap_clear_bits(ctl, bitmap, end, bytes, update_stat);
  2165. if (!bitmap->bytes)
  2166. free_bitmap(ctl, bitmap);
  2167. return true;
  2168. }
  2169. static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
  2170. struct btrfs_free_space *info,
  2171. bool update_stat)
  2172. {
  2173. struct btrfs_free_space *bitmap;
  2174. u64 bitmap_offset;
  2175. unsigned long i;
  2176. unsigned long j;
  2177. unsigned long prev_j;
  2178. u64 bytes;
  2179. bitmap_offset = offset_to_bitmap(ctl, info->offset);
  2180. /* If we're on a boundary, try the previous logical bitmap. */
  2181. if (bitmap_offset == info->offset) {
  2182. if (info->offset == 0)
  2183. return false;
  2184. bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
  2185. }
  2186. bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
  2187. if (!bitmap)
  2188. return false;
  2189. i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
  2190. j = 0;
  2191. prev_j = (unsigned long)-1;
  2192. for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
  2193. if (j > i)
  2194. break;
  2195. prev_j = j;
  2196. }
  2197. if (prev_j == i)
  2198. return false;
  2199. if (prev_j == (unsigned long)-1)
  2200. bytes = (i + 1) * ctl->unit;
  2201. else
  2202. bytes = (i - prev_j) * ctl->unit;
  2203. info->offset -= bytes;
  2204. info->bytes += bytes;
  2205. /* See try_merge_free_space() comment. */
  2206. if (!btrfs_free_space_trimmed(bitmap))
  2207. info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  2208. bitmap_clear_bits(ctl, bitmap, info->offset, bytes, update_stat);
  2209. if (!bitmap->bytes)
  2210. free_bitmap(ctl, bitmap);
  2211. return true;
  2212. }
  2213. /*
  2214. * We prefer always to allocate from extent entries, both for clustered and
  2215. * non-clustered allocation requests. So when attempting to add a new extent
  2216. * entry, try to see if there's adjacent free space in bitmap entries, and if
  2217. * there is, migrate that space from the bitmaps to the extent.
  2218. * Like this we get better chances of satisfying space allocation requests
  2219. * because we attempt to satisfy them based on a single cache entry, and never
  2220. * on 2 or more entries - even if the entries represent a contiguous free space
  2221. * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
  2222. * ends).
  2223. */
  2224. static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
  2225. struct btrfs_free_space *info,
  2226. bool update_stat)
  2227. {
  2228. /*
  2229. * Only work with disconnected entries, as we can change their offset,
  2230. * and must be extent entries.
  2231. */
  2232. ASSERT(!info->bitmap);
  2233. ASSERT(RB_EMPTY_NODE(&info->offset_index));
  2234. if (ctl->total_bitmaps > 0) {
  2235. bool stole_end;
  2236. bool stole_front = false;
  2237. stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
  2238. if (ctl->total_bitmaps > 0)
  2239. stole_front = steal_from_bitmap_to_front(ctl, info,
  2240. update_stat);
  2241. if (stole_end || stole_front)
  2242. try_merge_free_space(ctl, info, update_stat);
  2243. }
  2244. }
  2245. static int __btrfs_add_free_space(struct btrfs_block_group *block_group,
  2246. u64 offset, u64 bytes,
  2247. enum btrfs_trim_state trim_state)
  2248. {
  2249. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2250. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2251. struct btrfs_free_space *info;
  2252. int ret = 0;
  2253. u64 filter_bytes = bytes;
  2254. ASSERT(!btrfs_is_zoned(fs_info));
  2255. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  2256. if (!info)
  2257. return -ENOMEM;
  2258. info->offset = offset;
  2259. info->bytes = bytes;
  2260. info->trim_state = trim_state;
  2261. RB_CLEAR_NODE(&info->offset_index);
  2262. RB_CLEAR_NODE(&info->bytes_index);
  2263. spin_lock(&ctl->tree_lock);
  2264. if (try_merge_free_space(ctl, info, true))
  2265. goto link;
  2266. /*
  2267. * There was no extent directly to the left or right of this new
  2268. * extent then we know we're going to have to allocate a new extent, so
  2269. * before we do that see if we need to drop this into a bitmap
  2270. */
  2271. ret = insert_into_bitmap(ctl, info);
  2272. if (ret < 0) {
  2273. goto out;
  2274. } else if (ret) {
  2275. ret = 0;
  2276. goto out;
  2277. }
  2278. link:
  2279. /*
  2280. * Only steal free space from adjacent bitmaps if we're sure we're not
  2281. * going to add the new free space to existing bitmap entries - because
  2282. * that would mean unnecessary work that would be reverted. Therefore
  2283. * attempt to steal space from bitmaps if we're adding an extent entry.
  2284. */
  2285. steal_from_bitmap(ctl, info, true);
  2286. filter_bytes = max(filter_bytes, info->bytes);
  2287. ret = link_free_space(ctl, info);
  2288. if (ret)
  2289. kmem_cache_free(btrfs_free_space_cachep, info);
  2290. out:
  2291. btrfs_discard_update_discardable(block_group);
  2292. spin_unlock(&ctl->tree_lock);
  2293. if (ret) {
  2294. btrfs_crit(fs_info, "unable to add free space :%d", ret);
  2295. ASSERT(ret != -EEXIST);
  2296. }
  2297. if (trim_state != BTRFS_TRIM_STATE_TRIMMED) {
  2298. btrfs_discard_check_filter(block_group, filter_bytes);
  2299. btrfs_discard_queue_work(&fs_info->discard_ctl, block_group);
  2300. }
  2301. return ret;
  2302. }
  2303. static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group,
  2304. u64 bytenr, u64 size, bool used)
  2305. {
  2306. struct btrfs_space_info *sinfo = block_group->space_info;
  2307. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2308. u64 offset = bytenr - block_group->start;
  2309. u64 to_free, to_unusable;
  2310. int bg_reclaim_threshold = 0;
  2311. bool initial;
  2312. u64 reclaimable_unusable;
  2313. spin_lock(&block_group->lock);
  2314. initial = ((size == block_group->length) && (block_group->alloc_offset == 0));
  2315. WARN_ON(!initial && offset + size > block_group->zone_capacity);
  2316. if (!initial)
  2317. bg_reclaim_threshold = READ_ONCE(sinfo->bg_reclaim_threshold);
  2318. if (!used)
  2319. to_free = size;
  2320. else if (initial)
  2321. to_free = block_group->zone_capacity;
  2322. else if (offset >= block_group->alloc_offset)
  2323. to_free = size;
  2324. else if (offset + size <= block_group->alloc_offset)
  2325. to_free = 0;
  2326. else
  2327. to_free = offset + size - block_group->alloc_offset;
  2328. to_unusable = size - to_free;
  2329. spin_lock(&ctl->tree_lock);
  2330. ctl->free_space += to_free;
  2331. spin_unlock(&ctl->tree_lock);
  2332. /*
  2333. * If the block group is read-only, we should account freed space into
  2334. * bytes_readonly.
  2335. */
  2336. if (!block_group->ro) {
  2337. block_group->zone_unusable += to_unusable;
  2338. WARN_ON(block_group->zone_unusable > block_group->length);
  2339. }
  2340. if (!used) {
  2341. block_group->alloc_offset -= size;
  2342. }
  2343. reclaimable_unusable = block_group->zone_unusable -
  2344. (block_group->length - block_group->zone_capacity);
  2345. /* All the region is now unusable. Mark it as unused and reclaim */
  2346. if (block_group->zone_unusable == block_group->length) {
  2347. btrfs_mark_bg_unused(block_group);
  2348. } else if (bg_reclaim_threshold &&
  2349. reclaimable_unusable >=
  2350. mult_perc(block_group->zone_capacity, bg_reclaim_threshold)) {
  2351. btrfs_mark_bg_to_reclaim(block_group);
  2352. }
  2353. spin_unlock(&block_group->lock);
  2354. return 0;
  2355. }
  2356. int btrfs_add_free_space(struct btrfs_block_group *block_group,
  2357. u64 bytenr, u64 size)
  2358. {
  2359. enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  2360. if (btrfs_is_zoned(block_group->fs_info))
  2361. return __btrfs_add_free_space_zoned(block_group, bytenr, size,
  2362. true);
  2363. if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC))
  2364. trim_state = BTRFS_TRIM_STATE_TRIMMED;
  2365. return __btrfs_add_free_space(block_group, bytenr, size, trim_state);
  2366. }
  2367. int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
  2368. u64 bytenr, u64 size)
  2369. {
  2370. if (btrfs_is_zoned(block_group->fs_info))
  2371. return __btrfs_add_free_space_zoned(block_group, bytenr, size,
  2372. false);
  2373. return btrfs_add_free_space(block_group, bytenr, size);
  2374. }
  2375. /*
  2376. * This is a subtle distinction because when adding free space back in general,
  2377. * we want it to be added as untrimmed for async. But in the case where we add
  2378. * it on loading of a block group, we want to consider it trimmed.
  2379. */
  2380. int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
  2381. u64 bytenr, u64 size)
  2382. {
  2383. enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  2384. if (btrfs_is_zoned(block_group->fs_info))
  2385. return __btrfs_add_free_space_zoned(block_group, bytenr, size,
  2386. true);
  2387. if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) ||
  2388. btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
  2389. trim_state = BTRFS_TRIM_STATE_TRIMMED;
  2390. return __btrfs_add_free_space(block_group, bytenr, size, trim_state);
  2391. }
  2392. int btrfs_remove_free_space(struct btrfs_block_group *block_group,
  2393. u64 offset, u64 bytes)
  2394. {
  2395. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2396. struct btrfs_free_space *info;
  2397. int ret;
  2398. bool re_search = false;
  2399. if (btrfs_is_zoned(block_group->fs_info)) {
  2400. /*
  2401. * This can happen with conventional zones when replaying log.
  2402. * Since the allocation info of tree-log nodes are not recorded
  2403. * to the extent-tree, calculate_alloc_pointer() failed to
  2404. * advance the allocation pointer after last allocated tree log
  2405. * node blocks.
  2406. *
  2407. * This function is called from
  2408. * btrfs_pin_extent_for_log_replay() when replaying the log.
  2409. * Advance the pointer not to overwrite the tree-log nodes.
  2410. */
  2411. if (block_group->start + block_group->alloc_offset <
  2412. offset + bytes) {
  2413. block_group->alloc_offset =
  2414. offset + bytes - block_group->start;
  2415. }
  2416. return 0;
  2417. }
  2418. spin_lock(&ctl->tree_lock);
  2419. again:
  2420. ret = 0;
  2421. if (!bytes)
  2422. goto out_lock;
  2423. info = tree_search_offset(ctl, offset, 0, 0);
  2424. if (!info) {
  2425. /*
  2426. * oops didn't find an extent that matched the space we wanted
  2427. * to remove, look for a bitmap instead
  2428. */
  2429. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  2430. 1, 0);
  2431. if (!info) {
  2432. /*
  2433. * If we found a partial bit of our free space in a
  2434. * bitmap but then couldn't find the other part this may
  2435. * be a problem, so WARN about it.
  2436. */
  2437. WARN_ON(re_search);
  2438. goto out_lock;
  2439. }
  2440. }
  2441. re_search = false;
  2442. if (!info->bitmap) {
  2443. unlink_free_space(ctl, info, true);
  2444. if (offset == info->offset) {
  2445. u64 to_free = min(bytes, info->bytes);
  2446. info->bytes -= to_free;
  2447. info->offset += to_free;
  2448. if (info->bytes) {
  2449. ret = link_free_space(ctl, info);
  2450. WARN_ON(ret);
  2451. } else {
  2452. kmem_cache_free(btrfs_free_space_cachep, info);
  2453. }
  2454. offset += to_free;
  2455. bytes -= to_free;
  2456. goto again;
  2457. } else {
  2458. u64 old_end = info->bytes + info->offset;
  2459. info->bytes = offset - info->offset;
  2460. ret = link_free_space(ctl, info);
  2461. WARN_ON(ret);
  2462. if (ret)
  2463. goto out_lock;
  2464. /* Not enough bytes in this entry to satisfy us */
  2465. if (old_end < offset + bytes) {
  2466. bytes -= old_end - offset;
  2467. offset = old_end;
  2468. goto again;
  2469. } else if (old_end == offset + bytes) {
  2470. /* all done */
  2471. goto out_lock;
  2472. }
  2473. spin_unlock(&ctl->tree_lock);
  2474. ret = __btrfs_add_free_space(block_group,
  2475. offset + bytes,
  2476. old_end - (offset + bytes),
  2477. info->trim_state);
  2478. WARN_ON(ret);
  2479. goto out;
  2480. }
  2481. }
  2482. ret = remove_from_bitmap(ctl, info, &offset, &bytes);
  2483. if (ret == -EAGAIN) {
  2484. re_search = true;
  2485. goto again;
  2486. }
  2487. out_lock:
  2488. btrfs_discard_update_discardable(block_group);
  2489. spin_unlock(&ctl->tree_lock);
  2490. out:
  2491. return ret;
  2492. }
  2493. void btrfs_dump_free_space(struct btrfs_block_group *block_group,
  2494. u64 bytes)
  2495. {
  2496. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2497. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2498. struct btrfs_free_space *info;
  2499. struct rb_node *n;
  2500. int count = 0;
  2501. /*
  2502. * Zoned btrfs does not use free space tree and cluster. Just print
  2503. * out the free space after the allocation offset.
  2504. */
  2505. if (btrfs_is_zoned(fs_info)) {
  2506. btrfs_info(fs_info, "free space %llu active %d",
  2507. block_group->zone_capacity - block_group->alloc_offset,
  2508. test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
  2509. &block_group->runtime_flags));
  2510. return;
  2511. }
  2512. spin_lock(&ctl->tree_lock);
  2513. for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
  2514. info = rb_entry(n, struct btrfs_free_space, offset_index);
  2515. if (info->bytes >= bytes && !block_group->ro)
  2516. count++;
  2517. btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
  2518. info->offset, info->bytes,
  2519. (info->bitmap) ? "yes" : "no");
  2520. }
  2521. spin_unlock(&ctl->tree_lock);
  2522. btrfs_info(fs_info, "block group has cluster?: %s",
  2523. list_empty(&block_group->cluster_list) ? "no" : "yes");
  2524. btrfs_info(fs_info,
  2525. "%d free space entries at or bigger than %llu bytes",
  2526. count, bytes);
  2527. }
  2528. void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
  2529. struct btrfs_free_space_ctl *ctl)
  2530. {
  2531. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2532. spin_lock_init(&ctl->tree_lock);
  2533. ctl->unit = fs_info->sectorsize;
  2534. ctl->start = block_group->start;
  2535. ctl->block_group = block_group;
  2536. ctl->op = &free_space_op;
  2537. ctl->free_space_bytes = RB_ROOT_CACHED;
  2538. INIT_LIST_HEAD(&ctl->trimming_ranges);
  2539. mutex_init(&ctl->cache_writeout_mutex);
  2540. /*
  2541. * we only want to have 32k of ram per block group for keeping
  2542. * track of free space, and if we pass 1/2 of that we want to
  2543. * start converting things over to using bitmaps
  2544. */
  2545. ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
  2546. }
  2547. /*
  2548. * for a given cluster, put all of its extents back into the free
  2549. * space cache. If the block group passed doesn't match the block group
  2550. * pointed to by the cluster, someone else raced in and freed the
  2551. * cluster already. In that case, we just return without changing anything
  2552. */
  2553. static void __btrfs_return_cluster_to_free_space(
  2554. struct btrfs_block_group *block_group,
  2555. struct btrfs_free_cluster *cluster)
  2556. {
  2557. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2558. struct rb_node *node;
  2559. lockdep_assert_held(&ctl->tree_lock);
  2560. spin_lock(&cluster->lock);
  2561. if (cluster->block_group != block_group) {
  2562. spin_unlock(&cluster->lock);
  2563. return;
  2564. }
  2565. cluster->block_group = NULL;
  2566. cluster->window_start = 0;
  2567. list_del_init(&cluster->block_group_list);
  2568. node = rb_first(&cluster->root);
  2569. while (node) {
  2570. struct btrfs_free_space *entry;
  2571. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2572. node = rb_next(&entry->offset_index);
  2573. rb_erase(&entry->offset_index, &cluster->root);
  2574. RB_CLEAR_NODE(&entry->offset_index);
  2575. if (!entry->bitmap) {
  2576. /* Merging treats extents as if they were new */
  2577. if (!btrfs_free_space_trimmed(entry)) {
  2578. ctl->discardable_extents[BTRFS_STAT_CURR]--;
  2579. ctl->discardable_bytes[BTRFS_STAT_CURR] -=
  2580. entry->bytes;
  2581. }
  2582. try_merge_free_space(ctl, entry, false);
  2583. steal_from_bitmap(ctl, entry, false);
  2584. /* As we insert directly, update these statistics */
  2585. if (!btrfs_free_space_trimmed(entry)) {
  2586. ctl->discardable_extents[BTRFS_STAT_CURR]++;
  2587. ctl->discardable_bytes[BTRFS_STAT_CURR] +=
  2588. entry->bytes;
  2589. }
  2590. }
  2591. tree_insert_offset(ctl, NULL, entry);
  2592. rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes,
  2593. entry_less);
  2594. }
  2595. cluster->root = RB_ROOT;
  2596. spin_unlock(&cluster->lock);
  2597. btrfs_put_block_group(block_group);
  2598. }
  2599. void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
  2600. {
  2601. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2602. struct btrfs_free_cluster *cluster;
  2603. struct list_head *head;
  2604. spin_lock(&ctl->tree_lock);
  2605. while ((head = block_group->cluster_list.next) !=
  2606. &block_group->cluster_list) {
  2607. cluster = list_entry(head, struct btrfs_free_cluster,
  2608. block_group_list);
  2609. WARN_ON(cluster->block_group != block_group);
  2610. __btrfs_return_cluster_to_free_space(block_group, cluster);
  2611. cond_resched_lock(&ctl->tree_lock);
  2612. }
  2613. __btrfs_remove_free_space_cache(ctl);
  2614. btrfs_discard_update_discardable(block_group);
  2615. spin_unlock(&ctl->tree_lock);
  2616. }
  2617. /*
  2618. * Walk @block_group's free space rb_tree to determine if everything is trimmed.
  2619. */
  2620. bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group)
  2621. {
  2622. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2623. struct btrfs_free_space *info;
  2624. struct rb_node *node;
  2625. bool ret = true;
  2626. spin_lock(&ctl->tree_lock);
  2627. node = rb_first(&ctl->free_space_offset);
  2628. while (node) {
  2629. info = rb_entry(node, struct btrfs_free_space, offset_index);
  2630. if (!btrfs_free_space_trimmed(info)) {
  2631. ret = false;
  2632. break;
  2633. }
  2634. node = rb_next(node);
  2635. }
  2636. spin_unlock(&ctl->tree_lock);
  2637. return ret;
  2638. }
  2639. u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
  2640. u64 offset, u64 bytes, u64 empty_size,
  2641. u64 *max_extent_size)
  2642. {
  2643. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2644. struct btrfs_discard_ctl *discard_ctl =
  2645. &block_group->fs_info->discard_ctl;
  2646. struct btrfs_free_space *entry = NULL;
  2647. u64 bytes_search = bytes + empty_size;
  2648. u64 ret = 0;
  2649. u64 align_gap = 0;
  2650. u64 align_gap_len = 0;
  2651. enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  2652. bool use_bytes_index = (offset == block_group->start);
  2653. ASSERT(!btrfs_is_zoned(block_group->fs_info));
  2654. spin_lock(&ctl->tree_lock);
  2655. entry = find_free_space(ctl, &offset, &bytes_search,
  2656. block_group->full_stripe_len, max_extent_size,
  2657. use_bytes_index);
  2658. if (!entry)
  2659. goto out;
  2660. ret = offset;
  2661. if (entry->bitmap) {
  2662. bitmap_clear_bits(ctl, entry, offset, bytes, true);
  2663. if (!btrfs_free_space_trimmed(entry))
  2664. atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
  2665. if (!entry->bytes)
  2666. free_bitmap(ctl, entry);
  2667. } else {
  2668. unlink_free_space(ctl, entry, true);
  2669. align_gap_len = offset - entry->offset;
  2670. align_gap = entry->offset;
  2671. align_gap_trim_state = entry->trim_state;
  2672. if (!btrfs_free_space_trimmed(entry))
  2673. atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
  2674. entry->offset = offset + bytes;
  2675. WARN_ON(entry->bytes < bytes + align_gap_len);
  2676. entry->bytes -= bytes + align_gap_len;
  2677. if (!entry->bytes)
  2678. kmem_cache_free(btrfs_free_space_cachep, entry);
  2679. else
  2680. link_free_space(ctl, entry);
  2681. }
  2682. out:
  2683. btrfs_discard_update_discardable(block_group);
  2684. spin_unlock(&ctl->tree_lock);
  2685. if (align_gap_len)
  2686. __btrfs_add_free_space(block_group, align_gap, align_gap_len,
  2687. align_gap_trim_state);
  2688. return ret;
  2689. }
  2690. /*
  2691. * given a cluster, put all of its extents back into the free space
  2692. * cache. If a block group is passed, this function will only free
  2693. * a cluster that belongs to the passed block group.
  2694. *
  2695. * Otherwise, it'll get a reference on the block group pointed to by the
  2696. * cluster and remove the cluster from it.
  2697. */
  2698. void btrfs_return_cluster_to_free_space(
  2699. struct btrfs_block_group *block_group,
  2700. struct btrfs_free_cluster *cluster)
  2701. {
  2702. struct btrfs_free_space_ctl *ctl;
  2703. /* first, get a safe pointer to the block group */
  2704. spin_lock(&cluster->lock);
  2705. if (!block_group) {
  2706. block_group = cluster->block_group;
  2707. if (!block_group) {
  2708. spin_unlock(&cluster->lock);
  2709. return;
  2710. }
  2711. } else if (cluster->block_group != block_group) {
  2712. /* someone else has already freed it don't redo their work */
  2713. spin_unlock(&cluster->lock);
  2714. return;
  2715. }
  2716. btrfs_get_block_group(block_group);
  2717. spin_unlock(&cluster->lock);
  2718. ctl = block_group->free_space_ctl;
  2719. /* now return any extents the cluster had on it */
  2720. spin_lock(&ctl->tree_lock);
  2721. __btrfs_return_cluster_to_free_space(block_group, cluster);
  2722. spin_unlock(&ctl->tree_lock);
  2723. btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group);
  2724. /* finally drop our ref */
  2725. btrfs_put_block_group(block_group);
  2726. }
  2727. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
  2728. struct btrfs_free_cluster *cluster,
  2729. struct btrfs_free_space *entry,
  2730. u64 bytes, u64 min_start,
  2731. u64 *max_extent_size)
  2732. {
  2733. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2734. int err;
  2735. u64 search_start = cluster->window_start;
  2736. u64 search_bytes = bytes;
  2737. u64 ret = 0;
  2738. search_start = min_start;
  2739. search_bytes = bytes;
  2740. err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
  2741. if (err) {
  2742. *max_extent_size = max(get_max_extent_size(entry),
  2743. *max_extent_size);
  2744. return 0;
  2745. }
  2746. ret = search_start;
  2747. bitmap_clear_bits(ctl, entry, ret, bytes, false);
  2748. return ret;
  2749. }
  2750. /*
  2751. * given a cluster, try to allocate 'bytes' from it, returns 0
  2752. * if it couldn't find anything suitably large, or a logical disk offset
  2753. * if things worked out
  2754. */
  2755. u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
  2756. struct btrfs_free_cluster *cluster, u64 bytes,
  2757. u64 min_start, u64 *max_extent_size)
  2758. {
  2759. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2760. struct btrfs_discard_ctl *discard_ctl =
  2761. &block_group->fs_info->discard_ctl;
  2762. struct btrfs_free_space *entry = NULL;
  2763. struct rb_node *node;
  2764. u64 ret = 0;
  2765. ASSERT(!btrfs_is_zoned(block_group->fs_info));
  2766. spin_lock(&cluster->lock);
  2767. if (bytes > cluster->max_size)
  2768. goto out;
  2769. if (cluster->block_group != block_group)
  2770. goto out;
  2771. node = rb_first(&cluster->root);
  2772. if (!node)
  2773. goto out;
  2774. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2775. while (1) {
  2776. if (entry->bytes < bytes)
  2777. *max_extent_size = max(get_max_extent_size(entry),
  2778. *max_extent_size);
  2779. if (entry->bytes < bytes ||
  2780. (!entry->bitmap && entry->offset < min_start)) {
  2781. node = rb_next(&entry->offset_index);
  2782. if (!node)
  2783. break;
  2784. entry = rb_entry(node, struct btrfs_free_space,
  2785. offset_index);
  2786. continue;
  2787. }
  2788. if (entry->bitmap) {
  2789. ret = btrfs_alloc_from_bitmap(block_group,
  2790. cluster, entry, bytes,
  2791. cluster->window_start,
  2792. max_extent_size);
  2793. if (ret == 0) {
  2794. node = rb_next(&entry->offset_index);
  2795. if (!node)
  2796. break;
  2797. entry = rb_entry(node, struct btrfs_free_space,
  2798. offset_index);
  2799. continue;
  2800. }
  2801. cluster->window_start += bytes;
  2802. } else {
  2803. ret = entry->offset;
  2804. entry->offset += bytes;
  2805. entry->bytes -= bytes;
  2806. }
  2807. break;
  2808. }
  2809. out:
  2810. spin_unlock(&cluster->lock);
  2811. if (!ret)
  2812. return 0;
  2813. spin_lock(&ctl->tree_lock);
  2814. if (!btrfs_free_space_trimmed(entry))
  2815. atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
  2816. ctl->free_space -= bytes;
  2817. if (!entry->bitmap && !btrfs_free_space_trimmed(entry))
  2818. ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
  2819. spin_lock(&cluster->lock);
  2820. if (entry->bytes == 0) {
  2821. rb_erase(&entry->offset_index, &cluster->root);
  2822. ctl->free_extents--;
  2823. if (entry->bitmap) {
  2824. kmem_cache_free(btrfs_free_space_bitmap_cachep,
  2825. entry->bitmap);
  2826. ctl->total_bitmaps--;
  2827. recalculate_thresholds(ctl);
  2828. } else if (!btrfs_free_space_trimmed(entry)) {
  2829. ctl->discardable_extents[BTRFS_STAT_CURR]--;
  2830. }
  2831. kmem_cache_free(btrfs_free_space_cachep, entry);
  2832. }
  2833. spin_unlock(&cluster->lock);
  2834. spin_unlock(&ctl->tree_lock);
  2835. return ret;
  2836. }
  2837. static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
  2838. struct btrfs_free_space *entry,
  2839. struct btrfs_free_cluster *cluster,
  2840. u64 offset, u64 bytes,
  2841. u64 cont1_bytes, u64 min_bytes)
  2842. {
  2843. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2844. unsigned long next_zero;
  2845. unsigned long i;
  2846. unsigned long want_bits;
  2847. unsigned long min_bits;
  2848. unsigned long found_bits;
  2849. unsigned long max_bits = 0;
  2850. unsigned long start = 0;
  2851. unsigned long total_found = 0;
  2852. int ret;
  2853. lockdep_assert_held(&ctl->tree_lock);
  2854. i = offset_to_bit(entry->offset, ctl->unit,
  2855. max_t(u64, offset, entry->offset));
  2856. want_bits = bytes_to_bits(bytes, ctl->unit);
  2857. min_bits = bytes_to_bits(min_bytes, ctl->unit);
  2858. /*
  2859. * Don't bother looking for a cluster in this bitmap if it's heavily
  2860. * fragmented.
  2861. */
  2862. if (entry->max_extent_size &&
  2863. entry->max_extent_size < cont1_bytes)
  2864. return -ENOSPC;
  2865. again:
  2866. found_bits = 0;
  2867. for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
  2868. next_zero = find_next_zero_bit(entry->bitmap,
  2869. BITS_PER_BITMAP, i);
  2870. if (next_zero - i >= min_bits) {
  2871. found_bits = next_zero - i;
  2872. if (found_bits > max_bits)
  2873. max_bits = found_bits;
  2874. break;
  2875. }
  2876. if (next_zero - i > max_bits)
  2877. max_bits = next_zero - i;
  2878. i = next_zero;
  2879. }
  2880. if (!found_bits) {
  2881. entry->max_extent_size = (u64)max_bits * ctl->unit;
  2882. return -ENOSPC;
  2883. }
  2884. if (!total_found) {
  2885. start = i;
  2886. cluster->max_size = 0;
  2887. }
  2888. total_found += found_bits;
  2889. if (cluster->max_size < found_bits * ctl->unit)
  2890. cluster->max_size = found_bits * ctl->unit;
  2891. if (total_found < want_bits || cluster->max_size < cont1_bytes) {
  2892. i = next_zero + 1;
  2893. goto again;
  2894. }
  2895. cluster->window_start = start * ctl->unit + entry->offset;
  2896. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  2897. rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes);
  2898. /*
  2899. * We need to know if we're currently on the normal space index when we
  2900. * manipulate the bitmap so that we know we need to remove and re-insert
  2901. * it into the space_index tree. Clear the bytes_index node here so the
  2902. * bitmap manipulation helpers know not to mess with the space_index
  2903. * until this bitmap entry is added back into the normal cache.
  2904. */
  2905. RB_CLEAR_NODE(&entry->bytes_index);
  2906. ret = tree_insert_offset(ctl, cluster, entry);
  2907. ASSERT(!ret); /* -EEXIST; Logic error */
  2908. trace_btrfs_setup_cluster(block_group, cluster,
  2909. total_found * ctl->unit, 1);
  2910. return 0;
  2911. }
  2912. /*
  2913. * This searches the block group for just extents to fill the cluster with.
  2914. * Try to find a cluster with at least bytes total bytes, at least one
  2915. * extent of cont1_bytes, and other clusters of at least min_bytes.
  2916. */
  2917. static noinline int
  2918. setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
  2919. struct btrfs_free_cluster *cluster,
  2920. struct list_head *bitmaps, u64 offset, u64 bytes,
  2921. u64 cont1_bytes, u64 min_bytes)
  2922. {
  2923. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2924. struct btrfs_free_space *first = NULL;
  2925. struct btrfs_free_space *entry = NULL;
  2926. struct btrfs_free_space *last;
  2927. struct rb_node *node;
  2928. u64 window_free;
  2929. u64 max_extent;
  2930. u64 total_size = 0;
  2931. lockdep_assert_held(&ctl->tree_lock);
  2932. entry = tree_search_offset(ctl, offset, 0, 1);
  2933. if (!entry)
  2934. return -ENOSPC;
  2935. /*
  2936. * We don't want bitmaps, so just move along until we find a normal
  2937. * extent entry.
  2938. */
  2939. while (entry->bitmap || entry->bytes < min_bytes) {
  2940. if (entry->bitmap && list_empty(&entry->list))
  2941. list_add_tail(&entry->list, bitmaps);
  2942. node = rb_next(&entry->offset_index);
  2943. if (!node)
  2944. return -ENOSPC;
  2945. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2946. }
  2947. window_free = entry->bytes;
  2948. max_extent = entry->bytes;
  2949. first = entry;
  2950. last = entry;
  2951. for (node = rb_next(&entry->offset_index); node;
  2952. node = rb_next(&entry->offset_index)) {
  2953. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2954. if (entry->bitmap) {
  2955. if (list_empty(&entry->list))
  2956. list_add_tail(&entry->list, bitmaps);
  2957. continue;
  2958. }
  2959. if (entry->bytes < min_bytes)
  2960. continue;
  2961. last = entry;
  2962. window_free += entry->bytes;
  2963. if (entry->bytes > max_extent)
  2964. max_extent = entry->bytes;
  2965. }
  2966. if (window_free < bytes || max_extent < cont1_bytes)
  2967. return -ENOSPC;
  2968. cluster->window_start = first->offset;
  2969. node = &first->offset_index;
  2970. /*
  2971. * now we've found our entries, pull them out of the free space
  2972. * cache and put them into the cluster rbtree
  2973. */
  2974. do {
  2975. int ret;
  2976. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2977. node = rb_next(&entry->offset_index);
  2978. if (entry->bitmap || entry->bytes < min_bytes)
  2979. continue;
  2980. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  2981. rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes);
  2982. ret = tree_insert_offset(ctl, cluster, entry);
  2983. total_size += entry->bytes;
  2984. ASSERT(!ret); /* -EEXIST; Logic error */
  2985. } while (node && entry != last);
  2986. cluster->max_size = max_extent;
  2987. trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
  2988. return 0;
  2989. }
  2990. /*
  2991. * This specifically looks for bitmaps that may work in the cluster, we assume
  2992. * that we have already failed to find extents that will work.
  2993. */
  2994. static noinline int
  2995. setup_cluster_bitmap(struct btrfs_block_group *block_group,
  2996. struct btrfs_free_cluster *cluster,
  2997. struct list_head *bitmaps, u64 offset, u64 bytes,
  2998. u64 cont1_bytes, u64 min_bytes)
  2999. {
  3000. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  3001. struct btrfs_free_space *entry = NULL;
  3002. int ret = -ENOSPC;
  3003. u64 bitmap_offset = offset_to_bitmap(ctl, offset);
  3004. if (ctl->total_bitmaps == 0)
  3005. return -ENOSPC;
  3006. /*
  3007. * The bitmap that covers offset won't be in the list unless offset
  3008. * is just its start offset.
  3009. */
  3010. if (!list_empty(bitmaps))
  3011. entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
  3012. if (!entry || entry->offset != bitmap_offset) {
  3013. entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
  3014. if (entry && list_empty(&entry->list))
  3015. list_add(&entry->list, bitmaps);
  3016. }
  3017. list_for_each_entry(entry, bitmaps, list) {
  3018. if (entry->bytes < bytes)
  3019. continue;
  3020. ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
  3021. bytes, cont1_bytes, min_bytes);
  3022. if (!ret)
  3023. return 0;
  3024. }
  3025. /*
  3026. * The bitmaps list has all the bitmaps that record free space
  3027. * starting after offset, so no more search is required.
  3028. */
  3029. return -ENOSPC;
  3030. }
  3031. /*
  3032. * here we try to find a cluster of blocks in a block group. The goal
  3033. * is to find at least bytes+empty_size.
  3034. * We might not find them all in one contiguous area.
  3035. *
  3036. * returns zero and sets up cluster if things worked out, otherwise
  3037. * it returns -enospc
  3038. */
  3039. int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
  3040. struct btrfs_free_cluster *cluster,
  3041. u64 offset, u64 bytes, u64 empty_size)
  3042. {
  3043. struct btrfs_fs_info *fs_info = block_group->fs_info;
  3044. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  3045. struct btrfs_free_space *entry, *tmp;
  3046. LIST_HEAD(bitmaps);
  3047. u64 min_bytes;
  3048. u64 cont1_bytes;
  3049. int ret;
  3050. /*
  3051. * Choose the minimum extent size we'll require for this
  3052. * cluster. For SSD_SPREAD, don't allow any fragmentation.
  3053. * For metadata, allow allocates with smaller extents. For
  3054. * data, keep it dense.
  3055. */
  3056. if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
  3057. cont1_bytes = bytes + empty_size;
  3058. min_bytes = cont1_bytes;
  3059. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  3060. cont1_bytes = bytes;
  3061. min_bytes = fs_info->sectorsize;
  3062. } else {
  3063. cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
  3064. min_bytes = fs_info->sectorsize;
  3065. }
  3066. spin_lock(&ctl->tree_lock);
  3067. /*
  3068. * If we know we don't have enough space to make a cluster don't even
  3069. * bother doing all the work to try and find one.
  3070. */
  3071. if (ctl->free_space < bytes) {
  3072. spin_unlock(&ctl->tree_lock);
  3073. return -ENOSPC;
  3074. }
  3075. spin_lock(&cluster->lock);
  3076. /* someone already found a cluster, hooray */
  3077. if (cluster->block_group) {
  3078. ret = 0;
  3079. goto out;
  3080. }
  3081. trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
  3082. min_bytes);
  3083. ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
  3084. bytes + empty_size,
  3085. cont1_bytes, min_bytes);
  3086. if (ret)
  3087. ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
  3088. offset, bytes + empty_size,
  3089. cont1_bytes, min_bytes);
  3090. /* Clear our temporary list */
  3091. list_for_each_entry_safe(entry, tmp, &bitmaps, list)
  3092. list_del_init(&entry->list);
  3093. if (!ret) {
  3094. btrfs_get_block_group(block_group);
  3095. list_add_tail(&cluster->block_group_list,
  3096. &block_group->cluster_list);
  3097. cluster->block_group = block_group;
  3098. } else {
  3099. trace_btrfs_failed_cluster_setup(block_group);
  3100. }
  3101. out:
  3102. spin_unlock(&cluster->lock);
  3103. spin_unlock(&ctl->tree_lock);
  3104. return ret;
  3105. }
  3106. /*
  3107. * simple code to zero out a cluster
  3108. */
  3109. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  3110. {
  3111. spin_lock_init(&cluster->lock);
  3112. spin_lock_init(&cluster->refill_lock);
  3113. cluster->root = RB_ROOT;
  3114. cluster->max_size = 0;
  3115. cluster->fragmented = false;
  3116. INIT_LIST_HEAD(&cluster->block_group_list);
  3117. cluster->block_group = NULL;
  3118. }
  3119. static int do_trimming(struct btrfs_block_group *block_group,
  3120. u64 *total_trimmed, u64 start, u64 bytes,
  3121. u64 reserved_start, u64 reserved_bytes,
  3122. enum btrfs_trim_state reserved_trim_state,
  3123. struct btrfs_trim_range *trim_entry)
  3124. {
  3125. struct btrfs_space_info *space_info = block_group->space_info;
  3126. struct btrfs_fs_info *fs_info = block_group->fs_info;
  3127. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  3128. int ret;
  3129. int update = 0;
  3130. const u64 end = start + bytes;
  3131. const u64 reserved_end = reserved_start + reserved_bytes;
  3132. enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  3133. u64 trimmed = 0;
  3134. spin_lock(&space_info->lock);
  3135. spin_lock(&block_group->lock);
  3136. if (!block_group->ro) {
  3137. block_group->reserved += reserved_bytes;
  3138. space_info->bytes_reserved += reserved_bytes;
  3139. update = 1;
  3140. }
  3141. spin_unlock(&block_group->lock);
  3142. spin_unlock(&space_info->lock);
  3143. ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
  3144. if (!ret) {
  3145. *total_trimmed += trimmed;
  3146. trim_state = BTRFS_TRIM_STATE_TRIMMED;
  3147. }
  3148. mutex_lock(&ctl->cache_writeout_mutex);
  3149. if (reserved_start < start)
  3150. __btrfs_add_free_space(block_group, reserved_start,
  3151. start - reserved_start,
  3152. reserved_trim_state);
  3153. if (end < reserved_end)
  3154. __btrfs_add_free_space(block_group, end, reserved_end - end,
  3155. reserved_trim_state);
  3156. __btrfs_add_free_space(block_group, start, bytes, trim_state);
  3157. list_del(&trim_entry->list);
  3158. mutex_unlock(&ctl->cache_writeout_mutex);
  3159. if (update) {
  3160. spin_lock(&space_info->lock);
  3161. spin_lock(&block_group->lock);
  3162. if (block_group->ro)
  3163. space_info->bytes_readonly += reserved_bytes;
  3164. block_group->reserved -= reserved_bytes;
  3165. space_info->bytes_reserved -= reserved_bytes;
  3166. spin_unlock(&block_group->lock);
  3167. spin_unlock(&space_info->lock);
  3168. }
  3169. return ret;
  3170. }
  3171. /*
  3172. * If @async is set, then we will trim 1 region and return.
  3173. */
  3174. static int trim_no_bitmap(struct btrfs_block_group *block_group,
  3175. u64 *total_trimmed, u64 start, u64 end, u64 minlen,
  3176. bool async)
  3177. {
  3178. struct btrfs_discard_ctl *discard_ctl =
  3179. &block_group->fs_info->discard_ctl;
  3180. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  3181. struct btrfs_free_space *entry;
  3182. struct rb_node *node;
  3183. int ret = 0;
  3184. u64 extent_start;
  3185. u64 extent_bytes;
  3186. enum btrfs_trim_state extent_trim_state;
  3187. u64 bytes;
  3188. const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
  3189. while (start < end) {
  3190. struct btrfs_trim_range trim_entry;
  3191. mutex_lock(&ctl->cache_writeout_mutex);
  3192. spin_lock(&ctl->tree_lock);
  3193. if (ctl->free_space < minlen)
  3194. goto out_unlock;
  3195. entry = tree_search_offset(ctl, start, 0, 1);
  3196. if (!entry)
  3197. goto out_unlock;
  3198. /* Skip bitmaps and if async, already trimmed entries */
  3199. while (entry->bitmap ||
  3200. (async && btrfs_free_space_trimmed(entry))) {
  3201. node = rb_next(&entry->offset_index);
  3202. if (!node)
  3203. goto out_unlock;
  3204. entry = rb_entry(node, struct btrfs_free_space,
  3205. offset_index);
  3206. }
  3207. if (entry->offset >= end)
  3208. goto out_unlock;
  3209. extent_start = entry->offset;
  3210. extent_bytes = entry->bytes;
  3211. extent_trim_state = entry->trim_state;
  3212. if (async) {
  3213. start = entry->offset;
  3214. bytes = entry->bytes;
  3215. if (bytes < minlen) {
  3216. spin_unlock(&ctl->tree_lock);
  3217. mutex_unlock(&ctl->cache_writeout_mutex);
  3218. goto next;
  3219. }
  3220. unlink_free_space(ctl, entry, true);
  3221. /*
  3222. * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
  3223. * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim
  3224. * X when we come back around. So trim it now.
  3225. */
  3226. if (max_discard_size &&
  3227. bytes >= (max_discard_size +
  3228. BTRFS_ASYNC_DISCARD_MIN_FILTER)) {
  3229. bytes = max_discard_size;
  3230. extent_bytes = max_discard_size;
  3231. entry->offset += max_discard_size;
  3232. entry->bytes -= max_discard_size;
  3233. link_free_space(ctl, entry);
  3234. } else {
  3235. kmem_cache_free(btrfs_free_space_cachep, entry);
  3236. }
  3237. } else {
  3238. start = max(start, extent_start);
  3239. bytes = min(extent_start + extent_bytes, end) - start;
  3240. if (bytes < minlen) {
  3241. spin_unlock(&ctl->tree_lock);
  3242. mutex_unlock(&ctl->cache_writeout_mutex);
  3243. goto next;
  3244. }
  3245. unlink_free_space(ctl, entry, true);
  3246. kmem_cache_free(btrfs_free_space_cachep, entry);
  3247. }
  3248. spin_unlock(&ctl->tree_lock);
  3249. trim_entry.start = extent_start;
  3250. trim_entry.bytes = extent_bytes;
  3251. list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
  3252. mutex_unlock(&ctl->cache_writeout_mutex);
  3253. ret = do_trimming(block_group, total_trimmed, start, bytes,
  3254. extent_start, extent_bytes, extent_trim_state,
  3255. &trim_entry);
  3256. if (ret) {
  3257. block_group->discard_cursor = start + bytes;
  3258. break;
  3259. }
  3260. next:
  3261. start += bytes;
  3262. block_group->discard_cursor = start;
  3263. if (async && *total_trimmed)
  3264. break;
  3265. if (btrfs_trim_interrupted()) {
  3266. ret = -ERESTARTSYS;
  3267. break;
  3268. }
  3269. cond_resched();
  3270. }
  3271. return ret;
  3272. out_unlock:
  3273. block_group->discard_cursor = btrfs_block_group_end(block_group);
  3274. spin_unlock(&ctl->tree_lock);
  3275. mutex_unlock(&ctl->cache_writeout_mutex);
  3276. return ret;
  3277. }
  3278. /*
  3279. * If we break out of trimming a bitmap prematurely, we should reset the
  3280. * trimming bit. In a rather contrieved case, it's possible to race here so
  3281. * reset the state to BTRFS_TRIM_STATE_UNTRIMMED.
  3282. *
  3283. * start = start of bitmap
  3284. * end = near end of bitmap
  3285. *
  3286. * Thread 1: Thread 2:
  3287. * trim_bitmaps(start)
  3288. * trim_bitmaps(end)
  3289. * end_trimming_bitmap()
  3290. * reset_trimming_bitmap()
  3291. */
  3292. static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset)
  3293. {
  3294. struct btrfs_free_space *entry;
  3295. spin_lock(&ctl->tree_lock);
  3296. entry = tree_search_offset(ctl, offset, 1, 0);
  3297. if (entry) {
  3298. if (btrfs_free_space_trimmed(entry)) {
  3299. ctl->discardable_extents[BTRFS_STAT_CURR] +=
  3300. entry->bitmap_extents;
  3301. ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes;
  3302. }
  3303. entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  3304. }
  3305. spin_unlock(&ctl->tree_lock);
  3306. }
  3307. static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl,
  3308. struct btrfs_free_space *entry)
  3309. {
  3310. if (btrfs_free_space_trimming_bitmap(entry)) {
  3311. entry->trim_state = BTRFS_TRIM_STATE_TRIMMED;
  3312. ctl->discardable_extents[BTRFS_STAT_CURR] -=
  3313. entry->bitmap_extents;
  3314. ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes;
  3315. }
  3316. }
  3317. /*
  3318. * If @async is set, then we will trim 1 region and return.
  3319. */
  3320. static int trim_bitmaps(struct btrfs_block_group *block_group,
  3321. u64 *total_trimmed, u64 start, u64 end, u64 minlen,
  3322. u64 maxlen, bool async)
  3323. {
  3324. struct btrfs_discard_ctl *discard_ctl =
  3325. &block_group->fs_info->discard_ctl;
  3326. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  3327. struct btrfs_free_space *entry;
  3328. int ret = 0;
  3329. int ret2;
  3330. u64 bytes;
  3331. u64 offset = offset_to_bitmap(ctl, start);
  3332. const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
  3333. while (offset < end) {
  3334. bool next_bitmap = false;
  3335. struct btrfs_trim_range trim_entry;
  3336. mutex_lock(&ctl->cache_writeout_mutex);
  3337. spin_lock(&ctl->tree_lock);
  3338. if (ctl->free_space < minlen) {
  3339. block_group->discard_cursor =
  3340. btrfs_block_group_end(block_group);
  3341. spin_unlock(&ctl->tree_lock);
  3342. mutex_unlock(&ctl->cache_writeout_mutex);
  3343. break;
  3344. }
  3345. entry = tree_search_offset(ctl, offset, 1, 0);
  3346. /*
  3347. * Bitmaps are marked trimmed lossily now to prevent constant
  3348. * discarding of the same bitmap (the reason why we are bound
  3349. * by the filters). So, retrim the block group bitmaps when we
  3350. * are preparing to punt to the unused_bgs list. This uses
  3351. * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED
  3352. * which is the only discard index which sets minlen to 0.
  3353. */
  3354. if (!entry || (async && minlen && start == offset &&
  3355. btrfs_free_space_trimmed(entry))) {
  3356. spin_unlock(&ctl->tree_lock);
  3357. mutex_unlock(&ctl->cache_writeout_mutex);
  3358. next_bitmap = true;
  3359. goto next;
  3360. }
  3361. /*
  3362. * Async discard bitmap trimming begins at by setting the start
  3363. * to be key.objectid and the offset_to_bitmap() aligns to the
  3364. * start of the bitmap. This lets us know we are fully
  3365. * scanning the bitmap rather than only some portion of it.
  3366. */
  3367. if (start == offset)
  3368. entry->trim_state = BTRFS_TRIM_STATE_TRIMMING;
  3369. bytes = minlen;
  3370. ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
  3371. if (ret2 || start >= end) {
  3372. /*
  3373. * We lossily consider a bitmap trimmed if we only skip
  3374. * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER.
  3375. */
  3376. if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER)
  3377. end_trimming_bitmap(ctl, entry);
  3378. else
  3379. entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
  3380. spin_unlock(&ctl->tree_lock);
  3381. mutex_unlock(&ctl->cache_writeout_mutex);
  3382. next_bitmap = true;
  3383. goto next;
  3384. }
  3385. /*
  3386. * We already trimmed a region, but are using the locking above
  3387. * to reset the trim_state.
  3388. */
  3389. if (async && *total_trimmed) {
  3390. spin_unlock(&ctl->tree_lock);
  3391. mutex_unlock(&ctl->cache_writeout_mutex);
  3392. goto out;
  3393. }
  3394. bytes = min(bytes, end - start);
  3395. if (bytes < minlen || (async && maxlen && bytes > maxlen)) {
  3396. spin_unlock(&ctl->tree_lock);
  3397. mutex_unlock(&ctl->cache_writeout_mutex);
  3398. goto next;
  3399. }
  3400. /*
  3401. * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
  3402. * If X < @minlen, we won't trim X when we come back around.
  3403. * So trim it now. We differ here from trimming extents as we
  3404. * don't keep individual state per bit.
  3405. */
  3406. if (async &&
  3407. max_discard_size &&
  3408. bytes > (max_discard_size + minlen))
  3409. bytes = max_discard_size;
  3410. bitmap_clear_bits(ctl, entry, start, bytes, true);
  3411. if (entry->bytes == 0)
  3412. free_bitmap(ctl, entry);
  3413. spin_unlock(&ctl->tree_lock);
  3414. trim_entry.start = start;
  3415. trim_entry.bytes = bytes;
  3416. list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
  3417. mutex_unlock(&ctl->cache_writeout_mutex);
  3418. ret = do_trimming(block_group, total_trimmed, start, bytes,
  3419. start, bytes, 0, &trim_entry);
  3420. if (ret) {
  3421. reset_trimming_bitmap(ctl, offset);
  3422. block_group->discard_cursor =
  3423. btrfs_block_group_end(block_group);
  3424. break;
  3425. }
  3426. next:
  3427. if (next_bitmap) {
  3428. offset += BITS_PER_BITMAP * ctl->unit;
  3429. start = offset;
  3430. } else {
  3431. start += bytes;
  3432. }
  3433. block_group->discard_cursor = start;
  3434. if (btrfs_trim_interrupted()) {
  3435. if (start != offset)
  3436. reset_trimming_bitmap(ctl, offset);
  3437. ret = -ERESTARTSYS;
  3438. break;
  3439. }
  3440. cond_resched();
  3441. }
  3442. if (offset >= end)
  3443. block_group->discard_cursor = end;
  3444. out:
  3445. return ret;
  3446. }
  3447. int btrfs_trim_block_group(struct btrfs_block_group *block_group,
  3448. u64 *trimmed, u64 start, u64 end, u64 minlen)
  3449. {
  3450. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  3451. int ret;
  3452. u64 rem = 0;
  3453. ASSERT(!btrfs_is_zoned(block_group->fs_info));
  3454. *trimmed = 0;
  3455. spin_lock(&block_group->lock);
  3456. if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)) {
  3457. spin_unlock(&block_group->lock);
  3458. return 0;
  3459. }
  3460. btrfs_freeze_block_group(block_group);
  3461. spin_unlock(&block_group->lock);
  3462. ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
  3463. if (ret)
  3464. goto out;
  3465. ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false);
  3466. div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem);
  3467. /* If we ended in the middle of a bitmap, reset the trimming flag */
  3468. if (rem)
  3469. reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));
  3470. out:
  3471. btrfs_unfreeze_block_group(block_group);
  3472. return ret;
  3473. }
  3474. int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
  3475. u64 *trimmed, u64 start, u64 end, u64 minlen,
  3476. bool async)
  3477. {
  3478. int ret;
  3479. *trimmed = 0;
  3480. spin_lock(&block_group->lock);
  3481. if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)) {
  3482. spin_unlock(&block_group->lock);
  3483. return 0;
  3484. }
  3485. btrfs_freeze_block_group(block_group);
  3486. spin_unlock(&block_group->lock);
  3487. ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
  3488. btrfs_unfreeze_block_group(block_group);
  3489. return ret;
  3490. }
  3491. int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
  3492. u64 *trimmed, u64 start, u64 end, u64 minlen,
  3493. u64 maxlen, bool async)
  3494. {
  3495. int ret;
  3496. *trimmed = 0;
  3497. spin_lock(&block_group->lock);
  3498. if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)) {
  3499. spin_unlock(&block_group->lock);
  3500. return 0;
  3501. }
  3502. btrfs_freeze_block_group(block_group);
  3503. spin_unlock(&block_group->lock);
  3504. ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
  3505. async);
  3506. btrfs_unfreeze_block_group(block_group);
  3507. return ret;
  3508. }
  3509. bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info)
  3510. {
  3511. return btrfs_super_cache_generation(fs_info->super_copy);
  3512. }
  3513. static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info,
  3514. struct btrfs_trans_handle *trans)
  3515. {
  3516. struct btrfs_block_group *block_group;
  3517. struct rb_node *node;
  3518. int ret = 0;
  3519. btrfs_info(fs_info, "cleaning free space cache v1");
  3520. node = rb_first_cached(&fs_info->block_group_cache_tree);
  3521. while (node) {
  3522. block_group = rb_entry(node, struct btrfs_block_group, cache_node);
  3523. ret = btrfs_remove_free_space_inode(trans, NULL, block_group);
  3524. if (ret)
  3525. goto out;
  3526. node = rb_next(node);
  3527. }
  3528. out:
  3529. return ret;
  3530. }
  3531. int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active)
  3532. {
  3533. struct btrfs_trans_handle *trans;
  3534. int ret;
  3535. /*
  3536. * update_super_roots will appropriately set or unset
  3537. * super_copy->cache_generation based on SPACE_CACHE and
  3538. * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a
  3539. * transaction commit whether we are enabling space cache v1 and don't
  3540. * have any other work to do, or are disabling it and removing free
  3541. * space inodes.
  3542. */
  3543. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3544. if (IS_ERR(trans))
  3545. return PTR_ERR(trans);
  3546. if (!active) {
  3547. set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
  3548. ret = cleanup_free_space_cache_v1(fs_info, trans);
  3549. if (ret) {
  3550. btrfs_abort_transaction(trans, ret);
  3551. btrfs_end_transaction(trans);
  3552. goto out;
  3553. }
  3554. }
  3555. ret = btrfs_commit_transaction(trans);
  3556. out:
  3557. clear_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags);
  3558. return ret;
  3559. }
  3560. int __init btrfs_free_space_init(void)
  3561. {
  3562. btrfs_free_space_cachep = KMEM_CACHE(btrfs_free_space, 0);
  3563. if (!btrfs_free_space_cachep)
  3564. return -ENOMEM;
  3565. btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
  3566. PAGE_SIZE, PAGE_SIZE,
  3567. 0, NULL);
  3568. if (!btrfs_free_space_bitmap_cachep) {
  3569. kmem_cache_destroy(btrfs_free_space_cachep);
  3570. return -ENOMEM;
  3571. }
  3572. return 0;
  3573. }
  3574. void __cold btrfs_free_space_exit(void)
  3575. {
  3576. kmem_cache_destroy(btrfs_free_space_cachep);
  3577. kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
  3578. }
  3579. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  3580. /*
  3581. * Use this if you need to make a bitmap or extent entry specifically, it
  3582. * doesn't do any of the merging that add_free_space does, this acts a lot like
  3583. * how the free space cache loading stuff works, so you can get really weird
  3584. * configurations.
  3585. */
  3586. int test_add_free_space_entry(struct btrfs_block_group *cache,
  3587. u64 offset, u64 bytes, bool bitmap)
  3588. {
  3589. struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
  3590. struct btrfs_free_space *info = NULL, *bitmap_info;
  3591. void *map = NULL;
  3592. enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED;
  3593. u64 bytes_added;
  3594. int ret;
  3595. again:
  3596. if (!info) {
  3597. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  3598. if (!info)
  3599. return -ENOMEM;
  3600. }
  3601. if (!bitmap) {
  3602. spin_lock(&ctl->tree_lock);
  3603. info->offset = offset;
  3604. info->bytes = bytes;
  3605. info->max_extent_size = 0;
  3606. ret = link_free_space(ctl, info);
  3607. spin_unlock(&ctl->tree_lock);
  3608. if (ret)
  3609. kmem_cache_free(btrfs_free_space_cachep, info);
  3610. return ret;
  3611. }
  3612. if (!map) {
  3613. map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
  3614. if (!map) {
  3615. kmem_cache_free(btrfs_free_space_cachep, info);
  3616. return -ENOMEM;
  3617. }
  3618. }
  3619. spin_lock(&ctl->tree_lock);
  3620. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  3621. 1, 0);
  3622. if (!bitmap_info) {
  3623. info->bitmap = map;
  3624. map = NULL;
  3625. add_new_bitmap(ctl, info, offset);
  3626. bitmap_info = info;
  3627. info = NULL;
  3628. }
  3629. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
  3630. trim_state);
  3631. bytes -= bytes_added;
  3632. offset += bytes_added;
  3633. spin_unlock(&ctl->tree_lock);
  3634. if (bytes)
  3635. goto again;
  3636. if (info)
  3637. kmem_cache_free(btrfs_free_space_cachep, info);
  3638. if (map)
  3639. kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
  3640. return 0;
  3641. }
  3642. /*
  3643. * Checks to see if the given range is in the free space cache. This is really
  3644. * just used to check the absence of space, so if there is free space in the
  3645. * range at all we will return 1.
  3646. */
  3647. int test_check_exists(struct btrfs_block_group *cache,
  3648. u64 offset, u64 bytes)
  3649. {
  3650. struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
  3651. struct btrfs_free_space *info;
  3652. int ret = 0;
  3653. spin_lock(&ctl->tree_lock);
  3654. info = tree_search_offset(ctl, offset, 0, 0);
  3655. if (!info) {
  3656. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  3657. 1, 0);
  3658. if (!info)
  3659. goto out;
  3660. }
  3661. have_info:
  3662. if (info->bitmap) {
  3663. u64 bit_off, bit_bytes;
  3664. struct rb_node *n;
  3665. struct btrfs_free_space *tmp;
  3666. bit_off = offset;
  3667. bit_bytes = ctl->unit;
  3668. ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
  3669. if (!ret) {
  3670. if (bit_off == offset) {
  3671. ret = 1;
  3672. goto out;
  3673. } else if (bit_off > offset &&
  3674. offset + bytes > bit_off) {
  3675. ret = 1;
  3676. goto out;
  3677. }
  3678. }
  3679. n = rb_prev(&info->offset_index);
  3680. while (n) {
  3681. tmp = rb_entry(n, struct btrfs_free_space,
  3682. offset_index);
  3683. if (tmp->offset + tmp->bytes < offset)
  3684. break;
  3685. if (offset + bytes < tmp->offset) {
  3686. n = rb_prev(&tmp->offset_index);
  3687. continue;
  3688. }
  3689. info = tmp;
  3690. goto have_info;
  3691. }
  3692. n = rb_next(&info->offset_index);
  3693. while (n) {
  3694. tmp = rb_entry(n, struct btrfs_free_space,
  3695. offset_index);
  3696. if (offset + bytes < tmp->offset)
  3697. break;
  3698. if (tmp->offset + tmp->bytes < offset) {
  3699. n = rb_next(&tmp->offset_index);
  3700. continue;
  3701. }
  3702. info = tmp;
  3703. goto have_info;
  3704. }
  3705. ret = 0;
  3706. goto out;
  3707. }
  3708. if (info->offset == offset) {
  3709. ret = 1;
  3710. goto out;
  3711. }
  3712. if (offset > info->offset && offset < info->offset + info->bytes)
  3713. ret = 1;
  3714. out:
  3715. spin_unlock(&ctl->tree_lock);
  3716. return ret;
  3717. }
  3718. #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */