mballoc.c 117 KB

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