extents.c 134 KB

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