extents.c 120 KB

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