extents.c 156 KB

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