mballoc.c 143 KB

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