mballoc.c 134 KB

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