mballoc.c 143 KB

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