extents.c 82 KB

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