extents.c 84 KB

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