mballoc.c 138 KB

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