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 (handle->h_buffer_credits > needed)
  90. return 0;
  91. err = ext4_journal_extend(handle, needed);
  92. if (err <= 0)
  93. return err;
  94. return ext4_journal_restart(handle, needed);
  95. }
  96. /*
  97. * could return:
  98. * - EROFS
  99. * - ENOMEM
  100. */
  101. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  102. struct ext4_ext_path *path)
  103. {
  104. if (path->p_bh) {
  105. /* path points to block */
  106. return ext4_journal_get_write_access(handle, path->p_bh);
  107. }
  108. /* path points to leaf/index in inode body */
  109. /* we use in-core data, no need to protect them */
  110. return 0;
  111. }
  112. /*
  113. * could return:
  114. * - EROFS
  115. * - ENOMEM
  116. * - EIO
  117. */
  118. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  119. struct ext4_ext_path *path)
  120. {
  121. int err;
  122. if (path->p_bh) {
  123. /* path points to block */
  124. err = ext4_journal_dirty_metadata(handle, path->p_bh);
  125. } else {
  126. /* path points to leaf/index in inode body */
  127. err = ext4_mark_inode_dirty(handle, inode);
  128. }
  129. return err;
  130. }
  131. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  132. struct ext4_ext_path *path,
  133. ext4_lblk_t block)
  134. {
  135. struct ext4_inode_info *ei = EXT4_I(inode);
  136. ext4_fsblk_t bg_start;
  137. ext4_fsblk_t last_block;
  138. ext4_grpblk_t colour;
  139. int depth;
  140. if (path) {
  141. struct ext4_extent *ex;
  142. depth = path->p_depth;
  143. /* try to predict block placement */
  144. ex = path[depth].p_ext;
  145. if (ex)
  146. return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
  147. /* it looks like index is empty;
  148. * try to find starting block from index itself */
  149. if (path[depth].p_bh)
  150. return path[depth].p_bh->b_blocknr;
  151. }
  152. /* OK. use inode's group */
  153. bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
  154. le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
  155. last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
  156. if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
  157. colour = (current->pid % 16) *
  158. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  159. else
  160. colour = (current->pid % 16) * ((last_block - bg_start) / 16);
  161. return bg_start + colour + block;
  162. }
  163. /*
  164. * Allocation for a meta data block
  165. */
  166. static ext4_fsblk_t
  167. ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
  168. struct ext4_ext_path *path,
  169. struct ext4_extent *ex, int *err)
  170. {
  171. ext4_fsblk_t goal, newblock;
  172. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  173. newblock = ext4_new_meta_block(handle, inode, goal, err);
  174. return newblock;
  175. }
  176. static int ext4_ext_space_block(struct inode *inode)
  177. {
  178. int size;
  179. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  180. / sizeof(struct ext4_extent);
  181. #ifdef AGGRESSIVE_TEST
  182. if (size > 6)
  183. size = 6;
  184. #endif
  185. return size;
  186. }
  187. static int ext4_ext_space_block_idx(struct inode *inode)
  188. {
  189. int size;
  190. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  191. / sizeof(struct ext4_extent_idx);
  192. #ifdef AGGRESSIVE_TEST
  193. if (size > 5)
  194. size = 5;
  195. #endif
  196. return size;
  197. }
  198. static int ext4_ext_space_root(struct inode *inode)
  199. {
  200. int size;
  201. size = sizeof(EXT4_I(inode)->i_data);
  202. size -= sizeof(struct ext4_extent_header);
  203. size /= sizeof(struct ext4_extent);
  204. #ifdef AGGRESSIVE_TEST
  205. if (size > 3)
  206. size = 3;
  207. #endif
  208. return size;
  209. }
  210. static int ext4_ext_space_root_idx(struct inode *inode)
  211. {
  212. int size;
  213. size = sizeof(EXT4_I(inode)->i_data);
  214. size -= sizeof(struct ext4_extent_header);
  215. size /= sizeof(struct ext4_extent_idx);
  216. #ifdef AGGRESSIVE_TEST
  217. if (size > 4)
  218. size = 4;
  219. #endif
  220. return size;
  221. }
  222. /*
  223. * Calculate the number of metadata blocks needed
  224. * to allocate @blocks
  225. * Worse case is one block per extent
  226. */
  227. int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
  228. {
  229. int lcap, icap, rcap, leafs, idxs, num;
  230. int newextents = blocks;
  231. rcap = ext4_ext_space_root_idx(inode);
  232. lcap = ext4_ext_space_block(inode);
  233. icap = ext4_ext_space_block_idx(inode);
  234. /* number of new leaf blocks needed */
  235. num = leafs = (newextents + lcap - 1) / lcap;
  236. /*
  237. * Worse case, we need separate index block(s)
  238. * to link all new leaf blocks
  239. */
  240. idxs = (leafs + icap - 1) / icap;
  241. do {
  242. num += idxs;
  243. idxs = (idxs + icap - 1) / icap;
  244. } while (idxs > rcap);
  245. return num;
  246. }
  247. static int
  248. ext4_ext_max_entries(struct inode *inode, int depth)
  249. {
  250. int max;
  251. if (depth == ext_depth(inode)) {
  252. if (depth == 0)
  253. max = ext4_ext_space_root(inode);
  254. else
  255. max = ext4_ext_space_root_idx(inode);
  256. } else {
  257. if (depth == 0)
  258. max = ext4_ext_space_block(inode);
  259. else
  260. max = ext4_ext_space_block_idx(inode);
  261. }
  262. return max;
  263. }
  264. static int __ext4_ext_check_header(const char *function, struct inode *inode,
  265. struct ext4_extent_header *eh,
  266. int depth)
  267. {
  268. const char *error_msg;
  269. int max = 0;
  270. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  271. error_msg = "invalid magic";
  272. goto corrupted;
  273. }
  274. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  275. error_msg = "unexpected eh_depth";
  276. goto corrupted;
  277. }
  278. if (unlikely(eh->eh_max == 0)) {
  279. error_msg = "invalid eh_max";
  280. goto corrupted;
  281. }
  282. max = ext4_ext_max_entries(inode, depth);
  283. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  284. error_msg = "too large eh_max";
  285. goto corrupted;
  286. }
  287. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  288. error_msg = "invalid eh_entries";
  289. goto corrupted;
  290. }
  291. return 0;
  292. corrupted:
  293. ext4_error(inode->i_sb, function,
  294. "bad header in inode #%lu: %s - magic %x, "
  295. "entries %u, max %u(%u), depth %u(%u)",
  296. inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
  297. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  298. max, le16_to_cpu(eh->eh_depth), depth);
  299. return -EIO;
  300. }
  301. #define ext4_ext_check_header(inode, eh, depth) \
  302. __ext4_ext_check_header(__func__, inode, eh, depth)
  303. #ifdef EXT_DEBUG
  304. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  305. {
  306. int k, l = path->p_depth;
  307. ext_debug("path:");
  308. for (k = 0; k <= l; k++, path++) {
  309. if (path->p_idx) {
  310. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  311. idx_pblock(path->p_idx));
  312. } else if (path->p_ext) {
  313. ext_debug(" %d:%d:%llu ",
  314. le32_to_cpu(path->p_ext->ee_block),
  315. ext4_ext_get_actual_len(path->p_ext),
  316. ext_pblock(path->p_ext));
  317. } else
  318. ext_debug(" []");
  319. }
  320. ext_debug("\n");
  321. }
  322. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  323. {
  324. int depth = ext_depth(inode);
  325. struct ext4_extent_header *eh;
  326. struct ext4_extent *ex;
  327. int i;
  328. if (!path)
  329. return;
  330. eh = path[depth].p_hdr;
  331. ex = EXT_FIRST_EXTENT(eh);
  332. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  333. ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
  334. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  335. }
  336. ext_debug("\n");
  337. }
  338. #else
  339. #define ext4_ext_show_path(inode, path)
  340. #define ext4_ext_show_leaf(inode, path)
  341. #endif
  342. void ext4_ext_drop_refs(struct ext4_ext_path *path)
  343. {
  344. int depth = path->p_depth;
  345. int i;
  346. for (i = 0; i <= depth; i++, path++)
  347. if (path->p_bh) {
  348. brelse(path->p_bh);
  349. path->p_bh = NULL;
  350. }
  351. }
  352. /*
  353. * ext4_ext_binsearch_idx:
  354. * binary search for the closest index of the given block
  355. * the header must be checked before calling this
  356. */
  357. static void
  358. ext4_ext_binsearch_idx(struct inode *inode,
  359. struct ext4_ext_path *path, ext4_lblk_t block)
  360. {
  361. struct ext4_extent_header *eh = path->p_hdr;
  362. struct ext4_extent_idx *r, *l, *m;
  363. ext_debug("binsearch for %u(idx): ", block);
  364. l = EXT_FIRST_INDEX(eh) + 1;
  365. r = EXT_LAST_INDEX(eh);
  366. while (l <= r) {
  367. m = l + (r - l) / 2;
  368. if (block < le32_to_cpu(m->ei_block))
  369. r = m - 1;
  370. else
  371. l = m + 1;
  372. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
  373. m, le32_to_cpu(m->ei_block),
  374. r, le32_to_cpu(r->ei_block));
  375. }
  376. path->p_idx = l - 1;
  377. ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
  378. idx_pblock(path->p_idx));
  379. #ifdef CHECK_BINSEARCH
  380. {
  381. struct ext4_extent_idx *chix, *ix;
  382. int k;
  383. chix = ix = EXT_FIRST_INDEX(eh);
  384. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  385. if (k != 0 &&
  386. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  387. printk(KERN_DEBUG "k=%d, ix=0x%p, "
  388. "first=0x%p\n", k,
  389. ix, EXT_FIRST_INDEX(eh));
  390. printk(KERN_DEBUG "%u <= %u\n",
  391. le32_to_cpu(ix->ei_block),
  392. le32_to_cpu(ix[-1].ei_block));
  393. }
  394. BUG_ON(k && le32_to_cpu(ix->ei_block)
  395. <= le32_to_cpu(ix[-1].ei_block));
  396. if (block < le32_to_cpu(ix->ei_block))
  397. break;
  398. chix = ix;
  399. }
  400. BUG_ON(chix != path->p_idx);
  401. }
  402. #endif
  403. }
  404. /*
  405. * ext4_ext_binsearch:
  406. * binary search for closest extent of the given block
  407. * the header must be checked before calling this
  408. */
  409. static void
  410. ext4_ext_binsearch(struct inode *inode,
  411. struct ext4_ext_path *path, ext4_lblk_t block)
  412. {
  413. struct ext4_extent_header *eh = path->p_hdr;
  414. struct ext4_extent *r, *l, *m;
  415. if (eh->eh_entries == 0) {
  416. /*
  417. * this leaf is empty:
  418. * we get such a leaf in split/add case
  419. */
  420. return;
  421. }
  422. ext_debug("binsearch for %u: ", block);
  423. l = EXT_FIRST_EXTENT(eh) + 1;
  424. r = EXT_LAST_EXTENT(eh);
  425. while (l <= r) {
  426. m = l + (r - l) / 2;
  427. if (block < le32_to_cpu(m->ee_block))
  428. r = m - 1;
  429. else
  430. l = m + 1;
  431. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
  432. m, le32_to_cpu(m->ee_block),
  433. r, le32_to_cpu(r->ee_block));
  434. }
  435. path->p_ext = l - 1;
  436. ext_debug(" -> %d:%llu:%d ",
  437. le32_to_cpu(path->p_ext->ee_block),
  438. ext_pblock(path->p_ext),
  439. ext4_ext_get_actual_len(path->p_ext));
  440. #ifdef CHECK_BINSEARCH
  441. {
  442. struct ext4_extent *chex, *ex;
  443. int k;
  444. chex = ex = EXT_FIRST_EXTENT(eh);
  445. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  446. BUG_ON(k && le32_to_cpu(ex->ee_block)
  447. <= le32_to_cpu(ex[-1].ee_block));
  448. if (block < le32_to_cpu(ex->ee_block))
  449. break;
  450. chex = ex;
  451. }
  452. BUG_ON(chex != path->p_ext);
  453. }
  454. #endif
  455. }
  456. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  457. {
  458. struct ext4_extent_header *eh;
  459. eh = ext_inode_hdr(inode);
  460. eh->eh_depth = 0;
  461. eh->eh_entries = 0;
  462. eh->eh_magic = EXT4_EXT_MAGIC;
  463. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
  464. ext4_mark_inode_dirty(handle, inode);
  465. ext4_ext_invalidate_cache(inode);
  466. return 0;
  467. }
  468. struct ext4_ext_path *
  469. ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
  470. struct ext4_ext_path *path)
  471. {
  472. struct ext4_extent_header *eh;
  473. struct buffer_head *bh;
  474. short int depth, i, ppos = 0, alloc = 0;
  475. eh = ext_inode_hdr(inode);
  476. depth = ext_depth(inode);
  477. if (ext4_ext_check_header(inode, eh, depth))
  478. return ERR_PTR(-EIO);
  479. /* account possible depth increase */
  480. if (!path) {
  481. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  482. GFP_NOFS);
  483. if (!path)
  484. return ERR_PTR(-ENOMEM);
  485. alloc = 1;
  486. }
  487. path[0].p_hdr = eh;
  488. path[0].p_bh = NULL;
  489. i = depth;
  490. /* walk through the tree */
  491. while (i) {
  492. ext_debug("depth %d: num %d, max %d\n",
  493. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  494. ext4_ext_binsearch_idx(inode, path + ppos, block);
  495. path[ppos].p_block = idx_pblock(path[ppos].p_idx);
  496. path[ppos].p_depth = i;
  497. path[ppos].p_ext = NULL;
  498. bh = sb_bread(inode->i_sb, path[ppos].p_block);
  499. if (!bh)
  500. goto err;
  501. eh = ext_block_hdr(bh);
  502. ppos++;
  503. BUG_ON(ppos > depth);
  504. path[ppos].p_bh = bh;
  505. path[ppos].p_hdr = eh;
  506. i--;
  507. if (ext4_ext_check_header(inode, eh, i))
  508. goto err;
  509. }
  510. path[ppos].p_depth = i;
  511. path[ppos].p_ext = NULL;
  512. path[ppos].p_idx = NULL;
  513. /* find extent */
  514. ext4_ext_binsearch(inode, path + ppos, block);
  515. /* if not an empty leaf */
  516. if (path[ppos].p_ext)
  517. path[ppos].p_block = ext_pblock(path[ppos].p_ext);
  518. ext4_ext_show_path(inode, path);
  519. return path;
  520. err:
  521. ext4_ext_drop_refs(path);
  522. if (alloc)
  523. kfree(path);
  524. return ERR_PTR(-EIO);
  525. }
  526. /*
  527. * ext4_ext_insert_index:
  528. * insert new index [@logical;@ptr] into the block at @curp;
  529. * check where to insert: before @curp or after @curp
  530. */
  531. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  532. struct ext4_ext_path *curp,
  533. int logical, ext4_fsblk_t ptr)
  534. {
  535. struct ext4_extent_idx *ix;
  536. int len, err;
  537. err = ext4_ext_get_access(handle, inode, curp);
  538. if (err)
  539. return err;
  540. BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
  541. len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
  542. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  543. /* insert after */
  544. if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
  545. len = (len - 1) * sizeof(struct ext4_extent_idx);
  546. len = len < 0 ? 0 : len;
  547. ext_debug("insert new index %d after: %llu. "
  548. "move %d from 0x%p to 0x%p\n",
  549. logical, ptr, len,
  550. (curp->p_idx + 1), (curp->p_idx + 2));
  551. memmove(curp->p_idx + 2, curp->p_idx + 1, len);
  552. }
  553. ix = curp->p_idx + 1;
  554. } else {
  555. /* insert before */
  556. len = len * sizeof(struct ext4_extent_idx);
  557. len = len < 0 ? 0 : len;
  558. ext_debug("insert new index %d before: %llu. "
  559. "move %d from 0x%p to 0x%p\n",
  560. logical, ptr, len,
  561. curp->p_idx, (curp->p_idx + 1));
  562. memmove(curp->p_idx + 1, curp->p_idx, len);
  563. ix = curp->p_idx;
  564. }
  565. ix->ei_block = cpu_to_le32(logical);
  566. ext4_idx_store_pblock(ix, ptr);
  567. le16_add_cpu(&curp->p_hdr->eh_entries, 1);
  568. BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
  569. > le16_to_cpu(curp->p_hdr->eh_max));
  570. BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
  571. err = ext4_ext_dirty(handle, inode, curp);
  572. ext4_std_error(inode->i_sb, err);
  573. return err;
  574. }
  575. /*
  576. * ext4_ext_split:
  577. * inserts new subtree into the path, using free index entry
  578. * at depth @at:
  579. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  580. * - makes decision where to split
  581. * - moves remaining extents and index entries (right to the split point)
  582. * into the newly allocated blocks
  583. * - initializes subtree
  584. */
  585. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  586. struct ext4_ext_path *path,
  587. struct ext4_extent *newext, int at)
  588. {
  589. struct buffer_head *bh = NULL;
  590. int depth = ext_depth(inode);
  591. struct ext4_extent_header *neh;
  592. struct ext4_extent_idx *fidx;
  593. struct ext4_extent *ex;
  594. int i = at, k, m, a;
  595. ext4_fsblk_t newblock, oldblock;
  596. __le32 border;
  597. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  598. int err = 0;
  599. /* make decision: where to split? */
  600. /* FIXME: now decision is simplest: at current extent */
  601. /* if current leaf will be split, then we should use
  602. * border from split point */
  603. BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
  604. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  605. border = path[depth].p_ext[1].ee_block;
  606. ext_debug("leaf will be split."
  607. " next leaf starts at %d\n",
  608. le32_to_cpu(border));
  609. } else {
  610. border = newext->ee_block;
  611. ext_debug("leaf will be added."
  612. " next leaf starts at %d\n",
  613. le32_to_cpu(border));
  614. }
  615. /*
  616. * If error occurs, then we break processing
  617. * and mark filesystem read-only. index won't
  618. * be inserted and tree will be in consistent
  619. * state. Next mount will repair buffers too.
  620. */
  621. /*
  622. * Get array to track all allocated blocks.
  623. * We need this to handle errors and free blocks
  624. * upon them.
  625. */
  626. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  627. if (!ablocks)
  628. return -ENOMEM;
  629. /* allocate all needed blocks */
  630. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  631. for (a = 0; a < depth - at; a++) {
  632. newblock = ext4_ext_new_meta_block(handle, inode, path,
  633. newext, &err);
  634. if (newblock == 0)
  635. goto cleanup;
  636. ablocks[a] = newblock;
  637. }
  638. /* initialize new leaf */
  639. newblock = ablocks[--a];
  640. BUG_ON(newblock == 0);
  641. bh = sb_getblk(inode->i_sb, newblock);
  642. if (!bh) {
  643. err = -EIO;
  644. goto cleanup;
  645. }
  646. lock_buffer(bh);
  647. err = ext4_journal_get_create_access(handle, bh);
  648. if (err)
  649. goto cleanup;
  650. neh = ext_block_hdr(bh);
  651. neh->eh_entries = 0;
  652. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  653. neh->eh_magic = EXT4_EXT_MAGIC;
  654. neh->eh_depth = 0;
  655. ex = EXT_FIRST_EXTENT(neh);
  656. /* move remainder of path[depth] to the new leaf */
  657. BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
  658. /* start copy from next extent */
  659. /* TODO: we could do it by single memmove */
  660. m = 0;
  661. path[depth].p_ext++;
  662. while (path[depth].p_ext <=
  663. EXT_MAX_EXTENT(path[depth].p_hdr)) {
  664. ext_debug("move %d:%llu:%d in new leaf %llu\n",
  665. le32_to_cpu(path[depth].p_ext->ee_block),
  666. ext_pblock(path[depth].p_ext),
  667. ext4_ext_get_actual_len(path[depth].p_ext),
  668. newblock);
  669. /*memmove(ex++, path[depth].p_ext++,
  670. sizeof(struct ext4_extent));
  671. neh->eh_entries++;*/
  672. path[depth].p_ext++;
  673. m++;
  674. }
  675. if (m) {
  676. memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
  677. le16_add_cpu(&neh->eh_entries, m);
  678. }
  679. set_buffer_uptodate(bh);
  680. unlock_buffer(bh);
  681. err = ext4_journal_dirty_metadata(handle, bh);
  682. if (err)
  683. goto cleanup;
  684. brelse(bh);
  685. bh = NULL;
  686. /* correct old leaf */
  687. if (m) {
  688. err = ext4_ext_get_access(handle, inode, path + depth);
  689. if (err)
  690. goto cleanup;
  691. le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
  692. err = ext4_ext_dirty(handle, inode, path + depth);
  693. if (err)
  694. goto cleanup;
  695. }
  696. /* create intermediate indexes */
  697. k = depth - at - 1;
  698. BUG_ON(k < 0);
  699. if (k)
  700. ext_debug("create %d intermediate indices\n", k);
  701. /* insert new index into current index block */
  702. /* current depth stored in i var */
  703. i = depth - 1;
  704. while (k--) {
  705. oldblock = newblock;
  706. newblock = ablocks[--a];
  707. bh = sb_getblk(inode->i_sb, newblock);
  708. if (!bh) {
  709. err = -EIO;
  710. goto cleanup;
  711. }
  712. lock_buffer(bh);
  713. err = ext4_journal_get_create_access(handle, bh);
  714. if (err)
  715. goto cleanup;
  716. neh = ext_block_hdr(bh);
  717. neh->eh_entries = cpu_to_le16(1);
  718. neh->eh_magic = EXT4_EXT_MAGIC;
  719. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  720. neh->eh_depth = cpu_to_le16(depth - i);
  721. fidx = EXT_FIRST_INDEX(neh);
  722. fidx->ei_block = border;
  723. ext4_idx_store_pblock(fidx, oldblock);
  724. ext_debug("int.index at %d (block %llu): %u -> %llu\n",
  725. i, newblock, le32_to_cpu(border), oldblock);
  726. /* copy indexes */
  727. m = 0;
  728. path[i].p_idx++;
  729. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  730. EXT_MAX_INDEX(path[i].p_hdr));
  731. BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
  732. EXT_LAST_INDEX(path[i].p_hdr));
  733. while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
  734. ext_debug("%d: move %d:%llu in new index %llu\n", i,
  735. le32_to_cpu(path[i].p_idx->ei_block),
  736. idx_pblock(path[i].p_idx),
  737. newblock);
  738. /*memmove(++fidx, path[i].p_idx++,
  739. sizeof(struct ext4_extent_idx));
  740. neh->eh_entries++;
  741. BUG_ON(neh->eh_entries > neh->eh_max);*/
  742. path[i].p_idx++;
  743. m++;
  744. }
  745. if (m) {
  746. memmove(++fidx, path[i].p_idx - m,
  747. sizeof(struct ext4_extent_idx) * m);
  748. le16_add_cpu(&neh->eh_entries, m);
  749. }
  750. set_buffer_uptodate(bh);
  751. unlock_buffer(bh);
  752. err = ext4_journal_dirty_metadata(handle, bh);
  753. if (err)
  754. goto cleanup;
  755. brelse(bh);
  756. bh = NULL;
  757. /* correct old index */
  758. if (m) {
  759. err = ext4_ext_get_access(handle, inode, path + i);
  760. if (err)
  761. goto cleanup;
  762. le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
  763. err = ext4_ext_dirty(handle, inode, path + i);
  764. if (err)
  765. goto cleanup;
  766. }
  767. i--;
  768. }
  769. /* insert new index */
  770. err = ext4_ext_insert_index(handle, inode, path + at,
  771. le32_to_cpu(border), newblock);
  772. cleanup:
  773. if (bh) {
  774. if (buffer_locked(bh))
  775. unlock_buffer(bh);
  776. brelse(bh);
  777. }
  778. if (err) {
  779. /* free all allocated blocks in error case */
  780. for (i = 0; i < depth; i++) {
  781. if (!ablocks[i])
  782. continue;
  783. ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
  784. }
  785. }
  786. kfree(ablocks);
  787. return err;
  788. }
  789. /*
  790. * ext4_ext_grow_indepth:
  791. * implements tree growing procedure:
  792. * - allocates new block
  793. * - moves top-level data (index block or leaf) into the new block
  794. * - initializes new top-level, creating index that points to the
  795. * just created block
  796. */
  797. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  798. struct ext4_ext_path *path,
  799. struct ext4_extent *newext)
  800. {
  801. struct ext4_ext_path *curp = path;
  802. struct ext4_extent_header *neh;
  803. struct ext4_extent_idx *fidx;
  804. struct buffer_head *bh;
  805. ext4_fsblk_t newblock;
  806. int err = 0;
  807. newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
  808. if (newblock == 0)
  809. return err;
  810. bh = sb_getblk(inode->i_sb, newblock);
  811. if (!bh) {
  812. err = -EIO;
  813. ext4_std_error(inode->i_sb, err);
  814. return err;
  815. }
  816. lock_buffer(bh);
  817. err = ext4_journal_get_create_access(handle, bh);
  818. if (err) {
  819. unlock_buffer(bh);
  820. goto out;
  821. }
  822. /* move top-level index/leaf into new block */
  823. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  824. /* set size of new block */
  825. neh = ext_block_hdr(bh);
  826. /* old root could have indexes or leaves
  827. * so calculate e_max right way */
  828. if (ext_depth(inode))
  829. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  830. else
  831. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  832. neh->eh_magic = EXT4_EXT_MAGIC;
  833. set_buffer_uptodate(bh);
  834. unlock_buffer(bh);
  835. err = ext4_journal_dirty_metadata(handle, bh);
  836. if (err)
  837. goto out;
  838. /* create index in new top-level index: num,max,pointer */
  839. err = ext4_ext_get_access(handle, inode, curp);
  840. if (err)
  841. goto out;
  842. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  843. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
  844. curp->p_hdr->eh_entries = cpu_to_le16(1);
  845. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  846. if (path[0].p_hdr->eh_depth)
  847. curp->p_idx->ei_block =
  848. EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
  849. else
  850. curp->p_idx->ei_block =
  851. EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  852. ext4_idx_store_pblock(curp->p_idx, newblock);
  853. neh = ext_inode_hdr(inode);
  854. fidx = EXT_FIRST_INDEX(neh);
  855. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  856. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  857. le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
  858. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  859. err = ext4_ext_dirty(handle, inode, curp);
  860. out:
  861. brelse(bh);
  862. return err;
  863. }
  864. /*
  865. * ext4_ext_create_new_leaf:
  866. * finds empty index and adds new leaf.
  867. * if no free index is found, then it requests in-depth growing.
  868. */
  869. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  870. struct ext4_ext_path *path,
  871. struct ext4_extent *newext)
  872. {
  873. struct ext4_ext_path *curp;
  874. int depth, i, err = 0;
  875. repeat:
  876. i = depth = ext_depth(inode);
  877. /* walk up to the tree and look for free index entry */
  878. curp = path + depth;
  879. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  880. i--;
  881. curp--;
  882. }
  883. /* we use already allocated block for index block,
  884. * so subsequent data blocks should be contiguous */
  885. if (EXT_HAS_FREE_INDEX(curp)) {
  886. /* if we found index with free entry, then use that
  887. * entry: create all needed subtree and add new leaf */
  888. err = ext4_ext_split(handle, inode, path, newext, i);
  889. if (err)
  890. goto out;
  891. /* refill path */
  892. ext4_ext_drop_refs(path);
  893. path = ext4_ext_find_extent(inode,
  894. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  895. path);
  896. if (IS_ERR(path))
  897. err = PTR_ERR(path);
  898. } else {
  899. /* tree is full, time to grow in depth */
  900. err = ext4_ext_grow_indepth(handle, inode, path, newext);
  901. if (err)
  902. goto out;
  903. /* refill path */
  904. ext4_ext_drop_refs(path);
  905. path = ext4_ext_find_extent(inode,
  906. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  907. path);
  908. if (IS_ERR(path)) {
  909. err = PTR_ERR(path);
  910. goto out;
  911. }
  912. /*
  913. * only first (depth 0 -> 1) produces free space;
  914. * in all other cases we have to split the grown tree
  915. */
  916. depth = ext_depth(inode);
  917. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  918. /* now we need to split */
  919. goto repeat;
  920. }
  921. }
  922. out:
  923. return err;
  924. }
  925. /*
  926. * search the closest allocated block to the left for *logical
  927. * and returns it at @logical + it's physical address at @phys
  928. * if *logical is the smallest allocated block, the function
  929. * returns 0 at @phys
  930. * return value contains 0 (success) or error code
  931. */
  932. int
  933. ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
  934. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  935. {
  936. struct ext4_extent_idx *ix;
  937. struct ext4_extent *ex;
  938. int depth, ee_len;
  939. BUG_ON(path == NULL);
  940. depth = path->p_depth;
  941. *phys = 0;
  942. if (depth == 0 && path->p_ext == NULL)
  943. return 0;
  944. /* usually extent in the path covers blocks smaller
  945. * then *logical, but it can be that extent is the
  946. * first one in the file */
  947. ex = path[depth].p_ext;
  948. ee_len = ext4_ext_get_actual_len(ex);
  949. if (*logical < le32_to_cpu(ex->ee_block)) {
  950. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  951. while (--depth >= 0) {
  952. ix = path[depth].p_idx;
  953. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  954. }
  955. return 0;
  956. }
  957. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  958. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  959. *phys = ext_pblock(ex) + ee_len - 1;
  960. return 0;
  961. }
  962. /*
  963. * search the closest allocated block to the right for *logical
  964. * and returns it at @logical + it's physical address at @phys
  965. * if *logical is the smallest allocated block, the function
  966. * returns 0 at @phys
  967. * return value contains 0 (success) or error code
  968. */
  969. int
  970. ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
  971. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  972. {
  973. struct buffer_head *bh = NULL;
  974. struct ext4_extent_header *eh;
  975. struct ext4_extent_idx *ix;
  976. struct ext4_extent *ex;
  977. ext4_fsblk_t block;
  978. int depth, ee_len;
  979. BUG_ON(path == NULL);
  980. depth = path->p_depth;
  981. *phys = 0;
  982. if (depth == 0 && path->p_ext == NULL)
  983. return 0;
  984. /* usually extent in the path covers blocks smaller
  985. * then *logical, but it can be that extent is the
  986. * first one in the file */
  987. ex = path[depth].p_ext;
  988. ee_len = ext4_ext_get_actual_len(ex);
  989. if (*logical < le32_to_cpu(ex->ee_block)) {
  990. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  991. while (--depth >= 0) {
  992. ix = path[depth].p_idx;
  993. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  994. }
  995. *logical = le32_to_cpu(ex->ee_block);
  996. *phys = ext_pblock(ex);
  997. return 0;
  998. }
  999. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  1000. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  1001. /* next allocated block in this leaf */
  1002. ex++;
  1003. *logical = le32_to_cpu(ex->ee_block);
  1004. *phys = ext_pblock(ex);
  1005. return 0;
  1006. }
  1007. /* go up and search for index to the right */
  1008. while (--depth >= 0) {
  1009. ix = path[depth].p_idx;
  1010. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  1011. break;
  1012. }
  1013. if (depth < 0) {
  1014. /* we've gone up to the root and
  1015. * found no index to the right */
  1016. return 0;
  1017. }
  1018. /* we've found index to the right, let's
  1019. * follow it and find the closest allocated
  1020. * block to the right */
  1021. ix++;
  1022. block = idx_pblock(ix);
  1023. while (++depth < path->p_depth) {
  1024. bh = sb_bread(inode->i_sb, block);
  1025. if (bh == NULL)
  1026. return -EIO;
  1027. eh = ext_block_hdr(bh);
  1028. if (ext4_ext_check_header(inode, eh, depth)) {
  1029. put_bh(bh);
  1030. return -EIO;
  1031. }
  1032. ix = EXT_FIRST_INDEX(eh);
  1033. block = idx_pblock(ix);
  1034. put_bh(bh);
  1035. }
  1036. bh = sb_bread(inode->i_sb, block);
  1037. if (bh == NULL)
  1038. return -EIO;
  1039. eh = ext_block_hdr(bh);
  1040. if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
  1041. put_bh(bh);
  1042. return -EIO;
  1043. }
  1044. ex = EXT_FIRST_EXTENT(eh);
  1045. *logical = le32_to_cpu(ex->ee_block);
  1046. *phys = ext_pblock(ex);
  1047. put_bh(bh);
  1048. return 0;
  1049. }
  1050. /*
  1051. * ext4_ext_next_allocated_block:
  1052. * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
  1053. * NOTE: it considers block number from index entry as
  1054. * allocated block. Thus, index entries have to be consistent
  1055. * with leaves.
  1056. */
  1057. static ext4_lblk_t
  1058. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1059. {
  1060. int depth;
  1061. BUG_ON(path == NULL);
  1062. depth = path->p_depth;
  1063. if (depth == 0 && path->p_ext == NULL)
  1064. return EXT_MAX_BLOCK;
  1065. while (depth >= 0) {
  1066. if (depth == path->p_depth) {
  1067. /* leaf */
  1068. if (path[depth].p_ext !=
  1069. EXT_LAST_EXTENT(path[depth].p_hdr))
  1070. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1071. } else {
  1072. /* index */
  1073. if (path[depth].p_idx !=
  1074. EXT_LAST_INDEX(path[depth].p_hdr))
  1075. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1076. }
  1077. depth--;
  1078. }
  1079. return EXT_MAX_BLOCK;
  1080. }
  1081. /*
  1082. * ext4_ext_next_leaf_block:
  1083. * returns first allocated block from next leaf or EXT_MAX_BLOCK
  1084. */
  1085. static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
  1086. struct ext4_ext_path *path)
  1087. {
  1088. int depth;
  1089. BUG_ON(path == NULL);
  1090. depth = path->p_depth;
  1091. /* zero-tree has no leaf blocks at all */
  1092. if (depth == 0)
  1093. return EXT_MAX_BLOCK;
  1094. /* go to index block */
  1095. depth--;
  1096. while (depth >= 0) {
  1097. if (path[depth].p_idx !=
  1098. EXT_LAST_INDEX(path[depth].p_hdr))
  1099. return (ext4_lblk_t)
  1100. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1101. depth--;
  1102. }
  1103. return EXT_MAX_BLOCK;
  1104. }
  1105. /*
  1106. * ext4_ext_correct_indexes:
  1107. * if leaf gets modified and modified extent is first in the leaf,
  1108. * then we have to correct all indexes above.
  1109. * TODO: do we need to correct tree in all cases?
  1110. */
  1111. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1112. struct ext4_ext_path *path)
  1113. {
  1114. struct ext4_extent_header *eh;
  1115. int depth = ext_depth(inode);
  1116. struct ext4_extent *ex;
  1117. __le32 border;
  1118. int k, err = 0;
  1119. eh = path[depth].p_hdr;
  1120. ex = path[depth].p_ext;
  1121. BUG_ON(ex == NULL);
  1122. BUG_ON(eh == NULL);
  1123. if (depth == 0) {
  1124. /* there is no tree at all */
  1125. return 0;
  1126. }
  1127. if (ex != EXT_FIRST_EXTENT(eh)) {
  1128. /* we correct tree if first leaf got modified only */
  1129. return 0;
  1130. }
  1131. /*
  1132. * TODO: we need correction if border is smaller than current one
  1133. */
  1134. k = depth - 1;
  1135. border = path[depth].p_ext->ee_block;
  1136. err = ext4_ext_get_access(handle, inode, path + k);
  1137. if (err)
  1138. return err;
  1139. path[k].p_idx->ei_block = border;
  1140. err = ext4_ext_dirty(handle, inode, path + k);
  1141. if (err)
  1142. return err;
  1143. while (k--) {
  1144. /* change all left-side indexes */
  1145. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1146. break;
  1147. err = ext4_ext_get_access(handle, inode, path + k);
  1148. if (err)
  1149. break;
  1150. path[k].p_idx->ei_block = border;
  1151. err = ext4_ext_dirty(handle, inode, path + k);
  1152. if (err)
  1153. break;
  1154. }
  1155. return err;
  1156. }
  1157. static int
  1158. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1159. struct ext4_extent *ex2)
  1160. {
  1161. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  1162. /*
  1163. * Make sure that either both extents are uninitialized, or
  1164. * both are _not_.
  1165. */
  1166. if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
  1167. return 0;
  1168. if (ext4_ext_is_uninitialized(ex1))
  1169. max_len = EXT_UNINIT_MAX_LEN;
  1170. else
  1171. max_len = EXT_INIT_MAX_LEN;
  1172. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1173. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1174. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1175. le32_to_cpu(ex2->ee_block))
  1176. return 0;
  1177. /*
  1178. * To allow future support for preallocated extents to be added
  1179. * as an RO_COMPAT feature, refuse to merge to extents if
  1180. * this can result in the top bit of ee_len being set.
  1181. */
  1182. if (ext1_ee_len + ext2_ee_len > max_len)
  1183. return 0;
  1184. #ifdef AGGRESSIVE_TEST
  1185. if (ext1_ee_len >= 4)
  1186. return 0;
  1187. #endif
  1188. if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
  1189. return 1;
  1190. return 0;
  1191. }
  1192. /*
  1193. * This function tries to merge the "ex" extent to the next extent in the tree.
  1194. * It always tries to merge towards right. If you want to merge towards
  1195. * left, pass "ex - 1" as argument instead of "ex".
  1196. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1197. * 1 if they got merged.
  1198. */
  1199. int ext4_ext_try_to_merge(struct inode *inode,
  1200. struct ext4_ext_path *path,
  1201. struct ext4_extent *ex)
  1202. {
  1203. struct ext4_extent_header *eh;
  1204. unsigned int depth, len;
  1205. int merge_done = 0;
  1206. int uninitialized = 0;
  1207. depth = ext_depth(inode);
  1208. BUG_ON(path[depth].p_hdr == NULL);
  1209. eh = path[depth].p_hdr;
  1210. while (ex < EXT_LAST_EXTENT(eh)) {
  1211. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1212. break;
  1213. /* merge with next extent! */
  1214. if (ext4_ext_is_uninitialized(ex))
  1215. uninitialized = 1;
  1216. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1217. + ext4_ext_get_actual_len(ex + 1));
  1218. if (uninitialized)
  1219. ext4_ext_mark_uninitialized(ex);
  1220. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1221. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1222. * sizeof(struct ext4_extent);
  1223. memmove(ex + 1, ex + 2, len);
  1224. }
  1225. le16_add_cpu(&eh->eh_entries, -1);
  1226. merge_done = 1;
  1227. WARN_ON(eh->eh_entries == 0);
  1228. if (!eh->eh_entries)
  1229. ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
  1230. "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
  1231. }
  1232. return merge_done;
  1233. }
  1234. /*
  1235. * check if a portion of the "newext" extent overlaps with an
  1236. * existing extent.
  1237. *
  1238. * If there is an overlap discovered, it updates the length of the newext
  1239. * such that there will be no overlap, and then returns 1.
  1240. * If there is no overlap found, it returns 0.
  1241. */
  1242. unsigned int ext4_ext_check_overlap(struct inode *inode,
  1243. struct ext4_extent *newext,
  1244. struct ext4_ext_path *path)
  1245. {
  1246. ext4_lblk_t b1, b2;
  1247. unsigned int depth, len1;
  1248. unsigned int ret = 0;
  1249. b1 = le32_to_cpu(newext->ee_block);
  1250. len1 = ext4_ext_get_actual_len(newext);
  1251. depth = ext_depth(inode);
  1252. if (!path[depth].p_ext)
  1253. goto out;
  1254. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1255. /*
  1256. * get the next allocated block if the extent in the path
  1257. * is before the requested block(s)
  1258. */
  1259. if (b2 < b1) {
  1260. b2 = ext4_ext_next_allocated_block(path);
  1261. if (b2 == EXT_MAX_BLOCK)
  1262. goto out;
  1263. }
  1264. /* check for wrap through zero on extent logical start block*/
  1265. if (b1 + len1 < b1) {
  1266. len1 = EXT_MAX_BLOCK - b1;
  1267. newext->ee_len = cpu_to_le16(len1);
  1268. ret = 1;
  1269. }
  1270. /* check for overlap */
  1271. if (b1 + len1 > b2) {
  1272. newext->ee_len = cpu_to_le16(b2 - b1);
  1273. ret = 1;
  1274. }
  1275. out:
  1276. return ret;
  1277. }
  1278. /*
  1279. * ext4_ext_insert_extent:
  1280. * tries to merge requsted extent into the existing extent or
  1281. * inserts requested extent as new one into the tree,
  1282. * creating new leaf in the no-space case.
  1283. */
  1284. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1285. struct ext4_ext_path *path,
  1286. struct ext4_extent *newext)
  1287. {
  1288. struct ext4_extent_header *eh;
  1289. struct ext4_extent *ex, *fex;
  1290. struct ext4_extent *nearex; /* nearest extent */
  1291. struct ext4_ext_path *npath = NULL;
  1292. int depth, len, err;
  1293. ext4_lblk_t next;
  1294. unsigned uninitialized = 0;
  1295. BUG_ON(ext4_ext_get_actual_len(newext) == 0);
  1296. depth = ext_depth(inode);
  1297. ex = path[depth].p_ext;
  1298. BUG_ON(path[depth].p_hdr == NULL);
  1299. /* try to insert block into found extent and return */
  1300. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  1301. ext_debug("append %d block to %d:%d (from %llu)\n",
  1302. ext4_ext_get_actual_len(newext),
  1303. le32_to_cpu(ex->ee_block),
  1304. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  1305. err = ext4_ext_get_access(handle, inode, path + depth);
  1306. if (err)
  1307. return err;
  1308. /*
  1309. * ext4_can_extents_be_merged should have checked that either
  1310. * both extents are uninitialized, or both aren't. Thus we
  1311. * need to check only one of them here.
  1312. */
  1313. if (ext4_ext_is_uninitialized(ex))
  1314. uninitialized = 1;
  1315. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1316. + ext4_ext_get_actual_len(newext));
  1317. if (uninitialized)
  1318. ext4_ext_mark_uninitialized(ex);
  1319. eh = path[depth].p_hdr;
  1320. nearex = ex;
  1321. goto merge;
  1322. }
  1323. repeat:
  1324. depth = ext_depth(inode);
  1325. eh = path[depth].p_hdr;
  1326. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1327. goto has_space;
  1328. /* probably next leaf has space for us? */
  1329. fex = EXT_LAST_EXTENT(eh);
  1330. next = ext4_ext_next_leaf_block(inode, path);
  1331. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1332. && next != EXT_MAX_BLOCK) {
  1333. ext_debug("next leaf block - %d\n", next);
  1334. BUG_ON(npath != NULL);
  1335. npath = ext4_ext_find_extent(inode, next, NULL);
  1336. if (IS_ERR(npath))
  1337. return PTR_ERR(npath);
  1338. BUG_ON(npath->p_depth != path->p_depth);
  1339. eh = npath[depth].p_hdr;
  1340. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1341. ext_debug("next leaf isnt full(%d)\n",
  1342. le16_to_cpu(eh->eh_entries));
  1343. path = npath;
  1344. goto repeat;
  1345. }
  1346. ext_debug("next leaf has no free space(%d,%d)\n",
  1347. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1348. }
  1349. /*
  1350. * There is no free space in the found leaf.
  1351. * We're gonna add a new leaf in the tree.
  1352. */
  1353. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1354. if (err)
  1355. goto cleanup;
  1356. depth = ext_depth(inode);
  1357. eh = path[depth].p_hdr;
  1358. has_space:
  1359. nearex = path[depth].p_ext;
  1360. err = ext4_ext_get_access(handle, inode, path + depth);
  1361. if (err)
  1362. goto cleanup;
  1363. if (!nearex) {
  1364. /* there is no extent in this leaf, create first one */
  1365. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1366. le32_to_cpu(newext->ee_block),
  1367. ext_pblock(newext),
  1368. ext4_ext_get_actual_len(newext));
  1369. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1370. } else if (le32_to_cpu(newext->ee_block)
  1371. > le32_to_cpu(nearex->ee_block)) {
  1372. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1373. if (nearex != EXT_LAST_EXTENT(eh)) {
  1374. len = EXT_MAX_EXTENT(eh) - nearex;
  1375. len = (len - 1) * sizeof(struct ext4_extent);
  1376. len = len < 0 ? 0 : len;
  1377. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1378. "move %d from 0x%p to 0x%p\n",
  1379. le32_to_cpu(newext->ee_block),
  1380. ext_pblock(newext),
  1381. ext4_ext_get_actual_len(newext),
  1382. nearex, len, nearex + 1, nearex + 2);
  1383. memmove(nearex + 2, nearex + 1, len);
  1384. }
  1385. path[depth].p_ext = nearex + 1;
  1386. } else {
  1387. BUG_ON(newext->ee_block == nearex->ee_block);
  1388. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1389. len = len < 0 ? 0 : len;
  1390. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1391. "move %d from 0x%p to 0x%p\n",
  1392. le32_to_cpu(newext->ee_block),
  1393. ext_pblock(newext),
  1394. ext4_ext_get_actual_len(newext),
  1395. nearex, len, nearex + 1, nearex + 2);
  1396. memmove(nearex + 1, nearex, len);
  1397. path[depth].p_ext = nearex;
  1398. }
  1399. le16_add_cpu(&eh->eh_entries, 1);
  1400. nearex = path[depth].p_ext;
  1401. nearex->ee_block = newext->ee_block;
  1402. ext4_ext_store_pblock(nearex, ext_pblock(newext));
  1403. nearex->ee_len = newext->ee_len;
  1404. merge:
  1405. /* try to merge extents to the right */
  1406. ext4_ext_try_to_merge(inode, path, nearex);
  1407. /* try to merge extents to the left */
  1408. /* time to correct all indexes above */
  1409. err = ext4_ext_correct_indexes(handle, inode, path);
  1410. if (err)
  1411. goto cleanup;
  1412. err = ext4_ext_dirty(handle, inode, path + depth);
  1413. cleanup:
  1414. if (npath) {
  1415. ext4_ext_drop_refs(npath);
  1416. kfree(npath);
  1417. }
  1418. ext4_ext_tree_changed(inode);
  1419. ext4_ext_invalidate_cache(inode);
  1420. return err;
  1421. }
  1422. int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
  1423. ext4_lblk_t num, ext_prepare_callback func,
  1424. void *cbdata)
  1425. {
  1426. struct ext4_ext_path *path = NULL;
  1427. struct ext4_ext_cache cbex;
  1428. struct ext4_extent *ex;
  1429. ext4_lblk_t next, start = 0, end = 0;
  1430. ext4_lblk_t last = block + num;
  1431. int depth, exists, err = 0;
  1432. BUG_ON(func == NULL);
  1433. BUG_ON(inode == NULL);
  1434. while (block < last && block != EXT_MAX_BLOCK) {
  1435. num = last - block;
  1436. /* find extent for this block */
  1437. path = ext4_ext_find_extent(inode, block, path);
  1438. if (IS_ERR(path)) {
  1439. err = PTR_ERR(path);
  1440. path = NULL;
  1441. break;
  1442. }
  1443. depth = ext_depth(inode);
  1444. BUG_ON(path[depth].p_hdr == NULL);
  1445. ex = path[depth].p_ext;
  1446. next = ext4_ext_next_allocated_block(path);
  1447. exists = 0;
  1448. if (!ex) {
  1449. /* there is no extent yet, so try to allocate
  1450. * all requested space */
  1451. start = block;
  1452. end = block + num;
  1453. } else if (le32_to_cpu(ex->ee_block) > block) {
  1454. /* need to allocate space before found extent */
  1455. start = block;
  1456. end = le32_to_cpu(ex->ee_block);
  1457. if (block + num < end)
  1458. end = block + num;
  1459. } else if (block >= le32_to_cpu(ex->ee_block)
  1460. + ext4_ext_get_actual_len(ex)) {
  1461. /* need to allocate space after found extent */
  1462. start = block;
  1463. end = block + num;
  1464. if (end >= next)
  1465. end = next;
  1466. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1467. /*
  1468. * some part of requested space is covered
  1469. * by found extent
  1470. */
  1471. start = block;
  1472. end = le32_to_cpu(ex->ee_block)
  1473. + ext4_ext_get_actual_len(ex);
  1474. if (block + num < end)
  1475. end = block + num;
  1476. exists = 1;
  1477. } else {
  1478. BUG();
  1479. }
  1480. BUG_ON(end <= start);
  1481. if (!exists) {
  1482. cbex.ec_block = start;
  1483. cbex.ec_len = end - start;
  1484. cbex.ec_start = 0;
  1485. cbex.ec_type = EXT4_EXT_CACHE_GAP;
  1486. } else {
  1487. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1488. cbex.ec_len = ext4_ext_get_actual_len(ex);
  1489. cbex.ec_start = ext_pblock(ex);
  1490. cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
  1491. }
  1492. BUG_ON(cbex.ec_len == 0);
  1493. err = func(inode, path, &cbex, ex, cbdata);
  1494. ext4_ext_drop_refs(path);
  1495. if (err < 0)
  1496. break;
  1497. if (err == EXT_REPEAT)
  1498. continue;
  1499. else if (err == EXT_BREAK) {
  1500. err = 0;
  1501. break;
  1502. }
  1503. if (ext_depth(inode) != depth) {
  1504. /* depth was changed. we have to realloc path */
  1505. kfree(path);
  1506. path = NULL;
  1507. }
  1508. block = cbex.ec_block + cbex.ec_len;
  1509. }
  1510. if (path) {
  1511. ext4_ext_drop_refs(path);
  1512. kfree(path);
  1513. }
  1514. return err;
  1515. }
  1516. static void
  1517. ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
  1518. __u32 len, ext4_fsblk_t start, int type)
  1519. {
  1520. struct ext4_ext_cache *cex;
  1521. BUG_ON(len == 0);
  1522. cex = &EXT4_I(inode)->i_cached_extent;
  1523. cex->ec_type = type;
  1524. cex->ec_block = block;
  1525. cex->ec_len = len;
  1526. cex->ec_start = start;
  1527. }
  1528. /*
  1529. * ext4_ext_put_gap_in_cache:
  1530. * calculate boundaries of the gap that the requested block fits into
  1531. * and cache this gap
  1532. */
  1533. static void
  1534. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1535. ext4_lblk_t block)
  1536. {
  1537. int depth = ext_depth(inode);
  1538. unsigned long len;
  1539. ext4_lblk_t lblock;
  1540. struct ext4_extent *ex;
  1541. ex = path[depth].p_ext;
  1542. if (ex == NULL) {
  1543. /* there is no extent yet, so gap is [0;-] */
  1544. lblock = 0;
  1545. len = EXT_MAX_BLOCK;
  1546. ext_debug("cache gap(whole file):");
  1547. } else if (block < le32_to_cpu(ex->ee_block)) {
  1548. lblock = block;
  1549. len = le32_to_cpu(ex->ee_block) - block;
  1550. ext_debug("cache gap(before): %u [%u:%u]",
  1551. block,
  1552. le32_to_cpu(ex->ee_block),
  1553. ext4_ext_get_actual_len(ex));
  1554. } else if (block >= le32_to_cpu(ex->ee_block)
  1555. + ext4_ext_get_actual_len(ex)) {
  1556. ext4_lblk_t next;
  1557. lblock = le32_to_cpu(ex->ee_block)
  1558. + ext4_ext_get_actual_len(ex);
  1559. next = ext4_ext_next_allocated_block(path);
  1560. ext_debug("cache gap(after): [%u:%u] %u",
  1561. le32_to_cpu(ex->ee_block),
  1562. ext4_ext_get_actual_len(ex),
  1563. block);
  1564. BUG_ON(next == lblock);
  1565. len = next - lblock;
  1566. } else {
  1567. lblock = len = 0;
  1568. BUG();
  1569. }
  1570. ext_debug(" -> %u:%lu\n", lblock, len);
  1571. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1572. }
  1573. static int
  1574. ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
  1575. struct ext4_extent *ex)
  1576. {
  1577. struct ext4_ext_cache *cex;
  1578. cex = &EXT4_I(inode)->i_cached_extent;
  1579. /* has cache valid data? */
  1580. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1581. return EXT4_EXT_CACHE_NO;
  1582. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1583. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1584. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1585. ex->ee_block = cpu_to_le32(cex->ec_block);
  1586. ext4_ext_store_pblock(ex, cex->ec_start);
  1587. ex->ee_len = cpu_to_le16(cex->ec_len);
  1588. ext_debug("%u cached by %u:%u:%llu\n",
  1589. block,
  1590. cex->ec_block, cex->ec_len, cex->ec_start);
  1591. return cex->ec_type;
  1592. }
  1593. /* not in cache */
  1594. return EXT4_EXT_CACHE_NO;
  1595. }
  1596. /*
  1597. * ext4_ext_rm_idx:
  1598. * removes index from the index block.
  1599. * It's used in truncate case only, thus all requests are for
  1600. * last index in the block only.
  1601. */
  1602. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1603. struct ext4_ext_path *path)
  1604. {
  1605. struct buffer_head *bh;
  1606. int err;
  1607. ext4_fsblk_t leaf;
  1608. /* free index block */
  1609. path--;
  1610. leaf = idx_pblock(path->p_idx);
  1611. BUG_ON(path->p_hdr->eh_entries == 0);
  1612. err = ext4_ext_get_access(handle, inode, path);
  1613. if (err)
  1614. return err;
  1615. le16_add_cpu(&path->p_hdr->eh_entries, -1);
  1616. err = ext4_ext_dirty(handle, inode, path);
  1617. if (err)
  1618. return err;
  1619. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1620. bh = sb_find_get_block(inode->i_sb, leaf);
  1621. ext4_forget(handle, 1, inode, bh, leaf);
  1622. ext4_free_blocks(handle, inode, leaf, 1, 1);
  1623. return err;
  1624. }
  1625. /*
  1626. * ext4_ext_calc_credits_for_single_extent:
  1627. * This routine returns max. credits that needed to insert an extent
  1628. * to the extent tree.
  1629. * When pass the actual path, the caller should calculate credits
  1630. * under i_data_sem.
  1631. */
  1632. int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
  1633. struct ext4_ext_path *path)
  1634. {
  1635. if (path) {
  1636. int depth = ext_depth(inode);
  1637. int ret = 0;
  1638. /* probably there is space in leaf? */
  1639. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1640. < le16_to_cpu(path[depth].p_hdr->eh_max)) {
  1641. /*
  1642. * There are some space in the leaf tree, no
  1643. * need to account for leaf block credit
  1644. *
  1645. * bitmaps and block group descriptor blocks
  1646. * and other metadat blocks still need to be
  1647. * accounted.
  1648. */
  1649. /* 1 bitmap, 1 block group descriptor */
  1650. ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
  1651. }
  1652. }
  1653. return ext4_chunk_trans_blocks(inode, nrblocks);
  1654. }
  1655. /*
  1656. * How many index/leaf blocks need to change/allocate to modify nrblocks?
  1657. *
  1658. * if nrblocks are fit in a single extent (chunk flag is 1), then
  1659. * in the worse case, each tree level index/leaf need to be changed
  1660. * if the tree split due to insert a new extent, then the old tree
  1661. * index/leaf need to be updated too
  1662. *
  1663. * If the nrblocks are discontiguous, they could cause
  1664. * the whole tree split more than once, but this is really rare.
  1665. */
  1666. int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  1667. {
  1668. int index;
  1669. int depth = ext_depth(inode);
  1670. if (chunk)
  1671. index = depth * 2;
  1672. else
  1673. index = depth * 3;
  1674. return index;
  1675. }
  1676. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1677. struct ext4_extent *ex,
  1678. ext4_lblk_t from, ext4_lblk_t to)
  1679. {
  1680. struct buffer_head *bh;
  1681. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  1682. int i, metadata = 0;
  1683. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1684. metadata = 1;
  1685. #ifdef EXTENTS_STATS
  1686. {
  1687. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1688. spin_lock(&sbi->s_ext_stats_lock);
  1689. sbi->s_ext_blocks += ee_len;
  1690. sbi->s_ext_extents++;
  1691. if (ee_len < sbi->s_ext_min)
  1692. sbi->s_ext_min = ee_len;
  1693. if (ee_len > sbi->s_ext_max)
  1694. sbi->s_ext_max = ee_len;
  1695. if (ext_depth(inode) > sbi->s_depth_max)
  1696. sbi->s_depth_max = ext_depth(inode);
  1697. spin_unlock(&sbi->s_ext_stats_lock);
  1698. }
  1699. #endif
  1700. if (from >= le32_to_cpu(ex->ee_block)
  1701. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1702. /* tail removal */
  1703. ext4_lblk_t num;
  1704. ext4_fsblk_t start;
  1705. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  1706. start = ext_pblock(ex) + ee_len - num;
  1707. ext_debug("free last %u blocks starting %llu\n", num, start);
  1708. for (i = 0; i < num; i++) {
  1709. bh = sb_find_get_block(inode->i_sb, start + i);
  1710. ext4_forget(handle, 0, inode, bh, start + i);
  1711. }
  1712. ext4_free_blocks(handle, inode, start, num, metadata);
  1713. } else if (from == le32_to_cpu(ex->ee_block)
  1714. && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1715. printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
  1716. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1717. } else {
  1718. printk(KERN_INFO "strange request: removal(2) "
  1719. "%u-%u from %u:%u\n",
  1720. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1721. }
  1722. return 0;
  1723. }
  1724. static int
  1725. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1726. struct ext4_ext_path *path, ext4_lblk_t start)
  1727. {
  1728. int err = 0, correct_index = 0;
  1729. int depth = ext_depth(inode), credits;
  1730. struct ext4_extent_header *eh;
  1731. ext4_lblk_t a, b, block;
  1732. unsigned num;
  1733. ext4_lblk_t ex_ee_block;
  1734. unsigned short ex_ee_len;
  1735. unsigned uninitialized = 0;
  1736. struct ext4_extent *ex;
  1737. /* the header must be checked already in ext4_ext_remove_space() */
  1738. ext_debug("truncate since %u in leaf\n", start);
  1739. if (!path[depth].p_hdr)
  1740. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1741. eh = path[depth].p_hdr;
  1742. BUG_ON(eh == NULL);
  1743. /* find where to start removing */
  1744. ex = EXT_LAST_EXTENT(eh);
  1745. ex_ee_block = le32_to_cpu(ex->ee_block);
  1746. if (ext4_ext_is_uninitialized(ex))
  1747. uninitialized = 1;
  1748. ex_ee_len = ext4_ext_get_actual_len(ex);
  1749. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1750. ex_ee_block + ex_ee_len > start) {
  1751. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1752. path[depth].p_ext = ex;
  1753. a = ex_ee_block > start ? ex_ee_block : start;
  1754. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1755. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1756. ext_debug(" border %u:%u\n", a, b);
  1757. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1758. block = 0;
  1759. num = 0;
  1760. BUG();
  1761. } else if (a != ex_ee_block) {
  1762. /* remove tail of the extent */
  1763. block = ex_ee_block;
  1764. num = a - block;
  1765. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1766. /* remove head of the extent */
  1767. block = a;
  1768. num = b - a;
  1769. /* there is no "make a hole" API yet */
  1770. BUG();
  1771. } else {
  1772. /* remove whole extent: excellent! */
  1773. block = ex_ee_block;
  1774. num = 0;
  1775. BUG_ON(a != ex_ee_block);
  1776. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1777. }
  1778. /*
  1779. * 3 for leaf, sb, and inode plus 2 (bmap and group
  1780. * descriptor) for each block group; assume two block
  1781. * groups plus ex_ee_len/blocks_per_block_group for
  1782. * the worst case
  1783. */
  1784. credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
  1785. if (ex == EXT_FIRST_EXTENT(eh)) {
  1786. correct_index = 1;
  1787. credits += (ext_depth(inode)) + 1;
  1788. }
  1789. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1790. err = ext4_ext_journal_restart(handle, credits);
  1791. if (err)
  1792. goto out;
  1793. err = ext4_ext_get_access(handle, inode, path + depth);
  1794. if (err)
  1795. goto out;
  1796. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1797. if (err)
  1798. goto out;
  1799. if (num == 0) {
  1800. /* this extent is removed; mark slot entirely unused */
  1801. ext4_ext_store_pblock(ex, 0);
  1802. le16_add_cpu(&eh->eh_entries, -1);
  1803. }
  1804. ex->ee_block = cpu_to_le32(block);
  1805. ex->ee_len = cpu_to_le16(num);
  1806. /*
  1807. * Do not mark uninitialized if all the blocks in the
  1808. * extent have been removed.
  1809. */
  1810. if (uninitialized && num)
  1811. ext4_ext_mark_uninitialized(ex);
  1812. err = ext4_ext_dirty(handle, inode, path + depth);
  1813. if (err)
  1814. goto out;
  1815. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1816. ext_pblock(ex));
  1817. ex--;
  1818. ex_ee_block = le32_to_cpu(ex->ee_block);
  1819. ex_ee_len = ext4_ext_get_actual_len(ex);
  1820. }
  1821. if (correct_index && eh->eh_entries)
  1822. err = ext4_ext_correct_indexes(handle, inode, path);
  1823. /* if this leaf is free, then we should
  1824. * remove it from index block above */
  1825. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1826. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1827. out:
  1828. return err;
  1829. }
  1830. /*
  1831. * ext4_ext_more_to_rm:
  1832. * returns 1 if current index has to be freed (even partial)
  1833. */
  1834. static int
  1835. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1836. {
  1837. BUG_ON(path->p_idx == NULL);
  1838. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1839. return 0;
  1840. /*
  1841. * if truncate on deeper level happened, it wasn't partial,
  1842. * so we have to consider current index for truncation
  1843. */
  1844. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1845. return 0;
  1846. return 1;
  1847. }
  1848. static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
  1849. {
  1850. struct super_block *sb = inode->i_sb;
  1851. int depth = ext_depth(inode);
  1852. struct ext4_ext_path *path;
  1853. handle_t *handle;
  1854. int i = 0, err = 0;
  1855. ext_debug("truncate since %u\n", start);
  1856. /* probably first extent we're gonna free will be last in block */
  1857. handle = ext4_journal_start(inode, depth + 1);
  1858. if (IS_ERR(handle))
  1859. return PTR_ERR(handle);
  1860. ext4_ext_invalidate_cache(inode);
  1861. /*
  1862. * We start scanning from right side, freeing all the blocks
  1863. * after i_size and walking into the tree depth-wise.
  1864. */
  1865. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
  1866. if (path == NULL) {
  1867. ext4_journal_stop(handle);
  1868. return -ENOMEM;
  1869. }
  1870. path[0].p_hdr = ext_inode_hdr(inode);
  1871. if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
  1872. err = -EIO;
  1873. goto out;
  1874. }
  1875. path[0].p_depth = depth;
  1876. while (i >= 0 && err == 0) {
  1877. if (i == depth) {
  1878. /* this is leaf block */
  1879. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1880. /* root level has p_bh == NULL, brelse() eats this */
  1881. brelse(path[i].p_bh);
  1882. path[i].p_bh = NULL;
  1883. i--;
  1884. continue;
  1885. }
  1886. /* this is index block */
  1887. if (!path[i].p_hdr) {
  1888. ext_debug("initialize header\n");
  1889. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1890. }
  1891. if (!path[i].p_idx) {
  1892. /* this level hasn't been touched yet */
  1893. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1894. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1895. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1896. path[i].p_hdr,
  1897. le16_to_cpu(path[i].p_hdr->eh_entries));
  1898. } else {
  1899. /* we were already here, see at next index */
  1900. path[i].p_idx--;
  1901. }
  1902. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1903. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1904. path[i].p_idx);
  1905. if (ext4_ext_more_to_rm(path + i)) {
  1906. struct buffer_head *bh;
  1907. /* go to the next level */
  1908. ext_debug("move to level %d (block %llu)\n",
  1909. i + 1, idx_pblock(path[i].p_idx));
  1910. memset(path + i + 1, 0, sizeof(*path));
  1911. bh = sb_bread(sb, idx_pblock(path[i].p_idx));
  1912. if (!bh) {
  1913. /* should we reset i_size? */
  1914. err = -EIO;
  1915. break;
  1916. }
  1917. if (WARN_ON(i + 1 > depth)) {
  1918. err = -EIO;
  1919. break;
  1920. }
  1921. if (ext4_ext_check_header(inode, ext_block_hdr(bh),
  1922. depth - i - 1)) {
  1923. err = -EIO;
  1924. break;
  1925. }
  1926. path[i + 1].p_bh = bh;
  1927. /* save actual number of indexes since this
  1928. * number is changed at the next iteration */
  1929. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1930. i++;
  1931. } else {
  1932. /* we finished processing this index, go up */
  1933. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1934. /* index is empty, remove it;
  1935. * handle must be already prepared by the
  1936. * truncatei_leaf() */
  1937. err = ext4_ext_rm_idx(handle, inode, path + i);
  1938. }
  1939. /* root level has p_bh == NULL, brelse() eats this */
  1940. brelse(path[i].p_bh);
  1941. path[i].p_bh = NULL;
  1942. i--;
  1943. ext_debug("return to level %d\n", i);
  1944. }
  1945. }
  1946. /* TODO: flexible tree reduction should be here */
  1947. if (path->p_hdr->eh_entries == 0) {
  1948. /*
  1949. * truncate to zero freed all the tree,
  1950. * so we need to correct eh_depth
  1951. */
  1952. err = ext4_ext_get_access(handle, inode, path);
  1953. if (err == 0) {
  1954. ext_inode_hdr(inode)->eh_depth = 0;
  1955. ext_inode_hdr(inode)->eh_max =
  1956. cpu_to_le16(ext4_ext_space_root(inode));
  1957. err = ext4_ext_dirty(handle, inode, path);
  1958. }
  1959. }
  1960. out:
  1961. ext4_ext_tree_changed(inode);
  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 (test_opt(sb, 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 (!test_opt(sb, 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 long 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 successfull 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 long 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 goal, newblock;
  2382. int err = 0, depth, ret;
  2383. unsigned long 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/%lu requested for inode %u\n",
  2388. iblock, max_blocks, inode->i_ino);
  2389. /* check in cache */
  2390. goal = ext4_ext_in_cache(inode, iblock, &newex);
  2391. if (goal) {
  2392. if (goal == 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 (goal == 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. 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. handle->h_sync = 1;
  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 long 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=%lu", __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. 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. int ext4_xattr_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo)
  2798. {
  2799. __u64 physical = 0;
  2800. __u64 length;
  2801. __u32 flags = FIEMAP_EXTENT_LAST;
  2802. int blockbits = inode->i_sb->s_blocksize_bits;
  2803. int error = 0;
  2804. /* in-inode? */
  2805. if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
  2806. struct ext4_iloc iloc;
  2807. int offset; /* offset of xattr in inode */
  2808. error = ext4_get_inode_loc(inode, &iloc);
  2809. if (error)
  2810. return error;
  2811. physical = iloc.bh->b_blocknr << blockbits;
  2812. offset = EXT4_GOOD_OLD_INODE_SIZE +
  2813. EXT4_I(inode)->i_extra_isize;
  2814. physical += offset;
  2815. length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
  2816. flags |= FIEMAP_EXTENT_DATA_INLINE;
  2817. } else { /* external block */
  2818. physical = EXT4_I(inode)->i_file_acl << blockbits;
  2819. length = inode->i_sb->s_blocksize;
  2820. }
  2821. if (physical)
  2822. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  2823. length, flags);
  2824. return (error < 0 ? error : 0);
  2825. }
  2826. int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  2827. __u64 start, __u64 len)
  2828. {
  2829. ext4_lblk_t start_blk;
  2830. ext4_lblk_t len_blks;
  2831. int error = 0;
  2832. /* fallback to generic here if not in extents fmt */
  2833. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  2834. return generic_block_fiemap(inode, fieinfo, start, len,
  2835. ext4_get_block);
  2836. if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
  2837. return -EBADR;
  2838. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  2839. error = ext4_xattr_fiemap(inode, fieinfo);
  2840. } else {
  2841. start_blk = start >> inode->i_sb->s_blocksize_bits;
  2842. len_blks = len >> inode->i_sb->s_blocksize_bits;
  2843. /*
  2844. * Walk the extent tree gathering extent information.
  2845. * ext4_ext_fiemap_cb will push extents back to user.
  2846. */
  2847. down_write(&EXT4_I(inode)->i_data_sem);
  2848. error = ext4_ext_walk_space(inode, start_blk, len_blks,
  2849. ext4_ext_fiemap_cb, fieinfo);
  2850. up_write(&EXT4_I(inode)->i_data_sem);
  2851. }
  2852. return error;
  2853. }