mballoc.c 140 KB

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