extents.c 114 KB

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