extents.c 98 KB

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