mballoc.c 130 KB

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