extents.c 103 KB

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