mballoc.c 136 KB

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