extents.c 76 KB

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