mballoc.c 118 KB

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