extents.c 131 KB

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