mballoc.c 132 KB

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