extents.c 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/fs.h>
  31. #include <linux/time.h>
  32. #include <linux/jbd2.h>
  33. #include <linux/highuid.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/string.h>
  37. #include <linux/slab.h>
  38. #include <asm/uaccess.h>
  39. #include <linux/fiemap.h>
  40. #include <linux/backing-dev.h>
  41. #include "ext4_jbd2.h"
  42. #include "ext4_extents.h"
  43. #include "xattr.h"
  44. #include <trace/events/ext4.h>
  45. /*
  46. * used by extent splitting.
  47. */
  48. #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
  49. due to ENOSPC */
  50. #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
  51. #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
  52. #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
  53. #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
  54. static __le32 ext4_extent_block_csum(struct inode *inode,
  55. struct ext4_extent_header *eh)
  56. {
  57. struct ext4_inode_info *ei = EXT4_I(inode);
  58. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  59. __u32 csum;
  60. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
  61. EXT4_EXTENT_TAIL_OFFSET(eh));
  62. return cpu_to_le32(csum);
  63. }
  64. static int ext4_extent_block_csum_verify(struct inode *inode,
  65. struct ext4_extent_header *eh)
  66. {
  67. struct ext4_extent_tail *et;
  68. if (!ext4_has_metadata_csum(inode->i_sb))
  69. return 1;
  70. et = find_ext4_extent_tail(eh);
  71. if (et->et_checksum != ext4_extent_block_csum(inode, eh))
  72. return 0;
  73. return 1;
  74. }
  75. static void ext4_extent_block_csum_set(struct inode *inode,
  76. struct ext4_extent_header *eh)
  77. {
  78. struct ext4_extent_tail *et;
  79. if (!ext4_has_metadata_csum(inode->i_sb))
  80. return;
  81. et = find_ext4_extent_tail(eh);
  82. et->et_checksum = ext4_extent_block_csum(inode, eh);
  83. }
  84. static int ext4_split_extent(handle_t *handle,
  85. struct inode *inode,
  86. struct ext4_ext_path **ppath,
  87. struct ext4_map_blocks *map,
  88. int split_flag,
  89. int flags);
  90. static int ext4_split_extent_at(handle_t *handle,
  91. struct inode *inode,
  92. struct ext4_ext_path **ppath,
  93. ext4_lblk_t split,
  94. int split_flag,
  95. int flags);
  96. static int ext4_find_delayed_extent(struct inode *inode,
  97. struct extent_status *newes);
  98. static int ext4_ext_truncate_extend_restart(handle_t *handle,
  99. struct inode *inode,
  100. int needed)
  101. {
  102. int err;
  103. if (!ext4_handle_valid(handle))
  104. return 0;
  105. if (handle->h_buffer_credits >= needed)
  106. return 0;
  107. /*
  108. * If we need to extend the journal get a few extra blocks
  109. * while we're at it for efficiency's sake.
  110. */
  111. needed += 3;
  112. err = ext4_journal_extend(handle, needed - handle->h_buffer_credits);
  113. if (err <= 0)
  114. return err;
  115. err = ext4_truncate_restart_trans(handle, inode, needed);
  116. if (err == 0)
  117. err = -EAGAIN;
  118. return err;
  119. }
  120. /*
  121. * could return:
  122. * - EROFS
  123. * - ENOMEM
  124. */
  125. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  126. struct ext4_ext_path *path)
  127. {
  128. if (path->p_bh) {
  129. /* path points to block */
  130. BUFFER_TRACE(path->p_bh, "get_write_access");
  131. return ext4_journal_get_write_access(handle, path->p_bh);
  132. }
  133. /* path points to leaf/index in inode body */
  134. /* we use in-core data, no need to protect them */
  135. return 0;
  136. }
  137. /*
  138. * could return:
  139. * - EROFS
  140. * - ENOMEM
  141. * - EIO
  142. */
  143. int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
  144. struct inode *inode, struct ext4_ext_path *path)
  145. {
  146. int err;
  147. WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
  148. if (path->p_bh) {
  149. ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
  150. /* path points to block */
  151. err = __ext4_handle_dirty_metadata(where, line, handle,
  152. inode, path->p_bh);
  153. } else {
  154. /* path points to leaf/index in inode body */
  155. err = ext4_mark_inode_dirty(handle, inode);
  156. }
  157. return err;
  158. }
  159. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  160. struct ext4_ext_path *path,
  161. ext4_lblk_t block)
  162. {
  163. if (path) {
  164. int depth = path->p_depth;
  165. struct ext4_extent *ex;
  166. /*
  167. * Try to predict block placement assuming that we are
  168. * filling in a file which will eventually be
  169. * non-sparse --- i.e., in the case of libbfd writing
  170. * an ELF object sections out-of-order but in a way
  171. * the eventually results in a contiguous object or
  172. * executable file, or some database extending a table
  173. * space file. However, this is actually somewhat
  174. * non-ideal if we are writing a sparse file such as
  175. * qemu or KVM writing a raw image file that is going
  176. * to stay fairly sparse, since it will end up
  177. * fragmenting the file system's free space. Maybe we
  178. * should have some hueristics or some way to allow
  179. * userspace to pass a hint to file system,
  180. * especially if the latter case turns out to be
  181. * common.
  182. */
  183. ex = path[depth].p_ext;
  184. if (ex) {
  185. ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
  186. ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
  187. if (block > ext_block)
  188. return ext_pblk + (block - ext_block);
  189. else
  190. return ext_pblk - (ext_block - block);
  191. }
  192. /* it looks like index is empty;
  193. * try to find starting block from index itself */
  194. if (path[depth].p_bh)
  195. return path[depth].p_bh->b_blocknr;
  196. }
  197. /* OK. use inode's group */
  198. return ext4_inode_to_goal_block(inode);
  199. }
  200. /*
  201. * Allocation for a meta data block
  202. */
  203. static ext4_fsblk_t
  204. ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
  205. struct ext4_ext_path *path,
  206. struct ext4_extent *ex, int *err, unsigned int flags)
  207. {
  208. ext4_fsblk_t goal, newblock;
  209. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  210. newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
  211. NULL, err);
  212. return newblock;
  213. }
  214. static inline int ext4_ext_space_block(struct inode *inode, int check)
  215. {
  216. int size;
  217. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  218. / sizeof(struct ext4_extent);
  219. #ifdef AGGRESSIVE_TEST
  220. if (!check && size > 6)
  221. size = 6;
  222. #endif
  223. return size;
  224. }
  225. static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
  226. {
  227. int size;
  228. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  229. / sizeof(struct ext4_extent_idx);
  230. #ifdef AGGRESSIVE_TEST
  231. if (!check && size > 5)
  232. size = 5;
  233. #endif
  234. return size;
  235. }
  236. static inline int ext4_ext_space_root(struct inode *inode, int check)
  237. {
  238. int size;
  239. size = sizeof(EXT4_I(inode)->i_data);
  240. size -= sizeof(struct ext4_extent_header);
  241. size /= sizeof(struct ext4_extent);
  242. #ifdef AGGRESSIVE_TEST
  243. if (!check && size > 3)
  244. size = 3;
  245. #endif
  246. return size;
  247. }
  248. static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
  249. {
  250. int size;
  251. size = sizeof(EXT4_I(inode)->i_data);
  252. size -= sizeof(struct ext4_extent_header);
  253. size /= sizeof(struct ext4_extent_idx);
  254. #ifdef AGGRESSIVE_TEST
  255. if (!check && size > 4)
  256. size = 4;
  257. #endif
  258. return size;
  259. }
  260. static inline int
  261. ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
  262. struct ext4_ext_path **ppath, ext4_lblk_t lblk,
  263. int nofail)
  264. {
  265. struct ext4_ext_path *path = *ppath;
  266. int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
  267. return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
  268. EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
  269. EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
  270. (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
  271. }
  272. /*
  273. * Calculate the number of metadata blocks needed
  274. * to allocate @blocks
  275. * Worse case is one block per extent
  276. */
  277. int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
  278. {
  279. struct ext4_inode_info *ei = EXT4_I(inode);
  280. int idxs;
  281. idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  282. / sizeof(struct ext4_extent_idx));
  283. /*
  284. * If the new delayed allocation block is contiguous with the
  285. * previous da block, it can share index blocks with the
  286. * previous block, so we only need to allocate a new index
  287. * block every idxs leaf blocks. At ldxs**2 blocks, we need
  288. * an additional index block, and at ldxs**3 blocks, yet
  289. * another index blocks.
  290. */
  291. if (ei->i_da_metadata_calc_len &&
  292. ei->i_da_metadata_calc_last_lblock+1 == lblock) {
  293. int num = 0;
  294. if ((ei->i_da_metadata_calc_len % idxs) == 0)
  295. num++;
  296. if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
  297. num++;
  298. if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
  299. num++;
  300. ei->i_da_metadata_calc_len = 0;
  301. } else
  302. ei->i_da_metadata_calc_len++;
  303. ei->i_da_metadata_calc_last_lblock++;
  304. return num;
  305. }
  306. /*
  307. * In the worst case we need a new set of index blocks at
  308. * every level of the inode's extent tree.
  309. */
  310. ei->i_da_metadata_calc_len = 1;
  311. ei->i_da_metadata_calc_last_lblock = lblock;
  312. return ext_depth(inode) + 1;
  313. }
  314. static int
  315. ext4_ext_max_entries(struct inode *inode, int depth)
  316. {
  317. int max;
  318. if (depth == ext_depth(inode)) {
  319. if (depth == 0)
  320. max = ext4_ext_space_root(inode, 1);
  321. else
  322. max = ext4_ext_space_root_idx(inode, 1);
  323. } else {
  324. if (depth == 0)
  325. max = ext4_ext_space_block(inode, 1);
  326. else
  327. max = ext4_ext_space_block_idx(inode, 1);
  328. }
  329. return max;
  330. }
  331. static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
  332. {
  333. ext4_fsblk_t block = ext4_ext_pblock(ext);
  334. int len = ext4_ext_get_actual_len(ext);
  335. ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
  336. /*
  337. * We allow neither:
  338. * - zero length
  339. * - overflow/wrap-around
  340. */
  341. if (lblock + len <= lblock)
  342. return 0;
  343. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
  344. }
  345. static int ext4_valid_extent_idx(struct inode *inode,
  346. struct ext4_extent_idx *ext_idx)
  347. {
  348. ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
  349. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
  350. }
  351. static int ext4_valid_extent_entries(struct inode *inode,
  352. struct ext4_extent_header *eh,
  353. int depth)
  354. {
  355. unsigned short entries;
  356. if (eh->eh_entries == 0)
  357. return 1;
  358. entries = le16_to_cpu(eh->eh_entries);
  359. if (depth == 0) {
  360. /* leaf entries */
  361. struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
  362. struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  363. ext4_fsblk_t pblock = 0;
  364. ext4_lblk_t lblock = 0;
  365. ext4_lblk_t prev = 0;
  366. int len = 0;
  367. while (entries) {
  368. if (!ext4_valid_extent(inode, ext))
  369. return 0;
  370. /* Check for overlapping extents */
  371. lblock = le32_to_cpu(ext->ee_block);
  372. len = ext4_ext_get_actual_len(ext);
  373. if ((lblock <= prev) && prev) {
  374. pblock = ext4_ext_pblock(ext);
  375. es->s_last_error_block = cpu_to_le64(pblock);
  376. return 0;
  377. }
  378. ext++;
  379. entries--;
  380. prev = lblock + len - 1;
  381. }
  382. } else {
  383. struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
  384. while (entries) {
  385. if (!ext4_valid_extent_idx(inode, ext_idx))
  386. return 0;
  387. ext_idx++;
  388. entries--;
  389. }
  390. }
  391. return 1;
  392. }
  393. static int __ext4_ext_check(const char *function, unsigned int line,
  394. struct inode *inode, struct ext4_extent_header *eh,
  395. int depth, ext4_fsblk_t pblk)
  396. {
  397. const char *error_msg;
  398. int max = 0, err = -EFSCORRUPTED;
  399. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  400. error_msg = "invalid magic";
  401. goto corrupted;
  402. }
  403. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  404. error_msg = "unexpected eh_depth";
  405. goto corrupted;
  406. }
  407. if (unlikely(eh->eh_max == 0)) {
  408. error_msg = "invalid eh_max";
  409. goto corrupted;
  410. }
  411. max = ext4_ext_max_entries(inode, depth);
  412. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  413. error_msg = "too large eh_max";
  414. goto corrupted;
  415. }
  416. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  417. error_msg = "invalid eh_entries";
  418. goto corrupted;
  419. }
  420. if (!ext4_valid_extent_entries(inode, eh, depth)) {
  421. error_msg = "invalid extent entries";
  422. goto corrupted;
  423. }
  424. if (unlikely(depth > 32)) {
  425. error_msg = "too large eh_depth";
  426. goto corrupted;
  427. }
  428. /* Verify checksum on non-root extent tree nodes */
  429. if (ext_depth(inode) != depth &&
  430. !ext4_extent_block_csum_verify(inode, eh)) {
  431. error_msg = "extent tree corrupted";
  432. err = -EFSBADCRC;
  433. goto corrupted;
  434. }
  435. return 0;
  436. corrupted:
  437. ext4_error_inode(inode, function, line, 0,
  438. "pblk %llu bad header/extent: %s - magic %x, "
  439. "entries %u, max %u(%u), depth %u(%u)",
  440. (unsigned long long) pblk, error_msg,
  441. le16_to_cpu(eh->eh_magic),
  442. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  443. max, le16_to_cpu(eh->eh_depth), depth);
  444. return err;
  445. }
  446. #define ext4_ext_check(inode, eh, depth, pblk) \
  447. __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
  448. int ext4_ext_check_inode(struct inode *inode)
  449. {
  450. return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
  451. }
  452. static struct buffer_head *
  453. __read_extent_tree_block(const char *function, unsigned int line,
  454. struct inode *inode, ext4_fsblk_t pblk, int depth,
  455. int flags)
  456. {
  457. struct buffer_head *bh;
  458. int err;
  459. bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
  460. if (unlikely(!bh))
  461. return ERR_PTR(-ENOMEM);
  462. if (!bh_uptodate_or_lock(bh)) {
  463. trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
  464. err = bh_submit_read(bh);
  465. if (err < 0)
  466. goto errout;
  467. }
  468. if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
  469. return bh;
  470. err = __ext4_ext_check(function, line, inode,
  471. ext_block_hdr(bh), depth, pblk);
  472. if (err)
  473. goto errout;
  474. set_buffer_verified(bh);
  475. /*
  476. * If this is a leaf block, cache all of its entries
  477. */
  478. if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
  479. struct ext4_extent_header *eh = ext_block_hdr(bh);
  480. struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
  481. ext4_lblk_t prev = 0;
  482. int i;
  483. for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
  484. unsigned int status = EXTENT_STATUS_WRITTEN;
  485. ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
  486. int len = ext4_ext_get_actual_len(ex);
  487. if (prev && (prev != lblk))
  488. ext4_es_cache_extent(inode, prev,
  489. lblk - prev, ~0,
  490. EXTENT_STATUS_HOLE);
  491. if (ext4_ext_is_unwritten(ex))
  492. status = EXTENT_STATUS_UNWRITTEN;
  493. ext4_es_cache_extent(inode, lblk, len,
  494. ext4_ext_pblock(ex), status);
  495. prev = lblk + len;
  496. }
  497. }
  498. return bh;
  499. errout:
  500. put_bh(bh);
  501. return ERR_PTR(err);
  502. }
  503. #define read_extent_tree_block(inode, pblk, depth, flags) \
  504. __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
  505. (depth), (flags))
  506. /*
  507. * This function is called to cache a file's extent information in the
  508. * extent status tree
  509. */
  510. int ext4_ext_precache(struct inode *inode)
  511. {
  512. struct ext4_inode_info *ei = EXT4_I(inode);
  513. struct ext4_ext_path *path = NULL;
  514. struct buffer_head *bh;
  515. int i = 0, depth, ret = 0;
  516. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  517. return 0; /* not an extent-mapped inode */
  518. down_read(&ei->i_data_sem);
  519. depth = ext_depth(inode);
  520. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
  521. GFP_NOFS);
  522. if (path == NULL) {
  523. up_read(&ei->i_data_sem);
  524. return -ENOMEM;
  525. }
  526. /* Don't cache anything if there are no external extent blocks */
  527. if (depth == 0)
  528. goto out;
  529. path[0].p_hdr = ext_inode_hdr(inode);
  530. ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
  531. if (ret)
  532. goto out;
  533. path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
  534. while (i >= 0) {
  535. /*
  536. * If this is a leaf block or we've reached the end of
  537. * the index block, go up
  538. */
  539. if ((i == depth) ||
  540. path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
  541. brelse(path[i].p_bh);
  542. path[i].p_bh = NULL;
  543. i--;
  544. continue;
  545. }
  546. bh = read_extent_tree_block(inode,
  547. ext4_idx_pblock(path[i].p_idx++),
  548. depth - i - 1,
  549. EXT4_EX_FORCE_CACHE);
  550. if (IS_ERR(bh)) {
  551. ret = PTR_ERR(bh);
  552. break;
  553. }
  554. i++;
  555. path[i].p_bh = bh;
  556. path[i].p_hdr = ext_block_hdr(bh);
  557. path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
  558. }
  559. ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
  560. out:
  561. up_read(&ei->i_data_sem);
  562. ext4_ext_drop_refs(path);
  563. kfree(path);
  564. return ret;
  565. }
  566. #ifdef EXT_DEBUG
  567. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  568. {
  569. int k, l = path->p_depth;
  570. ext_debug("path:");
  571. for (k = 0; k <= l; k++, path++) {
  572. if (path->p_idx) {
  573. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  574. ext4_idx_pblock(path->p_idx));
  575. } else if (path->p_ext) {
  576. ext_debug(" %d:[%d]%d:%llu ",
  577. le32_to_cpu(path->p_ext->ee_block),
  578. ext4_ext_is_unwritten(path->p_ext),
  579. ext4_ext_get_actual_len(path->p_ext),
  580. ext4_ext_pblock(path->p_ext));
  581. } else
  582. ext_debug(" []");
  583. }
  584. ext_debug("\n");
  585. }
  586. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  587. {
  588. int depth = ext_depth(inode);
  589. struct ext4_extent_header *eh;
  590. struct ext4_extent *ex;
  591. int i;
  592. if (!path)
  593. return;
  594. eh = path[depth].p_hdr;
  595. ex = EXT_FIRST_EXTENT(eh);
  596. ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
  597. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  598. ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
  599. ext4_ext_is_unwritten(ex),
  600. ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
  601. }
  602. ext_debug("\n");
  603. }
  604. static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
  605. ext4_fsblk_t newblock, int level)
  606. {
  607. int depth = ext_depth(inode);
  608. struct ext4_extent *ex;
  609. if (depth != level) {
  610. struct ext4_extent_idx *idx;
  611. idx = path[level].p_idx;
  612. while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
  613. ext_debug("%d: move %d:%llu in new index %llu\n", level,
  614. le32_to_cpu(idx->ei_block),
  615. ext4_idx_pblock(idx),
  616. newblock);
  617. idx++;
  618. }
  619. return;
  620. }
  621. ex = path[depth].p_ext;
  622. while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
  623. ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
  624. le32_to_cpu(ex->ee_block),
  625. ext4_ext_pblock(ex),
  626. ext4_ext_is_unwritten(ex),
  627. ext4_ext_get_actual_len(ex),
  628. newblock);
  629. ex++;
  630. }
  631. }
  632. #else
  633. #define ext4_ext_show_path(inode, path)
  634. #define ext4_ext_show_leaf(inode, path)
  635. #define ext4_ext_show_move(inode, path, newblock, level)
  636. #endif
  637. void ext4_ext_drop_refs(struct ext4_ext_path *path)
  638. {
  639. int depth, i;
  640. if (!path)
  641. return;
  642. depth = path->p_depth;
  643. for (i = 0; i <= depth; i++, path++)
  644. if (path->p_bh) {
  645. brelse(path->p_bh);
  646. path->p_bh = NULL;
  647. }
  648. }
  649. /*
  650. * ext4_ext_binsearch_idx:
  651. * binary search for the closest index of the given block
  652. * the header must be checked before calling this
  653. */
  654. static void
  655. ext4_ext_binsearch_idx(struct inode *inode,
  656. struct ext4_ext_path *path, ext4_lblk_t block)
  657. {
  658. struct ext4_extent_header *eh = path->p_hdr;
  659. struct ext4_extent_idx *r, *l, *m;
  660. ext_debug("binsearch for %u(idx): ", block);
  661. l = EXT_FIRST_INDEX(eh) + 1;
  662. r = EXT_LAST_INDEX(eh);
  663. while (l <= r) {
  664. m = l + (r - l) / 2;
  665. if (block < le32_to_cpu(m->ei_block))
  666. r = m - 1;
  667. else
  668. l = m + 1;
  669. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
  670. m, le32_to_cpu(m->ei_block),
  671. r, le32_to_cpu(r->ei_block));
  672. }
  673. path->p_idx = l - 1;
  674. ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
  675. ext4_idx_pblock(path->p_idx));
  676. #ifdef CHECK_BINSEARCH
  677. {
  678. struct ext4_extent_idx *chix, *ix;
  679. int k;
  680. chix = ix = EXT_FIRST_INDEX(eh);
  681. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  682. if (k != 0 &&
  683. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  684. printk(KERN_DEBUG "k=%d, ix=0x%p, "
  685. "first=0x%p\n", k,
  686. ix, EXT_FIRST_INDEX(eh));
  687. printk(KERN_DEBUG "%u <= %u\n",
  688. le32_to_cpu(ix->ei_block),
  689. le32_to_cpu(ix[-1].ei_block));
  690. }
  691. BUG_ON(k && le32_to_cpu(ix->ei_block)
  692. <= le32_to_cpu(ix[-1].ei_block));
  693. if (block < le32_to_cpu(ix->ei_block))
  694. break;
  695. chix = ix;
  696. }
  697. BUG_ON(chix != path->p_idx);
  698. }
  699. #endif
  700. }
  701. /*
  702. * ext4_ext_binsearch:
  703. * binary search for closest extent of the given block
  704. * the header must be checked before calling this
  705. */
  706. static void
  707. ext4_ext_binsearch(struct inode *inode,
  708. struct ext4_ext_path *path, ext4_lblk_t block)
  709. {
  710. struct ext4_extent_header *eh = path->p_hdr;
  711. struct ext4_extent *r, *l, *m;
  712. if (eh->eh_entries == 0) {
  713. /*
  714. * this leaf is empty:
  715. * we get such a leaf in split/add case
  716. */
  717. return;
  718. }
  719. ext_debug("binsearch for %u: ", block);
  720. l = EXT_FIRST_EXTENT(eh) + 1;
  721. r = EXT_LAST_EXTENT(eh);
  722. while (l <= r) {
  723. m = l + (r - l) / 2;
  724. if (block < le32_to_cpu(m->ee_block))
  725. r = m - 1;
  726. else
  727. l = m + 1;
  728. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
  729. m, le32_to_cpu(m->ee_block),
  730. r, le32_to_cpu(r->ee_block));
  731. }
  732. path->p_ext = l - 1;
  733. ext_debug(" -> %d:%llu:[%d]%d ",
  734. le32_to_cpu(path->p_ext->ee_block),
  735. ext4_ext_pblock(path->p_ext),
  736. ext4_ext_is_unwritten(path->p_ext),
  737. ext4_ext_get_actual_len(path->p_ext));
  738. #ifdef CHECK_BINSEARCH
  739. {
  740. struct ext4_extent *chex, *ex;
  741. int k;
  742. chex = ex = EXT_FIRST_EXTENT(eh);
  743. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  744. BUG_ON(k && le32_to_cpu(ex->ee_block)
  745. <= le32_to_cpu(ex[-1].ee_block));
  746. if (block < le32_to_cpu(ex->ee_block))
  747. break;
  748. chex = ex;
  749. }
  750. BUG_ON(chex != path->p_ext);
  751. }
  752. #endif
  753. }
  754. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  755. {
  756. struct ext4_extent_header *eh;
  757. eh = ext_inode_hdr(inode);
  758. eh->eh_depth = 0;
  759. eh->eh_entries = 0;
  760. eh->eh_magic = EXT4_EXT_MAGIC;
  761. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
  762. ext4_mark_inode_dirty(handle, inode);
  763. return 0;
  764. }
  765. struct ext4_ext_path *
  766. ext4_find_extent(struct inode *inode, ext4_lblk_t block,
  767. struct ext4_ext_path **orig_path, int flags)
  768. {
  769. struct ext4_extent_header *eh;
  770. struct buffer_head *bh;
  771. struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
  772. short int depth, i, ppos = 0;
  773. int ret;
  774. eh = ext_inode_hdr(inode);
  775. depth = ext_depth(inode);
  776. if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
  777. EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
  778. depth);
  779. ret = -EFSCORRUPTED;
  780. goto err;
  781. }
  782. if (path) {
  783. ext4_ext_drop_refs(path);
  784. if (depth > path[0].p_maxdepth) {
  785. kfree(path);
  786. *orig_path = path = NULL;
  787. }
  788. }
  789. if (!path) {
  790. /* account possible depth increase */
  791. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  792. GFP_NOFS);
  793. if (unlikely(!path))
  794. return ERR_PTR(-ENOMEM);
  795. path[0].p_maxdepth = depth + 1;
  796. }
  797. path[0].p_hdr = eh;
  798. path[0].p_bh = NULL;
  799. i = depth;
  800. /* walk through the tree */
  801. while (i) {
  802. ext_debug("depth %d: num %d, max %d\n",
  803. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  804. ext4_ext_binsearch_idx(inode, path + ppos, block);
  805. path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
  806. path[ppos].p_depth = i;
  807. path[ppos].p_ext = NULL;
  808. bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
  809. flags);
  810. if (IS_ERR(bh)) {
  811. ret = PTR_ERR(bh);
  812. goto err;
  813. }
  814. eh = ext_block_hdr(bh);
  815. ppos++;
  816. path[ppos].p_bh = bh;
  817. path[ppos].p_hdr = eh;
  818. }
  819. path[ppos].p_depth = i;
  820. path[ppos].p_ext = NULL;
  821. path[ppos].p_idx = NULL;
  822. /* find extent */
  823. ext4_ext_binsearch(inode, path + ppos, block);
  824. /* if not an empty leaf */
  825. if (path[ppos].p_ext)
  826. path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
  827. ext4_ext_show_path(inode, path);
  828. return path;
  829. err:
  830. ext4_ext_drop_refs(path);
  831. kfree(path);
  832. if (orig_path)
  833. *orig_path = NULL;
  834. return ERR_PTR(ret);
  835. }
  836. /*
  837. * ext4_ext_insert_index:
  838. * insert new index [@logical;@ptr] into the block at @curp;
  839. * check where to insert: before @curp or after @curp
  840. */
  841. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  842. struct ext4_ext_path *curp,
  843. int logical, ext4_fsblk_t ptr)
  844. {
  845. struct ext4_extent_idx *ix;
  846. int len, err;
  847. err = ext4_ext_get_access(handle, inode, curp);
  848. if (err)
  849. return err;
  850. if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
  851. EXT4_ERROR_INODE(inode,
  852. "logical %d == ei_block %d!",
  853. logical, le32_to_cpu(curp->p_idx->ei_block));
  854. return -EFSCORRUPTED;
  855. }
  856. if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
  857. >= le16_to_cpu(curp->p_hdr->eh_max))) {
  858. EXT4_ERROR_INODE(inode,
  859. "eh_entries %d >= eh_max %d!",
  860. le16_to_cpu(curp->p_hdr->eh_entries),
  861. le16_to_cpu(curp->p_hdr->eh_max));
  862. return -EFSCORRUPTED;
  863. }
  864. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  865. /* insert after */
  866. ext_debug("insert new index %d after: %llu\n", logical, ptr);
  867. ix = curp->p_idx + 1;
  868. } else {
  869. /* insert before */
  870. ext_debug("insert new index %d before: %llu\n", logical, ptr);
  871. ix = curp->p_idx;
  872. }
  873. len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
  874. BUG_ON(len < 0);
  875. if (len > 0) {
  876. ext_debug("insert new index %d: "
  877. "move %d indices from 0x%p to 0x%p\n",
  878. logical, len, ix, ix + 1);
  879. memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
  880. }
  881. if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
  882. EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
  883. return -EFSCORRUPTED;
  884. }
  885. ix->ei_block = cpu_to_le32(logical);
  886. ext4_idx_store_pblock(ix, ptr);
  887. le16_add_cpu(&curp->p_hdr->eh_entries, 1);
  888. if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
  889. EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
  890. return -EFSCORRUPTED;
  891. }
  892. err = ext4_ext_dirty(handle, inode, curp);
  893. ext4_std_error(inode->i_sb, err);
  894. return err;
  895. }
  896. /*
  897. * ext4_ext_split:
  898. * inserts new subtree into the path, using free index entry
  899. * at depth @at:
  900. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  901. * - makes decision where to split
  902. * - moves remaining extents and index entries (right to the split point)
  903. * into the newly allocated blocks
  904. * - initializes subtree
  905. */
  906. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  907. unsigned int flags,
  908. struct ext4_ext_path *path,
  909. struct ext4_extent *newext, int at)
  910. {
  911. struct buffer_head *bh = NULL;
  912. int depth = ext_depth(inode);
  913. struct ext4_extent_header *neh;
  914. struct ext4_extent_idx *fidx;
  915. int i = at, k, m, a;
  916. ext4_fsblk_t newblock, oldblock;
  917. __le32 border;
  918. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  919. int err = 0;
  920. /* make decision: where to split? */
  921. /* FIXME: now decision is simplest: at current extent */
  922. /* if current leaf will be split, then we should use
  923. * border from split point */
  924. if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
  925. EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
  926. return -EFSCORRUPTED;
  927. }
  928. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  929. border = path[depth].p_ext[1].ee_block;
  930. ext_debug("leaf will be split."
  931. " next leaf starts at %d\n",
  932. le32_to_cpu(border));
  933. } else {
  934. border = newext->ee_block;
  935. ext_debug("leaf will be added."
  936. " next leaf starts at %d\n",
  937. le32_to_cpu(border));
  938. }
  939. /*
  940. * If error occurs, then we break processing
  941. * and mark filesystem read-only. index won't
  942. * be inserted and tree will be in consistent
  943. * state. Next mount will repair buffers too.
  944. */
  945. /*
  946. * Get array to track all allocated blocks.
  947. * We need this to handle errors and free blocks
  948. * upon them.
  949. */
  950. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  951. if (!ablocks)
  952. return -ENOMEM;
  953. /* allocate all needed blocks */
  954. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  955. for (a = 0; a < depth - at; a++) {
  956. newblock = ext4_ext_new_meta_block(handle, inode, path,
  957. newext, &err, flags);
  958. if (newblock == 0)
  959. goto cleanup;
  960. ablocks[a] = newblock;
  961. }
  962. /* initialize new leaf */
  963. newblock = ablocks[--a];
  964. if (unlikely(newblock == 0)) {
  965. EXT4_ERROR_INODE(inode, "newblock == 0!");
  966. err = -EFSCORRUPTED;
  967. goto cleanup;
  968. }
  969. bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
  970. if (unlikely(!bh)) {
  971. err = -ENOMEM;
  972. goto cleanup;
  973. }
  974. lock_buffer(bh);
  975. err = ext4_journal_get_create_access(handle, bh);
  976. if (err)
  977. goto cleanup;
  978. neh = ext_block_hdr(bh);
  979. neh->eh_entries = 0;
  980. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  981. neh->eh_magic = EXT4_EXT_MAGIC;
  982. neh->eh_depth = 0;
  983. /* move remainder of path[depth] to the new leaf */
  984. if (unlikely(path[depth].p_hdr->eh_entries !=
  985. path[depth].p_hdr->eh_max)) {
  986. EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
  987. path[depth].p_hdr->eh_entries,
  988. path[depth].p_hdr->eh_max);
  989. err = -EFSCORRUPTED;
  990. goto cleanup;
  991. }
  992. /* start copy from next extent */
  993. m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
  994. ext4_ext_show_move(inode, path, newblock, depth);
  995. if (m) {
  996. struct ext4_extent *ex;
  997. ex = EXT_FIRST_EXTENT(neh);
  998. memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
  999. le16_add_cpu(&neh->eh_entries, m);
  1000. }
  1001. ext4_extent_block_csum_set(inode, neh);
  1002. set_buffer_uptodate(bh);
  1003. unlock_buffer(bh);
  1004. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1005. if (err)
  1006. goto cleanup;
  1007. brelse(bh);
  1008. bh = NULL;
  1009. /* correct old leaf */
  1010. if (m) {
  1011. err = ext4_ext_get_access(handle, inode, path + depth);
  1012. if (err)
  1013. goto cleanup;
  1014. le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
  1015. err = ext4_ext_dirty(handle, inode, path + depth);
  1016. if (err)
  1017. goto cleanup;
  1018. }
  1019. /* create intermediate indexes */
  1020. k = depth - at - 1;
  1021. if (unlikely(k < 0)) {
  1022. EXT4_ERROR_INODE(inode, "k %d < 0!", k);
  1023. err = -EFSCORRUPTED;
  1024. goto cleanup;
  1025. }
  1026. if (k)
  1027. ext_debug("create %d intermediate indices\n", k);
  1028. /* insert new index into current index block */
  1029. /* current depth stored in i var */
  1030. i = depth - 1;
  1031. while (k--) {
  1032. oldblock = newblock;
  1033. newblock = ablocks[--a];
  1034. bh = sb_getblk(inode->i_sb, newblock);
  1035. if (unlikely(!bh)) {
  1036. err = -ENOMEM;
  1037. goto cleanup;
  1038. }
  1039. lock_buffer(bh);
  1040. err = ext4_journal_get_create_access(handle, bh);
  1041. if (err)
  1042. goto cleanup;
  1043. neh = ext_block_hdr(bh);
  1044. neh->eh_entries = cpu_to_le16(1);
  1045. neh->eh_magic = EXT4_EXT_MAGIC;
  1046. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  1047. neh->eh_depth = cpu_to_le16(depth - i);
  1048. fidx = EXT_FIRST_INDEX(neh);
  1049. fidx->ei_block = border;
  1050. ext4_idx_store_pblock(fidx, oldblock);
  1051. ext_debug("int.index at %d (block %llu): %u -> %llu\n",
  1052. i, newblock, le32_to_cpu(border), oldblock);
  1053. /* move remainder of path[i] to the new index block */
  1054. if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
  1055. EXT_LAST_INDEX(path[i].p_hdr))) {
  1056. EXT4_ERROR_INODE(inode,
  1057. "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
  1058. le32_to_cpu(path[i].p_ext->ee_block));
  1059. err = -EFSCORRUPTED;
  1060. goto cleanup;
  1061. }
  1062. /* start copy indexes */
  1063. m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
  1064. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  1065. EXT_MAX_INDEX(path[i].p_hdr));
  1066. ext4_ext_show_move(inode, path, newblock, i);
  1067. if (m) {
  1068. memmove(++fidx, path[i].p_idx,
  1069. sizeof(struct ext4_extent_idx) * m);
  1070. le16_add_cpu(&neh->eh_entries, m);
  1071. }
  1072. ext4_extent_block_csum_set(inode, neh);
  1073. set_buffer_uptodate(bh);
  1074. unlock_buffer(bh);
  1075. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1076. if (err)
  1077. goto cleanup;
  1078. brelse(bh);
  1079. bh = NULL;
  1080. /* correct old index */
  1081. if (m) {
  1082. err = ext4_ext_get_access(handle, inode, path + i);
  1083. if (err)
  1084. goto cleanup;
  1085. le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
  1086. err = ext4_ext_dirty(handle, inode, path + i);
  1087. if (err)
  1088. goto cleanup;
  1089. }
  1090. i--;
  1091. }
  1092. /* insert new index */
  1093. err = ext4_ext_insert_index(handle, inode, path + at,
  1094. le32_to_cpu(border), newblock);
  1095. cleanup:
  1096. if (bh) {
  1097. if (buffer_locked(bh))
  1098. unlock_buffer(bh);
  1099. brelse(bh);
  1100. }
  1101. if (err) {
  1102. /* free all allocated blocks in error case */
  1103. for (i = 0; i < depth; i++) {
  1104. if (!ablocks[i])
  1105. continue;
  1106. ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
  1107. EXT4_FREE_BLOCKS_METADATA);
  1108. }
  1109. }
  1110. kfree(ablocks);
  1111. return err;
  1112. }
  1113. /*
  1114. * ext4_ext_grow_indepth:
  1115. * implements tree growing procedure:
  1116. * - allocates new block
  1117. * - moves top-level data (index block or leaf) into the new block
  1118. * - initializes new top-level, creating index that points to the
  1119. * just created block
  1120. */
  1121. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  1122. unsigned int flags)
  1123. {
  1124. struct ext4_extent_header *neh;
  1125. struct buffer_head *bh;
  1126. ext4_fsblk_t newblock, goal = 0;
  1127. struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  1128. int err = 0;
  1129. /* Try to prepend new index to old one */
  1130. if (ext_depth(inode))
  1131. goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
  1132. if (goal > le32_to_cpu(es->s_first_data_block)) {
  1133. flags |= EXT4_MB_HINT_TRY_GOAL;
  1134. goal--;
  1135. } else
  1136. goal = ext4_inode_to_goal_block(inode);
  1137. newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
  1138. NULL, &err);
  1139. if (newblock == 0)
  1140. return err;
  1141. bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
  1142. if (unlikely(!bh))
  1143. return -ENOMEM;
  1144. lock_buffer(bh);
  1145. err = ext4_journal_get_create_access(handle, bh);
  1146. if (err) {
  1147. unlock_buffer(bh);
  1148. goto out;
  1149. }
  1150. /* move top-level index/leaf into new block */
  1151. memmove(bh->b_data, EXT4_I(inode)->i_data,
  1152. sizeof(EXT4_I(inode)->i_data));
  1153. /* set size of new block */
  1154. neh = ext_block_hdr(bh);
  1155. /* old root could have indexes or leaves
  1156. * so calculate e_max right way */
  1157. if (ext_depth(inode))
  1158. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  1159. else
  1160. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  1161. neh->eh_magic = EXT4_EXT_MAGIC;
  1162. ext4_extent_block_csum_set(inode, neh);
  1163. set_buffer_uptodate(bh);
  1164. unlock_buffer(bh);
  1165. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1166. if (err)
  1167. goto out;
  1168. /* Update top-level index: num,max,pointer */
  1169. neh = ext_inode_hdr(inode);
  1170. neh->eh_entries = cpu_to_le16(1);
  1171. ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
  1172. if (neh->eh_depth == 0) {
  1173. /* Root extent block becomes index block */
  1174. neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
  1175. EXT_FIRST_INDEX(neh)->ei_block =
  1176. EXT_FIRST_EXTENT(neh)->ee_block;
  1177. }
  1178. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  1179. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  1180. le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
  1181. ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
  1182. le16_add_cpu(&neh->eh_depth, 1);
  1183. ext4_mark_inode_dirty(handle, inode);
  1184. out:
  1185. brelse(bh);
  1186. return err;
  1187. }
  1188. /*
  1189. * ext4_ext_create_new_leaf:
  1190. * finds empty index and adds new leaf.
  1191. * if no free index is found, then it requests in-depth growing.
  1192. */
  1193. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  1194. unsigned int mb_flags,
  1195. unsigned int gb_flags,
  1196. struct ext4_ext_path **ppath,
  1197. struct ext4_extent *newext)
  1198. {
  1199. struct ext4_ext_path *path = *ppath;
  1200. struct ext4_ext_path *curp;
  1201. int depth, i, err = 0;
  1202. repeat:
  1203. i = depth = ext_depth(inode);
  1204. /* walk up to the tree and look for free index entry */
  1205. curp = path + depth;
  1206. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  1207. i--;
  1208. curp--;
  1209. }
  1210. /* we use already allocated block for index block,
  1211. * so subsequent data blocks should be contiguous */
  1212. if (EXT_HAS_FREE_INDEX(curp)) {
  1213. /* if we found index with free entry, then use that
  1214. * entry: create all needed subtree and add new leaf */
  1215. err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
  1216. if (err)
  1217. goto out;
  1218. /* refill path */
  1219. path = ext4_find_extent(inode,
  1220. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1221. ppath, gb_flags);
  1222. if (IS_ERR(path))
  1223. err = PTR_ERR(path);
  1224. } else {
  1225. /* tree is full, time to grow in depth */
  1226. err = ext4_ext_grow_indepth(handle, inode, mb_flags);
  1227. if (err)
  1228. goto out;
  1229. /* refill path */
  1230. path = ext4_find_extent(inode,
  1231. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1232. ppath, gb_flags);
  1233. if (IS_ERR(path)) {
  1234. err = PTR_ERR(path);
  1235. goto out;
  1236. }
  1237. /*
  1238. * only first (depth 0 -> 1) produces free space;
  1239. * in all other cases we have to split the grown tree
  1240. */
  1241. depth = ext_depth(inode);
  1242. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  1243. /* now we need to split */
  1244. goto repeat;
  1245. }
  1246. }
  1247. out:
  1248. return err;
  1249. }
  1250. /*
  1251. * search the closest allocated block to the left for *logical
  1252. * and returns it at @logical + it's physical address at @phys
  1253. * if *logical is the smallest allocated block, the function
  1254. * returns 0 at @phys
  1255. * return value contains 0 (success) or error code
  1256. */
  1257. static int ext4_ext_search_left(struct inode *inode,
  1258. struct ext4_ext_path *path,
  1259. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  1260. {
  1261. struct ext4_extent_idx *ix;
  1262. struct ext4_extent *ex;
  1263. int depth, ee_len;
  1264. if (unlikely(path == NULL)) {
  1265. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1266. return -EFSCORRUPTED;
  1267. }
  1268. depth = path->p_depth;
  1269. *phys = 0;
  1270. if (depth == 0 && path->p_ext == NULL)
  1271. return 0;
  1272. /* usually extent in the path covers blocks smaller
  1273. * then *logical, but it can be that extent is the
  1274. * first one in the file */
  1275. ex = path[depth].p_ext;
  1276. ee_len = ext4_ext_get_actual_len(ex);
  1277. if (*logical < le32_to_cpu(ex->ee_block)) {
  1278. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1279. EXT4_ERROR_INODE(inode,
  1280. "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
  1281. *logical, le32_to_cpu(ex->ee_block));
  1282. return -EFSCORRUPTED;
  1283. }
  1284. while (--depth >= 0) {
  1285. ix = path[depth].p_idx;
  1286. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1287. EXT4_ERROR_INODE(inode,
  1288. "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
  1289. ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
  1290. EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
  1291. le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
  1292. depth);
  1293. return -EFSCORRUPTED;
  1294. }
  1295. }
  1296. return 0;
  1297. }
  1298. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1299. EXT4_ERROR_INODE(inode,
  1300. "logical %d < ee_block %d + ee_len %d!",
  1301. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1302. return -EFSCORRUPTED;
  1303. }
  1304. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  1305. *phys = ext4_ext_pblock(ex) + ee_len - 1;
  1306. return 0;
  1307. }
  1308. /*
  1309. * search the closest allocated block to the right for *logical
  1310. * and returns it at @logical + it's physical address at @phys
  1311. * if *logical is the largest allocated block, the function
  1312. * returns 0 at @phys
  1313. * return value contains 0 (success) or error code
  1314. */
  1315. static int ext4_ext_search_right(struct inode *inode,
  1316. struct ext4_ext_path *path,
  1317. ext4_lblk_t *logical, ext4_fsblk_t *phys,
  1318. struct ext4_extent **ret_ex)
  1319. {
  1320. struct buffer_head *bh = NULL;
  1321. struct ext4_extent_header *eh;
  1322. struct ext4_extent_idx *ix;
  1323. struct ext4_extent *ex;
  1324. ext4_fsblk_t block;
  1325. int depth; /* Note, NOT eh_depth; depth from top of tree */
  1326. int ee_len;
  1327. if (unlikely(path == NULL)) {
  1328. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1329. return -EFSCORRUPTED;
  1330. }
  1331. depth = path->p_depth;
  1332. *phys = 0;
  1333. if (depth == 0 && path->p_ext == NULL)
  1334. return 0;
  1335. /* usually extent in the path covers blocks smaller
  1336. * then *logical, but it can be that extent is the
  1337. * first one in the file */
  1338. ex = path[depth].p_ext;
  1339. ee_len = ext4_ext_get_actual_len(ex);
  1340. if (*logical < le32_to_cpu(ex->ee_block)) {
  1341. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1342. EXT4_ERROR_INODE(inode,
  1343. "first_extent(path[%d].p_hdr) != ex",
  1344. depth);
  1345. return -EFSCORRUPTED;
  1346. }
  1347. while (--depth >= 0) {
  1348. ix = path[depth].p_idx;
  1349. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1350. EXT4_ERROR_INODE(inode,
  1351. "ix != EXT_FIRST_INDEX *logical %d!",
  1352. *logical);
  1353. return -EFSCORRUPTED;
  1354. }
  1355. }
  1356. goto found_extent;
  1357. }
  1358. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1359. EXT4_ERROR_INODE(inode,
  1360. "logical %d < ee_block %d + ee_len %d!",
  1361. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1362. return -EFSCORRUPTED;
  1363. }
  1364. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  1365. /* next allocated block in this leaf */
  1366. ex++;
  1367. goto found_extent;
  1368. }
  1369. /* go up and search for index to the right */
  1370. while (--depth >= 0) {
  1371. ix = path[depth].p_idx;
  1372. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  1373. goto got_index;
  1374. }
  1375. /* we've gone up to the root and found no index to the right */
  1376. return 0;
  1377. got_index:
  1378. /* we've found index to the right, let's
  1379. * follow it and find the closest allocated
  1380. * block to the right */
  1381. ix++;
  1382. block = ext4_idx_pblock(ix);
  1383. while (++depth < path->p_depth) {
  1384. /* subtract from p_depth to get proper eh_depth */
  1385. bh = read_extent_tree_block(inode, block,
  1386. path->p_depth - depth, 0);
  1387. if (IS_ERR(bh))
  1388. return PTR_ERR(bh);
  1389. eh = ext_block_hdr(bh);
  1390. ix = EXT_FIRST_INDEX(eh);
  1391. block = ext4_idx_pblock(ix);
  1392. put_bh(bh);
  1393. }
  1394. bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
  1395. if (IS_ERR(bh))
  1396. return PTR_ERR(bh);
  1397. eh = ext_block_hdr(bh);
  1398. ex = EXT_FIRST_EXTENT(eh);
  1399. found_extent:
  1400. *logical = le32_to_cpu(ex->ee_block);
  1401. *phys = ext4_ext_pblock(ex);
  1402. *ret_ex = ex;
  1403. if (bh)
  1404. put_bh(bh);
  1405. return 0;
  1406. }
  1407. /*
  1408. * ext4_ext_next_allocated_block:
  1409. * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
  1410. * NOTE: it considers block number from index entry as
  1411. * allocated block. Thus, index entries have to be consistent
  1412. * with leaves.
  1413. */
  1414. ext4_lblk_t
  1415. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1416. {
  1417. int depth;
  1418. BUG_ON(path == NULL);
  1419. depth = path->p_depth;
  1420. if (depth == 0 && path->p_ext == NULL)
  1421. return EXT_MAX_BLOCKS;
  1422. while (depth >= 0) {
  1423. if (depth == path->p_depth) {
  1424. /* leaf */
  1425. if (path[depth].p_ext &&
  1426. path[depth].p_ext !=
  1427. EXT_LAST_EXTENT(path[depth].p_hdr))
  1428. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1429. } else {
  1430. /* index */
  1431. if (path[depth].p_idx !=
  1432. EXT_LAST_INDEX(path[depth].p_hdr))
  1433. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1434. }
  1435. depth--;
  1436. }
  1437. return EXT_MAX_BLOCKS;
  1438. }
  1439. /*
  1440. * ext4_ext_next_leaf_block:
  1441. * returns first allocated block from next leaf or EXT_MAX_BLOCKS
  1442. */
  1443. static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
  1444. {
  1445. int depth;
  1446. BUG_ON(path == NULL);
  1447. depth = path->p_depth;
  1448. /* zero-tree has no leaf blocks at all */
  1449. if (depth == 0)
  1450. return EXT_MAX_BLOCKS;
  1451. /* go to index block */
  1452. depth--;
  1453. while (depth >= 0) {
  1454. if (path[depth].p_idx !=
  1455. EXT_LAST_INDEX(path[depth].p_hdr))
  1456. return (ext4_lblk_t)
  1457. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1458. depth--;
  1459. }
  1460. return EXT_MAX_BLOCKS;
  1461. }
  1462. /*
  1463. * ext4_ext_correct_indexes:
  1464. * if leaf gets modified and modified extent is first in the leaf,
  1465. * then we have to correct all indexes above.
  1466. * TODO: do we need to correct tree in all cases?
  1467. */
  1468. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1469. struct ext4_ext_path *path)
  1470. {
  1471. struct ext4_extent_header *eh;
  1472. int depth = ext_depth(inode);
  1473. struct ext4_extent *ex;
  1474. __le32 border;
  1475. int k, err = 0;
  1476. eh = path[depth].p_hdr;
  1477. ex = path[depth].p_ext;
  1478. if (unlikely(ex == NULL || eh == NULL)) {
  1479. EXT4_ERROR_INODE(inode,
  1480. "ex %p == NULL or eh %p == NULL", ex, eh);
  1481. return -EFSCORRUPTED;
  1482. }
  1483. if (depth == 0) {
  1484. /* there is no tree at all */
  1485. return 0;
  1486. }
  1487. if (ex != EXT_FIRST_EXTENT(eh)) {
  1488. /* we correct tree if first leaf got modified only */
  1489. return 0;
  1490. }
  1491. /*
  1492. * TODO: we need correction if border is smaller than current one
  1493. */
  1494. k = depth - 1;
  1495. border = path[depth].p_ext->ee_block;
  1496. err = ext4_ext_get_access(handle, inode, path + k);
  1497. if (err)
  1498. return err;
  1499. path[k].p_idx->ei_block = border;
  1500. err = ext4_ext_dirty(handle, inode, path + k);
  1501. if (err)
  1502. return err;
  1503. while (k--) {
  1504. /* change all left-side indexes */
  1505. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1506. break;
  1507. err = ext4_ext_get_access(handle, inode, path + k);
  1508. if (err)
  1509. break;
  1510. path[k].p_idx->ei_block = border;
  1511. err = ext4_ext_dirty(handle, inode, path + k);
  1512. if (err)
  1513. break;
  1514. }
  1515. return err;
  1516. }
  1517. int
  1518. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1519. struct ext4_extent *ex2)
  1520. {
  1521. unsigned short ext1_ee_len, ext2_ee_len;
  1522. if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
  1523. return 0;
  1524. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1525. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1526. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1527. le32_to_cpu(ex2->ee_block))
  1528. return 0;
  1529. /*
  1530. * To allow future support for preallocated extents to be added
  1531. * as an RO_COMPAT feature, refuse to merge to extents if
  1532. * this can result in the top bit of ee_len being set.
  1533. */
  1534. if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
  1535. return 0;
  1536. /*
  1537. * The check for IO to unwritten extent is somewhat racy as we
  1538. * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after
  1539. * dropping i_data_sem. But reserved blocks should save us in that
  1540. * case.
  1541. */
  1542. if (ext4_ext_is_unwritten(ex1) &&
  1543. (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
  1544. atomic_read(&EXT4_I(inode)->i_unwritten) ||
  1545. (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
  1546. return 0;
  1547. #ifdef AGGRESSIVE_TEST
  1548. if (ext1_ee_len >= 4)
  1549. return 0;
  1550. #endif
  1551. if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
  1552. return 1;
  1553. return 0;
  1554. }
  1555. /*
  1556. * This function tries to merge the "ex" extent to the next extent in the tree.
  1557. * It always tries to merge towards right. If you want to merge towards
  1558. * left, pass "ex - 1" as argument instead of "ex".
  1559. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1560. * 1 if they got merged.
  1561. */
  1562. static int ext4_ext_try_to_merge_right(struct inode *inode,
  1563. struct ext4_ext_path *path,
  1564. struct ext4_extent *ex)
  1565. {
  1566. struct ext4_extent_header *eh;
  1567. unsigned int depth, len;
  1568. int merge_done = 0, unwritten;
  1569. depth = ext_depth(inode);
  1570. BUG_ON(path[depth].p_hdr == NULL);
  1571. eh = path[depth].p_hdr;
  1572. while (ex < EXT_LAST_EXTENT(eh)) {
  1573. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1574. break;
  1575. /* merge with next extent! */
  1576. unwritten = ext4_ext_is_unwritten(ex);
  1577. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1578. + ext4_ext_get_actual_len(ex + 1));
  1579. if (unwritten)
  1580. ext4_ext_mark_unwritten(ex);
  1581. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1582. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1583. * sizeof(struct ext4_extent);
  1584. memmove(ex + 1, ex + 2, len);
  1585. }
  1586. le16_add_cpu(&eh->eh_entries, -1);
  1587. merge_done = 1;
  1588. WARN_ON(eh->eh_entries == 0);
  1589. if (!eh->eh_entries)
  1590. EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
  1591. }
  1592. return merge_done;
  1593. }
  1594. /*
  1595. * This function does a very simple check to see if we can collapse
  1596. * an extent tree with a single extent tree leaf block into the inode.
  1597. */
  1598. static void ext4_ext_try_to_merge_up(handle_t *handle,
  1599. struct inode *inode,
  1600. struct ext4_ext_path *path)
  1601. {
  1602. size_t s;
  1603. unsigned max_root = ext4_ext_space_root(inode, 0);
  1604. ext4_fsblk_t blk;
  1605. if ((path[0].p_depth != 1) ||
  1606. (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
  1607. (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
  1608. return;
  1609. /*
  1610. * We need to modify the block allocation bitmap and the block
  1611. * group descriptor to release the extent tree block. If we
  1612. * can't get the journal credits, give up.
  1613. */
  1614. if (ext4_journal_extend(handle, 2))
  1615. return;
  1616. /*
  1617. * Copy the extent data up to the inode
  1618. */
  1619. blk = ext4_idx_pblock(path[0].p_idx);
  1620. s = le16_to_cpu(path[1].p_hdr->eh_entries) *
  1621. sizeof(struct ext4_extent_idx);
  1622. s += sizeof(struct ext4_extent_header);
  1623. path[1].p_maxdepth = path[0].p_maxdepth;
  1624. memcpy(path[0].p_hdr, path[1].p_hdr, s);
  1625. path[0].p_depth = 0;
  1626. path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
  1627. (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
  1628. path[0].p_hdr->eh_max = cpu_to_le16(max_root);
  1629. brelse(path[1].p_bh);
  1630. ext4_free_blocks(handle, inode, NULL, blk, 1,
  1631. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  1632. }
  1633. /*
  1634. * This function tries to merge the @ex extent to neighbours in the tree.
  1635. * return 1 if merge left else 0.
  1636. */
  1637. static void ext4_ext_try_to_merge(handle_t *handle,
  1638. struct inode *inode,
  1639. struct ext4_ext_path *path,
  1640. struct ext4_extent *ex) {
  1641. struct ext4_extent_header *eh;
  1642. unsigned int depth;
  1643. int merge_done = 0;
  1644. depth = ext_depth(inode);
  1645. BUG_ON(path[depth].p_hdr == NULL);
  1646. eh = path[depth].p_hdr;
  1647. if (ex > EXT_FIRST_EXTENT(eh))
  1648. merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
  1649. if (!merge_done)
  1650. (void) ext4_ext_try_to_merge_right(inode, path, ex);
  1651. ext4_ext_try_to_merge_up(handle, inode, path);
  1652. }
  1653. /*
  1654. * check if a portion of the "newext" extent overlaps with an
  1655. * existing extent.
  1656. *
  1657. * If there is an overlap discovered, it updates the length of the newext
  1658. * such that there will be no overlap, and then returns 1.
  1659. * If there is no overlap found, it returns 0.
  1660. */
  1661. static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
  1662. struct inode *inode,
  1663. struct ext4_extent *newext,
  1664. struct ext4_ext_path *path)
  1665. {
  1666. ext4_lblk_t b1, b2;
  1667. unsigned int depth, len1;
  1668. unsigned int ret = 0;
  1669. b1 = le32_to_cpu(newext->ee_block);
  1670. len1 = ext4_ext_get_actual_len(newext);
  1671. depth = ext_depth(inode);
  1672. if (!path[depth].p_ext)
  1673. goto out;
  1674. b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
  1675. /*
  1676. * get the next allocated block if the extent in the path
  1677. * is before the requested block(s)
  1678. */
  1679. if (b2 < b1) {
  1680. b2 = ext4_ext_next_allocated_block(path);
  1681. if (b2 == EXT_MAX_BLOCKS)
  1682. goto out;
  1683. b2 = EXT4_LBLK_CMASK(sbi, b2);
  1684. }
  1685. /* check for wrap through zero on extent logical start block*/
  1686. if (b1 + len1 < b1) {
  1687. len1 = EXT_MAX_BLOCKS - b1;
  1688. newext->ee_len = cpu_to_le16(len1);
  1689. ret = 1;
  1690. }
  1691. /* check for overlap */
  1692. if (b1 + len1 > b2) {
  1693. newext->ee_len = cpu_to_le16(b2 - b1);
  1694. ret = 1;
  1695. }
  1696. out:
  1697. return ret;
  1698. }
  1699. /*
  1700. * ext4_ext_insert_extent:
  1701. * tries to merge requsted extent into the existing extent or
  1702. * inserts requested extent as new one into the tree,
  1703. * creating new leaf in the no-space case.
  1704. */
  1705. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1706. struct ext4_ext_path **ppath,
  1707. struct ext4_extent *newext, int gb_flags)
  1708. {
  1709. struct ext4_ext_path *path = *ppath;
  1710. struct ext4_extent_header *eh;
  1711. struct ext4_extent *ex, *fex;
  1712. struct ext4_extent *nearex; /* nearest extent */
  1713. struct ext4_ext_path *npath = NULL;
  1714. int depth, len, err;
  1715. ext4_lblk_t next;
  1716. int mb_flags = 0, unwritten;
  1717. if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  1718. mb_flags |= EXT4_MB_DELALLOC_RESERVED;
  1719. if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
  1720. EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
  1721. return -EFSCORRUPTED;
  1722. }
  1723. depth = ext_depth(inode);
  1724. ex = path[depth].p_ext;
  1725. eh = path[depth].p_hdr;
  1726. if (unlikely(path[depth].p_hdr == NULL)) {
  1727. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1728. return -EFSCORRUPTED;
  1729. }
  1730. /* try to insert block into found extent and return */
  1731. if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
  1732. /*
  1733. * Try to see whether we should rather test the extent on
  1734. * right from ex, or from the left of ex. This is because
  1735. * ext4_find_extent() can return either extent on the
  1736. * left, or on the right from the searched position. This
  1737. * will make merging more effective.
  1738. */
  1739. if (ex < EXT_LAST_EXTENT(eh) &&
  1740. (le32_to_cpu(ex->ee_block) +
  1741. ext4_ext_get_actual_len(ex) <
  1742. le32_to_cpu(newext->ee_block))) {
  1743. ex += 1;
  1744. goto prepend;
  1745. } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
  1746. (le32_to_cpu(newext->ee_block) +
  1747. ext4_ext_get_actual_len(newext) <
  1748. le32_to_cpu(ex->ee_block)))
  1749. ex -= 1;
  1750. /* Try to append newex to the ex */
  1751. if (ext4_can_extents_be_merged(inode, ex, newext)) {
  1752. ext_debug("append [%d]%d block to %u:[%d]%d"
  1753. "(from %llu)\n",
  1754. ext4_ext_is_unwritten(newext),
  1755. ext4_ext_get_actual_len(newext),
  1756. le32_to_cpu(ex->ee_block),
  1757. ext4_ext_is_unwritten(ex),
  1758. ext4_ext_get_actual_len(ex),
  1759. ext4_ext_pblock(ex));
  1760. err = ext4_ext_get_access(handle, inode,
  1761. path + depth);
  1762. if (err)
  1763. return err;
  1764. unwritten = ext4_ext_is_unwritten(ex);
  1765. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1766. + ext4_ext_get_actual_len(newext));
  1767. if (unwritten)
  1768. ext4_ext_mark_unwritten(ex);
  1769. eh = path[depth].p_hdr;
  1770. nearex = ex;
  1771. goto merge;
  1772. }
  1773. prepend:
  1774. /* Try to prepend newex to the ex */
  1775. if (ext4_can_extents_be_merged(inode, newext, ex)) {
  1776. ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
  1777. "(from %llu)\n",
  1778. le32_to_cpu(newext->ee_block),
  1779. ext4_ext_is_unwritten(newext),
  1780. ext4_ext_get_actual_len(newext),
  1781. le32_to_cpu(ex->ee_block),
  1782. ext4_ext_is_unwritten(ex),
  1783. ext4_ext_get_actual_len(ex),
  1784. ext4_ext_pblock(ex));
  1785. err = ext4_ext_get_access(handle, inode,
  1786. path + depth);
  1787. if (err)
  1788. return err;
  1789. unwritten = ext4_ext_is_unwritten(ex);
  1790. ex->ee_block = newext->ee_block;
  1791. ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
  1792. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1793. + ext4_ext_get_actual_len(newext));
  1794. if (unwritten)
  1795. ext4_ext_mark_unwritten(ex);
  1796. eh = path[depth].p_hdr;
  1797. nearex = ex;
  1798. goto merge;
  1799. }
  1800. }
  1801. depth = ext_depth(inode);
  1802. eh = path[depth].p_hdr;
  1803. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1804. goto has_space;
  1805. /* probably next leaf has space for us? */
  1806. fex = EXT_LAST_EXTENT(eh);
  1807. next = EXT_MAX_BLOCKS;
  1808. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
  1809. next = ext4_ext_next_leaf_block(path);
  1810. if (next != EXT_MAX_BLOCKS) {
  1811. ext_debug("next leaf block - %u\n", next);
  1812. BUG_ON(npath != NULL);
  1813. npath = ext4_find_extent(inode, next, NULL, 0);
  1814. if (IS_ERR(npath))
  1815. return PTR_ERR(npath);
  1816. BUG_ON(npath->p_depth != path->p_depth);
  1817. eh = npath[depth].p_hdr;
  1818. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1819. ext_debug("next leaf isn't full(%d)\n",
  1820. le16_to_cpu(eh->eh_entries));
  1821. path = npath;
  1822. goto has_space;
  1823. }
  1824. ext_debug("next leaf has no free space(%d,%d)\n",
  1825. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1826. }
  1827. /*
  1828. * There is no free space in the found leaf.
  1829. * We're gonna add a new leaf in the tree.
  1830. */
  1831. if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
  1832. mb_flags |= EXT4_MB_USE_RESERVED;
  1833. err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
  1834. ppath, newext);
  1835. if (err)
  1836. goto cleanup;
  1837. depth = ext_depth(inode);
  1838. eh = path[depth].p_hdr;
  1839. has_space:
  1840. nearex = path[depth].p_ext;
  1841. err = ext4_ext_get_access(handle, inode, path + depth);
  1842. if (err)
  1843. goto cleanup;
  1844. if (!nearex) {
  1845. /* there is no extent in this leaf, create first one */
  1846. ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
  1847. le32_to_cpu(newext->ee_block),
  1848. ext4_ext_pblock(newext),
  1849. ext4_ext_is_unwritten(newext),
  1850. ext4_ext_get_actual_len(newext));
  1851. nearex = EXT_FIRST_EXTENT(eh);
  1852. } else {
  1853. if (le32_to_cpu(newext->ee_block)
  1854. > le32_to_cpu(nearex->ee_block)) {
  1855. /* Insert after */
  1856. ext_debug("insert %u:%llu:[%d]%d before: "
  1857. "nearest %p\n",
  1858. le32_to_cpu(newext->ee_block),
  1859. ext4_ext_pblock(newext),
  1860. ext4_ext_is_unwritten(newext),
  1861. ext4_ext_get_actual_len(newext),
  1862. nearex);
  1863. nearex++;
  1864. } else {
  1865. /* Insert before */
  1866. BUG_ON(newext->ee_block == nearex->ee_block);
  1867. ext_debug("insert %u:%llu:[%d]%d after: "
  1868. "nearest %p\n",
  1869. le32_to_cpu(newext->ee_block),
  1870. ext4_ext_pblock(newext),
  1871. ext4_ext_is_unwritten(newext),
  1872. ext4_ext_get_actual_len(newext),
  1873. nearex);
  1874. }
  1875. len = EXT_LAST_EXTENT(eh) - nearex + 1;
  1876. if (len > 0) {
  1877. ext_debug("insert %u:%llu:[%d]%d: "
  1878. "move %d extents from 0x%p to 0x%p\n",
  1879. le32_to_cpu(newext->ee_block),
  1880. ext4_ext_pblock(newext),
  1881. ext4_ext_is_unwritten(newext),
  1882. ext4_ext_get_actual_len(newext),
  1883. len, nearex, nearex + 1);
  1884. memmove(nearex + 1, nearex,
  1885. len * sizeof(struct ext4_extent));
  1886. }
  1887. }
  1888. le16_add_cpu(&eh->eh_entries, 1);
  1889. path[depth].p_ext = nearex;
  1890. nearex->ee_block = newext->ee_block;
  1891. ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
  1892. nearex->ee_len = newext->ee_len;
  1893. merge:
  1894. /* try to merge extents */
  1895. if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
  1896. ext4_ext_try_to_merge(handle, inode, path, nearex);
  1897. /* time to correct all indexes above */
  1898. err = ext4_ext_correct_indexes(handle, inode, path);
  1899. if (err)
  1900. goto cleanup;
  1901. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  1902. cleanup:
  1903. ext4_ext_drop_refs(npath);
  1904. kfree(npath);
  1905. return err;
  1906. }
  1907. static int ext4_fill_fiemap_extents(struct inode *inode,
  1908. ext4_lblk_t block, ext4_lblk_t num,
  1909. struct fiemap_extent_info *fieinfo)
  1910. {
  1911. struct ext4_ext_path *path = NULL;
  1912. struct ext4_extent *ex;
  1913. struct extent_status es;
  1914. ext4_lblk_t next, next_del, start = 0, end = 0;
  1915. ext4_lblk_t last = block + num;
  1916. int exists, depth = 0, err = 0;
  1917. unsigned int flags = 0;
  1918. unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
  1919. while (block < last && block != EXT_MAX_BLOCKS) {
  1920. num = last - block;
  1921. /* find extent for this block */
  1922. down_read(&EXT4_I(inode)->i_data_sem);
  1923. path = ext4_find_extent(inode, block, &path, 0);
  1924. if (IS_ERR(path)) {
  1925. up_read(&EXT4_I(inode)->i_data_sem);
  1926. err = PTR_ERR(path);
  1927. path = NULL;
  1928. break;
  1929. }
  1930. depth = ext_depth(inode);
  1931. if (unlikely(path[depth].p_hdr == NULL)) {
  1932. up_read(&EXT4_I(inode)->i_data_sem);
  1933. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1934. err = -EFSCORRUPTED;
  1935. break;
  1936. }
  1937. ex = path[depth].p_ext;
  1938. next = ext4_ext_next_allocated_block(path);
  1939. flags = 0;
  1940. exists = 0;
  1941. if (!ex) {
  1942. /* there is no extent yet, so try to allocate
  1943. * all requested space */
  1944. start = block;
  1945. end = block + num;
  1946. } else if (le32_to_cpu(ex->ee_block) > block) {
  1947. /* need to allocate space before found extent */
  1948. start = block;
  1949. end = le32_to_cpu(ex->ee_block);
  1950. if (block + num < end)
  1951. end = block + num;
  1952. } else if (block >= le32_to_cpu(ex->ee_block)
  1953. + ext4_ext_get_actual_len(ex)) {
  1954. /* need to allocate space after found extent */
  1955. start = block;
  1956. end = block + num;
  1957. if (end >= next)
  1958. end = next;
  1959. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1960. /*
  1961. * some part of requested space is covered
  1962. * by found extent
  1963. */
  1964. start = block;
  1965. end = le32_to_cpu(ex->ee_block)
  1966. + ext4_ext_get_actual_len(ex);
  1967. if (block + num < end)
  1968. end = block + num;
  1969. exists = 1;
  1970. } else {
  1971. BUG();
  1972. }
  1973. BUG_ON(end <= start);
  1974. if (!exists) {
  1975. es.es_lblk = start;
  1976. es.es_len = end - start;
  1977. es.es_pblk = 0;
  1978. } else {
  1979. es.es_lblk = le32_to_cpu(ex->ee_block);
  1980. es.es_len = ext4_ext_get_actual_len(ex);
  1981. es.es_pblk = ext4_ext_pblock(ex);
  1982. if (ext4_ext_is_unwritten(ex))
  1983. flags |= FIEMAP_EXTENT_UNWRITTEN;
  1984. }
  1985. /*
  1986. * Find delayed extent and update es accordingly. We call
  1987. * it even in !exists case to find out whether es is the
  1988. * last existing extent or not.
  1989. */
  1990. next_del = ext4_find_delayed_extent(inode, &es);
  1991. if (!exists && next_del) {
  1992. exists = 1;
  1993. flags |= (FIEMAP_EXTENT_DELALLOC |
  1994. FIEMAP_EXTENT_UNKNOWN);
  1995. }
  1996. up_read(&EXT4_I(inode)->i_data_sem);
  1997. if (unlikely(es.es_len == 0)) {
  1998. EXT4_ERROR_INODE(inode, "es.es_len == 0");
  1999. err = -EFSCORRUPTED;
  2000. break;
  2001. }
  2002. /*
  2003. * This is possible iff next == next_del == EXT_MAX_BLOCKS.
  2004. * we need to check next == EXT_MAX_BLOCKS because it is
  2005. * possible that an extent is with unwritten and delayed
  2006. * status due to when an extent is delayed allocated and
  2007. * is allocated by fallocate status tree will track both of
  2008. * them in a extent.
  2009. *
  2010. * So we could return a unwritten and delayed extent, and
  2011. * its block is equal to 'next'.
  2012. */
  2013. if (next == next_del && next == EXT_MAX_BLOCKS) {
  2014. flags |= FIEMAP_EXTENT_LAST;
  2015. if (unlikely(next_del != EXT_MAX_BLOCKS ||
  2016. next != EXT_MAX_BLOCKS)) {
  2017. EXT4_ERROR_INODE(inode,
  2018. "next extent == %u, next "
  2019. "delalloc extent = %u",
  2020. next, next_del);
  2021. err = -EFSCORRUPTED;
  2022. break;
  2023. }
  2024. }
  2025. if (exists) {
  2026. err = fiemap_fill_next_extent(fieinfo,
  2027. (__u64)es.es_lblk << blksize_bits,
  2028. (__u64)es.es_pblk << blksize_bits,
  2029. (__u64)es.es_len << blksize_bits,
  2030. flags);
  2031. if (err < 0)
  2032. break;
  2033. if (err == 1) {
  2034. err = 0;
  2035. break;
  2036. }
  2037. }
  2038. block = es.es_lblk + es.es_len;
  2039. }
  2040. ext4_ext_drop_refs(path);
  2041. kfree(path);
  2042. return err;
  2043. }
  2044. /*
  2045. * ext4_ext_determine_hole - determine hole around given block
  2046. * @inode: inode we lookup in
  2047. * @path: path in extent tree to @lblk
  2048. * @lblk: pointer to logical block around which we want to determine hole
  2049. *
  2050. * Determine hole length (and start if easily possible) around given logical
  2051. * block. We don't try too hard to find the beginning of the hole but @path
  2052. * actually points to extent before @lblk, we provide it.
  2053. *
  2054. * The function returns the length of a hole starting at @lblk. We update @lblk
  2055. * to the beginning of the hole if we managed to find it.
  2056. */
  2057. static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
  2058. struct ext4_ext_path *path,
  2059. ext4_lblk_t *lblk)
  2060. {
  2061. int depth = ext_depth(inode);
  2062. struct ext4_extent *ex;
  2063. ext4_lblk_t len;
  2064. ex = path[depth].p_ext;
  2065. if (ex == NULL) {
  2066. /* there is no extent yet, so gap is [0;-] */
  2067. *lblk = 0;
  2068. len = EXT_MAX_BLOCKS;
  2069. } else if (*lblk < le32_to_cpu(ex->ee_block)) {
  2070. len = le32_to_cpu(ex->ee_block) - *lblk;
  2071. } else if (*lblk >= le32_to_cpu(ex->ee_block)
  2072. + ext4_ext_get_actual_len(ex)) {
  2073. ext4_lblk_t next;
  2074. *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
  2075. next = ext4_ext_next_allocated_block(path);
  2076. BUG_ON(next == *lblk);
  2077. len = next - *lblk;
  2078. } else {
  2079. BUG();
  2080. }
  2081. return len;
  2082. }
  2083. /*
  2084. * ext4_ext_put_gap_in_cache:
  2085. * calculate boundaries of the gap that the requested block fits into
  2086. * and cache this gap
  2087. */
  2088. static void
  2089. ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
  2090. ext4_lblk_t hole_len)
  2091. {
  2092. struct extent_status es;
  2093. ext4_es_find_delayed_extent_range(inode, hole_start,
  2094. hole_start + hole_len - 1, &es);
  2095. if (es.es_len) {
  2096. /* There's delayed extent containing lblock? */
  2097. if (es.es_lblk <= hole_start)
  2098. return;
  2099. hole_len = min(es.es_lblk - hole_start, hole_len);
  2100. }
  2101. ext_debug(" -> %u:%u\n", hole_start, hole_len);
  2102. ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
  2103. EXTENT_STATUS_HOLE);
  2104. }
  2105. /*
  2106. * ext4_ext_rm_idx:
  2107. * removes index from the index block.
  2108. */
  2109. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  2110. struct ext4_ext_path *path, int depth)
  2111. {
  2112. int err;
  2113. ext4_fsblk_t leaf;
  2114. /* free index block */
  2115. depth--;
  2116. path = path + depth;
  2117. leaf = ext4_idx_pblock(path->p_idx);
  2118. if (unlikely(path->p_hdr->eh_entries == 0)) {
  2119. EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
  2120. return -EFSCORRUPTED;
  2121. }
  2122. err = ext4_ext_get_access(handle, inode, path);
  2123. if (err)
  2124. return err;
  2125. if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
  2126. int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
  2127. len *= sizeof(struct ext4_extent_idx);
  2128. memmove(path->p_idx, path->p_idx + 1, len);
  2129. }
  2130. le16_add_cpu(&path->p_hdr->eh_entries, -1);
  2131. err = ext4_ext_dirty(handle, inode, path);
  2132. if (err)
  2133. return err;
  2134. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  2135. trace_ext4_ext_rm_idx(inode, leaf);
  2136. ext4_free_blocks(handle, inode, NULL, leaf, 1,
  2137. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  2138. while (--depth >= 0) {
  2139. if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
  2140. break;
  2141. path--;
  2142. err = ext4_ext_get_access(handle, inode, path);
  2143. if (err)
  2144. break;
  2145. path->p_idx->ei_block = (path+1)->p_idx->ei_block;
  2146. err = ext4_ext_dirty(handle, inode, path);
  2147. if (err)
  2148. break;
  2149. }
  2150. return err;
  2151. }
  2152. /*
  2153. * ext4_ext_calc_credits_for_single_extent:
  2154. * This routine returns max. credits that needed to insert an extent
  2155. * to the extent tree.
  2156. * When pass the actual path, the caller should calculate credits
  2157. * under i_data_sem.
  2158. */
  2159. int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
  2160. struct ext4_ext_path *path)
  2161. {
  2162. if (path) {
  2163. int depth = ext_depth(inode);
  2164. int ret = 0;
  2165. /* probably there is space in leaf? */
  2166. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  2167. < le16_to_cpu(path[depth].p_hdr->eh_max)) {
  2168. /*
  2169. * There are some space in the leaf tree, no
  2170. * need to account for leaf block credit
  2171. *
  2172. * bitmaps and block group descriptor blocks
  2173. * and other metadata blocks still need to be
  2174. * accounted.
  2175. */
  2176. /* 1 bitmap, 1 block group descriptor */
  2177. ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
  2178. return ret;
  2179. }
  2180. }
  2181. return ext4_chunk_trans_blocks(inode, nrblocks);
  2182. }
  2183. /*
  2184. * How many index/leaf blocks need to change/allocate to add @extents extents?
  2185. *
  2186. * If we add a single extent, then in the worse case, each tree level
  2187. * index/leaf need to be changed in case of the tree split.
  2188. *
  2189. * If more extents are inserted, they could cause the whole tree split more
  2190. * than once, but this is really rare.
  2191. */
  2192. int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
  2193. {
  2194. int index;
  2195. int depth;
  2196. /* If we are converting the inline data, only one is needed here. */
  2197. if (ext4_has_inline_data(inode))
  2198. return 1;
  2199. depth = ext_depth(inode);
  2200. if (extents <= 1)
  2201. index = depth * 2;
  2202. else
  2203. index = depth * 3;
  2204. return index;
  2205. }
  2206. static inline int get_default_free_blocks_flags(struct inode *inode)
  2207. {
  2208. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  2209. return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
  2210. else if (ext4_should_journal_data(inode))
  2211. return EXT4_FREE_BLOCKS_FORGET;
  2212. return 0;
  2213. }
  2214. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  2215. struct ext4_extent *ex,
  2216. long long *partial_cluster,
  2217. ext4_lblk_t from, ext4_lblk_t to)
  2218. {
  2219. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2220. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  2221. ext4_fsblk_t pblk;
  2222. int flags = get_default_free_blocks_flags(inode);
  2223. /*
  2224. * For bigalloc file systems, we never free a partial cluster
  2225. * at the beginning of the extent. Instead, we make a note
  2226. * that we tried freeing the cluster, and check to see if we
  2227. * need to free it on a subsequent call to ext4_remove_blocks,
  2228. * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
  2229. */
  2230. flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
  2231. trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
  2232. /*
  2233. * If we have a partial cluster, and it's different from the
  2234. * cluster of the last block, we need to explicitly free the
  2235. * partial cluster here.
  2236. */
  2237. pblk = ext4_ext_pblock(ex) + ee_len - 1;
  2238. if (*partial_cluster > 0 &&
  2239. *partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
  2240. ext4_free_blocks(handle, inode, NULL,
  2241. EXT4_C2B(sbi, *partial_cluster),
  2242. sbi->s_cluster_ratio, flags);
  2243. *partial_cluster = 0;
  2244. }
  2245. #ifdef EXTENTS_STATS
  2246. {
  2247. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2248. spin_lock(&sbi->s_ext_stats_lock);
  2249. sbi->s_ext_blocks += ee_len;
  2250. sbi->s_ext_extents++;
  2251. if (ee_len < sbi->s_ext_min)
  2252. sbi->s_ext_min = ee_len;
  2253. if (ee_len > sbi->s_ext_max)
  2254. sbi->s_ext_max = ee_len;
  2255. if (ext_depth(inode) > sbi->s_depth_max)
  2256. sbi->s_depth_max = ext_depth(inode);
  2257. spin_unlock(&sbi->s_ext_stats_lock);
  2258. }
  2259. #endif
  2260. if (from >= le32_to_cpu(ex->ee_block)
  2261. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  2262. /* tail removal */
  2263. ext4_lblk_t num;
  2264. long long first_cluster;
  2265. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  2266. pblk = ext4_ext_pblock(ex) + ee_len - num;
  2267. /*
  2268. * Usually we want to free partial cluster at the end of the
  2269. * extent, except for the situation when the cluster is still
  2270. * used by any other extent (partial_cluster is negative).
  2271. */
  2272. if (*partial_cluster < 0 &&
  2273. *partial_cluster == -(long long) EXT4_B2C(sbi, pblk+num-1))
  2274. flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
  2275. ext_debug("free last %u blocks starting %llu partial %lld\n",
  2276. num, pblk, *partial_cluster);
  2277. ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
  2278. /*
  2279. * If the block range to be freed didn't start at the
  2280. * beginning of a cluster, and we removed the entire
  2281. * extent and the cluster is not used by any other extent,
  2282. * save the partial cluster here, since we might need to
  2283. * delete if we determine that the truncate or punch hole
  2284. * operation has removed all of the blocks in the cluster.
  2285. * If that cluster is used by another extent, preserve its
  2286. * negative value so it isn't freed later on.
  2287. *
  2288. * If the whole extent wasn't freed, we've reached the
  2289. * start of the truncated/punched region and have finished
  2290. * removing blocks. If there's a partial cluster here it's
  2291. * shared with the remainder of the extent and is no longer
  2292. * a candidate for removal.
  2293. */
  2294. if (EXT4_PBLK_COFF(sbi, pblk) && ee_len == num) {
  2295. first_cluster = (long long) EXT4_B2C(sbi, pblk);
  2296. if (first_cluster != -*partial_cluster)
  2297. *partial_cluster = first_cluster;
  2298. } else {
  2299. *partial_cluster = 0;
  2300. }
  2301. } else
  2302. ext4_error(sbi->s_sb, "strange request: removal(2) "
  2303. "%u-%u from %u:%u",
  2304. from, to, le32_to_cpu(ex->ee_block), ee_len);
  2305. return 0;
  2306. }
  2307. /*
  2308. * ext4_ext_rm_leaf() Removes the extents associated with the
  2309. * blocks appearing between "start" and "end". Both "start"
  2310. * and "end" must appear in the same extent or EIO is returned.
  2311. *
  2312. * @handle: The journal handle
  2313. * @inode: The files inode
  2314. * @path: The path to the leaf
  2315. * @partial_cluster: The cluster which we'll have to free if all extents
  2316. * has been released from it. However, if this value is
  2317. * negative, it's a cluster just to the right of the
  2318. * punched region and it must not be freed.
  2319. * @start: The first block to remove
  2320. * @end: The last block to remove
  2321. */
  2322. static int
  2323. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  2324. struct ext4_ext_path *path,
  2325. long long *partial_cluster,
  2326. ext4_lblk_t start, ext4_lblk_t end)
  2327. {
  2328. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2329. int err = 0, correct_index = 0;
  2330. int depth = ext_depth(inode), credits;
  2331. struct ext4_extent_header *eh;
  2332. ext4_lblk_t a, b;
  2333. unsigned num;
  2334. ext4_lblk_t ex_ee_block;
  2335. unsigned short ex_ee_len;
  2336. unsigned unwritten = 0;
  2337. struct ext4_extent *ex;
  2338. ext4_fsblk_t pblk;
  2339. /* the header must be checked already in ext4_ext_remove_space() */
  2340. ext_debug("truncate since %u in leaf to %u\n", start, end);
  2341. if (!path[depth].p_hdr)
  2342. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  2343. eh = path[depth].p_hdr;
  2344. if (unlikely(path[depth].p_hdr == NULL)) {
  2345. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  2346. return -EFSCORRUPTED;
  2347. }
  2348. /* find where to start removing */
  2349. ex = path[depth].p_ext;
  2350. if (!ex)
  2351. ex = EXT_LAST_EXTENT(eh);
  2352. ex_ee_block = le32_to_cpu(ex->ee_block);
  2353. ex_ee_len = ext4_ext_get_actual_len(ex);
  2354. trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
  2355. while (ex >= EXT_FIRST_EXTENT(eh) &&
  2356. ex_ee_block + ex_ee_len > start) {
  2357. if (ext4_ext_is_unwritten(ex))
  2358. unwritten = 1;
  2359. else
  2360. unwritten = 0;
  2361. ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
  2362. unwritten, ex_ee_len);
  2363. path[depth].p_ext = ex;
  2364. a = ex_ee_block > start ? ex_ee_block : start;
  2365. b = ex_ee_block+ex_ee_len - 1 < end ?
  2366. ex_ee_block+ex_ee_len - 1 : end;
  2367. ext_debug(" border %u:%u\n", a, b);
  2368. /* If this extent is beyond the end of the hole, skip it */
  2369. if (end < ex_ee_block) {
  2370. /*
  2371. * We're going to skip this extent and move to another,
  2372. * so note that its first cluster is in use to avoid
  2373. * freeing it when removing blocks. Eventually, the
  2374. * right edge of the truncated/punched region will
  2375. * be just to the left.
  2376. */
  2377. if (sbi->s_cluster_ratio > 1) {
  2378. pblk = ext4_ext_pblock(ex);
  2379. *partial_cluster =
  2380. -(long long) EXT4_B2C(sbi, pblk);
  2381. }
  2382. ex--;
  2383. ex_ee_block = le32_to_cpu(ex->ee_block);
  2384. ex_ee_len = ext4_ext_get_actual_len(ex);
  2385. continue;
  2386. } else if (b != ex_ee_block + ex_ee_len - 1) {
  2387. EXT4_ERROR_INODE(inode,
  2388. "can not handle truncate %u:%u "
  2389. "on extent %u:%u",
  2390. start, end, ex_ee_block,
  2391. ex_ee_block + ex_ee_len - 1);
  2392. err = -EFSCORRUPTED;
  2393. goto out;
  2394. } else if (a != ex_ee_block) {
  2395. /* remove tail of the extent */
  2396. num = a - ex_ee_block;
  2397. } else {
  2398. /* remove whole extent: excellent! */
  2399. num = 0;
  2400. }
  2401. /*
  2402. * 3 for leaf, sb, and inode plus 2 (bmap and group
  2403. * descriptor) for each block group; assume two block
  2404. * groups plus ex_ee_len/blocks_per_block_group for
  2405. * the worst case
  2406. */
  2407. credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
  2408. if (ex == EXT_FIRST_EXTENT(eh)) {
  2409. correct_index = 1;
  2410. credits += (ext_depth(inode)) + 1;
  2411. }
  2412. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  2413. err = ext4_ext_truncate_extend_restart(handle, inode, credits);
  2414. if (err)
  2415. goto out;
  2416. err = ext4_ext_get_access(handle, inode, path + depth);
  2417. if (err)
  2418. goto out;
  2419. err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
  2420. a, b);
  2421. if (err)
  2422. goto out;
  2423. if (num == 0)
  2424. /* this extent is removed; mark slot entirely unused */
  2425. ext4_ext_store_pblock(ex, 0);
  2426. ex->ee_len = cpu_to_le16(num);
  2427. /*
  2428. * Do not mark unwritten if all the blocks in the
  2429. * extent have been removed.
  2430. */
  2431. if (unwritten && num)
  2432. ext4_ext_mark_unwritten(ex);
  2433. /*
  2434. * If the extent was completely released,
  2435. * we need to remove it from the leaf
  2436. */
  2437. if (num == 0) {
  2438. if (end != EXT_MAX_BLOCKS - 1) {
  2439. /*
  2440. * For hole punching, we need to scoot all the
  2441. * extents up when an extent is removed so that
  2442. * we dont have blank extents in the middle
  2443. */
  2444. memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
  2445. sizeof(struct ext4_extent));
  2446. /* Now get rid of the one at the end */
  2447. memset(EXT_LAST_EXTENT(eh), 0,
  2448. sizeof(struct ext4_extent));
  2449. }
  2450. le16_add_cpu(&eh->eh_entries, -1);
  2451. }
  2452. err = ext4_ext_dirty(handle, inode, path + depth);
  2453. if (err)
  2454. goto out;
  2455. ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
  2456. ext4_ext_pblock(ex));
  2457. ex--;
  2458. ex_ee_block = le32_to_cpu(ex->ee_block);
  2459. ex_ee_len = ext4_ext_get_actual_len(ex);
  2460. }
  2461. if (correct_index && eh->eh_entries)
  2462. err = ext4_ext_correct_indexes(handle, inode, path);
  2463. /*
  2464. * If there's a partial cluster and at least one extent remains in
  2465. * the leaf, free the partial cluster if it isn't shared with the
  2466. * current extent. If it is shared with the current extent
  2467. * we zero partial_cluster because we've reached the start of the
  2468. * truncated/punched region and we're done removing blocks.
  2469. */
  2470. if (*partial_cluster > 0 && ex >= EXT_FIRST_EXTENT(eh)) {
  2471. pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
  2472. if (*partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
  2473. ext4_free_blocks(handle, inode, NULL,
  2474. EXT4_C2B(sbi, *partial_cluster),
  2475. sbi->s_cluster_ratio,
  2476. get_default_free_blocks_flags(inode));
  2477. }
  2478. *partial_cluster = 0;
  2479. }
  2480. /* if this leaf is free, then we should
  2481. * remove it from index block above */
  2482. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  2483. err = ext4_ext_rm_idx(handle, inode, path, depth);
  2484. out:
  2485. return err;
  2486. }
  2487. /*
  2488. * ext4_ext_more_to_rm:
  2489. * returns 1 if current index has to be freed (even partial)
  2490. */
  2491. static int
  2492. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  2493. {
  2494. BUG_ON(path->p_idx == NULL);
  2495. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  2496. return 0;
  2497. /*
  2498. * if truncate on deeper level happened, it wasn't partial,
  2499. * so we have to consider current index for truncation
  2500. */
  2501. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  2502. return 0;
  2503. return 1;
  2504. }
  2505. int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
  2506. ext4_lblk_t end)
  2507. {
  2508. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2509. int depth = ext_depth(inode);
  2510. struct ext4_ext_path *path = NULL;
  2511. long long partial_cluster = 0;
  2512. handle_t *handle;
  2513. int i = 0, err = 0;
  2514. ext_debug("truncate since %u to %u\n", start, end);
  2515. /* probably first extent we're gonna free will be last in block */
  2516. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
  2517. if (IS_ERR(handle))
  2518. return PTR_ERR(handle);
  2519. again:
  2520. trace_ext4_ext_remove_space(inode, start, end, depth);
  2521. /*
  2522. * Check if we are removing extents inside the extent tree. If that
  2523. * is the case, we are going to punch a hole inside the extent tree
  2524. * so we have to check whether we need to split the extent covering
  2525. * the last block to remove so we can easily remove the part of it
  2526. * in ext4_ext_rm_leaf().
  2527. */
  2528. if (end < EXT_MAX_BLOCKS - 1) {
  2529. struct ext4_extent *ex;
  2530. ext4_lblk_t ee_block, ex_end, lblk;
  2531. ext4_fsblk_t pblk;
  2532. /* find extent for or closest extent to this block */
  2533. path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
  2534. if (IS_ERR(path)) {
  2535. ext4_journal_stop(handle);
  2536. return PTR_ERR(path);
  2537. }
  2538. depth = ext_depth(inode);
  2539. /* Leaf not may not exist only if inode has no blocks at all */
  2540. ex = path[depth].p_ext;
  2541. if (!ex) {
  2542. if (depth) {
  2543. EXT4_ERROR_INODE(inode,
  2544. "path[%d].p_hdr == NULL",
  2545. depth);
  2546. err = -EFSCORRUPTED;
  2547. }
  2548. goto out;
  2549. }
  2550. ee_block = le32_to_cpu(ex->ee_block);
  2551. ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
  2552. /*
  2553. * See if the last block is inside the extent, if so split
  2554. * the extent at 'end' block so we can easily remove the
  2555. * tail of the first part of the split extent in
  2556. * ext4_ext_rm_leaf().
  2557. */
  2558. if (end >= ee_block && end < ex_end) {
  2559. /*
  2560. * If we're going to split the extent, note that
  2561. * the cluster containing the block after 'end' is
  2562. * in use to avoid freeing it when removing blocks.
  2563. */
  2564. if (sbi->s_cluster_ratio > 1) {
  2565. pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
  2566. partial_cluster =
  2567. -(long long) EXT4_B2C(sbi, pblk);
  2568. }
  2569. /*
  2570. * Split the extent in two so that 'end' is the last
  2571. * block in the first new extent. Also we should not
  2572. * fail removing space due to ENOSPC so try to use
  2573. * reserved block if that happens.
  2574. */
  2575. err = ext4_force_split_extent_at(handle, inode, &path,
  2576. end + 1, 1);
  2577. if (err < 0)
  2578. goto out;
  2579. } else if (sbi->s_cluster_ratio > 1 && end >= ex_end) {
  2580. /*
  2581. * If there's an extent to the right its first cluster
  2582. * contains the immediate right boundary of the
  2583. * truncated/punched region. Set partial_cluster to
  2584. * its negative value so it won't be freed if shared
  2585. * with the current extent. The end < ee_block case
  2586. * is handled in ext4_ext_rm_leaf().
  2587. */
  2588. lblk = ex_end + 1;
  2589. err = ext4_ext_search_right(inode, path, &lblk, &pblk,
  2590. &ex);
  2591. if (err)
  2592. goto out;
  2593. if (pblk)
  2594. partial_cluster =
  2595. -(long long) EXT4_B2C(sbi, pblk);
  2596. }
  2597. }
  2598. /*
  2599. * We start scanning from right side, freeing all the blocks
  2600. * after i_size and walking into the tree depth-wise.
  2601. */
  2602. depth = ext_depth(inode);
  2603. if (path) {
  2604. int k = i = depth;
  2605. while (--k > 0)
  2606. path[k].p_block =
  2607. le16_to_cpu(path[k].p_hdr->eh_entries)+1;
  2608. } else {
  2609. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
  2610. GFP_NOFS);
  2611. if (path == NULL) {
  2612. ext4_journal_stop(handle);
  2613. return -ENOMEM;
  2614. }
  2615. path[0].p_maxdepth = path[0].p_depth = depth;
  2616. path[0].p_hdr = ext_inode_hdr(inode);
  2617. i = 0;
  2618. if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
  2619. err = -EFSCORRUPTED;
  2620. goto out;
  2621. }
  2622. }
  2623. err = 0;
  2624. while (i >= 0 && err == 0) {
  2625. if (i == depth) {
  2626. /* this is leaf block */
  2627. err = ext4_ext_rm_leaf(handle, inode, path,
  2628. &partial_cluster, start,
  2629. end);
  2630. /* root level has p_bh == NULL, brelse() eats this */
  2631. brelse(path[i].p_bh);
  2632. path[i].p_bh = NULL;
  2633. i--;
  2634. continue;
  2635. }
  2636. /* this is index block */
  2637. if (!path[i].p_hdr) {
  2638. ext_debug("initialize header\n");
  2639. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  2640. }
  2641. if (!path[i].p_idx) {
  2642. /* this level hasn't been touched yet */
  2643. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  2644. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  2645. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  2646. path[i].p_hdr,
  2647. le16_to_cpu(path[i].p_hdr->eh_entries));
  2648. } else {
  2649. /* we were already here, see at next index */
  2650. path[i].p_idx--;
  2651. }
  2652. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  2653. i, EXT_FIRST_INDEX(path[i].p_hdr),
  2654. path[i].p_idx);
  2655. if (ext4_ext_more_to_rm(path + i)) {
  2656. struct buffer_head *bh;
  2657. /* go to the next level */
  2658. ext_debug("move to level %d (block %llu)\n",
  2659. i + 1, ext4_idx_pblock(path[i].p_idx));
  2660. memset(path + i + 1, 0, sizeof(*path));
  2661. bh = read_extent_tree_block(inode,
  2662. ext4_idx_pblock(path[i].p_idx), depth - i - 1,
  2663. EXT4_EX_NOCACHE);
  2664. if (IS_ERR(bh)) {
  2665. /* should we reset i_size? */
  2666. err = PTR_ERR(bh);
  2667. break;
  2668. }
  2669. /* Yield here to deal with large extent trees.
  2670. * Should be a no-op if we did IO above. */
  2671. cond_resched();
  2672. if (WARN_ON(i + 1 > depth)) {
  2673. err = -EFSCORRUPTED;
  2674. break;
  2675. }
  2676. path[i + 1].p_bh = bh;
  2677. /* save actual number of indexes since this
  2678. * number is changed at the next iteration */
  2679. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  2680. i++;
  2681. } else {
  2682. /* we finished processing this index, go up */
  2683. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  2684. /* index is empty, remove it;
  2685. * handle must be already prepared by the
  2686. * truncatei_leaf() */
  2687. err = ext4_ext_rm_idx(handle, inode, path, i);
  2688. }
  2689. /* root level has p_bh == NULL, brelse() eats this */
  2690. brelse(path[i].p_bh);
  2691. path[i].p_bh = NULL;
  2692. i--;
  2693. ext_debug("return to level %d\n", i);
  2694. }
  2695. }
  2696. trace_ext4_ext_remove_space_done(inode, start, end, depth,
  2697. partial_cluster, path->p_hdr->eh_entries);
  2698. /*
  2699. * If we still have something in the partial cluster and we have removed
  2700. * even the first extent, then we should free the blocks in the partial
  2701. * cluster as well. (This code will only run when there are no leaves
  2702. * to the immediate left of the truncated/punched region.)
  2703. */
  2704. if (partial_cluster > 0 && err == 0) {
  2705. /* don't zero partial_cluster since it's not used afterwards */
  2706. ext4_free_blocks(handle, inode, NULL,
  2707. EXT4_C2B(sbi, partial_cluster),
  2708. sbi->s_cluster_ratio,
  2709. get_default_free_blocks_flags(inode));
  2710. }
  2711. /* TODO: flexible tree reduction should be here */
  2712. if (path->p_hdr->eh_entries == 0) {
  2713. /*
  2714. * truncate to zero freed all the tree,
  2715. * so we need to correct eh_depth
  2716. */
  2717. err = ext4_ext_get_access(handle, inode, path);
  2718. if (err == 0) {
  2719. ext_inode_hdr(inode)->eh_depth = 0;
  2720. ext_inode_hdr(inode)->eh_max =
  2721. cpu_to_le16(ext4_ext_space_root(inode, 0));
  2722. err = ext4_ext_dirty(handle, inode, path);
  2723. }
  2724. }
  2725. out:
  2726. ext4_ext_drop_refs(path);
  2727. kfree(path);
  2728. path = NULL;
  2729. if (err == -EAGAIN)
  2730. goto again;
  2731. ext4_journal_stop(handle);
  2732. return err;
  2733. }
  2734. /*
  2735. * called at mount time
  2736. */
  2737. void ext4_ext_init(struct super_block *sb)
  2738. {
  2739. /*
  2740. * possible initialization would be here
  2741. */
  2742. if (ext4_has_feature_extents(sb)) {
  2743. #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
  2744. printk(KERN_INFO "EXT4-fs: file extents enabled"
  2745. #ifdef AGGRESSIVE_TEST
  2746. ", aggressive tests"
  2747. #endif
  2748. #ifdef CHECK_BINSEARCH
  2749. ", check binsearch"
  2750. #endif
  2751. #ifdef EXTENTS_STATS
  2752. ", stats"
  2753. #endif
  2754. "\n");
  2755. #endif
  2756. #ifdef EXTENTS_STATS
  2757. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  2758. EXT4_SB(sb)->s_ext_min = 1 << 30;
  2759. EXT4_SB(sb)->s_ext_max = 0;
  2760. #endif
  2761. }
  2762. }
  2763. /*
  2764. * called at umount time
  2765. */
  2766. void ext4_ext_release(struct super_block *sb)
  2767. {
  2768. if (!ext4_has_feature_extents(sb))
  2769. return;
  2770. #ifdef EXTENTS_STATS
  2771. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  2772. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2773. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  2774. sbi->s_ext_blocks, sbi->s_ext_extents,
  2775. sbi->s_ext_blocks / sbi->s_ext_extents);
  2776. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  2777. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  2778. }
  2779. #endif
  2780. }
  2781. static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
  2782. {
  2783. ext4_lblk_t ee_block;
  2784. ext4_fsblk_t ee_pblock;
  2785. unsigned int ee_len;
  2786. ee_block = le32_to_cpu(ex->ee_block);
  2787. ee_len = ext4_ext_get_actual_len(ex);
  2788. ee_pblock = ext4_ext_pblock(ex);
  2789. if (ee_len == 0)
  2790. return 0;
  2791. return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
  2792. EXTENT_STATUS_WRITTEN);
  2793. }
  2794. /* FIXME!! we need to try to merge to left or right after zero-out */
  2795. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  2796. {
  2797. ext4_fsblk_t ee_pblock;
  2798. unsigned int ee_len;
  2799. ee_len = ext4_ext_get_actual_len(ex);
  2800. ee_pblock = ext4_ext_pblock(ex);
  2801. return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
  2802. ee_len);
  2803. }
  2804. /*
  2805. * ext4_split_extent_at() splits an extent at given block.
  2806. *
  2807. * @handle: the journal handle
  2808. * @inode: the file inode
  2809. * @path: the path to the extent
  2810. * @split: the logical block where the extent is splitted.
  2811. * @split_flags: indicates if the extent could be zeroout if split fails, and
  2812. * the states(init or unwritten) of new extents.
  2813. * @flags: flags used to insert new extent to extent tree.
  2814. *
  2815. *
  2816. * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
  2817. * of which are deterimined by split_flag.
  2818. *
  2819. * There are two cases:
  2820. * a> the extent are splitted into two extent.
  2821. * b> split is not needed, and just mark the extent.
  2822. *
  2823. * return 0 on success.
  2824. */
  2825. static int ext4_split_extent_at(handle_t *handle,
  2826. struct inode *inode,
  2827. struct ext4_ext_path **ppath,
  2828. ext4_lblk_t split,
  2829. int split_flag,
  2830. int flags)
  2831. {
  2832. struct ext4_ext_path *path = *ppath;
  2833. ext4_fsblk_t newblock;
  2834. ext4_lblk_t ee_block;
  2835. struct ext4_extent *ex, newex, orig_ex, zero_ex;
  2836. struct ext4_extent *ex2 = NULL;
  2837. unsigned int ee_len, depth;
  2838. int err = 0;
  2839. BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
  2840. (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
  2841. ext_debug("ext4_split_extents_at: inode %lu, logical"
  2842. "block %llu\n", inode->i_ino, (unsigned long long)split);
  2843. ext4_ext_show_leaf(inode, path);
  2844. depth = ext_depth(inode);
  2845. ex = path[depth].p_ext;
  2846. ee_block = le32_to_cpu(ex->ee_block);
  2847. ee_len = ext4_ext_get_actual_len(ex);
  2848. newblock = split - ee_block + ext4_ext_pblock(ex);
  2849. BUG_ON(split < ee_block || split >= (ee_block + ee_len));
  2850. BUG_ON(!ext4_ext_is_unwritten(ex) &&
  2851. split_flag & (EXT4_EXT_MAY_ZEROOUT |
  2852. EXT4_EXT_MARK_UNWRIT1 |
  2853. EXT4_EXT_MARK_UNWRIT2));
  2854. err = ext4_ext_get_access(handle, inode, path + depth);
  2855. if (err)
  2856. goto out;
  2857. if (split == ee_block) {
  2858. /*
  2859. * case b: block @split is the block that the extent begins with
  2860. * then we just change the state of the extent, and splitting
  2861. * is not needed.
  2862. */
  2863. if (split_flag & EXT4_EXT_MARK_UNWRIT2)
  2864. ext4_ext_mark_unwritten(ex);
  2865. else
  2866. ext4_ext_mark_initialized(ex);
  2867. if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
  2868. ext4_ext_try_to_merge(handle, inode, path, ex);
  2869. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  2870. goto out;
  2871. }
  2872. /* case a */
  2873. memcpy(&orig_ex, ex, sizeof(orig_ex));
  2874. ex->ee_len = cpu_to_le16(split - ee_block);
  2875. if (split_flag & EXT4_EXT_MARK_UNWRIT1)
  2876. ext4_ext_mark_unwritten(ex);
  2877. /*
  2878. * path may lead to new leaf, not to original leaf any more
  2879. * after ext4_ext_insert_extent() returns,
  2880. */
  2881. err = ext4_ext_dirty(handle, inode, path + depth);
  2882. if (err)
  2883. goto fix_extent_len;
  2884. ex2 = &newex;
  2885. ex2->ee_block = cpu_to_le32(split);
  2886. ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
  2887. ext4_ext_store_pblock(ex2, newblock);
  2888. if (split_flag & EXT4_EXT_MARK_UNWRIT2)
  2889. ext4_ext_mark_unwritten(ex2);
  2890. err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
  2891. if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2892. if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
  2893. if (split_flag & EXT4_EXT_DATA_VALID1) {
  2894. err = ext4_ext_zeroout(inode, ex2);
  2895. zero_ex.ee_block = ex2->ee_block;
  2896. zero_ex.ee_len = cpu_to_le16(
  2897. ext4_ext_get_actual_len(ex2));
  2898. ext4_ext_store_pblock(&zero_ex,
  2899. ext4_ext_pblock(ex2));
  2900. } else {
  2901. err = ext4_ext_zeroout(inode, ex);
  2902. zero_ex.ee_block = ex->ee_block;
  2903. zero_ex.ee_len = cpu_to_le16(
  2904. ext4_ext_get_actual_len(ex));
  2905. ext4_ext_store_pblock(&zero_ex,
  2906. ext4_ext_pblock(ex));
  2907. }
  2908. } else {
  2909. err = ext4_ext_zeroout(inode, &orig_ex);
  2910. zero_ex.ee_block = orig_ex.ee_block;
  2911. zero_ex.ee_len = cpu_to_le16(
  2912. ext4_ext_get_actual_len(&orig_ex));
  2913. ext4_ext_store_pblock(&zero_ex,
  2914. ext4_ext_pblock(&orig_ex));
  2915. }
  2916. if (err)
  2917. goto fix_extent_len;
  2918. /* update the extent length and mark as initialized */
  2919. ex->ee_len = cpu_to_le16(ee_len);
  2920. ext4_ext_try_to_merge(handle, inode, path, ex);
  2921. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  2922. if (err)
  2923. goto fix_extent_len;
  2924. /* update extent status tree */
  2925. err = ext4_zeroout_es(inode, &zero_ex);
  2926. goto out;
  2927. } else if (err)
  2928. goto fix_extent_len;
  2929. out:
  2930. ext4_ext_show_leaf(inode, path);
  2931. return err;
  2932. fix_extent_len:
  2933. ex->ee_len = orig_ex.ee_len;
  2934. ext4_ext_dirty(handle, inode, path + path->p_depth);
  2935. return err;
  2936. }
  2937. /*
  2938. * ext4_split_extents() splits an extent and mark extent which is covered
  2939. * by @map as split_flags indicates
  2940. *
  2941. * It may result in splitting the extent into multiple extents (up to three)
  2942. * There are three possibilities:
  2943. * a> There is no split required
  2944. * b> Splits in two extents: Split is happening at either end of the extent
  2945. * c> Splits in three extents: Somone is splitting in middle of the extent
  2946. *
  2947. */
  2948. static int ext4_split_extent(handle_t *handle,
  2949. struct inode *inode,
  2950. struct ext4_ext_path **ppath,
  2951. struct ext4_map_blocks *map,
  2952. int split_flag,
  2953. int flags)
  2954. {
  2955. struct ext4_ext_path *path = *ppath;
  2956. ext4_lblk_t ee_block;
  2957. struct ext4_extent *ex;
  2958. unsigned int ee_len, depth;
  2959. int err = 0;
  2960. int unwritten;
  2961. int split_flag1, flags1;
  2962. int allocated = map->m_len;
  2963. depth = ext_depth(inode);
  2964. ex = path[depth].p_ext;
  2965. ee_block = le32_to_cpu(ex->ee_block);
  2966. ee_len = ext4_ext_get_actual_len(ex);
  2967. unwritten = ext4_ext_is_unwritten(ex);
  2968. if (map->m_lblk + map->m_len < ee_block + ee_len) {
  2969. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
  2970. flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
  2971. if (unwritten)
  2972. split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
  2973. EXT4_EXT_MARK_UNWRIT2;
  2974. if (split_flag & EXT4_EXT_DATA_VALID2)
  2975. split_flag1 |= EXT4_EXT_DATA_VALID1;
  2976. err = ext4_split_extent_at(handle, inode, ppath,
  2977. map->m_lblk + map->m_len, split_flag1, flags1);
  2978. if (err)
  2979. goto out;
  2980. } else {
  2981. allocated = ee_len - (map->m_lblk - ee_block);
  2982. }
  2983. /*
  2984. * Update path is required because previous ext4_split_extent_at() may
  2985. * result in split of original leaf or extent zeroout.
  2986. */
  2987. path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
  2988. if (IS_ERR(path))
  2989. return PTR_ERR(path);
  2990. depth = ext_depth(inode);
  2991. ex = path[depth].p_ext;
  2992. if (!ex) {
  2993. EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
  2994. (unsigned long) map->m_lblk);
  2995. return -EFSCORRUPTED;
  2996. }
  2997. unwritten = ext4_ext_is_unwritten(ex);
  2998. split_flag1 = 0;
  2999. if (map->m_lblk >= ee_block) {
  3000. split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
  3001. if (unwritten) {
  3002. split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
  3003. split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
  3004. EXT4_EXT_MARK_UNWRIT2);
  3005. }
  3006. err = ext4_split_extent_at(handle, inode, ppath,
  3007. map->m_lblk, split_flag1, flags);
  3008. if (err)
  3009. goto out;
  3010. }
  3011. ext4_ext_show_leaf(inode, path);
  3012. out:
  3013. return err ? err : allocated;
  3014. }
  3015. /*
  3016. * This function is called by ext4_ext_map_blocks() if someone tries to write
  3017. * to an unwritten extent. It may result in splitting the unwritten
  3018. * extent into multiple extents (up to three - one initialized and two
  3019. * unwritten).
  3020. * There are three possibilities:
  3021. * a> There is no split required: Entire extent should be initialized
  3022. * b> Splits in two extents: Write is happening at either end of the extent
  3023. * c> Splits in three extents: Somone is writing in middle of the extent
  3024. *
  3025. * Pre-conditions:
  3026. * - The extent pointed to by 'path' is unwritten.
  3027. * - The extent pointed to by 'path' contains a superset
  3028. * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
  3029. *
  3030. * Post-conditions on success:
  3031. * - the returned value is the number of blocks beyond map->l_lblk
  3032. * that are allocated and initialized.
  3033. * It is guaranteed to be >= map->m_len.
  3034. */
  3035. static int ext4_ext_convert_to_initialized(handle_t *handle,
  3036. struct inode *inode,
  3037. struct ext4_map_blocks *map,
  3038. struct ext4_ext_path **ppath,
  3039. int flags)
  3040. {
  3041. struct ext4_ext_path *path = *ppath;
  3042. struct ext4_sb_info *sbi;
  3043. struct ext4_extent_header *eh;
  3044. struct ext4_map_blocks split_map;
  3045. struct ext4_extent zero_ex1, zero_ex2;
  3046. struct ext4_extent *ex, *abut_ex;
  3047. ext4_lblk_t ee_block, eof_block;
  3048. unsigned int ee_len, depth, map_len = map->m_len;
  3049. int allocated = 0, max_zeroout = 0;
  3050. int err = 0;
  3051. int split_flag = EXT4_EXT_DATA_VALID2;
  3052. ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
  3053. "block %llu, max_blocks %u\n", inode->i_ino,
  3054. (unsigned long long)map->m_lblk, map_len);
  3055. sbi = EXT4_SB(inode->i_sb);
  3056. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  3057. inode->i_sb->s_blocksize_bits;
  3058. if (eof_block < map->m_lblk + map_len)
  3059. eof_block = map->m_lblk + map_len;
  3060. depth = ext_depth(inode);
  3061. eh = path[depth].p_hdr;
  3062. ex = path[depth].p_ext;
  3063. ee_block = le32_to_cpu(ex->ee_block);
  3064. ee_len = ext4_ext_get_actual_len(ex);
  3065. zero_ex1.ee_len = 0;
  3066. zero_ex2.ee_len = 0;
  3067. trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
  3068. /* Pre-conditions */
  3069. BUG_ON(!ext4_ext_is_unwritten(ex));
  3070. BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
  3071. /*
  3072. * Attempt to transfer newly initialized blocks from the currently
  3073. * unwritten extent to its neighbor. This is much cheaper
  3074. * than an insertion followed by a merge as those involve costly
  3075. * memmove() calls. Transferring to the left is the common case in
  3076. * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
  3077. * followed by append writes.
  3078. *
  3079. * Limitations of the current logic:
  3080. * - L1: we do not deal with writes covering the whole extent.
  3081. * This would require removing the extent if the transfer
  3082. * is possible.
  3083. * - L2: we only attempt to merge with an extent stored in the
  3084. * same extent tree node.
  3085. */
  3086. if ((map->m_lblk == ee_block) &&
  3087. /* See if we can merge left */
  3088. (map_len < ee_len) && /*L1*/
  3089. (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
  3090. ext4_lblk_t prev_lblk;
  3091. ext4_fsblk_t prev_pblk, ee_pblk;
  3092. unsigned int prev_len;
  3093. abut_ex = ex - 1;
  3094. prev_lblk = le32_to_cpu(abut_ex->ee_block);
  3095. prev_len = ext4_ext_get_actual_len(abut_ex);
  3096. prev_pblk = ext4_ext_pblock(abut_ex);
  3097. ee_pblk = ext4_ext_pblock(ex);
  3098. /*
  3099. * A transfer of blocks from 'ex' to 'abut_ex' is allowed
  3100. * upon those conditions:
  3101. * - C1: abut_ex is initialized,
  3102. * - C2: abut_ex is logically abutting ex,
  3103. * - C3: abut_ex is physically abutting ex,
  3104. * - C4: abut_ex can receive the additional blocks without
  3105. * overflowing the (initialized) length limit.
  3106. */
  3107. if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
  3108. ((prev_lblk + prev_len) == ee_block) && /*C2*/
  3109. ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
  3110. (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
  3111. err = ext4_ext_get_access(handle, inode, path + depth);
  3112. if (err)
  3113. goto out;
  3114. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  3115. map, ex, abut_ex);
  3116. /* Shift the start of ex by 'map_len' blocks */
  3117. ex->ee_block = cpu_to_le32(ee_block + map_len);
  3118. ext4_ext_store_pblock(ex, ee_pblk + map_len);
  3119. ex->ee_len = cpu_to_le16(ee_len - map_len);
  3120. ext4_ext_mark_unwritten(ex); /* Restore the flag */
  3121. /* Extend abut_ex by 'map_len' blocks */
  3122. abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
  3123. /* Result: number of initialized blocks past m_lblk */
  3124. allocated = map_len;
  3125. }
  3126. } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
  3127. (map_len < ee_len) && /*L1*/
  3128. ex < EXT_LAST_EXTENT(eh)) { /*L2*/
  3129. /* See if we can merge right */
  3130. ext4_lblk_t next_lblk;
  3131. ext4_fsblk_t next_pblk, ee_pblk;
  3132. unsigned int next_len;
  3133. abut_ex = ex + 1;
  3134. next_lblk = le32_to_cpu(abut_ex->ee_block);
  3135. next_len = ext4_ext_get_actual_len(abut_ex);
  3136. next_pblk = ext4_ext_pblock(abut_ex);
  3137. ee_pblk = ext4_ext_pblock(ex);
  3138. /*
  3139. * A transfer of blocks from 'ex' to 'abut_ex' is allowed
  3140. * upon those conditions:
  3141. * - C1: abut_ex is initialized,
  3142. * - C2: abut_ex is logically abutting ex,
  3143. * - C3: abut_ex is physically abutting ex,
  3144. * - C4: abut_ex can receive the additional blocks without
  3145. * overflowing the (initialized) length limit.
  3146. */
  3147. if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
  3148. ((map->m_lblk + map_len) == next_lblk) && /*C2*/
  3149. ((ee_pblk + ee_len) == next_pblk) && /*C3*/
  3150. (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
  3151. err = ext4_ext_get_access(handle, inode, path + depth);
  3152. if (err)
  3153. goto out;
  3154. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  3155. map, ex, abut_ex);
  3156. /* Shift the start of abut_ex by 'map_len' blocks */
  3157. abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
  3158. ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
  3159. ex->ee_len = cpu_to_le16(ee_len - map_len);
  3160. ext4_ext_mark_unwritten(ex); /* Restore the flag */
  3161. /* Extend abut_ex by 'map_len' blocks */
  3162. abut_ex->ee_len = cpu_to_le16(next_len + map_len);
  3163. /* Result: number of initialized blocks past m_lblk */
  3164. allocated = map_len;
  3165. }
  3166. }
  3167. if (allocated) {
  3168. /* Mark the block containing both extents as dirty */
  3169. ext4_ext_dirty(handle, inode, path + depth);
  3170. /* Update path to point to the right extent */
  3171. path[depth].p_ext = abut_ex;
  3172. goto out;
  3173. } else
  3174. allocated = ee_len - (map->m_lblk - ee_block);
  3175. WARN_ON(map->m_lblk < ee_block);
  3176. /*
  3177. * It is safe to convert extent to initialized via explicit
  3178. * zeroout only if extent is fully inside i_size or new_size.
  3179. */
  3180. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  3181. if (EXT4_EXT_MAY_ZEROOUT & split_flag)
  3182. max_zeroout = sbi->s_extent_max_zeroout_kb >>
  3183. (inode->i_sb->s_blocksize_bits - 10);
  3184. if (ext4_encrypted_inode(inode))
  3185. max_zeroout = 0;
  3186. /*
  3187. * five cases:
  3188. * 1. split the extent into three extents.
  3189. * 2. split the extent into two extents, zeroout the head of the first
  3190. * extent.
  3191. * 3. split the extent into two extents, zeroout the tail of the second
  3192. * extent.
  3193. * 4. split the extent into two extents with out zeroout.
  3194. * 5. no splitting needed, just possibly zeroout the head and / or the
  3195. * tail of the extent.
  3196. */
  3197. split_map.m_lblk = map->m_lblk;
  3198. split_map.m_len = map->m_len;
  3199. if (max_zeroout && (allocated > split_map.m_len)) {
  3200. if (allocated <= max_zeroout) {
  3201. /* case 3 or 5 */
  3202. zero_ex1.ee_block =
  3203. cpu_to_le32(split_map.m_lblk +
  3204. split_map.m_len);
  3205. zero_ex1.ee_len =
  3206. cpu_to_le16(allocated - split_map.m_len);
  3207. ext4_ext_store_pblock(&zero_ex1,
  3208. ext4_ext_pblock(ex) + split_map.m_lblk +
  3209. split_map.m_len - ee_block);
  3210. err = ext4_ext_zeroout(inode, &zero_ex1);
  3211. if (err)
  3212. goto out;
  3213. split_map.m_len = allocated;
  3214. }
  3215. if (split_map.m_lblk - ee_block + split_map.m_len <
  3216. max_zeroout) {
  3217. /* case 2 or 5 */
  3218. if (split_map.m_lblk != ee_block) {
  3219. zero_ex2.ee_block = ex->ee_block;
  3220. zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
  3221. ee_block);
  3222. ext4_ext_store_pblock(&zero_ex2,
  3223. ext4_ext_pblock(ex));
  3224. err = ext4_ext_zeroout(inode, &zero_ex2);
  3225. if (err)
  3226. goto out;
  3227. }
  3228. split_map.m_len += split_map.m_lblk - ee_block;
  3229. split_map.m_lblk = ee_block;
  3230. allocated = map->m_len;
  3231. }
  3232. }
  3233. err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
  3234. flags);
  3235. if (err > 0)
  3236. err = 0;
  3237. out:
  3238. /* If we have gotten a failure, don't zero out status tree */
  3239. if (!err) {
  3240. err = ext4_zeroout_es(inode, &zero_ex1);
  3241. if (!err)
  3242. err = ext4_zeroout_es(inode, &zero_ex2);
  3243. }
  3244. return err ? err : allocated;
  3245. }
  3246. /*
  3247. * This function is called by ext4_ext_map_blocks() from
  3248. * ext4_get_blocks_dio_write() when DIO to write
  3249. * to an unwritten extent.
  3250. *
  3251. * Writing to an unwritten extent may result in splitting the unwritten
  3252. * extent into multiple initialized/unwritten extents (up to three)
  3253. * There are three possibilities:
  3254. * a> There is no split required: Entire extent should be unwritten
  3255. * b> Splits in two extents: Write is happening at either end of the extent
  3256. * c> Splits in three extents: Somone is writing in middle of the extent
  3257. *
  3258. * This works the same way in the case of initialized -> unwritten conversion.
  3259. *
  3260. * One of more index blocks maybe needed if the extent tree grow after
  3261. * the unwritten extent split. To prevent ENOSPC occur at the IO
  3262. * complete, we need to split the unwritten extent before DIO submit
  3263. * the IO. The unwritten extent called at this time will be split
  3264. * into three unwritten extent(at most). After IO complete, the part
  3265. * being filled will be convert to initialized by the end_io callback function
  3266. * via ext4_convert_unwritten_extents().
  3267. *
  3268. * Returns the size of unwritten extent to be written on success.
  3269. */
  3270. static int ext4_split_convert_extents(handle_t *handle,
  3271. struct inode *inode,
  3272. struct ext4_map_blocks *map,
  3273. struct ext4_ext_path **ppath,
  3274. int flags)
  3275. {
  3276. struct ext4_ext_path *path = *ppath;
  3277. ext4_lblk_t eof_block;
  3278. ext4_lblk_t ee_block;
  3279. struct ext4_extent *ex;
  3280. unsigned int ee_len;
  3281. int split_flag = 0, depth;
  3282. ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
  3283. __func__, inode->i_ino,
  3284. (unsigned long long)map->m_lblk, map->m_len);
  3285. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  3286. inode->i_sb->s_blocksize_bits;
  3287. if (eof_block < map->m_lblk + map->m_len)
  3288. eof_block = map->m_lblk + map->m_len;
  3289. /*
  3290. * It is safe to convert extent to initialized via explicit
  3291. * zeroout only if extent is fully insde i_size or new_size.
  3292. */
  3293. depth = ext_depth(inode);
  3294. ex = path[depth].p_ext;
  3295. ee_block = le32_to_cpu(ex->ee_block);
  3296. ee_len = ext4_ext_get_actual_len(ex);
  3297. /* Convert to unwritten */
  3298. if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
  3299. split_flag |= EXT4_EXT_DATA_VALID1;
  3300. /* Convert to initialized */
  3301. } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
  3302. split_flag |= ee_block + ee_len <= eof_block ?
  3303. EXT4_EXT_MAY_ZEROOUT : 0;
  3304. split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
  3305. }
  3306. flags |= EXT4_GET_BLOCKS_PRE_IO;
  3307. return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
  3308. }
  3309. static int ext4_convert_unwritten_extents_endio(handle_t *handle,
  3310. struct inode *inode,
  3311. struct ext4_map_blocks *map,
  3312. struct ext4_ext_path **ppath)
  3313. {
  3314. struct ext4_ext_path *path = *ppath;
  3315. struct ext4_extent *ex;
  3316. ext4_lblk_t ee_block;
  3317. unsigned int ee_len;
  3318. int depth;
  3319. int err = 0;
  3320. depth = ext_depth(inode);
  3321. ex = path[depth].p_ext;
  3322. ee_block = le32_to_cpu(ex->ee_block);
  3323. ee_len = ext4_ext_get_actual_len(ex);
  3324. ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
  3325. "block %llu, max_blocks %u\n", inode->i_ino,
  3326. (unsigned long long)ee_block, ee_len);
  3327. /* If extent is larger than requested it is a clear sign that we still
  3328. * have some extent state machine issues left. So extent_split is still
  3329. * required.
  3330. * TODO: Once all related issues will be fixed this situation should be
  3331. * illegal.
  3332. */
  3333. if (ee_block != map->m_lblk || ee_len > map->m_len) {
  3334. #ifdef EXT4_DEBUG
  3335. ext4_warning("Inode (%ld) finished: extent logical block %llu,"
  3336. " len %u; IO logical block %llu, len %u",
  3337. inode->i_ino, (unsigned long long)ee_block, ee_len,
  3338. (unsigned long long)map->m_lblk, map->m_len);
  3339. #endif
  3340. err = ext4_split_convert_extents(handle, inode, map, ppath,
  3341. EXT4_GET_BLOCKS_CONVERT);
  3342. if (err < 0)
  3343. return err;
  3344. path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
  3345. if (IS_ERR(path))
  3346. return PTR_ERR(path);
  3347. depth = ext_depth(inode);
  3348. ex = path[depth].p_ext;
  3349. }
  3350. err = ext4_ext_get_access(handle, inode, path + depth);
  3351. if (err)
  3352. goto out;
  3353. /* first mark the extent as initialized */
  3354. ext4_ext_mark_initialized(ex);
  3355. /* note: ext4_ext_correct_indexes() isn't needed here because
  3356. * borders are not changed
  3357. */
  3358. ext4_ext_try_to_merge(handle, inode, path, ex);
  3359. /* Mark modified extent as dirty */
  3360. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  3361. out:
  3362. ext4_ext_show_leaf(inode, path);
  3363. return err;
  3364. }
  3365. static void unmap_underlying_metadata_blocks(struct block_device *bdev,
  3366. sector_t block, int count)
  3367. {
  3368. int i;
  3369. for (i = 0; i < count; i++)
  3370. unmap_underlying_metadata(bdev, block + i);
  3371. }
  3372. /*
  3373. * Handle EOFBLOCKS_FL flag, clearing it if necessary
  3374. */
  3375. static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
  3376. ext4_lblk_t lblk,
  3377. struct ext4_ext_path *path,
  3378. unsigned int len)
  3379. {
  3380. int i, depth;
  3381. struct ext4_extent_header *eh;
  3382. struct ext4_extent *last_ex;
  3383. if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
  3384. return 0;
  3385. depth = ext_depth(inode);
  3386. eh = path[depth].p_hdr;
  3387. /*
  3388. * We're going to remove EOFBLOCKS_FL entirely in future so we
  3389. * do not care for this case anymore. Simply remove the flag
  3390. * if there are no extents.
  3391. */
  3392. if (unlikely(!eh->eh_entries))
  3393. goto out;
  3394. last_ex = EXT_LAST_EXTENT(eh);
  3395. /*
  3396. * We should clear the EOFBLOCKS_FL flag if we are writing the
  3397. * last block in the last extent in the file. We test this by
  3398. * first checking to see if the caller to
  3399. * ext4_ext_get_blocks() was interested in the last block (or
  3400. * a block beyond the last block) in the current extent. If
  3401. * this turns out to be false, we can bail out from this
  3402. * function immediately.
  3403. */
  3404. if (lblk + len < le32_to_cpu(last_ex->ee_block) +
  3405. ext4_ext_get_actual_len(last_ex))
  3406. return 0;
  3407. /*
  3408. * If the caller does appear to be planning to write at or
  3409. * beyond the end of the current extent, we then test to see
  3410. * if the current extent is the last extent in the file, by
  3411. * checking to make sure it was reached via the rightmost node
  3412. * at each level of the tree.
  3413. */
  3414. for (i = depth-1; i >= 0; i--)
  3415. if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
  3416. return 0;
  3417. out:
  3418. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3419. return ext4_mark_inode_dirty(handle, inode);
  3420. }
  3421. /**
  3422. * ext4_find_delalloc_range: find delayed allocated block in the given range.
  3423. *
  3424. * Return 1 if there is a delalloc block in the range, otherwise 0.
  3425. */
  3426. int ext4_find_delalloc_range(struct inode *inode,
  3427. ext4_lblk_t lblk_start,
  3428. ext4_lblk_t lblk_end)
  3429. {
  3430. struct extent_status es;
  3431. ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es);
  3432. if (es.es_len == 0)
  3433. return 0; /* there is no delay extent in this tree */
  3434. else if (es.es_lblk <= lblk_start &&
  3435. lblk_start < es.es_lblk + es.es_len)
  3436. return 1;
  3437. else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end)
  3438. return 1;
  3439. else
  3440. return 0;
  3441. }
  3442. int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
  3443. {
  3444. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3445. ext4_lblk_t lblk_start, lblk_end;
  3446. lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
  3447. lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
  3448. return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
  3449. }
  3450. /**
  3451. * Determines how many complete clusters (out of those specified by the 'map')
  3452. * are under delalloc and were reserved quota for.
  3453. * This function is called when we are writing out the blocks that were
  3454. * originally written with their allocation delayed, but then the space was
  3455. * allocated using fallocate() before the delayed allocation could be resolved.
  3456. * The cases to look for are:
  3457. * ('=' indicated delayed allocated blocks
  3458. * '-' indicates non-delayed allocated blocks)
  3459. * (a) partial clusters towards beginning and/or end outside of allocated range
  3460. * are not delalloc'ed.
  3461. * Ex:
  3462. * |----c---=|====c====|====c====|===-c----|
  3463. * |++++++ allocated ++++++|
  3464. * ==> 4 complete clusters in above example
  3465. *
  3466. * (b) partial cluster (outside of allocated range) towards either end is
  3467. * marked for delayed allocation. In this case, we will exclude that
  3468. * cluster.
  3469. * Ex:
  3470. * |----====c========|========c========|
  3471. * |++++++ allocated ++++++|
  3472. * ==> 1 complete clusters in above example
  3473. *
  3474. * Ex:
  3475. * |================c================|
  3476. * |++++++ allocated ++++++|
  3477. * ==> 0 complete clusters in above example
  3478. *
  3479. * The ext4_da_update_reserve_space will be called only if we
  3480. * determine here that there were some "entire" clusters that span
  3481. * this 'allocated' range.
  3482. * In the non-bigalloc case, this function will just end up returning num_blks
  3483. * without ever calling ext4_find_delalloc_range.
  3484. */
  3485. static unsigned int
  3486. get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
  3487. unsigned int num_blks)
  3488. {
  3489. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3490. ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
  3491. ext4_lblk_t lblk_from, lblk_to, c_offset;
  3492. unsigned int allocated_clusters = 0;
  3493. alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
  3494. alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
  3495. /* max possible clusters for this allocation */
  3496. allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
  3497. trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
  3498. /* Check towards left side */
  3499. c_offset = EXT4_LBLK_COFF(sbi, lblk_start);
  3500. if (c_offset) {
  3501. lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start);
  3502. lblk_to = lblk_from + c_offset - 1;
  3503. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
  3504. allocated_clusters--;
  3505. }
  3506. /* Now check towards right. */
  3507. c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks);
  3508. if (allocated_clusters && c_offset) {
  3509. lblk_from = lblk_start + num_blks;
  3510. lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
  3511. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
  3512. allocated_clusters--;
  3513. }
  3514. return allocated_clusters;
  3515. }
  3516. static int
  3517. convert_initialized_extent(handle_t *handle, struct inode *inode,
  3518. struct ext4_map_blocks *map,
  3519. struct ext4_ext_path **ppath,
  3520. unsigned int allocated)
  3521. {
  3522. struct ext4_ext_path *path = *ppath;
  3523. struct ext4_extent *ex;
  3524. ext4_lblk_t ee_block;
  3525. unsigned int ee_len;
  3526. int depth;
  3527. int err = 0;
  3528. /*
  3529. * Make sure that the extent is no bigger than we support with
  3530. * unwritten extent
  3531. */
  3532. if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
  3533. map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
  3534. depth = ext_depth(inode);
  3535. ex = path[depth].p_ext;
  3536. ee_block = le32_to_cpu(ex->ee_block);
  3537. ee_len = ext4_ext_get_actual_len(ex);
  3538. ext_debug("%s: inode %lu, logical"
  3539. "block %llu, max_blocks %u\n", __func__, inode->i_ino,
  3540. (unsigned long long)ee_block, ee_len);
  3541. if (ee_block != map->m_lblk || ee_len > map->m_len) {
  3542. err = ext4_split_convert_extents(handle, inode, map, ppath,
  3543. EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
  3544. if (err < 0)
  3545. return err;
  3546. path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
  3547. if (IS_ERR(path))
  3548. return PTR_ERR(path);
  3549. depth = ext_depth(inode);
  3550. ex = path[depth].p_ext;
  3551. if (!ex) {
  3552. EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
  3553. (unsigned long) map->m_lblk);
  3554. return -EFSCORRUPTED;
  3555. }
  3556. }
  3557. err = ext4_ext_get_access(handle, inode, path + depth);
  3558. if (err)
  3559. return err;
  3560. /* first mark the extent as unwritten */
  3561. ext4_ext_mark_unwritten(ex);
  3562. /* note: ext4_ext_correct_indexes() isn't needed here because
  3563. * borders are not changed
  3564. */
  3565. ext4_ext_try_to_merge(handle, inode, path, ex);
  3566. /* Mark modified extent as dirty */
  3567. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  3568. if (err)
  3569. return err;
  3570. ext4_ext_show_leaf(inode, path);
  3571. ext4_update_inode_fsync_trans(handle, inode, 1);
  3572. err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
  3573. if (err)
  3574. return err;
  3575. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3576. if (allocated > map->m_len)
  3577. allocated = map->m_len;
  3578. map->m_len = allocated;
  3579. return allocated;
  3580. }
  3581. static int
  3582. ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
  3583. struct ext4_map_blocks *map,
  3584. struct ext4_ext_path **ppath, int flags,
  3585. unsigned int allocated, ext4_fsblk_t newblock)
  3586. {
  3587. struct ext4_ext_path *path = *ppath;
  3588. int ret = 0;
  3589. int err = 0;
  3590. ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
  3591. "block %llu, max_blocks %u, flags %x, allocated %u\n",
  3592. inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
  3593. flags, allocated);
  3594. ext4_ext_show_leaf(inode, path);
  3595. /*
  3596. * When writing into unwritten space, we should not fail to
  3597. * allocate metadata blocks for the new extent block if needed.
  3598. */
  3599. flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
  3600. trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
  3601. allocated, newblock);
  3602. /* get_block() before submit the IO, split the extent */
  3603. if (flags & EXT4_GET_BLOCKS_PRE_IO) {
  3604. ret = ext4_split_convert_extents(handle, inode, map, ppath,
  3605. flags | EXT4_GET_BLOCKS_CONVERT);
  3606. if (ret <= 0)
  3607. goto out;
  3608. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3609. goto out;
  3610. }
  3611. /* IO end_io complete, convert the filled extent to written */
  3612. if (flags & EXT4_GET_BLOCKS_CONVERT) {
  3613. if (flags & EXT4_GET_BLOCKS_ZERO) {
  3614. if (allocated > map->m_len)
  3615. allocated = map->m_len;
  3616. err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
  3617. allocated);
  3618. if (err < 0)
  3619. goto out2;
  3620. }
  3621. ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
  3622. ppath);
  3623. if (ret >= 0) {
  3624. ext4_update_inode_fsync_trans(handle, inode, 1);
  3625. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  3626. path, map->m_len);
  3627. } else
  3628. err = ret;
  3629. map->m_flags |= EXT4_MAP_MAPPED;
  3630. map->m_pblk = newblock;
  3631. if (allocated > map->m_len)
  3632. allocated = map->m_len;
  3633. map->m_len = allocated;
  3634. goto out2;
  3635. }
  3636. /* buffered IO case */
  3637. /*
  3638. * repeat fallocate creation request
  3639. * we already have an unwritten extent
  3640. */
  3641. if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
  3642. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3643. goto map_out;
  3644. }
  3645. /* buffered READ or buffered write_begin() lookup */
  3646. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3647. /*
  3648. * We have blocks reserved already. We
  3649. * return allocated blocks so that delalloc
  3650. * won't do block reservation for us. But
  3651. * the buffer head will be unmapped so that
  3652. * a read from the block returns 0s.
  3653. */
  3654. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3655. goto out1;
  3656. }
  3657. /* buffered write, writepage time, convert*/
  3658. ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
  3659. if (ret >= 0)
  3660. ext4_update_inode_fsync_trans(handle, inode, 1);
  3661. out:
  3662. if (ret <= 0) {
  3663. err = ret;
  3664. goto out2;
  3665. } else
  3666. allocated = ret;
  3667. map->m_flags |= EXT4_MAP_NEW;
  3668. /*
  3669. * if we allocated more blocks than requested
  3670. * we need to make sure we unmap the extra block
  3671. * allocated. The actual needed block will get
  3672. * unmapped later when we find the buffer_head marked
  3673. * new.
  3674. */
  3675. if (allocated > map->m_len) {
  3676. unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
  3677. newblock + map->m_len,
  3678. allocated - map->m_len);
  3679. allocated = map->m_len;
  3680. }
  3681. map->m_len = allocated;
  3682. /*
  3683. * If we have done fallocate with the offset that is already
  3684. * delayed allocated, we would have block reservation
  3685. * and quota reservation done in the delayed write path.
  3686. * But fallocate would have already updated quota and block
  3687. * count for this offset. So cancel these reservation
  3688. */
  3689. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  3690. unsigned int reserved_clusters;
  3691. reserved_clusters = get_reserved_cluster_alloc(inode,
  3692. map->m_lblk, map->m_len);
  3693. if (reserved_clusters)
  3694. ext4_da_update_reserve_space(inode,
  3695. reserved_clusters,
  3696. 0);
  3697. }
  3698. map_out:
  3699. map->m_flags |= EXT4_MAP_MAPPED;
  3700. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
  3701. err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
  3702. map->m_len);
  3703. if (err < 0)
  3704. goto out2;
  3705. }
  3706. out1:
  3707. if (allocated > map->m_len)
  3708. allocated = map->m_len;
  3709. ext4_ext_show_leaf(inode, path);
  3710. map->m_pblk = newblock;
  3711. map->m_len = allocated;
  3712. out2:
  3713. return err ? err : allocated;
  3714. }
  3715. /*
  3716. * get_implied_cluster_alloc - check to see if the requested
  3717. * allocation (in the map structure) overlaps with a cluster already
  3718. * allocated in an extent.
  3719. * @sb The filesystem superblock structure
  3720. * @map The requested lblk->pblk mapping
  3721. * @ex The extent structure which might contain an implied
  3722. * cluster allocation
  3723. *
  3724. * This function is called by ext4_ext_map_blocks() after we failed to
  3725. * find blocks that were already in the inode's extent tree. Hence,
  3726. * we know that the beginning of the requested region cannot overlap
  3727. * the extent from the inode's extent tree. There are three cases we
  3728. * want to catch. The first is this case:
  3729. *
  3730. * |--- cluster # N--|
  3731. * |--- extent ---| |---- requested region ---|
  3732. * |==========|
  3733. *
  3734. * The second case that we need to test for is this one:
  3735. *
  3736. * |--------- cluster # N ----------------|
  3737. * |--- requested region --| |------- extent ----|
  3738. * |=======================|
  3739. *
  3740. * The third case is when the requested region lies between two extents
  3741. * within the same cluster:
  3742. * |------------- cluster # N-------------|
  3743. * |----- ex -----| |---- ex_right ----|
  3744. * |------ requested region ------|
  3745. * |================|
  3746. *
  3747. * In each of the above cases, we need to set the map->m_pblk and
  3748. * map->m_len so it corresponds to the return the extent labelled as
  3749. * "|====|" from cluster #N, since it is already in use for data in
  3750. * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
  3751. * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
  3752. * as a new "allocated" block region. Otherwise, we will return 0 and
  3753. * ext4_ext_map_blocks() will then allocate one or more new clusters
  3754. * by calling ext4_mb_new_blocks().
  3755. */
  3756. static int get_implied_cluster_alloc(struct super_block *sb,
  3757. struct ext4_map_blocks *map,
  3758. struct ext4_extent *ex,
  3759. struct ext4_ext_path *path)
  3760. {
  3761. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3762. ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
  3763. ext4_lblk_t ex_cluster_start, ex_cluster_end;
  3764. ext4_lblk_t rr_cluster_start;
  3765. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3766. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3767. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  3768. /* The extent passed in that we are trying to match */
  3769. ex_cluster_start = EXT4_B2C(sbi, ee_block);
  3770. ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
  3771. /* The requested region passed into ext4_map_blocks() */
  3772. rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
  3773. if ((rr_cluster_start == ex_cluster_end) ||
  3774. (rr_cluster_start == ex_cluster_start)) {
  3775. if (rr_cluster_start == ex_cluster_end)
  3776. ee_start += ee_len - 1;
  3777. map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
  3778. map->m_len = min(map->m_len,
  3779. (unsigned) sbi->s_cluster_ratio - c_offset);
  3780. /*
  3781. * Check for and handle this case:
  3782. *
  3783. * |--------- cluster # N-------------|
  3784. * |------- extent ----|
  3785. * |--- requested region ---|
  3786. * |===========|
  3787. */
  3788. if (map->m_lblk < ee_block)
  3789. map->m_len = min(map->m_len, ee_block - map->m_lblk);
  3790. /*
  3791. * Check for the case where there is already another allocated
  3792. * block to the right of 'ex' but before the end of the cluster.
  3793. *
  3794. * |------------- cluster # N-------------|
  3795. * |----- ex -----| |---- ex_right ----|
  3796. * |------ requested region ------|
  3797. * |================|
  3798. */
  3799. if (map->m_lblk > ee_block) {
  3800. ext4_lblk_t next = ext4_ext_next_allocated_block(path);
  3801. map->m_len = min(map->m_len, next - map->m_lblk);
  3802. }
  3803. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
  3804. return 1;
  3805. }
  3806. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
  3807. return 0;
  3808. }
  3809. /*
  3810. * Block allocation/map/preallocation routine for extents based files
  3811. *
  3812. *
  3813. * Need to be called with
  3814. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  3815. * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  3816. *
  3817. * return > 0, number of of blocks already mapped/allocated
  3818. * if create == 0 and these are pre-allocated blocks
  3819. * buffer head is unmapped
  3820. * otherwise blocks are mapped
  3821. *
  3822. * return = 0, if plain look up failed (blocks have not been allocated)
  3823. * buffer head is unmapped
  3824. *
  3825. * return < 0, error case.
  3826. */
  3827. int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
  3828. struct ext4_map_blocks *map, int flags)
  3829. {
  3830. struct ext4_ext_path *path = NULL;
  3831. struct ext4_extent newex, *ex, *ex2;
  3832. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3833. ext4_fsblk_t newblock = 0;
  3834. int free_on_err = 0, err = 0, depth, ret;
  3835. unsigned int allocated = 0, offset = 0;
  3836. unsigned int allocated_clusters = 0;
  3837. struct ext4_allocation_request ar;
  3838. ext4_lblk_t cluster_offset;
  3839. bool map_from_cluster = false;
  3840. ext_debug("blocks %u/%u requested for inode %lu\n",
  3841. map->m_lblk, map->m_len, inode->i_ino);
  3842. trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  3843. /* find extent for this block */
  3844. path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
  3845. if (IS_ERR(path)) {
  3846. err = PTR_ERR(path);
  3847. path = NULL;
  3848. goto out2;
  3849. }
  3850. depth = ext_depth(inode);
  3851. /*
  3852. * consistent leaf must not be empty;
  3853. * this situation is possible, though, _during_ tree modification;
  3854. * this is why assert can't be put in ext4_find_extent()
  3855. */
  3856. if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
  3857. EXT4_ERROR_INODE(inode, "bad extent address "
  3858. "lblock: %lu, depth: %d pblock %lld",
  3859. (unsigned long) map->m_lblk, depth,
  3860. path[depth].p_block);
  3861. err = -EFSCORRUPTED;
  3862. goto out2;
  3863. }
  3864. ex = path[depth].p_ext;
  3865. if (ex) {
  3866. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3867. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3868. unsigned short ee_len;
  3869. /*
  3870. * unwritten extents are treated as holes, except that
  3871. * we split out initialized portions during a write.
  3872. */
  3873. ee_len = ext4_ext_get_actual_len(ex);
  3874. trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
  3875. /* if found extent covers block, simply return it */
  3876. if (in_range(map->m_lblk, ee_block, ee_len)) {
  3877. newblock = map->m_lblk - ee_block + ee_start;
  3878. /* number of remaining blocks in the extent */
  3879. allocated = ee_len - (map->m_lblk - ee_block);
  3880. ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
  3881. ee_block, ee_len, newblock);
  3882. /*
  3883. * If the extent is initialized check whether the
  3884. * caller wants to convert it to unwritten.
  3885. */
  3886. if ((!ext4_ext_is_unwritten(ex)) &&
  3887. (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
  3888. allocated = convert_initialized_extent(
  3889. handle, inode, map, &path,
  3890. allocated);
  3891. goto out2;
  3892. } else if (!ext4_ext_is_unwritten(ex))
  3893. goto out;
  3894. ret = ext4_ext_handle_unwritten_extents(
  3895. handle, inode, map, &path, flags,
  3896. allocated, newblock);
  3897. if (ret < 0)
  3898. err = ret;
  3899. else
  3900. allocated = ret;
  3901. goto out2;
  3902. }
  3903. }
  3904. /*
  3905. * requested block isn't allocated yet;
  3906. * we couldn't try to create block if create flag is zero
  3907. */
  3908. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3909. ext4_lblk_t hole_start, hole_len;
  3910. hole_start = map->m_lblk;
  3911. hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
  3912. /*
  3913. * put just found gap into cache to speed up
  3914. * subsequent requests
  3915. */
  3916. ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
  3917. /* Update hole_len to reflect hole size after map->m_lblk */
  3918. if (hole_start != map->m_lblk)
  3919. hole_len -= map->m_lblk - hole_start;
  3920. map->m_pblk = 0;
  3921. map->m_len = min_t(unsigned int, map->m_len, hole_len);
  3922. goto out2;
  3923. }
  3924. /*
  3925. * Okay, we need to do block allocation.
  3926. */
  3927. newex.ee_block = cpu_to_le32(map->m_lblk);
  3928. cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
  3929. /*
  3930. * If we are doing bigalloc, check to see if the extent returned
  3931. * by ext4_find_extent() implies a cluster we can use.
  3932. */
  3933. if (cluster_offset && ex &&
  3934. get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
  3935. ar.len = allocated = map->m_len;
  3936. newblock = map->m_pblk;
  3937. map_from_cluster = true;
  3938. goto got_allocated_blocks;
  3939. }
  3940. /* find neighbour allocated blocks */
  3941. ar.lleft = map->m_lblk;
  3942. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  3943. if (err)
  3944. goto out2;
  3945. ar.lright = map->m_lblk;
  3946. ex2 = NULL;
  3947. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
  3948. if (err)
  3949. goto out2;
  3950. /* Check if the extent after searching to the right implies a
  3951. * cluster we can use. */
  3952. if ((sbi->s_cluster_ratio > 1) && ex2 &&
  3953. get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
  3954. ar.len = allocated = map->m_len;
  3955. newblock = map->m_pblk;
  3956. map_from_cluster = true;
  3957. goto got_allocated_blocks;
  3958. }
  3959. /*
  3960. * See if request is beyond maximum number of blocks we can have in
  3961. * a single extent. For an initialized extent this limit is
  3962. * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
  3963. * EXT_UNWRITTEN_MAX_LEN.
  3964. */
  3965. if (map->m_len > EXT_INIT_MAX_LEN &&
  3966. !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
  3967. map->m_len = EXT_INIT_MAX_LEN;
  3968. else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
  3969. (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
  3970. map->m_len = EXT_UNWRITTEN_MAX_LEN;
  3971. /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
  3972. newex.ee_len = cpu_to_le16(map->m_len);
  3973. err = ext4_ext_check_overlap(sbi, inode, &newex, path);
  3974. if (err)
  3975. allocated = ext4_ext_get_actual_len(&newex);
  3976. else
  3977. allocated = map->m_len;
  3978. /* allocate new block */
  3979. ar.inode = inode;
  3980. ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
  3981. ar.logical = map->m_lblk;
  3982. /*
  3983. * We calculate the offset from the beginning of the cluster
  3984. * for the logical block number, since when we allocate a
  3985. * physical cluster, the physical block should start at the
  3986. * same offset from the beginning of the cluster. This is
  3987. * needed so that future calls to get_implied_cluster_alloc()
  3988. * work correctly.
  3989. */
  3990. offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
  3991. ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
  3992. ar.goal -= offset;
  3993. ar.logical -= offset;
  3994. if (S_ISREG(inode->i_mode))
  3995. ar.flags = EXT4_MB_HINT_DATA;
  3996. else
  3997. /* disable in-core preallocation for non-regular files */
  3998. ar.flags = 0;
  3999. if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
  4000. ar.flags |= EXT4_MB_HINT_NOPREALLOC;
  4001. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  4002. ar.flags |= EXT4_MB_DELALLOC_RESERVED;
  4003. if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
  4004. ar.flags |= EXT4_MB_USE_RESERVED;
  4005. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  4006. if (!newblock)
  4007. goto out2;
  4008. ext_debug("allocate new block: goal %llu, found %llu/%u\n",
  4009. ar.goal, newblock, allocated);
  4010. free_on_err = 1;
  4011. allocated_clusters = ar.len;
  4012. ar.len = EXT4_C2B(sbi, ar.len) - offset;
  4013. if (ar.len > allocated)
  4014. ar.len = allocated;
  4015. got_allocated_blocks:
  4016. /* try to insert new extent into found leaf and return */
  4017. ext4_ext_store_pblock(&newex, newblock + offset);
  4018. newex.ee_len = cpu_to_le16(ar.len);
  4019. /* Mark unwritten */
  4020. if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
  4021. ext4_ext_mark_unwritten(&newex);
  4022. map->m_flags |= EXT4_MAP_UNWRITTEN;
  4023. }
  4024. err = 0;
  4025. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
  4026. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  4027. path, ar.len);
  4028. if (!err)
  4029. err = ext4_ext_insert_extent(handle, inode, &path,
  4030. &newex, flags);
  4031. if (err && free_on_err) {
  4032. int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
  4033. EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
  4034. /* free data blocks we just allocated */
  4035. /* not a good idea to call discard here directly,
  4036. * but otherwise we'd need to call it every free() */
  4037. ext4_discard_preallocations(inode);
  4038. ext4_free_blocks(handle, inode, NULL, newblock,
  4039. EXT4_C2B(sbi, allocated_clusters), fb_flags);
  4040. goto out2;
  4041. }
  4042. /* previous routine could use block we allocated */
  4043. newblock = ext4_ext_pblock(&newex);
  4044. allocated = ext4_ext_get_actual_len(&newex);
  4045. if (allocated > map->m_len)
  4046. allocated = map->m_len;
  4047. map->m_flags |= EXT4_MAP_NEW;
  4048. /*
  4049. * Update reserved blocks/metadata blocks after successful
  4050. * block allocation which had been deferred till now.
  4051. */
  4052. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  4053. unsigned int reserved_clusters;
  4054. /*
  4055. * Check how many clusters we had reserved this allocated range
  4056. */
  4057. reserved_clusters = get_reserved_cluster_alloc(inode,
  4058. map->m_lblk, allocated);
  4059. if (!map_from_cluster) {
  4060. BUG_ON(allocated_clusters < reserved_clusters);
  4061. if (reserved_clusters < allocated_clusters) {
  4062. struct ext4_inode_info *ei = EXT4_I(inode);
  4063. int reservation = allocated_clusters -
  4064. reserved_clusters;
  4065. /*
  4066. * It seems we claimed few clusters outside of
  4067. * the range of this allocation. We should give
  4068. * it back to the reservation pool. This can
  4069. * happen in the following case:
  4070. *
  4071. * * Suppose s_cluster_ratio is 4 (i.e., each
  4072. * cluster has 4 blocks. Thus, the clusters
  4073. * are [0-3],[4-7],[8-11]...
  4074. * * First comes delayed allocation write for
  4075. * logical blocks 10 & 11. Since there were no
  4076. * previous delayed allocated blocks in the
  4077. * range [8-11], we would reserve 1 cluster
  4078. * for this write.
  4079. * * Next comes write for logical blocks 3 to 8.
  4080. * In this case, we will reserve 2 clusters
  4081. * (for [0-3] and [4-7]; and not for [8-11] as
  4082. * that range has a delayed allocated blocks.
  4083. * Thus total reserved clusters now becomes 3.
  4084. * * Now, during the delayed allocation writeout
  4085. * time, we will first write blocks [3-8] and
  4086. * allocate 3 clusters for writing these
  4087. * blocks. Also, we would claim all these
  4088. * three clusters above.
  4089. * * Now when we come here to writeout the
  4090. * blocks [10-11], we would expect to claim
  4091. * the reservation of 1 cluster we had made
  4092. * (and we would claim it since there are no
  4093. * more delayed allocated blocks in the range
  4094. * [8-11]. But our reserved cluster count had
  4095. * already gone to 0.
  4096. *
  4097. * Thus, at the step 4 above when we determine
  4098. * that there are still some unwritten delayed
  4099. * allocated blocks outside of our current
  4100. * block range, we should increment the
  4101. * reserved clusters count so that when the
  4102. * remaining blocks finally gets written, we
  4103. * could claim them.
  4104. */
  4105. dquot_reserve_block(inode,
  4106. EXT4_C2B(sbi, reservation));
  4107. spin_lock(&ei->i_block_reservation_lock);
  4108. ei->i_reserved_data_blocks += reservation;
  4109. spin_unlock(&ei->i_block_reservation_lock);
  4110. }
  4111. /*
  4112. * We will claim quota for all newly allocated blocks.
  4113. * We're updating the reserved space *after* the
  4114. * correction above so we do not accidentally free
  4115. * all the metadata reservation because we might
  4116. * actually need it later on.
  4117. */
  4118. ext4_da_update_reserve_space(inode, allocated_clusters,
  4119. 1);
  4120. }
  4121. }
  4122. /*
  4123. * Cache the extent and update transaction to commit on fdatasync only
  4124. * when it is _not_ an unwritten extent.
  4125. */
  4126. if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
  4127. ext4_update_inode_fsync_trans(handle, inode, 1);
  4128. else
  4129. ext4_update_inode_fsync_trans(handle, inode, 0);
  4130. out:
  4131. if (allocated > map->m_len)
  4132. allocated = map->m_len;
  4133. ext4_ext_show_leaf(inode, path);
  4134. map->m_flags |= EXT4_MAP_MAPPED;
  4135. map->m_pblk = newblock;
  4136. map->m_len = allocated;
  4137. out2:
  4138. ext4_ext_drop_refs(path);
  4139. kfree(path);
  4140. trace_ext4_ext_map_blocks_exit(inode, flags, map,
  4141. err ? err : allocated);
  4142. return err ? err : allocated;
  4143. }
  4144. void ext4_ext_truncate(handle_t *handle, struct inode *inode)
  4145. {
  4146. struct super_block *sb = inode->i_sb;
  4147. ext4_lblk_t last_block;
  4148. int err = 0;
  4149. /*
  4150. * TODO: optimization is possible here.
  4151. * Probably we need not scan at all,
  4152. * because page truncation is enough.
  4153. */
  4154. /* we have to know where to truncate from in crash case */
  4155. EXT4_I(inode)->i_disksize = inode->i_size;
  4156. ext4_mark_inode_dirty(handle, inode);
  4157. last_block = (inode->i_size + sb->s_blocksize - 1)
  4158. >> EXT4_BLOCK_SIZE_BITS(sb);
  4159. retry:
  4160. err = ext4_es_remove_extent(inode, last_block,
  4161. EXT_MAX_BLOCKS - last_block);
  4162. if (err == -ENOMEM) {
  4163. cond_resched();
  4164. congestion_wait(BLK_RW_ASYNC, HZ/50);
  4165. goto retry;
  4166. }
  4167. if (err) {
  4168. ext4_std_error(inode->i_sb, err);
  4169. return;
  4170. }
  4171. err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
  4172. ext4_std_error(inode->i_sb, err);
  4173. }
  4174. static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
  4175. ext4_lblk_t len, loff_t new_size,
  4176. int flags, int mode)
  4177. {
  4178. struct inode *inode = file_inode(file);
  4179. handle_t *handle;
  4180. int ret = 0;
  4181. int ret2 = 0;
  4182. int retries = 0;
  4183. int depth = 0;
  4184. struct ext4_map_blocks map;
  4185. unsigned int credits;
  4186. loff_t epos;
  4187. BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
  4188. map.m_lblk = offset;
  4189. map.m_len = len;
  4190. /*
  4191. * Don't normalize the request if it can fit in one extent so
  4192. * that it doesn't get unnecessarily split into multiple
  4193. * extents.
  4194. */
  4195. if (len <= EXT_UNWRITTEN_MAX_LEN)
  4196. flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
  4197. /*
  4198. * credits to insert 1 extent into extent tree
  4199. */
  4200. credits = ext4_chunk_trans_blocks(inode, len);
  4201. depth = ext_depth(inode);
  4202. retry:
  4203. while (ret >= 0 && len) {
  4204. /*
  4205. * Recalculate credits when extent tree depth changes.
  4206. */
  4207. if (depth >= 0 && depth != ext_depth(inode)) {
  4208. credits = ext4_chunk_trans_blocks(inode, len);
  4209. depth = ext_depth(inode);
  4210. }
  4211. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
  4212. credits);
  4213. if (IS_ERR(handle)) {
  4214. ret = PTR_ERR(handle);
  4215. break;
  4216. }
  4217. ret = ext4_map_blocks(handle, inode, &map, flags);
  4218. if (ret <= 0) {
  4219. ext4_debug("inode #%lu: block %u: len %u: "
  4220. "ext4_ext_map_blocks returned %d",
  4221. inode->i_ino, map.m_lblk,
  4222. map.m_len, ret);
  4223. ext4_mark_inode_dirty(handle, inode);
  4224. ret2 = ext4_journal_stop(handle);
  4225. break;
  4226. }
  4227. map.m_lblk += ret;
  4228. map.m_len = len = len - ret;
  4229. epos = (loff_t)map.m_lblk << inode->i_blkbits;
  4230. inode->i_ctime = ext4_current_time(inode);
  4231. if (new_size) {
  4232. if (epos > new_size)
  4233. epos = new_size;
  4234. if (ext4_update_inode_size(inode, epos) & 0x1)
  4235. inode->i_mtime = inode->i_ctime;
  4236. } else {
  4237. if (epos > inode->i_size)
  4238. ext4_set_inode_flag(inode,
  4239. EXT4_INODE_EOFBLOCKS);
  4240. }
  4241. ext4_mark_inode_dirty(handle, inode);
  4242. ext4_update_inode_fsync_trans(handle, inode, 1);
  4243. ret2 = ext4_journal_stop(handle);
  4244. if (ret2)
  4245. break;
  4246. }
  4247. if (ret == -ENOSPC &&
  4248. ext4_should_retry_alloc(inode->i_sb, &retries)) {
  4249. ret = 0;
  4250. goto retry;
  4251. }
  4252. return ret > 0 ? ret2 : ret;
  4253. }
  4254. static long ext4_zero_range(struct file *file, loff_t offset,
  4255. loff_t len, int mode)
  4256. {
  4257. struct inode *inode = file_inode(file);
  4258. handle_t *handle = NULL;
  4259. unsigned int max_blocks;
  4260. loff_t new_size = 0;
  4261. int ret = 0;
  4262. int flags;
  4263. int credits;
  4264. int partial_begin, partial_end;
  4265. loff_t start, end;
  4266. ext4_lblk_t lblk;
  4267. unsigned int blkbits = inode->i_blkbits;
  4268. trace_ext4_zero_range(inode, offset, len, mode);
  4269. if (!S_ISREG(inode->i_mode))
  4270. return -EINVAL;
  4271. /* Call ext4_force_commit to flush all data in case of data=journal. */
  4272. if (ext4_should_journal_data(inode)) {
  4273. ret = ext4_force_commit(inode->i_sb);
  4274. if (ret)
  4275. return ret;
  4276. }
  4277. /*
  4278. * Round up offset. This is not fallocate, we neet to zero out
  4279. * blocks, so convert interior block aligned part of the range to
  4280. * unwritten and possibly manually zero out unaligned parts of the
  4281. * range.
  4282. */
  4283. start = round_up(offset, 1 << blkbits);
  4284. end = round_down((offset + len), 1 << blkbits);
  4285. if (start < offset || end > offset + len)
  4286. return -EINVAL;
  4287. partial_begin = offset & ((1 << blkbits) - 1);
  4288. partial_end = (offset + len) & ((1 << blkbits) - 1);
  4289. lblk = start >> blkbits;
  4290. max_blocks = (end >> blkbits);
  4291. if (max_blocks < lblk)
  4292. max_blocks = 0;
  4293. else
  4294. max_blocks -= lblk;
  4295. inode_lock(inode);
  4296. /*
  4297. * Indirect files do not support unwritten extnets
  4298. */
  4299. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  4300. ret = -EOPNOTSUPP;
  4301. goto out_mutex;
  4302. }
  4303. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4304. (offset + len > i_size_read(inode) ||
  4305. offset + len > EXT4_I(inode)->i_disksize)) {
  4306. new_size = offset + len;
  4307. ret = inode_newsize_ok(inode, new_size);
  4308. if (ret)
  4309. goto out_mutex;
  4310. }
  4311. flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
  4312. if (mode & FALLOC_FL_KEEP_SIZE)
  4313. flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
  4314. /* Wait all existing dio workers, newcomers will block on i_mutex */
  4315. ext4_inode_block_unlocked_dio(inode);
  4316. inode_dio_wait(inode);
  4317. /* Preallocate the range including the unaligned edges */
  4318. if (partial_begin || partial_end) {
  4319. ret = ext4_alloc_file_blocks(file,
  4320. round_down(offset, 1 << blkbits) >> blkbits,
  4321. (round_up((offset + len), 1 << blkbits) -
  4322. round_down(offset, 1 << blkbits)) >> blkbits,
  4323. new_size, flags, mode);
  4324. if (ret)
  4325. goto out_dio;
  4326. }
  4327. /* Zero range excluding the unaligned edges */
  4328. if (max_blocks > 0) {
  4329. flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
  4330. EXT4_EX_NOCACHE);
  4331. /*
  4332. * Prevent page faults from reinstantiating pages we have
  4333. * released from page cache.
  4334. */
  4335. down_write(&EXT4_I(inode)->i_mmap_sem);
  4336. ret = ext4_update_disksize_before_punch(inode, offset, len);
  4337. if (ret) {
  4338. up_write(&EXT4_I(inode)->i_mmap_sem);
  4339. goto out_dio;
  4340. }
  4341. /* Now release the pages and zero block aligned part of pages */
  4342. truncate_pagecache_range(inode, start, end - 1);
  4343. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  4344. ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
  4345. flags, mode);
  4346. up_write(&EXT4_I(inode)->i_mmap_sem);
  4347. if (ret)
  4348. goto out_dio;
  4349. }
  4350. if (!partial_begin && !partial_end)
  4351. goto out_dio;
  4352. /*
  4353. * In worst case we have to writeout two nonadjacent unwritten
  4354. * blocks and update the inode
  4355. */
  4356. credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
  4357. if (ext4_should_journal_data(inode))
  4358. credits += 2;
  4359. handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
  4360. if (IS_ERR(handle)) {
  4361. ret = PTR_ERR(handle);
  4362. ext4_std_error(inode->i_sb, ret);
  4363. goto out_dio;
  4364. }
  4365. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  4366. if (new_size) {
  4367. ext4_update_inode_size(inode, new_size);
  4368. } else {
  4369. /*
  4370. * Mark that we allocate beyond EOF so the subsequent truncate
  4371. * can proceed even if the new size is the same as i_size.
  4372. */
  4373. if ((offset + len) > i_size_read(inode))
  4374. ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  4375. }
  4376. ext4_mark_inode_dirty(handle, inode);
  4377. /* Zero out partial block at the edges of the range */
  4378. ret = ext4_zero_partial_blocks(handle, inode, offset, len);
  4379. if (ret >= 0)
  4380. ext4_update_inode_fsync_trans(handle, inode, 1);
  4381. if (file->f_flags & O_SYNC)
  4382. ext4_handle_sync(handle);
  4383. ext4_journal_stop(handle);
  4384. out_dio:
  4385. ext4_inode_resume_unlocked_dio(inode);
  4386. out_mutex:
  4387. inode_unlock(inode);
  4388. return ret;
  4389. }
  4390. /*
  4391. * preallocate space for a file. This implements ext4's fallocate file
  4392. * operation, which gets called from sys_fallocate system call.
  4393. * For block-mapped files, posix_fallocate should fall back to the method
  4394. * of writing zeroes to the required new blocks (the same behavior which is
  4395. * expected for file systems which do not support fallocate() system call).
  4396. */
  4397. long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  4398. {
  4399. struct inode *inode = file_inode(file);
  4400. loff_t new_size = 0;
  4401. unsigned int max_blocks;
  4402. int ret = 0;
  4403. int flags;
  4404. ext4_lblk_t lblk;
  4405. unsigned int blkbits = inode->i_blkbits;
  4406. /*
  4407. * Encrypted inodes can't handle collapse range or insert
  4408. * range since we would need to re-encrypt blocks with a
  4409. * different IV or XTS tweak (which are based on the logical
  4410. * block number).
  4411. *
  4412. * XXX It's not clear why zero range isn't working, but we'll
  4413. * leave it disabled for encrypted inodes for now. This is a
  4414. * bug we should fix....
  4415. */
  4416. if (ext4_encrypted_inode(inode) &&
  4417. (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
  4418. FALLOC_FL_ZERO_RANGE)))
  4419. return -EOPNOTSUPP;
  4420. /* Return error if mode is not supported */
  4421. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  4422. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
  4423. FALLOC_FL_INSERT_RANGE))
  4424. return -EOPNOTSUPP;
  4425. if (mode & FALLOC_FL_PUNCH_HOLE)
  4426. return ext4_punch_hole(inode, offset, len);
  4427. ret = ext4_convert_inline_data(inode);
  4428. if (ret)
  4429. return ret;
  4430. if (mode & FALLOC_FL_COLLAPSE_RANGE)
  4431. return ext4_collapse_range(inode, offset, len);
  4432. if (mode & FALLOC_FL_INSERT_RANGE)
  4433. return ext4_insert_range(inode, offset, len);
  4434. if (mode & FALLOC_FL_ZERO_RANGE)
  4435. return ext4_zero_range(file, offset, len, mode);
  4436. trace_ext4_fallocate_enter(inode, offset, len, mode);
  4437. lblk = offset >> blkbits;
  4438. max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
  4439. flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
  4440. if (mode & FALLOC_FL_KEEP_SIZE)
  4441. flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
  4442. inode_lock(inode);
  4443. /*
  4444. * We only support preallocation for extent-based files only
  4445. */
  4446. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  4447. ret = -EOPNOTSUPP;
  4448. goto out;
  4449. }
  4450. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4451. (offset + len > i_size_read(inode) ||
  4452. offset + len > EXT4_I(inode)->i_disksize)) {
  4453. new_size = offset + len;
  4454. ret = inode_newsize_ok(inode, new_size);
  4455. if (ret)
  4456. goto out;
  4457. }
  4458. /* Wait all existing dio workers, newcomers will block on i_mutex */
  4459. ext4_inode_block_unlocked_dio(inode);
  4460. inode_dio_wait(inode);
  4461. ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
  4462. flags, mode);
  4463. ext4_inode_resume_unlocked_dio(inode);
  4464. if (ret)
  4465. goto out;
  4466. if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
  4467. ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
  4468. EXT4_I(inode)->i_sync_tid);
  4469. }
  4470. out:
  4471. inode_unlock(inode);
  4472. trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
  4473. return ret;
  4474. }
  4475. /*
  4476. * This function convert a range of blocks to written extents
  4477. * The caller of this function will pass the start offset and the size.
  4478. * all unwritten extents within this range will be converted to
  4479. * written extents.
  4480. *
  4481. * This function is called from the direct IO end io call back
  4482. * function, to convert the fallocated extents after IO is completed.
  4483. * Returns 0 on success.
  4484. */
  4485. int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
  4486. loff_t offset, ssize_t len)
  4487. {
  4488. unsigned int max_blocks;
  4489. int ret = 0;
  4490. int ret2 = 0;
  4491. struct ext4_map_blocks map;
  4492. unsigned int credits, blkbits = inode->i_blkbits;
  4493. map.m_lblk = offset >> blkbits;
  4494. max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
  4495. /*
  4496. * This is somewhat ugly but the idea is clear: When transaction is
  4497. * reserved, everything goes into it. Otherwise we rather start several
  4498. * smaller transactions for conversion of each extent separately.
  4499. */
  4500. if (handle) {
  4501. handle = ext4_journal_start_reserved(handle,
  4502. EXT4_HT_EXT_CONVERT);
  4503. if (IS_ERR(handle))
  4504. return PTR_ERR(handle);
  4505. credits = 0;
  4506. } else {
  4507. /*
  4508. * credits to insert 1 extent into extent tree
  4509. */
  4510. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  4511. }
  4512. while (ret >= 0 && ret < max_blocks) {
  4513. map.m_lblk += ret;
  4514. map.m_len = (max_blocks -= ret);
  4515. if (credits) {
  4516. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
  4517. credits);
  4518. if (IS_ERR(handle)) {
  4519. ret = PTR_ERR(handle);
  4520. break;
  4521. }
  4522. }
  4523. ret = ext4_map_blocks(handle, inode, &map,
  4524. EXT4_GET_BLOCKS_IO_CONVERT_EXT);
  4525. if (ret <= 0)
  4526. ext4_warning(inode->i_sb,
  4527. "inode #%lu: block %u: len %u: "
  4528. "ext4_ext_map_blocks returned %d",
  4529. inode->i_ino, map.m_lblk,
  4530. map.m_len, ret);
  4531. ext4_mark_inode_dirty(handle, inode);
  4532. if (credits)
  4533. ret2 = ext4_journal_stop(handle);
  4534. if (ret <= 0 || ret2)
  4535. break;
  4536. }
  4537. if (!credits)
  4538. ret2 = ext4_journal_stop(handle);
  4539. return ret > 0 ? ret2 : ret;
  4540. }
  4541. /*
  4542. * If newes is not existing extent (newes->ec_pblk equals zero) find
  4543. * delayed extent at start of newes and update newes accordingly and
  4544. * return start of the next delayed extent.
  4545. *
  4546. * If newes is existing extent (newes->ec_pblk is not equal zero)
  4547. * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
  4548. * extent found. Leave newes unmodified.
  4549. */
  4550. static int ext4_find_delayed_extent(struct inode *inode,
  4551. struct extent_status *newes)
  4552. {
  4553. struct extent_status es;
  4554. ext4_lblk_t block, next_del;
  4555. if (newes->es_pblk == 0) {
  4556. ext4_es_find_delayed_extent_range(inode, newes->es_lblk,
  4557. newes->es_lblk + newes->es_len - 1, &es);
  4558. /*
  4559. * No extent in extent-tree contains block @newes->es_pblk,
  4560. * then the block may stay in 1)a hole or 2)delayed-extent.
  4561. */
  4562. if (es.es_len == 0)
  4563. /* A hole found. */
  4564. return 0;
  4565. if (es.es_lblk > newes->es_lblk) {
  4566. /* A hole found. */
  4567. newes->es_len = min(es.es_lblk - newes->es_lblk,
  4568. newes->es_len);
  4569. return 0;
  4570. }
  4571. newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
  4572. }
  4573. block = newes->es_lblk + newes->es_len;
  4574. ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es);
  4575. if (es.es_len == 0)
  4576. next_del = EXT_MAX_BLOCKS;
  4577. else
  4578. next_del = es.es_lblk;
  4579. return next_del;
  4580. }
  4581. /* fiemap flags we can handle specified here */
  4582. #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  4583. static int ext4_xattr_fiemap(struct inode *inode,
  4584. struct fiemap_extent_info *fieinfo)
  4585. {
  4586. __u64 physical = 0;
  4587. __u64 length;
  4588. __u32 flags = FIEMAP_EXTENT_LAST;
  4589. int blockbits = inode->i_sb->s_blocksize_bits;
  4590. int error = 0;
  4591. /* in-inode? */
  4592. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  4593. struct ext4_iloc iloc;
  4594. int offset; /* offset of xattr in inode */
  4595. error = ext4_get_inode_loc(inode, &iloc);
  4596. if (error)
  4597. return error;
  4598. physical = (__u64)iloc.bh->b_blocknr << blockbits;
  4599. offset = EXT4_GOOD_OLD_INODE_SIZE +
  4600. EXT4_I(inode)->i_extra_isize;
  4601. physical += offset;
  4602. length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
  4603. flags |= FIEMAP_EXTENT_DATA_INLINE;
  4604. brelse(iloc.bh);
  4605. } else { /* external block */
  4606. physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
  4607. length = inode->i_sb->s_blocksize;
  4608. }
  4609. if (physical)
  4610. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  4611. length, flags);
  4612. return (error < 0 ? error : 0);
  4613. }
  4614. int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4615. __u64 start, __u64 len)
  4616. {
  4617. ext4_lblk_t start_blk;
  4618. int error = 0;
  4619. if (ext4_has_inline_data(inode)) {
  4620. int has_inline = 1;
  4621. error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
  4622. start, len);
  4623. if (has_inline)
  4624. return error;
  4625. }
  4626. if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
  4627. error = ext4_ext_precache(inode);
  4628. if (error)
  4629. return error;
  4630. }
  4631. /* fallback to generic here if not in extents fmt */
  4632. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  4633. return generic_block_fiemap(inode, fieinfo, start, len,
  4634. ext4_get_block);
  4635. if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
  4636. return -EBADR;
  4637. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  4638. error = ext4_xattr_fiemap(inode, fieinfo);
  4639. } else {
  4640. ext4_lblk_t len_blks;
  4641. __u64 last_blk;
  4642. start_blk = start >> inode->i_sb->s_blocksize_bits;
  4643. last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
  4644. if (last_blk >= EXT_MAX_BLOCKS)
  4645. last_blk = EXT_MAX_BLOCKS-1;
  4646. len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
  4647. /*
  4648. * Walk the extent tree gathering extent information
  4649. * and pushing extents back to the user.
  4650. */
  4651. error = ext4_fill_fiemap_extents(inode, start_blk,
  4652. len_blks, fieinfo);
  4653. }
  4654. return error;
  4655. }
  4656. /*
  4657. * ext4_access_path:
  4658. * Function to access the path buffer for marking it dirty.
  4659. * It also checks if there are sufficient credits left in the journal handle
  4660. * to update path.
  4661. */
  4662. static int
  4663. ext4_access_path(handle_t *handle, struct inode *inode,
  4664. struct ext4_ext_path *path)
  4665. {
  4666. int credits, err;
  4667. if (!ext4_handle_valid(handle))
  4668. return 0;
  4669. /*
  4670. * Check if need to extend journal credits
  4671. * 3 for leaf, sb, and inode plus 2 (bmap and group
  4672. * descriptor) for each block group; assume two block
  4673. * groups
  4674. */
  4675. if (handle->h_buffer_credits < 7) {
  4676. credits = ext4_writepage_trans_blocks(inode);
  4677. err = ext4_ext_truncate_extend_restart(handle, inode, credits);
  4678. /* EAGAIN is success */
  4679. if (err && err != -EAGAIN)
  4680. return err;
  4681. }
  4682. err = ext4_ext_get_access(handle, inode, path);
  4683. return err;
  4684. }
  4685. /*
  4686. * ext4_ext_shift_path_extents:
  4687. * Shift the extents of a path structure lying between path[depth].p_ext
  4688. * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
  4689. * if it is right shift or left shift operation.
  4690. */
  4691. static int
  4692. ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
  4693. struct inode *inode, handle_t *handle,
  4694. enum SHIFT_DIRECTION SHIFT)
  4695. {
  4696. int depth, err = 0;
  4697. struct ext4_extent *ex_start, *ex_last;
  4698. bool update = 0;
  4699. depth = path->p_depth;
  4700. while (depth >= 0) {
  4701. if (depth == path->p_depth) {
  4702. ex_start = path[depth].p_ext;
  4703. if (!ex_start)
  4704. return -EFSCORRUPTED;
  4705. ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
  4706. err = ext4_access_path(handle, inode, path + depth);
  4707. if (err)
  4708. goto out;
  4709. if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
  4710. update = 1;
  4711. while (ex_start <= ex_last) {
  4712. if (SHIFT == SHIFT_LEFT) {
  4713. le32_add_cpu(&ex_start->ee_block,
  4714. -shift);
  4715. /* Try to merge to the left. */
  4716. if ((ex_start >
  4717. EXT_FIRST_EXTENT(path[depth].p_hdr))
  4718. &&
  4719. ext4_ext_try_to_merge_right(inode,
  4720. path, ex_start - 1))
  4721. ex_last--;
  4722. else
  4723. ex_start++;
  4724. } else {
  4725. le32_add_cpu(&ex_last->ee_block, shift);
  4726. ext4_ext_try_to_merge_right(inode, path,
  4727. ex_last);
  4728. ex_last--;
  4729. }
  4730. }
  4731. err = ext4_ext_dirty(handle, inode, path + depth);
  4732. if (err)
  4733. goto out;
  4734. if (--depth < 0 || !update)
  4735. break;
  4736. }
  4737. /* Update index too */
  4738. err = ext4_access_path(handle, inode, path + depth);
  4739. if (err)
  4740. goto out;
  4741. if (SHIFT == SHIFT_LEFT)
  4742. le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
  4743. else
  4744. le32_add_cpu(&path[depth].p_idx->ei_block, shift);
  4745. err = ext4_ext_dirty(handle, inode, path + depth);
  4746. if (err)
  4747. goto out;
  4748. /* we are done if current index is not a starting index */
  4749. if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
  4750. break;
  4751. depth--;
  4752. }
  4753. out:
  4754. return err;
  4755. }
  4756. /*
  4757. * ext4_ext_shift_extents:
  4758. * All the extents which lies in the range from @start to the last allocated
  4759. * block for the @inode are shifted either towards left or right (depending
  4760. * upon @SHIFT) by @shift blocks.
  4761. * On success, 0 is returned, error otherwise.
  4762. */
  4763. static int
  4764. ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
  4765. ext4_lblk_t start, ext4_lblk_t shift,
  4766. enum SHIFT_DIRECTION SHIFT)
  4767. {
  4768. struct ext4_ext_path *path;
  4769. int ret = 0, depth;
  4770. struct ext4_extent *extent;
  4771. ext4_lblk_t stop, *iterator, ex_start, ex_end;
  4772. /* Let path point to the last extent */
  4773. path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
  4774. EXT4_EX_NOCACHE);
  4775. if (IS_ERR(path))
  4776. return PTR_ERR(path);
  4777. depth = path->p_depth;
  4778. extent = path[depth].p_ext;
  4779. if (!extent)
  4780. goto out;
  4781. stop = le32_to_cpu(extent->ee_block);
  4782. /*
  4783. * For left shifts, make sure the hole on the left is big enough to
  4784. * accommodate the shift. For right shifts, make sure the last extent
  4785. * won't be shifted beyond EXT_MAX_BLOCKS.
  4786. */
  4787. if (SHIFT == SHIFT_LEFT) {
  4788. path = ext4_find_extent(inode, start - 1, &path,
  4789. EXT4_EX_NOCACHE);
  4790. if (IS_ERR(path))
  4791. return PTR_ERR(path);
  4792. depth = path->p_depth;
  4793. extent = path[depth].p_ext;
  4794. if (extent) {
  4795. ex_start = le32_to_cpu(extent->ee_block);
  4796. ex_end = le32_to_cpu(extent->ee_block) +
  4797. ext4_ext_get_actual_len(extent);
  4798. } else {
  4799. ex_start = 0;
  4800. ex_end = 0;
  4801. }
  4802. if ((start == ex_start && shift > ex_start) ||
  4803. (shift > start - ex_end)) {
  4804. ret = -EINVAL;
  4805. goto out;
  4806. }
  4807. } else {
  4808. if (shift > EXT_MAX_BLOCKS -
  4809. (stop + ext4_ext_get_actual_len(extent))) {
  4810. ret = -EINVAL;
  4811. goto out;
  4812. }
  4813. }
  4814. /*
  4815. * In case of left shift, iterator points to start and it is increased
  4816. * till we reach stop. In case of right shift, iterator points to stop
  4817. * and it is decreased till we reach start.
  4818. */
  4819. if (SHIFT == SHIFT_LEFT)
  4820. iterator = &start;
  4821. else
  4822. iterator = &stop;
  4823. /*
  4824. * Its safe to start updating extents. Start and stop are unsigned, so
  4825. * in case of right shift if extent with 0 block is reached, iterator
  4826. * becomes NULL to indicate the end of the loop.
  4827. */
  4828. while (iterator && start <= stop) {
  4829. path = ext4_find_extent(inode, *iterator, &path,
  4830. EXT4_EX_NOCACHE);
  4831. if (IS_ERR(path))
  4832. return PTR_ERR(path);
  4833. depth = path->p_depth;
  4834. extent = path[depth].p_ext;
  4835. if (!extent) {
  4836. EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
  4837. (unsigned long) *iterator);
  4838. return -EFSCORRUPTED;
  4839. }
  4840. if (SHIFT == SHIFT_LEFT && *iterator >
  4841. le32_to_cpu(extent->ee_block)) {
  4842. /* Hole, move to the next extent */
  4843. if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
  4844. path[depth].p_ext++;
  4845. } else {
  4846. *iterator = ext4_ext_next_allocated_block(path);
  4847. continue;
  4848. }
  4849. }
  4850. if (SHIFT == SHIFT_LEFT) {
  4851. extent = EXT_LAST_EXTENT(path[depth].p_hdr);
  4852. *iterator = le32_to_cpu(extent->ee_block) +
  4853. ext4_ext_get_actual_len(extent);
  4854. } else {
  4855. extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
  4856. if (le32_to_cpu(extent->ee_block) > 0)
  4857. *iterator = le32_to_cpu(extent->ee_block) - 1;
  4858. else
  4859. /* Beginning is reached, end of the loop */
  4860. iterator = NULL;
  4861. /* Update path extent in case we need to stop */
  4862. while (le32_to_cpu(extent->ee_block) < start)
  4863. extent++;
  4864. path[depth].p_ext = extent;
  4865. }
  4866. ret = ext4_ext_shift_path_extents(path, shift, inode,
  4867. handle, SHIFT);
  4868. if (ret)
  4869. break;
  4870. }
  4871. out:
  4872. ext4_ext_drop_refs(path);
  4873. kfree(path);
  4874. return ret;
  4875. }
  4876. /*
  4877. * ext4_collapse_range:
  4878. * This implements the fallocate's collapse range functionality for ext4
  4879. * Returns: 0 and non-zero on error.
  4880. */
  4881. int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
  4882. {
  4883. struct super_block *sb = inode->i_sb;
  4884. ext4_lblk_t punch_start, punch_stop;
  4885. handle_t *handle;
  4886. unsigned int credits;
  4887. loff_t new_size, ioffset;
  4888. int ret;
  4889. /*
  4890. * We need to test this early because xfstests assumes that a
  4891. * collapse range of (0, 1) will return EOPNOTSUPP if the file
  4892. * system does not support collapse range.
  4893. */
  4894. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  4895. return -EOPNOTSUPP;
  4896. /* Collapse range works only on fs block size aligned offsets. */
  4897. if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
  4898. len & (EXT4_CLUSTER_SIZE(sb) - 1))
  4899. return -EINVAL;
  4900. if (!S_ISREG(inode->i_mode))
  4901. return -EINVAL;
  4902. trace_ext4_collapse_range(inode, offset, len);
  4903. punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  4904. punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
  4905. /* Call ext4_force_commit to flush all data in case of data=journal. */
  4906. if (ext4_should_journal_data(inode)) {
  4907. ret = ext4_force_commit(inode->i_sb);
  4908. if (ret)
  4909. return ret;
  4910. }
  4911. inode_lock(inode);
  4912. /*
  4913. * There is no need to overlap collapse range with EOF, in which case
  4914. * it is effectively a truncate operation
  4915. */
  4916. if (offset + len >= i_size_read(inode)) {
  4917. ret = -EINVAL;
  4918. goto out_mutex;
  4919. }
  4920. /* Currently just for extent based files */
  4921. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  4922. ret = -EOPNOTSUPP;
  4923. goto out_mutex;
  4924. }
  4925. /* Wait for existing dio to complete */
  4926. ext4_inode_block_unlocked_dio(inode);
  4927. inode_dio_wait(inode);
  4928. /*
  4929. * Prevent page faults from reinstantiating pages we have released from
  4930. * page cache.
  4931. */
  4932. down_write(&EXT4_I(inode)->i_mmap_sem);
  4933. /*
  4934. * Need to round down offset to be aligned with page size boundary
  4935. * for page size > block size.
  4936. */
  4937. ioffset = round_down(offset, PAGE_SIZE);
  4938. /*
  4939. * Write tail of the last page before removed range since it will get
  4940. * removed from the page cache below.
  4941. */
  4942. ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
  4943. if (ret)
  4944. goto out_mmap;
  4945. /*
  4946. * Write data that will be shifted to preserve them when discarding
  4947. * page cache below. We are also protected from pages becoming dirty
  4948. * by i_mmap_sem.
  4949. */
  4950. ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
  4951. LLONG_MAX);
  4952. if (ret)
  4953. goto out_mmap;
  4954. truncate_pagecache(inode, ioffset);
  4955. credits = ext4_writepage_trans_blocks(inode);
  4956. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  4957. if (IS_ERR(handle)) {
  4958. ret = PTR_ERR(handle);
  4959. goto out_mmap;
  4960. }
  4961. down_write(&EXT4_I(inode)->i_data_sem);
  4962. ext4_discard_preallocations(inode);
  4963. ret = ext4_es_remove_extent(inode, punch_start,
  4964. EXT_MAX_BLOCKS - punch_start);
  4965. if (ret) {
  4966. up_write(&EXT4_I(inode)->i_data_sem);
  4967. goto out_stop;
  4968. }
  4969. ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
  4970. if (ret) {
  4971. up_write(&EXT4_I(inode)->i_data_sem);
  4972. goto out_stop;
  4973. }
  4974. ext4_discard_preallocations(inode);
  4975. ret = ext4_ext_shift_extents(inode, handle, punch_stop,
  4976. punch_stop - punch_start, SHIFT_LEFT);
  4977. if (ret) {
  4978. up_write(&EXT4_I(inode)->i_data_sem);
  4979. goto out_stop;
  4980. }
  4981. new_size = i_size_read(inode) - len;
  4982. i_size_write(inode, new_size);
  4983. EXT4_I(inode)->i_disksize = new_size;
  4984. up_write(&EXT4_I(inode)->i_data_sem);
  4985. if (IS_SYNC(inode))
  4986. ext4_handle_sync(handle);
  4987. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  4988. ext4_mark_inode_dirty(handle, inode);
  4989. ext4_update_inode_fsync_trans(handle, inode, 1);
  4990. out_stop:
  4991. ext4_journal_stop(handle);
  4992. out_mmap:
  4993. up_write(&EXT4_I(inode)->i_mmap_sem);
  4994. ext4_inode_resume_unlocked_dio(inode);
  4995. out_mutex:
  4996. inode_unlock(inode);
  4997. return ret;
  4998. }
  4999. /*
  5000. * ext4_insert_range:
  5001. * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
  5002. * The data blocks starting from @offset to the EOF are shifted by @len
  5003. * towards right to create a hole in the @inode. Inode size is increased
  5004. * by len bytes.
  5005. * Returns 0 on success, error otherwise.
  5006. */
  5007. int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
  5008. {
  5009. struct super_block *sb = inode->i_sb;
  5010. handle_t *handle;
  5011. struct ext4_ext_path *path;
  5012. struct ext4_extent *extent;
  5013. ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
  5014. unsigned int credits, ee_len;
  5015. int ret = 0, depth, split_flag = 0;
  5016. loff_t ioffset;
  5017. /*
  5018. * We need to test this early because xfstests assumes that an
  5019. * insert range of (0, 1) will return EOPNOTSUPP if the file
  5020. * system does not support insert range.
  5021. */
  5022. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  5023. return -EOPNOTSUPP;
  5024. /* Insert range works only on fs block size aligned offsets. */
  5025. if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
  5026. len & (EXT4_CLUSTER_SIZE(sb) - 1))
  5027. return -EINVAL;
  5028. if (!S_ISREG(inode->i_mode))
  5029. return -EOPNOTSUPP;
  5030. trace_ext4_insert_range(inode, offset, len);
  5031. offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  5032. len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
  5033. /* Call ext4_force_commit to flush all data in case of data=journal */
  5034. if (ext4_should_journal_data(inode)) {
  5035. ret = ext4_force_commit(inode->i_sb);
  5036. if (ret)
  5037. return ret;
  5038. }
  5039. inode_lock(inode);
  5040. /* Currently just for extent based files */
  5041. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  5042. ret = -EOPNOTSUPP;
  5043. goto out_mutex;
  5044. }
  5045. /* Check for wrap through zero */
  5046. if (inode->i_size + len > inode->i_sb->s_maxbytes) {
  5047. ret = -EFBIG;
  5048. goto out_mutex;
  5049. }
  5050. /* Offset should be less than i_size */
  5051. if (offset >= i_size_read(inode)) {
  5052. ret = -EINVAL;
  5053. goto out_mutex;
  5054. }
  5055. /* Wait for existing dio to complete */
  5056. ext4_inode_block_unlocked_dio(inode);
  5057. inode_dio_wait(inode);
  5058. /*
  5059. * Prevent page faults from reinstantiating pages we have released from
  5060. * page cache.
  5061. */
  5062. down_write(&EXT4_I(inode)->i_mmap_sem);
  5063. /*
  5064. * Need to round down to align start offset to page size boundary
  5065. * for page size > block size.
  5066. */
  5067. ioffset = round_down(offset, PAGE_SIZE);
  5068. /* Write out all dirty pages */
  5069. ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
  5070. LLONG_MAX);
  5071. if (ret)
  5072. goto out_mmap;
  5073. truncate_pagecache(inode, ioffset);
  5074. credits = ext4_writepage_trans_blocks(inode);
  5075. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  5076. if (IS_ERR(handle)) {
  5077. ret = PTR_ERR(handle);
  5078. goto out_mmap;
  5079. }
  5080. /* Expand file to avoid data loss if there is error while shifting */
  5081. inode->i_size += len;
  5082. EXT4_I(inode)->i_disksize += len;
  5083. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  5084. ret = ext4_mark_inode_dirty(handle, inode);
  5085. if (ret)
  5086. goto out_stop;
  5087. down_write(&EXT4_I(inode)->i_data_sem);
  5088. ext4_discard_preallocations(inode);
  5089. path = ext4_find_extent(inode, offset_lblk, NULL, 0);
  5090. if (IS_ERR(path)) {
  5091. up_write(&EXT4_I(inode)->i_data_sem);
  5092. goto out_stop;
  5093. }
  5094. depth = ext_depth(inode);
  5095. extent = path[depth].p_ext;
  5096. if (extent) {
  5097. ee_start_lblk = le32_to_cpu(extent->ee_block);
  5098. ee_len = ext4_ext_get_actual_len(extent);
  5099. /*
  5100. * If offset_lblk is not the starting block of extent, split
  5101. * the extent @offset_lblk
  5102. */
  5103. if ((offset_lblk > ee_start_lblk) &&
  5104. (offset_lblk < (ee_start_lblk + ee_len))) {
  5105. if (ext4_ext_is_unwritten(extent))
  5106. split_flag = EXT4_EXT_MARK_UNWRIT1 |
  5107. EXT4_EXT_MARK_UNWRIT2;
  5108. ret = ext4_split_extent_at(handle, inode, &path,
  5109. offset_lblk, split_flag,
  5110. EXT4_EX_NOCACHE |
  5111. EXT4_GET_BLOCKS_PRE_IO |
  5112. EXT4_GET_BLOCKS_METADATA_NOFAIL);
  5113. }
  5114. ext4_ext_drop_refs(path);
  5115. kfree(path);
  5116. if (ret < 0) {
  5117. up_write(&EXT4_I(inode)->i_data_sem);
  5118. goto out_stop;
  5119. }
  5120. } else {
  5121. ext4_ext_drop_refs(path);
  5122. kfree(path);
  5123. }
  5124. ret = ext4_es_remove_extent(inode, offset_lblk,
  5125. EXT_MAX_BLOCKS - offset_lblk);
  5126. if (ret) {
  5127. up_write(&EXT4_I(inode)->i_data_sem);
  5128. goto out_stop;
  5129. }
  5130. /*
  5131. * if offset_lblk lies in a hole which is at start of file, use
  5132. * ee_start_lblk to shift extents
  5133. */
  5134. ret = ext4_ext_shift_extents(inode, handle,
  5135. ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
  5136. len_lblk, SHIFT_RIGHT);
  5137. up_write(&EXT4_I(inode)->i_data_sem);
  5138. if (IS_SYNC(inode))
  5139. ext4_handle_sync(handle);
  5140. if (ret >= 0)
  5141. ext4_update_inode_fsync_trans(handle, inode, 1);
  5142. out_stop:
  5143. ext4_journal_stop(handle);
  5144. out_mmap:
  5145. up_write(&EXT4_I(inode)->i_mmap_sem);
  5146. ext4_inode_resume_unlocked_dio(inode);
  5147. out_mutex:
  5148. inode_unlock(inode);
  5149. return ret;
  5150. }
  5151. /**
  5152. * ext4_swap_extents - Swap extents between two inodes
  5153. *
  5154. * @inode1: First inode
  5155. * @inode2: Second inode
  5156. * @lblk1: Start block for first inode
  5157. * @lblk2: Start block for second inode
  5158. * @count: Number of blocks to swap
  5159. * @mark_unwritten: Mark second inode's extents as unwritten after swap
  5160. * @erp: Pointer to save error value
  5161. *
  5162. * This helper routine does exactly what is promise "swap extents". All other
  5163. * stuff such as page-cache locking consistency, bh mapping consistency or
  5164. * extent's data copying must be performed by caller.
  5165. * Locking:
  5166. * i_mutex is held for both inodes
  5167. * i_data_sem is locked for write for both inodes
  5168. * Assumptions:
  5169. * All pages from requested range are locked for both inodes
  5170. */
  5171. int
  5172. ext4_swap_extents(handle_t *handle, struct inode *inode1,
  5173. struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
  5174. ext4_lblk_t count, int unwritten, int *erp)
  5175. {
  5176. struct ext4_ext_path *path1 = NULL;
  5177. struct ext4_ext_path *path2 = NULL;
  5178. int replaced_count = 0;
  5179. BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
  5180. BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
  5181. BUG_ON(!inode_is_locked(inode1));
  5182. BUG_ON(!inode_is_locked(inode2));
  5183. *erp = ext4_es_remove_extent(inode1, lblk1, count);
  5184. if (unlikely(*erp))
  5185. return 0;
  5186. *erp = ext4_es_remove_extent(inode2, lblk2, count);
  5187. if (unlikely(*erp))
  5188. return 0;
  5189. while (count) {
  5190. struct ext4_extent *ex1, *ex2, tmp_ex;
  5191. ext4_lblk_t e1_blk, e2_blk;
  5192. int e1_len, e2_len, len;
  5193. int split = 0;
  5194. path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
  5195. if (IS_ERR(path1)) {
  5196. *erp = PTR_ERR(path1);
  5197. path1 = NULL;
  5198. finish:
  5199. count = 0;
  5200. goto repeat;
  5201. }
  5202. path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
  5203. if (IS_ERR(path2)) {
  5204. *erp = PTR_ERR(path2);
  5205. path2 = NULL;
  5206. goto finish;
  5207. }
  5208. ex1 = path1[path1->p_depth].p_ext;
  5209. ex2 = path2[path2->p_depth].p_ext;
  5210. /* Do we have somthing to swap ? */
  5211. if (unlikely(!ex2 || !ex1))
  5212. goto finish;
  5213. e1_blk = le32_to_cpu(ex1->ee_block);
  5214. e2_blk = le32_to_cpu(ex2->ee_block);
  5215. e1_len = ext4_ext_get_actual_len(ex1);
  5216. e2_len = ext4_ext_get_actual_len(ex2);
  5217. /* Hole handling */
  5218. if (!in_range(lblk1, e1_blk, e1_len) ||
  5219. !in_range(lblk2, e2_blk, e2_len)) {
  5220. ext4_lblk_t next1, next2;
  5221. /* if hole after extent, then go to next extent */
  5222. next1 = ext4_ext_next_allocated_block(path1);
  5223. next2 = ext4_ext_next_allocated_block(path2);
  5224. /* If hole before extent, then shift to that extent */
  5225. if (e1_blk > lblk1)
  5226. next1 = e1_blk;
  5227. if (e2_blk > lblk2)
  5228. next2 = e1_blk;
  5229. /* Do we have something to swap */
  5230. if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
  5231. goto finish;
  5232. /* Move to the rightest boundary */
  5233. len = next1 - lblk1;
  5234. if (len < next2 - lblk2)
  5235. len = next2 - lblk2;
  5236. if (len > count)
  5237. len = count;
  5238. lblk1 += len;
  5239. lblk2 += len;
  5240. count -= len;
  5241. goto repeat;
  5242. }
  5243. /* Prepare left boundary */
  5244. if (e1_blk < lblk1) {
  5245. split = 1;
  5246. *erp = ext4_force_split_extent_at(handle, inode1,
  5247. &path1, lblk1, 0);
  5248. if (unlikely(*erp))
  5249. goto finish;
  5250. }
  5251. if (e2_blk < lblk2) {
  5252. split = 1;
  5253. *erp = ext4_force_split_extent_at(handle, inode2,
  5254. &path2, lblk2, 0);
  5255. if (unlikely(*erp))
  5256. goto finish;
  5257. }
  5258. /* ext4_split_extent_at() may result in leaf extent split,
  5259. * path must to be revalidated. */
  5260. if (split)
  5261. goto repeat;
  5262. /* Prepare right boundary */
  5263. len = count;
  5264. if (len > e1_blk + e1_len - lblk1)
  5265. len = e1_blk + e1_len - lblk1;
  5266. if (len > e2_blk + e2_len - lblk2)
  5267. len = e2_blk + e2_len - lblk2;
  5268. if (len != e1_len) {
  5269. split = 1;
  5270. *erp = ext4_force_split_extent_at(handle, inode1,
  5271. &path1, lblk1 + len, 0);
  5272. if (unlikely(*erp))
  5273. goto finish;
  5274. }
  5275. if (len != e2_len) {
  5276. split = 1;
  5277. *erp = ext4_force_split_extent_at(handle, inode2,
  5278. &path2, lblk2 + len, 0);
  5279. if (*erp)
  5280. goto finish;
  5281. }
  5282. /* ext4_split_extent_at() may result in leaf extent split,
  5283. * path must to be revalidated. */
  5284. if (split)
  5285. goto repeat;
  5286. BUG_ON(e2_len != e1_len);
  5287. *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
  5288. if (unlikely(*erp))
  5289. goto finish;
  5290. *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
  5291. if (unlikely(*erp))
  5292. goto finish;
  5293. /* Both extents are fully inside boundaries. Swap it now */
  5294. tmp_ex = *ex1;
  5295. ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
  5296. ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
  5297. ex1->ee_len = cpu_to_le16(e2_len);
  5298. ex2->ee_len = cpu_to_le16(e1_len);
  5299. if (unwritten)
  5300. ext4_ext_mark_unwritten(ex2);
  5301. if (ext4_ext_is_unwritten(&tmp_ex))
  5302. ext4_ext_mark_unwritten(ex1);
  5303. ext4_ext_try_to_merge(handle, inode2, path2, ex2);
  5304. ext4_ext_try_to_merge(handle, inode1, path1, ex1);
  5305. *erp = ext4_ext_dirty(handle, inode2, path2 +
  5306. path2->p_depth);
  5307. if (unlikely(*erp))
  5308. goto finish;
  5309. *erp = ext4_ext_dirty(handle, inode1, path1 +
  5310. path1->p_depth);
  5311. /*
  5312. * Looks scarry ah..? second inode already points to new blocks,
  5313. * and it was successfully dirtied. But luckily error may happen
  5314. * only due to journal error, so full transaction will be
  5315. * aborted anyway.
  5316. */
  5317. if (unlikely(*erp))
  5318. goto finish;
  5319. lblk1 += len;
  5320. lblk2 += len;
  5321. replaced_count += len;
  5322. count -= len;
  5323. repeat:
  5324. ext4_ext_drop_refs(path1);
  5325. kfree(path1);
  5326. ext4_ext_drop_refs(path2);
  5327. kfree(path2);
  5328. path1 = path2 = NULL;
  5329. }
  5330. return replaced_count;
  5331. }