extents.c 104 KB

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