extents.c 136 KB

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