extents.c 134 KB

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