mballoc.c 122 KB

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