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