free-space-cache.c 114 KB

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