extents.c 134 KB

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