mballoc.c 135 KB

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