extents.c 132 KB

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