mballoc.c 131 KB

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