extents.c 85 KB

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