extents.c 150 KB

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