mballoc.c 128 KB

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