extents.c 135 KB

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