mballoc.c 131 KB

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