mballoc.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public Licens
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. */
  18. /*
  19. * mballoc.c contains the multiblocks allocation routines
  20. */
  21. #include "mballoc.h"
  22. #include <linux/debugfs.h>
  23. #include <trace/events/ext4.h>
  24. /*
  25. * MUSTDO:
  26. * - test ext4_ext_search_left() and ext4_ext_search_right()
  27. * - search for metadata in few groups
  28. *
  29. * TODO v4:
  30. * - normalization should take into account whether file is still open
  31. * - discard preallocations if no free space left (policy?)
  32. * - don't normalize tails
  33. * - quota
  34. * - reservation for superuser
  35. *
  36. * TODO v3:
  37. * - bitmap read-ahead (proposed by Oleg Drokin aka green)
  38. * - track min/max extents in each group for better group selection
  39. * - mb_mark_used() may allocate chunk right after splitting buddy
  40. * - tree of groups sorted by number of free blocks
  41. * - error handling
  42. */
  43. /*
  44. * The allocation request involve request for multiple number of blocks
  45. * near to the goal(block) value specified.
  46. *
  47. * During initialization phase of the allocator we decide to use the
  48. * group preallocation or inode preallocation depending on the size of
  49. * the file. The size of the file could be the resulting file size we
  50. * would have after allocation, or the current file size, which ever
  51. * is larger. If the size is less than sbi->s_mb_stream_request we
  52. * select to use the group preallocation. The default value of
  53. * s_mb_stream_request is 16 blocks. This can also be tuned via
  54. * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
  55. * terms of number of blocks.
  56. *
  57. * The main motivation for having small file use group preallocation is to
  58. * ensure that we have small files closer together on the disk.
  59. *
  60. * First stage the allocator looks at the inode prealloc list,
  61. * ext4_inode_info->i_prealloc_list, which contains list of prealloc
  62. * spaces for this particular inode. The inode prealloc space is
  63. * represented as:
  64. *
  65. * pa_lstart -> the logical start block for this prealloc space
  66. * pa_pstart -> the physical start block for this prealloc space
  67. * pa_len -> length for this prealloc space
  68. * pa_free -> free space available in this prealloc space
  69. *
  70. * The inode preallocation space is used looking at the _logical_ start
  71. * block. If only the logical file block falls within the range of prealloc
  72. * space we will consume the particular prealloc space. This make sure that
  73. * that the we have contiguous physical blocks representing the file blocks
  74. *
  75. * The important thing to be noted in case of inode prealloc space is that
  76. * we don't modify the values associated to inode prealloc space except
  77. * pa_free.
  78. *
  79. * If we are not able to find blocks in the inode prealloc space and if we
  80. * have the group allocation flag set then we look at the locality group
  81. * prealloc space. These are per CPU prealloc list repreasented as
  82. *
  83. * ext4_sb_info.s_locality_groups[smp_processor_id()]
  84. *
  85. * The reason for having a per cpu locality group is to reduce the contention
  86. * between CPUs. It is possible to get scheduled at this point.
  87. *
  88. * The locality group prealloc space is used looking at whether we have
  89. * enough free space (pa_free) withing the prealloc space.
  90. *
  91. * If we can't allocate blocks via inode prealloc or/and locality group
  92. * prealloc then we look at the buddy cache. The buddy cache is represented
  93. * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
  94. * mapped to the buddy and bitmap information regarding different
  95. * groups. The buddy information is attached to buddy cache inode so that
  96. * we can access them through the page cache. The information regarding
  97. * each group is loaded via ext4_mb_load_buddy. The information involve
  98. * block bitmap and buddy information. The information are stored in the
  99. * inode as:
  100. *
  101. * { page }
  102. * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
  103. *
  104. *
  105. * one block each for bitmap and buddy information. So for each group we
  106. * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
  107. * blocksize) blocks. So it can have information regarding groups_per_page
  108. * which is blocks_per_page/2
  109. *
  110. * The buddy cache inode is not stored on disk. The inode is thrown
  111. * away when the filesystem is unmounted.
  112. *
  113. * We look for count number of blocks in the buddy cache. If we were able
  114. * to locate that many free blocks we return with additional information
  115. * regarding rest of the contiguous physical block available
  116. *
  117. * Before allocating blocks via buddy cache we normalize the request
  118. * blocks. This ensure we ask for more blocks that we needed. The extra
  119. * blocks that we get after allocation is added to the respective prealloc
  120. * list. In case of inode preallocation we follow a list of heuristics
  121. * based on file size. This can be found in ext4_mb_normalize_request. If
  122. * we are doing a group prealloc we try to normalize the request to
  123. * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
  124. * 512 blocks. This can be tuned via
  125. * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
  126. * terms of number of blocks. If we have mounted the file system with -O
  127. * stripe=<value> option the group prealloc request is normalized to the
  128. * stripe value (sbi->s_stripe)
  129. *
  130. * The regular allocator(using the buddy cache) supports few tunables.
  131. *
  132. * /sys/fs/ext4/<partition>/mb_min_to_scan
  133. * /sys/fs/ext4/<partition>/mb_max_to_scan
  134. * /sys/fs/ext4/<partition>/mb_order2_req
  135. *
  136. * The regular allocator uses buddy scan only if the request len is power of
  137. * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
  138. * value of s_mb_order2_reqs can be tuned via
  139. * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
  140. * stripe size (sbi->s_stripe), we try to search for contiguous block in
  141. * stripe size. This should result in better allocation on RAID setups. If
  142. * not, we search in the specific group using bitmap for best extents. The
  143. * tunable min_to_scan and max_to_scan control the behaviour here.
  144. * min_to_scan indicate how long the mballoc __must__ look for a best
  145. * extent and max_to_scan indicates how long the mballoc __can__ look for a
  146. * best extent in the found extents. Searching for the blocks starts with
  147. * the group specified as the goal value in allocation context via
  148. * ac_g_ex. Each group is first checked based on the criteria whether it
  149. * can used for allocation. ext4_mb_good_group explains how the groups are
  150. * checked.
  151. *
  152. * Both the prealloc space are getting populated as above. So for the first
  153. * request we will hit the buddy cache which will result in this prealloc
  154. * space getting filled. The prealloc space is then later used for the
  155. * subsequent request.
  156. */
  157. /*
  158. * mballoc operates on the following data:
  159. * - on-disk bitmap
  160. * - in-core buddy (actually includes buddy and bitmap)
  161. * - preallocation descriptors (PAs)
  162. *
  163. * there are two types of preallocations:
  164. * - inode
  165. * assiged to specific inode and can be used for this inode only.
  166. * it describes part of inode's space preallocated to specific
  167. * physical blocks. any block from that preallocated can be used
  168. * independent. the descriptor just tracks number of blocks left
  169. * unused. so, before taking some block from descriptor, one must
  170. * make sure corresponded logical block isn't allocated yet. this
  171. * also means that freeing any block within descriptor's range
  172. * must discard all preallocated blocks.
  173. * - locality group
  174. * assigned to specific locality group which does not translate to
  175. * permanent set of inodes: inode can join and leave group. space
  176. * from this type of preallocation can be used for any inode. thus
  177. * it's consumed from the beginning to the end.
  178. *
  179. * relation between them can be expressed as:
  180. * in-core buddy = on-disk bitmap + preallocation descriptors
  181. *
  182. * this mean blocks mballoc considers used are:
  183. * - allocated blocks (persistent)
  184. * - preallocated blocks (non-persistent)
  185. *
  186. * consistency in mballoc world means that at any time a block is either
  187. * free or used in ALL structures. notice: "any time" should not be read
  188. * literally -- time is discrete and delimited by locks.
  189. *
  190. * to keep it simple, we don't use block numbers, instead we count number of
  191. * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
  192. *
  193. * all operations can be expressed as:
  194. * - init buddy: buddy = on-disk + PAs
  195. * - new PA: buddy += N; PA = N
  196. * - use inode PA: on-disk += N; PA -= N
  197. * - discard inode PA buddy -= on-disk - PA; PA = 0
  198. * - use locality group PA on-disk += N; PA -= N
  199. * - discard locality group PA buddy -= PA; PA = 0
  200. * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
  201. * is used in real operation because we can't know actual used
  202. * bits from PA, only from on-disk bitmap
  203. *
  204. * if we follow this strict logic, then all operations above should be atomic.
  205. * given some of them can block, we'd have to use something like semaphores
  206. * killing performance on high-end SMP hardware. let's try to relax it using
  207. * the following knowledge:
  208. * 1) if buddy is referenced, it's already initialized
  209. * 2) while block is used in buddy and the buddy is referenced,
  210. * nobody can re-allocate that block
  211. * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
  212. * bit set and PA claims same block, it's OK. IOW, one can set bit in
  213. * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
  214. * block
  215. *
  216. * so, now we're building a concurrency table:
  217. * - init buddy vs.
  218. * - new PA
  219. * blocks for PA are allocated in the buddy, buddy must be referenced
  220. * until PA is linked to allocation group to avoid concurrent buddy init
  221. * - use inode PA
  222. * we need to make sure that either on-disk bitmap or PA has uptodate data
  223. * given (3) we care that PA-=N operation doesn't interfere with init
  224. * - discard inode PA
  225. * the simplest way would be to have buddy initialized by the discard
  226. * - use locality group PA
  227. * again PA-=N must be serialized with init
  228. * - discard locality group PA
  229. * the simplest way would be to have buddy initialized by the discard
  230. * - new PA vs.
  231. * - use inode PA
  232. * i_data_sem serializes them
  233. * - discard inode PA
  234. * discard process must wait until PA isn't used by another process
  235. * - use locality group PA
  236. * some mutex should serialize them
  237. * - discard locality group PA
  238. * discard process must wait until PA isn't used by another process
  239. * - use inode PA
  240. * - use inode PA
  241. * i_data_sem or another mutex should serializes them
  242. * - discard inode PA
  243. * discard process must wait until PA isn't used by another process
  244. * - use locality group PA
  245. * nothing wrong here -- they're different PAs covering different blocks
  246. * - discard locality group PA
  247. * discard process must wait until PA isn't used by another process
  248. *
  249. * now we're ready to make few consequences:
  250. * - PA is referenced and while it is no discard is possible
  251. * - PA is referenced until block isn't marked in on-disk bitmap
  252. * - PA changes only after on-disk bitmap
  253. * - discard must not compete with init. either init is done before
  254. * any discard or they're serialized somehow
  255. * - buddy init as sum of on-disk bitmap and PAs is done atomically
  256. *
  257. * a special case when we've used PA to emptiness. no need to modify buddy
  258. * in this case, but we should care about concurrent init
  259. *
  260. */
  261. /*
  262. * Logic in few words:
  263. *
  264. * - allocation:
  265. * load group
  266. * find blocks
  267. * mark bits in on-disk bitmap
  268. * release group
  269. *
  270. * - use preallocation:
  271. * find proper PA (per-inode or group)
  272. * load group
  273. * mark bits in on-disk bitmap
  274. * release group
  275. * release PA
  276. *
  277. * - free:
  278. * load group
  279. * mark bits in on-disk bitmap
  280. * release group
  281. *
  282. * - discard preallocations in group:
  283. * mark PAs deleted
  284. * move them onto local list
  285. * load on-disk bitmap
  286. * load group
  287. * remove PA from object (inode or locality group)
  288. * mark free blocks in-core
  289. *
  290. * - discard inode's preallocations:
  291. */
  292. /*
  293. * Locking rules
  294. *
  295. * Locks:
  296. * - bitlock on a group (group)
  297. * - object (inode/locality) (object)
  298. * - per-pa lock (pa)
  299. *
  300. * Paths:
  301. * - new pa
  302. * object
  303. * group
  304. *
  305. * - find and use pa:
  306. * pa
  307. *
  308. * - release consumed pa:
  309. * pa
  310. * group
  311. * object
  312. *
  313. * - generate in-core bitmap:
  314. * group
  315. * pa
  316. *
  317. * - discard all for given object (inode, locality group):
  318. * object
  319. * pa
  320. * group
  321. *
  322. * - discard all for given group:
  323. * group
  324. * pa
  325. * group
  326. * object
  327. *
  328. */
  329. static struct kmem_cache *ext4_pspace_cachep;
  330. static struct kmem_cache *ext4_ac_cachep;
  331. static struct kmem_cache *ext4_free_ext_cachep;
  332. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  333. ext4_group_t group);
  334. static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
  335. ext4_group_t group);
  336. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
  337. static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
  338. {
  339. #if BITS_PER_LONG == 64
  340. *bit += ((unsigned long) addr & 7UL) << 3;
  341. addr = (void *) ((unsigned long) addr & ~7UL);
  342. #elif BITS_PER_LONG == 32
  343. *bit += ((unsigned long) addr & 3UL) << 3;
  344. addr = (void *) ((unsigned long) addr & ~3UL);
  345. #else
  346. #error "how many bits you are?!"
  347. #endif
  348. return addr;
  349. }
  350. static inline int mb_test_bit(int bit, void *addr)
  351. {
  352. /*
  353. * ext4_test_bit on architecture like powerpc
  354. * needs unsigned long aligned address
  355. */
  356. addr = mb_correct_addr_and_bit(&bit, addr);
  357. return ext4_test_bit(bit, addr);
  358. }
  359. static inline void mb_set_bit(int bit, void *addr)
  360. {
  361. addr = mb_correct_addr_and_bit(&bit, addr);
  362. ext4_set_bit(bit, addr);
  363. }
  364. static inline void mb_clear_bit(int bit, void *addr)
  365. {
  366. addr = mb_correct_addr_and_bit(&bit, addr);
  367. ext4_clear_bit(bit, addr);
  368. }
  369. static inline int mb_find_next_zero_bit(void *addr, int max, int start)
  370. {
  371. int fix = 0, ret, tmpmax;
  372. addr = mb_correct_addr_and_bit(&fix, addr);
  373. tmpmax = max + fix;
  374. start += fix;
  375. ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
  376. if (ret > max)
  377. return max;
  378. return ret;
  379. }
  380. static inline int mb_find_next_bit(void *addr, int max, int start)
  381. {
  382. int fix = 0, ret, tmpmax;
  383. addr = mb_correct_addr_and_bit(&fix, addr);
  384. tmpmax = max + fix;
  385. start += fix;
  386. ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
  387. if (ret > max)
  388. return max;
  389. return ret;
  390. }
  391. static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
  392. {
  393. char *bb;
  394. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  395. BUG_ON(max == NULL);
  396. if (order > e4b->bd_blkbits + 1) {
  397. *max = 0;
  398. return NULL;
  399. }
  400. /* at order 0 we see each particular block */
  401. *max = 1 << (e4b->bd_blkbits + 3);
  402. if (order == 0)
  403. return EXT4_MB_BITMAP(e4b);
  404. bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
  405. *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
  406. return bb;
  407. }
  408. #ifdef DOUBLE_CHECK
  409. static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
  410. int first, int count)
  411. {
  412. int i;
  413. struct super_block *sb = e4b->bd_sb;
  414. if (unlikely(e4b->bd_info->bb_bitmap == NULL))
  415. return;
  416. assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
  417. for (i = 0; i < count; i++) {
  418. if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
  419. ext4_fsblk_t blocknr;
  420. blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
  421. blocknr += first + i;
  422. ext4_grp_locked_error(sb, e4b->bd_group,
  423. __func__, "double-free of inode"
  424. " %lu's block %llu(bit %u in group %u)",
  425. inode ? inode->i_ino : 0, blocknr,
  426. first + i, e4b->bd_group);
  427. }
  428. mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
  429. }
  430. }
  431. static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
  432. {
  433. int i;
  434. if (unlikely(e4b->bd_info->bb_bitmap == NULL))
  435. return;
  436. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  437. for (i = 0; i < count; i++) {
  438. BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
  439. mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
  440. }
  441. }
  442. static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  443. {
  444. if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
  445. unsigned char *b1, *b2;
  446. int i;
  447. b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
  448. b2 = (unsigned char *) bitmap;
  449. for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
  450. if (b1[i] != b2[i]) {
  451. printk(KERN_ERR "corruption in group %u "
  452. "at byte %u(%u): %x in copy != %x "
  453. "on disk/prealloc\n",
  454. e4b->bd_group, i, i * 8, b1[i], b2[i]);
  455. BUG();
  456. }
  457. }
  458. }
  459. }
  460. #else
  461. static inline void mb_free_blocks_double(struct inode *inode,
  462. struct ext4_buddy *e4b, int first, int count)
  463. {
  464. return;
  465. }
  466. static inline void mb_mark_used_double(struct ext4_buddy *e4b,
  467. int first, int count)
  468. {
  469. return;
  470. }
  471. static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  472. {
  473. return;
  474. }
  475. #endif
  476. #ifdef AGGRESSIVE_CHECK
  477. #define MB_CHECK_ASSERT(assert) \
  478. do { \
  479. if (!(assert)) { \
  480. printk(KERN_EMERG \
  481. "Assertion failure in %s() at %s:%d: \"%s\"\n", \
  482. function, file, line, # assert); \
  483. BUG(); \
  484. } \
  485. } while (0)
  486. static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
  487. const char *function, int line)
  488. {
  489. struct super_block *sb = e4b->bd_sb;
  490. int order = e4b->bd_blkbits + 1;
  491. int max;
  492. int max2;
  493. int i;
  494. int j;
  495. int k;
  496. int count;
  497. struct ext4_group_info *grp;
  498. int fragments = 0;
  499. int fstart;
  500. struct list_head *cur;
  501. void *buddy;
  502. void *buddy2;
  503. {
  504. static int mb_check_counter;
  505. if (mb_check_counter++ % 100 != 0)
  506. return 0;
  507. }
  508. while (order > 1) {
  509. buddy = mb_find_buddy(e4b, order, &max);
  510. MB_CHECK_ASSERT(buddy);
  511. buddy2 = mb_find_buddy(e4b, order - 1, &max2);
  512. MB_CHECK_ASSERT(buddy2);
  513. MB_CHECK_ASSERT(buddy != buddy2);
  514. MB_CHECK_ASSERT(max * 2 == max2);
  515. count = 0;
  516. for (i = 0; i < max; i++) {
  517. if (mb_test_bit(i, buddy)) {
  518. /* only single bit in buddy2 may be 1 */
  519. if (!mb_test_bit(i << 1, buddy2)) {
  520. MB_CHECK_ASSERT(
  521. mb_test_bit((i<<1)+1, buddy2));
  522. } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
  523. MB_CHECK_ASSERT(
  524. mb_test_bit(i << 1, buddy2));
  525. }
  526. continue;
  527. }
  528. /* both bits in buddy2 must be 0 */
  529. MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
  530. MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
  531. for (j = 0; j < (1 << order); j++) {
  532. k = (i * (1 << order)) + j;
  533. MB_CHECK_ASSERT(
  534. !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
  535. }
  536. count++;
  537. }
  538. MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
  539. order--;
  540. }
  541. fstart = -1;
  542. buddy = mb_find_buddy(e4b, 0, &max);
  543. for (i = 0; i < max; i++) {
  544. if (!mb_test_bit(i, buddy)) {
  545. MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
  546. if (fstart == -1) {
  547. fragments++;
  548. fstart = i;
  549. }
  550. continue;
  551. }
  552. fstart = -1;
  553. /* check used bits only */
  554. for (j = 0; j < e4b->bd_blkbits + 1; j++) {
  555. buddy2 = mb_find_buddy(e4b, j, &max2);
  556. k = i >> j;
  557. MB_CHECK_ASSERT(k < max2);
  558. MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
  559. }
  560. }
  561. MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
  562. MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
  563. grp = ext4_get_group_info(sb, e4b->bd_group);
  564. buddy = mb_find_buddy(e4b, 0, &max);
  565. list_for_each(cur, &grp->bb_prealloc_list) {
  566. ext4_group_t groupnr;
  567. struct ext4_prealloc_space *pa;
  568. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  569. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
  570. MB_CHECK_ASSERT(groupnr == e4b->bd_group);
  571. for (i = 0; i < pa->pa_len; i++)
  572. MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
  573. }
  574. return 0;
  575. }
  576. #undef MB_CHECK_ASSERT
  577. #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
  578. __FILE__, __func__, __LINE__)
  579. #else
  580. #define mb_check_buddy(e4b)
  581. #endif
  582. /* FIXME!! need more doc */
  583. static void ext4_mb_mark_free_simple(struct super_block *sb,
  584. void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
  585. struct ext4_group_info *grp)
  586. {
  587. struct ext4_sb_info *sbi = EXT4_SB(sb);
  588. ext4_grpblk_t min;
  589. ext4_grpblk_t max;
  590. ext4_grpblk_t chunk;
  591. unsigned short border;
  592. BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
  593. border = 2 << sb->s_blocksize_bits;
  594. while (len > 0) {
  595. /* find how many blocks can be covered since this position */
  596. max = ffs(first | border) - 1;
  597. /* find how many blocks of power 2 we need to mark */
  598. min = fls(len) - 1;
  599. if (max < min)
  600. min = max;
  601. chunk = 1 << min;
  602. /* mark multiblock chunks only */
  603. grp->bb_counters[min]++;
  604. if (min > 0)
  605. mb_clear_bit(first >> min,
  606. buddy + sbi->s_mb_offsets[min]);
  607. len -= chunk;
  608. first += chunk;
  609. }
  610. }
  611. static noinline_for_stack
  612. void ext4_mb_generate_buddy(struct super_block *sb,
  613. void *buddy, void *bitmap, ext4_group_t group)
  614. {
  615. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  616. ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
  617. ext4_grpblk_t i = 0;
  618. ext4_grpblk_t first;
  619. ext4_grpblk_t len;
  620. unsigned free = 0;
  621. unsigned fragments = 0;
  622. unsigned long long period = get_cycles();
  623. /* initialize buddy from bitmap which is aggregation
  624. * of on-disk bitmap and preallocations */
  625. i = mb_find_next_zero_bit(bitmap, max, 0);
  626. grp->bb_first_free = i;
  627. while (i < max) {
  628. fragments++;
  629. first = i;
  630. i = mb_find_next_bit(bitmap, max, i);
  631. len = i - first;
  632. free += len;
  633. if (len > 1)
  634. ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
  635. else
  636. grp->bb_counters[0]++;
  637. if (i < max)
  638. i = mb_find_next_zero_bit(bitmap, max, i);
  639. }
  640. grp->bb_fragments = fragments;
  641. if (free != grp->bb_free) {
  642. ext4_grp_locked_error(sb, group, __func__,
  643. "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
  644. group, free, grp->bb_free);
  645. /*
  646. * If we intent to continue, we consider group descritor
  647. * corrupt and update bb_free using bitmap value
  648. */
  649. grp->bb_free = free;
  650. }
  651. clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
  652. period = get_cycles() - period;
  653. spin_lock(&EXT4_SB(sb)->s_bal_lock);
  654. EXT4_SB(sb)->s_mb_buddies_generated++;
  655. EXT4_SB(sb)->s_mb_generation_time += period;
  656. spin_unlock(&EXT4_SB(sb)->s_bal_lock);
  657. }
  658. /* The buddy information is attached the buddy cache inode
  659. * for convenience. The information regarding each group
  660. * is loaded via ext4_mb_load_buddy. The information involve
  661. * block bitmap and buddy information. The information are
  662. * stored in the inode as
  663. *
  664. * { page }
  665. * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
  666. *
  667. *
  668. * one block each for bitmap and buddy information.
  669. * So for each group we take up 2 blocks. A page can
  670. * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
  671. * So it can have information regarding groups_per_page which
  672. * is blocks_per_page/2
  673. */
  674. static int ext4_mb_init_cache(struct page *page, char *incore)
  675. {
  676. ext4_group_t ngroups;
  677. int blocksize;
  678. int blocks_per_page;
  679. int groups_per_page;
  680. int err = 0;
  681. int i;
  682. ext4_group_t first_group;
  683. int first_block;
  684. struct super_block *sb;
  685. struct buffer_head *bhs;
  686. struct buffer_head **bh;
  687. struct inode *inode;
  688. char *data;
  689. char *bitmap;
  690. mb_debug(1, "init page %lu\n", page->index);
  691. inode = page->mapping->host;
  692. sb = inode->i_sb;
  693. ngroups = ext4_get_groups_count(sb);
  694. blocksize = 1 << inode->i_blkbits;
  695. blocks_per_page = PAGE_CACHE_SIZE / blocksize;
  696. groups_per_page = blocks_per_page >> 1;
  697. if (groups_per_page == 0)
  698. groups_per_page = 1;
  699. /* allocate buffer_heads to read bitmaps */
  700. if (groups_per_page > 1) {
  701. err = -ENOMEM;
  702. i = sizeof(struct buffer_head *) * groups_per_page;
  703. bh = kzalloc(i, GFP_NOFS);
  704. if (bh == NULL)
  705. goto out;
  706. } else
  707. bh = &bhs;
  708. first_group = page->index * blocks_per_page / 2;
  709. /* read all groups the page covers into the cache */
  710. for (i = 0; i < groups_per_page; i++) {
  711. struct ext4_group_desc *desc;
  712. if (first_group + i >= ngroups)
  713. break;
  714. err = -EIO;
  715. desc = ext4_get_group_desc(sb, first_group + i, NULL);
  716. if (desc == NULL)
  717. goto out;
  718. err = -ENOMEM;
  719. bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
  720. if (bh[i] == NULL)
  721. goto out;
  722. if (bitmap_uptodate(bh[i]))
  723. continue;
  724. lock_buffer(bh[i]);
  725. if (bitmap_uptodate(bh[i])) {
  726. unlock_buffer(bh[i]);
  727. continue;
  728. }
  729. ext4_lock_group(sb, first_group + i);
  730. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  731. ext4_init_block_bitmap(sb, bh[i],
  732. first_group + i, desc);
  733. set_bitmap_uptodate(bh[i]);
  734. set_buffer_uptodate(bh[i]);
  735. ext4_unlock_group(sb, first_group + i);
  736. unlock_buffer(bh[i]);
  737. continue;
  738. }
  739. ext4_unlock_group(sb, first_group + i);
  740. if (buffer_uptodate(bh[i])) {
  741. /*
  742. * if not uninit if bh is uptodate,
  743. * bitmap is also uptodate
  744. */
  745. set_bitmap_uptodate(bh[i]);
  746. unlock_buffer(bh[i]);
  747. continue;
  748. }
  749. get_bh(bh[i]);
  750. /*
  751. * submit the buffer_head for read. We can
  752. * safely mark the bitmap as uptodate now.
  753. * We do it here so the bitmap uptodate bit
  754. * get set with buffer lock held.
  755. */
  756. set_bitmap_uptodate(bh[i]);
  757. bh[i]->b_end_io = end_buffer_read_sync;
  758. submit_bh(READ, bh[i]);
  759. mb_debug(1, "read bitmap for group %u\n", first_group + i);
  760. }
  761. /* wait for I/O completion */
  762. for (i = 0; i < groups_per_page && bh[i]; i++)
  763. wait_on_buffer(bh[i]);
  764. err = -EIO;
  765. for (i = 0; i < groups_per_page && bh[i]; i++)
  766. if (!buffer_uptodate(bh[i]))
  767. goto out;
  768. err = 0;
  769. first_block = page->index * blocks_per_page;
  770. /* init the page */
  771. memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
  772. for (i = 0; i < blocks_per_page; i++) {
  773. int group;
  774. struct ext4_group_info *grinfo;
  775. group = (first_block + i) >> 1;
  776. if (group >= ngroups)
  777. break;
  778. /*
  779. * data carry information regarding this
  780. * particular group in the format specified
  781. * above
  782. *
  783. */
  784. data = page_address(page) + (i * blocksize);
  785. bitmap = bh[group - first_group]->b_data;
  786. /*
  787. * We place the buddy block and bitmap block
  788. * close together
  789. */
  790. if ((first_block + i) & 1) {
  791. /* this is block of buddy */
  792. BUG_ON(incore == NULL);
  793. mb_debug(1, "put buddy for group %u in page %lu/%x\n",
  794. group, page->index, i * blocksize);
  795. grinfo = ext4_get_group_info(sb, group);
  796. grinfo->bb_fragments = 0;
  797. memset(grinfo->bb_counters, 0,
  798. sizeof(*grinfo->bb_counters) *
  799. (sb->s_blocksize_bits+2));
  800. /*
  801. * incore got set to the group block bitmap below
  802. */
  803. ext4_lock_group(sb, group);
  804. ext4_mb_generate_buddy(sb, data, incore, group);
  805. ext4_unlock_group(sb, group);
  806. incore = NULL;
  807. } else {
  808. /* this is block of bitmap */
  809. BUG_ON(incore != NULL);
  810. mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
  811. group, page->index, i * blocksize);
  812. /* see comments in ext4_mb_put_pa() */
  813. ext4_lock_group(sb, group);
  814. memcpy(data, bitmap, blocksize);
  815. /* mark all preallocated blks used in in-core bitmap */
  816. ext4_mb_generate_from_pa(sb, data, group);
  817. ext4_mb_generate_from_freelist(sb, data, group);
  818. ext4_unlock_group(sb, group);
  819. /* set incore so that the buddy information can be
  820. * generated using this
  821. */
  822. incore = data;
  823. }
  824. }
  825. SetPageUptodate(page);
  826. out:
  827. if (bh) {
  828. for (i = 0; i < groups_per_page && bh[i]; i++)
  829. brelse(bh[i]);
  830. if (bh != &bhs)
  831. kfree(bh);
  832. }
  833. return err;
  834. }
  835. static noinline_for_stack
  836. int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
  837. {
  838. int ret = 0;
  839. void *bitmap;
  840. int blocks_per_page;
  841. int block, pnum, poff;
  842. int num_grp_locked = 0;
  843. struct ext4_group_info *this_grp;
  844. struct ext4_sb_info *sbi = EXT4_SB(sb);
  845. struct inode *inode = sbi->s_buddy_cache;
  846. struct page *page = NULL, *bitmap_page = NULL;
  847. mb_debug(1, "init group %u\n", group);
  848. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  849. this_grp = ext4_get_group_info(sb, group);
  850. /*
  851. * This ensures that we don't reinit the buddy cache
  852. * page which map to the group from which we are already
  853. * allocating. If we are looking at the buddy cache we would
  854. * have taken a reference using ext4_mb_load_buddy and that
  855. * would have taken the alloc_sem lock.
  856. */
  857. num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
  858. if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
  859. /*
  860. * somebody initialized the group
  861. * return without doing anything
  862. */
  863. ret = 0;
  864. goto err;
  865. }
  866. /*
  867. * the buddy cache inode stores the block bitmap
  868. * and buddy information in consecutive blocks.
  869. * So for each group we need two blocks.
  870. */
  871. block = group * 2;
  872. pnum = block / blocks_per_page;
  873. poff = block % blocks_per_page;
  874. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  875. if (page) {
  876. BUG_ON(page->mapping != inode->i_mapping);
  877. ret = ext4_mb_init_cache(page, NULL);
  878. if (ret) {
  879. unlock_page(page);
  880. goto err;
  881. }
  882. unlock_page(page);
  883. }
  884. if (page == NULL || !PageUptodate(page)) {
  885. ret = -EIO;
  886. goto err;
  887. }
  888. mark_page_accessed(page);
  889. bitmap_page = page;
  890. bitmap = page_address(page) + (poff * sb->s_blocksize);
  891. /* init buddy cache */
  892. block++;
  893. pnum = block / blocks_per_page;
  894. poff = block % blocks_per_page;
  895. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  896. if (page == bitmap_page) {
  897. /*
  898. * If both the bitmap and buddy are in
  899. * the same page we don't need to force
  900. * init the buddy
  901. */
  902. unlock_page(page);
  903. } else if (page) {
  904. BUG_ON(page->mapping != inode->i_mapping);
  905. ret = ext4_mb_init_cache(page, bitmap);
  906. if (ret) {
  907. unlock_page(page);
  908. goto err;
  909. }
  910. unlock_page(page);
  911. }
  912. if (page == NULL || !PageUptodate(page)) {
  913. ret = -EIO;
  914. goto err;
  915. }
  916. mark_page_accessed(page);
  917. err:
  918. ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
  919. if (bitmap_page)
  920. page_cache_release(bitmap_page);
  921. if (page)
  922. page_cache_release(page);
  923. return ret;
  924. }
  925. static noinline_for_stack int
  926. ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
  927. struct ext4_buddy *e4b)
  928. {
  929. int blocks_per_page;
  930. int block;
  931. int pnum;
  932. int poff;
  933. struct page *page;
  934. int ret;
  935. struct ext4_group_info *grp;
  936. struct ext4_sb_info *sbi = EXT4_SB(sb);
  937. struct inode *inode = sbi->s_buddy_cache;
  938. mb_debug(1, "load group %u\n", group);
  939. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  940. grp = ext4_get_group_info(sb, group);
  941. e4b->bd_blkbits = sb->s_blocksize_bits;
  942. e4b->bd_info = ext4_get_group_info(sb, group);
  943. e4b->bd_sb = sb;
  944. e4b->bd_group = group;
  945. e4b->bd_buddy_page = NULL;
  946. e4b->bd_bitmap_page = NULL;
  947. e4b->alloc_semp = &grp->alloc_sem;
  948. /* Take the read lock on the group alloc
  949. * sem. This would make sure a parallel
  950. * ext4_mb_init_group happening on other
  951. * groups mapped by the page is blocked
  952. * till we are done with allocation
  953. */
  954. repeat_load_buddy:
  955. down_read(e4b->alloc_semp);
  956. if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
  957. /* we need to check for group need init flag
  958. * with alloc_semp held so that we can be sure
  959. * that new blocks didn't get added to the group
  960. * when we are loading the buddy cache
  961. */
  962. up_read(e4b->alloc_semp);
  963. /*
  964. * we need full data about the group
  965. * to make a good selection
  966. */
  967. ret = ext4_mb_init_group(sb, group);
  968. if (ret)
  969. return ret;
  970. goto repeat_load_buddy;
  971. }
  972. /*
  973. * the buddy cache inode stores the block bitmap
  974. * and buddy information in consecutive blocks.
  975. * So for each group we need two blocks.
  976. */
  977. block = group * 2;
  978. pnum = block / blocks_per_page;
  979. poff = block % blocks_per_page;
  980. /* we could use find_or_create_page(), but it locks page
  981. * what we'd like to avoid in fast path ... */
  982. page = find_get_page(inode->i_mapping, pnum);
  983. if (page == NULL || !PageUptodate(page)) {
  984. if (page)
  985. /*
  986. * drop the page reference and try
  987. * to get the page with lock. If we
  988. * are not uptodate that implies
  989. * somebody just created the page but
  990. * is yet to initialize the same. So
  991. * wait for it to initialize.
  992. */
  993. page_cache_release(page);
  994. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  995. if (page) {
  996. BUG_ON(page->mapping != inode->i_mapping);
  997. if (!PageUptodate(page)) {
  998. ret = ext4_mb_init_cache(page, NULL);
  999. if (ret) {
  1000. unlock_page(page);
  1001. goto err;
  1002. }
  1003. mb_cmp_bitmaps(e4b, page_address(page) +
  1004. (poff * sb->s_blocksize));
  1005. }
  1006. unlock_page(page);
  1007. }
  1008. }
  1009. if (page == NULL || !PageUptodate(page)) {
  1010. ret = -EIO;
  1011. goto err;
  1012. }
  1013. e4b->bd_bitmap_page = page;
  1014. e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
  1015. mark_page_accessed(page);
  1016. block++;
  1017. pnum = block / blocks_per_page;
  1018. poff = block % blocks_per_page;
  1019. page = find_get_page(inode->i_mapping, pnum);
  1020. if (page == NULL || !PageUptodate(page)) {
  1021. if (page)
  1022. page_cache_release(page);
  1023. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1024. if (page) {
  1025. BUG_ON(page->mapping != inode->i_mapping);
  1026. if (!PageUptodate(page)) {
  1027. ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
  1028. if (ret) {
  1029. unlock_page(page);
  1030. goto err;
  1031. }
  1032. }
  1033. unlock_page(page);
  1034. }
  1035. }
  1036. if (page == NULL || !PageUptodate(page)) {
  1037. ret = -EIO;
  1038. goto err;
  1039. }
  1040. e4b->bd_buddy_page = page;
  1041. e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
  1042. mark_page_accessed(page);
  1043. BUG_ON(e4b->bd_bitmap_page == NULL);
  1044. BUG_ON(e4b->bd_buddy_page == NULL);
  1045. return 0;
  1046. err:
  1047. if (e4b->bd_bitmap_page)
  1048. page_cache_release(e4b->bd_bitmap_page);
  1049. if (e4b->bd_buddy_page)
  1050. page_cache_release(e4b->bd_buddy_page);
  1051. e4b->bd_buddy = NULL;
  1052. e4b->bd_bitmap = NULL;
  1053. /* Done with the buddy cache */
  1054. up_read(e4b->alloc_semp);
  1055. return ret;
  1056. }
  1057. static void ext4_mb_release_desc(struct ext4_buddy *e4b)
  1058. {
  1059. if (e4b->bd_bitmap_page)
  1060. page_cache_release(e4b->bd_bitmap_page);
  1061. if (e4b->bd_buddy_page)
  1062. page_cache_release(e4b->bd_buddy_page);
  1063. /* Done with the buddy cache */
  1064. if (e4b->alloc_semp)
  1065. up_read(e4b->alloc_semp);
  1066. }
  1067. static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
  1068. {
  1069. int order = 1;
  1070. void *bb;
  1071. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  1072. BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
  1073. bb = EXT4_MB_BUDDY(e4b);
  1074. while (order <= e4b->bd_blkbits + 1) {
  1075. block = block >> 1;
  1076. if (!mb_test_bit(block, bb)) {
  1077. /* this block is part of buddy of order 'order' */
  1078. return order;
  1079. }
  1080. bb += 1 << (e4b->bd_blkbits - order);
  1081. order++;
  1082. }
  1083. return 0;
  1084. }
  1085. static void mb_clear_bits(void *bm, int cur, int len)
  1086. {
  1087. __u32 *addr;
  1088. len = cur + len;
  1089. while (cur < len) {
  1090. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1091. /* fast path: clear whole word at once */
  1092. addr = bm + (cur >> 3);
  1093. *addr = 0;
  1094. cur += 32;
  1095. continue;
  1096. }
  1097. mb_clear_bit(cur, bm);
  1098. cur++;
  1099. }
  1100. }
  1101. static void mb_set_bits(void *bm, int cur, int len)
  1102. {
  1103. __u32 *addr;
  1104. len = cur + len;
  1105. while (cur < len) {
  1106. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1107. /* fast path: set whole word at once */
  1108. addr = bm + (cur >> 3);
  1109. *addr = 0xffffffff;
  1110. cur += 32;
  1111. continue;
  1112. }
  1113. mb_set_bit(cur, bm);
  1114. cur++;
  1115. }
  1116. }
  1117. static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
  1118. int first, int count)
  1119. {
  1120. int block = 0;
  1121. int max = 0;
  1122. int order;
  1123. void *buddy;
  1124. void *buddy2;
  1125. struct super_block *sb = e4b->bd_sb;
  1126. BUG_ON(first + count > (sb->s_blocksize << 3));
  1127. assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
  1128. mb_check_buddy(e4b);
  1129. mb_free_blocks_double(inode, e4b, first, count);
  1130. e4b->bd_info->bb_free += count;
  1131. if (first < e4b->bd_info->bb_first_free)
  1132. e4b->bd_info->bb_first_free = first;
  1133. /* let's maintain fragments counter */
  1134. if (first != 0)
  1135. block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
  1136. if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
  1137. max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
  1138. if (block && max)
  1139. e4b->bd_info->bb_fragments--;
  1140. else if (!block && !max)
  1141. e4b->bd_info->bb_fragments++;
  1142. /* let's maintain buddy itself */
  1143. while (count-- > 0) {
  1144. block = first++;
  1145. order = 0;
  1146. if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
  1147. ext4_fsblk_t blocknr;
  1148. blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
  1149. blocknr += block;
  1150. ext4_grp_locked_error(sb, e4b->bd_group,
  1151. __func__, "double-free of inode"
  1152. " %lu's block %llu(bit %u in group %u)",
  1153. inode ? inode->i_ino : 0, blocknr, block,
  1154. e4b->bd_group);
  1155. }
  1156. mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
  1157. e4b->bd_info->bb_counters[order]++;
  1158. /* start of the buddy */
  1159. buddy = mb_find_buddy(e4b, order, &max);
  1160. do {
  1161. block &= ~1UL;
  1162. if (mb_test_bit(block, buddy) ||
  1163. mb_test_bit(block + 1, buddy))
  1164. break;
  1165. /* both the buddies are free, try to coalesce them */
  1166. buddy2 = mb_find_buddy(e4b, order + 1, &max);
  1167. if (!buddy2)
  1168. break;
  1169. if (order > 0) {
  1170. /* for special purposes, we don't set
  1171. * free bits in bitmap */
  1172. mb_set_bit(block, buddy);
  1173. mb_set_bit(block + 1, buddy);
  1174. }
  1175. e4b->bd_info->bb_counters[order]--;
  1176. e4b->bd_info->bb_counters[order]--;
  1177. block = block >> 1;
  1178. order++;
  1179. e4b->bd_info->bb_counters[order]++;
  1180. mb_clear_bit(block, buddy2);
  1181. buddy = buddy2;
  1182. } while (1);
  1183. }
  1184. mb_check_buddy(e4b);
  1185. }
  1186. static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
  1187. int needed, struct ext4_free_extent *ex)
  1188. {
  1189. int next = block;
  1190. int max;
  1191. int ord;
  1192. void *buddy;
  1193. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1194. BUG_ON(ex == NULL);
  1195. buddy = mb_find_buddy(e4b, order, &max);
  1196. BUG_ON(buddy == NULL);
  1197. BUG_ON(block >= max);
  1198. if (mb_test_bit(block, buddy)) {
  1199. ex->fe_len = 0;
  1200. ex->fe_start = 0;
  1201. ex->fe_group = 0;
  1202. return 0;
  1203. }
  1204. /* FIXME dorp order completely ? */
  1205. if (likely(order == 0)) {
  1206. /* find actual order */
  1207. order = mb_find_order_for_block(e4b, block);
  1208. block = block >> order;
  1209. }
  1210. ex->fe_len = 1 << order;
  1211. ex->fe_start = block << order;
  1212. ex->fe_group = e4b->bd_group;
  1213. /* calc difference from given start */
  1214. next = next - ex->fe_start;
  1215. ex->fe_len -= next;
  1216. ex->fe_start += next;
  1217. while (needed > ex->fe_len &&
  1218. (buddy = mb_find_buddy(e4b, order, &max))) {
  1219. if (block + 1 >= max)
  1220. break;
  1221. next = (block + 1) * (1 << order);
  1222. if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
  1223. break;
  1224. ord = mb_find_order_for_block(e4b, next);
  1225. order = ord;
  1226. block = next >> order;
  1227. ex->fe_len += 1 << order;
  1228. }
  1229. BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
  1230. return ex->fe_len;
  1231. }
  1232. static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
  1233. {
  1234. int ord;
  1235. int mlen = 0;
  1236. int max = 0;
  1237. int cur;
  1238. int start = ex->fe_start;
  1239. int len = ex->fe_len;
  1240. unsigned ret = 0;
  1241. int len0 = len;
  1242. void *buddy;
  1243. BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
  1244. BUG_ON(e4b->bd_group != ex->fe_group);
  1245. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1246. mb_check_buddy(e4b);
  1247. mb_mark_used_double(e4b, start, len);
  1248. e4b->bd_info->bb_free -= len;
  1249. if (e4b->bd_info->bb_first_free == start)
  1250. e4b->bd_info->bb_first_free += len;
  1251. /* let's maintain fragments counter */
  1252. if (start != 0)
  1253. mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
  1254. if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
  1255. max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
  1256. if (mlen && max)
  1257. e4b->bd_info->bb_fragments++;
  1258. else if (!mlen && !max)
  1259. e4b->bd_info->bb_fragments--;
  1260. /* let's maintain buddy itself */
  1261. while (len) {
  1262. ord = mb_find_order_for_block(e4b, start);
  1263. if (((start >> ord) << ord) == start && len >= (1 << ord)) {
  1264. /* the whole chunk may be allocated at once! */
  1265. mlen = 1 << ord;
  1266. buddy = mb_find_buddy(e4b, ord, &max);
  1267. BUG_ON((start >> ord) >= max);
  1268. mb_set_bit(start >> ord, buddy);
  1269. e4b->bd_info->bb_counters[ord]--;
  1270. start += mlen;
  1271. len -= mlen;
  1272. BUG_ON(len < 0);
  1273. continue;
  1274. }
  1275. /* store for history */
  1276. if (ret == 0)
  1277. ret = len | (ord << 16);
  1278. /* we have to split large buddy */
  1279. BUG_ON(ord <= 0);
  1280. buddy = mb_find_buddy(e4b, ord, &max);
  1281. mb_set_bit(start >> ord, buddy);
  1282. e4b->bd_info->bb_counters[ord]--;
  1283. ord--;
  1284. cur = (start >> ord) & ~1U;
  1285. buddy = mb_find_buddy(e4b, ord, &max);
  1286. mb_clear_bit(cur, buddy);
  1287. mb_clear_bit(cur + 1, buddy);
  1288. e4b->bd_info->bb_counters[ord]++;
  1289. e4b->bd_info->bb_counters[ord]++;
  1290. }
  1291. mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
  1292. mb_check_buddy(e4b);
  1293. return ret;
  1294. }
  1295. /*
  1296. * Must be called under group lock!
  1297. */
  1298. static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
  1299. struct ext4_buddy *e4b)
  1300. {
  1301. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1302. int ret;
  1303. BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
  1304. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1305. ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
  1306. ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
  1307. ret = mb_mark_used(e4b, &ac->ac_b_ex);
  1308. /* preallocation can change ac_b_ex, thus we store actually
  1309. * allocated blocks for history */
  1310. ac->ac_f_ex = ac->ac_b_ex;
  1311. ac->ac_status = AC_STATUS_FOUND;
  1312. ac->ac_tail = ret & 0xffff;
  1313. ac->ac_buddy = ret >> 16;
  1314. /*
  1315. * take the page reference. We want the page to be pinned
  1316. * so that we don't get a ext4_mb_init_cache_call for this
  1317. * group until we update the bitmap. That would mean we
  1318. * double allocate blocks. The reference is dropped
  1319. * in ext4_mb_release_context
  1320. */
  1321. ac->ac_bitmap_page = e4b->bd_bitmap_page;
  1322. get_page(ac->ac_bitmap_page);
  1323. ac->ac_buddy_page = e4b->bd_buddy_page;
  1324. get_page(ac->ac_buddy_page);
  1325. /* on allocation we use ac to track the held semaphore */
  1326. ac->alloc_semp = e4b->alloc_semp;
  1327. e4b->alloc_semp = NULL;
  1328. /* store last allocated for subsequent stream allocation */
  1329. if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
  1330. spin_lock(&sbi->s_md_lock);
  1331. sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
  1332. sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
  1333. spin_unlock(&sbi->s_md_lock);
  1334. }
  1335. }
  1336. /*
  1337. * regular allocator, for general purposes allocation
  1338. */
  1339. static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
  1340. struct ext4_buddy *e4b,
  1341. int finish_group)
  1342. {
  1343. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1344. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1345. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1346. struct ext4_free_extent ex;
  1347. int max;
  1348. if (ac->ac_status == AC_STATUS_FOUND)
  1349. return;
  1350. /*
  1351. * We don't want to scan for a whole year
  1352. */
  1353. if (ac->ac_found > sbi->s_mb_max_to_scan &&
  1354. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1355. ac->ac_status = AC_STATUS_BREAK;
  1356. return;
  1357. }
  1358. /*
  1359. * Haven't found good chunk so far, let's continue
  1360. */
  1361. if (bex->fe_len < gex->fe_len)
  1362. return;
  1363. if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
  1364. && bex->fe_group == e4b->bd_group) {
  1365. /* recheck chunk's availability - we don't know
  1366. * when it was found (within this lock-unlock
  1367. * period or not) */
  1368. max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
  1369. if (max >= gex->fe_len) {
  1370. ext4_mb_use_best_found(ac, e4b);
  1371. return;
  1372. }
  1373. }
  1374. }
  1375. /*
  1376. * The routine checks whether found extent is good enough. If it is,
  1377. * then the extent gets marked used and flag is set to the context
  1378. * to stop scanning. Otherwise, the extent is compared with the
  1379. * previous found extent and if new one is better, then it's stored
  1380. * in the context. Later, the best found extent will be used, if
  1381. * mballoc can't find good enough extent.
  1382. *
  1383. * FIXME: real allocation policy is to be designed yet!
  1384. */
  1385. static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
  1386. struct ext4_free_extent *ex,
  1387. struct ext4_buddy *e4b)
  1388. {
  1389. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1390. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1391. BUG_ON(ex->fe_len <= 0);
  1392. BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1393. BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1394. BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
  1395. ac->ac_found++;
  1396. /*
  1397. * The special case - take what you catch first
  1398. */
  1399. if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1400. *bex = *ex;
  1401. ext4_mb_use_best_found(ac, e4b);
  1402. return;
  1403. }
  1404. /*
  1405. * Let's check whether the chuck is good enough
  1406. */
  1407. if (ex->fe_len == gex->fe_len) {
  1408. *bex = *ex;
  1409. ext4_mb_use_best_found(ac, e4b);
  1410. return;
  1411. }
  1412. /*
  1413. * If this is first found extent, just store it in the context
  1414. */
  1415. if (bex->fe_len == 0) {
  1416. *bex = *ex;
  1417. return;
  1418. }
  1419. /*
  1420. * If new found extent is better, store it in the context
  1421. */
  1422. if (bex->fe_len < gex->fe_len) {
  1423. /* if the request isn't satisfied, any found extent
  1424. * larger than previous best one is better */
  1425. if (ex->fe_len > bex->fe_len)
  1426. *bex = *ex;
  1427. } else if (ex->fe_len > gex->fe_len) {
  1428. /* if the request is satisfied, then we try to find
  1429. * an extent that still satisfy the request, but is
  1430. * smaller than previous one */
  1431. if (ex->fe_len < bex->fe_len)
  1432. *bex = *ex;
  1433. }
  1434. ext4_mb_check_limits(ac, e4b, 0);
  1435. }
  1436. static noinline_for_stack
  1437. int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
  1438. struct ext4_buddy *e4b)
  1439. {
  1440. struct ext4_free_extent ex = ac->ac_b_ex;
  1441. ext4_group_t group = ex.fe_group;
  1442. int max;
  1443. int err;
  1444. BUG_ON(ex.fe_len <= 0);
  1445. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1446. if (err)
  1447. return err;
  1448. ext4_lock_group(ac->ac_sb, group);
  1449. max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
  1450. if (max > 0) {
  1451. ac->ac_b_ex = ex;
  1452. ext4_mb_use_best_found(ac, e4b);
  1453. }
  1454. ext4_unlock_group(ac->ac_sb, group);
  1455. ext4_mb_release_desc(e4b);
  1456. return 0;
  1457. }
  1458. static noinline_for_stack
  1459. int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
  1460. struct ext4_buddy *e4b)
  1461. {
  1462. ext4_group_t group = ac->ac_g_ex.fe_group;
  1463. int max;
  1464. int err;
  1465. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1466. struct ext4_free_extent ex;
  1467. if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
  1468. return 0;
  1469. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1470. if (err)
  1471. return err;
  1472. ext4_lock_group(ac->ac_sb, group);
  1473. max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
  1474. ac->ac_g_ex.fe_len, &ex);
  1475. if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
  1476. ext4_fsblk_t start;
  1477. start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
  1478. ex.fe_start;
  1479. /* use do_div to get remainder (would be 64-bit modulo) */
  1480. if (do_div(start, sbi->s_stripe) == 0) {
  1481. ac->ac_found++;
  1482. ac->ac_b_ex = ex;
  1483. ext4_mb_use_best_found(ac, e4b);
  1484. }
  1485. } else if (max >= ac->ac_g_ex.fe_len) {
  1486. BUG_ON(ex.fe_len <= 0);
  1487. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1488. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1489. ac->ac_found++;
  1490. ac->ac_b_ex = ex;
  1491. ext4_mb_use_best_found(ac, e4b);
  1492. } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
  1493. /* Sometimes, caller may want to merge even small
  1494. * number of blocks to an existing extent */
  1495. BUG_ON(ex.fe_len <= 0);
  1496. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1497. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1498. ac->ac_found++;
  1499. ac->ac_b_ex = ex;
  1500. ext4_mb_use_best_found(ac, e4b);
  1501. }
  1502. ext4_unlock_group(ac->ac_sb, group);
  1503. ext4_mb_release_desc(e4b);
  1504. return 0;
  1505. }
  1506. /*
  1507. * The routine scans buddy structures (not bitmap!) from given order
  1508. * to max order and tries to find big enough chunk to satisfy the req
  1509. */
  1510. static noinline_for_stack
  1511. void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
  1512. struct ext4_buddy *e4b)
  1513. {
  1514. struct super_block *sb = ac->ac_sb;
  1515. struct ext4_group_info *grp = e4b->bd_info;
  1516. void *buddy;
  1517. int i;
  1518. int k;
  1519. int max;
  1520. BUG_ON(ac->ac_2order <= 0);
  1521. for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
  1522. if (grp->bb_counters[i] == 0)
  1523. continue;
  1524. buddy = mb_find_buddy(e4b, i, &max);
  1525. BUG_ON(buddy == NULL);
  1526. k = mb_find_next_zero_bit(buddy, max, 0);
  1527. BUG_ON(k >= max);
  1528. ac->ac_found++;
  1529. ac->ac_b_ex.fe_len = 1 << i;
  1530. ac->ac_b_ex.fe_start = k << i;
  1531. ac->ac_b_ex.fe_group = e4b->bd_group;
  1532. ext4_mb_use_best_found(ac, e4b);
  1533. BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
  1534. if (EXT4_SB(sb)->s_mb_stats)
  1535. atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
  1536. break;
  1537. }
  1538. }
  1539. /*
  1540. * The routine scans the group and measures all found extents.
  1541. * In order to optimize scanning, caller must pass number of
  1542. * free blocks in the group, so the routine can know upper limit.
  1543. */
  1544. static noinline_for_stack
  1545. void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
  1546. struct ext4_buddy *e4b)
  1547. {
  1548. struct super_block *sb = ac->ac_sb;
  1549. void *bitmap = EXT4_MB_BITMAP(e4b);
  1550. struct ext4_free_extent ex;
  1551. int i;
  1552. int free;
  1553. free = e4b->bd_info->bb_free;
  1554. BUG_ON(free <= 0);
  1555. i = e4b->bd_info->bb_first_free;
  1556. while (free && ac->ac_status == AC_STATUS_CONTINUE) {
  1557. i = mb_find_next_zero_bit(bitmap,
  1558. EXT4_BLOCKS_PER_GROUP(sb), i);
  1559. if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
  1560. /*
  1561. * IF we have corrupt bitmap, we won't find any
  1562. * free blocks even though group info says we
  1563. * we have free blocks
  1564. */
  1565. ext4_grp_locked_error(sb, e4b->bd_group,
  1566. __func__, "%d free blocks as per "
  1567. "group info. But bitmap says 0",
  1568. free);
  1569. break;
  1570. }
  1571. mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
  1572. BUG_ON(ex.fe_len <= 0);
  1573. if (free < ex.fe_len) {
  1574. ext4_grp_locked_error(sb, e4b->bd_group,
  1575. __func__, "%d free blocks as per "
  1576. "group info. But got %d blocks",
  1577. free, ex.fe_len);
  1578. /*
  1579. * The number of free blocks differs. This mostly
  1580. * indicate that the bitmap is corrupt. So exit
  1581. * without claiming the space.
  1582. */
  1583. break;
  1584. }
  1585. ext4_mb_measure_extent(ac, &ex, e4b);
  1586. i += ex.fe_len;
  1587. free -= ex.fe_len;
  1588. }
  1589. ext4_mb_check_limits(ac, e4b, 1);
  1590. }
  1591. /*
  1592. * This is a special case for storages like raid5
  1593. * we try to find stripe-aligned chunks for stripe-size requests
  1594. * XXX should do so at least for multiples of stripe size as well
  1595. */
  1596. static noinline_for_stack
  1597. void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
  1598. struct ext4_buddy *e4b)
  1599. {
  1600. struct super_block *sb = ac->ac_sb;
  1601. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1602. void *bitmap = EXT4_MB_BITMAP(e4b);
  1603. struct ext4_free_extent ex;
  1604. ext4_fsblk_t first_group_block;
  1605. ext4_fsblk_t a;
  1606. ext4_grpblk_t i;
  1607. int max;
  1608. BUG_ON(sbi->s_stripe == 0);
  1609. /* find first stripe-aligned block in group */
  1610. first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
  1611. a = first_group_block + sbi->s_stripe - 1;
  1612. do_div(a, sbi->s_stripe);
  1613. i = (a * sbi->s_stripe) - first_group_block;
  1614. while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
  1615. if (!mb_test_bit(i, bitmap)) {
  1616. max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
  1617. if (max >= sbi->s_stripe) {
  1618. ac->ac_found++;
  1619. ac->ac_b_ex = ex;
  1620. ext4_mb_use_best_found(ac, e4b);
  1621. break;
  1622. }
  1623. }
  1624. i += sbi->s_stripe;
  1625. }
  1626. }
  1627. static int ext4_mb_good_group(struct ext4_allocation_context *ac,
  1628. ext4_group_t group, int cr)
  1629. {
  1630. unsigned free, fragments;
  1631. unsigned i, bits;
  1632. int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
  1633. struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
  1634. BUG_ON(cr < 0 || cr >= 4);
  1635. BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
  1636. free = grp->bb_free;
  1637. fragments = grp->bb_fragments;
  1638. if (free == 0)
  1639. return 0;
  1640. if (fragments == 0)
  1641. return 0;
  1642. switch (cr) {
  1643. case 0:
  1644. BUG_ON(ac->ac_2order == 0);
  1645. /* Avoid using the first bg of a flexgroup for data files */
  1646. if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
  1647. (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
  1648. ((group % flex_size) == 0))
  1649. return 0;
  1650. bits = ac->ac_sb->s_blocksize_bits + 1;
  1651. for (i = ac->ac_2order; i <= bits; i++)
  1652. if (grp->bb_counters[i] > 0)
  1653. return 1;
  1654. break;
  1655. case 1:
  1656. if ((free / fragments) >= ac->ac_g_ex.fe_len)
  1657. return 1;
  1658. break;
  1659. case 2:
  1660. if (free >= ac->ac_g_ex.fe_len)
  1661. return 1;
  1662. break;
  1663. case 3:
  1664. return 1;
  1665. default:
  1666. BUG();
  1667. }
  1668. return 0;
  1669. }
  1670. /*
  1671. * lock the group_info alloc_sem of all the groups
  1672. * belonging to the same buddy cache page. This
  1673. * make sure other parallel operation on the buddy
  1674. * cache doesn't happen whild holding the buddy cache
  1675. * lock
  1676. */
  1677. int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
  1678. {
  1679. int i;
  1680. int block, pnum;
  1681. int blocks_per_page;
  1682. int groups_per_page;
  1683. ext4_group_t ngroups = ext4_get_groups_count(sb);
  1684. ext4_group_t first_group;
  1685. struct ext4_group_info *grp;
  1686. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1687. /*
  1688. * the buddy cache inode stores the block bitmap
  1689. * and buddy information in consecutive blocks.
  1690. * So for each group we need two blocks.
  1691. */
  1692. block = group * 2;
  1693. pnum = block / blocks_per_page;
  1694. first_group = pnum * blocks_per_page / 2;
  1695. groups_per_page = blocks_per_page >> 1;
  1696. if (groups_per_page == 0)
  1697. groups_per_page = 1;
  1698. /* read all groups the page covers into the cache */
  1699. for (i = 0; i < groups_per_page; i++) {
  1700. if ((first_group + i) >= ngroups)
  1701. break;
  1702. grp = ext4_get_group_info(sb, first_group + i);
  1703. /* take all groups write allocation
  1704. * semaphore. This make sure there is
  1705. * no block allocation going on in any
  1706. * of that groups
  1707. */
  1708. down_write_nested(&grp->alloc_sem, i);
  1709. }
  1710. return i;
  1711. }
  1712. void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
  1713. ext4_group_t group, int locked_group)
  1714. {
  1715. int i;
  1716. int block, pnum;
  1717. int blocks_per_page;
  1718. ext4_group_t first_group;
  1719. struct ext4_group_info *grp;
  1720. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1721. /*
  1722. * the buddy cache inode stores the block bitmap
  1723. * and buddy information in consecutive blocks.
  1724. * So for each group we need two blocks.
  1725. */
  1726. block = group * 2;
  1727. pnum = block / blocks_per_page;
  1728. first_group = pnum * blocks_per_page / 2;
  1729. /* release locks on all the groups */
  1730. for (i = 0; i < locked_group; i++) {
  1731. grp = ext4_get_group_info(sb, first_group + i);
  1732. /* take all groups write allocation
  1733. * semaphore. This make sure there is
  1734. * no block allocation going on in any
  1735. * of that groups
  1736. */
  1737. up_write(&grp->alloc_sem);
  1738. }
  1739. }
  1740. static noinline_for_stack int
  1741. ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
  1742. {
  1743. ext4_group_t ngroups, group, i;
  1744. int cr;
  1745. int err = 0;
  1746. int bsbits;
  1747. struct ext4_sb_info *sbi;
  1748. struct super_block *sb;
  1749. struct ext4_buddy e4b;
  1750. sb = ac->ac_sb;
  1751. sbi = EXT4_SB(sb);
  1752. ngroups = ext4_get_groups_count(sb);
  1753. /* non-extent files are limited to low blocks/groups */
  1754. if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL))
  1755. ngroups = sbi->s_blockfile_groups;
  1756. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1757. /* first, try the goal */
  1758. err = ext4_mb_find_by_goal(ac, &e4b);
  1759. if (err || ac->ac_status == AC_STATUS_FOUND)
  1760. goto out;
  1761. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  1762. goto out;
  1763. /*
  1764. * ac->ac2_order is set only if the fe_len is a power of 2
  1765. * if ac2_order is set we also set criteria to 0 so that we
  1766. * try exact allocation using buddy.
  1767. */
  1768. i = fls(ac->ac_g_ex.fe_len);
  1769. ac->ac_2order = 0;
  1770. /*
  1771. * We search using buddy data only if the order of the request
  1772. * is greater than equal to the sbi_s_mb_order2_reqs
  1773. * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
  1774. */
  1775. if (i >= sbi->s_mb_order2_reqs) {
  1776. /*
  1777. * This should tell if fe_len is exactly power of 2
  1778. */
  1779. if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
  1780. ac->ac_2order = i - 1;
  1781. }
  1782. bsbits = ac->ac_sb->s_blocksize_bits;
  1783. /* if stream allocation is enabled, use global goal */
  1784. if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
  1785. /* TBD: may be hot point */
  1786. spin_lock(&sbi->s_md_lock);
  1787. ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
  1788. ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
  1789. spin_unlock(&sbi->s_md_lock);
  1790. }
  1791. /* Let's just scan groups to find more-less suitable blocks */
  1792. cr = ac->ac_2order ? 0 : 1;
  1793. /*
  1794. * cr == 0 try to get exact allocation,
  1795. * cr == 3 try to get anything
  1796. */
  1797. repeat:
  1798. for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
  1799. ac->ac_criteria = cr;
  1800. /*
  1801. * searching for the right group start
  1802. * from the goal value specified
  1803. */
  1804. group = ac->ac_g_ex.fe_group;
  1805. for (i = 0; i < ngroups; group++, i++) {
  1806. struct ext4_group_info *grp;
  1807. struct ext4_group_desc *desc;
  1808. if (group == ngroups)
  1809. group = 0;
  1810. /* quick check to skip empty groups */
  1811. grp = ext4_get_group_info(sb, group);
  1812. if (grp->bb_free == 0)
  1813. continue;
  1814. err = ext4_mb_load_buddy(sb, group, &e4b);
  1815. if (err)
  1816. goto out;
  1817. ext4_lock_group(sb, group);
  1818. if (!ext4_mb_good_group(ac, group, cr)) {
  1819. /* someone did allocation from this group */
  1820. ext4_unlock_group(sb, group);
  1821. ext4_mb_release_desc(&e4b);
  1822. continue;
  1823. }
  1824. ac->ac_groups_scanned++;
  1825. desc = ext4_get_group_desc(sb, group, NULL);
  1826. if (cr == 0)
  1827. ext4_mb_simple_scan_group(ac, &e4b);
  1828. else if (cr == 1 &&
  1829. ac->ac_g_ex.fe_len == sbi->s_stripe)
  1830. ext4_mb_scan_aligned(ac, &e4b);
  1831. else
  1832. ext4_mb_complex_scan_group(ac, &e4b);
  1833. ext4_unlock_group(sb, group);
  1834. ext4_mb_release_desc(&e4b);
  1835. if (ac->ac_status != AC_STATUS_CONTINUE)
  1836. break;
  1837. }
  1838. }
  1839. if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
  1840. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1841. /*
  1842. * We've been searching too long. Let's try to allocate
  1843. * the best chunk we've found so far
  1844. */
  1845. ext4_mb_try_best_found(ac, &e4b);
  1846. if (ac->ac_status != AC_STATUS_FOUND) {
  1847. /*
  1848. * Someone more lucky has already allocated it.
  1849. * The only thing we can do is just take first
  1850. * found block(s)
  1851. printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
  1852. */
  1853. ac->ac_b_ex.fe_group = 0;
  1854. ac->ac_b_ex.fe_start = 0;
  1855. ac->ac_b_ex.fe_len = 0;
  1856. ac->ac_status = AC_STATUS_CONTINUE;
  1857. ac->ac_flags |= EXT4_MB_HINT_FIRST;
  1858. cr = 3;
  1859. atomic_inc(&sbi->s_mb_lost_chunks);
  1860. goto repeat;
  1861. }
  1862. }
  1863. out:
  1864. return err;
  1865. }
  1866. static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
  1867. {
  1868. struct super_block *sb = seq->private;
  1869. ext4_group_t group;
  1870. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  1871. return NULL;
  1872. group = *pos + 1;
  1873. return (void *) ((unsigned long) group);
  1874. }
  1875. static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
  1876. {
  1877. struct super_block *sb = seq->private;
  1878. ext4_group_t group;
  1879. ++*pos;
  1880. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  1881. return NULL;
  1882. group = *pos + 1;
  1883. return (void *) ((unsigned long) group);
  1884. }
  1885. static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
  1886. {
  1887. struct super_block *sb = seq->private;
  1888. ext4_group_t group = (ext4_group_t) ((unsigned long) v);
  1889. int i;
  1890. int err;
  1891. struct ext4_buddy e4b;
  1892. struct sg {
  1893. struct ext4_group_info info;
  1894. ext4_grpblk_t counters[16];
  1895. } sg;
  1896. group--;
  1897. if (group == 0)
  1898. seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
  1899. "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
  1900. "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
  1901. "group", "free", "frags", "first",
  1902. "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
  1903. "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
  1904. i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
  1905. sizeof(struct ext4_group_info);
  1906. err = ext4_mb_load_buddy(sb, group, &e4b);
  1907. if (err) {
  1908. seq_printf(seq, "#%-5u: I/O error\n", group);
  1909. return 0;
  1910. }
  1911. ext4_lock_group(sb, group);
  1912. memcpy(&sg, ext4_get_group_info(sb, group), i);
  1913. ext4_unlock_group(sb, group);
  1914. ext4_mb_release_desc(&e4b);
  1915. seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
  1916. sg.info.bb_fragments, sg.info.bb_first_free);
  1917. for (i = 0; i <= 13; i++)
  1918. seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
  1919. sg.info.bb_counters[i] : 0);
  1920. seq_printf(seq, " ]\n");
  1921. return 0;
  1922. }
  1923. static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
  1924. {
  1925. }
  1926. static const struct seq_operations ext4_mb_seq_groups_ops = {
  1927. .start = ext4_mb_seq_groups_start,
  1928. .next = ext4_mb_seq_groups_next,
  1929. .stop = ext4_mb_seq_groups_stop,
  1930. .show = ext4_mb_seq_groups_show,
  1931. };
  1932. static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
  1933. {
  1934. struct super_block *sb = PDE(inode)->data;
  1935. int rc;
  1936. rc = seq_open(file, &ext4_mb_seq_groups_ops);
  1937. if (rc == 0) {
  1938. struct seq_file *m = (struct seq_file *)file->private_data;
  1939. m->private = sb;
  1940. }
  1941. return rc;
  1942. }
  1943. static const struct file_operations ext4_mb_seq_groups_fops = {
  1944. .owner = THIS_MODULE,
  1945. .open = ext4_mb_seq_groups_open,
  1946. .read = seq_read,
  1947. .llseek = seq_lseek,
  1948. .release = seq_release,
  1949. };
  1950. /* Create and initialize ext4_group_info data for the given group. */
  1951. int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
  1952. struct ext4_group_desc *desc)
  1953. {
  1954. int i, len;
  1955. int metalen = 0;
  1956. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1957. struct ext4_group_info **meta_group_info;
  1958. /*
  1959. * First check if this group is the first of a reserved block.
  1960. * If it's true, we have to allocate a new table of pointers
  1961. * to ext4_group_info structures
  1962. */
  1963. if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
  1964. metalen = sizeof(*meta_group_info) <<
  1965. EXT4_DESC_PER_BLOCK_BITS(sb);
  1966. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  1967. if (meta_group_info == NULL) {
  1968. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  1969. "buddy group\n");
  1970. goto exit_meta_group_info;
  1971. }
  1972. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
  1973. meta_group_info;
  1974. }
  1975. /*
  1976. * calculate needed size. if change bb_counters size,
  1977. * don't forget about ext4_mb_generate_buddy()
  1978. */
  1979. len = offsetof(typeof(**meta_group_info),
  1980. bb_counters[sb->s_blocksize_bits + 2]);
  1981. meta_group_info =
  1982. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
  1983. i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  1984. meta_group_info[i] = kzalloc(len, GFP_KERNEL);
  1985. if (meta_group_info[i] == NULL) {
  1986. printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
  1987. goto exit_group_info;
  1988. }
  1989. set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
  1990. &(meta_group_info[i]->bb_state));
  1991. /*
  1992. * initialize bb_free to be able to skip
  1993. * empty groups without initialization
  1994. */
  1995. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  1996. meta_group_info[i]->bb_free =
  1997. ext4_free_blocks_after_init(sb, group, desc);
  1998. } else {
  1999. meta_group_info[i]->bb_free =
  2000. ext4_free_blks_count(sb, desc);
  2001. }
  2002. INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
  2003. init_rwsem(&meta_group_info[i]->alloc_sem);
  2004. meta_group_info[i]->bb_free_root = RB_ROOT;
  2005. #ifdef DOUBLE_CHECK
  2006. {
  2007. struct buffer_head *bh;
  2008. meta_group_info[i]->bb_bitmap =
  2009. kmalloc(sb->s_blocksize, GFP_KERNEL);
  2010. BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
  2011. bh = ext4_read_block_bitmap(sb, group);
  2012. BUG_ON(bh == NULL);
  2013. memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
  2014. sb->s_blocksize);
  2015. put_bh(bh);
  2016. }
  2017. #endif
  2018. return 0;
  2019. exit_group_info:
  2020. /* If a meta_group_info table has been allocated, release it now */
  2021. if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
  2022. kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
  2023. exit_meta_group_info:
  2024. return -ENOMEM;
  2025. } /* ext4_mb_add_groupinfo */
  2026. static int ext4_mb_init_backend(struct super_block *sb)
  2027. {
  2028. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2029. ext4_group_t i;
  2030. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2031. struct ext4_super_block *es = sbi->s_es;
  2032. int num_meta_group_infos;
  2033. int num_meta_group_infos_max;
  2034. int array_size;
  2035. struct ext4_group_desc *desc;
  2036. /* This is the number of blocks used by GDT */
  2037. num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
  2038. 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
  2039. /*
  2040. * This is the total number of blocks used by GDT including
  2041. * the number of reserved blocks for GDT.
  2042. * The s_group_info array is allocated with this value
  2043. * to allow a clean online resize without a complex
  2044. * manipulation of pointer.
  2045. * The drawback is the unused memory when no resize
  2046. * occurs but it's very low in terms of pages
  2047. * (see comments below)
  2048. * Need to handle this properly when META_BG resizing is allowed
  2049. */
  2050. num_meta_group_infos_max = num_meta_group_infos +
  2051. le16_to_cpu(es->s_reserved_gdt_blocks);
  2052. /*
  2053. * array_size is the size of s_group_info array. We round it
  2054. * to the next power of two because this approximation is done
  2055. * internally by kmalloc so we can have some more memory
  2056. * for free here (e.g. may be used for META_BG resize).
  2057. */
  2058. array_size = 1;
  2059. while (array_size < sizeof(*sbi->s_group_info) *
  2060. num_meta_group_infos_max)
  2061. array_size = array_size << 1;
  2062. /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
  2063. * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
  2064. * So a two level scheme suffices for now. */
  2065. sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
  2066. if (sbi->s_group_info == NULL) {
  2067. printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
  2068. return -ENOMEM;
  2069. }
  2070. sbi->s_buddy_cache = new_inode(sb);
  2071. if (sbi->s_buddy_cache == NULL) {
  2072. printk(KERN_ERR "EXT4-fs: can't get new inode\n");
  2073. goto err_freesgi;
  2074. }
  2075. EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
  2076. for (i = 0; i < ngroups; i++) {
  2077. desc = ext4_get_group_desc(sb, i, NULL);
  2078. if (desc == NULL) {
  2079. printk(KERN_ERR
  2080. "EXT4-fs: can't read descriptor %u\n", i);
  2081. goto err_freebuddy;
  2082. }
  2083. if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
  2084. goto err_freebuddy;
  2085. }
  2086. return 0;
  2087. err_freebuddy:
  2088. while (i-- > 0)
  2089. kfree(ext4_get_group_info(sb, i));
  2090. i = num_meta_group_infos;
  2091. while (i-- > 0)
  2092. kfree(sbi->s_group_info[i]);
  2093. iput(sbi->s_buddy_cache);
  2094. err_freesgi:
  2095. kfree(sbi->s_group_info);
  2096. return -ENOMEM;
  2097. }
  2098. int ext4_mb_init(struct super_block *sb, int needs_recovery)
  2099. {
  2100. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2101. unsigned i, j;
  2102. unsigned offset;
  2103. unsigned max;
  2104. int ret;
  2105. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
  2106. sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
  2107. if (sbi->s_mb_offsets == NULL) {
  2108. return -ENOMEM;
  2109. }
  2110. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
  2111. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2112. if (sbi->s_mb_maxs == NULL) {
  2113. kfree(sbi->s_mb_offsets);
  2114. return -ENOMEM;
  2115. }
  2116. /* order 0 is regular bitmap */
  2117. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2118. sbi->s_mb_offsets[0] = 0;
  2119. i = 1;
  2120. offset = 0;
  2121. max = sb->s_blocksize << 2;
  2122. do {
  2123. sbi->s_mb_offsets[i] = offset;
  2124. sbi->s_mb_maxs[i] = max;
  2125. offset += 1 << (sb->s_blocksize_bits - i);
  2126. max = max >> 1;
  2127. i++;
  2128. } while (i <= sb->s_blocksize_bits + 1);
  2129. /* init file for buddy data */
  2130. ret = ext4_mb_init_backend(sb);
  2131. if (ret != 0) {
  2132. kfree(sbi->s_mb_offsets);
  2133. kfree(sbi->s_mb_maxs);
  2134. return ret;
  2135. }
  2136. spin_lock_init(&sbi->s_md_lock);
  2137. spin_lock_init(&sbi->s_bal_lock);
  2138. sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
  2139. sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
  2140. sbi->s_mb_stats = MB_DEFAULT_STATS;
  2141. sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
  2142. sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
  2143. sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
  2144. sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
  2145. if (sbi->s_locality_groups == NULL) {
  2146. kfree(sbi->s_mb_offsets);
  2147. kfree(sbi->s_mb_maxs);
  2148. return -ENOMEM;
  2149. }
  2150. for_each_possible_cpu(i) {
  2151. struct ext4_locality_group *lg;
  2152. lg = per_cpu_ptr(sbi->s_locality_groups, i);
  2153. mutex_init(&lg->lg_mutex);
  2154. for (j = 0; j < PREALLOC_TB_SIZE; j++)
  2155. INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
  2156. spin_lock_init(&lg->lg_prealloc_lock);
  2157. }
  2158. if (sbi->s_proc)
  2159. proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
  2160. &ext4_mb_seq_groups_fops, sb);
  2161. if (sbi->s_journal)
  2162. sbi->s_journal->j_commit_callback = release_blocks_on_commit;
  2163. return 0;
  2164. }
  2165. /* need to called with the ext4 group lock held */
  2166. static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
  2167. {
  2168. struct ext4_prealloc_space *pa;
  2169. struct list_head *cur, *tmp;
  2170. int count = 0;
  2171. list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
  2172. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2173. list_del(&pa->pa_group_list);
  2174. count++;
  2175. kmem_cache_free(ext4_pspace_cachep, pa);
  2176. }
  2177. if (count)
  2178. mb_debug(1, "mballoc: %u PAs left\n", count);
  2179. }
  2180. int ext4_mb_release(struct super_block *sb)
  2181. {
  2182. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2183. ext4_group_t i;
  2184. int num_meta_group_infos;
  2185. struct ext4_group_info *grinfo;
  2186. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2187. if (sbi->s_group_info) {
  2188. for (i = 0; i < ngroups; i++) {
  2189. grinfo = ext4_get_group_info(sb, i);
  2190. #ifdef DOUBLE_CHECK
  2191. kfree(grinfo->bb_bitmap);
  2192. #endif
  2193. ext4_lock_group(sb, i);
  2194. ext4_mb_cleanup_pa(grinfo);
  2195. ext4_unlock_group(sb, i);
  2196. kfree(grinfo);
  2197. }
  2198. num_meta_group_infos = (ngroups +
  2199. EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2200. EXT4_DESC_PER_BLOCK_BITS(sb);
  2201. for (i = 0; i < num_meta_group_infos; i++)
  2202. kfree(sbi->s_group_info[i]);
  2203. kfree(sbi->s_group_info);
  2204. }
  2205. kfree(sbi->s_mb_offsets);
  2206. kfree(sbi->s_mb_maxs);
  2207. if (sbi->s_buddy_cache)
  2208. iput(sbi->s_buddy_cache);
  2209. if (sbi->s_mb_stats) {
  2210. printk(KERN_INFO
  2211. "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
  2212. atomic_read(&sbi->s_bal_allocated),
  2213. atomic_read(&sbi->s_bal_reqs),
  2214. atomic_read(&sbi->s_bal_success));
  2215. printk(KERN_INFO
  2216. "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
  2217. "%u 2^N hits, %u breaks, %u lost\n",
  2218. atomic_read(&sbi->s_bal_ex_scanned),
  2219. atomic_read(&sbi->s_bal_goals),
  2220. atomic_read(&sbi->s_bal_2orders),
  2221. atomic_read(&sbi->s_bal_breaks),
  2222. atomic_read(&sbi->s_mb_lost_chunks));
  2223. printk(KERN_INFO
  2224. "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
  2225. sbi->s_mb_buddies_generated++,
  2226. sbi->s_mb_generation_time);
  2227. printk(KERN_INFO
  2228. "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
  2229. atomic_read(&sbi->s_mb_preallocated),
  2230. atomic_read(&sbi->s_mb_discarded));
  2231. }
  2232. free_percpu(sbi->s_locality_groups);
  2233. if (sbi->s_proc)
  2234. remove_proc_entry("mb_groups", sbi->s_proc);
  2235. return 0;
  2236. }
  2237. /*
  2238. * This function is called by the jbd2 layer once the commit has finished,
  2239. * so we know we can free the blocks that were released with that commit.
  2240. */
  2241. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
  2242. {
  2243. struct super_block *sb = journal->j_private;
  2244. struct ext4_buddy e4b;
  2245. struct ext4_group_info *db;
  2246. int err, count = 0, count2 = 0;
  2247. struct ext4_free_data *entry;
  2248. struct list_head *l, *ltmp;
  2249. list_for_each_safe(l, ltmp, &txn->t_private_list) {
  2250. entry = list_entry(l, struct ext4_free_data, list);
  2251. mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
  2252. entry->count, entry->group, entry);
  2253. err = ext4_mb_load_buddy(sb, entry->group, &e4b);
  2254. /* we expect to find existing buddy because it's pinned */
  2255. BUG_ON(err != 0);
  2256. db = e4b.bd_info;
  2257. /* there are blocks to put in buddy to make them really free */
  2258. count += entry->count;
  2259. count2++;
  2260. ext4_lock_group(sb, entry->group);
  2261. /* Take it out of per group rb tree */
  2262. rb_erase(&entry->node, &(db->bb_free_root));
  2263. mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
  2264. if (!db->bb_free_root.rb_node) {
  2265. /* No more items in the per group rb tree
  2266. * balance refcounts from ext4_mb_free_metadata()
  2267. */
  2268. page_cache_release(e4b.bd_buddy_page);
  2269. page_cache_release(e4b.bd_bitmap_page);
  2270. }
  2271. ext4_unlock_group(sb, entry->group);
  2272. if (test_opt(sb, DISCARD)) {
  2273. ext4_fsblk_t discard_block;
  2274. discard_block = entry->start_blk +
  2275. ext4_group_first_block_no(sb, entry->group);
  2276. trace_ext4_discard_blocks(sb,
  2277. (unsigned long long)discard_block,
  2278. entry->count);
  2279. sb_issue_discard(sb, discard_block, entry->count);
  2280. }
  2281. kmem_cache_free(ext4_free_ext_cachep, entry);
  2282. ext4_mb_release_desc(&e4b);
  2283. }
  2284. mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
  2285. }
  2286. #ifdef CONFIG_EXT4_DEBUG
  2287. u8 mb_enable_debug __read_mostly;
  2288. static struct dentry *debugfs_dir;
  2289. static struct dentry *debugfs_debug;
  2290. static void __init ext4_create_debugfs_entry(void)
  2291. {
  2292. debugfs_dir = debugfs_create_dir("ext4", NULL);
  2293. if (debugfs_dir)
  2294. debugfs_debug = debugfs_create_u8("mballoc-debug",
  2295. S_IRUGO | S_IWUSR,
  2296. debugfs_dir,
  2297. &mb_enable_debug);
  2298. }
  2299. static void ext4_remove_debugfs_entry(void)
  2300. {
  2301. debugfs_remove(debugfs_debug);
  2302. debugfs_remove(debugfs_dir);
  2303. }
  2304. #else
  2305. static void __init ext4_create_debugfs_entry(void)
  2306. {
  2307. }
  2308. static void ext4_remove_debugfs_entry(void)
  2309. {
  2310. }
  2311. #endif
  2312. int __init init_ext4_mballoc(void)
  2313. {
  2314. ext4_pspace_cachep =
  2315. kmem_cache_create("ext4_prealloc_space",
  2316. sizeof(struct ext4_prealloc_space),
  2317. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2318. if (ext4_pspace_cachep == NULL)
  2319. return -ENOMEM;
  2320. ext4_ac_cachep =
  2321. kmem_cache_create("ext4_alloc_context",
  2322. sizeof(struct ext4_allocation_context),
  2323. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2324. if (ext4_ac_cachep == NULL) {
  2325. kmem_cache_destroy(ext4_pspace_cachep);
  2326. return -ENOMEM;
  2327. }
  2328. ext4_free_ext_cachep =
  2329. kmem_cache_create("ext4_free_block_extents",
  2330. sizeof(struct ext4_free_data),
  2331. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2332. if (ext4_free_ext_cachep == NULL) {
  2333. kmem_cache_destroy(ext4_pspace_cachep);
  2334. kmem_cache_destroy(ext4_ac_cachep);
  2335. return -ENOMEM;
  2336. }
  2337. ext4_create_debugfs_entry();
  2338. return 0;
  2339. }
  2340. void exit_ext4_mballoc(void)
  2341. {
  2342. /*
  2343. * Wait for completion of call_rcu()'s on ext4_pspace_cachep
  2344. * before destroying the slab cache.
  2345. */
  2346. rcu_barrier();
  2347. kmem_cache_destroy(ext4_pspace_cachep);
  2348. kmem_cache_destroy(ext4_ac_cachep);
  2349. kmem_cache_destroy(ext4_free_ext_cachep);
  2350. ext4_remove_debugfs_entry();
  2351. }
  2352. /*
  2353. * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
  2354. * Returns 0 if success or error code
  2355. */
  2356. static noinline_for_stack int
  2357. ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2358. handle_t *handle, unsigned int reserv_blks)
  2359. {
  2360. struct buffer_head *bitmap_bh = NULL;
  2361. struct ext4_super_block *es;
  2362. struct ext4_group_desc *gdp;
  2363. struct buffer_head *gdp_bh;
  2364. struct ext4_sb_info *sbi;
  2365. struct super_block *sb;
  2366. ext4_fsblk_t block;
  2367. int err, len;
  2368. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2369. BUG_ON(ac->ac_b_ex.fe_len <= 0);
  2370. sb = ac->ac_sb;
  2371. sbi = EXT4_SB(sb);
  2372. es = sbi->s_es;
  2373. err = -EIO;
  2374. bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2375. if (!bitmap_bh)
  2376. goto out_err;
  2377. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2378. if (err)
  2379. goto out_err;
  2380. err = -EIO;
  2381. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2382. if (!gdp)
  2383. goto out_err;
  2384. ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
  2385. ext4_free_blks_count(sb, gdp));
  2386. err = ext4_journal_get_write_access(handle, gdp_bh);
  2387. if (err)
  2388. goto out_err;
  2389. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  2390. len = ac->ac_b_ex.fe_len;
  2391. if (!ext4_data_block_valid(sbi, block, len)) {
  2392. ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
  2393. "fs metadata\n", block, block+len);
  2394. /* File system mounted not to panic on error
  2395. * Fix the bitmap and repeat the block allocation
  2396. * We leak some of the blocks here.
  2397. */
  2398. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2399. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2400. ac->ac_b_ex.fe_len);
  2401. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2402. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2403. if (!err)
  2404. err = -EAGAIN;
  2405. goto out_err;
  2406. }
  2407. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2408. #ifdef AGGRESSIVE_CHECK
  2409. {
  2410. int i;
  2411. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2412. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2413. bitmap_bh->b_data));
  2414. }
  2415. }
  2416. #endif
  2417. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
  2418. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2419. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2420. ext4_free_blks_set(sb, gdp,
  2421. ext4_free_blocks_after_init(sb,
  2422. ac->ac_b_ex.fe_group, gdp));
  2423. }
  2424. len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
  2425. ext4_free_blks_set(sb, gdp, len);
  2426. gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
  2427. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2428. percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
  2429. /*
  2430. * Now reduce the dirty block count also. Should not go negative
  2431. */
  2432. if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  2433. /* release all the reserved blocks if non delalloc */
  2434. percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
  2435. if (sbi->s_log_groups_per_flex) {
  2436. ext4_group_t flex_group = ext4_flex_group(sbi,
  2437. ac->ac_b_ex.fe_group);
  2438. atomic_sub(ac->ac_b_ex.fe_len,
  2439. &sbi->s_flex_groups[flex_group].free_blocks);
  2440. }
  2441. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2442. if (err)
  2443. goto out_err;
  2444. err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
  2445. out_err:
  2446. sb->s_dirt = 1;
  2447. brelse(bitmap_bh);
  2448. return err;
  2449. }
  2450. /*
  2451. * here we normalize request for locality group
  2452. * Group request are normalized to s_strip size if we set the same via mount
  2453. * option. If not we set it to s_mb_group_prealloc which can be configured via
  2454. * /sys/fs/ext4/<partition>/mb_group_prealloc
  2455. *
  2456. * XXX: should we try to preallocate more than the group has now?
  2457. */
  2458. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2459. {
  2460. struct super_block *sb = ac->ac_sb;
  2461. struct ext4_locality_group *lg = ac->ac_lg;
  2462. BUG_ON(lg == NULL);
  2463. if (EXT4_SB(sb)->s_stripe)
  2464. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
  2465. else
  2466. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2467. mb_debug(1, "#%u: goal %u blocks for locality group\n",
  2468. current->pid, ac->ac_g_ex.fe_len);
  2469. }
  2470. /*
  2471. * Normalization means making request better in terms of
  2472. * size and alignment
  2473. */
  2474. static noinline_for_stack void
  2475. ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2476. struct ext4_allocation_request *ar)
  2477. {
  2478. int bsbits, max;
  2479. ext4_lblk_t end;
  2480. loff_t size, orig_size, start_off;
  2481. ext4_lblk_t start, orig_start;
  2482. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2483. struct ext4_prealloc_space *pa;
  2484. /* do normalize only data requests, metadata requests
  2485. do not need preallocation */
  2486. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2487. return;
  2488. /* sometime caller may want exact blocks */
  2489. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2490. return;
  2491. /* caller may indicate that preallocation isn't
  2492. * required (it's a tail, for example) */
  2493. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2494. return;
  2495. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2496. ext4_mb_normalize_group_request(ac);
  2497. return ;
  2498. }
  2499. bsbits = ac->ac_sb->s_blocksize_bits;
  2500. /* first, let's learn actual file size
  2501. * given current request is allocated */
  2502. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  2503. size = size << bsbits;
  2504. if (size < i_size_read(ac->ac_inode))
  2505. size = i_size_read(ac->ac_inode);
  2506. /* max size of free chunks */
  2507. max = 2 << bsbits;
  2508. #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
  2509. (req <= (size) || max <= (chunk_size))
  2510. /* first, try to predict filesize */
  2511. /* XXX: should this table be tunable? */
  2512. start_off = 0;
  2513. if (size <= 16 * 1024) {
  2514. size = 16 * 1024;
  2515. } else if (size <= 32 * 1024) {
  2516. size = 32 * 1024;
  2517. } else if (size <= 64 * 1024) {
  2518. size = 64 * 1024;
  2519. } else if (size <= 128 * 1024) {
  2520. size = 128 * 1024;
  2521. } else if (size <= 256 * 1024) {
  2522. size = 256 * 1024;
  2523. } else if (size <= 512 * 1024) {
  2524. size = 512 * 1024;
  2525. } else if (size <= 1024 * 1024) {
  2526. size = 1024 * 1024;
  2527. } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
  2528. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2529. (21 - bsbits)) << 21;
  2530. size = 2 * 1024 * 1024;
  2531. } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
  2532. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2533. (22 - bsbits)) << 22;
  2534. size = 4 * 1024 * 1024;
  2535. } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
  2536. (8<<20)>>bsbits, max, 8 * 1024)) {
  2537. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2538. (23 - bsbits)) << 23;
  2539. size = 8 * 1024 * 1024;
  2540. } else {
  2541. start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
  2542. size = ac->ac_o_ex.fe_len << bsbits;
  2543. }
  2544. orig_size = size = size >> bsbits;
  2545. orig_start = start = start_off >> bsbits;
  2546. /* don't cover already allocated blocks in selected range */
  2547. if (ar->pleft && start <= ar->lleft) {
  2548. size -= ar->lleft + 1 - start;
  2549. start = ar->lleft + 1;
  2550. }
  2551. if (ar->pright && start + size - 1 >= ar->lright)
  2552. size -= start + size - ar->lright;
  2553. end = start + size;
  2554. /* check we don't cross already preallocated blocks */
  2555. rcu_read_lock();
  2556. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2557. ext4_lblk_t pa_end;
  2558. if (pa->pa_deleted)
  2559. continue;
  2560. spin_lock(&pa->pa_lock);
  2561. if (pa->pa_deleted) {
  2562. spin_unlock(&pa->pa_lock);
  2563. continue;
  2564. }
  2565. pa_end = pa->pa_lstart + pa->pa_len;
  2566. /* PA must not overlap original request */
  2567. BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
  2568. ac->ac_o_ex.fe_logical < pa->pa_lstart));
  2569. /* skip PAs this normalized request doesn't overlap with */
  2570. if (pa->pa_lstart >= end || pa_end <= start) {
  2571. spin_unlock(&pa->pa_lock);
  2572. continue;
  2573. }
  2574. BUG_ON(pa->pa_lstart <= start && pa_end >= end);
  2575. /* adjust start or end to be adjacent to this pa */
  2576. if (pa_end <= ac->ac_o_ex.fe_logical) {
  2577. BUG_ON(pa_end < start);
  2578. start = pa_end;
  2579. } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
  2580. BUG_ON(pa->pa_lstart > end);
  2581. end = pa->pa_lstart;
  2582. }
  2583. spin_unlock(&pa->pa_lock);
  2584. }
  2585. rcu_read_unlock();
  2586. size = end - start;
  2587. /* XXX: extra loop to check we really don't overlap preallocations */
  2588. rcu_read_lock();
  2589. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2590. ext4_lblk_t pa_end;
  2591. spin_lock(&pa->pa_lock);
  2592. if (pa->pa_deleted == 0) {
  2593. pa_end = pa->pa_lstart + pa->pa_len;
  2594. BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
  2595. }
  2596. spin_unlock(&pa->pa_lock);
  2597. }
  2598. rcu_read_unlock();
  2599. if (start + size <= ac->ac_o_ex.fe_logical &&
  2600. start > ac->ac_o_ex.fe_logical) {
  2601. printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
  2602. (unsigned long) start, (unsigned long) size,
  2603. (unsigned long) ac->ac_o_ex.fe_logical);
  2604. }
  2605. BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
  2606. start > ac->ac_o_ex.fe_logical);
  2607. BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  2608. /* now prepare goal request */
  2609. /* XXX: is it better to align blocks WRT to logical
  2610. * placement or satisfy big request as is */
  2611. ac->ac_g_ex.fe_logical = start;
  2612. ac->ac_g_ex.fe_len = size;
  2613. /* define goal start in order to merge */
  2614. if (ar->pright && (ar->lright == (start + size))) {
  2615. /* merge to the right */
  2616. ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
  2617. &ac->ac_f_ex.fe_group,
  2618. &ac->ac_f_ex.fe_start);
  2619. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2620. }
  2621. if (ar->pleft && (ar->lleft + 1 == start)) {
  2622. /* merge to the left */
  2623. ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
  2624. &ac->ac_f_ex.fe_group,
  2625. &ac->ac_f_ex.fe_start);
  2626. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2627. }
  2628. mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
  2629. (unsigned) orig_size, (unsigned) start);
  2630. }
  2631. static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
  2632. {
  2633. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2634. if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
  2635. atomic_inc(&sbi->s_bal_reqs);
  2636. atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
  2637. if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
  2638. atomic_inc(&sbi->s_bal_success);
  2639. atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
  2640. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2641. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2642. atomic_inc(&sbi->s_bal_goals);
  2643. if (ac->ac_found > sbi->s_mb_max_to_scan)
  2644. atomic_inc(&sbi->s_bal_breaks);
  2645. }
  2646. if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
  2647. trace_ext4_mballoc_alloc(ac);
  2648. else
  2649. trace_ext4_mballoc_prealloc(ac);
  2650. }
  2651. /*
  2652. * Called on failure; free up any blocks from the inode PA for this
  2653. * context. We don't need this for MB_GROUP_PA because we only change
  2654. * pa_free in ext4_mb_release_context(), but on failure, we've already
  2655. * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
  2656. */
  2657. static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
  2658. {
  2659. struct ext4_prealloc_space *pa = ac->ac_pa;
  2660. int len;
  2661. if (pa && pa->pa_type == MB_INODE_PA) {
  2662. len = ac->ac_b_ex.fe_len;
  2663. pa->pa_free += len;
  2664. }
  2665. }
  2666. /*
  2667. * use blocks preallocated to inode
  2668. */
  2669. static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
  2670. struct ext4_prealloc_space *pa)
  2671. {
  2672. ext4_fsblk_t start;
  2673. ext4_fsblk_t end;
  2674. int len;
  2675. /* found preallocated blocks, use them */
  2676. start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
  2677. end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
  2678. len = end - start;
  2679. ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
  2680. &ac->ac_b_ex.fe_start);
  2681. ac->ac_b_ex.fe_len = len;
  2682. ac->ac_status = AC_STATUS_FOUND;
  2683. ac->ac_pa = pa;
  2684. BUG_ON(start < pa->pa_pstart);
  2685. BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
  2686. BUG_ON(pa->pa_free < len);
  2687. pa->pa_free -= len;
  2688. mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
  2689. }
  2690. /*
  2691. * use blocks preallocated to locality group
  2692. */
  2693. static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
  2694. struct ext4_prealloc_space *pa)
  2695. {
  2696. unsigned int len = ac->ac_o_ex.fe_len;
  2697. ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
  2698. &ac->ac_b_ex.fe_group,
  2699. &ac->ac_b_ex.fe_start);
  2700. ac->ac_b_ex.fe_len = len;
  2701. ac->ac_status = AC_STATUS_FOUND;
  2702. ac->ac_pa = pa;
  2703. /* we don't correct pa_pstart or pa_plen here to avoid
  2704. * possible race when the group is being loaded concurrently
  2705. * instead we correct pa later, after blocks are marked
  2706. * in on-disk bitmap -- see ext4_mb_release_context()
  2707. * Other CPUs are prevented from allocating from this pa by lg_mutex
  2708. */
  2709. mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
  2710. }
  2711. /*
  2712. * Return the prealloc space that have minimal distance
  2713. * from the goal block. @cpa is the prealloc
  2714. * space that is having currently known minimal distance
  2715. * from the goal block.
  2716. */
  2717. static struct ext4_prealloc_space *
  2718. ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
  2719. struct ext4_prealloc_space *pa,
  2720. struct ext4_prealloc_space *cpa)
  2721. {
  2722. ext4_fsblk_t cur_distance, new_distance;
  2723. if (cpa == NULL) {
  2724. atomic_inc(&pa->pa_count);
  2725. return pa;
  2726. }
  2727. cur_distance = abs(goal_block - cpa->pa_pstart);
  2728. new_distance = abs(goal_block - pa->pa_pstart);
  2729. if (cur_distance < new_distance)
  2730. return cpa;
  2731. /* drop the previous reference */
  2732. atomic_dec(&cpa->pa_count);
  2733. atomic_inc(&pa->pa_count);
  2734. return pa;
  2735. }
  2736. /*
  2737. * search goal blocks in preallocated space
  2738. */
  2739. static noinline_for_stack int
  2740. ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
  2741. {
  2742. int order, i;
  2743. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2744. struct ext4_locality_group *lg;
  2745. struct ext4_prealloc_space *pa, *cpa = NULL;
  2746. ext4_fsblk_t goal_block;
  2747. /* only data can be preallocated */
  2748. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2749. return 0;
  2750. /* first, try per-file preallocation */
  2751. rcu_read_lock();
  2752. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2753. /* all fields in this condition don't change,
  2754. * so we can skip locking for them */
  2755. if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
  2756. ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
  2757. continue;
  2758. /* non-extent files can't have physical blocks past 2^32 */
  2759. if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL) &&
  2760. pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
  2761. continue;
  2762. /* found preallocated blocks, use them */
  2763. spin_lock(&pa->pa_lock);
  2764. if (pa->pa_deleted == 0 && pa->pa_free) {
  2765. atomic_inc(&pa->pa_count);
  2766. ext4_mb_use_inode_pa(ac, pa);
  2767. spin_unlock(&pa->pa_lock);
  2768. ac->ac_criteria = 10;
  2769. rcu_read_unlock();
  2770. return 1;
  2771. }
  2772. spin_unlock(&pa->pa_lock);
  2773. }
  2774. rcu_read_unlock();
  2775. /* can we use group allocation? */
  2776. if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
  2777. return 0;
  2778. /* inode may have no locality group for some reason */
  2779. lg = ac->ac_lg;
  2780. if (lg == NULL)
  2781. return 0;
  2782. order = fls(ac->ac_o_ex.fe_len) - 1;
  2783. if (order > PREALLOC_TB_SIZE - 1)
  2784. /* The max size of hash table is PREALLOC_TB_SIZE */
  2785. order = PREALLOC_TB_SIZE - 1;
  2786. goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
  2787. /*
  2788. * search for the prealloc space that is having
  2789. * minimal distance from the goal block.
  2790. */
  2791. for (i = order; i < PREALLOC_TB_SIZE; i++) {
  2792. rcu_read_lock();
  2793. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
  2794. pa_inode_list) {
  2795. spin_lock(&pa->pa_lock);
  2796. if (pa->pa_deleted == 0 &&
  2797. pa->pa_free >= ac->ac_o_ex.fe_len) {
  2798. cpa = ext4_mb_check_group_pa(goal_block,
  2799. pa, cpa);
  2800. }
  2801. spin_unlock(&pa->pa_lock);
  2802. }
  2803. rcu_read_unlock();
  2804. }
  2805. if (cpa) {
  2806. ext4_mb_use_group_pa(ac, cpa);
  2807. ac->ac_criteria = 20;
  2808. return 1;
  2809. }
  2810. return 0;
  2811. }
  2812. /*
  2813. * the function goes through all block freed in the group
  2814. * but not yet committed and marks them used in in-core bitmap.
  2815. * buddy must be generated from this bitmap
  2816. * Need to be called with the ext4 group lock held
  2817. */
  2818. static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
  2819. ext4_group_t group)
  2820. {
  2821. struct rb_node *n;
  2822. struct ext4_group_info *grp;
  2823. struct ext4_free_data *entry;
  2824. grp = ext4_get_group_info(sb, group);
  2825. n = rb_first(&(grp->bb_free_root));
  2826. while (n) {
  2827. entry = rb_entry(n, struct ext4_free_data, node);
  2828. mb_set_bits(bitmap, entry->start_blk, entry->count);
  2829. n = rb_next(n);
  2830. }
  2831. return;
  2832. }
  2833. /*
  2834. * the function goes through all preallocation in this group and marks them
  2835. * used in in-core bitmap. buddy must be generated from this bitmap
  2836. * Need to be called with ext4 group lock held
  2837. */
  2838. static noinline_for_stack
  2839. void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  2840. ext4_group_t group)
  2841. {
  2842. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  2843. struct ext4_prealloc_space *pa;
  2844. struct list_head *cur;
  2845. ext4_group_t groupnr;
  2846. ext4_grpblk_t start;
  2847. int preallocated = 0;
  2848. int count = 0;
  2849. int len;
  2850. /* all form of preallocation discards first load group,
  2851. * so the only competing code is preallocation use.
  2852. * we don't need any locking here
  2853. * notice we do NOT ignore preallocations with pa_deleted
  2854. * otherwise we could leave used blocks available for
  2855. * allocation in buddy when concurrent ext4_mb_put_pa()
  2856. * is dropping preallocation
  2857. */
  2858. list_for_each(cur, &grp->bb_prealloc_list) {
  2859. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2860. spin_lock(&pa->pa_lock);
  2861. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  2862. &groupnr, &start);
  2863. len = pa->pa_len;
  2864. spin_unlock(&pa->pa_lock);
  2865. if (unlikely(len == 0))
  2866. continue;
  2867. BUG_ON(groupnr != group);
  2868. mb_set_bits(bitmap, start, len);
  2869. preallocated += len;
  2870. count++;
  2871. }
  2872. mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
  2873. }
  2874. static void ext4_mb_pa_callback(struct rcu_head *head)
  2875. {
  2876. struct ext4_prealloc_space *pa;
  2877. pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
  2878. kmem_cache_free(ext4_pspace_cachep, pa);
  2879. }
  2880. /*
  2881. * drops a reference to preallocated space descriptor
  2882. * if this was the last reference and the space is consumed
  2883. */
  2884. static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
  2885. struct super_block *sb, struct ext4_prealloc_space *pa)
  2886. {
  2887. ext4_group_t grp;
  2888. ext4_fsblk_t grp_blk;
  2889. if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
  2890. return;
  2891. /* in this short window concurrent discard can set pa_deleted */
  2892. spin_lock(&pa->pa_lock);
  2893. if (pa->pa_deleted == 1) {
  2894. spin_unlock(&pa->pa_lock);
  2895. return;
  2896. }
  2897. pa->pa_deleted = 1;
  2898. spin_unlock(&pa->pa_lock);
  2899. grp_blk = pa->pa_pstart;
  2900. /*
  2901. * If doing group-based preallocation, pa_pstart may be in the
  2902. * next group when pa is used up
  2903. */
  2904. if (pa->pa_type == MB_GROUP_PA)
  2905. grp_blk--;
  2906. ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
  2907. /*
  2908. * possible race:
  2909. *
  2910. * P1 (buddy init) P2 (regular allocation)
  2911. * find block B in PA
  2912. * copy on-disk bitmap to buddy
  2913. * mark B in on-disk bitmap
  2914. * drop PA from group
  2915. * mark all PAs in buddy
  2916. *
  2917. * thus, P1 initializes buddy with B available. to prevent this
  2918. * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
  2919. * against that pair
  2920. */
  2921. ext4_lock_group(sb, grp);
  2922. list_del(&pa->pa_group_list);
  2923. ext4_unlock_group(sb, grp);
  2924. spin_lock(pa->pa_obj_lock);
  2925. list_del_rcu(&pa->pa_inode_list);
  2926. spin_unlock(pa->pa_obj_lock);
  2927. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  2928. }
  2929. /*
  2930. * creates new preallocated space for given inode
  2931. */
  2932. static noinline_for_stack int
  2933. ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
  2934. {
  2935. struct super_block *sb = ac->ac_sb;
  2936. struct ext4_prealloc_space *pa;
  2937. struct ext4_group_info *grp;
  2938. struct ext4_inode_info *ei;
  2939. /* preallocate only when found space is larger then requested */
  2940. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  2941. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2942. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  2943. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  2944. if (pa == NULL)
  2945. return -ENOMEM;
  2946. if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
  2947. int winl;
  2948. int wins;
  2949. int win;
  2950. int offs;
  2951. /* we can't allocate as much as normalizer wants.
  2952. * so, found space must get proper lstart
  2953. * to cover original request */
  2954. BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
  2955. BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
  2956. /* we're limited by original request in that
  2957. * logical block must be covered any way
  2958. * winl is window we can move our chunk within */
  2959. winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
  2960. /* also, we should cover whole original request */
  2961. wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
  2962. /* the smallest one defines real window */
  2963. win = min(winl, wins);
  2964. offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
  2965. if (offs && offs < win)
  2966. win = offs;
  2967. ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
  2968. BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
  2969. BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
  2970. }
  2971. /* preallocation can change ac_b_ex, thus we store actually
  2972. * allocated blocks for history */
  2973. ac->ac_f_ex = ac->ac_b_ex;
  2974. pa->pa_lstart = ac->ac_b_ex.fe_logical;
  2975. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  2976. pa->pa_len = ac->ac_b_ex.fe_len;
  2977. pa->pa_free = pa->pa_len;
  2978. atomic_set(&pa->pa_count, 1);
  2979. spin_lock_init(&pa->pa_lock);
  2980. INIT_LIST_HEAD(&pa->pa_inode_list);
  2981. INIT_LIST_HEAD(&pa->pa_group_list);
  2982. pa->pa_deleted = 0;
  2983. pa->pa_type = MB_INODE_PA;
  2984. mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
  2985. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  2986. trace_ext4_mb_new_inode_pa(ac, pa);
  2987. ext4_mb_use_inode_pa(ac, pa);
  2988. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  2989. ei = EXT4_I(ac->ac_inode);
  2990. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  2991. pa->pa_obj_lock = &ei->i_prealloc_lock;
  2992. pa->pa_inode = ac->ac_inode;
  2993. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2994. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  2995. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2996. spin_lock(pa->pa_obj_lock);
  2997. list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
  2998. spin_unlock(pa->pa_obj_lock);
  2999. return 0;
  3000. }
  3001. /*
  3002. * creates new preallocated space for locality group inodes belongs to
  3003. */
  3004. static noinline_for_stack int
  3005. ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
  3006. {
  3007. struct super_block *sb = ac->ac_sb;
  3008. struct ext4_locality_group *lg;
  3009. struct ext4_prealloc_space *pa;
  3010. struct ext4_group_info *grp;
  3011. /* preallocate only when found space is larger then requested */
  3012. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3013. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3014. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3015. BUG_ON(ext4_pspace_cachep == NULL);
  3016. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3017. if (pa == NULL)
  3018. return -ENOMEM;
  3019. /* preallocation can change ac_b_ex, thus we store actually
  3020. * allocated blocks for history */
  3021. ac->ac_f_ex = ac->ac_b_ex;
  3022. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3023. pa->pa_lstart = pa->pa_pstart;
  3024. pa->pa_len = ac->ac_b_ex.fe_len;
  3025. pa->pa_free = pa->pa_len;
  3026. atomic_set(&pa->pa_count, 1);
  3027. spin_lock_init(&pa->pa_lock);
  3028. INIT_LIST_HEAD(&pa->pa_inode_list);
  3029. INIT_LIST_HEAD(&pa->pa_group_list);
  3030. pa->pa_deleted = 0;
  3031. pa->pa_type = MB_GROUP_PA;
  3032. mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
  3033. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3034. trace_ext4_mb_new_group_pa(ac, pa);
  3035. ext4_mb_use_group_pa(ac, pa);
  3036. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3037. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3038. lg = ac->ac_lg;
  3039. BUG_ON(lg == NULL);
  3040. pa->pa_obj_lock = &lg->lg_prealloc_lock;
  3041. pa->pa_inode = NULL;
  3042. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3043. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3044. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3045. /*
  3046. * We will later add the new pa to the right bucket
  3047. * after updating the pa_free in ext4_mb_release_context
  3048. */
  3049. return 0;
  3050. }
  3051. static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
  3052. {
  3053. int err;
  3054. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3055. err = ext4_mb_new_group_pa(ac);
  3056. else
  3057. err = ext4_mb_new_inode_pa(ac);
  3058. return err;
  3059. }
  3060. /*
  3061. * finds all unused blocks in on-disk bitmap, frees them in
  3062. * in-core bitmap and buddy.
  3063. * @pa must be unlinked from inode and group lists, so that
  3064. * nobody else can find/use it.
  3065. * the caller MUST hold group/inode locks.
  3066. * TODO: optimize the case when there are no in-core structures yet
  3067. */
  3068. static noinline_for_stack int
  3069. ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
  3070. struct ext4_prealloc_space *pa,
  3071. struct ext4_allocation_context *ac)
  3072. {
  3073. struct super_block *sb = e4b->bd_sb;
  3074. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3075. unsigned int end;
  3076. unsigned int next;
  3077. ext4_group_t group;
  3078. ext4_grpblk_t bit;
  3079. unsigned long long grp_blk_start;
  3080. sector_t start;
  3081. int err = 0;
  3082. int free = 0;
  3083. BUG_ON(pa->pa_deleted == 0);
  3084. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3085. grp_blk_start = pa->pa_pstart - bit;
  3086. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3087. end = bit + pa->pa_len;
  3088. if (ac) {
  3089. ac->ac_sb = sb;
  3090. ac->ac_inode = pa->pa_inode;
  3091. }
  3092. while (bit < end) {
  3093. bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3094. if (bit >= end)
  3095. break;
  3096. next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
  3097. start = ext4_group_first_block_no(sb, group) + bit;
  3098. mb_debug(1, " free preallocated %u/%u in group %u\n",
  3099. (unsigned) start, (unsigned) next - bit,
  3100. (unsigned) group);
  3101. free += next - bit;
  3102. if (ac) {
  3103. ac->ac_b_ex.fe_group = group;
  3104. ac->ac_b_ex.fe_start = bit;
  3105. ac->ac_b_ex.fe_len = next - bit;
  3106. ac->ac_b_ex.fe_logical = 0;
  3107. trace_ext4_mballoc_discard(ac);
  3108. }
  3109. trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
  3110. next - bit);
  3111. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3112. bit = next + 1;
  3113. }
  3114. if (free != pa->pa_free) {
  3115. printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
  3116. pa, (unsigned long) pa->pa_lstart,
  3117. (unsigned long) pa->pa_pstart,
  3118. (unsigned long) pa->pa_len);
  3119. ext4_grp_locked_error(sb, group,
  3120. __func__, "free %u, pa_free %u",
  3121. free, pa->pa_free);
  3122. /*
  3123. * pa is already deleted so we use the value obtained
  3124. * from the bitmap and continue.
  3125. */
  3126. }
  3127. atomic_add(free, &sbi->s_mb_discarded);
  3128. return err;
  3129. }
  3130. static noinline_for_stack int
  3131. ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3132. struct ext4_prealloc_space *pa,
  3133. struct ext4_allocation_context *ac)
  3134. {
  3135. struct super_block *sb = e4b->bd_sb;
  3136. ext4_group_t group;
  3137. ext4_grpblk_t bit;
  3138. trace_ext4_mb_release_group_pa(ac, pa);
  3139. BUG_ON(pa->pa_deleted == 0);
  3140. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3141. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3142. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3143. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3144. if (ac) {
  3145. ac->ac_sb = sb;
  3146. ac->ac_inode = NULL;
  3147. ac->ac_b_ex.fe_group = group;
  3148. ac->ac_b_ex.fe_start = bit;
  3149. ac->ac_b_ex.fe_len = pa->pa_len;
  3150. ac->ac_b_ex.fe_logical = 0;
  3151. trace_ext4_mballoc_discard(ac);
  3152. }
  3153. return 0;
  3154. }
  3155. /*
  3156. * releases all preallocations in given group
  3157. *
  3158. * first, we need to decide discard policy:
  3159. * - when do we discard
  3160. * 1) ENOSPC
  3161. * - how many do we discard
  3162. * 1) how many requested
  3163. */
  3164. static noinline_for_stack int
  3165. ext4_mb_discard_group_preallocations(struct super_block *sb,
  3166. ext4_group_t group, int needed)
  3167. {
  3168. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3169. struct buffer_head *bitmap_bh = NULL;
  3170. struct ext4_prealloc_space *pa, *tmp;
  3171. struct ext4_allocation_context *ac;
  3172. struct list_head list;
  3173. struct ext4_buddy e4b;
  3174. int err;
  3175. int busy = 0;
  3176. int free = 0;
  3177. mb_debug(1, "discard preallocation for group %u\n", group);
  3178. if (list_empty(&grp->bb_prealloc_list))
  3179. return 0;
  3180. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3181. if (bitmap_bh == NULL) {
  3182. ext4_error(sb, "Error reading block bitmap for %u", group);
  3183. return 0;
  3184. }
  3185. err = ext4_mb_load_buddy(sb, group, &e4b);
  3186. if (err) {
  3187. ext4_error(sb, "Error loading buddy information for %u", group);
  3188. put_bh(bitmap_bh);
  3189. return 0;
  3190. }
  3191. if (needed == 0)
  3192. needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
  3193. INIT_LIST_HEAD(&list);
  3194. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3195. if (ac)
  3196. ac->ac_sb = sb;
  3197. repeat:
  3198. ext4_lock_group(sb, group);
  3199. list_for_each_entry_safe(pa, tmp,
  3200. &grp->bb_prealloc_list, pa_group_list) {
  3201. spin_lock(&pa->pa_lock);
  3202. if (atomic_read(&pa->pa_count)) {
  3203. spin_unlock(&pa->pa_lock);
  3204. busy = 1;
  3205. continue;
  3206. }
  3207. if (pa->pa_deleted) {
  3208. spin_unlock(&pa->pa_lock);
  3209. continue;
  3210. }
  3211. /* seems this one can be freed ... */
  3212. pa->pa_deleted = 1;
  3213. /* we can trust pa_free ... */
  3214. free += pa->pa_free;
  3215. spin_unlock(&pa->pa_lock);
  3216. list_del(&pa->pa_group_list);
  3217. list_add(&pa->u.pa_tmp_list, &list);
  3218. }
  3219. /* if we still need more blocks and some PAs were used, try again */
  3220. if (free < needed && busy) {
  3221. busy = 0;
  3222. ext4_unlock_group(sb, group);
  3223. /*
  3224. * Yield the CPU here so that we don't get soft lockup
  3225. * in non preempt case.
  3226. */
  3227. yield();
  3228. goto repeat;
  3229. }
  3230. /* found anything to free? */
  3231. if (list_empty(&list)) {
  3232. BUG_ON(free != 0);
  3233. goto out;
  3234. }
  3235. /* now free all selected PAs */
  3236. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3237. /* remove from object (inode or locality group) */
  3238. spin_lock(pa->pa_obj_lock);
  3239. list_del_rcu(&pa->pa_inode_list);
  3240. spin_unlock(pa->pa_obj_lock);
  3241. if (pa->pa_type == MB_GROUP_PA)
  3242. ext4_mb_release_group_pa(&e4b, pa, ac);
  3243. else
  3244. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3245. list_del(&pa->u.pa_tmp_list);
  3246. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3247. }
  3248. out:
  3249. ext4_unlock_group(sb, group);
  3250. if (ac)
  3251. kmem_cache_free(ext4_ac_cachep, ac);
  3252. ext4_mb_release_desc(&e4b);
  3253. put_bh(bitmap_bh);
  3254. return free;
  3255. }
  3256. /*
  3257. * releases all non-used preallocated blocks for given inode
  3258. *
  3259. * It's important to discard preallocations under i_data_sem
  3260. * We don't want another block to be served from the prealloc
  3261. * space when we are discarding the inode prealloc space.
  3262. *
  3263. * FIXME!! Make sure it is valid at all the call sites
  3264. */
  3265. void ext4_discard_preallocations(struct inode *inode)
  3266. {
  3267. struct ext4_inode_info *ei = EXT4_I(inode);
  3268. struct super_block *sb = inode->i_sb;
  3269. struct buffer_head *bitmap_bh = NULL;
  3270. struct ext4_prealloc_space *pa, *tmp;
  3271. struct ext4_allocation_context *ac;
  3272. ext4_group_t group = 0;
  3273. struct list_head list;
  3274. struct ext4_buddy e4b;
  3275. int err;
  3276. if (!S_ISREG(inode->i_mode)) {
  3277. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3278. return;
  3279. }
  3280. mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
  3281. trace_ext4_discard_preallocations(inode);
  3282. INIT_LIST_HEAD(&list);
  3283. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3284. if (ac) {
  3285. ac->ac_sb = sb;
  3286. ac->ac_inode = inode;
  3287. }
  3288. repeat:
  3289. /* first, collect all pa's in the inode */
  3290. spin_lock(&ei->i_prealloc_lock);
  3291. while (!list_empty(&ei->i_prealloc_list)) {
  3292. pa = list_entry(ei->i_prealloc_list.next,
  3293. struct ext4_prealloc_space, pa_inode_list);
  3294. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3295. spin_lock(&pa->pa_lock);
  3296. if (atomic_read(&pa->pa_count)) {
  3297. /* this shouldn't happen often - nobody should
  3298. * use preallocation while we're discarding it */
  3299. spin_unlock(&pa->pa_lock);
  3300. spin_unlock(&ei->i_prealloc_lock);
  3301. printk(KERN_ERR "uh-oh! used pa while discarding\n");
  3302. WARN_ON(1);
  3303. schedule_timeout_uninterruptible(HZ);
  3304. goto repeat;
  3305. }
  3306. if (pa->pa_deleted == 0) {
  3307. pa->pa_deleted = 1;
  3308. spin_unlock(&pa->pa_lock);
  3309. list_del_rcu(&pa->pa_inode_list);
  3310. list_add(&pa->u.pa_tmp_list, &list);
  3311. continue;
  3312. }
  3313. /* someone is deleting pa right now */
  3314. spin_unlock(&pa->pa_lock);
  3315. spin_unlock(&ei->i_prealloc_lock);
  3316. /* we have to wait here because pa_deleted
  3317. * doesn't mean pa is already unlinked from
  3318. * the list. as we might be called from
  3319. * ->clear_inode() the inode will get freed
  3320. * and concurrent thread which is unlinking
  3321. * pa from inode's list may access already
  3322. * freed memory, bad-bad-bad */
  3323. /* XXX: if this happens too often, we can
  3324. * add a flag to force wait only in case
  3325. * of ->clear_inode(), but not in case of
  3326. * regular truncate */
  3327. schedule_timeout_uninterruptible(HZ);
  3328. goto repeat;
  3329. }
  3330. spin_unlock(&ei->i_prealloc_lock);
  3331. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3332. BUG_ON(pa->pa_type != MB_INODE_PA);
  3333. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3334. err = ext4_mb_load_buddy(sb, group, &e4b);
  3335. if (err) {
  3336. ext4_error(sb, "Error loading buddy information for %u",
  3337. group);
  3338. continue;
  3339. }
  3340. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3341. if (bitmap_bh == NULL) {
  3342. ext4_error(sb, "Error reading block bitmap for %u",
  3343. group);
  3344. ext4_mb_release_desc(&e4b);
  3345. continue;
  3346. }
  3347. ext4_lock_group(sb, group);
  3348. list_del(&pa->pa_group_list);
  3349. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3350. ext4_unlock_group(sb, group);
  3351. ext4_mb_release_desc(&e4b);
  3352. put_bh(bitmap_bh);
  3353. list_del(&pa->u.pa_tmp_list);
  3354. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3355. }
  3356. if (ac)
  3357. kmem_cache_free(ext4_ac_cachep, ac);
  3358. }
  3359. /*
  3360. * finds all preallocated spaces and return blocks being freed to them
  3361. * if preallocated space becomes full (no block is used from the space)
  3362. * then the function frees space in buddy
  3363. * XXX: at the moment, truncate (which is the only way to free blocks)
  3364. * discards all preallocations
  3365. */
  3366. static void ext4_mb_return_to_preallocation(struct inode *inode,
  3367. struct ext4_buddy *e4b,
  3368. sector_t block, int count)
  3369. {
  3370. BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
  3371. }
  3372. #ifdef CONFIG_EXT4_DEBUG
  3373. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3374. {
  3375. struct super_block *sb = ac->ac_sb;
  3376. ext4_group_t ngroups, i;
  3377. printk(KERN_ERR "EXT4-fs: Can't allocate:"
  3378. " Allocation context details:\n");
  3379. printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
  3380. ac->ac_status, ac->ac_flags);
  3381. printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
  3382. "best %lu/%lu/%lu@%lu cr %d\n",
  3383. (unsigned long)ac->ac_o_ex.fe_group,
  3384. (unsigned long)ac->ac_o_ex.fe_start,
  3385. (unsigned long)ac->ac_o_ex.fe_len,
  3386. (unsigned long)ac->ac_o_ex.fe_logical,
  3387. (unsigned long)ac->ac_g_ex.fe_group,
  3388. (unsigned long)ac->ac_g_ex.fe_start,
  3389. (unsigned long)ac->ac_g_ex.fe_len,
  3390. (unsigned long)ac->ac_g_ex.fe_logical,
  3391. (unsigned long)ac->ac_b_ex.fe_group,
  3392. (unsigned long)ac->ac_b_ex.fe_start,
  3393. (unsigned long)ac->ac_b_ex.fe_len,
  3394. (unsigned long)ac->ac_b_ex.fe_logical,
  3395. (int)ac->ac_criteria);
  3396. printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
  3397. ac->ac_found);
  3398. printk(KERN_ERR "EXT4-fs: groups: \n");
  3399. ngroups = ext4_get_groups_count(sb);
  3400. for (i = 0; i < ngroups; i++) {
  3401. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3402. struct ext4_prealloc_space *pa;
  3403. ext4_grpblk_t start;
  3404. struct list_head *cur;
  3405. ext4_lock_group(sb, i);
  3406. list_for_each(cur, &grp->bb_prealloc_list) {
  3407. pa = list_entry(cur, struct ext4_prealloc_space,
  3408. pa_group_list);
  3409. spin_lock(&pa->pa_lock);
  3410. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3411. NULL, &start);
  3412. spin_unlock(&pa->pa_lock);
  3413. printk(KERN_ERR "PA:%u:%d:%u \n", i,
  3414. start, pa->pa_len);
  3415. }
  3416. ext4_unlock_group(sb, i);
  3417. if (grp->bb_free == 0)
  3418. continue;
  3419. printk(KERN_ERR "%u: %d/%d \n",
  3420. i, grp->bb_free, grp->bb_fragments);
  3421. }
  3422. printk(KERN_ERR "\n");
  3423. }
  3424. #else
  3425. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3426. {
  3427. return;
  3428. }
  3429. #endif
  3430. /*
  3431. * We use locality group preallocation for small size file. The size of the
  3432. * file is determined by the current size or the resulting size after
  3433. * allocation which ever is larger
  3434. *
  3435. * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
  3436. */
  3437. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3438. {
  3439. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3440. int bsbits = ac->ac_sb->s_blocksize_bits;
  3441. loff_t size, isize;
  3442. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3443. return;
  3444. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3445. return;
  3446. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  3447. isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
  3448. >> bsbits;
  3449. if ((size == isize) &&
  3450. !ext4_fs_is_busy(sbi) &&
  3451. (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
  3452. ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
  3453. return;
  3454. }
  3455. /* don't use group allocation for large files */
  3456. size = max(size, isize);
  3457. if (size > sbi->s_mb_stream_request) {
  3458. ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
  3459. return;
  3460. }
  3461. BUG_ON(ac->ac_lg != NULL);
  3462. /*
  3463. * locality group prealloc space are per cpu. The reason for having
  3464. * per cpu locality group is to reduce the contention between block
  3465. * request from multiple CPUs.
  3466. */
  3467. ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
  3468. /* we're going to use group allocation */
  3469. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3470. /* serialize all allocations in the group */
  3471. mutex_lock(&ac->ac_lg->lg_mutex);
  3472. }
  3473. static noinline_for_stack int
  3474. ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3475. struct ext4_allocation_request *ar)
  3476. {
  3477. struct super_block *sb = ar->inode->i_sb;
  3478. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3479. struct ext4_super_block *es = sbi->s_es;
  3480. ext4_group_t group;
  3481. unsigned int len;
  3482. ext4_fsblk_t goal;
  3483. ext4_grpblk_t block;
  3484. /* we can't allocate > group size */
  3485. len = ar->len;
  3486. /* just a dirty hack to filter too big requests */
  3487. if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
  3488. len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
  3489. /* start searching from the goal */
  3490. goal = ar->goal;
  3491. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3492. goal >= ext4_blocks_count(es))
  3493. goal = le32_to_cpu(es->s_first_data_block);
  3494. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3495. /* set up allocation goals */
  3496. memset(ac, 0, sizeof(struct ext4_allocation_context));
  3497. ac->ac_b_ex.fe_logical = ar->logical;
  3498. ac->ac_status = AC_STATUS_CONTINUE;
  3499. ac->ac_sb = sb;
  3500. ac->ac_inode = ar->inode;
  3501. ac->ac_o_ex.fe_logical = ar->logical;
  3502. ac->ac_o_ex.fe_group = group;
  3503. ac->ac_o_ex.fe_start = block;
  3504. ac->ac_o_ex.fe_len = len;
  3505. ac->ac_g_ex.fe_logical = ar->logical;
  3506. ac->ac_g_ex.fe_group = group;
  3507. ac->ac_g_ex.fe_start = block;
  3508. ac->ac_g_ex.fe_len = len;
  3509. ac->ac_flags = ar->flags;
  3510. /* we have to define context: we'll we work with a file or
  3511. * locality group. this is a policy, actually */
  3512. ext4_mb_group_or_file(ac);
  3513. mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3514. "left: %u/%u, right %u/%u to %swritable\n",
  3515. (unsigned) ar->len, (unsigned) ar->logical,
  3516. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3517. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3518. (unsigned) ar->lright, (unsigned) ar->pright,
  3519. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3520. return 0;
  3521. }
  3522. static noinline_for_stack void
  3523. ext4_mb_discard_lg_preallocations(struct super_block *sb,
  3524. struct ext4_locality_group *lg,
  3525. int order, int total_entries)
  3526. {
  3527. ext4_group_t group = 0;
  3528. struct ext4_buddy e4b;
  3529. struct list_head discard_list;
  3530. struct ext4_prealloc_space *pa, *tmp;
  3531. struct ext4_allocation_context *ac;
  3532. mb_debug(1, "discard locality group preallocation\n");
  3533. INIT_LIST_HEAD(&discard_list);
  3534. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3535. if (ac)
  3536. ac->ac_sb = sb;
  3537. spin_lock(&lg->lg_prealloc_lock);
  3538. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
  3539. pa_inode_list) {
  3540. spin_lock(&pa->pa_lock);
  3541. if (atomic_read(&pa->pa_count)) {
  3542. /*
  3543. * This is the pa that we just used
  3544. * for block allocation. So don't
  3545. * free that
  3546. */
  3547. spin_unlock(&pa->pa_lock);
  3548. continue;
  3549. }
  3550. if (pa->pa_deleted) {
  3551. spin_unlock(&pa->pa_lock);
  3552. continue;
  3553. }
  3554. /* only lg prealloc space */
  3555. BUG_ON(pa->pa_type != MB_GROUP_PA);
  3556. /* seems this one can be freed ... */
  3557. pa->pa_deleted = 1;
  3558. spin_unlock(&pa->pa_lock);
  3559. list_del_rcu(&pa->pa_inode_list);
  3560. list_add(&pa->u.pa_tmp_list, &discard_list);
  3561. total_entries--;
  3562. if (total_entries <= 5) {
  3563. /*
  3564. * we want to keep only 5 entries
  3565. * allowing it to grow to 8. This
  3566. * mak sure we don't call discard
  3567. * soon for this list.
  3568. */
  3569. break;
  3570. }
  3571. }
  3572. spin_unlock(&lg->lg_prealloc_lock);
  3573. list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
  3574. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3575. if (ext4_mb_load_buddy(sb, group, &e4b)) {
  3576. ext4_error(sb, "Error loading buddy information for %u",
  3577. group);
  3578. continue;
  3579. }
  3580. ext4_lock_group(sb, group);
  3581. list_del(&pa->pa_group_list);
  3582. ext4_mb_release_group_pa(&e4b, pa, ac);
  3583. ext4_unlock_group(sb, group);
  3584. ext4_mb_release_desc(&e4b);
  3585. list_del(&pa->u.pa_tmp_list);
  3586. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3587. }
  3588. if (ac)
  3589. kmem_cache_free(ext4_ac_cachep, ac);
  3590. }
  3591. /*
  3592. * We have incremented pa_count. So it cannot be freed at this
  3593. * point. Also we hold lg_mutex. So no parallel allocation is
  3594. * possible from this lg. That means pa_free cannot be updated.
  3595. *
  3596. * A parallel ext4_mb_discard_group_preallocations is possible.
  3597. * which can cause the lg_prealloc_list to be updated.
  3598. */
  3599. static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
  3600. {
  3601. int order, added = 0, lg_prealloc_count = 1;
  3602. struct super_block *sb = ac->ac_sb;
  3603. struct ext4_locality_group *lg = ac->ac_lg;
  3604. struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
  3605. order = fls(pa->pa_free) - 1;
  3606. if (order > PREALLOC_TB_SIZE - 1)
  3607. /* The max size of hash table is PREALLOC_TB_SIZE */
  3608. order = PREALLOC_TB_SIZE - 1;
  3609. /* Add the prealloc space to lg */
  3610. rcu_read_lock();
  3611. list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
  3612. pa_inode_list) {
  3613. spin_lock(&tmp_pa->pa_lock);
  3614. if (tmp_pa->pa_deleted) {
  3615. spin_unlock(&tmp_pa->pa_lock);
  3616. continue;
  3617. }
  3618. if (!added && pa->pa_free < tmp_pa->pa_free) {
  3619. /* Add to the tail of the previous entry */
  3620. list_add_tail_rcu(&pa->pa_inode_list,
  3621. &tmp_pa->pa_inode_list);
  3622. added = 1;
  3623. /*
  3624. * we want to count the total
  3625. * number of entries in the list
  3626. */
  3627. }
  3628. spin_unlock(&tmp_pa->pa_lock);
  3629. lg_prealloc_count++;
  3630. }
  3631. if (!added)
  3632. list_add_tail_rcu(&pa->pa_inode_list,
  3633. &lg->lg_prealloc_list[order]);
  3634. rcu_read_unlock();
  3635. /* Now trim the list to be not more than 8 elements */
  3636. if (lg_prealloc_count > 8) {
  3637. ext4_mb_discard_lg_preallocations(sb, lg,
  3638. order, lg_prealloc_count);
  3639. return;
  3640. }
  3641. return ;
  3642. }
  3643. /*
  3644. * release all resource we used in allocation
  3645. */
  3646. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3647. {
  3648. struct ext4_prealloc_space *pa = ac->ac_pa;
  3649. if (pa) {
  3650. if (pa->pa_type == MB_GROUP_PA) {
  3651. /* see comment in ext4_mb_use_group_pa() */
  3652. spin_lock(&pa->pa_lock);
  3653. pa->pa_pstart += ac->ac_b_ex.fe_len;
  3654. pa->pa_lstart += ac->ac_b_ex.fe_len;
  3655. pa->pa_free -= ac->ac_b_ex.fe_len;
  3656. pa->pa_len -= ac->ac_b_ex.fe_len;
  3657. spin_unlock(&pa->pa_lock);
  3658. }
  3659. }
  3660. if (ac->alloc_semp)
  3661. up_read(ac->alloc_semp);
  3662. if (pa) {
  3663. /*
  3664. * We want to add the pa to the right bucket.
  3665. * Remove it from the list and while adding
  3666. * make sure the list to which we are adding
  3667. * doesn't grow big. We need to release
  3668. * alloc_semp before calling ext4_mb_add_n_trim()
  3669. */
  3670. if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
  3671. spin_lock(pa->pa_obj_lock);
  3672. list_del_rcu(&pa->pa_inode_list);
  3673. spin_unlock(pa->pa_obj_lock);
  3674. ext4_mb_add_n_trim(ac);
  3675. }
  3676. ext4_mb_put_pa(ac, ac->ac_sb, pa);
  3677. }
  3678. if (ac->ac_bitmap_page)
  3679. page_cache_release(ac->ac_bitmap_page);
  3680. if (ac->ac_buddy_page)
  3681. page_cache_release(ac->ac_buddy_page);
  3682. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3683. mutex_unlock(&ac->ac_lg->lg_mutex);
  3684. ext4_mb_collect_stats(ac);
  3685. return 0;
  3686. }
  3687. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3688. {
  3689. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3690. int ret;
  3691. int freed = 0;
  3692. trace_ext4_mb_discard_preallocations(sb, needed);
  3693. for (i = 0; i < ngroups && needed > 0; i++) {
  3694. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3695. freed += ret;
  3696. needed -= ret;
  3697. }
  3698. return freed;
  3699. }
  3700. /*
  3701. * Main entry point into mballoc to allocate blocks
  3702. * it tries to use preallocation first, then falls back
  3703. * to usual allocation
  3704. */
  3705. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3706. struct ext4_allocation_request *ar, int *errp)
  3707. {
  3708. int freed;
  3709. struct ext4_allocation_context *ac = NULL;
  3710. struct ext4_sb_info *sbi;
  3711. struct super_block *sb;
  3712. ext4_fsblk_t block = 0;
  3713. unsigned int inquota = 0;
  3714. unsigned int reserv_blks = 0;
  3715. sb = ar->inode->i_sb;
  3716. sbi = EXT4_SB(sb);
  3717. trace_ext4_request_blocks(ar);
  3718. /*
  3719. * For delayed allocation, we could skip the ENOSPC and
  3720. * EDQUOT check, as blocks and quotas have been already
  3721. * reserved when data being copied into pagecache.
  3722. */
  3723. if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3724. ar->flags |= EXT4_MB_DELALLOC_RESERVED;
  3725. else {
  3726. /* Without delayed allocation we need to verify
  3727. * there is enough free blocks to do block allocation
  3728. * and verify allocation doesn't exceed the quota limits.
  3729. */
  3730. while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
  3731. /* let others to free the space */
  3732. yield();
  3733. ar->len = ar->len >> 1;
  3734. }
  3735. if (!ar->len) {
  3736. *errp = -ENOSPC;
  3737. return 0;
  3738. }
  3739. reserv_blks = ar->len;
  3740. while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
  3741. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3742. ar->len--;
  3743. }
  3744. inquota = ar->len;
  3745. if (ar->len == 0) {
  3746. *errp = -EDQUOT;
  3747. goto out3;
  3748. }
  3749. }
  3750. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3751. if (!ac) {
  3752. ar->len = 0;
  3753. *errp = -ENOMEM;
  3754. goto out1;
  3755. }
  3756. *errp = ext4_mb_initialize_context(ac, ar);
  3757. if (*errp) {
  3758. ar->len = 0;
  3759. goto out2;
  3760. }
  3761. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3762. if (!ext4_mb_use_preallocated(ac)) {
  3763. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3764. ext4_mb_normalize_request(ac, ar);
  3765. repeat:
  3766. /* allocate space in core */
  3767. ext4_mb_regular_allocator(ac);
  3768. /* as we've just preallocated more space than
  3769. * user requested orinally, we store allocated
  3770. * space in a special descriptor */
  3771. if (ac->ac_status == AC_STATUS_FOUND &&
  3772. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  3773. ext4_mb_new_preallocation(ac);
  3774. }
  3775. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  3776. *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
  3777. if (*errp == -EAGAIN) {
  3778. /*
  3779. * drop the reference that we took
  3780. * in ext4_mb_use_best_found
  3781. */
  3782. ext4_mb_release_context(ac);
  3783. ac->ac_b_ex.fe_group = 0;
  3784. ac->ac_b_ex.fe_start = 0;
  3785. ac->ac_b_ex.fe_len = 0;
  3786. ac->ac_status = AC_STATUS_CONTINUE;
  3787. goto repeat;
  3788. } else if (*errp) {
  3789. ext4_discard_allocated_blocks(ac);
  3790. ac->ac_b_ex.fe_len = 0;
  3791. ar->len = 0;
  3792. ext4_mb_show_ac(ac);
  3793. } else {
  3794. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3795. ar->len = ac->ac_b_ex.fe_len;
  3796. }
  3797. } else {
  3798. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  3799. if (freed)
  3800. goto repeat;
  3801. *errp = -ENOSPC;
  3802. ac->ac_b_ex.fe_len = 0;
  3803. ar->len = 0;
  3804. ext4_mb_show_ac(ac);
  3805. }
  3806. ext4_mb_release_context(ac);
  3807. out2:
  3808. kmem_cache_free(ext4_ac_cachep, ac);
  3809. out1:
  3810. if (inquota && ar->len < inquota)
  3811. dquot_free_block(ar->inode, inquota - ar->len);
  3812. out3:
  3813. if (!ar->len) {
  3814. if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3815. /* release all the reserved blocks if non delalloc */
  3816. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  3817. reserv_blks);
  3818. }
  3819. trace_ext4_allocate_blocks(ar, (unsigned long long)block);
  3820. return block;
  3821. }
  3822. /*
  3823. * We can merge two free data extents only if the physical blocks
  3824. * are contiguous, AND the extents were freed by the same transaction,
  3825. * AND the blocks are associated with the same group.
  3826. */
  3827. static int can_merge(struct ext4_free_data *entry1,
  3828. struct ext4_free_data *entry2)
  3829. {
  3830. if ((entry1->t_tid == entry2->t_tid) &&
  3831. (entry1->group == entry2->group) &&
  3832. ((entry1->start_blk + entry1->count) == entry2->start_blk))
  3833. return 1;
  3834. return 0;
  3835. }
  3836. static noinline_for_stack int
  3837. ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
  3838. struct ext4_free_data *new_entry)
  3839. {
  3840. ext4_grpblk_t block;
  3841. struct ext4_free_data *entry;
  3842. struct ext4_group_info *db = e4b->bd_info;
  3843. struct super_block *sb = e4b->bd_sb;
  3844. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3845. struct rb_node **n = &db->bb_free_root.rb_node, *node;
  3846. struct rb_node *parent = NULL, *new_node;
  3847. BUG_ON(!ext4_handle_valid(handle));
  3848. BUG_ON(e4b->bd_bitmap_page == NULL);
  3849. BUG_ON(e4b->bd_buddy_page == NULL);
  3850. new_node = &new_entry->node;
  3851. block = new_entry->start_blk;
  3852. if (!*n) {
  3853. /* first free block exent. We need to
  3854. protect buddy cache from being freed,
  3855. * otherwise we'll refresh it from
  3856. * on-disk bitmap and lose not-yet-available
  3857. * blocks */
  3858. page_cache_get(e4b->bd_buddy_page);
  3859. page_cache_get(e4b->bd_bitmap_page);
  3860. }
  3861. while (*n) {
  3862. parent = *n;
  3863. entry = rb_entry(parent, struct ext4_free_data, node);
  3864. if (block < entry->start_blk)
  3865. n = &(*n)->rb_left;
  3866. else if (block >= (entry->start_blk + entry->count))
  3867. n = &(*n)->rb_right;
  3868. else {
  3869. ext4_grp_locked_error(sb, e4b->bd_group, __func__,
  3870. "Double free of blocks %d (%d %d)",
  3871. block, entry->start_blk, entry->count);
  3872. return 0;
  3873. }
  3874. }
  3875. rb_link_node(new_node, parent, n);
  3876. rb_insert_color(new_node, &db->bb_free_root);
  3877. /* Now try to see the extent can be merged to left and right */
  3878. node = rb_prev(new_node);
  3879. if (node) {
  3880. entry = rb_entry(node, struct ext4_free_data, node);
  3881. if (can_merge(entry, new_entry)) {
  3882. new_entry->start_blk = entry->start_blk;
  3883. new_entry->count += entry->count;
  3884. rb_erase(node, &(db->bb_free_root));
  3885. spin_lock(&sbi->s_md_lock);
  3886. list_del(&entry->list);
  3887. spin_unlock(&sbi->s_md_lock);
  3888. kmem_cache_free(ext4_free_ext_cachep, entry);
  3889. }
  3890. }
  3891. node = rb_next(new_node);
  3892. if (node) {
  3893. entry = rb_entry(node, struct ext4_free_data, node);
  3894. if (can_merge(new_entry, entry)) {
  3895. new_entry->count += entry->count;
  3896. rb_erase(node, &(db->bb_free_root));
  3897. spin_lock(&sbi->s_md_lock);
  3898. list_del(&entry->list);
  3899. spin_unlock(&sbi->s_md_lock);
  3900. kmem_cache_free(ext4_free_ext_cachep, entry);
  3901. }
  3902. }
  3903. /* Add the extent to transaction's private list */
  3904. spin_lock(&sbi->s_md_lock);
  3905. list_add(&new_entry->list, &handle->h_transaction->t_private_list);
  3906. spin_unlock(&sbi->s_md_lock);
  3907. return 0;
  3908. }
  3909. /**
  3910. * ext4_free_blocks() -- Free given blocks and update quota
  3911. * @handle: handle for this transaction
  3912. * @inode: inode
  3913. * @block: start physical block to free
  3914. * @count: number of blocks to count
  3915. * @metadata: Are these metadata blocks
  3916. */
  3917. void ext4_free_blocks(handle_t *handle, struct inode *inode,
  3918. struct buffer_head *bh, ext4_fsblk_t block,
  3919. unsigned long count, int flags)
  3920. {
  3921. struct buffer_head *bitmap_bh = NULL;
  3922. struct super_block *sb = inode->i_sb;
  3923. struct ext4_allocation_context *ac = NULL;
  3924. struct ext4_group_desc *gdp;
  3925. struct ext4_super_block *es;
  3926. unsigned long freed = 0;
  3927. unsigned int overflow;
  3928. ext4_grpblk_t bit;
  3929. struct buffer_head *gd_bh;
  3930. ext4_group_t block_group;
  3931. struct ext4_sb_info *sbi;
  3932. struct ext4_buddy e4b;
  3933. int err = 0;
  3934. int ret;
  3935. if (bh) {
  3936. if (block)
  3937. BUG_ON(block != bh->b_blocknr);
  3938. else
  3939. block = bh->b_blocknr;
  3940. }
  3941. sbi = EXT4_SB(sb);
  3942. es = EXT4_SB(sb)->s_es;
  3943. if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
  3944. !ext4_data_block_valid(sbi, block, count)) {
  3945. ext4_error(sb, "Freeing blocks not in datazone - "
  3946. "block = %llu, count = %lu", block, count);
  3947. goto error_return;
  3948. }
  3949. ext4_debug("freeing block %llu\n", block);
  3950. trace_ext4_free_blocks(inode, block, count, flags);
  3951. if (flags & EXT4_FREE_BLOCKS_FORGET) {
  3952. struct buffer_head *tbh = bh;
  3953. int i;
  3954. BUG_ON(bh && (count > 1));
  3955. for (i = 0; i < count; i++) {
  3956. if (!bh)
  3957. tbh = sb_find_get_block(inode->i_sb,
  3958. block + i);
  3959. ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
  3960. inode, tbh, block + i);
  3961. }
  3962. }
  3963. /*
  3964. * We need to make sure we don't reuse the freed block until
  3965. * after the transaction is committed, which we can do by
  3966. * treating the block as metadata, below. We make an
  3967. * exception if the inode is to be written in writeback mode
  3968. * since writeback mode has weak data consistency guarantees.
  3969. */
  3970. if (!ext4_should_writeback_data(inode))
  3971. flags |= EXT4_FREE_BLOCKS_METADATA;
  3972. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3973. if (ac) {
  3974. ac->ac_inode = inode;
  3975. ac->ac_sb = sb;
  3976. }
  3977. do_more:
  3978. overflow = 0;
  3979. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  3980. /*
  3981. * Check to see if we are freeing blocks across a group
  3982. * boundary.
  3983. */
  3984. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  3985. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  3986. count -= overflow;
  3987. }
  3988. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  3989. if (!bitmap_bh) {
  3990. err = -EIO;
  3991. goto error_return;
  3992. }
  3993. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  3994. if (!gdp) {
  3995. err = -EIO;
  3996. goto error_return;
  3997. }
  3998. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  3999. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  4000. in_range(block, ext4_inode_table(sb, gdp),
  4001. EXT4_SB(sb)->s_itb_per_group) ||
  4002. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  4003. EXT4_SB(sb)->s_itb_per_group)) {
  4004. ext4_error(sb, "Freeing blocks in system zone - "
  4005. "Block = %llu, count = %lu", block, count);
  4006. /* err = 0. ext4_std_error should be a no op */
  4007. goto error_return;
  4008. }
  4009. BUFFER_TRACE(bitmap_bh, "getting write access");
  4010. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4011. if (err)
  4012. goto error_return;
  4013. /*
  4014. * We are about to modify some metadata. Call the journal APIs
  4015. * to unshare ->b_data if a currently-committing transaction is
  4016. * using it
  4017. */
  4018. BUFFER_TRACE(gd_bh, "get_write_access");
  4019. err = ext4_journal_get_write_access(handle, gd_bh);
  4020. if (err)
  4021. goto error_return;
  4022. #ifdef AGGRESSIVE_CHECK
  4023. {
  4024. int i;
  4025. for (i = 0; i < count; i++)
  4026. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  4027. }
  4028. #endif
  4029. if (ac) {
  4030. ac->ac_b_ex.fe_group = block_group;
  4031. ac->ac_b_ex.fe_start = bit;
  4032. ac->ac_b_ex.fe_len = count;
  4033. trace_ext4_mballoc_free(ac);
  4034. }
  4035. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  4036. if (err)
  4037. goto error_return;
  4038. if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
  4039. struct ext4_free_data *new_entry;
  4040. /*
  4041. * blocks being freed are metadata. these blocks shouldn't
  4042. * be used until this transaction is committed
  4043. */
  4044. new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
  4045. new_entry->start_blk = bit;
  4046. new_entry->group = block_group;
  4047. new_entry->count = count;
  4048. new_entry->t_tid = handle->h_transaction->t_tid;
  4049. ext4_lock_group(sb, block_group);
  4050. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4051. ext4_mb_free_metadata(handle, &e4b, new_entry);
  4052. } else {
  4053. /* need to update group_info->bb_free and bitmap
  4054. * with group lock held. generate_buddy look at
  4055. * them with group lock_held
  4056. */
  4057. ext4_lock_group(sb, block_group);
  4058. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4059. mb_free_blocks(inode, &e4b, bit, count);
  4060. ext4_mb_return_to_preallocation(inode, &e4b, block, count);
  4061. }
  4062. ret = ext4_free_blks_count(sb, gdp) + count;
  4063. ext4_free_blks_set(sb, gdp, ret);
  4064. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  4065. ext4_unlock_group(sb, block_group);
  4066. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  4067. if (sbi->s_log_groups_per_flex) {
  4068. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4069. atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
  4070. }
  4071. ext4_mb_release_desc(&e4b);
  4072. freed += count;
  4073. /* We dirtied the bitmap block */
  4074. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4075. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  4076. /* And the group descriptor block */
  4077. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4078. ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
  4079. if (!err)
  4080. err = ret;
  4081. if (overflow && !err) {
  4082. block += count;
  4083. count = overflow;
  4084. put_bh(bitmap_bh);
  4085. goto do_more;
  4086. }
  4087. sb->s_dirt = 1;
  4088. error_return:
  4089. if (freed)
  4090. dquot_free_block(inode, freed);
  4091. brelse(bitmap_bh);
  4092. ext4_std_error(sb, err);
  4093. if (ac)
  4094. kmem_cache_free(ext4_ac_cachep, ac);
  4095. return;
  4096. }