mballoc.c 116 KB

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