extents.c 163 KB

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