extents.c 96 KB

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