mballoc.c 134 KB

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