mballoc.c 117 KB

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