mballoc.c 118 KB

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