mballoc.c 126 KB

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