mballoc.c 127 KB

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